The per-object callbacks have been obsoleted by per-state callbacks. As a result, the callbacks test modules have been obsoleted by updated klp state tests. The callbacks test modules are re-used in the sysfs selftests. It would be possible to replace them by klp state test modules but the newly generated logs are hard to review because there is a lot of noise caused by the callbacks. Instead, introduce a simple test module in a "Hello World" style and corresponding livepatch. The expected log can be reviewed easily. The test module might be later extended to provide more functionality which might be used in more tests. It would allow to create tests focusing on some particular feature with an easier output. Signed-off-by: Petr Mladek <pmladek@xxxxxxxx> --- lib/livepatch/Makefile | 2 + lib/livepatch/test_klp_speaker.c | 34 +++++++++++++ lib/livepatch/test_klp_speaker_livepatch.c | 50 +++++++++++++++++++ .../testing/selftests/livepatch/test-sysfs.sh | 48 ++++++++---------- 4 files changed, 106 insertions(+), 28 deletions(-) create mode 100644 lib/livepatch/test_klp_speaker.c create mode 100644 lib/livepatch/test_klp_speaker_livepatch.c diff --git a/lib/livepatch/Makefile b/lib/livepatch/Makefile index dcc912b3478f..a8a5f6597633 100644 --- a/lib/livepatch/Makefile +++ b/lib/livepatch/Makefile @@ -9,6 +9,8 @@ obj-$(CONFIG_TEST_LIVEPATCH) += test_klp_atomic_replace.o \ test_klp_callbacks_mod.o \ test_klp_livepatch.o \ test_klp_shadow_vars.o \ + test_klp_speaker.o \ + test_klp_speaker_livepatch.o \ test_klp_state.o \ test_klp_state2.o \ test_klp_state3.o diff --git a/lib/livepatch/test_klp_speaker.c b/lib/livepatch/test_klp_speaker.c new file mode 100644 index 000000000000..d2d31072639a --- /dev/null +++ b/lib/livepatch/test_klp_speaker.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2023 SUSE + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#ifndef _VER_NAME +#define _VER_NAME(name) name +#endif + +#include <linux/module.h> +#include <linux/printk.h> + +noinline +void speaker_welcome(void) +{ + pr_info("%s: Hello, World!\n", __func__); +} + +static int test_klp_speaker_init(void) +{ + pr_info("%s\n", __func__); + + return 0; +} + +static void test_klp_speaker_exit(void) +{ + pr_info("%s\n", __func__); +} + +module_init(test_klp_speaker_init); +module_exit(test_klp_speaker_exit); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Livepatch test: test functions"); diff --git a/lib/livepatch/test_klp_speaker_livepatch.c b/lib/livepatch/test_klp_speaker_livepatch.c new file mode 100644 index 000000000000..0317a4937b78 --- /dev/null +++ b/lib/livepatch/test_klp_speaker_livepatch.c @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/livepatch.h> +#include <linux/init.h> + + +void livepatch_speaker_welcome(void) +{ + pr_info("%s: Ladies and gentleman, ...\n", __func__); +} + + +static struct klp_func test_klp_speaker_funcs[] = { + { + .old_name = "speaker_welcome", + .new_func = livepatch_speaker_welcome, + }, + { } +}; + +static struct klp_object objs[] = { + { + .name = "test_klp_speaker", + .funcs = test_klp_speaker_funcs, + }, + { } +}; + +static struct klp_patch patch = { + .mod = THIS_MODULE, + .objs = objs, +}; + +static int test_klp_speaker_livepatch_init(void) +{ + return klp_enable_patch(&patch); +} + +static void test_klp_speaker_livepatch_exit(void) +{ +} + +module_init(test_klp_speaker_livepatch_init); +module_exit(test_klp_speaker_livepatch_exit); +MODULE_LICENSE("GPL"); +MODULE_INFO(livepatch, "Y"); diff --git a/tools/testing/selftests/livepatch/test-sysfs.sh b/tools/testing/selftests/livepatch/test-sysfs.sh index 7f76f280189a..424b6af32c99 100755 --- a/tools/testing/selftests/livepatch/test-sysfs.sh +++ b/tools/testing/selftests/livepatch/test-sysfs.sh @@ -42,8 +42,8 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete start_test "sysfs test object/patched" -MOD_LIVEPATCH=test_klp_callbacks_demo -MOD_TARGET=test_klp_callbacks_mod +MOD_LIVEPATCH=test_klp_speaker_livepatch +MOD_TARGET=test_klp_speaker load_lp $MOD_LIVEPATCH # check the "patch" file changes as target module loads/unloads @@ -56,31 +56,23 @@ check_sysfs_value "$MOD_LIVEPATCH" "$MOD_TARGET/patched" "0" disable_lp $MOD_LIVEPATCH unload_lp $MOD_LIVEPATCH -check_result "% modprobe test_klp_callbacks_demo -livepatch: enabling patch 'test_klp_callbacks_demo' -livepatch: 'test_klp_callbacks_demo': initializing patching transition -test_klp_callbacks_demo: pre_patch_callback: vmlinux -livepatch: 'test_klp_callbacks_demo': starting patching transition -livepatch: 'test_klp_callbacks_demo': completing patching transition -test_klp_callbacks_demo: post_patch_callback: vmlinux -livepatch: 'test_klp_callbacks_demo': patching complete -% modprobe test_klp_callbacks_mod -livepatch: applying patch 'test_klp_callbacks_demo' to loading module 'test_klp_callbacks_mod' -test_klp_callbacks_demo: pre_patch_callback: test_klp_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init -test_klp_callbacks_demo: post_patch_callback: test_klp_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init -test_klp_callbacks_mod: test_klp_callbacks_mod_init -% rmmod test_klp_callbacks_mod -test_klp_callbacks_mod: test_klp_callbacks_mod_exit -test_klp_callbacks_demo: pre_unpatch_callback: test_klp_callbacks_mod -> [MODULE_STATE_GOING] Going away -livepatch: reverting patch 'test_klp_callbacks_demo' on unloading module 'test_klp_callbacks_mod' -test_klp_callbacks_demo: post_unpatch_callback: test_klp_callbacks_mod -> [MODULE_STATE_GOING] Going away -% echo 0 > /sys/kernel/livepatch/test_klp_callbacks_demo/enabled -livepatch: 'test_klp_callbacks_demo': initializing unpatching transition -test_klp_callbacks_demo: pre_unpatch_callback: vmlinux -livepatch: 'test_klp_callbacks_demo': starting unpatching transition -livepatch: 'test_klp_callbacks_demo': completing unpatching transition -test_klp_callbacks_demo: post_unpatch_callback: vmlinux -livepatch: 'test_klp_callbacks_demo': unpatching complete -% rmmod test_klp_callbacks_demo" +check_result "% modprobe $MOD_LIVEPATCH +livepatch: enabling patch '$MOD_LIVEPATCH' +livepatch: '$MOD_LIVEPATCH': initializing patching transition +livepatch: '$MOD_LIVEPATCH': starting patching transition +livepatch: '$MOD_LIVEPATCH': completing patching transition +livepatch: '$MOD_LIVEPATCH': patching complete +% modprobe $MOD_TARGET +livepatch: applying patch '$MOD_LIVEPATCH' to loading module '$MOD_TARGET' +$MOD_TARGET: ${MOD_TARGET}_init +% rmmod $MOD_TARGET +$MOD_TARGET: ${MOD_TARGET}_exit +livepatch: reverting patch '$MOD_LIVEPATCH' on unloading module '$MOD_TARGET' +% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled +livepatch: '$MOD_LIVEPATCH': initializing unpatching transition +livepatch: '$MOD_LIVEPATCH': starting unpatching transition +livepatch: '$MOD_LIVEPATCH': completing unpatching transition +livepatch: '$MOD_LIVEPATCH': unpatching complete +% rmmod $MOD_LIVEPATCH" exit 0 -- 2.35.3