Skip to contentSkip to conversation

selftests/bpf: Speed up module_attach test

PATCH Applied Viktor Malik 4 participants 4 messages Mar 6 – Mar 11

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Speed up module_attach test

The module_attach test contains subtests which check that unloading a module while there are BPF programs attached to its functions is not possible because the module is still referenced.

The problem is that the test calls the generic unload_module() helper function which is used for module cleanup after test_progs terminate and tries to wait until all module references are released. This unnecessarily slows down the module_attach subtests since each unsuccessful call to unload_module() takes about 1 second.

Introduce try_unload_module() which takes the number of retries as a parameter. Make unload_module() call it with the currently used amount of 10000 retries but call it with just 1 retry from module_attach tests as it is always expected to fail. This speeds up the module_attach() test significantly.

Before:

    # time ./test_progs -t module_attach
    [...]
    Summary: 1/14 PASSED, 0 SKIPPED, 0 FAILED
    real        0m5.011s
    user        0m0.293s
    sys         0m0.108s

After:

    # time ./test_progs -t module_attach
    [...]
    Summary: 1/14 PASSED, 0 SKIPPED, 0 FAILED
    real        0m0.350s
    user        0m0.197s
    sys         0m0.063s
---
 tools/testing/selftests/bpf/prog_tests/module_attach.c | 2 +-
 tools/testing/selftests/bpf/testing_helpers.c          | 9 +++++++--
 tools/testing/selftests/bpf/testing_helpers.h          | 1 +
 3 files changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/module_attach.c +1 -1
index 67763f609b6e..92c336333fcb 100644
@@ -151,7 +151,7 @@ static void test_module_attach_detach(const char *prog_name)
 	if (!ASSERT_OK_PTR(link, "module_attach attach"))
 		goto cleanup;
 
-	ASSERT_ERR(unload_bpf_testmod(false), "unload_bpf_testmod");
+	ASSERT_ERR(try_unload_module("bpf_testmod", 1, false), "try_unload_module");
 	bpf_link__destroy(link);
 cleanup:
 	test_module_attach__destroy(skel);
tools/testing/selftests/bpf/testing_helpers.c +7 -2
index 66af0d13751a..6fbe1e995660 100644
@@ -368,7 +368,7 @@ int delete_module(const char *name, int flags)
 	return syscall(__NR_delete_module, name, flags);
 }
 
-int unload_module(const char *name, bool verbose)
+int try_unload_module(const char *name, int retries, bool verbose)
 {
 	int ret, cnt = 0;
 
@@ -379,7 +379,7 @@ int unload_module(const char *name, bool verbose)
 		ret = delete_module(name, 0);
 		if (!ret || errno != EAGAIN)
 			break;
-		if (++cnt > 10000) {
+		if (++cnt > retries) {
 			fprintf(stdout, "Unload of %s timed out\n", name);
 			break;
 		}
@@ -400,6 +400,11 @@ int unload_module(const char *name, bool verbose)
 	return 0;
 }
 
+int unload_module(const char *name, bool verbose)
+{
+	return try_unload_module(name, 10000, verbose);
+}
+
 static int __load_module(const char *path, const char *param_values, bool verbose)
 {
 	int fd;
tools/testing/selftests/bpf/testing_helpers.h +1 -0
index eb20d3772218..2ca2356a0b58 100644
@@ -40,6 +40,7 @@ int finit_module(int fd, const char *param_values, int flags);
 int delete_module(const char *name, int flags);
 int load_module(const char *path, bool verbose);
 int load_module_params(const char *path, const char *param_values, bool verbose);
+int try_unload_module(const char *name, int retries, bool verbose);
 int unload_module(const char *name, bool verbose);
 
 static inline __u64 get_time_ns(void)
Signature · 1 line
2.53.0
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 32 lines · Viktor Malik

The module_attach test contains subtests which check that unloading a module while there are BPF programs attached to its functions is not possible because the module is still referenced.

The problem is that the test calls the generic unload_module() helper function which is used for module cleanup after test_progs terminate and tries to wait until all module references are released. This unnecessarily slows down the module_attach subtests since each unsuccessful call to unload_module() takes about 1 second.

Introduce try_unload_module() which takes the number of retries as a parameter. Make unload_module() call it with the currently used amount of 10000 retries but call it with just 1 retry from module_attach tests as it is always expected to fail. This speeds up the module_attach() test significantly.

Before:

    # time ./test_progs -t module_attach
    [...]
    Summary: 1/14 PASSED, 0 SKIPPED, 0 FAILED
    real        0m5.011s
    user        0m0.293s
    sys         0m0.108s

After:

    # time ./test_progs -t module_attach
    [...]
    Summary: 1/14 PASSED, 0 SKIPPED, 0 FAILED
Quoted 5 lines
    real        0m0.350s
    user        0m0.197s
    sys         0m0.063s

nice speedup!

Quoted 65 lines
---
 tools/testing/selftests/bpf/prog_tests/module_attach.c | 2 +-
 tools/testing/selftests/bpf/testing_helpers.c          | 9 +++++++--
 tools/testing/selftests/bpf/testing_helpers.h          | 1 +
 3 files changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/module_attach.c +1 -1
index 67763f609b6e..92c336333fcb 100644
@@ -151,7 +151,7 @@ static void test_module_attach_detach(const char *prog_name)
 	if (!ASSERT_OK_PTR(link, "module_attach attach"))
 		goto cleanup;
 
-	ASSERT_ERR(unload_bpf_testmod(false), "unload_bpf_testmod");
+	ASSERT_ERR(try_unload_module("bpf_testmod", 1, false), "try_unload_module");
 	bpf_link__destroy(link);
 cleanup:
 	test_module_attach__destroy(skel);
tools/testing/selftests/bpf/testing_helpers.c +7 -2
index 66af0d13751a..6fbe1e995660 100644
@@ -368,7 +368,7 @@ int delete_module(const char *name, int flags)
 	return syscall(__NR_delete_module, name, flags);
 }
 
-int unload_module(const char *name, bool verbose)
+int try_unload_module(const char *name, int retries, bool verbose)
 {
 	int ret, cnt = 0;
 
@@ -379,7 +379,7 @@ int unload_module(const char *name, bool verbose)
 		ret = delete_module(name, 0);
 		if (!ret || errno != EAGAIN)
 			break;
-		if (++cnt > 10000) {
+		if (++cnt > retries) {
 			fprintf(stdout, "Unload of %s timed out\n", name);
 			break;
 		}
@@ -400,6 +400,11 @@ int unload_module(const char *name, bool verbose)
 	return 0;
 }
 
+int unload_module(const char *name, bool verbose)
+{
+	return try_unload_module(name, 10000, verbose);
+}
+
 static int __load_module(const char *path, const char *param_values, bool verbose)
 {
 	int fd;
tools/testing/selftests/bpf/testing_helpers.h +1 -0
index eb20d3772218..2ca2356a0b58 100644
@@ -40,6 +40,7 @@ int finit_module(int fd, const char *param_values, int flags);
 int delete_module(const char *name, int flags);
 int load_module(const char *path, bool verbose);
 int load_module_params(const char *path, const char *param_values, bool verbose);
+int try_unload_module(const char *name, int retries, bool verbose);
 int unload_module(const char *name, bool verbose);
 
 static inline __u64 get_time_ns(void)
maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Viktor Malik
Quoted 33 lines · Viktor Malik

The module_attach test contains subtests which check that unloading a module while there are BPF programs attached to its functions is not possible because the module is still referenced.

The problem is that the test calls the generic unload_module() helper function which is used for module cleanup after test_progs terminate and tries to wait until all module references are released. This unnecessarily slows down the module_attach subtests since each unsuccessful call to unload_module() takes about 1 second.

Introduce try_unload_module() which takes the number of retries as a parameter. Make unload_module() call it with the currently used amount of 10000 retries but call it with just 1 retry from module_attach tests as it is always expected to fail. This speeds up the module_attach() test significantly.

Before:

    # time ./test_progs -t module_attach
    [...]
    Summary: 1/14 PASSED, 0 SKIPPED, 0 FAILED
    real        0m5.011s
    user        0m0.293s
    sys         0m0.108s

After:

    # time ./test_progs -t module_attach
    [...]
    Summary: 1/14 PASSED, 0 SKIPPED, 0 FAILED
Quoted 6 lines
    real        0m0.350s
    user        0m0.197s
    sys         0m0.063s

---

Quoted 1 line

[...]

#3 lore raw
In reply to Viktor Malik
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 11 lines · On Fri, 6 Mar 2026 11:16:28 +0100 you wrote:

The module_attach test contains subtests which check that unloading a module while there are BPF programs attached to its functions is not possible because the module is still referenced.

The problem is that the test calls the generic unload_module() helper function which is used for module cleanup after test_progs terminate and tries to wait until all module references are released. This unnecessarily slows down the module_attach subtests since each unsuccessful call to unload_module() takes about 1 second.

[...]

Here is the summary with links:
- [bpf-next] selftests/bpf: Speed up module_attach test

    https://git.kernel.org/bpf/bpf-next/c/900b7cc73ce9

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html