mbox series

[v7,0/5] Generate modules.builtin.alias from match ids

Message ID 20221216221703.294683-1-allenwebb@google.com
Headers show
Series Generate modules.builtin.alias from match ids | expand

Message

Allen Webb Dec. 16, 2022, 10:16 p.m. UTC
Generate modules.builtin.alias from match ids

This patch series (v7) pivots to adding `modules.builtin.alias` from the
previous approach of adding a sysfs attribute. The goal is for tools
like USBGuard to leverage not only modules.aliases but also
`modules.builtin.aliases` to associate devices with the modules that may
be bound before deciding to authorize a device or not. This is
particularly useful in cases when new devices of a particular type
shouldn't be allowed part of the time like for lock screens.

Note that `modules.builtin.alias` is generated directly by modpost. This
differs from how `modules.alias` is generated because modpost converts
the match-id based module aliases into c-files that add additional
aliases to the module info. No such c-file is present for vmlinuz though
it would be possible to add one. A downside of this would be vmlinuz
would grow by 100-200kb for a typical ChromeOS kernel config.


--

# Generate modules.builtin.alias from match ids

Previous versions of this patch series addressed the same problem by
adding a sysfs attribute instead of `modules.builtin.alias`.
Consequently, they have a different name and include completely
different commits than this version.
Note, cover letters were first added in v5.

  RFC (broken patch): https://lore.kernel.org/lkml/CAJzde042-M4UbpNYKw0eDVg4JqYmwmPYSsmgK+kCMTqsi+-2Yw@mail.gmail.com/
  v1 (missing v1 label): https://lore.kernel.org/lkml/20221111152852.2837363-1-allenwebb@google.com/
  v2 (missing v2 label): https://lore.kernel.org/lkml/20221128201332.3482092-1-allenwebb@google.com/
  v3: https://lore.kernel.org/lkml/20221129224313.455862-1-allenwebb@google.com/
  v4: https://lore.kernel.org/lkml/20221130221447.1202206-1-allenwebb@google.com/
  v5: https://lore.kernel.org/lkml/20221201211630.101541-1-allenwebb@google.com/
  v6: https://lore.kernel.org/lkml/20221202224540.1446952-1-allenwebb@google.com/
  v7: This version


## Patch series status

This series is still going through revisions in response to comments.
This version generates match-id based aliases for all subsystems unlike
previous patch series versions which only implemented aliases for USB.

I believe there is potential to improve the Makefile part of the patch
series as well as an open question of whether modpost should generate
`modules.built.alias` directly or create a vmlinuz.mod.c containing the
missing module info for the match-id based aliases for built-in modules.

## Acknowledgements

Thanks to Greg Kroah-Hartman and the Linux maintainers for being patient
with me as I have worked through learning the kernel workflow to get
this series into a more presentable state.

Thanks to Luis Chamberlain for raising the alternative of using kmod to
address the primary motivation of the patch series.

Also, thanks to Intel's kernel test robot <lkp@intel.com> for catching
issues that showed up on different kernel configurations.




Allen Webb (5):
  module.h: MODULE_DEVICE_TABLE for built-in modules
  modpost: Track module name for built-in modules
  modpost: Add -b option for emitting built-in aliases
  file2alias.c: Implement builtin.alias generation
  build: Add modules.builtin.alias

 .gitignore               |  1 +
 Makefile                 |  1 +
 include/linux/module.h   | 10 ++++-
 scripts/Makefile.modpost | 17 +++++++-
 scripts/mod/file2alias.c | 92 +++++++++++++++++++++++++++++++---------
 scripts/mod/modpost.c    | 23 +++++++++-
 scripts/mod/modpost.h    |  2 +
 7 files changed, 121 insertions(+), 25 deletions(-)

Comments

Christophe Leroy Dec. 17, 2022, 10:05 a.m. UTC | #1
Le 16/12/2022 à 23:16, Allen Webb a écrit :
> Implement MODULE_DEVICE_TABLE for build-in modules to make it possible
> to generate a builtin.alias file to complement modules.alias.
> 
> Signed-off-by: Allen Webb <allenwebb@google.com>
> ---
>   include/linux/module.h | 10 +++++++++-
>   1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/module.h b/include/linux/module.h
> index ec61fb53979a9..49e4019393127 100644
> --- a/include/linux/module.h
> +++ b/include/linux/module.h
> @@ -243,7 +243,15 @@ extern void cleanup_module(void);
>   extern typeof(name) __mod_##type##__##name##_device_table		\
>     __attribute__ ((unused, alias(__stringify(name))))
>   #else  /* !MODULE */
> -#define MODULE_DEVICE_TABLE(type, name)
> +/* The names may not be unique for built-in modules, so include the module name
> + * to guarantee uniqueness.
> + */

This is network only comment style.

Other parts of kenel have different style, see 
https://docs.kernel.org/process/coding-style.html#commenting

> +#define MODULE_DEVICE_TABLE(type, name)					\
> +extern void *CONCATENATE(						\

'extern' keyword is pointless of function prototypes and deprecated. 
Don't add new occurences.

> +	CONCATENATE(__mod_##type##__##name##__,				\
> +		__KBUILD_MODNAME),					\
> +	_device_table)							\
> +	__attribute__ ((unused, alias(__stringify(name))))
>   #endif
>   
>   /* Version of form [<epoch>:]<version>[-<extra-version>].
Allen Webb Dec. 19, 2022, 3:56 p.m. UTC | #2
On Sat, Dec 17, 2022 at 4:05 AM Christophe Leroy
<christophe.leroy@csgroup.eu> wrote:
>
>
>
> Le 16/12/2022 à 23:16, Allen Webb a écrit :
> > Implement MODULE_DEVICE_TABLE for build-in modules to make it possible
> > to generate a builtin.alias file to complement modules.alias.
> >
> > Signed-off-by: Allen Webb <allenwebb@google.com>
> > ---
> >   include/linux/module.h | 10 +++++++++-
> >   1 file changed, 9 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/linux/module.h b/include/linux/module.h
> > index ec61fb53979a9..49e4019393127 100644
> > --- a/include/linux/module.h
> > +++ b/include/linux/module.h
> > @@ -243,7 +243,15 @@ extern void cleanup_module(void);
> >   extern typeof(name) __mod_##type##__##name##_device_table           \
> >     __attribute__ ((unused, alias(__stringify(name))))
> >   #else  /* !MODULE */
> > -#define MODULE_DEVICE_TABLE(type, name)
> > +/* The names may not be unique for built-in modules, so include the module name
> > + * to guarantee uniqueness.
> > + */
>
> This is network only comment style.

I have fixed this in my local copy and will include it with the next upload.

>
> Other parts of kenel have different style, see
> https://docs.kernel.org/process/coding-style.html#commenting
>
> > +#define MODULE_DEVICE_TABLE(type, name)                                      \
> > +extern void *CONCATENATE(                                            \
>
> 'extern' keyword is pointless of function prototypes and deprecated.
> Don't add new occurences.


This is a weird case because these symbols are used for post
compilation processing by modpost. If I drop the extern keyword, the
build fails with a bunch of errors of the form:

/mnt/host/source/src/third_party/kernel/upstream/drivers/hid/hid-generic.c:79:1:
error: definition
'__mod_hid__hid_table__kmod_hid_generic_device_table' cannot also be
an alias
MODULE_DEVICE_TABLE(hid, hid_table);
^
/mnt/host/source/src/third_party/kernel/upstream/include/linux/module.h:255:26:
note: expanded from macro 'MODULE_DEVICE_TABLE'
        __attribute__ ((unused, alias(__stringify(name))))

>
>
> > +     CONCATENATE(__mod_##type##__##name##__,                         \
> > +             __KBUILD_MODNAME),                                      \
> > +     _device_table)                                                  \
> > +     __attribute__ ((unused, alias(__stringify(name))))
> >   #endif
> >
> >   /* Version of form [<epoch>:]<version>[-<extra-version>].
Greg Kroah-Hartman Dec. 19, 2022, 7:21 p.m. UTC | #3
On Mon, Dec 19, 2022 at 01:18:47PM -0600, Allen Webb wrote:
> A one character difference in the name supplied to MODULE_DEVICE_TABLE
> breaks a future patch set, so fix the typo.
> 
> Reported-by: kernel test robot <lkp@intel.com>
> Signed-off-by: Allen Webb <allenwebb@google.com>
> ---
>  drivers/soc/imx/imx8mp-blk-ctrl.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/soc/imx/imx8mp-blk-ctrl.c b/drivers/soc/imx/imx8mp-blk-ctrl.c
> index 0e3b6ba22f943..344a0a71df14a 100644
> --- a/drivers/soc/imx/imx8mp-blk-ctrl.c
> +++ b/drivers/soc/imx/imx8mp-blk-ctrl.c
> @@ -743,7 +743,7 @@ static const struct of_device_id imx8mp_blk_ctrl_of_match[] = {
>  		/* Sentinel */
>  	}
>  };
> -MODULE_DEVICE_TABLE(of, imx8m_blk_ctrl_of_match);
> +MODULE_DEVICE_TABLE(of, imx8mp_blk_ctrl_of_match);

What commit id does this fix?  Shouldn't this be also cc: stable to
resolve this issue for older kernels as obviousl the module device table
for auto-loading is not correct?

thanks,

greg k-h
Luis Chamberlain Dec. 19, 2022, 8:06 p.m. UTC | #4
On Mon, Dec 19, 2022 at 01:18:46PM -0600, Allen Webb wrote:
> Generate modules.builtin.alias from match ids

This is looking much better, thanks! Please expand with proper
documentation on the use case / value of this on the file:

Documentation/kbuild/kbuild.rst

  Luis