blob: 05b48b33baf0b811b2788750d4539f18c9bd2255 [file] [log] [blame]
Alexander Potapenko2eac3972021-01-22 09:18:35 +00001/* SPDX-License-Identifier: GPL-2.0 */
Marco Elverea098a12021-01-22 09:18:35 +00002/*
3 * x86 KFENCE support.
4 *
5 * Copyright (C) 2020, Google LLC.
6 */
Alexander Potapenko2eac3972021-01-22 09:18:35 +00007
8#ifndef _ASM_X86_KFENCE_H
9#define _ASM_X86_KFENCE_H
10
11#include <linux/bug.h>
12#include <linux/kfence.h>
13
14#include <asm/pgalloc.h>
15#include <asm/pgtable.h>
16#include <asm/set_memory.h>
17#include <asm/tlbflush.h>
18
Alexander Potapenko2eac3972021-01-22 09:18:35 +000019/* Force 4K pages for __kfence_pool. */
20static inline bool arch_kfence_init_pool(void)
21{
22 unsigned long addr;
23
24 for (addr = (unsigned long)__kfence_pool; is_kfence_address((void *)addr);
25 addr += PAGE_SIZE) {
26 unsigned int level;
27
28 if (!lookup_address(addr, &level))
29 return false;
30
31 if (level != PG_LEVEL_4K)
32 set_memory_4k(addr, 1);
33 }
34
35 return true;
36}
37
38/* Protect the given page and flush TLB. */
39static inline bool kfence_protect_page(unsigned long addr, bool protect)
40{
41 unsigned int level;
42 pte_t *pte = lookup_address(addr, &level);
43
44 if (WARN_ON(!pte || level != PG_LEVEL_4K))
45 return false;
46
47 /*
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.
52 */
53
54 if (protect)
55 set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
56 else
57 set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
58
Marco Elveraed2e272021-04-09 13:27:44 -070059 /*
60 * Flush this CPU's TLB, assuming whoever did the allocation/free is
61 * likely to continue running on this CPU.
62 */
63 preempt_disable();
Alexander Potapenko2eac3972021-01-22 09:18:35 +000064 flush_tlb_one_kernel(addr);
Marco Elveraed2e272021-04-09 13:27:44 -070065 preempt_enable();
Alexander Potapenko2eac3972021-01-22 09:18:35 +000066 return true;
67}
68
69#endif /* _ASM_X86_KFENCE_H */