[PATCH v5 6/8] kbuild: Allow adding modules into the FIT ramdisk

Simon Glass posted 8 patches 2 months, 3 weeks ago
There is a newer version of this series
[PATCH v5 6/8] kbuild: Allow adding modules into the FIT ramdisk
Posted by Simon Glass 2 months, 3 weeks ago
Support 'make image.fit FIT_MODULES=1' to put all the modules into a
ramdisk image within the FIT.

Add image.fit as a target which requires modules, so that modules will
built automatically when using FIT_MODULES=1

Signed-off-by: Simon Glass <sjg@chromium.org>
Suggested-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Suggested-by: Reviewed-by: Nicolas Schier <nsc@kernel.org>
Acked-by: Nathan Chancellor <nathan@kernel.org>
---

Changes in v5:
- Build modules automatically if needed (fix from Nicolas Schier)

Changes in v4:
- Rename the Makefile variable from 'EXTRA' to 'MAKE_FIT_FLAGS'
- Use an empty FIT_MODULES to disable the feature, instead of '0'
- Make use of the 'modules' dependency to ensure modules are built
- Pass the list of modules to the script

 Makefile             |  1 +
 arch/arm64/Makefile  |  1 +
 scripts/Makefile.lib | 10 ++++++++--
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 8cd46222fc48..4eccaef95826 100644
--- a/Makefile
+++ b/Makefile
@@ -773,6 +773,7 @@ endif
 # Just "make" or "make all" shall build modules as well
 
 modules-targets := all
+modules-targets += $(if $(FIT_MODULES),image.fit)
 modules-targets += modules
 modules-targets += nsdeps
 modules-targets += compile_commands.json
diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index 73a10f65ce8b..7036f251ab40 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -174,6 +174,7 @@ endif
 all:	$(notdir $(KBUILD_IMAGE))
 
 image.fit: dtbs
+image.fit: $(if $(FIT_MODULES),modules)
 
 vmlinuz.efi image.fit: Image
 $(BOOT_TARGETS): vmlinux
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 1d581ba5df66..c6a3aa653035 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -398,11 +398,17 @@ MAKE_FIT := $(srctree)/scripts/make_fit.py
 # Use this to override the compression algorithm
 FIT_COMPRESSION ?= gzip
 
+# Set this to 1 to include an initrd with all the kernel modules
+FIT_MODULES ?=
+
 quiet_cmd_fit = FIT     $@
-      cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
-		--name '$(UIMAGE_NAME)' \
+      cmd_fit = $(if $(FIT_MODULES), \
+		find $(objtree) -name '*.ko' > $(objtree)/.modules-list 2>/dev/null &&) \
+		$(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
+		--name '$(UIMAGE_NAME)' $(MAKE_FIT_FLAGS) \
 		$(if $(findstring 1,$(KBUILD_VERBOSE)),-v) \
 		$(if $(FIT_DECOMPOSE_DTBS),--decompose-dtbs) \
+		$(if $(FIT_MODULES),--modules @$(objtree)/.modules-list) \
 		--compress $(FIT_COMPRESSION) -k $< @$(word 2,$^)
 
 # XZ
-- 
2.43.0
Re: [PATCH v5 6/8] kbuild: Allow adding modules into the FIT ramdisk
Posted by Thomas Weißschuh 2 months, 3 weeks ago
On Fri, Nov 14, 2025 at 07:27:32AM -0700, Simon Glass wrote:
> Support 'make image.fit FIT_MODULES=1' to put all the modules into a
> ramdisk image within the FIT.
> 
> Add image.fit as a target which requires modules, so that modules will
> built automatically when using FIT_MODULES=1
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>
> Suggested-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> Suggested-by: Reviewed-by: Nicolas Schier <nsc@kernel.org>
> Acked-by: Nathan Chancellor <nathan@kernel.org>
> ---
> 
> Changes in v5:
> - Build modules automatically if needed (fix from Nicolas Schier)
> 
> Changes in v4:
> - Rename the Makefile variable from 'EXTRA' to 'MAKE_FIT_FLAGS'
> - Use an empty FIT_MODULES to disable the feature, instead of '0'
> - Make use of the 'modules' dependency to ensure modules are built
> - Pass the list of modules to the script
> 
>  Makefile             |  1 +
>  arch/arm64/Makefile  |  1 +
>  scripts/Makefile.lib | 10 ++++++++--
>  3 files changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 8cd46222fc48..4eccaef95826 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -773,6 +773,7 @@ endif
>  # Just "make" or "make all" shall build modules as well
>  
>  modules-targets := all
> +modules-targets += $(if $(FIT_MODULES),image.fit)
>  modules-targets += modules
>  modules-targets += nsdeps
>  modules-targets += compile_commands.json
> diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
> index 73a10f65ce8b..7036f251ab40 100644
> --- a/arch/arm64/Makefile
> +++ b/arch/arm64/Makefile
> @@ -174,6 +174,7 @@ endif
>  all:	$(notdir $(KBUILD_IMAGE))
>  
>  image.fit: dtbs
> +image.fit: $(if $(FIT_MODULES),modules)
>  
>  vmlinuz.efi image.fit: Image
>  $(BOOT_TARGETS): vmlinux
> diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
> index 1d581ba5df66..c6a3aa653035 100644
> --- a/scripts/Makefile.lib
> +++ b/scripts/Makefile.lib
> @@ -398,11 +398,17 @@ MAKE_FIT := $(srctree)/scripts/make_fit.py
>  # Use this to override the compression algorithm
>  FIT_COMPRESSION ?= gzip
>  
> +# Set this to 1 to include an initrd with all the kernel modules
> +FIT_MODULES ?=

'0' will also trigger that behavior.

> +
>  quiet_cmd_fit = FIT     $@
> -      cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
> -		--name '$(UIMAGE_NAME)' \
> +      cmd_fit = $(if $(FIT_MODULES), \
> +		find $(objtree) -name '*.ko' > $(objtree)/.modules-list 2>/dev/null &&) \

This will include stale module files. You can get an up-to-date list from
$(objtree)/modules.order with a bit post-processing.
Maybe kbuild can be extended to also create a list of the .ko files.
(I would be interested in that for my own usecases, too)

> +		$(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
> +		--name '$(UIMAGE_NAME)' $(MAKE_FIT_FLAGS) \
>  		$(if $(findstring 1,$(KBUILD_VERBOSE)),-v) \
>  		$(if $(FIT_DECOMPOSE_DTBS),--decompose-dtbs) \
> +		$(if $(FIT_MODULES),--modules @$(objtree)/.modules-list) \
>  		--compress $(FIT_COMPRESSION) -k $< @$(word 2,$^)
>  
>  # XZ
> -- 
> 2.43.0
>
Re: [PATCH v5 6/8] kbuild: Allow adding modules into the FIT ramdisk
Posted by Nicolas Schier 2 months, 3 weeks ago
On Fri, Nov 14, 2025 at 04:29:33PM +0100, Thomas Weißschuh wrote:
> On Fri, Nov 14, 2025 at 07:27:32AM -0700, Simon Glass wrote:
> > Support 'make image.fit FIT_MODULES=1' to put all the modules into a
> > ramdisk image within the FIT.
> > 
> > Add image.fit as a target which requires modules, so that modules will
> > built automatically when using FIT_MODULES=1
> > 
> > Signed-off-by: Simon Glass <sjg@chromium.org>
> > Suggested-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> > Suggested-by: Reviewed-by: Nicolas Schier <nsc@kernel.org>
> > Acked-by: Nathan Chancellor <nathan@kernel.org>
> > ---
> > 
> > Changes in v5:
> > - Build modules automatically if needed (fix from Nicolas Schier)
> > 
> > Changes in v4:
> > - Rename the Makefile variable from 'EXTRA' to 'MAKE_FIT_FLAGS'
> > - Use an empty FIT_MODULES to disable the feature, instead of '0'
> > - Make use of the 'modules' dependency to ensure modules are built
> > - Pass the list of modules to the script
> > 
> >  Makefile             |  1 +
> >  arch/arm64/Makefile  |  1 +
> >  scripts/Makefile.lib | 10 ++++++++--
> >  3 files changed, 10 insertions(+), 2 deletions(-)
> > 
> > diff --git a/Makefile b/Makefile
> > index 8cd46222fc48..4eccaef95826 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -773,6 +773,7 @@ endif
> >  # Just "make" or "make all" shall build modules as well
> >  
> >  modules-targets := all
> > +modules-targets += $(if $(FIT_MODULES),image.fit)
> >  modules-targets += modules
> >  modules-targets += nsdeps
> >  modules-targets += compile_commands.json
> > diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
> > index 73a10f65ce8b..7036f251ab40 100644
> > --- a/arch/arm64/Makefile
> > +++ b/arch/arm64/Makefile
> > @@ -174,6 +174,7 @@ endif
> >  all:	$(notdir $(KBUILD_IMAGE))
> >  
> >  image.fit: dtbs
> > +image.fit: $(if $(FIT_MODULES),modules)
> >  
> >  vmlinuz.efi image.fit: Image
> >  $(BOOT_TARGETS): vmlinux
> > diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
> > index 1d581ba5df66..c6a3aa653035 100644
> > --- a/scripts/Makefile.lib
> > +++ b/scripts/Makefile.lib
> > @@ -398,11 +398,17 @@ MAKE_FIT := $(srctree)/scripts/make_fit.py
> >  # Use this to override the compression algorithm
> >  FIT_COMPRESSION ?= gzip
> >  
> > +# Set this to 1 to include an initrd with all the kernel modules
> > +FIT_MODULES ?=
> 
> '0' will also trigger that behavior.
> 
> > +
> >  quiet_cmd_fit = FIT     $@
> > -      cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
> > -		--name '$(UIMAGE_NAME)' \
> > +      cmd_fit = $(if $(FIT_MODULES), \
> > +		find $(objtree) -name '*.ko' > $(objtree)/.modules-list 2>/dev/null &&) \
> 
> This will include stale module files. You can get an up-to-date list from
> $(objtree)/modules.order with a bit post-processing.
> Maybe kbuild can be extended to also create a list of the .ko files.
> (I would be interested in that for my own usecases, too)

oh yes, thanks for the pointer.  This is indeed quite simple and much
better than calling find.  For in-tree kmods:

    compiled-modules = $(patsubst %.o,%.ko,$(call read-file, $(objtree)/modules.order))

But as we need the list of modules in a file, we can also add a
$(call write-file,FILE,TEXT) macro (cp. read-file in
scripts/Kbuild.include).

Thomas, is this sufficient for your use case?  Or do you also need a
make target outputting the list of kmods?


Kind regards,
Nicolas
Re: [PATCH v5 6/8] kbuild: Allow adding modules into the FIT ramdisk
Posted by Thomas Weißschuh 2 months, 3 weeks ago
On Fri, Nov 14, 2025 at 08:50:27PM +0100, Nicolas Schier wrote:
> On Fri, Nov 14, 2025 at 04:29:33PM +0100, Thomas Weißschuh wrote:
> > On Fri, Nov 14, 2025 at 07:27:32AM -0700, Simon Glass wrote:

(...)

> > >  quiet_cmd_fit = FIT     $@
> > > -      cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
> > > -		--name '$(UIMAGE_NAME)' \
> > > +      cmd_fit = $(if $(FIT_MODULES), \
> > > +		find $(objtree) -name '*.ko' > $(objtree)/.modules-list 2>/dev/null &&) \
> > 
> > This will include stale module files. You can get an up-to-date list from
> > $(objtree)/modules.order with a bit post-processing.
> > Maybe kbuild can be extended to also create a list of the .ko files.
> > (I would be interested in that for my own usecases, too)
> 
> oh yes, thanks for the pointer.  This is indeed quite simple and much
> better than calling find.  For in-tree kmods:
> 
>     compiled-modules = $(patsubst %.o,%.ko,$(call read-file, $(objtree)/modules.order))

Tiny nitpick: IMO 'built' modules would be more accurate than 'compiled' modules.

> But as we need the list of modules in a file, we can also add a
> $(call write-file,FILE,TEXT) macro (cp. read-file in
> scripts/Kbuild.include).

> Thomas, is this sufficient for your use case?  Or do you also need a
> make target outputting the list of kmods?

For my usecase I need it in a file [0], passing them on the command line will
likely run into argument list length limitations.

A new file 'modules.built' generated from modules.order would be useful both
for this patch, mine and other tools like scripts/package/builddeb.
(Or 'modules.loadable', matching the scheme from 'modules.builtin')
On the other hand the modules.order seems to have quite some special logic in
the Makefile, so it may not be straigtforward to add such a file.

[0] https://lore.kernel.org/lkml/20250429-module-hashes-v3-9-00e9258def9e@weissschuh.net/#Z31scripts:module-hashes.sh


Thomas