Skip to contentSkip to conversation

Breakage in arch/mips/kernel/traps.c for 64bit

DISCUSSION Thomas Bogendoerfer 4 participants 10 messages May 1, 2008 – May 5, 2008 MIPS

lore
Share & tools
lore

Thomas Bogendoerfer

author maintainer
#0 lore raw

Hi,

it would be nice, if people started thinking before supplying such crappy^Winteresting code:

arch/mips/kernel/traps.c:

#define IS_KVA01(a) ((((unsigned int)a) & 0xc0000000) == 0x80000000)

Kills every 64bit kernel build...

Why is this needed at all ?

Thomas.

Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessary a
good idea.                                                [ RFC1925, 2.3 ]
#1 lore raw
Quoted 8 lines · Thomas Bogendoerfer

it would be nice, if people started thinking before supplying such crappy^Winteresting code:

arch/mips/kernel/traps.c:

#define IS_KVA01(a) ((((unsigned int)a) & 0xc0000000) == 0x80000000)

Kills every 64bit kernel build...

Not everybody tests 64-bit stuff as some people limit themselves to
32-bit systems only. It looks like a step backwards, but there you go.

Quoted 1 line

Why is this needed at all ?

It looks like an attempt to avoid TLB exceptions for the stack dump -- if
that is the case, then obviously a piece of code like one in arch/mips/lib/uncached.c should be used to check for CKSEG0/1 and XKPHYS. If there are two uses of this code, then it should be wrapped in an inline function and put in a header; <asm/addrspace.h>, perhaps.

Maciej

#2 lore raw
In reply to Thomas Bogendoerfer
Quoted 10 lines · Thomas Bogendoerfer

it would be nice, if people started thinking before supplying such crappy^Winteresting code:

arch/mips/kernel/traps.c:

#define IS_KVA01(a) ((((unsigned int)a) & 0xc0000000) == 0x80000000)

Kills every 64bit kernel build...

Why is this needed at all ?

It came as part of 39b8d5254246ac56342b72f812255c8f7a74dca9 which is a patch amalgated from several other patches. Below is the original patch it came with. I think the idea of the patch is valid but the idea needs a bit of mending.

From: Chris Dearman <[email protected]>
Date: Wed, 3 Oct 2007 10:19:18 +0100
Subject: [PATCH] Skip raw backtrace for non KSEG stack addresses
This is to avoid recursive stackdumps as the kernel kernel falls off of the user stack.

arch/mips/kernel/traps.c +9 -6
index 2948b86..3d56171 100644
@@ -79,19 +79,22 @@ void (*board_bind_eic_interrupt)(int irq, int regset);
 
 static void show_raw_backtrace(unsigned long reg29)
 {
-	unsigned long *sp = (unsigned long *)reg29;
+	unsigned long *sp = (unsigned long *)(reg29 & ~3);
 	unsigned long addr;
 
 	printk("Call Trace:");
 #ifdef CONFIG_KALLSYMS
 	printk("\n");
 #endif
-	while (!kstack_end(sp)) {
-		addr = *sp++;
-		if (__kernel_text_address(addr))
-			print_ip_sym(addr);
+#define IS_KVA01(a) ((((unsigned int)a) & 0xc0000000) == 0x80000000)
+	if (IS_KVA01(sp)) {
+		while (!kstack_end(sp)) {
+			addr = *sp++;
+			if (__kernel_text_address(addr))
+				print_ip_sym(addr);
+		}
+		printk("\n");
 	}
-	printk("\n");
 }
 
 #ifdef CONFIG_KALLSYMS
#3 lore raw
Quoted 4 lines · Ralf Baechle

It came as part of 39b8d5254246ac56342b72f812255c8f7a74dca9 which is a patch amalgated from several other patches. Below is the original patch it came with. I think the idea of the patch is valid but the idea needs a bit of mending.

Then how about this fix?

---------------------------------------------------------------------
Subject: [PATCH] Fix detection of kernel segment on 64-bit

---

arch/mips/kernel/traps.c +14 -2
index cb8b0e2..7893bb3 100644
@@ -78,6 +78,19 @@ void (*board_nmi_handler_setup)(void);
 void (*board_ejtag_handler_setup)(void);
 void (*board_bind_eic_interrupt)(int irq, int regset);
 
+static inline int kernel_unmapped_seg(void *addr)
+{
+	unsigned long a = (unsigned long)addr;
+
+#ifdef CONFIG_32BIT
+	/* KSEG0 or KSEG1 */
+	return (a & 0xc0000000) == KSEG0;
+#else
+	/* CKSEG0, CKSEG1 or XKPHYS  */
+	return ((a & 0xffffffffc0000000L) == CKSEG0) ||
+		((a & 0xc000000000000000L) == XKPHYS);
+#endif
+}
 
 static void show_raw_backtrace(unsigned long reg29)
 {
@@ -88,8 +101,7 @@ static void show_raw_backtrace(unsigned long reg29)
 #ifdef CONFIG_KALLSYMS
 	printk("\n");
 #endif
-#define IS_KVA01(a) ((((unsigned int)a) & 0xc0000000) == 0x80000000)
-	if (IS_KVA01(sp)) {
+	if (kernel_unmapped_seg(sp)) {
 		while (!kstack_end(sp)) {
 			addr = *sp++;
 			if (__kernel_text_address(addr))
#4 lore raw
Quoted 12 lines · Atsushi Nemoto

Then how about this fix?

---------------------------------------------------------------------
Subject: [PATCH] Fix detection of kernel segment on 64-bit

---

arch/mips/kernel/traps.c +0 -0
index cb8b0e2..7893bb3 100644

arch/mips/kernel/traps.c void (*board_nmi_handler_setup)(void); · line 87

Quoted 10 lines
arch/mips/kernel/traps.c +7 -0
@@ -78,6 +78,19 @@ void (*board_nmi_handler_setup)(void);
 void (*board_ejtag_handler_setup)(void);
 void (*board_bind_eic_interrupt)(int irq, int regset);
 
+static inline int kernel_unmapped_seg(void *addr)
+{
+	unsigned long a = (unsigned long)addr;
+
+#ifdef CONFIG_32BIT
+	/* KSEG0 or KSEG1 */
+	return (a & 0xc0000000) == KSEG0;

Slightly cleaner:

  return KSEGX(a) == KSEG0;

Unfortunately there is no such macro for the 64-bit segments nor does the existing KSEGX() work correctly for non-CKSEGx 64-bit addresses.

Ralf

#5 lore raw
Quoted 3 lines · Ralf Baechle

Slightly cleaner:

  return KSEGX(a) == KSEG0;

You mean:

return KSEGX(a) == KSEG0 || KSEGX(a) == KSEG1;

right?

Quoted 2 lines

Unfortunately there is no such macro for the 64-bit segments nor does the existing KSEGX() work correctly for non-CKSEGx 64-bit addresses.

As I mentioned there is suitable code doing exactly this in
arch/mips/lib/uncached.c and it can be extracted to an inline function to be put in <asm/addrspace.h> to be reused here and in the future possibly elsewhere.

Maciej

author maintainer
#6 lore raw
In reply to Atsushi Nemoto
Quoted 7 lines · Atsushi Nemoto
Quoted 4 lines · Ralf Baechle

It came as part of 39b8d5254246ac56342b72f812255c8f7a74dca9 which is a patch amalgated from several other patches. Below is the original patch it came with. I think the idea of the patch is valid but the idea needs a bit of mending.

Then how about this fix?

hmm, why not simply use __get_user() when accessing the stack content ? show_stacktrace() already does it for stack dumping ? This would avoid any work for whatever sick stack mappings. Below is a patch, which does this.

Thomas.

The newly added check for valid stack pointer address breaks at least for 64bit kernels. Use __get_user() for accessing stack content to avoid crashes, when doing the backtrace.

---

 arch/mips/kernel/traps.c |   16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)
arch/mips/kernel/traps.c +8 -8
index cb8b0e2..c9ce8d6 100644
@@ -81,22 +81,22 @@ void (*board_bind_eic_interrupt)(int irq, int regset);
 
 static void show_raw_backtrace(unsigned long reg29)
 {
-	unsigned long *sp = (unsigned long *)(reg29 & ~3);
+	unsigned long __user *sp = (unsigned long __user *)(reg29 & ~3);
 	unsigned long addr;
 
 	printk("Call Trace:");
 #ifdef CONFIG_KALLSYMS
 	printk("\n");
 #endif
-#define IS_KVA01(a) ((((unsigned int)a) & 0xc0000000) == 0x80000000)
-	if (IS_KVA01(sp)) {
-		while (!kstack_end(sp)) {
-			addr = *sp++;
-			if (__kernel_text_address(addr))
-				print_ip_sym(addr);
+	while (!kstack_end(sp)) {
+		if (__get_user(addr, sp++)) {
+			printk(" (Bad stack address)");
+			break;
 		}
-		printk("\n");
+		if (__kernel_text_address(addr))
+			print_ip_sym(addr);
 	}
+	printk("\n");
 }
 
 #ifdef CONFIG_KALLSYMS
Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessary a
good idea.                                                [ RFC1925, 2.3 ]
#7 lore raw
Quoted 4 lines · On Sun, 4 May 2008 00:48:49 +0200, [email protected] (Thomas…

hmm, why not simply use __get_user() when accessing the stack content ? show_stacktrace() already does it for stack dumping ? This would avoid any work for whatever sick stack mappings. Below is a patch, which does this.

I like this patch. One minor request:

Quoted 1 line
arch/mips/kernel/traps.c +1 -0
+	unsigned long __user *sp = (unsigned long __user *)(reg29 & ~3);

...

arch/mips/kernel/traps.c void (*board_bind_eic_interrupt)(int irq, int regset); · line 92

Quoted 2 lines
arch/mips/kernel/traps.c +2 -0
+	while (!kstack_end(sp)) {
+		if (__get_user(addr, sp++)) {

This will leads a sparse warning since an argument for kstack_end is 'void *'.

	while (!kstack_end((void *)(unsigned long)sp)) {

will make this part sparse-free, though it seems a bit ugly.

---
Atsushi Nemoto

author maintainer
#8 lore raw
Quoted 10 lines · Atsushi Nemoto
Quoted 1 line
arch/mips/kernel/traps.c +1 -0
+	unsigned long __user *sp = (unsigned long __user *)(reg29 & ~3);

...

arch/mips/kernel/traps.c void (*board_bind_eic_interrupt)(int irq, int regset); · line 92

Quoted 2 lines
arch/mips/kernel/traps.c +2 -0
+	while (!kstack_end(sp)) {
+		if (__get_user(addr, sp++)) {

This will leads a sparse warning since an argument for kstack_end is 'void *'.

	while (!kstack_end((void *)(unsigned long)sp)) {

will make this part sparse-free, though it seems a bit ugly.

hmm, would leaving sp as unsigned long * and casting it for __get_user() make sparse happy ?

Thomas.

Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessary a
good idea.                                                [ RFC1925, 2.3 ]
#9 lore raw
Quoted 6 lines · On Mon, 5 May 2008 00:08:04 +0200, [email protected] (Thomas…
Quoted 3 lines
	while (!kstack_end((void *)(unsigned long)sp)) {

will make this part sparse-free, though it seems a bit ugly.

hmm, would leaving sp as unsigned long * and casting it for __get_user() make sparse happy ?

Well, not as expected... Here is some warning patterns.

1.	unsigned long __user *sp = (unsigned long __user *)(reg29 & ~3);
	...
	while (!kstack_end(sp)) {
		if (__get_user(addr, sp++)) {

linux/arch/mips/kernel/traps.c:91:21: warning: incorrect type in argument 1 (different address spaces)
linux/arch/mips/kernel/traps.c:91:21: expected void *addr
linux/arch/mips/kernel/traps.c:91:21: got unsigned long [noderef] <asn:1>*sp

2.	unsigned long *sp = (unsigned long *)(reg29 & ~3);
	...
	while (!kstack_end(sp)) {
		if (__get_user(addr, (unsigned long __user *)sp++)) {

linux/arch/mips/kernel/traps.c:92:7: warning: cast adds address space to expression (<asn:1>) linux/arch/mips/kernel/traps.c:92:7: warning: cast adds address space to expression (<asn:1>) linux/arch/mips/kernel/traps.c:92:7: warning: cast adds address space to expression (<asn:1>) linux/arch/mips/kernel/traps.c:92:7: warning: cast adds address space to expression (<asn:1>) linux/arch/mips/kernel/traps.c:92:7: warning: cast adds address space to expression (<asn:1>) linux/arch/mips/kernel/traps.c:92:7: warning: cast adds address space to expression (<asn:1>) linux/arch/mips/kernel/traps.c:92:7: warning: cast adds address space to expression (<asn:1>) linux/arch/mips/kernel/traps.c:92:7: warning: cast adds address space to expression (<asn:1>) linux/arch/mips/kernel/traps.c:92:7: warning: cast adds address space to expression (<asn:1>)

3.	unsigned long __user *sp = (unsigned long __user *)(reg29 & ~3);
	...
	while (!kstack_end((void *)(unsigned long)sp)) {
		if (__get_user(addr, sp++)) {

No warnings.

4.	unsigned long *sp = (unsigned long *)(reg29 & ~3);
	...
	while (!kstack_end(sp)) {
		unsigned long __user *p = (unsigned long __user *)sp++;
		if (__get_user(addr, p)) {

linux/arch/mips/kernel/traps.c:92:30: warning: cast adds address space to expression (<asn:1>)

4.	unsigned long *sp = (unsigned long *)(reg29 & ~3);
	...
	while (!kstack_end(sp)) {
		unsigned long __user *p =
			(unsigned long __user *)(unsigned long)sp++;
		if (__get_user(addr, p)) {

No warnings.

I think the "cast adds address space to expression" sparse warning is not worth to handle so seriously. So I'm OK with any of (2) to (4).

---
Atsushi Nemoto