module: Use complete RCU protection instead a mix of RCU and RCU-sched.
PATCH 1/1 module: Use complete RCU protection instead a mix of RCU and RCU-sched.
The 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.
Replace preempt-disable sections with RCU-read sections. Replace rcu_dereference_sched() with rcu_dereference(). Remove module_assert_mutex_or_preempt(), its goal is covered by the following RCU usage.
--- kernel/module/internal.h | 11 ------- kernel/module/kallsyms.c | 45 +++++++++------------------- kernel/module/main.c | 64 ++++++++++++---------------------------- kernel/module/tracking.c | 2 -- kernel/module/version.c | 11 ++++--- 5 files changed, 38 insertions(+), 95 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/kallsyms.c +14 -31
135 lines not on this page · raw message
kernel/module/main.c +19 -45
208 lines not on this page · raw message
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;
kernel/module/version.c +5 -6
@@ -65,14 +65,13 @@ int check_modstruct_version(const struct load_info *info,/* * 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 (); +scoped_guard (rcu) { +if (!find_symbol (&fsa)) +BUG (); } -preempt_enable ();return check_version (info,"module_layout" , mod, fsa.crc); }
Signature · 1 line
2.45.2
Quoted 26 lines · Sebastian Andrzej Siewior
The RCU usage in module was introduced in commit
d72b37513cdfb("Removestop_machineduring module load v2") and it claimed not to be RCU but similar. Then there was another improvement in commite91defa26c527("module: don't usestop_machineon module load"). It become a mix of RCU and RCU-sched and was eventually fixed0be964be0d450("module:
Sanitize RCU usage and locking"). Later RCU & RCU-sched was merged in commitcb2f55369d3a9("modules: Replacesynchronize_sched()andcall_rcu_sched()") so that was aligned.Looking at it today, there is still leftovers. The
preempt_disable()was used insteadrcu_read_lock_sched(). The RCU & RCU-sched merge was not complete as there is stillrcu_dereference_sched()formodule::kallsyms.The RCU-list modules and
unloaded_tainted_modulesare always accessed under RCU protection or themodule_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 asynchronize_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::kallsymsis first assigned inadd_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 alsosynchronize_rcu()indo_free_init(). That means A RCU read lock is enough for protection andrcu_dereference()can be safely used.
Quoted 6 lines
Replace preempt-disable sections with RCU-read sections. Replace
rcu_dereference_sched()withrcu_dereference(). Removemodule_assert_mutex_or_preempt(), its goal is covered by the following RCU usage.Signed-off-by Sebastian Andrzej Siewior <[email protected]>
Looks about right,
Quoted 31 lines · Sebastian Andrzej Siewior
The RCU usage in module was introduced in commit
d72b37513cdfb("Removestop_machineduring module load v2") and it claimed not to be RCU but similar. Then there was another improvement in commite91defa26c527("module: don't usestop_machineon module load"). It become a mix of RCU and RCU-sched and was eventually fixed0be964be0d450("module:
Sanitize RCU usage and locking"). Later RCU & RCU-sched was merged in commitcb2f55369d3a9("modules: Replacesynchronize_sched()andcall_rcu_sched()") so that was aligned.Looking at it today, there is still leftovers. The
preempt_disable()was used insteadrcu_read_lock_sched(). The RCU & RCU-sched merge was not complete as there is stillrcu_dereference_sched()formodule::kallsyms.The RCU-list modules and
unloaded_tainted_modulesare always accessed under RCU protection or themodule_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 asynchronize_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::kallsymsis first assigned inadd_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 alsosynchronize_rcu()indo_free_init(). That means A RCU read lock is enough for protection andrcu_dereference()can be safely used.Replace preempt-disable sections with RCU-read sections. Replace
rcu_dereference_sched()withrcu_dereference(). Removemodule_assert_mutex_or_preempt(), its goal is covered by the following RCU usage.
Quoted 8 lines
Signed-off-by Sebastian Andrzej Siewior <[email protected]>--- kernel/module/internal.h | 11 ------- kernel/module/kallsyms.c | 45 +++++++++------------------- kernel/module/main.c | 64 ++++++++++++---------------------------- kernel/module/tracking.c | 2 -- kernel/module/version.c | 11 ++++--- 5 files changed, 38 insertions(+), 95 deletions(-)
There's more in kernel/jump_label.c and possibly other sites, grep for __module_address.
Quoted 2 lines · Peter Zijlstra
There's more in
kernel/jump_label.cand possibly other sites, grep for__module_address.
Thank you.
Sebastian