1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com>
7 * livepatch-callbacks-demo.c - (un)patching callbacks livepatch demo
13 * Demonstration of registering livepatch (un)patching callbacks.
19 * Step 1 - load the simple module
21 * insmod samples/livepatch/livepatch-callbacks-mod.ko
24 * Step 2 - load the demonstration livepatch (with callbacks)
26 * insmod samples/livepatch/livepatch-callbacks-demo.ko
31 * echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled
32 * rmmod livepatch_callbacks_demo
33 * rmmod livepatch_callbacks_mod
35 * Watch dmesg output to see livepatch enablement, callback execution
36 * and patching operations for both vmlinux and module targets.
38 * NOTE: swap the insmod order of livepatch-callbacks-mod.ko and
39 * livepatch-callbacks-demo.ko to observe what happens when a
40 * target module is loaded after a livepatch with callbacks.
42 * NOTE: 'pre_patch_ret' is a module parameter that sets the pre-patch
43 * callback return status. Try setting up a non-zero status
44 * such as -19 (-ENODEV):
46 * # Load demo livepatch, vmlinux is patched
47 * insmod samples/livepatch/livepatch-callbacks-demo.ko
49 * # Setup next pre-patch callback to return -ENODEV
50 * echo -19 > /sys/module/livepatch_callbacks_demo/parameters/pre_patch_ret
52 * # Module loader refuses to load the target module
53 * insmod samples/livepatch/livepatch-callbacks-mod.ko
54 * insmod: ERROR: could not insert module samples/livepatch/livepatch-callbacks-mod.ko: No such device
56 * NOTE: There is a second target module,
57 * livepatch-callbacks-busymod.ko, available for experimenting
58 * with livepatch (un)patch callbacks. This module contains
59 * a 'sleep_secs' parameter that parks the module on one of the
60 * functions that the livepatch demo module wants to patch.
61 * Modifying this value and tweaking the order of module loads can
62 * effectively demonstrate stalled patch transitions:
64 * # Load a target module, let it park on 'busymod_work_func' for
66 * insmod samples/livepatch/livepatch-callbacks-busymod.ko sleep_secs=30
68 * # Meanwhile load the livepatch
69 * insmod samples/livepatch/livepatch-callbacks-demo.ko
71 * # ... then load and unload another target module while the
72 * # transition is in progress
73 * insmod samples/livepatch/livepatch-callbacks-mod.ko
74 * rmmod samples/livepatch/livepatch-callbacks-mod.ko
77 * echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled
78 * rmmod samples/livepatch/livepatch-callbacks-demo.ko
81 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
83 #include <linux/module.h>
84 #include <linux/kernel.h>
85 #include <linux/livepatch.h>
87 static int pre_patch_ret
;
88 module_param(pre_patch_ret
, int, 0644);
89 MODULE_PARM_DESC(pre_patch_ret
, "pre_patch_ret (default=0)");
91 static 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",
98 static void callback_info(const char *callback
, struct klp_object
*obj
)
101 pr_info("%s: %s -> %s\n", callback
, obj
->mod
->name
,
102 module_state
[obj
->mod
->state
]);
104 pr_info("%s: vmlinux\n", callback
);
107 /* Executed on object patching (ie, patch enablement) */
108 static int pre_patch_callback(struct klp_object
*obj
)
110 callback_info(__func__
, obj
);
111 return pre_patch_ret
;
114 /* Executed on object unpatching (ie, patch disablement) */
115 static void post_patch_callback(struct klp_object
*obj
)
117 callback_info(__func__
, obj
);
120 /* Executed on object unpatching (ie, patch disablement) */
121 static void pre_unpatch_callback(struct klp_object
*obj
)
123 callback_info(__func__
, obj
);
126 /* Executed on object unpatching (ie, patch disablement) */
127 static void post_unpatch_callback(struct klp_object
*obj
)
129 callback_info(__func__
, obj
);
132 static void patched_work_func(struct work_struct
*work
)
134 pr_info("%s\n", __func__
);
137 static struct klp_func no_funcs
[] = {
141 static struct klp_func busymod_funcs
[] = {
143 .old_name
= "busymod_work_func",
144 .new_func
= patched_work_func
,
148 static struct klp_object objs
[] = {
150 .name
= NULL
, /* vmlinux */
153 .pre_patch
= pre_patch_callback
,
154 .post_patch
= post_patch_callback
,
155 .pre_unpatch
= pre_unpatch_callback
,
156 .post_unpatch
= post_unpatch_callback
,
159 .name
= "livepatch_callbacks_mod",
162 .pre_patch
= pre_patch_callback
,
163 .post_patch
= post_patch_callback
,
164 .pre_unpatch
= pre_unpatch_callback
,
165 .post_unpatch
= post_unpatch_callback
,
168 .name
= "livepatch_callbacks_busymod",
169 .funcs
= busymod_funcs
,
171 .pre_patch
= pre_patch_callback
,
172 .post_patch
= post_patch_callback
,
173 .pre_unpatch
= pre_unpatch_callback
,
174 .post_unpatch
= post_unpatch_callback
,
179 static struct klp_patch patch
= {
184 static int livepatch_callbacks_demo_init(void)
186 return klp_enable_patch(&patch
);
189 static void livepatch_callbacks_demo_exit(void)
193 module_init(livepatch_callbacks_demo_init
);
194 module_exit(livepatch_callbacks_demo_exit
);
195 MODULE_LICENSE("GPL");
196 MODULE_INFO(livepatch
, "Y");