Skip to content

Commit f7ae023

Browse files
AlexGhitipalmer-dabbelt
authored andcommitted
riscv: Move KASAN mapping next to the kernel mapping
Now that KASAN_SHADOW_OFFSET is defined at compile time as a config, this value must remain constant whatever the size of the virtual address space, which is only possible by pushing this region at the end of the address space next to the kernel mapping. Signed-off-by: Alexandre Ghiti <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent db1503d commit f7ae023

File tree

6 files changed

+38
-31
lines changed

6 files changed

+38
-31
lines changed

Documentation/riscv/vm-layout.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ RISC-V Linux Kernel SV39
4747
| Kernel-space virtual memory, shared between all processes:
4848
____________________________________________________________|___________________________________________________________
4949
| | | |
50-
ffffffc000000000 | -256 GB | ffffffc7ffffffff | 32 GB | kasan
51-
ffffffcefee00000 | -196 GB | ffffffcefeffffff | 2 MB | fixmap
52-
ffffffceff000000 | -196 GB | ffffffceffffffff | 16 MB | PCI io
53-
ffffffcf00000000 | -196 GB | ffffffcfffffffff | 4 GB | vmemmap
54-
ffffffd000000000 | -192 GB | ffffffdfffffffff | 64 GB | vmalloc/ioremap space
55-
ffffffe000000000 | -128 GB | ffffffff7fffffff | 124 GB | direct mapping of all physical memory
50+
ffffffc6fee00000 | -228 GB | ffffffc6feffffff | 2 MB | fixmap
51+
ffffffc6ff000000 | -228 GB | ffffffc6ffffffff | 16 MB | PCI io
52+
ffffffc700000000 | -228 GB | ffffffc7ffffffff | 4 GB | vmemmap
53+
ffffffc800000000 | -224 GB | ffffffd7ffffffff | 64 GB | vmalloc/ioremap space
54+
ffffffd800000000 | -160 GB | fffffff6ffffffff | 124 GB | direct mapping of all physical memory
55+
fffffff700000000 | -36 GB | fffffffeffffffff | 32 GB | kasan
5656
__________________|____________|__________________|_________|____________________________________________________________
5757
|
5858
|

arch/riscv/Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ config PAGE_OFFSET
160160
hex
161161
default 0xC0000000 if 32BIT
162162
default 0x80000000 if 64BIT && !MMU
163-
default 0xffffffe000000000 if 64BIT
163+
default 0xffffffd800000000 if 64BIT
164164

165165
config KASAN_SHADOW_OFFSET
166166
hex
167167
depends on KASAN_GENERIC
168-
default 0xdfffffc800000000 if 64BIT
168+
default 0xdfffffff00000000 if 64BIT
169169
default 0xffffffff if 32BIT
170170

171171
config ARCH_FLATMEM_ENABLE

arch/riscv/include/asm/kasan.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#define KASAN_SHADOW_SCALE_SHIFT 3
2929

3030
#define KASAN_SHADOW_SIZE (UL(1) << ((CONFIG_VA_BITS - 1) - KASAN_SHADOW_SCALE_SHIFT))
31-
#define KASAN_SHADOW_START KERN_VIRT_START
32-
#define KASAN_SHADOW_END (KASAN_SHADOW_START + KASAN_SHADOW_SIZE)
31+
#define KASAN_SHADOW_START (KASAN_SHADOW_END - KASAN_SHADOW_SIZE)
32+
#define KASAN_SHADOW_END MODULES_LOWEST_VADDR
3333
#define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
3434

3535
void kasan_init(void);

arch/riscv/include/asm/page.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
*/
3434
#define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL)
3535

36-
#define KERN_VIRT_SIZE (-PAGE_OFFSET)
37-
3836
#ifndef __ASSEMBLY__
3937

4038
#define clear_page(pgaddr) memset((pgaddr), 0, PAGE_SIZE)

arch/riscv/include/asm/pgtable.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
#define KERNEL_LINK_ADDR PAGE_OFFSET
2525
#endif
2626

27+
/* Number of entries in the page global directory */
28+
#define PTRS_PER_PGD (PAGE_SIZE / sizeof(pgd_t))
29+
/* Number of entries in the page table */
30+
#define PTRS_PER_PTE (PAGE_SIZE / sizeof(pte_t))
31+
32+
/*
33+
* Half of the kernel address space (half of the entries of the page global
34+
* directory) is for the direct mapping.
35+
*/
36+
#define KERN_VIRT_SIZE ((PTRS_PER_PGD / 2 * PGDIR_SIZE) / 2)
37+
2738
#define VMALLOC_SIZE (KERN_VIRT_SIZE >> 1)
2839
#define VMALLOC_END (PAGE_OFFSET - 1)
2940
#define VMALLOC_START (PAGE_OFFSET - VMALLOC_SIZE)
@@ -39,8 +50,10 @@
3950

4051
/* Modules always live before the kernel */
4152
#ifdef CONFIG_64BIT
42-
#define MODULES_VADDR (PFN_ALIGN((unsigned long)&_end) - SZ_2G)
43-
#define MODULES_END (PFN_ALIGN((unsigned long)&_start))
53+
/* This is used to define the end of the KASAN shadow region */
54+
#define MODULES_LOWEST_VADDR (KERNEL_LINK_ADDR - SZ_2G)
55+
#define MODULES_VADDR (PFN_ALIGN((unsigned long)&_end) - SZ_2G)
56+
#define MODULES_END (PFN_ALIGN((unsigned long)&_start))
4457
#endif
4558

4659
/*
@@ -108,11 +121,6 @@
108121
#endif /* CONFIG_XIP_KERNEL */
109122

110123
#ifdef CONFIG_MMU
111-
/* Number of entries in the page global directory */
112-
#define PTRS_PER_PGD (PAGE_SIZE / sizeof(pgd_t))
113-
/* Number of entries in the page table */
114-
#define PTRS_PER_PTE (PAGE_SIZE / sizeof(pte_t))
115-
116124
/* Number of PGD entries that a user-mode program can use */
117125
#define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE)
118126

arch/riscv/mm/init.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ static void __init print_vm_layout(void)
103103
print_mlm("lowmem", (unsigned long)PAGE_OFFSET,
104104
(unsigned long)high_memory);
105105
#ifdef CONFIG_64BIT
106+
#ifdef CONFIG_KASAN
107+
print_mlm("kasan", KASAN_SHADOW_START, KASAN_SHADOW_END);
108+
#endif
106109
print_mlm("kernel", (unsigned long)KERNEL_LINK_ADDR,
107110
(unsigned long)ADDRESS_SPACE_END);
108111
#endif
@@ -130,18 +133,8 @@ void __init mem_init(void)
130133
print_vm_layout();
131134
}
132135

133-
/*
134-
* The default maximal physical memory size is -PAGE_OFFSET for 32-bit kernel,
135-
* whereas for 64-bit kernel, the end of the virtual address space is occupied
136-
* by the modules/BPF/kernel mappings which reduces the available size of the
137-
* linear mapping.
138-
* Limit the memory size via mem.
139-
*/
140-
#ifdef CONFIG_64BIT
141-
static phys_addr_t memory_limit = -PAGE_OFFSET - SZ_4G;
142-
#else
143-
static phys_addr_t memory_limit = -PAGE_OFFSET;
144-
#endif
136+
/* Limit the memory size via mem. */
137+
static phys_addr_t memory_limit;
145138

146139
static int __init early_mem(char *p)
147140
{
@@ -612,6 +605,14 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
612605

613606
riscv_pfn_base = PFN_DOWN(kernel_map.phys_addr);
614607

608+
/*
609+
* The default maximal physical memory size is KERN_VIRT_SIZE for 32-bit
610+
* kernel, whereas for 64-bit kernel, the end of the virtual address
611+
* space is occupied by the modules/BPF/kernel mappings which reduces
612+
* the available size of the linear mapping.
613+
*/
614+
memory_limit = KERN_VIRT_SIZE - (IS_ENABLED(CONFIG_64BIT) ? SZ_4G : 0);
615+
615616
/* Sanity check alignment and size */
616617
BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
617618
BUG_ON((kernel_map.phys_addr % PMD_SIZE) != 0);

0 commit comments

Comments
 (0)