1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun // Copyright (C) 2019 SUSE
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/slab.h>
7*4882a593Smuzhiyun #include <linux/module.h>
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/printk.h>
10*4882a593Smuzhiyun #include <linux/livepatch.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #define CONSOLE_LOGLEVEL_STATE 1
13*4882a593Smuzhiyun /* Version 1 does not support migration. */
14*4882a593Smuzhiyun #define CONSOLE_LOGLEVEL_STATE_VERSION 1
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun static const char *const module_state[] = {
17*4882a593Smuzhiyun [MODULE_STATE_LIVE] = "[MODULE_STATE_LIVE] Normal state",
18*4882a593Smuzhiyun [MODULE_STATE_COMING] = "[MODULE_STATE_COMING] Full formed, running module_init",
19*4882a593Smuzhiyun [MODULE_STATE_GOING] = "[MODULE_STATE_GOING] Going away",
20*4882a593Smuzhiyun [MODULE_STATE_UNFORMED] = "[MODULE_STATE_UNFORMED] Still setting it up",
21*4882a593Smuzhiyun };
22*4882a593Smuzhiyun
callback_info(const char * callback,struct klp_object * obj)23*4882a593Smuzhiyun static void callback_info(const char *callback, struct klp_object *obj)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun if (obj->mod)
26*4882a593Smuzhiyun pr_info("%s: %s -> %s\n", callback, obj->mod->name,
27*4882a593Smuzhiyun module_state[obj->mod->state]);
28*4882a593Smuzhiyun else
29*4882a593Smuzhiyun pr_info("%s: vmlinux\n", callback);
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun static struct klp_patch patch;
33*4882a593Smuzhiyun
allocate_loglevel_state(void)34*4882a593Smuzhiyun static int allocate_loglevel_state(void)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun struct klp_state *loglevel_state;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun loglevel_state = klp_get_state(&patch, CONSOLE_LOGLEVEL_STATE);
39*4882a593Smuzhiyun if (!loglevel_state)
40*4882a593Smuzhiyun return -EINVAL;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun loglevel_state->data = kzalloc(sizeof(console_loglevel), GFP_KERNEL);
43*4882a593Smuzhiyun if (!loglevel_state->data)
44*4882a593Smuzhiyun return -ENOMEM;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun pr_info("%s: allocating space to store console_loglevel\n",
47*4882a593Smuzhiyun __func__);
48*4882a593Smuzhiyun return 0;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
fix_console_loglevel(void)51*4882a593Smuzhiyun static void fix_console_loglevel(void)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun struct klp_state *loglevel_state;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun loglevel_state = klp_get_state(&patch, CONSOLE_LOGLEVEL_STATE);
56*4882a593Smuzhiyun if (!loglevel_state)
57*4882a593Smuzhiyun return;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun pr_info("%s: fixing console_loglevel\n", __func__);
60*4882a593Smuzhiyun *(int *)loglevel_state->data = console_loglevel;
61*4882a593Smuzhiyun console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
restore_console_loglevel(void)64*4882a593Smuzhiyun static void restore_console_loglevel(void)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun struct klp_state *loglevel_state;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun loglevel_state = klp_get_state(&patch, CONSOLE_LOGLEVEL_STATE);
69*4882a593Smuzhiyun if (!loglevel_state)
70*4882a593Smuzhiyun return;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun pr_info("%s: restoring console_loglevel\n", __func__);
73*4882a593Smuzhiyun console_loglevel = *(int *)loglevel_state->data;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
free_loglevel_state(void)76*4882a593Smuzhiyun static void free_loglevel_state(void)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun struct klp_state *loglevel_state;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun loglevel_state = klp_get_state(&patch, CONSOLE_LOGLEVEL_STATE);
81*4882a593Smuzhiyun if (!loglevel_state)
82*4882a593Smuzhiyun return;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun pr_info("%s: freeing space for the stored console_loglevel\n",
85*4882a593Smuzhiyun __func__);
86*4882a593Smuzhiyun kfree(loglevel_state->data);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* Executed on object patching (ie, patch enablement) */
pre_patch_callback(struct klp_object * obj)90*4882a593Smuzhiyun static int pre_patch_callback(struct klp_object *obj)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun callback_info(__func__, obj);
93*4882a593Smuzhiyun return allocate_loglevel_state();
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /* Executed on object unpatching (ie, patch disablement) */
post_patch_callback(struct klp_object * obj)97*4882a593Smuzhiyun static void post_patch_callback(struct klp_object *obj)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun callback_info(__func__, obj);
100*4882a593Smuzhiyun fix_console_loglevel();
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /* Executed on object unpatching (ie, patch disablement) */
pre_unpatch_callback(struct klp_object * obj)104*4882a593Smuzhiyun static void pre_unpatch_callback(struct klp_object *obj)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun callback_info(__func__, obj);
107*4882a593Smuzhiyun restore_console_loglevel();
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /* Executed on object unpatching (ie, patch disablement) */
post_unpatch_callback(struct klp_object * obj)111*4882a593Smuzhiyun static void post_unpatch_callback(struct klp_object *obj)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun callback_info(__func__, obj);
114*4882a593Smuzhiyun free_loglevel_state();
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun static struct klp_func no_funcs[] = {
118*4882a593Smuzhiyun {}
119*4882a593Smuzhiyun };
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun static struct klp_object objs[] = {
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun .name = NULL, /* vmlinux */
124*4882a593Smuzhiyun .funcs = no_funcs,
125*4882a593Smuzhiyun .callbacks = {
126*4882a593Smuzhiyun .pre_patch = pre_patch_callback,
127*4882a593Smuzhiyun .post_patch = post_patch_callback,
128*4882a593Smuzhiyun .pre_unpatch = pre_unpatch_callback,
129*4882a593Smuzhiyun .post_unpatch = post_unpatch_callback,
130*4882a593Smuzhiyun },
131*4882a593Smuzhiyun }, { }
132*4882a593Smuzhiyun };
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun static struct klp_state states[] = {
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun .id = CONSOLE_LOGLEVEL_STATE,
137*4882a593Smuzhiyun .version = CONSOLE_LOGLEVEL_STATE_VERSION,
138*4882a593Smuzhiyun }, { }
139*4882a593Smuzhiyun };
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun static struct klp_patch patch = {
142*4882a593Smuzhiyun .mod = THIS_MODULE,
143*4882a593Smuzhiyun .objs = objs,
144*4882a593Smuzhiyun .states = states,
145*4882a593Smuzhiyun .replace = true,
146*4882a593Smuzhiyun };
147*4882a593Smuzhiyun
test_klp_callbacks_demo_init(void)148*4882a593Smuzhiyun static int test_klp_callbacks_demo_init(void)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun return klp_enable_patch(&patch);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
test_klp_callbacks_demo_exit(void)153*4882a593Smuzhiyun static void test_klp_callbacks_demo_exit(void)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun module_init(test_klp_callbacks_demo_init);
158*4882a593Smuzhiyun module_exit(test_klp_callbacks_demo_exit);
159*4882a593Smuzhiyun MODULE_LICENSE("GPL");
160*4882a593Smuzhiyun MODULE_INFO(livepatch, "Y");
161*4882a593Smuzhiyun MODULE_AUTHOR("Petr Mladek <pmladek@suse.com>");
162*4882a593Smuzhiyun MODULE_DESCRIPTION("Livepatch test: system state modification");
163