Skip to content

Commit 47513f2

Browse files
Liu Shixinpalmer-dabbelt
Liu Shixin
authored andcommitted
riscv: Enable KFENCE for riscv64
Add architecture specific implementation details for KFENCE and enable KFENCE for the riscv64 architecture. In particular, this implements the required interface in <asm/kfence.h>. KFENCE requires that attributes for pages from its memory pool can individually be set. Therefore, force the kfence pool to be mapped at page granularity. Testing this patch using the testcases in kfence_test.c and all passed. Signed-off-by: Liu Shixin <[email protected]> Acked-by: Marco Elver <[email protected]> Reviewed-by: Kefeng Wang <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent f627476 commit 47513f2

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

arch/riscv/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ config RISCV
6464
select HAVE_ARCH_JUMP_LABEL_RELATIVE
6565
select HAVE_ARCH_KASAN if MMU && 64BIT
6666
select HAVE_ARCH_KASAN_VMALLOC if MMU && 64BIT
67+
select HAVE_ARCH_KFENCE if MMU && 64BIT
6768
select HAVE_ARCH_KGDB
6869
select HAVE_ARCH_KGDB_QXFER_PKT
6970
select HAVE_ARCH_MMAP_RND_BITS if MMU

arch/riscv/include/asm/kfence.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#ifndef _ASM_RISCV_KFENCE_H
4+
#define _ASM_RISCV_KFENCE_H
5+
6+
#include <linux/kfence.h>
7+
#include <linux/pfn.h>
8+
#include <asm-generic/pgalloc.h>
9+
#include <asm/pgtable.h>
10+
11+
static inline int split_pmd_page(unsigned long addr)
12+
{
13+
int i;
14+
unsigned long pfn = PFN_DOWN(__pa((addr & PMD_MASK)));
15+
pmd_t *pmd = pmd_off_k(addr);
16+
pte_t *pte = pte_alloc_one_kernel(&init_mm);
17+
18+
if (!pte)
19+
return -ENOMEM;
20+
21+
for (i = 0; i < PTRS_PER_PTE; i++)
22+
set_pte(pte + i, pfn_pte(pfn + i, PAGE_KERNEL));
23+
set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa(pte)), PAGE_TABLE));
24+
25+
flush_tlb_kernel_range(addr, addr + PMD_SIZE);
26+
return 0;
27+
}
28+
29+
static inline bool arch_kfence_init_pool(void)
30+
{
31+
int ret;
32+
unsigned long addr;
33+
pmd_t *pmd;
34+
35+
for (addr = (unsigned long)__kfence_pool; is_kfence_address((void *)addr);
36+
addr += PAGE_SIZE) {
37+
pmd = pmd_off_k(addr);
38+
39+
if (pmd_leaf(*pmd)) {
40+
ret = split_pmd_page(addr);
41+
if (ret)
42+
return false;
43+
}
44+
}
45+
46+
return true;
47+
}
48+
49+
static inline bool kfence_protect_page(unsigned long addr, bool protect)
50+
{
51+
pte_t *pte = virt_to_kpte(addr);
52+
53+
if (protect)
54+
set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
55+
else
56+
set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
57+
58+
flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
59+
60+
return true;
61+
}
62+
63+
#endif /* _ASM_RISCV_KFENCE_H */

arch/riscv/mm/fault.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/signal.h>
1515
#include <linux/uaccess.h>
1616
#include <linux/kprobes.h>
17+
#include <linux/kfence.h>
1718

1819
#include <asm/ptrace.h>
1920
#include <asm/tlbflush.h>
@@ -45,7 +46,15 @@ static inline void no_context(struct pt_regs *regs, unsigned long addr)
4546
* Oops. The kernel tried to access some bad page. We'll have to
4647
* terminate things with extreme prejudice.
4748
*/
48-
msg = (addr < PAGE_SIZE) ? "NULL pointer dereference" : "paging request";
49+
if (addr < PAGE_SIZE)
50+
msg = "NULL pointer dereference";
51+
else {
52+
if (kfence_handle_page_fault(addr, regs->cause == EXC_STORE_PAGE_FAULT, regs))
53+
return;
54+
55+
msg = "paging request";
56+
}
57+
4958
die_kernel_fault(msg, addr, regs);
5059
}
5160

0 commit comments

Comments
 (0)