[PATCH v2 06/12] selftests/livepatch: Test klp-prepare-patch with independent patches

From
Song Liu <song@kernel.org>
Date
2026-10-28 21:46:10
Also in
linux-kselftest, lkml
Reply to
Song Liu, 2026-10-28
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
Signed-off-by: Song Liu <song@kernel.org>
---
 .../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
diff --git a/tools/testing/selftests/livepatch/test_klp_build/Makefile b/tools/testing/selftests/livepatch/test_klp_build/Makefile
index 71ae793b0595..50205a047dcd 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-independent-patches.sh
 
 TEST_PROGS_EXTENDED := functions.sh
 
diff --git a/tools/testing/selftests/livepatch/test_klp_build/test-independent-patches.sh b/tools/testing/selftests/livepatch/test_klp_build/test-independent-patches.sh
new file mode 100755
index 000000000000..eebb075da7ce
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_klp_build/test-independent-patches.sh
@@ -0,0 +1,124 @@
+#!/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_file include/linux/foo.h <<'EOT'
+#ifndef _LINUX_FOO_H
+#define _LINUX_FOO_H
+
+int foo(int x);
+int bar(void);
+
+#endif
+EOT
+
+# - patches to different parts of a file are combined into one patch, with
+#   #line numbers from the original file even where an earlier patch moved
+#   the lines down
+
+start_test "patches to different functions of a file"
+
+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-baz.patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -14,7 +14,7 @@ int bar(void)
+
+ int baz(int x)
+ {
+-	if (x > 100)
+-		return 100;
++	if (x > FOO_MAX)
++		return FOO_MAX;
+ 	return x;
+ }
+--- a/include/linux/foo.h
++++ b/include/linux/foo.h
+@@ -1,6 +1,8 @@
+ #ifndef _LINUX_FOO_H
+ #define _LINUX_FOO_H
+
++#define FOO_MAX 100
++
+ int foo(int x);
+ int bar(void);
+
+EOT
+
+run_klp_prepare_patch 0001-foo.patch 0002-baz.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 0;
++#line 3
+ 	if (x < 0)
+ 		return -1;
+ 	return x;
+@@ -12,7 +15,8 @@ int bar(void)
+
+ int baz(int x)
+ {
+-	if (x > 100)
+-		return 100;
++	if (x > FOO_MAX)
++		return FOO_MAX;
++#line 17
+ 	return x;
+ }
+--- a/include/linux/foo.h
++++ b/include/linux/foo.h
+@@ -1,6 +1,9 @@
+ #ifndef _LINUX_FOO_H
+ #define _LINUX_FOO_H
+
++#define FOO_MAX 100
++
++#line 4
+ int foo(int x);
+ int bar(void);
+
+EOT
+
+exit 0
-- 
2.53.0-Meta
Thread (18 messages) 18 messages, 4 authors, last reply 4d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help