Skip to contentSkip to conversation

mm: Add debug_virt_to_phys()

RFC PATCH In review Florian Fainelli 4 participants 8 messages Nov 12, 2016 – Nov 14, 2016

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

RFC PATCH 1/1 mm: Add debug_virt_to_phys()

When CONFIG_DEBUG_VM is turned on, virt_to_phys() maps to debug_virt_to_phys() which helps catch vmalloc space addresses being passed. This is helpful in debugging bogus drivers that just assume linear mappings all over the place.

For ARM, ARM64, Unicore32 and Microblaze, the architectures define __virt_to_phys() as being the functional implementation of the address translation, so we special case the debug stub to call into __virt_to_phys directly.

---
 arch/arm/include/asm/memory.h      |  4 ++++
 arch/arm64/include/asm/memory.h    |  4 ++++
 include/asm-generic/memory_model.h |  4 ++++
 mm/debug.c                         | 15 +++++++++++++++
 4 files changed, 27 insertions(+)
arch/arm/include/asm/memory.h +4 -0
index 76cbd9c674df..448dec9b8b00 100644
@@ -260,11 +260,15 @@ static inline unsigned long __phys_to_virt(phys_addr_t x)
  * translation for translating DMA addresses.  Use the driver
  * DMA support - see dma-mapping.h.
  */
+#ifndef CONFIG_DEBUG_VM
 #define virt_to_phys virt_to_phys
 static inline phys_addr_t virt_to_phys(const volatile void *x)
 {
 	return __virt_to_phys((unsigned long)(x));
 }
+#else
+#define virt_to_phys debug_virt_to_phys
+#endif
 
 #define phys_to_virt phys_to_virt
 static inline void *phys_to_virt(phys_addr_t x)
arch/arm64/include/asm/memory.h +4 -0
index b71086d25195..c9e436b28523 100644
@@ -186,11 +186,15 @@ extern u64			kimage_voffset;
  * translation for translating DMA addresses.  Use the driver
  * DMA support - see dma-mapping.h.
  */
+#ifndef CONFIG_DEBUG_VM
 #define virt_to_phys virt_to_phys
 static inline phys_addr_t virt_to_phys(const volatile void *x)
 {
 	return __virt_to_phys((unsigned long)(x));
 }
+#else
+#define virt_to_phys debug_virt_to_phys
+#endif
 
 #define phys_to_virt phys_to_virt
 static inline void *phys_to_virt(phys_addr_t x)
include/asm-generic/memory_model.h +4 -0
index 5148150cc80b..426085757258 100644
@@ -80,6 +80,10 @@
 #define page_to_pfn __page_to_pfn
 #define pfn_to_page __pfn_to_page
 
+#ifdef CONFIG_DEBUG_VM
+unsigned long debug_virt_to_phys(volatile void *address);
+#endif /* CONFIG_DEBUG_VM */
+
 #endif /* __ASSEMBLY__ */
 
 #endif
mm/debug.c +15 -0
index 9feb699c5d25..72b2ca9b11f4 100644
@@ -161,4 +161,19 @@ void dump_mm(const struct mm_struct *mm)
 	);
 }
 
+#include <asm/memory.h>
+#include <linux/mm.h>
+
+unsigned long debug_virt_to_phys(volatile void *address)
+{
+	BUG_ON(is_vmalloc_addr((const void *)address));
+#if defined(CONFIG_ARM) || defined(CONFIG_ARM64) || defined(CONFIG_UNICORE32) || \
+	defined(CONFIG_MICROBLAZE)
+	return __virt_to_phys(address);
+#else
+	return virt_to_phys(address);
+#endif
+}
+EXPORT_SYMBOL(debug_virt_to_phys);
+
 #endif		/* CONFIG_DEBUG_VM */
Signature · 7 lines
2.9.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 25 lines · Florian Fainelli

When CONFIG_DEBUG_VM is turned on, virt_to_phys() maps to debug_virt_to_phys() which helps catch vmalloc space addresses being passed. This is helpful in debugging bogus drivers that just assume linear mappings all over the place.

For ARM, ARM64, Unicore32 and Microblaze, the architectures define __virt_to_phys() as being the functional implementation of the address translation, so we special case the debug stub to call into __virt_to_phys directly.

---
 arch/arm/include/asm/memory.h      |  4 ++++
 arch/arm64/include/asm/memory.h    |  4 ++++
 include/asm-generic/memory_model.h |  4 ++++
 mm/debug.c                         | 15 +++++++++++++++
 4 files changed, 27 insertions(+)
arch/arm/include/asm/memory.h +0 -0
index 76cbd9c674df..448dec9b8b00 100644
@@ -260,11 +260,15 @@ static inline unsigned long __phys_to_virt(phys_addr_t x)
  * translation for translating DMA addresses.  Use the driver
  * DMA support - see dma-mapping.h.

arch/arm/include/asm/memory.h static inline unsigned long __phys_to_virt(phys_addr_t x) · line 271

Quoted 10 lines
arch/arm/include/asm/memory.h +4 -0
  */
+#ifndef CONFIG_DEBUG_VM
 #define virt_to_phys virt_to_phys
 static inline phys_addr_t virt_to_phys(const volatile void *x)
 {
 	return __virt_to_phys((unsigned long)(x));
 }
+#else
+#define virt_to_phys debug_virt_to_phys
+#endif

[...]

Why don't you do something more like:

static inline phys_addr_t virt_to_phys(const volatile void *x) {

+        debug_virt_to_phys(x);
         return __virt_to_phys((unsigned long)(x));

}

[...]

static inline void debug_virt_to_phys(const void *address) {

#ifdef CONFIG_DEBUG_VM
        BUG_ON(is_vmalloc_addr(address));

#endif
}

?

Nicolas

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
Quoted 49 lines · Nicolas Pitre
Quoted 25 lines · Florian Fainelli

When CONFIG_DEBUG_VM is turned on, virt_to_phys() maps to debug_virt_to_phys() which helps catch vmalloc space addresses being passed. This is helpful in debugging bogus drivers that just assume linear mappings all over the place.

For ARM, ARM64, Unicore32 and Microblaze, the architectures define __virt_to_phys() as being the functional implementation of the address translation, so we special case the debug stub to call into __virt_to_phys directly.

---
 arch/arm/include/asm/memory.h      |  4 ++++
 arch/arm64/include/asm/memory.h    |  4 ++++
 include/asm-generic/memory_model.h |  4 ++++
 mm/debug.c                         | 15 +++++++++++++++
 4 files changed, 27 insertions(+)
arch/arm/include/asm/memory.h +0 -0
index 76cbd9c674df..448dec9b8b00 100644
@@ -260,11 +260,15 @@ static inline unsigned long __phys_to_virt(phys_addr_t x)
  * translation for translating DMA addresses.  Use the driver
  * DMA support - see dma-mapping.h.

arch/arm/include/asm/memory.h static inline unsigned long __phys_to_virt(phys_addr_t x) · line 271

Quoted 10 lines
arch/arm/include/asm/memory.h +4 -0
  */
+#ifndef CONFIG_DEBUG_VM
 #define virt_to_phys virt_to_phys
 static inline phys_addr_t virt_to_phys(const volatile void *x)
 {
 	return __virt_to_phys((unsigned long)(x));
 }
+#else
+#define virt_to_phys debug_virt_to_phys
+#endif

[...]

Why don't you do something more like:

static inline phys_addr_t virt_to_phys(const volatile void *x) {

+        debug_virt_to_phys(x);
         return __virt_to_phys((unsigned long)(x));

}

[...]

Quoted 8 lines

static inline void debug_virt_to_phys(const void *address) {

#ifdef CONFIG_DEBUG_VM
        BUG_ON(is_vmalloc_addr(address));

#endif
}

?

This is how I started doing it initially, but to get the is_vmalloc_addr() definition, we need to include linux/mm.h and then everything falls apart because of the include and dependencies chain. We could open code the is_vmalloc_addr() check because that's simple enough, but we still need VMALLOC_START and VMALLOC_END and to get there we need to include pgtable.h, and there are still some inclusion problems in doing so.

The other reason was to avoid putting the same checks in architecture specific code, except for those like ARM/ARM64/Unicore32/Microblaze where I could not find a simple way to undefined virt_to_phys and redefine it to debug_virt_to_phys.

Do you see an other way around this? Thanks!

Signature · 1 line
Florian
#4 lore raw
In reply to Florian Fainelli
Quoted 10 lines · Florian Fainelli

When CONFIG_DEBUG_VM is turned on, virt_to_phys() maps to debug_virt_to_phys() which helps catch vmalloc space addresses being passed. This is helpful in debugging bogus drivers that just assume linear mappings all over the place.

For ARM, ARM64, Unicore32 and Microblaze, the architectures define __virt_to_phys() as being the functional implementation of the address translation, so we special case the debug stub to call into __virt_to_phys directly.

Quoted 7 lines
---
 arch/arm/include/asm/memory.h      |  4 ++++
 arch/arm64/include/asm/memory.h    |  4 ++++
 include/asm-generic/memory_model.h |  4 ++++
 mm/debug.c                         | 15 +++++++++++++++
 4 files changed, 27 insertions(+)

What's the interaction between this and the DEBUG_VIRTUAL patches from Laura?

http://lkml.kernel.org/r/[email protected]

They seem to be tackling the exact same problem afaict.

Will

#3 lore raw
In reply to Florian Fainelli
Quoted 67 lines · Florian Fainelli
Quoted 49 lines · Nicolas Pitre
Quoted 25 lines · Florian Fainelli

When CONFIG_DEBUG_VM is turned on, virt_to_phys() maps to debug_virt_to_phys() which helps catch vmalloc space addresses being passed. This is helpful in debugging bogus drivers that just assume linear mappings all over the place.

For ARM, ARM64, Unicore32 and Microblaze, the architectures define __virt_to_phys() as being the functional implementation of the address translation, so we special case the debug stub to call into __virt_to_phys directly.

---
 arch/arm/include/asm/memory.h      |  4 ++++
 arch/arm64/include/asm/memory.h    |  4 ++++
 include/asm-generic/memory_model.h |  4 ++++
 mm/debug.c                         | 15 +++++++++++++++
 4 files changed, 27 insertions(+)
arch/arm/include/asm/memory.h +0 -0
index 76cbd9c674df..448dec9b8b00 100644
@@ -260,11 +260,15 @@ static inline unsigned long __phys_to_virt(phys_addr_t x)
  * translation for translating DMA addresses.  Use the driver
  * DMA support - see dma-mapping.h.

arch/arm/include/asm/memory.h static inline unsigned long __phys_to_virt(phys_addr_t x) · line 271

Quoted 10 lines
arch/arm/include/asm/memory.h +4 -0
  */
+#ifndef CONFIG_DEBUG_VM
 #define virt_to_phys virt_to_phys
 static inline phys_addr_t virt_to_phys(const volatile void *x)
 {
 	return __virt_to_phys((unsigned long)(x));
 }
+#else
+#define virt_to_phys debug_virt_to_phys
+#endif

[...]

Why don't you do something more like:

static inline phys_addr_t virt_to_phys(const volatile void *x) {

+        debug_virt_to_phys(x);
         return __virt_to_phys((unsigned long)(x));

}

[...]

Quoted 8 lines

static inline void debug_virt_to_phys(const void *address) {

#ifdef CONFIG_DEBUG_VM
        BUG_ON(is_vmalloc_addr(address));

#endif
}

?

This is how I started doing it initially, but to get the is_vmalloc_addr() definition, we need to include linux/mm.h and then everything falls apart because of the include and dependencies chain. We could open code the is_vmalloc_addr() check because that's simple enough, but we still need VMALLOC_START and VMALLOC_END and to get there we need to include pgtable.h, and there are still some inclusion problems in doing so.

Quoted 4 lines

The other reason was to avoid putting the same checks in architecture specific code, except for those like ARM/ARM64/Unicore32/Microblaze where I could not find a simple way to undefined virt_to_phys and redefine it to debug_virt_to_phys.

You could still move the check out of line like in your patch. But the debug function doesn't have to be the one returning the translated address. This has the advantage of cutting on the amount of ifdefery.

Nicolas

#5 lore raw
In reply to Will Deacon

Le 11/11/2016 ? 21:43, Will Deacon a ?crit :

Quoted 19 lines · Florian Fainelli
Quoted 10 lines

When CONFIG_DEBUG_VM is turned on, virt_to_phys() maps to debug_virt_to_phys() which helps catch vmalloc space addresses being passed. This is helpful in debugging bogus drivers that just assume linear mappings all over the place.

For ARM, ARM64, Unicore32 and Microblaze, the architectures define __virt_to_phys() as being the functional implementation of the address translation, so we special case the debug stub to call into __virt_to_phys directly.

Quoted 7 lines
---
 arch/arm/include/asm/memory.h      |  4 ++++
 arch/arm64/include/asm/memory.h    |  4 ++++
 include/asm-generic/memory_model.h |  4 ++++
 mm/debug.c                         | 15 +++++++++++++++
 4 files changed, 27 insertions(+)
Quoted 5 lines

What's the interaction between this and the DEBUG_VIRTUAL patches from Laura?

http://lkml.kernel.org/r/20161102210054.16621-7-labbott at redhat.com

They seem to be tackling the exact same problem afaict.

Indeed thanks for pointing that out, I guess I could piggy back on this patchset and try to cover ARM.

Thanks!

Signature · 1 line
Florian
#6 lore raw
In reply to Florian Fainelli
Quoted 88 lines · Florian Fainelli

When CONFIG_DEBUG_VM is turned on, virt_to_phys() maps to debug_virt_to_phys() which helps catch vmalloc space addresses being passed. This is helpful in debugging bogus drivers that just assume linear mappings all over the place.

For ARM, ARM64, Unicore32 and Microblaze, the architectures define __virt_to_phys() as being the functional implementation of the address translation, so we special case the debug stub to call into __virt_to_phys directly.

---
 arch/arm/include/asm/memory.h      |  4 ++++
 arch/arm64/include/asm/memory.h    |  4 ++++
 include/asm-generic/memory_model.h |  4 ++++
 mm/debug.c                         | 15 +++++++++++++++
 4 files changed, 27 insertions(+)
arch/arm/include/asm/memory.h +4 -0
index 76cbd9c674df..448dec9b8b00 100644
@@ -260,11 +260,15 @@ static inline unsigned long __phys_to_virt(phys_addr_t x)
  * translation for translating DMA addresses.  Use the driver
  * DMA support - see dma-mapping.h.
  */
+#ifndef CONFIG_DEBUG_VM
 #define virt_to_phys virt_to_phys
 static inline phys_addr_t virt_to_phys(const volatile void *x)
 {
 	return __virt_to_phys((unsigned long)(x));
 }
+#else
+#define virt_to_phys debug_virt_to_phys
+#endif
 
 #define phys_to_virt phys_to_virt
 static inline void *phys_to_virt(phys_addr_t x)
arch/arm64/include/asm/memory.h +4 -0
index b71086d25195..c9e436b28523 100644
@@ -186,11 +186,15 @@ extern u64			kimage_voffset;
  * translation for translating DMA addresses.  Use the driver
  * DMA support - see dma-mapping.h.
  */
+#ifndef CONFIG_DEBUG_VM
 #define virt_to_phys virt_to_phys
 static inline phys_addr_t virt_to_phys(const volatile void *x)
 {
 	return __virt_to_phys((unsigned long)(x));
 }
+#else
+#define virt_to_phys debug_virt_to_phys
+#endif
 
 #define phys_to_virt phys_to_virt
 static inline void *phys_to_virt(phys_addr_t x)
include/asm-generic/memory_model.h +4 -0
index 5148150cc80b..426085757258 100644
@@ -80,6 +80,10 @@
 #define page_to_pfn __page_to_pfn
 #define pfn_to_page __pfn_to_page
 
+#ifdef CONFIG_DEBUG_VM
+unsigned long debug_virt_to_phys(volatile void *address);
+#endif /* CONFIG_DEBUG_VM */
+
 #endif /* __ASSEMBLY__ */
 
 #endif
mm/debug.c +6 -0
index 9feb699c5d25..72b2ca9b11f4 100644
@@ -161,4 +161,19 @@ void dump_mm(const struct mm_struct *mm)
 	);
 }
 
+#include <asm/memory.h>
+#include <linux/mm.h>
+
+unsigned long debug_virt_to_phys(volatile void *address)
+{
+	BUG_ON(is_vmalloc_addr((const void *)address));

mm/debug.c void dump_mm(const struct mm_struct *mm) · line 179

Quoted 10 lines
mm/debug.c +9 -0
+#if defined(CONFIG_ARM) || defined(CONFIG_ARM64) || defined(CONFIG_UNICORE32) || \
+	defined(CONFIG_MICROBLAZE)
+	return __virt_to_phys(address);
+#else
+	return virt_to_phys(address);
+#endif
+}
+EXPORT_SYMBOL(debug_virt_to_phys);
+
 #endif		/* CONFIG_DEBUG_VM */

is_vmalloc_addr is necessary but not sufficient. This misses cases like module addresses. The x86 version (CONFIG_DEBUG_VIRTUAL) bounds checks against the known linear map to catch all cases. I'm for a generic approach to this if it can catch all cases that an architecture specific version would catch.

Thanks,
Laura

#7 lore raw
Quoted 94 lines · Laura Abbott
Quoted 93 lines · Florian Fainelli

When CONFIG_DEBUG_VM is turned on, virt_to_phys() maps to debug_virt_to_phys() which helps catch vmalloc space addresses being passed. This is helpful in debugging bogus drivers that just assume linear mappings all over the place.

For ARM, ARM64, Unicore32 and Microblaze, the architectures define __virt_to_phys() as being the functional implementation of the address translation, so we special case the debug stub to call into __virt_to_phys directly.

---
 arch/arm/include/asm/memory.h      |  4 ++++
 arch/arm64/include/asm/memory.h    |  4 ++++
 include/asm-generic/memory_model.h |  4 ++++
 mm/debug.c                         | 15 +++++++++++++++
 4 files changed, 27 insertions(+)
arch/arm/include/asm/memory.h +4 -0
index 76cbd9c674df..448dec9b8b00 100644
@@ -260,11 +260,15 @@ static inline unsigned long __phys_to_virt(phys_addr_t x)
  * translation for translating DMA addresses.  Use the driver
  * DMA support - see dma-mapping.h.
  */
+#ifndef CONFIG_DEBUG_VM
 #define virt_to_phys virt_to_phys
 static inline phys_addr_t virt_to_phys(const volatile void *x)
 {
 	return __virt_to_phys((unsigned long)(x));
 }
+#else
+#define virt_to_phys debug_virt_to_phys
+#endif
 
 #define phys_to_virt phys_to_virt
 static inline void *phys_to_virt(phys_addr_t x)
arch/arm64/include/asm/memory.h +4 -0
index b71086d25195..c9e436b28523 100644
@@ -186,11 +186,15 @@ extern u64			kimage_voffset;
  * translation for translating DMA addresses.  Use the driver
  * DMA support - see dma-mapping.h.
  */
+#ifndef CONFIG_DEBUG_VM
 #define virt_to_phys virt_to_phys
 static inline phys_addr_t virt_to_phys(const volatile void *x)
 {
 	return __virt_to_phys((unsigned long)(x));
 }
+#else
+#define virt_to_phys debug_virt_to_phys
+#endif
 
 #define phys_to_virt phys_to_virt
 static inline void *phys_to_virt(phys_addr_t x)
include/asm-generic/memory_model.h +4 -0
index 5148150cc80b..426085757258 100644
@@ -80,6 +80,10 @@
 #define page_to_pfn __page_to_pfn
 #define pfn_to_page __pfn_to_page
 
+#ifdef CONFIG_DEBUG_VM
+unsigned long debug_virt_to_phys(volatile void *address);
+#endif /* CONFIG_DEBUG_VM */
+
 #endif /* __ASSEMBLY__ */
 
 #endif
mm/debug.c +12 -0
index 9feb699c5d25..72b2ca9b11f4 100644
@@ -161,4 +161,19 @@ void dump_mm(const struct mm_struct *mm)
 	);
 }
 
+#include <asm/memory.h>
+#include <linux/mm.h>
+
+unsigned long debug_virt_to_phys(volatile void *address)
+{
+	BUG_ON(is_vmalloc_addr((const void *)address));
+#if defined(CONFIG_ARM) || defined(CONFIG_ARM64) || defined(CONFIG_UNICORE32) || \
+	defined(CONFIG_MICROBLAZE)
+	return __virt_to_phys(address);
+#else
+	return virt_to_phys(address);
+#endif
Quoted 8 lines

mm/debug.c void dump_mm(const struct mm_struct *mm) · line 179

Quoted 5 lines
mm/debug.c +3 -0
+}
+EXPORT_SYMBOL(debug_virt_to_phys);
+
 #endif		/* CONFIG_DEBUG_VM */

is_vmalloc_addr is necessary but not sufficient. This misses cases like module addresses.

Indeed, thanks.

Quoted 4 lines

The x86 version (CONFIG_DEBUG_VIRTUAL)
bounds checks against the known linear map to catch all cases. I'm for a generic approach to this if it can catch all cases that an architecture specific version would catch.

For one, my patch causes an early BUG to occur on ARM64 during arch/arm64/kernel/setup.c::setup_arch when we call cpu_uninstall_idmap(). I suspect there could be a bunch of little checks like these where we'd have to have an architecture specific "is this physical/virtual address valid" that may make a generic implementation hard to come up with.

Signature · 7 lines
Florian

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>