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