Skip to content

Commit ec6d06e

Browse files
labbottwildea01
authored andcommitted
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. This inclues callers using virt_to_phys on image addresses instead of __pa_symbol. As features such as CONFIG_VMAP_STACK get enabled for arm64, this becomes increasingly important. Add checks to catch bad virt_to_phys usage. Reviewed-by: Mark Rutland <mark.rutland@arm.com> Tested-by: Mark Rutland <mark.rutland@arm.com> Signed-off-by: Laura Abbott <labbott@redhat.com> Signed-off-by: Will Deacon <will.deacon@arm.com>
1 parent 2077be6 commit ec6d06e

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ config ARM64
66
select ACPI_MCFG if ACPI
77
select ACPI_SPCR_TABLE if ACPI
88
select ARCH_CLOCKSOURCE_DATA
9+
select ARCH_HAS_DEBUG_VIRTUAL
910
select ARCH_HAS_DEVMEM_IS_ALLOWED
1011
select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI
1112
select ARCH_HAS_ELF_RANDOMIZE

arch/arm64/include/asm/memory.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,33 @@ static inline unsigned long kaslr_offset(void)
172172
* private definitions which should NOT be used outside memory.h
173173
* files. Use virt_to_phys/phys_to_virt/__pa/__va instead.
174174
*/
175-
#define __virt_to_phys(x) ({ \
175+
176+
177+
/*
178+
* The linear kernel range starts in the middle of the virtual adddress
179+
* space. Testing the top bit for the start of the region is a
180+
* sufficient check.
181+
*/
182+
#define __is_lm_address(addr) (!!((addr) & BIT(VA_BITS - 1)))
183+
184+
#define __lm_to_phys(addr) (((addr) & ~PAGE_OFFSET) + PHYS_OFFSET)
185+
#define __kimg_to_phys(addr) ((addr) - kimage_voffset)
186+
187+
#define __virt_to_phys_nodebug(x) ({ \
176188
phys_addr_t __x = (phys_addr_t)(x); \
177-
__x & BIT(VA_BITS - 1) ? (__x & ~PAGE_OFFSET) + PHYS_OFFSET : \
178-
(__x - kimage_voffset); })
189+
__is_lm_address(__x) ? __lm_to_phys(__x) : \
190+
__kimg_to_phys(__x); \
191+
})
192+
193+
#define __pa_symbol_nodebug(x) __kimg_to_phys((phys_addr_t)(x))
194+
195+
#ifdef CONFIG_DEBUG_VIRTUAL
196+
extern phys_addr_t __virt_to_phys(unsigned long x);
197+
extern phys_addr_t __phys_addr_symbol(unsigned long x);
198+
#else
199+
#define __virt_to_phys(x) __virt_to_phys_nodebug(x)
200+
#define __phys_addr_symbol(x) __pa_symbol_nodebug(x)
201+
#endif
179202

180203
#define __phys_to_virt(x) ((unsigned long)((x) - PHYS_OFFSET) | PAGE_OFFSET)
181204
#define __phys_to_kimg(x) ((unsigned long)((x) + kimage_voffset))
@@ -207,6 +230,8 @@ static inline void *phys_to_virt(phys_addr_t x)
207230
* Drivers should NOT use these either.
208231
*/
209232
#define __pa(x) __virt_to_phys((unsigned long)(x))
233+
#define __pa_symbol(x) __phys_addr_symbol(RELOC_HIDE((unsigned long)(x), 0))
234+
#define __pa_nodebug(x) __virt_to_phys_nodebug((unsigned long)(x))
210235
#define __va(x) ((void *)__phys_to_virt((phys_addr_t)(x)))
211236
#define pfn_to_kaddr(pfn) __va((pfn) << PAGE_SHIFT)
212237
#define virt_to_pfn(x) __phys_to_pfn(__virt_to_phys((unsigned long)(x)))

arch/arm64/mm/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o
66
obj-$(CONFIG_ARM64_PTDUMP_CORE) += dump.o
77
obj-$(CONFIG_ARM64_PTDUMP_DEBUGFS) += ptdump_debugfs.o
88
obj-$(CONFIG_NUMA) += numa.o
9+
obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o
10+
KASAN_SANITIZE_physaddr.o += n
911

1012
obj-$(CONFIG_KASAN) += kasan_init.o
1113
KASAN_SANITIZE_kasan_init.o := n

arch/arm64/mm/physaddr.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <linux/bug.h>
2+
#include <linux/export.h>
3+
#include <linux/types.h>
4+
#include <linux/mmdebug.h>
5+
#include <linux/mm.h>
6+
7+
#include <asm/memory.h>
8+
9+
phys_addr_t __virt_to_phys(unsigned long x)
10+
{
11+
WARN(!__is_lm_address(x),
12+
"virt_to_phys used for non-linear address: %pK (%pS)\n",
13+
(void *)x,
14+
(void *)x);
15+
16+
return __virt_to_phys_nodebug(x);
17+
}
18+
EXPORT_SYMBOL(__virt_to_phys);
19+
20+
phys_addr_t __phys_addr_symbol(unsigned long x)
21+
{
22+
/*
23+
* This is bounds checking against the kernel image only.
24+
* __pa_symbol should only be used on kernel symbol addresses.
25+
*/
26+
VIRTUAL_BUG_ON(x < (unsigned long) KERNEL_START ||
27+
x > (unsigned long) KERNEL_END);
28+
return __pa_symbol_nodebug(x);
29+
}
30+
EXPORT_SYMBOL(__phys_addr_symbol);

0 commit comments

Comments
 (0)