Commit 5156dca3 authored by Qiu Peiyang's avatar Qiu Peiyang Committed by Steven Rostedt

ftrace: Fix the race between ftrace and insmod

We hit ftrace_bug report when booting Android on a 64bit ATOM SOC chip.
Basically, there is a race between insmod and ftrace_run_update_code.

After load_module=>ftrace_module_init, another thread jumps in to call
ftrace_run_update_code=>ftrace_arch_code_modify_prepare
                        =>set_all_modules_text_rw, to change all modules
as RW. Since the new module is at MODULE_STATE_UNFORMED, the text attribute
is not changed. Then, the 2nd thread goes ahead to change codes.
However, load_module continues to call complete_formation=>set_section_ro_nx,
then 2nd thread would fail when probing the module's TEXT.

The patch fixes it by using notifier to delay the enabling of ftrace
records to the time when module is at state MODULE_STATE_COMING.

Link: http://lkml.kernel.org/r/567CE628.3000609@intel.comSigned-off-by: default avatarQiu Peiyang <peiyangx.qiu@intel.com>
Signed-off-by: default avatarZhang Yanmin <yanmin.zhang@intel.com>
Signed-off-by: default avatarSteven Rostedt <rostedt@goodmis.org>
parent b7ffffbb
...@@ -5037,29 +5037,36 @@ void ftrace_module_init(struct module *mod) ...@@ -5037,29 +5037,36 @@ void ftrace_module_init(struct module *mod)
ftrace_process_locs(mod, mod->ftrace_callsites, ftrace_process_locs(mod, mod->ftrace_callsites,
mod->ftrace_callsites + mod->num_ftrace_callsites); mod->ftrace_callsites + mod->num_ftrace_callsites);
ftrace_module_enable(mod);
} }
static int ftrace_module_notify_exit(struct notifier_block *self, static int ftrace_module_notify(struct notifier_block *self,
unsigned long val, void *data) unsigned long val, void *data)
{ {
struct module *mod = data; struct module *mod = data;
if (val == MODULE_STATE_GOING) switch (val) {
case MODULE_STATE_COMING:
ftrace_module_enable(mod);
break;
case MODULE_STATE_GOING:
ftrace_release_mod(mod); ftrace_release_mod(mod);
break;
default:
break;
}
return 0; return 0;
} }
#else #else
static int ftrace_module_notify_exit(struct notifier_block *self, static int ftrace_module_notify(struct notifier_block *self,
unsigned long val, void *data) unsigned long val, void *data)
{ {
return 0; return 0;
} }
#endif /* CONFIG_MODULES */ #endif /* CONFIG_MODULES */
struct notifier_block ftrace_module_exit_nb = { struct notifier_block ftrace_module_nb = {
.notifier_call = ftrace_module_notify_exit, .notifier_call = ftrace_module_notify,
.priority = INT_MIN, /* Run after anything that can remove kprobes */ .priority = INT_MIN, /* Run after anything that can remove kprobes */
}; };
...@@ -5091,7 +5098,7 @@ void __init ftrace_init(void) ...@@ -5091,7 +5098,7 @@ void __init ftrace_init(void)
__start_mcount_loc, __start_mcount_loc,
__stop_mcount_loc); __stop_mcount_loc);
ret = register_module_notifier(&ftrace_module_exit_nb); ret = register_module_notifier(&ftrace_module_nb);
if (ret) if (ret)
pr_warning("Failed to register trace ftrace module exit notifier\n"); pr_warning("Failed to register trace ftrace module exit notifier\n");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment