arm64: Add support for CONFIG_DEBUG_VIRTUAL
Subsystems (4)
RFC PATCH 1/1 arm64: Add support for CONFIG_DEBUG_VIRTUAL
x86 has an option CONFIG_DEBUG_VIRTUAL to do additional checks on virt_to_phys calls. The goal is to catch users who are calling virt_to_phys on non-linear addresses immediately. As features such as CONFIG_VMAP_STACK get enabled for arm64, this becomes increasingly important. Add checks to catch bad virt_to_phys usage.
---
This has been on my TODO list for a while. It caught a few bugs with CONFIG_VMAP_STACK on x86 so when that eventually gets added
for arm64 it will be useful to have. This caught one driver calling __pa on an ioremapped address already. RFC for a couple of reasons:
1) This is basically a direct port of the x86 approach.
2) I needed some #ifndef __ASSEMBLY__ which I don't like to throw around.
3) I'm not quite sure about the bounds check for the VIRTUAL_BUG_ON with KASLR, specifically the _end check.
4) Is it worth actually keeping this as DEBUG_VIRTUAL vs. folding it into another option?
--- arch/arm64/include/asm/memory.h | 11 ++++++++++- arch/arm64/mm/Makefile | 2 +- arch/arm64/mm/physaddr.c | 17 +++++++++++++++++ lib/Kconfig.debug | 2 +- mm/cma.c | 2 +- 5 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 arch/arm64/mm/physaddr.c
arch/arm64/include/asm/memory.h +10 -1
@@ -106,11 +106,19 @@* private definitions which should NOT be used outside memory.h * files. Use virt_to_phys/phys_to_virt/__pa/__va instead. */ -#define __virt_to_phys (x) ({ \ +#define __virt_to_phys_nodebug (x) ({ \ phys_addr_t __x = (phys_addr_t)(x); \ __x & BIT(VA_BITS - 1) ? (__x & ~PAGE_OFFSET) + PHYS_OFFSET : \ (__x - kimage_voffset); }) +#ifdef CONFIG_DEBUG_VIRTUAL +#ifndef __ASSEMBLY__ +extern unsigned long __virt_to_phys (unsigned long x); +#endif +#else +#define __virt_to_phys (x) __virt_to_phys_nodebug(x) +#endif +#define __phys_to_virt (x) ((unsigned long)((x) - PHYS_OFFSET) | PAGE_OFFSET)#define __phys_to_kimg (x) ((unsigned long)((x) + kimage_voffset))
@@ -202,6 +210,7 @@ static inline void *phys_to_virt(phys_addr_t x)* Drivers should NOT use these either. */ #define __pa (x) __virt_to_phys((unsigned long)(x)) +#define __pa_nodebug (x) __virt_to_phys_nodebug((unsigned long)(x))#define __va (x) ((void *)__phys_to_virt((phys_addr_t)(x)))#define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT)#define virt_to_pfn (x) __phys_to_pfn(__virt_to_phys(x))
arch/arm64/mm/Makefile +1 -1
@@ -5,6 +5,6 @@ obj-y := dma-mapping.o extable.o fault.o init.o \ obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o obj-$(CONFIG_ARM64_PTDUMP) += dump.o obj-$(CONFIG_NUMA) += numa.o - +obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o obj-$(CONFIG_KASAN) += kasan_init.o KASAN_SANITIZE_kasan_init.o := n
arch/arm64/mm/physaddr.c new file +17 -0
@@ -0,0 +1,17 @@ +#include <linux/mm.h> + +#include <asm/memory.h> + +unsigned long __virt_to_phys (unsigned long x) +{ +phys_addr_t __x = (phys_addr_t )x; + +if (__x &BIT (VA_BITS -1 )) { +/* The bit check ensures this is the right range */ +return (__x & ~PAGE_OFFSET ) +PHYS_OFFSET ; + }else { +VIRTUAL_BUG_ON (x < kimage_vaddr || x > (unsigned long )_end); +return (__x - kimage_voffset); + } +} +EXPORT_SYMBOL (__virt_to_phys);
lib/Kconfig.debug +1 -1
@@ -604,7 +604,7 @@ config DEBUG_VM_PGFLAGS config DEBUG_VIRTUAL bool "Debug VM translations" - depends on DEBUG_KERNEL && X86 + depends on DEBUG_KERNEL && (X86 || ARM64) help Enable some costly sanity checks in virtual to page code. This can catch mistakes with virt_to_page() and friends.
mm/cma.c +1 -1
@@ -235,7 +235,7 @@ int __init cma_declare_contiguous(phys_addr_t base,phys_addr_t highmem_start;int ret =0 ; -#ifdef CONFIG_X86 +#if defined(CONFIG_X86 ) || defined(CONFIG_ARM64 )/* * high_memory isn't direct mapped memory so retrieving its physical * address isn't appropriate. But it would be useful to check the
Signature · 1 line
2.7.4
Hi Laura,
Quoted 7 lines · Laura Abbott
x86 has an option
CONFIG_DEBUG_VIRTUALto do additional checks onvirt_to_physcalls. The goal is to catch users who are callingvirt_to_physon non-linear addresses immediately. As features such asCONFIG_VMAP_STACKget enabled for arm64, this becomes increasingly important. Add checks to catch badvirt_to_physusage.
I think this is a useful thing to have. However, the Kconfig description talks about virt to page translations, not virt to phys. Of course, this is a shift away from being equivalent on x86, but not so much on arm64. Any concerns there?
Quoted 35 lines
Signed-off-by Laura Abbott <[email protected]>---
This has been on my TODO list for a while. It caught a few bugs withCONFIG_VMAP_STACKon x86 so when that eventually gets added
for arm64 it will be useful to have. This caught one driver calling__paon an ioremapped address already. RFC for a couple of reasons:1) This is basically a direct port of the x86 approach.
2) I needed some #ifndef__ASSEMBLY__which I don't like to throw around.
3) I'm not quite sure about the bounds check for theVIRTUAL_BUG_ONwith KASLR, specifically the_endcheck.
4) Is it worth actually keeping this asDEBUG_VIRTUALvs. folding it into another option?--- arch/arm64/include/asm/memory.h | 11 ++++++++++- arch/arm64/mm/Makefile | 2 +- arch/arm64/mm/physaddr.c | 17 +++++++++++++++++ lib/Kconfig.debug | 2 +- mm/cma.c | 2 +- 5 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 arch/arm64/mm/physaddr.carch/arm64/include/asm/memory.h +1
-1@@ -106,11 +106,19 @@* private definitions which should NOT be used outside memory.h * files. Use virt_to_phys/phys_to_virt/__pa/__va instead. */ -#define __virt_to_phys (x) ({ \ +#define __virt_to_phys_nodebug (x) ({ \ phys_addr_t __x = (phys_addr_t)(x); \ __x & BIT(VA_BITS - 1) ? (__x & ~PAGE_OFFSET) + PHYS_OFFSET : \
arch/arm64/include/asm/memory.h line 121
Quoted 10 lines
arch/arm64/include/asm/memory.h +8
-0(__x - kimage_voffset); }) +#ifdef CONFIG_DEBUG_VIRTUAL +#ifndef __ASSEMBLY__ +extern unsigned long __virt_to_phys (unsigned long x); +#endif +#else +#define __virt_to_phys (x) __virt_to_phys_nodebug(x) +#endif +
Couldn't you move this whole block into the #ifndef __ASSEMBLY__ section lower down in the file?
Quoted 29 lines
arch/arm64/include/asm/memory.h +1
-0#define __phys_to_virt (x) ((unsigned long)((x) - PHYS_OFFSET) | PAGE_OFFSET)#define __phys_to_kimg (x) ((unsigned long)((x) + kimage_voffset))@@ -202,6 +210,7 @@ static inline void *phys_to_virt(phys_addr_t x)* Drivers should NOT use these either. */ #define __pa (x) __virt_to_phys((unsigned long)(x)) +#define __pa_nodebug (x) __virt_to_phys_nodebug((unsigned long)(x))#define __va (x) ((void *)__phys_to_virt((phys_addr_t)(x)))#define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT)#define virt_to_pfn (x) __phys_to_pfn(__virt_to_phys(x))arch/arm64/mm/Makefile +1
-1@@ -5,6 +5,6 @@ obj-y := dma-mapping.o extable.o fault.o init.o \ obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o obj-$(CONFIG_ARM64_PTDUMP) += dump.o obj-$(CONFIG_NUMA) += numa.o - +obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o obj-$(CONFIG_KASAN) += kasan_init.o KASAN_SANITIZE_kasan_init.o := narch/arm64/mm/physaddr.c new file +0
-0
arch/arm64/mm/physaddr.c line 10
Quoted 10 lines
arch/arm64/mm/physaddr.c new file +10
-0@@ -0,0 +1,17 @@ +#include <linux/mm.h> + +#include <asm/memory.h> + +unsigned long __virt_to_phys (unsigned long x) +{ +phys_addr_t __x = (phys_addr_t )x; + +if (__x &BIT (VA_BITS -1 )) { +/* The bit check ensures this is the right range */
Could you expand the comment to clarify that the linear region starts right in the middle of the kernel virtual address space, and so we only have to test the top bit?
arch/arm64/mm/physaddr.c line 13
Quoted 3 lines
arch/arm64/mm/physaddr.c new file +3
-0+return (__x & ~PAGE_OFFSET ) +PHYS_OFFSET ; + }else {l + VIRTUAL_BUG_ON(x < kimage_vaddr || x > (unsigned long)_end);
This looks fine. References to _end are relocated at boot to refer to the actual runtime offset.
Quoted 33 lines
arch/arm64/mm/physaddr.c new file +4
-0+return (__x - kimage_voffset); + } +} +EXPORT_SYMBOL (__virt_to_phys);lib/Kconfig.debug +1
-1@@ -604,7 +604,7 @@ config DEBUG_VM_PGFLAGS config DEBUG_VIRTUAL bool "Debug VM translations" - depends on DEBUG_KERNEL && X86 + depends on DEBUG_KERNEL && (X86 || ARM64) help Enable some costly sanity checks in virtual to page code. This can catch mistakes with virt_to_page() and friends.mm/cma.c +1
-2@@ -235,7 +235,7 @@ int __init cma_declare_contiguous(phys_addr_t base,phys_addr_t highmem_start;int ret =0 ; -#ifdef CONFIG_X86 +#if defined(CONFIG_X86 ) || defined(CONFIG_ARM64 )/* * high_memory isn't direct mapped memory so retrieving its physical * address isn't appropriate. But it would be useful to check the -- 2.7.4
Mark Rutland
reviewerQuoted 10 lines · Ard Biesheuvel
Hi Laura,
Quoted 6 lines · Laura Abbott
x86 has an option
CONFIG_DEBUG_VIRTUALto do additional checks onvirt_to_physcalls. The goal is to catch users who are callingvirt_to_physon non-linear addresses immediately. As features such asCONFIG_VMAP_STACKget enabled for arm64, this becomes increasingly important. Add checks to catch badvirt_to_physusage.
Quoted 4 lines
I think this is a useful thing to have. However, the
Kconfigdescription talks about virt to page translations, not virt to phys. Of course, this is a shift away from being equivalent on x86, but not so much on arm64. Any concerns there?
See commit 59ea746337c69f6a ("MM: virtual address debug"); the existing x86 cases cover virt to phys also.
The Kconfig text does say "and friends"...
Thanks,
Mark.
Quoted 11 lines · Ard Biesheuvel
Hi Laura,
Quoted 7 lines · Laura Abbott
x86 has an option
CONFIG_DEBUG_VIRTUALto do additional checks onvirt_to_physcalls. The goal is to catch users who are callingvirt_to_physon non-linear addresses immediately. As features such asCONFIG_VMAP_STACKget enabled for arm64, this becomes increasingly important. Add checks to catch badvirt_to_physusage.
Quoted 5 lines
I think this is a useful thing to have. However, the
Kconfigdescription talks about virt to page translations, not virt to phys. Of course, this is a shift away from being equivalent on x86, but not so much on arm64. Any concerns there?
I think the description is vague. I'm guessing there were problems with virt_to_page but the actual check ends up going in virt_to_phys because that's where the actual problem occurs.
Quoted 37 lines
Quoted 37 lines
Signed-off-by Laura Abbott <[email protected]>---
This has been on my TODO list for a while. It caught a few bugs withCONFIG_VMAP_STACKon x86 so when that eventually gets added
for arm64 it will be useful to have. This caught one driver calling__paon an ioremapped address already. RFC for a couple of reasons:1) This is basically a direct port of the x86 approach.
2) I needed some #ifndef__ASSEMBLY__which I don't like to throw around.
3) I'm not quite sure about the bounds check for theVIRTUAL_BUG_ONwith KASLR, specifically the_endcheck.
4) Is it worth actually keeping this asDEBUG_VIRTUALvs. folding it into another option?--- arch/arm64/include/asm/memory.h | 11 ++++++++++- arch/arm64/mm/Makefile | 2 +- arch/arm64/mm/physaddr.c | 17 +++++++++++++++++ lib/Kconfig.debug | 2 +- mm/cma.c | 2 +- 5 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 arch/arm64/mm/physaddr.carch/arm64/include/asm/memory.h +1
-1@@ -106,11 +106,19 @@* private definitions which should NOT be used outside memory.h * files. Use virt_to_phys/phys_to_virt/__pa/__va instead. */ -#define __virt_to_phys (x) ({ \ +#define __virt_to_phys_nodebug (x) ({ \ phys_addr_t __x = (phys_addr_t)(x); \ __x & BIT(VA_BITS - 1) ? (__x & ~PAGE_OFFSET) + PHYS_OFFSET : \ (__x - kimage_voffset); })
Quoted 12 lines
arch/arm64/include/asm/memory.h line 121
Quoted 8 lines
arch/arm64/include/asm/memory.h +8
-0+#ifdef CONFIG_DEBUG_VIRTUAL +#ifndef __ASSEMBLY__ +extern unsigned long __virt_to_phys (unsigned long x); +#endif +#else +#define __virt_to_phys (x) __virt_to_phys_nodebug(x) +#endif +Couldn't you move this whole block into the #ifndef
__ASSEMBLY__section lower down in the file?
Yes, I think that makes more sense.
Quoted 35 lines
Quoted 35 lines
arch/arm64/include/asm/memory.h +1
-0#define __phys_to_virt (x) ((unsigned long)((x) - PHYS_OFFSET) | PAGE_OFFSET)#define __phys_to_kimg (x) ((unsigned long)((x) + kimage_voffset))@@ -202,6 +210,7 @@ static inline void *phys_to_virt(phys_addr_t x)* Drivers should NOT use these either. */ #define __pa (x) __virt_to_phys((unsigned long)(x)) +#define __pa_nodebug (x) __virt_to_phys_nodebug((unsigned long)(x))#define __va (x) ((void *)__phys_to_virt((phys_addr_t)(x)))#define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT)#define virt_to_pfn (x) __phys_to_pfn(__virt_to_phys(x))arch/arm64/mm/Makefile +1
-1@@ -5,6 +5,6 @@ obj-y := dma-mapping.o extable.o fault.o init.o \ obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o obj-$(CONFIG_ARM64_PTDUMP) += dump.o obj-$(CONFIG_NUMA) += numa.o - +obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o obj-$(CONFIG_KASAN) += kasan_init.o KASAN_SANITIZE_kasan_init.o := narch/arm64/mm/physaddr.c new file +6
-0@@ -0,0 +1,17 @@ +#include <linux/mm.h> + +#include <asm/memory.h> + +unsigned long __virt_to_phys (unsigned long x) +{
Quoted 9 lines
arch/arm64/mm/physaddr.c line 10
Quoted 4 lines
arch/arm64/mm/physaddr.c new file +4
-0+phys_addr_t __x = (phys_addr_t )x; + +if (__x &BIT (VA_BITS -1 )) { +/* The bit check ensures this is the right range */ Could you expand the comment to clarify that the linear region starts right in the middle of the kernel virtual address space, and so we only have to test the top bit?
Sure.
Quoted 7 lines
arch/arm64/mm/physaddr.c line 13
Quoted 3 lines
arch/arm64/mm/physaddr.c new file +3
-0+return (__x & ~PAGE_OFFSET ) +PHYS_OFFSET ; + }else {l + VIRTUAL_BUG_ON(x < kimage_vaddr || x > (unsigned long)_end);This looks fine. References to
_endare relocated at boot to refer to the actual runtime offset.
Thanks for clarifying.
Quoted 33 lines
Quoted 33 lines
arch/arm64/mm/physaddr.c new file +4
-0+return (__x - kimage_voffset); + } +} +EXPORT_SYMBOL (__virt_to_phys);lib/Kconfig.debug +1
-1@@ -604,7 +604,7 @@ config DEBUG_VM_PGFLAGS config DEBUG_VIRTUAL bool "Debug VM translations" - depends on DEBUG_KERNEL && X86 + depends on DEBUG_KERNEL && (X86 || ARM64) help Enable some costly sanity checks in virtual to page code. This can catch mistakes with virt_to_page() and friends.mm/cma.c +1
-2@@ -235,7 +235,7 @@ int __init cma_declare_contiguous(phys_addr_t base,phys_addr_t highmem_start;int ret =0 ; -#ifdef CONFIG_X86 +#if defined(CONFIG_X86 ) || defined(CONFIG_ARM64 )/* * high_memory isn't direct mapped memory so retrieving its physical * address isn't appropriate. But it would be useful to check the -- 2.7.4
Mark Rutland
reviewerHi Laura,
Quoted 7 lines · Laura Abbott
x86 has an option
CONFIG_DEBUG_VIRTUALto do additional checks onvirt_to_physcalls. The goal is to catch users who are callingvirt_to_physon non-linear addresses immediately. As features such asCONFIG_VMAP_STACKget enabled for arm64, this becomes increasingly important. Add checks to catch badvirt_to_physusage.
Quoted 6 lines
Signed-off-by Laura Abbott <[email protected]>---
This has been on my TODO list for a while. It caught a few bugs withCONFIG_VMAP_STACKon x86 so when that eventually gets added
for arm64 it will be useful to have. This caught one driver calling__paon an ioremapped address already.
This is fantastic; thanks for putting this together!
Quoted 8 lines
RFC for a couple of reasons:
1) This is basically a direct port of the x86 approach.
2) I needed some #ifndef__ASSEMBLY__which I don't like to throw around.
3) I'm not quite sure about the bounds check for theVIRTUAL_BUG_ONwith KASLR, specifically the_endcheck.
4) Is it worth actually keeping this asDEBUG_VIRTUALvs. folding it into another option?
I think it's worth aligning with x86, so modulo a couple of comments below, (1) and (4) seem fine. I think (2) just requires an additional shuffle.
Quoted 23 lines
--- arch/arm64/include/asm/memory.h | 11 ++++++++++- arch/arm64/mm/Makefile | 2 +- arch/arm64/mm/physaddr.c | 17 +++++++++++++++++ lib/Kconfig.debug | 2 +- mm/cma.c | 2 +- 5 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 arch/arm64/mm/physaddr.carch/arm64/include/asm/memory.h +1
-1@@ -106,11 +106,19 @@* private definitions which should NOT be used outside memory.h * files. Use virt_to_phys/phys_to_virt/__pa/__va instead. */ -#define __virt_to_phys (x) ({ \ +#define __virt_to_phys_nodebug (x) ({ \ phys_addr_t __x = (phys_addr_t)(x); \ __x & BIT(VA_BITS - 1) ? (__x & ~PAGE_OFFSET) + PHYS_OFFSET : \ (__x - kimage_voffset); })
arch/arm64/include/asm/memory.h line 123
Quoted 10 lines
arch/arm64/include/asm/memory.h +8
-0+#ifdef CONFIG_DEBUG_VIRTUAL +#ifndef __ASSEMBLY__ +extern unsigned long __virt_to_phys (unsigned long x); +#endif +#else +#define __virt_to_phys (x) __virt_to_phys_nodebug(x) +#endif +#define __phys_to_virt (x) ((unsigned long)((x) - PHYS_OFFSET) | PAGE_OFFSET)#define __phys_to_kimg (x) ((unsigned long)((x) + kimage_voffset))
I think we can move all the existing conversion logic here (including into page_to_phys/phys_to_page) into the existing #ifndef __ASSEMBLY__ block at the end of the file. Assembly clearly can't use these, and we already have virt_to_phys and others in that #ifndef.
Assuming that works, would you mind doing that as a preparatory patch?
Quoted 29 lines
arch/arm64/include/asm/memory.h +1
-0@@ -202,6 +210,7 @@ static inline void *phys_to_virt(phys_addr_t x)* Drivers should NOT use these either. */ #define __pa (x) __virt_to_phys((unsigned long)(x)) +#define __pa_nodebug (x) __virt_to_phys_nodebug((unsigned long)(x))#define __va (x) ((void *)__phys_to_virt((phys_addr_t)(x)))#define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT)#define virt_to_pfn (x) __phys_to_pfn(__virt_to_phys(x))arch/arm64/mm/Makefile +1
-1@@ -5,6 +5,6 @@ obj-y := dma-mapping.o extable.o fault.o init.o \ obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o obj-$(CONFIG_ARM64_PTDUMP) += dump.o obj-$(CONFIG_NUMA) += numa.o - +obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o obj-$(CONFIG_KASAN) += kasan_init.o KASAN_SANITIZE_kasan_init.o := narch/arm64/mm/physaddr.c new file +3
-0@@ -0,0 +1,17 @@ +#include <linux/mm.h> + +#include <asm/memory.h>
arch/arm64/mm/physaddr.c line 13
Quoted 10 lines
arch/arm64/mm/physaddr.c new file +10
-0+ +unsigned long __virt_to_phys (unsigned long x) +{ +phys_addr_t __x = (phys_addr_t )x; + +if (__x &BIT (VA_BITS -1 )) { +/* The bit check ensures this is the right range */ +return (__x & ~PAGE_OFFSET ) +PHYS_OFFSET ; + }else { +VIRTUAL_BUG_ON (x < kimage_vaddr || x > (unsigned long )_end);
IIUC, in (3) you were asking if the last check should be '>' or '>='?
To match high_memory, I suspect the latter, as _end doesn't fall within the mapped virtual address space.
arch/arm64/mm/physaddr.c line 17
Quoted 4 lines
arch/arm64/mm/physaddr.c new file +4
-0+return (__x - kimage_voffset); + } +} +EXPORT_SYMBOL (__virt_to_phys);
It's a bit annoying that we have to duplicate the logic here to add the VIRTUAL_BUG_ON(), but I see that x86 also do this, and I don't have a better suggestion.
lib/Kconfig.debug config DEBUG_VM_PGFLAGS · line 607
Quoted 10 lines
lib/Kconfig.debug +1
-1@@ -604,7 +604,7 @@ config DEBUG_VM_PGFLAGS config DEBUG_VIRTUAL bool "Debug VM translations" - depends on DEBUG_KERNEL && X86 + depends on DEBUG_KERNEL && (X86 || ARM64)
I have no strong feelings about this, but perhaps it's nicer to have something like ARCH_HAS_DEBUG_VIRTUAL?
mm/cma.c int __init cma_declare_contiguous(phys_addr_t base, · line 238
Quoted 13 lines
lib/Kconfig.debug +0
-0help Enable some costly sanity checks in virtual to page code. This can catch mistakes with virt_to_page() and friends.mm/cma.c +1
-1@@ -235,7 +235,7 @@ int __init cma_declare_contiguous(phys_addr_t base,phys_addr_t highmem_start;int ret =0 ; -#ifdef CONFIG_X86 +#if defined(CONFIG_X86 ) || defined(CONFIG_ARM64 )
Rather than an architecture list, can we depend on DEBUG_VIRTUAL? Are there other checks that we're trying to avoid?
... or could we avoid ifdeffery entirely with something like:
/*
* We can't use __pa(high_memory) directly, since high_memory
* isn't a valid direct map VA, and DEBUG_VIRTUAL will (validly)
* complain. Find the boundary by adding one to the last valid
* address.
*/
highmem_start = __pa(high_memory - 1) + 1;
Thanks,
Mark.
Quoted 6 lines
mm/cma.c +0
-0/* * high_memory isn't direct mapped memory so retrieving its physical * address isn't appropriate. But it would be useful to check the Signature · 1 line
2.7.4
Quoted 71 lines · Mark Rutland
Hi Laura,
Quoted 7 lines · Laura Abbott
x86 has an option
CONFIG_DEBUG_VIRTUALto do additional checks onvirt_to_physcalls. The goal is to catch users who are callingvirt_to_physon non-linear addresses immediately. As features such asCONFIG_VMAP_STACKget enabled for arm64, this becomes increasingly important. Add checks to catch badvirt_to_physusage.Quoted 6 lines
Signed-off-by Laura Abbott <[email protected]>---
This has been on my TODO list for a while. It caught a few bugs withCONFIG_VMAP_STACKon x86 so when that eventually gets added
for arm64 it will be useful to have. This caught one driver calling__paon an ioremapped address already.This is fantastic; thanks for putting this together!
Quoted 8 lines
RFC for a couple of reasons:
1) This is basically a direct port of the x86 approach.
2) I needed some #ifndef__ASSEMBLY__which I don't like to throw around.
3) I'm not quite sure about the bounds check for theVIRTUAL_BUG_ONwith KASLR, specifically the_endcheck.
4) Is it worth actually keeping this asDEBUG_VIRTUALvs. folding it into another option?I think it's worth aligning with x86, so modulo a couple of comments below, (1) and (4) seem fine. I think (2) just requires an additional shuffle.
Quoted 23 lines
--- arch/arm64/include/asm/memory.h | 11 ++++++++++- arch/arm64/mm/Makefile | 2 +- arch/arm64/mm/physaddr.c | 17 +++++++++++++++++ lib/Kconfig.debug | 2 +- mm/cma.c | 2 +- 5 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 arch/arm64/mm/physaddr.carch/arm64/include/asm/memory.h +1
-1@@ -106,11 +106,19 @@* private definitions which should NOT be used outside memory.h * files. Use virt_to_phys/phys_to_virt/__pa/__va instead. */ -#define __virt_to_phys (x) ({ \ +#define __virt_to_phys_nodebug (x) ({ \ phys_addr_t __x = (phys_addr_t)(x); \ __x & BIT(VA_BITS - 1) ? (__x & ~PAGE_OFFSET) + PHYS_OFFSET : \ (__x - kimage_voffset); })arch/arm64/include/asm/memory.h line 123
Quoted 10 lines
arch/arm64/include/asm/memory.h +8
-0+#ifdef CONFIG_DEBUG_VIRTUAL +#ifndef __ASSEMBLY__ +extern unsigned long __virt_to_phys (unsigned long x); +#endif +#else +#define __virt_to_phys (x) __virt_to_phys_nodebug(x) +#endif +#define __phys_to_virt (x) ((unsigned long)((x) - PHYS_OFFSET) | PAGE_OFFSET)#define __phys_to_kimg (x) ((unsigned long)((x) + kimage_voffset))I think we can move all the existing conversion logic here (including into page_to_phys/phys_to_page) into the existing #ifndef
__ASSEMBLY__block at the end of the file. Assembly clearly can't use these, and we already havevirt_to_physand others in that #ifndef.
Quoted 2 lines
Assuming that works, would you mind doing that as a preparatory patch?
Sure.
Quoted 42 lines
Quoted 29 lines
arch/arm64/include/asm/memory.h +1
-0@@ -202,6 +210,7 @@ static inline void *phys_to_virt(phys_addr_t x)* Drivers should NOT use these either. */ #define __pa (x) __virt_to_phys((unsigned long)(x)) +#define __pa_nodebug (x) __virt_to_phys_nodebug((unsigned long)(x))#define __va (x) ((void *)__phys_to_virt((phys_addr_t)(x)))#define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT)#define virt_to_pfn (x) __phys_to_pfn(__virt_to_phys(x))arch/arm64/mm/Makefile +1
-1@@ -5,6 +5,6 @@ obj-y := dma-mapping.o extable.o fault.o init.o \ obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o obj-$(CONFIG_ARM64_PTDUMP) += dump.o obj-$(CONFIG_NUMA) += numa.o - +obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o obj-$(CONFIG_KASAN) += kasan_init.o KASAN_SANITIZE_kasan_init.o := narch/arm64/mm/physaddr.c new file +3
-0@@ -0,0 +1,17 @@ +#include <linux/mm.h> + +#include <asm/memory.h> arch/arm64/mm/physaddr.c line 13
Quoted 10 lines
arch/arm64/mm/physaddr.c new file +10
-0+ +unsigned long __virt_to_phys (unsigned long x) +{ +phys_addr_t __x = (phys_addr_t )x; + +if (__x &BIT (VA_BITS -1 )) { +/* The bit check ensures this is the right range */ +return (__x & ~PAGE_OFFSET ) +PHYS_OFFSET ; + }else { +VIRTUAL_BUG_ON (x < kimage_vaddr || x > (unsigned long )_end);IIUC, in (3) you were asking if the last check should be '>' or '>='?
Quoted 3 lines
To match
high_memory, I suspect the latter, as_enddoesn't fall within the mapped virtual address space.
I was actually concerned about if _end would be correct with KASLR. Ard confirmed that it gets fixed up to be correct. I'll change the check to check for >=.
Quoted 15 lines
arch/arm64/mm/physaddr.c line 17
Quoted 4 lines
arch/arm64/mm/physaddr.c new file +4
-0+return (__x - kimage_voffset); + } +} +EXPORT_SYMBOL (__virt_to_phys);It's a bit annoying that we have to duplicate the logic here to add the
VIRTUAL_BUG_ON(), but I see that x86 also do this, and I don't have a better suggestion.Quoted 6 lines
lib/Kconfig.debug +0
-0@@ -604,7 +604,7 @@ config DEBUG_VM_PGFLAGS
Quoted 8 lines
lib/Kconfig.debug config DEBUG_VM_PGFLAGS · line 607
Quoted 4 lines
lib/Kconfig.debug +1
-1config DEBUG_VIRTUAL bool "Debug VM translations" - depends on DEBUG_KERNEL && X86 + depends on DEBUG_KERNEL && (X86 || ARM64)I have no strong feelings about this, but perhaps it's nicer to have something like
ARCH_HAS_DEBUG_VIRTUAL?
Yes, if this ever gets added for other arches this will start to get unwieldy. I'll look at cleaning that up.
Quoted 19 lines
mm/cma.c int __init cma_declare_contiguous(phys_addr_t base, · line 238
Quoted 13 lines
lib/Kconfig.debug +0
-0help Enable some costly sanity checks in virtual to page code. This can catch mistakes with virt_to_page() and friends.mm/cma.c +1
-1@@ -235,7 +235,7 @@ int __init cma_declare_contiguous(phys_addr_t base,phys_addr_t highmem_start;int ret =0 ; -#ifdef CONFIG_X86 +#if defined(CONFIG_X86 ) || defined(CONFIG_ARM64 )Rather than an architecture list, can we depend on
DEBUG_VIRTUAL? Are there other checks that we're trying to avoid?... or could we avoid ifdeffery entirely with something like:
Quoted 8 lines
/*
* We can't use__pa(high_memory)directly, sincehigh_memory
* isn't a valid direct map VA, andDEBUG_VIRTUALwill (validly)
* complain. Find the boundary by adding one to the last valid
* address.
*/
highmem_start=__pa(high_memory - 1)+ 1;
I like getting rid of the #ifdef there. Maybe future cleanup could turn this into a #define, HIGHMEM_PHYS since it seems to be used in quite a few places in the kernel.
Quoted 3 lines
Thanks,
Mark.
Thanks,
Laura
Quoted 6 lines
Quoted 6 lines
mm/cma.c +0
-1/* * high_memory isn't direct mapped memory so retrieving its physical * address isn't appropriate. But it would be useful to check the -- 2.7.4
Quoted 22 lines
Quoted 22 lines
Quoted 9 lines
arch/arm64/mm/physaddr.c new file +3
-0@@ -0,0 +1,17 @@ +#include <linux/mm.h> + +#include <asm/memory.h> arch/arm64/mm/physaddr.c line 13
Quoted 10 lines
arch/arm64/mm/physaddr.c new file +10
-0+ +unsigned long __virt_to_phys (unsigned long x) +{ +phys_addr_t __x = (phys_addr_t )x; + +if (__x &BIT (VA_BITS -1 )) { +/* The bit check ensures this is the right range */ +return (__x & ~PAGE_OFFSET ) +PHYS_OFFSET ; + }else { +VIRTUAL_BUG_ON (x < kimage_vaddr || x > (unsigned long )_end);IIUC, in (3) you were asking if the last check should be '>' or '>='?
Quoted 8 lines
Quoted 3 lines
To match
high_memory, I suspect the latter, as_enddoesn't fall within the mapped virtual address space.I was actually concerned about if
_endwould be correct with KASLR. Ard confirmed that it gets fixed up to be correct. I'll change the check to check for >=.
While testing this, I found two places with __pa(_end) to get bounds, one in arm64 code and one in memblock code. x86 gets away with this because memblock is actually __pa_symbol and x86 does image placement different and can check against the maximum image size. I think including _end in __pa_symbol but excluding it from the generic __virt_to_phys makes sense. It's a bit nicer than doing _end - 1 + 1 everywhere.
Thanks,
Laura
Quoted 32 lines · Laura Abbott
Quoted 23 lines
Quoted 23 lines
Quoted 9 lines
arch/arm64/mm/physaddr.c new file +3
-0@@ -0,0 +1,17 @@ +#include <linux/mm.h> + +#include <asm/memory.h> arch/arm64/mm/physaddr.c line 13
Quoted 10 lines
arch/arm64/mm/physaddr.c new file +10
-0+ +unsigned long __virt_to_phys (unsigned long x) +{ +phys_addr_t __x = (phys_addr_t )x; + +if (__x &BIT (VA_BITS -1 )) { +/* The bit check ensures this is the right range */ +return (__x & ~PAGE_OFFSET ) +PHYS_OFFSET ; + }else { +VIRTUAL_BUG_ON (x < kimage_vaddr || x > (unsigned long )_end);IIUC, in (3) you were asking if the last check should be '>' or '>='?
Quoted 8 lines
Quoted 3 lines
To match
high_memory, I suspect the latter, as_enddoesn't fall within the mapped virtual address space.I was actually concerned about if
_endwould be correct with KASLR. Ard confirmed that it gets fixed up to be correct. I'll change the check to check for >=.
Quoted 8 lines
While testing this, I found two places with
__pa(_end)to get bounds, one in arm64 code and one in memblock code. x86 gets away with this because memblock is actually__pa_symboland x86 does image placement different and can check against the maximum image size. I think including_endin__pa_symbolbut excluding it from the generic__virt_to_physmakes sense. It's a bit nicer than doing_end- 1 + 1 everywhere.
Could we redefine __pa_symbol() under CONFIG_DEBUG_VIRTUAL to something that checks (x >= kimage_vaddr + TEXT_OFFSET || x <= (unsigned long)_end), i.e., reject linear virtual addresses? (Assuming my understanding of the meaning of __pa_symbol() is correct)
Quoted 42 lines · Ard Biesheuvel
Quoted 32 lines · Laura Abbott
Quoted 23 lines
Quoted 23 lines
Quoted 9 lines
arch/arm64/mm/physaddr.c new file +3
-0@@ -0,0 +1,17 @@ +#include <linux/mm.h> + +#include <asm/memory.h> arch/arm64/mm/physaddr.c line 13
Quoted 10 lines
arch/arm64/mm/physaddr.c new file +10
-0+ +unsigned long __virt_to_phys (unsigned long x) +{ +phys_addr_t __x = (phys_addr_t )x; + +if (__x &BIT (VA_BITS -1 )) { +/* The bit check ensures this is the right range */ +return (__x & ~PAGE_OFFSET ) +PHYS_OFFSET ; + }else { +VIRTUAL_BUG_ON (x < kimage_vaddr || x > (unsigned long )_end);IIUC, in (3) you were asking if the last check should be '>' or '>='?
Quoted 8 lines
Quoted 3 lines
To match
high_memory, I suspect the latter, as_enddoesn't fall within the mapped virtual address space.I was actually concerned about if
_endwould be correct with KASLR. Ard confirmed that it gets fixed up to be correct. I'll change the check to check for >=.Quoted 8 lines
While testing this, I found two places with
__pa(_end)to get bounds, one in arm64 code and one in memblock code. x86 gets away with this because memblock is actually__pa_symboland x86 does image placement different and can check against the maximum image size. I think including_endin__pa_symbolbut excluding it from the generic__virt_to_physmakes sense. It's a bit nicer than doing_end- 1 + 1 everywhere.
Quoted 5 lines
Could we redefine
__pa_symbol()underCONFIG_DEBUG_VIRTUALto something that checks (x >=kimage_vaddr+TEXT_OFFSET|| x <= (unsigned long)_end), i.e., reject linear virtual addresses? (Assuming my understanding of the meaning of__pa_symbol()is correct)
Yes, that's going to be my approach. I originally prototyped this with just x >= kimage_vaddr but kimage_vaddr + TEXT_OFFSET is a tighter bound.
Thanks,
Laura