klp-build: Fix dependent patches, add selftests
Subsystems (2)
Song Liu
maintainerv2 cover klp-build: Fix dependent patches, add selftests
klp-build fails when a later patch in the series depends on an earlier one, because it fixes each patch against the original source on its own. Patch 1 fixes this by combining the patches into a single patch before adding the #line directives.
Patch 2 does this while validating the patches, before the original kernel build, so problems with the patches show up without waiting for a build.
Patch 3 moves the patch processing into a new script, klp-prepare-patch, which klp-build runs. It doesn't need a kernel build, so it can be tested on its own.
Patches 4-12 add a kselftest target, livepatch/test_klp_build. Each test builds a small source tree, runs klp-prepare-patch on some patches, and checks the patch it prints. The tests compile nothing, need no root, and take about a second. To run them:
make -C tools/testing/selftests/livepatch/test_klp_build tests
Changes in v2:
- Patch 1: describe combine_patches() without "refresh", rename fix_patches() to process_patches(), keep the combined patch in $COMBINED_PATCH rather than $PATCHES, change the status message to "Processing patch(es)", fold get_patch_{input,output}_files() into get_patch_files(), and keep tmp.patch in klp-tmp (Josh)
- New patch 2: process the patches while validating them, before the original kernel build (Josh)
- Replace the --patch-only option with a klp-prepare-patch script, which the selftests run directly (Josh). The intermediate patch without #line directives now lives in klp-prepare-patch's own temporary directory, which is removed on exit.
- Make the dependent patches test a case of test-basic.sh (Sashiko)
- Name the selftest helpers and subjects after klp-prepare-patch
v1: https://lore.kernel.org/all/[email protected]/
Song Liu (12):
klp-build: Fix dependent patches in a series
klp-build: Process the patches before building the original kernel
klp-build: Move patch processing to klp-prepare-patch selftests/livepatch: Add tests for klp-prepare-patch selftests/livepatch: Test klp-prepare-patch with dependent patches selftests/livepatch: Test klp-prepare-patch with independent patches selftests/livepatch: Test klp-prepare-patch with patches which cancel
out
selftests/livepatch: Test klp-prepare-patch with created and deleted
files
selftests/livepatch: Test where klp-prepare-patch adds #line
directives
selftests/livepatch: Test klp-prepare-patch with unsupported files selftests/livepatch: Test klp-prepare-patch with patches which don't
apply
selftests/livepatch: Test klp-prepare-patch with offset and fuzz
scripts/livepatch/Makefile | 3 +- scripts/livepatch/klp-build | 137 +-------- scripts/livepatch/klp-prepare-patch | 277 ++++++++++++++++++ tools/testing/selftests/Makefile | 1 + tools/testing/selftests/livepatch/README | 16 + .../livepatch/test_klp_build/Makefile | 26 ++ .../livepatch/test_klp_build/functions.sh | 149 ++++++++++ .../livepatch/test_klp_build/run-tests.sh | 68 +++++ .../livepatch/test_klp_build/test-basic.sh | 93 ++++++ .../test_klp_build/test-cancelling-patches.sh | 102 +++++++ .../test-independent-patches.sh | 124 ++++++++ .../test_klp_build/test-line-directives.sh | 175 +++++++++++ .../test-new-and-deleted-files.sh | 94 ++++++ .../test_klp_build/test-offset-and-fuzz.sh | 120 ++++++++ .../test_klp_build/test-patch-not-applying.sh | 73 +++++ .../test_klp_build/test-unsupported-files.sh | 30 ++ 16 files changed, 1366 insertions(+), 122 deletions(-) create mode 100755 scripts/livepatch/klp-prepare-patch create mode 100644 tools/testing/selftests/livepatch/test_klp_build/Makefile create mode 100644 tools/testing/selftests/livepatch/test_klp_build/functions.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/run-tests.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-basic.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-independent-patches.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-line-directives.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-new-and-deleted-files.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-offset-and-fuzz.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-patch-not-applying.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-unsupported-files.sh
Signature · 1 line
2.53.0-Meta
Song Liu
maintainerPATCH v2 1/12 klp-build: Fix dependent patches in a series
fix_patches() refreshes each patch on its own against the original
source: it applies the patch, copies the patched files, and reverts it. If a later patch in the series depends on an earlier one, it doesn't apply to the original source, and klp-build fails even though validate_patches(), which applies the patches in order, accepted the series:
$ ./scripts/livepatch/klp-build 0001-dummy.patch 0002-Revert-dummy.patch Validating patch(es) Building original kernel Copying original object files Fixing patch(es) Reversed (or previously applied) patch detected! Assume -R? [n] Apply anyway? [n] 1 out of 1 hunk ignored error: klp-build: .../klp-tmp/tmp.patch did not apply
Refreshing each patch on top of the previous ones wouldn't work either. fix-patch-lines adds #line directives to each fixed patch, so the next patch's context wouldn't match. The #line numbers in later patches would also refer to the intermediate source rather than the original source, which defeats the purpose of preserving __LINE__.
Instead, apply all the patches in order and diff the result against the original source to get a single combined patch, then run fix-patch-lines on that. If the patches have no net effect, fail with "no changes detected" instead of building the patched kernel for nothing.
The combined patch gets its own variable, $COMBINED_PATCH, instead of replacing the patches in $PATCHES, and fix_patches() becomes process_patches(), as it no longer fixes the patches it is given. The intermediate patch without #line directives is left in klp-tmp for debugging. get_patch_files() is now the only user of get_patch_input_files() and get_patch_output_files(), so fold them into it.
--- scripts/livepatch/klp-build | 121 +++++++++++++++++------------------- 1 file changed, 57 insertions(+), 64 deletions(-)
scripts/livepatch/klp-build +57 -64
@@ -47,6 +47,7 @@ KMOD_DIR="$TMP_DIR/5-kmod" STASH_DIR="$TMP_DIR/stash" TIMESTAMP="$TMP_DIR/timestamp" PATCH_TMP_DIR="$TMP_DIR/tmp" +COMBINED_PATCH="$TMP_DIR/combined.patch" KLP_DIFF_LOG="$DIFF_DIR/diff.log"
@@ -328,33 +329,16 @@ set_kernelversion() { sed -i "2i echo $kernelrelease; exit 0" scripts/setlocalversion } -get_patch_input_files() { - local patch="$1" - - grep0 -E '^--- ' "$patch" \ - | grep0 -v -e '/dev/null' -e '1969-12-31' -e '1970-01-01' \ - | gawk '{print $2}' \ - | sed 's|^[^/]*/||' \ - | sort -u -} - -get_patch_output_files() { +get_patch_files() { 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}' \ | sed 's|^[^/]*/||' \ | sort -u } -get_patch_files() { - local patch="$1" - - { get_patch_input_files "$patch"; get_patch_output_files "$patch"; } \ - | sort -u -} - check_unsupported_patches() { local patch
@@ -467,60 +451,69 @@ do_init() { set_kernelversion } -# Refresh the patch hunk headers, specifically the line numbers and counts. -refresh_patch() { - local patch="$1" +# Combine the patches into a single patch against the original source. The +# patches may depend on each other, so they can't be diffed individually. +combine_patches() { + local outfile="$1" local tmpdir="$PATCH_TMP_DIR" - local input_files=() - local output_files=() + local files=() + local patch + local file rm -rf "$tmpdir" - mkdir -p "$tmpdir/a" - mkdir -p "$tmpdir/b" + mkdir -p "$tmpdir" - # Get all source files affected by the patch - get_patch_input_files "$patch" | mapfile -t input_files - get_patch_output_files "$patch" | mapfile -t output_files + # Get all source files affected by the patches + for patch in "${PATCHES[@]}"; do + get_patch_files "$patch" + done | sort -u | mapfile -t files - # Copy orig source files to 'a' - echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" + # Copy orig source files to $tmpdir + for file in "${files[@]}"; do + if [[ -e "$file" ]]; then + cp --parents "$file" "$tmpdir" + fi + done - # Copy patched source files to 'b' - apply_patch "$patch" "--silent" - echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" - revert_patch "$patch" + # 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" + for file in "${files[@]}"; do + local old="$tmpdir/$file" + local new="$file" + local old_label="a/$file" + local new_label="b/$file" + local status=0 + + if [[ ! -e "$old" ]]; then + old=/dev/null + old_label=/dev/null + fi + if [[ ! -e "$new" ]]; then + new=/dev/null + new_label=/dev/null + fi - # Diff 'a' and 'b' to make a clean patch - ( cd "$tmpdir" && diff -Nupr a b > "$patch" ) || true + diff -up --label "$old_label" --label "$new_label" "$old" "$new" \ + >> "$outfile" || status=$? + if (( status > 1 )); then + die "diff failed for $file" + fi + done + revert_patches } -# Copy the patches to a temporary directory, fix their lines so as not to +# 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, and update $PATCHES to point to the fixed patches. -fix_patches() { - local idx - local i - - rm -f "$TMP_DIR"/*.patch - - idx=0001 - for i in "${!PATCHES[@]}"; do - local old_patch="${PATCHES[$i]}" - local tmp_patch="$TMP_DIR/tmp.patch" - local patch="${PATCHES[$i]}" - local new_patch +# file. +process_patches() { + local tmp_patch="$TMP_DIR/tmp.patch" - new_patch="$TMP_DIR/$idx-fixed-$(basename "$patch")" + combine_patches "$tmp_patch" + [[ ! -s "$tmp_patch" ]] && die "no changes detected" - cp -f "$old_patch" "$tmp_patch" - refresh_patch "$tmp_patch" - "$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch" - - PATCHES[i]="$new_patch" - - rm -f "$tmp_patch" - idx=$(printf "%04d" $(( 10#$idx + 1 ))) - done + "$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$COMBINED_PATCH" } clean_kernel() {
@@ -909,9 +902,9 @@ if (( SHORT_CIRCUIT <= 1 )); then fi if (( SHORT_CIRCUIT <= 2 )); then - status "Fixing patch(es)" - fix_patches - apply_patches "--silent" + status "Processing patch(es)" + process_patches + apply_patch "$COMBINED_PATCH" "--silent" status "Building patched kernel" build_kernel "patched" revert_patches
Signature · 1 line
2.53.0-Meta
Song Liu
maintainerPATCH v2 2/12 klp-build: Process the patches before building the original kernel
In reply to Song Liuklp-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.
--- scripts/livepatch/klp-build | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-)
scripts/livepatch/klp-build +8 -16
@@ -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"
Signature · 1 line
2.53.0-Meta
Song Liu
maintainerPATCH v2 3/12 klp-build: Move patch processing to klp-prepare-patch
In reply to Song Liuklp-build's patch processing doesn't need a kernel build, but the only way to run it is through klp-build, which builds the kernel twice. That makes it hard to test.
Move it into a new script, klp-prepare-patch, so that tests can run it directly. It validates the patches, combines them into a single patch against the source tree in the current directory, adds #line directives and prints the result. klp-build runs it and builds the patched kernel with its output.
klp-prepare-patch doesn't need a configured kernel tree, and it uses its own temporary directory, so it leaves klp-tmp alone. The intermediate patch without #line directives, which klp-build left in klp-tmp, now goes away with that directory.
klp-build still applies and reverts the combined patch, so a little of the patch handling is duplicated between the two scripts.
--- scripts/livepatch/Makefile | 3 +- scripts/livepatch/klp-build | 106 +---------- scripts/livepatch/klp-prepare-patch | 277 ++++++++++++++++++++++++++++ 3 files changed, 287 insertions(+), 99 deletions(-) create mode 100755 scripts/livepatch/klp-prepare-patch
scripts/livepatch/Makefile +2 -1
@@ -4,7 +4,8 @@ SHELLCHECK := $(shell which shellcheck 2> /dev/null) SRCS := \ - klp-build + klp-build \ + klp-prepare-patch .DEFAULT_GOAL := help .PHONY: help
scripts/livepatch/klp-build +8 -98
148 lines not on this page · raw message
scripts/livepatch/klp-prepare-patch new file +277 -0
277 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Josh Poimboeuf
maintainerRe: [PATCH v2 03/12] klp-build: Move patch processing to klp-prepare-patch
Review
Quoted 11 lines · Song Liu
(quoted diff) +0
-8-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:
scripts/livepatch/klp-build +9 -47
@@ -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
Song Liu
maintainerPATCH v2 4/12 selftests/livepatch: Add tests for klp-prepare-patch
In reply to Song LiuBefore building the patched kernel, klp-build runs klp-prepare-patch to combine the patches it is given into a single patch and add #line directives to it. Mistakes there break the build, or make klp-build include functions which didn't change.
Add a kselftest target, livepatch/test_klp_build, which runs klp-prepare-patch on patches to a small source tree and checks the patch it prints. Nothing is compiled, so the tests don't need a configured kernel, root, or the livepatch test modules.
Each test creates its source files and patches from heredocs, so it is self-contained. functions.sh provides:
- setup_tree() creates a scratch source tree and a patch directory
- add_file() and add_patch() fill them
- run_klp_prepare_patch() runs klp-prepare-patch from the top of the tree and checks that it left the tree as it found it
- expect_patch() checks that klp-prepare-patch succeeded without warnings and printed the expected patch. Only what the patch changes is compared: trailing whitespace is ignored, so blank context lines can be written as empty lines, and so are "diff" lines and timestamps, which depend on how the patch was generated.
Installed tests don't have the source tree, so klp-prepare-patch and fix-patch-lines are installed next to them. The tests skip if gawk, recountdiff or GNU patch is missing.
kselftest prints several lines for every test. For running the tests by hand, add run-tests.sh and a "tests" make target, which print one line per test case, the details of any failure, and the totals.
Start with a test of a single patch, which gets a #line directive after the lines it adds.
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/livepatch/README | 16 +++ .../livepatch/test_klp_build/Makefile | 19 +++ .../livepatch/test_klp_build/functions.sh | 135 ++++++++++++++++++ .../livepatch/test_klp_build/run-tests.sh | 68 +++++++++ .../livepatch/test_klp_build/test-basic.sh | 57 ++++++++ 6 files changed, 296 insertions(+) create mode 100644 tools/testing/selftests/livepatch/test_klp_build/Makefile create mode 100644 tools/testing/selftests/livepatch/test_klp_build/functions.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/run-tests.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-basic.sh
tools/testing/selftests/Makefile +1 -0
@@ -64,6 +64,7 @@ TARGETS += kvm TARGETS += landlock TARGETS += lib TARGETS += livepatch +TARGETS += livepatch/test_klp_build TARGETS += liveupdate TARGETS += lkdtm TARGETS += lsm
tools/testing/selftests/livepatch/README +16 -0
@@ -44,6 +44,22 @@ or % make kselftest TARGETS=livepatch +klp-build tests +--------------- + +The test_klp_build directory holds tests for klp-prepare-patch, which +scripts/livepatch/klp-build uses to prepare the patches it is given. They +don't build a kernel or load any modules, so they don't need root, and they +are a kselftest target of their own: + + % make -C tools/testing/selftests TARGETS=livepatch/test_klp_build run_tests + +For one line of output per test case, which is easier to read when running +them by hand: + + % make -C tools/testing/selftests/livepatch/test_klp_build tests + + Adding tests ------------
tools/testing/selftests/livepatch/test_klp_build/Makefile new file +19 -0
@@ -0,0 +1,19 @@ +# SPDX-License-Identifier: GPL-2.0 + +TEST_PROGS := \ + test-basic.sh + +TEST_PROGS_EXTENDED := functions.sh + +# Installed tests run without the source tree, so install the scripts too +TEST_FILES := \ + ../../../../../scripts/livepatch/klp-prepare-patch \ + ../../../../../scripts/livepatch/fix-patch-lines + +include ../../lib.mk + +# Run the tests by hand, with one line of output per test case +tests: + @./run-tests.sh $(TEST_PROGS) + +.PHONY: tests
tools/testing/selftests/livepatch/test_klp_build/functions.sh new file +135 -0
135 lines not on this page · raw message
tools/testing/selftests/livepatch/test_klp_build/run-tests.sh new file +68 -0
68 lines not on this page · raw message
tools/testing/selftests/livepatch/test_klp_build/test-basic.sh new file +57 -0
57 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Song Liu
maintainerPATCH v2 5/12 selftests/livepatch: Test klp-prepare-patch with dependent patches
In reply to Song LiuAdd a test case where the second patch changes lines added by the first, so it only applies on top of it. klp-prepare-patch has to combine the two into a single patch against the original source, rather than fixing each one against the original source on its own.
The case uses the same source file and first patch as the single patch test, so add it to test-basic.sh.
--- .../livepatch/test_klp_build/test-basic.sh | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+)
tools/testing/selftests/livepatch/test_klp_build/test-basic.sh +36 -0
@@ -54,4 +54,40 @@ expect_patch <<'EOT' return x; EOT +# - a patch which changes lines added by an earlier patch doesn't apply to the +# original source on its own, but the combined patch does + +start_test "patch which depends on an earlier one" + +add_patch 0002-foo-again.patch <<'EOT' +--- a/drivers/foo/foo.c ++++ b/drivers/foo/foo.c +@@ -1,8 +1,8 @@ + int foo(int x) + { + if (!x) +- return 0; ++ return 1; + if (x < 0) + return -1; + return x; + } +EOT + +run_klp_prepare_patch 0001-foo.patch 0002-foo-again.patch + +expect_patch <<'EOT' +--- a/drivers/foo/foo.c ++++ b/drivers/foo/foo.c +@@ -1,5 +1,8 @@ + int foo(int x) + { ++ if (!x) ++ return 1; ++#line 3 + if (x < 0) + return -1; + return x; +EOT + exit 0
Signature · 1 line
2.53.0-Meta
Song Liu
maintainerPATCH v2 6/12 selftests/livepatch: Test klp-prepare-patch with independent patches
In reply to Song LiuAdd a test where two patches change different functions of the same file, and the second also changes a header. The patches are combined into one patch per file, and the #line directives use line numbers from the original file, even for the function the first patch moved down.
--- .../livepatch/test_klp_build/Makefile | 3 +- .../test-independent-patches.sh | 124 ++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-independent-patches.sh
tools/testing/selftests/livepatch/test_klp_build/Makefile +2 -1
@@ -1,7 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 TEST_PROGS := \ - test-basic.sh + test-basic.sh \ + test-independent-patches.sh TEST_PROGS_EXTENDED := functions.sh
tools/testing/selftests/livepatch/test_klp_build/test-independent-patches.sh new file +124 -0
124 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Song Liu
maintainerPATCH v2 7/12 selftests/livepatch: Test klp-prepare-patch with patches which cancel out
In reply to Song LiuAdd a test where a patch is followed by its revert. On their own the two leave nothing to build, so klp-prepare-patch fails with "no changes detected", and klp-build stops before building anything. With another patch after them, only that patch's change is left in the combined patch.
Add expect_error() to functions.sh to check for a failure with a given error message.
--- .../livepatch/test_klp_build/Makefile | 3 +- .../livepatch/test_klp_build/functions.sh | 14 +++ .../test_klp_build/test-cancelling-patches.sh | 102 ++++++++++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh
tools/testing/selftests/livepatch/test_klp_build/Makefile +2 -1
@@ -2,7 +2,8 @@ TEST_PROGS := \ test-basic.sh \ - test-independent-patches.sh + test-independent-patches.sh \ + test-cancelling-patches.sh TEST_PROGS_EXTENDED := functions.sh
tools/testing/selftests/livepatch/test_klp_build/functions.sh +14 -0
@@ -133,3 +133,17 @@ function expect_patch() { echo "ok" } + +# expect_error(msg) - check that klp-prepare-patch failed with an error message +# msg - the message, without the "error: klp-prepare-patch: " prefix +function expect_error() { + [[ $STATUS -ne 0 ]] || + not_ok "klp-prepare-patch succeeded unexpectedly:" "$WORK_DIR/stdout" + [[ -s "$WORK_DIR/stdout" ]] && + not_ok "klp-prepare-patch printed a patch:" "$WORK_DIR/stdout" + + grep -qxF "error: klp-prepare-patch: $1" "$WORK_DIR/stderr" || + not_ok "klp-prepare-patch didn't fail with \"$1\":" "$WORK_DIR/stderr" + + echo "ok" +}
tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh new file +102 -0
102 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Song Liu
maintainerPATCH v2 8/12 selftests/livepatch: Test klp-prepare-patch with created and deleted files
In reply to Song LiuAdd a test of patches which create and delete files. Both are diffed against /dev/null, and the created file gets no #line directive, as it has no original line numbers to preserve. A file created by one patch and changed by the next is a new file in the combined patch.
--- .../livepatch/test_klp_build/Makefile | 3 +- .../test-new-and-deleted-files.sh | 94 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-new-and-deleted-files.sh
tools/testing/selftests/livepatch/test_klp_build/Makefile +2 -1
@@ -3,7 +3,8 @@ TEST_PROGS := \ test-basic.sh \ test-independent-patches.sh \ - test-cancelling-patches.sh + test-cancelling-patches.sh \ + test-new-and-deleted-files.sh TEST_PROGS_EXTENDED := functions.sh
tools/testing/selftests/livepatch/test_klp_build/test-new-and-deleted-files.sh new file +94 -0
94 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Song Liu
maintainerPATCH v2 9/12 selftests/livepatch: Test where klp-prepare-patch adds #line directives
In reply to Song LiuAdd a test of where #line directives go:
- each group of changed lines in a hunk gets its own directive
- after removed lines, the directive skips over them
- files other than C sources and headers get none
- lines added at the end of a file get none, as nothing after them needs renumbering
--- .../livepatch/test_klp_build/Makefile | 3 +- .../test_klp_build/test-line-directives.sh | 175 ++++++++++++++++++ 2 files changed, 177 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-line-directives.sh
tools/testing/selftests/livepatch/test_klp_build/Makefile +2 -1
@@ -4,7 +4,8 @@ TEST_PROGS := \ test-basic.sh \ test-independent-patches.sh \ test-cancelling-patches.sh \ - test-new-and-deleted-files.sh + test-new-and-deleted-files.sh \ + test-line-directives.sh TEST_PROGS_EXTENDED := functions.sh
tools/testing/selftests/livepatch/test_klp_build/test-line-directives.sh new file +175 -0
175 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Song Liu
maintainerPATCH v2 10/12 selftests/livepatch: Test klp-prepare-patch with unsupported files
In reply to Song LiuAdd a test that klp-prepare-patch rejects patches to lib/, the vDSO, real mode code and assembly files, which klp-build can't build a livepatch for.
--- .../livepatch/test_klp_build/Makefile | 3 +- .../test_klp_build/test-unsupported-files.sh | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-unsupported-files.sh
tools/testing/selftests/livepatch/test_klp_build/Makefile +2 -1
@@ -5,7 +5,8 @@ TEST_PROGS := \ test-independent-patches.sh \ test-cancelling-patches.sh \ test-new-and-deleted-files.sh \ - test-line-directives.sh + test-line-directives.sh \ + test-unsupported-files.sh TEST_PROGS_EXTENDED := functions.sh
tools/testing/selftests/livepatch/test_klp_build/test-unsupported-files.sh new file +30 -0
@@ -0,0 +1,30 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +. "$(dirname "$0")/functions.sh" + +setup_tree + +# - patches to lib/, the vDSO, real mode code and assembly are rejected + +for file in lib/string.c \ + arch/x86/entry/vdso/vma.c \ + arch/x86/realmode/rm/video-mode.c \ + arch/x86/kernel/head_64.S; do + + start_test "patch to $file" + + add_patch unsupported.patch <<EOT +--- a/$file ++++ b/$file +@@ -1 +1 @@ +-int x; ++int y; +EOT + + run_klp_prepare_patch unsupported.patch + + expect_error "$PATCH_DIR/unsupported.patch: unsupported patch to $file" +done + +exit 0
Signature · 1 line
2.53.0-Meta
Song Liu
maintainerPATCH v2 11/12 selftests/livepatch: Test klp-prepare-patch with patches which don't apply
In reply to Song LiuAdd a test that klp-prepare-patch rejects a patch which doesn't match the source, and that when the second patch of a series doesn't apply, the first one is reverted, leaving the source tree as it was.
--- .../livepatch/test_klp_build/Makefile | 3 +- .../test_klp_build/test-patch-not-applying.sh | 73 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-patch-not-applying.sh
tools/testing/selftests/livepatch/test_klp_build/Makefile +2 -1
@@ -6,7 +6,8 @@ TEST_PROGS := \ test-cancelling-patches.sh \ test-new-and-deleted-files.sh \ test-line-directives.sh \ - test-unsupported-files.sh + test-unsupported-files.sh \ + test-patch-not-applying.sh TEST_PROGS_EXTENDED := functions.sh
tools/testing/selftests/livepatch/test_klp_build/test-patch-not-applying.sh new file +73 -0
73 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Song Liu
maintainerPATCH v2 12/12 selftests/livepatch: Test klp-prepare-patch with offset and fuzz
In reply to Song Liuklp-prepare-patch accepts patches which apply with an offset or fuzz, and warns about them. Add a test that both are accepted with a warning, and that the combined patch has the line numbers and context of the real source, so the #line directives refer to the right lines.
--- .../livepatch/test_klp_build/Makefile | 3 +- .../test_klp_build/test-offset-and-fuzz.sh | 120 ++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-offset-and-fuzz.sh
tools/testing/selftests/livepatch/test_klp_build/Makefile +2 -1
@@ -7,7 +7,8 @@ TEST_PROGS := \ test-new-and-deleted-files.sh \ test-line-directives.sh \ test-unsupported-files.sh \ - test-patch-not-applying.sh + test-patch-not-applying.sh \ + test-offset-and-fuzz.sh TEST_PROGS_EXTENDED := functions.sh
tools/testing/selftests/livepatch/test_klp_build/test-offset-and-fuzz.sh new file +120 -0
120 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Petr Mladek
maintainerQuoted 26 lines · Song Liu
klp-build fails when a later patch in the series depends on an earlier one, because it fixes each patch against the original source on its own. Patch 1 fixes this by combining the patches into a single patch before adding the #line directives.
scripts/livepatch/Makefile | 3 +- scripts/livepatch/klp-build | 137 +-------- scripts/livepatch/klp-prepare-patch | 277 ++++++++++++++++++ tools/testing/selftests/Makefile | 1 + tools/testing/selftests/livepatch/README | 16 + .../livepatch/test_klp_build/Makefile | 26 ++ .../livepatch/test_klp_build/functions.sh | 149 ++++++++++ .../livepatch/test_klp_build/run-tests.sh | 68 +++++ .../livepatch/test_klp_build/test-basic.sh | 93 ++++++ .../test_klp_build/test-cancelling-patches.sh | 102 +++++++ .../test-independent-patches.sh | 124 ++++++++ .../test_klp_build/test-line-directives.sh | 175 +++++++++++ .../test-new-and-deleted-files.sh | 94 ++++++ .../test_klp_build/test-offset-and-fuzz.sh | 120 ++++++++ .../test_klp_build/test-patch-not-applying.sh | 73 +++++ .../test_klp_build/test-unsupported-files.sh | 30 ++ 16 files changed, 1366 insertions(+), 122 deletions(-) create mode 100755 scripts/livepatch/klp-prepare-patch create mode 100644 tools/testing/selftests/livepatch/test_klp_build/Makefile create mode 100644 tools/testing/selftests/livepatch/test_klp_build/functions.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/run-tests.sh
Quoted 8 lines
create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-basic.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-independent-patches.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-line-directives.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-new-and-deleted-files.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-offset-and-fuzz.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-patch-not-applying.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-unsupported-files.sh
I am sorry, I am overloaded and do not have time for a proper review.
I just see that it adds a lot of new files. And I wonder how much it shares with the livepatch selftests. Would it make sense to put it into a separate directory, e.g. tools/testing/selftests/klp-build ?
Best Regards,
Petr
Song Liu
maintainerQuoted 1 line · Petr Mladek
[...]
Quoted 13 lines
Quoted 4 lines
create mode 100755 scripts/livepatch/klp-prepare-patch create mode 100644 tools/testing/selftests/livepatch/test_klp_build/Makefile create mode 100644 tools/testing/selftests/livepatch/test_klp_build/functions.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/run-tests.shQuoted 8 lines
create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-basic.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-independent-patches.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-line-directives.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-new-and-deleted-files.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-offset-and-fuzz.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-patch-not-applying.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-unsupported-files.sh
Quoted 1 line
I am sorry, I am overloaded and do not have time for a proper review.
No worries.
Quoted 3 lines
I just see that it adds a lot of new files. And I wonder how much it shares with the livepatch selftests. Would it make sense to put it into a separate directory, e.g.
tools/testing/selftests/klp-build?
This set of tests is mostly independent to current livepatch selftests. But in the longer term, as we add more end to end tests with klp-build, there will be more overlap between klp-build tests, and livepatch tests.
Thanks,
Song
Petr Mladek
maintainerQuoted 20 lines · Song Liu
Quoted 1 line · Petr Mladek
[...]
Quoted 13 lines
Quoted 4 lines
create mode 100755 scripts/livepatch/klp-prepare-patch create mode 100644 tools/testing/selftests/livepatch/test_klp_build/Makefile create mode 100644 tools/testing/selftests/livepatch/test_klp_build/functions.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/run-tests.shQuoted 8 lines
create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-basic.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-independent-patches.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-line-directives.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-new-and-deleted-files.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-offset-and-fuzz.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-patch-not-applying.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-unsupported-files.shQuoted 1 line
I am sorry, I am overloaded and do not have time for a proper review.
No worries.
Quoted 7 lines
Quoted 3 lines
I just see that it adds a lot of new files. And I wonder how much it shares with the livepatch selftests. Would it make sense to put it into a separate directory, e.g.
tools/testing/selftests/klp-build?This set of tests is mostly independent to current livepatch selftests. But in the longer term, as we add more end to end tests with klp-build, there will be more overlap between klp-build tests, and livepatch tests.
OK, fair enough. It was just an idea based on the git log stats of this patchset.
Best Regards,
Petr
Quoted 5 lines · Song Liu
klp-build fails when a later patch in the series depends on an earlier one, because it fixes each patch against the original source on its own. Patch 1 fixes this by combining the patches into a single patch before adding the #line directives.
I'm curious, what is the advantage of using separate .patch files? Since patches can require changes/conflict resolutions for older kernels, we commit all patches into per-target-kernel branches and generate the .patch file on the fly at build time.
Quoted 9 lines
Patch 2 does this while validating the patches, before the original kernel build, so problems with the patches show up without waiting for a build.
Patch 3 moves the patch processing into a new script, klp-prepare-patch, which klp-build runs. It doesn't need a kernel build, so it can be tested on its own.
Quoted 6 lines
Patches 4-12 add a kselftest target, livepatch/test_klp_build. Each test builds a small source tree, runs klp-prepare-patch on some patches, and checks the patch it prints. The tests compile nothing, need no root, and take about a second. To run them:
make -C
tools/testing/selftests/livepatch/test_klp_buildtests
Ran these tests and they work on my end (applied on v7.4-rc1).
Quoted 70 lines
Changes in v2:
- Patch 1: describecombine_patches()without "refresh", renamefix_patches()toprocess_patches(), keep the combined patch in $COMBINED_PATCH rather than $PATCHES, change the status message to "Processing patch(es)", fold get_patch_{input,output}_files() intoget_patch_files(), and keep tmp.patch in klp-tmp (Josh)
- New patch 2: process the patches while validating them, before the original kernel build (Josh)
- Replace the --patch-only option with a klp-prepare-patch script, which the selftests run directly (Josh). The intermediate patch without #line directives now lives in klp-prepare-patch's own temporary directory, which is removed on exit.
- Make the dependent patches test a case oftest-basic.sh(Sashiko)
- Name the selftest helpers and subjects after klp-prepare-patchv1: https://lore.kernel.org/all/[email protected]/
Song Liu (12):
klp-build: Fix dependent patches in a series
klp-build: Process the patches before building the original kernel
klp-build: Move patch processing to klp-prepare-patch selftests/livepatch: Add tests for klp-prepare-patch selftests/livepatch: Test klp-prepare-patch with dependent patches selftests/livepatch: Test klp-prepare-patch with independent patches selftests/livepatch: Test klp-prepare-patch with patches which cancel
out
selftests/livepatch: Test klp-prepare-patch with created and deleted
files
selftests/livepatch: Test where klp-prepare-patch adds #line
directives
selftests/livepatch: Test klp-prepare-patch with unsupported files selftests/livepatch: Test klp-prepare-patch with patches which don't
apply
selftests/livepatch: Test klp-prepare-patch with offset and fuzzscripts/livepatch/Makefile | 3 +- scripts/livepatch/klp-build | 137 +-------- scripts/livepatch/klp-prepare-patch | 277 ++++++++++++++++++ tools/testing/selftests/Makefile | 1 + tools/testing/selftests/livepatch/README | 16 + .../livepatch/test_klp_build/Makefile | 26 ++ .../livepatch/test_klp_build/functions.sh | 149 ++++++++++ .../livepatch/test_klp_build/run-tests.sh | 68 +++++ .../livepatch/test_klp_build/test-basic.sh | 93 ++++++ .../test_klp_build/test-cancelling-patches.sh | 102 +++++++ .../test-independent-patches.sh | 124 ++++++++ .../test_klp_build/test-line-directives.sh | 175 +++++++++++ .../test-new-and-deleted-files.sh | 94 ++++++ .../test_klp_build/test-offset-and-fuzz.sh | 120 ++++++++ .../test_klp_build/test-patch-not-applying.sh | 73 +++++ .../test_klp_build/test-unsupported-files.sh | 30 ++ 16 files changed, 1366 insertions(+), 122 deletions(-) create mode 100755 scripts/livepatch/klp-prepare-patch create mode 100644 tools/testing/selftests/livepatch/test_klp_build/Makefile create mode 100644 tools/testing/selftests/livepatch/test_klp_build/functions.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/run-tests.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-basic.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-independent-patches.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-line-directives.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-new-and-deleted-files.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-offset-and-fuzz.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-patch-not-applying.sh create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-unsupported-files.sh--
2.53.0-Meta