diff options
Diffstat (limited to 'tools/testing/selftests/livepatch/test_modules')
4 files changed, 118 insertions, 9 deletions
diff --git a/tools/testing/selftests/livepatch/test_modules/Makefile b/tools/testing/selftests/livepatch/test_modules/Makefile index 939230e571f5..a13d398585dc 100644 --- a/tools/testing/selftests/livepatch/test_modules/Makefile +++ b/tools/testing/selftests/livepatch/test_modules/Makefile @@ -8,6 +8,8 @@ obj-m += test_klp_atomic_replace.o \ test_klp_callbacks_mod.o \ test_klp_kprobe.o \ test_klp_livepatch.o \ + test_klp_mod_patch.o \ + test_klp_mod_target.o \ test_klp_shadow_vars.o \ test_klp_state.o \ test_klp_state2.o \ diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_mod_patch.c b/tools/testing/selftests/livepatch/test_modules/test_klp_mod_patch.c new file mode 100644 index 000000000000..6725b4720365 --- /dev/null +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_mod_patch.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Pablo Hugen <phugen@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_mod_target_show(struct seq_file *m, void *v) +{ + seq_printf(m, "%s: %s\n", THIS_MODULE->name, + "this has been live patched"); + return 0; +} + +static struct klp_func funcs[] = { + { + .old_name = "test_klp_mod_target_show", + .new_func = livepatch_mod_target_show, + }, + {}, +}; + +static struct klp_object objs[] = { + { + .name = "test_klp_mod_target", + .funcs = funcs, + }, + {}, +}; + +static struct klp_patch patch = { + .mod = THIS_MODULE, + .objs = objs, +}; + +static int test_klp_mod_patch_init(void) +{ + return klp_enable_patch(&patch); +} + +static void test_klp_mod_patch_exit(void) +{ +} + +module_init(test_klp_mod_patch_init); +module_exit(test_klp_mod_patch_exit); +MODULE_LICENSE("GPL"); +MODULE_INFO(livepatch, "Y"); +MODULE_AUTHOR("Pablo Hugen <phugen@redhat.com>"); +MODULE_DESCRIPTION("Livepatch test: patch for module-provided function"); diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_mod_target.c b/tools/testing/selftests/livepatch/test_modules/test_klp_mod_target.c new file mode 100644 index 000000000000..9643984d2402 --- /dev/null +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_mod_target.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Pablo Hugen <phugen@redhat.com> + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/proc_fs.h> +#include <linux/seq_file.h> + +static struct proc_dir_entry *pde; + +static noinline int test_klp_mod_target_show(struct seq_file *m, void *v) +{ + seq_printf(m, "%s: %s\n", THIS_MODULE->name, "original output"); + return 0; +} + +static int test_klp_mod_target_init(void) +{ + pr_info("%s\n", __func__); + pde = proc_create_single("test_klp_mod_target", 0, NULL, + test_klp_mod_target_show); + if (!pde) + return -ENOMEM; + return 0; +} + +static void test_klp_mod_target_exit(void) +{ + pr_info("%s\n", __func__); + proc_remove(pde); +} + +module_init(test_klp_mod_target_init); +module_exit(test_klp_mod_target_exit); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Pablo Hugen <phugen@redhat.com>"); +MODULE_DESCRIPTION("Livepatch test: target module with proc entry"); diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c b/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c index dd802783ea84..08aacc0e14de 100644 --- a/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c @@ -12,15 +12,26 @@ #include <linux/slab.h> #include <linux/livepatch.h> -#if defined(__x86_64__) -#define FN_PREFIX __x64_ -#elif defined(__s390x__) -#define FN_PREFIX __s390x_ -#elif defined(__aarch64__) -#define FN_PREFIX __arm64_ +/* + * Before CONFIG_ARCH_HAS_SYSCALL_WRAPPER was introduced there were no + * prefixes for system calls. + * powerpc set this config based on configs, so it can be enabled or not. + */ +#if defined(CONFIG_ARCH_HAS_SYSCALL_WRAPPER) + #if defined(__x86_64__) + #define FN_PREFIX __x64_ + #elif defined(__s390x__) + #define FN_PREFIX __s390x_ + #elif defined(__aarch64__) + #define FN_PREFIX __arm64_ + #elif defined(__powerpc__) + #define FN_PREFIX + #else + #error "Missing syscall wrapper for the given architecture." + #endif #else -/* powerpc does not select ARCH_HAS_SYSCALL_WRAPPER */ -#define FN_PREFIX + /* Do not set a prefix for architectures that do not enable wrappers. */ + #define FN_PREFIX #endif /* Protects klp_pids */ @@ -98,7 +109,11 @@ static int livepatch_init(void) */ npids = npids_pending; - return klp_enable_patch(&patch); + ret = klp_enable_patch(&patch); + if (ret) + kobject_put(klp_kobj); + + return ret; } static void livepatch_exit(void) |
