Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1298084 > unrolled thread

[PATCH 0/2] fix ftrace initialization issue when a module is loaded

Started by"Qiu, PeiyangX" <peiyangx.qiu@intel.com>
First post2015-12-25 07:30 +0100
Last post2015-12-25 08:10 +0100
Articles 4 — 1 participant

Back to article view | Back to linux.kernel


Contents

  [PATCH 0/2] fix ftrace initialization issue when a module is loaded "Qiu, PeiyangX" <peiyangx.qiu@intel.com> - 2015-12-25 07:30 +0100
    [PATCH 1/2] ftrace: fix the race between ftrace and insmod "Qiu, PeiyangX" <peiyangx.qiu@intel.com> - 2015-12-25 07:50 +0100
    [PATCH 2/2] module: deal with the failure of complete_formation "Qiu, PeiyangX" <peiyangx.qiu@intel.com> - 2015-12-25 08:10 +0100
    [PATCH 1/2] ftrace: fix the race between ftrace and insmod "Qiu, PeiyangX" <peiyangx.qiu@intel.com> - 2015-12-25 08:10 +0100

#1298084 — [PATCH 0/2] fix ftrace initialization issue when a module is loaded

From"Qiu, PeiyangX" <peiyangx.qiu@intel.com>
Date2015-12-25 07:30 +0100
Subject[PATCH 0/2] fix ftrace initialization issue when a module is loaded
Message-ID<qJu2Z-8rQ-1@gated-at.bofh.it>
When a module is loaded, current ftrace initialization around the new module
has some issues.

1) ftrace might race with insmod: Just after load_module calls
ftrace_module_init to add ftrace records of the module, ftrace_run_update_code
might jump in to change module codes. But load_module calls
complete_formation=>set_section_ro_nx to put the module TEXT attribute to RO.
Then, ftrace_run_update_code triggers ftrace_bug and fails.

2) complete_formation might fail and the module's ftrace records are not
cleaned up.

This patchset fixes above issues.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1298085 — [PATCH 1/2] ftrace: fix the race between ftrace and insmod

From"Qiu, PeiyangX" <peiyangx.qiu@intel.com>
Date2015-12-25 07:50 +0100
Subject[PATCH 1/2] ftrace: fix the race between ftrace and insmod
Message-ID<qJumm-6s-3@gated-at.bofh.it>
In reply to#1298084
From: Qiu Peiyang <peiyangx.qiu@intel.com>

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 <peiyangx.qiu@intel.com>
Signed-off-by: Zhang Yanmin <yanmin.zhang@intel.com>
---
 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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1298088 — [PATCH 2/2] module: deal with the failure of complete_formation

From"Qiu, PeiyangX" <peiyangx.qiu@intel.com>
Date2015-12-25 08:10 +0100
Subject[PATCH 2/2] module: deal with the failure of complete_formation
Message-ID<qJuFH-sh-3@gated-at.bofh.it>
In reply to#1298084
From: Qiu Peiyang <peiyangx.qiu@intel.com>

complete_formation might fail. kernel need clean up
ftrace records of the module.

The patch fixes it by tuning the operation sequence in
complete_formation. After complete_formation checks
verify_export_symbols, call ftrace_module_init to init
ftrace records.

Signed-off-by: Qiu Peiyang <peiyangx.qiu@intel.com>
Signed-off-by: Zhang Yanmin <yanmin.zhang@intel.com>
---
 kernel/module.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/kernel/module.c b/kernel/module.c
index 8f051a1..0a67c4e 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -3373,6 +3373,11 @@ static int complete_formation(struct module *mod, struct load_info *info)
 	/* This relies on module_mutex for list integrity. */
 	module_bug_finalize(info->hdr, info->sechdrs, mod);
 
+	mutex_unlock(&module_mutex);
+
+	/* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
+	ftrace_module_init(mod);
+
 	/* Set RO and NX regions for core */
 	set_section_ro_nx(mod->module_core,
 				mod->core_text_size,
@@ -3388,7 +3393,6 @@ static int complete_formation(struct module *mod, struct load_info *info)
 	/* Mark state as coming so strong_try_module_get() ignores us,
 	 * but kallsyms etc. can see us. */
 	mod->state = MODULE_STATE_COMING;
-	mutex_unlock(&module_mutex);
 
 	blocking_notifier_call_chain(&module_notify_list,
 				     MODULE_STATE_COMING, mod);
@@ -3505,9 +3509,6 @@ static int load_module(struct load_info *info, const char __user *uargs,
 
 	dynamic_debug_setup(info->debug, info->num_debug);
 
-	/* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
-	ftrace_module_init(mod);
-
 	/* Finally it's fully formed, ready to start executing. */
 	err = complete_formation(mod, info);
 	if (err)
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1298089 — [PATCH 1/2] ftrace: fix the race between ftrace and insmod

From"Qiu, PeiyangX" <peiyangx.qiu@intel.com>
Date2015-12-25 08:10 +0100
Subject[PATCH 1/2] ftrace: fix the race between ftrace and insmod
Message-ID<qJuFH-sh-1@gated-at.bofh.it>
In reply to#1298084
From: Qiu Peiyang <peiyangx.qiu@intel.com>

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 <peiyangx.qiu@intel.com>
Signed-off-by: Zhang Yanmin <yanmin.zhang@intel.com>
---
 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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web