linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tong Li <tongli@intel.com>
To: phil.el@wanadoo.fr
Cc: linux-kernel@vger.kernel.org, tongli@cs.duke.edu
Subject: [PATCH 1/1] OProfile: fixed x86_64 incorrect kernel call graphs
Date: Thu, 19 Jan 2006 12:02:01 -0800 (PST)	[thread overview]
Message-ID: <Pine.LNX.4.64.0601191122281.4881@tongli.jf.intel.com> (raw)

From: Tong Li <tong.n.li@intel.com>

This patch fixes the problem in kernel 2.6.15.1 (and early versions) that 
OProfile on x86_64 does not correctly collect the stack traces for kernel 
functions. The original code in valid_kernel_stack() in 
arch/i386/oprofile/backtrace.c assumes that the frame pointer (headaddr) 
should be greater than stack (i.e., regs). This assumption is wrong for 
x86_64 because NMIs in x86_64 use a seperate stack different from the 
kernel stack. Therefore, the variable stack now points to some location on 
the NMI stack, which turns out to be at a higher address than the frame 
pointer (headaddr) on the kernel stack. The correct comparison here should 
be between headaddr and regs->rsp for x86_64.

Signed-off-by: Tong Li <tong.n.li@intel.com>
---
--- linux-2.6.15.1/arch/i386/oprofile/backtrace.c.orig	2006-01-18 22:29:56.000000000 -0800
+++ linux-2.6.15.1/arch/i386/oprofile/backtrace.c	2006-01-19 09:31:32.000000000 -0800
@@ -49,7 +49,9 @@ dump_backtrace(struct frame_head * head)
   * |    stack    |
   * --------------- saved regs->ebp value if valid (frame_head address)
   * .             .
- * --------------- struct pt_regs stored on stack (struct pt_regs *)
+ * --------------- saved regs->rsp value if x86_64
+ * |             |
+ * --------------- struct pt_regs * stored on stack if 32-bit
   * |             |
   * .             .
   * |             |
@@ -57,13 +59,26 @@ dump_backtrace(struct frame_head * head)
   * |             |
   * |             | \/ Lower addresses
   *
- * Thus, &pt_regs <-> stack base restricts the valid(ish) ebp values
+ * Thus, regs (or regs->rsp for x86_64) <-> stack base restricts the 
+ * valid(ish) ebp values. Note: (1) for x86_64, NMI and several other 
+ * exceptions use special stacks, maintained by the interrupt stack table
+ * (IST). These stacks are set up in trap_init() in
+ * arch/x86_64/kernel/traps.c. Thus, for x86_64, regs now does not point 
+ * to the kernel stack; instead, it points to some location on the NMI 
+ * stack. On the other hand, regs->rsp is the stack pointer saved when the 
+ * NMI occurred. (2) For 32-bit, regs->esp is not valid because the
+ * processor does not save %esp on the kernel stack when interrupts occur 
+ * in the kernel mode.
   */
  #ifdef CONFIG_FRAME_POINTER
  static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs)
  {
  	unsigned long headaddr = (unsigned long)head;
+#ifdef CONFIG_X86_64
+	unsigned long stack = (unsigned long)regs->rsp;
+#else
  	unsigned long stack = (unsigned long)regs;
+#endif
  	unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE;

  	return headaddr > stack && headaddr < stack_base;

                 reply	other threads:[~2006-01-19 20:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=Pine.LNX.4.64.0601191122281.4881@tongli.jf.intel.com \
    --to=tongli@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=phil.el@wanadoo.fr \
    --cc=tongli@cs.duke.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).