From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754491AbbLYGqI (ORCPT ); Fri, 25 Dec 2015 01:46:08 -0500 Received: from mga14.intel.com ([192.55.52.115]:34405 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751513AbbLYGqD (ORCPT ); Fri, 25 Dec 2015 01:46:03 -0500 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,477,1444719600"; d="scan'208";a="623507784" Subject: [PATCH 1/2] ftrace: fix the race between ftrace and insmod To: Steven Rostedt , Ingo Molnar , Rusty Russell References: <567CE0A7.80605@intel.com> Cc: linux-kernel@vger.kernel.org, yanmin_zhang@linux.intel.com From: "Qiu, PeiyangX" Message-ID: <567CE628.3000609@intel.com> Date: Fri, 25 Dec 2015 14:46:00 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 In-Reply-To: <567CE0A7.80605@intel.com> Content-Type: text/plain; charset=gbk Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qiu Peiyang 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. Signed-off-by: Qiu Peiyang Signed-off-by: Zhang Yanmin --- kernel/trace/ftrace.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 64f865b..52d1908 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -4993,7 +4993,6 @@ static void ftrace_init_module(struct module *mod, if (ftrace_disabled || start == end) return; ftrace_process_locs(mod, start, end); - ftrace_module_enable(mod); } void ftrace_module_init(struct module *mod) @@ -5003,26 +5002,34 @@ void ftrace_module_init(struct module *mod) mod->num_ftrace_callsites); } -static int ftrace_module_notify_exit(struct notifier_block *self, +static int ftrace_module_notify(struct notifier_block *self, unsigned long val, void *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); + break; + default: + break; + } return 0; } #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) { return 0; } #endif /* CONFIG_MODULES */ -struct notifier_block ftrace_module_exit_nb = { - .notifier_call = ftrace_module_notify_exit, +struct notifier_block ftrace_module_nb = { + .notifier_call = ftrace_module_notify, .priority = INT_MIN, /* Run after anything that can remove kprobes */ }; @@ -5054,7 +5061,7 @@ void __init ftrace_init(void) __start_mcount_loc, __stop_mcount_loc); - ret = register_module_notifier(&ftrace_module_exit_nb); + ret = register_module_notifier(&ftrace_module_nb); if (ret) pr_warning("Failed to register trace ftrace module exit notifier\n"); -- 1.9.1