1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright (C) 2020, Google LLC.
8 #ifndef _ASM_X86_KFENCE_H
9 #define _ASM_X86_KFENCE_H
13 #include <linux/bug.h>
14 #include <linux/kfence.h>
16 #include <asm/pgalloc.h>
17 #include <asm/pgtable.h>
18 #include <asm/set_memory.h>
19 #include <asm/tlbflush.h>
21 /* Force 4K pages for __kfence_pool. */
22 static inline bool arch_kfence_init_pool(void)
26 for (addr = (unsigned long)__kfence_pool; is_kfence_address((void *)addr);
30 if (!lookup_address(addr, &level))
33 if (level != PG_LEVEL_4K)
34 set_memory_4k(addr, 1);
40 /* Protect the given page and flush TLB. */
41 static inline bool kfence_protect_page(unsigned long addr, bool protect)
44 pte_t *pte = lookup_address(addr, &level);
46 if (WARN_ON(!pte || level != PG_LEVEL_4K))
50 * We need to avoid IPIs, as we may get KFENCE allocations or faults
51 * with interrupts disabled. Therefore, the below is best-effort, and
52 * does not flush TLBs on all CPUs. We can tolerate some inaccuracy;
53 * lazy fault handling takes care of faults after the page is PRESENT.
57 set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
59 set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
62 * Flush this CPU's TLB, assuming whoever did the allocation/free is
63 * likely to continue running on this CPU.
66 flush_tlb_one_kernel(addr);
73 #endif /* _ASM_X86_KFENCE_H */