Re: [PATCH v2 03/12] klp-build: Move patch processing to klp-prepare-patch
From: Josh Poimboeuf
Date: Tue Nov 10 2026 - 14:57:01 EST
On Wed, Oct 28, 2026 at 02:45:25PM -0700, Song Liu wrote:
> -apply_patches() {
> - local patch
> -
> - for patch in "${PATCHES[@]}"; do
> - apply_patch "$patch"
> - done
> -}
> -
> revert_patches() {
> local patches=("${APPLIED_PATCHES[@]}")
>
I think we can remove even more cruft:
diff --git a/scripts/livepatch/klp-build b/scripts/livepatch/klp-build
index 4796b36b5cc8e..b93e1227b4c03 100755
--- a/scripts/livepatch/klp-build
+++ b/scripts/livepatch/klp-build
@@ -19,7 +19,7 @@ set -o nounset
# This helps keep execution in pipes so pipefail+ERR trap can catch errors.
shopt -s lastpipe
-unset DEBUG_CLONE DIFF_CHECKSUM SKIP_CLEANUP VERBOSE XTRACE
+unset DEBUG_CLONE DIFF_CHECKSUM PATCH_APPLIED SKIP_CLEANUP VERBOSE XTRACE
REPLACE=1
SHORT_CIRCUIT=0
@@ -110,7 +110,7 @@ restore_files() {
cleanup() {
set +o nounset
- revert_patches
+ revert_patch
restore_files
[[ "$KEEP_TMP" -eq 0 ]] && rm -rf "$TMP_DIR"
return 0
@@ -329,50 +329,14 @@ set_kernelversion() {
}
apply_patch() {
- local patch="$1"
- shift
- local extra_args=("$@")
- local drift_regex="with fuzz|offset [0-9]+ line"
- local output
- local status
-
- [[ ! -f "$patch" ]] && die "$patch doesn't exist"
- status=0
- output=$(patch -p1 --dry-run --no-backup-if-mismatch -r /dev/null "${extra_args[@]}" < "$patch" 2>&1) || status=$?
- if [[ "$status" -ne 0 ]]; then
- echo "$output" >&2
- die "$patch did not apply"
- elif [[ "$output" =~ $drift_regex ]]; then
- [[ -v VERBOSE ]] && echo "$output" >&2
- warn "${patch} applied with fuzz"
- fi
-
- APPLIED_PATCHES+=("$patch")
- patch -p1 --no-backup-if-mismatch -r /dev/null "${extra_args[@]}" --silent < "$patch"
+ PATCH_APPLIED=1
+ patch -p1 --no-backup-if-mismatch -r /dev/null --silent < "$COMBINED_PATCH"
}
revert_patch() {
- local patch="$1"
- local tmp=()
-
- patch -p1 -R --force --no-backup-if-mismatch -r /dev/null &> /dev/null < "$patch" || true
-
- for p in "${APPLIED_PATCHES[@]}"; do
- [[ "$p" == "$patch" ]] && continue
- tmp+=("$p")
- done
-
- APPLIED_PATCHES=("${tmp[@]}")
-}
-
-revert_patches() {
- local patches=("${APPLIED_PATCHES[@]}")
-
- for (( i=${#patches[@]}-1 ; i>=0 ; i-- )) ; do
- revert_patch "${patches[$i]}"
- done
-
- APPLIED_PATCHES=()
+ [[ -v PATCH_APPLIED ]] || return 0
+ patch -p1 -R --force --no-backup-if-mismatch -r /dev/null &> /dev/null < "$COMBINED_PATCH" || true
+ unset PATCH_APPLIED
}
do_init() {
@@ -397,8 +361,6 @@ do_init() {
(( SHORT_CIRCUIT <= 1 )) && rm -rf "$TMP_DIR"
mkdir -p "$TMP_DIR"
- APPLIED_PATCHES=()
-
[[ -x "$PREPARE_PATCH" ]] || die "can't find klp-prepare-patch"
validate_config
@@ -806,10 +768,10 @@ if (( SHORT_CIRCUIT <= 1 )); then
fi
if (( SHORT_CIRCUIT <= 2 )); then
- apply_patch "$COMBINED_PATCH" "--silent"
+ apply_patch
status "Building patched kernel"
build_kernel "patched"
- revert_patches
+ revert_patch
status "Copying patched object files"
copy_patched_objects
fi