Skip to content

Commit bfa7965

Browse files
Zhenhua Huangwilldeacon
authored andcommitted
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 <[email protected]> Signed-off-by: Zhenhua Huang <[email protected]> Reviewed-by: Kefeng Wang <[email protected]> Reviewed-by: Marco Elver <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent e8d018d commit bfa7965

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

arch/arm64/include/asm/kfence.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@ static inline bool kfence_protect_page(unsigned long addr, bool protect)
1919
return true;
2020
}
2121

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+
2232
#endif /* __ASM_KFENCE_H */

arch/arm64/mm/mmu.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/mm.h>
2525
#include <linux/vmalloc.h>
2626
#include <linux/set_memory.h>
27+
#include <linux/kfence.h>
2728

2829
#include <asm/barrier.h>
2930
#include <asm/cputype.h>
@@ -38,6 +39,7 @@
3839
#include <asm/ptdump.h>
3940
#include <asm/tlbflush.h>
4041
#include <asm/pgalloc.h>
42+
#include <asm/kfence.h>
4143

4244
#define NO_BLOCK_MAPPINGS BIT(0)
4345
#define NO_CONT_MAPPINGS BIT(1)
@@ -525,12 +527,67 @@ static int __init enable_crash_mem_map(char *arg)
525527
}
526528
early_param("crashkernel", enable_crash_mem_map);
527529

530+
#ifdef CONFIG_KFENCE
531+
532+
bool __ro_after_init kfence_early_init = !!CONFIG_KFENCE_SAMPLE_INTERVAL;
533+
534+
/* early_param() will be parsed before map_mem() below. */
535+
static int __init parse_kfence_early_init(char *arg)
536+
{
537+
int val;
538+
539+
if (get_option(&arg, &val))
540+
kfence_early_init = !!val;
541+
return 0;
542+
}
543+
early_param("kfence.sample_interval", parse_kfence_early_init);
544+
545+
static phys_addr_t __init arm64_kfence_alloc_pool(void)
546+
{
547+
phys_addr_t kfence_pool;
548+
549+
if (!kfence_early_init)
550+
return 0;
551+
552+
kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
553+
if (!kfence_pool) {
554+
pr_err("failed to allocate kfence pool\n");
555+
kfence_early_init = false;
556+
return 0;
557+
}
558+
559+
/* Temporarily mark as NOMAP. */
560+
memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE);
561+
562+
return kfence_pool;
563+
}
564+
565+
static void __init arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp)
566+
{
567+
if (!kfence_pool)
568+
return;
569+
570+
/* KFENCE pool needs page-level mapping. */
571+
__map_memblock(pgdp, kfence_pool, kfence_pool + KFENCE_POOL_SIZE,
572+
pgprot_tagged(PAGE_KERNEL),
573+
NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
574+
memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE);
575+
__kfence_pool = phys_to_virt(kfence_pool);
576+
}
577+
#else /* CONFIG_KFENCE */
578+
579+
static inline phys_addr_t arm64_kfence_alloc_pool(void) { return 0; }
580+
static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) { }
581+
582+
#endif /* CONFIG_KFENCE */
583+
528584
static void __init map_mem(pgd_t *pgdp)
529585
{
530586
static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);
531587
phys_addr_t kernel_start = __pa_symbol(_stext);
532588
phys_addr_t kernel_end = __pa_symbol(__init_begin);
533589
phys_addr_t start, end;
590+
phys_addr_t early_kfence_pool;
534591
int flags = NO_EXEC_MAPPINGS;
535592
u64 i;
536593

@@ -543,6 +600,8 @@ static void __init map_mem(pgd_t *pgdp)
543600
*/
544601
BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end));
545602

603+
early_kfence_pool = arm64_kfence_alloc_pool();
604+
546605
if (can_set_direct_map())
547606
flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
548607

@@ -608,6 +667,8 @@ static void __init map_mem(pgd_t *pgdp)
608667
}
609668
}
610669
#endif
670+
671+
arm64_kfence_map_pool(early_kfence_pool, pgdp);
611672
}
612673

613674
void mark_rodata_ro(void)

arch/arm64/mm/pageattr.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <asm/cacheflush.h>
1212
#include <asm/set_memory.h>
1313
#include <asm/tlbflush.h>
14+
#include <asm/kfence.h>
1415

1516
struct page_change_data {
1617
pgprot_t set_mask;
@@ -22,12 +23,14 @@ bool rodata_full __ro_after_init = IS_ENABLED(CONFIG_RODATA_FULL_DEFAULT_ENABLED
2223
bool can_set_direct_map(void)
2324
{
2425
/*
25-
* rodata_full, DEBUG_PAGEALLOC and KFENCE require linear map to be
26+
* rodata_full and DEBUG_PAGEALLOC require linear map to be
2627
* mapped at page granularity, so that it is possible to
2728
* protect/unprotect single pages.
29+
*
30+
* KFENCE pool requires page-granular mapping if initialized late.
2831
*/
2932
return (rodata_enabled && rodata_full) || debug_pagealloc_enabled() ||
30-
IS_ENABLED(CONFIG_KFENCE);
33+
arm64_kfence_can_set_direct_map();
3134
}
3235

3336
static int change_page_range(pte_t *ptep, unsigned long addr, void *data)

mm/kfence/core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,10 @@ void __init kfence_alloc_pool(void)
814814
if (!kfence_sample_interval)
815815
return;
816816

817+
/* if the pool has already been initialized by arch, skip the below. */
818+
if (__kfence_pool)
819+
return;
820+
817821
__kfence_pool = memblock_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
818822

819823
if (!__kfence_pool)

0 commit comments

Comments
 (0)