public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: "Emil Tsalapatis" <emil@etsalapatis.com>
To: "Mykyta Yatsenko" <mykyta.yatsenko5@gmail.com>,
	<bpf@vger.kernel.org>, <ast@kernel.org>, <andrii@kernel.org>,
	<daniel@iogearbox.net>, <kafai@meta.com>, <kernel-team@meta.com>,
	<eddyz87@gmail.com>, <memxor@gmail.com>,
	<herbert@gondor.apana.org.au>
Cc: "Mykyta Yatsenko" <yatsenko@meta.com>
Subject: Re: [PATCH RFC bpf-next v2 08/18] bpf: Implement iterator APIs for resizable hashtab
Date: Tue, 14 Apr 2026 13:49:51 -0400	[thread overview]
Message-ID: <DHT2G66ICGQ6.QY3NGX24ZE12@etsalapatis.com> (raw)
In-Reply-To: <20260408-rhash-v2-8-3b3675da1f6e@meta.com>

On Wed Apr 8, 2026 at 11:10 AM EDT, Mykyta Yatsenko wrote:
> From: Mykyta Yatsenko <yatsenko@meta.com>
>
> Wire up seq_file BPF iterator for BPF_MAP_TYPE_RHASH so that
> bpf_iter and bpftool map dump work with resizable hash maps.
>
> Use rhashtable_walk_enter_from() with a saved last_key to resume
> iteration across read() calls without linear skip from the
> beginning on each seq_start.
>
> Also implement rhtab_map_seq_show_elem() for bpftool map dump
> in non-iterator mode.
>
> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>

Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>

> ---
>  kernel/bpf/hashtab.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++---
>  1 file changed, 94 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> index a79d434dc626..492c6a9154b6 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
> @@ -3008,6 +3008,19 @@ static int rhtab_map_get_next_key(struct bpf_map *map, void *key, void *next_key
>  
>  static void rhtab_map_seq_show_elem(struct bpf_map *map, void *key, struct seq_file *m)
>  {
> +	void *value;
> +
> +	/* Guarantee that hashtab value is not freed */
> +	guard(rcu)();
> +
> +	value = rhtab_map_lookup_elem(map, key);
> +	if (!value)
> +		return;
> +
> +	btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
> +	seq_puts(m, ": ");
> +	btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
> +	seq_putc(m, '\n');
>  }
>  
>  static long bpf_each_rhash_elem(struct bpf_map *map, bpf_callback_t callback_fn,
> @@ -3201,36 +3214,113 @@ struct bpf_iter_seq_rhash_map_info {
>  	struct bpf_map *map;
>  	struct bpf_rhtab *rhtab;
>  	struct rhashtable_iter iter;
> -	u32 skip_elems;
> +	void *last_key;

Nit: Could we avoid adding skip_elems to this in the first place since
it's never used?

>  	bool iter_active;
>  };
>  

Question: Would it make sense/be worth annotating the seq functions with 
__acquires/__releases(RCU)?

>  static void *bpf_rhash_map_seq_start(struct seq_file *seq, loff_t *pos)
>  {
> -	return NULL;
> +	struct bpf_iter_seq_rhash_map_info *info = seq->private;
> +	struct rhtab_elem *elem;
> +	void *key = *pos > 0 ? info->last_key : NULL;
> +
> +	scoped_guard(rcu) {
> +		rhashtable_walk_enter_from(&info->rhtab->ht, &info->iter,
> +					   key, info->rhtab->params);
> +		rhashtable_walk_start(&info->iter);
> +	}
> +	info->iter_active = true;
> +
> +	elem = rhtab_iter_next(&info->iter);
> +	if (!elem)
> +		return NULL;
> +	/*
> +	 * if *pos is not 0, previously iteration failed on this elem,
> +	 * so we are restarting it. That's why no need to increment *pos.
> +	 */
> +	if (*pos == 0)
> +		++*pos;
> +	return elem;
>  }
>  
>  static void *bpf_rhash_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
>  {
> -	return NULL;
> +	struct bpf_iter_seq_rhash_map_info *info = seq->private;
> +	struct rhtab_elem *elem = v;
> +
> +	/* Save current key for O(1) resume in next seq_start */
> +	memcpy(info->last_key, elem->data, info->map->key_size);
> +
> +	++*pos;
> +
> +	return rhtab_iter_next(&info->iter);
> +}
> +
> +static int __bpf_rhash_map_seq_show(struct seq_file *seq,
> +				    struct rhtab_elem *elem)
> +{
> +	struct bpf_iter_seq_rhash_map_info *info = seq->private;
> +	struct bpf_iter__bpf_map_elem ctx = {};
> +	struct bpf_iter_meta meta;
> +	struct bpf_prog *prog;
> +	int ret = 0;
> +
> +	meta.seq = seq;
> +	prog = bpf_iter_get_info(&meta, elem == NULL);
> +	if (prog) {
> +		ctx.meta = &meta;
> +		ctx.map = info->map;
> +		if (elem) {
> +			ctx.key = elem->data;
> +			ctx.value = rhtab_elem_value(elem, info->map->key_size);
> +		}
> +		ret = bpf_iter_run_prog(prog, &ctx);
> +	}
> +
> +	return ret;
>  }
>  
>  static int bpf_rhash_map_seq_show(struct seq_file *seq, void *v)
>  {
> -	return 0;
> +	return __bpf_rhash_map_seq_show(seq, v);
>  }
>  
>  static void bpf_rhash_map_seq_stop(struct seq_file *seq, void *v)
>  {
> +	struct bpf_iter_seq_rhash_map_info *info = seq->private;
> +
> +	if (!v)
> +		(void)__bpf_rhash_map_seq_show(seq, NULL);
> +
> +	if (info->iter_active) {
> +		rhashtable_walk_stop(&info->iter);
> +		rhashtable_walk_exit(&info->iter);
> +		info->iter_active = false;
> +	}
>  }
>  
>  static int bpf_iter_init_rhash_map(void *priv_data, struct bpf_iter_aux_info *aux)
>  {
> +	struct bpf_iter_seq_rhash_map_info *info = priv_data;
> +	struct bpf_map *map = aux->map;
> +
> +	info->last_key = kmalloc(map->key_size, GFP_USER);
> +	if (!info->last_key)
> +		return -ENOMEM;
> +
> +	bpf_map_inc_with_uref(map);
> +	info->map = map;
> +	info->rhtab = container_of(map, struct bpf_rhtab, map);
> +	info->iter_active = false;
>  	return 0;
>  }
>  
>  static void bpf_iter_fini_rhash_map(void *priv_data)
>  {
> +	struct bpf_iter_seq_rhash_map_info *info = priv_data;
> +
> +	kfree(info->last_key);
> +	bpf_map_put_with_uref(info->map);
>  }
>  
>  static const struct seq_operations bpf_rhash_map_seq_ops = {


  reply	other threads:[~2026-04-14 17:49 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-08 15:10 [PATCH RFC bpf-next v2 00/18] bpf: Introduce resizable hash map Mykyta Yatsenko
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 01/18] bpf: Register rhash map Mykyta Yatsenko
2026-04-10 22:31   ` Emil Tsalapatis
2026-04-13  8:10     ` Mykyta Yatsenko
2026-04-14 17:50       ` Emil Tsalapatis
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 02/18] bpf: Add resizable hashtab skeleton Mykyta Yatsenko
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 03/18] bpf: Implement lookup, delete, update for resizable hashtab Mykyta Yatsenko
2026-04-12 23:10   ` Alexei Starovoitov
2026-04-13 10:52     ` Mykyta Yatsenko
2026-04-13 16:24       ` Alexei Starovoitov
2026-04-13 16:27         ` Daniel Borkmann
2026-04-13 19:43           ` Mykyta Yatsenko
2026-04-13 20:37   ` Emil Tsalapatis
2026-04-14  8:34     ` Mykyta Yatsenko
2026-04-14 10:25   ` Leon Hwang
2026-04-14 10:28     ` Mykyta Yatsenko
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 04/18] rhashtable: Add rhashtable_walk_enter_from() Mykyta Yatsenko
2026-04-12 23:13   ` Alexei Starovoitov
2026-04-13 12:22     ` Mykyta Yatsenko
2026-04-13 22:22   ` Emil Tsalapatis
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 05/18] bpf: Implement get_next_key and free_internal_structs for resizable hashtab Mykyta Yatsenko
2026-04-13 22:44   ` Emil Tsalapatis
2026-04-14  8:11     ` Mykyta Yatsenko
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 06/18] bpf: Implement bpf_each_rhash_elem() using walk API Mykyta Yatsenko
2026-04-13 23:02   ` Emil Tsalapatis
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 07/18] bpf: Implement batch ops for resizable hashtab Mykyta Yatsenko
2026-04-13 23:25   ` Emil Tsalapatis
2026-04-14  8:08     ` Mykyta Yatsenko
2026-04-14 17:47       ` Emil Tsalapatis
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 08/18] bpf: Implement iterator APIs " Mykyta Yatsenko
2026-04-14 17:49   ` Emil Tsalapatis [this message]
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 09/18] bpf: Implement alloc and free " Mykyta Yatsenko
2026-04-12 23:15   ` Alexei Starovoitov
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 10/18] bpf: Allow timers, workqueues and task_work in " Mykyta Yatsenko
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 11/18] libbpf: Support resizable hashtable Mykyta Yatsenko
2026-04-14 17:46   ` Emil Tsalapatis
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 12/18] selftests/bpf: Add basic tests for resizable hash map Mykyta Yatsenko
2026-04-12 23:16   ` Alexei Starovoitov
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 13/18] selftests/bpf: Support resizable hashtab in test_maps Mykyta Yatsenko
2026-04-12 23:17   ` Alexei Starovoitov
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 14/18] selftests/bpf: Resizable hashtab BPF_F_LOCK tests Mykyta Yatsenko
2026-04-12 23:18   ` Alexei Starovoitov
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 15/18] selftests/bpf: Add stress tests for resizable hash get_next_key Mykyta Yatsenko
2026-04-12 23:19   ` Alexei Starovoitov
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 16/18] selftests/bpf: Add BPF iterator tests for resizable hash map Mykyta Yatsenko
2026-04-12 23:20   ` Alexei Starovoitov
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 17/18] bpftool: Add rhash map documentation Mykyta Yatsenko
2026-04-14 17:51   ` Emil Tsalapatis
2026-04-08 15:10 ` [PATCH RFC bpf-next v2 18/18] selftests/bpf: Add resizable hashmap to benchmarks Mykyta Yatsenko
2026-04-12 23:25   ` Alexei Starovoitov
2026-04-12 23:11 ` [PATCH RFC bpf-next v2 00/18] bpf: Introduce resizable hash map Alexei Starovoitov
2026-04-13  8:28   ` Mykyta Yatsenko
2026-04-15  3:27 ` Herbert Xu
2026-04-15  5:13   ` Alexei Starovoitov

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=DHT2G66ICGQ6.QY3NGX24ZE12@etsalapatis.com \
    --to=emil@etsalapatis.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=kafai@meta.com \
    --cc=kernel-team@meta.com \
    --cc=memxor@gmail.com \
    --cc=mykyta.yatsenko5@gmail.com \
    --cc=yatsenko@meta.com \
    /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