kmalloc_objs.cocci 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /// Use kmalloc_obj family of macros for allocations
  3. ///
  4. // Confidence: High
  5. // Options: --include-headers-for-types --all-includes --include-headers --keep-comments
  6. virtual patch
  7. @initialize:python@
  8. @@
  9. import sys
  10. def alloc_array(name):
  11. func = "FAILED_RENAME"
  12. if name == "kmalloc_array":
  13. func = "kmalloc_objs"
  14. elif name == "kvmalloc_array":
  15. func = "kvmalloc_objs"
  16. elif name == "kcalloc":
  17. func = "kzalloc_objs"
  18. elif name == "kvcalloc":
  19. func = "kvzalloc_objs"
  20. else:
  21. print(f"Unknown transform for {name}", file=sys.stderr)
  22. return func
  23. // This excludes anything that is assigning to or from integral types or
  24. // string literals. Everything else gets the sizeof() extracted for the
  25. // kmalloc_obj() type/var argument. sizeof(void *) is also excluded because
  26. // it will need case-by-case double-checking to make sure the right type is
  27. // being assigned.
  28. @direct depends on patch && !(file in "tools") && !(file in "samples")@
  29. typedef u8, u16, u32, u64;
  30. typedef __u8, __u16, __u32, __u64;
  31. typedef uint8_t, uint16_t, uint32_t, uint64_t;
  32. typedef uchar, ushort, uint, ulong;
  33. typedef __le16, __le32, __le64;
  34. typedef __be16, __be32, __be64;
  35. typedef wchar_t;
  36. type INTEGRAL = {u8,__u8,uint8_t,char,unsigned char,uchar,wchar_t,
  37. u16,__u16,uint16_t,unsigned short,ushort,
  38. u32,__u32,uint32_t,unsigned int,uint,
  39. u64,__u64,uint64_t,unsigned long,ulong,
  40. __le16,__le32,__le64,__be16,__be32,__be64};
  41. char [] STRING;
  42. INTEGRAL *BYTES;
  43. INTEGRAL **BYTES_PTRS;
  44. type TYPE;
  45. expression VAR;
  46. expression GFP;
  47. expression COUNT;
  48. expression FLEX;
  49. expression E;
  50. identifier ALLOC =~ "^kv?[mz]alloc$";
  51. fresh identifier ALLOC_OBJ = ALLOC ## "_obj";
  52. fresh identifier ALLOC_FLEX = ALLOC ## "_flex";
  53. identifier ALLOC_ARRAY = {kmalloc_array,kvmalloc_array,kcalloc,kvcalloc};
  54. fresh identifier ALLOC_OBJS = script:python(ALLOC_ARRAY) { alloc_array(ALLOC_ARRAY) };
  55. @@
  56. (
  57. - VAR = ALLOC((sizeof(*VAR)), GFP)
  58. + VAR = ALLOC_OBJ(*VAR, GFP)
  59. |
  60. ALLOC((\(sizeof(STRING)\|sizeof(INTEGRAL)\|sizeof(INTEGRAL *)\)), GFP)
  61. |
  62. BYTES = ALLOC((sizeof(E)), GFP)
  63. |
  64. BYTES = ALLOC((sizeof(TYPE)), GFP)
  65. |
  66. BYTES_PTRS = ALLOC((sizeof(E)), GFP)
  67. |
  68. BYTES_PTRS = ALLOC((sizeof(TYPE)), GFP)
  69. |
  70. ALLOC((sizeof(void *)), GFP)
  71. |
  72. - ALLOC((sizeof(E)), GFP)
  73. + ALLOC_OBJ(E, GFP)
  74. |
  75. - ALLOC((sizeof(TYPE)), GFP)
  76. + ALLOC_OBJ(TYPE, GFP)
  77. |
  78. ALLOC_ARRAY(COUNT, (\(sizeof(STRING)\|sizeof(INTEGRAL)\|sizeof(INTEGRAL *)\)), GFP)
  79. |
  80. BYTES = ALLOC_ARRAY(COUNT, (sizeof(E)), GFP)
  81. |
  82. BYTES = ALLOC_ARRAY(COUNT, (sizeof(TYPE)), GFP)
  83. |
  84. BYTES_PTRS = ALLOC_ARRAY(COUNT, (sizeof(E)), GFP)
  85. |
  86. BYTES_PTRS = ALLOC_ARRAY(COUNT, (sizeof(TYPE)), GFP)
  87. |
  88. ALLOC_ARRAY((\(sizeof(STRING)\|sizeof(INTEGRAL)\|sizeof(INTEGRAL *)\)), COUNT, GFP)
  89. |
  90. BYTES = ALLOC_ARRAY((sizeof(E)), COUNT, GFP)
  91. |
  92. BYTES = ALLOC_ARRAY((sizeof(TYPE)), COUNT, GFP)
  93. |
  94. BYTES_PTRS = ALLOC_ARRAY((sizeof(E)), COUNT, GFP)
  95. |
  96. BYTES_PTRS = ALLOC_ARRAY((sizeof(TYPE)), COUNT, GFP)
  97. |
  98. ALLOC_ARRAY(COUNT, (sizeof(void *)), GFP)
  99. |
  100. ALLOC_ARRAY((sizeof(void *)), COUNT, GFP)
  101. |
  102. - ALLOC_ARRAY(COUNT, (sizeof(E)), GFP)
  103. + ALLOC_OBJS(E, COUNT, GFP)
  104. |
  105. - ALLOC_ARRAY(COUNT, (sizeof(TYPE)), GFP)
  106. + ALLOC_OBJS(TYPE, COUNT, GFP)
  107. |
  108. - ALLOC_ARRAY((sizeof(E)), COUNT, GFP)
  109. + ALLOC_OBJS(E, COUNT, GFP)
  110. |
  111. - ALLOC_ARRAY((sizeof(TYPE)), COUNT, GFP)
  112. + ALLOC_OBJS(TYPE, COUNT, GFP)
  113. |
  114. - ALLOC(struct_size(VAR, FLEX, COUNT), GFP)
  115. + ALLOC_FLEX(*VAR, FLEX, COUNT, GFP)
  116. |
  117. - ALLOC(struct_size_t(TYPE, FLEX, COUNT), GFP)
  118. + ALLOC_FLEX(TYPE, FLEX, COUNT, GFP)
  119. )
  120. @drop_gfp_kernel depends on patch && !(file in "tools") && !(file in "samples")@
  121. identifier ALLOC = {kmalloc_obj,kmalloc_objs,kmalloc_flex,
  122. kzalloc_obj,kzalloc_objs,kzalloc_flex,
  123. kvmalloc_obj,kvmalloc_objs,kvmalloc_flex,
  124. kvzalloc_obj,kvzalloc_objs,kvzalloc_flex};
  125. @@
  126. ALLOC(...
  127. - , GFP_KERNEL
  128. )