Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2#include <regex.h>
3#include <test_progs.h>
4#include <network_helpers.h>
5
6#include "test_spin_lock.skel.h"
7#include "test_spin_lock_fail.skel.h"
8
9static char log_buf[1024 * 1024];
10
11static struct {
12 const char *prog_name;
13 const char *err_msg;
14} spin_lock_fail_tests[] = {
15 { "lock_id_kptr_preserve",
16 "[0-9]\\+: (bf) r1 = r0 ; R0=ptr_foo(id=2,ref_obj_id=2)"
17 " R1=ptr_foo(id=2,ref_obj_id=2) refs=2\n"
18 "[0-9]\\+: (85) call bpf_this_cpu_ptr#154\n"
19 "R1 type=ptr_ expected=percpu_ptr_" },
20 { "lock_id_global_zero",
21 "; R1=map_value(map=.data.A,ks=4,vs=4)\n2: (85) call bpf_this_cpu_ptr#154\n"
22 "R1 type=map_value expected=percpu_ptr_" },
23 { "lock_id_mapval_preserve",
24 "[0-9]\\+: (bf) r1 = r0 ;"
25 " R0=map_value(id=1,map=array_map,ks=4,vs=8)"
26 " R1=map_value(id=1,map=array_map,ks=4,vs=8)\n"
27 "[0-9]\\+: (85) call bpf_this_cpu_ptr#154\n"
28 "R1 type=map_value expected=percpu_ptr_" },
29 { "lock_id_innermapval_preserve",
30 "[0-9]\\+: (bf) r1 = r0 ;"
31 " R0=map_value(id=2,ks=4,vs=8)"
32 " R1=map_value(id=2,ks=4,vs=8)\n"
33 "[0-9]\\+: (85) call bpf_this_cpu_ptr#154\n"
34 "R1 type=map_value expected=percpu_ptr_" },
35 { "lock_id_mismatch_kptr_kptr", "bpf_spin_unlock of different lock" },
36 { "lock_id_mismatch_kptr_global", "bpf_spin_unlock of different lock" },
37 { "lock_id_mismatch_kptr_mapval", "bpf_spin_unlock of different lock" },
38 { "lock_id_mismatch_kptr_innermapval", "bpf_spin_unlock of different lock" },
39 { "lock_id_mismatch_global_global", "bpf_spin_unlock of different lock" },
40 { "lock_id_mismatch_global_kptr", "bpf_spin_unlock of different lock" },
41 { "lock_id_mismatch_global_mapval", "bpf_spin_unlock of different lock" },
42 { "lock_id_mismatch_global_innermapval", "bpf_spin_unlock of different lock" },
43 { "lock_id_mismatch_mapval_mapval", "bpf_spin_unlock of different lock" },
44 { "lock_id_mismatch_mapval_kptr", "bpf_spin_unlock of different lock" },
45 { "lock_id_mismatch_mapval_global", "bpf_spin_unlock of different lock" },
46 { "lock_id_mismatch_mapval_innermapval", "bpf_spin_unlock of different lock" },
47 { "lock_id_mismatch_innermapval_innermapval1", "bpf_spin_unlock of different lock" },
48 { "lock_id_mismatch_innermapval_innermapval2", "bpf_spin_unlock of different lock" },
49 { "lock_id_mismatch_innermapval_kptr", "bpf_spin_unlock of different lock" },
50 { "lock_id_mismatch_innermapval_global", "bpf_spin_unlock of different lock" },
51 { "lock_id_mismatch_innermapval_mapval", "bpf_spin_unlock of different lock" },
52 { "lock_global_subprog_call1", "global function calls are not allowed while holding a lock" },
53 { "lock_global_subprog_call2", "global function calls are not allowed while holding a lock" },
54 { "lock_global_sleepable_helper_subprog", "global function calls are not allowed while holding a lock" },
55 { "lock_global_sleepable_kfunc_subprog", "global function calls are not allowed while holding a lock" },
56 { "lock_global_sleepable_subprog_indirect", "global function calls are not allowed while holding a lock" },
57};
58
59static int match_regex(const char *pattern, const char *string)
60{
61 int err, rc;
62 regex_t re;
63
64 err = regcomp(&re, pattern, REG_NOSUB);
65 if (err) {
66 char errbuf[512];
67
68 regerror(err, &re, errbuf, sizeof(errbuf));
69 PRINT_FAIL("Can't compile regex: %s\n", errbuf);
70 return -1;
71 }
72 rc = regexec(&re, string, 0, NULL, 0);
73 regfree(&re);
74 return rc == 0 ? 1 : 0;
75}
76
77static void test_spin_lock_fail_prog(const char *prog_name, const char *err_msg)
78{
79 LIBBPF_OPTS(bpf_object_open_opts, opts, .kernel_log_buf = log_buf,
80 .kernel_log_size = sizeof(log_buf),
81 .kernel_log_level = 1);
82 struct test_spin_lock_fail *skel;
83 struct bpf_program *prog;
84 int ret;
85
86 skel = test_spin_lock_fail__open_opts(&opts);
87 if (!ASSERT_OK_PTR(skel, "test_spin_lock_fail__open_opts"))
88 return;
89
90 prog = bpf_object__find_program_by_name(skel->obj, prog_name);
91 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
92 goto end;
93
94 bpf_program__set_autoload(prog, true);
95
96 ret = test_spin_lock_fail__load(skel);
97 if (!ASSERT_ERR(ret, "test_spin_lock_fail__load must fail"))
98 goto end;
99
100 /* Skip check if JIT does not support kfuncs */
101 if (strstr(log_buf, "JIT does not support calling kernel function")) {
102 test__skip();
103 goto end;
104 }
105
106 ret = match_regex(err_msg, log_buf);
107 if (!ASSERT_GE(ret, 0, "match_regex"))
108 goto end;
109
110 if (!ASSERT_TRUE(ret, "no match for expected error message")) {
111 fprintf(stderr, "Expected: %s\n", err_msg);
112 fprintf(stderr, "Verifier: %s\n", log_buf);
113 }
114
115end:
116 test_spin_lock_fail__destroy(skel);
117}
118
119static void *spin_lock_thread(void *arg)
120{
121 int err, prog_fd = *(u32 *) arg;
122 LIBBPF_OPTS(bpf_test_run_opts, topts,
123 .data_in = &pkt_v4,
124 .data_size_in = sizeof(pkt_v4),
125 .repeat = 10000,
126 );
127
128 err = bpf_prog_test_run_opts(prog_fd, &topts);
129 ASSERT_OK(err, "test_run");
130 ASSERT_OK(topts.retval, "test_run retval");
131 pthread_exit(arg);
132}
133
134void test_spin_lock_success(void)
135{
136 struct test_spin_lock *skel;
137 pthread_t thread_id[4];
138 int prog_fd, i;
139 void *ret;
140
141 skel = test_spin_lock__open_and_load();
142 if (!ASSERT_OK_PTR(skel, "test_spin_lock__open_and_load"))
143 return;
144 prog_fd = bpf_program__fd(skel->progs.bpf_spin_lock_test);
145 for (i = 0; i < 4; i++) {
146 int err;
147
148 err = pthread_create(&thread_id[i], NULL, &spin_lock_thread, &prog_fd);
149 if (!ASSERT_OK(err, "pthread_create"))
150 goto end;
151 }
152
153 for (i = 0; i < 4; i++) {
154 if (!ASSERT_OK(pthread_join(thread_id[i], &ret), "pthread_join"))
155 goto end;
156 if (!ASSERT_EQ(ret, &prog_fd, "ret == prog_fd"))
157 goto end;
158 }
159end:
160 test_spin_lock__destroy(skel);
161}
162
163void test_spin_lock(void)
164{
165 int i;
166
167 test_spin_lock_success();
168
169 for (i = 0; i < ARRAY_SIZE(spin_lock_fail_tests); i++) {
170 if (!test__start_subtest(spin_lock_fail_tests[i].prog_name))
171 continue;
172 test_spin_lock_fail_prog(spin_lock_fail_tests[i].prog_name,
173 spin_lock_fail_tests[i].err_msg);
174 }
175}