Re: [PATCH v3 05/18] rv: Add generic monitor event tracepoints and BPF kfuncs

From: Alexei Starovoitov

Date: Fri Nov 27 2026 - 18:33:14 EST


On Thu, Nov 26, 2026 at 04:59 AM Gabriele Monaco <gmonaco@xxxxxxxxxx> wrote:
> Add also bpf_rv_event_monitor() / bpf_rv_error_monitor() kfuncs wrapping
> them, so BPF monitors can emit similar events to in-kernel monitors.
>
> Userspace callers need now to use ftrace filters to get the right events
> but events and errors are separate channels to ease the most common
> filtering and account for dramatically different volume (errors are
> rare).

I thought we agreed that plain bpf_printk() works with glob filtering on top.
Why do you need these new kfuncs?
See below diff that illustrates the point.

> + if (strnlen(msg, msg__sz) == msg__sz)
> + return -EINVAL;
> +
> + trace_event_monitor(mon__str, id__nullable, msg);

This will read out of bounds. msg can point to a map value.
Take a BPF_F_MMAPABLE array with one page size value. NUL is the last byte:

prog user space
strnlen(msg, msg__sz) finds the NUL
writes 'x' over the NUL
__string(msg, msg) -> strlen(msg)
runs into the vmalloc guard page

There is no limit on msg__sz either.
When perf is on the event, a msg above 8192 bytes hits WARN_ONCE() in
perf_trace_buf_alloc().


diff --git a/tools/verification/rv/bpf_monitors/da_monitor_bpf.h b/tools/verification/rv/bpf_monitors/da_monitor_bpf.h
index e9e673c..6996c01 100644
--- a/tools/verification/rv/bpf_monitors/da_monitor_bpf.h
+++ b/tools/verification/rv/bpf_monitors/da_monitor_bpf.h
@@ -24,8 +24,6 @@
#define da_implicit_guard()
#define IS_ENABLED(conf) 0

-#define DA_TRACE_MSG_MAX 128
-
/*
* Also used to filter out events higher values imply lower ones.
* Userspace will parse this enum but expects EVENT to imply ERROR(s) and ERROR
@@ -328,10 +326,9 @@ static inline void da_reset_bpf(da_id_type id, monitor_target_bpf target)

#endif /* RV_MON_TYPE */

-extern int bpf_rv_event_monitor(const char *mon, const int *id__nullable,
- const char *msg, u32 msg__sz) __ksym;
-extern int bpf_rv_error_monitor(const char *mon, const int *id__nullable,
- const char *msg, u32 msg__sz) __ksym;
+/* "rv: <type>: <monitor>: <text>" in bpf_trace:bpf_trace_printk */
+#define rv_printk(type, fmt, ...) \
+ bpf_printk("rv: " type ": " __stringify(MONITOR_NAME) ": " fmt, __VA_ARGS__)

#if RV_MON_TYPE == RV_MON_GLOBAL || RV_MON_TYPE == RV_MON_PER_CPU
/*
@@ -345,28 +342,22 @@ static inline void da_trace_event(struct da_monitor *da_mon,
const char *next_state, bool is_final,
da_id_type id)
{
- char msg[DA_TRACE_MSG_MAX];
-
if (trace_level < RV_TRACE_EVENT)
return;

- BPF_SNPRINTF(msg, sizeof(msg), "%s x %s -> %s%s",
- curr_state, event, next_state, is_final ? " (final)" : "");
- bpf_rv_event_monitor(__stringify(MONITOR_NAME), NULL, msg, sizeof(msg));
+ rv_printk("event", "%s x %s -> %s%s",
+ curr_state, event, next_state, is_final ? " (final)" : "");
}

static inline void da_trace_error(struct da_monitor *da_mon,
const char *curr_state, const char *event,
da_id_type id)
{
- char msg[DA_TRACE_MSG_MAX];
-
if (trace_level < RV_TRACE_ERROR)
return;

- BPF_SNPRINTF(msg, sizeof(msg), "event %s not expected in the state %s",
- event, curr_state);
- bpf_rv_error_monitor(__stringify(MONITOR_NAME), NULL, msg, sizeof(msg));
+ rv_printk("error", "event %s not expected in the state %s",
+ event, curr_state);
}

#elif RV_MON_TYPE == RV_MON_PER_TASK || RV_MON_TYPE == RV_MON_PER_OBJ
@@ -379,28 +370,22 @@ static inline void da_trace_event(struct da_monitor *da_mon,
const char *next_state, bool is_final,
da_id_type id)
{
- char msg[DA_TRACE_MSG_MAX];
-
if (trace_level < RV_TRACE_EVENT)
return;

- BPF_SNPRINTF(msg, sizeof(msg), "%d: %s x %s -> %s%s", id,
- curr_state, event, next_state, is_final ? " (final)" : "");
- bpf_rv_event_monitor(__stringify(MONITOR_NAME), &id, msg, sizeof(msg));
+ rv_printk("event", "%d: %s x %s -> %s%s", id,
+ curr_state, event, next_state, is_final ? " (final)" : "");
}

static inline void da_trace_error(struct da_monitor *da_mon,
const char *curr_state, const char *event,
da_id_type id)
{
- char msg[DA_TRACE_MSG_MAX];
-
if (trace_level < RV_TRACE_ERROR)
return;

- BPF_SNPRINTF(msg, sizeof(msg), "%d: event %s not expected in the state %s",
- id, event, curr_state);
- bpf_rv_error_monitor(__stringify(MONITOR_NAME), &id, msg, sizeof(msg));
+ rv_printk("error", "%d: event %s not expected in the state %s",
+ id, event, curr_state);
}
#endif /* RV_MON_TYPE */

diff --git a/tools/verification/rv/src/trace.c b/tools/verification/rv/src/trace.c
index 629b4f6..91f86bf 100644
--- a/tools/verification/rv/src/trace.c
+++ b/tools/verification/rv/src/trace.c
@@ -15,7 +15,7 @@
#include <trace.h>
#include <utils.h>

-#define FILTER_MAX 128
+#define FILTER_MAX 256

/**
* collect_registered_events - print an event collected from the trace buffer
@@ -176,20 +176,25 @@ static int enable_specific_event(struct trace_instance *inst, const char *event_
}

/*
- * enable_generic_event - enable a generic event with filters
+ * enable_bpf_events - collect the messages of a BPF monitor
+ *
+ * BPF monitors report via bpf_printk("rv: <event|error>: <monitor>: ...").
+ *
+ * Returns 0 on success, -1 otherwise.
*/
-static int enable_generic_event(struct trace_instance *inst, const char *event_name, char *name)
+static int enable_bpf_events(struct trace_instance *inst, char *name)
{
- struct tep_event *tevent = tep_find_event_by_name(inst->tep, "rv", event_name);
+ struct tep_event *tevent;
char filter[FILTER_MAX] = "";
size_t len;

+ tevent = tep_find_event_by_name(inst->tep, "bpf_trace", "bpf_trace_printk");
if (!tevent) {
- err_msg("trace: fail finding the rv:%s event\n", event_name);
+ err_msg("trace: fail finding the bpf_trace_printk event\n");
return -1;
}

- len = snprintf(filter, sizeof(filter), "mon == \"%s\"", name);
+ len = snprintf(filter, sizeof(filter), "bpf_string ~ \"rv: *: %s: *\"", name);
if (len >= sizeof(filter)) {
err_msg("trace: monitor name %s too long\n", name);
return -1;
@@ -197,31 +202,12 @@ static int enable_generic_event(struct trace_instance *inst, const char *event_n

if (config.my_pid > 0)
snprintf(filter + len, sizeof(filter) - len,
- " && common_pid != %d && !(has_id == 1 && id == %d)",
- config.my_pid, config.my_pid);
+ " && common_pid != %d && !(bpf_string ~ \"rv: *: %s: %d: *\")",
+ config.my_pid, name, config.my_pid);

return apply_filter_enable(inst, tevent, filter);
}

-/*
- * enable_bpf_events - enable rv:event_monitor and rv:error_monitor for a monitor
- *
- * BPF monitors report via generic rv:event_monitor and rv:error_monitor events.
- * Enable the events and filter them to only collect the messages of the given monitor.
- *
- * Returns 0 on success, -1 otherwise.
- */
-static int enable_bpf_events(struct trace_instance *inst, char *name)
-{
- if (!config.trace_errors_only && enable_generic_event(inst, "event_monitor", name))
- return -1;
-
- if (enable_generic_event(inst, "error_monitor", name))
- return -1;
-
- return 0;
-}
-
/**
* trace_enable_monitor_events - enable a monitor's events in a trace instance
*
diff --git a/tools/verification/rv/tests/rv_mon.t b/tools/verification/rv/tests/rv_mon.t
index 30d1dda..f51f194 100644
--- a/tools/verification/rv/tests/rv_mon.t
+++ b/tools/verification/rv/tests/rv_mon.t
@@ -104,16 +104,16 @@ cpu="\[[0-9]\{3\}\][[:space:]]\+"
state="[a-z_]\+ "
id="[0-9]\+"
selfid="\$pid"
-# BPF monitors report via the common rv:event_monitor/rv:error_monitor
-# events, which add a "<mon>: " prefix to the message.
+# BPF monitors report via bpf_printk()
+type_bpf="bpf_trace_printk: rv: \(event\|error\)"
trace_task="${genpid}${cpu}${type}: ${id}: ${state}"
trace_task_self="${genpid}${cpu}${type}: ${selfid}: ${state}"
trace_cpu="${genpid}${cpu}${type}: ${state}"
trace_cpu_self="${selfpid}${cpu}${type}: ${state}"
-trace_task_bpf="${genpid}${cpu}${type}: ${mon}: ${id}: ${state}"
-trace_task_bpf_self="${genpid}${cpu}${type}: ${mon}: ${selfid}: ${state}"
-trace_cpu_bpf="${genpid}${cpu}${type}: ${mon}: ${state}"
-trace_cpu_bpf_self="${selfpid}${cpu}${type}: ${mon}: ${state}"
+trace_task_bpf="${genpid}${cpu}${type_bpf}: ${mon}: ${id}: ${state}"
+trace_task_bpf_self="${genpid}${cpu}${type_bpf}: ${mon}: ${selfid}: ${state}"
+trace_cpu_bpf="${genpid}${cpu}${type_bpf}: ${mon}: ${state}"
+trace_cpu_bpf_self="${selfpid}${cpu}${type_bpf}: ${mon}: ${state}"

check_if_exists "run per-task monitor with tracing" \
"$RV mon sssw -t" "$RVDIR/monitors/sched/sssw" \
@@ -154,10 +154,10 @@ check_if_exists "run per-task monitor tracing only errors" \

check_if_exists "run per-task BPF monitor tracing only errors" \
"$RV mon tqueue -t -E" "$RVTOOL/bpf_monitors/tqueue.o" \
- "$header" "event_monitor"
+ "$header" "rv: event"

check_if_exists "run per-cpu BPF monitor tracing only errors" \
"$RV mon nohz -t -E" "$RVTOOL/bpf_monitors/nohz.o" \
- "error_monitor" "event_monitor"
+ "rv: error" "rv: event"

test_end