module: Use RCU instead of RCU-sched.
Subsystems (17)
v2 cover module: Use RCU instead of RCU-sched.
Hi,
This is an updated version of the inital post after PeterZ made me aware that there are users outside of the module directory.
The goal is replace the mix auf rcu_read_lock(), rcu_read_lock_sched() and preempt_disable() with just rcu_read_lock().
I've splitted it into smaller chunks which can be applied/ reviewed independently. I'm just not sure about the cfi patch (28/28) so I added just a comment instead.
v1…v2: https://lore.kernel.org/all/[email protected]/
- Splitted into smaller patches.
- Converted all users.
Sebastian
PATCH v2 1/28 module: Extend the preempt disabled section in dereference_symbol_descriptor().
dereference_symbol_descriptor() needs to obtain the module pointer belonging to pointer in order to resolve that pointer.
The returned mod pointer is obtained under RCU-sched/ preempt_disable() guarantees and needs to be used within this section to ensure that the module is not removed in the meantime.
Extend the preempt_disable() section to also cover dereference_module_function_descriptor().
--- include/linux/kallsyms.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kallsyms.h +1 -1
@@ -57,10 +57,10 @@ static inline void *dereference_symbol_descriptor(void *ptr)preempt_disable (); mod =__module_address ((unsigned long )ptr); -preempt_enable ();if (mod) ptr =dereference_module_function_descriptor (mod, ptr); +preempt_enable ();#endif return ptr; }
Signature · 1 line
2.45.2
Re: [PATCH v2 01/28] module: Extend the preempt disabled section in dereference_symbol_descriptor().
Quoted 9 lines · Sebastian Andrzej Siewior
dereference_symbol_descriptor()needs to obtain the module pointer belonging to pointer in order to resolve that pointer.
The returned mod pointer is obtained under RCU-sched/preempt_disable()guarantees and needs to be used within this section to ensure that the module is not removed in the meantime.Extend the
preempt_disable()section to also coverdereference_module_function_descriptor().
Quoted 12 lines
Fixes 04b8eb7a4ccd9 ("symbol lookup: introduce dereference_symbol_descriptor()")Cc "James E.J. Bottomley" <[email protected]>Cc Christophe Leroy <[email protected]>Cc Helge Deller <[email protected]>Cc Madhavan Srinivasan <[email protected]>Cc Michael Ellerman <[email protected]>Cc Naveen N Rao <[email protected]>Cc Nicholas Piggin <[email protected]>Cc Sergey Senozhatsky <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>
FWIW
PATCH v2 2/28 module: Begin to move from RCU-sched to RCU.
In reply to Sebastian Andrzej SiewiorThe RCU usage in module was introduced in commit d72b37513cdfb ("Remove stop_machine during module load v2") and it claimed not to be RCU but similar. Then there was another improvement in commit e91defa26c527 ("module: don't use stop_machine on module load"). It become a mix of RCU and RCU-sched and was eventually fixed 0be964be0d450 ("module:
Sanitize RCU usage and locking"). Later RCU & RCU-sched was merged in commit cb2f55369d3a9 ("modules: Replace synchronize_sched() and call_rcu_sched()") so that was aligned.
Looking at it today, there is still leftovers. The preempt_disable() was used instead rcu_read_lock_sched(). The RCU & RCU-sched merge was not complete as there is still rcu_dereference_sched() for module::kallsyms.
The RCU-list modules and unloaded_tainted_modules are always accessed under RCU protection or the module_mutex. The modules list iteration can always happen safely because the module will not disappear. Once the module is removed (free_module()) then after removing the module from the list, there is a synchronize_rcu() which waits until every RCU reader left the section. That means iterating over the list within a RCU-read section is enough, there is no need to disable preemption. module::kallsyms is first assigned in add_kallsyms() before the module is added to the list. At this point, it points to init data. This pointer is later updated and before the init code is removed there is also synchronize_rcu() in do_free_init(). That means A RCU read lock is enough for protection and rcu_dereference() can be safely used.
Convert module code and its users step by step. Update comments and convert print_modules() to use RCU.
--- kernel/module/main.c | 9 ++++----- kernel/module/tree_lookup.c | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-)
kernel/module/main.c +4 -5
@@ -67,7 +67,7 @@/* * Mutex protects: -* 1) List of modules (also safely readable with preempt_disable), +* 1) List of modules (also safely readable within RCU read section), * 2) module_use links, * 3) mod_tree.addr_min/mod_tree.addr_max. * (delete and add uses RCU list operations).
@@ -1348,7 +1348,7 @@ static void free_module(struct module *mod)mod_tree_remove (mod);/* Remove this module from bug list, this uses list_del_rcu */ module_bug_cleanup (mod); -/* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */ +/* Wait for RCU synchronizing before releasing mod->list and buglist. */ synchronize_rcu ();if (try_add_tainted_module (mod)) pr_err("%s: adding tainted module to the unloaded tainted modules list failed.\n",
@@ -2965,7 +2965,7 @@ static noinline int do_init_module(struct module *mod)#endif /* * We want to free module_init, but be aware that kallsyms may be -* walking this with preempt disabled. In all the failure paths, we +* walking this within an RCU read section. In all the failure paths, we * call synchronize_rcu(), but we don't want to slow down the success * path. execmem_free() cannot be called in an interrupt, so do the * work and call synchronize_rcu() in a work queue.
@@ -3754,7 +3754,7 @@ void print_modules(void)printk (KERN_DEFAULT "Modules linked in:" );/* Most callers should already have preempt disabled, but make sure */ -preempt_disable (); +guard (rcu)();list_for_each_entry_rcu (mod, &modules, list) {if (mod->state ==MODULE_STATE_UNFORMED )continue ;
@@ -3762,7 +3762,6 @@ void print_modules(void) }print_unloaded_tainted_modules (); -preempt_enable ();if (last_unloaded_module.name[0 ])pr_cont (" [last unloaded: %s%s]" , last_unloaded_module.name, last_unloaded_module.taints);
kernel/module/tree_lookup.c +4 -4
@@ -12,11 +12,11 @@/* * Use a latched RB-tree for __module_address(); this allows us to use -* RCU-sched lookups of the address from any context. +* RCU lookups of the address from any context. * -* This is conditional on PERF_EVENTS || TRACING because those can really hit -* __module_address() hard by doing a lot of stack unwinding; potentially from -* NMI context. +* This is conditional on PERF_EVENTS || TRACING || CFI_CLANG because those can +* really hit __module_address() hard by doing a lot of stack unwinding; +* potentially from NMI context. */ static __always_inline unsigned long __mod_tree_val (struct latch_tree_node *n)
Signature · 1 line
2.45.2
PATCH v2 3/28 module: Use proper RCU assignment in add_kallsyms().
In reply to Sebastian Andrzej Siewioradd_kallsyms() assigns the RCU pointer module::kallsyms and setups the structures behind it which point to init-data. The module was not published yet, nothing can see the kallsyms pointer and the data behind it. Also module's init function was not yet invoked.
There is no need to use rcu_dereference() here, it is just to keep checkers quiet. The whole RCU read section is also not needed.
Use a local kallsyms pointer and setup the data structures. Assign that pointer to the data structure at the end via rcu_assign_pointer().
--- kernel/module/kallsyms.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-)
kernel/module/kallsyms.c +14 -17
@@ -177,19 +177,15 @@ void add_kallsyms(struct module *mod, const struct load_info *info)unsigned long strtab_size;void *data_base = mod->mem[MOD_DATA ].base;void *init_data_base = mod->mem[MOD_INIT_DATA ].base; +struct mod_kallsyms *kallsyms; -/* Set up to point into init section. */ - mod->kallsyms = (void __rcu *)init_data_base + - info->mod_kallsyms_init_off; + kallsyms = init_data_base + info->mod_kallsyms_init_off; -rcu_read_lock (); -/* The following is safe since this pointer cannot change */ -rcu_dereference (mod->kallsyms)->symtab = (void *)symsec->sh_addr; -rcu_dereference (mod->kallsyms)->num_symtab = symsec->sh_size /sizeof (Elf_Sym); + kallsyms->symtab = (void *)symsec->sh_addr; + kallsyms->num_symtab = symsec->sh_size /sizeof (Elf_Sym);/* Make sure we get permanent strtab: don't use info->strtab. */ -rcu_dereference (mod->kallsyms)->strtab = - (void *)info->sechdrs[info->index.str].sh_addr; -rcu_dereference (mod->kallsyms)->typetab = init_data_base + info->init_typeoffs; + kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr; + kallsyms->typetab = init_data_base + info->init_typeoffs;/* * Now populate the cut down core kallsyms for after init
@@ -199,20 +195,19 @@ void add_kallsyms(struct module *mod, const struct load_info *info) mod->core_kallsyms.strtab = s = data_base + info->stroffs; mod->core_kallsyms.typetab = data_base + info->core_typeoffs; strtab_size = info->core_typeoffs - info->stroffs; - src =rcu_dereference (mod->kallsyms)->symtab; -for (ndst = i =0 ; i <rcu_dereference (mod->kallsyms)->num_symtab; i++) { -rcu_dereference (mod->kallsyms)->typetab[i] =elf_type (src + i, info); + src = kallsyms->symtab; +for (ndst = i =0 ; i < kallsyms->num_symtab; i++) { + kallsyms->typetab[i] =elf_type (src + i, info);if (i ==0 ||is_livepatch_module (mod) ||is_core_symbol (src + i, info->sechdrs, info->hdr->e_shnum, info->index.pcpu)) {ssize_t ret; mod->core_kallsyms.typetab[ndst] = -rcu_dereference (mod->kallsyms)->typetab[i]; + kallsyms->typetab[i]; dst[ndst] = src[i]; dst[ndst++].st_name = s - mod->core_kallsyms.strtab; - ret =strscpy (s, - &rcu_dereference (mod->kallsyms)->strtab[src[i].st_name], + ret =strscpy (s, &kallsyms->strtab[src[i].st_name], strtab_size);if (ret <0 )break ;
@@ -220,7 +215,9 @@ void add_kallsyms(struct module *mod, const struct load_info *info) strtab_size -= ret +1 ; } } -rcu_read_unlock (); + +/* Set up to point into init section. */ +rcu_assign_pointer (mod->kallsyms, kallsyms); mod->core_kallsyms.num_symtab = ndst; }
Signature · 1 line
2.45.2
PATCH v2 4/28 module: Use RCU in find_kallsyms_symbol().
In reply to Sebastian Andrzej SiewiorThe modules list and module::kallsyms can be accessed under RCU assumption.
Use rcu_dereference() to reference the kallsyms pointer in find_kallsyms_symbol(). Use a RCU section instead of preempt_disable in callers of find_kallsyms_symbol(). Keep the preempt-disable in module_address_lookup() due to __module_address().
--- kernel/module/kallsyms.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/module/kallsyms.c +3 -4
@@ -257,7 +257,7 @@ static const char *find_kallsyms_symbol(struct module *mod, {unsigned int i, best =0 ;unsigned long nextval, bestval; -struct mod_kallsyms *kallsyms =rcu_dereference_sched (mod->kallsyms); +struct mod_kallsyms *kallsyms =rcu_dereference (mod->kallsyms);struct module_memory *mod_mem;/* At worse, next value is at end of module */
@@ -329,6 +329,7 @@ int module_address_lookup(unsigned long addr,int ret =0 ;struct module *mod; +guard (rcu)();preempt_disable (); mod =__module_address (addr);if (mod) {
@@ -356,7 +357,7 @@ int lookup_module_symbol_name(unsigned long addr, char *symname) {struct module *mod; -preempt_disable (); +guard (rcu)();list_for_each_entry_rcu (mod, &modules, list) {if (mod->state ==MODULE_STATE_UNFORMED )continue ;
@@ -368,12 +369,10 @@ int lookup_module_symbol_name(unsigned long addr, char *symname) goto out;strscpy (symname, sym,KSYM_NAME_LEN ); -preempt_enable ();return 0 ; } } out: -preempt_enable ();return -ERANGE ; }
Signature · 1 line
2.45.2
PATCH v2 5/28 module: Use RCU in module_get_kallsym().
In reply to Sebastian Andrzej SiewiorThe modules list and module::kallsyms can be accessed under RCU assumption.
Iterate the modules with RCU protection, use rcu_dereference() to access the kallsyms pointer.
--- kernel/module/kallsyms.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/module/kallsyms.c +2 -4
@@ -381,13 +381,13 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, {struct module *mod; -preempt_disable (); +guard (rcu)();list_for_each_entry_rcu (mod, &modules, list) {struct mod_kallsyms *kallsyms;if (mod->state ==MODULE_STATE_UNFORMED )continue ; - kallsyms =rcu_dereference_sched (mod->kallsyms); + kallsyms =rcu_dereference (mod->kallsyms);if (symnum < kallsyms->num_symtab) {const Elf_Sym *sym = &kallsyms->symtab[symnum];
@@ -396,12 +396,10 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,strscpy (name,kallsyms_symbol_name (kallsyms, symnum),KSYM_NAME_LEN );strscpy (module_name, mod->name,MODULE_NAME_LEN ); *exported =is_exported (name, *value, mod); -preempt_enable ();return 0 ; } symnum -= kallsyms->num_symtab; } -preempt_enable ();return -ERANGE ; }
Signature · 1 line
2.45.2
PATCH v2 6/28 module: Use RCU in find_module_all().
In reply to Sebastian Andrzej SiewiorThe modules list and module::kallsyms can be accessed under RCU assumption.
Remove module_assert_mutex_or_preempt() from find_module_all() so it can be used under RCU protection without warnings. Update its callers to use RCU protection instead of preempt_disable().
--- include/linux/module.h | 2 +- kernel/livepatch/core.c | 4 +--- kernel/module/kallsyms.c | 1 + kernel/module/main.c | 6 ++---- kernel/trace/trace_kprobe.c | 9 +++------ 5 files changed, 8 insertions(+), 14 deletions(-)
include/linux/module.h +1 -1
@@ -663,7 +663,7 @@ static inline bool within_module(unsigned long addr, const struct module *mod)return within_module_init (addr, mod) ||within_module_core (addr, mod); } -/* Search for module by name: must be in a RCU-sched critical section. */ +/* Search for module by name: must be in a RCU critical section. */ struct module *find_module (const char *name); extern void __noreturn __module_put_and_kthread_exit(struct module *mod,
kernel/livepatch/core.c +1 -3
@@ -59,7 +59,7 @@ static void klp_find_object_module(struct klp_object *obj)if (!klp_is_module (obj))return ; -rcu_read_lock_sched (); +guard (rcu)();/* * We do not want to block removal of patched modules and therefore * we do not take a reference here. The patches are removed by
@@ -75,8 +75,6 @@ static void klp_find_object_module(struct klp_object *obj)*/ if (mod && mod->klp_alive) obj->mod = mod; - -rcu_read_unlock_sched (); }static bool klp_initialized (void )
kernel/module/kallsyms.c +1 -0
@@ -450,6 +450,7 @@ unsigned long module_kallsyms_lookup_name(const char *name)unsigned long ret;/* Don't lock: we're in enough trouble already. */ +guard (rcu)();preempt_disable (); ret =__module_kallsyms_lookup_name (name);preempt_enable ();
kernel/module/main.c +2 -4
@@ -374,16 +374,14 @@ bool find_symbol(struct find_symbol_arg *fsa) }/* -* Search for module by name: must hold module_mutex (or preempt disabled -* for read-only access). +* Search for module by name: must hold module_mutex (or RCU for read-only +* access). */ struct module *find_module_all (const char *name,size_t len,bool even_unformed) {struct module *mod; -module_assert_mutex_or_preempt (); -list_for_each_entry_rcu (mod, &modules, list,lockdep_is_held (&module_mutex)) {if (!even_unformed && mod->state ==MODULE_STATE_UNFORMED )
kernel/trace/trace_kprobe.c +3 -6
@@ -123,9 +123,8 @@ static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)if (!p)return true; *p ='\0' ; -rcu_read_lock_sched (); - ret = !!find_module (tk->symbol); -rcu_read_unlock_sched (); +scoped_guard (rcu) + ret = !!find_module (tk->symbol); *p =':' ;return ret;
@@ -800,12 +799,10 @@ static struct module *try_module_get_by_name(const char *name) {struct module *mod; -rcu_read_lock_sched (); +guard (rcu)(); mod =find_module (name);if (mod && !try_module_get (mod)) mod =NULL ; -rcu_read_unlock_sched (); -return mod; }#else
Signature · 1 line
2.45.2
Re: [PATCH v2 06/28] module: Use RCU in find_module_all().
Quoted 18 lines · Sebastian Andrzej Siewior
The modules list and
module::kallsymscan be accessed under RCU assumption.Remove
module_assert_mutex_or_preempt()fromfind_module_all()so it can be used under RCU protection without warnings. Update its callers to use RCU protection instead ofpreempt_disable().Cc Jiri Kosina <[email protected]>Cc Joe Lawrence <[email protected]>Cc Josh Poimboeuf <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Mathieu Desnoyers <[email protected]>Cc Miroslav Benes <[email protected]>Cc Petr Mladek <[email protected]>Cc Steven Rostedt <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>
Quoted 7 lines
--- include/linux/module.h | 2 +- kernel/livepatch/core.c | 4 +--- kernel/module/kallsyms.c | 1 + kernel/module/main.c | 6 ++---- kernel/trace/trace_kprobe.c | 9 +++------ 5 files changed, 8 insertions(+), 14 deletions(-)
I looked primary on the changes in the livepatch code. But the entire patch looks good to me:
Best Regards,
Petr
PATCH v2 7/28 module: Use RCU in __find_kallsyms_symbol_value().
In reply to Sebastian Andrzej Siewiormodule::kallsyms can be accessed under RCU assumption.
Use rcu_dereference() to access module::kallsyms.
Update callers.
--- kernel/module/kallsyms.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-)
kernel/module/kallsyms.c +4 -13
@@ -407,7 +407,7 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,static unsigned long __find_kallsyms_symbol_value (struct module *mod,const char *name) {unsigned int i; -struct mod_kallsyms *kallsyms =rcu_dereference_sched (mod->kallsyms); +struct mod_kallsyms *kallsyms =rcu_dereference (mod->kallsyms);for (i =0 ; i < kallsyms->num_symtab; i++) {const Elf_Sym *sym = &kallsyms->symtab[i];
@@ -447,24 +447,15 @@ static unsigned long __module_kallsyms_lookup_name(const char *name)/* Look for this name: can be of form module:name. */ unsigned long module_kallsyms_lookup_name (const char *name) { -unsigned long ret; -/* Don't lock: we're in enough trouble already. */ guard (rcu)(); -preempt_disable (); - ret =__module_kallsyms_lookup_name (name); -preempt_enable (); -return ret; +return __module_kallsyms_lookup_name (name); }unsigned long find_kallsyms_symbol_value (struct module *mod,const char *name) { -unsigned long ret; - -preempt_disable (); - ret =__find_kallsyms_symbol_value (mod, name); -preempt_enable (); -return ret; +guard (rcu)(); +return __find_kallsyms_symbol_value (mod, name); } int module_kallsyms_on_each_symbol(const char *modname,
Signature · 1 line
2.45.2
PATCH v2 8/28 module: Use RCU in module_kallsyms_on_each_symbol().
In reply to Sebastian Andrzej Siewiormodule::kallsyms can be accessed under RCU assumption.
Use rcu_dereference() to access module::kallsyms.
Update callers.
--- kernel/module/kallsyms.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/module/kallsyms.c +2 -4
@@ -476,10 +476,8 @@ int module_kallsyms_on_each_symbol(const char *modname,if (modname &&strcmp (modname, mod->name))continue ; -/* Use rcu_dereference_sched() to remain compliant with the sparse tool */ -preempt_disable (); - kallsyms =rcu_dereference_sched (mod->kallsyms); -preempt_enable (); + kallsyms =rcu_dereference_check (mod->kallsyms, +lockdep_is_held (&module_mutex));for (i =0 ; i < kallsyms->num_symtab; i++) {const Elf_Sym *sym = &kallsyms->symtab[i];
Signature · 1 line
2.45.2
PATCH v2 9/28 module: Remove module_assert_mutex_or_preempt() from try_add_tainted_module().
In reply to Sebastian Andrzej Siewiormodule_assert_mutex_or_preempt() is not needed in try_add_tainted_module(). The function checks for RCU-sched or the module_mutex to be acquired. The list_for_each_entry_rcu() below does the same check.
Remove module_assert_mutex_or_preempt() from try_add_tainted_module().
--- kernel/module/tracking.c | 2 -- 1 file changed, 2 deletions(-)
kernel/module/tracking.c +0 -2
@@ -21,8 +21,6 @@ int try_add_tainted_module(struct module *mod) {struct mod_unload_taint *mod_taint; -module_assert_mutex_or_preempt (); -if (!mod->taints) goto out;
Signature · 1 line
2.45.2
PATCH v2 10/28 module: Use RCU in find_symbol().
In reply to Sebastian Andrzej Siewiormodule_assert_mutex_or_preempt() is not needed in find_symbol(). The function checks for RCU-sched or the module_mutex to be acquired. The list_for_each_entry_rcu() below does the same check.
Remove module_assert_mutex_or_preempt() from try_add_tainted_module(). Use RCU protection to invoke find_symbol() and update callers.
--- kernel/module/main.c | 30 ++++++++++++------------------ kernel/module/version.c | 14 +++++++------- 2 files changed, 19 insertions(+), 25 deletions(-)
kernel/module/main.c +12 -18
@@ -331,7 +331,7 @@ static bool find_exported_symbol_in_section(const struct symsearch *syms,/* * Find an exported symbol and return it, along with, (optional) crc and -* (optional) module which owns it. Needs preempt disabled or module_mutex. +* (optional) module which owns it. Needs RCU or module_mutex. */ bool find_symbol (struct find_symbol_arg *fsa) {
@@ -345,8 +345,6 @@ bool find_symbol(struct find_symbol_arg *fsa)struct module *mod;unsigned int i; -module_assert_mutex_or_preempt (); -for (i =0 ; i <ARRAY_SIZE (arr); i++)if (find_exported_symbol_in_section (&arr[i],NULL , fsa))return true;
@@ -812,10 +810,9 @@ void __symbol_put(const char *symbol) .gplok = true, }; -preempt_disable (); +guard (rcu)();BUG_ON (!find_symbol (&fsa));module_put (fsa.owner); -preempt_enable (); }EXPORT_SYMBOL (__symbol_put);
@@ -1369,21 +1366,18 @@ void *__symbol_get(const char *symbol) .warn = true, }; -preempt_disable (); -if (!find_symbol (&fsa)) - goto fail; -if (fsa.license !=GPL_ONLY ) { -pr_warn ("failing symbol_get of non-GPLONLY symbol %s.\n" , - symbol); - goto fail; +scoped_guard (rcu) { +if (!find_symbol (&fsa)) +return NULL ; +if (fsa.license !=GPL_ONLY ) { +pr_warn ("failing symbol_get of non-GPLONLY symbol %s.\n" , + symbol); +return NULL ; + } +if (strong_try_module_get (fsa.owner)) +return NULL ; } -if (strong_try_module_get (fsa.owner)) - goto fail; -preempt_enable ();return (void *)kernel_symbol_value (fsa.sym); -fail: -preempt_enable (); -return NULL ; }EXPORT_SYMBOL_GPL (__symbol_get);
kernel/module/version.c +7 -7
@@ -62,17 +62,17 @@ int check_modstruct_version(const struct load_info *info, .name = "module_layout", .gplok = true, }; +bool have_symbol;/* * Since this should be found in kernel (which can't be removed), no -* locking is necessary -- use preempt_disable() to placate lockdep. +* locking is necessary. Regardless use a RCU read section to keep +* lockdep happy. */ -preempt_disable (); -if (!find_symbol (&fsa)) { -preempt_enable (); -BUG (); - } -preempt_enable (); +scoped_guard (rcu) + have_symbol =find_symbol (&fsa); +BUG_ON (!have_symbol); +return check_version (info,"module_layout" , mod, fsa.crc); }
Signature · 1 line
2.45.2
PATCH v2 11/28 module: Use RCU in __is_module_percpu_address().
In reply to Sebastian Andrzej SiewiorThe modules list can be accessed under RCU assumption.
Use RCU protection instead preempt_disable().
--- kernel/module/main.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/module/main.c +1 -5
@@ -450,8 +450,7 @@ bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)struct module *mod;unsigned int cpu; -preempt_disable (); - +guard (rcu)();list_for_each_entry_rcu (mod, &modules, list) {if (mod->state ==MODULE_STATE_UNFORMED )continue ;
@@ -468,13 +467,10 @@ bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)per_cpu_ptr (mod->percpu,get_boot_cpu_id ()); } -preempt_enable ();return true; } } } - -preempt_enable ();return false; }
Signature · 1 line
2.45.2
PATCH v2 12/28 module: Allow __module_address() to be called from RCU section.
In reply to Sebastian Andrzej Siewiormod_find() uses either the modules list to find a module or a tree lookup (CONFIG_MODULES_TREE_LOOKUP). The list and the tree can both be iterated under RCU assumption (as well as RCU-sched).
Remove module_assert_mutex_or_preempt() from __module_address() and entirely since __module_address() is the last user. Update comments.
--- kernel/module/internal.h | 11 ----------- kernel/module/main.c | 4 +--- 2 files changed, 1 insertion(+), 14 deletions(-)
kernel/module/internal.h +0 -11
@@ -122,17 +122,6 @@ char *module_next_tag_pair(char *string, unsigned long *secsize);#define for_each_modinfo_entry (entry, info, name) \ for (entry = get_modinfo(info, name); entry; entry = get_next_modinfo(info, name, entry)) -static inline void module_assert_mutex_or_preempt (void ) -{ -#ifdef CONFIG_LOCKDEP -if (unlikely (!debug_locks)) -return ; - -WARN_ON_ONCE (!rcu_read_lock_sched_held () && - !lockdep_is_held (&module_mutex)); -#endif -} -static inline unsigned long kernel_symbol_value (const struct kernel_symbol *sym) {#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
kernel/module/main.c +1 -3
@@ -3667,7 +3667,7 @@ bool is_module_address(unsigned long addr)* __module_address() - get the module which contains an address. * @addr: the address. * -* Must be called with preempt disabled or module mutex held so that +* Must be called within RCU read section or module mutex held so that * module doesn't get freed during this. */ struct module *__module_address (unsigned long addr)
@@ -3685,8 +3685,6 @@ struct module *__module_address(unsigned long addr)return NULL ; lookup: -module_assert_mutex_or_preempt (); - mod =mod_find (addr, &mod_tree);if (mod) {BUG_ON (!within_module (addr, mod));
Signature · 1 line
2.45.2
PATCH v2 13/28 module: Use RCU in search_module_extables().
In reply to Sebastian Andrzej Siewiorsearch_module_extables() returns an exception_table_entry belonging to a module. The lookup via __module_address() can be performed with RCU protection.
The returned exception_table_entry remains valid because the passed address usually belongs to a module that is currently executed. So the module can not be removed because "something else" holds a reference to it, ensuring that it can not be removed.
Exceptions here are:
- kprobe, acquires a reference on the module beforehand
- MCE, invokes the function from within a timer and the RCU lifetime guarantees (of the timer) are sufficient.
Therefore it is safe to return the exception_table_entry outside the RCU section which provided the module.
Use RCU for the lookup in search_module_extables() and update the comment.
--- kernel/module/main.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-)
kernel/module/main.c +9 -14
@@ -3621,28 +3621,23 @@ char *module_flags(struct module *mod, char *buf, bool show_state)/* Given an address, look for it in the module exception tables. */ const struct exception_table_entry *search_module_extables (unsigned long addr) { -const struct exception_table_entry *e =NULL ;struct module *mod; -preempt_disable (); +guard (rcu)(); mod =__module_address (addr);if (!mod) - goto out; +return NULL ;if (!mod->num_exentries) - goto out; - - e =search_extable (mod->extable, - mod->num_exentries, - addr); -out: -preempt_enable (); - +return NULL ;/* -* Now, if we found one, we are running inside it now, hence -* we cannot unload the module, hence no refcnt needed. +* The address passed here belongs to a module that is currently +* invoked (we are running inside it). Therefore its module::refcnt +* needs already be >0 to ensure that it is not removed at this stage. +* All other user need to invoke this function within a RCU read +* section. */ -return e; +return search_extable (mod->extable, mod->num_exentries, addr); }/**
Signature · 1 line
2.45.2
PATCH v2 14/28 module: Use RCU in all users of __module_address().
In reply to Sebastian Andrzej Siewior__module_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_address() with RCU.
--- include/linux/kallsyms.h | 3 +-- kernel/module/kallsyms.c | 5 +---- kernel/module/main.c | 9 ++------- 3 files changed, 4 insertions(+), 13 deletions(-)
include/linux/kallsyms.h +1 -2
@@ -55,12 +55,11 @@ static inline void *dereference_symbol_descriptor(void *ptr)if (is_ksym_addr ((unsigned long )ptr))return ptr; -preempt_disable (); +guard (rcu)(); mod =__module_address ((unsigned long )ptr);if (mod) ptr =dereference_module_function_descriptor (mod, ptr); -preempt_enable ();#endif return ptr; }
kernel/module/kallsyms.c +1 -4
@@ -316,7 +316,7 @@ void * __weak dereference_module_function_descriptor(struct module *mod,/* * For kallsyms to ask for address resolution. NULL means not found. Careful -* not to lock to avoid deadlock on oopses, simply disable preemption. +* not to lock to avoid deadlock on oopses, RCU is enough. */ int module_address_lookup(unsigned long addr, unsigned long *size,
@@ -330,7 +330,6 @@ int module_address_lookup(unsigned long addr,struct module *mod;guard (rcu)(); -preempt_disable (); mod =__module_address (addr);if (mod) {if (modname)
@@ -348,8 +347,6 @@ int module_address_lookup(unsigned long addr,if (sym) ret =strscpy (namebuf, sym,KSYM_NAME_LEN ); } -preempt_enable (); -return ret; }
kernel/module/main.c +2 -7
@@ -3649,13 +3649,8 @@ const struct exception_table_entry *search_module_extables(unsigned long addr)*/ bool is_module_address (unsigned long addr) { -bool ret; - -preempt_disable (); - ret =__module_address (addr) !=NULL ; -preempt_enable (); - -return ret; +guard (rcu)(); +return __module_address (addr) !=NULL ; }/**
Signature · 1 line
2.45.2
PATCH v2 15/28 module: Use RCU in all users of __module_text_address().
In reply to Sebastian Andrzej Siewior__module_text_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_text_address() with RCU.
--- kernel/module/main.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)
kernel/module/main.c +5 -11
@@ -823,13 +823,12 @@ void symbol_put_addr(void *addr)/* * Even though we hold a reference on the module; we still need to -* disable preemption in order to safely traverse the data structure. +* RCU read section in order to safely traverse the data structure. */ -preempt_disable (); +guard (rcu)(); modaddr =__module_text_address (a);BUG_ON (!modaddr);module_put (modaddr); -preempt_enable (); }EXPORT_SYMBOL_GPL (symbol_put_addr);
@@ -3694,20 +3693,15 @@ struct module *__module_address(unsigned long addr)*/ bool is_module_text_address (unsigned long addr) { -bool ret; - -preempt_disable (); - ret =__module_text_address (addr) !=NULL ; -preempt_enable (); - -return ret; +guard (rcu)(); +return __module_text_address (addr) !=NULL ; }/** * __module_text_address() - get the module whose code contains an address. * @addr: the address. * -* Must be called with preempt disabled or module mutex held so that +* Must be called within RCU read section or module mutex held so that * module doesn't get freed during this. */ struct module *__module_text_address (unsigned long addr)
Signature · 1 line
2.45.2
PATCH v2 16/28 ARM: module: Use RCU in all users of __module_text_address().
In reply to Sebastian Andrzej Siewior__module_text_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_text_address() with RCU.
--- arch/arm/kernel/module-plts.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
arch/arm/kernel/module-plts.c +1 -3
@@ -285,11 +285,9 @@ bool in_module_plt(unsigned long loc)struct module *mod;bool ret; -preempt_disable (); +guard (rcu)(); mod =__module_text_address (loc); ret = mod && (loc - (u32 )mod->arch.core.plt_ent < mod->arch.core.plt_count *PLT_ENT_SIZE || loc - (u32 )mod->arch.init.plt_ent < mod->arch.init.plt_count *PLT_ENT_SIZE ); -preempt_enable (); -return ret; }
Signature · 1 line
2.45.2
PATCH v2 17/28 arm64: module: Use RCU in all users of __module_text_address().
In reply to Sebastian Andrzej Siewior__module_text_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_text_address() with RCU.
--- arch/arm64/kernel/ftrace.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
arch/arm64/kernel/ftrace.c +3 -4
@@ -257,14 +257,13 @@ static bool ftrace_find_callable_addr(struct dyn_ftrace *rec,* dealing with an out-of-range condition, we can assume it * is due to a module being loaded far away from the kernel. * -* NOTE: __module_text_address() must be called with preemption -* disabled, but we can rely on ftrace_lock to ensure that 'mod' +* NOTE: __module_text_address() must be called within a RCU read +* section, but we can rely on ftrace_lock to ensure that 'mod' * retains its validity throughout the remainder of this code. */ if (!mod) { -preempt_disable (); +guard (rcu)(); mod =__module_text_address (pc); -preempt_enable (); }if (WARN_ON (!mod))
Signature · 1 line
2.45.2
PATCH v2 18/28 LoongArch/orc: Use RCU in all users of __module_address().
In reply to Sebastian Andrzej Siewior__module_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_address() with RCU.
--- arch/loongarch/kernel/unwind_orc.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
arch/loongarch/kernel/unwind_orc.c +1 -3
@@ -399,7 +399,7 @@ bool unwind_next_frame(struct unwind_state *state)return false;/* Don't let modules unload while we're reading their ORC data. */ -preempt_disable (); +guard (rcu)();if (is_entry_func (state->pc)) goto end;
@@ -514,14 +514,12 @@ bool unwind_next_frame(struct unwind_state *state)if (!__kernel_text_address (state->pc)) goto err; -preempt_enable ();return true; err: state->error = true; end: -preempt_enable (); state->stack_info.type =STACK_TYPE_UNKNOWN ;return false; }
Signature · 1 line
2.45.2
PATCH v2 19/28 LoongArch: ftrace: Use RCU in all users of __module_text_address().
In reply to Sebastian Andrzej Siewior__module_text_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_text_address() with RCU.
--- arch/loongarch/kernel/ftrace_dyn.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
arch/loongarch/kernel/ftrace_dyn.c +3 -4
@@ -85,14 +85,13 @@ static bool ftrace_find_callable_addr(struct dyn_ftrace *rec, struct module *mod* dealing with an out-of-range condition, we can assume it * is due to a module being loaded far away from the kernel. * -* NOTE: __module_text_address() must be called with preemption -* disabled, but we can rely on ftrace_lock to ensure that 'mod' +* NOTE: __module_text_address() must be called within a RCU read +* section, but we can rely on ftrace_lock to ensure that 'mod' * retains its validity throughout the remainder of this code. */ if (!mod) { -preempt_disable (); +guard (rcu)(); mod =__module_text_address (pc); -preempt_enable (); }if (WARN_ON (!mod))
Signature · 1 line
2.45.2
Steven Rostedt
maintainerRe: [PATCH v2 19/28] LoongArch: ftrace: Use RCU in all users of __module_text_address().
Quoted 11 lines · On Fri, 20 Dec 2024 18:41:33 +0100 Sebastian Andrzej Siewior…
arch/loongarch/kernel/ftrace_dyn.c +2
-2@@ -85,14 +85,13 @@ static bool ftrace_find_callable_addr(struct dyn_ftrace *rec, struct module *mod* dealing with an out-of-range condition, we can assume it * is due to a module being loaded far away from the kernel. * -* NOTE: __module_text_address() must be called with preemption -* disabled, but we can rely on ftrace_lock to ensure that 'mod' +* NOTE: __module_text_address() must be called within a RCU read +* section, but we can rely on ftrace_lock to ensure that 'mod'
arch/loongarch/kernel/ftrace_dyn.c static bool ftrace_find_callable_addr(struct dyn_ftrace *rec, struct module *mod · line 97
Quoted 10 lines
arch/loongarch/kernel/ftrace_dyn.c +1
-2* retains its validity throughout the remainder of this code. */ if (!mod) { -preempt_disable (); +guard (rcu)(); mod =__module_text_address (pc); -preempt_enable (); }if (WARN_ON (!mod))
I personally dislike swapping one line of protection for the guard() code.
Although, I do think scoped_guard() can work.
That is:
if (!mod) {
read_rcu_lock();
mod = __module_text_address(pc);
read_rcu_unlock();
}Is easier to understand than:
if (!mod) {
guard(rcu)();
mod = __module_text_address(pc);
}Because it makes me wonder, why use a guard() for a one liner?
But, when I saw your other patch, if we had:
if (!mod) {
scoped_guard(rcu)()
mod = __module_text_address(pc);
}To me, hat looks much better than the guard() as it is obvious to what the code is protecting. Even though, I still prefer the explicit, lock/unlock. Maybe, just because I'm more used to it.
IMHO, guard() is for complex functions that are error prone. A single line is not something that is error prone (unless you don't match the lock and unlock properly, but that's pretty obvious when that happens).
But this is just my own opinion.
-- Steve
Re: [PATCH v2 19/28] LoongArch: ftrace: Use RCU in all users of __module_text_address().
On 2024-12-27 12:19:46 [-0500], Steven Rostedt wrote:
…
Quoted 8 lines
Is easier to understand than:
if (!mod) { guard(rcu)(); mod = __module_text_address(pc); }Because it makes me wonder, why use a
guard()for a one liner?
Why not? The context ends immediately after.
Quoted 6 lines
But, when I saw your other patch, if we had:
if (!mod) { scoped_guard(rcu)() mod = __module_text_address(pc); }
Okay, if this looks better, let me update it. It just you already have a scope (the {} after the if) and then we start yet another block. But if this looks better so be it.
Quoted 3 lines
To me, hat looks much better than the
guard()as it is obvious to what the code is protecting. Even though, I still prefer the explicit, lock/unlock. Maybe, just because I'm more used to it.
I'm probably also used to the explicit part but numerous people said to use this from now on. And it results in less lines and you don't have to worry about each return statement. So it somehow looks/ feels line an upgrade.
Quoted 3 lines
IMHO,
guard()is for complex functions that are error prone. A single line is not something that is error prone (unless you don't match the lock and unlock properly, but that's pretty obvious when that happens).
True but this is now a one liner which might be extended later on. Also the context is one line.
Quoted 3 lines
But this is just my own opinion.
-- Steve
Sebastian
PATCH v2 20/28 powerpc/ftrace: Use RCU in all users of __module_text_address().
In reply to Sebastian Andrzej Siewior__module_text_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_text_address() with RCU.
--- arch/powerpc/kernel/trace/ftrace.c | 6 ++---- arch/powerpc/kernel/trace/ftrace_64_pg.c | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +2 -4
@@ -115,10 +115,8 @@ static unsigned long ftrace_lookup_module_stub(unsigned long ip, unsigned long a {struct module *mod =NULL ; -preempt_disable (); - mod =__module_text_address (ip); -preempt_enable (); - +scoped_guard (rcu) + mod =__module_text_address (ip);if (!mod)pr_err ("No module loaded at addr=%lx\n" , ip);
arch/powerpc/kernel/trace/ftrace_64_pg.c +2 -4
@@ -120,10 +120,8 @@ static struct module *ftrace_lookup_module(struct dyn_ftrace *rec) {struct module *mod; -preempt_disable (); - mod =__module_text_address (rec->ip); -preempt_enable (); - +scoped_guard (rcu) + mod =__module_text_address (rec->ip);if (!mod)pr_err ("No module loaded at addr=%lx\n" , rec->ip);
Signature · 1 line
2.45.2
PATCH v2 21/28 x86: Use RCU in all users of __module_address().
In reply to Sebastian Andrzej Siewior__module_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_address() with RCU.
--- arch/x86/kernel/callthunks.c | 3 +-- arch/x86/kernel/unwind_orc.c | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-)
arch/x86/kernel/callthunks.c +1 -2
@@ -98,11 +98,10 @@ static inline bool within_module_coretext(void *addr)#ifdef CONFIG_MODULES struct module *mod; -preempt_disable (); +guard (rcu)(); mod =__module_address ((unsigned long )addr);if (mod &&within_module_core ((unsigned long )addr, mod)) ret = true; -preempt_enable ();#endif return ret; }
arch/x86/kernel/unwind_orc.c +1 -3
@@ -476,7 +476,7 @@ bool unwind_next_frame(struct unwind_state *state)return false;/* Don't let modules unload while we're reading their ORC data. */ -preempt_disable (); +guard (rcu)();/* End-of-stack check for user tasks: */ if (state->regs &&user_mode (state->regs))
@@ -669,14 +669,12 @@ bool unwind_next_frame(struct unwind_state *state) goto err; } -preempt_enable ();return true; err: state->error = true; the_end: -preempt_enable (); state->stack_info.type =STACK_TYPE_UNKNOWN ;return false; }
Signature · 1 line
2.45.2
PATCH v2 22/28 jump_label: Use RCU in all users of __module_address().
In reply to Sebastian Andrzej Siewior__module_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_address() with RCU.
--- kernel/jump_label.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
kernel/jump_label.c +9 -9
@@ -746,9 +746,9 @@ static int jump_label_add_module(struct module *mod)kfree (jlm);return -ENOMEM ; } -preempt_disable (); - jlm2->mod =__module_address ((unsigned long )key); -preempt_enable (); +scoped_guard (rcu) + jlm2->mod = __module_address((unsigned long)key); + jlm2->entries =static_key_entries (key); jlm2->next =NULL ;static_key_set_mod (key, jlm2);
@@ -906,13 +906,13 @@ static void jump_label_update(struct static_key *key) return; } -preempt_disable (); - mod =__module_address ((unsigned long )key); -if (mod) { - stop = mod->jump_entries + mod->num_jump_entries; - init = mod->state ==MODULE_STATE_COMING ; +scoped_guard (rcu) { + mod =__module_address ((unsigned long )key); +if (mod) { + stop = mod->jump_entries + mod->num_jump_entries; + init = mod->state ==MODULE_STATE_COMING ; + } } -preempt_enable ();#endif entry =static_key_entries (key);/* if there are no users, entry can be NULL */
Signature · 1 line
2.45.2
PATCH v2 23/28 jump_label: Use RCU in all users of __module_text_address().
In reply to Sebastian Andrzej Siewior__module_text_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_text_address() with RCU.
--- kernel/jump_label.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
kernel/jump_label.c +6 -7
@@ -653,13 +653,12 @@ static int __jump_label_mod_text_reserved(void *start, void *end)struct module *mod;int ret; -preempt_disable (); - mod =__module_text_address ((unsigned long )start); -WARN_ON_ONCE (__module_text_address ((unsigned long )end) != mod); -if (!try_module_get (mod)) - mod =NULL ; -preempt_enable (); - +scoped_guard (rcu) { + mod =__module_text_address ((unsigned long )start); +WARN_ON_ONCE (__module_text_address ((unsigned long )end) != mod); +if (!try_module_get (mod)) + mod =NULL ; + }if (!mod)return 0 ;
Signature · 1 line
2.45.2
PATCH v2 24/28 bpf: Use RCU in all users of __module_text_address().
In reply to Sebastian Andrzej Siewior__module_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_address() with RCU.
--- kernel/trace/bpf_trace.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)
kernel/trace/bpf_trace.c +8 -11
@@ -2336,10 +2336,9 @@ void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp) {struct module *mod; -preempt_disable (); +guard (rcu)(); mod =__module_address ((unsigned long )btp);module_put (mod); -preempt_enable (); } static __always_inline
@@ -2907,16 +2906,14 @@ static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u3for (i =0 ; i < addrs_cnt; i++) {struct module *mod; -preempt_disable (); - mod =__module_address (addrs[i]); -/* Either no module or we it's already stored */ -if (!mod ||has_module (&arr, mod)) { -preempt_enable (); -continue ; +scoped_guard (rcu) { + mod =__module_address (addrs[i]); +/* Either no module or we it's already stored */ +if (!mod ||has_module (&arr, mod)) +continue ; +if (!try_module_get (mod)) + err = -EINVAL ; } -if (!try_module_get (mod)) - err = -EINVAL ; -preempt_enable ();if (err)break ; err =add_module (&arr, mod);
Signature · 1 line
2.45.2
PATCH v2 25/28 kprobes: Use RCU in all users of __module_text_address().
In reply to Sebastian Andrzej Siewior__module_text_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_text_address() with RCU.
--- kernel/kprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/kprobes.c +2 -2
@@ -1566,7 +1566,7 @@ static int check_kprobe_address_safe(struct kprobe *p,if (ret)return ret;jump_label_lock (); -preempt_disable (); +rcu_read_lock ();/* Ensure the address is in a text area, and find a module if exists. */ *probed_mod =NULL ;
@@ -1612,7 +1612,7 @@ static int check_kprobe_address_safe(struct kprobe *p, } out: -preempt_enable (); +rcu_read_unlock ();jump_label_unlock ();return ret;
Signature · 1 line
2.45.2
PATCH v2 26/28 static_call: Use RCU in all users of __module_text_address().
In reply to Sebastian Andrzej Siewior__module_text_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
Replace the preempt_disable() section around __module_text_address() with RCU.
--- kernel/static_call_inline.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
kernel/static_call_inline.c +6 -7
@@ -325,13 +325,12 @@ static int __static_call_mod_text_reserved(void *start, void *end)struct module *mod;int ret; -preempt_disable (); - mod =__module_text_address ((unsigned long )start); -WARN_ON_ONCE (__module_text_address ((unsigned long )end) != mod); -if (!try_module_get (mod)) - mod =NULL ; -preempt_enable (); - +scoped_guard (rcu) { + mod =__module_text_address ((unsigned long )start); +WARN_ON_ONCE (__module_text_address ((unsigned long )end) != mod); +if (!try_module_get (mod)) + mod =NULL ; + }if (!mod)return 0 ;
Signature · 1 line
2.45.2
PATCH v2 27/28 bug: Use RCU instead RCU-sched to protect module_bug_list.
In reply to Sebastian Andrzej SiewiorThe list module_bug_list relies on module_mutex for writer synchronisation. The list is already RCU style.
The list removal is synchronized with modules' synchronize_rcu() in free_module().
Use RCU read lock protection instead of RCU-sched.
--- lib/bug.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
lib/bug.c +9 -13
@@ -66,23 +66,19 @@ static LIST_HEAD(module_bug_list);static struct bug_entry *module_find_bug (unsigned long bugaddr) { +struct bug_entry *bug;struct module *mod; -struct bug_entry *bug =NULL ; -rcu_read_lock_sched (); +guard (rcu)();list_for_each_entry_rcu (mod, &module_bug_list, bug_list) {unsigned i; bug = mod->bug_table;for (i =0 ; i < mod->num_bugs; ++i, ++bug)if (bugaddr ==bug_addr (bug)) - goto out; +return bug; } - bug =NULL ; -out: -rcu_read_unlock_sched (); - -return bug; +return NULL ; } void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
@@ -235,11 +231,11 @@ void generic_bug_clear_once(void)#ifdef CONFIG_MODULES struct module *mod; -rcu_read_lock_sched (); -list_for_each_entry_rcu (mod, &module_bug_list, bug_list) -clear_once_table (mod->bug_table, - mod->bug_table + mod->num_bugs); -rcu_read_unlock_sched (); +scoped_guard (rcu) { +list_for_each_entry_rcu (mod, &module_bug_list, bug_list) +clear_once_table (mod->bug_table, + mod->bug_table + mod->num_bugs); + }#endif clear_once_table (__start___bug_table, __stop___bug_table);
Signature · 1 line
2.45.2
PATCH v2 28/28 cfi: Use RCU while invoking __module_address().
In reply to Sebastian Andrzej Siewior__module_address() can be invoked within a RCU section, there is no requirement to have preemption disabled.
I'm not sure if using rcu_read_lock() will introduce the regression that has been fixed in commit 14c4c8e41511a ("cfi: Use rcu_read_{un}lock_sched_notrace").
--- kernel/cfi.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/cfi.c +4 -0
@@ -71,6 +71,10 @@ static bool is_module_cfi_trap(unsigned long addr)struct module *mod;bool found = false; +/* +* XXX this could be RCU protected but would it introcude the regression +* fixed in 14c4c8e41511a ("cfi: Use rcu_read_{un}lock_sched_notrace") +*/ rcu_read_lock_sched_notrace (); mod =__module_address (addr);
Signature · 1 line
2.45.2
Petr Pavlu
maintainerRe: [PATCH v2 28/28] cfi: Use RCU while invoking __module_address().
Quoted 23 lines · Sebastian Andrzej Siewior
__module_address()can be invoked within a RCU section, there is no requirement to have preemption disabled.I'm not sure if using
rcu_read_lock()will introduce the regression that has been fixed in commit14c4c8e41511a("cfi: Use rcu_read_{un}lock_sched_notrace").Cc Elliot Berman <[email protected]>Cc Kees Cook <[email protected]>Cc Nathan Chancellor <[email protected]>Cc Sami Tolvanen <[email protected]>Cc Steven Rostedt <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>--- kernel/cfi.c | 4 ++++ 1 file changed, 4 insertions(+)kernel/cfi.c +0
-0
kernel/cfi.c static bool is_module_cfi_trap(unsigned long addr) · line 80
Quoted 10 lines
kernel/cfi.c +4
-0@@ -71,6 +71,10 @@ static bool is_module_cfi_trap(unsigned long addr)struct module *mod;bool found = false; +/* +* XXX this could be RCU protected but would it introcude the regression +* fixed in 14c4c8e41511a ("cfi: Use rcu_read_{un}lock_sched_notrace") +*/ rcu_read_lock_sched_notrace (); mod =__module_address (addr);
I think that since 89245600941e ("cfi: Switch to -fsanitize=kcfi"), this can be a call to rcu_read_lock_sched(), or in your case rcu_read_lock(). The recursive case where __cfi_slowpath_diag() could end up calling itself is no longer present, as all that logic is gone. I then don't see another reason this should use the notrace variant.
@Sami, could you please confirm this?
Signature · 2 lines
Thanks, Petr
Sami Tolvanen
maintainerRe: [PATCH v2 28/28] cfi: Use RCU while invoking __module_address().
Hi Petr,
Quoted 42 lines · Petr Pavlu
Quoted 23 lines · Sebastian Andrzej Siewior
__module_address()can be invoked within a RCU section, there is no requirement to have preemption disabled.I'm not sure if using
rcu_read_lock()will introduce the regression that has been fixed in commit14c4c8e41511a("cfi: Use rcu_read_{un}lock_sched_notrace").Cc Elliot Berman <[email protected]>Cc Kees Cook <[email protected]>Cc Nathan Chancellor <[email protected]>Cc Sami Tolvanen <[email protected]>Cc Steven Rostedt <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>--- kernel/cfi.c | 4 ++++ 1 file changed, 4 insertions(+)kernel/cfi.c +0
-0kernel/cfi.c static bool is_module_cfi_trap(unsigned long addr) · line 80
Quoted 10 lines
kernel/cfi.c +4
-0@@ -71,6 +71,10 @@ static bool is_module_cfi_trap(unsigned long addr)struct module *mod;bool found = false; +/* +* XXX this could be RCU protected but would it introcude the regression +* fixed in 14c4c8e41511a ("cfi: Use rcu_read_{un}lock_sched_notrace") +*/ rcu_read_lock_sched_notrace (); mod =__module_address (addr);I think that since
89245600941e("cfi: Switch to -fsanitize=kcfi"), this can be a call torcu_read_lock_sched(), or in your casercu_read_lock(). The recursive case where__cfi_slowpath_diag()could end up calling itself is no longer present, as all that logic is gone. I then don't see another reason this should use the notrace variant.
Quoted 1 line
@Sami, could you please confirm this?
Switching is_module_cfi_trap() to use rcu_read_lock() in this series should be fine. KCFI checks don't perform function calls, so there's no risk of recursion, and this function is only called during the error handling path.
Sami
Re: [PATCH v2 28/28] cfi: Use RCU while invoking __module_address().
In reply to Sebastian Andrzej SiewiorQuoted 7 lines · Sebastian Andrzej Siewior
__module_address()can be invoked within a RCU section, there is no requirement to have preemption disabled.I'm not sure if using
rcu_read_lock()will introduce the regression that has been fixed in commit14c4c8e41511a("cfi: Use rcu_read_{un}lock_sched_notrace").
You can replace the rcu_read_lock_sched_notrace() with guard(rcu)(). Regular rcu lock doesn't generate function traces, so the recursive loop isn't possible.
I've tested:
- the current kernel (no recursive loop)
- Revert back to rcu_read_lock_sched() (fails)
- Your series as-is (no recurisve loop)
- Replace with guard(rcu)() (no recursive loop)
Whether you'd like to stick with the current patch or replace with guard(rcu)():
-
I don't know why I didn't mention steps to reproduce, even for my own benefit. Lesson learned :)
Here are the steps to reproduce; you'll need a system with support for
CFI: qemu arm64 probably does the trick and you'll need clang>=16. I'm happy to help test future revisions of this series since I have the setup all done.
``` modprobe -a dummy_stm stm_ftrace stm_p_basic mkdir -p /sys/kernel/config/stp-policy/dummy_stm.0.my-policy/default echo function > /sys/kernel/tracing/current_tracer echo 1 > /sys/kernel/tracing/tracing_on echo dummy_stm.0 > /sys/class/stm_source/ftrace/stm_source_link ```
The trace buffer should not be full of stm calls due to the recursive loop as mentioned in my original commit.
Regards,
Elliot Berman
Quoted 29 lines
Cc Elliot Berman <[email protected]>Cc Kees Cook <[email protected]>Cc Nathan Chancellor <[email protected]>Cc Sami Tolvanen <[email protected]>Cc Steven Rostedt <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>--- kernel/cfi.c | 4 ++++ 1 file changed, 4 insertions(+)kernel/cfi.c +4
-0@@ -71,6 +71,10 @@ static bool is_module_cfi_trap(unsigned long addr)struct module *mod;bool found = false; +/* +* XXX this could be RCU protected but would it introcude the regression +* fixed in 14c4c8e41511a ("cfi: Use rcu_read_{un}lock_sched_notrace") +*/ rcu_read_lock_sched_notrace (); mod =__module_address (addr);Signature · 1 line
2.45.2
Sami Tolvanen
maintainerRe: [PATCH v2 28/28] cfi: Use RCU while invoking __module_address().
Hi Elliot,
Quoted 10 lines · Elliot Berman
Quoted 7 lines · Sebastian Andrzej Siewior
__module_address()can be invoked within a RCU section, there is no requirement to have preemption disabled.I'm not sure if using
rcu_read_lock()will introduce the regression that has been fixed in commit14c4c8e41511a("cfi: Use rcu_read_{un}lock_sched_notrace").
Quoted 7 lines
You can replace the
rcu_read_lock_sched_notrace()with guard(rcu)(). Regular rcu lock doesn't generate function traces, so the recursive loop isn't possible.I've tested:
- the current kernel (no recursive loop)
- Revert back torcu_read_lock_sched()(fails)
Which kernel version did you test? I assume something pre-KCFI as arm64 doesn't use this code since v6.1.
Quoted 1 line
- Your series as-is (no recurisve loop)
Note that this patch only adds a comment to is_module_cfi_trap(), so I wouldn't expect a functional change.
Sami
Re: [PATCH v2 28/28] cfi: Use RCU while invoking __module_address().
Quoted 18 lines · Sami Tolvanen
Hi Elliot,
Quoted 14 lines · Elliot Berman
Quoted 7 lines · Sebastian Andrzej Siewior
__module_address()can be invoked within a RCU section, there is no requirement to have preemption disabled.I'm not sure if using
rcu_read_lock()will introduce the regression that has been fixed in commit14c4c8e41511a("cfi: Use rcu_read_{un}lock_sched_notrace").You can replace the
rcu_read_lock_sched_notrace()with guard(rcu)(). Regular rcu lock doesn't generate function traces, so the recursive loop isn't possible.
Quoted 7 lines
Quoted 3 lines
I've tested:
- the current kernel (no recursive loop)
- Revert back torcu_read_lock_sched()(fails)Which kernel version did you test? I assume something pre-KCFI as arm64 doesn't use this code since v6.1.
Ah, thanks for calling me out. I dug a bit more, I thought I was looking at a recursive loop in the ftrace buffers, but was actually the expected behavior. When I tested on the other configurations, the stm dummy driver hadn't kicked in yet by the time I looked at the ftrace. Indeed, this function code is not used on arm64.
I experimented with an x86 build as well and I was able to get the hang I remember seeing after some tweaks to force a CFI failure. Still, guard(rcu)() is okay by me :)
Quoted 5 lines
Quoted 1 line
- Your series as-is (no recurisve loop)
Note that this patch only adds a comment to
is_module_cfi_trap(), so I wouldn't expect a functional change.
Agreed I wouldn't expect it to make any issues; I mentioned it for completeness sake.
Regards,
Elliot
Sami Tolvanen
maintainerRe: [PATCH v2 28/28] cfi: Use RCU while invoking __module_address().
Hi,
Quoted 34 lines · Elliot Berman
Quoted 18 lines · Sami Tolvanen
Hi Elliot,
Quoted 14 lines · Elliot Berman
Quoted 7 lines · Sebastian Andrzej Siewior
__module_address()can be invoked within a RCU section, there is no requirement to have preemption disabled.I'm not sure if using
rcu_read_lock()will introduce the regression that has been fixed in commit14c4c8e41511a("cfi: Use rcu_read_{un}lock_sched_notrace").You can replace the
rcu_read_lock_sched_notrace()with guard(rcu)(). Regular rcu lock doesn't generate function traces, so the recursive loop isn't possible.Quoted 7 lines
Quoted 3 lines
I've tested:
- the current kernel (no recursive loop)
- Revert back torcu_read_lock_sched()(fails)Which kernel version did you test? I assume something pre-KCFI as arm64 doesn't use this code since v6.1.
Ah, thanks for calling me out. I dug a bit more, I thought I was looking at a recursive loop in the ftrace buffers, but was actually the expected behavior. When I tested on the other configurations, the stm dummy driver hadn't kicked in yet by the time I looked at the ftrace. Indeed, this function code is not used on arm64.
Quoted 3 lines
I experimented with an x86 build as well and I was able to get the hang I remember seeing after some tweaks to force a CFI failure. Still, guard(rcu)() is okay by me :)
OK, great. That makes sense. Thanks for taking the time to test this!
Sami
Re: [PATCH v2 28/28] cfi: Use RCU while invoking __module_address().
Quoted 1 line · Sami Tolvanen
Hi,
Hi,
Quoted 1 line
OK, great. That makes sense. Thanks for taking the time to test this!
Thank you two for testing, confirming and adding more informations to what and why. I take some of this for the patch description.
Quoted 1 line
Sami
Sebastian
Hi Sebastian,
Le 20/12/2024 à 18:41, Sebastian Andrzej Siewior a écrit :
Quoted 7 lines
Hi,
This is an updated version of the inital post after PeterZ made me aware that there are users outside of the module directory.
The goal is replace the mix aufrcu_read_lock(),rcu_read_lock_sched()andpreempt_disable()with justrcu_read_lock().
Quoted 8 lines
I've splitted it into smaller chunks which can be applied/ reviewed independently. I'm just not sure about the cfi patch (28/28) so I added just a comment instead.
v1…v2: https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fall%2F20241205215102.hRywUW2A%40linutronix.de%2F&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7Cbad0e7f9344a47371a0808dd211fdefd%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638703142988931970%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=a%2FV5g5zRqceJWedAAZJ4zt6XJpZ0Yqm%2BqKPrsItuEXs%3D&reserved=0
- Splitted into smaller patches.
- Converted all users.
How did you generate that cover letter ?
It should contain the full list of files modified by the series, so that I can see if any of them is of interest to me without going into each patch.
This is done automatically when you use 'git format-patch --cover-letter'.
Christophe
Quoted 1 line · Christophe Leroy
Hi Sebastian,
Hi Christophe,
Quoted 6 lines
How did you generate that cover letter ?
It should contain the full list of files modified by the series, so that I can see if any of them is of interest to me without going into each patch.
This is done automatically when you use 'git format-patch --cover-letter'.
I could do so for v3. This was format-patch without --cover-letter.
Quoted 1 line
Christophe
Sebastian
Quoted 7 lines · Sebastian Andrzej Siewior
Hi,
This is an updated version of the inital post after PeterZ made me aware that there are users outside of the module directory.
The goal is replace the mix aufrcu_read_lock(),rcu_read_lock_sched()andpreempt_disable()with justrcu_read_lock().
Quoted 7 lines
I've splitted it into smaller chunks which can be applied/ reviewed independently. I'm just not sure about the cfi patch (28/28) so I added just a comment instead.
v1…v2: https://lore.kernel.org/all/[email protected]/
- Splitted into smaller patches.
- Converted all users.
Thanks for doing the cleanup!