xref: /linux/scripts/coccinelle/api/kmalloc_objs.cocci (revision bcd347f31e84ecfb1e45be488b1c7c835b8feb4c)
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
7virtual patch
8
9@initialize:python@
10@@
11import sys
12
13def alloc_array(name):
14	func = "FAILED_RENAME"
15	if name == "kmalloc_array":
16		func = "kmalloc_objs"
17	elif name == "kvmalloc_array":
18		func = "kvmalloc_objs"
19	elif name == "kcalloc":
20		func = "kzalloc_objs"
21	elif name == "kvcalloc":
22		func = "kvzalloc_objs"
23	elif name == "devm_kmalloc_array":
24		func = "devm_kmalloc_objs"
25	elif name == "devm_kcalloc":
26		func = "devm_kzalloc_objs"
27	else:
28		print(f"Unknown transform for {name}", file=sys.stderr)
29	return func
30
31// Allocations sized by a byte-sized type (BYTE_TYPES) or a string literal,
32// and allocations assigned to a pointer to a byte-sized type, are byte
33// buffers and are left alone. sizeof(void *) is also excluded because it
34// will need case-by-case double-checking to make sure the right type is
35// being assigned.
36//
37// Allocations sized by a multi-byte integral type (MULTIBYTE_TYPES) are
38// converted when they are assigned to a pointer to that same type.
39// Otherwise they are left alone: the target may be a pointer to an array
40// of that type, such as "s16 (*pairs)[2]", for which the converted
41// allocation would have the wrong pointer type. For the same reason,
42// arrays of pointers to integral types sized as sizeof(char *) and the
43// like are left alone. Allocations of other types assigned to a pointer to
44// a multi-byte integral type are left alone too.
45//
46// Everything else gets the sizeof() extracted for the kmalloc_obj()
47// type/var argument.
48//
49// The device-managed allocators differ from the plain ones only by taking
50// the managing "struct device *" as a leading argument, so rather than
51// repeating every pattern for them, the leading arguments are captured in
52// an "expression list" metavariable and replayed on the replacement side.
53// ARGS is empty for the plain allocators, in which case Coccinelle drops
54// the separating comma as well. Note that there is no devm_kvmalloc
55// family, and that the devm array helpers only exist as
56// (dev, count, size, gfp).
57//
58// The first matching alternative below wins, so the exclusions must come
59// before the more general conversions.
60@direct depends on patch && !(file in "tools") && !(file in "samples")@
61typedef u8, u16, u32, u64;
62typedef __u8, __u16, __u32, __u64;
63typedef uint8_t, uint16_t, uint32_t, uint64_t;
64typedef s8, s16, s32, s64;
65typedef __s8, __s16, __s32, __s64;
66typedef int8_t, int16_t, int32_t, int64_t;
67typedef uchar, ushort, uint, ulong;
68typedef __le16, __le32, __le64;
69typedef __be16, __be32, __be64;
70typedef wchar_t;
71type BYTE_TYPES = {char,signed char,unsigned char,uchar,
72		   u8,__u8,uint8_t,s8,__s8,int8_t};
73type MULTIBYTE_TYPES = {short,short int,signed short,signed short int,
74			unsigned short,unsigned short int,ushort,
75			int,signed,signed int,unsigned,unsigned int,uint,
76			long,long int,signed long,signed long int,
77			unsigned long,unsigned long int,ulong,
78			long long,long long int,
79			signed long long,signed long long int,
80			unsigned long long,unsigned long long int,
81			u16,__u16,uint16_t,s16,__s16,int16_t,
82			u32,__u32,uint32_t,s32,__s32,int32_t,
83			u64,__u64,uint64_t,s64,__s64,int64_t,
84			__le16,__le32,__le64,__be16,__be32,__be64,
85			wchar_t};
86char [] STRING;
87BYTE_TYPES *BYTES;
88MULTIBYTE_TYPES *MULTIBYTES;
89const MULTIBYTE_TYPES *CONST_MULTIBYTES;
90MULTIBYTE_TYPES MULTIBYTE;
91type TYPE;
92expression VAR;
93expression GFP;
94expression COUNT;
95expression FLEX;
96expression E;
97expression list ARGS;
98identifier ALLOC = {kmalloc,kzalloc,kvmalloc,kvzalloc,
99		    devm_kmalloc,devm_kzalloc};
100fresh identifier ALLOC_OBJ = ALLOC ## "_obj";
101fresh identifier ALLOC_FLEX = ALLOC ## "_flex";
102identifier ALLOC_ARRAY = {kmalloc_array,kvmalloc_array,kcalloc,kvcalloc,
103			  devm_kmalloc_array,devm_kcalloc};
104fresh identifier ALLOC_OBJS = script:python(ALLOC_ARRAY) { alloc_array(ALLOC_ARRAY) };
105@@
106
107(
108// Convert a single object sized by its target: p = kmalloc(sizeof(*p), gfp)
109-	VAR = ALLOC(ARGS, (sizeof(*VAR)), GFP)
110+	VAR = ALLOC_OBJ(ARGS, *VAR, GFP)
111|
112// Exclude byte buffers and integral pointers: kmalloc(sizeof(u8), gfp),
113//   kmalloc(sizeof("str"), gfp), kmalloc(sizeof(char *), gfp)
114	ALLOC(ARGS, (\(sizeof(STRING)\|sizeof(BYTE_TYPES)\|
115		sizeof(BYTE_TYPES *)\|sizeof(MULTIBYTE_TYPES *)\)), GFP)
116|
117// Exclude anything assigned to a byte pointer:
118//   u8 *buf = kmalloc(sizeof(*hdr), gfp)
119	BYTES = ALLOC(ARGS, (\(sizeof(E)\|sizeof(TYPE)\)), GFP)
120|
121// Convert a multi-byte type to a pointer to it:
122//   u32 *p = kmalloc(sizeof(u32), gfp)
123	\(MULTIBYTES\|CONST_MULTIBYTES\) =
124-		ALLOC(ARGS, (sizeof(MULTIBYTE_TYPES)), GFP)
125+		ALLOC_OBJ(ARGS, MULTIBYTE_TYPES, GFP)
126|
127// Same by expression: u32 *p = kmalloc(sizeof(p[0]), gfp)
128	\(MULTIBYTES\|CONST_MULTIBYTES\) =
129-		ALLOC(ARGS, (sizeof(MULTIBYTE)), GFP)
130+		ALLOC_OBJ(ARGS, MULTIBYTE, GFP)
131|
132// Exclude other multi-byte sizes, e.g. to pointers to arrays: s16 (*p)[2] = ...
133	ALLOC(ARGS, (\(sizeof(MULTIBYTE_TYPES)\|sizeof(MULTIBYTE)\)), GFP)
134|
135// Exclude anything else assigned to a multi-byte pointer:
136//   u32 *p = kmalloc(sizeof(*hdr), gfp)
137	\(MULTIBYTES\|CONST_MULTIBYTES\) = ALLOC(ARGS, (\(sizeof(E)\|sizeof(TYPE)\)), GFP)
138|
139// Exclude void pointers, to be checked by hand: kmalloc(sizeof(void *), gfp)
140	ALLOC(ARGS, (sizeof(void *)), GFP)
141|
142// Convert any other expression: p = kmalloc(sizeof(s->item), gfp)
143-	ALLOC(ARGS, (sizeof(E)), GFP)
144+	ALLOC_OBJ(ARGS, E, GFP)
145|
146// Convert any other type: p = kmalloc(sizeof(struct item), gfp)
147-	ALLOC(ARGS, (sizeof(TYPE)), GFP)
148+	ALLOC_OBJ(ARGS, TYPE, GFP)
149|
150// The same, for arrays allocated as (count, size):
151// Exclude byte buffers and integral pointers: kcalloc(n, sizeof(u8), gfp),
152//   kcalloc(n, sizeof(char *), gfp)
153	ALLOC_ARRAY(ARGS, COUNT, (\(sizeof(STRING)\|sizeof(BYTE_TYPES)\|
154			    sizeof(BYTE_TYPES *)\|sizeof(MULTIBYTE_TYPES *)\)), GFP)
155|
156// Exclude arrays assigned to a byte pointer:
157//   u8 *buf = kcalloc(n, sizeof(*hdr), gfp)
158	BYTES = ALLOC_ARRAY(ARGS, COUNT, (\(sizeof(E)\|sizeof(TYPE)\)), GFP)
159|
160// Convert a multi-byte array to a pointer to it:
161//   u32 *p = kcalloc(n, sizeof(u32), gfp)
162	\(MULTIBYTES\|CONST_MULTIBYTES\) =
163-		ALLOC_ARRAY(ARGS, COUNT, (sizeof(MULTIBYTE_TYPES)), GFP)
164+		ALLOC_OBJS(ARGS, MULTIBYTE_TYPES, COUNT, GFP)
165|
166// Same by expression: u32 *p = kcalloc(n, sizeof(*p), gfp)
167	\(MULTIBYTES\|CONST_MULTIBYTES\) =
168-		ALLOC_ARRAY(ARGS, COUNT, (sizeof(MULTIBYTE)), GFP)
169+		ALLOC_OBJS(ARGS, MULTIBYTE, COUNT, GFP)
170|
171// Exclude other multi-byte arrays:
172//   s16 (*pairs)[2] = kcalloc(n, sizeof(s16), gfp)
173	ALLOC_ARRAY(ARGS, COUNT, (\(sizeof(MULTIBYTE_TYPES)\|sizeof(MULTIBYTE)\)), GFP)
174|
175// Exclude other arrays assigned to a multi-byte pointer:
176//   u32 *p = kcalloc(n, sizeof(*hdr), gfp)
177	\(MULTIBYTES\|CONST_MULTIBYTES\) = ALLOC_ARRAY(ARGS, COUNT, (\(sizeof(E)\|sizeof(TYPE)\)), GFP)
178|
179// Exclude arrays of void pointers: kcalloc(n, sizeof(void *), gfp)
180	ALLOC_ARRAY(ARGS, COUNT, (sizeof(void *)), GFP)
181|
182// Convert any other expression: p = kcalloc(n, sizeof(*p), gfp)
183-	ALLOC_ARRAY(ARGS, COUNT, (sizeof(E)), GFP)
184+	ALLOC_OBJS(ARGS, E, COUNT, GFP)
185|
186// Convert any other type: p = kcalloc(n, sizeof(struct item), gfp)
187-	ALLOC_ARRAY(ARGS, COUNT, (sizeof(TYPE)), GFP)
188+	ALLOC_OBJS(ARGS, TYPE, COUNT, GFP)
189|
190// The same, for arrays allocated as (size, count):
191// Exclude byte buffers and integral pointers: kcalloc(sizeof(u8), n, gfp),
192//   kcalloc(sizeof(char *), n, gfp)
193	ALLOC_ARRAY(ARGS, (\(sizeof(STRING)\|sizeof(BYTE_TYPES)\|
194		      sizeof(BYTE_TYPES *)\|sizeof(MULTIBYTE_TYPES *)\)), COUNT, GFP)
195|
196// Exclude arrays assigned to a byte pointer:
197//   u8 *buf = kcalloc(sizeof(*hdr), n, gfp)
198	BYTES = ALLOC_ARRAY(ARGS, (\(sizeof(E)\|sizeof(TYPE)\)), COUNT, GFP)
199|
200// Convert a multi-byte array to a pointer to it:
201//   u32 *p = kcalloc(sizeof(u32), n, gfp)
202	\(MULTIBYTES\|CONST_MULTIBYTES\) =
203-		ALLOC_ARRAY(ARGS, (sizeof(MULTIBYTE_TYPES)), COUNT, GFP)
204+		ALLOC_OBJS(ARGS, MULTIBYTE_TYPES, COUNT, GFP)
205|
206// Same by expression: u32 *p = kcalloc(sizeof(*p), n, gfp)
207	\(MULTIBYTES\|CONST_MULTIBYTES\) =
208-		ALLOC_ARRAY(ARGS, (sizeof(MULTIBYTE)), COUNT, GFP)
209+		ALLOC_OBJS(ARGS, MULTIBYTE, COUNT, GFP)
210|
211// Exclude other multi-byte arrays:
212//   s16 (*pairs)[2] = kcalloc(sizeof(s16), n, gfp)
213	ALLOC_ARRAY(ARGS, (\(sizeof(MULTIBYTE_TYPES)\|sizeof(MULTIBYTE)\)), COUNT, GFP)
214|
215// Exclude other arrays assigned to a multi-byte pointer:
216//   u32 *p = kcalloc(sizeof(*hdr), n, gfp)
217	\(MULTIBYTES\|CONST_MULTIBYTES\) = ALLOC_ARRAY(ARGS, (\(sizeof(E)\|sizeof(TYPE)\)), COUNT, GFP)
218|
219// Exclude arrays of void pointers: kcalloc(sizeof(void *), n, gfp)
220	ALLOC_ARRAY(ARGS, (sizeof(void *)), COUNT, GFP)
221|
222// Convert any other expression: p = kcalloc(sizeof(*p), n, gfp)
223-	ALLOC_ARRAY(ARGS, (sizeof(E)), COUNT, GFP)
224+	ALLOC_OBJS(ARGS, E, COUNT, GFP)
225|
226// Convert any other type: p = kcalloc(sizeof(struct item), n, gfp)
227-	ALLOC_ARRAY(ARGS, (sizeof(TYPE)), COUNT, GFP)
228+	ALLOC_OBJS(ARGS, TYPE, COUNT, GFP)
229|
230// Convert flexible array structures: p = kmalloc(struct_size(p, data, n), gfp)
231-	ALLOC(ARGS, struct_size(VAR, FLEX, COUNT), GFP)
232+	ALLOC_FLEX(ARGS, *VAR, FLEX, COUNT, GFP)
233|
234// Same by type: kmalloc(struct_size_t(struct item, data, n), gfp)
235-	ALLOC(ARGS, struct_size_t(TYPE, FLEX, COUNT), GFP)
236+	ALLOC_FLEX(ARGS, TYPE, FLEX, COUNT, GFP)
237)
238
239@drop_gfp_kernel depends on patch && !(file in "tools") && !(file in "samples")@
240identifier ALLOC = {kmalloc_obj,kmalloc_objs,kmalloc_flex,
241		    kzalloc_obj,kzalloc_objs,kzalloc_flex,
242		    kvmalloc_obj,kvmalloc_objs,kvmalloc_flex,
243		    kvzalloc_obj,kvzalloc_objs,kvzalloc_flex,
244		    devm_kmalloc_obj,devm_kmalloc_objs,devm_kmalloc_flex,
245		    devm_kzalloc_obj,devm_kzalloc_objs,devm_kzalloc_flex};
246@@
247
248	ALLOC(...
249-		 , GFP_KERNEL
250	     )
251