Thread (48 messages) flat view 48 messages, 6 authors, 5d ago
COOLING5d

[RFC PATCH 05/20] rv: Add in-kernel support for BPF monitors

From: Gabriele Monaco <gmonaco@redhat.com>
Date: 2026-08-31 09:07:21
Also in: bpf, lkml
Subsystem: runtime verification (rv), the rest, tracing · Maintainers: Steven Rostedt, Gabriele Monaco, Linus Torvalds, Masami Hiramatsu

Add support for BPF monitors via the struct_ops mechanism, which allows
BPF monitors to be loaded and appear almost like in-kernel modules.

Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
---
 kernel/trace/rv/Kconfig  |  10 ++++
 kernel/trace/rv/Makefile |   1 +
 kernel/trace/rv/rv_bpf.c | 118 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 129 insertions(+)
 create mode 100644 kernel/trace/rv/rv_bpf.c
diff --git a/kernel/trace/rv/Kconfig b/kernel/trace/rv/Kconfig
index efa930f94ea4..be11e5c85a60 100644
--- a/kernel/trace/rv/Kconfig
+++ b/kernel/trace/rv/Kconfig
@@ -126,3 +126,13 @@ config RV_MONITORS_KUNIT_TEST
 	  unrelated KUnit test is running.
 
 	  If unsure, say N.
+
+config RV_MON_BPF_STRUCT_OPS
+	bool "BPF struct_ops support for RV monitors"
+	depends on RV && BPF_SYSCALL && BPF_JIT
+	help
+	  Enable BPF programs to register as Runtime Verification monitors
+	  using the BPF struct_ops mechanism. This allows BPF monitors to
+	  be managed through the standard RV interface in sysfs alongside
+	  kernel monitors.
+	  It has no performance impact if not used.
diff --git a/kernel/trace/rv/Makefile b/kernel/trace/rv/Makefile
index cdbf68c84f5a..635461395cba 100644
--- a/kernel/trace/rv/Makefile
+++ b/kernel/trace/rv/Makefile
@@ -26,3 +26,4 @@ obj-$(CONFIG_RV_REACTORS) += rv_reactors.o
 obj-$(CONFIG_RV_REACT_PRINTK) += reactor_printk.o
 obj-$(CONFIG_RV_REACT_PANIC) += reactor_panic.o
 obj-$(CONFIG_RV_MONITORS_KUNIT_TEST) += rv_monitors_test.o
+obj-$(CONFIG_RV_MON_BPF_STRUCT_OPS) += rv_bpf.o
diff --git a/kernel/trace/rv/rv_bpf.c b/kernel/trace/rv/rv_bpf.c
new file mode 100644
index 000000000000..0450bcaf1b79
--- /dev/null
+++ b/kernel/trace/rv/rv_bpf.c
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF struct_ops support for Runtime Verification monitors
+ *
+ * Allows BPF programs to register as RV monitors via struct_ops.
+ * BPF monitors appear in /sys/kernel/tracing/rv/ alongside kernel monitors.
+ *
+ * Copyright (C) 2026-2029 Red Hat, Inc. Gabriele Monaco <gmonaco@redhat.com>
+ */
+
+#include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/filter.h>
+#include <linux/rv.h>
+
+static int bpf_rv_monitor_init(struct btf *btf)
+{
+	return 0;
+}
+
+static int bpf_rv_monitor_init_member(const struct btf_type *t,
+				      const struct btf_member *member,
+				      void *kdata, const void *udata)
+{
+	const struct rv_monitor *umon = udata;
+	struct rv_monitor *kmon = kdata;
+	u32 moff = __btf_member_bit_offset(t, member) / 8;
+	int ret;
+
+	switch (moff) {
+	case offsetof(struct rv_monitor, name):
+		ret = bpf_obj_name_cpy(kmon->name, umon->name,
+				       sizeof(kmon->name));
+		if (ret < 0)
+			return ret;
+		if (ret == 0)
+			return -EINVAL;
+		return 1;
+	case offsetof(struct rv_monitor, description):
+		ret = strscpy(kmon->description, umon->description);
+		if (ret < 0)
+			return ret;
+		if (ret == 0)
+			return -EINVAL;
+		return 1;
+	}
+
+	return 0;
+}
+
+static int bpf_rv_monitor_reg(void *kdata, struct bpf_link *link)
+{
+	struct rv_monitor *mon = kdata;
+
+	pr_info("rv: Registering BPF monitor %s\n", mon->name);
+	return rv_register_monitor(mon, NULL);
+}
+
+static void bpf_rv_monitor_unreg(void *kdata, struct bpf_link *link)
+{
+	struct rv_monitor *mon = kdata;
+
+	pr_info("rv: Unregistering BPF monitor %s\n", mon->name);
+	rv_unregister_monitor(mon);
+}
+
+static int bpf_rv_monitor_validate(void *kdata)
+{
+	struct rv_monitor *mon = kdata;
+
+	if (!mon->enable)
+		return -EINVAL;
+
+	return 0;
+}
+
+static const struct bpf_verifier_ops bpf_rv_monitor_verifier_ops = {
+	.get_func_proto = bpf_base_func_proto,
+	.is_valid_access = NULL,
+};
+
+static int rv_ops__mon_enable(void)
+{
+	return 0;
+}
+
+static void rv_ops__mon_disable(void) { }
+
+static void rv_ops__mon_reset(void) { }
+
+static struct rv_monitor __bpf_ops_rv_monitor = {
+	.name = "rv_monitor",
+	.description = "stub BPF monitor.",
+	.enable = rv_ops__mon_enable,
+	.disable = rv_ops__mon_disable,
+	.reset = rv_ops__mon_reset,
+	.enabled = 0,
+};
+
+static struct bpf_struct_ops bpf_rv_monitor_ops = {
+	.verifier_ops = &bpf_rv_monitor_verifier_ops,
+	.init = bpf_rv_monitor_init,
+	.init_member = bpf_rv_monitor_init_member,
+	.reg = bpf_rv_monitor_reg,
+	.unreg = bpf_rv_monitor_unreg,
+	.validate = bpf_rv_monitor_validate,
+	.name = "rv_monitor",
+	.cfi_stubs = &__bpf_ops_rv_monitor,
+	.owner = THIS_MODULE,
+};
+
+static int __init bpf_rv_monitor_init_ops(void)
+{
+	return register_bpf_struct_ops(&bpf_rv_monitor_ops, rv_monitor);
+}
+late_initcall(bpf_rv_monitor_init_ops);
-- 
2.55.0
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help