Commit 11642eb9 authored by Jiri Olsa's avatar Jiri Olsa Committed by Alexei Starovoitov

selftests/bpf: Use un/load_bpf_testmod functions in tests

Now that we have un/load_bpf_testmod helpers in testing_helpers.h,
we can use it in other tests and save some lines.
Acked-by: default avatarDavid Vernet <void@manifault.com>
Signed-off-by: default avatarJiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/r/20230515133756.1658301-7-jolsa@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent b58f3f0e
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "ksym_race.skel.h" #include "ksym_race.skel.h"
#include "bpf_mod_race.skel.h" #include "bpf_mod_race.skel.h"
#include "kfunc_call_race.skel.h" #include "kfunc_call_race.skel.h"
#include "testing_helpers.h"
/* This test crafts a race between btf_try_get_module and do_init_module, and /* This test crafts a race between btf_try_get_module and do_init_module, and
* checks whether btf_try_get_module handles the invocation for a well-formed * checks whether btf_try_get_module handles the invocation for a well-formed
...@@ -44,35 +45,10 @@ enum bpf_test_state { ...@@ -44,35 +45,10 @@ enum bpf_test_state {
static _Atomic enum bpf_test_state state = _TS_INVALID; static _Atomic enum bpf_test_state state = _TS_INVALID;
static int sys_finit_module(int fd, const char *param_values, int flags)
{
return syscall(__NR_finit_module, fd, param_values, flags);
}
static int sys_delete_module(const char *name, unsigned int flags)
{
return syscall(__NR_delete_module, name, flags);
}
static int load_module(const char *mod)
{
int ret, fd;
fd = open("bpf_testmod.ko", O_RDONLY);
if (fd < 0)
return fd;
ret = sys_finit_module(fd, "", 0);
close(fd);
if (ret < 0)
return ret;
return 0;
}
static void *load_module_thread(void *p) static void *load_module_thread(void *p)
{ {
if (!ASSERT_NEQ(load_module("bpf_testmod.ko"), 0, "load_module_thread must fail")) if (!ASSERT_NEQ(load_bpf_testmod(false), 0, "load_module_thread must fail"))
atomic_store(&state, TS_MODULE_LOAD); atomic_store(&state, TS_MODULE_LOAD);
else else
atomic_store(&state, TS_MODULE_LOAD_FAIL); atomic_store(&state, TS_MODULE_LOAD_FAIL);
...@@ -124,7 +100,7 @@ static void test_bpf_mod_race_config(const struct test_config *config) ...@@ -124,7 +100,7 @@ static void test_bpf_mod_race_config(const struct test_config *config)
if (!ASSERT_NEQ(fault_addr, MAP_FAILED, "mmap for uffd registration")) if (!ASSERT_NEQ(fault_addr, MAP_FAILED, "mmap for uffd registration"))
return; return;
if (!ASSERT_OK(sys_delete_module("bpf_testmod", 0), "unload bpf_testmod")) if (!ASSERT_OK(unload_bpf_testmod(false), "unload bpf_testmod"))
goto end_mmap; goto end_mmap;
skel = bpf_mod_race__open(); skel = bpf_mod_race__open();
...@@ -202,8 +178,8 @@ static void test_bpf_mod_race_config(const struct test_config *config) ...@@ -202,8 +178,8 @@ static void test_bpf_mod_race_config(const struct test_config *config)
bpf_mod_race__destroy(skel); bpf_mod_race__destroy(skel);
ASSERT_OK(kern_sync_rcu(), "kern_sync_rcu"); ASSERT_OK(kern_sync_rcu(), "kern_sync_rcu");
end_module: end_module:
sys_delete_module("bpf_testmod", 0); unload_bpf_testmod(false);
ASSERT_OK(load_module("bpf_testmod.ko"), "restore bpf_testmod"); ASSERT_OK(load_bpf_testmod(false), "restore bpf_testmod");
end_mmap: end_mmap:
munmap(fault_addr, 4096); munmap(fault_addr, 4096);
atomic_store(&state, _TS_INVALID); atomic_store(&state, _TS_INVALID);
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <test_progs.h> #include <test_progs.h>
#include <stdbool.h> #include <stdbool.h>
#include "test_module_attach.skel.h" #include "test_module_attach.skel.h"
#include "testing_helpers.h"
static int duration; static int duration;
...@@ -32,11 +33,6 @@ static int trigger_module_test_writable(int *val) ...@@ -32,11 +33,6 @@ static int trigger_module_test_writable(int *val)
return 0; return 0;
} }
static int delete_module(const char *name, int flags)
{
return syscall(__NR_delete_module, name, flags);
}
void test_module_attach(void) void test_module_attach(void)
{ {
const int READ_SZ = 456; const int READ_SZ = 456;
...@@ -93,21 +89,21 @@ void test_module_attach(void) ...@@ -93,21 +89,21 @@ void test_module_attach(void)
if (!ASSERT_OK_PTR(link, "attach_fentry")) if (!ASSERT_OK_PTR(link, "attach_fentry"))
goto cleanup; goto cleanup;
ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module"); ASSERT_ERR(unload_bpf_testmod(false), "unload_bpf_testmod");
bpf_link__destroy(link); bpf_link__destroy(link);
link = bpf_program__attach(skel->progs.handle_fexit); link = bpf_program__attach(skel->progs.handle_fexit);
if (!ASSERT_OK_PTR(link, "attach_fexit")) if (!ASSERT_OK_PTR(link, "attach_fexit"))
goto cleanup; goto cleanup;
ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module"); ASSERT_ERR(unload_bpf_testmod(false), "unload_bpf_testmod");
bpf_link__destroy(link); bpf_link__destroy(link);
link = bpf_program__attach(skel->progs.kprobe_multi); link = bpf_program__attach(skel->progs.kprobe_multi);
if (!ASSERT_OK_PTR(link, "attach_kprobe_multi")) if (!ASSERT_OK_PTR(link, "attach_kprobe_multi"))
goto cleanup; goto cleanup;
ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module"); ASSERT_ERR(unload_bpf_testmod(false), "unload_bpf_testmod");
bpf_link__destroy(link); bpf_link__destroy(link);
cleanup: cleanup:
......
...@@ -338,7 +338,7 @@ static int delete_module(const char *name, int flags) ...@@ -338,7 +338,7 @@ static int delete_module(const char *name, int flags)
return syscall(__NR_delete_module, name, flags); return syscall(__NR_delete_module, name, flags);
} }
void unload_bpf_testmod(bool verbose) int unload_bpf_testmod(bool verbose)
{ {
if (kern_sync_rcu()) if (kern_sync_rcu())
fprintf(stdout, "Failed to trigger kernel-side RCU sync!\n"); fprintf(stdout, "Failed to trigger kernel-side RCU sync!\n");
...@@ -346,13 +346,14 @@ void unload_bpf_testmod(bool verbose) ...@@ -346,13 +346,14 @@ void unload_bpf_testmod(bool verbose)
if (errno == ENOENT) { if (errno == ENOENT) {
if (verbose) if (verbose)
fprintf(stdout, "bpf_testmod.ko is already unloaded.\n"); fprintf(stdout, "bpf_testmod.ko is already unloaded.\n");
return; return -1;
} }
fprintf(stdout, "Failed to unload bpf_testmod.ko from kernel: %d\n", -errno); fprintf(stdout, "Failed to unload bpf_testmod.ko from kernel: %d\n", -errno);
return; return -1;
} }
if (verbose) if (verbose)
fprintf(stdout, "Successfully unloaded bpf_testmod.ko.\n"); fprintf(stdout, "Successfully unloaded bpf_testmod.ko.\n");
return 0;
} }
int load_bpf_testmod(bool verbose) int load_bpf_testmod(bool verbose)
......
...@@ -30,7 +30,7 @@ int parse_test_list_file(const char *path, ...@@ -30,7 +30,7 @@ int parse_test_list_file(const char *path,
__u64 read_perf_max_sample_freq(void); __u64 read_perf_max_sample_freq(void);
int load_bpf_testmod(bool verbose); int load_bpf_testmod(bool verbose);
void unload_bpf_testmod(bool verbose); int unload_bpf_testmod(bool verbose);
int kern_sync_rcu(void); int kern_sync_rcu(void);
#endif /* __TESTING_HELPERS_H */ #endif /* __TESTING_HELPERS_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment