Skip to content

Commit d7fbcf4

Browse files
AlexGhitipalmer-dabbelt
authored andcommitted
riscv: Improve kasan population by using hugepages when possible
The kasan functions that populates the shadow regions used to allocate them page by page and did not take advantage of hugepages, so fix this by trying to allocate hugepages of 1GB and fallback to 2MB hugepages or 4K pages in case it fails. This reduces the page table memory consumption and improves TLB usage, as shown below: Before this patch: ---[ Kasan shadow start ]--- 0xffffffc000000000-0xffffffc400000000 0x00000000818ef000 16G PTE . A . . . . R V 0xffffffc400000000-0xffffffc447fc0000 0x00000002b7f4f000 1179392K PTE D A . . . W R V 0xffffffc480000000-0xffffffc800000000 0x00000000818ef000 14G PTE . A . . . . R V ---[ Kasan shadow end ]--- After this patch: ---[ Kasan shadow start ]--- 0xffffffc000000000-0xffffffc400000000 0x00000000818ef000 16G PTE . A . . . . R V 0xffffffc400000000-0xffffffc440000000 0x0000000240000000 1G PGD D A . . . W R V 0xffffffc440000000-0xffffffc447e00000 0x00000002b7e00000 126M PMD D A . . . W R V 0xffffffc447e00000-0xffffffc447fc0000 0x00000002b818f000 1792K PTE D A . . . W R V 0xffffffc480000000-0xffffffc800000000 0x00000000818ef000 14G PTE . A . . . . R V ---[ Kasan shadow end ]--- Signed-off-by: Alexandre Ghiti <alex@ghiti.fr> Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
1 parent d127c19 commit d7fbcf4

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

arch/riscv/mm/kasan_init.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ static void kasan_populate_pmd(pgd_t *pgd, unsigned long vaddr, unsigned long en
9696

9797
do {
9898
next = pmd_addr_end(vaddr, end);
99+
100+
if (pmd_none(*pmdp) && IS_ALIGNED(vaddr, PMD_SIZE) && (next - vaddr) >= PMD_SIZE) {
101+
phys_addr = memblock_phys_alloc(PMD_SIZE, PMD_SIZE);
102+
if (phys_addr) {
103+
set_pmd(pmdp, pfn_pmd(PFN_DOWN(phys_addr), PAGE_KERNEL));
104+
continue;
105+
}
106+
}
107+
99108
kasan_populate_pte(pmdp, vaddr, next);
100109
} while (pmdp++, vaddr = next, vaddr != end);
101110

@@ -116,6 +125,21 @@ static void kasan_populate_pgd(unsigned long vaddr, unsigned long end)
116125

117126
do {
118127
next = pgd_addr_end(vaddr, end);
128+
129+
/*
130+
* pgdp can't be none since kasan_early_init initialized all KASAN
131+
* shadow region with kasan_early_shadow_pmd: if this is stillthe case,
132+
* that means we can try to allocate a hugepage as a replacement.
133+
*/
134+
if (pgd_page_vaddr(*pgdp) == (unsigned long)lm_alias(kasan_early_shadow_pmd) &&
135+
IS_ALIGNED(vaddr, PGDIR_SIZE) && (next - vaddr) >= PGDIR_SIZE) {
136+
phys_addr = memblock_phys_alloc(PGDIR_SIZE, PGDIR_SIZE);
137+
if (phys_addr) {
138+
set_pgd(pgdp, pfn_pgd(PFN_DOWN(phys_addr), PAGE_KERNEL));
139+
continue;
140+
}
141+
}
142+
119143
kasan_populate_pmd(pgdp, vaddr, next);
120144
} while (pgdp++, vaddr = next, vaddr != end);
121145
}

0 commit comments

Comments
 (0)