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// This excludes anything that is assigning to or from integral types or 28// string literals. Everything else gets the sizeof() extracted for the 29// kmalloc_obj() type/var argument. sizeof(void *) is also excluded because 30// it will need case-by-case double-checking to make sure the right type is 31// being assigned. 32@direct depends on patch && !(file in "tools") && !(file in "samples")@ 33typedef u8, u16, u32, u64; 34typedef __u8, __u16, __u32, __u64; 35typedef uint8_t, uint16_t, uint32_t, uint64_t; 36typedef uchar, ushort, uint, ulong; 37typedef __le16, __le32, __le64; 38typedef __be16, __be32, __be64; 39typedef wchar_t; 40type INTEGRAL = {u8,__u8,uint8_t,char,unsigned char,uchar,wchar_t, 41 u16,__u16,uint16_t,unsigned short,ushort, 42 u32,__u32,uint32_t,unsigned int,uint, 43 u64,__u64,uint64_t,unsigned long,ulong, 44 __le16,__le32,__le64,__be16,__be32,__be64}; 45char [] STRING; 46INTEGRAL *BYTES; 47INTEGRAL **BYTES_PTRS; 48type TYPE; 49expression VAR; 50expression GFP; 51expression COUNT; 52expression FLEX; 53expression E; 54identifier ALLOC =~ "^kv?[mz]alloc$"; 55fresh identifier ALLOC_OBJ = ALLOC ## "_obj"; 56fresh identifier ALLOC_FLEX = ALLOC ## "_flex"; 57identifier ALLOC_ARRAY = {kmalloc_array,kvmalloc_array,kcalloc,kvcalloc}; 58fresh identifier ALLOC_OBJS = script:python(ALLOC_ARRAY) { alloc_array(ALLOC_ARRAY) }; 59@@ 60 61( 62- VAR = ALLOC((sizeof(*VAR)), GFP) 63+ VAR = ALLOC_OBJ(*VAR, GFP) 64| 65 ALLOC((\(sizeof(STRING)\|sizeof(INTEGRAL)\|sizeof(INTEGRAL *)\)), GFP) 66| 67 BYTES = ALLOC((sizeof(E)), GFP) 68| 69 BYTES = ALLOC((sizeof(TYPE)), GFP) 70| 71 BYTES_PTRS = ALLOC((sizeof(E)), GFP) 72| 73 BYTES_PTRS = ALLOC((sizeof(TYPE)), GFP) 74| 75 ALLOC((sizeof(void *)), GFP) 76| 77- ALLOC((sizeof(E)), GFP) 78+ ALLOC_OBJ(E, GFP) 79| 80- ALLOC((sizeof(TYPE)), GFP) 81+ ALLOC_OBJ(TYPE, GFP) 82| 83 ALLOC_ARRAY(COUNT, (\(sizeof(STRING)\|sizeof(INTEGRAL)\|sizeof(INTEGRAL *)\)), GFP) 84| 85 BYTES = ALLOC_ARRAY(COUNT, (sizeof(E)), GFP) 86| 87 BYTES = ALLOC_ARRAY(COUNT, (sizeof(TYPE)), GFP) 88| 89 BYTES_PTRS = ALLOC_ARRAY(COUNT, (sizeof(E)), GFP) 90| 91 BYTES_PTRS = ALLOC_ARRAY(COUNT, (sizeof(TYPE)), GFP) 92| 93 ALLOC_ARRAY((\(sizeof(STRING)\|sizeof(INTEGRAL)\|sizeof(INTEGRAL *)\)), COUNT, GFP) 94| 95 BYTES = ALLOC_ARRAY((sizeof(E)), COUNT, GFP) 96| 97 BYTES = ALLOC_ARRAY((sizeof(TYPE)), COUNT, GFP) 98| 99 BYTES_PTRS = ALLOC_ARRAY((sizeof(E)), COUNT, GFP) 100| 101 BYTES_PTRS = ALLOC_ARRAY((sizeof(TYPE)), COUNT, GFP) 102| 103 ALLOC_ARRAY(COUNT, (sizeof(void *)), GFP) 104| 105 ALLOC_ARRAY((sizeof(void *)), COUNT, GFP) 106| 107- ALLOC_ARRAY(COUNT, (sizeof(E)), GFP) 108+ ALLOC_OBJS(E, COUNT, GFP) 109| 110- ALLOC_ARRAY(COUNT, (sizeof(TYPE)), GFP) 111+ ALLOC_OBJS(TYPE, COUNT, GFP) 112| 113- ALLOC_ARRAY((sizeof(E)), COUNT, GFP) 114+ ALLOC_OBJS(E, COUNT, GFP) 115| 116- ALLOC_ARRAY((sizeof(TYPE)), COUNT, GFP) 117+ ALLOC_OBJS(TYPE, COUNT, GFP) 118| 119- ALLOC(struct_size(VAR, FLEX, COUNT), GFP) 120+ ALLOC_FLEX(*VAR, FLEX, COUNT, GFP) 121| 122- ALLOC(struct_size_t(TYPE, FLEX, COUNT), GFP) 123+ ALLOC_FLEX(TYPE, FLEX, COUNT, GFP) 124) 125