riscv: fix KFENCE pool initialization
Subsystems (3)
cover riscv: fix KFENCE pool initialization
From: Jackie Liu <[email protected]>
RISC-V KFENCE pool reservation failures can prevent the system from booting. These fixes allow boot to continue with KFENCE disabled and prepare late pools with the page mappings KFENCE requires.
The first patch fixes P4D leaf detection in the linear-map splitter. The second uses that splitter to prepare late KFENCE pools, while retaining the early PTE mapping.
Built RV64 kernels and RV32 pageattr.o. QEMU KFENCE KUnit covered boot and runtime enabling, pool reservation failure, and page-table allocation failures.
Thanks,
Jackie
Jackie Liu (2):
riscv: Recognize P4D leaves when splitting the linear mapping
riscv: Prepare late KFENCE pools with PTE mappings
arch/riscv/include/asm/kfence.h | 4 +- arch/riscv/include/asm/set_memory.h | 3 ++ arch/riscv/mm/init.c | 23 +++++++++-- arch/riscv/mm/pageattr.c | 59 ++++++++++++++++++++--------- 4 files changed, 67 insertions(+), 22 deletions(-)
base-commit: c339f4010dea8b0d754f84ace70cd1b089878ef8
PATCH 1/2 riscv: Recognize P4D leaves when splitting the linear mapping
From: Jackie Liu <[email protected]>
The linear mapping can contain P4D leaves on Sv57, but p4d_leaf() uses the generic definition which always returns false. Splitting a partial P4D range therefore treats the mapped physical memory as a PUD table instead of allocating and populating that table.
Check the hardware leaf bits in the linear-map splitter. Only do so when the PUD level is present: on Sv39 the folded P4D entry may contain a PUD leaf, which must be handled by the PUD splitter.
Keep this check local to kernel mappings. Defining p4d_leaf() globally would conflict with the GUP assertions that userspace P4D leaves are unsupported.
--- arch/riscv/mm/pageattr.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/riscv/mm/pageattr.c +2 -1
@@ -197,7 +197,8 @@ static int __split_linear_mapping_p4d(pgd_t *pgdp, vaddr <= (vaddr & P4D_MASK) && end >= next)continue ; -if (p4d_leaf (p4dp_get (p4dp))) { +/* p4d_leaf() does not cover kernel-only leaf mappings. */ +if (pgtable_l4_enabled && (p4d_val (p4dp_get (p4dp)) & _PAGE_LEAF)) {struct page *pud_page;unsigned long pfn =_p4d_pfn (p4dp_get (p4dp));pgprot_t prot =__pgprot (p4d_val (p4dp_get (p4dp)) & ~_PAGE_PFN_MASK);
Signature · 1 line
2.43.0
PATCH 2/2 riscv: Prepare late KFENCE pools with PTE mappings
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
RISC-V preallocates a PTE-mapped KFENCE pool, but pools allocated after boot may use huge mappings. arch_kfence_init_pool() leaves those mappings unchanged, while kfence_protect_page() requires PTEs.
Use set_memory_4k() to prepare each pool. Add a force_pte mode to the linear-map splitter so fully covered huge mappings are split as well. Preserve permissions and flush the TLB even if splitting fails partway.
Keep the early PTE mapping to avoid boot-time page-table allocations. Release that pool when boot parameters disable sampling, so late enabling initializes a fresh pool. If reservation fails, continue booting with sampling disabled to avoid an unprepared fallback pool, while allowing a later retry.
--- arch/riscv/include/asm/kfence.h | 4 ++- arch/riscv/include/asm/set_memory.h | 3 ++ arch/riscv/mm/init.c | 23 +++++++++--- arch/riscv/mm/pageattr.c | 56 ++++++++++++++++++++--------- 4 files changed, 65 insertions(+), 21 deletions(-)
arch/riscv/include/asm/kfence.h +3 -1
@@ -8,10 +8,12 @@#include <asm-generic/pgalloc.h> #include <asm/cacheflush.h> #include <asm/pgtable.h> +#include <asm/set_memory.h> static inline bool arch_kfence_init_pool (void ) { -return true; +return !set_memory_4k ((unsigned long )__kfence_pool, +KFENCE_POOL_SIZE /PAGE_SIZE ); }static inline bool kfence_protect_page (unsigned long addr,bool protect)
arch/riscv/include/asm/set_memory.h +3 -0
@@ -11,6 +11,9 @@* Functions to change memory attributes. */ #ifdef CONFIG_MMU +#ifdef CONFIG_64BIT +int set_memory_4k (unsigned long addr,int numpages); +#endif int set_memory_ro (unsigned long addr,int numpages);int set_memory_rw (unsigned long addr,int numpages);int set_memory_x (unsigned long addr,int numpages);
arch/riscv/mm/init.c +19 -4
@@ -173,6 +173,16 @@ void __init arch_mm_preinit(void)BUG_ON (!mem_map);#endif /* CONFIG_FLATMEM */ +#ifdef CONFIG_KFENCE +/* Boot parameters have been parsed, and memblock can still free the pool. */ +if (!__kfence_pool) { + kfence_sample_interval =0 ; + }else if (!kfence_sample_interval) { +memblock_free (__kfence_pool,KFENCE_POOL_SIZE ); + __kfence_pool =NULL ; + } +#endif +if (IS_ENABLED (CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC ) && !swiotlb && dma_cache_alignment !=1 ) {/*
@@ -1230,10 +1240,12 @@ static void __init create_linear_mapping_page_table(void)* for this region. */ kfence_pool =memblock_phys_alloc (KFENCE_POOL_SIZE ,PAGE_SIZE ); -BUG_ON (!kfence_pool); - -memblock_mark_nomap (kfence_pool,KFENCE_POOL_SIZE ); - __kfence_pool =__va (kfence_pool); +if (kfence_pool) { +memblock_mark_nomap (kfence_pool,KFENCE_POOL_SIZE ); + __kfence_pool =__va (kfence_pool); + }else { +pr_err ("failed to allocate kfence pool\n" ); + }#endif /* Map all memory banks in the linear mapping */
@@ -1254,6 +1266,9 @@ static void __init create_linear_mapping_page_table(void)#endif #ifdef CONFIG_KFENCE +if (!kfence_pool) +return ; +create_linear_mapping_range (kfence_pool, kfence_pool +KFENCE_POOL_SIZE ,PAGE_SIZE ,NULL );memblock_clear_nomap (kfence_pool,KFENCE_POOL_SIZE );
arch/riscv/mm/pageattr.c +40 -16
136 lines not on this page · raw message
Signature · 1 line
2.43.0