Skip to contentSkip to conversation

Faultable Tracepoints

PATCH In review Mathieu Desnoyers 2 participants 11 messages Aug 28, 2024 – Sep 9, 2024

Subsystems (4)
lore
Share & tools
lore

Mathieu Desnoyers

author reviewer
#0 lore raw

v6 cover Faultable Tracepoints

Wire up the system call tracepoints with Tasks Trace RCU to allow the ftrace, perf, and eBPF tracers to handle page faults.

This series does the initial wire-up allowing tracers to handle page faults, but leaves out the actual handling of said page faults as future work.

I have tested this against a feature branch of lttng-modules which implements handling of page faults for the filename argument of the openat(2) system call.

This v6 rebases v5 on top of v6.11-rc5. It requires the "cleanup.h:
Introduce DEFINE_INACTIVE_GUARD()/activate_guard()" series.

Thanks,

Mathieu

Mathieu Desnoyers (5):
tracing: Introduce faultable tracepoints tracing/ftrace: Add support for faultable tracepoints tracing/bpf-trace: Add support for faultable tracepoints tracing/perf: Add support for faultable tracepoints
tracing: Convert sys_enter/exit to faultable tracepoints

 include/linux/tracepoint-defs.h | 14 ++++++
 include/linux/tracepoint.h      | 88 +++++++++++++++++++++++----------
 include/trace/bpf_probe.h       | 21 ++++++--
 include/trace/define_trace.h    |  7 +++
 include/trace/events/syscalls.h |  4 +-
 include/trace/perf.h            | 22 ++++++++-
 include/trace/trace_events.h    | 68 ++++++++++++++++++++++++-
 init/Kconfig                    |  1 +
 kernel/trace/bpf_trace.c        |  4 +-
 kernel/trace/trace_events.c     | 16 +++---
 kernel/trace/trace_fprobe.c     |  5 +-
 kernel/trace/trace_syscalls.c   | 52 ++++++++++++++++---
 kernel/tracepoint.c             | 65 ++++++++++++++----------
 13 files changed, 288 insertions(+), 79 deletions(-)
Signature · 1 line
2.39.2
author reviewer
#1 lore raw

PATCH v6 1/5 tracing: Introduce faultable tracepoints

When invoked from system call enter/exit instrumentation, accessing user-space data is a common use-case for tracers. However, tracepoints currently disable preemption around iteration on the registered tracepoint probes and invocation of the probe callbacks, which prevents tracers from handling page faults.

Extend the tracepoint and trace event APIs to allow defining a faultable tracepoint which invokes its callback with preemption enabled.

Also extend the tracepoint API to allow tracers to request specific probes to be connected to those faultable tracepoints. When the TRACEPOINT_MAY_FAULT flag is provided on registration, the probe callback will be called with preemption enabled, and is allowed to take page faults. Faultable probes can only be registered on faultable tracepoints and non-faultable probes on non-faultable tracepoints.

The tasks trace rcu mechanism is used to synchronize read-side marshalling of the registered probes with respect to faultable probes unregistration and teardown.

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Joel Fernandes <[email protected]>

---
Changes since v1:
- Cleanup __DO_TRACE() implementation.
- Rename "sleepable tracepoints" to "faultable tracepoints", MAYSLEEP to MAYFAULT, and use might_fault() rather than might_sleep(), to properly convey that the tracepoints are meant to be able to take a page fault, which requires to be able to sleep *and* to hold the mmap_sem.
Changes since v2:
- Rename MAYFAULT to MAY_FAULT.
- Rebased on 6.5.5.
- Introduce MAY_EXIST tracepoint flag.
Changes since v3:
- Rebased on 6.6.2.
Changes since v4:
- Rebased on 6.9.6.
- Simplify flag check in tracepoint_probe_register_prio_flags().
- Update MAY_EXIST flag description.
Changes since v5:
- Rebased on v6.11-rc5.

---
 include/linux/tracepoint-defs.h | 14 ++++++
 include/linux/tracepoint.h      | 88 +++++++++++++++++++++++----------
 include/trace/define_trace.h    |  7 +++
 include/trace/trace_events.h    |  6 +++
 init/Kconfig                    |  1 +
 kernel/trace/bpf_trace.c        |  4 +-
 kernel/trace/trace_fprobe.c     |  5 +-
 kernel/tracepoint.c             | 65 ++++++++++++++----------
 8 files changed, 136 insertions(+), 54 deletions(-)
include/linux/tracepoint-defs.h +14 -0
index 4dc4955f0fbf..94e39c86b49f 100644
@@ -29,6 +29,19 @@ struct tracepoint_func {
 	int prio;
 };
 
+/**
+ * enum tracepoint_flags - Tracepoint flags
+ * @TRACEPOINT_MAY_EXIST: On registration, don't warn if the tracepoint is
+ *                        already registered.
+ * @TRACEPOINT_MAY_FAULT: The tracepoint probe callback will be called with
+ *                        preemption enabled, and is allowed to take page
+ *                        faults.
+ */
+enum tracepoint_flags {
+	TRACEPOINT_MAY_EXIST = (1 << 0),
+	TRACEPOINT_MAY_FAULT = (1 << 1),
+};
+
 struct tracepoint {
 	const char *name;		/* Tracepoint name */
 	struct static_key key;
@@ -39,6 +52,7 @@ struct tracepoint {
 	int (*regfunc)(void);
 	void (*unregfunc)(void);
 	struct tracepoint_func __rcu *funcs;
+	unsigned int flags;
 };
 
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
include/linux/tracepoint.h +63 -25

215 lines not on this page · raw message

include/trace/define_trace.h +7 -0
index 00723935dcc7..1b8ca143724a 100644
@@ -41,6 +41,12 @@
 		assign, print, reg, unreg)			\
 	DEFINE_TRACE_FN(name, reg, unreg, PARAMS(proto), PARAMS(args))
 
+#undef TRACE_EVENT_FN_MAY_FAULT
+#define TRACE_EVENT_FN_MAY_FAULT(name, proto, args, tstruct,	\
+		assign, print, reg, unreg)			\
+	DEFINE_TRACE_FN_FLAGS(name, reg, unreg, PARAMS(proto),	\
+			      PARAMS(args), TRACEPOINT_MAY_FAULT)
+
 #undef TRACE_EVENT_FN_COND
 #define TRACE_EVENT_FN_COND(name, proto, args, cond, tstruct,		\
 		assign, print, reg, unreg)			\
@@ -106,6 +112,7 @@
 
 #undef TRACE_EVENT
 #undef TRACE_EVENT_FN
+#undef TRACE_EVENT_FN_MAY_FAULT
 #undef TRACE_EVENT_FN_COND
 #undef TRACE_EVENT_CONDITION
 #undef TRACE_EVENT_NOP
include/trace/trace_events.h +6 -0
index c2f9cabf154d..df590eea8ae4 100644
@@ -77,6 +77,12 @@
 	TRACE_EVENT(name, PARAMS(proto), PARAMS(args),			\
 		PARAMS(tstruct), PARAMS(assign), PARAMS(print))		\
 
+#undef TRACE_EVENT_FN_MAY_FAULT
+#define TRACE_EVENT_FN_MAY_FAULT(name, proto, args, tstruct,		\
+		assign, print, reg, unreg)				\
+	TRACE_EVENT(name, PARAMS(proto), PARAMS(args),			\
+		PARAMS(tstruct), PARAMS(assign), PARAMS(print))		\
+
 #undef TRACE_EVENT_FN_COND
 #define TRACE_EVENT_FN_COND(name, proto, args, cond, tstruct,	\
 		assign, print, reg, unreg)				\
init/Kconfig +1 -0
index 5783a0b87517..72e13ee73c43 100644
@@ -1936,6 +1936,7 @@ config BINDGEN_VERSION_TEXT
 #
 config TRACEPOINTS
 	bool
+	select TASKS_TRACE_RCU
 
 source "kernel/Kconfig.kexec"
 
kernel/trace/bpf_trace.c +3 -1
index cd098846e251..c77eb80cbd7f 100644
@@ -2471,7 +2471,9 @@ int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *li
 	if (prog->aux->max_tp_access > btp->writable_size)
 		return -EINVAL;
 
-	return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func, link);
+	return tracepoint_probe_register_prio_flags(tp, (void *)btp->bpf_func,
+						    link, TRACEPOINT_DEFAULT_PRIO,
+						    TRACEPOINT_MAY_EXIST);
 }
 
 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link)
kernel/trace/trace_fprobe.c +3 -2
index 62e6a8f4aae9..f4f77dfed565 100644
@@ -705,8 +705,9 @@ static int __register_trace_fprobe(struct trace_fprobe *tf)
 		 * At first, put __probestub_##TP function on the tracepoint
 		 * and put a fprobe on the stub function.
 		 */
-		ret = tracepoint_probe_register_prio_may_exist(tpoint,
-					tpoint->probestub, NULL, 0);
+		ret = tracepoint_probe_register_prio_flags(tpoint,
+					tpoint->probestub, NULL, 0,
+					TRACEPOINT_MAY_EXIST);
 		if (ret < 0)
 			return ret;
 		return register_fprobe_ips(&tf->fp, &ip, 1);
kernel/tracepoint.c +39 -26

134 lines not on this page · raw message

Signature · 1 line
2.39.2
author reviewer
#2 lore raw

PATCH v6 2/5 tracing/ftrace: Add support for faultable tracepoints

In reply to Mathieu Desnoyers

In preparation for converting system call enter/exit instrumentation into faultable tracepoints, make sure that ftrace can handle registering to such tracepoints by explicitly disabling preemption within the ftrace tracepoint probes to respect the current expectations within ftrace ring buffer code.

This change does not yet allow ftrace to take page faults per se within its probe, but allows its existing probes to connect to faultable tracepoints.

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Joel Fernandes <[email protected]>

---
Changes since v4:
- Use DEFINE_INACTIVE_GUARD.
- Add brackets to multiline 'if' statements.
Changes since v5:
- Pass the TRACEPOINT_MAY_FAULT flag directly to tracepoint_probe_register_prio_flags.

---
 include/trace/trace_events.h | 64 ++++++++++++++++++++++++++++++++++--
 kernel/trace/trace_events.c  | 16 +++++----
 2 files changed, 71 insertions(+), 9 deletions(-)
include/trace/trace_events.h +61 -3
index df590eea8ae4..c887f7b6fbe9 100644
@@ -45,6 +45,16 @@
 			     PARAMS(print));		       \
 	DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));
 
+#undef TRACE_EVENT_MAY_FAULT
+#define TRACE_EVENT_MAY_FAULT(name, proto, args, tstruct, assign, print) \
+	DECLARE_EVENT_CLASS_MAY_FAULT(name,		       \
+			     PARAMS(proto),		       \
+			     PARAMS(args),		       \
+			     PARAMS(tstruct),		       \
+			     PARAMS(assign),		       \
+			     PARAMS(print));		       \
+	DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));
+
 #include "stages/stage1_struct_define.h"
 
 #undef DECLARE_EVENT_CLASS
@@ -57,6 +67,11 @@
 									\
 	static struct trace_event_class event_class_##name;
 
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(name, proto, args, tstruct, assign, print) \
+	DECLARE_EVENT_CLASS(name, PARAMS(proto), PARAMS(args),		\
+			    PARAMS(tstruct), PARAMS(assign), PARAMS(print))
+
 #undef DEFINE_EVENT
 #define DEFINE_EVENT(template, name, proto, args)	\
 	static struct trace_event_call	__used		\
@@ -80,7 +95,7 @@
 #undef TRACE_EVENT_FN_MAY_FAULT
 #define TRACE_EVENT_FN_MAY_FAULT(name, proto, args, tstruct,		\
 		assign, print, reg, unreg)				\
-	TRACE_EVENT(name, PARAMS(proto), PARAMS(args),			\
+	TRACE_EVENT_MAY_FAULT(name, PARAMS(proto), PARAMS(args),	\
 		PARAMS(tstruct), PARAMS(assign), PARAMS(print))		\
 
 #undef TRACE_EVENT_FN_COND
@@ -123,6 +138,11 @@
 		tstruct;						\
 	};
 
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(call, proto, args, tstruct, assign, print) \
+	DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args),		\
+			    PARAMS(tstruct), PARAMS(assign), PARAMS(print))
+
 #undef DEFINE_EVENT
 #define DEFINE_EVENT(template, name, proto, args)
 
@@ -214,6 +234,11 @@ static struct trace_event_functions trace_event_type_funcs_##call = {	\
 	.trace			= trace_raw_output_##call,		\
 };
 
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(call, proto, args, tstruct, assign, print) \
+	DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args),		\
+			    PARAMS(tstruct), PARAMS(assign), PARAMS(print))
+
 #undef DEFINE_EVENT_PRINT
 #define DEFINE_EVENT_PRINT(template, call, proto, args, print)		\
 static notrace enum print_line_t					\
@@ -250,6 +275,11 @@ static struct trace_event_fields trace_event_fields_##call[] = {	\
 	tstruct								\
 	{} };
 
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(call, proto, args, tstruct, assign, print) \
+	DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args),		\
+			    PARAMS(tstruct), PARAMS(assign), PARAMS(print))
+
 #undef DEFINE_EVENT_PRINT
 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
 
@@ -271,6 +301,11 @@ static inline notrace int trace_event_get_offsets_##call(		\
 	return __data_size;						\
 }
 
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(call, proto, args, tstruct, assign, print) \
+	DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args),		\
+			    PARAMS(tstruct), PARAMS(assign), PARAMS(print))
+
 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
 
 /*
@@ -380,8 +415,8 @@ static inline notrace int trace_event_get_offsets_##call(		\
 
 #include "stages/stage6_event_callback.h"
 
-#undef DECLARE_EVENT_CLASS
-#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
+#undef _DECLARE_EVENT_CLASS
+#define _DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print, tp_flags) \
 									\
 static notrace void							\
 trace_event_raw_event_##call(void *__data, proto)			\
@@ -392,6 +427,13 @@ trace_event_raw_event_##call(void *__data, proto)			\
 	struct trace_event_raw_##call *entry;				\
 	int __data_size;						\
 									\
+	DEFINE_INACTIVE_GUARD(preempt_notrace, trace_event_guard);	\
+									\
+	if ((tp_flags) & TRACEPOINT_MAY_FAULT) {			\
+		might_fault();						\
+		activate_guard(preempt_notrace, trace_event_guard)();	\
+	}								\
+									\
 	if (trace_trigger_soft_disabled(trace_file))			\
 		return;							\
 									\
@@ -409,6 +451,17 @@ trace_event_raw_event_##call(void *__data, proto)			\
 									\
 	trace_event_buffer_commit(&fbuffer);				\
 }
+
+#undef DECLARE_EVENT_CLASS
+#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
+	_DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args),		\
+			    PARAMS(tstruct), PARAMS(assign), PARAMS(print), 0)
+
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(call, proto, args, tstruct, assign, print) \
+	_DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args),		\
+			    PARAMS(tstruct), PARAMS(assign), PARAMS(print), TRACEPOINT_MAY_FAULT)
+
 /*
  * The ftrace_test_probe is compiled out, it is only here as a build time check
  * to make sure that if the tracepoint handling changes, the ftrace probe will
@@ -440,6 +493,11 @@ static struct trace_event_class __used __refdata event_class_##call = { \
 	_TRACE_PERF_INIT(call)						\
 };
 
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(call, proto, args, tstruct, assign, print) \
+	DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args),		\
+			    PARAMS(tstruct), PARAMS(assign), PARAMS(print))
+
 #undef DEFINE_EVENT
 #define DEFINE_EVENT(template, call, proto, args)			\
 									\
kernel/trace/trace_events.c +10 -6
index 7266ec2a4eea..5722e426143d 100644
@@ -532,9 +532,11 @@ int trace_event_reg(struct trace_event_call *call,
 	WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
 	switch (type) {
 	case TRACE_REG_REGISTER:
-		return tracepoint_probe_register(call->tp,
-						 call->class->probe,
-						 file);
+		return tracepoint_probe_register_prio_flags(call->tp,
+							    call->class->probe,
+							    file,
+							    TRACEPOINT_DEFAULT_PRIO,
+							    call->tp->flags & TRACEPOINT_MAY_FAULT);
 	case TRACE_REG_UNREGISTER:
 		tracepoint_probe_unregister(call->tp,
 					    call->class->probe,
@@ -543,9 +545,11 @@ int trace_event_reg(struct trace_event_call *call,
 
 #ifdef CONFIG_PERF_EVENTS
 	case TRACE_REG_PERF_REGISTER:
-		return tracepoint_probe_register(call->tp,
-						 call->class->perf_probe,
-						 call);
+		return tracepoint_probe_register_prio_flags(call->tp,
+							    call->class->perf_probe,
+							    call,
+							    TRACEPOINT_DEFAULT_PRIO,
+							    call->tp->flags & TRACEPOINT_MAY_FAULT);
 	case TRACE_REG_PERF_UNREGISTER:
 		tracepoint_probe_unregister(call->tp,
 					    call->class->perf_probe,
Signature · 1 line
2.39.2
author reviewer
#3 lore raw

PATCH v6 3/5 tracing/bpf-trace: Add support for faultable tracepoints

In reply to Mathieu Desnoyers

In preparation for converting system call enter/exit instrumentation into faultable tracepoints, make sure that bpf can handle registering to such tracepoints by explicitly disabling preemption within the bpf tracepoint probes to respect the current expectations within bpf tracing code.

This change does not yet allow bpf to take page faults per se within its probe, but allows its existing probes to connect to faultable tracepoints.

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Joel Fernandes <[email protected]>

---
Changes since v4:
- Use DEFINE_INACTIVE_GUARD.
- Add brackets to multiline 'if' statements.
Changes since v5:
- Rebased on v6.11-rc5.
- Pass the TRACEPOINT_MAY_FAULT flag directly to tracepoint_probe_register_prio_flags.

---
 include/trace/bpf_probe.h | 21 ++++++++++++++++-----
 kernel/trace/bpf_trace.c  |  2 +-
 2 files changed, 17 insertions(+), 6 deletions(-)
include/trace/bpf_probe.h +16 -5
index a2ea11cc912e..cc96dd1e7c3d 100644
@@ -42,16 +42,27 @@
 /* tracepoints with more than 12 arguments will hit build error */
 #define CAST_TO_U64(...) CONCATENATE(__CAST, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
 
-#define __BPF_DECLARE_TRACE(call, proto, args)				\
+#define __BPF_DECLARE_TRACE(call, proto, args, tp_flags)		\
 static notrace void							\
 __bpf_trace_##call(void *__data, proto)					\
 {									\
-	CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args));	\
+	DEFINE_INACTIVE_GUARD(preempt_notrace, bpf_trace_guard);	\
+									\
+	if ((tp_flags) & TRACEPOINT_MAY_FAULT) {			\
+		might_fault();						\
+		activate_guard(preempt_notrace, bpf_trace_guard)();	\
+	}								\
+									\
+	CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args)); \
 }
 
 #undef DECLARE_EVENT_CLASS
 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
-	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))
+	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0)
+
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(call, proto, args, tstruct, assign, print) \
+	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), TRACEPOINT_MAY_FAULT)
 
 /*
  * This part is compiled out, it is only here as a build time check
@@ -105,13 +116,13 @@ static inline void bpf_test_buffer_##call(void)				\
 
 #undef DECLARE_TRACE
 #define DECLARE_TRACE(call, proto, args)				\
-	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))		\
+	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0)	\
 	__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0)
 
 #undef DECLARE_TRACE_WRITABLE
 #define DECLARE_TRACE_WRITABLE(call, proto, args, size) \
 	__CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
-	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
+	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0) \
 	__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size)
 
 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
kernel/trace/bpf_trace.c +1 -1
index c77eb80cbd7f..ed07283d505b 100644
@@ -2473,7 +2473,7 @@ int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *li
 
 	return tracepoint_probe_register_prio_flags(tp, (void *)btp->bpf_func,
 						    link, TRACEPOINT_DEFAULT_PRIO,
-						    TRACEPOINT_MAY_EXIST);
+						    TRACEPOINT_MAY_EXIST | (tp->flags & TRACEPOINT_MAY_FAULT));
 }
 
 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link)
Signature · 1 line
2.39.2
#4 lore raw

Re: [PATCH v6 3/5] tracing/bpf-trace: Add support for faultable tracepoints

Quoted 106 lines · Mathieu Desnoyers

In preparation for converting system call enter/exit instrumentation into faultable tracepoints, make sure that bpf can handle registering to such tracepoints by explicitly disabling preemption within the bpf tracepoint probes to respect the current expectations within bpf tracing code.

This change does not yet allow bpf to take page faults per se within its probe, but allows its existing probes to connect to faultable tracepoints.

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Joel Fernandes <[email protected]>

---
Changes since v4:
- Use DEFINE_INACTIVE_GUARD.
- Add brackets to multiline 'if' statements.
Changes since v5:
- Rebased on v6.11-rc5.
- Pass the TRACEPOINT_MAY_FAULT flag directly to tracepoint_probe_register_prio_flags.

---
 include/trace/bpf_probe.h | 21 ++++++++++++++++-----
 kernel/trace/bpf_trace.c  |  2 +-
 2 files changed, 17 insertions(+), 6 deletions(-)
include/trace/bpf_probe.h +16 -5
index a2ea11cc912e..cc96dd1e7c3d 100644
@@ -42,16 +42,27 @@
 /* tracepoints with more than 12 arguments will hit build error */
 #define CAST_TO_U64(...) CONCATENATE(__CAST, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
 
-#define __BPF_DECLARE_TRACE(call, proto, args)                         \
+#define __BPF_DECLARE_TRACE(call, proto, args, tp_flags)               \
 static notrace void                                                    \
 __bpf_trace_##call(void *__data, proto)                                        \
 {                                                                      \
-       CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args));        \
+       DEFINE_INACTIVE_GUARD(preempt_notrace, bpf_trace_guard);        \
+                                                                       \
+       if ((tp_flags) & TRACEPOINT_MAY_FAULT) {                        \
+               might_fault();                                          \
+               activate_guard(preempt_notrace, bpf_trace_guard)();     \
+       }                                                               \
+                                                                       \
+       CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args)); \
 }
 
 #undef DECLARE_EVENT_CLASS
 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
-       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0)
+
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(call, proto, args, tstruct, assign, print) \
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), TRACEPOINT_MAY_FAULT)
 
 /*
  * This part is compiled out, it is only here as a build time check
@@ -105,13 +116,13 @@ static inline void bpf_test_buffer_##call(void)                           \
 
 #undef DECLARE_TRACE
 #define DECLARE_TRACE(call, proto, args)                               \
-       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))          \
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0)       \
        __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0)
 
 #undef DECLARE_TRACE_WRITABLE
 #define DECLARE_TRACE_WRITABLE(call, proto, args, size) \
        __CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
-       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0) \
        __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size)
 
 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
kernel/trace/bpf_trace.c +1 -2
index c77eb80cbd7f..ed07283d505b 100644
@@ -2473,7 +2473,7 @@ int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *li
 
        return tracepoint_probe_register_prio_flags(tp, (void *)btp->bpf_func,
                                                    link, TRACEPOINT_DEFAULT_PRIO,
-                                                   TRACEPOINT_MAY_EXIST);
+                                                   TRACEPOINT_MAY_EXIST | (tp->flags & TRACEPOINT_MAY_FAULT));
 }
 
 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link)
--
Quoted 3 lines

2.39.2

I wonder if it would be better to just do this, instead of that preempt guard. I think we don't strictly need preemption to be disabled, we just need to stay on the same CPU, just like we do that for many other program types.

We'll need some more BPF-specific plumbing to fully support faultable (sleepable) tracepoints, but this should unblock your work, unless I'm missing something. And we can take it from there, once your patches land, to take advantage of faultable tracepoints in the BPF ecosystem.

kernel/trace/bpf_trace.c +0 -0
index b69a39316c0c..415639b7c7a4 100644
@@ -2302,7 +2302,8 @@ void __bpf_trace_run(struct bpf_raw_tp_link

*link, u64 *args)

        struct bpf_run_ctx *old_run_ctx;
        struct bpf_trace_run_ctx run_ctx;

- cant_sleep();
+ migrate_disable();
+

        if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
                bpf_prog_inc_misses_counter(prog);
                goto out;
@@ -2318,6 +2319,8 @@ void __bpf_trace_run(struct bpf_raw_tp_link
*link, u64 *args)
        bpf_reset_run_ctx(old_run_ctx);
 out:
        this_cpu_dec(*(prog->active));

+
+ migrate_enable();
}

author reviewer
#5 lore raw

Re: [PATCH v6 3/5] tracing/bpf-trace: Add support for faultable tracepoints

Quoted 106 lines · Andrii Nakryiko
Quoted 104 lines · Mathieu Desnoyers

In preparation for converting system call enter/exit instrumentation into faultable tracepoints, make sure that bpf can handle registering to such tracepoints by explicitly disabling preemption within the bpf tracepoint probes to respect the current expectations within bpf tracing code.

This change does not yet allow bpf to take page faults per se within its probe, but allows its existing probes to connect to faultable tracepoints.

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Joel Fernandes <[email protected]>

---
Changes since v4:
- Use DEFINE_INACTIVE_GUARD.
- Add brackets to multiline 'if' statements.
Changes since v5:
- Rebased on v6.11-rc5.
- Pass the TRACEPOINT_MAY_FAULT flag directly to tracepoint_probe_register_prio_flags.

---
  include/trace/bpf_probe.h | 21 ++++++++++++++++-----
  kernel/trace/bpf_trace.c  |  2 +-
  2 files changed, 17 insertions(+), 6 deletions(-)
include/trace/bpf_probe.h +16 -5
index a2ea11cc912e..cc96dd1e7c3d 100644
@@ -42,16 +42,27 @@
  /* tracepoints with more than 12 arguments will hit build error */
  #define CAST_TO_U64(...) CONCATENATE(__CAST, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
 
-#define __BPF_DECLARE_TRACE(call, proto, args)                         \
+#define __BPF_DECLARE_TRACE(call, proto, args, tp_flags)               \
  static notrace void                                                    \
  __bpf_trace_##call(void *__data, proto)                                        \
  {                                                                      \
-       CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args));        \
+       DEFINE_INACTIVE_GUARD(preempt_notrace, bpf_trace_guard);        \
+                                                                       \
+       if ((tp_flags) & TRACEPOINT_MAY_FAULT) {                        \
+               might_fault();                                          \
+               activate_guard(preempt_notrace, bpf_trace_guard)();     \
+       }                                                               \
+                                                                       \
+       CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args)); \
  }
 
  #undef DECLARE_EVENT_CLASS
  #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
-       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0)
+
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(call, proto, args, tstruct, assign, print) \
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), TRACEPOINT_MAY_FAULT)
 
  /*
   * This part is compiled out, it is only here as a build time check
@@ -105,13 +116,13 @@ static inline void bpf_test_buffer_##call(void)                           \
 
  #undef DECLARE_TRACE
  #define DECLARE_TRACE(call, proto, args)                               \
-       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))          \
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0)       \
         __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0)
 
  #undef DECLARE_TRACE_WRITABLE
  #define DECLARE_TRACE_WRITABLE(call, proto, args, size) \
         __CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
-       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0) \
         __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size)
 
  #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
kernel/trace/bpf_trace.c +1 -1
index c77eb80cbd7f..ed07283d505b 100644
@@ -2473,7 +2473,7 @@ int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *li
 
         return tracepoint_probe_register_prio_flags(tp, (void *)btp->bpf_func,
                                                     link, TRACEPOINT_DEFAULT_PRIO,
-                                                   TRACEPOINT_MAY_EXIST);
+                                                   TRACEPOINT_MAY_EXIST | (tp->flags & TRACEPOINT_MAY_FAULT));
  }
 
Quoted 10 lines
Quoted 5 lines
kernel/trace/bpf_trace.c +0 -1
  int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link)
--

2.39.2

I wonder if it would be better to just do this, instead of that preempt guard. I think we don't strictly need preemption to be disabled, we just need to stay on the same CPU, just like we do that for many other program types.

I'm worried about introducing any kind of subtle synchronization change in this series, and moving from preempt-off to migrate-disable definitely falls under that umbrella.

I would recommend auditing all uses of this_cpu_*() APIs to make sure accesses to per-cpu data structures are using atomics and not just using operations that expect use of preempt-off to prevent concurrent threads from updating to the per-cpu data concurrently.

So what you are suggesting may be a good idea, but I prefer to leave this kind of change to a separate bpf-specific series, and I would leave this work to someone who knows more about ebpf than me.

Thanks,

Mathieu

Quoted 29 lines

We'll need some more BPF-specific plumbing to fully support faultable (sleepable) tracepoints, but this should unblock your work, unless I'm missing something. And we can take it from there, once your patches land, to take advantage of faultable tracepoints in the BPF ecosystem.

kernel/trace/bpf_trace.c +2 -1
index b69a39316c0c..415639b7c7a4 100644
@@ -2302,7 +2302,8 @@ void __bpf_trace_run(struct bpf_raw_tp_link
 *link, u64 *args)
         struct bpf_run_ctx *old_run_ctx;
         struct bpf_trace_run_ctx run_ctx;
 
-       cant_sleep();
+       migrate_disable();
+
         if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
                 bpf_prog_inc_misses_counter(prog);
                 goto out;
kernel/trace/bpf_trace.c +2 -0
@@ -2318,6 +2319,8 @@ void __bpf_trace_run(struct bpf_raw_tp_link
 *link, u64 *args)
         bpf_reset_run_ctx(old_run_ctx);
  out:
         this_cpu_dec(*(prog->active));
+
+       migrate_enable();
  }
Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
#6 lore raw

Re: [PATCH v6 3/5] tracing/bpf-trace: Add support for faultable tracepoints

Quoted 123 lines · Mathieu Desnoyers
Quoted 106 lines · Andrii Nakryiko
Quoted 104 lines · Mathieu Desnoyers

In preparation for converting system call enter/exit instrumentation into faultable tracepoints, make sure that bpf can handle registering to such tracepoints by explicitly disabling preemption within the bpf tracepoint probes to respect the current expectations within bpf tracing code.

This change does not yet allow bpf to take page faults per se within its probe, but allows its existing probes to connect to faultable tracepoints.

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Joel Fernandes <[email protected]>

---
Changes since v4:
- Use DEFINE_INACTIVE_GUARD.
- Add brackets to multiline 'if' statements.
Changes since v5:
- Rebased on v6.11-rc5.
- Pass the TRACEPOINT_MAY_FAULT flag directly to tracepoint_probe_register_prio_flags.

---
  include/trace/bpf_probe.h | 21 ++++++++++++++++-----
  kernel/trace/bpf_trace.c  |  2 +-
  2 files changed, 17 insertions(+), 6 deletions(-)
include/trace/bpf_probe.h +16 -5
index a2ea11cc912e..cc96dd1e7c3d 100644
@@ -42,16 +42,27 @@
  /* tracepoints with more than 12 arguments will hit build error */
  #define CAST_TO_U64(...) CONCATENATE(__CAST, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
 
-#define __BPF_DECLARE_TRACE(call, proto, args)                         \
+#define __BPF_DECLARE_TRACE(call, proto, args, tp_flags)               \
  static notrace void                                                    \
  __bpf_trace_##call(void *__data, proto)                                        \
  {                                                                      \
-       CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args));        \
+       DEFINE_INACTIVE_GUARD(preempt_notrace, bpf_trace_guard);        \
+                                                                       \
+       if ((tp_flags) & TRACEPOINT_MAY_FAULT) {                        \
+               might_fault();                                          \
+               activate_guard(preempt_notrace, bpf_trace_guard)();     \
+       }                                                               \
+                                                                       \
+       CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args)); \
  }
 
  #undef DECLARE_EVENT_CLASS
  #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
-       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0)
+
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(call, proto, args, tstruct, assign, print) \
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), TRACEPOINT_MAY_FAULT)
 
  /*
   * This part is compiled out, it is only here as a build time check
@@ -105,13 +116,13 @@ static inline void bpf_test_buffer_##call(void)                           \
 
  #undef DECLARE_TRACE
  #define DECLARE_TRACE(call, proto, args)                               \
-       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))          \
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0)       \
         __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0)
 
  #undef DECLARE_TRACE_WRITABLE
  #define DECLARE_TRACE_WRITABLE(call, proto, args, size) \
         __CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
-       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
+       __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args), 0) \
         __DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size)
 
  #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
kernel/trace/bpf_trace.c +1 -1
index c77eb80cbd7f..ed07283d505b 100644
@@ -2473,7 +2473,7 @@ int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *li
 
         return tracepoint_probe_register_prio_flags(tp, (void *)btp->bpf_func,
                                                     link, TRACEPOINT_DEFAULT_PRIO,
-                                                   TRACEPOINT_MAY_EXIST);
+                                                   TRACEPOINT_MAY_EXIST | (tp->flags & TRACEPOINT_MAY_FAULT));
  }
 
Quoted 10 lines
Quoted 5 lines
kernel/trace/bpf_trace.c +0 -1
  int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link)
--

2.39.2

I wonder if it would be better to just do this, instead of that preempt guard. I think we don't strictly need preemption to be disabled, we just need to stay on the same CPU, just like we do that for many other program types.

I'm worried about introducing any kind of subtle synchronization change in this series, and moving from preempt-off to migrate-disable definitely falls under that umbrella.

Quoted 9 lines

I would recommend auditing all uses of this_cpu_*() APIs to make sure accesses to per-cpu data structures are using atomics and not just using operations that expect use of preempt-off to prevent concurrent threads from updating to the per-cpu data concurrently.

So what you are suggesting may be a good idea, but I prefer to leave this kind of change to a separate bpf-specific series, and I would leave this work to someone who knows more about ebpf than me.

Yeah, that's ok. migrate_disable() switch is probably going a bit too far too fast, but I think we should just add preempt_disable/preempt_enable inside __bpf_trace_run() instead of leaving it inside those hard to find and follow tracepoint macros. So maybe you can just pass a bool into __bpf_trace_run() and do preempt guard (or explicit disable/enable) there?

Quoted 39 lines

Thanks,

Mathieu

Quoted 22 lines

We'll need some more BPF-specific plumbing to fully support faultable (sleepable) tracepoints, but this should unblock your work, unless I'm missing something. And we can take it from there, once your patches land, to take advantage of faultable tracepoints in the BPF ecosystem.

kernel/trace/bpf_trace.c +2 -1
index b69a39316c0c..415639b7c7a4 100644
@@ -2302,7 +2302,8 @@ void __bpf_trace_run(struct bpf_raw_tp_link
 *link, u64 *args)
         struct bpf_run_ctx *old_run_ctx;
         struct bpf_trace_run_ctx run_ctx;
 
-       cant_sleep();
+       migrate_disable();
+
         if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
                 bpf_prog_inc_misses_counter(prog);
                 goto out;

kernel/trace/bpf_trace.c void __bpf_trace_run(struct bpf_raw_tp_link · line 2325

Quoted 7 lines
kernel/trace/bpf_trace.c +2 -0
@@ -2318,6 +2319,8 @@ void __bpf_trace_run(struct bpf_raw_tp_link
 *link, u64 *args)
         bpf_reset_run_ctx(old_run_ctx);
  out:
         this_cpu_dec(*(prog->active));
+
+       migrate_enable();
  }

--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com

author reviewer
#7 lore raw

Re: [PATCH v6 3/5] tracing/bpf-trace: Add support for faultable tracepoints

Quoted 1 line · Andrii Nakryiko

On Mon, Sep 9, 2024 at 8:11 AM Mathieu Desnoyers

[...]

Quoted 20 lines
Quoted 10 lines
Quoted 5 lines

I wonder if it would be better to just do this, instead of that preempt guard. I think we don't strictly need preemption to be disabled, we just need to stay on the same CPU, just like we do that for many other program types.

I'm worried about introducing any kind of subtle synchronization change in this series, and moving from preempt-off to migrate-disable definitely falls under that umbrella.

Quoted 9 lines

I would recommend auditing all uses of this_cpu_*() APIs to make sure accesses to per-cpu data structures are using atomics and not just using operations that expect use of preempt-off to prevent concurrent threads from updating to the per-cpu data concurrently.

So what you are suggesting may be a good idea, but I prefer to leave this kind of change to a separate bpf-specific series, and I would leave this work to someone who knows more about ebpf than me.

Quoted 7 lines

Yeah, that's ok. migrate_disable() switch is probably going a bit too far too fast, but I think we should just add preempt_disable/preempt_enable inside __bpf_trace_run() instead of leaving it inside those hard to find and follow tracepoint macros. So maybe you can just pass a bool into __bpf_trace_run() and do preempt guard (or explicit disable/enable) there?

Passing an extra boolean to __bpf_trace_run would impact all tracepoints calling into ebpf, adding an extra function argument and extra tests for all of those. The impact may be small, but it is non-zero in both code size and overhead, so it would not be my preferred approach.

I have modified the macros to add the guard within __bpf_trace_##call following suggestions from Linus:

https://lore.kernel.org/lkml/CAHk-=wggDLDeTKbhb5hh--x=-DQd69v41137M72m6NOTmbD-cw@mail.gmail.com/

I'll Cc you on that version of the series.

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
#8 lore raw

Re: [PATCH v6 3/5] tracing/bpf-trace: Add support for faultable tracepoints

Quoted 32 lines · Mathieu Desnoyers
Quoted 1 line · Andrii Nakryiko

On Mon, Sep 9, 2024 at 8:11 AM Mathieu Desnoyers

[...]

Quoted 20 lines
Quoted 10 lines
Quoted 5 lines

I wonder if it would be better to just do this, instead of that preempt guard. I think we don't strictly need preemption to be disabled, we just need to stay on the same CPU, just like we do that for many other program types.

I'm worried about introducing any kind of subtle synchronization change in this series, and moving from preempt-off to migrate-disable definitely falls under that umbrella.

Quoted 9 lines

I would recommend auditing all uses of this_cpu_*() APIs to make sure accesses to per-cpu data structures are using atomics and not just using operations that expect use of preempt-off to prevent concurrent threads from updating to the per-cpu data concurrently.

So what you are suggesting may be a good idea, but I prefer to leave this kind of change to a separate bpf-specific series, and I would leave this work to someone who knows more about ebpf than me.

Quoted 7 lines

Yeah, that's ok. migrate_disable() switch is probably going a bit too far too fast, but I think we should just add preempt_disable/preempt_enable inside __bpf_trace_run() instead of leaving it inside those hard to find and follow tracepoint macros. So maybe you can just pass a bool into __bpf_trace_run() and do preempt guard (or explicit disable/enable) there?

Quoted 5 lines

Passing an extra boolean to __bpf_trace_run would impact all tracepoints calling into ebpf, adding an extra function argument and extra tests for all of those. The impact may be small, but it is non-zero in both code size and overhead, so it would not be my preferred approach.

Ok, sounds good to me, we can always change that after your patch set makes it into upstream.

Quoted 6 lines

I have modified the macros to add the guard within __bpf_trace_##call following suggestions from Linus:

   https://lore.kernel.org/lkml/CAHk-=wggDLDeTKbhb5hh--x=-DQd69v41137M72m6NOTmbD-cw@mail.gmail.com/

I'll Cc you on that version of the series.

Thanks!

Quoted 11 lines

Thanks,

Mathieu

--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com

author reviewer
#9 lore raw

PATCH v6 4/5 tracing/perf: Add support for faultable tracepoints

In reply to Mathieu Desnoyers

In preparation for converting system call enter/exit instrumentation into faultable tracepoints, make sure that perf can handle registering to such tracepoints by explicitly disabling preemption within the perf tracepoint probes to respect the current expectations within perf ring buffer code.

This change does not yet allow perf to take page faults per se within its probe, but allows its existing probes to connect to faultable tracepoints.

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Joel Fernandes <[email protected]>

---
Changes since v4:
- Use DEFINE_INACTIVE_GUARD.

---
 include/trace/perf.h | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
include/trace/perf.h +20 -2
index 2c11181c82e0..161e1655b953 100644
@@ -12,8 +12,8 @@
 #undef __perf_task
 #define __perf_task(t)	(__task = (t))
 
-#undef DECLARE_EVENT_CLASS
-#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
+#undef _DECLARE_EVENT_CLASS
+#define _DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print, tp_flags) \
 static notrace void							\
 perf_trace_##call(void *__data, proto)					\
 {									\
@@ -28,6 +28,13 @@ perf_trace_##call(void *__data, proto)					\
 	int __data_size;						\
 	int rctx;							\
 									\
+	DEFINE_INACTIVE_GUARD(preempt_notrace, trace_event_guard);	\
+									\
+	if ((tp_flags) & TRACEPOINT_MAY_FAULT) {			\
+		might_fault();						\
+		activate_guard(preempt_notrace, trace_event_guard)();	\
+	}								\
+									\
 	__data_size = trace_event_get_offsets_##call(&__data_offsets, args); \
 									\
 	head = this_cpu_ptr(event_call->perf_events);			\
@@ -55,6 +62,17 @@ perf_trace_##call(void *__data, proto)					\
 				  head, __task);			\
 }
 
+#undef DECLARE_EVENT_CLASS
+#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
+	_DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args),		\
+			     PARAMS(tstruct), PARAMS(assign), PARAMS(print), 0)
+
+#undef DECLARE_EVENT_CLASS_MAY_FAULT
+#define DECLARE_EVENT_CLASS_MAY_FAULT(call, proto, args, tstruct, assign, print) \
+	_DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args),		\
+			     PARAMS(tstruct), PARAMS(assign), PARAMS(print), \
+			     TRACEPOINT_MAY_FAULT)
+
 /*
  * This part is compiled out, it is only here as a build time check
  * to make sure that if the tracepoint handling changes, the
Signature · 1 line
2.39.2
author reviewer
#10 lore raw

PATCH v6 5/5 tracing: Convert sys_enter/exit to faultable tracepoints

In reply to Mathieu Desnoyers

Convert the definition of the system call enter/exit tracepoints to faultable tracepoints now that all upstream tracers handle it.

This allows tracers to fault-in userspace system call arguments such as path strings within their probe callbacks.

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Joel Fernandes <[email protected]>

---
Since v4:
- Use 'guard(preempt_notrace)'.
- Add brackets to multiline 'if' statements.

---
 include/trace/events/syscalls.h |  4 +--
 kernel/trace/trace_syscalls.c   | 52 ++++++++++++++++++++++++++++-----
 2 files changed, 46 insertions(+), 10 deletions(-)
include/trace/events/syscalls.h +2 -2
index b6e0cbc2c71f..dc30e3004818 100644
@@ -15,7 +15,7 @@
 
 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
 
-TRACE_EVENT_FN(sys_enter,
+TRACE_EVENT_FN_MAY_FAULT(sys_enter,
 
 	TP_PROTO(struct pt_regs *regs, long id),
 
@@ -41,7 +41,7 @@ TRACE_EVENT_FN(sys_enter,
 
 TRACE_EVENT_FLAGS(sys_enter, TRACE_EVENT_FL_CAP_ANY)
 
-TRACE_EVENT_FN(sys_exit,
+TRACE_EVENT_FN_MAY_FAULT(sys_exit,
 
 	TP_PROTO(struct pt_regs *regs, long ret),
 
kernel/trace/trace_syscalls.c +44 -8

100 lines not on this page · raw message

Signature · 1 line
2.39.2