From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: "T.J. Mercier" <tjmercier@google.com>,
ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev,
song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org,
emil@etsalapatis.com
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] bpf: htab: Reduce elem_size by 8 bytes for small key sizes
Date: Thu, 23 Jul 2026 15:12:01 +0100 [thread overview]
Message-ID: <0fe51955-39f7-48ca-8ce5-bc842868d53e@gmail.com> (raw)
In-Reply-To: <20260722203801.1854941-3-tjmercier@google.com>
On 7/22/26 9:38 PM, T.J. Mercier wrote:
> For standard and PCPU (non-LRU) hash maps with small key sizes (<= 8
> bytes), comparing keys requires only an 8 byte compare. Storing a cached
> 32-bit hash value to shortcut full key comparisons provides no
> performance advantage for small keys, and consumes memory for every
> element.
>
> This memory can be saved by eliminating hash along with its associated
> 4 byte padding before the key, reducing the elem_size (and key_offset)
> by 8 bytes for standard and PCPU maps.
>
> Introduce htab_has_hash() to check whether a map requires a cached hash
> field. Update htab_elem_set_hash(), lookup_elem_raw(), and
> lookup_nulls_elem_raw() to conditionally bypass hash checking and
> storage when htab_has_hash() is false.
>
> Together with the previous patch, this reduces the minimum standard and
> preallocated hash map element size from 64 bytes down to 32 bytes, and
> non-preallocated per-CPU element size from 64 bytes down to 40 bytes.
>
> Signed-off-by: T.J. Mercier <tjmercier@google.com>
> ---
> kernel/bpf/hashtab.c | 35 +++++++++++++------
> .../selftests/bpf/progs/map_ptr_kern.c | 2 +-
> 2 files changed, 25 insertions(+), 12 deletions(-)
>
> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> index 57729c3dff3d..b8c30d402958 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
...
>
> hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
> - if (htab_elem_hash(htab, l) == hash &&
> + if ((!htab_has_hash(htab) || htab_elem_hash(htab, l) == hash) &&
> !memcmp(htab_elem_key(htab, l), key, key_size))
> return l;
>
> @@ -812,7 +825,7 @@ static struct htab_elem *lookup_nulls_elem_raw(struct bpf_htab *htab,
>
> again:
> hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
> - if (htab_elem_hash(htab, l) == hash &&
> + if ((!htab_has_hash(htab) || htab_elem_hash(htab, l) == hash) &&
htab_has_hash() is constant for any concrete map, maybe we can store it in the
struct bpf_htab (it has few holes) and we already describe layout there,
or at least not calculate on each element in the bucket in the loop.
> !memcmp(htab_elem_key(htab, l), key, key_size))
> return l;
>
> @@ -3219,7 +3232,7 @@ static int rhtab_map_check_btf(struct bpf_map *map, const struct btf *btf,
> {
> struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map);
>
> - return bpf_ma_set_dtor(map, &rhtab->ma, rhtab_mem_dtor);
> + return bpf_ma_set_dtor(map, &rhtab->ma, rhtab_mem_dtor, offsetof(struct rhtab_elem, data));
> }
>
> static void rhtab_map_free_internal_structs(struct bpf_map *map)
> diff --git a/tools/testing/selftests/bpf/progs/map_ptr_kern.c b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> index 373c8d17ea55..6bd4cb68c20c 100644
> --- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> +++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> @@ -114,7 +114,7 @@ static inline int check_hash(void)
> VERIFY(check_default_noinline(&hash->map, map));
>
> VERIFY(hash->n_buckets == MAX_ENTRIES);
> - VERIFY(hash->elem_size == 64);
> + VERIFY(hash->elem_size == 32);
>
> VERIFY(hash->count.counter == 0);
> VERIFY(bpf_map_sum_elem_count(map) == 0);
next prev parent reply other threads:[~2026-07-23 14:12 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-22 20:37 [PATCH 0/2] bpf: htab: Reduce memory use of hash maps T.J. Mercier
2026-07-22 20:38 ` [PATCH 1/2] bpf: htab: Split htab_elem_lru and htab_elem_pcpu off of htab_elem T.J. Mercier
2026-07-23 14:11 ` Mykyta Yatsenko
2026-07-23 17:26 ` T.J. Mercier
2026-07-24 13:10 ` Mykyta Yatsenko
2026-07-22 20:38 ` [PATCH 2/2] bpf: htab: Reduce elem_size by 8 bytes for small key sizes T.J. Mercier
2026-07-23 14:12 ` Mykyta Yatsenko [this message]
2026-07-23 17:27 ` T.J. Mercier
2026-07-23 14:11 ` [PATCH 0/2] bpf: htab: Reduce memory use of hash maps Mykyta Yatsenko
2026-07-23 17:22 ` T.J. Mercier
2026-07-24 14:35 ` Mykyta Yatsenko
2026-07-31 1:06 ` T.J. Mercier
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=0fe51955-39f7-48ca-8ce5-bc842868d53e@gmail.com \
--to=mykyta.yatsenko5@gmail.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=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=song@kernel.org \
--cc=tjmercier@google.com \
--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
all inboxes | Powered by JetHome®