From: Mykyta Yatsenko A NULL cache[] slot now means that the storage has no element for the slot, so a delete must refill the slot from the other elements. Use more maps than slots, and check the other lookups after each delete. Signed-off-by: Mykyta Yatsenko --- .../bpf/prog_tests/sk_storage_shared_slots.c | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_storage_shared_slots.c b/tools/testing/selftests/bpf/prog_tests/sk_storage_shared_slots.c new file mode 100644 index 0000000000000..d75b0f6115f5d --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/sk_storage_shared_slots.c @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +/* More maps than the 16 cache[] slots, so that maps share slots */ +#define NR_MAPS 20 + +void test_sk_storage_shared_slots(void) +{ + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC, + .btf_key_type_id = 1, .btf_value_type_id = 1); + int fds[NR_MAPS], sk = -1, n = 0, i, j, val; + struct btf *btf; + + /* sk storage maps need BTF for the key and the value: type 1 is int */ + btf = btf__new_empty(); + if (!ASSERT_OK_PTR(btf, "btf__new_empty")) + return; + btf__add_int(btf, "int", 4, BTF_INT_SIGNED); + if (!ASSERT_OK(btf__load_into_kernel(btf), "btf__load_into_kernel")) + goto out; + opts.btf_fd = btf__fd(btf); + for (n = 0; n < NR_MAPS; n++) { + fds[n] = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, NULL, 4, 4, 0, + &opts); + if (!ASSERT_OK_FD(fds[n], "bpf_map_create")) + goto out; + } + sk = socket(AF_INET6, SOCK_STREAM, 0); + if (!ASSERT_OK_FD(sk, "socket")) + goto out; + for (i = 0; i < NR_MAPS; i++) + ASSERT_OK(bpf_map_update_elem(fds[i], &sk, &i, BPF_NOEXIST), + "update"); + + /* + * A link puts its element in the slot, so the last maps hold the shared + * slots. Delete these maps first: each delete must refill the slot. + */ + for (i = NR_MAPS - 1; i >= 0; i--) { + ASSERT_OK(bpf_map_delete_elem(fds[i], &sk), "delete"); + ASSERT_EQ(bpf_map_lookup_elem(fds[i], &sk, &val), -ENOENT, + "lookup deleted"); + for (j = 0; j < i; j++) { + val = -1; + ASSERT_OK(bpf_map_lookup_elem(fds[j], &sk, &val), + "lookup"); + ASSERT_EQ(val, j, "value"); + } + } +out: + if (sk >= 0) + close(sk); + while (n--) + close(fds[n]); + btf__free(btf); +} -- 2.53.1-Meta