Re: [PATCH v4 bpf-next 1/3] bpf: Add kmem_cache iterator

From: Song Liu
Date: Fri Oct 04 2024 - 17:47:03 EST


On Fri, Oct 4, 2024 at 2:37 PM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
>
> Hi Song,
>
> On Fri, Oct 04, 2024 at 01:33:19PM -0700, Song Liu wrote:
[...]
> > > +
> > > +static void *kmem_cache_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos)
> > > +{
> > > + struct kmem_cache *s = v;
> > > + struct kmem_cache *next = NULL;
> > > + bool destroy = false;
> > > +
> > > + ++*pos;
> > > +
> > > + mutex_lock(&slab_mutex);
> > > +
> > > + if (list_last_entry(&slab_caches, struct kmem_cache, list) != s) {
> > > + next = list_next_entry(s, list);
> > > + if (next->refcount > 0)
> > > + next->refcount++;
> >
> > What if next->refcount <=0? Shall we find next of next?
>
> The slab_mutex should protect refcount == 0 case so it won't see that.
> The negative refcount means it's a boot_cache and we shouldn't touch the
> refcount.

I see. Thanks for the explanation!

Please add a comment here, and maybe also add

WARN_ON_ONCE(next ->refcount == 0).

Song