Skip to contentSkip to conversation

x86: fix virt_addr_valid() with CONFIG_DEBUG_VIRTUAL=y

PATCH In review Vegard Nossum 4 participants 11 messages Oct 1, 2008 – Oct 2, 2008

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

PATCH 1/1 x86: fix virt_addr_valid() with CONFIG_DEBUG_VIRTUAL=y

Hi,

Fix for tip/x86/mm-debug (commit 59ea746337c69f6a5f1bc4d5e8544b3cbf12f801). I'm not sure if choice of names/structure is entirely correct, comments are appreciated.

Vegard

Quoted 1 line

From 01613a1949de51c7ab9d0acaaa9a5444722a5cfa Mon Sep 17 00:00:00 2001

From: Vegard Nossum <[email protected]>
Date: Wed, 1 Oct 2008 12:36:34 +0200
Subject: [PATCH] x86: fix virt_addr_valid() with CONFIG_DEBUG_VIRTUAL=y

virt_addr_valid() calls __pa(), which calls __phys_addr(). With CONFIG_DEBUG_VIRTUAL=y, __phys_addr() will kill the kernel if the address *isn't* valid. That's clearly wrong for virt_addr_valid().

---
 arch/x86/kernel/doublefault_32.c |    2 +-
 arch/x86/mm/ioremap.c            |    2 +-
 include/asm-x86/page.h           |    3 ++-
 include/asm-x86/page_32.h        |    4 ++--
 include/asm-x86/page_64.h        |    1 +
 5 files changed, 7 insertions(+), 5 deletions(-)
arch/x86/kernel/doublefault_32.c +1 -1
index 395acb1..1580e0b 100644
@@ -66,6 +66,6 @@ struct tss_struct doublefault_tss __cacheline_aligned = {
 		.ds		= __USER_DS,
 		.fs		= __KERNEL_PERCPU,
 
-		.__cr3		= __phys_addr_const((unsigned long)swapper_pg_dir)
+		.__cr3	= __phys_addr_nodebug((unsigned long)swapper_pg_dir)
 	}
 };
arch/x86/mm/ioremap.c +1 -1
index 835e062..4d49e88 100644
@@ -58,7 +58,7 @@ unsigned long __phys_addr(unsigned long x)
 	/* VMALLOC_* aren't constants; not available at the boot time */
 	VIRTUAL_BUG_ON(x < PAGE_OFFSET || (system_state != SYSTEM_BOOTING &&
 					is_vmalloc_addr((void *)x)));
-	return x - PAGE_OFFSET;
+	return __phys_addr_nodebug(x);
 }
 EXPORT_SYMBOL(__phys_addr);
 #endif
include/asm-x86/page.h +2 -1
index c915747..9b70b41 100644
@@ -179,6 +179,7 @@ static inline pteval_t native_pte_flags(pte_t pte)
 #endif	/* CONFIG_PARAVIRT */
 
 #define __pa(x)		__phys_addr((unsigned long)(x))
+#define __pa_nodebug(x)	__phys_addr_nodebug((unsigned long)(x))
 /* __pa_symbol should be used for C visible symbols.
    This seems to be the official gcc blessed way to do such arithmetic. */
 #define __pa_symbol(x)	__pa(__phys_reloc_hide((unsigned long)(x)))
@@ -190,7 +191,7 @@ static inline pteval_t native_pte_flags(pte_t pte)
 
 #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
 #define pfn_to_kaddr(pfn)      __va((pfn) << PAGE_SHIFT)
-#define virt_addr_valid(kaddr)	pfn_valid(__pa(kaddr) >> PAGE_SHIFT)
+#define virt_addr_valid(kaddr)	pfn_valid(__pa_nodebug(kaddr) >> PAGE_SHIFT)
 
 #endif	/* __ASSEMBLY__ */
 
include/asm-x86/page_32.h +2 -2
index 82b0109..deec501 100644
@@ -71,11 +71,11 @@ typedef struct page *pgtable_t;
 #endif
 
 #ifndef __ASSEMBLY__
-#define __phys_addr_const(x)	((x) - PAGE_OFFSET)
+#define __phys_addr_nodebug(x)	((x) - PAGE_OFFSET)
 #ifdef CONFIG_DEBUG_VIRTUAL
 extern unsigned long __phys_addr(unsigned long);
 #else
-#define __phys_addr(x)		((x) - PAGE_OFFSET)
+#define __phys_addr(x)		__phys_addr_nodebug(x)
 #endif
 #define __phys_reloc_hide(x)	RELOC_HIDE((x), 0)
 
include/asm-x86/page_64.h +1 -0
index 49380b8..8697b6f 100644
@@ -69,6 +69,7 @@ extern unsigned long max_pfn;
 extern unsigned long phys_base;
 
 extern unsigned long __phys_addr(unsigned long);
+#define __phys_addr_nodebug(x) __phys_addr(x)
 #define __phys_reloc_hide(x)	(x)
 
 /*
Signature · 1 line
1.5.6.4
#1 lore raw
Quoted 25 lines · Vegard Nossum

Hi,

Fix for tip/x86/mm-debug (commit 59ea746337c69f6a5f1bc4d5e8544b3cbf12f801). I'm not sure if choice of names/structure is entirely correct, comments are appreciated.

From 01613a1949de51c7ab9d0acaaa9a5444722a5cfa Mon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Wed, 1 Oct 2008 12:36:34 +0200
Subject: [PATCH] x86: fix virt_addr_valid() with CONFIG_DEBUG_VIRTUAL=y

virt_addr_valid() calls __pa(), which calls __phys_addr(). With CONFIG_DEBUG_VIRTUAL=y, __phys_addr() will kill the kernel if the address *isn't* valid. That's clearly wrong for virt_addr_valid().

---

arch/x86/kernel/doublefault_32.c +0 -0
index 395acb1..1580e0b 100644

arch/x86/kernel/doublefault_32.c struct tss_struct doublefault_tss __cacheline_aligned = { · line 69

Quoted 5 lines
arch/x86/kernel/doublefault_32.c +1 -1
@@ -66,6 +66,6 @@ struct tss_struct doublefault_tss __cacheline_aligned = {
 		.ds		= __USER_DS,
 		.fs		= __KERNEL_PERCPU,
 
-		.__cr3		= __phys_addr_const((unsigned long)swapper_pg_dir)
+		.__cr3	= __phys_addr_nodebug((unsigned long)swapper_pg_dir)

Now, this may be switched back to __pa: __pa_nodebug(swapper_pg_dir);

Quoted 2 lines
arch/x86/kernel/doublefault_32.c +0 -0
 	}
 };

[...]

include/asm-x86/page_64.h extern unsigned long max_pfn; · line 73

Quoted 8 lines
include/asm-x86/page_64.h +1 -0
@@ -69,6 +69,7 @@ extern unsigned long max_pfn;
 extern unsigned long phys_base;
 
 extern unsigned long __phys_addr(unsigned long);
+#define __phys_addr_nodebug(x) __phys_addr(x)
 #define __phys_reloc_hide(x)	(x)

x86_64 is screwed in the same way, isn't it?

#2 lore raw

Ah, and thanks for spotting this!

Quoted 2 lines · Jiri Slaby
Quoted 1 line · Vegard Nossum

Subject: [PATCH] x86: fix virt_addr_valid() with CONFIG_DEBUG_VIRTUAL=y

#3 lore raw
In reply to Jiri Slaby
Quoted 17 lines · Jiri Slaby
Quoted 16 lines · Vegard Nossum

Hi,

Fix for tip/x86/mm-debug (commit 59ea746337c69f6a5f1bc4d5e8544b3cbf12f801). I'm not sure if choice of names/structure is entirely correct, comments are appreciated.

From 01613a1949de51c7ab9d0acaaa9a5444722a5cfa Mon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Wed, 1 Oct 2008 12:36:34 +0200
Subject: [PATCH] x86: fix virt_addr_valid() with CONFIG_DEBUG_VIRTUAL=y

virt_addr_valid() calls __pa(), which calls __phys_addr(). With CONFIG_DEBUG_VIRTUAL=y, __phys_addr() will kill the kernel if the address *isn't* valid. That's clearly wrong for virt_addr_valid().

...

Quoted 10 lines

include/asm-x86/page_64.h extern unsigned long max_pfn; · line 73

Quoted 8 lines
include/asm-x86/page_64.h +1 -0
@@ -69,6 +69,7 @@ extern unsigned long max_pfn;
 extern unsigned long phys_base;
 
 extern unsigned long __phys_addr(unsigned long);
+#define __phys_addr_nodebug(x) __phys_addr(x)
 #define __phys_reloc_hide(x) (x)

x86_64 is screwed in the same way, isn't it?

Hm. I didn't see any #ifdef CONFIG_DEBUG_VIRTUAL in the x86_64 code, so I assumed it wasn't. But it seems that you are right (because the checks, or at least some kind of checks, are _always_ performed on x86_64 regardless of the CONFIG_DEBUG_VIRTUAL setting). Why doesn't the checking in x86_64 code depend on DEBUG_VIRTUAL?

I will try to make another patch, thanks.

Vegard

Signature · 4 lines
"The animistic metaphor of the bug that maliciously sneaked in while
the programmer was not looking is intellectually dishonest as it
disguises that the error is the programmer's own creation."
	-- E. W. Dijkstra, EWD1036
#4 lore raw
Quoted 8 lines · Vegard Nossum
Quoted 1 line · Jiri Slaby

x86_64 is screwed in the same way, isn't it?

Hm. I didn't see any #ifdef CONFIG_DEBUG_VIRTUAL in the x86_64 code, so I assumed it wasn't. But it seems that you are right (because the checks, or at least some kind of checks, are _always_ performed on x86_64 regardless of the CONFIG_DEBUG_VIRTUAL setting). Why doesn't the checking in x86_64 code depend on DEBUG_VIRTUAL?

Yeah, it does: VIRTUAL_BUG_ON depends on it...

x86_64 just distinguish pointer to kernel image addresses (which are mapped only up to kernel image size from phys_base physical address) and whole physical memory map at another virtual address.

#5 lore raw
Quoted 10 lines · Jiri Slaby
Quoted 8 lines · Vegard Nossum
Quoted 1 line · Jiri Slaby

x86_64 is screwed in the same way, isn't it?

Hm. I didn't see any #ifdef CONFIG_DEBUG_VIRTUAL in the x86_64 code, so I assumed it wasn't. But it seems that you are right (because the checks, or at least some kind of checks, are _always_ performed on x86_64 regardless of the CONFIG_DEBUG_VIRTUAL setting). Why doesn't the checking in x86_64 code depend on DEBUG_VIRTUAL?

Quoted 5 lines

Yeah, it does: VIRTUAL_BUG_ON depends on it...

x86_64 just distinguish pointer to kernel image addresses (which are mapped only up to kernel image size from phys_base physical address) and whole physical memory map at another virtual address.

You are right.

But it seems that the current virt_addr_valid() doesn't take this into account. Should virt_addr_valid() be modified (on both x86_32 and x86_64) to take into account the same checks as __phys_addr() does when DEBUG_VIRTUAL=y? Or is it enough to use pfn_valid()?

Vegard

Signature · 4 lines
"The animistic metaphor of the bug that maliciously sneaked in while
the programmer was not looking is intellectually dishonest as it
disguises that the error is the programmer's own creation."
	-- E. W. Dijkstra, EWD1036
#6 lore raw
Quoted 4 lines

But it seems that the current virt_addr_valid() doesn't take this into account. Should virt_addr_valid() be modified (on both x86_32 and x86_64) to take into account the same checks as __phys_addr() does when DEBUG_VIRTUAL=y? Or is it enough to use pfn_valid()?

At least in Linus' tree virt_addr_valid() is just a wrapper around pfn_valid()

-Andi

#7 lore raw
Quoted 7 lines · Andi Kleen
Quoted 4 lines

But it seems that the current virt_addr_valid() doesn't take this into account. Should virt_addr_valid() be modified (on both x86_32 and x86_64) to take into account the same checks as __phys_addr() does when DEBUG_VIRTUAL=y? Or is it enough to use pfn_valid()?

At least in Linus' tree virt_addr_valid() is just a wrapper around pfn_valid()

Yes, but __pa() used for converting to a physical address used as a parameter for __pfn_valid() will panic on invalid addresses passed to it when DEBUG_VIRTUAL=y.

Anyway virt_addr_valid() is IMHO wrong. E.g. first modules VM address 0xffffffffa0000000 is after __pa() 200M which is valid pfn after the shift even on the flatmem model with enough memory.

Am I missing something? What's the exact purpose of the virt_addr_valid()?

#8 lore raw
Quoted 2 lines

Yes, but __pa() used for converting to a physical address used as a parameter for __pfn_valid() will panic on invalid addresses passed to it when DEBUG_VIRTUAL=y.

Ok that's a bug indeed.

Quoted 5 lines

Anyway virt_addr_valid() is IMHO wrong. E.g. first modules VM address 0xffffffffa0000000 is after __pa() 200M which is valid pfn after the shift even on the flatmem model with enough memory.

Am I missing something? What's the exact purpose of the virt_addr_valid()?

I think it's supposed to be only used on direct mapping anyways (judging from a quick look a the users)

So not handling text mapping is ok, but don't panic on it.

-Andi

Signature · 1 line
[email protected]
#9 lore raw
Quoted 8 lines · Andi Kleen
Quoted 5 lines

Anyway virt_addr_valid() is IMHO wrong. E.g. first modules VM address 0xffffffffa0000000 is after __pa() 200M which is valid pfn after the shift even on the flatmem model with enough memory.

Am I missing something? What's the exact purpose of the virt_addr_valid()?

I think it's supposed to be only used on direct mapping anyways (judging from a quick look a the users)

Then kmemcheck assumes something else. Citing:
* We need to be extremely careful not to follow any invalid pointers,
* because this function can be called for *any* possible address.
and the very first check is !virt_addr_valid(address).

Quoted 1 line

So not handling text mapping is ok, but don't panic on it.

It doesn't handle properly anything but text and direct mapping. Now it oopses/causes BUG on that wrong cases.

I think we should set it down there that it was intended to be used only on text/direct mapping and only for checking if there is a physical memory page behind this kind of virtual address.

#10 lore raw
Quoted 7 lines · Jiri Slaby
Quoted 6 lines · Andi Kleen
Quoted 5 lines

Anyway virt_addr_valid() is IMHO wrong. E.g. first modules VM address 0xffffffffa0000000 is after __pa() 200M which is valid pfn after the shift even on the flatmem model with enough memory.

Am I missing something? What's the exact purpose of the virt_addr_valid()?

Quoted 7 lines
Quoted 2 lines

I think it's supposed to be only used on direct mapping anyways (judging from a quick look a the users)

Then kmemcheck assumes something else. Citing:
* We need to be extremely careful not to follow any invalid pointers,
* because this function can be called for *any* possible address.
and the very first check is !virt_addr_valid(address).

The purpose of this call was to make sure that the page behind the virtual address has an associated struct page. That is the assumption:
virt_to_page() will return something meaningful if and only if virt_addr_valid().

Quoted 9 lines
Quoted 2 lines

So not handling text mapping is ok, but don't panic on it.

It doesn't handle properly anything but text and direct mapping. Now it oopses/causes BUG on that wrong cases.

I think we should set it down there that it was intended to be used only on text/direct mapping and only for checking if there is a physical memory page behind this kind of virtual address.

Vegard

Signature · 4 lines
"The animistic metaphor of the bug that maliciously sneaked in while
the programmer was not looking is intellectually dishonest as it
disguises that the error is the programmer's own creation."
	-- E. W. Dijkstra, EWD1036