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
11 #include <linux/bug.h>
12 #include <linux/kfence.h>
14 #include <asm/pgalloc.h>
15 #include <asm/pgtable.h>
16 #include <asm/set_memory.h>
17 #include <asm/tlbflush.h>
19 /* Force 4K pages for __kfence_pool. */
20 static inline bool arch_kfence_init_pool(void)
24 for (addr = (unsigned long)__kfence_pool; is_kfence_address((void *)addr);
28 if (!lookup_address(addr, &level))
31 if (level != PG_LEVEL_4K)
32 set_memory_4k(addr, 1);
38 /* Protect the given page and flush TLB. */
39 static inline bool kfence_protect_page(unsigned long addr, bool protect)
42 pte_t *pte = lookup_address(addr, &level);
44 if (WARN_ON(!pte || level != PG_LEVEL_4K))
48 * We need to avoid IPIs, as we may get KFENCE allocations or faults
49 * with interrupts disabled. Therefore, the below is best-effort, and
50 * does not flush TLBs on all CPUs. We can tolerate some inaccuracy;
51 * lazy fault handling takes care of faults after the page is PRESENT.
55 set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
57 set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
60 * Flush this CPU's TLB, assuming whoever did the allocation/free is
61 * likely to continue running on this CPU.
64 flush_tlb_one_kernel(addr);
69 #endif /* _ASM_X86_KFENCE_H */