]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Cover tail-call cgroup storage prog-array checks
authorLin Ma <malin89@huawei.com>
Wed, 10 Jun 2026 10:55:39 +0000 (12:55 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 10 Jun 2026 23:16:46 +0000 (16:16 -0700)
Add tail-call selftests for prog-array ownership when cgroup storage
is in use. Verify that loading succeeds when callers and callees reuse
the owner's cgroup storage map, and that loading fails for a different
storage map and for the A(storage) -> B(no storage) -> C(storage)
bridge case addressed in the previous commit.

Also verify that a storage-less leaf program which cannot perform tail
calls itself is still allowed to join a storage-owned prog array, while
a storage-less tail-caller is rejected also at map update time.

  # LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t tailcalls
  [...]
  #475/25  tailcalls/tailcall_freplace:OK
  #475/26  tailcalls/tailcall_bpf2bpf_freplace:OK
  #475/27  tailcalls/tailcall_failure:OK
  #475/28  tailcalls/reject_tail_call_spin_lock:OK
  #475/29  tailcalls/reject_tail_call_rcu_lock:OK
  #475/30  tailcalls/reject_tail_call_preempt_lock:OK
  #475/31  tailcalls/reject_tail_call_ref:OK
  #475/32  tailcalls/tailcall_sleepable:OK
  #475/33  tailcalls/tailcall_cgrp_storage:OK
  #475/34  tailcalls/tailcall_cgrp_storage_diff_storage:OK
  #475/35  tailcalls/tailcall_cgrp_storage_no_storage:OK
  #475/36  tailcalls/tailcall_cgrp_storage_no_storage_leaf:OK
  #475/37  tailcalls/tailcall_cgrp_storage_no_storage_bridge:OK
  #475     tailcalls:OK
  Summary: 1/37 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Lin Ma <malin89@huawei.com>
Signed-off-by: Rongzhen Cui <cuirongzhen@huawei.com>
Signed-off-by: Jingguo Tan <tanjingguo@huawei.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20260610105539.705887-2-daniel@iogearbox.net
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/tailcalls.c
tools/testing/selftests/bpf/progs/tailcall_cgrp_storage.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/tailcall_cgrp_storage_no_storage.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/tailcall_cgrp_storage_owner.c [new file with mode: 0644]

index 7d534fde0af9bf0d7640e69ff5ced39b1b4b2ac9..a5a226d0104cd2c687f7d80f6ceac73367b89196 100644 (file)
@@ -8,6 +8,9 @@
 #include "tailcall_freplace.skel.h"
 #include "tc_bpf2bpf.skel.h"
 #include "tailcall_fail.skel.h"
+#include "tailcall_cgrp_storage_owner.skel.h"
+#include "tailcall_cgrp_storage_no_storage.skel.h"
+#include "tailcall_cgrp_storage.skel.h"
 #include "tailcall_sleepable.skel.h"
 
 /* test_tailcall_1 checks basic functionality by patching multiple locations
@@ -1654,6 +1657,179 @@ static void test_tailcall_failure()
        RUN_TESTS(tailcall_fail);
 }
 
+static void test_tailcall_cgrp_storage(void)
+{
+       struct tailcall_cgrp_storage_owner *owner_skel = NULL;
+       struct tailcall_cgrp_storage *skel = NULL;
+       int err, key = 0, prog_array_fd, prog_fd, storage_map_fd;
+
+       owner_skel = tailcall_cgrp_storage_owner__open_and_load();
+       if (!ASSERT_OK_PTR(owner_skel, "owner_open_and_load"))
+               return;
+
+       prog_array_fd = bpf_map__fd(owner_skel->maps.prog_array);
+       storage_map_fd = bpf_map__fd(owner_skel->maps.storage_map);
+
+       skel = tailcall_cgrp_storage__open();
+       if (!ASSERT_OK_PTR(skel, "tailcall_cgrp_storage__open"))
+               goto out;
+
+       err = bpf_map__reuse_fd(skel->maps.prog_array, prog_array_fd);
+       if (!ASSERT_OK(err, "reuse_prog_array"))
+               goto out;
+
+       err = bpf_map__reuse_fd(skel->maps.storage_map, storage_map_fd);
+       if (!ASSERT_OK(err, "reuse_storage_map"))
+               goto out;
+
+       err = bpf_object__load(skel->obj);
+       if (!ASSERT_OK(err, "tailcall_cgrp_storage__load"))
+               goto out;
+
+       prog_fd = bpf_program__fd(skel->progs.callee_prog);
+       err = bpf_map_update_elem(prog_array_fd, &key, &prog_fd, BPF_ANY);
+       ASSERT_OK(err, "update_prog_array");
+out:
+       tailcall_cgrp_storage__destroy(skel);
+       tailcall_cgrp_storage_owner__destroy(owner_skel);
+}
+
+static void test_tailcall_cgrp_storage_diff_storage(void)
+{
+       struct tailcall_cgrp_storage_owner *owner_skel = NULL;
+       struct tailcall_cgrp_storage *skel = NULL;
+       int err, prog_array_fd;
+
+       owner_skel = tailcall_cgrp_storage_owner__open_and_load();
+       if (!ASSERT_OK_PTR(owner_skel, "owner_open_and_load"))
+               return;
+
+       prog_array_fd = bpf_map__fd(owner_skel->maps.prog_array);
+
+       skel = tailcall_cgrp_storage__open();
+       if (!ASSERT_OK_PTR(skel, "tailcall_cgrp_storage__open"))
+               goto out;
+
+       err = bpf_map__reuse_fd(skel->maps.prog_array, prog_array_fd);
+       if (!ASSERT_OK(err, "reuse_prog_array"))
+               goto out;
+
+       err = bpf_object__load(skel->obj);
+       ASSERT_ERR(err, "tailcall_cgrp_storage__load");
+out:
+       tailcall_cgrp_storage__destroy(skel);
+       tailcall_cgrp_storage_owner__destroy(owner_skel);
+}
+
+static void test_tailcall_cgrp_storage_no_storage(void)
+{
+       struct tailcall_cgrp_storage_owner *owner_skel = NULL;
+       struct tailcall_cgrp_storage_no_storage *skel = NULL;
+       int err, prog_array_fd;
+
+       owner_skel = tailcall_cgrp_storage_owner__open_and_load();
+       if (!ASSERT_OK_PTR(owner_skel, "owner_open_and_load"))
+               return;
+
+       prog_array_fd = bpf_map__fd(owner_skel->maps.prog_array);
+
+       skel = tailcall_cgrp_storage_no_storage__open();
+       if (!ASSERT_OK_PTR(skel, "tailcall_cgrp_storage_no_storage__open"))
+               goto out;
+
+       err = bpf_map__reuse_fd(skel->maps.prog_array, prog_array_fd);
+       if (!ASSERT_OK(err, "reuse_prog_array"))
+               goto out;
+
+       err = bpf_object__load(skel->obj);
+       ASSERT_ERR(err, "tailcall_cgrp_storage_no_storage__load");
+out:
+       tailcall_cgrp_storage_no_storage__destroy(skel);
+       tailcall_cgrp_storage_owner__destroy(owner_skel);
+}
+
+static void test_tailcall_cgrp_storage_no_storage_leaf(void)
+{
+       struct tailcall_cgrp_storage_owner *owner_skel = NULL;
+       struct tailcall_cgrp_storage_no_storage *skel = NULL;
+       int err, key = 0, prog_array_fd, prog_fd;
+
+       owner_skel = tailcall_cgrp_storage_owner__open_and_load();
+       if (!ASSERT_OK_PTR(owner_skel, "owner_open_and_load"))
+               return;
+
+       prog_array_fd = bpf_map__fd(owner_skel->maps.prog_array);
+
+       skel = tailcall_cgrp_storage_no_storage__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "tailcall_cgrp_storage_no_storage__open_and_load"))
+               goto out;
+
+       prog_fd = bpf_program__fd(skel->progs.leaf_prog);
+       err = bpf_map_update_elem(prog_array_fd, &key, &prog_fd, BPF_ANY);
+       if (!ASSERT_OK(err, "update_prog_array_leaf"))
+               goto out;
+
+       prog_fd = bpf_program__fd(skel->progs.caller_prog);
+       err = bpf_map_update_elem(prog_array_fd, &key, &prog_fd, BPF_ANY);
+       ASSERT_ERR(err, "update_prog_array_bridge");
+out:
+       tailcall_cgrp_storage_no_storage__destroy(skel);
+       tailcall_cgrp_storage_owner__destroy(owner_skel);
+}
+
+static void test_tailcall_cgrp_storage_no_storage_bridge(void)
+{
+       struct tailcall_cgrp_storage_owner *owner_skel = NULL;
+       struct tailcall_cgrp_storage_no_storage *bridge_skel = NULL;
+       struct tailcall_cgrp_storage *callee_skel = NULL;
+       int err, key = 0, prog_array_fd, prog_fd, storage_map_fd;
+
+       owner_skel = tailcall_cgrp_storage_owner__open_and_load();
+       if (!ASSERT_OK_PTR(owner_skel, "owner_open_and_load"))
+               return;
+
+       prog_array_fd = bpf_map__fd(owner_skel->maps.prog_array);
+       storage_map_fd = bpf_map__fd(owner_skel->maps.storage_map);
+
+       callee_skel = tailcall_cgrp_storage__open();
+       if (!ASSERT_OK_PTR(callee_skel, "tailcall_cgrp_storage__open"))
+               goto out;
+
+       bpf_program__set_autoload(callee_skel->progs.caller_prog, false);
+
+       err = bpf_map__reuse_fd(callee_skel->maps.prog_array, prog_array_fd);
+       if (!ASSERT_OK(err, "reuse_prog_array"))
+               goto out;
+
+       err = bpf_map__reuse_fd(callee_skel->maps.storage_map, storage_map_fd);
+       if (!ASSERT_OK(err, "reuse_storage_map"))
+               goto out;
+
+       err = bpf_object__load(callee_skel->obj);
+       if (!ASSERT_OK(err, "tailcall_cgrp_storage__load"))
+               goto out;
+
+       prog_fd = bpf_program__fd(callee_skel->progs.callee_prog);
+       err = bpf_map_update_elem(prog_array_fd, &key, &prog_fd, BPF_ANY);
+       if (!ASSERT_OK(err, "update_prog_array"))
+               goto out;
+
+       bridge_skel = tailcall_cgrp_storage_no_storage__open();
+       if (!ASSERT_OK_PTR(bridge_skel, "tailcall_cgrp_storage_no_storage__open"))
+               goto out;
+
+       err = bpf_map__reuse_fd(bridge_skel->maps.prog_array, prog_array_fd);
+       if (!ASSERT_OK(err, "reuse_prog_array"))
+               goto out;
+
+       err = bpf_object__load(bridge_skel->obj);
+       ASSERT_ERR(err, "tailcall_cgrp_storage_no_storage_bridge__load");
+out:
+       tailcall_cgrp_storage_no_storage__destroy(bridge_skel);
+       tailcall_cgrp_storage__destroy(callee_skel);
+       tailcall_cgrp_storage_owner__destroy(owner_skel);
+}
+
 noinline void uprobe_sleepable_trigger(void)
 {
        asm volatile ("");
@@ -1781,4 +1957,14 @@ void test_tailcalls(void)
                test_tailcall_failure();
        if (test__start_subtest("tailcall_sleepable"))
                test_tailcall_sleepable();
+       if (test__start_subtest("tailcall_cgrp_storage"))
+               test_tailcall_cgrp_storage();
+       if (test__start_subtest("tailcall_cgrp_storage_diff_storage"))
+               test_tailcall_cgrp_storage_diff_storage();
+       if (test__start_subtest("tailcall_cgrp_storage_no_storage"))
+               test_tailcall_cgrp_storage_no_storage();
+       if (test__start_subtest("tailcall_cgrp_storage_no_storage_leaf"))
+               test_tailcall_cgrp_storage_no_storage_leaf();
+       if (test__start_subtest("tailcall_cgrp_storage_no_storage_bridge"))
+               test_tailcall_cgrp_storage_no_storage_bridge();
 }
diff --git a/tools/testing/selftests/bpf/progs/tailcall_cgrp_storage.c b/tools/testing/selftests/bpf/progs/tailcall_cgrp_storage.c
new file mode 100644 (file)
index 0000000..4dd3a00
--- /dev/null
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+
+struct {
+       __uint(type, BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
+       __type(key, struct bpf_cgroup_storage_key);
+       __type(value, __u64);
+} storage_map SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+       __uint(max_entries, 1);
+       __uint(key_size, sizeof(__u32));
+       __uint(value_size, sizeof(__u32));
+} prog_array SEC(".maps");
+
+SEC("cgroup_skb/egress")
+int caller_prog(struct __sk_buff *skb)
+{
+       __u64 *storage;
+
+       storage = bpf_get_local_storage(&storage_map, 0);
+       if (storage)
+               *storage = 1;
+
+       bpf_tail_call(skb, &prog_array, 0);
+       return 1;
+}
+
+SEC("cgroup_skb/egress")
+int callee_prog(struct __sk_buff *skb)
+{
+       __u64 *storage;
+
+       storage = bpf_get_local_storage(&storage_map, 0);
+       if (storage)
+               *storage = 1;
+
+       return 1;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/tailcall_cgrp_storage_no_storage.c b/tools/testing/selftests/bpf/progs/tailcall_cgrp_storage_no_storage.c
new file mode 100644 (file)
index 0000000..5c69b0a
--- /dev/null
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+
+struct {
+       __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+       __uint(max_entries, 1);
+       __uint(key_size, sizeof(__u32));
+       __uint(value_size, sizeof(__u32));
+} prog_array SEC(".maps");
+
+SEC("cgroup_skb/egress")
+int caller_prog(struct __sk_buff *skb)
+{
+       bpf_tail_call(skb, &prog_array, 0);
+       return 1;
+}
+
+SEC("cgroup_skb/egress")
+int leaf_prog(struct __sk_buff *skb)
+{
+       return 1;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/tailcall_cgrp_storage_owner.c b/tools/testing/selftests/bpf/progs/tailcall_cgrp_storage_owner.c
new file mode 100644 (file)
index 0000000..d7e8ec9
--- /dev/null
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+
+struct {
+       __uint(type, BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
+       __type(key, struct bpf_cgroup_storage_key);
+       __type(value, __u64);
+} storage_map SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+       __uint(max_entries, 1);
+       __uint(key_size, sizeof(__u32));
+       __uint(value_size, sizeof(__u32));
+} prog_array SEC(".maps");
+
+SEC("cgroup_skb/egress")
+int prog_array_owner(struct __sk_buff *skb)
+{
+       __u64 *storage;
+
+       storage = bpf_get_local_storage(&storage_map, 0);
+       if (storage)
+               *storage = 1;
+
+       bpf_tail_call(skb, &prog_array, 0);
+       return 1;
+}
+
+char _license[] SEC("license") = "GPL";