Skip to contentSkip to conversation

x86: fix kernel_trap_sp()

PATCH In review Masami Hiramatsu 4 participants 10 messages May 11, 2009 – May 12, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: fix kernel_trap_sp()

Use &regs->sp instead of regs for getting the top of stack in kernel mode. (on x86-64, regs->sp always points the top of stack)

[ impact: Oprofile decodes only stack for backtracing on i386 ]

Cc Harvey Harrison <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Jan Blunck <[email protected]>
Cc Christoph Hellwig <[email protected]>

---

 arch/x86/include/asm/ptrace.h |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/ptrace.h +2 -2
index 5cdd19f..90b76b3 100644
@@ -187,14 +187,14 @@ static inline int v8086_mode(struct pt_regs *regs)
 
 /*
  * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
- * when it traps.  So regs will be the current sp.
+ * when it traps.  So &regs->sp will be the current sp.
  *
  * This is valid only for kernel mode traps.
  */
 static inline unsigned long kernel_trap_sp(struct pt_regs *regs)
 {
 #ifdef CONFIG_X86_32
-	return (unsigned long)regs;
+	return (unsigned long)&regs->sp;
 #else
 	return regs->sp;
 #endif
#1 lore raw

[tip:x86/urgent] x86, 32-bit: fix kernel_trap_sp()

Commit-ID:  11d0f25f06fb67e707e6ed4bf9967b859e403639
Gitweb:     http://git.kernel.org/tip/11d0f25f06fb67e707e6ed4bf9967b859e403639
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Mon, 11 May 2009 17:03:00 -0400
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 11 May 2009 23:49:17 +0200

x86, 32-bit: fix kernel_trap_sp()

Use &regs->sp instead of regs for getting the top of stack in kernel mode. (on x86-64, regs->sp always points the top of stack)

[ Impact: Oprofile decodes only stack for backtracing on i386 ]

Cc Masami Hiramatsu <[email protected]>
Cc Harvey Harrison <[email protected]>
Cc Jan Blunck <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Linus Torvalds <[email protected]>

LKML-Reference: <[email protected]>

---
 arch/x86/include/asm/ptrace.h |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/ptrace.h +2 -2
index e304b66..aed0894 100644
@@ -187,14 +187,14 @@ static inline int v8086_mode(struct pt_regs *regs)
 
 /*
  * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
- * when it traps.  So regs will be the current sp.
+ * when it traps.  So &regs->sp will be the current sp.
  *
  * This is valid only for kernel mode traps.
  */
 static inline unsigned long kernel_trap_sp(struct pt_regs *regs)
 {
 #ifdef CONFIG_X86_32
-	return (unsigned long)regs;
+	return (unsigned long)&regs->sp;
 #else
 	return regs->sp;
 #endif
#2 lore raw
In reply to Masami Hiramatsu
Quoted 3 lines · Masami Hiramatsu

Use &regs->sp instead of regs for getting the top of stack in kernel mode. (on x86-64, regs->sp always points the top of stack)

Ack.

That said, we have only _one_ use of this "kernel_trap_sp()" in the whole kernel, and that use is actually fairly odd too, in that it does it _before_ checking that it's in kernel mode.

Admittedly it will then only _use_ the value after it has checked that things are in kernel mode, but it all boils down to "ok, that's pretty odd".

So how about fixing that, and also fixing the naming of the function. Call it "kernel_stack_pointer()" to match its more widely used sibling function "user_stack_pointer()".

IOW, something like this?

Linus

---
 arch/x86/include/asm/ptrace.h |    7 ++++---
 arch/x86/oprofile/backtrace.c |    2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/ptrace.h +4 -3
index e304b66..624f133 100644
@@ -187,14 +187,15 @@ static inline int v8086_mode(struct pt_regs *regs)
 
 /*
  * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
- * when it traps.  So regs will be the current sp.
+ * when it traps.  The previous stack will be directly underneath the saved
+ * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
  *
  * This is valid only for kernel mode traps.
  */
-static inline unsigned long kernel_trap_sp(struct pt_regs *regs)
+static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
 {
 #ifdef CONFIG_X86_32
-	return (unsigned long)regs;
+	return (unsigned long)(&regs->sp);
 #else
 	return regs->sp;
 #endif
arch/x86/oprofile/backtrace.c +1 -1
index 04df67f..044897b 100644
@@ -76,9 +76,9 @@ void
 x86_backtrace(struct pt_regs * const regs, unsigned int depth)
 {
 	struct frame_head *head = (struct frame_head *)frame_pointer(regs);
-	unsigned long stack = kernel_trap_sp(regs);
 
 	if (!user_mode_vm(regs)) {
+		unsigned long stack = kernel_stack_pointer(regs);
 		if (depth)
 			dump_trace(NULL, regs, (unsigned long *)stack, 0,
 				   &backtrace_ops, &depth);
#3 lore raw
Quoted 15 lines · Linus Torvalds
Quoted 3 lines · Masami Hiramatsu

Use &regs->sp instead of regs for getting the top of stack in kernel mode. (on x86-64, regs->sp always points the top of stack)

Ack.

That said, we have only _one_ use of this "kernel_trap_sp()" in the whole kernel, and that use is actually fairly odd too, in that it does it _before_ checking that it's in kernel mode.

Admittedly it will then only _use_ the value after it has checked that things are in kernel mode, but it all boils down to "ok, that's pretty odd".

Quoted 5 lines

So how about fixing that, and also fixing the naming of the function. Call it "kernel_stack_pointer()" to match its more widely used sibling function "user_stack_pointer()".

IOW, something like this?

yeah, this is cleaner and probably a tad faster. I've applied it to x86/urgent with your acked-by - or would you like to apply this straight away?

The use of the backtracing feature on the oprofile side is not very common (and the bug is ancient) so i wanted to wait a few days with this.

One small detail:

arch/x86/include/asm/ptrace.h static inline int v8086_mode(struct pt_regs *regs) · line 198

Quoted 1 line
arch/x86/include/asm/ptrace.h +1 -0
+	return (unsigned long)(&regs->sp);

the original commit had:

Review

Quoted 1 line
(quoted diff) +1 -0
+	return (unsigned long)&regs->sp;

and this latter is correct as well due to operator priorities. No strong preference from my side so i took your version as-is. I kept Masami as the author and a comment for your changes.

( Not sure how to express this in a changelog properly - i didnt
want two commits but it was authored by two people in essence. So i went for the version that will show up in the commit notification email in a few minutes - also attached below. )

Ingo

#4 lore raw
Quoted 2 lines · Ingo Molnar

yeah, this is cleaner and probably a tad faster.

Well, I don't care about the 'faster' part per se, but I could actually see some case where a kernel-only version did some pointer dereference that was invalid for the user version, and could oops, so putting it inside the code that explicitly tests that it's not user-or-vm seems like conceptually the right thing to do.

Of course, in this case it's the other way around - it's the non-kernel version that does a dereference, and it just so happens to be safe (but return an invalid pointer) when the trap was in kernel mode. So the argument is pretty theoretical, but I think it's cleaner.

Quoted 7 lines

One small detail:

arch/x86/include/asm/ptrace.h static inline int v8086_mode(struct pt_regs *regs) · line 198

Quoted 1 line
arch/x86/include/asm/ptrace.h +1 -0
+	return (unsigned long)(&regs->sp);

the original commit had:

Review

Quoted 1 line
(quoted diff) +1 -0
+	return (unsigned long)&regs->sp;

Ok, that's just because I didn't actually apply the original patch, I just rewrote it entirely, and for some reason I put parenthesis around the expression. You're right that it doesn't matter, and either is fine. I don't really care, I suspect I add the parenthesis just because I don't even want to have to think about the proper operator precedence rules.

So pick whichever version.

Linus

#5 lore raw
In reply to Linus Torvalds
Quoted 36 lines · Linus Torvalds
Quoted 3 lines · Masami Hiramatsu

Use &regs->sp instead of regs for getting the top of stack in kernel mode. (on x86-64, regs->sp always points the top of stack)

Ack.

That said, we have only _one_ use of this "kernel_trap_sp()" in the whole kernel, and that use is actually fairly odd too, in that it does it _before_ checking that it's in kernel mode.

Admittedly it will then only _use_ the value after it has checked that things are in kernel mode, but it all boils down to "ok, that's pretty odd".

So how about fixing that, and also fixing the naming of the function. Call it "kernel_stack_pointer()" to match its more widely used sibling function "user_stack_pointer()".

IOW, something like this?

Linus

---
 arch/x86/include/asm/ptrace.h |    7 ++++---
 arch/x86/oprofile/backtrace.c |    2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/ptrace.h +0 -0
index e304b66..624f133 100644
@@ -187,14 +187,15 @@ static inline int v8086_mode(struct pt_regs *regs)
 

arch/x86/include/asm/ptrace.h static inline int v8086_mode(struct pt_regs *regs) · line 195

Quoted 10 lines
arch/x86/include/asm/ptrace.h +3 -2
 /*
  * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
- * when it traps.  So regs will be the current sp.
+ * when it traps.  The previous stack will be directly underneath the saved
+ * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
  *
  * This is valid only for kernel mode traps.
  */
-static inline unsigned long kernel_trap_sp(struct pt_regs *regs)
+static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)

Why not have it return an unsigned long *?

Quoted 12 lines
arch/x86/include/asm/ptrace.h +1 -1
 {
 #ifdef CONFIG_X86_32
-	return (unsigned long)regs;
+	return (unsigned long)(&regs->sp);
 #else
 	return regs->sp;
 #endif
arch/x86/oprofile/backtrace.c +0 -0
index 04df67f..044897b 100644

arch/x86/oprofile/backtrace.c void · line 81

Quoted 7 lines
arch/x86/oprofile/backtrace.c +1 -1
@@ -76,9 +76,9 @@ void
 x86_backtrace(struct pt_regs * const regs, unsigned int depth)
 {
 	struct frame_head *head = (struct frame_head *)frame_pointer(regs);
-	unsigned long stack = kernel_trap_sp(regs);
 
 	if (!user_mode_vm(regs)) {
+		unsigned long stack = kernel_stack_pointer(regs);

Make this an unsigned long *?

arch/x86/oprofile/backtrace.c void · line 83

Quoted 2 lines
arch/x86/oprofile/backtrace.c +0 -0
 		if (depth)
 			dump_trace(NULL, regs, (unsigned long *)stack, 0,

Then get rid of the cast?

Robin

#6 lore raw
Quoted 46 lines · Robin Holt
Quoted 33 lines · Linus Torvalds
Quoted 2 lines · Masami Hiramatsu

Use &regs->sp instead of regs for getting the top of stack in kernel mode. (on x86-64, regs->sp always points the top of stack)

Ack.

That said, we have only _one_ use of this "kernel_trap_sp()" in the whole kernel, and that use is actually fairly odd too, in that it does it _before_ checking that it's in kernel mode.

Admittedly it will then only _use_ the value after it has checked that things are in kernel mode, but it all boils down to "ok, that's pretty odd".

So how about fixing that, and also fixing the naming of the function. Call it "kernel_stack_pointer()" to match its more widely used sibling function "user_stack_pointer()".

IOW, something like this?

Linus

---
 arch/x86/include/asm/ptrace.h |    7 ++++---
 arch/x86/oprofile/backtrace.c |    2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/ptrace.h +0 -0
index e304b66..624f133 100644
@@ -187,14 +187,15 @@ static inline int v8086_mode(struct pt_regs *regs)
 

arch/x86/include/asm/ptrace.h static inline int v8086_mode(struct pt_regs *regs) · line 195

Quoted 10 lines
arch/x86/include/asm/ptrace.h +3 -2
 /*
  * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
- * when it traps.  So regs will be the current sp.
+ * when it traps.  The previous stack will be directly underneath the saved
+ * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
  *
  * This is valid only for kernel mode traps.
  */
-static inline unsigned long kernel_trap_sp(struct pt_regs *regs)
+static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)

Why not have it return an unsigned long *?

Quoted 6 lines

arch/x86/include/asm/ptrace.h static inline int v8086_mode(struct pt_regs *regs) · line 200

Quoted 6 lines
arch/x86/include/asm/ptrace.h +1 -1
 {
 #ifdef CONFIG_X86_32
-	return (unsigned long)regs;
+	return (unsigned long)(&regs->sp);
 #else
 	return regs->sp;

Perhaps, you might forget that this line needs a cast :-).

Anyway, IMHO, it does not come from coding, but meaning. kernel/user_stack_pointer() just return
"the value of stack pointer register", not a "stack pointer".

Thank you,

Quoted 22 lines

arch/x86/oprofile/backtrace.c void · line 81

Quoted 13 lines
arch/x86/include/asm/ptrace.h +0 -0
 #endif
arch/x86/oprofile/backtrace.c +1 -1
index 04df67f..044897b 100644
@@ -76,9 +76,9 @@ void
 x86_backtrace(struct pt_regs * const regs, unsigned int depth)
 {
 	struct frame_head *head = (struct frame_head *)frame_pointer(regs);
-	unsigned long stack = kernel_trap_sp(regs);
 
 	if (!user_mode_vm(regs)) {
+		unsigned long stack = kernel_stack_pointer(regs);

Make this an unsigned long *?

arch/x86/oprofile/backtrace.c void · line 83

Quoted 2 lines
arch/x86/oprofile/backtrace.c +0 -0
 		if (depth)
 			dump_trace(NULL, regs, (unsigned long *)stack, 0,

Then get rid of the cast?

Robin

Signature · 7 lines
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America) Inc.
Software Solutions Division

e-mail: [email protected]
#7 lore raw
In reply to Robin Holt
Quoted 5 lines · Robin Holt

arch/x86/include/asm/ptrace.h static inline int v8086_mode(struct pt_regs *regs) · line 195

Quoted 3 lines

*/

arch/x86/include/asm/ptrace.h +1 -1
-static inline unsigned long kernel_trap_sp(struct pt_regs *regs)
+static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)

Why not have it return an unsigned long *?

I considered it, but since part of the point of the patch was to make it look like the mirror of "user_stack_pointer()", returning "unsigned long" looked more appropriate.

But I have no really strong opinions. I wouldn't object to making it return "unsigned long *" either.

Linus

#8 lore raw

[tip:x86/urgent] x86, 32-bit: fix kernel_trap_sp()

In reply to Masami Hiramatsu
Commit-ID:  8310d65ae37ce9180cd939334218df59f1ad3de6
Gitweb:     http://git.kernel.org/tip/8310d65ae37ce9180cd939334218df59f1ad3de6
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Mon, 11 May 2009 17:03:00 -0400
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 12 May 2009 00:37:40 +0200

x86, 32-bit: fix kernel_trap_sp()

Use &regs->sp instead of regs for getting the top of stack in kernel mode. (on x86-64, regs->sp always points the top of stack)

[ Impact: Oprofile decodes only stack for backtracing on i386 ]

[ v2: rename the API to kernel_stack_pointer(), move variable inside ]
Cc Masami Hiramatsu <[email protected]>
Cc Harvey Harrison <[email protected]>
Cc Jan Blunck <[email protected]>
Cc Christoph Hellwig <[email protected]>

LKML-Reference: <[email protected]>

---
 arch/x86/include/asm/ptrace.h |    7 ++++---
 arch/x86/oprofile/backtrace.c |    2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/ptrace.h +4 -3
index e304b66..624f133 100644
@@ -187,14 +187,15 @@ static inline int v8086_mode(struct pt_regs *regs)
 
 /*
  * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
- * when it traps.  So regs will be the current sp.
+ * when it traps.  The previous stack will be directly underneath the saved
+ * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
  *
  * This is valid only for kernel mode traps.
  */
-static inline unsigned long kernel_trap_sp(struct pt_regs *regs)
+static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
 {
 #ifdef CONFIG_X86_32
-	return (unsigned long)regs;
+	return (unsigned long)(&regs->sp);
 #else
 	return regs->sp;
 #endif
arch/x86/oprofile/backtrace.c +1 -1
index 04df67f..044897b 100644
@@ -76,9 +76,9 @@ void
 x86_backtrace(struct pt_regs * const regs, unsigned int depth)
 {
 	struct frame_head *head = (struct frame_head *)frame_pointer(regs);
-	unsigned long stack = kernel_trap_sp(regs);
 
 	if (!user_mode_vm(regs)) {
+		unsigned long stack = kernel_stack_pointer(regs);
 		if (depth)
 			dump_trace(NULL, regs, (unsigned long *)stack, 0,
 				   &backtrace_ops, &depth);
#9 lore raw

[tip:x86/urgent] x86, 32-bit: fix kernel_trap_sp()

In reply to Masami Hiramatsu
Commit-ID:  7b6c6c77732ca1d2498eda7eabb64f9648896e96
Gitweb:     http://git.kernel.org/tip/7b6c6c77732ca1d2498eda7eabb64f9648896e96
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Mon, 11 May 2009 17:03:00 -0400
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 12 May 2009 00:39:52 +0200

x86, 32-bit: fix kernel_trap_sp()

Use &regs->sp instead of regs for getting the top of stack in kernel mode. (on x86-64, regs->sp always points the top of stack)

[ Impact: Oprofile decodes only stack for backtracing on i386 ]

[ v2: rename the API to kernel_stack_pointer(), move variable inside ]
Cc Harvey Harrison <[email protected]>
Cc Jan Blunck <[email protected]>
Cc Christoph Hellwig <[email protected]>

LKML-Reference: <[email protected]>

---
 arch/x86/include/asm/ptrace.h |    7 ++++---
 arch/x86/oprofile/backtrace.c |    2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/ptrace.h +4 -3
index e304b66..624f133 100644
@@ -187,14 +187,15 @@ static inline int v8086_mode(struct pt_regs *regs)
 
 /*
  * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
- * when it traps.  So regs will be the current sp.
+ * when it traps.  The previous stack will be directly underneath the saved
+ * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
  *
  * This is valid only for kernel mode traps.
  */
-static inline unsigned long kernel_trap_sp(struct pt_regs *regs)
+static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
 {
 #ifdef CONFIG_X86_32
-	return (unsigned long)regs;
+	return (unsigned long)(&regs->sp);
 #else
 	return regs->sp;
 #endif
arch/x86/oprofile/backtrace.c +1 -1
index 04df67f..044897b 100644
@@ -76,9 +76,9 @@ void
 x86_backtrace(struct pt_regs * const regs, unsigned int depth)
 {
 	struct frame_head *head = (struct frame_head *)frame_pointer(regs);
-	unsigned long stack = kernel_trap_sp(regs);
 
 	if (!user_mode_vm(regs)) {
+		unsigned long stack = kernel_stack_pointer(regs);
 		if (depth)
 			dump_trace(NULL, regs, (unsigned long *)stack, 0,
 				   &backtrace_ops, &depth);