Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / lib / livepatch / test_klp_callbacks_demo2.c
blob5417573e80af5ae2a28a98374cbdf8e723bd7876
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 replace;
11 module_param(replace, int, 0644);
12 MODULE_PARM_DESC(replace, "replace (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)
23 if (obj->mod)
24 pr_info("%s: %s -> %s\n", callback, obj->mod->name,
25 module_state[obj->mod->state]);
26 else
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);
34 return 0;
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 struct klp_func no_funcs[] = {
56 { }
59 static struct klp_object objs[] = {
61 .name = NULL, /* vmlinux */
62 .funcs = no_funcs,
63 .callbacks = {
64 .pre_patch = pre_patch_callback,
65 .post_patch = post_patch_callback,
66 .pre_unpatch = pre_unpatch_callback,
67 .post_unpatch = post_unpatch_callback,
69 }, { }
72 static struct klp_patch patch = {
73 .mod = THIS_MODULE,
74 .objs = objs,
75 /* set .replace in the init function below for demo purposes */
78 static int test_klp_callbacks_demo2_init(void)
80 patch.replace = replace;
81 return klp_enable_patch(&patch);
84 static void test_klp_callbacks_demo2_exit(void)
88 module_init(test_klp_callbacks_demo2_init);
89 module_exit(test_klp_callbacks_demo2_exit);
90 MODULE_LICENSE("GPL");
91 MODULE_INFO(livepatch, "Y");
92 MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>");
93 MODULE_DESCRIPTION("Livepatch test: livepatch demo2");