[PATCH] riscv: add tracepoints for page fault

From: Zhu Hengbo
Date: Mon Jul 15 2024 - 06:14:51 EST


Introduce page_fault_user and page_fault_kernel for riscv page fault.
Help to get more detail information when page fault happen.

---
Simple test go below:

root@riscv-ubuntu2204 ~ # bin/perf list | grep exceptions
exceptions:page_fault_kernel [Tracepoint event]
exceptions:page_fault_user [Tracepoint event]

root@riscv-ubuntu2204 ~ # bin/perf record -e exceptions:page_fault_kernel -e exceptions:page_fault_user
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.091 MB perf.data (19 samples) ]

perf report tracepoint:

perf 826 [007] 894.795520: exceptions:page_fault_user: user page fault, address=0x7fff95af4400 cause=0xd
perf 826 [007] 894.795970: exceptions:page_fault_user: user page fault, address=0x7fff95a73400 cause=0xd
perf 826 [007] 894.796738: exceptions:page_fault_user: user page fault, address=0x7fff959f2400 cause=0xd
perf 826 [007] 894.797088: exceptions:page_fault_user: user page fault, address=0x7fff95971400 cause=0xd
perf 826 [007] 894.797273: exceptions:page_fault_user: user page fault, address=0x7fff958f0400 cause=0xd
perf 826 [007] 894.797445: exceptions:page_fault_user: user page fault, address=0x7fff9586f400 cause=0xd
perf 826 [007] 894.797998: exceptions:page_fault_kernel: kernel page fault, address=0x7fff95870000 cause=0xd

Signed-off-by: Zhu Hengbo <zhuhengbo@xxxxxxxxxxx>
---
arch/riscv/include/asm/trace/exceptions.h | 60 +++++++++++++++++++++++
arch/riscv/mm/fault.c | 15 ++++++
2 files changed, 75 insertions(+)
create mode 100644 arch/riscv/include/asm/trace/exceptions.h

diff --git a/arch/riscv/include/asm/trace/exceptions.h b/arch/riscv/include/asm/trace/exceptions.h
new file mode 100644
index 000000000000..a9a68d471703
--- /dev/null
+++ b/arch/riscv/include/asm/trace/exceptions.h
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Tracepoints for RISC-V exceptions
+ *
+ * Copyright (C) 2024 ISCAS. All rights reserved
+ *
+ */
+
+#if !defined(_TRACE_PAGE_FAULT_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_PAGE_FAULT_H
+
+#include <linux/tracepoint.h>
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM exceptions
+
+TRACE_EVENT(page_fault_user,
+ TP_PROTO(struct pt_regs *regs),
+ TP_ARGS(regs),
+
+ TP_STRUCT__entry(
+ __field(unsigned long, address)
+ __field(unsigned long, cause)
+ ),
+
+ TP_fast_assign(
+ __entry->address = regs->badaddr;
+ __entry->cause = regs->cause;
+ ),
+
+ TP_printk("user page fault, address=%ps cause=0x%lx",
+ (void *)__entry->address, __entry->cause)
+);
+
+TRACE_EVENT(page_fault_kernel,
+ TP_PROTO(struct pt_regs *regs),
+ TP_ARGS(regs),
+
+ TP_STRUCT__entry(
+ __field(unsigned long, address)
+ __field(unsigned long, cause)
+ ),
+
+ TP_fast_assign(
+ __entry->address = regs->badaddr;
+ __entry->cause = regs->cause;
+ ),
+
+ TP_printk("kernel page fault, address=%ps cause=0x%lx",
+ (void *)__entry->address, __entry->cause)
+);
+
+#undef TRACE_INCLUDE_PATH
+#undef TRACE_INCLUDE_FILE
+#define TRACE_INCLUDE_PATH asm/trace/
+#define TRACE_INCLUDE_FILE exceptions
+#endif /* _TRACE_PAGE_FAULT_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c
index 5224f3733802..22874074c5bc 100644
--- a/arch/riscv/mm/fault.c
+++ b/arch/riscv/mm/fault.c
@@ -22,6 +22,10 @@

#include "../kernel/head.h"

+
+#define CREATE_TRACE_POINTS
+#include <asm/trace/exceptions.h>
+
static void die_kernel_fault(const char *msg, unsigned long addr,
struct pt_regs *regs)
{
@@ -215,6 +219,15 @@ static inline bool access_error(unsigned long cause, struct vm_area_struct *vma)
return false;
}

+
+static inline void trace_page_fault(struct pt_regs *regs)
+{
+ if (user_mode(regs))
+ trace_page_fault_user(regs);
+ else
+ trace_page_fault_kernel(regs);
+}
+
/*
* This routine handles page faults. It determines the address and the
* problem, and then passes it off to one of the appropriate routines.
@@ -235,6 +248,8 @@ void handle_page_fault(struct pt_regs *regs)
tsk = current;
mm = tsk->mm;

+ trace_page_fault(regs);
+
if (kprobe_page_fault(regs, cause))
return;

--
2.34.1