History

module: Use RCU in find_module_all().

The 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().

Sebastian Andrzej Siewior committed 1 year ago

Commit febaa651 parent c4fadf35 files · +8 −14Browse files

Unified · Split

5 files
  1. include/linux/module.h +1 −1
  2. kernel/livepatch/core.c +1 −3
  3. kernel/module/kallsyms.c +1 −0
  4. kernel/module/main.c +2 −4
  5. kernel/trace/trace_kprobe.c +3 −6
include/linux/module.h+1−1
@@ -666,7 +666,7 @@ static inline bool within_module(unsigned long addr, const struct module *mod) · 665 unmodified lines
666666 return within_module_init(addr, mod) || within_module_core(addr, mod);
667667 }
668668
669−/* Search for module by name: must be in a RCU-sched critical section. */
669+/* Search for module by name: must be in a RCU critical section. */
670670 struct module *find_module(const char *name);
671671
672672 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) · 58 unmodified lines
5959 if (!klp_is_module(obj))
6060 return;
6161
62− rcu_read_lock_sched();
62+ guard(rcu)();
6363 /*
6464 * We do not want to block removal of patched modules and therefore
6565 * 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) · 9 unmodified lines
7575 */
7676 if (mod && mod->klp_alive)
7777 obj->mod = mod;
78−
79− rcu_read_unlock_sched();
8078 }
8179
8280 static bool klp_initialized(void)
kernel/module/kallsyms.c+1−0
@@ -450,6 +450,7 @@ unsigned long module_kallsyms_lookup_name(const char *name) · 449 unmodified lines
450450 unsigned long ret;
451451
452452 /* Don't lock: we're in enough trouble already. */
453+ guard(rcu)();
453454 preempt_disable();
454455 ret = __module_kallsyms_lookup_name(name);
455456 preempt_enable();
kernel/module/main.c+2−4
@@ -374,16 +374,14 @@ bool find_symbol(struct find_symbol_arg *fsa) · 373 unmodified lines
374374 }
375375
376376 /*
377− * Search for module by name: must hold module_mutex (or preempt disabled
378− * for read-only access).
377+ * Search for module by name: must hold module_mutex (or RCU for read-only
378+ * access).
379379 */
380380 struct module *find_module_all(const char *name, size_t len,
381381 bool even_unformed)
382382 {
383383 struct module *mod;
384384
385− module_assert_mutex_or_preempt();
386−
387385 list_for_each_entry_rcu(mod, &modules, list,
388386 lockdep_is_held(&module_mutex)) {
389387 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
kernel/trace/trace_kprobe.c+3−6
@@ -124,9 +124,8 @@ static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk) · 123 unmodified lines
124124 if (!p)
125125 return true;
126126 *p = '\0';
127− rcu_read_lock_sched();
128− ret = !!find_module(tk->symbol);
129− rcu_read_unlock_sched();
127+ scoped_guard(rcu)
128+ ret = !!find_module(tk->symbol);
130129 *p = ':';
131130
132131 return ret;
@@ -796,12 +795,10 @@ static struct module *try_module_get_by_name(const char *name) · 663 unmodified lines
796795 {
797796 struct module *mod;
798797
799− rcu_read_lock_sched();
798+ guard(rcu)();
800799 mod = find_module(name);
801800 if (mod && !try_module_get(mod))
802801 mod = NULL;
803− rcu_read_unlock_sched();
804−
805802 return mod;
806803 }
807804 #else