From: Gyutae Bae <gyutae.opensource@navercorp.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
bpf@vger.kernel.org
Cc: John Fastabend <john.fastabend@gmail.com>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Shuah Khan <shuah@kernel.org>,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
Minsu Jeon <minsu.jeon@navercorp.com>,
Siwan Kim <siwan.kim@navercorp.com>,
Jonghyeon Kim <jong-hyeon.kim@navercorp.com>,
Gyutae Bae <gyutae.bae@navercorp.com>
Subject: [RFC bpf-next 3/3] selftests/bpf: test BPF_F_COMPARE compare-and-delete
Date: Mon, 22 Jun 2026 16:16:49 +0900 [thread overview]
Message-ID: <20260622071649.31541-4-gyutae.opensource@navercorp.com> (raw)
In-Reply-To: <20260622071649.31541-1-gyutae.opensource@navercorp.com>
From: Gyutae Bae <gyutae.bae@navercorp.com>
Add a test for compare-and-delete on a hash map. There is no libbpf
wrapper yet, so it issues the request through the raw bpf() syscall and
compares against a revision field stored in the value.
A matching expected value deletes the element, a stale one leaves it in
place and returns -EBUSY, and deleting an already-removed key returns
-ENOENT. A compare window that reaches past value_size is rejected with
-EINVAL, as are compare fields passed without BPF_F_COMPARE, so a dropped
flag cannot silently turn into an unconditional delete. Running the same
call on an array map, which has no compare-and-delete handler, returns
-EOPNOTSUPP.
Co-developed-by: Minsu Jeon <minsu.jeon@navercorp.com>
Signed-off-by: Minsu Jeon <minsu.jeon@navercorp.com>
Co-developed-by: Siwan Kim <siwan.kim@navercorp.com>
Signed-off-by: Siwan Kim <siwan.kim@navercorp.com>
Co-developed-by: Jonghyeon Kim <jong-hyeon.kim@navercorp.com>
Signed-off-by: Jonghyeon Kim <jong-hyeon.kim@navercorp.com>
Signed-off-by: Gyutae Bae <gyutae.bae@navercorp.com>
---
.../selftests/bpf/prog_tests/map_cmp_delete.c | 106 ++++++++++++++++++
1 file changed, 106 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/map_cmp_delete.c
diff --git a/tools/testing/selftests/bpf/prog_tests/map_cmp_delete.c b/tools/testing/selftests/bpf/prog_tests/map_cmp_delete.c
new file mode 100644
index 000000000000..6d6df13adcc4
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/map_cmp_delete.c
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2026 NAVER Corp.
+ *
+ * Author: Gyutae Bae <gyutae.bae@navercorp.com>
+ */
+
+#include <test_progs.h>
+#include <sys/syscall.h>
+
+struct val {
+ __u64 revision; /* synchronization field we compare on */
+ __u64 payload;
+};
+
+/* libbpf has no wrapper that passes the compare-and-delete fields, so
+ * issue bpf() directly. 'flags' is a parameter so tests can also
+ * exercise the no-BPF_F_COMPARE case.
+ */
+static int cmp_delete_flags(int fd, const void *key, const void *compare,
+ __u32 off, __u32 size, __u64 flags)
+{
+ union bpf_attr attr;
+ int ret;
+
+ memset(&attr, 0, sizeof(attr));
+ attr.map_fd = fd;
+ attr.key = (__u64)(unsigned long)key;
+ attr.flags = flags;
+ attr.compare = (__u64)(unsigned long)compare;
+ attr.compare_offset = off;
+ attr.compare_size = size;
+ ret = syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
+ return ret < 0 ? -errno : ret;
+}
+
+static int cmp_delete(int fd, const void *key, const void *compare,
+ __u32 off, __u32 size)
+{
+ return cmp_delete_flags(fd, key, compare, off, size, BPF_F_COMPARE);
+}
+
+static int hash_map(void)
+{
+ return bpf_map_create(BPF_MAP_TYPE_HASH, "cmp_hash",
+ sizeof(__u32), sizeof(struct val), 64, NULL);
+}
+
+static void test_contract(void)
+{
+ const __u32 off = offsetof(struct val, revision);
+ const __u32 sz = sizeof(((struct val *)0)->revision);
+ struct val v = { .revision = 10, .payload = 0xabc };
+ struct val snap = v;
+ __u32 k = 1;
+ int fd = hash_map();
+
+ if (!ASSERT_GE(fd, 0, "map_create"))
+ return;
+ ASSERT_OK(bpf_map_update_elem(fd, &k, &v, BPF_ANY), "insert");
+
+ /* mismatch -> -EBUSY, entry preserved */
+ snap.revision = 9;
+ ASSERT_EQ(cmp_delete(fd, &k, &snap, off, sz), -EBUSY, "mismatch_ebusy");
+ ASSERT_OK(bpf_map_lookup_elem(fd, &k, &v), "still_present");
+
+ /* compare fields set but BPF_F_COMPARE omitted -> -EINVAL, entry preserved
+ * (a dropped flag must not silently become an unconditional delete)
+ */
+ ASSERT_EQ(cmp_delete_flags(fd, &k, &snap, off, sz, 0), -EINVAL,
+ "cmp_fields_no_flag_einval");
+ ASSERT_OK(bpf_map_lookup_elem(fd, &k, &v), "preserved_no_flag");
+
+ /* match -> 0, entry gone */
+ snap.revision = 10;
+ ASSERT_OK(cmp_delete(fd, &k, &snap, off, sz), "match_deletes");
+ ASSERT_EQ(bpf_map_lookup_elem(fd, &k, &v), -ENOENT, "gone");
+
+ /* absent -> -ENOENT */
+ ASSERT_EQ(cmp_delete(fd, &k, &snap, off, sz), -ENOENT, "absent_enoent");
+
+ /* bad window -> -EINVAL */
+ ASSERT_EQ(cmp_delete(fd, &k, &snap, 0, sizeof(struct val) + 8), -EINVAL,
+ "oversize_einval");
+
+ /* unsupported map type (no map_delete_elem_cmp op) -> -EOPNOTSUPP */
+ {
+ int afd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "cmp_arr",
+ sizeof(__u32), sizeof(struct val), 4, NULL);
+ __u32 ak = 0;
+
+ if (ASSERT_GE(afd, 0, "array_create")) {
+ ASSERT_EQ(cmp_delete(afd, &ak, &snap, off, sz),
+ -EOPNOTSUPP, "array_eopnotsupp");
+ close(afd);
+ }
+ }
+ close(fd);
+}
+
+void test_map_cmp_delete(void)
+{
+ if (test__start_subtest("contract"))
+ test_contract();
+}
--
2.39.5 (Apple Git-154)
next prev parent reply other threads:[~2026-06-22 7:17 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-22 7:16 [RFC bpf-next 0/3] bpf: compare-and-delete (BPF_F_COMPARE) for hash maps Gyutae Bae
2026-06-22 7:16 ` [RFC bpf-next 1/3] bpf: add BPF_F_COMPARE flag and compare fields to map elem UAPI Gyutae Bae
2026-06-22 7:16 ` [RFC bpf-next 2/3] bpf: implement compare-and-delete (BPF_F_COMPARE) for BPF_MAP_TYPE_HASH Gyutae Bae
2026-06-22 7:16 ` Gyutae Bae [this message]
2026-06-22 22:32 ` [RFC bpf-next 0/3] bpf: compare-and-delete (BPF_F_COMPARE) for hash maps Alexei Starovoitov
2026-06-23 2:58 ` Gyutae Bae
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=20260622071649.31541-4-gyutae.opensource@navercorp.com \
--to=gyutae.opensource@navercorp.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=gyutae.bae@navercorp.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=jong-hyeon.kim@navercorp.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=minsu.jeon@navercorp.com \
--cc=shuah@kernel.org \
--cc=siwan.kim@navercorp.com \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox