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 | 73 ++++++++++++++++++++++++++++++
3 files changed, 84 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..99cabf816da4
--- /dev/null
+++ b/drivers/leds/trigger/ledtrig-bpf.c
@@ -0,0 +1,73 @@
+// 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/init.h>
+#include <linux/kernel.h>
+#include <linux/leds.h>
+#include <linux/module.h>
+#include <linux/rcupdate.h>
+
+
+DEFINE_LED_TRIGGER(ledtrig_bpf);
+
+__bpf_kfunc_start_defs();
+__bpf_kfunc void bpf_ledtrig_blink(const char *led_name__str, unsigned long
+ delay_on, unsigned long delay_off, int invert)
+{
+ struct led_classdev *led_cdev;
+
+ rcu_read_lock();
+ list_for_each_entry_rcu(led_cdev, &ledtrig_bpf->led_cdevs, trig_list) {
+ if (strcmp(led_name__str, led_cdev->name) == 0) {
+ led_blink_set_oneshot(led_cdev, &delay_on, &delay_off,
+ invert);
+ break;
+ }
+ }
+ rcu_read_unlock();
+}
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(ledtrig_bpf_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_ledtrig_blink, KF_TRUSTED_ARGS)
+BTF_KFUNCS_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);
+ return ret;
+}
+
+static int __init 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.2
On Mon, 25 Mar 2024, Daniel Hodges wrote:
> 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 | 73 ++++++++++++++++++++++++++++++
> 3 files changed, 84 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..99cabf816da4
> --- /dev/null
> +++ b/drivers/leds/trigger/ledtrig-bpf.c
> @@ -0,0 +1,73 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * LED BPF Trigger
> + *
> + * Author: Daniel Hodges <hodges.daniel.scott@gmail.com>
Any copyright?
> + */
> +
> +#include <linux/bpf.h>
> +#include <linux/btf.h>
> +#include <linux/btf_ids.h>
> +#include <linux/init.h>
> +#include <linux/kernel.h>
> +#include <linux/leds.h>
> +#include <linux/module.h>
> +#include <linux/rcupdate.h>
> +
> +
> +DEFINE_LED_TRIGGER(ledtrig_bpf);
> +
> +__bpf_kfunc_start_defs();
__bpf_hook_start()?
> +__bpf_kfunc void bpf_ledtrig_blink(const char *led_name__str, unsigned long
> + delay_on, unsigned long delay_off, int invert)
Nit: Try to keep the variable types and names together please.
I.e. break before the 'unsigned' or after 'delay_on'.
> +{
> + struct led_classdev *led_cdev;
> +
> + rcu_read_lock();
> + list_for_each_entry_rcu(led_cdev, &ledtrig_bpf->led_cdevs, trig_list) {
> + if (strcmp(led_name__str, led_cdev->name) == 0) {
> + led_blink_set_oneshot(led_cdev, &delay_on, &delay_off,
> + invert);
Use 100-chars to avoid the break.
> + break;
> + }
> + }
> + rcu_read_unlock();
> +}
> +__bpf_kfunc_end_defs();
_bpf_hook_end()?
> +BTF_KFUNCS_START(ledtrig_bpf_kfunc_ids)
> +BTF_ID_FLAGS(func, bpf_ledtrig_blink, KF_TRUSTED_ARGS)
> +BTF_KFUNCS_END(ledtrig_bpf_kfunc_ids)
Why not a single macro to output the full struct?
Or better yet, be less opaque and not use a macro at all?
> +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);
> + return ret;
return register_btf_kfunc_id_set()?
Or again, better yet, put the single function call directly into
ledtrig_bpf_init()? Creating a whole function just for a single
invocation seems superfluous.
> +}
> +
> +static int __init 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.2
>
--
Lee Jones [李琼斯]
© 2016 - 2026 Red Hat, Inc.