Skip to content

Commit e477203

Browse files
ameryhungMartin KaFai Lau
authored andcommitted
selftests/bpf: Update task_local_storage/recursion test
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
1 parent d652f42 commit e477203

2 files changed

Lines changed: 7 additions & 17 deletions

File tree

tools/testing/selftests/bpf/prog_tests/task_local_storage.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,24 @@ static void test_recursion(void)
112112
task_ls_recursion__detach(skel);
113113

114114
/* Refer to the comment in BPF_PROG(on_update) for
115-
* the explanation on the value 201 and 100.
115+
* the explanation on the value 200 and 1.
116116
*/
117117
map_fd = bpf_map__fd(skel->maps.map_a);
118118
err = bpf_map_lookup_elem(map_fd, &task_fd, &value);
119119
ASSERT_OK(err, "lookup map_a");
120-
ASSERT_EQ(value, 201, "map_a value");
121-
ASSERT_EQ(skel->bss->nr_del_errs, 1, "bpf_task_storage_delete busy");
120+
ASSERT_EQ(value, 200, "map_a value");
121+
ASSERT_EQ(skel->bss->nr_del_errs, 0, "bpf_task_storage_delete busy");
122122

123123
map_fd = bpf_map__fd(skel->maps.map_b);
124124
err = bpf_map_lookup_elem(map_fd, &task_fd, &value);
125125
ASSERT_OK(err, "lookup map_b");
126-
ASSERT_EQ(value, 100, "map_b value");
126+
ASSERT_EQ(value, 1, "map_b value");
127127

128128
prog_fd = bpf_program__fd(skel->progs.on_update);
129129
memset(&info, 0, sizeof(info));
130130
err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
131131
ASSERT_OK(err, "get prog info");
132-
ASSERT_EQ(info.recursion_misses, 0, "on_update prog recursion");
132+
ASSERT_EQ(info.recursion_misses, 2, "on_update prog recursion");
133133

134134
prog_fd = bpf_program__fd(skel->progs.on_enter);
135135
memset(&info, 0, sizeof(info));

tools/testing/selftests/bpf/progs/task_ls_recursion.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,9 @@ int BPF_PROG(on_update)
3636
if (!test_pid || task->pid != test_pid)
3737
return 0;
3838

39+
/* This will succeed as there is no real deadlock */
3940
ptr = bpf_task_storage_get(&map_a, task, 0,
4041
BPF_LOCAL_STORAGE_GET_F_CREATE);
41-
/* ptr will not be NULL when it is called from
42-
* the bpf_task_storage_get(&map_b,...F_CREATE) in
43-
* the BPF_PROG(on_enter) below. It is because
44-
* the value can be found in map_a and the kernel
45-
* does not need to acquire any spin_lock.
46-
*/
4742
if (ptr) {
4843
int err;
4944

@@ -53,12 +48,7 @@ int BPF_PROG(on_update)
5348
nr_del_errs++;
5449
}
5550

56-
/* This will still fail because map_b is empty and
57-
* this BPF_PROG(on_update) has failed to acquire
58-
* the percpu busy lock => meaning potential
59-
* deadlock is detected and it will fail to create
60-
* new storage.
61-
*/
51+
/* This will succeed as there is no real deadlock */
6252
ptr = bpf_task_storage_get(&map_b, task, 0,
6353
BPF_LOCAL_STORAGE_GET_F_CREATE);
6454
if (ptr)

0 commit comments

Comments
 (0)