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

From: Song Liu

Date: Fri Oct 16 2026 - 20:25:29 EST


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
Signed-off-by: Song Liu <song@xxxxxxxxxx>
---
.../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

diff --git a/tools/testing/selftests/livepatch/test_klp_build/Makefile b/tools/testing/selftests/livepatch/test_klp_build/Makefile
index 6ebcecc828a1..405545c72303 100644
--- a/tools/testing/selftests/livepatch/test_klp_build/Makefile
+++ b/tools/testing/selftests/livepatch/test_klp_build/Makefile
@@ -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

diff --git a/tools/testing/selftests/livepatch/test_klp_build/test-dependent-patches.sh b/tools/testing/selftests/livepatch/test_klp_build/test-dependent-patches.sh
new file mode 100755
index 000000000000..75b957ac002c
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_klp_build/test-dependent-patches.sh
@@ -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
+@@ -1,6 +1,8 @@
+ int foo(int x)
+ {
++ if (!x)
++ return 0;
+ if (x < 0)
+ return -1;
+ return x;
+ }
+EOT
+
+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_build 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
--
2.53.0-Meta