Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

module: make module_memory_{alloc,free} more self-contained

Move the logic related to the memory allocation and freeing into
module_memory_alloc() and module_memory_free().

Signed-off-by: Mike Rapoport (IBM) <rppt@kernel.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

authored by

Mike Rapoport (IBM) and committed by
Luis Chamberlain
bc6b94d3 e8dbc6a8

+39 -25
+39 -25
kernel/module/main.c
··· 1203 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 1213 if (mod_mem_use_vmalloc(type)) 1209 - return vzalloc(size); 1210 - return module_alloc(size); 1214 + ptr = vmalloc(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 1244 if (mod_mem_use_vmalloc(type)) 1216 1245 vfree(ptr); 1217 1246 else ··· 1258 1229 /* Free lock-classes; relies on the preceding sync_rcu(). */ 1259 1230 lockdep_free_key_range(mod_mem->base, mod_mem->size); 1260 1231 if (mod_mem->size) 1261 - module_memory_free(mod_mem->base, type); 1232 + module_memory_free(mod, type); 1262 1233 } 1263 1234 1264 1235 /* MOD_DATA hosts mod, so free it at last */ 1265 1236 lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size); 1266 - module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA); 1237 + module_memory_free(mod, MOD_DATA); 1267 1238 } 1268 1239 1269 1240 /* Free a module, remove from lists, etc. */ ··· 2254 2225 static int move_module(struct module *mod, struct load_info *info) 2255 2226 { 2256 2227 int i; 2257 - void *ptr; 2258 2228 enum mod_mem_type t = 0; 2259 2229 int ret = -ENOMEM; 2260 2230 ··· 2262 2234 mod->mem[type].base = NULL; 2263 2235 continue; 2264 2236 } 2265 - mod->mem[type].size = PAGE_ALIGN(mod->mem[type].size); 2266 - ptr = module_memory_alloc(mod->mem[type].size, type); 2267 - /* 2268 - * The pointer to these blocks of memory are stored on the module 2269 - * structure and we keep that around so long as the module is 2270 - * around. We only free that memory when we unload the module. 2271 - * Just mark them as not being a leak then. The .init* ELF 2272 - * sections *do* get freed after boot so we *could* treat them 2273 - * slightly differently with kmemleak_ignore() and only grey 2274 - * them out as they work as typical memory allocations which 2275 - * *do* eventually get freed, but let's just keep things simple 2276 - * and avoid *any* false positives. 2277 - */ 2278 - kmemleak_not_leak(ptr); 2279 - if (!ptr) { 2237 + 2238 + ret = module_memory_alloc(mod, type); 2239 + if (ret) { 2280 2240 t = type; 2281 2241 goto out_enomem; 2282 2242 } 2283 - memset(ptr, 0, mod->mem[type].size); 2284 - mod->mem[type].base = ptr; 2285 2243 } 2286 2244 2287 2245 /* Transfer each section which specifies SHF_ALLOC */ ··· 2310 2296 return 0; 2311 2297 out_enomem: 2312 2298 for (t--; t >= 0; t--) 2313 - module_memory_free(mod->mem[t].base, t); 2299 + module_memory_free(mod, t); 2314 2300 return ret; 2315 2301 } 2316 2302