[PATCH 2/2] kbuild: sign modules in bulk when the signing key is on PKCS#11
From: Massimiliano Marretta
Date: Fri Oct 09 2026 - 06:21:52 EST
When CONFIG_MODULE_SIG_KEY contains a PKCS#11 URI, each module is
currently signed by a separate invocation of scripts/sign-file.
This is inefficient because every invocation pays the fixed cost
of initializing the PKCS#11 token, verifying the PIN and setting
up the session, which can take tens of seconds per module.
Use the newly added '-b' option of scripts/sign-file to sign all
modules in a single invocation when the signing key is provided
as a PKCS#11 URI. Modules are installed first, then signed in
bulk in a dedicated target that runs before depmod.
When the signing key is a PEM file, the classic per-module signing
path is kept unchanged, since the bulk mode provides no benefit
in that case.
Measured on a build with 50 modules and the key stored on a
SmartCard-HSM:
- before: ~24 minutes
- after: ~3 min 17 s
Signed-off-by: Massimiliano Marretta <massimiliano.marretta@xxxxxxxxxx>
---
scripts/Makefile.modinst | 43 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 42 insertions(+), 1 deletion(-)
diff --git a/scripts/Makefile.modinst b/scripts/Makefile.modinst
index 9ba45e5b32b1..e7894d195e7d 100644
--- a/scripts/Makefile.modinst
+++ b/scripts/Makefile.modinst
@@ -101,34 +101,63 @@ endif
#
ifeq ($(filter pkcs11:%, $(CONFIG_MODULE_SIG_KEY)),)
sig-key := $(if $(wildcard $(CONFIG_MODULE_SIG_KEY)),,$(objtree)/)$(CONFIG_MODULE_SIG_KEY)
+sign-bulk :=
else
sig-key := $(CONFIG_MODULE_SIG_KEY)
+# Signing via a PKCS#11 URI is much faster in bulk mode, as the
+# per-invocation initialization cost of the token is high.
+sign-bulk := 1
endif
+
quiet_cmd_sign = SIGN $@
cmd_sign = $(objtree)/scripts/sign-file $(CONFIG_MODULE_SIG_HASH) "$(sig-key)" $(objtree)/certs/signing_key.x509 $@ \
$(if $(KBUILD_EXTMOD),|| true)
+quiet_cmd_bulk_sign = BULK SIGN
+ cmd_bulk_sign = $(objtree)/scripts/sign-file -b $(CONFIG_MODULE_SIG_HASH) "$(sig-key)" $(objtree)/certs/signing_key.x509 $(modules) \
+ $(if $(KBUILD_EXTMOD),|| true)
+
ifeq ($(sign-only),)
# During modules_install, modules are signed only when CONFIG_MODULE_SIG_ALL=y.
ifndef CONFIG_MODULE_SIG_ALL
quiet_cmd_sign :=
cmd_sign := :
+quiet_cmd_bulk_sign :=
+ cmd_bulk_sign := :
endif
# Create necessary directories
$(foreach dir, $(sort $(dir $(install-y))), $(shell mkdir -p $(dir)))
+ifeq ($(sign-bulk),)
+
$(dst)/%.ko: %.ko FORCE
$(call cmd,install)
$(call cmd,strip)
$(call cmd,sign)
+else
+
+$(dst)/%.ko: %.ko FORCE
+ $(call cmd,install)
+ $(call cmd,strip)
+
+PHONY += bulk-sign-modules
+bulk-sign-modules: $(install-y)
+ $(call cmd,bulk_sign)
+
+__modinst: bulk-sign-modules
+
+BULK_SIGN_DEP := bulk-sign-modules
+
+endif
+
ifdef CONFIG_MODULES
__modinst: depmod
PHONY += depmod
-depmod: $(install-y)
+depmod: $(install-y) $(BULK_SIGN_DEP)
$(call cmd,depmod)
quiet_cmd_depmod = DEPMOD $(MODLIB)
@@ -137,9 +166,21 @@ endif
else
+ifeq ($(sign-bulk),)
+
$(dst)/%.ko: FORCE
$(call cmd,sign)
+else
+
+PHONY += bulk-sign-modules
+bulk-sign-modules: $(install-y)
+ $(call cmd,bulk_sign)
+
+__modinst: bulk-sign-modules
+
+endif
+
endif
#
--
2.43.0