* [RFC PATCH] modules: extend {kstrdup,kfree}_const() to handle per-module .rodata
@ 2026-03-27 13:22 Dmitry Antipov
2026-03-31 13:37 ` kernel test robot
0 siblings, 1 reply; 2+ messages in thread
From: Dmitry Antipov @ 2026-03-27 13:22 UTC (permalink / raw)
To: Luis Chamberlain, Petr Pavlu, Daniel Gomez, Sami Tolvanen
Cc: linux-modules, linux-mm, Dmitry Antipov
Since kernel modules has their own .rodata sections, functions like
'kstrdup_const()' called from the module context are not required to
copy string constants from these sections. Likewise, 'kfree_const()'
in such a context becomes a no-op also because the whole module's
.rodata is freed at module unloading. OTOH this proof-of-concept
implementation introduces substantial overhead due to calls to
'__module_address()' from 'is_module_rodata()', which is much slower
than 'is_kernel_rodata()'. Anyway, comments are highly appreciated.
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
include/linux/module.h | 6 ++++++
kernel/module/main.c | 9 +++++++++
mm/util.c | 7 +++++--
3 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/include/linux/module.h b/include/linux/module.h
index 14f391b186c6..7edaf2b730ce 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -623,6 +623,7 @@ bool is_module_address(unsigned long addr);
bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr);
bool is_module_percpu_address(unsigned long addr);
bool is_module_text_address(unsigned long addr);
+bool is_module_rodata(unsigned long addr);
static inline bool within_module_mem_type(unsigned long addr,
const struct module *mod,
@@ -807,6 +808,11 @@ static inline bool is_module_text_address(unsigned long addr)
return false;
}
+static inline bool is_module_rodata(unsigned long addr)
+{
+ return false;
+}
+
static inline bool within_module_core(unsigned long addr,
const struct module *mod)
{
diff --git a/kernel/module/main.c b/kernel/module/main.c
index c3ce106c70af..5003cacd0786 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -3858,6 +3858,15 @@ bool is_module_text_address(unsigned long addr)
return __module_text_address(addr) != NULL;
}
+bool is_module_rodata(unsigned long addr)
+{
+ struct module *mod;
+
+ guard(rcu)();
+ mod = __module_address(addr);
+ return mod && within_module_mem_type(addr, mod, MOD_RODATA);
+}
+
void module_for_each_mod(int(*func)(struct module *mod, void *data), void *data)
{
struct module *mod;
diff --git a/mm/util.c b/mm/util.c
index b05ab6f97e11..8dd1f1e95554 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -26,6 +26,7 @@
#include <linux/compat.h>
#include <linux/fsnotify.h>
#include <linux/page_idle.h>
+#include <linux/module.h>
#include <linux/uaccess.h>
@@ -42,7 +43,8 @@
*/
void kfree_const(const void *x)
{
- if (!is_kernel_rodata((unsigned long)x))
+ if (!is_kernel_rodata((unsigned long)x) &&
+ !is_module_rodata((unsigned long)x))
kfree(x);
}
EXPORT_SYMBOL(kfree_const);
@@ -98,7 +100,8 @@ EXPORT_SYMBOL(kstrdup);
*/
const char *kstrdup_const(const char *s, gfp_t gfp)
{
- if (is_kernel_rodata((unsigned long)s))
+ if (is_kernel_rodata((unsigned long)s) ||
+ is_module_rodata((unsigned long)s))
return s;
return kstrdup(s, gfp);
--
2.53.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [RFC PATCH] modules: extend {kstrdup,kfree}_const() to handle per-module .rodata
2026-03-27 13:22 [RFC PATCH] modules: extend {kstrdup,kfree}_const() to handle per-module .rodata Dmitry Antipov
@ 2026-03-31 13:37 ` kernel test robot
0 siblings, 0 replies; 2+ messages in thread
From: kernel test robot @ 2026-03-31 13:37 UTC (permalink / raw)
To: Dmitry Antipov
Cc: oe-lkp, lkp, linux-modules, linux-kernel, linux-mm,
Luis Chamberlain, Petr Pavlu, Daniel Gomez, Sami Tolvanen,
Dmitry Antipov, oliver.sang
Hello,
kernel test robot noticed "kernel_BUG_at_arch/x86/mm/physaddr.c" on:
commit: 0803ad97505a632fc3d0cddd6659617deb0ff34b ("[RFC PATCH] modules: extend {kstrdup,kfree}_const() to handle per-module .rodata")
url: https://github.com/intel-lab-lkp/linux/commits/Dmitry-Antipov/modules-extend-kstrdup-kfree-_const-to-handle-per-module-rodata/20260329-123400
base: https://git.kernel.org/cgit/linux/kernel/git/akpm/mm.git mm-everything
patch link: https://lore.kernel.org/all/20260327132247.861984-1-dmantipov@yandex.ru/
patch subject: [RFC PATCH] modules: extend {kstrdup,kfree}_const() to handle per-module .rodata
in testcase: boot
config: i386-randconfig-002-20260331
compiler: gcc-14
test machine: qemu-system-i386 -enable-kvm -cpu SandyBridge -smp 2 -m 4G
(please refer to attached dmesg/kmsg for entire log/backtrace)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <oliver.sang@intel.com>
| Closes: https://lore.kernel.org/oe-lkp/202603312130.f9ed8d89-lkp@intel.com
[ 17.209008][ C0] ------------[ cut here ]------------
[ 17.209572][ C0] kernel BUG at arch/x86/mm/physaddr.c:66!
[ 17.210058][ C0] Oops: invalid opcode: 0000 [#1]
[ 17.210470][ C0] CPU: 0 UID: 0 PID: 13 Comm: ksoftirqd/0 Not tainted 7.0.0-rc5-00667-g0803ad97505a #1 PREEMPT(full) 4fef6f10f7892f9689059524fd4d050b8163a9be
[ 17.211614][ C0] EIP: __phys_addr (arch/x86/mm/physaddr.c:66 (discriminator 3))
[ 17.211995][ C0] Code: e5 56 53 85 c0 78 09 0f 0b b8 ec a1 e6 81 eb 21 80 3d f8 e9 4d 82 00 89 c3 8d b0 00 00 00 80 74 15 e8 cf c5 19 00 84 c0 74 0c <0f> 0b b8 e0 a1 e6 81 e8 77 3e fb ff a1 30 14 d3 82 85 c0 74 2a 89
All code
========
0: e5 56 in $0x56,%eax
2: 53 push %rbx
3: 85 c0 test %eax,%eax
5: 78 09 js 0x10
7: 0f 0b ud2
9: b8 ec a1 e6 81 mov $0x81e6a1ec,%eax
e: eb 21 jmp 0x31
10: 80 3d f8 e9 4d 82 00 cmpb $0x0,-0x7db21608(%rip) # 0xffffffff824dea0f
17: 89 c3 mov %eax,%ebx
19: 8d b0 00 00 00 80 lea -0x80000000(%rax),%esi
1f: 74 15 je 0x36
21: e8 cf c5 19 00 call 0x19c5f5
26: 84 c0 test %al,%al
28: 74 0c je 0x36
2a:* 0f 0b ud2 <-- trapping instruction
2c: b8 e0 a1 e6 81 mov $0x81e6a1e0,%eax
31: e8 77 3e fb ff call 0xfffffffffffb3ead
36: a1 30 14 d3 82 85 c0 movabs 0x2a74c08582d31430,%eax
3d: 74 2a
3f: 89 .byte 0x89
Code starting with the faulting instruction
===========================================
0: 0f 0b ud2
2: b8 e0 a1 e6 81 mov $0x81e6a1e0,%eax
7: e8 77 3e fb ff call 0xfffffffffffb3e83
c: a1 30 14 d3 82 85 c0 movabs 0x2a74c08582d31430,%eax
13: 74 2a
15: 89 .byte 0x89
[ 17.214426][ C0] EAX: f7e9f401 EBX: f7e9f4ac ECX: fffff000 EDX: ff5fe000
[ 17.215476][ C0] ESI: 77e9f4ac EDI: 00000000 EBP: 80b0be68 ESP: 80b0be60
[ 17.216539][ C0] DS: 007b ES: 007b FS: 0000 GS: 0000 SS: 0068 EFLAGS: 00010202
[ 17.217741][ C0] CR0: 80050033 CR2: 7fbf9dd8 CR3: 032cd000 CR4: 000406b0
[ 17.218849][ C0] Call Trace:
[ 17.219371][ C0] kfree (mm/slub.c:6474 (discriminator 1))
[ 17.220011][ C0] ? rcu_read_unlock (include/linux/rcupdate.h:884)
[ 17.220786][ C0] kfree_const (mm/util.c:49)
[ 17.221549][ C0] kernfs_free_rcu (fs/kernfs/dir.c:550)
[ 17.222201][ C0] ? lock_is_held (arch/x86/events/intel/core.c:4034)
[ 17.222920][ C0] rcu_do_batch+0x329/0x6a8
[ 17.223812][ C0] rcu_core (kernel/rcu/tree.c:2871)
[ 17.224493][ C0] rcu_core_si (kernel/rcu/tree.c:2887)
[ 17.225255][ C0] handle_softirqs (arch/x86/include/asm/atomic.h:23 include/linux/atomic/atomic-arch-fallback.h:457 include/linux/jump_label.h:262 include/trace/events/irq.h:142 kernel/softirq.c:623)
[ 17.226002][ C0] run_ksoftirqd (kernel/softirq.c:479 kernel/softirq.c:1064 kernel/softirq.c:1055)
[ 17.226737][ C0] smpboot_thread_fn (kernel/smpboot.c:160)
[ 17.227510][ C0] ? sort_range (kernel/smpboot.c:103)
[ 17.228198][ C0] kthread (kernel/kthread.c:438)
[ 17.228839][ C0] ? sort_range (kernel/smpboot.c:103)
[ 17.229601][ C0] ? kthread_affine_node (kernel/kthread.c:381)
[ 17.230400][ C0] ret_from_fork (arch/x86/kernel/process.c:164)
[ 17.231112][ C0] ? kthread_affine_node (kernel/kthread.c:381)
[ 17.231971][ C0] ret_from_fork_asm (arch/x86/entry/entry_32.S:737)
[ 17.232755][ C0] entry_INT80_32 (arch/x86/entry/entry_32.S:942)
[ 17.233621][ C0] Modules linked in: usbtest(+) serio_raw evdev parport_pc parport qemu_fw_cfg button
[ 17.235190][ C0] ---[ end trace 0000000000000000 ]---
[ 17.236068][ C0] EIP: __phys_addr (arch/x86/mm/physaddr.c:66 (discriminator 3))
[ 17.236831][ C0] Code: e5 56 53 85 c0 78 09 0f 0b b8 ec a1 e6 81 eb 21 80 3d f8 e9 4d 82 00 89 c3 8d b0 00 00 00 80 74 15 e8 cf c5 19 00 84 c0 74 0c <0f> 0b b8 e0 a1 e6 81 e8 77 3e fb ff a1 30 14 d3 82 85 c0 74 2a 89
All code
========
0: e5 56 in $0x56,%eax
2: 53 push %rbx
3: 85 c0 test %eax,%eax
5: 78 09 js 0x10
7: 0f 0b ud2
9: b8 ec a1 e6 81 mov $0x81e6a1ec,%eax
e: eb 21 jmp 0x31
10: 80 3d f8 e9 4d 82 00 cmpb $0x0,-0x7db21608(%rip) # 0xffffffff824dea0f
17: 89 c3 mov %eax,%ebx
19: 8d b0 00 00 00 80 lea -0x80000000(%rax),%esi
1f: 74 15 je 0x36
21: e8 cf c5 19 00 call 0x19c5f5
26: 84 c0 test %al,%al
28: 74 0c je 0x36
2a:* 0f 0b ud2 <-- trapping instruction
2c: b8 e0 a1 e6 81 mov $0x81e6a1e0,%eax
31: e8 77 3e fb ff call 0xfffffffffffb3ead
36: a1 30 14 d3 82 85 c0 movabs 0x2a74c08582d31430,%eax
3d: 74 2a
3f: 89 .byte 0x89
Code starting with the faulting instruction
===========================================
0: 0f 0b ud2
2: b8 e0 a1 e6 81 mov $0x81e6a1e0,%eax
7: e8 77 3e fb ff call 0xfffffffffffb3e83
c: a1 30 14 d3 82 85 c0 movabs 0x2a74c08582d31430,%eax
13: 74 2a
15: 89 .byte 0x89
The kernel config and materials to reproduce are available at:
https://download.01.org/0day-ci/archive/20260331/202603312130.f9ed8d89-lkp@intel.com
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-03-31 13:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-27 13:22 [RFC PATCH] modules: extend {kstrdup,kfree}_const() to handle per-module .rodata Dmitry Antipov
2026-03-31 13:37 ` kernel test robot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox