Add testing to confirm that task-storage allocations use the right sleepable vs maylock kmalloc variant for an allocation given the context the call is made in. Signed-off-by: Emil Tsalapatis --- .../bpf/prog_tests/task_storage_sleepable.c | 94 +++++++++++++ .../bpf/progs/task_storage_sleepable.c | 126 ++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_storage_sleepable.c create mode 100644 tools/testing/selftests/bpf/progs/task_storage_sleepable.c diff --git a/tools/testing/selftests/bpf/prog_tests/task_storage_sleepable.c b/tools/testing/selftests/bpf/prog_tests/task_storage_sleepable.c new file mode 100644 index 0000000000000..fa0818f63c6bc --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/task_storage_sleepable.c @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include +#include +#include "task_storage_sleepable.skel.h" + +static void *run_storage(void *arg) +{ + struct task_storage_sleepable *skel = arg; + + /* A new thread has no task storage from earlier tests. */ + skel->bss->target_pid = syscall(SYS_gettid); + syscall(SYS_getpgid, 0); + skel->bss->target_pid = 0; + return NULL; +} + +static void test_alloc(const char *prog_name, int regular_allocs, int nonsleepable_allocs) +{ + struct task_storage_sleepable *skel; + struct bpf_link *link = NULL; + struct bpf_program *regular_probe, *prog; + pthread_t thread; + int err; + + skel = task_storage_sleepable__open(); + if (!ASSERT_OK_PTR(skel, "open")) + return; + + regular_probe = skel->progs.on_regular_alloc; + err = bpf_program__set_attach_target(regular_probe, 0, "bpf_map_kzalloc"); + if (err == -ESRCH) { + printf("%s: bpf_map_kzalloc missing from kernel BTF\n", __func__); + err = bpf_program__set_autoload(regular_probe, false); + } + if (!ASSERT_OK(err, "configure_regular_alloc_probe")) + goto out; + + /* Task storage uses the nolock allocator until the fallback is added. */ + prog = skel->progs.on_nonsleepable_alloc; + err = bpf_program__set_attach_target(prog, 0, "bpf_map_kmalloc_maylock"); + if (err == -ESRCH) + err = bpf_program__set_attach_target(prog, 0, "bpf_map_kmalloc_nolock"); + if (!ASSERT_OK(err, "set_nonsleepable_alloc_target")) + goto out; + + err = task_storage_sleepable__load(skel); + if (!ASSERT_OK(err, "load")) + goto out; + + if (bpf_program__autoload(regular_probe)) { + skel->links.on_regular_alloc = bpf_program__attach(regular_probe); + if (!ASSERT_OK_PTR(skel->links.on_regular_alloc, "attach_regular_alloc")) + goto out; + } + skel->links.on_nonsleepable_alloc = bpf_program__attach(skel->progs.on_nonsleepable_alloc); + if (!ASSERT_OK_PTR(skel->links.on_nonsleepable_alloc, "attach_nonsleepable_alloc")) + goto out; + + prog = bpf_object__find_program_by_name(skel->obj, prog_name); + if (!ASSERT_OK_PTR(prog, "find_program")) + goto out; + link = bpf_program__attach(prog); + if (!ASSERT_OK_PTR(link, "attach_program")) + goto out; + + err = pthread_create(&thread, NULL, run_storage, skel); + if (!ASSERT_OK(err, "pthread_create")) + goto out; + err = pthread_join(thread, NULL); + ASSERT_OK(err, "pthread_join"); + ASSERT_EQ(skel->bss->errors, 0, "storage_errors"); + if (skel->links.on_regular_alloc) + ASSERT_EQ(skel->bss->regular_allocs, regular_allocs, "regular_allocs"); + ASSERT_EQ(skel->bss->nonsleepable_allocs, nonsleepable_allocs, "nonsleepable_allocs"); +out: + bpf_link__destroy(link); + task_storage_sleepable__destroy(skel); +} + +void test_task_storage_sleepable(void) +{ + if (test__start_subtest("sleepable")) + test_alloc("sleepable", 3, 0); + if (test__start_subtest("non_sleepable")) + test_alloc("non_sleepable", 0, 3); + if (test__start_subtest("sleepable_rcu")) + test_alloc("sleepable_rcu", 0, 3); + if (test__start_subtest("mixed_contexts")) + test_alloc("mixed_contexts", 2, 2); +} diff --git a/tools/testing/selftests/bpf/progs/task_storage_sleepable.c b/tools/testing/selftests/bpf/progs/task_storage_sleepable.c new file mode 100644 index 0000000000000..f9851c5718916 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/task_storage_sleepable.c @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ + +#include "vmlinux.h" +#include +#include +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_TASK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, long); +} map_a SEC(".maps"), map_b SEC(".maps"); + +int target_pid; +int errors; +int regular_allocs; +int nonsleepable_allocs; + +void bpf_rcu_read_lock(void) __ksym; +void bpf_rcu_read_unlock(void) __ksym; + +static bool is_target(void) +{ + return target_pid && (__u32)bpf_get_current_pid_tgid() == target_pid; +} + +SEC("fentry/bpf_map_kzalloc") +int BPF_PROG(on_regular_alloc, struct bpf_map *map) +{ + if (is_target() && map->map_type == BPF_MAP_TYPE_TASK_STORAGE) + regular_allocs++; + return 0; +} + +SEC("fentry") +int BPF_PROG(on_nonsleepable_alloc, struct bpf_map *map) +{ + if (is_target() && map->map_type == BPF_MAP_TYPE_TASK_STORAGE) + nonsleepable_allocs++; + return 0; +} + +static __always_inline void create_storage(void) +{ + struct task_struct *task = bpf_get_current_task_btf(); + long initial = 123, *value, *again; + + /* The first map allocates both the element and the storage container. */ + value = bpf_task_storage_get(&map_a, task, &initial, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (!value || *value != initial) + errors++; + + again = bpf_task_storage_get(&map_a, task, NULL, 0); + if (!again || again != value) + errors++; + + /* The second map allocates an element in the existing container. */ + value = bpf_task_storage_get(&map_b, task, NULL, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (!value || *value) + errors++; + + if (bpf_task_storage_get(&map_a, task, NULL, 1ULL << 63)) + errors++; + if (bpf_task_storage_delete(&map_a, task)) + errors++; + if (bpf_task_storage_delete(&map_b, task)) + errors++; +} + +SEC("fentry.s/" SYS_PREFIX "sys_getpgid") +int sleepable(void *ctx) +{ + if (is_target()) + create_storage(); + return 0; +} + +SEC("fentry/" SYS_PREFIX "sys_getpgid") +int non_sleepable(void *ctx) +{ + if (is_target()) + create_storage(); + return 0; +} + +SEC("fentry.s/" SYS_PREFIX "sys_getpgid") +int sleepable_rcu(void *ctx) +{ + if (!is_target()) + return 0; + bpf_rcu_read_lock(); + create_storage(); + bpf_rcu_read_unlock(); + return 0; +} + +static __noinline void shared_storage(void) +{ + struct task_struct *task = bpf_get_current_task_btf(); + long *value; + + value = bpf_task_storage_get(&map_a, task, NULL, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (!value || *value) + errors++; + if (bpf_task_storage_delete(&map_a, task)) + errors++; +} + +SEC("fentry.s/" SYS_PREFIX "sys_getpgid") +int mixed_contexts(void *ctx) +{ + if (!is_target()) + return 0; + shared_storage(); + bpf_rcu_read_lock(); + shared_storage(); + bpf_rcu_read_unlock(); + return 0; +} -- 2.52.1