module: check for exit sections in layout_sections() instead of module_init_section()
PATCH 1/1 module: check for exit sections in layout_sections() instead of module_init_section()
Previously, when CONFIG_MODULE_UNLOAD=n, the module loader just does not attempt to load exit sections since it never expects that any code in those sections will ever execute. However, dynamic code patching (alternatives, jump_label and static_call) can have sites in __exit code, even if __exit is never executed. Therefore __exit must be present at runtime, at least for as long as __init code is.
Commit 33121347fb1c ("module: treat exit sections the same as init sections when !CONFIG_MODULE_UNLOAD") solves the requirements of jump_labels and static_calls by putting the exit sections in the init region of the module so that they are at least present at init, and discarded afterwards. It does this by including a check for exit sections in module_init_section(), so that it also returns true for exit sections, and the module loader will automatically sort them in the init region of the module.
However, the solution there was not completely arch-independent. ARM is a special case where it supplies its own module_{init, exit}_section() functions. Instead of pushing the exit section checks into module_init_section(), just implement the exit section check in layout_sections(), so that we don't have to touch arch-dependent code.
--- kernel/module.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
kernel/module.c +7 -4
@@ -2430,6 +2430,9 @@ static void layout_sections(struct module *mod, struct load_info *info)if ((s->sh_flags & masks[m][0 ]) != masks[m][0 ] || (s->sh_flags & masks[m][1 ]) || s->sh_entsize != ~0UL +#ifndef CONFIG_MODULE_UNLOAD + ||module_exit_section (sname) +#endif ||module_init_section (sname))continue ; s->sh_entsize =get_offset (mod, &mod->core_layout.size, s, i);
@@ -2463,7 +2466,11 @@ static void layout_sections(struct module *mod, struct load_info *info)if ((s->sh_flags & masks[m][0 ]) != masks[m][0 ] || (s->sh_flags & masks[m][1 ]) || s->sh_entsize != ~0UL +#ifndef CONFIG_MODULE_UNLOAD + || (!module_init_section (sname) && !module_exit_section (sname))) +#else || !module_init_section(sname)) +#endifcontinue ; s->sh_entsize = (get_offset (mod, &mod->init_layout.size, s, i) |INIT_OFFSET_MASK );
@@ -2802,11 +2809,7 @@ void * __weak module_alloc(unsigned long size)bool __weak module_init_section (const char *name) { -#ifndef CONFIG_MODULE_UNLOAD -return strstarts (name,".init" ) ||module_exit_section (name); -#else return strstarts (name,".init" ); -#endif }bool __weak module_exit_section (const char *name)
Signature · 1 line
2.31.1
Hi,
kernel/module.c static void layout_sections(struct module *mod, struct load_info *info) · line 2436
Quoted 12 lines · Jessica Yu
kernel/module.c +3
-0@@ -2430,6 +2430,9 @@ static void layout_sections(struct module *mod, struct load_info *info)if ((s->sh_flags & masks[m][0 ]) != masks[m][0 ] || (s->sh_flags & masks[m][1 ]) || s->sh_entsize != ~0UL +#ifndef CONFIG_MODULE_UNLOAD + ||module_exit_section (sname) +#endif ||module_init_section (sname))
How about a helper to make this a bit easier in both these places to make the code more undertsandable? I think the great value comes from the resulting change in the second hunk.
static bool module_evictable_section(const char *sname) {
#ifndef CONFIG_MODULE_UNLOAD if (module_exit_section(sname)) return true; #endif return module_init_section(sname); }
and then just use that above?
kernel/module.c static void layout_sections(struct module *mod, struct load_info *info) · line 2473
Quoted 11 lines
kernel/module.c +4
-0continue ; s->sh_entsize =get_offset (mod, &mod->core_layout.size, s, i);@@ -2463,7 +2466,11 @@ static void layout_sections(struct module *mod, struct load_info *info)if ((s->sh_flags & masks[m][0 ]) != masks[m][0 ] || (s->sh_flags & masks[m][1 ]) || s->sh_entsize != ~0UL +#ifndef CONFIG_MODULE_UNLOAD + || (!module_init_section (sname) && !module_exit_section (sname))) +#else || !module_init_section(sname)) +#endif
I find this a tad confusing, and this is the reason for my suggestion above. With that, this becomes:
|| !module_evictable_section(sname))
which can be clearly seen is the opposite condition from the above without doing mental logic gymnastics.
Thanks.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
+++ Russell King - ARM Linux admin [12/05/21 17:06 +0100]:
Quoted 46 lines
Hi,
kernel/module.c static void layout_sections(struct module *mod, struct load_info *info) · line 2436
Quoted 12 lines · Jessica Yu
kernel/module.c +3
-0@@ -2430,6 +2430,9 @@ static void layout_sections(struct module *mod, struct load_info *info)if ((s->sh_flags & masks[m][0 ]) != masks[m][0 ] || (s->sh_flags & masks[m][1 ]) || s->sh_entsize != ~0UL +#ifndef CONFIG_MODULE_UNLOAD + ||module_exit_section (sname) +#endif ||module_init_section (sname))How about a helper to make this a bit easier in both these places to make the code more undertsandable? I think the great value comes from the resulting change in the second hunk.
static bool
module_evictable_section(const char *sname){#ifndef CONFIG_MODULE_UNLOAD if (module_exit_section(sname)) return true; #endif return module_init_section(sname); }and then just use that above?
kernel/module.c static void layout_sections(struct module *mod, struct load_info *info) · line 2473
Quoted 11 lines
kernel/module.c +4
-0continue ; s->sh_entsize =get_offset (mod, &mod->core_layout.size, s, i);@@ -2463,7 +2466,11 @@ static void layout_sections(struct module *mod, struct load_info *info)if ((s->sh_flags & masks[m][0 ]) != masks[m][0 ] || (s->sh_flags & masks[m][1 ]) || s->sh_entsize != ~0UL +#ifndef CONFIG_MODULE_UNLOAD + || (!module_init_section (sname) && !module_exit_section (sname))) +#else || !module_init_section(sname)) +#endifI find this a tad confusing, and this is the reason for my suggestion above. With that, this becomes:
Quoted 4 lines
|| !module_evictable_section(sname))which can be clearly seen is the opposite condition from the above without doing mental logic gymnastics.
Thanks Russell for the feedback! Yeah, agreed that it could be made easier to read - will respin with a helper function.
Jessica