Code using IS_ENABLED(CONFIG_MODULES) as a C expression may need access to the module structure definitions to compile. Make sure these structure definitions are always visible.
This will conflict with commit 6bb37af62634 ("module: Move modprobe_path and modules_disabled ctl_tables into the module subsys") from the sysctl tree, but the resolution is trivial.
Signed-off-by: Thomas Weißschuh thomas.weissschuh@linutronix.de --- Changes in v2: - Pick up tags from v1 - Keep MODULE_ARCH_INIT and 'struct module' definitions together - Link to v1: https://lore.kernel.org/r/20250612-kunit-ifdef-modules-v1-0-fdccd42dcff8@lin...
--- Thomas Weißschuh (3): module: move 'struct module_use' to internal.h module: make structure definitions always visible kunit: test: Drop CONFIG_MODULE ifdeffery
include/linux/module.h | 29 +++++++++++------------------ kernel/module/internal.h | 7 +++++++ lib/kunit/test.c | 8 -------- 3 files changed, 18 insertions(+), 26 deletions(-) --- base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494 change-id: 20250611-kunit-ifdef-modules-0fefd13ae153
Best regards,
The struct was moved to the public header file in commit c8e21ced08b3 ("module: fix kdb's illicit use of struct module_use."). Back then the structure was used outside of the module core. Nowadays this is not true anymore, so the structure can be made internal.
Signed-off-by: Thomas Weißschuh thomas.weissschuh@linutronix.de Reviewed-by: Daniel Gomez da.gomez@samsung.com Reviewed-by: Petr Pavlu petr.pavlu@suse.com --- include/linux/module.h | 7 ------- kernel/module/internal.h | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/include/linux/module.h b/include/linux/module.h index 92e1420fccdffc9de9f49da9061546cc1e0c89d1..52f7b0487a2733c56e2531a434887e56e1bf45b2 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -313,13 +313,6 @@ void *__symbol_get_gpl(const char *symbol); __used __section(".no_trim_symbol") = __stringify(x); \ (typeof(&x))(__symbol_get(__stringify(x))); })
-/* modules using other modules: kdb wants to see this. */ -struct module_use { - struct list_head source_list; - struct list_head target_list; - struct module *source, *target; -}; - enum module_state { MODULE_STATE_LIVE, /* Normal state. */ MODULE_STATE_COMING, /* Full formed, running module_init. */ diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 8d74b0a21c82b5360977a29736eca78ee6b6be3e..1c2e0b0dc52e72d5ecd2f1b310ce535364b3f33b 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -109,6 +109,13 @@ struct find_symbol_arg { enum mod_license license; };
+/* modules using other modules */ +struct module_use { + struct list_head source_list; + struct list_head target_list; + struct module *source, *target; +}; + int mod_verify_sig(const void *mod, struct load_info *info); int try_to_force_load(struct module *mod, const char *reason); bool find_symbol(struct find_symbol_arg *fsa);
To write code that works with both CONFIG_MODULES=y and CONFIG_MODULES=n it is convenient to use "if (IS_ENABLED(CONFIG_MODULES))" over raw #ifdef. The code will still fully typechecked but the unreachable parts are discarded by the compiler. This prevents accidental breakage when a certain kconfig combination was not specifically tested by the developer. This pattern is already supported to some extend by module.h defining empty stub functions if CONFIG_MODULES=n. However some users of module.h work on the structured defined by module.h.
Therefore these structure definitions need to be visible, too.
Many structure members are still gated by specific configuration settings. The assumption for those is that the code using them will be gated behind the same configuration setting anyways.
Signed-off-by: Thomas Weißschuh thomas.weissschuh@linutronix.de Reviewed-by: Daniel Gomez da.gomez@samsung.com --- include/linux/module.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/include/linux/module.h b/include/linux/module.h index 52f7b0487a2733c56e2531a434887e56e1bf45b2..2f330e60a7420a144abeed6d357ac93c39a705e9 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -302,17 +302,6 @@ static typeof(name) __mod_device_table__##type##__##name \
struct notifier_block;
-#ifdef CONFIG_MODULES - -extern int modules_disabled; /* for sysctl */ -/* Get/put a kernel symbol (calls must be symmetric) */ -void *__symbol_get(const char *symbol); -void *__symbol_get_gpl(const char *symbol); -#define symbol_get(x) ({ \ - static const char __notrim[] \ - __used __section(".no_trim_symbol") = __stringify(x); \ - (typeof(&x))(__symbol_get(__stringify(x))); }) - enum module_state { MODULE_STATE_LIVE, /* Normal state. */ MODULE_STATE_COMING, /* Full formed, running module_init. */ @@ -602,6 +591,17 @@ struct module { #define MODULE_ARCH_INIT {} #endif
+#ifdef CONFIG_MODULES + +extern int modules_disabled; /* for sysctl */ +/* Get/put a kernel symbol (calls must be symmetric) */ +void *__symbol_get(const char *symbol); +void *__symbol_get_gpl(const char *symbol); +#define symbol_get(x) ({ \ + static const char __notrim[] \ + __used __section(".no_trim_symbol") = __stringify(x); \ + (typeof(&x))(__symbol_get(__stringify(x))); }) + #ifndef HAVE_ARCH_KALLSYMS_SYMBOL_VALUE static inline unsigned long kallsyms_symbol_value(const Elf_Sym *sym) {
The function stubs exposed by module.h allow the code to compile properly without the ifdeffery. The generated object code stays the same, as the compiler can optimize away all the dead code. As the code is still typechecked developer errors can be detected faster.
Signed-off-by: Thomas Weißschuh thomas.weissschuh@linutronix.de Acked-by: David Gow davidgow@google.com Reviewed-by: Daniel Gomez da.gomez@samsung.com --- lib/kunit/test.c | 8 -------- 1 file changed, 8 deletions(-)
diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 146d1b48a0965e8aaddb6162928f408bbb542645..019b2ac9c8469021542b610278f8842e100d57ad 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -759,7 +759,6 @@ void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites) } EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
-#ifdef CONFIG_MODULES static void kunit_module_init(struct module *mod) { struct kunit_suite_set suite_set, filtered_set; @@ -847,7 +846,6 @@ static struct notifier_block kunit_mod_nb = { .notifier_call = kunit_module_notify, .priority = 0, }; -#endif
KUNIT_DEFINE_ACTION_WRAPPER(kfree_action_wrapper, kfree, const void *)
@@ -938,20 +936,14 @@ static int __init kunit_init(void) kunit_debugfs_init();
kunit_bus_init(); -#ifdef CONFIG_MODULES return register_module_notifier(&kunit_mod_nb); -#else - return 0; -#endif } late_initcall(kunit_init);
static void __exit kunit_exit(void) { memset(&kunit_hooks, 0, sizeof(kunit_hooks)); -#ifdef CONFIG_MODULES unregister_module_notifier(&kunit_mod_nb); -#endif
kunit_bus_shutdown();
On Fri, 11 Jul 2025 15:31:35 +0200, Thomas Weißschuh wrote:
Code using IS_ENABLED(CONFIG_MODULES) as a C expression may need access to the module structure definitions to compile. Make sure these structure definitions are always visible.
This will conflict with commit 6bb37af62634 ("module: Move modprobe_path and modules_disabled ctl_tables into the module subsys") from the sysctl tree, but the resolution is trivial.
[...]
Applied, thanks!
[1/3] module: move 'struct module_use' to internal.h commit: bb02f22eaabc4d878577e2b8c46ed7b6be5f5459 [2/3] module: make structure definitions always visible commit: 02281b559cd1fdfdc8f7eb05bbbe3ab7b35246f0 [3/3] kunit: test: Drop CONFIG_MODULE ifdeffery commit: dffcba8acea3a80b3478750ac32f17bd5345b68e
Best regards,
On Fri, Jul 11, 2025 at 03:39:04PM +0200, Daniel Gomez wrote:
On Fri, 11 Jul 2025 15:31:35 +0200, Thomas Weißschuh wrote:
Code using IS_ENABLED(CONFIG_MODULES) as a C expression may need access to the module structure definitions to compile. Make sure these structure definitions are always visible.
This will conflict with commit 6bb37af62634 ("module: Move modprobe_path and modules_disabled ctl_tables into the module subsys") from the sysctl tree, but the resolution is trivial.
[...]
Applied, thanks!
[1/3] module: move 'struct module_use' to internal.h commit: bb02f22eaabc4d878577e2b8c46ed7b6be5f5459 [2/3] module: make structure definitions always visible commit: 02281b559cd1fdfdc8f7eb05bbbe3ab7b35246f0 [3/3] kunit: test: Drop CONFIG_MODULE ifdeffery commit: dffcba8acea3a80b3478750ac32f17bd5345b68e
Thanks!
FYI If you apply a patch you need to add yourself to the Signed-off-by chain. And Link tags are nice. For example:
b4 shazam --add-my-sob --add-link
On 11/07/2025 15.51, Thomas WeiÃschuh wrote:
On Fri, Jul 11, 2025 at 03:39:04PM +0200, Daniel Gomez wrote:
On Fri, 11 Jul 2025 15:31:35 +0200, Thomas Weißschuh wrote:
Code using IS_ENABLED(CONFIG_MODULES) as a C expression may need access to the module structure definitions to compile. Make sure these structure definitions are always visible.
This will conflict with commit 6bb37af62634 ("module: Move modprobe_path and modules_disabled ctl_tables into the module subsys") from the sysctl tree, but the resolution is trivial.
[...]
Applied, thanks!
[1/3] module: move 'struct module_use' to internal.h commit: bb02f22eaabc4d878577e2b8c46ed7b6be5f5459 [2/3] module: make structure definitions always visible commit: 02281b559cd1fdfdc8f7eb05bbbe3ab7b35246f0 [3/3] kunit: test: Drop CONFIG_MODULE ifdeffery commit: dffcba8acea3a80b3478750ac32f17bd5345b68e
Thanks!
FYI If you apply a patch you need to add yourself to the Signed-off-by chain. And Link tags are nice. For example:
b4 shazam --add-my-sob --add-link
You're correct. I had a lapse there. Branch updated. Thanks!
[1/3] module: move 'struct module_use' to internal.h commit: 6633d3a45a8c075193304d12ba10a1771d1dbf10 [2/3] module: make structure definitions always visible commit: a55842991352a8b512f40d1424b65c911ffbf6fa [3/3] kunit: test: Drop CONFIG_MODULE ifdeffery commit: 699657e8e50ae967ae26f704f6fbfa598fcb0cef
linux-kselftest-mirror@lists.linaro.org