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