File Index
Files and directories
Sign in
FILE

spin_lock.c

File preview with syntax highlighting.

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