Skip to content

Commit f754f27

Browse files
Xu Lupalmer-dabbelt
authored andcommitted
riscv: mm: Fix the out of bound issue of vmemmap address
In sparse vmemmap model, the virtual address of vmemmap is calculated as: ((struct page *)VMEMMAP_START - (phys_ram_base >> PAGE_SHIFT)). And the struct page's va can be calculated with an offset: (vmemmap + (pfn)). However, when initializing struct pages, kernel actually starts from the first page from the same section that phys_ram_base belongs to. If the first page's physical address is not (phys_ram_base >> PAGE_SHIFT), then we get an va below VMEMMAP_START when calculating va for it's struct page. For example, if phys_ram_base starts from 0x82000000 with pfn 0x82000, the first page in the same section is actually pfn 0x80000. During init_unavailable_range(), we will initialize struct page for pfn 0x80000 with virtual address ((struct page *)VMEMMAP_START - 0x2000), which is below VMEMMAP_START as well as PCI_IO_END. This commit fixes this bug by introducing a new variable 'vmemmap_start_pfn' which is aligned with memory section size and using it to calculate vmemmap address instead of phys_ram_base. Fixes: a11dd49 ("riscv: Sparse-Memory/vmemmap out-of-bounds fix") Signed-off-by: Xu Lu <[email protected]> Reviewed-by: Alexandre Ghiti <[email protected]> Tested-by: Björn Töpel <[email protected]> Reviewed-by: Björn Töpel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 7e25044 commit f754f27

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

arch/riscv/include/asm/page.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ struct kernel_mapping {
122122

123123
extern struct kernel_mapping kernel_map;
124124
extern phys_addr_t phys_ram_base;
125+
extern unsigned long vmemmap_start_pfn;
125126

126127
#define is_kernel_mapping(x) \
127128
((x) >= kernel_map.virt_addr && (x) < (kernel_map.virt_addr + kernel_map.size))

arch/riscv/include/asm/pgtable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
* Define vmemmap for pfn_to_page & page_to_pfn calls. Needed if kernel
8888
* is configured with CONFIG_SPARSEMEM_VMEMMAP enabled.
8989
*/
90-
#define vmemmap ((struct page *)VMEMMAP_START - (phys_ram_base >> PAGE_SHIFT))
90+
#define vmemmap ((struct page *)VMEMMAP_START - vmemmap_start_pfn)
9191

9292
#define PCI_IO_SIZE SZ_16M
9393
#define PCI_IO_END VMEMMAP_START

arch/riscv/mm/init.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <asm/pgtable.h>
3434
#include <asm/sections.h>
3535
#include <asm/soc.h>
36+
#include <asm/sparsemem.h>
3637
#include <asm/tlbflush.h>
3738

3839
#include "../kernel/head.h"
@@ -62,6 +63,13 @@ EXPORT_SYMBOL(pgtable_l5_enabled);
6263
phys_addr_t phys_ram_base __ro_after_init;
6364
EXPORT_SYMBOL(phys_ram_base);
6465

66+
#ifdef CONFIG_SPARSEMEM_VMEMMAP
67+
#define VMEMMAP_ADDR_ALIGN (1ULL << SECTION_SIZE_BITS)
68+
69+
unsigned long vmemmap_start_pfn __ro_after_init;
70+
EXPORT_SYMBOL(vmemmap_start_pfn);
71+
#endif
72+
6573
unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
6674
__page_aligned_bss;
6775
EXPORT_SYMBOL(empty_zero_page);
@@ -240,8 +248,12 @@ static void __init setup_bootmem(void)
240248
* Make sure we align the start of the memory on a PMD boundary so that
241249
* at worst, we map the linear mapping with PMD mappings.
242250
*/
243-
if (!IS_ENABLED(CONFIG_XIP_KERNEL))
251+
if (!IS_ENABLED(CONFIG_XIP_KERNEL)) {
244252
phys_ram_base = memblock_start_of_DRAM() & PMD_MASK;
253+
#ifdef CONFIG_SPARSEMEM_VMEMMAP
254+
vmemmap_start_pfn = round_down(phys_ram_base, VMEMMAP_ADDR_ALIGN) >> PAGE_SHIFT;
255+
#endif
256+
}
245257

246258
/*
247259
* In 64-bit, any use of __va/__pa before this point is wrong as we
@@ -1101,6 +1113,9 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
11011113
kernel_map.xiprom_sz = (uintptr_t)(&_exiprom) - (uintptr_t)(&_xiprom);
11021114

11031115
phys_ram_base = CONFIG_PHYS_RAM_BASE;
1116+
#ifdef CONFIG_SPARSEMEM_VMEMMAP
1117+
vmemmap_start_pfn = round_down(phys_ram_base, VMEMMAP_ADDR_ALIGN) >> PAGE_SHIFT;
1118+
#endif
11041119
kernel_map.phys_addr = (uintptr_t)CONFIG_PHYS_RAM_BASE;
11051120
kernel_map.size = (uintptr_t)(&_end) - (uintptr_t)(&_start);
11061121

0 commit comments

Comments
 (0)