1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/livepatch.h>
10 static int pre_patch_ret;
11 module_param(pre_patch_ret, int, 0644);
12 MODULE_PARM_DESC(pre_patch_ret, "pre_patch_ret (default=0)");
14 static const char *const module_state[] = {
15 [MODULE_STATE_LIVE] = "[MODULE_STATE_LIVE] Normal state",
16 [MODULE_STATE_COMING] = "[MODULE_STATE_COMING] Full formed, running module_init",
17 [MODULE_STATE_GOING] = "[MODULE_STATE_GOING] Going away",
18 [MODULE_STATE_UNFORMED] = "[MODULE_STATE_UNFORMED] Still setting it up",
21 static void callback_info(const char *callback, struct klp_object *obj)
24 pr_info("%s: %s -> %s\n", callback, obj->mod->name,
25 module_state[obj->mod->state]);
27 pr_info("%s: vmlinux\n", callback);
30 /* Executed on object patching (ie, patch enablement) */
31 static int pre_patch_callback(struct klp_object *obj)
33 callback_info(__func__, obj);
37 /* Executed on object unpatching (ie, patch disablement) */
38 static void post_patch_callback(struct klp_object *obj)
40 callback_info(__func__, obj);
43 /* Executed on object unpatching (ie, patch disablement) */
44 static void pre_unpatch_callback(struct klp_object *obj)
46 callback_info(__func__, obj);
49 /* Executed on object unpatching (ie, patch disablement) */
50 static void post_unpatch_callback(struct klp_object *obj)
52 callback_info(__func__, obj);
55 static void patched_work_func(struct work_struct *work)
57 pr_info("%s\n", __func__);
60 static struct klp_func no_funcs[] = {
64 static struct klp_func busymod_funcs[] = {
66 .old_name = "busymod_work_func",
67 .new_func = patched_work_func,
71 static struct klp_object objs[] = {
73 .name = NULL, /* vmlinux */
76 .pre_patch = pre_patch_callback,
77 .post_patch = post_patch_callback,
78 .pre_unpatch = pre_unpatch_callback,
79 .post_unpatch = post_unpatch_callback,
82 .name = "test_klp_callbacks_mod",
85 .pre_patch = pre_patch_callback,
86 .post_patch = post_patch_callback,
87 .pre_unpatch = pre_unpatch_callback,
88 .post_unpatch = post_unpatch_callback,
91 .name = "test_klp_callbacks_busy",
92 .funcs = busymod_funcs,
94 .pre_patch = pre_patch_callback,
95 .post_patch = post_patch_callback,
96 .pre_unpatch = pre_unpatch_callback,
97 .post_unpatch = post_unpatch_callback,
102 static struct klp_patch patch = {
107 static int test_klp_callbacks_demo_init(void)
109 return klp_enable_patch(&patch);
112 static void test_klp_callbacks_demo_exit(void)
116 module_init(test_klp_callbacks_demo_init);
117 module_exit(test_klp_callbacks_demo_exit);
118 MODULE_LICENSE("GPL");
119 MODULE_INFO(livepatch, "Y");
120 MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>");
121 MODULE_DESCRIPTION("Livepatch test: livepatch demo");