]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Fix arena_spin_lock on systems with less than 16 CPUs
authorIlya Leoshkevich <iii@linux.ibm.com>
Thu, 24 Apr 2025 16:41:26 +0000 (18:41 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 25 Apr 2025 00:24:28 +0000 (17:24 -0700)
test_arena_spin_lock_size() explicitly requires having at least 2 CPUs,
but if the machine has less than 16, then pthread_setaffinity_np() call
in spin_lock_thread() fails.

Cap threads to the number of CPUs.

Alternative solutions are raising the number of required CPUs to 16, or
pinning multiple threads to the same CPU, but they are not that useful.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Link: https://lore.kernel.org/r/20250424165525.154403-3-iii@linux.ibm.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/arena_spin_lock.c

index 7565fc7690c2757760277374ba29f9fe62c34be9..0223fce4db2bc260e93f1066563c0db31d38bfa6 100644 (file)
@@ -51,9 +51,11 @@ static void test_arena_spin_lock_size(int size)
        struct arena_spin_lock *skel;
        pthread_t thread_id[16];
        int prog_fd, i, err;
+       int nthreads;
        void *ret;
 
-       if (get_nprocs() < 2) {
+       nthreads = MIN(get_nprocs(), ARRAY_SIZE(thread_id));
+       if (nthreads < 2) {
                test__skip();
                return;
        }
@@ -66,25 +68,25 @@ static void test_arena_spin_lock_size(int size)
                goto end;
        }
        skel->bss->cs_count = size;
-       skel->bss->limit = repeat * 16;
+       skel->bss->limit = repeat * nthreads;
 
-       ASSERT_OK(pthread_barrier_init(&barrier, NULL, 16), "barrier init");
+       ASSERT_OK(pthread_barrier_init(&barrier, NULL, nthreads), "barrier init");
 
        prog_fd = bpf_program__fd(skel->progs.prog);
-       for (i = 0; i < 16; i++) {
+       for (i = 0; i < nthreads; i++) {
                err = pthread_create(&thread_id[i], NULL, &spin_lock_thread, &prog_fd);
                if (!ASSERT_OK(err, "pthread_create"))
                        goto end_barrier;
        }
 
-       for (i = 0; i < 16; i++) {
+       for (i = 0; i < nthreads; i++) {
                if (!ASSERT_OK(pthread_join(thread_id[i], &ret), "pthread_join"))
                        goto end_barrier;
                if (!ASSERT_EQ(ret, &prog_fd, "ret == prog_fd"))
                        goto end_barrier;
        }
 
-       ASSERT_EQ(skel->bss->counter, repeat * 16, "check counter value");
+       ASSERT_EQ(skel->bss->counter, repeat * nthreads, "check counter value");
 
 end_barrier:
        pthread_barrier_destroy(&barrier);