From: Leon Hwang <leon.hwang@linux.dev>
To: bpf@vger.kernel.org
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>, Shuah Khan <shuah@kernel.org>,
Leon Hwang <leon.hwang@linux.dev>,
Saket Kumar Bhaskar <skb99@linux.ibm.com>,
"David S . Miller" <davem@davemloft.net>,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
kernel-patches-bot@fb.com
Subject: [PATCH bpf-next v2 5/5] selftests/bpf: Add tests to verify no unintended eviction when updating lru_[percpu_,]hash maps
Date: Mon, 5 Jan 2026 23:18:13 +0800 [thread overview]
Message-ID: <20260105151813.6968-6-leon.hwang@linux.dev> (raw)
In-Reply-To: <20260105151813.6968-1-leon.hwang@linux.dev>
Add four tests to verify that updating an existing element in LRU hash
maps does not cause unintended eviction of other elements.
The test creates lru_hash/lru_percpu_hash maps with max_entries slots and
populates all of them. It then updates an existing key and verifies that:
1. The update succeeds without error
2. The updated key has the new value
3. All other keys still exist with their original values
This validates the fix that prevents unnecessary LRU eviction when
updating existing elements in full LRU hash maps.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
.../selftests/bpf/prog_tests/htab_update.c | 129 ++++++++++++++++++
1 file changed, 129 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/htab_update.c b/tools/testing/selftests/bpf/prog_tests/htab_update.c
index d0b405eb2966..a0c93aae2b99 100644
--- a/tools/testing/selftests/bpf/prog_tests/htab_update.c
+++ b/tools/testing/selftests/bpf/prog_tests/htab_update.c
@@ -143,3 +143,132 @@ void test_htab_update(void)
if (test__start_subtest("concurrent_update"))
test_concurrent_update();
}
+
+static void __setaffinity(cpu_set_t *cpus, int cpu)
+{
+ CPU_ZERO(cpus);
+ CPU_SET(cpu, cpus);
+ pthread_setaffinity_np(pthread_self(), sizeof(*cpus), cpus);
+}
+
+static void test_lru_hash_map_update_elem(enum bpf_map_type map_type, u64 map_flags)
+{
+ bool percpu = map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
+ int err, map_fd, i, key, nr_cpus, max_entries = 128;
+ u64 *values, value = 0xDEADC0DE;
+ cpu_set_t cpus;
+ LIBBPF_OPTS(bpf_map_create_opts, opts,
+ .map_flags = map_flags,
+ );
+
+ nr_cpus = libbpf_num_possible_cpus();
+ if (!ASSERT_GT(nr_cpus, 0, "libbpf_num_possible_cpus"))
+ return;
+
+ values = calloc(nr_cpus, sizeof(u64));
+ if (!ASSERT_OK_PTR(values, "calloc values"))
+ return;
+ for (i = 0; i < nr_cpus; i++)
+ values[i] = value;
+
+ map_fd = bpf_map_create(map_type, "test_lru", sizeof(int), sizeof(u64), max_entries, &opts);
+ if (!ASSERT_GE(map_fd, 0, "bpf_map_create")) {
+ free(values);
+ return;
+ }
+
+ /* populate all slots */
+ for (key = 0; key < max_entries; key++) {
+ __setaffinity(&cpus, key%nr_cpus);
+ err = bpf_map_update_elem(map_fd, &key, values, 0);
+ if (!ASSERT_OK(err, "bpf_map_update_elem"))
+ goto out;
+ }
+
+ /* LRU eviction should not happen */
+
+#define CHECK_OTHER_CPUS_VALUES(__val) \
+ do { \
+ if (!percpu) \
+ break; \
+ for (i = 1; i < nr_cpus; i++) \
+ if (!ASSERT_EQ(values[i], __val, "bpf_map_lookup_elem value")) \
+ goto out; \
+ } while (0)
+
+ __setaffinity(&cpus, 0);
+ key = 0;
+ memset(values, 0, nr_cpus * sizeof(u64));
+ err = bpf_map_update_elem(map_fd, &key, values, 0);
+ if (!ASSERT_OK(err, "bpf_map_update_elem"))
+ goto out;
+
+ err = bpf_map_lookup_elem(map_fd, &key, values);
+ if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
+ goto out;
+ if (!ASSERT_EQ(*values, 0, "bpf_map_lookup_elem value"))
+ goto out;
+ CHECK_OTHER_CPUS_VALUES(0);
+
+ for (key = 1; key < max_entries; key++) {
+ err = bpf_map_lookup_elem(map_fd, &key, values);
+ if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
+ goto out;
+ if (!ASSERT_EQ(*values, value, "bpf_map_lookup_elem value"))
+ goto out;
+ CHECK_OTHER_CPUS_VALUES(value);
+ }
+
+ for (i = 0; i < nr_cpus; i++)
+ values[i] = value;
+
+ key = max_entries;
+ err = bpf_map_update_elem(map_fd, &key, values, 0);
+ if (!ASSERT_OK(err, "bpf_map_update_elem"))
+ goto out;
+
+ err = bpf_map_lookup_elem(map_fd, &key, values);
+ if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
+ goto out;
+ if (!ASSERT_EQ(*values, value, "bpf_map_lookup_elem value"))
+ goto out;
+ CHECK_OTHER_CPUS_VALUES(value);
+
+#undef CHECK_OTHER_CPUS_VALUES
+
+out:
+ close(map_fd);
+ free(values);
+}
+
+static void test_update_lru_hash_map_common_lru(void)
+{
+ test_lru_hash_map_update_elem(BPF_MAP_TYPE_LRU_HASH, 0);
+}
+
+static void test_update_lru_hash_map_percpu_lru(void)
+{
+ test_lru_hash_map_update_elem(BPF_MAP_TYPE_LRU_HASH, BPF_F_NO_COMMON_LRU);
+}
+
+static void test_update_lru_percpu_hash_map_common_lru(void)
+{
+ test_lru_hash_map_update_elem(BPF_MAP_TYPE_LRU_PERCPU_HASH, 0);
+}
+
+static void test_update_lru_percpu_hash_map_percpu_lru(void)
+{
+ test_lru_hash_map_update_elem(BPF_MAP_TYPE_LRU_PERCPU_HASH, BPF_F_NO_COMMON_LRU);
+}
+
+void test_update_lru_hash_maps(void)
+{
+ if (test__start_subtest("lru_hash/common_lru"))
+ test_update_lru_hash_map_common_lru();
+ if (test__start_subtest("lru_hash/percpu_lru"))
+ test_update_lru_hash_map_percpu_lru();
+ if (test__start_subtest("lru_percpu_hash/common_lru"))
+ test_update_lru_percpu_hash_map_common_lru();
+ if (test__start_subtest("lru_percpu_hash/percpu_lru"))
+ test_update_lru_percpu_hash_map_percpu_lru();
+}
--
2.52.0
prev parent reply other threads:[~2026-01-05 15:19 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-05 15:18 [PATCH bpf-next v2 0/5] bpf: lru: Fix unintended eviction when updating lru hash maps Leon Hwang
2026-01-05 15:18 ` [PATCH bpf-next v2 1/5] bpf: lru: Tidy hash handling in LRU code Leon Hwang
2026-01-05 15:18 ` [PATCH bpf-next v2 2/5] bpf: lru: Factor out bpf_lru_node_reset_state helper Leon Hwang
2026-01-05 15:18 ` [PATCH bpf-next v2 3/5] bpf: lru: Factor out bpf_lru_move_next_inactive_rotation helper Leon Hwang
2026-01-05 15:18 ` [PATCH bpf-next v2 4/5] bpf: lru: Fix unintended eviction when updating lru hash maps Leon Hwang
2026-01-05 15:43 ` bot+bpf-ci
2026-01-05 16:16 ` Leon Hwang
2026-01-05 15:18 ` Leon Hwang [this message]
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=20260105151813.6968-6-leon.hwang@linux.dev \
--to=leon.hwang@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kernel-patches-bot@fb.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=skb99@linux.ibm.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