Skip to contentSkip to conversation

riscv: Enable KFENCE for riscv64

PATCH In review Liu Shixin 3 participants 4 messages Jun 15, 2021 – Jul 1, 2021

Subsystems (2)
lore
Share & tools
lore

Liu Shixin

author
#0 lore raw

PATCH v2 1/1 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.

---
v1->v2: Add the acked-by and Reviewed-by.

 arch/riscv/Kconfig              |  1 +
 arch/riscv/include/asm/kfence.h | 63 +++++++++++++++++++++++++++++++++
 arch/riscv/mm/fault.c           | 11 +++++-
 3 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/include/asm/kfence.h
arch/riscv/Kconfig +1 -0
index 4c0bfb2569e9..1421da5ef1a2 100644
@@ -65,6 +65,7 @@ config RISCV
 	select HAVE_ARCH_JUMP_LABEL_RELATIVE
 	select HAVE_ARCH_KASAN if MMU && 64BIT
 	select HAVE_ARCH_KASAN_VMALLOC if MMU && 64BIT
+	select HAVE_ARCH_KFENCE if MMU && 64BIT
 	select HAVE_ARCH_KGDB
 	select HAVE_ARCH_KGDB_QXFER_PKT
 	select HAVE_ARCH_MMAP_RND_BITS if MMU
arch/riscv/include/asm/kfence.h new file +63 -0

63 lines not on this page · raw message

arch/riscv/mm/fault.c +10 -1
index 096463cc6fff..aa08dd2f8fae 100644
@@ -14,6 +14,7 @@
 #include <linux/signal.h>
 #include <linux/uaccess.h>
 #include <linux/kprobes.h>
+#include <linux/kfence.h>
 
 #include <asm/ptrace.h>
 #include <asm/tlbflush.h>
@@ -45,7 +46,15 @@ static inline void no_context(struct pt_regs *regs, unsigned long addr)
 	 * Oops. The kernel tried to access some bad page. We'll have to
 	 * terminate things with extreme prejudice.
 	 */
-	msg = (addr < PAGE_SIZE) ? "NULL pointer dereference" : "paging request";
+	if (addr < PAGE_SIZE)
+		msg = "NULL pointer dereference";
+	else {
+		if (kfence_handle_page_fault(addr, regs->cause == EXC_STORE_PAGE_FAULT, regs))
+			return;
+
+		msg = "paging request";
+	}
+
 	die_kernel_fault(msg, addr, regs);
 }
 
Signature · 1 line
2.18.0.huawei.25
maintainer
#1 lore raw
Quoted 8 lines · Liu Shixin

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.

Quoted 5 lines

Testing this patch using the testcases in kfence_test.c and all passed.

I can't see this in -next yet. It would be nice if riscv64 could get KFENCE support.

Thanks,
-- Marco

#2 lore raw
Quoted 9 lines · On Wed, 16 Jun 2021 02:11:53 PDT (-0700), [email protected] wrote:
Quoted 8 lines · Liu Shixin

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.

Quoted 8 lines
Quoted 5 lines

Testing this patch using the testcases in kfence_test.c and all passed.

I can't see this in -next yet. It would be nice if riscv64 could get KFENCE support.

Thanks, this is on for-next. I'm just doing a boot test with CONFIG_KFENCE=y (and whatever that turns on for defconfig), let me know if there's anything more interesting to test on the KFENCE side of things.

maintainer
#3 lore raw
Quoted 16 lines · Palmer Dabbelt' via kasan-dev
Quoted 15 lines · On Wed, 16 Jun 2021 02:11:53 PDT (-0700), [email protected] wrote:
Quoted 13 lines · Liu Shixin

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.

Quoted 7 lines
Quoted 2 lines

I can't see this in -next yet. It would be nice if riscv64 could get KFENCE support.

Thanks, this is on for-next. I'm just doing a boot test with CONFIG_KFENCE=y (and whatever that turns on for defconfig), let me know if there's anything more interesting to test on the KFENCE side of things.

To test if everything still works, CONFIG_KFENCE_KUNIT_TEST=y (requires CONFIG_KUNIT=y) will run the KFENCE test suite on boot.

Thanks,
-- Marco