linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: mingo@kernel.org, hpa@zytor.com, paulus@samba.org,
	linux-kernel@vger.kernel.org, acme@ghostprotocols.net,
	seiji.aguchi@hds.com, jolsa@redhat.com, vincent.weaver@maine.edu,
	tglx@linutronix.de, hpa@linux.intel.com,
	linux-tip-commits@vger.kernel.org
Subject: Re: [tip:x86/urgent] x86, trace: Fix CR2 corruption when tracing page faults
Date: Wed, 5 Mar 2014 13:54:21 +0100	[thread overview]
Message-ID: <20140305125421.GB9987@twins.programming.kicks-ass.net> (raw)
In-Reply-To: <20140305073344.2f179931@gandalf.local.home>

On Wed, Mar 05, 2014 at 07:33:44AM -0500, Steven Rostedt wrote:
> Then we better make sure that __do_page_fault() is never inlined.
> Otherwise, it wont be available to trace.
> 
> I'm fine with adding "notrace" to do_page_fault() and to
> trace_do_page_fault() as long as we also include a "noinline" to
> __do_page_fault(). Would need a comment stating why that noinline is
> there though.

When CONFIG_TRACING there's two callers, which makes it highly unlikely
GCC would inline the massive __do_page_fault() function, but sure.

How about something like so then; still has the normal_do_page_fault()
thing, although I suppose we could drop that.

It also puts trace_page_fault_entries() and trace_do_page_fault() under
CONFIG_TRACING. I could only find the entry_32.S user; I suppose the
64bit one is hidden by CPP goo somewhere?

---
 arch/x86/include/asm/traps.h |  2 +-
 arch/x86/kernel/entry_32.S   |  2 +-
 arch/x86/kernel/entry_64.S   |  2 +-
 arch/x86/kernel/kvm.c        |  2 +-
 arch/x86/mm/fault.c          | 42 +++++++++++++++++++++++++++---------------
 5 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/arch/x86/include/asm/traps.h b/arch/x86/include/asm/traps.h
index 58d66fe06b61..1280f72deea8 100644
--- a/arch/x86/include/asm/traps.h
+++ b/arch/x86/include/asm/traps.h
@@ -71,7 +71,7 @@ dotraplinkage void do_double_fault(struct pt_regs *, long);
 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *);
 #endif
 dotraplinkage void do_general_protection(struct pt_regs *, long);
-dotraplinkage void do_page_fault(struct pt_regs *, unsigned long);
+dotraplinkage void normal_do_page_fault(struct pt_regs *, unsigned long);
 #ifdef CONFIG_TRACING
 dotraplinkage void trace_do_page_fault(struct pt_regs *, unsigned long);
 #endif
diff --git a/arch/x86/kernel/entry_32.S b/arch/x86/kernel/entry_32.S
index a2a4f4697889..9a9f64755da8 100644
--- a/arch/x86/kernel/entry_32.S
+++ b/arch/x86/kernel/entry_32.S
@@ -1257,7 +1257,7 @@ END(trace_page_fault)
 ENTRY(page_fault)
 	RING0_EC_FRAME
 	ASM_CLAC
-	pushl_cfi $do_page_fault
+	pushl_cfi $normal_do_page_fault
 	ALIGN
 error_code:
 	/* the function address is in %gs's slot on the stack */
diff --git a/arch/x86/kernel/entry_64.S b/arch/x86/kernel/entry_64.S
index 1e96c3628bf2..7d49812741ac 100644
--- a/arch/x86/kernel/entry_64.S
+++ b/arch/x86/kernel/entry_64.S
@@ -1491,7 +1491,7 @@ zeroentry xen_int3 do_int3
 errorentry xen_stack_segment do_stack_segment
 #endif
 errorentry general_protection do_general_protection
-trace_errorentry page_fault do_page_fault
+trace_errorentry page_fault normal_do_page_fault
 #ifdef CONFIG_KVM_GUEST
 errorentry async_page_fault do_async_page_fault
 #endif
diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
index 713f1b3bad52..9e7db22ec437 100644
--- a/arch/x86/kernel/kvm.c
+++ b/arch/x86/kernel/kvm.c
@@ -259,7 +259,7 @@ do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
 
 	switch (kvm_read_and_reset_pf_reason()) {
 	default:
-		do_page_fault(regs, error_code);
+		normal_do_page_fault(regs, error_code);
 		break;
 	case KVM_PV_REASON_PAGE_NOT_PRESENT:
 		/* page is swapped out by the host. */
diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
index e7fa28bf3262..8134e5ada329 100644
--- a/arch/x86/mm/fault.c
+++ b/arch/x86/mm/fault.c
@@ -1020,10 +1020,13 @@ static inline bool smap_violation(int error_code, struct pt_regs *regs)
  * This routine handles page faults.  It determines the address,
  * and the problem, and then passes it off to one of the appropriate
  * routines.
+ *
+ * This function must have noinline because both callers
+ * {normal,trace}_do_page_fault() have notrace on. Having this an actual function
+ * guarantees there's a function trace entry.
  */
-static void __kprobes
-__do_page_fault(struct pt_regs *regs, unsigned long error_code,
-		unsigned long address)
+static void __kprobes noinline
+do_page_fault(struct pt_regs *regs, unsigned long error_code, unsigned long address)
 {
 	struct vm_area_struct *vma;
 	struct task_struct *tsk;
@@ -1245,31 +1248,38 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,
 	up_read(&mm->mmap_sem);
 }
 
-dotraplinkage void __kprobes
-do_page_fault(struct pt_regs *regs, unsigned long error_code)
+dotraplinkage void __kprobes notrace
+normal_do_page_fault(struct pt_regs *regs, unsigned long error_code)
 {
+	unsigned long address = read_cr2(); /* Get the faulting address */
 	enum ctx_state prev_state;
-	/* Get the faulting address: */
-	unsigned long address = read_cr2();
+
+	/*
+	 * We must have this function tagged with __kprobes, notrace and call
+	 * read_cr2() before calling anything else. To avoid calling any kind
+	 * of tracing machinery before we've observed the CR2 value.
+	 *
+	 * exception_{enter,exit}() contain all sorts of tracepoints.
+	 */
 
 	prev_state = exception_enter();
-	__do_page_fault(regs, error_code, address);
+	do_page_fault(regs, error_code, address);
 	exception_exit(prev_state);
 }
 
-static void trace_page_fault_entries(struct pt_regs *regs,
+#ifdef CONFIG_TRACING
+static void trace_page_fault_entries(unsigned long address, struct pt_regs *regs,
 				     unsigned long error_code)
 {
 	if (user_mode(regs))
-		trace_page_fault_user(read_cr2(), regs, error_code);
+		trace_page_fault_user(address, regs, error_code);
 	else
-		trace_page_fault_kernel(read_cr2(), regs, error_code);
+		trace_page_fault_kernel(address, regs, error_code);
 }
 
-dotraplinkage void __kprobes
+dotraplinkage void __kprobes notrace
 trace_do_page_fault(struct pt_regs *regs, unsigned long error_code)
 {
-	enum ctx_state prev_state;
 	/*
 	 * The exception_enter and tracepoint processing could
 	 * trigger another page faults (user space callchain
@@ -1277,9 +1287,11 @@ trace_do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	 * the faulting address now.
 	 */
 	unsigned long address = read_cr2();
+	enum ctx_state prev_state;
 
 	prev_state = exception_enter();
-	trace_page_fault_entries(regs, error_code);
-	__do_page_fault(regs, error_code, address);
+	trace_page_fault_entries(address, regs, error_code);
+	do_page_fault(regs, error_code, address);
 	exception_exit(prev_state);
 }
+#endif

  reply	other threads:[~2014-03-05 12:54 UTC|newest]

Thread overview: 115+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-28 15:33 [PATCH] x86 trace: Fix page fault tracing bug Jiri Olsa
2014-02-28 15:47 ` Peter Zijlstra
2014-02-28 16:05   ` [PATCHv2] " Jiri Olsa
2014-02-28 16:11     ` H. Peter Anvin
2014-02-28 16:23       ` Steven Rostedt
2014-02-28 16:15     ` Steven Rostedt
2014-03-05  0:03     ` [tip:x86/urgent] x86, trace: Fix CR2 corruption when tracing page faults tip-bot for Jiri Olsa
2014-03-05 11:14       ` Peter Zijlstra
2014-03-05 12:20         ` Steven Rostedt
2014-03-05 12:25           ` Peter Zijlstra
2014-03-05 12:33             ` Steven Rostedt
2014-03-05 12:54               ` Peter Zijlstra [this message]
2014-03-05 13:02                 ` Peter Zijlstra
2014-03-05 13:07                   ` Peter Zijlstra
2014-03-05 12:36             ` Peter Zijlstra
2014-03-05 13:00               ` Steven Rostedt
2014-03-05 13:08                 ` Peter Zijlstra
2014-03-05 21:37                   ` H. Peter Anvin
2014-03-06  8:40                     ` Peter Zijlstra
2014-03-06 11:02                       ` Steven Rostedt
2014-03-06 14:53                         ` [PATCH] x86: Further robustify CR2 handling vs tracing Peter Zijlstra
2014-03-07 23:07                           ` [tip:x86/urgent] x86, trace: " tip-bot for Peter Zijlstra
2014-02-28 15:47 ` [PATCH] x86 trace: Fix page fault tracing bug Jiri Olsa
2014-02-28 16:00   ` Steven Rostedt
2014-02-28 16:01 ` Steven Rostedt
  -- strict thread matches above, loose matches on Subject: below --
2014-02-21 20:25 perf_fuzzer causes reboot Vince Weaver
2014-02-21 22:13 ` perf_fuzzer compiled for x32 " Vince Weaver
2014-02-21 22:34   ` Vince Weaver
2014-02-22  4:50     ` Vince Weaver
2014-02-22  5:03       ` H. Peter Anvin
2014-02-22  6:26       ` H. Peter Anvin
2014-02-23  5:18         ` Vince Weaver
2014-02-23  5:24           ` H. Peter Anvin
2014-02-23  6:07           ` H. Peter Anvin
2014-02-23 14:05             ` Vince Weaver
2014-02-24  3:02               ` Vince Weaver
2014-02-24  5:22                 ` H. Peter Anvin
2014-02-24 15:35                   ` Vince Weaver
2014-02-24 16:34                     ` Vince Weaver
2014-02-24 16:47                       ` H. Peter Anvin
2014-02-24 17:10                         ` Vince Weaver
2014-02-24 17:25                           ` Peter Zijlstra
2014-02-24 17:32                             ` Vince Weaver
2014-02-24 17:40                               ` H. Peter Anvin
2014-02-24 18:00                                 ` Vince Weaver
2014-02-24 18:07                                   ` Vince Weaver
2014-02-24 18:34                                     ` H. Peter Anvin
2014-02-24 19:13                                       ` Steven Rostedt
2014-02-24 19:15                                         ` H. Peter Anvin
2014-02-24 19:30                                         ` Peter Zijlstra
2014-02-24 19:32                                           ` Steven Rostedt
2014-02-25  3:49                                           ` H. Peter Anvin
2014-02-25 14:07                                             ` Vince Weaver
2014-02-25 14:34                                               ` H. Peter Anvin
2014-02-25 14:43                                                 ` Steven Rostedt
2014-02-25 15:33                                                   ` Vince Weaver
2014-02-26 15:06                                                     ` Vince Weaver
2014-02-27 22:06                                                       ` Vince Weaver
2014-02-27 22:31                                                         ` Steven Rostedt
2014-02-27 22:52                                                           ` H. Peter Anvin
2014-02-27 23:30                                                             ` Steven Rostedt
2014-02-27 23:46                                                               ` H. Peter Anvin
2014-02-28  1:00                                                                 ` Vince Weaver
2014-02-28 20:34                                                                   ` Paul E. McKenney
2014-02-28 20:47                                                                     ` Steven Rostedt
2014-02-28 20:54                                                                       ` Peter Zijlstra
2014-02-28 21:17                                                                         ` Paul E. McKenney
2014-02-28 21:27                                                                           ` Peter Zijlstra
2014-02-28 21:51                                                                             ` Paul E. McKenney
2014-02-28 21:55                                                                               ` Peter Zijlstra
2014-02-28 22:05                                                                                 ` Steven Rostedt
2014-02-28 22:23                                                                                   ` Paul E. McKenney
2014-02-28  1:34                                                             ` Vince Weaver
2014-02-28  2:17                                                               ` H. Peter Anvin
2014-02-28  2:57                                                               ` Steven Rostedt
2014-02-28 11:11                                                                 ` Peter Zijlstra
2014-02-28 13:37                                                                   ` Steven Rostedt
2014-02-28 14:15                                                                 ` Vince Weaver
2014-02-28 14:23                                                                   ` Steven Rostedt
2014-02-28 15:07                                                                     ` Vince Weaver
2014-02-28 15:13                                                                       ` H. Peter Anvin
2014-02-28 15:40                                                                         ` Peter Zijlstra
2014-02-28 16:15                                                                           ` H. Peter Anvin
2014-02-28 16:29                                                                             ` Steven Rostedt
2014-02-28 19:33                                                                               ` [PATCH] x86: Rename copy_from_user_nmi() to copy_from_user_trace() Steven Rostedt
2014-02-28 20:46                                                                                 ` Peter Zijlstra
2014-02-28 20:51                                                                                   ` Steven Rostedt
2014-02-28 20:58                                                                                     ` Peter Zijlstra
2014-02-28 21:01                                                                                   ` Steven Rostedt
2014-02-28 21:17                                                                                     ` Peter Zijlstra
2014-02-28 20:56                                                                               ` perf_fuzzer compiled for x32 causes reboot Peter Zijlstra
2014-02-28 21:06                                                                                 ` Steven Rostedt
2014-03-01  9:16                                                                                   ` Ingo Molnar
2014-03-01  9:50                                                                                     ` Borislav Petkov
2014-03-01 16:50                                                                                       ` H. Peter Anvin
2014-03-04 23:05                                                                                         ` Borislav Petkov
2014-03-03  9:16                                                                                     ` Peter Zijlstra
2014-02-28 20:55                                                                             ` Peter Zijlstra
2014-02-28 15:20                                                                       ` Steven Rostedt
2014-02-28 15:30                                                                         ` Steven Rostedt
2014-02-28 20:38                                                                       ` H. Peter Anvin
2014-02-28 20:46                                                                         ` Steven Rostedt
2014-02-28 21:18                                                                         ` Vince Weaver
2014-02-28 21:30                                                                           ` Steven Rostedt
2014-02-28 23:34                                                                             ` Vince Weaver
2014-03-01  0:43                                                                               ` H. Peter Anvin
2014-03-01  3:36                                                                               ` Steven Rostedt
2014-03-01 16:24                                                                                 ` Andi Kleen
2014-03-02 15:34                                                                                   ` Vince Weaver
2014-03-02 16:02                                                                                 ` Vince Weaver
2014-02-28  9:39                                                           ` Peter Zijlstra
2014-02-24 17:40                               ` Peter Zijlstra
2014-02-24 17:41                               ` Vince Weaver
2014-02-24 17:42                                 ` H. Peter Anvin
2014-02-24 17:52                             ` H. Peter Anvin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140305125421.GB9987@twins.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@ghostprotocols.net \
    --cc=hpa@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=paulus@samba.org \
    --cc=rostedt@goodmis.org \
    --cc=seiji.aguchi@hds.com \
    --cc=tglx@linutronix.de \
    --cc=vincent.weaver@maine.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).