1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
82
83#include <linux/module.h>
84#include <linux/kernel.h>
85#include <linux/livepatch.h>
86
87static int pre_patch_ret;
88module_param(pre_patch_ret, int, 0644);
89MODULE_PARM_DESC(pre_patch_ret, "pre_patch_ret (default=0)");
90
91static const char *const module_state[] = {
92 [MODULE_STATE_LIVE] = "[MODULE_STATE_LIVE] Normal state",
93 [MODULE_STATE_COMING] = "[MODULE_STATE_COMING] Full formed, running module_init",
94 [MODULE_STATE_GOING] = "[MODULE_STATE_GOING] Going away",
95 [MODULE_STATE_UNFORMED] = "[MODULE_STATE_UNFORMED] Still setting it up",
96};
97
98static void callback_info(const char *callback, struct klp_object *obj)
99{
100 if (obj->mod)
101 pr_info("%s: %s -> %s\n", callback, obj->mod->name,
102 module_state[obj->mod->state]);
103 else
104 pr_info("%s: vmlinux\n", callback);
105}
106
107
108static int pre_patch_callback(struct klp_object *obj)
109{
110 callback_info(__func__, obj);
111 return pre_patch_ret;
112}
113
114
115static void post_patch_callback(struct klp_object *obj)
116{
117 callback_info(__func__, obj);
118}
119
120
121static void pre_unpatch_callback(struct klp_object *obj)
122{
123 callback_info(__func__, obj);
124}
125
126
127static void post_unpatch_callback(struct klp_object *obj)
128{
129 callback_info(__func__, obj);
130}
131
132static void patched_work_func(struct work_struct *work)
133{
134 pr_info("%s\n", __func__);
135}
136
137static struct klp_func no_funcs[] = {
138 { }
139};
140
141static struct klp_func busymod_funcs[] = {
142 {
143 .old_name = "busymod_work_func",
144 .new_func = patched_work_func,
145 }, { }
146};
147
148static struct klp_object objs[] = {
149 {
150 .name = NULL,
151 .funcs = no_funcs,
152 .callbacks = {
153 .pre_patch = pre_patch_callback,
154 .post_patch = post_patch_callback,
155 .pre_unpatch = pre_unpatch_callback,
156 .post_unpatch = post_unpatch_callback,
157 },
158 }, {
159 .name = "livepatch_callbacks_mod",
160 .funcs = no_funcs,
161 .callbacks = {
162 .pre_patch = pre_patch_callback,
163 .post_patch = post_patch_callback,
164 .pre_unpatch = pre_unpatch_callback,
165 .post_unpatch = post_unpatch_callback,
166 },
167 }, {
168 .name = "livepatch_callbacks_busymod",
169 .funcs = busymod_funcs,
170 .callbacks = {
171 .pre_patch = pre_patch_callback,
172 .post_patch = post_patch_callback,
173 .pre_unpatch = pre_unpatch_callback,
174 .post_unpatch = post_unpatch_callback,
175 },
176 }, { }
177};
178
179static struct klp_patch patch = {
180 .mod = THIS_MODULE,
181 .objs = objs,
182};
183
184static int livepatch_callbacks_demo_init(void)
185{
186 return klp_enable_patch(&patch);
187}
188
189static void livepatch_callbacks_demo_exit(void)
190{
191}
192
193module_init(livepatch_callbacks_demo_init);
194module_exit(livepatch_callbacks_demo_exit);
195MODULE_LICENSE("GPL");
196MODULE_INFO(livepatch, "Y");
197