[PATCH v2 2/2] selftests: ALSA: pcmtest: Load the module when it is missing
From: HyeongJun An
Date: Sat Nov 28 2026 - 01:52:48 EST
test-pcmtest-driver skips when the pcmtest debugfs files are missing.
The driver has no device to bind to, so nothing loads the module, and the
test skips even on a kernel that builds it.
Run modprobe once before skipping, as the net pktgen test does. Each test
runs in a child process, so record whether the module was loaded before
the tests start, and unload it at exit if the tests loaded it.
Signed-off-by: HyeongJun An <sammiee5311@xxxxxxxxx>
---
A built-in pcmtest creates its debugfs files at boot, so read_patterns()
succeeds and modprobe is not run. If debugfs is not mounted, modprobe of
a built-in module returns 0, the second read fails, and the test skips as
before.
.../selftests/alsa/test-pcmtest-driver.c | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/tools/testing/selftests/alsa/test-pcmtest-driver.c b/tools/testing/selftests/alsa/test-pcmtest-driver.c
index b796a5613c6f..3bd6f75ac6f6 100644
--- a/tools/testing/selftests/alsa/test-pcmtest-driver.c
+++ b/tools/testing/selftests/alsa/test-pcmtest-driver.c
@@ -6,6 +6,7 @@
* Copyright 2023 Ivan Orlov <ivan.orlov0322@xxxxxxxxx>
*/
#include <errno.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <alsa/asoundlib.h>
@@ -149,6 +150,22 @@ static int setup_handle(snd_pcm_t **handle, snd_pcm_sw_params_t *swparams,
return 0;
}
+static bool module_was_loaded;
+
+/* remember whether snd-pcmtest was loaded before the tests ran */
+static void __attribute__((constructor)) check_module(void)
+{
+ module_was_loaded = !access("/sys/module/snd_pcmtest", F_OK);
+}
+
+/* each test runs in a child, so unload in the parent once all are done */
+static void __attribute__((destructor)) unload_module(void)
+{
+ if (!module_was_loaded && !access("/sys/module/snd_pcmtest", F_OK) &&
+ system("modprobe -r -q snd-pcmtest"))
+ ksft_print_msg("Failed to unload snd-pcmtest\n");
+}
+
FIXTURE(pcmtest) {
int card;
snd_pcm_sw_params_t *swparams;
@@ -171,6 +188,9 @@ FIXTURE_SETUP(pcmtest) {
SKIP(return, "This test needs root to run!");
err = read_patterns();
+ /* nothing loads the module on its own, so ask for it */
+ if (err && !system("modprobe -q snd-pcmtest"))
+ err = read_patterns();
if (err)
SKIP(return, "Can't read patterns. Probably, module isn't loaded");
--
2.43.0