public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ftrace: protect executing nmi
@ 2009-03-16 12:54 Lai Jiangshan
  2009-03-16 17:42 ` Steven Rostedt
  0 siblings, 1 reply; 10+ messages in thread
From: Lai Jiangshan @ 2009-03-16 12:54 UTC (permalink / raw)
  To: Ingo Molnar, Steven Rostedt, LKML


When I review the sensitive code ftrace_nmi_enter(), I found
the atomic variable nmi_running does protect NMI VS do_ftrace_mod_code(),
but it can not protects NMI(entered nmi) VS NMI(ftrace_nmi_enter()).

cpu#1                   | cpu#2                 | cpu#3
ftrace_nmi_enter()      | do_ftrace_mod_code()  |
  not modify            |                       |
------------------------|-----------------------|--
executing               | set mod_code_write = 1|
executing             --|-----------------------|--------------------
executing               |                       | ftrace_nmi_enter()
executing               |                       |    do modify
------------------------|-----------------------|-----------------
ftrace_nmi_exit()       |                       |

cpu#3 may be being modified the code which is still being executed on cpu#1,
it will have undefined results and possibly take a GPF, this patch
prevents it occurred.

Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
---
diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c
index 1d0d7f4..e016f5e 100644
--- a/arch/x86/kernel/ftrace.c
+++ b/arch/x86/kernel/ftrace.c
@@ -87,7 +87,8 @@ static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  *
  * If an NMI is executed, the first thing it does is to call
  * "ftrace_nmi_enter". This will check if the flag is set to write
- * and if it is, it will write what is in the IP and "code" buffers.
+ * and if it is, and there is no executing nmi, it will write
+ * what is in the IP and "code" buffers.
  *
  * The trick is, it does not matter if everyone is writing the same
  * content to the code location. Also, if a CPU is executing code
@@ -96,6 +97,7 @@ static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  */
 
 static atomic_t nmi_running = ATOMIC_INIT(0);
+static atomic_t nmi_executing = ATOMIC_INIT(0);
 static int mod_code_status;		/* holds return value of text write */
 static int mod_code_write;		/* set when NMI should do the write */
 static void *mod_code_ip;		/* holds the IP to write to */
@@ -135,14 +137,18 @@ void ftrace_nmi_enter(void)
 	atomic_inc(&nmi_running);
 	/* Must have nmi_running seen before reading write flag */
 	smp_mb();
-	if (mod_code_write) {
+	if (!atomic_read(&nmi_executing) && mod_code_write) {
 		ftrace_mod_code();
 		atomic_inc(&nmi_update_count);
 	}
+	atomic_inc(&nmi_executing);
+	smp_mb();
 }
 
 void ftrace_nmi_exit(void)
 {
+	smp_mb();
+	atomic_dec(&nmi_executing);
 	/* Finish all executions before clearing nmi_running */
 	smp_wmb();
 	atomic_dec(&nmi_running);





^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2009-03-20 10:19 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-16 12:54 [PATCH] ftrace: protect executing nmi Lai Jiangshan
2009-03-16 17:42 ` Steven Rostedt
2009-03-17 12:54   ` [PATCH 1/2] ftrace: protect running nmi Lai Jiangshan
2009-03-17 12:58     ` [PATCH 2/2] ftrace: fast path for do_ftrace_mod_code() Lai Jiangshan
2009-03-17 14:39       ` Steven Rostedt
2009-03-18  7:02         ` Lai Jiangshan
2009-03-18  8:42     ` [PATCH] ftrace: protect running nmi (V3) Lai Jiangshan
2009-03-19  0:33       ` Steven Rostedt
2009-03-19  2:02         ` Lai Jiangshan
2009-03-20 10:18       ` [tip:tracing/ftrace] " Lai Jiangshan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox