Skip to content

Commit 243b72a

Browse files
aryabininKAGA-KOKO
authored andcommitted
x86/mm/ptdump: Optimize check for W+X mappings for CONFIG_KASAN=y
Enabling both DEBUG_WX=y and KASAN=y options significantly increases boot time (dozens of seconds at least). KASAN fills kernel page tables with repeated values to map several TBs of the virtual memory to the single kasan_zero_page: kasan_zero_pud -> kasan_zero_pmd-> kasan_zero_pte-> kasan_zero_page So, the page table walker used to find W+X mapping check the same kasan_zero_p?d page table entries a lot more than once. With patch pud walker will skip the pud if it has the same value as the previous one . Skipping done iff we search for W+X mappings, so this optimization won't affect the page table dump via debugfs. This dropped time spend in W+X check from ~30 sec to reasonable 0.1 sec: Before: [ 4.579991] Freeing unused kernel memory: 1000K [ 35.257523] x86/mm: Checked W+X mappings: passed, no W+X pages found. After: [ 5.138756] Freeing unused kernel memory: 1000K [ 5.266496] x86/mm: Checked W+X mappings: passed, no W+X pages found. Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com> Reviewed-by: Alexander Potapenko <glider@google.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: kasan-dev@googlegroups.com Cc: Tobias Regnery <tobias.regnery@gmail.com> Cc: Dmitry Vyukov <dvyukov@google.com> Link: http://lkml.kernel.org/r/20170214100839.17186-1-aryabinin@virtuozzo.com Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 5b1ad68 commit 243b72a

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

arch/x86/mm/dump_pagetables.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,18 +327,31 @@ static void walk_pmd_level(struct seq_file *m, struct pg_state *st, pud_t addr,
327327

328328
#if PTRS_PER_PUD > 1
329329

330+
/*
331+
* This is an optimization for CONFIG_DEBUG_WX=y + CONFIG_KASAN=y
332+
* KASAN fills page tables with the same values. Since there is no
333+
* point in checking page table more than once we just skip repeated
334+
* entries. This saves us dozens of seconds during boot.
335+
*/
336+
static bool pud_already_checked(pud_t *prev_pud, pud_t *pud, bool checkwx)
337+
{
338+
return checkwx && prev_pud && (pud_val(*prev_pud) == pud_val(*pud));
339+
}
340+
330341
static void walk_pud_level(struct seq_file *m, struct pg_state *st, pgd_t addr,
331342
unsigned long P)
332343
{
333344
int i;
334345
pud_t *start;
335346
pgprotval_t prot;
347+
pud_t *prev_pud = NULL;
336348

337349
start = (pud_t *) pgd_page_vaddr(addr);
338350

339351
for (i = 0; i < PTRS_PER_PUD; i++) {
340352
st->current_address = normalize_addr(P + i * PUD_LEVEL_MULT);
341-
if (!pud_none(*start)) {
353+
if (!pud_none(*start) &&
354+
!pud_already_checked(prev_pud, start, st->check_wx)) {
342355
if (pud_large(*start) || !pud_present(*start)) {
343356
prot = pud_flags(*start);
344357
note_page(m, st, __pgprot(prot), 2);
@@ -349,6 +362,7 @@ static void walk_pud_level(struct seq_file *m, struct pg_state *st, pgd_t addr,
349362
} else
350363
note_page(m, st, __pgprot(0), 2);
351364

365+
prev_pud = start;
352366
start++;
353367
}
354368
}

0 commit comments

Comments
 (0)