]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Update task_local_storage/recursion test
authorAmery Hung <ameryhung@gmail.com>
Thu, 5 Feb 2026 22:29:11 +0000 (14:29 -0800)
committerMartin KaFai Lau <martin.lau@kernel.org>
Fri, 6 Feb 2026 22:48:05 +0000 (14:48 -0800)
Update the expected result of the selftest as recursion of task local
storage syscall and helpers have been relaxed. Now that the percpu
counter is removed, task local storage helpers, bpf_task_storage_get()
and bpf_task_storage_delete() can now run on the same CPU at the same
time unless they cause deadlock.

Note that since there is no percpu counter preventing recursion in
task local storage helpers, bpf_trampoline now catches the recursion
of on_update as reported by recursion_misses.

on_enter: tp_btf/sys_enter
on_update: fentry/bpf_local_storage_update

           Old behavior                         New behavior
           ____________                         ____________
on_enter                             on_enter
  bpf_task_storage_get(&map_a)         bpf_task_storage_get(&map_a)
    bpf_task_storage_trylock succeed     bpf_local_storage_update(&map_a)
    bpf_local_storage_update(&map_a)

    on_update                            on_update
      bpf_task_storage_get(&map_a)         bpf_task_storage_get(&map_a)
        bpf_task_storage_trylock fail        on_update::misses++ (1)
        return NULL                        create and return map_a::ptr

                                           map_a::ptr += 1 (1)

                                           bpf_task_storage_delete(&map_a)
                                             return 0

      bpf_task_storage_get(&map_b)         bpf_task_storage_get(&map_b)
        bpf_task_storage_trylock fail        on_update::misses++ (2)
        return NULL                        create and return map_b::ptr

                                           map_b::ptr += 1 (1)

    create and return map_a::ptr         create and return map_a::ptr
  map_a::ptr = 200                     map_a::ptr = 200

  bpf_task_storage_get(&map_b)         bpf_task_storage_get(&map_b)
    bpf_task_storage_trylock succeed     lockless lookup succeed
    bpf_local_storage_update(&map_b)     return map_b::ptr

    on_update
      bpf_task_storage_get(&map_a)
        bpf_task_storage_trylock fail
        lockless lookup succeed
        return map_a::ptr

      map_a::ptr += 1 (201)

      bpf_task_storage_delete(&map_a)
        bpf_task_storage_trylock fail
        return -EBUSY
      nr_del_errs++ (1)

      bpf_task_storage_get(&map_b)
        bpf_task_storage_trylock fail
        return NULL

    create and return ptr

  map_b::ptr = 100

Expected result:

map_a::ptr = 201                          map_a::ptr = 200
map_b::ptr = 100                          map_b::ptr = 1
nr_del_err = 1                            nr_del_err = 0
on_update::recursion_misses = 0           on_update::recursion_misses = 2
On_enter::recursion_misses = 0            on_enter::recursion_misses = 0

Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://patch.msgid.link/20260205222916.1788211-14-ameryhung@gmail.com
tools/testing/selftests/bpf/prog_tests/task_local_storage.c
tools/testing/selftests/bpf/progs/task_ls_recursion.c

index 42e822ea352f1c7d48ae0ed4fc6d68f367da3ba5..7bee33797c710450159b4c24023a61c545490c5f 100644 (file)
@@ -112,24 +112,24 @@ static void test_recursion(void)
        task_ls_recursion__detach(skel);
 
        /* Refer to the comment in BPF_PROG(on_update) for
-        * the explanation on the value 201 and 100.
+        * the explanation on the value 200 and 1.
         */
        map_fd = bpf_map__fd(skel->maps.map_a);
        err = bpf_map_lookup_elem(map_fd, &task_fd, &value);
        ASSERT_OK(err, "lookup map_a");
-       ASSERT_EQ(value, 201, "map_a value");
-       ASSERT_EQ(skel->bss->nr_del_errs, 1, "bpf_task_storage_delete busy");
+       ASSERT_EQ(value, 200, "map_a value");
+       ASSERT_EQ(skel->bss->nr_del_errs, 0, "bpf_task_storage_delete busy");
 
        map_fd = bpf_map__fd(skel->maps.map_b);
        err = bpf_map_lookup_elem(map_fd, &task_fd, &value);
        ASSERT_OK(err, "lookup map_b");
-       ASSERT_EQ(value, 100, "map_b value");
+       ASSERT_EQ(value, 1, "map_b value");
 
        prog_fd = bpf_program__fd(skel->progs.on_update);
        memset(&info, 0, sizeof(info));
        err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
        ASSERT_OK(err, "get prog info");
-       ASSERT_EQ(info.recursion_misses, 0, "on_update prog recursion");
+       ASSERT_EQ(info.recursion_misses, 2, "on_update prog recursion");
 
        prog_fd = bpf_program__fd(skel->progs.on_enter);
        memset(&info, 0, sizeof(info));
index f1853c38aada84cd7cbf477602dfeda28e5ed9e6..b3735943269200af05717cd18c60243ce9b218d8 100644 (file)
@@ -36,14 +36,9 @@ int BPF_PROG(on_update)
        if (!test_pid || task->pid != test_pid)
                return 0;
 
+       /* This will succeed as there is no real deadlock */
        ptr = bpf_task_storage_get(&map_a, task, 0,
                                   BPF_LOCAL_STORAGE_GET_F_CREATE);
-       /* ptr will not be NULL when it is called from
-        * the bpf_task_storage_get(&map_b,...F_CREATE) in
-        * the BPF_PROG(on_enter) below.  It is because
-        * the value can be found in map_a and the kernel
-        * does not need to acquire any spin_lock.
-        */
        if (ptr) {
                int err;
 
@@ -53,12 +48,7 @@ int BPF_PROG(on_update)
                        nr_del_errs++;
        }
 
-       /* This will still fail because map_b is empty and
-        * this BPF_PROG(on_update) has failed to acquire
-        * the percpu busy lock => meaning potential
-        * deadlock is detected and it will fail to create
-        * new storage.
-        */
+       /* This will succeed as there is no real deadlock */
        ptr = bpf_task_storage_get(&map_b, task, 0,
                                   BPF_LOCAL_STORAGE_GET_F_CREATE);
        if (ptr)