xref: /openbmc/linux/lib/livepatch/test_klp_callbacks_demo2.c (revision 597473720f4dc69749542bfcfed4a927a43d935e)
1*a2818ee4SJoe Lawrence // SPDX-License-Identifier: GPL-2.0
2*a2818ee4SJoe Lawrence // Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
3*a2818ee4SJoe Lawrence 
4*a2818ee4SJoe Lawrence #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5*a2818ee4SJoe Lawrence 
6*a2818ee4SJoe Lawrence #include <linux/module.h>
7*a2818ee4SJoe Lawrence #include <linux/kernel.h>
8*a2818ee4SJoe Lawrence #include <linux/livepatch.h>
9*a2818ee4SJoe Lawrence 
10*a2818ee4SJoe Lawrence static int replace;
11*a2818ee4SJoe Lawrence module_param(replace, int, 0644);
12*a2818ee4SJoe Lawrence MODULE_PARM_DESC(replace, "replace (default=0)");
13*a2818ee4SJoe Lawrence 
14*a2818ee4SJoe Lawrence static const char *const module_state[] = {
15*a2818ee4SJoe Lawrence 	[MODULE_STATE_LIVE]	= "[MODULE_STATE_LIVE] Normal state",
16*a2818ee4SJoe Lawrence 	[MODULE_STATE_COMING]	= "[MODULE_STATE_COMING] Full formed, running module_init",
17*a2818ee4SJoe Lawrence 	[MODULE_STATE_GOING]	= "[MODULE_STATE_GOING] Going away",
18*a2818ee4SJoe Lawrence 	[MODULE_STATE_UNFORMED]	= "[MODULE_STATE_UNFORMED] Still setting it up",
19*a2818ee4SJoe Lawrence };
20*a2818ee4SJoe Lawrence 
callback_info(const char * callback,struct klp_object * obj)21*a2818ee4SJoe Lawrence static void callback_info(const char *callback, struct klp_object *obj)
22*a2818ee4SJoe Lawrence {
23*a2818ee4SJoe Lawrence 	if (obj->mod)
24*a2818ee4SJoe Lawrence 		pr_info("%s: %s -> %s\n", callback, obj->mod->name,
25*a2818ee4SJoe Lawrence 			module_state[obj->mod->state]);
26*a2818ee4SJoe Lawrence 	else
27*a2818ee4SJoe Lawrence 		pr_info("%s: vmlinux\n", callback);
28*a2818ee4SJoe Lawrence }
29*a2818ee4SJoe Lawrence 
30*a2818ee4SJoe Lawrence /* Executed on object patching (ie, patch enablement) */
pre_patch_callback(struct klp_object * obj)31*a2818ee4SJoe Lawrence static int pre_patch_callback(struct klp_object *obj)
32*a2818ee4SJoe Lawrence {
33*a2818ee4SJoe Lawrence 	callback_info(__func__, obj);
34*a2818ee4SJoe Lawrence 	return 0;
35*a2818ee4SJoe Lawrence }
36*a2818ee4SJoe Lawrence 
37*a2818ee4SJoe Lawrence /* Executed on object unpatching (ie, patch disablement) */
post_patch_callback(struct klp_object * obj)38*a2818ee4SJoe Lawrence static void post_patch_callback(struct klp_object *obj)
39*a2818ee4SJoe Lawrence {
40*a2818ee4SJoe Lawrence 	callback_info(__func__, obj);
41*a2818ee4SJoe Lawrence }
42*a2818ee4SJoe Lawrence 
43*a2818ee4SJoe Lawrence /* Executed on object unpatching (ie, patch disablement) */
pre_unpatch_callback(struct klp_object * obj)44*a2818ee4SJoe Lawrence static void pre_unpatch_callback(struct klp_object *obj)
45*a2818ee4SJoe Lawrence {
46*a2818ee4SJoe Lawrence 	callback_info(__func__, obj);
47*a2818ee4SJoe Lawrence }
48*a2818ee4SJoe Lawrence 
49*a2818ee4SJoe Lawrence /* Executed on object unpatching (ie, patch disablement) */
post_unpatch_callback(struct klp_object * obj)50*a2818ee4SJoe Lawrence static void post_unpatch_callback(struct klp_object *obj)
51*a2818ee4SJoe Lawrence {
52*a2818ee4SJoe Lawrence 	callback_info(__func__, obj);
53*a2818ee4SJoe Lawrence }
54*a2818ee4SJoe Lawrence 
55*a2818ee4SJoe Lawrence static struct klp_func no_funcs[] = {
56*a2818ee4SJoe Lawrence 	{ }
57*a2818ee4SJoe Lawrence };
58*a2818ee4SJoe Lawrence 
59*a2818ee4SJoe Lawrence static struct klp_object objs[] = {
60*a2818ee4SJoe Lawrence 	{
61*a2818ee4SJoe Lawrence 		.name = NULL,	/* vmlinux */
62*a2818ee4SJoe Lawrence 		.funcs = no_funcs,
63*a2818ee4SJoe Lawrence 		.callbacks = {
64*a2818ee4SJoe Lawrence 			.pre_patch = pre_patch_callback,
65*a2818ee4SJoe Lawrence 			.post_patch = post_patch_callback,
66*a2818ee4SJoe Lawrence 			.pre_unpatch = pre_unpatch_callback,
67*a2818ee4SJoe Lawrence 			.post_unpatch = post_unpatch_callback,
68*a2818ee4SJoe Lawrence 		},
69*a2818ee4SJoe Lawrence 	}, { }
70*a2818ee4SJoe Lawrence };
71*a2818ee4SJoe Lawrence 
72*a2818ee4SJoe Lawrence static struct klp_patch patch = {
73*a2818ee4SJoe Lawrence 	.mod = THIS_MODULE,
74*a2818ee4SJoe Lawrence 	.objs = objs,
75*a2818ee4SJoe Lawrence 	/* set .replace in the init function below for demo purposes */
76*a2818ee4SJoe Lawrence };
77*a2818ee4SJoe Lawrence 
test_klp_callbacks_demo2_init(void)78*a2818ee4SJoe Lawrence static int test_klp_callbacks_demo2_init(void)
79*a2818ee4SJoe Lawrence {
80*a2818ee4SJoe Lawrence 	patch.replace = replace;
81*a2818ee4SJoe Lawrence 	return klp_enable_patch(&patch);
82*a2818ee4SJoe Lawrence }
83*a2818ee4SJoe Lawrence 
test_klp_callbacks_demo2_exit(void)84*a2818ee4SJoe Lawrence static void test_klp_callbacks_demo2_exit(void)
85*a2818ee4SJoe Lawrence {
86*a2818ee4SJoe Lawrence }
87*a2818ee4SJoe Lawrence 
88*a2818ee4SJoe Lawrence module_init(test_klp_callbacks_demo2_init);
89*a2818ee4SJoe Lawrence module_exit(test_klp_callbacks_demo2_exit);
90*a2818ee4SJoe Lawrence MODULE_LICENSE("GPL");
91*a2818ee4SJoe Lawrence MODULE_INFO(livepatch, "Y");
92*a2818ee4SJoe Lawrence MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>");
93*a2818ee4SJoe Lawrence MODULE_DESCRIPTION("Livepatch test: livepatch demo2");
94