[RFC PATCH 43/56] module: Update alternatives

David Kaplan
2025-10-13T14:36:21Z

Update alternatives in modules by making the module text writeable and
then calling the arch-specific update alternatives function.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
include/linux/module.h | 6 ++++++
kernel/module/main.c | 42 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 48 insertions(+)

diff --git a/include/linux/module.h b/include/linux/module.h
index 2d8c34cb961f..f29974cae6bc 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -1023,6 +1023,12 @@ DEFINE_FREE(module_put, struct module *, if (_T) module_put(_T))
#ifdef CONFIG_DYNAMIC_MITIGATIONS
void modules_prepare_repatch(void);
void modules_post_repatch(void);
+void modules_update_alternatives(void);
+void arch_module_update_alternatives(struct module *mod);
+void arch_module_pre_update_alternatives(struct module *mod);
+void arch_module_post_update_alternatives(struct module *mod);
+void modules_pre_update_alternatives(void);
+void modules_post_update_alternatives(void);
#endif

#endif /* _LINUX_MODULE_H */
diff --git a/kernel/module/main.c b/kernel/module/main.c
index 0525b1c6d5b9..fa46ce4285dd 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -3943,4 +3943,46 @@ void modules_post_repatch(void)
change_mod_mem_perm(mod, MOD_TEXT, false);
}
}
+
+void __weak arch_module_update_alternatives(struct module *mod)
+{
+}
+
+void modules_update_alternatives(void)
+{
+ struct module *mod;
+
+ list_for_each_entry(mod, &modules, list) {
+ arch_module_update_alternatives(mod);
+ update_all_static_calls(mod->static_call_sites,
+ mod->static_call_sites +
+ mod->num_static_call_sites, mod);
+ }
+}
+
+void __weak arch_module_pre_update_alternatives(struct module *mod)
+{
+}
+
+void __weak arch_module_post_update_alternatives(struct module *mod)
+{
+}
+
+void modules_pre_update_alternatives(void)
+{
+ struct module *mod;
+
+ list_for_each_entry(mod, &modules, list) {
+ arch_module_pre_update_alternatives(mod);
+ }
+}
+
+void modules_post_update_alternatives(void)
+{
+ struct module *mod;
+
+ list_for_each_entry(mod, &modules, list) {
+ arch_module_post_update_alternatives(mod);
+ }
+}
#endif
--
2.34.1