All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gabriele Monaco <gmonaco@redhat.com>
To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	bpf@vger.kernel.org, Steven Rostedt <rostedt@goodmis.org>,
	Gabriele Monaco <gmonaco@redhat.com>,
	Masami Hiramatsu <mhiramat@kernel.org>
Cc: Nam Cao <namcao@linutronix.de>, Wen Yang <wen.yang@linux.dev>,
	Tobias Schaffner <tobias.schaffner@siemens.com>,
	Viktor Malik <vmalik@redhat.com>
Subject: [RFC PATCH 05/20] rv: Add in-kernel support for BPF monitors
Date: Mon, 31 Aug 2026 11:05:09 +0200	[thread overview]
Message-ID: <20260831090524.106845-6-gmonaco@redhat.com> (raw)
In-Reply-To: <20260831090524.106845-1-gmonaco@redhat.com>

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


  parent reply	other threads:[~2026-08-31  9:07 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-31  9:05 [RFC PATCH 00/20] rv: Add support for BPF monitors Gabriele Monaco
2026-08-31  9:05 ` [RFC PATCH 01/20] sched: Add task enqueue/dequeue trace points Gabriele Monaco
2026-08-31  9:32   ` sashiko-bot
2026-08-31  9:05 ` [RFC PATCH 02/20] tools/rv: Skip empty pid error in selftest if command failed Gabriele Monaco
2026-08-31  9:05 ` [RFC PATCH 03/20] rv: Refactor da_trace() functions to get strings internally Gabriele Monaco
2026-08-31  9:05 ` [RFC PATCH 04/20] rv: Use static arrays for rv_monitor name and description Gabriele Monaco
2026-08-31  9:05 ` Gabriele Monaco [this message]
2026-08-31  9:05 ` [RFC PATCH 06/20] rv: Add rv_get_monitor_by_name() Gabriele Monaco
2026-08-31  9:24   ` sashiko-bot
2026-08-31  9:05 ` [RFC PATCH 07/20] rv: Add reactors support to BPF monitors Gabriele Monaco
2026-08-31  9:05 ` [RFC PATCH 08/20] rv: Cast result of model_get_*_name() Gabriele Monaco
2026-08-31  9:05 ` [RFC PATCH 09/20] rv: Handle unregistered monitors safely in tracefs Gabriele Monaco
2026-08-31  9:24   ` sashiko-bot
2026-08-31  9:05 ` [RFC PATCH 10/20] tools/build: Add a feature test for bpftool-btf Gabriele Monaco
2026-08-31  9:05 ` [RFC PATCH 11/20] tools/rv: Move argument parsing from in_kernel to utils Gabriele Monaco
2026-08-31  9:05 ` [RFC PATCH 12/20] tools/rv: Export functionality for in_kernel monitors Gabriele Monaco
2026-08-31  9:05 ` [RFC PATCH 13/20] tools/rv: Implement BPF monitor loading and tracing Gabriele Monaco
2026-08-31  9:34   ` sashiko-bot
2026-08-31  9:05 ` [RFC PATCH 14/20] tools/rv: Implement BPF monitor registration logic Gabriele Monaco
2026-08-31  9:38   ` sashiko-bot
2026-08-31  9:05 ` [RFC PATCH 15/20] tools/rv: Copy stripped bpf_atomic.h from libarena Gabriele Monaco
2026-08-31  9:38   ` sashiko-bot
2026-08-31  9:05 ` [RFC PATCH 16/20] tools/rv: Add BPF monitors Gabriele Monaco
2026-08-31  9:40   ` sashiko-bot
2026-08-31  9:05 ` [RFC PATCH 17/20] tools/rv: Define CONFIG_X86_64 statically for " Gabriele Monaco
2026-08-31  9:05 ` [RFC PATCH 18/20] verification/rvgen: Add support " Gabriele Monaco
2026-08-31  9:41   ` sashiko-bot
2026-08-31  9:05 ` [RFC PATCH 19/20] tools/rv: Add selftest for rv bpf Gabriele Monaco
2026-08-31  9:44   ` sashiko-bot
2026-08-31  9:05 ` [RFC PATCH 20/20] verification/rvgen: Add selftest for rvgen -b Gabriele Monaco
2026-09-01 18:35 ` [RFC PATCH 00/20] rv: Add support for BPF monitors Nam Cao
2026-09-02  6:52   ` Gabriele Monaco
2026-09-02  7:46     ` Nam Cao
2026-09-03  1:57     ` Alexei Starovoitov
2026-09-03  7:21       ` Gabriele Monaco
2026-09-03 13:02         ` Steven Rostedt
2026-09-04  3:30           ` Alexei Starovoitov
2026-09-04 11:43             ` Steven Rostedt
2026-09-04 16:16               ` Alexei Starovoitov
2026-09-04 16:31                 ` Steven Rostedt
2026-09-04 17:24                   ` Steven Rostedt
2026-09-04 23:34                     ` Alexei Starovoitov
2026-09-04 23:46                       ` Steven Rostedt
2026-09-04 23:48                         ` Alexei Starovoitov
2026-09-05  0:17                           ` Steven Rostedt
2026-09-05  0:22                             ` Alexei Starovoitov
2026-09-05  4:53                               ` Gabriele Monaco
2026-09-13  1:26                                 ` Alexei Starovoitov
2026-09-04 11:23       ` Tomas Glozar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260831090524.106845-6-gmonaco@redhat.com \
    --to=gmonaco@redhat.com \
    --cc=bpf@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=namcao@linutronix.de \
    --cc=rostedt@goodmis.org \
    --cc=tobias.schaffner@siemens.com \
    --cc=vmalik@redhat.com \
    --cc=wen.yang@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.