module: allocate codetag sections before the regular module layout
Subsystems (4)
Hao Ge
reviewerv6 cover module: allocate codetag sections before the regular module layout
I ran into an overflow problem in the module tag area. With profiling toggled off, the overflow check in reserve_module_tags() did not run, a module could load with more tags than the page flags can address, and re-enabling profiling then silently corrupted /proc/allocinfo. After discussion with Suren and Andrew the fix went for a graceful approach: on overflow shut profiling down, release the reservation and return -EAGAIN, and retry the load with profiling disabled, so the codetag section lands as regular module data and the module loads without profiling.
Review of that series by Sashiko turned up two more problems.
One is a race. layout_sections() and move_module() both asked codetag_needs_module_section() where a codetag section goes, and mem_profiling_support can change between the two calls, for instance when another module load overflows the tag index and shuts profiling down. move_module() then copies the codetag section to offset 0 of its regular destination and clobbers the first section placed in that region.
The other is a maple tree entry leak. The failure paths of reserve_module_tags() return with the reservation still stored, and a failed load never unloads the module, so nothing releases it.
v6 fixes both by reworking where codetag sections are allocated, on a prototype by Petr Pavlu [1]. The allocation now runs before layout_sections() and the placement is decided in one step, so nothing re-asks the question and the race is gone. The retry is gone too, on -EAGAIN the section is laid out as regular module data right in the same load, and both failure paths release the reservation.
[1] https://lore.kernel.org/all/[email protected]/
Sending this as an RFC since 2/2 is a rework of Petr's prototype and the approach changed quite a bit from v5, feedback on the direction would be welcome.
Patch 1 moves release_module_tags() above reserve_module_tags(), since the overflow path now has to call it and the helper sits below it.
Patch 2 moves the codetag allocation out of move_module() in front of layout_sections(). On overflow reserve_module_tags() shuts profiling down, releases the reservation and returns -EAGAIN, and the section is laid out as regular module data, so the module loads without profiling instead of failing. Any other error fails the load.
Tested on an x86_64 virtual machine:
Booted without sysctl.vm.mem_profiling=1,compressed:
# cat /proc/allocinfo is fine
Booted with sysctl.vm.mem_profiling=1,compressed:
# cat /proc/allocinfo is fine # insmod overflow_tag.ko # dmesg With module overflow_tag there are too many tags to fit in 13 page flag bits. Memory allocation profiling is disabled! # rmmod overflow_tag The module loads without profiling and unloads cleanly.
Changes in v6:
- rework 2/2 on Petr's prototype and allocate codetag sections before layout_sections(), the retry and its state resets are gone
- fix the layout_sections()/move_module() race (Found by Sashiko)
- release the reservation on populate failure as well (Found by Sashiko)
- only -EAGAIN keeps the fallback, other errors fail the load
Changes in v5:
- add Fixes: and Cc: stable to patch 1/2 as well, since 2/2 does not compile without it (Andrew Morton)
- restore frob-adjusted mem[type].size on retry instead of zeroing, as s390 and parisc add GOT/PLT space there in module_frob_arch_sections() (Reported by Sashiko)
- drop the load_module() mem_profiling_support check; the percpu counter leak is pre-existing and orthogonal to this fix
Changes in v4:
- add a new patch (1/2) to move release_module_tags() above reserve_module_tags(); the overflow fix is 2/2
- release the reservation on the -EAGAIN path
- return -EAGAIN instead of -ENOMEM so the module can still load without profiling (Suren)
- reset sh_addr, mem[type].size and sym/str SHF_ALLOC before retry
- skip percpu counters in load_module() when profiling is off
Changes in v3:
- use pr_warn_once() instead of pr_warn()
- return -ENOMEM instead of -ENOSPC (Suren)
- expand the commit message to describe the /proc/allocinfo impact (Andrew)
Changes in v2:
- return an error after shutdown_mem_profiling() to skip vm_module_tags_populate()
v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v5: https://lore.kernel.org/all/[email protected]/
Hao Ge (2):
alloc_tag: move release_module_tags() above reserve_module_tags()
module: allocate codetag sections before the regular module layout
include/linux/module.h | 2 + kernel/module/internal.h | 4 ++ kernel/module/main.c | 120 ++++++++++++++++++++------------------- mm/alloc_tag.c | 101 ++++++++++++++++---------------- 4 files changed, 121 insertions(+), 106 deletions(-)
Signature · 1 line
2.25.1
Hao Ge
reviewerRFC PATCH v6 1/2 alloc_tag: move release_module_tags() above reserve_module_tags()
release_module_tags() is a cleanup helper. reserve_module_tags() can also fail after storing the reservation in the maple tree, in which case it should call release_module_tags() to undo it. Move the helper above reserve_module_tags() so no forward declaration is needed.
No functional change.
--- mm/alloc_tag.c | 92 +++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 46 deletions(-)
mm/alloc_tag.c +46 -46
@@ -843,6 +843,52 @@ static int vm_module_tags_populate(void)return 0 ; } +static void release_module_tags (struct module *mod,bool used) +{ +MA_STATE (mas, &mod_area_mt, module_tags.size, module_tags.size); +struct alloc_tag *start_tag; +struct alloc_tag *end_tag; +struct module *val; + +mas_lock (&mas); +mas_for_each_rev (&mas, val,0 ) +if (val == mod) +break ; + +if (!val)/* module not found */ + goto out; + +if (!used) + goto release_area; + + start_tag = (struct alloc_tag *)(module_tags.start_addr + mas.index); + end_tag = (struct alloc_tag *)(module_tags.start_addr + mas.last); +if (!clean_unused_counters (start_tag, end_tag)) { +struct alloc_tag *tag; + +for (tag = start_tag; tag <= end_tag; tag++) { +struct alloc_tag_counters counter; + +if (!tag->counters) +continue ; + + counter =alloc_tag_read (tag); +pr_info ("%s:%u module %s func:%s has %llu allocated at module unload\n" , + tag->ct.filename, tag->ct.lineno, tag->ct.modname, + tag->ct.function, counter.bytes); + } + }else { + used = false; + } +release_area: +mas_store (&mas, used ? &unloaded_mod :NULL ); + val =mas_prev_range (&mas,0 ); +if (val == &prepend_mod) +mas_store (&mas,NULL ); +out: +mas_unlock (&mas); +} +static void *reserve_module_tags (struct module *mod,unsigned long size,unsigned int prepend,unsigned long align) {
@@ -930,52 +976,6 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,return (struct alloc_tag *)(module_tags.start_addr + offset); } -static void release_module_tags (struct module *mod,bool used) -{ -MA_STATE (mas, &mod_area_mt, module_tags.size, module_tags.size); -struct alloc_tag *start_tag; -struct alloc_tag *end_tag; -struct module *val; - -mas_lock (&mas); -mas_for_each_rev (&mas, val,0 ) -if (val == mod) -break ; - -if (!val)/* module not found */ - goto out; - -if (!used) - goto release_area; - - start_tag = (struct alloc_tag *)(module_tags.start_addr + mas.index); - end_tag = (struct alloc_tag *)(module_tags.start_addr + mas.last); -if (!clean_unused_counters (start_tag, end_tag)) { -struct alloc_tag *tag; - -for (tag = start_tag; tag <= end_tag; tag++) { -struct alloc_tag_counters counter; - -if (!tag->counters) -continue ; - - counter =alloc_tag_read (tag); -pr_info ("%s:%u module %s func:%s has %llu allocated at module unload\n" , - tag->ct.filename, tag->ct.lineno, tag->ct.modname, - tag->ct.function, counter.bytes); - } - }else { - used = false; - } -release_area: -mas_store (&mas, used ? &unloaded_mod :NULL ); - val =mas_prev_range (&mas,0 ); -if (val == &prepend_mod) -mas_store (&mas,NULL ); -out: -mas_unlock (&mas); -} -static int load_module (struct module *mod,struct codetag *start,struct codetag *stop) {/* Allocate module alloc_tag percpu counters */
Signature · 1 line
2.25.1
Hao Ge
reviewerRFC PATCH v6 2/2 module: allocate codetag sections before the regular module layout
In reply to Hao GeWhether a codetag section goes to the codetag region is decided by layout_sections() and asked again in move_module(). A concurrent load can shut profiling down in between, and move_module() then copies the section to offset 0 of its regular destination, overwriting whatever is there.
Decide and allocate in one pass, before the layout. Allocation errors fail the load. On a tag area overflow profiling is already disabled, so -EAGAIN makes the section fall back to regular module data and the module still loads.
The overflow and populate failure paths of reserve_module_tags() now release their reservation instead of leaking the maple tree entry. When profiling was toggled off, the overflow check did not run, a module could load with more tags than the page flags can address, and re-enabling profiling then silently corrupted /proc/allocinfo. The check no longer depends on mem_alloc_profiling_enabled().
---
Changes against Petr's prototype:
- allocate_codetag_sections() returns an error instead of void, and only -EAGAIN falls back to a regular section. Any other error now fails the load. The prototype fell back on everything, which can leave live tags in module memory.
- reserve_module_tags() releases its reservation when populate fails too, that path used to leak the maple tree entry.
- codetag_free_module_sections() on the move_module() error path uses info->mod, the local mod is assigned only after a successful move.
- The percpu section is marked only when index.pcpu != 0, otherwise sechdrs[0] gets marked.
- Dropped the SHF_ALLOC check, .codetag.* sections always have it.
--- include/linux/module.h | 2 + kernel/module/internal.h | 4 ++ kernel/module/main.c | 120 ++++++++++++++++++++------------------- mm/alloc_tag.c | 9 ++- 4 files changed, 75 insertions(+), 60 deletions(-)
include/linux/module.h +2 -0
@@ -325,6 +325,8 @@ enum mod_mem_type {MOD_INIT_RODATA ,MOD_MEM_NUM_TYPES , + +MOD_STANDALONE =-2 , MOD_INVALID = -1, };
kernel/module/internal.h +4 -0
@@ -29,6 +29,10 @@#define SH_ENTSIZE_TYPE_MASK ((1UL << SH_ENTSIZE_TYPE_BITS) - 1)#define SH_ENTSIZE_OFFSET_MASK ((1UL << (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)) - 1) +#define SH_ENTSIZE_STANDALONE \ + (((unsigned long)MOD_STANDALONE & SH_ENTSIZE_TYPE_MASK) \ + << SH_ENTSIZE_TYPE_SHIFT) +/* Maximum number of characters written by module_flags() */ #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
kernel/module/main.c +63 -57
192 lines not on this page · raw message
mm/alloc_tag.c +6 -3
@@ -958,10 +958,12 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,int grow_res; module_tags.size = offset + size; -if (mem_alloc_profiling_enabled () && !tags_addressable ()) { +if (!tags_addressable ()) {shutdown_mem_profiling (true); -pr_warn ("With module %s there are too many tags to fit in %d page flag bits. Memory allocation profiling is disabled!\n" , - mod->name,NR_UNUSED_PAGEFLAG_BITS ); +pr_warn_once ("With module %s there are too many tags to fit in %d page flag bits. Memory allocation profiling is disabled!\n" , + mod->name,NR_UNUSED_PAGEFLAG_BITS ); +release_module_tags (mod, false); +return ERR_PTR (-EAGAIN ); } grow_res =vm_module_tags_populate ();
@@ -969,6 +971,7 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,shutdown_mem_profiling (true);pr_err ("Failed to allocate memory for allocation tags in the module %s. Memory allocation profiling is disabled!\n" , mod->name); +release_module_tags (mod, false);return ERR_PTR (grow_res); } }
Signature · 1 line
2.25.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Percpu counters are permanently leaked when a codetag section falls back to regular module memory.…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Percpu counters are permanently leaked when a codetag section falls back to regular module memory. --
commit 67e030141d036ccecf172380286b7317fac48a51
Author: Hao Ge <[email protected]>
module: allocate codetag sections before the regular module layout
This patch moves codetag section allocation to occur before the regular module layout and fails the load on allocation errors. However, on tag area overflow, it gracefully falls back to allocating the codetag section in regular module memory instead.
Quoted 4 lines
kernel/module/main.c +0
-0
[ ... ]
Quoted 19 lines
kernel/module/main.c +15
-0@@ -2951,9 +2906,48 @@ static bool blacklisted(const char *module_name) }core_param (module_blacklist, module_blacklist, charp,0400 ); +/* +* Allocate codetag sections separately. They are loaded into preallocated +* contiguous memory because they may still be used after the module is +* unloaded. +* +* If the separate allocation overflows, allocate the section normally +* so that the module can still be loaded. +*/ +static int allocate_codetag_sections (struct load_info *info) +{ +for (unsigned int i =1 ; i < info->hdr->e_shnum; i++) { +Elf_Shdr *shdr = &info->sechdrs[i]; +const char *sname = info->secstrings + shdr->sh_name; +void *dest; +
kernel/module/main.c static bool blacklisted(const char *module_name) · line 2933
Quoted 10 lines
kernel/module/main.c +10
-0+if (!codetag_needs_module_section (info->mod, sname, shdr->sh_size)) +continue ; + + dest =codetag_alloc_module_section (info->mod, sname, shdr->sh_size, +arch_mod_section_prepend (info->mod, i), shdr->sh_addralign); +if (WARN_ON (!dest)) +return -EINVAL ; +if (dest ==ERR_PTR (-EAGAIN )) +/* Allocate the section as a regular section. */ +continue ;
[Severity: High]
If reserve_module_tags() returns -EAGAIN due to reaching the tag addressable limit, the module load proceeds by falling back to regular module memory. However, does this cause a permanent percpu memory leak when the module is eventually unloaded?
When the codetag section falls back to regular memory, the module is never added to the mod_area_mt maple tree. Later, load_module() in mm/alloc_tag.c unconditionally allocates percpu memory for all tags in the module:
mm/alloc_tag.c:load_module() {
...
for (tag = start_tag; tag < stop_tag; tag++) {
...
tag->counters = alloc_percpu(struct alloc_tag_counters);
}
}During unload, release_module_tags() attempts to free these counters. But since the module was never added to the maple tree, it exits early before cleaning up the counters:
mm/alloc_tag.c:release_module_tags() {
...
mas_for_each_rev(&mas, val, 0)
if (val == mod)
break;
if (!val) /* module not found */
goto out;
...
}Will this cause all percpu counters allocated for this module to be leaked, potentially leading to percpu memory exhaustion over time?
Quoted 12 lines
kernel/module/main.c +12
-0+if (IS_ERR (dest)) +return PTR_ERR (dest); + +if (shdr->sh_type !=SHT_NOBITS ) +memcpy (dest, (void *)shdr->sh_addr, shdr->sh_size); + shdr->sh_addr = (unsigned long )dest; + shdr->sh_flags &= ~(unsigned long )SHF_ALLOC ; + shdr->sh_entsize =SH_ENTSIZE_STANDALONE ; + } + +return 0 ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Petr Pavlu
maintainerQuoted 48 lines · Hao Ge
Whether a codetag section goes to the codetag region is decided by
layout_sections()and asked again inmove_module(). A concurrent load can shut profiling down in between, andmove_module()then copies the section to offset 0 of its regular destination, overwriting whatever is there.Decide and allocate in one pass, before the layout. Allocation errors fail the load. On a tag area overflow profiling is already disabled, so
-EAGAINmakes the section fall back to regular module data and the module still loads.The overflow and populate failure paths of
reserve_module_tags()now release their reservation instead of leaking the maple tree entry. When profiling was toggled off, the overflow check did not run, a module could load with more tags than the page flags can address, and re-enabling profiling then silently corrupted/proc/allocinfo. The check no longer depends onmem_alloc_profiling_enabled().Fixes 4835f747d3ed ("alloc_tag: support for page allocation tag compression")Reported-by Sashiko <[email protected]>Based-on-a-patch-by Petr Pavlu <[email protected]>Cc Suren Baghdasaryan <[email protected]>Signed-off-by Hao Ge <[email protected]>---
Changes against Petr's prototype:
-allocate_codetag_sections()returns an error instead of void, and only-EAGAINfalls back to a regular section. Any other error now fails the load. The prototype fell back on everything, which can leave live tags in module memory.
-reserve_module_tags()releases its reservation when populate fails too, that path used to leak the maple tree entry.
-codetag_free_module_sections()on themove_module()error path usesinfo->mod, the local mod is assigned only after a successful move.
- The percpu section is marked only when index.pcpu != 0, otherwise sechdrs[0] gets marked.
- Dropped theSHF_ALLOCcheck, .codetag.* sections always have it.--- include/linux/module.h | 2 + kernel/module/internal.h | 4 ++ kernel/module/main.c | 120 ++++++++++++++++++++------------------- mm/alloc_tag.c | 9 ++- 4 files changed, 75 insertions(+), 60 deletions(-)include/linux/module.h +0
-0
include/linux/module.h enum mod_mem_type { · line 332
Quoted 8 lines
include/linux/module.h +2
-0@@ -325,6 +325,8 @@ enum mod_mem_type {MOD_INIT_RODATA ,MOD_MEM_NUM_TYPES , + +MOD_STANDALONE =-2 , MOD_INVALID = -1, };
It might be better to split this patch into two: the first to introduce MOD_STANDALONE and use it only for the percpu section, and the second with all the codetag-related changes.
The introduction of SH_ENTSIZE_STANDALONE should also allow us to clean up the current resetting of SHF_ALLOC for the percpu section, and now also for codetag sections. The problem is that find_sec(".data..percpu") can currently return different results depending on whether it is called before layout_and_allocate() or later. In addition, apply_relocations() needs a special case for SH_ENTSIZE_STANDALONE, where it could otherwise just test SHF_ALLOC.
Instead of resetting SHF_ALLOC for percpu/codetag sections, both __layout_sections() and move_module() can check for SH_ENTSIZE_STANDALONE to determine whether a section is handled specially and should be skipped.
I think it would be useful to include this change in the first patch introducing MOD_STANDALONE, but I'm also ok with the current version. I can send a separate patch later to make more use of SH_ENTSIZE_STANDALONE in this way.
include/linux/module.h static struct module *layout_and_allocate(struct load_info *info, int flags) · line 2973
Quoted 13 lines
include/linux/module.h +4
-5@@ -2966,18 +2967,23 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)*/ module_mark_ro_after_init (info->hdr, info->sechdrs, info->secstrings); -/* -* Determine total sizes, and put offsets in sh_entsize. For now -* this is done generically; there doesn't appear to be any -* special cases for the architectures. -*/ +/* Allow codetag sections to be allocated separately first. */ + err =allocate_codetag_sections (info); +if (err) { +codetag_free_module_sections (info->mod);
The usual convention is that functions clean up after themselves on error. That means this codetag_free_module_sections() call should be done ideally by allocate_codetag_sections().
Signature · 2 lines
Thanks, Petr
Suren Baghdasaryan
maintainerQuoted 76 lines · Petr Pavlu
Quoted 48 lines · Hao Ge
Whether a codetag section goes to the codetag region is decided by
layout_sections()and asked again inmove_module(). A concurrent load can shut profiling down in between, andmove_module()then copies the section to offset 0 of its regular destination, overwriting whatever is there.Decide and allocate in one pass, before the layout. Allocation errors fail the load. On a tag area overflow profiling is already disabled, so
-EAGAINmakes the section fall back to regular module data and the module still loads.The overflow and populate failure paths of
reserve_module_tags()now release their reservation instead of leaking the maple tree entry. When profiling was toggled off, the overflow check did not run, a module could load with more tags than the page flags can address, and re-enabling profiling then silently corrupted/proc/allocinfo. The check no longer depends onmem_alloc_profiling_enabled().Fixes 4835f747d3ed ("alloc_tag: support for page allocation tag compression")Reported-by Sashiko <[email protected]>Based-on-a-patch-by Petr Pavlu <[email protected]>Cc Suren Baghdasaryan <[email protected]>Signed-off-by Hao Ge <[email protected]>---
Changes against Petr's prototype:
-allocate_codetag_sections()returns an error instead of void, and only-EAGAINfalls back to a regular section. Any other error now fails the load. The prototype fell back on everything, which can leave live tags in module memory.
-reserve_module_tags()releases its reservation when populate fails too, that path used to leak the maple tree entry.
-codetag_free_module_sections()on themove_module()error path usesinfo->mod, the local mod is assigned only after a successful move.
- The percpu section is marked only when index.pcpu != 0, otherwise sechdrs[0] gets marked.
- Dropped theSHF_ALLOCcheck, .codetag.* sections always have it.--- include/linux/module.h | 2 + kernel/module/internal.h | 4 ++ kernel/module/main.c | 120 ++++++++++++++++++++------------------- mm/alloc_tag.c | 9 ++- 4 files changed, 75 insertions(+), 60 deletions(-)include/linux/module.h +0
-0include/linux/module.h enum mod_mem_type { · line 332
Quoted 8 lines
include/linux/module.h +2
-0@@ -325,6 +325,8 @@ enum mod_mem_type {MOD_INIT_RODATA ,MOD_MEM_NUM_TYPES , + +MOD_STANDALONE =-2 , MOD_INVALID = -1, };It might be better to split this patch into two: the first to introduce
MOD_STANDALONEand use it only for the percpu section, and the second with all the codetag-related changes.The introduction of
SH_ENTSIZE_STANDALONEshould also allow us to clean up the current resetting ofSHF_ALLOCfor the percpu section, and now also for codetag sections. The problem is thatfind_sec(".data..percpu")can currently return different results depending on whether it is called beforelayout_and_allocate()or later. In addition,apply_relocations()needs a special case forSH_ENTSIZE_STANDALONE, where it could otherwise just testSHF_ALLOC.Instead of resetting
SHF_ALLOCfor percpu/codetag sections, both__layout_sections()andmove_module()can check forSH_ENTSIZE_STANDALONEto determine whether a section is handled specially and should be skipped.
Quoted 4 lines
I think it would be useful to include this change in the first patch introducing
MOD_STANDALONE, but I'm also ok with the current version. I can send a separate patch later to make more use ofSH_ENTSIZE_STANDALONEin this way.
I ran some tests on my side and nothing blew up.
Petr's suggestion to split the patch sounds good to me and release_module_tags() change in alloc_tag.c could also be done in a separate patch. It's the cleanup after we do shutdown_mem_profiling(), so I think it would be correct on its own.
Thanks,
Suren.
Quoted 22 lines
Quoted 4 lines
include/linux/module.h +0
-0@@ -2966,18 +2967,23 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)*/ module_mark_ro_after_init (info->hdr, info->sechdrs, info->secstrings);include/linux/module.h static struct module *layout_and_allocate(struct load_info *info, int flags) · line 2973
Quoted 10 lines
include/linux/module.h +4
-5-/* -* Determine total sizes, and put offsets in sh_entsize. For now -* this is done generically; there doesn't appear to be any -* special cases for the architectures. -*/ +/* Allow codetag sections to be allocated separately first. */ + err =allocate_codetag_sections (info); +if (err) { +codetag_free_module_sections (info->mod);The usual convention is that functions clean up after themselves on error. That means this
codetag_free_module_sections()call should be done ideally byallocate_codetag_sections().--
Thanks,
Petr
Hao Ge
reviewerHi Petr
Thanks for review.
Quoted 51 lines · Petr Pavlu
Quoted 50 lines · Hao Ge
Whether a codetag section goes to the codetag region is decided by
layout_sections()and asked again inmove_module(). A concurrent load can shut profiling down in between, andmove_module()then copies the section to offset 0 of its regular destination, overwriting whatever is there.Decide and allocate in one pass, before the layout. Allocation errors fail the load. On a tag area overflow profiling is already disabled, so
-EAGAINmakes the section fall back to regular module data and the module still loads.The overflow and populate failure paths of
reserve_module_tags()now release their reservation instead of leaking the maple tree entry. When profiling was toggled off, the overflow check did not run, a module could load with more tags than the page flags can address, and re-enabling profiling then silently corrupted/proc/allocinfo. The check no longer depends onmem_alloc_profiling_enabled().Fixes 4835f747d3ed ("alloc_tag: support for page allocation tag compression")Reported-by Sashiko <[email protected]>Based-on-a-patch-by Petr Pavlu <[email protected]>Cc Suren Baghdasaryan <[email protected]>Signed-off-by Hao Ge <[email protected]>---
Changes against Petr's prototype:
-allocate_codetag_sections()returns an error instead of void, and only-EAGAINfalls back to a regular section. Any other error now fails the load. The prototype fell back on everything, which can leave live tags in module memory.
-reserve_module_tags()releases its reservation when populate fails too, that path used to leak the maple tree entry.
-codetag_free_module_sections()on themove_module()error path usesinfo->mod, the local mod is assigned only after a successful move.
- The percpu section is marked only when index.pcpu != 0, otherwise sechdrs[0] gets marked.
- Dropped theSHF_ALLOCcheck, .codetag.* sections always have it.--- include/linux/module.h | 2 + kernel/module/internal.h | 4 ++ kernel/module/main.c | 120 ++++++++++++++++++++------------------- mm/alloc_tag.c | 9 ++- 4 files changed, 75 insertions(+), 60 deletions(-)include/linux/module.h +0
-0@@ -325,6 +325,8 @@ enum mod_mem_type { MOD_INIT_RODATA,
Quoted 11 lines
include/linux/module.h enum mod_mem_type { · line 332
Quoted 6 lines
include/linux/module.h +2
-0MOD_MEM_NUM_TYPES , + +MOD_STANDALONE =-2 , MOD_INVALID = -1, };It might be better to split this patch into two: the first to introduce
MOD_STANDALONEand use it only for the percpu section, and the second with all the codetag-related changes.
OK, will do for the next version.
Quoted 3 lines
The introduction of
SH_ENTSIZE_STANDALONEshould also allow us to clean up the current resetting ofSHF_ALLOCfor the percpu section, and now also for codetag sections. The problem is thatfind_sec(".data..percpu")
I also like that this keeps SHF_ALLOC semantics closer to the ELF spec.
https://www.sco.com/developers/gabi/latest/ch4.sheader.html
SHF_ALLOC
The section occupies memory during process execution. Some control sections do not reside in the memory image of an object file; this attribute is off for those sections.
(I spent some time reading up on this, so hopefully I've understood it correctly.)
So it feels like we were taking a bit of a shortcut before. These sections really do satisfy the SHF_ALLOC property by definition.
There were probably other reasons for doing it that way back then, but either‑way I think adding SH_ENTSIZE_STANDALONE is a much cleaner solution.
Quoted 5 lines
can currently return different results depending on whether it is called before
layout_and_allocate()or later. In addition,apply_relocations()needs a special case forSH_ENTSIZE_STANDALONE, where it could otherwise just testSHF_ALLOC.
Yeah, fair point.
Quoted 5 lines
Instead of resetting
SHF_ALLOCfor percpu/codetag sections, both__layout_sections()andmove_module()can check forSH_ENTSIZE_STANDALONEto determine whether a section is handled specially and should be skipped.
Right, While working on the code, I noticed that __layout_sections already has a check for this.
if ((s->sh_flags & masks[m][0]) != masks[m][0]
|| (s->sh_flags & masks[m][1])
|| s->sh_entsize != ~0UL
|| is_init != module_init_layout_section(sname))
continue;s->sh_entsize != ~0UL can be used to handle this.
I'll add a short comment here.
Quoted 4 lines
I think it would be useful to include this change in the first patch introducing
MOD_STANDALONE, but I'm also ok with the current version. I can send a separate patch later to make more use ofSH_ENTSIZE_STANDALONEin this way.
So cool.
Quoted 5 lines
Quoted 5 lines
include/linux/module.h +0
-0@@ -2966,18 +2967,23 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)*/ module_mark_ro_after_init (info->hdr, info->sechdrs, info->secstrings);
Quoted 14 lines
include/linux/module.h static struct module *layout_and_allocate(struct load_info *info, int flags) · line 2973
Quoted 9 lines
include/linux/module.h +4
-5-/* -* Determine total sizes, and put offsets in sh_entsize. For now -* this is done generically; there doesn't appear to be any -* special cases for the architectures. -*/ +/* Allow codetag sections to be allocated separately first. */ + err =allocate_codetag_sections (info); +if (err) { +codetag_free_module_sections (info->mod);The usual convention is that functions clean up after themselves on error. That means this
codetag_free_module_sections()call should be done ideally byallocate_codetag_sections().
Ack.
Thanks
Best Regards
Hao
Hao Ge
reviewerHi Suren
Thanks for your review.
Quoted 77 lines · Suren Baghdasaryan
Quoted 76 lines · Petr Pavlu
Quoted 48 lines · Hao Ge
Whether a codetag section goes to the codetag region is decided by
layout_sections()and asked again inmove_module(). A concurrent load can shut profiling down in between, andmove_module()then copies the section to offset 0 of its regular destination, overwriting whatever is there.Decide and allocate in one pass, before the layout. Allocation errors fail the load. On a tag area overflow profiling is already disabled, so
-EAGAINmakes the section fall back to regular module data and the module still loads.The overflow and populate failure paths of
reserve_module_tags()now release their reservation instead of leaking the maple tree entry. When profiling was toggled off, the overflow check did not run, a module could load with more tags than the page flags can address, and re-enabling profiling then silently corrupted/proc/allocinfo. The check no longer depends onmem_alloc_profiling_enabled().Fixes 4835f747d3ed ("alloc_tag: support for page allocation tag compression")Reported-by Sashiko <[email protected]>Based-on-a-patch-by Petr Pavlu <[email protected]>Cc Suren Baghdasaryan <[email protected]>Signed-off-by Hao Ge <[email protected]>---
Changes against Petr's prototype:
-allocate_codetag_sections()returns an error instead of void, and only-EAGAINfalls back to a regular section. Any other error now fails the load. The prototype fell back on everything, which can leave live tags in module memory.
-reserve_module_tags()releases its reservation when populate fails too, that path used to leak the maple tree entry.
-codetag_free_module_sections()on themove_module()error path usesinfo->mod, the local mod is assigned only after a successful move.
- The percpu section is marked only when index.pcpu != 0, otherwise sechdrs[0] gets marked.
- Dropped theSHF_ALLOCcheck, .codetag.* sections always have it.--- include/linux/module.h | 2 + kernel/module/internal.h | 4 ++ kernel/module/main.c | 120 ++++++++++++++++++++------------------- mm/alloc_tag.c | 9 ++- 4 files changed, 75 insertions(+), 60 deletions(-)include/linux/module.h +0
-0include/linux/module.h enum mod_mem_type { · line 332
Quoted 8 lines
include/linux/module.h +2
-0@@ -325,6 +325,8 @@ enum mod_mem_type {MOD_INIT_RODATA ,MOD_MEM_NUM_TYPES , + +MOD_STANDALONE =-2 , MOD_INVALID = -1, };It might be better to split this patch into two: the first to introduce
MOD_STANDALONEand use it only for the percpu section, and the second with all the codetag-related changes.The introduction of
SH_ENTSIZE_STANDALONEshould also allow us to clean up the current resetting ofSHF_ALLOCfor the percpu section, and now also for codetag sections. The problem is thatfind_sec(".data..percpu")can currently return different results depending on whether it is called beforelayout_and_allocate()or later. In addition,apply_relocations()needs a special case forSH_ENTSIZE_STANDALONE, where it could otherwise just testSHF_ALLOC.Instead of resetting
SHF_ALLOCfor percpu/codetag sections, both__layout_sections()andmove_module()can check forSH_ENTSIZE_STANDALONEto determine whether a section is handled specially and should be skipped.
Quoted 7 lines
Quoted 4 lines
I think it would be useful to include this change in the first patch introducing
MOD_STANDALONE, but I'm also ok with the current version. I can send a separate patch later to make more use ofSH_ENTSIZE_STANDALONEin this way.I ran some tests on my side and nothing blew up.
Thanks.
Quoted 4 lines
Petr's suggestion to split the patch sounds good to me and
release_module_tags()change inalloc_tag.ccould also be done in a separate patch. It's the cleanup after we doshutdown_mem_profiling(), so I think it would be correct on its own.
OK, if I understand you correctly, you'd like the release_module_tags() call on vm_module_tags_populate() failure to go into its own separate patch. That's because our -EAGAIN fallback depends on the reserve_module_tags() change. Without it the overflow entry stays in the maple tree, codetag_module_replaced() retargets it at the live module, and rmmod then walks the never-populated tag area and faults.
Thanks
Best Regards
Hao
Quoted 25 lines
Thanks,
Suren.Quoted 22 lines
Quoted 4 lines
include/linux/module.h +0
-0@@ -2966,18 +2967,23 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)*/ module_mark_ro_after_init (info->hdr, info->sechdrs, info->secstrings);include/linux/module.h static struct module *layout_and_allocate(struct load_info *info, int flags) · line 2973
Quoted 10 lines
include/linux/module.h +4
-5-/* -* Determine total sizes, and put offsets in sh_entsize. For now -* this is done generically; there doesn't appear to be any -* special cases for the architectures. -*/ +/* Allow codetag sections to be allocated separately first. */ + err =allocate_codetag_sections (info); +if (err) { +codetag_free_module_sections (info->mod);The usual convention is that functions clean up after themselves on error. That means this
codetag_free_module_sections()call should be done ideally byallocate_codetag_sections().--
Thanks,
Petr