xref: /linux/tools/testing/selftests/bpf/prog_tests/cpumask.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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_global_mask_nested_deep_array_rcu",
27 	"test_cpumask_weight",
28 };
29 
30 static void verify_success(const char *prog_name)
31 {
32 	struct cpumask_success *skel;
33 	struct bpf_program *prog;
34 	struct bpf_link *link = NULL;
35 	pid_t child_pid;
36 	int status, err;
37 
38 	skel = cpumask_success__open();
39 	if (!ASSERT_OK_PTR(skel, "cpumask_success__open"))
40 		return;
41 
42 	skel->bss->pid = getpid();
43 	skel->bss->nr_cpus = libbpf_num_possible_cpus();
44 
45 	err = cpumask_success__load(skel);
46 	if (!ASSERT_OK(err, "cpumask_success__load"))
47 		goto cleanup;
48 
49 	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
50 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
51 		goto cleanup;
52 
53 	link = bpf_program__attach(prog);
54 	if (!ASSERT_OK_PTR(link, "bpf_program__attach"))
55 		goto cleanup;
56 
57 	child_pid = fork();
58 	if (!ASSERT_GT(child_pid, -1, "child_pid"))
59 		goto cleanup;
60 	if (child_pid == 0)
61 		_exit(0);
62 	waitpid(child_pid, &status, 0);
63 	ASSERT_OK(skel->bss->err, "post_wait_err");
64 
65 cleanup:
66 	bpf_link__destroy(link);
67 	cpumask_success__destroy(skel);
68 }
69 
70 void test_cpumask(void)
71 {
72 	int i;
73 
74 	for (i = 0; i < ARRAY_SIZE(cpumask_success_testcases); i++) {
75 		if (!test__start_subtest(cpumask_success_testcases[i]))
76 			continue;
77 
78 		verify_success(cpumask_success_testcases[i]);
79 	}
80 
81 	RUN_TESTS(cpumask_success);
82 	RUN_TESTS(cpumask_failure);
83 }
84