Skip to contentSkip to conversation

klp-build: Fix dependent patches, add selftests

PATCH Superseded Song Liu 3 participants 16 messages Oct 17 – Oct 28

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Song Liu

author maintainer
#0 lore raw

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.

So far, testing this step has needed a full kernel build. Patch 2 adds a --patch-only option, which validates and fixes the patches, prints the result, and exits without building.

Patches 3-11 use it to add a kselftest target, livepatch/test_klp_build. Each test builds a small source tree, runs "klp-build --patch-only" 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

Song Liu (11):
klp-build: Fix dependent patches in a series
klp-build: Add --patch-only option
selftests/livepatch: Add tests for klp-build's patch handling selftests/livepatch: Test klp-build with dependent patches selftests/livepatch: Test klp-build with patches to different
functions
selftests/livepatch: Test klp-build with patches which cancel out selftests/livepatch: Test klp-build with created and deleted files selftests/livepatch: Test where klp-build adds #line directives selftests/livepatch: Test klp-build's rejection of unsupported files selftests/livepatch: Test klp-build with patches which don't apply selftests/livepatch: Test klp-build with patches which need offset or
fuzz

 scripts/livepatch/klp-build                   | 121 ++++++++----
 tools/testing/selftests/Makefile              |   1 +
 tools/testing/selftests/livepatch/README      |  16 ++
 .../livepatch/test_klp_build/Makefile         |  27 +++
 .../livepatch/test_klp_build/functions.sh     | 150 +++++++++++++++
 .../livepatch/test_klp_build/run-tests.sh     |  68 +++++++
 .../livepatch/test_klp_build/test-basic.sh    |  57 ++++++
 .../test_klp_build/test-cancelling-patches.sh | 102 ++++++++++
 .../test_klp_build/test-dependent-patches.sh  |  72 +++++++
 .../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 +++
 15 files changed, 1189 insertions(+), 41 deletions(-)
 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-dependent-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
author maintainer
#1 lore raw

PATCH 1/11 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 early with "no changes detected" instead of building the patched kernel for nothing.

Fixes 24ebfcd65a87 ("livepatch/klp-build: Introduce klp-build script for generating livepatch modules")
Assisted-by Claude:claude-opus-5
---
 scripts/livepatch/klp-build | 93 +++++++++++++++++++++----------------
 1 file changed, 54 insertions(+), 39 deletions(-)
scripts/livepatch/klp-build +54 -39
index b52a8489d9f6..6cdc36e97c21 100755
@@ -467,60 +467,75 @@ 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, with
+# refreshed hunk headers.  The patches may depend on each other, so they can't
+# be refreshed 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
-# affect the __LINE__ macro for otherwise unchanged functions further down the
-# file, and update $PATCHES to point to the fixed patches.
+# Combine the patches into a single patch, 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 patch.
 fix_patches() {
-	local idx
-	local i
+	local tmp_patch="$TMP_DIR/tmp.patch"
+	local new_patch="$TMP_DIR/fixed.patch"
 
 	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
-
-		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"
+	"$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch"
+	PATCHES=("$new_patch")
 
-		PATCHES[i]="$new_patch"
-
-		rm -f "$tmp_patch"
-		idx=$(printf "%04d" $(( 10#$idx + 1 )))
-	done
+	rm -f "$tmp_patch"
 }
 
 clean_kernel() {
Signature · 1 line
2.53.0-Meta
maintainer
#2 lore raw

Re: [PATCH 01/11] klp-build: Fix dependent patches in a series

Quoted 7 lines · Song Liu

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:

Quoted 9 lines
  $ ./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

The patch fixing step should probably be done earlier, during the patch validating step, so any such errors get rooted out before the build. That would simplify things and unify the apply/revert cycles. (can be a separate followup patch)

scripts/livepatch/klp-build do_init() { · line 472

Quoted 11 lines

+++ b/scripts/livepatch/klp-build

scripts/livepatch/klp-build +3 -3
@@ -467,60 +467,75 @@ 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, with
+# refreshed hunk headers.  The patches may depend on each other, so they can't
+# be refreshed individually.

It's probably no longer accurate to describe this as refreshing things since it's now creating a new patch?

Quoted 7 lines
scripts/livepatch/klp-build +4 -2
+combine_patches() {
+	local outfile="$1"
 	local tmpdir="$PATCH_TMP_DIR"
-	local input_files=()
-	local output_files=()
+	local files=()
+	local patch

scripts/livepatch/klp-build do_init() { · line 483

Quoted 10 lines
scripts/livepatch/klp-build +2 -5
+	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

Now that these helpers are only called by get_patch_files(), can they be folded into it and simplified with a single grep for '^(---|\+\+\+) '?

Quoted 45 lines
scripts/livepatch/klp-build +34 -8
+	# 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

scripts/livepatch/klp-build do_init() { · line 526

Quoted 10 lines
scripts/livepatch/klp-build +4 -3
+	revert_patches
 }
 
-# Copy the patches to a temporary directory, fix their 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.
+# Combine the patches into a single patch, 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 patch.
 fix_patches() {

This no longer fixes the patches, probably a function rename is warranted?

Quoted 19 lines
scripts/livepatch/klp-build +4 -11
-	local idx
-	local i
+	local tmp_patch="$TMP_DIR/tmp.patch"
+	local new_patch="$TMP_DIR/fixed.patch"
 
 	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
-
-		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"

scripts/livepatch/klp-build do_init() { · line 523

Quoted 10 lines
scripts/livepatch/klp-build +2 -7
-		refresh_patch "$tmp_patch"
-		"$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch"
+	"$FIX_PATCH_LINES" "$tmp_patch" | recountdiff > "$new_patch"
+	PATCHES=("$new_patch")
 
-		PATCHES[i]="$new_patch"
-
-		rm -f "$tmp_patch"
-		idx=$(printf "%04d" $(( 10#$idx + 1 )))
-	done

It's confusing that from here on out, PATCHES becomes a single-element array (yet its users treat it like it might have multiple entries).

It should probably be a separate variable (COMBINED_PATCH?), with its users simplified. And e.g., the "Fixing patch(es)" status message can be changed. "Processing patch(es)"?

scripts/livepatch/klp-build do_init() { · line 538

Quoted 1 line
scripts/livepatch/klp-build +1 -0
+	rm -f "$tmp_patch"

I think this doesn't need to be removed here, it shows an intermediate state which could be useful for debugging.

Signature · 1 line
Josh
author maintainer
#3 lore raw

Re: [PATCH 01/11] klp-build: Fix dependent patches in a series

Hi Josh,

Thanks for the thorough review!

All these comments make sense. I will send v2 soon.

Song

Quoted 4 lines · Josh Poimboeuf
Quoted 2 lines · Song Liu

fix_patches() refreshes each patch on its own against the original
source: it applies the patch, copies the patched files, and reverts it.

author maintainer
#4 lore raw

PATCH 2/11 klp-build: Add --patch-only option

In reply to Song Liu

klp-build fixes the input patches before building the patched kernel:
it combines them into a single patch against the original source and adds #line directives, so that unchanged functions further down a patched file keep their __LINE__ values. This only happens after the original kernel has been built, so seeing the result, or testing this step, takes a full kernel build and --keep-tmp.

Add a --patch-only option which validates and fixes the patches, prints the fixed patch to stdout, and exits without building. It skips the .config checks and the kernel version setup, which only matter for building, so the tree it runs in doesn't need to be configured. It uses a private tmp dir rather than klp-tmp, which may hold a previous build that a later --short-circuit run wants to reuse.

Assisted-by Claude:claude-opus-5
---
 scripts/livepatch/klp-build | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
scripts/livepatch/klp-build +26 -2
index 6cdc36e97c21..295fad62d7b8 100755
@@ -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_ONLY SKIP_CLEANUP VERBOSE XTRACE
 
 REPLACE=1
 SHORT_CIRCUIT=0
@@ -137,6 +137,7 @@ Options:
 
 Advanced Options:
    -d, --debug			Show symbol/reloc cloning decisions
+       --patch-only		Print the fixed patch(es) and exit without building
    -S, --short-circuit=STEP	Start at build step (requires prior --keep-tmp)
 				   1|orig		Build original kernel (default)
 				   2|patched		Build patched kernel
@@ -160,7 +161,7 @@ process_args() {
 	local patch
 
 	short="hfj:o:vdS:T"
-	long="help,show-first-changed,jobs:,output:,no-replace,verbose,debug,short-circuit:,keep-tmp"
+	long="help,show-first-changed,jobs:,output:,no-replace,verbose,debug,patch-only,short-circuit:,keep-tmp"
 
 	args=$(getopt --options "$short" --longoptions "$long" -- "$@") || {
 		echo; usage; exit
@@ -202,6 +203,10 @@ process_args() {
 				keep_tmp=1
 				shift
 				;;
+			--patch-only)
+				PATCH_ONLY=1
+				shift
+				;;
 			-S | --short-circuit)
 				[[ ! -d "$TMP_DIR" ]] && die "--short-circuit requires preserved klp-tmp dir"
 				keep_tmp=1
@@ -238,6 +243,15 @@ process_args() {
 	KEEP_TMP="$keep_tmp"
 	PATCHES=("$@")
 
+	if [[ -v PATCH_ONLY ]]; then
+		(( SHORT_CIRCUIT > 0 )) && die "--patch-only can't be used with --short-circuit"
+
+		# Leave klp-tmp alone, it may hold a previous build
+		TMP_DIR="$(mktemp -d)"
+		PATCH_TMP_DIR="$TMP_DIR/tmp"
+		KEEP_TMP=0
+	fi
+
 	for patch in "${PATCHES[@]}"; do
 		[[ -f "$patch" ]] || die "$patch doesn't exist"
 	done
@@ -462,6 +476,9 @@ do_init() {
 	[[ -x "$FIX_PATCH_LINES" ]] || die "can't find fix-patch-lines"
 	command -v recountdiff &>/dev/null || die "recountdiff not found (install patchutils)"
 
+	# The rest is only needed for building
+	[[ -v PATCH_ONLY ]] && return 0
+
 	validate_config
 	set_module_name
 	set_kernelversion
@@ -910,6 +927,13 @@ build_patch_module() {
 process_args "$@"
 do_init
 
+if [[ -v PATCH_ONLY ]]; then
+	validate_patches
+	fix_patches
+	cat "${PATCHES[@]}"
+	exit 0
+fi
+
 if (( SHORT_CIRCUIT <= 2 )); then
 	status "Validating patch(es)"
 	validate_patches
Signature · 1 line
2.53.0-Meta
maintainer
#5 lore raw

Re: [PATCH 02/11] klp-build: Add --patch-only option

Quoted 6 lines · Song Liu

klp-build fixes the input patches before building the patched kernel:
it combines them into a single patch against the original source and adds #line directives, so that unchanged functions further down a patched file keep their __LINE__ values. This only happens after the original kernel has been built, so seeing the result, or testing this step, takes a full kernel build and --keep-tmp.

Not sure how this last sentence is relevant, either way klp-build will run to completion (or failure)?

Also the above feels like a strawman, I think the real justification for this is the selftests?

Quoted 11 lines

+++ b/scripts/livepatch/klp-build

scripts/livepatch/klp-build +1 -1
@@ -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_ONLY SKIP_CLEANUP VERBOSE XTRACE
 
 REPLACE=1
 SHORT_CIRCUIT=0

scripts/livepatch/klp-build Options: · line 140

Quoted 4 lines
scripts/livepatch/klp-build +1 -0
@@ -137,6 +137,7 @@ Options:
 
 Advanced Options:
    -d, --debug			Show symbol/reloc cloning decisions
+       --patch-only		Print the fixed patch(es) and exit without building

This is basically bolting a test hook onto klp-build.

What if we move the patch validating/fixing into a self-contained klp-prepare-patch script? There might be a small amount of duplication for some of the patch handling but that would hopefully be minimal.

Then the selftests can run it directly and klp-build doesn't need the new option and the PATCH_ONLY special casing.

Signature · 1 line
Josh
author maintainer
#6 lore raw

PATCH 3/11 selftests/livepatch: Add tests for klp-build's patch handling

In reply to Song Liu

Before building the patched kernel, klp-build combines the patches it is given into a single patch and adds #line directives to it. Mistakes there break the build, or make klp-build include functions which didn't change, but testing this step has so far needed a full kernel build.

Add a kselftest target, livepatch/test_klp_build, which runs "klp-build --patch-only" on patches to a small source tree and checks the fixed 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 tree with klp-build linked into scripts/livepatch/, since klp-build runs from the top of its own tree
- add_file() and add_patch() fill the tree and the patch directory
- run_klp_build() runs klp-build and checks that it left the tree as it found it
- expect_patch() checks that klp-build 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-build 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.

Assisted-by Claude:claude-opus-5
---
 tools/testing/selftests/Makefile              |   1 +
 tools/testing/selftests/livepatch/README      |  16 ++
 .../livepatch/test_klp_build/Makefile         |  19 +++
 .../livepatch/test_klp_build/functions.sh     | 138 ++++++++++++++++++
 .../livepatch/test_klp_build/run-tests.sh     |  68 +++++++++
 .../livepatch/test_klp_build/test-basic.sh    |  57 ++++++++
 6 files changed, 299 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
index 2d960626750e..30d46cb71cbb 100644
@@ -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
index d2035dd64a2b..47b1a41b9771 100644
@@ -44,6 +44,22 @@ or
   % make kselftest TARGETS=livepatch
 
 
+klp-build tests
+---------------
+
+The test_klp_build directory holds tests for how scripts/livepatch/klp-build
+prepares 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
new file mode 100644
index 000000000000..6ebcecc828a1
@@ -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 klp-build too
+TEST_FILES := \
+	../../../../../scripts/livepatch/klp-build \
+	../../../../../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 +138 -0

138 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
author maintainer
#7 lore raw

PATCH 4/11 selftests/livepatch: Test klp-build with dependent patches

In reply to Song Liu

Add a test where the second patch changes lines added by the first, so it only applies on top of it. klp-build 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.

Assisted-by Claude:claude-opus-5
---
 .../livepatch/test_klp_build/Makefile         |  3 +-
 .../test_klp_build/test-dependent-patches.sh  | 72 +++++++++++++++++++
 2 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-dependent-patches.sh
tools/testing/selftests/livepatch/test_klp_build/Makefile +2 -1
index 6ebcecc828a1..405545c72303 100644
@@ -1,7 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
 
 TEST_PROGS := \
-	test-basic.sh
+	test-basic.sh \
+	test-dependent-patches.sh
 
 TEST_PROGS_EXTENDED := functions.sh
 
tools/testing/selftests/livepatch/test_klp_build/test-dependent-patches.sh new file +72 -0

72 lines not on this page · raw message

Signature · 1 line
2.53.0-Meta
#8 lore raw

Re: [PATCH 04/11] selftests/livepatch: Test klp-build with dependent patches

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] selftests/livepatch: test-dependent-patches.sh duplicates test-basic.sh setup --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] selftests/livepatch: test-dependent-patches.sh duplicates test-basic.sh setup --

commit fc2a73d39b4eb0e68d2bd9630878b28c5feeaab1
Author: Song Liu <[email protected]>

selftests/livepatch: Test klp-build with dependent patches

This commit adds a new selftest script, test-dependent-patches.sh, to test klp-build when a second patch modifies lines added by an earlier patch.

Quoted 35 lines
tools/testing/selftests/livepatch/test_klp_build/test-dependent-patches.sh new file +29 -0
new file mode 100755
index 0000000000000..75b957ac002c0
@@ -0,0 +1,72 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(dirname "$0")/functions.sh"
+
+setup_tree
+
+add_file drivers/foo/foo.c <<'EOT'
+int foo(int x)
+{
+	if (x < 0)
+		return -1;
+	return x;
+}
+
+int bar(void)
+{
+	return __LINE__;
+}
+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 0001-foo.patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c

tools/testing/selftests/livepatch/test_klp_build/test-dependent-patches.sh line 39

Quoted 10 lines
tools/testing/selftests/livepatch/test_klp_build/test-dependent-patches.sh new file +10 -0
+@@ -1,6 +1,8 @@
+ int foo(int x)
+ {
++	if (!x)
++		return 0;
+ 	if (x < 0)
+ 		return -1;
+ 	return x;
+ }
+EOT

[Severity: Low]
In tools/testing/selftests/livepatch/test_klp_build/test-basic.sh, the script already calls setup_tree, creates the identical drivers/foo/foo.c via add_file, and defines the identical 0001-foo.patch via add_patch.

Since run_klp_build() in functions.sh leaves $TREE unmodified between invocations and supports running multiple start_test cases sequentially in a single script, should this dependent-patch test case be added to test-basic.sh instead of duplicating the setup in a new standalone test-dependent-patches.sh file?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#9 lore raw

PATCH 5/11 selftests/livepatch: Test klp-build with patches to different functions

In reply to Song Liu

Add 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.

Assisted-by Claude:claude-opus-5
---
 .../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
index 405545c72303..536b27767435 100644
@@ -2,7 +2,8 @@
 
 TEST_PROGS := \
 	test-basic.sh \
-	test-dependent-patches.sh
+	test-dependent-patches.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
author maintainer
#10 lore raw

PATCH 6/11 selftests/livepatch: Test klp-build with patches which cancel out

In reply to Song Liu

Add a test where a patch is followed by its revert. On their own the two leave nothing to build, so klp-build fails with "no changes detected" 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.

Assisted-by Claude:claude-opus-5
---
 .../livepatch/test_klp_build/Makefile         |   3 +-
 .../livepatch/test_klp_build/functions.sh     |  12 +++
 .../test_klp_build/test-cancelling-patches.sh | 102 ++++++++++++++++++
 3 files changed, 116 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
index 536b27767435..b3191b5c1925 100644
@@ -3,7 +3,8 @@
 TEST_PROGS := \
 	test-basic.sh \
 	test-dependent-patches.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 +12 -0
index 4e9d4ccfe300..04538efbee5d 100644
@@ -136,3 +136,15 @@ function expect_patch() {
 
 	echo "ok"
 }
+
+# expect_error(msg) - check that klp-build failed with an error message
+#	msg - the message, without the "error: klp-build: " prefix
+function expect_error() {
+	[[ $STATUS -ne 0 ]] || not_ok "klp-build succeeded unexpectedly:" "$WORK_DIR/stdout"
+	[[ -s "$WORK_DIR/stdout" ]] && not_ok "klp-build printed a patch:" "$WORK_DIR/stdout"
+
+	grep -qxF "error: klp-build: $1" "$WORK_DIR/stderr" ||
+		not_ok "klp-build 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
author maintainer
#11 lore raw

PATCH 7/11 selftests/livepatch: Test klp-build with created and deleted files

In reply to Song Liu

Add 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.

Assisted-by Claude:claude-opus-5
---
 .../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
index b3191b5c1925..96144f99b1aa 100644
@@ -4,7 +4,8 @@ TEST_PROGS := \
 	test-basic.sh \
 	test-dependent-patches.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
author maintainer
#12 lore raw

PATCH 8/11 selftests/livepatch: Test where klp-build adds #line directives

In reply to Song Liu

Add 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

Assisted-by Claude:claude-opus-5
---
 .../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
index 96144f99b1aa..83f4e23460ac 100644
@@ -5,7 +5,8 @@ TEST_PROGS := \
 	test-dependent-patches.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
author maintainer
#13 lore raw

PATCH 9/11 selftests/livepatch: Test klp-build's rejection of unsupported files

In reply to Song Liu

Add a test that klp-build rejects patches to lib/, the vDSO, real mode code and assembly files, which it can't build a livepatch for.

Assisted-by Claude:claude-opus-5
---
 .../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
index 83f4e23460ac..af8f350226c2 100644
@@ -6,7 +6,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
new file mode 100755
index 000000000000..968ae80f7128
@@ -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_build unsupported.patch
+
+	expect_error "$PATCH_DIR/unsupported.patch: unsupported patch to $file"
+done
+
+exit 0
Signature · 1 line
2.53.0-Meta
author maintainer
#14 lore raw

PATCH 10/11 selftests/livepatch: Test klp-build with patches which don't apply

In reply to Song Liu

Add a test that klp-build 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.

Assisted-by Claude:claude-opus-5
---
 .../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
index af8f350226c2..2513105b3e1c 100644
@@ -7,7 +7,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
author maintainer
#15 lore raw

PATCH 11/11 selftests/livepatch: Test klp-build with patches which need offset or fuzz

In reply to Song Liu

klp-build 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 fixed patch has the line numbers and context of the real source, so the #line directives refer to the right lines.

Assisted-by Claude:claude-opus-5
---
 .../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
index 2513105b3e1c..a62ae5340fa4 100644
@@ -8,7 +8,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