diff mbox series

[3/5] codetag: Introduce codetag_early_walk()

Message ID 20240809073309.2134488-3-kees@kernel.org (mailing list archive)
State New
Headers show
Series slab: Allocate and use per-call-site caches | expand

Commit Message

Kees Cook Aug. 9, 2024, 7:33 a.m. UTC
In order to process builtin alloc_tags much earlier during boot (before
register_codetag() is processed), provide codetag_early_walk() that
perform a lockless walk with a specified callback function. This will be
used to allocate required caches that cannot be allocated on demand.

Signed-off-by: Kees Cook <kees@kernel.org>
---
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Kent Overstreet <kent.overstreet@linux.dev>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Christoph Lameter <cl@linux.com>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: linux-mm@kvack.org
---
 include/linux/codetag.h |  2 ++
 lib/codetag.c           | 16 ++++++++++++++++
 2 files changed, 18 insertions(+)

Comments

Suren Baghdasaryan Aug. 29, 2024, 3:39 p.m. UTC | #1
On Fri, Aug 9, 2024 at 12:33 AM Kees Cook <kees@kernel.org> wrote:
>
> In order to process builtin alloc_tags much earlier during boot (before
> register_codetag() is processed), provide codetag_early_walk() that
> perform a lockless walk with a specified callback function. This will be
> used to allocate required caches that cannot be allocated on demand.
>
> Signed-off-by: Kees Cook <kees@kernel.org>
> ---
> Cc: Suren Baghdasaryan <surenb@google.com>
> Cc: Kent Overstreet <kent.overstreet@linux.dev>
> Cc: Vlastimil Babka <vbabka@suse.cz>
> Cc: Christoph Lameter <cl@linux.com>
> Cc: Pekka Enberg <penberg@kernel.org>
> Cc: David Rientjes <rientjes@google.com>
> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Roman Gushchin <roman.gushchin@linux.dev>
> Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
> Cc: linux-mm@kvack.org
> ---
>  include/linux/codetag.h |  2 ++
>  lib/codetag.c           | 16 ++++++++++++++++
>  2 files changed, 18 insertions(+)
>
> diff --git a/include/linux/codetag.h b/include/linux/codetag.h
> index c2a579ccd455..9eb1fcd90570 100644
> --- a/include/linux/codetag.h
> +++ b/include/linux/codetag.h
> @@ -64,6 +64,8 @@ void codetag_lock_module_list(struct codetag_type *cttype, bool lock);
>  bool codetag_trylock_module_list(struct codetag_type *cttype);
>  struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype);
>  struct codetag *codetag_next_ct(struct codetag_iterator *iter);
> +void codetag_early_walk(const struct codetag_type_desc *desc,
> +                       void (*callback)(struct codetag *ct));
>
>  void codetag_to_text(struct seq_buf *out, struct codetag *ct);
>
> diff --git a/lib/codetag.c b/lib/codetag.c
> index ef7634c7ee18..9d563c8c088a 100644
> --- a/lib/codetag.c
> +++ b/lib/codetag.c
> @@ -154,6 +154,22 @@ static struct codetag_range get_section_range(struct module *mod,
>         };
>  }
>
> +void codetag_early_walk(const struct codetag_type_desc *desc,
> +                       void (*callback)(struct codetag *ct))
> +{
> +       struct codetag_range range;
> +       struct codetag *ct;
> +
> +       range = get_section_range(NULL, desc->section);
> +       if (!range.start || !range.stop ||
> +           range.start == range.stop ||
> +           range.start > range.stop)
> +               return;

I think this check can be simplified to:

        if (!range.start || range.start >= range.stop)
                return;

nit: Technically (!range.start) should also never trigger. In a valid
image these symbols are either missing (range.start == range.stop ==
NULL) or both are defined and (range.start < range.stop).

> +
> +       for (ct = range.start; ct < range.stop; ct = ((void *)ct + desc->tag_size))
> +               callback(ct);
> +}
> +
>  static int codetag_module_init(struct codetag_type *cttype, struct module *mod)
>  {
>         struct codetag_range range;
> --
> 2.34.1
>
Kees Cook Sept. 11, 2024, 10:18 p.m. UTC | #2
On Thu, Aug 29, 2024 at 08:39:29AM -0700, Suren Baghdasaryan wrote:
> On Fri, Aug 9, 2024 at 12:33 AM Kees Cook <kees@kernel.org> wrote:
> >
> > In order to process builtin alloc_tags much earlier during boot (before
> > register_codetag() is processed), provide codetag_early_walk() that
> > perform a lockless walk with a specified callback function. This will be
> > used to allocate required caches that cannot be allocated on demand.
> >
> > Signed-off-by: Kees Cook <kees@kernel.org>
> > ---
> > Cc: Suren Baghdasaryan <surenb@google.com>
> > Cc: Kent Overstreet <kent.overstreet@linux.dev>
> > Cc: Vlastimil Babka <vbabka@suse.cz>
> > Cc: Christoph Lameter <cl@linux.com>
> > Cc: Pekka Enberg <penberg@kernel.org>
> > Cc: David Rientjes <rientjes@google.com>
> > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
> > Cc: Andrew Morton <akpm@linux-foundation.org>
> > Cc: Roman Gushchin <roman.gushchin@linux.dev>
> > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
> > Cc: linux-mm@kvack.org
> > ---
> >  include/linux/codetag.h |  2 ++
> >  lib/codetag.c           | 16 ++++++++++++++++
> >  2 files changed, 18 insertions(+)
> >
> > diff --git a/include/linux/codetag.h b/include/linux/codetag.h
> > index c2a579ccd455..9eb1fcd90570 100644
> > --- a/include/linux/codetag.h
> > +++ b/include/linux/codetag.h
> > @@ -64,6 +64,8 @@ void codetag_lock_module_list(struct codetag_type *cttype, bool lock);
> >  bool codetag_trylock_module_list(struct codetag_type *cttype);
> >  struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype);
> >  struct codetag *codetag_next_ct(struct codetag_iterator *iter);
> > +void codetag_early_walk(const struct codetag_type_desc *desc,
> > +                       void (*callback)(struct codetag *ct));
> >
> >  void codetag_to_text(struct seq_buf *out, struct codetag *ct);
> >
> > diff --git a/lib/codetag.c b/lib/codetag.c
> > index ef7634c7ee18..9d563c8c088a 100644
> > --- a/lib/codetag.c
> > +++ b/lib/codetag.c
> > @@ -154,6 +154,22 @@ static struct codetag_range get_section_range(struct module *mod,
> >         };
> >  }
> >
> > +void codetag_early_walk(const struct codetag_type_desc *desc,
> > +                       void (*callback)(struct codetag *ct))
> > +{
> > +       struct codetag_range range;
> > +       struct codetag *ct;
> > +
> > +       range = get_section_range(NULL, desc->section);
> > +       if (!range.start || !range.stop ||
> > +           range.start == range.stop ||
> > +           range.start > range.stop)
> > +               return;
> 
> I think this check can be simplified to:
> 
>         if (!range.start || range.start >= range.stop)
>                 return;
> 
> nit: Technically (!range.start) should also never trigger. In a valid
> image these symbols are either missing (range.start == range.stop ==
> NULL) or both are defined and (range.start < range.stop).

Yeah, all true. I was mainly copying all the checks that existed in the
"slow path" version.

I will adjust this for the next version.
diff mbox series

Patch

diff --git a/include/linux/codetag.h b/include/linux/codetag.h
index c2a579ccd455..9eb1fcd90570 100644
--- a/include/linux/codetag.h
+++ b/include/linux/codetag.h
@@ -64,6 +64,8 @@  void codetag_lock_module_list(struct codetag_type *cttype, bool lock);
 bool codetag_trylock_module_list(struct codetag_type *cttype);
 struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype);
 struct codetag *codetag_next_ct(struct codetag_iterator *iter);
+void codetag_early_walk(const struct codetag_type_desc *desc,
+			void (*callback)(struct codetag *ct));
 
 void codetag_to_text(struct seq_buf *out, struct codetag *ct);
 
diff --git a/lib/codetag.c b/lib/codetag.c
index ef7634c7ee18..9d563c8c088a 100644
--- a/lib/codetag.c
+++ b/lib/codetag.c
@@ -154,6 +154,22 @@  static struct codetag_range get_section_range(struct module *mod,
 	};
 }
 
+void codetag_early_walk(const struct codetag_type_desc *desc,
+			void (*callback)(struct codetag *ct))
+{
+	struct codetag_range range;
+	struct codetag *ct;
+
+	range = get_section_range(NULL, desc->section);
+	if (!range.start || !range.stop ||
+	    range.start == range.stop ||
+	    range.start > range.stop)
+		return;
+
+	for (ct = range.start; ct < range.stop; ct = ((void *)ct + desc->tag_size))
+		callback(ct);
+}
+
 static int codetag_module_init(struct codetag_type *cttype, struct module *mod)
 {
 	struct codetag_range range;