Skip to contentSkip to conversation

tracing,mm - add kernel pagefault tracepoint for x86 & x86_64

PATCH Superseded Jiri Olsa 8 participants 27 messages Nov 10, 2010 – Nov 18, 2010

Subsystems (3)
lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

cover tracing,mm - add kernel pagefault tracepoint for x86 & x86_64

This provides a tracepoint to trace kernel pagefault event for x86 and x86_64 architectures.

There's a problem with this tracepoint when having the userstacktrace option enabled, since it might generated the page fault itself. The 2/2 patch address this issue.

attached patches:
1/2 tracing,mm - add kernel pagefault tracepoint for x86 & x86_64 2/2 tracing - fix recursive user stack trace

wbr,
jirka

---
 arch/x86/mm/fault.c         |   32 +++++++++++++++++++++-----------
 include/trace/events/kmem.h |   22 ++++++++++++++++++++++
 kernel/trace/trace.c        |   19 +++++++++++++++++++
 3 files changed, 62 insertions(+), 11 deletions(-)
author
#1 lore raw

PATCH 1/2 tracing,mm - add kernel pagefault tracepoint for x86 & x86_64

This provides a tracepoint to trace kernel pagefault event.

When analyzing a vmcore resulting from a kernel failure, we _often_ hypothesize that "there should have a pagefault event just before this instruction" or similar. Sometimes it means that there should have a small delay between instructions that extends a critical session and exposed a missing lock. Since there have been no evidence of kernel pagefault, it is quite difficult to adopt the hypothesis.

If we can trace the kernel pagefault event, it will help narrow the possible cause of failure and will accelerate the investigation a lot.

---
 arch/x86/mm/fault.c         |   32 +++++++++++++++++++++-----------
 include/trace/events/kmem.h |   22 ++++++++++++++++++++++
 2 files changed, 43 insertions(+), 11 deletions(-)
arch/x86/mm/fault.c +21 -11
index 7d90ceb..f776c45 100644
@@ -12,6 +12,7 @@
 #include <linux/mmiotrace.h>		/* kmmio_handler, ...		*/
 #include <linux/perf_event.h>		/* perf_sw_event		*/
 #include <linux/hugetlb.h>		/* hstate_index_to_shift	*/
+#include <trace/events/kmem.h>
 
 #include <asm/traps.h>			/* dotraplinkage, ...		*/
 #include <asm/pgalloc.h>		/* pgd_*(), ...			*/
@@ -944,17 +945,10 @@ static int fault_in_kernel_space(unsigned long address)
 	return address >= TASK_SIZE_MAX;
 }
 
-/*
- * This routine handles page faults.  It determines the address,
- * and the problem, and then passes it off to one of the appropriate
- * routines.
- */
-dotraplinkage void __kprobes
-do_page_fault(struct pt_regs *regs, unsigned long error_code)
+static inline void __do_page_fault(struct pt_regs *regs, unsigned long address, unsigned long error_code)
 {
 	struct vm_area_struct *vma;
 	struct task_struct *tsk;
-	unsigned long address;
 	struct mm_struct *mm;
 	int fault;
 	int write = error_code & PF_WRITE;
@@ -964,9 +958,6 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	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.
@@ -1158,3 +1149,22 @@ good_area:
 
 	up_read(&mm->mmap_sem);
 }
+
+/*
+ * This routine handles page faults.  It determines the address,
+ * and the problem, and then passes it off to one of the appropriate
+ * routines.
+ */
+dotraplinkage void __kprobes
+do_page_fault(struct pt_regs *regs, unsigned long error_code)
+{
+	unsigned long address;
+
+	/* Get the faulting address: */
+	address = read_cr2();
+
+	__do_page_fault(regs, address, error_code);
+
+	if (!user_mode(regs))
+		trace_mm_kernel_pagefault(current, address, regs);
+}
include/trace/events/kmem.h +22 -0
index a9c87ad..f14535b 100644
@@ -302,6 +302,28 @@ TRACE_EVENT(mm_page_alloc_extfrag,
 		__entry->alloc_migratetype == __entry->fallback_migratetype)
 );
 
+TRACE_EVENT(mm_kernel_pagefault,
+
+	TP_PROTO(struct task_struct *task, unsigned long address, struct pt_regs *regs),
+
+	TP_ARGS(task, address, regs),
+
+	TP_STRUCT__entry(
+		__field(struct task_struct *, task)
+		__field(unsigned long, address)
+		__field(struct pt_regs *, regs)
+	),
+
+	TP_fast_assign(
+		__entry->task = task;
+		__entry->address = address;
+		__entry->regs = regs;
+	),
+
+	TP_printk("task=%lx, address=%lx, regs=%lx",
+		(unsigned long)__entry->task, (unsigned long)__entry->address,
+			__entry->regs)
+	);
 #endif /* _TRACE_KMEM_H */
 
 /* This part must be outside protection */
Signature · 1 line
1.7.1
#2 lore raw

include/trace/events/kmem.h TRACE_EVENT(mm_page_alloc_extfrag, · line 325

Quoted 3 lines · Jiri Olsa
include/trace/events/kmem.h +3 -0
+	TP_printk("task=%lx, address=%lx, regs=%lx",
+		(unsigned long)__entry->task, (unsigned long)__entry->address,
+			__entry->regs)

How exactly do you use the information in this trace point? Especially the undecoded pt_regs doesn't seem very useful to me at all.

author
#3 lore raw
Quoted 7 lines · Christoph Hellwig

include/trace/events/kmem.h TRACE_EVENT(mm_page_alloc_extfrag, · line 325

Quoted 3 lines · Jiri Olsa
include/trace/events/kmem.h +3 -0
+	TP_printk("task=%lx, address=%lx, regs=%lx",
+		(unsigned long)__entry->task, (unsigned long)__entry->address,
+			__entry->regs)

How exactly do you use the information in this trace point? Especially the undecoded pt_regs doesn't seem very useful to me at all.

agreed, the registers pointer are not very useful in the trace file output, and could be taken away.. just wanted to be complete I guess

but I believe they are useful when you register the mm_kernel_pagefault tracepoint and process the information by yourself

wbr,
jirka

#4 lore raw
Quoted 9 lines · Jiri Olsa
Quoted 7 lines · Christoph Hellwig

include/trace/events/kmem.h TRACE_EVENT(mm_page_alloc_extfrag, · line 325

Quoted 3 lines · Jiri Olsa
include/trace/events/kmem.h +3 -0
+	TP_printk("task=%lx, address=%lx, regs=%lx",
+		(unsigned long)__entry->task, (unsigned long)__entry->address,
+			__entry->regs)

How exactly do you use the information in this trace point? Especially the undecoded pt_regs doesn't seem very useful to me at all.

Quoted 5 lines

agreed, the registers pointer are not very useful in the trace file output, and could be taken away.. just wanted to be complete I guess

but I believe they are useful when you register the mm_kernel_pagefault tracepoint and process the information by yourself

That would be expressed in a better and more generic fashion via adding PERF_SAMPLE_REGS to perf_event_sample_format, and add a ptregs dump in kernel/perf_event.c, perf_output_sample(). That way any tracepoint can request a (user-space)ptregs state snapshot, not just the pagefault ones.

Thanks,

Ingo

#5 lore raw
Quoted 18 lines · Ingo Molnar
Quoted 9 lines · Jiri Olsa
Quoted 7 lines · Christoph Hellwig

include/trace/events/kmem.h TRACE_EVENT(mm_page_alloc_extfrag, · line 325

Quoted 3 lines · Jiri Olsa
include/trace/events/kmem.h +3 -0
+	TP_printk("task=%lx, address=%lx, regs=%lx",
+		(unsigned long)__entry->task, (unsigned long)__entry->address,
+			__entry->regs)

How exactly do you use the information in this trace point? Especially the undecoded pt_regs doesn't seem very useful to me at all.

Quoted 5 lines

agreed, the registers pointer are not very useful in the trace file output, and could be taken away.. just wanted to be complete I guess

but I believe they are useful when you register the mm_kernel_pagefault tracepoint and process the information by yourself

Quoted 8 lines

That would be expressed in a better and more generic fashion via adding PERF_SAMPLE_REGS to perf_event_sample_format, and add a ptregs dump in kernel/perf_event.c, perf_output_sample(). That way any tracepoint can request a (user-space)ptregs state snapshot, not just the pagefault ones.

Thanks,

Ingo

We are going to have that with the dwarf based callchain patchset. I'm cooking this.

Thanks.

author
#6 lore raw
Quoted 26 lines · Frederic Weisbecker
Quoted 25 lines · Ingo Molnar
Quoted 9 lines · Jiri Olsa
Quoted 7 lines · Christoph Hellwig

include/trace/events/kmem.h TRACE_EVENT(mm_page_alloc_extfrag, · line 325

Quoted 3 lines · Jiri Olsa
include/trace/events/kmem.h +3 -0
+	TP_printk("task=%lx, address=%lx, regs=%lx",
+		(unsigned long)__entry->task, (unsigned long)__entry->address,
+			__entry->regs)

How exactly do you use the information in this trace point? Especially the undecoded pt_regs doesn't seem very useful to me at all.

Quoted 5 lines

agreed, the registers pointer are not very useful in the trace file output, and could be taken away.. just wanted to be complete I guess

but I believe they are useful when you register the mm_kernel_pagefault tracepoint and process the information by yourself

That would be expressed in a better and more generic fashion via adding PERF_SAMPLE_REGS to perf_event_sample_format, and add a ptregs dump in kernel/perf_event.c, perf_output_sample(). That way any tracepoint can request a (user-space)ptregs state snapshot, not just the pagefault ones.

Thanks,

Quoted 8 lines
Quoted 1 line

Ingo

We are going to have that with the dwarf based callchain patchset. I'm cooking this.

Thanks.

I guess I can take the regs out then.. would that patch be acceptable afterwards..?

thanks,
jirka

#7 lore raw
Quoted 2 lines · Jiri Olsa

I guess I can take the regs out then.. would that patch be acceptable afterwards..?

I really can't make much sense out of this whole trace point. What use it the task_struct address for example?

What would be much more interesting is telling us if we had a major/minor fault, if ->page_mkwrite was called, etc.

#8 lore raw
Quoted 9 lines · Christoph Hellwig
Quoted 2 lines · Jiri Olsa

I guess I can take the regs out then.. would that patch be acceptable afterwards..?

I really can't make much sense out of this whole trace point. What use it the task_struct address for example?

What would be much more interesting is telling us if we had a major/minor fault, if ->page_mkwrite was called, etc.

Also on x86 I would log the error code.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only.
#9 lore raw
In reply to Jiri Olsa
Quoted 30 lines · Jiri Olsa
Quoted 29 lines · Frederic Weisbecker
Quoted 26 lines · Ingo Molnar
Quoted 9 lines · Jiri Olsa
Quoted 7 lines · Christoph Hellwig

include/trace/events/kmem.h TRACE_EVENT(mm_page_alloc_extfrag, · line 325

Quoted 3 lines · Jiri Olsa
include/trace/events/kmem.h +3 -0
+	TP_printk("task=%lx, address=%lx, regs=%lx",
+		(unsigned long)__entry->task, (unsigned long)__entry->address,
+			__entry->regs)

How exactly do you use the information in this trace point? Especially the undecoded pt_regs doesn't seem very useful to me at all.

Quoted 5 lines

agreed, the registers pointer are not very useful in the trace file output, and could be taken away.. just wanted to be complete I guess

but I believe they are useful when you register the mm_kernel_pagefault tracepoint and process the information by yourself

That would be expressed in a better and more generic fashion via adding PERF_SAMPLE_REGS to perf_event_sample_format, and add a ptregs dump in kernel/perf_event.c, perf_output_sample(). That way any tracepoint can request a (user-space)ptregs state snapshot, not just the pagefault ones.

Thanks,

Ingo

Quoted 8 lines
Quoted 5 lines

We are going to have that with the dwarf based callchain patchset. I'm cooking this.

Thanks.

I guess I can take the regs out then.. would that patch be acceptable afterwards..?

Please take out the regs yeah.

Thanks.

author
#10 lore raw

PATCH v2 1/2 tracing - fix recursive user stack trace

The user stack trace can fault when examining the trace. Which would call the do_page_fault handler, which would trace again, which would do the user stack trace, which would fault and call do_page_fault again ...

Thus this is causing a recursive bug. We need to have a recursion detector here.

---
 kernel/trace/trace.c |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)
kernel/trace/trace.c +17 -0
index 82d9b81..1905a72 100644
@@ -1284,6 +1284,8 @@ void trace_dump_stack(void)
 	__ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
 }
 
+static DEFINE_PER_CPU(int, user_stack_count);
+
 void
 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 {
@@ -1302,6 +1304,16 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 	if (unlikely(in_nmi()))
 		return;
 
+	/*
+	 * Prevent recursion, since the user stack tracing may
+	 * trigger other kernel events.
+	 */
+	preempt_disable();
+	if (__get_cpu_var(user_stack_count))
+		goto out;
+
+	__get_cpu_var(user_stack_count)++;
+
 	event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
 					  sizeof(*entry), flags, pc);
 	if (!event)
@@ -1319,6 +1331,11 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 	save_stack_trace_user(&trace);
 	if (!filter_check_discard(call, entry, buffer, event))
 		ring_buffer_unlock_commit(buffer, event);
+
+	__get_cpu_var(user_stack_count)--;
+
+ out:
+	preempt_enable();
 }
 
 #ifdef UNUSED
Signature · 1 line
1.7.1
#11 lore raw

Re: [PATCHv2 1/2] tracing - fix recursive user stack trace

Quoted 7 lines · Jiri Olsa

The user stack trace can fault when examining the trace. Which would call the do_page_fault handler, which would trace again, which would do the user stack trace, which would fault and call do_page_fault again ...

Thus this is causing a recursive bug. We need to have a recursion detector here.

I suspect this problem is in more trace points: any NMI or MCE trace point or any other trace point that can be triggered from a page. so it seems to be a general bug fix independent of the other patch.

-Andi

author
#12 lore raw

PATCH v2 2/2 tracing,mm - add kernel pagefault tracepoint for x86 & x86_64

In reply to Frederic Weisbecker

This provides a tracepoint to trace kernel pagefault event.

When analyzing a vmcore resulting from a kernel failure, we often hypothesize that "there should have a pagefault event just before this instruction" or similar. Sometimes it means that there should have a small delay between instructions that extends a critical session and exposed a missing lock. Since there have been no evidence of kernel pagefault, it is quite difficult to adopt the hypothesis.

If we can trace the kernel pagefault event, it will help narrow the possible cause of failure and will accelerate the investigation a lot.

---
 arch/x86/mm/fault.c         |   33 ++++++++++++++++++++++-----------
 include/trace/events/kmem.h |   23 +++++++++++++++++++++++
 2 files changed, 45 insertions(+), 11 deletions(-)
arch/x86/mm/fault.c +22 -11
index 7d90ceb..171dcc9 100644
@@ -12,6 +12,7 @@
 #include <linux/mmiotrace.h>		/* kmmio_handler, ...		*/
 #include <linux/perf_event.h>		/* perf_sw_event		*/
 #include <linux/hugetlb.h>		/* hstate_index_to_shift	*/
+#include <trace/events/kmem.h>
 
 #include <asm/traps.h>			/* dotraplinkage, ...		*/
 #include <asm/pgalloc.h>		/* pgd_*(), ...			*/
@@ -944,17 +945,11 @@ static int fault_in_kernel_space(unsigned long address)
 	return address >= TASK_SIZE_MAX;
 }
 
-/*
- * This routine handles page faults.  It determines the address,
- * and the problem, and then passes it off to one of the appropriate
- * routines.
- */
-dotraplinkage void __kprobes
-do_page_fault(struct pt_regs *regs, unsigned long error_code)
+static inline void __do_page_fault(struct pt_regs *regs, unsigned long address,
+				   unsigned long error_code)
 {
 	struct vm_area_struct *vma;
 	struct task_struct *tsk;
-	unsigned long address;
 	struct mm_struct *mm;
 	int fault;
 	int write = error_code & PF_WRITE;
@@ -964,9 +959,6 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	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.
@@ -1158,3 +1150,22 @@ good_area:
 
 	up_read(&mm->mmap_sem);
 }
+
+/*
+ * This routine handles page faults.  It determines the address,
+ * and the problem, and then passes it off to one of the appropriate
+ * routines.
+ */
+dotraplinkage void __kprobes
+do_page_fault(struct pt_regs *regs, unsigned long error_code)
+{
+	unsigned long address;
+
+	/* Get the faulting address: */
+	address = read_cr2();
+
+	__do_page_fault(regs, address, error_code);
+
+	if (!user_mode(regs))
+		trace_mm_kernel_pagefault(current, address, error_code);
+}
include/trace/events/kmem.h +23 -0
index a9c87ad..b17cdf3 100644
@@ -302,6 +302,29 @@ TRACE_EVENT(mm_page_alloc_extfrag,
 		__entry->alloc_migratetype == __entry->fallback_migratetype)
 );
 
+TRACE_EVENT(mm_kernel_pagefault,
+
+	TP_PROTO(struct task_struct *task, unsigned long address,
+		 int error_code),
+
+	TP_ARGS(task, address, error_code),
+
+	TP_STRUCT__entry(
+		__field(struct task_struct *, task)
+		__field(unsigned long, address)
+		__field(unsigned long, error_code)
+	),
+
+	TP_fast_assign(
+		__entry->task = task;
+		__entry->address = address;
+		__entry->error_code = error_code;
+	),
+
+	TP_printk("task=%lx, address=%lx, error code=%lx",
+		(unsigned long)__entry->task, (unsigned long)__entry->address,
+			__entry->error_code)
+	);
 #endif /* _TRACE_KMEM_H */
 
 /* This part must be outside protection */
Signature · 1 line
1.7.1
#13 lore raw

Can you explain again who this is supposed to be used? It's missing a lot of essential information about the page fault, but on the other hand traces totally useless information like the task struct pointer.

What's your motivation for posting this given that you obviously haven't actually tried to use it in practice?

Btw, this doesn't mean I'm against a page faul trace point - quite contrary. It just seems you're not actually pushing something that's been tested in real life or is generally useful.

author
#14 lore raw
Quoted 6 lines · Christoph Hellwig

Can you explain again who this is supposed to be used? It's missing a lot of essential information about the page fault, but on the other hand traces totally useless information like the task struct pointer.

What's your motivation for posting this given that you obviously haven't actually tried to use it in practice?

yep, kind of doing this for Larry... hoping he'd fill in to the discussion ;)

Quoted 4 lines

Btw, this doesn't mean I'm against a page faul trace point - quite contrary. It just seems you're not actually pushing something that's been tested in real life or is generally useful.

What would be your idea of the page fault tracepoint?

thanks,
jirka

#15 lore raw
In reply to Jiri Olsa
Quoted 77 lines · Jiri Olsa

This provides a tracepoint to trace kernel pagefault event.

When analyzing a vmcore resulting from a kernel failure, we often hypothesize that "there should have a pagefault event just before this instruction" or similar. Sometimes it means that there should have a small delay between instructions that extends a critical session and exposed a missing lock. Since there have been no evidence of kernel pagefault, it is quite difficult to adopt the hypothesis.

If we can trace the kernel pagefault event, it will help narrow the possible cause of failure and will accelerate the investigation a lot.

---
 arch/x86/mm/fault.c         |   33 ++++++++++++++++++++++-----------
 include/trace/events/kmem.h |   23 +++++++++++++++++++++++
 2 files changed, 45 insertions(+), 11 deletions(-)
arch/x86/mm/fault.c +12 -11
index 7d90ceb..171dcc9 100644
@@ -12,6 +12,7 @@
 #include <linux/mmiotrace.h>		/* kmmio_handler, ...		*/
 #include <linux/perf_event.h>		/* perf_sw_event		*/
 #include <linux/hugetlb.h>		/* hstate_index_to_shift	*/
+#include <trace/events/kmem.h>
 
 #include <asm/traps.h>			/* dotraplinkage, ...		*/
 #include <asm/pgalloc.h>		/* pgd_*(), ...			*/
@@ -944,17 +945,11 @@ static int fault_in_kernel_space(unsigned long address)
 	return address >= TASK_SIZE_MAX;
 }
 
-/*
- * This routine handles page faults.  It determines the address,
- * and the problem, and then passes it off to one of the appropriate
- * routines.
- */
-dotraplinkage void __kprobes
-do_page_fault(struct pt_regs *regs, unsigned long error_code)
+static inline void __do_page_fault(struct pt_regs *regs, unsigned long address,
+				   unsigned long error_code)
 {
 	struct vm_area_struct *vma;
 	struct task_struct *tsk;
-	unsigned long address;
 	struct mm_struct *mm;
 	int fault;
 	int write = error_code & PF_WRITE;
@@ -964,9 +959,6 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
 	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.
@@ -1158,3 +1150,22 @@ good_area:
 
 	up_read(&mm->mmap_sem);
 }
+
+/*
+ * This routine handles page faults.  It determines the address,
+ * and the problem, and then passes it off to one of the appropriate
+ * routines.
+ */
+dotraplinkage void __kprobes
+do_page_fault(struct pt_regs *regs, unsigned long error_code)
+{

arch/x86/mm/fault.c good_area: · line 1171

Quoted 10 lines
arch/x86/mm/fault.c +10 -0
+	unsigned long address;
+
+	/* Get the faulting address: */
+	address = read_cr2();
+
+	__do_page_fault(regs, address, error_code);
+
+	if (!user_mode(regs))
+		trace_mm_kernel_pagefault(current, address, error_code);
+}

I (and others) have been testing your patch to measure the latencies of page faults.

So I have several comments about it.

First, we don't want a pointer to current, we can already retrieve the pid from a trace.

Second, it would be definetly interesting to also have the instruction address that faulted (regs->ip).

Three, I wonder why this tracepoint only traces kernel faults. And in fact kernel faults is a confusing name. Users will be confused whether this is about tracing only faults happening in kernel or also faults happening in kernel data.
Actually I don't see any reason right now to trace only kernel faults. Do you? If that's needed, one can still check on post-processing that the address was in the kernel.

And four, measuring page fault handling duration can be desired, it would be nice to have a page_fault_start, page_fault_end.

So in the end we can get:

dotraplinkage void __kprobes
do_page_fault(struct pt_regs *regs, unsigned long error_code)

{
	unsigned long address;

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

	trace_mm_pagefault_start(address, error_code);
	__do_page_fault(regs, address, error_code);
	trace_mm_pagefault_end(address);
}

Would you be ok with that?

Last thing I worry about is that error_code that is very arch dependent. If someone writes a script that depends on the x86 code, it won't work elsewhere while it's fairly possible to have a generic tracepoint there.

So perhaps we rather need a generic enum field instead of the error_code, to express the most interesting specific fault attributes. Than can probably be added later though, once someone really needs it.

Hm?

#16 lore raw
Quoted 3 lines

Actually I don't see any reason right now to trace only kernel faults. Do you? If that's needed, one can still check on post-processing that the address was in the kernel.

I think the idea is to get more context on oopses. If the event only covers that the overhead in the common case (minus *_user) is much less, versus the more generalized points you use.

For tracing the whole page fault me think it's better to have a generalized exception tracer with a filter on page fault.

-Andi

#17 lore raw
Quoted 7 lines · Andi Kleen
Quoted 3 lines

Actually I don't see any reason right now to trace only kernel faults. Do you? If that's needed, one can still check on post-processing that the address was in the kernel.

I think the idea is to get more context on oopses. If the event only covers that the overhead in the common case (minus *_user) is much less, versus the more generalized points you use.

I see. OTOH, page faults should be pretty low freq events most of time, probably not something that would add much tracing overhead.

The pity is that we have something like exclude_user/exclude_kernel properties for events when used by perf, but we consider trace event as always firing in the kernel, this one is an exception but it would too tricky and too much an overkill to handle these attributes just for this trace events.

Quoted 2 lines

For tracing the whole page fault me think it's better to have a generalized exception tracer with a filter on page fault.

You're right. A tracepoint in handle_mm_fault() would be perhaps better. It should catch most tracepoints the users are interested in. On the other hand we may miss part of the page fault latency, like the mmap_sem contention. This can be measured using lock events though.

But I'm probably missing other important things.

maintainer
#18 lore raw
Quoted 6 lines · Frederic Weisbecker
Quoted 2 lines · Andi Kleen

For tracing the whole page fault me think it's better to have a generalized exception tracer with a filter on page fault.

Quoted 7 lines

You're right. A tracepoint in handle_mm_fault() would be perhaps better. It should catch most tracepoints the users are interested in. On the other hand we may miss part of the page fault latency, like the mmap_sem contention. This can be measured using lock events though.

But I'm probably missing other important things.

I would have the general exception handler as a tracepoint that would have a "stable tracepoint" hook to it. That is, when enabling it as a infield debugging tracepoint, we would get the "raw tracepoint", but the stable tracepoint would massage it into different general types of exceptions, and the code there would do the filtering.

-- Steve

maintainer
#19 lore raw
In reply to Frederic Weisbecker
Quoted 20 lines · Frederic Weisbecker

dotraplinkage void __kprobes
do_page_fault(struct pt_regs *regs, unsigned long error_code)

{
	unsigned long address;

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

	trace_mm_pagefault_start(address, error_code);
	__do_page_fault(regs, address, error_code);
	trace_mm_pagefault_end(address);
}

Would you be ok with that?

Last thing I worry about is that error_code that is very arch dependent. If someone writes a script that depends on the x86 code, it won't work elsewhere while it's fairly possible to have a generic tracepoint there.

Quoted 5 lines

So perhaps we rather need a generic enum field instead of the error_code, to express the most interesting specific fault attributes. Than can probably be added later though, once someone really needs it.

Hm?

Perhaps we should have:

	trace_arch_mm_pagefault_start(address, error_code);
	__do_page_fault(regs, address, error_code);
	trace_mm_pagefault_end(address);

Then we have a arch/x86/kernel/trace.c that can map trace_arch_... events to generic events. This file will hold the trace_mm_pagefault_start(), which is called by trace_arch_mm_pagefault_start(). Have a hook that when the trace_mm_pagefault_start() is enabled, we also enable trace_arch_mm_pagefault_start() that calls this tracepoint. The trace_mm_pagefault_start() will then translate the trace_arch_mm_pagfault_start() into the generic trace_mm_pagefault_start event with the generic error_code that all archs will use.

Reason being, we don't need to do any extra processing in the fast path when tracing is not enabled.

Also, I'm going to start working on the stable ABI today.

-- Steve

author
#20 lore raw
Quoted 46 lines · Steven Rostedt
Quoted 20 lines · Frederic Weisbecker

dotraplinkage void __kprobes
do_page_fault(struct pt_regs *regs, unsigned long error_code)

{
	unsigned long address;

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

	trace_mm_pagefault_start(address, error_code);
	__do_page_fault(regs, address, error_code);
	trace_mm_pagefault_end(address);
}

Would you be ok with that?

Last thing I worry about is that error_code that is very arch dependent. If someone writes a script that depends on the x86 code, it won't work elsewhere while it's fairly possible to have a generic tracepoint there.

Quoted 5 lines

So perhaps we rather need a generic enum field instead of the error_code, to express the most interesting specific fault attributes. Than can probably be added later though, once someone really needs it.

Hm?

Perhaps we should have:

	trace_arch_mm_pagefault_start(address, error_code);
	__do_page_fault(regs, address, error_code);
	trace_mm_pagefault_end(address);

Then we have a arch/x86/kernel/trace.c that can map trace_arch_... events to generic events. This file will hold the trace_mm_pagefault_start(), which is called by trace_arch_mm_pagefault_start(). Have a hook that when the trace_mm_pagefault_start() is enabled, we also enable trace_arch_mm_pagefault_start() that calls this tracepoint. The trace_mm_pagefault_start() will then translate the trace_arch_mm_pagfault_start() into the generic trace_mm_pagefault_start event with the generic error_code that all archs will use.

Quoted 4 lines

Reason being, we don't need to do any extra processing in the fast path when tracing is not enabled.

Also, I'm going to start working on the stable ABI today.

do you mean stable api for the hook you described above? or should I come up with smth..

thanks,
jirka

maintainer
#21 lore raw
Quoted 3 lines · Jiri Olsa
Quoted 1 line

Also, I'm going to start working on the stable ABI today.

do you mean stable api for the hook you described above? or should I come up with smth..

The stable api will hook into the raw tracepoints (I'm calling the tracepoints we have now "raw"). The tracepoints will not need anything new so please continue with what you are doing.

-- Steve

author
#22 lore raw

v2 cover tracing,mm - add kernel pagefault tracepoint for x86 & x86_64

In reply to Frederic Weisbecker

This provides a tracepoint to trace kernel pagefault event for x86 and x86_64 architectures.

There's a problem with this tracepoint when having the userstacktrace option enabled, since it might generated the page fault itself. The 1/2 patch address this issue.

v2 changes:
- replaced regs for error_code in page fault trace
- swapped the patch order not to introduce the regression
- minor code style change

attached patches:
1/2 tracing - fix recursive user stack trace
2/2 tracing,mm - add kernel pagefault tracepoint for x86 & x86_64

wbr,
jirka

---
 arch/x86/mm/fault.c         |   32 +++++++++++++++++++++-----------
 include/trace/events/kmem.h |   22 ++++++++++++++++++++++
 kernel/trace/trace.c        |   17 +++++++++++++++++
 3 files changed, 60 insertions(+), 11 deletions(-)
author
#23 lore raw

PATCH 2/2 tracing - fix recursive user stack trace

In reply to Jiri Olsa

The user stack trace can fault when examining the trace. Which would call the do_page_fault handler, which would trace again, which would do the user stack trace, which would fault and call do_page_fault again ...

Thus this is causing a recursive bug. We need to have a recursion detector here.

---
 kernel/trace/trace.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
kernel/trace/trace.c +19 -0
index 82d9b81..0215e87 100644
@@ -1284,6 +1284,8 @@ void trace_dump_stack(void)
 	__ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
 }
 
+static DEFINE_PER_CPU(int, user_stack_count);
+
 void
 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 {
@@ -1302,6 +1304,18 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 	if (unlikely(in_nmi()))
 		return;
 
+	/*
+	 * prevent recursion, since the user stack tracing may
+	 * trigger other kernel events.
+	 */
+	preempt_disable();
+	if (__get_cpu_var(user_stack_count))
+		goto out;
+
+	__get_cpu_var(user_stack_count)++;
+
+
+
 	event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
 					  sizeof(*entry), flags, pc);
 	if (!event)
@@ -1319,6 +1333,11 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 	save_stack_trace_user(&trace);
 	if (!filter_check_discard(call, entry, buffer, event))
 		ring_buffer_unlock_commit(buffer, event);
+
+	__get_cpu_var(user_stack_count)--;
+
+ out:
+	preempt_enable();
 }
 
 #ifdef UNUSED
Signature · 1 line
1.7.1
#24 lore raw

Re: [PATCH 2/2] tracing - fix recursive user stack trace

Quoted 8 lines · Jiri Olsa

The user stack trace can fault when examining the trace. Which would call the do_page_fault handler, which would trace again, which would do the user stack trace, which would fault and call do_page_fault again ...

Thus this is causing a recursive bug. We need to have a recursion detector here.

I guess this is from what I reported to Redhat, triggered by the ftrace stress test. ;)

This patch should be the first patch, otherwise you introduce a regression. Though it merely a problem in this case, better avoid it.

A nitpick below:

Quoted 26 lines
---
 kernel/trace/trace.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
kernel/trace/trace.c +4 -0
index 82d9b81..0215e87 100644
@@ -1284,6 +1284,8 @@ void trace_dump_stack(void)
 	__ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
 }
 
+static DEFINE_PER_CPU(int, user_stack_count);
+
 void
 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 {
@@ -1302,6 +1304,18 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 	if (unlikely(in_nmi()))
 		return;
 
+	/*
+	 * prevent recursion, since the user stack tracing may

kernel/trace/trace.c ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc) · line 1318

Quoted 10 lines
kernel/trace/trace.c +10 -0
+	 * trigger other kernel events.
+	 */
+	preempt_disable();
+	if (__get_cpu_var(user_stack_count))
+		goto out;
+
+	__get_cpu_var(user_stack_count)++;
+
+
+

redundant blank lines.

Quoted 15 lines
kernel/trace/trace.c +5 -0
 	event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
 					  sizeof(*entry), flags, pc);
 	if (!event)
@@ -1319,6 +1333,11 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 	save_stack_trace_user(&trace);
 	if (!filter_check_discard(call, entry, buffer, event))
 		ring_buffer_unlock_commit(buffer, event);
+
+	__get_cpu_var(user_stack_count)--;
+
+ out:
+	preempt_enable();
 }
 
 #ifdef UNUSED
maintainer
#25 lore raw

Re: [PATCH 2/2] tracing - fix recursive user stack trace

Quoted 10 lines · Li Zefan
Quoted 8 lines · Jiri Olsa

The user stack trace can fault when examining the trace. Which would call the do_page_fault handler, which would trace again, which would do the user stack trace, which would fault and call do_page_fault again ...

Thus this is causing a recursive bug. We need to have a recursion detector here.

Quoted 6 lines

I guess this is from what I reported to Redhat, triggered by the ftrace stress test. ;)

This patch should be the first patch, otherwise you introduce a regression. Though it merely a problem in this case, better avoid it.

Yeah, this should go into urgent, and the other patch can be queued for 38.

Quoted 27 lines

A nitpick below:

Quoted 24 lines
---
 kernel/trace/trace.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
kernel/trace/trace.c +2 -0
index 82d9b81..0215e87 100644
@@ -1284,6 +1284,8 @@ void trace_dump_stack(void)
 	__ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
 }
 
+static DEFINE_PER_CPU(int, user_stack_count);
+
 void
 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 {
@@ -1302,6 +1304,18 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 	if (unlikely(in_nmi()))
 		return;
 
Quoted 14 lines

kernel/trace/trace.c ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc) · line 1318

Quoted 12 lines
kernel/trace/trace.c +12 -0
+	/*
+	 * prevent recursion, since the user stack tracing may
+	 * trigger other kernel events.
+	 */
+	preempt_disable();
+	if (__get_cpu_var(user_stack_count))
+		goto out;
+
+	__get_cpu_var(user_stack_count)++;
+
+
+

redundant blank lines.

I can pull this patch with the fix.

Thanks!

-- Steve

Quoted 16 lines
Quoted 16 lines
kernel/trace/trace.c +5 -0
 	event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
 					  sizeof(*entry), flags, pc);
 	if (!event)
@@ -1319,6 +1333,11 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 	save_stack_trace_user(&trace);
 	if (!filter_check_discard(call, entry, buffer, event))
 		ring_buffer_unlock_commit(buffer, event);
+
+	__get_cpu_var(user_stack_count)--;
+
+ out:
+	preempt_enable();
 }
 
 #ifdef UNUSED
#26 lore raw

[tip:perf/core] tracing: Fix recursive user stack trace

In reply to Jiri Olsa
Commit-ID:  91e86e560d0b3ce4c5fc64fd2bbb99f856a30a4e
Gitweb:     http://git.kernel.org/tip/91e86e560d0b3ce4c5fc64fd2bbb99f856a30a4e
Author:     Steven Rostedt <[email protected]>
AuthorDate: Wed, 10 Nov 2010 12:56:12 +0100
Committer:  Steven Rostedt <[email protected]>
CommitDate: Fri, 12 Nov 2010 21:20:08 -0500

tracing: Fix recursive user stack trace

The user stack trace can fault when examining the trace. Which would call the do_page_fault handler, which would trace again, which would do the user stack trace, which would fault and call do_page_fault again ...

Thus this is causing a recursive bug. We need to have a recursion detector here.

[ Resubmitted by Jiri Olsa ]

[ Eric Dumazet recommended using __this_cpu_* instead of __get_cpu_* ]

Cc Eric Dumazet <[email protected]>

LKML-Reference: <[email protected]>

---
 kernel/trace/trace.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
kernel/trace/trace.c +19 -0
index 82d9b81..ee6a733 100644
@@ -1284,6 +1284,8 @@ void trace_dump_stack(void)
 	__ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
 }
 
+static DEFINE_PER_CPU(int, user_stack_count);
+
 void
 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 {
@@ -1302,6 +1304,18 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 	if (unlikely(in_nmi()))
 		return;
 
+	/*
+	 * prevent recursion, since the user stack tracing may
+	 * trigger other kernel events.
+	 */
+	preempt_disable();
+	if (__this_cpu_read(user_stack_count))
+		goto out;
+
+	__this_cpu_inc(user_stack_count);
+
+
+
 	event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
 					  sizeof(*entry), flags, pc);
 	if (!event)
@@ -1319,6 +1333,11 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
 	save_stack_trace_user(&trace);
 	if (!filter_check_discard(call, entry, buffer, event))
 		ring_buffer_unlock_commit(buffer, event);
+
+	__this_cpu_dec(user_stack_count);
+
+ out:
+	preempt_enable();
 }
 
 #ifdef UNUSED