Skip to contentSkip to conversation

ARM: Support KFENCE feature

PATCH Superseded Kefeng Wang 4 participants 10 messages Nov 3, 2021 – Nov 4, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover ARM: Support KFENCE feature

This patchset supports Kfence feature, tested the kfence_test on ARM QEMU with or without ARM_LPAE and all passed.

V2:
- drop patch4 in v1, which is used a new way to skip kfence test see commit c40c6e593bf9 ("kfence: test: fail fast if disabled at boot")
- fix some issue about NO MMU
- drop useless set_memory_valid() under no mmu
- fix implicit declaration of function ‘is_write_fault’ if no mmu
- make KFENCE depends on !XIP_KERNEL, no tested with xip

v1:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

Kefeng Wang (3):
ARM: mm: Provide set_memory_valid()
ARM: mm: Provide is_write_fault()
ARM: Support KFENCE for ARM

 arch/arm/Kconfig                  |  1 +
 arch/arm/include/asm/kfence.h     | 53 +++++++++++++++++++++++++++++++
 arch/arm/include/asm/set_memory.h |  1 +
 arch/arm/mm/fault.c               | 16 ++++++++--
 arch/arm/mm/pageattr.c            | 42 ++++++++++++++++++------
 5 files changed, 100 insertions(+), 13 deletions(-)
 create mode 100644 arch/arm/include/asm/kfence.h
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 1/3 ARM: mm: Provide set_memory_valid()

This function validates and invalidates PTE entries, it will be used in the later patch.

---
 arch/arm/include/asm/set_memory.h |  1 +
 arch/arm/mm/pageattr.c            | 42 +++++++++++++++++++++++--------
 2 files changed, 33 insertions(+), 10 deletions(-)
arch/arm/include/asm/set_memory.h +1 -0
index ec17fc0fda7a..0211b9c5b14d 100644
@@ -11,6 +11,7 @@ int set_memory_ro(unsigned long addr, int numpages);
 int set_memory_rw(unsigned long addr, int numpages);
 int set_memory_x(unsigned long addr, int numpages);
 int set_memory_nx(unsigned long addr, int numpages);
+int set_memory_valid(unsigned long addr, int numpages, int enable);
 #else
 static inline int set_memory_ro(unsigned long addr, int numpages) { return 0; }
 static inline int set_memory_rw(unsigned long addr, int numpages) { return 0; }
arch/arm/mm/pageattr.c +32 -10

63 lines not on this page · raw message

Signature · 1 line
2.26.2
#2 lore raw

PATCH v2 2/3 ARM: mm: Provide is_write_fault()

In reply to Kefeng Wang

The function will check whether the fault is caused by a write access.

---
 arch/arm/mm/fault.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm/mm/fault.c +6 -1
index bc8779d54a64..f7ab6dabe89f 100644
@@ -207,6 +207,11 @@ static inline bool is_permission_fault(unsigned int fsr)
 	return false;
 }
 
+static inline bool is_write_fault(unsigned int fsr)
+{
+	return (fsr & FSR_WRITE) && !(fsr & FSR_CM);
+}
+
 static vm_fault_t __kprobes
 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int flags,
 		unsigned long vma_flags, struct pt_regs *regs)
@@ -261,7 +266,7 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
 	if (user_mode(regs))
 		flags |= FAULT_FLAG_USER;
 
-	if ((fsr & FSR_WRITE) && !(fsr & FSR_CM)) {
+	if (is_write_fault(fsr)) {
 		flags |= FAULT_FLAG_WRITE;
 		vm_flags = VM_WRITE;
 	}
Signature · 1 line
2.26.2
#3 lore raw

PATCH v2 3/3 ARM: Support KFENCE for ARM

In reply to Kefeng Wang

Add architecture specific implementation details for KFENCE and enable KFENCE on ARM. 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 with or without ARM_LPAE.

---
 arch/arm/Kconfig              |  1 +
 arch/arm/include/asm/kfence.h | 53 +++++++++++++++++++++++++++++++++++
 arch/arm/mm/fault.c           | 19 ++++++++-----
 3 files changed, 66 insertions(+), 7 deletions(-)
 create mode 100644 arch/arm/include/asm/kfence.h
arch/arm/Kconfig +1 -0
index b9f72337224c..6d1f6f48995c 100644
@@ -69,6 +69,7 @@ config ARM
 	select HAVE_ARCH_AUDITSYSCALL if AEABI && !OABI_COMPAT
 	select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
 	select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU
+	select HAVE_ARCH_KFENCE if MMU && !XIP_KERNEL
 	select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32 && MMU
 	select HAVE_ARCH_KASAN if MMU && !XIP_KERNEL
 	select HAVE_ARCH_MMAP_RND_BITS if MMU
arch/arm/include/asm/kfence.h new file +53 -0

53 lines not on this page · raw message

arch/arm/mm/fault.c +12 -7
index f7ab6dabe89f..49148b675b43 100644
@@ -17,6 +17,7 @@
 #include <linux/sched/debug.h>
 #include <linux/highmem.h>
 #include <linux/perf_event.h>
+#include <linux/kfence.h>
 
 #include <asm/system_misc.h>
 #include <asm/system_info.h>
@@ -99,6 +100,11 @@ void show_pte(const char *lvl, struct mm_struct *mm, unsigned long addr)
 { }
 #endif					/* CONFIG_MMU */
 
+static inline bool is_write_fault(unsigned int fsr)
+{
+	return (fsr & FSR_WRITE) && !(fsr & FSR_CM);
+}
+
 static void die_kernel_fault(const char *msg, struct mm_struct *mm,
 			     unsigned long addr, unsigned int fsr,
 			     struct pt_regs *regs)
@@ -131,10 +137,14 @@ __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
 	/*
 	 * No handler, we'll have to terminate things with extreme prejudice.
 	 */
-	if (addr < PAGE_SIZE)
+	if (addr < PAGE_SIZE) {
 		msg = "NULL pointer dereference";
-	else
+	} else {
+		if (kfence_handle_page_fault(addr, is_write_fault(fsr), regs))
+			return;
+
 		msg = "paging request";
+	}
 
 	die_kernel_fault(msg, mm, addr, fsr, regs);
 }
@@ -207,11 +217,6 @@ static inline bool is_permission_fault(unsigned int fsr)
 	return false;
 }
 
-static inline bool is_write_fault(unsigned int fsr)
-{
-	return (fsr & FSR_WRITE) && !(fsr & FSR_CM);
-}
-
 static vm_fault_t __kprobes
 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int flags,
 		unsigned long vma_flags, struct pt_regs *regs)
Signature · 1 line
2.26.2
#4 lore raw

Re: [PATCH v2 3/3] ARM: Support KFENCE for ARM

Quoted 104 lines · Kefeng Wang

Add architecture specific implementation details for KFENCE and enable KFENCE on ARM. 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 with or without ARM_LPAE.

---
 arch/arm/Kconfig              |  1 +
 arch/arm/include/asm/kfence.h | 53 +++++++++++++++++++++++++++++++++++
 arch/arm/mm/fault.c           | 19 ++++++++-----
 3 files changed, 66 insertions(+), 7 deletions(-)
 create mode 100644 arch/arm/include/asm/kfence.h
arch/arm/Kconfig +1 -0
index b9f72337224c..6d1f6f48995c 100644
@@ -69,6 +69,7 @@ config ARM
        select HAVE_ARCH_AUDITSYSCALL if AEABI && !OABI_COMPAT
        select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
        select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU
+       select HAVE_ARCH_KFENCE if MMU && !XIP_KERNEL
        select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32 && MMU
        select HAVE_ARCH_KASAN if MMU && !XIP_KERNEL
        select HAVE_ARCH_MMAP_RND_BITS if MMU
arch/arm/include/asm/kfence.h new file +53 -0
new file mode 100644
index 000000000000..7980d0f2271f
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __ASM_ARM_KFENCE_H
+#define __ASM_ARM_KFENCE_H
+
+#include <linux/kfence.h>
+
+#include <asm/pgalloc.h>
+#include <asm/set_memory.h>
+
+static inline int split_pmd_page(pmd_t *pmd, unsigned long addr)
+{
+       int i;
+       unsigned long pfn = PFN_DOWN(__pa(addr));
+       pte_t *pte = pte_alloc_one_kernel(&init_mm);
+
+       if (!pte)
+               return -ENOMEM;
+
+       for (i = 0; i < PTRS_PER_PTE; i++)
+               set_pte_ext(pte + i, pfn_pte(pfn + i, PAGE_KERNEL), 0);
+       pmd_populate_kernel(&init_mm, pmd, pte);
+
+       flush_tlb_kernel_range(addr, addr + PMD_SIZE);
+       return 0;
+}
+
+static inline bool arch_kfence_init_pool(void)
+{
+       unsigned long addr;
+       pmd_t *pmd;
+
+       for (addr = (unsigned long)__kfence_pool; is_kfence_address((void *)addr);
+            addr += PAGE_SIZE) {
+               pmd = pmd_off_k(addr);
+
+               if (pmd_leaf(*pmd)) {
+                       if (split_pmd_page(pmd, addr & PMD_MASK))
+                               return false;
+               }
+       }
+
+       return true;
+}
+
+static inline bool kfence_protect_page(unsigned long addr, bool protect)
+{
+       set_memory_valid(addr, 1, !protect);
+
+       return true;
+}
+
+#endif /* __ASM_ARM_KFENCE_H */
arch/arm/mm/fault.c +1 -0
index f7ab6dabe89f..49148b675b43 100644
@@ -17,6 +17,7 @@
 #include <linux/sched/debug.h>
 #include <linux/highmem.h>
 #include <linux/perf_event.h>
+#include <linux/kfence.h>
 
 #include <asm/system_misc.h>
 #include <asm/system_info.h>

arch/arm/mm/fault.c void show_pte(const char *lvl, struct mm_struct *mm, unsigned long addr) · line 106

Quoted 7 lines
arch/arm/mm/fault.c +4 -0
@@ -99,6 +100,11 @@ void show_pte(const char *lvl, struct mm_struct *mm, unsigned long addr)
 { }
 #endif                                 /* CONFIG_MMU */
 
+static inline bool is_write_fault(unsigned int fsr)
+{
+       return (fsr & FSR_WRITE) && !(fsr & FSR_CM);
+}

Please don't increase the diff by moving the code around. Consider putting is_write_fault() in the right place in "ARM: mm: Provide is_write_fault()" instead.

#5 lore raw

Re: [PATCH v2 3/3] ARM: Support KFENCE for ARM

Quoted 14 lines · Alexander Potapenko
Quoted 13 lines · Kefeng Wang

Add architecture specific implementation details for KFENCE and enable KFENCE on ARM. 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 with or without ARM_LPAE.

...

Quoted 16 lines
Quoted 16 lines
arch/arm/mm/fault.c +1 -0
index f7ab6dabe89f..49148b675b43 100644
@@ -17,6 +17,7 @@
  #include <linux/sched/debug.h>
  #include <linux/highmem.h>
  #include <linux/perf_event.h>
+#include <linux/kfence.h>
 
  #include <asm/system_misc.h>
  #include <asm/system_info.h>
@@ -99,6 +100,11 @@ void show_pte(const char *lvl, struct mm_struct *mm, unsigned long addr)
  { }
  #endif                                 /* CONFIG_MMU */
 
Quoted 8 lines

arch/arm/mm/fault.c void show_pte(const char *lvl, struct mm_struct *mm, unsigned long addr) · line 106

Quoted 4 lines
arch/arm/mm/fault.c +4 -0
+static inline bool is_write_fault(unsigned int fsr)
+{
+       return (fsr & FSR_WRITE) && !(fsr & FSR_CM);
+}

Please don't increase the diff by moving the code around. Consider putting is_write_fault() in the right place in "ARM: mm: Provide is_write_fault()" instead.

Sure. Let's wait some time to see whether there are any other comments, and resend, thanks.

Quoted 2 lines

.

maintainer Acked-by on the v2 series
#6 lore raw
In reply to Kefeng Wang
Quoted 12 lines · Kefeng Wang

This patchset supports Kfence feature, tested the kfence_test on ARM QEMU with or without ARM_LPAE and all passed.

V2:
- drop patch4 in v1, which is used a new way to skip kfence test see commit c40c6e593bf9 ("kfence: test: fail fast if disabled at boot")
- fix some issue about NO MMU
- drop useless set_memory_valid() under no mmu
- fix implicit declaration of function ‘is_write_fault’ if no mmu
- make KFENCE depends on !XIP_KERNEL, no tested with xip

Quoted 7 lines

v1:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

Kefeng Wang (3):
ARM: mm: Provide set_memory_valid()
ARM: mm: Provide is_write_fault()
ARM: Support KFENCE for ARM

Looks good to me.

Quoted 11 lines
 arch/arm/Kconfig                  |  1 +
 arch/arm/include/asm/kfence.h     | 53 +++++++++++++++++++++++++++++++
 arch/arm/include/asm/set_memory.h |  1 +
 arch/arm/mm/fault.c               | 16 ++++++++--
 arch/arm/mm/pageattr.c            | 42 ++++++++++++++++++------
 5 files changed, 100 insertions(+), 13 deletions(-)
 create mode 100644 arch/arm/include/asm/kfence.h

--
2.26.2

#7 lore raw
Quoted 16 lines · Marco Elver
Quoted 15 lines · Kefeng Wang

This patchset supports Kfence feature, tested the kfence_test on ARM QEMU with or without ARM_LPAE and all passed.

V2:
- drop patch4 in v1, which is used a new way to skip kfence test see commit c40c6e593bf9 ("kfence: test: fail fast if disabled at boot")
- fix some issue about NO MMU
- drop useless set_memory_valid() under no mmu
- fix implicit declaration of function ‘is_write_fault’ if no mmu
- make KFENCE depends on !XIP_KERNEL, no tested with xip

v1:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

Quoted 8 lines
Quoted 4 lines

Kefeng Wang (3):
ARM: mm: Provide set_memory_valid()
ARM: mm: Provide is_write_fault()
ARM: Support KFENCE for ARM

Looks good to me.

Thanks Marco.

#8 lore raw
In reply to Kefeng Wang

The ARM tree is closed; we're in the mainline merge window. Please resend after -rc1 is released.

Quoted 31 lines · Kefeng Wang

This patchset supports Kfence feature, tested the kfence_test on ARM QEMU with or without ARM_LPAE and all passed.

V2:
- drop patch4 in v1, which is used a new way to skip kfence test see commit c40c6e593bf9 ("kfence: test: fail fast if disabled at boot")
- fix some issue about NO MMU
- drop useless set_memory_valid() under no mmu
- fix implicit declaration of function ‘is_write_fault’ if no mmu
- make KFENCE depends on !XIP_KERNEL, no tested with xip

v1:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

Kefeng Wang (3):
ARM: mm: Provide set_memory_valid()
ARM: mm: Provide is_write_fault()
ARM: Support KFENCE for ARM

 arch/arm/Kconfig                  |  1 +
 arch/arm/include/asm/kfence.h     | 53 +++++++++++++++++++++++++++++++
 arch/arm/include/asm/set_memory.h |  1 +
 arch/arm/mm/fault.c               | 16 ++++++++--
 arch/arm/mm/pageattr.c            | 42 ++++++++++++++++++------
 5 files changed, 100 insertions(+), 13 deletions(-)
 create mode 100644 arch/arm/include/asm/kfence.h
Signature · 1 line
2.26.2
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
#9 lore raw
Quoted 2 lines · Russell King (Oracle)

The ARM tree is closed; we're in the mainline merge window. Please resend after -rc1 is released.

Got it, will do.

Quoted 34 lines · Kefeng Wang
Quoted 31 lines

This patchset supports Kfence feature, tested the kfence_test on ARM QEMU with or without ARM_LPAE and all passed.

V2:
- drop patch4 in v1, which is used a new way to skip kfence test see commit c40c6e593bf9 ("kfence: test: fail fast if disabled at boot")
- fix some issue about NO MMU
- drop useless set_memory_valid() under no mmu
- fix implicit declaration of function ‘is_write_fault’ if no mmu
- make KFENCE depends on !XIP_KERNEL, no tested with xip

v1:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

Kefeng Wang (3):
ARM: mm: Provide set_memory_valid()
ARM: mm: Provide is_write_fault()
ARM: Support KFENCE for ARM

  arch/arm/Kconfig                  |  1 +
  arch/arm/include/asm/kfence.h     | 53 +++++++++++++++++++++++++++++++
  arch/arm/include/asm/set_memory.h |  1 +
  arch/arm/mm/fault.c               | 16 ++++++++--
  arch/arm/mm/pageattr.c            | 42 ++++++++++++++++++------
  5 files changed, 100 insertions(+), 13 deletions(-)

create mode 100644 arch/arm/include/asm/kfence.h

Signature · 1 line
2.26.2