xref: /linux/tools/testing/selftests/bpf/prog_tests/arena_spin_lock.c (revision 25489a4f556414445d342951615178368ee45cde)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3 #include <test_progs.h>
4 #include <network_helpers.h>
5 #include <sys/sysinfo.h>
6 
7 struct __qspinlock { int val; };
8 typedef struct __qspinlock arena_spinlock_t;
9 
10 struct arena_qnode {
11 	unsigned long next;
12 	int count;
13 	int locked;
14 };
15 
16 #include "arena_spin_lock.skel.h"
17 
18 static long cpu;
19 static int repeat;
20 
21 pthread_barrier_t barrier;
22 
23 static void *spin_lock_thread(void *arg)
24 {
25 	int err, prog_fd = *(u32 *)arg;
26 	LIBBPF_OPTS(bpf_test_run_opts, topts,
27 		.data_in = &pkt_v4,
28 		.data_size_in = sizeof(pkt_v4),
29 		.repeat = repeat,
30 	);
31 	cpu_set_t cpuset;
32 
33 	CPU_ZERO(&cpuset);
34 	CPU_SET(__sync_fetch_and_add(&cpu, 1), &cpuset);
35 	ASSERT_OK(pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset), "cpu affinity");
36 
37 	err = pthread_barrier_wait(&barrier);
38 	if (err != PTHREAD_BARRIER_SERIAL_THREAD && err != 0)
39 		ASSERT_FALSE(true, "pthread_barrier");
40 
41 	err = bpf_prog_test_run_opts(prog_fd, &topts);
42 	ASSERT_OK(err, "test_run err");
43 	ASSERT_EQ((int)topts.retval, 0, "test_run retval");
44 
45 	pthread_exit(arg);
46 }
47 
48 static void test_arena_spin_lock_size(int size)
49 {
50 	LIBBPF_OPTS(bpf_test_run_opts, topts);
51 	struct arena_spin_lock *skel;
52 	pthread_t thread_id[16];
53 	int prog_fd, i, err;
54 	int nthreads;
55 	void *ret;
56 
57 	nthreads = MIN(get_nprocs(), ARRAY_SIZE(thread_id));
58 	if (nthreads < 2) {
59 		test__skip();
60 		return;
61 	}
62 
63 	skel = arena_spin_lock__open_and_load();
64 	if (!ASSERT_OK_PTR(skel, "arena_spin_lock__open_and_load"))
65 		return;
66 	if (skel->data->test_skip == 2) {
67 		test__skip();
68 		goto end;
69 	}
70 	skel->bss->cs_count = size;
71 	skel->bss->limit = repeat * nthreads;
72 
73 	ASSERT_OK(pthread_barrier_init(&barrier, NULL, nthreads), "barrier init");
74 
75 	prog_fd = bpf_program__fd(skel->progs.prog);
76 	for (i = 0; i < nthreads; i++) {
77 		err = pthread_create(&thread_id[i], NULL, &spin_lock_thread, &prog_fd);
78 		if (!ASSERT_OK(err, "pthread_create"))
79 			goto end_barrier;
80 	}
81 
82 	for (i = 0; i < nthreads; i++) {
83 		if (!ASSERT_OK(pthread_join(thread_id[i], &ret), "pthread_join"))
84 			goto end_barrier;
85 		if (!ASSERT_EQ(ret, &prog_fd, "ret == prog_fd"))
86 			goto end_barrier;
87 	}
88 
89 	ASSERT_EQ(skel->bss->counter, repeat * nthreads, "check counter value");
90 
91 end_barrier:
92 	pthread_barrier_destroy(&barrier);
93 end:
94 	arena_spin_lock__destroy(skel);
95 	return;
96 }
97 
98 void test_arena_spin_lock(void)
99 {
100 	repeat = 1000;
101 	if (test__start_subtest("arena_spin_lock_1"))
102 		test_arena_spin_lock_size(1);
103 	cpu = 0;
104 	if (test__start_subtest("arena_spin_lock_1000"))
105 		test_arena_spin_lock_size(1000);
106 	cpu = 0;
107 	repeat = 100;
108 	if (test__start_subtest("arena_spin_lock_50000"))
109 		test_arena_spin_lock_size(50000);
110 }
111