Skip to content

Commit 0d69206

Browse files
committed
alloc_tag: load module tags into separate contiguous memory
jira LE-4694 Rebuild_History Non-Buildable kernel-6.12.0-55.43.1.el10_0 commit-author Suren Baghdasaryan <surenb@google.com> commit 0db6f8d 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. [surenb@google.com: fix empty codetag module section handling] Link: https://lkml.kernel.org/r/20241101000017.3856204-1-surenb@google.com [akpm@linux-foundation.org: update comment, per Dan] Link: https://lkml.kernel.org/r/20241023170759.999909-4-surenb@google.com Signed-off-by: Suren Baghdasaryan <surenb@google.com> Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Borislav Petkov (AMD) <bp@alien8.de> Cc: Christoph Hellwig <hch@infradead.org> Cc: Daniel Gomez <da.gomez@samsung.com> Cc: David Hildenbrand <david@redhat.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: David Rientjes <rientjes@google.com> Cc: Dennis Zhou <dennis@kernel.org> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: John Hubbard <jhubbard@nvidia.com> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Kalesh Singh <kaleshsingh@google.com> Cc: Kees Cook <keescook@chromium.org> Cc: Kent Overstreet <kent.overstreet@linux.dev> Cc: Liam R. Howlett <Liam.Howlett@Oracle.com> Cc: Luis Chamberlain <mcgrof@kernel.org> Cc: Matthew Wilcox <willy@infradead.org> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Rapoport (Microsoft) <rppt@kernel.org> Cc: Minchan Kim <minchan@google.com> Cc: Paul E. McKenney <paulmck@kernel.org> Cc: Petr Pavlu <petr.pavlu@suse.com> Cc: Roman Gushchin <roman.gushchin@linux.dev> Cc: Sami Tolvanen <samitolvanen@google.com> Cc: Sourav Panda <souravpanda@google.com> Cc: Steven Rostedt (Google) <rostedt@goodmis.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Thomas Huth <thuth@redhat.com> Cc: Uladzislau Rezki (Sony) <urezki@gmail.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Xiongwei Song <xiongwei.song@windriver.com> Cc: Yu Zhao <yuzhao@google.com> Cc: Dan Carpenter <dan.carpenter@linaro.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> (cherry picked from commit 0db6f8d) Signed-off-by: Jonathan Maple <jmaple@ciq.com>
1 parent 66e3e16 commit 0d69206

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
@@ -1255,22 +1255,17 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type)
12551255
return 0;
12561256
}
12571257

1258-
static void module_memory_free(struct module *mod, enum mod_mem_type type,
1259-
bool unload_codetags)
1258+
static void module_memory_free(struct module *mod, enum mod_mem_type type)
12601259
{
12611260
struct module_memory *mem = &mod->mem[type];
1262-
void *ptr = mem->base;
12631261

12641262
if (mem->is_rox)
12651263
vfree(mem->rw_copy);
12661264

1267-
if (!unload_codetags && mod_mem_type_is_core_data(type))
1268-
return;
1269-
1270-
execmem_free(ptr);
1265+
execmem_free(mem->base);
12711266
}
12721267

1273-
static void free_mod_mem(struct module *mod, bool unload_codetags)
1268+
static void free_mod_mem(struct module *mod)
12741269
{
12751270
for_each_mod_mem_type(type) {
12761271
struct module_memory *mod_mem = &mod->mem[type];
@@ -1281,25 +1276,20 @@ static void free_mod_mem(struct module *mod, bool unload_codetags)
12811276
/* Free lock-classes; relies on the preceding sync_rcu(). */
12821277
lockdep_free_key_range(mod_mem->base, mod_mem->size);
12831278
if (mod_mem->size)
1284-
module_memory_free(mod, type, unload_codetags);
1279+
module_memory_free(mod, type);
12851280
}
12861281

12871282
/* MOD_DATA hosts mod, so free it at last */
12881283
lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size);
1289-
module_memory_free(mod, MOD_DATA, unload_codetags);
1284+
module_memory_free(mod, MOD_DATA);
12901285
}
12911286

12921287
/* Free a module, remove from lists, etc. */
12931288
static void free_module(struct module *mod)
12941289
{
1295-
bool unload_codetags;
1296-
12971290
trace_module_free(mod);
12981291

1299-
unload_codetags = codetag_unload_module(mod);
1300-
if (!unload_codetags)
1301-
pr_warn("%s: memory allocation(s) from the module still alive, cannot unload cleanly\n",
1302-
mod->name);
1292+
codetag_unload_module(mod);
13031293

13041294
mod_sysfs_teardown(mod);
13051295

@@ -1342,7 +1332,7 @@ static void free_module(struct module *mod)
13421332
kfree(mod->args);
13431333
percpu_modfree(mod);
13441334

1345-
free_mod_mem(mod, unload_codetags);
1335+
free_mod_mem(mod);
13461336
}
13471337

13481338
void *__symbol_get(const char *symbol)
@@ -1607,6 +1597,20 @@ static void __layout_sections(struct module *mod, struct load_info *info, bool i
16071597
if (WARN_ON_ONCE(type == MOD_INVALID))
16081598
continue;
16091599

1600+
/*
1601+
* Do not allocate codetag memory as we load it into
1602+
* preallocated contiguous memory.
1603+
*/
1604+
if (codetag_needs_module_section(mod, sname, s->sh_size)) {
1605+
/*
1606+
* s->sh_entsize won't be used but populate the
1607+
* type field to avoid confusion.
1608+
*/
1609+
s->sh_entsize = ((unsigned long)(type) & SH_ENTSIZE_TYPE_MASK)
1610+
<< SH_ENTSIZE_TYPE_SHIFT;
1611+
continue;
1612+
}
1613+
16101614
s->sh_entsize = module_get_offset_and_type(mod, type, s, i);
16111615
pr_debug("\t%s\n", sname);
16121616
}
@@ -2281,6 +2285,7 @@ static int move_module(struct module *mod, struct load_info *info)
22812285
int i;
22822286
enum mod_mem_type t = 0;
22832287
int ret = -ENOMEM;
2288+
bool codetag_section_found = false;
22842289

22852290
for_each_mod_mem_type(type) {
22862291
if (!mod->mem[type].size) {
@@ -2292,7 +2297,7 @@ static int move_module(struct module *mod, struct load_info *info)
22922297
ret = module_memory_alloc(mod, type);
22932298
if (ret) {
22942299
t = type;
2295-
goto out_enomem;
2300+
goto out_err;
22962301
}
22972302
}
22982303

@@ -2301,15 +2306,37 @@ static int move_module(struct module *mod, struct load_info *info)
23012306
for (i = 0; i < info->hdr->e_shnum; i++) {
23022307
void *dest;
23032308
Elf_Shdr *shdr = &info->sechdrs[i];
2304-
enum mod_mem_type type = shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT;
2305-
unsigned long offset = shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK;
2309+
const char *sname;
23062310
unsigned long addr;
23072311

23082312
if (!(shdr->sh_flags & SHF_ALLOC))
23092313
continue;
23102314

2311-
addr = (unsigned long)mod->mem[type].base + offset;
2312-
dest = mod->mem[type].rw_copy + offset;
2315+
sname = info->secstrings + shdr->sh_name;
2316+
/*
2317+
* Load codetag sections separately as they might still be used
2318+
* after module unload.
2319+
*/
2320+
if (codetag_needs_module_section(mod, sname, shdr->sh_size)) {
2321+
dest = codetag_alloc_module_section(mod, sname, shdr->sh_size,
2322+
arch_mod_section_prepend(mod, i), shdr->sh_addralign);
2323+
if (WARN_ON(!dest)) {
2324+
ret = -EINVAL;
2325+
goto out_err;
2326+
}
2327+
if (IS_ERR(dest)) {
2328+
ret = PTR_ERR(dest);
2329+
goto out_err;
2330+
}
2331+
addr = (unsigned long)dest;
2332+
codetag_section_found = true;
2333+
} else {
2334+
enum mod_mem_type type = shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT;
2335+
unsigned long offset = shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK;
2336+
2337+
addr = (unsigned long)mod->mem[type].base + offset;
2338+
dest = mod->mem[type].rw_copy + offset;
2339+
}
23132340

23142341
if (shdr->sh_type != SHT_NOBITS) {
23152342
/*
@@ -2321,7 +2348,7 @@ static int move_module(struct module *mod, struct load_info *info)
23212348
if (i == info->index.mod &&
23222349
(WARN_ON_ONCE(shdr->sh_size != sizeof(struct module)))) {
23232350
ret = -ENOEXEC;
2324-
goto out_enomem;
2351+
goto out_err;
23252352
}
23262353
memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
23272354
}
@@ -2337,9 +2364,12 @@ static int move_module(struct module *mod, struct load_info *info)
23372364
}
23382365

23392366
return 0;
2340-
out_enomem:
2367+
out_err:
23412368
for (t--; t >= 0; t--)
2342-
module_memory_free(mod, t, true);
2369+
module_memory_free(mod, t);
2370+
if (codetag_section_found)
2371+
codetag_free_module_sections(mod);
2372+
23432373
return ret;
23442374
}
23452375

@@ -2460,6 +2490,8 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
24602490
/* Module has been copied to its final place now: return it. */
24612491
mod = (void *)info->sechdrs[info->index.mod].sh_addr;
24622492
kmemleak_load_module(mod, info);
2493+
codetag_module_replaced(info->mod, mod);
2494+
24632495
return mod;
24642496
}
24652497

@@ -2469,7 +2501,7 @@ static void module_deallocate(struct module *mod, struct load_info *info)
24692501
percpu_modfree(mod);
24702502
module_arch_freeing_init(mod);
24712503

2472-
free_mod_mem(mod, true);
2504+
free_mod_mem(mod);
24732505
}
24742506

24752507
int __weak module_finalize(const Elf_Ehdr *hdr,

0 commit comments

Comments
 (0)