mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Soeren Sandmann <sandmann@daimi.au.dk>
To: mingo@elte.hu
Cc: Pavel Roskin <proski@gnu.org>, Pekka Paalanen <pq@iki.fi>,
	Steven Rostedt <rostedt@goodmis.org>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] ftrace/sysprof: Don't trace the user stack if we are a kernel thread.
Date: 12 May 2008 05:28:50 +0200	[thread overview]
Message-ID: <ye8tzh4dwul.fsf@camel09.daimi.au.dk> (raw)

Check that current->mm is non-NULL before attempting to trace the user
stack.

Also take depth of the kernel stack into account when comparing
against sample_max_depth.
---
 kernel/trace/trace_sysprof.c |   56 ++++++++++++++++++++++++------------------
 1 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/kernel/trace/trace_sysprof.c b/kernel/trace/trace_sysprof.c
index fe23d6d..4862000 100644
--- a/kernel/trace/trace_sysprof.c
+++ b/kernel/trace/trace_sysprof.c
@@ -95,13 +95,12 @@ const static struct stacktrace_ops backtrace_ops = {
 	.address		= backtrace_address,
 };
 
-static struct pt_regs *
+static int
 trace_kernel(struct pt_regs *regs, struct trace_array *tr,
 	     struct trace_array_cpu *data)
 {
 	struct backtrace_info info;
 	unsigned long bp;
-	char *user_stack;
 	char *stack;
 
 	info.tr = tr;
@@ -119,10 +118,7 @@ trace_kernel(struct pt_regs *regs, struct trace_array *tr,
 
 	dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, &info);
 
-	/* Now trace the user stack */
-	user_stack = ((char *)current->thread.sp0 - sizeof(struct pt_regs));
-
-	return (struct pt_regs *)user_stack;
+	return info.pos;
 }
 
 static void timer_notify(struct pt_regs *regs, int cpu)
@@ -150,32 +146,44 @@ static void timer_notify(struct pt_regs *regs, int cpu)
 	__trace_special(tr, data, 0, 0, current->pid);
 
 	if (!is_user)
-		regs = trace_kernel(regs, tr, data);
-
-	fp = (void __user *)regs->bp;
-
-	__trace_special(tr, data, 2, regs->ip, 0);
-
-	for (i = 0; i < sample_max_depth; i++) {
-		frame.next_fp = 0;
-		frame.return_address = 0;
-		if (!copy_stack_frame(fp, &frame))
-			break;
-		if ((unsigned long)fp < regs->sp)
-			break;
+		i = trace_kernel(regs, tr, data);
+	else
+		i = 0;
 
-		__trace_special(tr, data, 2, frame.return_address,
-			      (unsigned long)fp);
-		fp = frame.next_fp;
+	/*
+	 * Trace user stack if we are not a kernel thread
+	 */
+	if (current->mm && i < sample_max_depth) {
+		regs = (struct pt_regs *)current->thread.sp0 - 1;
+
+		fp = (void __user *)regs->bp;
+	    
+		__trace_special(tr, data, 2, regs->ip, 0);
+
+		while (i < sample_max_depth) {
+			frame.next_fp = 0;
+			frame.return_address = 0;
+			if (!copy_stack_frame(fp, &frame))
+				break;
+			if ((unsigned long)fp < regs->sp)
+				break;
+			
+			__trace_special(tr, data, 2, frame.return_address,
+					(unsigned long)fp);
+			fp = frame.next_fp;
+
+			i++;
+		}
+		
 	}
 
-	__trace_special(tr, data, 3, current->pid, i);
-
 	/*
 	 * Special trace entry if we overflow the max depth:
 	 */
 	if (i == sample_max_depth)
 		__trace_special(tr, data, -1, -1, -1);
+	
+	__trace_special(tr, data, 3, current->pid, i);
 }
 
 static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
-- 
1.5.4.5


             reply	other threads:[~2008-05-12  3:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-12  3:28 Soeren Sandmann [this message]
2008-05-13 13:52 ` Ingo Molnar

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=ye8tzh4dwul.fsf@camel09.daimi.au.dk \
    --to=sandmann@daimi.au.dk \
    --cc=mingo@elte.hu \
    --cc=pq@iki.fi \
    --cc=proski@gnu.org \
    --cc=rostedt@goodmis.org \
    /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

all inboxes | Powered by JetHome®