Skip to contentSkip to conversation

modules: wait do_free_init correctly

PATCH RESEND Superseded Changbin Du 3 participants 6 messages Jan 29, 2024 – Feb 17, 2024 Module support

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND v2 1/1 modules: wait do_free_init correctly

The commit 1a7b7d922081 ("modules: Use vmalloc special flag") moves do_free_init() into a global workqueue instead of call_rcu(). So now rcu_barrier() can not ensure that do_free_init has completed. We should wait it via flush_work().

Without this fix, we still could encounter false positive reports in W+X checking, and rcu synchronization is unnecessary.

Fixes 1a7b7d922081 ("modules: Use vmalloc special flag")
Cc Xiaoyi Su <[email protected]>

---
v2: fix compilation issue for no CONFIG_MODULES found by 0-DAY.

---
 include/linux/moduleloader.h | 8 ++++++++
 init/main.c                  | 5 +++--
 kernel/module/main.c         | 5 +++++
 3 files changed, 16 insertions(+), 2 deletions(-)
include/linux/moduleloader.h +8 -0
index 001b2ce83832..89b1e0ed9811 100644
@@ -115,6 +115,14 @@ int module_finalize(const Elf_Ehdr *hdr,
 		    const Elf_Shdr *sechdrs,
 		    struct module *mod);
 
+#ifdef CONFIG_MODULES
+void flush_module_init_free_work(void);
+#else
+static inline void flush_module_init_free_work(void)
+{
+}
+#endif
+
 /* Any cleanup needed when module leaves. */
 void module_arch_cleanup(struct module *mod);
 
init/main.c +3 -2
index e24b0780fdff..f0b7e21ac67f 100644
@@ -99,6 +99,7 @@
 #include <linux/init_syscalls.h>
 #include <linux/stackdepot.h>
 #include <linux/randomize_kstack.h>
+#include <linux/moduleloader.h>
 #include <net/net_namespace.h>
 
 #include <asm/io.h>
@@ -1402,11 +1403,11 @@ static void mark_readonly(void)
 	if (rodata_enabled) {
 		/*
 		 * load_module() results in W+X mappings, which are cleaned
-		 * up with call_rcu().  Let's make sure that queued work is
+		 * up with init_free_wq. Let's make sure that queued work is
 		 * flushed so that we don't hit false positives looking for
 		 * insecure pages which are W+X.
 		 */
-		rcu_barrier();
+		flush_module_init_free_work();
 		mark_rodata_ro();
 		rodata_test();
 	} else
kernel/module/main.c +5 -0
index 36681911c05a..ea66b5c2a2a1 100644
@@ -2489,6 +2489,11 @@ static void do_free_init(struct work_struct *w)
 	}
 }
 
+void flush_module_init_free_work(void)
+{
+	flush_work(&init_free_wq);
+}
+
 #undef MODULE_PARAM_PREFIX
 #define MODULE_PARAM_PREFIX "module."
 /* Default value for module->async_probe_requested */
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 7 lines · Changbin Du

The commit 1a7b7d922081 ("modules: Use vmalloc special flag") moves do_free_init() into a global workqueue instead of call_rcu(). So now rcu_barrier() can not ensure that do_free_init has completed. We should wait it via flush_work().

Without this fix, we still could encounter false positive reports in W+X checking, and rcu synchronization is unnecessary.

You didn't answer my question, which should be documented in the commit log.

Does this mean we never freed modules init because of this? If so then your commit log should clearly explain that. It should also explain that if true (you have to verify) then it means we were no longer saving the memory we wished to save, and that is important for distributions which do want to save anything on memory. You may want to do a general estimate on how much that means these days on any desktop / server.

Luis

#2 lore raw
Quoted 9 lines · Luis Chamberlain
Quoted 7 lines · Changbin Du

The commit 1a7b7d922081 ("modules: Use vmalloc special flag") moves do_free_init() into a global workqueue instead of call_rcu(). So now rcu_barrier() can not ensure that do_free_init has completed. We should wait it via flush_work().

Without this fix, we still could encounter false positive reports in W+X checking, and rcu synchronization is unnecessary.

Quoted 9 lines

You didn't answer my question, which should be documented in the commit log.

Does this mean we never freed modules init because of this? If so then your commit log should clearly explain that. It should also explain that if true (you have to verify) then it means we were no longer saving the memory we wished to save, and that is important for distributions which do want to save anything on memory. You may want to do a general estimate on how much that means these days on any desktop / server.

Actually, I have explained it in commit msg. It's not about saving memory. The synchronization here is just to ensure the module init's been freed before doing W+X checking. The problem is that the current implementation is wrong, rcu_barrier() cannot guarantee that. So we can encounter false positive reports. But anyway, the module init will be freed, and it's just a timing related issue.

Quoted 1 line

Luis

Signature · 2 lines
Cheers,
Changbin Du
maintainer
#3 lore raw
Quoted 19 lines · Changbin Du
Quoted 9 lines · Luis Chamberlain
Quoted 7 lines · Changbin Du

The commit 1a7b7d922081 ("modules: Use vmalloc special flag") moves do_free_init() into a global workqueue instead of call_rcu(). So now rcu_barrier() can not ensure that do_free_init has completed. We should wait it via flush_work().

Without this fix, we still could encounter false positive reports in W+X checking, and rcu synchronization is unnecessary.

Quoted 8 lines

You didn't answer my question, which should be documented in the commit log.

Does this mean we never freed modules init because of this? If so then your commit log should clearly explain that. It should also explain that if true (you have to verify) then it means we were no longer saving the memory we wished to save, and that is important for distributions which do want to save anything on memory. You may want to do a general estimate on how much that means these days on any desktop / server.

Quoted 5 lines

Actually, I have explained it in commit msg. It's not about saving memory. The synchronization here is just to ensure the module init's been freed before doing W+X checking. The problem is that the current implementation is wrong, rcu_barrier() cannot guarantee that. So we can encounter false positive reports. But anyway, the module init will be freed, and it's just a timing related issue.

Your desciption here is better than the commit log.

Luis

#4 lore raw
Quoted 10 lines · Luis Chamberlain
Quoted 9 lines · Changbin Du
Quoted 8 lines · Luis Chamberlain
Quoted 7 lines · Changbin Du

The commit 1a7b7d922081 ("modules: Use vmalloc special flag") moves do_free_init() into a global workqueue instead of call_rcu(). So now rcu_barrier() can not ensure that do_free_init has completed. We should wait it via flush_work().

Without this fix, we still could encounter false positive reports in W+X checking, and rcu synchronization is unnecessary.

The comment in do_init_module(), just before schedule_work(&init_free_wq), mentioning rcu_barrier(), should be amended as well.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

You didn't answer my question, which should be documented in the commit log.

Does this mean we never freed modules init because of this? If so then your commit log should clearly explain that. It should also explain that if true (you have to verify) then it means we were no longer saving the memory we wished to save, and that is important for distributions which do want to save anything on memory. You may want to do a general estimate on how much that means these days on any desktop / server.

Quoted 7 lines
Quoted 5 lines

Actually, I have explained it in commit msg. It's not about saving memory. The synchronization here is just to ensure the module init's been freed before doing W+X checking. The problem is that the current implementation is wrong, rcu_barrier() cannot guarantee that. So we can encounter false positive reports. But anyway, the module init will be freed, and it's just a timing related issue.

Your desciption here is better than the commit log.

I saw this problem using a PREEMPT_RT kernel as well. Setting DEBUG_WX=n stills show a significant delay due to the rcu_barrier:

  [    0.291444] Freeing unused kernel memory: 5568K
  [    0.402442] Run /sbin/init as init process

The same delay is shorter using linux-next, but still noticeable (DEBUG_WX=n):

  [    0.384362] Freeing unused kernel memory: 14080K
  [    0.413423] Run /sbin/init as init process

Matching trace_event=rcu:rcu_barrier trace:

         systemd-1       [002] .....     0.384391: rcu_barrier: rcu_preempt Begin cpu -1 remaining 0 # 4
         systemd-1       [002] d..1.     0.384394: rcu_barrier: rcu_preempt Inc1 cpu -1 remaining 0 # 1
         systemd-1       [002] .....     0.384395: rcu_barrier: rcu_preempt NQ cpu 0 remaining 2 # 1
          <idle>-0       [001] d.h2.     0.384407: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 2 # 1
         systemd-1       [002] .....     0.384408: rcu_barrier: rcu_preempt OnlineQ cpu 1 remaining 3 # 1
         systemd-1       [002] .....     0.384409: rcu_barrier: rcu_preempt NQ cpu 2 remaining 3 # 1
          <idle>-0       [003] d.h2.     0.384416: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 3 # 1
         systemd-1       [002] .....     0.384418: rcu_barrier: rcu_preempt OnlineQ cpu 3 remaining 4 # 1
          <idle>-0       [004] d.h2.     0.384428: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 4 # 1
         systemd-1       [002] .....     0.384430: rcu_barrier: rcu_preempt OnlineQ cpu 4 remaining 5 # 1
          <idle>-0       [005] d.h2.     0.384438: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 5 # 1
         systemd-1       [002] .....     0.384441: rcu_barrier: rcu_preempt OnlineQ cpu 5 remaining 6 # 1
          <idle>-0       [006] d.h2.     0.384450: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 6 # 1
         systemd-1       [002] .....     0.384452: rcu_barrier: rcu_preempt OnlineQ cpu 6 remaining 7 # 1
          <idle>-0       [007] d.h2.     0.384461: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 7 # 1
         systemd-1       [002] .....     0.384463: rcu_barrier: rcu_preempt OnlineQ cpu 7 remaining 8 # 1
          <idle>-0       [004] ..s1.     0.385339: rcu_barrier: rcu_preempt CB cpu -1 remaining 5 # 1
          <idle>-0       [007] ..s1.     0.397335: rcu_barrier: rcu_preempt CB cpu -1 remaining 4 # 1
          <idle>-0       [003] ..s1.     0.397337: rcu_barrier: rcu_preempt CB cpu -1 remaining 3 # 1
          <idle>-0       [005] ..s1.     0.401336: rcu_barrier: rcu_preempt CB cpu -1 remaining 2 # 1
          <idle>-0       [006] ..s1.     0.401336: rcu_barrier: rcu_preempt CB cpu -1 remaining 1 # 1
          <idle>-0       [001] .Ns1.     0.413338: rcu_barrier: rcu_preempt LastCB cpu -1 remaining 0 # 1
         systemd-1       [002] .....     0.413351: rcu_barrier: rcu_preempt Inc2 cpu -1 remaining 0 # 1

With this patch the delay is no longer there:

  [    0.377662] Freeing unused kernel memory: 14080K
  [    0.377767] Run /sbin/init as init process

AFAIU, for the race to happen, module_alloc() needs to create a W+X mapping (neither x86 nor arm64 does) and debug_checkwx() has to happen before module_enable_nx() in complete_formation(), I didn't get a reproducer so far.

Best,

Signature · 1 line
Eric Chanudet
#5 lore raw
Quoted 9 lines · Eric Chanudet
Quoted 8 lines · Luis Chamberlain
Quoted 7 lines · Changbin Du
Quoted 6 lines · Luis Chamberlain
Quoted 5 lines · Changbin Du

The commit 1a7b7d922081 ("modules: Use vmalloc special flag") moves do_free_init() into a global workqueue instead of call_rcu(). So now rcu_barrier() can not ensure that do_free_init has completed. We should wait it via flush_work().

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Without this fix, we still could encounter false positive reports in W+X checking, and rcu synchronization is unnecessary.

The comment in do_init_module(), just before schedule_work(&init_free_wq), mentioning rcu_barrier(), should be amended as well.

yes, I'll update it as well.

Quoted 53 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

You didn't answer my question, which should be documented in the commit log.

Does this mean we never freed modules init because of this? If so then your commit log should clearly explain that. It should also explain that if true (you have to verify) then it means we were no longer saving the memory we wished to save, and that is important for distributions which do want to save anything on memory. You may want to do a general estimate on how much that means these days on any desktop / server.

Quoted 7 lines
Quoted 5 lines

Actually, I have explained it in commit msg. It's not about saving memory. The synchronization here is just to ensure the module init's been freed before doing W+X checking. The problem is that the current implementation is wrong, rcu_barrier() cannot guarantee that. So we can encounter false positive reports. But anyway, the module init will be freed, and it's just a timing related issue.

Your desciption here is better than the commit log.

I saw this problem using a PREEMPT_RT kernel as well. Setting DEBUG_WX=n stills show a significant delay due to the rcu_barrier:

  [    0.291444] Freeing unused kernel memory: 5568K
  [    0.402442] Run /sbin/init as init process

The same delay is shorter using linux-next, but still noticeable (DEBUG_WX=n):

  [    0.384362] Freeing unused kernel memory: 14080K
  [    0.413423] Run /sbin/init as init process

Matching trace_event=rcu:rcu_barrier trace:

         systemd-1       [002] .....     0.384391: rcu_barrier: rcu_preempt Begin cpu -1 remaining 0 # 4
         systemd-1       [002] d..1.     0.384394: rcu_barrier: rcu_preempt Inc1 cpu -1 remaining 0 # 1
         systemd-1       [002] .....     0.384395: rcu_barrier: rcu_preempt NQ cpu 0 remaining 2 # 1
          <idle>-0       [001] d.h2.     0.384407: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 2 # 1
         systemd-1       [002] .....     0.384408: rcu_barrier: rcu_preempt OnlineQ cpu 1 remaining 3 # 1
         systemd-1       [002] .....     0.384409: rcu_barrier: rcu_preempt NQ cpu 2 remaining 3 # 1
          <idle>-0       [003] d.h2.     0.384416: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 3 # 1
         systemd-1       [002] .....     0.384418: rcu_barrier: rcu_preempt OnlineQ cpu 3 remaining 4 # 1
          <idle>-0       [004] d.h2.     0.384428: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 4 # 1
         systemd-1       [002] .....     0.384430: rcu_barrier: rcu_preempt OnlineQ cpu 4 remaining 5 # 1
          <idle>-0       [005] d.h2.     0.384438: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 5 # 1
         systemd-1       [002] .....     0.384441: rcu_barrier: rcu_preempt OnlineQ cpu 5 remaining 6 # 1
          <idle>-0       [006] d.h2.     0.384450: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 6 # 1
         systemd-1       [002] .....     0.384452: rcu_barrier: rcu_preempt OnlineQ cpu 6 remaining 7 # 1
          <idle>-0       [007] d.h2.     0.384461: rcu_barrier: rcu_preempt IRQ cpu -1 remaining 7 # 1
         systemd-1       [002] .....     0.384463: rcu_barrier: rcu_preempt OnlineQ cpu 7 remaining 8 # 1
          <idle>-0       [004] ..s1.     0.385339: rcu_barrier: rcu_preempt CB cpu -1 remaining 5 # 1
          <idle>-0       [007] ..s1.     0.397335: rcu_barrier: rcu_preempt CB cpu -1 remaining 4 # 1
          <idle>-0       [003] ..s1.     0.397337: rcu_barrier: rcu_preempt CB cpu -1 remaining 3 # 1
          <idle>-0       [005] ..s1.     0.401336: rcu_barrier: rcu_preempt CB cpu -1 remaining 2 # 1
          <idle>-0       [006] ..s1.     0.401336: rcu_barrier: rcu_preempt CB cpu -1 remaining 1 # 1
          <idle>-0       [001] .Ns1.     0.413338: rcu_barrier: rcu_preempt LastCB cpu -1 remaining 0 # 1
         systemd-1       [002] .....     0.413351: rcu_barrier: rcu_preempt Inc2 cpu -1 remaining 0 # 1
Quoted 4 lines

With this patch the delay is no longer there:

  [    0.377662] Freeing unused kernel memory: 14080K
  [    0.377767] Run /sbin/init as init process

Thanks for your info. We encounter similar delay in our scenario. I'll add your testing data in commit msg.

Quoted 10 lines

AFAIU, for the race to happen, module_alloc() needs to create a W+X mapping (neither x86 nor arm64 does) and debug_checkwx() has to happen before module_enable_nx() in complete_formation(), I didn't get a reproducer so far.

Best,

Signature · 1 line
Eric Chanudet
Signature · 2 lines
Cheers,
Changbin Du