xref: /linux/tools/testing/selftests/bpf/prog_tests/cpumask.c (revision 6af91e3d2cfc8bb579b1aa2d22cd91f8c34acdf6)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3 
4 #include <test_progs.h>
5 #include "cpumask_failure.skel.h"
6 #include "cpumask_success.skel.h"
7 
8 static const char * const cpumask_success_testcases[] = {
9 	"test_alloc_free_cpumask",
10 	"test_set_clear_cpu",
11 	"test_setall_clear_cpu",
12 	"test_first_firstzero_cpu",
13 	"test_firstand_nocpu",
14 	"test_test_and_set_clear",
15 	"test_and_or_xor",
16 	"test_intersects_subset",
17 	"test_copy_any_anyand",
18 	"test_insert_leave",
19 	"test_insert_remove_release",
20 	"test_global_mask_rcu",
21 	"test_global_mask_array_one_rcu",
22 	"test_global_mask_array_rcu",
23 	"test_global_mask_array_l2_rcu",
24 	"test_global_mask_nested_rcu",
25 	"test_global_mask_nested_deep_rcu",
26 	"test_cpumask_weight",
27 };
28 
29 static void verify_success(const char *prog_name)
30 {
31 	struct cpumask_success *skel;
32 	struct bpf_program *prog;
33 	struct bpf_link *link = NULL;
34 	pid_t child_pid;
35 	int status, err;
36 
37 	skel = cpumask_success__open();
38 	if (!ASSERT_OK_PTR(skel, "cpumask_success__open"))
39 		return;
40 
41 	skel->bss->pid = getpid();
42 	skel->bss->nr_cpus = libbpf_num_possible_cpus();
43 
44 	err = cpumask_success__load(skel);
45 	if (!ASSERT_OK(err, "cpumask_success__load"))
46 		goto cleanup;
47 
48 	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
49 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
50 		goto cleanup;
51 
52 	link = bpf_program__attach(prog);
53 	if (!ASSERT_OK_PTR(link, "bpf_program__attach"))
54 		goto cleanup;
55 
56 	child_pid = fork();
57 	if (!ASSERT_GT(child_pid, -1, "child_pid"))
58 		goto cleanup;
59 	if (child_pid == 0)
60 		_exit(0);
61 	waitpid(child_pid, &status, 0);
62 	ASSERT_OK(skel->bss->err, "post_wait_err");
63 
64 cleanup:
65 	bpf_link__destroy(link);
66 	cpumask_success__destroy(skel);
67 }
68 
69 void test_cpumask(void)
70 {
71 	int i;
72 
73 	for (i = 0; i < ARRAY_SIZE(cpumask_success_testcases); i++) {
74 		if (!test__start_subtest(cpumask_success_testcases[i]))
75 			continue;
76 
77 		verify_success(cpumask_success_testcases[i]);
78 	}
79 
80 	RUN_TESTS(cpumask_success);
81 	RUN_TESTS(cpumask_failure);
82 }
83