From: Martin KaFai Lau <martin.lau@linux.dev>
To: Leon Hwang <leon.hwang@linux.dev>
Cc: 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>,
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, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v3 1/5] bpf: lru: Tidy hash handling in LRU code
Date: Wed, 14 Jan 2026 10:44:27 -0800 [thread overview]
Message-ID: <b20c1231-c8ef-4d66-97a9-120f2d77738e@linux.dev> (raw)
In-Reply-To: <20260107151456.72539-2-leon.hwang@linux.dev>
On 1/7/26 7:14 AM, Leon Hwang wrote:
> The hash field is not used by the LRU list itself.
>
> Setting hash while manipulating the LRU list also obscures the intent
> of the code and makes it harder to follow.
>
> Tidy this up by moving the hash assignment to prealloc_lru_pop(),
> where the element is prepared for insertion into the hash table.
>
> Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
> ---
> kernel/bpf/bpf_lru_list.c | 24 +++++++++---------------
> kernel/bpf/bpf_lru_list.h | 5 ++---
> kernel/bpf/hashtab.c | 5 ++---
> 3 files changed, 13 insertions(+), 21 deletions(-)
>
> diff --git a/kernel/bpf/bpf_lru_list.c b/kernel/bpf/bpf_lru_list.c
> index e7a2fc60523f..f4e183a9c28f 100644
> --- a/kernel/bpf/bpf_lru_list.c
> +++ b/kernel/bpf/bpf_lru_list.c
> @@ -344,10 +344,8 @@ static void bpf_lru_list_pop_free_to_local(struct bpf_lru *lru,
> static void __local_list_add_pending(struct bpf_lru *lru,
> struct bpf_lru_locallist *loc_l,
> int cpu,
> - struct bpf_lru_node *node,
> - u32 hash)
> + struct bpf_lru_node *node)
> {
> - *(u32 *)((void *)node + lru->hash_offset) = hash;
> node->cpu = cpu;
> node->type = BPF_LRU_LOCAL_LIST_T_PENDING;
> bpf_lru_node_clear_ref(node);
> @@ -393,8 +391,7 @@ __local_list_pop_pending(struct bpf_lru *lru, struct bpf_lru_locallist *loc_l)
> return NULL;
> }
>
> -static struct bpf_lru_node *bpf_percpu_lru_pop_free(struct bpf_lru *lru,
> - u32 hash)
> +static struct bpf_lru_node *bpf_percpu_lru_pop_free(struct bpf_lru *lru)
> {
> struct list_head *free_list;
> struct bpf_lru_node *node = NULL;
> @@ -415,7 +412,6 @@ static struct bpf_lru_node *bpf_percpu_lru_pop_free(struct bpf_lru *lru,
>
> if (!list_empty(free_list)) {
> node = list_first_entry(free_list, struct bpf_lru_node, list);
> - *(u32 *)((void *)node + lru->hash_offset) = hash;
> bpf_lru_node_clear_ref(node);
> __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_INACTIVE);
init the hash value later (after releasing l->lock) is not correct. The
node is in the inactive list. The inactive list is one of the rotate and
_evict_ candidates, meaning tgt_l->hash will be used in
htab_lru_map_delete_node(). In practice, it does not matter if
htab_lru_map_delete_node() cannot find the node in an incorrect bucket.
However, it still should not use an uninitialized value to begin with.
> index 441ff5bc54ac..c2d12db9036a 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
> @@ -296,12 +296,13 @@ static void htab_free_elems(struct bpf_htab *htab)
> static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
> u32 hash)
> {
> - struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
> + struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru);
> struct htab_elem *l;
>
> if (node) {
> bpf_map_inc_elem_count(&htab->map);
> l = container_of(node, struct htab_elem, lru_node);
> + l->hash = hash;
> memcpy(l->key, key, htab->map.key_size);
> return l;
> }
next prev parent reply other threads:[~2026-01-14 18:44 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-07 15:14 [PATCH bpf-next v3 0/5] bpf: lru: Fix unintended eviction when updating lru hash maps Leon Hwang
2026-01-07 15:14 ` [PATCH bpf-next v3 1/5] bpf: lru: Tidy hash handling in LRU code Leon Hwang
2026-01-14 18:44 ` Martin KaFai Lau [this message]
2026-01-15 3:33 ` Leon Hwang
2026-01-07 15:14 ` [PATCH bpf-next v3 2/5] bpf: lru: Factor out bpf_lru_node_reset_state helper Leon Hwang
2026-01-07 15:14 ` [PATCH bpf-next v3 3/5] bpf: lru: Factor out bpf_lru_move_next_inactive_rotation helper Leon Hwang
2026-01-07 15:14 ` [PATCH bpf-next v3 4/5] bpf: lru: Fix unintended eviction when updating lru hash maps Leon Hwang
2026-01-14 19:39 ` Martin KaFai Lau
2026-01-15 3:25 ` Leon Hwang
2026-01-07 15:14 ` [PATCH bpf-next v3 5/5] selftests/bpf: Add tests to verify no unintended eviction when updating lru_[percpu_,]hash maps Leon Hwang
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=b20c1231-c8ef-4d66-97a9-120f2d77738e@linux.dev \
--to=martin.lau@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=leon.hwang@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--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