Skip to content

Commit 0db6f8d

Browse files
surenbaghdasaryanakpm00
authored andcommitted
alloc_tag: load module tags into separate contiguous memory
When a module gets unloaded there is a possibility that some of the allocations it made are still used and therefore the allocation tags corresponding to these allocations are still referenced. As such, the memory for these tags can't be freed. This is currently handled as an abnormal situation and module's data section is not being unloaded. To handle this situation without keeping module's data in memory, allow codetags with longer lifespan than the module to be loaded into their own separate memory. The in-use memory areas and gaps after module unloading in this separate memory are tracked using maple trees. Allocation tags arrange their separate memory so that it is virtually contiguous and that will allow simple allocation tag indexing later on in this patchset. The size of this virtually contiguous memory is set to store up to 100000 allocation tags. [[email protected]: fix empty codetag module section handling] Link: https://lkml.kernel.org/r/[email protected] [[email protected]: update comment, per Dan] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Suren Baghdasaryan <[email protected]> Reviewed-by: Pasha Tatashin <[email protected]> Cc: Ard Biesheuvel <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Borislav Petkov (AMD) <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Daniel Gomez <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Davidlohr Bueso <[email protected]> Cc: David Rientjes <[email protected]> Cc: Dennis Zhou <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: John Hubbard <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Joonsoo Kim <[email protected]> Cc: Kalesh Singh <[email protected]> Cc: Kees Cook <[email protected]> Cc: Kent Overstreet <[email protected]> Cc: Liam R. Howlett <[email protected]> Cc: Luis Chamberlain <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Mike Rapoport (Microsoft) <[email protected]> Cc: Minchan Kim <[email protected]> Cc: Paul E. McKenney <[email protected]> Cc: Petr Pavlu <[email protected]> Cc: Roman Gushchin <[email protected]> Cc: Sami Tolvanen <[email protected]> Cc: Sourav Panda <[email protected]> Cc: Steven Rostedt (Google) <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Thomas Huth <[email protected]> Cc: Uladzislau Rezki (Sony) <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: Xiongwei Song <[email protected]> Cc: Yu Zhao <[email protected]> Cc: Dan Carpenter <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 3e09c50 commit 0db6f8d

File tree

7 files changed

+445
-62
lines changed

7 files changed

+445
-62
lines changed

include/asm-generic/codetag.lds.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,23 @@
1111
#define CODETAG_SECTIONS() \
1212
SECTION_WITH_BOUNDARIES(alloc_tags)
1313

14+
/*
15+
* Module codetags which aren't used after module unload, therefore have the
16+
* same lifespan as the module and can be safely unloaded with the module.
17+
*/
18+
#define MOD_CODETAG_SECTIONS()
19+
20+
#define MOD_SEPARATE_CODETAG_SECTION(_name) \
21+
.codetag.##_name : { \
22+
SECTION_WITH_BOUNDARIES(_name) \
23+
}
24+
25+
/*
26+
* For codetags which might be used after module unload, therefore might stay
27+
* longer in memory. Each such codetag type has its own section so that we can
28+
* unload them individually once unused.
29+
*/
30+
#define MOD_SEPARATE_CODETAG_SECTIONS() \
31+
MOD_SEPARATE_CODETAG_SECTION(alloc_tags)
32+
1433
#endif /* __ASM_GENERIC_CODETAG_LDS_H */

include/linux/alloc_tag.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ struct alloc_tag {
3030
struct alloc_tag_counters __percpu *counters;
3131
} __aligned(8);
3232

33+
struct alloc_tag_module_section {
34+
unsigned long start_addr;
35+
unsigned long end_addr;
36+
/* used size */
37+
unsigned long size;
38+
};
39+
3340
#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
3441

3542
#define CODETAG_EMPTY ((void *)1)
@@ -54,6 +61,8 @@ static inline void set_codetag_empty(union codetag_ref *ref) {}
5461

5562
#ifdef CONFIG_MEM_ALLOC_PROFILING
5663

64+
#define ALLOC_TAG_SECTION_NAME "alloc_tags"
65+
5766
struct codetag_bytes {
5867
struct codetag *ct;
5968
s64 bytes;
@@ -76,7 +85,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
7685

7786
#define DEFINE_ALLOC_TAG(_alloc_tag) \
7887
static struct alloc_tag _alloc_tag __used __aligned(8) \
79-
__section("alloc_tags") = { \
88+
__section(ALLOC_TAG_SECTION_NAME) = { \
8089
.ct = CODE_TAG_INIT, \
8190
.counters = &_shared_alloc_tag };
8291

@@ -85,7 +94,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
8594
#define DEFINE_ALLOC_TAG(_alloc_tag) \
8695
static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr); \
8796
static struct alloc_tag _alloc_tag __used __aligned(8) \
88-
__section("alloc_tags") = { \
97+
__section(ALLOC_TAG_SECTION_NAME) = { \
8998
.ct = CODE_TAG_INIT, \
9099
.counters = &_alloc_tag_cntr };
91100

include/linux/codetag.h

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ struct codetag_type_desc {
3535
size_t tag_size;
3636
void (*module_load)(struct codetag_type *cttype,
3737
struct codetag_module *cmod);
38-
bool (*module_unload)(struct codetag_type *cttype,
38+
void (*module_unload)(struct codetag_type *cttype,
3939
struct codetag_module *cmod);
40+
#ifdef CONFIG_MODULES
41+
void (*module_replaced)(struct module *mod, struct module *new_mod);
42+
bool (*needs_section_mem)(struct module *mod, unsigned long size);
43+
void *(*alloc_section_mem)(struct module *mod, unsigned long size,
44+
unsigned int prepend, unsigned long align);
45+
void (*free_section_mem)(struct module *mod, bool used);
46+
#endif
4047
};
4148

4249
struct codetag_iterator {
@@ -71,11 +78,31 @@ struct codetag_type *
7178
codetag_register_type(const struct codetag_type_desc *desc);
7279

7380
#if defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES)
81+
82+
bool codetag_needs_module_section(struct module *mod, const char *name,
83+
unsigned long size);
84+
void *codetag_alloc_module_section(struct module *mod, const char *name,
85+
unsigned long size, unsigned int prepend,
86+
unsigned long align);
87+
void codetag_free_module_sections(struct module *mod);
88+
void codetag_module_replaced(struct module *mod, struct module *new_mod);
7489
void codetag_load_module(struct module *mod);
75-
bool codetag_unload_module(struct module *mod);
76-
#else
90+
void codetag_unload_module(struct module *mod);
91+
92+
#else /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */
93+
94+
static inline bool
95+
codetag_needs_module_section(struct module *mod, const char *name,
96+
unsigned long size) { return false; }
97+
static inline void *
98+
codetag_alloc_module_section(struct module *mod, const char *name,
99+
unsigned long size, unsigned int prepend,
100+
unsigned long align) { return NULL; }
101+
static inline void codetag_free_module_sections(struct module *mod) {}
102+
static inline void codetag_module_replaced(struct module *mod, struct module *new_mod) {}
77103
static inline void codetag_load_module(struct module *mod) {}
78-
static inline bool codetag_unload_module(struct module *mod) { return true; }
79-
#endif
104+
static inline void codetag_unload_module(struct module *mod) {}
105+
106+
#endif /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */
80107

81108
#endif /* _LINUX_CODETAG_H */

kernel/module/main.c

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,22 +1251,17 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type)
12511251
return 0;
12521252
}
12531253

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)
12561255
{
12571256
struct module_memory *mem = &mod->mem[type];
1258-
void *ptr = mem->base;
12591257

12601258
if (mem->is_rox)
12611259
vfree(mem->rw_copy);
12621260

1263-
if (!unload_codetags && mod_mem_type_is_core_data(type))
1264-
return;
1265-
1266-
execmem_free(ptr);
1261+
execmem_free(mem->base);
12671262
}
12681263

1269-
static void free_mod_mem(struct module *mod, bool unload_codetags)
1264+
static void free_mod_mem(struct module *mod)
12701265
{
12711266
for_each_mod_mem_type(type) {
12721267
struct module_memory *mod_mem = &mod->mem[type];
@@ -1277,25 +1272,20 @@ static void free_mod_mem(struct module *mod, bool unload_codetags)
12771272
/* Free lock-classes; relies on the preceding sync_rcu(). */
12781273
lockdep_free_key_range(mod_mem->base, mod_mem->size);
12791274
if (mod_mem->size)
1280-
module_memory_free(mod, type, unload_codetags);
1275+
module_memory_free(mod, type);
12811276
}
12821277

12831278
/* MOD_DATA hosts mod, so free it at last */
12841279
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);
12861281
}
12871282

12881283
/* Free a module, remove from lists, etc. */
12891284
static void free_module(struct module *mod)
12901285
{
1291-
bool unload_codetags;
1292-
12931286
trace_module_free(mod);
12941287

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);
12991289

13001290
mod_sysfs_teardown(mod);
13011291

@@ -1338,7 +1328,7 @@ static void free_module(struct module *mod)
13381328
kfree(mod->args);
13391329
percpu_modfree(mod);
13401330

1341-
free_mod_mem(mod, unload_codetags);
1331+
free_mod_mem(mod);
13421332
}
13431333

13441334
void *__symbol_get(const char *symbol)
@@ -1603,6 +1593,20 @@ static void __layout_sections(struct module *mod, struct load_info *info, bool i
16031593
if (WARN_ON_ONCE(type == MOD_INVALID))
16041594
continue;
16051595

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+
16061610
s->sh_entsize = module_get_offset_and_type(mod, type, s, i);
16071611
pr_debug("\t%s\n", sname);
16081612
}
@@ -2277,6 +2281,7 @@ static int move_module(struct module *mod, struct load_info *info)
22772281
int i;
22782282
enum mod_mem_type t = 0;
22792283
int ret = -ENOMEM;
2284+
bool codetag_section_found = false;
22802285

22812286
for_each_mod_mem_type(type) {
22822287
if (!mod->mem[type].size) {
@@ -2288,7 +2293,7 @@ static int move_module(struct module *mod, struct load_info *info)
22882293
ret = module_memory_alloc(mod, type);
22892294
if (ret) {
22902295
t = type;
2291-
goto out_enomem;
2296+
goto out_err;
22922297
}
22932298
}
22942299

@@ -2297,15 +2302,37 @@ static int move_module(struct module *mod, struct load_info *info)
22972302
for (i = 0; i < info->hdr->e_shnum; i++) {
22982303
void *dest;
22992304
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;
23022306
unsigned long addr;
23032307

23042308
if (!(shdr->sh_flags & SHF_ALLOC))
23052309
continue;
23062310

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+
}
23092336

23102337
if (shdr->sh_type != SHT_NOBITS) {
23112338
/*
@@ -2317,7 +2344,7 @@ static int move_module(struct module *mod, struct load_info *info)
23172344
if (i == info->index.mod &&
23182345
(WARN_ON_ONCE(shdr->sh_size != sizeof(struct module)))) {
23192346
ret = -ENOEXEC;
2320-
goto out_enomem;
2347+
goto out_err;
23212348
}
23222349
memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
23232350
}
@@ -2333,9 +2360,12 @@ static int move_module(struct module *mod, struct load_info *info)
23332360
}
23342361

23352362
return 0;
2336-
out_enomem:
2363+
out_err:
23372364
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+
23392369
return ret;
23402370
}
23412371

@@ -2456,6 +2486,8 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
24562486
/* Module has been copied to its final place now: return it. */
24572487
mod = (void *)info->sechdrs[info->index.mod].sh_addr;
24582488
kmemleak_load_module(mod, info);
2489+
codetag_module_replaced(info->mod, mod);
2490+
24592491
return mod;
24602492
}
24612493

@@ -2465,7 +2497,7 @@ static void module_deallocate(struct module *mod, struct load_info *info)
24652497
percpu_modfree(mod);
24662498
module_arch_freeing_init(mod);
24672499

2468-
free_mod_mem(mod, true);
2500+
free_mod_mem(mod);
24692501
}
24702502

24712503
int __weak module_finalize(const Elf_Ehdr *hdr,

0 commit comments

Comments
 (0)