Skip to content

Commit e412bba

Browse files
rostedtJiri Slaby
authored andcommitted
ftrace/module: Hardcode ftrace_module_init() call into load_module()
commit a949ae5 upstream. A race exists between module loading and enabling of function tracer. CPU 1 CPU 2 ----- ----- load_module() module->state = MODULE_STATE_COMING register_ftrace_function() mutex_lock(&ftrace_lock); ftrace_startup() update_ftrace_function(); ftrace_arch_code_modify_prepare() set_all_module_text_rw(); <enables-ftrace> ftrace_arch_code_modify_post_process() set_all_module_text_ro(); [ here all module text is set to RO, including the module that is loading!! ] blocking_notifier_call_chain(MODULE_STATE_COMING); ftrace_init_module() [ tries to modify code, but it's RO, and fails! ftrace_bug() is called] When this race happens, ftrace_bug() will produces a nasty warning and all of the function tracing features will be disabled until reboot. The simple solution is to treate module load the same way the core kernel is treated at boot. To hardcode the ftrace function modification of converting calls to mcount into nops. This is done in init/main.c there's no reason it could not be done in load_module(). This gives a better control of the changes and doesn't tie the state of the module to its notifiers as much. Ftrace is special, it needs to be treated as such. The reason this would work, is that the ftrace_module_init() would be called while the module is in MODULE_STATE_UNFORMED, which is ignored by the set_all_module_text_ro() call. Link: http://lkml.kernel.org/r/1395637826-3312-1-git-send-email-indou.takao@jp.fujitsu.com Reported-by: Takao Indoh <indou.takao@jp.fujitsu.com> Acked-by: Rusty Russell <rusty@rustcorp.com.au> Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent 923aef0 commit e412bba

3 files changed

Lines changed: 9 additions & 23 deletions

File tree

‎include/linux/ftrace.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_a
524524
extern int ftrace_arch_read_dyn_info(char *buf, int size);
525525

526526
extern int skip_trace(unsigned long ip);
527+
extern void ftrace_module_init(struct module *mod);
527528

528529
extern void ftrace_disable_daemon(void);
529530
extern void ftrace_enable_daemon(void);
@@ -533,6 +534,7 @@ static inline int ftrace_force_update(void) { return 0; }
533534
static inline void ftrace_disable_daemon(void) { }
534535
static inline void ftrace_enable_daemon(void) { }
535536
static inline void ftrace_release_mod(struct module *mod) {}
537+
static inline void ftrace_module_init(struct module *mod) {}
536538
static inline int register_ftrace_command(struct ftrace_func_command *cmd)
537539
{
538540
return -EINVAL;

‎kernel/module.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3296,6 +3296,9 @@ static int load_module(struct load_info *info, const char __user *uargs,
32963296

32973297
dynamic_debug_setup(info->debug, info->num_debug);
32983298

3299+
/* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3300+
ftrace_module_init(mod);
3301+
32993302
/* Finally it's fully formed, ready to start executing. */
33003303
err = complete_formation(mod, info);
33013304
if (err)

‎kernel/trace/ftrace.c‎

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4252,16 +4252,11 @@ static void ftrace_init_module(struct module *mod,
42524252
ftrace_process_locs(mod, start, end);
42534253
}
42544254

4255-
static int ftrace_module_notify_enter(struct notifier_block *self,
4256-
unsigned long val, void *data)
4255+
void ftrace_module_init(struct module *mod)
42574256
{
4258-
struct module *mod = data;
4259-
4260-
if (val == MODULE_STATE_COMING)
4261-
ftrace_init_module(mod, mod->ftrace_callsites,
4262-
mod->ftrace_callsites +
4263-
mod->num_ftrace_callsites);
4264-
return 0;
4257+
ftrace_init_module(mod, mod->ftrace_callsites,
4258+
mod->ftrace_callsites +
4259+
mod->num_ftrace_callsites);
42654260
}
42664261

42674262
static int ftrace_module_notify_exit(struct notifier_block *self,
@@ -4275,23 +4270,13 @@ static int ftrace_module_notify_exit(struct notifier_block *self,
42754270
return 0;
42764271
}
42774272
#else
4278-
static int ftrace_module_notify_enter(struct notifier_block *self,
4279-
unsigned long val, void *data)
4280-
{
4281-
return 0;
4282-
}
42834273
static int ftrace_module_notify_exit(struct notifier_block *self,
42844274
unsigned long val, void *data)
42854275
{
42864276
return 0;
42874277
}
42884278
#endif /* CONFIG_MODULES */
42894279

4290-
struct notifier_block ftrace_module_enter_nb = {
4291-
.notifier_call = ftrace_module_notify_enter,
4292-
.priority = INT_MAX, /* Run before anything that can use kprobes */
4293-
};
4294-
42954280
struct notifier_block ftrace_module_exit_nb = {
42964281
.notifier_call = ftrace_module_notify_exit,
42974282
.priority = INT_MIN, /* Run after anything that can remove kprobes */
@@ -4328,10 +4313,6 @@ void __init ftrace_init(void)
43284313
__start_mcount_loc,
43294314
__stop_mcount_loc);
43304315

4331-
ret = register_module_notifier(&ftrace_module_enter_nb);
4332-
if (ret)
4333-
pr_warning("Failed to register trace ftrace module enter notifier\n");
4334-
43354316
ret = register_module_notifier(&ftrace_module_exit_nb);
43364317
if (ret)
43374318
pr_warning("Failed to register trace ftrace module exit notifier\n");

0 commit comments

Comments
 (0)