linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] module: check for exit sections in layout_sections() instead of module_init_section()
@ 2021-05-14 16:09 Jessica Yu
  2021-05-14 17:09 ` Russell King (Oracle)
  0 siblings, 1 reply; 3+ messages in thread
From: Jessica Yu @ 2021-05-14 16:09 UTC (permalink / raw)
  To: linux-kernel; +Cc: Peter Zijlstra, Russell King, Jessica Yu

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.

Fixes: 33121347fb1c ("module: treat exit sections the same as init sections when !CONFIG_MODULE_UNLOAD")
Signed-off-by: Jessica Yu <jeyu@kernel.org>
---

v2:
    - Use a helper function to make reading the conditionals in
      layout_sections easier (Russell King)

I named the helper 'module_init_layout_section' since we're trying to sort
sections into either the module core region or init region, which I think
is slightly more descriptive.

 kernel/module.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/kernel/module.c b/kernel/module.c
index faf9114a9981..1d0e59f95a9a 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2400,6 +2400,15 @@ static long get_offset(struct module *mod, unsigned int *size,
 	return ret;
 }
 
+static bool module_init_layout_section(const char *sname)
+{
+#ifndef CONFIG_MODULE_UNLOAD
+	if (module_exit_section(sname))
+		return true;
+#endif
+	return module_init_section(sname);
+}
+
 /*
  * Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  * might -- code, read-only data, read-write data, small data.  Tally
@@ -2434,7 +2443,7 @@ 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
-			    || module_init_section(sname))
+			    || module_init_layout_section(sname))
 				continue;
 			s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
 			pr_debug("\t%s\n", sname);
@@ -2467,7 +2476,7 @@ 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
-			    || !module_init_section(sname))
+			    || !module_init_layout_section(sname))
 				continue;
 			s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
 					 | INIT_OFFSET_MASK);
@@ -2806,11 +2815,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)
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] module: check for exit sections in layout_sections() instead of module_init_section()
  2021-05-14 16:09 [PATCH v2] module: check for exit sections in layout_sections() instead of module_init_section() Jessica Yu
@ 2021-05-14 17:09 ` Russell King (Oracle)
  2021-05-17  7:56   ` Jessica Yu
  0 siblings, 1 reply; 3+ messages in thread
From: Russell King (Oracle) @ 2021-05-14 17:09 UTC (permalink / raw)
  To: Jessica Yu; +Cc: linux-kernel, Peter Zijlstra

On Fri, May 14, 2021 at 06:09:04PM +0200, Jessica Yu wrote:
> 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.
> 
> Fixes: 33121347fb1c ("module: treat exit sections the same as init sections when !CONFIG_MODULE_UNLOAD")
> Signed-off-by: Jessica Yu <jeyu@kernel.org>

Looks good!

Reviewed-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>

Thanks!

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] module: check for exit sections in layout_sections() instead of module_init_section()
  2021-05-14 17:09 ` Russell King (Oracle)
@ 2021-05-17  7:56   ` Jessica Yu
  0 siblings, 0 replies; 3+ messages in thread
From: Jessica Yu @ 2021-05-17  7:56 UTC (permalink / raw)
  To: Russell King (Oracle); +Cc: linux-kernel, Peter Zijlstra

+++ Russell King (Oracle) [14/05/21 18:09 +0100]:
>On Fri, May 14, 2021 at 06:09:04PM +0200, Jessica Yu wrote:
>> 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.
>>
>> Fixes: 33121347fb1c ("module: treat exit sections the same as init sections when !CONFIG_MODULE_UNLOAD")
>> Signed-off-by: Jessica Yu <jeyu@kernel.org>
>
>Looks good!
>
>Reviewed-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
>
>Thanks!

Queued, thanks for the review Russell!

Jessica

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-17  7:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-14 16:09 [PATCH v2] module: check for exit sections in layout_sections() instead of module_init_section() Jessica Yu
2021-05-14 17:09 ` Russell King (Oracle)
2021-05-17  7:56   ` Jessica Yu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).