Skip to content

Commit febaa65

Browse files
Sebastian Andrzej Siewiorpetrpavlu
authored andcommitted
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(). 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]> Cc: [email protected] Cc: [email protected] Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Petr Pavlu <[email protected]>
1 parent c4fadf3 commit febaa65

File tree

5 files changed

+8
-14
lines changed

5 files changed

+8
-14
lines changed

include/linux/module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ static inline bool within_module(unsigned long addr, const struct module *mod)
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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static void klp_find_object_module(struct klp_object *obj)
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)
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ unsigned long module_kallsyms_lookup_name(const char *name)
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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,14 @@ bool find_symbol(struct find_symbol_arg *fsa)
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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
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)
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

0 commit comments

Comments
 (0)