2020-01-02 03:13:41

by Zong Li

[permalink] [raw]
Subject: [PATCH 0/2] riscv: mm: add support for CONFIG_DEBUG_VIRTUAL

This patch implements CONFIG_DEBUG_VIRTUAL to do additional checks on
virt_to_phys and __pa_symbol calls. virt_to_phys used for linear mapping
check, and __pa_symbol used for kernel symbol check. In current RISC-V,
kernel image maps to linear mapping area. If CONFIG_DEBUG_VIRTUAL is
disable, these two functions calculate the offset on the address feded
directly without any checks.

Zong Li (2):
riscv: mm: add support for CONFIG_DEBUG_VIRTUAL
riscv: mm: use __pa_symbol for kernel symbols

arch/riscv/Kconfig | 1 +
arch/riscv/include/asm/page.h | 16 +++++++++++++--
arch/riscv/mm/Makefile | 2 ++
arch/riscv/mm/init.c | 12 ++++++------
arch/riscv/mm/physaddr.c | 37 +++++++++++++++++++++++++++++++++++
5 files changed, 60 insertions(+), 8 deletions(-)
create mode 100644 arch/riscv/mm/physaddr.c

--
2.24.1


2020-01-02 03:13:44

by Zong Li

[permalink] [raw]
Subject: [PATCH 1/2] riscv: mm: add support for CONFIG_DEBUG_VIRTUAL

This patch implements CONFIG_DEBUG_VIRTUAL to do additional checks on
virt_to_phys and __pa_symbol calls. virt_to_phys used for linear mapping
check, and __pa_symbol used for kernel symbol check. In current RISC-V,
kernel image maps to linear mapping area. If CONFIG_DEBUG_VIRTUAL is
disable, these two functions calculate the offset on the address feded
directly without any checks.

The result of test_debug_virtual as follows:

[ 0.358456] ------------[ cut here ]------------
[ 0.358738] virt_to_phys used for non-linear address: (____ptrval____) (0xffffffd000000000)
[ 0.359174] WARNING: CPU: 0 PID: 1 at arch/riscv/mm/physaddr.c:16 __virt_to_phys+0x3c/0x50
[ 0.359409] Modules linked in:
[ 0.359630] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.5.0-rc3-00002-g5133c5c0ca13 #57
[ 0.359861] epc: ffffffe000253d1a ra : ffffffe000253d1a sp : ffffffe03aa87da0
[ 0.360019] gp : ffffffe000ae03b0 tp : ffffffe03aa88000 t0 : ffffffe000af2660
[ 0.360175] t1 : 0000000000000064 t2 : 00000000000000b7 s0 : ffffffe03aa87dc0
[ 0.360330] s1 : ffffffd000000000 a0 : 000000000000004f a1 : 0000000000000000
[ 0.360492] a2 : 0000000000000000 a3 : 0000000000000000 a4 : ffffffe000a84358
[ 0.360672] a5 : 0000000000000000 a6 : 0000000000000000 a7 : 0000000000000000
[ 0.360876] s2 : ffffffe000ae0600 s3 : ffffffe00000fc7c s4 : ffffffe0000224b0
[ 0.361067] s5 : ffffffe000030890 s6 : ffffffe000022470 s7 : 0000000000000008
[ 0.361267] s8 : ffffffe0000002c4 s9 : ffffffe000ae0640 s10: ffffffe000ae0630
[ 0.361453] s11: 0000000000000000 t3 : 0000000000000000 t4 : 000000000001e6d0
[ 0.361636] t5 : ffffffe000ae0a18 t6 : ffffffe000aee54e
[ 0.361806] status: 0000000000000120 badaddr: 0000000000000000 cause: 0000000000000003
[ 0.362056] ---[ end trace aec0bf78d4978122 ]---
[ 0.362404] PA: 0xfffffff080200000 for VA: 0xffffffd000000000
[ 0.362607] PA: 0x00000000baddd2d0 for VA: 0xffffffe03abdd2d0

Signed-off-by: Zong Li <[email protected]>
---
arch/riscv/Kconfig | 1 +
arch/riscv/include/asm/page.h | 16 +++++++++++++--
arch/riscv/mm/Makefile | 2 ++
arch/riscv/mm/physaddr.c | 37 +++++++++++++++++++++++++++++++++++
4 files changed, 54 insertions(+), 2 deletions(-)
create mode 100644 arch/riscv/mm/physaddr.c

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index d8efbaa78d67..7f268816be5b 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -57,6 +57,7 @@ config RISCV
select GENERIC_ARCH_TOPOLOGY if SMP
select ARCH_HAS_PTE_SPECIAL
select ARCH_HAS_MMIOWB
+ select ARCH_HAS_DEBUG_VIRTUAL
select HAVE_EBPF_JIT if 64BIT
select EDAC_SUPPORT
select ARCH_HAS_GIGANTIC_PAGE
diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h
index ac699246ae7e..8ca1930caa44 100644
--- a/arch/riscv/include/asm/page.h
+++ b/arch/riscv/include/asm/page.h
@@ -100,8 +100,20 @@ extern unsigned long pfn_base;
extern unsigned long max_low_pfn;
extern unsigned long min_low_pfn;

-#define __pa(x) ((unsigned long)(x) - va_pa_offset)
-#define __va(x) ((void *)((unsigned long) (x) + va_pa_offset))
+#define __pa_to_va_nodebug(x) ((void *)((unsigned long) (x) + va_pa_offset))
+#define __va_to_pa_nodebug(x) ((unsigned long)(x) - va_pa_offset)
+
+#ifdef CONFIG_DEBUG_VIRTUAL
+extern phys_addr_t __virt_to_phys(unsigned long x);
+extern phys_addr_t __phys_addr_symbol(unsigned long x);
+#else
+#define __virt_to_phys(x) __va_to_pa_nodebug(x)
+#define __phys_addr_symbol(x) __va_to_pa_nodebug(x)
+#endif /* CONFIG_DEBUG_VIRTUAL */
+
+#define __pa_symbol(x) __phys_addr_symbol(RELOC_HIDE((unsigned long)(x), 0))
+#define __pa(x) __virt_to_phys((unsigned long)(x))
+#define __va(x) ((void *)__pa_to_va_nodebug((phys_addr_t)(x)))

#define phys_to_pfn(phys) (PFN_DOWN(phys))
#define pfn_to_phys(pfn) (PFN_PHYS(pfn))
diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile
index a1bd95c8047a..7de157ecfb77 100644
--- a/arch/riscv/mm/Makefile
+++ b/arch/riscv/mm/Makefile
@@ -15,3 +15,5 @@ ifeq ($(CONFIG_MMU),y)
obj-$(CONFIG_SMP) += tlbflush.o
endif
obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o
+
+obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o
diff --git a/arch/riscv/mm/physaddr.c b/arch/riscv/mm/physaddr.c
new file mode 100644
index 000000000000..e8e4dcd39fed
--- /dev/null
+++ b/arch/riscv/mm/physaddr.c
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/types.h>
+#include <linux/mmdebug.h>
+#include <linux/mm.h>
+#include <asm/page.h>
+#include <asm/sections.h>
+
+phys_addr_t __virt_to_phys(unsigned long x)
+{
+ phys_addr_t y = x - PAGE_OFFSET;
+
+ /*
+ * Boundary checking aginst the kernel linear mapping space.
+ */
+ WARN(y >= KERN_VIRT_SIZE,
+ "virt_to_phys used for non-linear address: %pK (%pS)\n",
+ (void *)x, (void *)x);
+
+ return __va_to_pa_nodebug(x);
+}
+EXPORT_SYMBOL(__virt_to_phys);
+
+phys_addr_t __phys_addr_symbol(unsigned long x)
+{
+ unsigned long kernel_start = (unsigned long)PAGE_OFFSET;
+ unsigned long kernel_end = (unsigned long)_end;
+
+ /*
+ * Boundary checking aginst the kernel image mapping.
+ * __pa_symbol should only be used on kernel symbol addresses.
+ */
+ VIRTUAL_BUG_ON(x < kernel_start || x > kernel_end);
+
+ return __va_to_pa_nodebug(x);
+}
+EXPORT_SYMBOL(__phys_addr_symbol);
--
2.24.1

2020-01-02 03:15:07

by Zong Li

[permalink] [raw]
Subject: [PATCH 2/2] riscv: mm: use __pa_symbol for kernel symbols

__pa_symbol is the marcro that should be used for kernel symbols. It is
also a pre-requisite for DEBUG_VIRTUAL which will do bounds checking.

Signed-off-by: Zong Li <[email protected]>
---
arch/riscv/mm/init.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index 69f6678db7f3..965a8cf4829c 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -99,13 +99,13 @@ static void __init setup_initrd(void)
pr_info("initrd not found or empty");
goto disable;
}
- if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
+ if (__pa_symbol(initrd_end) > PFN_PHYS(max_low_pfn)) {
pr_err("initrd extends beyond end of memory");
goto disable;
}

size = initrd_end - initrd_start;
- memblock_reserve(__pa(initrd_start), size);
+ memblock_reserve(__pa_symbol(initrd_start), size);
initrd_below_start_ok = 1;

pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
@@ -124,8 +124,8 @@ void __init setup_bootmem(void)
{
struct memblock_region *reg;
phys_addr_t mem_size = 0;
- phys_addr_t vmlinux_end = __pa(&_end);
- phys_addr_t vmlinux_start = __pa(&_start);
+ phys_addr_t vmlinux_end = __pa_symbol(&_end);
+ phys_addr_t vmlinux_start = __pa_symbol(&_start);

/* Find the memory region containing the kernel */
for_each_memblock(memory, reg) {
@@ -445,7 +445,7 @@ static void __init setup_vm_final(void)

/* Setup swapper PGD for fixmap */
create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
- __pa(fixmap_pgd_next),
+ __pa_symbol(fixmap_pgd_next),
PGDIR_SIZE, PAGE_TABLE);

/* Map all memory banks */
@@ -474,7 +474,7 @@ static void __init setup_vm_final(void)
clear_fixmap(FIX_PMD);

/* Move to swapper page table */
- csr_write(CSR_SATP, PFN_DOWN(__pa(swapper_pg_dir)) | SATP_MODE);
+ csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
local_flush_tlb_all();
}
#else
--
2.24.1

2020-01-02 03:33:13

by Anup Patel

[permalink] [raw]
Subject: Re: [PATCH 2/2] riscv: mm: use __pa_symbol for kernel symbols

On Thu, Jan 2, 2020 at 8:42 AM Zong Li <[email protected]> wrote:
>
> __pa_symbol is the marcro that should be used for kernel symbols. It is
> also a pre-requisite for DEBUG_VIRTUAL which will do bounds checking.
>
> Signed-off-by: Zong Li <[email protected]>
> ---
> arch/riscv/mm/init.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> index 69f6678db7f3..965a8cf4829c 100644
> --- a/arch/riscv/mm/init.c
> +++ b/arch/riscv/mm/init.c
> @@ -99,13 +99,13 @@ static void __init setup_initrd(void)
> pr_info("initrd not found or empty");
> goto disable;
> }
> - if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
> + if (__pa_symbol(initrd_end) > PFN_PHYS(max_low_pfn)) {
> pr_err("initrd extends beyond end of memory");
> goto disable;
> }
>
> size = initrd_end - initrd_start;
> - memblock_reserve(__pa(initrd_start), size);
> + memblock_reserve(__pa_symbol(initrd_start), size);
> initrd_below_start_ok = 1;
>
> pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
> @@ -124,8 +124,8 @@ void __init setup_bootmem(void)
> {
> struct memblock_region *reg;
> phys_addr_t mem_size = 0;
> - phys_addr_t vmlinux_end = __pa(&_end);
> - phys_addr_t vmlinux_start = __pa(&_start);
> + phys_addr_t vmlinux_end = __pa_symbol(&_end);
> + phys_addr_t vmlinux_start = __pa_symbol(&_start);
>
> /* Find the memory region containing the kernel */
> for_each_memblock(memory, reg) {
> @@ -445,7 +445,7 @@ static void __init setup_vm_final(void)
>
> /* Setup swapper PGD for fixmap */
> create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
> - __pa(fixmap_pgd_next),
> + __pa_symbol(fixmap_pgd_next),
> PGDIR_SIZE, PAGE_TABLE);
>
> /* Map all memory banks */
> @@ -474,7 +474,7 @@ static void __init setup_vm_final(void)
> clear_fixmap(FIX_PMD);
>
> /* Move to swapper page table */
> - csr_write(CSR_SATP, PFN_DOWN(__pa(swapper_pg_dir)) | SATP_MODE);
> + csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
> local_flush_tlb_all();
> }
> #else
> --
> 2.24.1
>

Overall looks good to me.

Reviewed-by: Anup Patel <[email protected]>

I have not tried this patch but can you confirm that
__pa_symbol() works fine even when DEBUG_VIRTUAL=n

Regards,
Anup

2020-01-02 03:50:57

by Zong Li

[permalink] [raw]
Subject: Re: [PATCH 2/2] riscv: mm: use __pa_symbol for kernel symbols

On Thu, Jan 2, 2020 at 11:32 AM Anup Patel <[email protected]> wrote:
>
> On Thu, Jan 2, 2020 at 8:42 AM Zong Li <[email protected]> wrote:
> >
> > __pa_symbol is the marcro that should be used for kernel symbols. It is
> > also a pre-requisite for DEBUG_VIRTUAL which will do bounds checking.
> >
> > Signed-off-by: Zong Li <[email protected]>
> > ---
> > arch/riscv/mm/init.c | 12 ++++++------
> > 1 file changed, 6 insertions(+), 6 deletions(-)
> >
> > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> > index 69f6678db7f3..965a8cf4829c 100644
> > --- a/arch/riscv/mm/init.c
> > +++ b/arch/riscv/mm/init.c
> > @@ -99,13 +99,13 @@ static void __init setup_initrd(void)
> > pr_info("initrd not found or empty");
> > goto disable;
> > }
> > - if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
> > + if (__pa_symbol(initrd_end) > PFN_PHYS(max_low_pfn)) {
> > pr_err("initrd extends beyond end of memory");
> > goto disable;
> > }
> >
> > size = initrd_end - initrd_start;
> > - memblock_reserve(__pa(initrd_start), size);
> > + memblock_reserve(__pa_symbol(initrd_start), size);
> > initrd_below_start_ok = 1;
> >
> > pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
> > @@ -124,8 +124,8 @@ void __init setup_bootmem(void)
> > {
> > struct memblock_region *reg;
> > phys_addr_t mem_size = 0;
> > - phys_addr_t vmlinux_end = __pa(&_end);
> > - phys_addr_t vmlinux_start = __pa(&_start);
> > + phys_addr_t vmlinux_end = __pa_symbol(&_end);
> > + phys_addr_t vmlinux_start = __pa_symbol(&_start);
> >
> > /* Find the memory region containing the kernel */
> > for_each_memblock(memory, reg) {
> > @@ -445,7 +445,7 @@ static void __init setup_vm_final(void)
> >
> > /* Setup swapper PGD for fixmap */
> > create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
> > - __pa(fixmap_pgd_next),
> > + __pa_symbol(fixmap_pgd_next),
> > PGDIR_SIZE, PAGE_TABLE);
> >
> > /* Map all memory banks */
> > @@ -474,7 +474,7 @@ static void __init setup_vm_final(void)
> > clear_fixmap(FIX_PMD);
> >
> > /* Move to swapper page table */
> > - csr_write(CSR_SATP, PFN_DOWN(__pa(swapper_pg_dir)) | SATP_MODE);
> > + csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
> > local_flush_tlb_all();
> > }
> > #else
> > --
> > 2.24.1
> >
>
> Overall looks good to me.
>
> Reviewed-by: Anup Patel <[email protected]>
>
> I have not tried this patch but can you confirm that
> __pa_symbol() works fine even when DEBUG_VIRTUAL=n

Yes, it works fine through original way when DEBUG_VIRTUAL is not set.

>
> Regards,
> Anup

2020-01-04 00:12:57

by Paul Walmsley

[permalink] [raw]
Subject: Re: [PATCH 2/2] riscv: mm: use __pa_symbol for kernel symbols

On Thu, 2 Jan 2020, Zong Li wrote:

> __pa_symbol is the marcro that should be used for kernel symbols. It is
> also a pre-requisite for DEBUG_VIRTUAL which will do bounds checking.
>
> Signed-off-by: Zong Li <[email protected]>

Thanks, queued for v5.5-rc.


- Paul

2020-01-22 04:40:10

by Paul Walmsley

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: mm: add support for CONFIG_DEBUG_VIRTUAL

On Thu, 2 Jan 2020, Zong Li wrote:

> This patch implements CONFIG_DEBUG_VIRTUAL to do additional checks on
> virt_to_phys and __pa_symbol calls. virt_to_phys used for linear mapping
> check, and __pa_symbol used for kernel symbol check. In current RISC-V,
> kernel image maps to linear mapping area. If CONFIG_DEBUG_VIRTUAL is
> disable, these two functions calculate the offset on the address feded
> directly without any checks.

Reviewed-by: Paul Walmsley <[email protected]>
Tested-by: Paul Walmsley <[email protected]>


- Paul

2020-01-22 21:12:34

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: mm: add support for CONFIG_DEBUG_VIRTUAL

On Wed, 01 Jan 2020 19:12:39 PST (-0800), [email protected] wrote:
> This patch implements CONFIG_DEBUG_VIRTUAL to do additional checks on
> virt_to_phys and __pa_symbol calls. virt_to_phys used for linear mapping
> check, and __pa_symbol used for kernel symbol check. In current RISC-V,
> kernel image maps to linear mapping area. If CONFIG_DEBUG_VIRTUAL is
> disable, these two functions calculate the offset on the address feded
> directly without any checks.
>
> The result of test_debug_virtual as follows:
>
> [ 0.358456] ------------[ cut here ]------------
> [ 0.358738] virt_to_phys used for non-linear address: (____ptrval____) (0xffffffd000000000)
> [ 0.359174] WARNING: CPU: 0 PID: 1 at arch/riscv/mm/physaddr.c:16 __virt_to_phys+0x3c/0x50
> [ 0.359409] Modules linked in:
> [ 0.359630] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.5.0-rc3-00002-g5133c5c0ca13 #57
> [ 0.359861] epc: ffffffe000253d1a ra : ffffffe000253d1a sp : ffffffe03aa87da0
> [ 0.360019] gp : ffffffe000ae03b0 tp : ffffffe03aa88000 t0 : ffffffe000af2660
> [ 0.360175] t1 : 0000000000000064 t2 : 00000000000000b7 s0 : ffffffe03aa87dc0
> [ 0.360330] s1 : ffffffd000000000 a0 : 000000000000004f a1 : 0000000000000000
> [ 0.360492] a2 : 0000000000000000 a3 : 0000000000000000 a4 : ffffffe000a84358
> [ 0.360672] a5 : 0000000000000000 a6 : 0000000000000000 a7 : 0000000000000000
> [ 0.360876] s2 : ffffffe000ae0600 s3 : ffffffe00000fc7c s4 : ffffffe0000224b0
> [ 0.361067] s5 : ffffffe000030890 s6 : ffffffe000022470 s7 : 0000000000000008
> [ 0.361267] s8 : ffffffe0000002c4 s9 : ffffffe000ae0640 s10: ffffffe000ae0630
> [ 0.361453] s11: 0000000000000000 t3 : 0000000000000000 t4 : 000000000001e6d0
> [ 0.361636] t5 : ffffffe000ae0a18 t6 : ffffffe000aee54e
> [ 0.361806] status: 0000000000000120 badaddr: 0000000000000000 cause: 0000000000000003
> [ 0.362056] ---[ end trace aec0bf78d4978122 ]---
> [ 0.362404] PA: 0xfffffff080200000 for VA: 0xffffffd000000000
> [ 0.362607] PA: 0x00000000baddd2d0 for VA: 0xffffffe03abdd2d0
>
> Signed-off-by: Zong Li <[email protected]>

Thanks. Looks like patch 2 landed in Linus' tree as part of 5.5, but this one
didn't. I've taken it on to for-next.

> ---
> arch/riscv/Kconfig | 1 +
> arch/riscv/include/asm/page.h | 16 +++++++++++++--
> arch/riscv/mm/Makefile | 2 ++
> arch/riscv/mm/physaddr.c | 37 +++++++++++++++++++++++++++++++++++
> 4 files changed, 54 insertions(+), 2 deletions(-)
> create mode 100644 arch/riscv/mm/physaddr.c
>
> diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
> index d8efbaa78d67..7f268816be5b 100644
> --- a/arch/riscv/Kconfig
> +++ b/arch/riscv/Kconfig
> @@ -57,6 +57,7 @@ config RISCV
> select GENERIC_ARCH_TOPOLOGY if SMP
> select ARCH_HAS_PTE_SPECIAL
> select ARCH_HAS_MMIOWB
> + select ARCH_HAS_DEBUG_VIRTUAL
> select HAVE_EBPF_JIT if 64BIT
> select EDAC_SUPPORT
> select ARCH_HAS_GIGANTIC_PAGE
> diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h
> index ac699246ae7e..8ca1930caa44 100644
> --- a/arch/riscv/include/asm/page.h
> +++ b/arch/riscv/include/asm/page.h
> @@ -100,8 +100,20 @@ extern unsigned long pfn_base;
> extern unsigned long max_low_pfn;
> extern unsigned long min_low_pfn;
>
> -#define __pa(x) ((unsigned long)(x) - va_pa_offset)
> -#define __va(x) ((void *)((unsigned long) (x) + va_pa_offset))
> +#define __pa_to_va_nodebug(x) ((void *)((unsigned long) (x) + va_pa_offset))
> +#define __va_to_pa_nodebug(x) ((unsigned long)(x) - va_pa_offset)
> +
> +#ifdef CONFIG_DEBUG_VIRTUAL
> +extern phys_addr_t __virt_to_phys(unsigned long x);
> +extern phys_addr_t __phys_addr_symbol(unsigned long x);
> +#else
> +#define __virt_to_phys(x) __va_to_pa_nodebug(x)
> +#define __phys_addr_symbol(x) __va_to_pa_nodebug(x)
> +#endif /* CONFIG_DEBUG_VIRTUAL */
> +
> +#define __pa_symbol(x) __phys_addr_symbol(RELOC_HIDE((unsigned long)(x), 0))
> +#define __pa(x) __virt_to_phys((unsigned long)(x))
> +#define __va(x) ((void *)__pa_to_va_nodebug((phys_addr_t)(x)))
>
> #define phys_to_pfn(phys) (PFN_DOWN(phys))
> #define pfn_to_phys(pfn) (PFN_PHYS(pfn))
> diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile
> index a1bd95c8047a..7de157ecfb77 100644
> --- a/arch/riscv/mm/Makefile
> +++ b/arch/riscv/mm/Makefile
> @@ -15,3 +15,5 @@ ifeq ($(CONFIG_MMU),y)
> obj-$(CONFIG_SMP) += tlbflush.o
> endif
> obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o
> +
> +obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o
> diff --git a/arch/riscv/mm/physaddr.c b/arch/riscv/mm/physaddr.c
> new file mode 100644
> index 000000000000..e8e4dcd39fed
> --- /dev/null
> +++ b/arch/riscv/mm/physaddr.c
> @@ -0,0 +1,37 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/types.h>
> +#include <linux/mmdebug.h>
> +#include <linux/mm.h>
> +#include <asm/page.h>
> +#include <asm/sections.h>
> +
> +phys_addr_t __virt_to_phys(unsigned long x)
> +{
> + phys_addr_t y = x - PAGE_OFFSET;
> +
> + /*
> + * Boundary checking aginst the kernel linear mapping space.
> + */
> + WARN(y >= KERN_VIRT_SIZE,
> + "virt_to_phys used for non-linear address: %pK (%pS)\n",
> + (void *)x, (void *)x);
> +
> + return __va_to_pa_nodebug(x);
> +}
> +EXPORT_SYMBOL(__virt_to_phys);
> +
> +phys_addr_t __phys_addr_symbol(unsigned long x)
> +{
> + unsigned long kernel_start = (unsigned long)PAGE_OFFSET;
> + unsigned long kernel_end = (unsigned long)_end;
> +
> + /*
> + * Boundary checking aginst the kernel image mapping.
> + * __pa_symbol should only be used on kernel symbol addresses.
> + */
> + VIRTUAL_BUG_ON(x < kernel_start || x > kernel_end);
> +
> + return __va_to_pa_nodebug(x);
> +}
> +EXPORT_SYMBOL(__phys_addr_symbol);