Add event_monitor and error_monitor tracepoints, carrying a monitor name and a single formatted message. These tracepoints aim to replace the per-monitor tracepoints for a set of monitors (now BPF only) and are generic enough to allow reporting from different monitor classes. 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). Signed-off-by: Gabriele Monaco --- kernel/trace/rv/Makefile | 1 + kernel/trace/rv/rv_bpf.c | 76 ++++++++++++++++++++++++++++++++++++++ kernel/trace/rv/rv_trace.h | 37 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 kernel/trace/rv/rv_bpf.c diff --git a/kernel/trace/rv/Makefile b/kernel/trace/rv/Makefile index c895d81dfdad..588250ce3fa9 100644 --- a/kernel/trace/rv/Makefile +++ b/kernel/trace/rv/Makefile @@ -3,6 +3,7 @@ ccflags-y += -I $(src) # needed for trace events obj-$(CONFIG_RV) += rv.o +obj-$(CONFIG_RV) += rv_bpf.o obj-$(CONFIG_RV_MON_WIP) += monitors/wip/wip.o obj-$(CONFIG_RV_MON_WWNR) += monitors/wwnr/wwnr.o obj-$(CONFIG_RV_MON_SCHED) += monitors/sched/sched.o diff --git a/kernel/trace/rv/rv_bpf.c b/kernel/trace/rv/rv_bpf.c new file mode 100644 index 000000000000..f39f33335c71 --- /dev/null +++ b/kernel/trace/rv/rv_bpf.c @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * BPF kfuncs for Runtime Verification (RV) + */ + +#include +#include +#include +#include +#include + +__bpf_kfunc_start_defs(); + +/** + * bpf_rv_event_monitor - emit an RV monitor event tracepoint + * + * @msg must be NULL-terminated, drop the event if it isn't. This happens by + * construction if using snprintf-like functions and passing the right @msg__sz + * to both. + * + * @mon__str: name of the monitor + * @msg: message string + * @msg__sz: message string size + * + * Return -EINVAL if @msg is not NULL-terminated, 0 otherwise. + */ +__bpf_kfunc int bpf_rv_event_monitor(const char *mon__str, const int *id__nullable, + const char *msg, u32 msg__sz) +{ + if (strnlen(msg, msg__sz) == msg__sz) + return -EINVAL; + + trace_event_monitor(mon__str, id__nullable, msg); + return 0; +} + +/** + * bpf_rv_error_monitor - emit an RV monitor error tracepoint + * + * @msg must be NULL-terminated, drop the event if it isn't. This happens by + * construction if using snprintf-like functions and passing the right @msg__sz + * to both. + * + * @mon__str: name of the monitor + * @msg: message string + * @msg__sz: message string size + * + * Return -EINVAL if @msg is not NULL-terminated, 0 otherwise. + */ +__bpf_kfunc int bpf_rv_error_monitor(const char *mon__str, const int *id__nullable, + const char *msg, u32 msg__sz) +{ + if (strnlen(msg, msg__sz) == msg__sz) + return -EINVAL; + + trace_error_monitor(mon__str, id__nullable, msg); + return 0; +} + +__bpf_kfunc_end_defs(); + +BTF_KFUNCS_START(bpf_rv_kfunc_ids) +BTF_ID_FLAGS(func, bpf_rv_event_monitor) +BTF_ID_FLAGS(func, bpf_rv_error_monitor) +BTF_KFUNCS_END(bpf_rv_kfunc_ids) + +static const struct btf_kfunc_id_set bpf_rv_kfunc_set = { + .owner = THIS_MODULE, + .set = &bpf_rv_kfunc_ids, +}; + +static int __init bpf_rv_kfunc_init(void) +{ + return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_rv_kfunc_set); +} +late_initcall(bpf_rv_kfunc_init); diff --git a/kernel/trace/rv/rv_trace.h b/kernel/trace/rv/rv_trace.h index 24d4b94a54a5..9fa9e1230e84 100644 --- a/kernel/trace/rv/rv_trace.h +++ b/kernel/trace/rv/rv_trace.h @@ -292,6 +292,43 @@ DECLARE_EVENT_CLASS(error_ltl_monitor_id, // Add new monitors based on CONFIG_LTL_MON_EVENTS_ID here #endif /* CONFIG_LTL_MON_EVENTS_ID */ +DECLARE_EVENT_CLASS(event_template_monitor, + + TP_PROTO(const char *mon, const int *id, const char *msg), + + TP_ARGS(mon, id, msg), + + TP_STRUCT__entry( + __string( mon, mon ) + __field( u8, has_id ) + __field( int, id ) + __string( msg, msg ) + ), + + TP_fast_assign( + __assign_str(mon); + __entry->has_id = !!id; + __entry->id = id ? *id : 0; + __assign_str(msg); + ), + + /* + * The id is used only for filters, if it is of interest in the format, + * callers should pass it to the msg too. + */ + TP_printk("%s: %s", + __get_str(mon), + __get_str(msg)) +); + +DEFINE_EVENT(event_template_monitor, event_monitor, + TP_PROTO(const char *mon, const int *id, const char *msg), + TP_ARGS(mon, id, msg)); + +DEFINE_EVENT(event_template_monitor, error_monitor, + TP_PROTO(const char *mon, const int *id, const char *msg), + TP_ARGS(mon, id, msg)); + #ifdef CONFIG_RV_MON_MAINTENANCE_EVENTS /* Tracepoint useful for monitors development, currenly only used in DA */ TRACE_EVENT(rv_retries_error, -- 2.56.1