History

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().

Mike Rapoport (IBM) committed 2 years ago

Commit bc6b94d1 parent e8dbc6a1 file · +39 −25Browse files

Showing the changes to kernel/module (1 of 1 file). Show all

Unified · Split

kernel/module/main.c+39−25
@@ -1203,15 +1203,44 @@ static bool mod_mem_use_vmalloc(enum mod_mem_type type) · 1202 unmodified lines
12031203 mod_mem_type_is_core_data(type);
12041204 }
12051205
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)
12071207 {
1208+ unsigned int size = PAGE_ALIGN(mod->mem[type].size);
1209+ void *ptr;
1210+
1211+ mod->mem[type].size = size;
1212+
12081213 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;
12111238 }
12121239
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)
12141241 {
1242+ void *ptr = mod->mem[type].base;
1243+
12151244 if (mod_mem_use_vmalloc(type))
12161245 vfree(ptr);
12171246 else
@@ -1229,12 +1258,12 @@ static void free_mod_mem(struct module *mod) · 11 unmodified lines
12291258 /* Free lock-classes; relies on the preceding sync_rcu(). */
12301259 lockdep_free_key_range(mod_mem->base, mod_mem->size);
12311260 if (mod_mem->size)
1232− module_memory_free(mod_mem->base, type);
1261+ module_memory_free(mod, type);
12331262 }
12341263
12351264 /* MOD_DATA hosts mod, so free it at last */
12361265 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);
12381267 }
12391268
12401269 /* 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
22252254 static int move_module(struct module *mod, struct load_info *info)
22262255 {
22272256 int i;
2228− void *ptr;
22292257 enum mod_mem_type t = 0;
22302258 int ret = -ENOMEM;
22312259
@@ -2234,26 +2262,12 @@ static int move_module(struct module *mod, struct load_info *info) · 2 unmodified lines
22342262 mod->mem[type].base = NULL;
22352263 continue;
22362264 }
2237− mod->mem[type].size = PAGE_ALIGN(mod->mem[type].size);
2238− ptr = module_memory_alloc(mod->mem[type].size, type);
2239− /*
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) {
2265+
2266+ ret = module_memory_alloc(mod, type);
2267+ if (ret) {
22522268 t = type;
22532269 goto out_enomem;
22542270 }
2255− memset(ptr, 0, mod->mem[type].size);
2256− mod->mem[type].base = ptr;
22572271 }
22582272
22592273 /* 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
22962310 return 0;
22972311 out_enomem:
22982312 for (t--; t >= 0; t--)
2299− module_memory_free(mod->mem[t].base, t);
2313+ module_memory_free(mod, t);
23002314 return ret;
23012315 }
23022316