1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
3 
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5 
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/livepatch.h>
9 
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)");
13 
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",
19 };
20 
callback_info(const char * callback,struct klp_object * obj)21 static void callback_info(const char *callback, struct klp_object *obj)
22 {
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);
28 }
29 
30 /* Executed on object patching (ie, patch enablement) */
pre_patch_callback(struct klp_object * obj)31 static int pre_patch_callback(struct klp_object *obj)
32 {
33 	callback_info(__func__, obj);
34 	return pre_patch_ret;
35 }
36 
37 /* Executed on object unpatching (ie, patch disablement) */
post_patch_callback(struct klp_object * obj)38 static void post_patch_callback(struct klp_object *obj)
39 {
40 	callback_info(__func__, obj);
41 }
42 
43 /* Executed on object unpatching (ie, patch disablement) */
pre_unpatch_callback(struct klp_object * obj)44 static void pre_unpatch_callback(struct klp_object *obj)
45 {
46 	callback_info(__func__, obj);
47 }
48 
49 /* Executed on object unpatching (ie, patch disablement) */
post_unpatch_callback(struct klp_object * obj)50 static void post_unpatch_callback(struct klp_object *obj)
51 {
52 	callback_info(__func__, obj);
53 }
54 
patched_work_func(struct work_struct * work)55 static void patched_work_func(struct work_struct *work)
56 {
57 	pr_info("%s\n", __func__);
58 }
59 
60 static struct klp_func no_funcs[] = {
61 	{}
62 };
63 
64 static struct klp_func busymod_funcs[] = {
65 	{
66 		.old_name = "busymod_work_func",
67 		.new_func = patched_work_func,
68 	}, {}
69 };
70 
71 static struct klp_object objs[] = {
72 	{
73 		.name = NULL,	/* vmlinux */
74 		.funcs = no_funcs,
75 		.callbacks = {
76 			.pre_patch = pre_patch_callback,
77 			.post_patch = post_patch_callback,
78 			.pre_unpatch = pre_unpatch_callback,
79 			.post_unpatch = post_unpatch_callback,
80 		},
81 	},	{
82 		.name = "test_klp_callbacks_mod",
83 		.funcs = no_funcs,
84 		.callbacks = {
85 			.pre_patch = pre_patch_callback,
86 			.post_patch = post_patch_callback,
87 			.pre_unpatch = pre_unpatch_callback,
88 			.post_unpatch = post_unpatch_callback,
89 		},
90 	},	{
91 		.name = "test_klp_callbacks_busy",
92 		.funcs = busymod_funcs,
93 		.callbacks = {
94 			.pre_patch = pre_patch_callback,
95 			.post_patch = post_patch_callback,
96 			.pre_unpatch = pre_unpatch_callback,
97 			.post_unpatch = post_unpatch_callback,
98 		},
99 	}, { }
100 };
101 
102 static struct klp_patch patch = {
103 	.mod = THIS_MODULE,
104 	.objs = objs,
105 };
106 
test_klp_callbacks_demo_init(void)107 static int test_klp_callbacks_demo_init(void)
108 {
109 	return klp_enable_patch(&patch);
110 }
111 
test_klp_callbacks_demo_exit(void)112 static void test_klp_callbacks_demo_exit(void)
113 {
114 }
115 
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");
122