Skip to content

Commit 3f148f3

Browse files
aryabininhansendc
authored andcommitted
x86/kasan: Map shadow for percpu pages on demand
KASAN maps shadow for the entire CPU-entry-area: [CPU_ENTRY_AREA_BASE, CPU_ENTRY_AREA_BASE + CPU_ENTRY_AREA_MAP_SIZE] This will explode once the per-cpu entry areas are randomized since it will increase CPU_ENTRY_AREA_MAP_SIZE to 512 GB and KASAN fails to allocate shadow for such big area. Fix this by allocating KASAN shadow only for really used cpu entry area addresses mapped by cea_map_percpu_pages() Thanks to the 0day folks for finding and reporting this to be an issue. [ dhansen: tweak changelog since this will get committed before peterz's actual cpu-entry-area randomization ] Signed-off-by: Andrey Ryabinin <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Tested-by: Yujie Liu <[email protected]> Cc: kernel test robot <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 30a0b95 commit 3f148f3

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

arch/x86/include/asm/kasan.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
#ifdef CONFIG_KASAN
2929
void __init kasan_early_init(void);
3030
void __init kasan_init(void);
31+
void __init kasan_populate_shadow_for_vaddr(void *va, size_t size, int nid);
3132
#else
3233
static inline void kasan_early_init(void) { }
3334
static inline void kasan_init(void) { }
35+
static inline void kasan_populate_shadow_for_vaddr(void *va, size_t size,
36+
int nid) { }
3437
#endif
3538

3639
#endif

arch/x86/mm/cpu_entry_area.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <asm/cpu_entry_area.h>
1010
#include <asm/fixmap.h>
1111
#include <asm/desc.h>
12+
#include <asm/kasan.h>
1213

1314
static DEFINE_PER_CPU_PAGE_ALIGNED(struct entry_stack_page, entry_stack_storage);
1415

@@ -53,8 +54,13 @@ void cea_set_pte(void *cea_vaddr, phys_addr_t pa, pgprot_t flags)
5354
static void __init
5455
cea_map_percpu_pages(void *cea_vaddr, void *ptr, int pages, pgprot_t prot)
5556
{
57+
phys_addr_t pa = per_cpu_ptr_to_phys(ptr);
58+
59+
kasan_populate_shadow_for_vaddr(cea_vaddr, pages * PAGE_SIZE,
60+
early_pfn_to_nid(PFN_DOWN(pa)));
61+
5662
for ( ; pages; pages--, cea_vaddr+= PAGE_SIZE, ptr += PAGE_SIZE)
57-
cea_set_pte(cea_vaddr, per_cpu_ptr_to_phys(ptr), prot);
63+
cea_set_pte(cea_vaddr, pa, prot);
5864
}
5965

6066
static void __init percpu_setup_debug_store(unsigned int cpu)

arch/x86/mm/kasan_init_64.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,18 @@ void __init kasan_early_init(void)
316316
kasan_map_early_shadow(init_top_pgt);
317317
}
318318

319+
void __init kasan_populate_shadow_for_vaddr(void *va, size_t size, int nid)
320+
{
321+
unsigned long shadow_start, shadow_end;
322+
323+
shadow_start = (unsigned long)kasan_mem_to_shadow(va);
324+
shadow_start = round_down(shadow_start, PAGE_SIZE);
325+
shadow_end = (unsigned long)kasan_mem_to_shadow(va + size);
326+
shadow_end = round_up(shadow_end, PAGE_SIZE);
327+
328+
kasan_populate_shadow(shadow_start, shadow_end, nid);
329+
}
330+
319331
void __init kasan_init(void)
320332
{
321333
int i;
@@ -393,9 +405,6 @@ void __init kasan_init(void)
393405
kasan_mem_to_shadow((void *)VMALLOC_END + 1),
394406
shadow_cpu_entry_begin);
395407

396-
kasan_populate_shadow((unsigned long)shadow_cpu_entry_begin,
397-
(unsigned long)shadow_cpu_entry_end, 0);
398-
399408
kasan_populate_early_shadow(shadow_cpu_entry_end,
400409
kasan_mem_to_shadow((void *)__START_KERNEL_map));
401410

0 commit comments

Comments
 (0)