Re: [PATCH v4 3/7] vmalloc: Add text_alloc() and text_free()

From: Mike Rapoport
Date: Fri Jul 24 2020 - 06:13:12 EST


On Fri, Jul 24, 2020 at 01:28:35AM +0300, Jarkko Sakkinen wrote:
> On Sat, Jul 18, 2020 at 07:23:59PM +0300, Mike Rapoport wrote:
> > On Fri, Jul 17, 2020 at 06:04:17AM +0300, Jarkko Sakkinen wrote:
> > > Introduce functions for allocating memory for dynamic trampolines, such
> > > as kprobes. An arch can promote the availability of these functions with
> > > CONFIG_ARCH_HAS_TEXT_ALLOC. Provide default/fallback implementation
> > > wrapping module_alloc() and module_memfree().
> > >
> > > Cc: Andi Kleen <ak@xxxxxxxxxxxxxxx>
> > > Cc: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
> > > Suggested-by: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> > > Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@xxxxxxxxxxxxxxx>
> > > ---
> > > include/linux/vmalloc.h | 23 +++++++++++++++++++++++
> > > 1 file changed, 23 insertions(+)
> > >
> > > diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
> > > index 0221f852a7e1..e981436e30b6 100644
> > > --- a/include/linux/vmalloc.h
> > > +++ b/include/linux/vmalloc.h
> > > @@ -9,6 +9,7 @@
> > > #include <asm/page.h> /* pgprot_t */
> > > #include <linux/rbtree.h>
> > > #include <linux/overflow.h>
> > > +#include <linux/moduleloader.h>
> > >
> > > #include <asm/vmalloc.h>
> > >
> > > @@ -249,4 +250,26 @@ pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
> > > int register_vmap_purge_notifier(struct notifier_block *nb);
> > > int unregister_vmap_purge_notifier(struct notifier_block *nb);
> > >
> > > +#ifdef CONFIG_ARCH_HAS_TEXT_ALLOC
> > > +/*
> > > + * Allocate memory to be used for dynamic trampoline code.
> > > + */
> > > +void *text_alloc(unsigned long size);
> > > +
> > > +/*
> > > + * Free memory returned from text_alloc().
> > > + */
> > > +void text_free(void *region);
> > > +#else
> > > +static inline void *text_alloc(unsigned long size)
> > > +{
> > > + return module_alloc(size);
> > > +}
> > > +
> > > +static inline void text_free(void *region)
> > > +{
> > > + module_memfree(region);
> > > +}
> >
> > Using module_alloc() as the default implementation of generic
> > text_alloc() does not sound right to me.
> >
> > I would suggest rename module_alloc() to text_alloc() on x86, as Peter
> > proposed and then add text_alloc_kprobes() that can be overridden by the
> > architectures. x86 could use text_alloc(), arm64 vmalloc() with options
> > of their choice and the fallback would remain module_alloc(). Something
> > like (untested) patch below:
>
> I'm not exactly sure which of the below is relevant as the patch set
> includes the exact same changes with maybe different phrasing:

The difference in parsing is what differentiates semantically clean code
from duct tape.

As several people pointed out, a single text_alloc(), and apprently a
single ARCH_HAS_TEXT_ALLOC, would not fit all architectures and some
ground work required to implement a generic text allocation.

Your patch works aroung this for x86 with broken semantics of
text_alloc() when ARCH_HAS_TEXT_ALLOC is not defined.

My suggestion does not make text_alloc() a special case of
module_alloc() but rather makes text_alloc_kprobes() to fallback to
module_alloc() when architecture does not provide its implementation.

> https://lore.kernel.org/lkml/20200717030422.679972-1-jarkko.sakkinen@xxxxxxxxxxxxxxx/
>
> If there is something that these patches are missing, please do remark
> but these seven patches have been at least tested and split in
> reasonable manner.

My patch is not tested because I only wanted to help with
transiontion from module_alloc() in kprobes to a new clean interface, I
don't really care if kprobes will depend on MODULES...

> /Jarkko

--
Sincerely yours,
Mike.