xref: /linux/tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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 replace;
11 module_param(replace, int, 0644);
12 MODULE_PARM_DESC(replace, "replace (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 0;
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 
55 static struct klp_func no_funcs[] = {
56 	{ }
57 };
58 
59 static struct klp_object objs[] = {
60 	{
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,
68 		},
69 	}, { }
70 };
71 
72 static struct klp_patch patch = {
73 	.mod = THIS_MODULE,
74 	.objs = objs,
75 	/* set .replace in the init function below for demo purposes */
76 };
77 
test_klp_callbacks_demo2_init(void)78 static int test_klp_callbacks_demo2_init(void)
79 {
80 	patch.replace = replace;
81 	return klp_enable_patch(&patch);
82 }
83 
test_klp_callbacks_demo2_exit(void)84 static void test_klp_callbacks_demo2_exit(void)
85 {
86 }
87 
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");
94