Move the logic related to the memory allocation and freeing into module_memory_alloc() and module_memory_free().
Unified · Split
| @@ -1203,15 +1203,44 @@ static bool mod_mem_use_vmalloc(enum mod_mem_type type) · 1202 unmodified lines | |||
| 1203 | mod_mem_type_is_core_data(type); | 1203 | mod_mem_type_is_core_data(type); |
| 1204 | } | 1204 | } |
| 1205 | 1205 | ||
| 1206 | static void *module_memory_alloc(unsigned int size, enum mod_mem_type type) | 1206 | static int module_memory_alloc(struct module *mod, enum mod_mem_type type) |
| 1207 | { | 1207 | { |
| 1208 | unsigned int size = PAGE_ALIGN(mod->mem[type].size); | ||
| 1209 | void *ptr; | ||
| 1210 | |||
| 1211 | mod->mem[type].size = size; | ||
| 1212 | |||
| 1208 | if (mod_mem_use_vmalloc(type)) | 1213 | if (mod_mem_use_vmalloc(type)) |
| 1209 | return vzalloc(size); | 1214 | ptr = vmalloc(size); |
| 1210 | return module_alloc(size); | 1215 | else |
| 1216 | ptr = module_alloc(size); | ||
| 1217 | |||
| 1218 | if (!ptr) | ||
| 1219 | return -ENOMEM; | ||
| 1220 | |||
| 1221 | /* | ||
| 1222 | * The pointer to these blocks of memory are stored on the module | ||
| 1223 | * structure and we keep that around so long as the module is | ||
| 1224 | * around. We only free that memory when we unload the module. | ||
| 1225 | * Just mark them as not being a leak then. The .init* ELF | ||
| 1226 | * sections *do* get freed after boot so we *could* treat them | ||
| 1227 | * slightly differently with kmemleak_ignore() and only grey | ||
| 1228 | * them out as they work as typical memory allocations which | ||
| 1229 | * *do* eventually get freed, but let's just keep things simple | ||
| 1230 | * and avoid *any* false positives. | ||
| 1231 | */ | ||
| 1232 | kmemleak_not_leak(ptr); | ||
| 1233 | |||
| 1234 | memset(ptr, 0, size); | ||
| 1235 | mod->mem[type].base = ptr; | ||
| 1236 | |||
| 1237 | return 0; | ||
| 1211 | } | 1238 | } |
| 1212 | 1239 | ||
| 1213 | static void module_memory_free(void *ptr, enum mod_mem_type type) | 1240 | static void module_memory_free(struct module *mod, enum mod_mem_type type) |
| 1214 | { | 1241 | { |
| 1242 | void *ptr = mod->mem[type].base; | ||
| 1243 | |||
| 1215 | if (mod_mem_use_vmalloc(type)) | 1244 | if (mod_mem_use_vmalloc(type)) |
| 1216 | vfree(ptr); | 1245 | vfree(ptr); |
| 1217 | else | 1246 | else |
| @@ -1229,12 +1258,12 @@ static void free_mod_mem(struct module *mod) · 11 unmodified lines | |||
| 1229 | /* Free lock-classes; relies on the preceding sync_rcu(). */ | 1258 | /* Free lock-classes; relies on the preceding sync_rcu(). */ |
| 1230 | lockdep_free_key_range(mod_mem->base, mod_mem->size); | 1259 | lockdep_free_key_range(mod_mem->base, mod_mem->size); |
| 1231 | if (mod_mem->size) | 1260 | if (mod_mem->size) |
| 1232 | module_memory_free(mod_mem->base, type); | 1261 | module_memory_free(mod, type); |
| 1233 | } | 1262 | } |
| 1234 | 1263 | ||
| 1235 | /* MOD_DATA hosts mod, so free it at last */ | 1264 | /* MOD_DATA hosts mod, so free it at last */ |
| 1236 | lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size); | 1265 | lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size); |
| 1237 | module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA); | 1266 | module_memory_free(mod, MOD_DATA); |
| 1238 | } | 1267 | } |
| 1239 | 1268 | ||
| 1240 | /* Free a module, remove from lists, etc. */ | 1269 | /* Free a module, remove from lists, etc. */ |
| @@ -2225,7 +2254,6 @@ static int find_module_sections(struct module *mod, struct load_info *info) · 984 unmodified lines | |||
| 2225 | static int move_module(struct module *mod, struct load_info *info) | 2254 | static int move_module(struct module *mod, struct load_info *info) |
| 2226 | { | 2255 | { |
| 2227 | int i; | 2256 | int i; |
| 2228 | void *ptr; | ||
| 2229 | enum mod_mem_type t = 0; | 2257 | enum mod_mem_type t = 0; |
| 2230 | int ret = -ENOMEM; | 2258 | int ret = -ENOMEM; |
| 2231 | 2259 | ||
| @@ -2234,26 +2262,12 @@ static int move_module(struct module *mod, struct load_info *info) · 2 unmodified lines | |||
| 2234 | mod->mem[type].base = NULL; | 2262 | mod->mem[type].base = NULL; |
| 2235 | continue; | 2263 | continue; |
| 2236 | } | 2264 | } |
| 2237 | mod->mem[type].size = PAGE_ALIGN(mod->mem[type].size); | 2265 | |
| 2238 | ptr = module_memory_alloc(mod->mem[type].size, type); | 2266 | ret = module_memory_alloc(mod, type); |
| 2239 | /* | 2267 | if (ret) { |
| 2240 | * The pointer to these blocks of memory are stored on the module | ||
| 2241 | * structure and we keep that around so long as the module is | ||
| 2242 | * around. We only free that memory when we unload the module. | ||
| 2243 | * Just mark them as not being a leak then. The .init* ELF | ||
| 2244 | * sections *do* get freed after boot so we *could* treat them | ||
| 2245 | * slightly differently with kmemleak_ignore() and only grey | ||
| 2246 | * them out as they work as typical memory allocations which | ||
| 2247 | * *do* eventually get freed, but let's just keep things simple | ||
| 2248 | * and avoid *any* false positives. | ||
| 2249 | */ | ||
| 2250 | kmemleak_not_leak(ptr); | ||
| 2251 | if (!ptr) { | ||
| 2252 | t = type; | 2268 | t = type; |
| 2253 | goto out_enomem; | 2269 | goto out_enomem; |
| 2254 | } | 2270 | } |
| 2255 | memset(ptr, 0, mod->mem[type].size); | ||
| 2256 | mod->mem[type].base = ptr; | ||
| 2257 | } | 2271 | } |
| 2258 | 2272 | ||
| 2259 | /* Transfer each section which specifies SHF_ALLOC */ | 2273 | /* Transfer each section which specifies SHF_ALLOC */ |
| @@ -2296,7 +2310,7 @@ static int move_module(struct module *mod, struct load_info *info) · 36 unmodified lines | |||
| 2296 | return 0; | 2310 | return 0; |
| 2297 | out_enomem: | 2311 | out_enomem: |
| 2298 | for (t--; t >= 0; t--) | 2312 | for (t--; t >= 0; t--) |
| 2299 | module_memory_free(mod->mem[t].base, t); | 2313 | module_memory_free(mod, t); |
| 2300 | return ret; | 2314 | return ret; |
| 2301 | } | 2315 | } |
| 2302 | 2316 | ||