Skip to contentSkip to conversation

selftests/bpf: convert lirc_mode2 to prog_tests and extend coverage

PATCH Superseded Sean Young 2 participants 2 messages Aug 20

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Sean Young

author
#0 lore raw

PATCH v2 1/1 selftests/bpf: convert lirc_mode2 to prog_tests and extend coverage

While porting, extend coverage of the BPF_LIRC_MODE2 attach/detach/ query API:

- bpf_prog_attach() with invalid flags is rejected with -EINVAL and does not attach the program
- bpf_prog_query() with invalid flags is rejected with -EINVAL without disturbing existing attachments
- bpf_prog_query() reports the correct program id, not just count, at each step, via bpf_prog_get_info_by_fd()
- a lirc chardev can hold more than one attached program: load a second, independent instance, attach it alongside the first, confirm both are reported by bpf_prog_query(), then detach it without disturbing the first program's attachment
- detaching an already-detached program consistently fails with -ENOENT, for both the first and second program

Assisted-by Claude:claude-sonnet-5

---
Since v1:
- Remove obsolete test_lirc_mode2_user.c file
- Use SYS_NOFAIL() rather than system()
- Fix comment formatting
- Fix leaks in error paths

 tools/testing/selftests/bpf/.gitignore        |   1 -
 tools/testing/selftests/bpf/Makefile          |   3 -
 .../selftests/bpf/prog_tests/lirc_mode2.c     | 334 ++++++++++++++++++
 .../testing/selftests/bpf/progs/lirc_mode2.c  |  32 ++
 .../bpf/progs/test_lirc_mode2_kern.c          |  26 --
 .../testing/selftests/bpf/test_lirc_mode2.sh  |  41 ---
 .../selftests/bpf/test_lirc_mode2_user.c      | 177 ----------
 7 files changed, 366 insertions(+), 248 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/lirc_mode2.c
 create mode 100644 tools/testing/selftests/bpf/progs/lirc_mode2.c
 delete mode 100644 tools/testing/selftests/bpf/progs/test_lirc_mode2_kern.c
 delete mode 100755 tools/testing/selftests/bpf/test_lirc_mode2.sh
 delete mode 100644 tools/testing/selftests/bpf/test_lirc_mode2_user.c
tools/testing/selftests/bpf/.gitignore +0 -1
index 986a6389186b..b815bf0d8877 100644
@@ -17,7 +17,6 @@ test_verifier_log
 feature
 urandom_read
 test_sockmap
-test_lirc_mode2_user
 flow_dissector_load
 test_tcpnotify_user
 test_libbpf
tools/testing/selftests/bpf/Makefile +0 -3
index 2b2f93dec474..f8ec0f574559 100644
@@ -123,7 +123,6 @@ TEST_FILES = xsk_prereqs.sh $(wildcard progs/btf_dump_test_case_*.c)
 
 # Order correspond to 'make run_tests' order
 TEST_PROGS := test_kmod.sh \
-	test_lirc_mode2.sh \
 	test_bpftool_build.sh \
 	test_doc_build.sh \
 	test_xsk.sh \
@@ -141,7 +140,6 @@ TEST_GEN_PROGS_EXTENDED = \
 	bench \
 	flow_dissector_load \
 	test_cpp \
-	test_lirc_mode2_user \
 	veristat \
 	xdp_features \
 	xdp_hw_metadata \
@@ -338,7 +336,6 @@ $(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS)
 $(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS)
 $(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS)
 $(OUTPUT)/test_tag: $(TESTING_HELPERS)
-$(OUTPUT)/test_lirc_mode2_user: $(TESTING_HELPERS)
 $(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS)
 $(OUTPUT)/test_maps: $(TESTING_HELPERS)
 $(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) $(UNPRIV_HELPERS)
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +334 -0

334 lines not on this page · raw message

tools/testing/selftests/bpf/progs/lirc_mode2.c new file +32 -0
new file mode 100644
index 000000000000..98137f3c5c03
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+// test ir decoder
+//
+// Copyright (C) 2018 Sean Young <[email protected]>
+
+#include <linux/bpf.h>
+#include <linux/lirc.h>
+#include <bpf/bpf_helpers.h>
+
+SEC("lirc_mode2")
+int bpf_decoder(unsigned int *sample)
+{
+	if (LIRC_IS_PULSE(*sample)) {
+		unsigned int duration = LIRC_VALUE(*sample);
+
+		/*
+		 * Flag bits picked deliberately low: rc-loopback simulates
+		 * a receiver overflow for any pulse over MS_TO_US(50) (see
+		 * loop_tx_ir() in rc-loopback.c), which would silently
+		 * swallow the sample before it ever reaches this decoder.
+		 */
+		if (duration & 0x8000)
+			bpf_rc_keydown(sample, 0x40, duration & 0x3fff, 0);
+		if (duration & 0x4000)
+			bpf_rc_pointer_rel(sample, (duration >> 7) & 0x7f,
+					   duration & 0x7f);
+	}
+
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/test_lirc_mode2_kern.c deleted +0 -26
deleted file mode 100644
index cbe4284c032f..000000000000
@@ -1,26 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-// test ir decoder
-//
-// Copyright (C) 2018 Sean Young <[email protected]>
-
-#include <linux/bpf.h>
-#include <linux/lirc.h>
-#include <bpf/bpf_helpers.h>
-
-SEC("lirc_mode2")
-int bpf_decoder(unsigned int *sample)
-{
-	if (LIRC_IS_PULSE(*sample)) {
-		unsigned int duration = LIRC_VALUE(*sample);
-
-		if (duration & 0x1000)
-			bpf_rc_keydown(sample, 0x40, duration & 0xffff, 0);
-		if (duration & 0x2000)
-			bpf_rc_pointer_rel(sample, (duration >> 8) & 0xff,
-					   duration & 0xff);
-	}
-
-	return 0;
-}
-
-char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_lirc_mode2.sh deleted +0 -41

41 lines not on this page · raw message

tools/testing/selftests/bpf/test_lirc_mode2_user.c deleted +0 -177

177 lines not on this page · raw message

Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Low] A stray `?` character before a closing brace causes a compilation error. - [Low] Multi-line block…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Low] A stray ? character before a closing brace causes a compilation error.
- [Low] Multi-line block comments use C++ style // instead of the required kernel /* ... */ style.
- [Low] The printed error message for missing rc devices incorrectly repeats CONFIG_RC_LOOPBACK. --

--- Patch [1]: [PATCH v2] selftests/bpf: convert lirc_mode2 to prog_tests and extend coverage --- commit b838818703c3bdf7a467c66079361ca289b5a98e
Author: Sean Young <[email protected]>

selftests/bpf: convert lirc_mode2 to prog_tests and extend coverage

This patch ports the lirc_mode2 test to the prog_tests framework and extends its coverage. It tests attaching, querying, and detaching BPF programs to and from a lirc chardev.

Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +11 -0
new file mode 100644
index 0000000000000..8e485db70b78a
@@ -0,0 +1,334 @@
+// SPDX-License-Identifier: GPL-2.0
+// test ir decoder
+//
+// Copyright (C) 2018 Sean Young <[email protected]>
+
+// A lirc chardev is a device representing a consumer IR (cir) device which
+// can receive infrared signals from remote control and/or transmit IR.
+//
+// IR is sent as a series of pulses and space somewhat like morse code. The
+// BPF program can decode this into scancodes so that rc-core can translate
+// this into input key codes using the rc keymap.

tools/testing/selftests/bpf/prog_tests/lirc_mode2.c line 21

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +10 -0
+//
+// This test works by sending IR over rc-loopback, so the IR is processed by
+// BPF and then decoded into scancodes. The lirc chardev must be the one
+// associated with rc-loopback, see the output of ir-keytable(1).
+//
+// The following CONFIG options must be enabled for the test to succeed:
+// CONFIG_RC_CORE=y
+// CONFIG_BPF_RAWIR_EVENT=y
+// CONFIG_RC_LOOPBACK=y
+// CONFIG_LIRC=y

[Severity: Low]
Is it possible to use standard C-style multi-line comments (/* ... */) here instead of C++ style comments? The BPF subsystem guidelines prefer the standard kernel multi-line comment style.

[ ... ]

tools/testing/selftests/bpf/prog_tests/lirc_mode2.c line 116

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +4 -0
+out:
+	if (!found) {
+		fprintf(stderr, "No rc devices found\n");
+		fprintf(stderr, "Enable CONFIG_RC_LOOPBACK, CONFIG_RC_LOOPBACK, and CONFIG_LIRC\n");

[Severity: Low]
This isn't a bug, but there's a typo here where CONFIG_RC_LOOPBACK is listed twice in the error message.

tools/testing/selftests/bpf/prog_tests/lirc_mode2.c line 117

Quoted 1 line
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +1 -0
+?	}

[Severity: Low]
Is this stray '?' character intended? It appears this will cause a compilation error when building the selftests.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +4 -0
+
+	globfree(&gl);
+	return found;
+}

[ ... ]

tools/testing/selftests/bpf/progs/lirc_mode2.c line 4

Quoted 10 lines
tools/testing/selftests/bpf/progs/lirc_mode2.c new file +4 -0
new file mode 100644
index 0000000000000..98137f3c5c03d
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+// test ir decoder
+//
+// Copyright (C) 2018 Sean Young <[email protected]>

[Severity: Low]
Similarly here, should this be converted to a standard C-style multi-line comment block?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1