xref: /linux/tools/testing/selftests/bpf/prog_tests/percpu_alloc.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "percpu_alloc_array.skel.h"
4 #include "percpu_alloc_cgrp_local_storage.skel.h"
5 #include "percpu_alloc_fail.skel.h"
6 
7 static void test_array(void)
8 {
9 	struct percpu_alloc_array *skel;
10 	int err, prog_fd;
11 	LIBBPF_OPTS(bpf_test_run_opts, topts);
12 
13 	skel = percpu_alloc_array__open();
14 	if (!ASSERT_OK_PTR(skel, "percpu_alloc_array__open"))
15 		return;
16 
17 	bpf_program__set_autoload(skel->progs.test_array_map_1, true);
18 	bpf_program__set_autoload(skel->progs.test_array_map_2, true);
19 	bpf_program__set_autoload(skel->progs.test_array_map_3, true);
20 	bpf_program__set_autoload(skel->progs.test_array_map_4, true);
21 
22 	skel->bss->my_pid = getpid();
23 	skel->rodata->nr_cpus = libbpf_num_possible_cpus();
24 
25 	err = percpu_alloc_array__load(skel);
26 	if (!ASSERT_OK(err, "percpu_alloc_array__load"))
27 		goto out;
28 
29 	err = percpu_alloc_array__attach(skel);
30 	if (!ASSERT_OK(err, "percpu_alloc_array__attach"))
31 		goto out;
32 
33 	prog_fd = bpf_program__fd(skel->progs.test_array_map_1);
34 	err = bpf_prog_test_run_opts(prog_fd, &topts);
35 	ASSERT_OK(err, "test_run array_map 1-4");
36 	ASSERT_EQ(topts.retval, 0, "test_run array_map 1-4");
37 	ASSERT_EQ(skel->bss->cpu0_field_d, 2, "cpu0_field_d");
38 	ASSERT_EQ(skel->bss->sum_field_c, 1, "sum_field_c");
39 out:
40 	percpu_alloc_array__destroy(skel);
41 }
42 
43 static void test_array_sleepable(void)
44 {
45 	struct percpu_alloc_array *skel;
46 	int err, prog_fd;
47 	LIBBPF_OPTS(bpf_test_run_opts, topts);
48 
49 	skel = percpu_alloc_array__open();
50 	if (!ASSERT_OK_PTR(skel, "percpu_alloc__open"))
51 		return;
52 
53 	bpf_program__set_autoload(skel->progs.test_array_map_10, true);
54 
55 	skel->bss->my_pid = getpid();
56 	skel->rodata->nr_cpus = libbpf_num_possible_cpus();
57 
58 	err = percpu_alloc_array__load(skel);
59 	if (!ASSERT_OK(err, "percpu_alloc_array__load"))
60 		goto out;
61 
62 	err = percpu_alloc_array__attach(skel);
63 	if (!ASSERT_OK(err, "percpu_alloc_array__attach"))
64 		goto out;
65 
66 	prog_fd = bpf_program__fd(skel->progs.test_array_map_10);
67 	err = bpf_prog_test_run_opts(prog_fd, &topts);
68 	ASSERT_OK(err, "test_run array_map_10");
69 	ASSERT_EQ(topts.retval, 0, "test_run array_map_10");
70 	ASSERT_EQ(skel->bss->cpu0_field_d, 2, "cpu0_field_d");
71 	ASSERT_EQ(skel->bss->sum_field_c, 1, "sum_field_c");
72 out:
73 	percpu_alloc_array__destroy(skel);
74 }
75 
76 static void test_cgrp_local_storage(void)
77 {
78 	struct percpu_alloc_cgrp_local_storage *skel;
79 	int err, cgroup_fd, prog_fd;
80 	LIBBPF_OPTS(bpf_test_run_opts, topts);
81 
82 	cgroup_fd = test__join_cgroup("/percpu_alloc");
83 	if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup /percpu_alloc"))
84 		return;
85 
86 	skel = percpu_alloc_cgrp_local_storage__open();
87 	if (!ASSERT_OK_PTR(skel, "percpu_alloc_cgrp_local_storage__open"))
88 		goto close_fd;
89 
90 	skel->bss->my_pid = getpid();
91 	skel->rodata->nr_cpus = libbpf_num_possible_cpus();
92 
93 	err = percpu_alloc_cgrp_local_storage__load(skel);
94 	if (!ASSERT_OK(err, "percpu_alloc_cgrp_local_storage__load"))
95 		goto destroy_skel;
96 
97 	err = percpu_alloc_cgrp_local_storage__attach(skel);
98 	if (!ASSERT_OK(err, "percpu_alloc_cgrp_local_storage__attach"))
99 		goto destroy_skel;
100 
101 	prog_fd = bpf_program__fd(skel->progs.test_cgrp_local_storage_1);
102 	err = bpf_prog_test_run_opts(prog_fd, &topts);
103 	ASSERT_OK(err, "test_run cgrp_local_storage 1-3");
104 	ASSERT_EQ(topts.retval, 0, "test_run cgrp_local_storage 1-3");
105 	ASSERT_EQ(skel->bss->cpu0_field_d, 2, "cpu0_field_d");
106 	ASSERT_EQ(skel->bss->sum_field_c, 1, "sum_field_c");
107 
108 destroy_skel:
109 	percpu_alloc_cgrp_local_storage__destroy(skel);
110 close_fd:
111 	close(cgroup_fd);
112 }
113 
114 static void test_failure(void) {
115 	RUN_TESTS(percpu_alloc_fail);
116 }
117 
118 void test_percpu_alloc(void)
119 {
120 	if (test__start_subtest("array"))
121 		test_array();
122 	if (test__start_subtest("array_sleepable"))
123 		test_array_sleepable();
124 	if (test__start_subtest("cgrp_local_storage"))
125 		test_cgrp_local_storage();
126 	if (test__start_subtest("failure_tests"))
127 		test_failure();
128 }
129