Skip to contentSkip to conversation

selftests/bpf: convert lirc_mode2 to prog_tests and extend coverage

PATCH Superseded Sean Young 3 participants 3 messages Aug 20

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Sean Young

author
#0 lore raw

PATCH 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
---
 tools/testing/selftests/bpf/Makefile          |   3 -
 .../selftests/bpf/prog_tests/lirc_mode2.c     | 328 ++++++++++++++++++
 .../testing/selftests/bpf/progs/lirc_mode2.c  |  32 ++
 .../bpf/progs/test_lirc_mode2_kern.c          |  26 --
 .../testing/selftests/bpf/test_lirc_mode2.sh  |  41 ---
 5 files changed, 360 insertions(+), 70 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
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 +328 -0

328 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

Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Missing `globfree()` when `glob()` returns an error in `find_loopback_devices`. - [Low] Orphaned…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Missing globfree() when glob() returns an error in find_loopback_devices.
- [Low] Orphaned test file test_lirc_mode2_user.c left in the source tree.
- [Low] Multi-line comment formatting violates BPF subsystem guidelines.
--

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

selftests/bpf: convert lirc_mode2 to prog_tests and extend coverage

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

[ ... ]

Quoted 4 lines
tools/testing/selftests/bpf/Makefile +0 -0
index 2b2f93dec474b..f8ec0f574559a 100644

[ ... ]

tools/testing/selftests/bpf/Makefile TEST_GEN_PROGS_EXTENDED = \ · line 143

Quoted 6 lines
tools/testing/selftests/bpf/Makefile +0 -1
@@ -141,7 +140,6 @@ TEST_GEN_PROGS_EXTENDED = \
 	bench \
 	flow_dissector_load \
 	test_cpp \
-	test_lirc_mode2_user \
 	veristat \

[Severity: Low]
This isn't a bug, but it looks like tools/testing/selftests/bpf/test_lirc_mode2_user.c was removed from the Makefile here but not deleted from the source tree. Should that file be deleted as part of this patch?

[ ... ]

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +0 -0
new file mode 100644
index 0000000000000..17a38cd03845c
@@ -0,0 +1,328 @@

[ ... ]

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

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +4 -0
+/* Read the DEVNAME= line out of the first uevent file that matches
+ * pattern, and turn it into a /dev/<name> path.
+ */
+static bool find_devname(const char *pattern, char *path, size_t path_sz)

[Severity: Low]
This isn't a bug, but does this violate the BPF subsystem multi-line comment style guidelines? The opening /* should probably be on its own line:

/*
* Read the DEVNAME= line out of the first uevent file that matches
* pattern, and turn it into a /dev/<name> path.
*/

[ ... ]

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +4 -0
+/* Load rc-loopback and find the lirc and input chardevs it created. */
+static bool find_loopback_devices(char *lirc_path, char *input_path,
+				  size_t path_sz)
+{

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

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +10 -0
+	glob_t gl = {};
+	bool found = false;
+
+	/* Ignore failure, we check for the resulting devices below. */
+	system("modprobe rc-loopback > /dev/null 2>&1");
+
+	if (glob("/sys/class/rc/rc*", 0, NULL, &gl)) {
+		fprintf(stderr, "No rc devices found, is CONFIG_RC_LOOPBACK enabled?\n");
+		return false;
+	}

[Severity: Medium]
Could this leak memory if glob() returns an error like GLOB_NOMATCH? Some implementations of glob() may allocate memory for gl_pathv even on error paths. Should there be a call to globfree(&gl) before returning false?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Sean Young
A subsystem pattern flags this as potentially concerning: this hand-rolls a fire-and-forget shell invocation when test_progs.h already provides SYS_NOFAIL(fmt, ...) at test_progs.h:444, which…
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +0 -0
new file mode 100644
index 000000000000..17a38cd03845

[ ... ]

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

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +9 -0
+/* Load rc-loopback and find the lirc and input chardevs it created. */
+static bool find_loopback_devices(char *lirc_path, char *input_path,
+				  size_t path_sz)
+{
+	glob_t gl = {};
+	bool found = false;
+
+	/* Ignore failure, we check for the resulting devices below. */
+	system("modprobe rc-loopback > /dev/null 2>&1");

A subsystem pattern flags this as potentially concerning: this hand-rolls a fire-and-forget shell invocation when test_progs.h already provides SYS_NOFAIL(fmt, ...) at test_progs.h:444, which handles both the ignored return value and the redirect to /dev/null. The line would become just SYS_NOFAIL("modprobe rc-loopback"). SYS_NOFAIL() is used 119 times across prog_tests/ for exactly this pattern.

The subsystem guide flags tests that reimplement existing helpers instead of using them, though in this case raw system() is also widespread in prog_tests/ so the behaviour is identical either way. Worth considering for consistency?

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +7 -0
+
+	if (glob("/sys/class/rc/rc*", 0, NULL, &gl)) {
+		fprintf(stderr, "No rc devices found, is CONFIG_RC_LOOPBACK enabled?\n");
+		return false;
+	}
+
+	for (size_t i = 0; i < gl.gl_pathc; i++) {

[ ... ]

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +8 -0
+		if (!strstr(uevent, "DRV_NAME=rc-loopback"))
+			continue;
+
+		snprintf(pattern, sizeof(pattern), "%s/lirc*/uevent", rcdir);
+		if (!find_devname(pattern, lirc_path, path_sz))
+			continue;
+
+		snprintf(pattern, sizeof(pattern), "%s/input*/event*/uevent", rcdir);

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

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +10 -0
+		if (!find_devname(pattern, input_path, path_sz))
+			continue;
+
+		found = true;
+		break;
+	}
+
+	globfree(&gl);
+	return found;
+}

A subsystem pattern flags this as potentially concerning: the only diagnostic is attached to the wrong condition, so the common unconfigured case skips with no explanation.

glob("/sys/class/rc/rc*") fails only when there is no rc device at all (CONFIG_RC_CORE=n or no IR hardware), yet that's where the message names CONFIG_RC_LOOPBACK. The actual CONFIG_RC_LOOPBACK=n case (rc devices exist, but none has DRV_NAME=rc-loopback) and the CONFIG_LIRC=n case (no lirc*/uevent under the loopback rc device) both fall through the loop and reach the silent 'return found' with found=false.

test_lirc_mode2() then calls test__skip() with no message (test__skip() takes no argument), so the run reports lirc_mode2:SKIP with nothing telling the reader which prerequisite was missing.

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +6 -0
+
+void test_lirc_mode2(void)
+{
+	char lirc_path[PATH_MAX], input_path[PATH_MAX];
+	int lircfd = -1, inputfd = -1, progfd, progfd2 = -1;
+	struct lirc_mode2 *skel = NULL, *skel2 = NULL;

[ ... ]

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

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/lirc_mode2.c new file +10 -0
+
+	if (getuid() != 0) {
+		test__skip();
+		return;
+	}
+
+	if (!find_loopback_devices(lirc_path, input_path, sizeof(lirc_path))) {
+		test__skip();
+		return;
+	}

A subsystem pattern flags this as potentially concerning: both skip paths call bare test__skip() with no accompanying message, so the two very different reasons for skipping are indistinguishable in the test output.

The first skips because the test is not running as root; the second skips because find_loopback_devices() could not find an rc-loopback lirc/input device pair (which can mean CONFIG_RC_LOOPBACK is not built, or modprobe rc-loopback failed, or the sysfs walk did not match). A user seeing lirc_mode2:SKIP has no way to tell which precondition was missing.

That said, bare test__skip() with no message is the overwhelmingly dominant idiom in this directory (126 call sites use it with no message), and test__skip() accepts no message parameter, so there is no in-framework way to attach a reason. By the 'compare to the neighbours' rule this would normally be consistent with established practice.

Quoted 5 lines
tools/testing/selftests/bpf/progs/lirc_mode2.c new file +0 -0
new file mode 100644
index 000000000000..98137f3c5c03

[ ... ]

Quoted 11 lines
tools/testing/selftests/bpf/progs/lirc_mode2.c new file +11 -0
+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.

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

Quoted 10 lines
tools/testing/selftests/bpf/progs/lirc_mode2.c new file +10 -0
+		 */
+		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;
+}

Does the comment match what the code does? Looking at the deleted progs/test_lirc_mode2_kern.c, the old decoder used flag bits 0x1000/0x2000 with masks 0xffff, >>8 & 0xff, and & 0xff. The new code uses 0x8000/0x4000 with masks 0x3fff, >>7 & 0x7f, and & 0x7f.

The comment claims the flag bits were "picked deliberately low" to stay under rc-loopback's MS_TO_US(50) overflow threshold, but the bits were moved UP: the largest encodable sample went from 0x3FFF (16383 us, ample margin under the 50000 us threshold) to 0xBFFF (49151 us, 849 us of margin), and with both flags set, 0xFFFF (65535 us) exceeds the threshold and would be discarded by ir_raw_event_overflow() rather than decoded.

The two committed test vectors (0x8ead and 0x4081) are both safe, so the test passes today. The concern is future-facing: the comment actively misleads anyone adding a third vector into believing high-bit values are the safe choice, when the new layout leaves only 849 us of headroom and any value with both flag bits set is silently swallowed by the loopback overflow simulation.

The new bit layout is genuinely cleaner than the old one (with flags at 0x1000/0x2000 and rel_x extracted as (duration >> 8) & 0xff, the 0x2000 flag bit landed inside rel_x, so flags and payload overlapped). Moving flags to the top two bits and shrinking the payload to 14 bits removes that overlap. Could the comment be updated to match what the code actually does, and perhaps mention the 50 ms ceiling on encodable samples?

Also, the commit message enumerates five coverage additions but does not mention the format change at all.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/32401351183