All of lore.kernel.org
 help / color / mirror / Atom feed
From: Song Liu <song@kernel.org>
To: live-patching@vger.kernel.org, linux-kselftest@vger.kernel.org
Cc: jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz,
	pmladek@suse.com, joe.lawrence@redhat.com, shuah@kernel.org,
	linux-kernel@vger.kernel.org, Song Liu <song@kernel.org>
Subject: [PATCH v2 07/12] selftests/livepatch: Test klp-prepare-patch with patches which cancel out
Date: Wed, 28 Oct 2026 14:45:29 -0700	[thread overview]
Message-ID: <20261028214534.1919398-8-song@kernel.org> (raw)
In-Reply-To: <20261028214534.1919398-1-song@kernel.org>

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

Assisted-by: Claude:claude-opus-5
Signed-off-by: Song Liu <song@kernel.org>
---
 .../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

diff --git a/tools/testing/selftests/livepatch/test_klp_build/Makefile b/tools/testing/selftests/livepatch/test_klp_build/Makefile
index 50205a047dcd..158a08d8a1b8 100644
--- a/tools/testing/selftests/livepatch/test_klp_build/Makefile
+++ b/tools/testing/selftests/livepatch/test_klp_build/Makefile
@@ -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
 
diff --git a/tools/testing/selftests/livepatch/test_klp_build/functions.sh b/tools/testing/selftests/livepatch/test_klp_build/functions.sh
index 6b2f30d72f99..e28988d07bc9 100644
--- a/tools/testing/selftests/livepatch/test_klp_build/functions.sh
+++ b/tools/testing/selftests/livepatch/test_klp_build/functions.sh
@@ -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"
+}
diff --git a/tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh b/tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh
new file mode 100755
index 000000000000..da6a12064be9
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh
@@ -0,0 +1,102 @@
+#!/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__;
+}
+
+int baz(int x)
+{
+	if (x > 100)
+		return 100;
+	return x;
+}
+EOT
+
+add_patch 0001-foo.patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -1,6 +1,8 @@
+ int foo(int x)
+ {
++	if (!x)
++		return 0;
+ 	if (x < 0)
+ 		return -1;
+ 	return x;
+ }
+EOT
+
+add_patch 0002-revert-foo.patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -1,8 +1,6 @@
+ int foo(int x)
+ {
+-	if (!x)
+-		return 0;
+ 	if (x < 0)
+ 		return -1;
+ 	return x;
+ }
+EOT
+
+add_patch 0003-baz.patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -12,7 +12,7 @@ int bar(void)
+
+ int baz(int x)
+ {
+-	if (x > 100)
+-		return 100;
++	if (x > 1000)
++		return 1000;
+ 	return x;
+ }
+EOT
+
+# - a patch and its revert leave nothing to build
+
+start_test "patch and its revert"
+
+run_klp_prepare_patch 0001-foo.patch 0002-revert-foo.patch
+
+expect_error "no changes detected"
+
+# - with another patch after them, only that patch's change is left
+
+start_test "patch and its revert, then another patch"
+
+run_klp_prepare_patch 0001-foo.patch 0002-revert-foo.patch 0003-baz.patch
+
+expect_patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -12,7 +12,8 @@ int bar(void)
+
+ int baz(int x)
+ {
+-	if (x > 100)
+-		return 100;
++	if (x > 1000)
++		return 1000;
++#line 17
+ 	return x;
+ }
+EOT
+
+exit 0
-- 
2.53.0-Meta


  parent reply	other threads:[~2026-10-28 21:46 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-28 21:45 [PATCH v2 00/12] klp-build: Fix dependent patches, add selftests Song Liu
2026-10-28 21:45 ` [PATCH v2 01/12] klp-build: Fix dependent patches in a series Song Liu
2026-10-28 21:45 ` [PATCH v2 02/12] klp-build: Process the patches before building the original kernel Song Liu
2026-10-28 21:45 ` [PATCH v2 03/12] klp-build: Move patch processing to klp-prepare-patch Song Liu
2026-10-28 21:45 ` [PATCH v2 04/12] selftests/livepatch: Add tests for klp-prepare-patch Song Liu
2026-10-28 21:45 ` [PATCH v2 05/12] selftests/livepatch: Test klp-prepare-patch with dependent patches Song Liu
2026-10-28 21:45 ` [PATCH v2 06/12] selftests/livepatch: Test klp-prepare-patch with independent patches Song Liu
2026-10-28 21:45 ` Song Liu [this message]
2026-10-28 21:45 ` [PATCH v2 08/12] selftests/livepatch: Test klp-prepare-patch with created and deleted files Song Liu
2026-10-28 21:45 ` [PATCH v2 09/12] selftests/livepatch: Test where klp-prepare-patch adds #line directives Song Liu
2026-10-28 21:45 ` [PATCH v2 10/12] selftests/livepatch: Test klp-prepare-patch with unsupported files Song Liu
2026-10-28 21:45 ` [PATCH v2 11/12] selftests/livepatch: Test klp-prepare-patch with patches which don't apply Song Liu
2026-10-28 21:45 ` [PATCH v2 12/12] selftests/livepatch: Test klp-prepare-patch with offset and fuzz Song Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261028214534.1919398-8-song@kernel.org \
    --to=song@kernel.org \
    --cc=jikos@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=pmladek@suse.com \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.