[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <ZwBgLmcEwuplwNSt@google.com>
Date: Fri, 4 Oct 2024 14:37:50 -0700
From: Namhyung Kim <namhyung@...nel.org>
To: Song Liu <song@...nel.org>
Cc: Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>,
Andrii Nakryiko <andrii@...nel.org>,
Martin KaFai Lau <martin.lau@...ux.dev>,
Eduard Zingerman <eddyz87@...il.com>,
Yonghong Song <yonghong.song@...ux.dev>,
John Fastabend <john.fastabend@...il.com>,
KP Singh <kpsingh@...nel.org>, Stanislav Fomichev <sdf@...ichev.me>,
Hao Luo <haoluo@...gle.com>, Jiri Olsa <jolsa@...nel.org>,
LKML <linux-kernel@...r.kernel.org>, bpf@...r.kernel.org,
Andrew Morton <akpm@...ux-foundation.org>,
Christoph Lameter <cl@...ux.com>, Pekka Enberg <penberg@...nel.org>,
David Rientjes <rientjes@...gle.com>,
Joonsoo Kim <iamjoonsoo.kim@....com>,
Vlastimil Babka <vbabka@...e.cz>,
Roman Gushchin <roman.gushchin@...ux.dev>,
Hyeonggon Yoo <42.hyeyoo@...il.com>, linux-mm@...ck.org,
Arnaldo Carvalho de Melo <acme@...nel.org>,
Kees Cook <kees@...nel.org>
Subject: Re: [PATCH v4 bpf-next 1/3] bpf: Add kmem_cache iterator
Hi Song,
On Fri, Oct 04, 2024 at 01:33:19PM -0700, Song Liu wrote:
> On Wed, Oct 2, 2024 at 11:09 AM Namhyung Kim <namhyung@...nel.org> wrote:
> >
> [...]
> > +
> > + mutex_lock(&slab_mutex);
> > +
> > + /*
> > + * Find an entry at the given position in the slab_caches list instead
>
> Nit: style of multi-line comment: "/* Find ...".
Ok, will update.
>
> > + * of keeping a reference (of the last visited entry, if any) out of
> > + * slab_mutex. It might miss something if one is deleted in the middle
> > + * while it releases the lock. But it should be rare and there's not
> > + * much we can do about it.
> > + */
> > + list_for_each_entry(s, &slab_caches, list) {
> > + if (cnt == *pos) {
> > + /*
> > + * Make sure this entry remains in the list by getting
> > + * a new reference count. Note that boot_cache entries
> > + * have a negative refcount, so don't touch them.
> > + */
> > + if (s->refcount > 0)
> > + s->refcount++;
> > + found = true;
> > + break;
> > + }
> > + cnt++;
> > + }
> > + mutex_unlock(&slab_mutex);
> > +
> > + if (!found)
> > + return NULL;
> > +
> > + ++*pos;
> > + return s;
> > +}
> > +
> > +static void kmem_cache_iter_seq_stop(struct seq_file *seq, void *v)
> > +{
> > + struct bpf_iter_meta meta;
> > + struct bpf_iter__kmem_cache ctx = {
> > + .meta = &meta,
> > + .s = v,
> > + };
> > + struct bpf_prog *prog;
> > + bool destroy = false;
> > +
> > + meta.seq = seq;
> > + prog = bpf_iter_get_info(&meta, true);
> > + if (prog)
> > + bpf_iter_run_prog(prog, &ctx);
> > +
> > + if (ctx.s == NULL)
> > + return;
> > +
> > + mutex_lock(&slab_mutex);
> > +
> > + /* Skip kmem_cache_destroy() for active entries */
> > + if (ctx.s->refcount > 1)
> > + ctx.s->refcount--;
> > + else if (ctx.s->refcount == 1)
> > + destroy = true;
> > +
> > + mutex_unlock(&slab_mutex);
> > +
> > + if (destroy)
> > + kmem_cache_destroy(ctx.s);
> > +}
> > +
> > +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.
Thanks,
Namhyung
>
> > + }
> > +
> > + /* Skip kmem_cache_destroy() for active entries */
> > + if (s->refcount > 1)
> > + s->refcount--;
> > + else if (s->refcount == 1)
> > + destroy = true;
> > +
> > + mutex_unlock(&slab_mutex);
> > +
> > + if (destroy)
> > + kmem_cache_destroy(s);
> > +
> > + return next;
> > +}
> [...]
Powered by blists - more mailing lists