Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm,kfence: decouple kfence from page granularity mapping judgement

Kfence only needs its pool to be mapped as page granularity, if it is
inited early. Previous judgement was a bit over protected. From [1], Mark
suggested to "just map the KFENCE region a page granularity". So I
decouple it from judgement and do page granularity mapping for kfence
pool only. Need to be noticed that late init of kfence pool still requires
page granularity mapping.

Page granularity mapping in theory cost more(2M per 1GB) memory on arm64
platform. Like what I've tested on QEMU(emulated 1GB RAM) with
gki_defconfig, also turning off rodata protection:
Before:
[root@liebao ]# cat /proc/meminfo
MemTotal: 999484 kB
After:
[root@liebao ]# cat /proc/meminfo
MemTotal: 1001480 kB

To implement this, also relocate the kfence pool allocation before the
linear mapping setting up, arm64_kfence_alloc_pool is to allocate phys
addr, __kfence_pool is to be set after linear mapping set up.

LINK: [1] https://lore.kernel.org/linux-arm-kernel/Y+IsdrvDNILA59UN@FVFF77S0Q05N/
Suggested-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Zhenhua Huang <quic_zhenhuah@quicinc.com>
Reviewed-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Reviewed-by: Marco Elver <elver@google.com>
Link: https://lore.kernel.org/r/1679066974-690-1-git-send-email-quic_zhenhuah@quicinc.com
Signed-off-by: Will Deacon <will@kernel.org>

authored by

Zhenhua Huang and committed by
Will Deacon
bfa7965b e8d018dd

+80 -2
+10
arch/arm64/include/asm/kfence.h
··· 19 19 return true; 20 20 } 21 21 22 + #ifdef CONFIG_KFENCE 23 + extern bool kfence_early_init; 24 + static inline bool arm64_kfence_can_set_direct_map(void) 25 + { 26 + return !kfence_early_init; 27 + } 28 + #else /* CONFIG_KFENCE */ 29 + static inline bool arm64_kfence_can_set_direct_map(void) { return false; } 30 + #endif /* CONFIG_KFENCE */ 31 + 22 32 #endif /* __ASM_KFENCE_H */
+61
arch/arm64/mm/mmu.c
··· 24 24 #include <linux/mm.h> 25 25 #include <linux/vmalloc.h> 26 26 #include <linux/set_memory.h> 27 + #include <linux/kfence.h> 27 28 28 29 #include <asm/barrier.h> 29 30 #include <asm/cputype.h> ··· 39 38 #include <asm/ptdump.h> 40 39 #include <asm/tlbflush.h> 41 40 #include <asm/pgalloc.h> 41 + #include <asm/kfence.h> 42 42 43 43 #define NO_BLOCK_MAPPINGS BIT(0) 44 44 #define NO_CONT_MAPPINGS BIT(1) ··· 527 525 } 528 526 early_param("crashkernel", enable_crash_mem_map); 529 527 528 + #ifdef CONFIG_KFENCE 529 + 530 + bool __ro_after_init kfence_early_init = !!CONFIG_KFENCE_SAMPLE_INTERVAL; 531 + 532 + /* early_param() will be parsed before map_mem() below. */ 533 + static int __init parse_kfence_early_init(char *arg) 534 + { 535 + int val; 536 + 537 + if (get_option(&arg, &val)) 538 + kfence_early_init = !!val; 539 + return 0; 540 + } 541 + early_param("kfence.sample_interval", parse_kfence_early_init); 542 + 543 + static phys_addr_t __init arm64_kfence_alloc_pool(void) 544 + { 545 + phys_addr_t kfence_pool; 546 + 547 + if (!kfence_early_init) 548 + return 0; 549 + 550 + kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE); 551 + if (!kfence_pool) { 552 + pr_err("failed to allocate kfence pool\n"); 553 + kfence_early_init = false; 554 + return 0; 555 + } 556 + 557 + /* Temporarily mark as NOMAP. */ 558 + memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE); 559 + 560 + return kfence_pool; 561 + } 562 + 563 + static void __init arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) 564 + { 565 + if (!kfence_pool) 566 + return; 567 + 568 + /* KFENCE pool needs page-level mapping. */ 569 + __map_memblock(pgdp, kfence_pool, kfence_pool + KFENCE_POOL_SIZE, 570 + pgprot_tagged(PAGE_KERNEL), 571 + NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS); 572 + memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE); 573 + __kfence_pool = phys_to_virt(kfence_pool); 574 + } 575 + #else /* CONFIG_KFENCE */ 576 + 577 + static inline phys_addr_t arm64_kfence_alloc_pool(void) { return 0; } 578 + static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) { } 579 + 580 + #endif /* CONFIG_KFENCE */ 581 + 530 582 static void __init map_mem(pgd_t *pgdp) 531 583 { 532 584 static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN); 533 585 phys_addr_t kernel_start = __pa_symbol(_stext); 534 586 phys_addr_t kernel_end = __pa_symbol(__init_begin); 535 587 phys_addr_t start, end; 588 + phys_addr_t early_kfence_pool; 536 589 int flags = NO_EXEC_MAPPINGS; 537 590 u64 i; 538 591 ··· 599 542 * which case it is guaranteed to be true for all other levels as well. 600 543 */ 601 544 BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end)); 545 + 546 + early_kfence_pool = arm64_kfence_alloc_pool(); 602 547 603 548 if (can_set_direct_map()) 604 549 flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; ··· 667 608 } 668 609 } 669 610 #endif 611 + 612 + arm64_kfence_map_pool(early_kfence_pool, pgdp); 670 613 } 671 614 672 615 void mark_rodata_ro(void)
+5 -2
arch/arm64/mm/pageattr.c
··· 11 11 #include <asm/cacheflush.h> 12 12 #include <asm/set_memory.h> 13 13 #include <asm/tlbflush.h> 14 + #include <asm/kfence.h> 14 15 15 16 struct page_change_data { 16 17 pgprot_t set_mask; ··· 23 22 bool can_set_direct_map(void) 24 23 { 25 24 /* 26 - * rodata_full, DEBUG_PAGEALLOC and KFENCE require linear map to be 25 + * rodata_full and DEBUG_PAGEALLOC require linear map to be 27 26 * mapped at page granularity, so that it is possible to 28 27 * protect/unprotect single pages. 28 + * 29 + * KFENCE pool requires page-granular mapping if initialized late. 29 30 */ 30 31 return (rodata_enabled && rodata_full) || debug_pagealloc_enabled() || 31 - IS_ENABLED(CONFIG_KFENCE); 32 + arm64_kfence_can_set_direct_map(); 32 33 } 33 34 34 35 static int change_page_range(pte_t *ptep, unsigned long addr, void *data)
+4
mm/kfence/core.c
··· 814 814 if (!kfence_sample_interval) 815 815 return; 816 816 817 + /* if the pool has already been initialized by arch, skip the below. */ 818 + if (__kfence_pool) 819 + return; 820 + 817 821 __kfence_pool = memblock_alloc(KFENCE_POOL_SIZE, PAGE_SIZE); 818 822 819 823 if (!__kfence_pool)