[PATCH v2 02/12] klp-build: Process the patches before building the original kernel

From: Song Liu

Date: Wed Oct 28 2026 - 17:46:33 EST


klp-build validates the patches before building the original kernel,
but only combines them and adds #line directives after the build, which
applies and reverts them a second time. Nothing in the second step
needs the original build, but the errors it finds, like patches with no
net effect, only show up after a full kernel build.

Do both in one step, before the build. process_patches() checks for
patches to unsupported files, then applies the patches in order, which
also catches patches that don't apply and warns about those which apply
with fuzz, and combines them.

Suggested-by: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
Assisted-by: Claude:claude-opus-5
Signed-off-by: Song Liu <song@xxxxxxxxxx>
---
scripts/livepatch/klp-build | 24 ++++++++----------------
1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/scripts/livepatch/klp-build b/scripts/livepatch/klp-build
index a0bfc40093f9..28c3bf6b6418 100755
--- a/scripts/livepatch/klp-build
+++ b/scripts/livepatch/klp-build
@@ -395,11 +395,10 @@ revert_patch() {
}

apply_patches() {
- local extra_args=("$@")
local patch

for patch in "${PATCHES[@]}"; do
- apply_patch "$patch" "${extra_args[@]}"
+ apply_patch "$patch"
done
}

@@ -413,12 +412,6 @@ revert_patches() {
APPLIED_PATCHES=()
}

-validate_patches() {
- check_unsupported_patches
- apply_patches
- revert_patches
-}
-
do_init() {
# We're not yet smart enough to handle anything other than in-tree
# builds in pwd.
@@ -478,7 +471,7 @@ combine_patches() {
# Diff the orig source files against the patched ones in place to make a
# clean patch. Use /dev/null for created and deleted files.
: > "$outfile"
- apply_patches "--silent"
+ apply_patches
for file in "${files[@]}"; do
local old="$tmpdir/$file"
local new="$file"
@@ -504,12 +497,13 @@ combine_patches() {
revert_patches
}

-# Combine the patches into $COMBINED_PATCH, and fix its lines so as not to
-# affect the __LINE__ macro for otherwise unchanged functions further down the
-# file.
+# Validate the patches, combine them into $COMBINED_PATCH, and fix its lines so
+# as not to affect the __LINE__ macro for otherwise unchanged functions further
+# down the file.
process_patches() {
local tmp_patch="$TMP_DIR/tmp.patch"

+ check_unsupported_patches
combine_patches "$tmp_patch"
[[ ! -s "$tmp_patch" ]] && die "no changes detected"

@@ -889,8 +883,8 @@ process_args "$@"
do_init

if (( SHORT_CIRCUIT <= 2 )); then
- status "Validating patch(es)"
- validate_patches
+ status "Processing patch(es)"
+ process_patches
fi

if (( SHORT_CIRCUIT <= 1 )); then
@@ -902,8 +896,6 @@ if (( SHORT_CIRCUIT <= 1 )); then
fi

if (( SHORT_CIRCUIT <= 2 )); then
- status "Processing patch(es)"
- process_patches
apply_patch "$COMBINED_PATCH" "--silent"
status "Building patched kernel"
build_kernel "patched"
--
2.53.0-Meta