Skip to content

Commit

Permalink
x86, trace: Fix CR2 corruption when tracing page faults
Browse files Browse the repository at this point in the history
The trace_do_page_fault function trigger tracepoint
and then handles the actual page fault.

This could lead to error if the tracepoint caused page
fault. The original cr2 value gets lost and the original
page fault handler kills current process with SIGSEGV.

This happens if you record page faults with callchain
data, the user part of it will cause tracepoint handler
to page fault:

  # perf record -g -e exceptions:page_fault_user ls

Fixing this by saving the original cr2 value
and using it after tracepoint handler is done.

v2: Moving the cr2 read before exception_enter, because
    it could trigger tracepoint as well.

Reported-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Reported-by: Vince Weaver <vincent.weaver@maine.edu>
Tested-by: Vince Weaver <vincent.weaver@maine.edu>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Seiji Aguchi <seiji.aguchi@hds.com>
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Link: http://lkml.kernel.org/r/alpine.DEB.2.10.1402211701380.6395@vincent-weaver-1.um.maine.edu
Link: http://lkml.kernel.org/r/20140228160526.GD1133@krava.brq.redhat.com
  • Loading branch information
Jiri Olsa authored and H. Peter Anvin committed Mar 5, 2014
1 parent 3c0b566 commit 0ac09f9
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions arch/x86/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,21 +1022,18 @@ static inline bool smap_violation(int error_code, struct pt_regs *regs)
* routines.
*/
static void __kprobes
__do_page_fault(struct pt_regs *regs, unsigned long error_code)
__do_page_fault(struct pt_regs *regs, unsigned long error_code,
unsigned long address)
{
struct vm_area_struct *vma;
struct task_struct *tsk;
unsigned long address;
struct mm_struct *mm;
int fault;
unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;

tsk = current;
mm = tsk->mm;

/* Get the faulting address: */
address = read_cr2();

/*
* Detect and handle instructions that would cause a page fault for
* both a tracked kernel page and a userspace page.
Expand Down Expand Up @@ -1252,9 +1249,11 @@ dotraplinkage void __kprobes
do_page_fault(struct pt_regs *regs, unsigned long error_code)
{
enum ctx_state prev_state;
/* Get the faulting address: */
unsigned long address = read_cr2();

prev_state = exception_enter();
__do_page_fault(regs, error_code);
__do_page_fault(regs, error_code, address);
exception_exit(prev_state);
}

Expand All @@ -1271,9 +1270,16 @@ dotraplinkage void __kprobes
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
* reading) and destroy the original cr2 value, so read
* the faulting address now.
*/
unsigned long address = read_cr2();

prev_state = exception_enter();
trace_page_fault_entries(regs, error_code);
__do_page_fault(regs, error_code);
__do_page_fault(regs, error_code, address);
exception_exit(prev_state);
}

0 comments on commit 0ac09f9

Please sign in to comment.