Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Joe Lawrence and committed by
Josh Poimboeuf
d36a7343 757bd10f

+13 -46
+13 -46
scripts/livepatch/klp-build
··· 95 95 96 96 cleanup() { 97 97 set +o nounset 98 - revert_patches "--recount" 98 + revert_patches 99 99 restore_files 100 100 [[ "$KEEP_TMP" -eq 0 ]] && rm -rf "$TMP_DIR" 101 101 return 0 ··· 282 282 } 283 283 284 284 # Hardcode the value printed by the localversion script to prevent patch 285 - # application from appending it with '+' due to a dirty git working tree. 285 + # application from appending it with '+' due to a dirty working tree. 286 286 set_kernelversion() { 287 287 local file="$SRC/scripts/setlocalversion" 288 288 local localversion ··· 300 300 local patch="$1" 301 301 302 302 grep0 -E '^--- ' "$patch" \ 303 + | grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ 303 304 | gawk '{print $2}' \ 304 - | grep0 -v '^/dev/null$' \ 305 305 | sed 's|^[^/]*/||' \ 306 306 | sort -u 307 307 } ··· 310 310 local patch="$1" 311 311 312 312 grep0 -E '^\+\+\+ ' "$patch" \ 313 + | grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ 313 314 | gawk '{print $2}' \ 314 - | grep0 -v '^/dev/null$' \ 315 315 | sed 's|^[^/]*/||' \ 316 316 | sort -u 317 317 } ··· 321 321 322 322 { get_patch_input_files "$patch"; get_patch_output_files "$patch"; } \ 323 323 | sort -u 324 - } 325 - 326 - # Make sure git re-stats the changed files 327 - git_refresh() { 328 - local patch="$1" 329 - local files=() 330 - 331 - [[ ! -e "$SRC/.git" ]] && return 332 - 333 - get_patch_input_files "$patch" | mapfile -t files 334 - 335 - ( 336 - cd "$SRC" 337 - git update-index -q --refresh -- "${files[@]}" 338 - ) 339 324 } 340 325 341 326 check_unsupported_patches() { ··· 343 358 344 359 apply_patch() { 345 360 local patch="$1" 346 - shift 347 - local extra_args=("$@") 348 361 349 362 [[ ! -f "$patch" ]] && die "$patch doesn't exist" 350 - 351 - ( 352 - cd "$SRC" 353 - 354 - # The sed strips the version signature from 'git format-patch', 355 - # otherwise 'git apply --recount' warns. 356 - sed -n '/^-- /q;p' "$patch" | 357 - git apply "${extra_args[@]}" 358 - ) 363 + patch -d "$SRC" -p1 --dry-run --silent --no-backup-if-mismatch -r /dev/null < "$patch" 364 + patch -d "$SRC" -p1 --silent --no-backup-if-mismatch -r /dev/null < "$patch" 359 365 360 366 APPLIED_PATCHES+=("$patch") 361 367 } 362 368 363 369 revert_patch() { 364 370 local patch="$1" 365 - shift 366 - local extra_args=("$@") 367 371 local tmp=() 368 372 369 - ( 370 - cd "$SRC" 371 - 372 - sed -n '/^-- /q;p' "$patch" | 373 - git apply --reverse "${extra_args[@]}" 374 - ) 375 - git_refresh "$patch" 373 + patch -d "$SRC" -p1 -R --silent --no-backup-if-mismatch -r /dev/null < "$patch" 376 374 377 375 for p in "${APPLIED_PATCHES[@]}"; do 378 376 [[ "$p" == "$patch" ]] && continue ··· 374 406 } 375 407 376 408 revert_patches() { 377 - local extra_args=("$@") 378 409 local patches=("${APPLIED_PATCHES[@]}") 379 410 380 411 for (( i=${#patches[@]}-1 ; i>=0 ; i-- )) ; do 381 - revert_patch "${patches[$i]}" "${extra_args[@]}" 412 + revert_patch "${patches[$i]}" 382 413 done 383 414 384 415 APPLIED_PATCHES=() ··· 401 434 APPLIED_PATCHES=() 402 435 403 436 [[ -x "$FIX_PATCH_LINES" ]] || die "can't find fix-patch-lines" 437 + command -v recountdiff &>/dev/null || die "recountdiff not found (install patchutils)" 404 438 405 439 validate_config 406 440 set_module_name ··· 427 459 ( cd "$SRC" && echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" ) 428 460 429 461 # Copy patched source files to 'b' 430 - apply_patch "$patch" --recount 462 + apply_patch "$patch" 431 463 ( cd "$SRC" && echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" ) 432 - revert_patch "$patch" --recount 464 + revert_patch "$patch" 433 465 434 466 # Diff 'a' and 'b' to make a clean patch 435 - ( cd "$tmpdir" && git diff --no-index --no-prefix a b > "$patch" ) || true 467 + ( cd "$tmpdir" && diff -Nupr a b > "$patch" ) || true 436 468 } 437 469 438 470 # Copy the patches to a temporary directory, fix their lines so as not to ··· 455 487 456 488 cp -f "$old_patch" "$tmp_patch" 457 489 refresh_patch "$tmp_patch" 458 - "$FIX_PATCH_LINES" "$tmp_patch" > "$new_patch" 459 - refresh_patch "$new_patch" 490 + "$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch" 460 491 461 492 PATCHES[i]="$new_patch" 462 493