@@ -1251,22 +1251,17 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type)
1251
1251
return 0 ;
1252
1252
}
1253
1253
1254
- static void module_memory_free (struct module * mod , enum mod_mem_type type ,
1255
- bool unload_codetags )
1254
+ static void module_memory_free (struct module * mod , enum mod_mem_type type )
1256
1255
{
1257
1256
struct module_memory * mem = & mod -> mem [type ];
1258
- void * ptr = mem -> base ;
1259
1257
1260
1258
if (mem -> is_rox )
1261
1259
vfree (mem -> rw_copy );
1262
1260
1263
- if (!unload_codetags && mod_mem_type_is_core_data (type ))
1264
- return ;
1265
-
1266
- execmem_free (ptr );
1261
+ execmem_free (mem -> base );
1267
1262
}
1268
1263
1269
- static void free_mod_mem (struct module * mod , bool unload_codetags )
1264
+ static void free_mod_mem (struct module * mod )
1270
1265
{
1271
1266
for_each_mod_mem_type (type ) {
1272
1267
struct module_memory * mod_mem = & mod -> mem [type ];
@@ -1277,25 +1272,20 @@ static void free_mod_mem(struct module *mod, bool unload_codetags)
1277
1272
/* Free lock-classes; relies on the preceding sync_rcu(). */
1278
1273
lockdep_free_key_range (mod_mem -> base , mod_mem -> size );
1279
1274
if (mod_mem -> size )
1280
- module_memory_free (mod , type , unload_codetags );
1275
+ module_memory_free (mod , type );
1281
1276
}
1282
1277
1283
1278
/* MOD_DATA hosts mod, so free it at last */
1284
1279
lockdep_free_key_range (mod -> mem [MOD_DATA ].base , mod -> mem [MOD_DATA ].size );
1285
- module_memory_free (mod , MOD_DATA , unload_codetags );
1280
+ module_memory_free (mod , MOD_DATA );
1286
1281
}
1287
1282
1288
1283
/* Free a module, remove from lists, etc. */
1289
1284
static void free_module (struct module * mod )
1290
1285
{
1291
- bool unload_codetags ;
1292
-
1293
1286
trace_module_free (mod );
1294
1287
1295
- unload_codetags = codetag_unload_module (mod );
1296
- if (!unload_codetags )
1297
- pr_warn ("%s: memory allocation(s) from the module still alive, cannot unload cleanly\n" ,
1298
- mod -> name );
1288
+ codetag_unload_module (mod );
1299
1289
1300
1290
mod_sysfs_teardown (mod );
1301
1291
@@ -1338,7 +1328,7 @@ static void free_module(struct module *mod)
1338
1328
kfree (mod -> args );
1339
1329
percpu_modfree (mod );
1340
1330
1341
- free_mod_mem (mod , unload_codetags );
1331
+ free_mod_mem (mod );
1342
1332
}
1343
1333
1344
1334
void * __symbol_get (const char * symbol )
@@ -1603,6 +1593,20 @@ static void __layout_sections(struct module *mod, struct load_info *info, bool i
1603
1593
if (WARN_ON_ONCE (type == MOD_INVALID ))
1604
1594
continue ;
1605
1595
1596
+ /*
1597
+ * Do not allocate codetag memory as we load it into
1598
+ * preallocated contiguous memory.
1599
+ */
1600
+ if (codetag_needs_module_section (mod , sname , s -> sh_size )) {
1601
+ /*
1602
+ * s->sh_entsize won't be used but populate the
1603
+ * type field to avoid confusion.
1604
+ */
1605
+ s -> sh_entsize = ((unsigned long )(type ) & SH_ENTSIZE_TYPE_MASK )
1606
+ << SH_ENTSIZE_TYPE_SHIFT ;
1607
+ continue ;
1608
+ }
1609
+
1606
1610
s -> sh_entsize = module_get_offset_and_type (mod , type , s , i );
1607
1611
pr_debug ("\t%s\n" , sname );
1608
1612
}
@@ -2277,6 +2281,7 @@ static int move_module(struct module *mod, struct load_info *info)
2277
2281
int i ;
2278
2282
enum mod_mem_type t = 0 ;
2279
2283
int ret = - ENOMEM ;
2284
+ bool codetag_section_found = false;
2280
2285
2281
2286
for_each_mod_mem_type (type ) {
2282
2287
if (!mod -> mem [type ].size ) {
@@ -2288,7 +2293,7 @@ static int move_module(struct module *mod, struct load_info *info)
2288
2293
ret = module_memory_alloc (mod , type );
2289
2294
if (ret ) {
2290
2295
t = type ;
2291
- goto out_enomem ;
2296
+ goto out_err ;
2292
2297
}
2293
2298
}
2294
2299
@@ -2297,15 +2302,37 @@ static int move_module(struct module *mod, struct load_info *info)
2297
2302
for (i = 0 ; i < info -> hdr -> e_shnum ; i ++ ) {
2298
2303
void * dest ;
2299
2304
Elf_Shdr * shdr = & info -> sechdrs [i ];
2300
- enum mod_mem_type type = shdr -> sh_entsize >> SH_ENTSIZE_TYPE_SHIFT ;
2301
- unsigned long offset = shdr -> sh_entsize & SH_ENTSIZE_OFFSET_MASK ;
2305
+ const char * sname ;
2302
2306
unsigned long addr ;
2303
2307
2304
2308
if (!(shdr -> sh_flags & SHF_ALLOC ))
2305
2309
continue ;
2306
2310
2307
- addr = (unsigned long )mod -> mem [type ].base + offset ;
2308
- dest = mod -> mem [type ].rw_copy + offset ;
2311
+ sname = info -> secstrings + shdr -> sh_name ;
2312
+ /*
2313
+ * Load codetag sections separately as they might still be used
2314
+ * after module unload.
2315
+ */
2316
+ if (codetag_needs_module_section (mod , sname , shdr -> sh_size )) {
2317
+ dest = codetag_alloc_module_section (mod , sname , shdr -> sh_size ,
2318
+ arch_mod_section_prepend (mod , i ), shdr -> sh_addralign );
2319
+ if (WARN_ON (!dest )) {
2320
+ ret = - EINVAL ;
2321
+ goto out_err ;
2322
+ }
2323
+ if (IS_ERR (dest )) {
2324
+ ret = PTR_ERR (dest );
2325
+ goto out_err ;
2326
+ }
2327
+ addr = (unsigned long )dest ;
2328
+ codetag_section_found = true;
2329
+ } else {
2330
+ enum mod_mem_type type = shdr -> sh_entsize >> SH_ENTSIZE_TYPE_SHIFT ;
2331
+ unsigned long offset = shdr -> sh_entsize & SH_ENTSIZE_OFFSET_MASK ;
2332
+
2333
+ addr = (unsigned long )mod -> mem [type ].base + offset ;
2334
+ dest = mod -> mem [type ].rw_copy + offset ;
2335
+ }
2309
2336
2310
2337
if (shdr -> sh_type != SHT_NOBITS ) {
2311
2338
/*
@@ -2317,7 +2344,7 @@ static int move_module(struct module *mod, struct load_info *info)
2317
2344
if (i == info -> index .mod &&
2318
2345
(WARN_ON_ONCE (shdr -> sh_size != sizeof (struct module )))) {
2319
2346
ret = - ENOEXEC ;
2320
- goto out_enomem ;
2347
+ goto out_err ;
2321
2348
}
2322
2349
memcpy (dest , (void * )shdr -> sh_addr , shdr -> sh_size );
2323
2350
}
@@ -2333,9 +2360,12 @@ static int move_module(struct module *mod, struct load_info *info)
2333
2360
}
2334
2361
2335
2362
return 0 ;
2336
- out_enomem :
2363
+ out_err :
2337
2364
for (t -- ; t >= 0 ; t -- )
2338
- module_memory_free (mod , t , true);
2365
+ module_memory_free (mod , t );
2366
+ if (codetag_section_found )
2367
+ codetag_free_module_sections (mod );
2368
+
2339
2369
return ret ;
2340
2370
}
2341
2371
@@ -2456,6 +2486,8 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
2456
2486
/* Module has been copied to its final place now: return it. */
2457
2487
mod = (void * )info -> sechdrs [info -> index .mod ].sh_addr ;
2458
2488
kmemleak_load_module (mod , info );
2489
+ codetag_module_replaced (info -> mod , mod );
2490
+
2459
2491
return mod ;
2460
2492
}
2461
2493
@@ -2465,7 +2497,7 @@ static void module_deallocate(struct module *mod, struct load_info *info)
2465
2497
percpu_modfree (mod );
2466
2498
module_arch_freeing_init (mod );
2467
2499
2468
- free_mod_mem (mod , true );
2500
+ free_mod_mem (mod );
2469
2501
}
2470
2502
2471
2503
int __weak module_finalize (const Elf_Ehdr * hdr ,
0 commit comments