From: Daniel Hodges <hodges.daniel.scott@gmail.com>
To: ast@kernel.org, pavel@ucw.cz, lee@kernel.org,
linux-leds@vger.kernel.org, daniel@iogearbox.net,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Daniel Hodges <hodges.daniel.scott@gmail.com>
Subject: [PATCH 1/3] leds: trigger: legtrig-bpf: Add ledtrig-bpf module
Date: Fri, 22 Mar 2024 10:08:14 -0400 [thread overview]
Message-ID: <ac8e77881212e18d117059a698affd6afc2607af.1711113657.git.hodges.daniel.scott@gmail.com> (raw)
In-Reply-To: <cover.1711113657.git.hodges.daniel.scott@gmail.com>
This patch adds a led trigger that interfaces with the bpf subsystem. It
allows for BPF programs to control LED activity through calling bpf
kfuncs. This functionality is useful in giving users a physical
indication that a BPF program has performed an operation such as
handling a packet or probe point.
Signed-off-by: Daniel Hodges <hodges.daniel.scott@gmail.com>
---
drivers/leds/trigger/Kconfig | 10 +++++
drivers/leds/trigger/Makefile | 1 +
drivers/leds/trigger/ledtrig-bpf.c | 72 ++++++++++++++++++++++++++++++
3 files changed, 83 insertions(+)
create mode 100644 drivers/leds/trigger/ledtrig-bpf.c
diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig
index d11d80176fc0..30b0fd3847be 100644
--- a/drivers/leds/trigger/Kconfig
+++ b/drivers/leds/trigger/Kconfig
@@ -152,4 +152,14 @@ config LEDS_TRIGGER_TTY
When build as a module this driver will be called ledtrig-tty.
+config LEDS_TRIGGER_BPF
+ tristate "LED BPF Trigger"
+ depends on BPF
+ depends on BPF_SYSCALL
+ help
+ This allows LEDs to be controlled by the BPF subsystem. This trigger
+ must be used with a loaded BPF program in order to control LED state.
+ BPF programs can control LED state with kfuncs.
+ If unsure, say N.
+
endif # LEDS_TRIGGERS
diff --git a/drivers/leds/trigger/Makefile b/drivers/leds/trigger/Makefile
index 25c4db97cdd4..ac47128d406c 100644
--- a/drivers/leds/trigger/Makefile
+++ b/drivers/leds/trigger/Makefile
@@ -16,3 +16,4 @@ obj-$(CONFIG_LEDS_TRIGGER_NETDEV) += ledtrig-netdev.o
obj-$(CONFIG_LEDS_TRIGGER_PATTERN) += ledtrig-pattern.o
obj-$(CONFIG_LEDS_TRIGGER_AUDIO) += ledtrig-audio.o
obj-$(CONFIG_LEDS_TRIGGER_TTY) += ledtrig-tty.o
+obj-$(CONFIG_LEDS_TRIGGER_BPF) += ledtrig-bpf.o
diff --git a/drivers/leds/trigger/ledtrig-bpf.c b/drivers/leds/trigger/ledtrig-bpf.c
new file mode 100644
index 000000000000..e3b0b8281b70
--- /dev/null
+++ b/drivers/leds/trigger/ledtrig-bpf.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * LED BPF Trigger
+ *
+ * Author: Daniel Hodges <hodges.daniel.scott@gmail.com>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/leds.h>
+
+
+DEFINE_LED_TRIGGER(ledtrig_bpf);
+
+__bpf_hook_start()
+
+__bpf_kfunc void bpf_ledtrig_blink(unsigned long delay_on, unsigned long delay_off, int invert)
+{
+ led_trigger_blink_oneshot(ledtrig_bpf, delay_on, delay_off, invert);
+}
+__bpf_hook_end();
+
+BTF_SET8_START(ledtrig_bpf_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_ledtrig_blink)
+BTF_SET8_END(ledtrig_bpf_kfunc_ids)
+
+static const struct btf_kfunc_id_set ledtrig_bpf_kfunc_set = {
+ .owner = THIS_MODULE,
+ .set = &ledtrig_bpf_kfunc_ids,
+};
+
+static int init_bpf(void)
+{
+ int ret;
+
+ ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC,
+ &ledtrig_bpf_kfunc_set);
+ ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
+ &ledtrig_bpf_kfunc_set);
+ ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
+ &ledtrig_bpf_kfunc_set);
+ ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SK_SKB,
+ &ledtrig_bpf_kfunc_set);
+ ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS,
+ &ledtrig_bpf_kfunc_set);
+ ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT,
+ &ledtrig_bpf_kfunc_set);
+ return ret;
+}
+
+static int ledtrig_bpf_init(void)
+{
+ led_trigger_register_simple("bpf", &ledtrig_bpf);
+
+ return init_bpf();
+}
+
+static void __exit ledtrig_bpf_exit(void)
+{
+ led_trigger_unregister_simple(ledtrig_bpf);
+}
+
+module_init(ledtrig_bpf_init);
+module_exit(ledtrig_bpf_exit);
+
+MODULE_AUTHOR("Daniel Hodges <hodges.daniel.scott@gmail.com>");
+MODULE_DESCRIPTION("BPF LED trigger");
+MODULE_LICENSE("GPL v2");
--
2.43.0
next prev parent reply other threads:[~2024-03-22 14:08 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-22 14:08 [PATCH 0/3] leds: trigger: Add led trigger for bpf Daniel Hodges
2024-03-22 14:08 ` Daniel Hodges [this message]
2024-03-22 15:24 ` [PATCH 1/3] leds: trigger: legtrig-bpf: Add ledtrig-bpf module Yonghong Song
2024-03-22 21:52 ` Pavel Machek
2024-03-23 19:19 ` Alexei Starovoitov
2024-03-23 22:25 ` Andrew Lunn
2024-03-27 1:53 ` Daniel Hodges
2024-03-23 22:18 ` Andrew Lunn
2024-03-24 2:15 ` Daniel Hodges
2024-03-24 2:15 ` Daniel Hodges
2024-03-26 1:14 ` [PATCH v2 0/3] leds: trigger: legtrig-bpf: Add ledtrig-bpf trigger Daniel Hodges
2024-03-26 1:14 ` [PATCH v2 1/3] " Daniel Hodges
2024-04-11 10:40 ` Lee Jones
2024-03-26 1:14 ` [PATCH v2 2/3] selftests/bpf: Add selftests for bpf led programs Daniel Hodges
2024-03-26 1:14 ` [PATCH v2 3/3] leds: trigger: Add documentation for ledtrig-bpf Daniel Hodges
2024-04-11 10:46 ` Lee Jones
2024-03-22 14:08 ` [PATCH 2/3] selftests/bpf: Add selftests for bpf led programs Daniel Hodges
2024-03-22 14:08 ` [PATCH 3/3] leds: trigger: Add documentation for ledtrig-bpf Daniel Hodges
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=ac8e77881212e18d117059a698affd6afc2607af.1711113657.git.hodges.daniel.scott@gmail.com \
--to=hodges.daniel.scott@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=lee@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=pavel@ucw.cz \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®