The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Wardenjohn <zhangwarden@gmail.com>
To: jpoimboe@kernel.org, mbenes@suse.cz, jikos@kernel.org,
	pmladek@suse.com, joe.lawrence@redhat.com
Cc: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org,
	Wardenjohn <zhangwarden@gmail.com>
Subject: [PATCH 1/1] selftests: livepatch: add test cases of stack_order sysfs interface
Date: Tue,  8 Oct 2024 15:52:03 +0800	[thread overview]
Message-ID: <20241008075203.36235-2-zhangwarden@gmail.com> (raw)
In-Reply-To: <20241008075203.36235-1-zhangwarden@gmail.com>

Add selftest test cases to sysfs attribute 'stack_order'.

Signed-off-by: Wardenjohn <zhangwarden@gmail.com>
---
 .../testing/selftests/livepatch/test-sysfs.sh | 71 +++++++++++++++++++
 .../selftests/livepatch/test_modules/Makefile |  5 +-
 .../test_klp_livepatch_noreplace.c            | 53 ++++++++++++++
 .../test_klp_livepatch_noreplace2.c           | 53 ++++++++++++++
 .../test_klp_livepatch_noreplace3.c           | 53 ++++++++++++++
 5 files changed, 234 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace.c
 create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace2.c
 create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace3.c

diff --git a/tools/testing/selftests/livepatch/test-sysfs.sh b/tools/testing/selftests/livepatch/test-sysfs.sh
index 05a14f5a7bfb..a086b62fb488 100755
--- a/tools/testing/selftests/livepatch/test-sysfs.sh
+++ b/tools/testing/selftests/livepatch/test-sysfs.sh
@@ -5,6 +5,9 @@
 . $(dirname $0)/functions.sh
 
 MOD_LIVEPATCH=test_klp_livepatch
+MOD_LIVEPATCH_NOREPLACE=test_klp_livepatch_noreplace
+MOD_LIVEPATCH_NOREPLACE2=test_klp_livepatch_noreplace2
+MOD_LIVEPATCH_NOREPLACE3=test_klp_livepatch_noreplace3
 
 setup_config
 
@@ -131,4 +134,72 @@ livepatch: '$MOD_LIVEPATCH': completing unpatching transition
 livepatch: '$MOD_LIVEPATCH': unpatching complete
 % rmmod $MOD_LIVEPATCH"
 
+start_test "sysfs test stack_order read"
+
+load_lp $MOD_LIVEPATCH_NOREPLACE
+
+check_sysfs_rights "$MOD_LIVEPATCH_NOREPLACE" "stack_order" "-r--r--r--"
+check_sysfs_value  "$MOD_LIVEPATCH_NOREPLACE" "stack_order" "1"
+
+load_lp $MOD_LIVEPATCH_NOREPLACE2
+
+check_sysfs_rights "$MOD_LIVEPATCH_NOREPLACE2" "stack_order" "-r--r--r--"
+check_sysfs_value  "$MOD_LIVEPATCH_NOREPLACE2" "stack_order" "2"
+
+load_lp $MOD_LIVEPATCH_NOREPLACE3
+
+check_sysfs_rights "$MOD_LIVEPATCH_NOREPLACE3" "stack_order" "-r--r--r--"
+check_sysfs_value  "$MOD_LIVEPATCH_NOREPLACE3" "stack_order" "3"
+
+disable_lp $MOD_LIVEPATCH_NOREPLACE2
+unload_lp $MOD_LIVEPATCH_NOREPLACE2
+
+check_sysfs_rights "$MOD_LIVEPATCH_NOREPLACE" "stack_order" "-r--r--r--"
+check_sysfs_value  "$MOD_LIVEPATCH_NOREPLACE" "stack_order" "1"
+check_sysfs_rights "$MOD_LIVEPATCH_NOREPLACE3" "stack_order" "-r--r--r--"
+check_sysfs_value  "$MOD_LIVEPATCH_NOREPLACE3" "stack_order" "2"
+
+disable_lp $MOD_LIVEPATCH_NOREPLACE3
+unload_lp $MOD_LIVEPATCH_NOREPLACE3
+
+disable_lp $MOD_LIVEPATCH_NOREPLACE
+unload_lp $MOD_LIVEPATCH_NOREPLACE
+
+check_result "% insmod test_modules/$MOD_LIVEPATCH_NOREPLACE.ko
+livepatch: enabling patch '$MOD_LIVEPATCH_NOREPLACE'
+livepatch: '$MOD_LIVEPATCH_NOREPLACE': initializing patching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE': starting patching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE': completing patching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE': patching complete
+% insmod test_modules/$MOD_LIVEPATCH_NOREPLACE2.ko
+livepatch: enabling patch '$MOD_LIVEPATCH_NOREPLACE2'
+livepatch: '$MOD_LIVEPATCH_NOREPLACE2': initializing patching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE2': starting patching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE2': completing patching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE2': patching complete
+% insmod test_modules/$MOD_LIVEPATCH_NOREPLACE3.ko
+livepatch: enabling patch '$MOD_LIVEPATCH_NOREPLACE3'
+livepatch: '$MOD_LIVEPATCH_NOREPLACE3': initializing patching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE3': starting patching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE3': completing patching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE3': patching complete
+% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH_NOREPLACE2/enabled
+livepatch: '$MOD_LIVEPATCH_NOREPLACE2': initializing unpatching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE2': starting unpatching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE2': completing unpatching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE2': unpatching complete
+% rmmod $MOD_LIVEPATCH_NOREPLACE2
+% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH_NOREPLACE3/enabled
+livepatch: '$MOD_LIVEPATCH_NOREPLACE3': initializing unpatching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE3': starting unpatching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE3': completing unpatching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE3': unpatching complete
+% rmmod $MOD_LIVEPATCH_NOREPLACE3
+% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH_NOREPLACE/enabled
+livepatch: '$MOD_LIVEPATCH_NOREPLACE': initializing unpatching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE': starting unpatching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE': completing unpatching transition
+livepatch: '$MOD_LIVEPATCH_NOREPLACE': unpatching complete
+% rmmod $MOD_LIVEPATCH_NOREPLACE"
+
 exit 0
diff --git a/tools/testing/selftests/livepatch/test_modules/Makefile b/tools/testing/selftests/livepatch/test_modules/Makefile
index e6e638c4bcba..dad6ca00d3e6 100644
--- a/tools/testing/selftests/livepatch/test_modules/Makefile
+++ b/tools/testing/selftests/livepatch/test_modules/Makefile
@@ -11,7 +11,10 @@ obj-m += test_klp_atomic_replace.o \
 	test_klp_state2.o \
 	test_klp_state3.o \
 	test_klp_shadow_vars.o \
-	test_klp_syscall.o
+	test_klp_syscall.o \
+	test_klp_livepatch_noreplace.o \
+	test_klp_livepatch_noreplace2.o \
+	test_klp_livepatch_noreplace3.o \
 
 # Ensure that KDIR exists, otherwise skip the compilation
 modules:
diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace.c b/tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace.c
new file mode 100644
index 000000000000..ead609aeac67
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace.c
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/livepatch.h>
+
+#include <linux/seq_file.h>
+static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
+{
+	seq_printf(m, "%s: %s\n", THIS_MODULE->name,
+		   "this has been live patched with number 1");
+	return 0;
+}
+
+static struct klp_func funcs[] = {
+	{
+		.old_name = "cmdline_proc_show",
+		.new_func = livepatch_cmdline_proc_show,
+	}, { }
+};
+
+static struct klp_object objs[] = {
+	{
+		/* name being NULL means vmlinux */
+		.funcs = funcs,
+	}, { }
+};
+
+static struct klp_patch patch = {
+	.mod = THIS_MODULE,
+	.objs = objs,
+	.replace = false,
+};
+
+static int test_klp_livepatch_init(void)
+{
+	return klp_enable_patch(&patch);
+}
+
+static void test_klp_livepatch_exit(void)
+{
+}
+
+module_init(test_klp_livepatch_init);
+module_exit(test_klp_livepatch_exit);
+MODULE_LICENSE("GPL");
+MODULE_INFO(livepatch, "Y");
+MODULE_AUTHOR("Seth Jennings <sjenning@redhat.com>");
+MODULE_AUTHOR("Wardenjohn <zhangwarden@gmail.com>");
+MODULE_DESCRIPTION("Livepatch test: livepatch module");
diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace2.c b/tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace2.c
new file mode 100644
index 000000000000..8d54b0976be1
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace2.c
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/livepatch.h>
+
+#include <linux/seq_file.h>
+static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
+{
+	seq_printf(m, "%s: %s\n", THIS_MODULE->name,
+		   "this has been live patched with number 2");
+	return 0;
+}
+
+static struct klp_func funcs[] = {
+	{
+		.old_name = "cmdline_proc_show",
+		.new_func = livepatch_cmdline_proc_show,
+	}, { }
+};
+
+static struct klp_object objs[] = {
+	{
+		/* name being NULL means vmlinux */
+		.funcs = funcs,
+	}, { }
+};
+
+static struct klp_patch patch = {
+	.mod = THIS_MODULE,
+	.objs = objs,
+	.replace = false,
+};
+
+static int test_klp_livepatch_init(void)
+{
+	return klp_enable_patch(&patch);
+}
+
+static void test_klp_livepatch_exit(void)
+{
+}
+
+module_init(test_klp_livepatch_init);
+module_exit(test_klp_livepatch_exit);
+MODULE_LICENSE("GPL");
+MODULE_INFO(livepatch, "Y");
+MODULE_AUTHOR("Seth Jennings <sjenning@redhat.com>");
+MODULE_AUTHOR("Wardenjohn <zhangwarden@gmail.com>");
+MODULE_DESCRIPTION("Livepatch test: livepatch module");
diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace3.c b/tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace3.c
new file mode 100644
index 000000000000..a267c58e07d4
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_modules/test_klp_livepatch_noreplace3.c
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/livepatch.h>
+
+#include <linux/seq_file.h>
+static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
+{
+	seq_printf(m, "%s: %s\n", THIS_MODULE->name,
+		   "this has been live patched with number 3");
+	return 0;
+}
+
+static struct klp_func funcs[] = {
+	{
+		.old_name = "cmdline_proc_show",
+		.new_func = livepatch_cmdline_proc_show,
+	}, { }
+};
+
+static struct klp_object objs[] = {
+	{
+		/* name being NULL means vmlinux */
+		.funcs = funcs,
+	}, { }
+};
+
+static struct klp_patch patch = {
+	.mod = THIS_MODULE,
+	.objs = objs,
+	.replace = false,
+};
+
+static int test_klp_livepatch_init(void)
+{
+	return klp_enable_patch(&patch);
+}
+
+static void test_klp_livepatch_exit(void)
+{
+}
+
+module_init(test_klp_livepatch_init);
+module_exit(test_klp_livepatch_exit);
+MODULE_LICENSE("GPL");
+MODULE_INFO(livepatch, "Y");
+MODULE_AUTHOR("Seth Jennings <sjenning@redhat.com>");
+MODULE_AUTHOR("Wardenjohn <zhangwarden@gmail.com>");
+MODULE_DESCRIPTION("Livepatch test: livepatch module");
-- 
2.43.5


  reply	other threads:[~2024-10-08  7:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-08  7:52 [PATCH 0/1] selftests: livepatch: add test cases of stack_order sysfs Wardenjohn
2024-10-08  7:52 ` Wardenjohn [this message]
2024-10-10 12:31   ` [PATCH 1/1] selftests: livepatch: add test cases of stack_order sysfs interface Marcos Paulo de Souza
2024-10-10 15:11     ` zhang warden
2024-10-10 15:51       ` Josh Poimboeuf
2024-10-11  1:51         ` zhang warden
2024-10-11  4:09           ` Josh Poimboeuf
2024-10-11  8:36           ` Petr Mladek

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=20241008075203.36235-2-zhangwarden@gmail.com \
    --to=zhangwarden@gmail.com \
    --cc=jikos@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=pmladek@suse.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox