On Thu, Oct 17, 2024 at 1:06 AM Namhyung Kim <namhyung@xxxxxxxxxx> wrote: > > Add a new open coded iterator for kmem_cache which can be called from a > BPF program like below. It doesn't take any argument and traverses all > kmem_cache entries. > > struct kmem_cache *pos; > > bpf_for_each(kmem_cache, pos) { > ... > } > > As it needs to grab slab_mutex, it should be called from sleepable BPF > programs only. > > Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx> > --- > kernel/bpf/helpers.c | 3 ++ > kernel/bpf/kmem_cache_iter.c | 87 ++++++++++++++++++++++++++++++++++++ > 2 files changed, 90 insertions(+) > > diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > index 073e6f04f4d765ff..d1dfa4f335577914 100644 > --- a/kernel/bpf/helpers.c > +++ b/kernel/bpf/helpers.c > @@ -3111,6 +3111,9 @@ BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL) > BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY) > BTF_ID_FLAGS(func, bpf_copy_from_user_str, KF_SLEEPABLE) > BTF_ID_FLAGS(func, bpf_get_kmem_cache) > +BTF_ID_FLAGS(func, bpf_iter_kmem_cache_new, KF_ITER_NEW | KF_SLEEPABLE) > +BTF_ID_FLAGS(func, bpf_iter_kmem_cache_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE) > +BTF_ID_FLAGS(func, bpf_iter_kmem_cache_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) I'm curious. Having bpf_iter_kmem_cache_{new,next,destroy} functions, can we rewrite kmem_cache_iter_seq_next in terms of these ones, so that we have less duplication of iteration logic? Or there will be some locking concerns preventing this? (I haven't looked into the actual logic much, sorry, lazy question) > BTF_KFUNCS_END(common_btf_ids) > > static const struct btf_kfunc_id_set common_kfunc_set = { > diff --git a/kernel/bpf/kmem_cache_iter.c b/kernel/bpf/kmem_cache_iter.c > index ebc101d7da51b57c..31ddaf452b20a458 100644 > --- a/kernel/bpf/kmem_cache_iter.c > +++ b/kernel/bpf/kmem_cache_iter.c > @@ -145,6 +145,93 @@ static const struct bpf_iter_seq_info kmem_cache_iter_seq_info = { > .seq_ops = &kmem_cache_iter_seq_ops, > }; > [...]