livepatch/klp-build: switch to GNU patch and recountdiff

The klp-build script is currently very strict with input patches,
requiring them to apply cleanly via `git apply --recount`.  This
prevents the use of patches with minor contextual fuzz relative to the
target kernel sources.

To allow users to reuse a patch across similar kernel streams, switch to
using GNU patch and patchutils for intermediate patch manipulation.
Update the logic for applying, reverting, and regenerating patches:

- Use 'patch -p1' for better handling of context fuzz.
- Use 'recountdiff' to update line counts after FIX_PATCH_LINES.
- Drop git_refresh() and related git-specific logic.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
Acked-by: Song Liu <song@kernel.org>
Link: https://patch.msgid.link/20260310203751.1479229-5-joe.lawrence@redhat.com
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
This commit is contained in:
Joe Lawrence 2026-03-10 16:37:43 -04:00 committed by Josh Poimboeuf
parent 757bd10ff0
commit d36a7343f4

View File

@ -95,7 +95,7 @@ restore_files() {
cleanup() { cleanup() {
set +o nounset set +o nounset
revert_patches "--recount" revert_patches
restore_files restore_files
[[ "$KEEP_TMP" -eq 0 ]] && rm -rf "$TMP_DIR" [[ "$KEEP_TMP" -eq 0 ]] && rm -rf "$TMP_DIR"
return 0 return 0
@ -282,7 +282,7 @@ set_module_name() {
} }
# Hardcode the value printed by the localversion script to prevent patch # Hardcode the value printed by the localversion script to prevent patch
# application from appending it with '+' due to a dirty git working tree. # application from appending it with '+' due to a dirty working tree.
set_kernelversion() { set_kernelversion() {
local file="$SRC/scripts/setlocalversion" local file="$SRC/scripts/setlocalversion"
local localversion local localversion
@ -300,8 +300,8 @@ get_patch_input_files() {
local patch="$1" local patch="$1"
grep0 -E '^--- ' "$patch" \ grep0 -E '^--- ' "$patch" \
| grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \
| gawk '{print $2}' \ | gawk '{print $2}' \
| grep0 -v '^/dev/null$' \
| sed 's|^[^/]*/||' \ | sed 's|^[^/]*/||' \
| sort -u | sort -u
} }
@ -310,8 +310,8 @@ get_patch_output_files() {
local patch="$1" local patch="$1"
grep0 -E '^\+\+\+ ' "$patch" \ grep0 -E '^\+\+\+ ' "$patch" \
| grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \
| gawk '{print $2}' \ | gawk '{print $2}' \
| grep0 -v '^/dev/null$' \
| sed 's|^[^/]*/||' \ | sed 's|^[^/]*/||' \
| sort -u | sort -u
} }
@ -323,21 +323,6 @@ get_patch_files() {
| sort -u | sort -u
} }
# Make sure git re-stats the changed files
git_refresh() {
local patch="$1"
local files=()
[[ ! -e "$SRC/.git" ]] && return
get_patch_input_files "$patch" | mapfile -t files
(
cd "$SRC"
git update-index -q --refresh -- "${files[@]}"
)
}
check_unsupported_patches() { check_unsupported_patches() {
local patch local patch
@ -358,36 +343,19 @@ check_unsupported_patches() {
apply_patch() { apply_patch() {
local patch="$1" local patch="$1"
shift
local extra_args=("$@")
[[ ! -f "$patch" ]] && die "$patch doesn't exist" [[ ! -f "$patch" ]] && die "$patch doesn't exist"
patch -d "$SRC" -p1 --dry-run --silent --no-backup-if-mismatch -r /dev/null < "$patch"
( patch -d "$SRC" -p1 --silent --no-backup-if-mismatch -r /dev/null < "$patch"
cd "$SRC"
# The sed strips the version signature from 'git format-patch',
# otherwise 'git apply --recount' warns.
sed -n '/^-- /q;p' "$patch" |
git apply "${extra_args[@]}"
)
APPLIED_PATCHES+=("$patch") APPLIED_PATCHES+=("$patch")
} }
revert_patch() { revert_patch() {
local patch="$1" local patch="$1"
shift
local extra_args=("$@")
local tmp=() local tmp=()
( patch -d "$SRC" -p1 -R --silent --no-backup-if-mismatch -r /dev/null < "$patch"
cd "$SRC"
sed -n '/^-- /q;p' "$patch" |
git apply --reverse "${extra_args[@]}"
)
git_refresh "$patch"
for p in "${APPLIED_PATCHES[@]}"; do for p in "${APPLIED_PATCHES[@]}"; do
[[ "$p" == "$patch" ]] && continue [[ "$p" == "$patch" ]] && continue
@ -406,11 +374,10 @@ apply_patches() {
} }
revert_patches() { revert_patches() {
local extra_args=("$@")
local patches=("${APPLIED_PATCHES[@]}") local patches=("${APPLIED_PATCHES[@]}")
for (( i=${#patches[@]}-1 ; i>=0 ; i-- )) ; do for (( i=${#patches[@]}-1 ; i>=0 ; i-- )) ; do
revert_patch "${patches[$i]}" "${extra_args[@]}" revert_patch "${patches[$i]}"
done done
APPLIED_PATCHES=() APPLIED_PATCHES=()
@ -434,6 +401,7 @@ do_init() {
APPLIED_PATCHES=() APPLIED_PATCHES=()
[[ -x "$FIX_PATCH_LINES" ]] || die "can't find fix-patch-lines" [[ -x "$FIX_PATCH_LINES" ]] || die "can't find fix-patch-lines"
command -v recountdiff &>/dev/null || die "recountdiff not found (install patchutils)"
validate_config validate_config
set_module_name set_module_name
@ -459,12 +427,12 @@ refresh_patch() {
( cd "$SRC" && echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" ) ( cd "$SRC" && echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" )
# Copy patched source files to 'b' # Copy patched source files to 'b'
apply_patch "$patch" --recount apply_patch "$patch"
( cd "$SRC" && echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" ) ( cd "$SRC" && echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" )
revert_patch "$patch" --recount revert_patch "$patch"
# Diff 'a' and 'b' to make a clean patch # Diff 'a' and 'b' to make a clean patch
( cd "$tmpdir" && git diff --no-index --no-prefix a b > "$patch" ) || true ( cd "$tmpdir" && diff -Nupr a b > "$patch" ) || true
} }
# Copy the patches to a temporary directory, fix their lines so as not to # Copy the patches to a temporary directory, fix their lines so as not to
@ -487,8 +455,7 @@ fix_patches() {
cp -f "$old_patch" "$tmp_patch" cp -f "$old_patch" "$tmp_patch"
refresh_patch "$tmp_patch" refresh_patch "$tmp_patch"
"$FIX_PATCH_LINES" "$tmp_patch" > "$new_patch" "$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch"
refresh_patch "$new_patch"
PATCHES[i]="$new_patch" PATCHES[i]="$new_patch"