[PATCH 2/2] LoongArch: Set FAULT_FLAG_INSTRUCTION in __do_page_fault()

Lorenz Hetterich posted 2 patches 2 weeks, 5 days ago
[PATCH 2/2] LoongArch: Set FAULT_FLAG_INSTRUCTION in __do_page_fault()
Posted by Lorenz Hetterich 2 weeks, 5 days ago
The page-fault handler never sets the FAULT_FLAG_INSTRUCTION flag.

Set FAULT_FLAG_INSTRUCTION for faults related to instruction fetches.

Signed-off-by: Lorenz Hetterich <lorenz.hetterich@cispa.de>
---
 arch/loongarch/include/asm/exception.h | 3 +++
 arch/loongarch/mm/fault.c              | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/arch/loongarch/include/asm/exception.h b/arch/loongarch/include/asm/exception.h
index c6d20736fd92..7305b7065a43 100644
--- a/arch/loongarch/include/asm/exception.h
+++ b/arch/loongarch/include/asm/exception.h
@@ -6,6 +6,9 @@
 #include <asm/ptrace.h>
 #include <linux/kprobes.h>
 
+#define EXC_PIF	3  /* Page Invalid Fetch */
+#define EXC_PNX	6  /* Page Non-eXecutable */
+
 extern void *exception_table[];
 
 void show_registers(struct pt_regs *regs);
diff --git a/arch/loongarch/mm/fault.c b/arch/loongarch/mm/fault.c
index c7703bdec4fc..7cd422c1224e 100644
--- a/arch/loongarch/mm/fault.c
+++ b/arch/loongarch/mm/fault.c
@@ -215,6 +215,9 @@ static void __kprobes __do_page_fault(struct pt_regs *regs,
 	if (user_mode(regs))
 		flags |= FAULT_FLAG_USER;
 
+	if (current->thread.trap_nr == EXC_PIF || current->thread.trap_nr == EXC_PNX)
+		flags |= FAULT_FLAG_INSTRUCTION;
+
 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
 
 	if (!(flags & FAULT_FLAG_USER))
-- 
2.43.0
Re: [PATCH 2/2] LoongArch: Set FAULT_FLAG_INSTRUCTION in __do_page_fault()
Posted by Huacai Chen 2 weeks, 4 days ago
Hi, Lorenz,

On Wed, Mar 18, 2026 at 11:18 PM Lorenz Hetterich
<lorenz.hetterich@cispa.de> wrote:
>
> The page-fault handler never sets the FAULT_FLAG_INSTRUCTION flag.
>
> Set FAULT_FLAG_INSTRUCTION for faults related to instruction fetches.
>
> Signed-off-by: Lorenz Hetterich <lorenz.hetterich@cispa.de>
> ---
>  arch/loongarch/include/asm/exception.h | 3 +++
>  arch/loongarch/mm/fault.c              | 3 +++
>  2 files changed, 6 insertions(+)
>
> diff --git a/arch/loongarch/include/asm/exception.h b/arch/loongarch/include/asm/exception.h
> index c6d20736fd92..7305b7065a43 100644
> --- a/arch/loongarch/include/asm/exception.h
> +++ b/arch/loongarch/include/asm/exception.h
> @@ -6,6 +6,9 @@
>  #include <asm/ptrace.h>
>  #include <linux/kprobes.h>
>
> +#define EXC_PIF        3  /* Page Invalid Fetch */
> +#define EXC_PNX        6  /* Page Non-eXecutable */
Needn't, you can use EXCCODE_TLBI/EXCCODE_TLBNX in loongarch.h directly.


Huacai

> +
>  extern void *exception_table[];
>
>  void show_registers(struct pt_regs *regs);
> diff --git a/arch/loongarch/mm/fault.c b/arch/loongarch/mm/fault.c
> index c7703bdec4fc..7cd422c1224e 100644
> --- a/arch/loongarch/mm/fault.c
> +++ b/arch/loongarch/mm/fault.c
> @@ -215,6 +215,9 @@ static void __kprobes __do_page_fault(struct pt_regs *regs,
>         if (user_mode(regs))
>                 flags |= FAULT_FLAG_USER;
>
> +       if (current->thread.trap_nr == EXC_PIF || current->thread.trap_nr == EXC_PNX)
> +               flags |= FAULT_FLAG_INSTRUCTION;
> +
>         perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
>
>         if (!(flags & FAULT_FLAG_USER))
> --
> 2.43.0
>