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