xref: /aosp_15_r20/external/linux-kselftest/tools/testing/selftests/bpf/prog_tests/spinlock.c (revision 053f45be4e351dfd5e965df293cd45b779f579ee)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 
spin_lock_thread(void * arg)5 static void *spin_lock_thread(void *arg)
6 {
7 	int err, prog_fd = *(u32 *) arg;
8 	LIBBPF_OPTS(bpf_test_run_opts, topts,
9 		.data_in = &pkt_v4,
10 		.data_size_in = sizeof(pkt_v4),
11 		.repeat = 10000,
12 	);
13 
14 	err = bpf_prog_test_run_opts(prog_fd, &topts);
15 	ASSERT_OK(err, "test_run");
16 	ASSERT_OK(topts.retval, "test_run retval");
17 	pthread_exit(arg);
18 }
19 
test_spinlock(void)20 void test_spinlock(void)
21 {
22 	const char *file = "./test_spin_lock.bpf.o";
23 	pthread_t thread_id[4];
24 	struct bpf_object *obj = NULL;
25 	int prog_fd;
26 	int err = 0, i;
27 	void *ret;
28 
29 	err = bpf_prog_test_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd);
30 	if (CHECK_FAIL(err)) {
31 		printf("test_spin_lock:bpf_prog_test_load errno %d\n", errno);
32 		goto close_prog;
33 	}
34 	for (i = 0; i < 4; i++)
35 		if (CHECK_FAIL(pthread_create(&thread_id[i], NULL,
36 					      &spin_lock_thread, &prog_fd)))
37 			goto close_prog;
38 
39 	for (i = 0; i < 4; i++)
40 		if (CHECK_FAIL(pthread_join(thread_id[i], &ret) ||
41 			       ret != (void *)&prog_fd))
42 			goto close_prog;
43 close_prog:
44 	bpf_object__close(obj);
45 }
46