All of lore.kernel.org
 help / color / mirror / Atom feed
From: Emil Tsalapatis <emil@etsalapatis.com>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, andrii@kernel.org, eddyz87@gmail.com,
	memxor@gmail.com, daniel@iogearbox.net,
	Emil Tsalapatis <emil@etsalapatis.com>
Subject: [PATCH bpf-next v1 4/4] selftests/bpf: Test kmalloc variant selection for task storage
Date: Tue, 24 Nov 2026 06:03:57 +0000	[thread overview]
Message-ID: <20261124060357.11700-5-emil@etsalapatis.com> (raw)
In-Reply-To: <20261124060357.11700-1-emil@etsalapatis.com>

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 <emil@etsalapatis.com>
---
 .../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 <pthread.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <test_progs.h>
+#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 <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#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


  parent reply	other threads:[~2026-11-24  6:04 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  6:03 [PATCH bpf-next v1 0/4] bpf: Use kmalloc for local storage if possible Emil Tsalapatis
2026-11-24  6:03 ` [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time Emil Tsalapatis
2026-11-24  6:21   ` sashiko-bot
2026-11-24  6:40   ` Alexei Starovoitov
2026-11-24 13:59     ` Harry Yoo
2026-11-24 18:07       ` Emil Tsalapatis
2026-11-25  4:03       ` Alexei Starovoitov
2026-11-25 14:09       ` Peter Zijlstra
2026-11-25 14:30         ` Harry Yoo
2026-11-25 16:04           ` Peter Zijlstra
2026-11-26  1:07             ` Alexei Starovoitov
2026-11-26  1:04         ` Alexei Starovoitov
2026-11-26  9:44           ` Peter Zijlstra
2026-11-26  9:46             ` David Hildenbrand (Arm)
2026-11-26 20:40               ` Alexei Starovoitov
2026-11-26 20:45                 ` David Hildenbrand (Arm)
2026-11-26 21:04                   ` Alexei Starovoitov
2026-11-26 20:39             ` Alexei Starovoitov
2026-11-24  6:40   ` bot+bpf-ci
2026-11-24  6:03 ` [PATCH bpf-next v1 2/4] bpf: Plumb local storage context Emil Tsalapatis
2026-11-24  6:17   ` sashiko-bot
2026-11-24  6:03 ` [PATCH bpf-next v1 3/4] bpf: Add sleepable local storage Emil Tsalapatis
2026-11-24  6:40   ` bot+bpf-ci
2026-11-24  6:41   ` sashiko-bot
2026-11-24  6:03 ` Emil Tsalapatis [this message]
2026-11-24  6:40   ` [PATCH bpf-next v1 4/4] selftests/bpf: Test kmalloc variant selection for task storage bot+bpf-ci

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124060357.11700-5-emil@etsalapatis.com \
    --to=emil@etsalapatis.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=memxor@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.