1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2019 SUSE
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/slab.h>
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/printk.h>
10 #include <linux/livepatch.h>
12 #define CONSOLE_LOGLEVEL_STATE 1
13 /* Version 1 does not support migration. */
14 #define CONSOLE_LOGLEVEL_STATE_VERSION 1
16 static const char *const module_state
[] = {
17 [MODULE_STATE_LIVE
] = "[MODULE_STATE_LIVE] Normal state",
18 [MODULE_STATE_COMING
] = "[MODULE_STATE_COMING] Full formed, running module_init",
19 [MODULE_STATE_GOING
] = "[MODULE_STATE_GOING] Going away",
20 [MODULE_STATE_UNFORMED
] = "[MODULE_STATE_UNFORMED] Still setting it up",
23 static void callback_info(const char *callback
, struct klp_object
*obj
)
26 pr_info("%s: %s -> %s\n", callback
, obj
->mod
->name
,
27 module_state
[obj
->mod
->state
]);
29 pr_info("%s: vmlinux\n", callback
);
32 static struct klp_patch patch
;
34 static int allocate_loglevel_state(void)
36 struct klp_state
*loglevel_state
;
38 loglevel_state
= klp_get_state(&patch
, CONSOLE_LOGLEVEL_STATE
);
42 loglevel_state
->data
= kzalloc(sizeof(console_loglevel
), GFP_KERNEL
);
43 if (!loglevel_state
->data
)
46 pr_info("%s: allocating space to store console_loglevel\n",
51 static void fix_console_loglevel(void)
53 struct klp_state
*loglevel_state
;
55 loglevel_state
= klp_get_state(&patch
, CONSOLE_LOGLEVEL_STATE
);
59 pr_info("%s: fixing console_loglevel\n", __func__
);
60 *(int *)loglevel_state
->data
= console_loglevel
;
61 console_loglevel
= CONSOLE_LOGLEVEL_MOTORMOUTH
;
64 static void restore_console_loglevel(void)
66 struct klp_state
*loglevel_state
;
68 loglevel_state
= klp_get_state(&patch
, CONSOLE_LOGLEVEL_STATE
);
72 pr_info("%s: restoring console_loglevel\n", __func__
);
73 console_loglevel
= *(int *)loglevel_state
->data
;
76 static void free_loglevel_state(void)
78 struct klp_state
*loglevel_state
;
80 loglevel_state
= klp_get_state(&patch
, CONSOLE_LOGLEVEL_STATE
);
84 pr_info("%s: freeing space for the stored console_loglevel\n",
86 kfree(loglevel_state
->data
);
89 /* Executed on object patching (ie, patch enablement) */
90 static int pre_patch_callback(struct klp_object
*obj
)
92 callback_info(__func__
, obj
);
93 return allocate_loglevel_state();
96 /* Executed on object unpatching (ie, patch disablement) */
97 static void post_patch_callback(struct klp_object
*obj
)
99 callback_info(__func__
, obj
);
100 fix_console_loglevel();
103 /* Executed on object unpatching (ie, patch disablement) */
104 static void pre_unpatch_callback(struct klp_object
*obj
)
106 callback_info(__func__
, obj
);
107 restore_console_loglevel();
110 /* Executed on object unpatching (ie, patch disablement) */
111 static void post_unpatch_callback(struct klp_object
*obj
)
113 callback_info(__func__
, obj
);
114 free_loglevel_state();
117 static struct klp_func no_funcs
[] = {
121 static struct klp_object objs
[] = {
123 .name
= NULL
, /* vmlinux */
126 .pre_patch
= pre_patch_callback
,
127 .post_patch
= post_patch_callback
,
128 .pre_unpatch
= pre_unpatch_callback
,
129 .post_unpatch
= post_unpatch_callback
,
134 static struct klp_state states
[] = {
136 .id
= CONSOLE_LOGLEVEL_STATE
,
137 .version
= CONSOLE_LOGLEVEL_STATE_VERSION
,
141 static struct klp_patch patch
= {
148 static int test_klp_callbacks_demo_init(void)
150 return klp_enable_patch(&patch
);
153 static void test_klp_callbacks_demo_exit(void)
157 module_init(test_klp_callbacks_demo_init
);
158 module_exit(test_klp_callbacks_demo_exit
);
159 MODULE_LICENSE("GPL");
160 MODULE_INFO(livepatch
, "Y");
161 MODULE_AUTHOR("Petr Mladek <pmladek@suse.com>");
162 MODULE_DESCRIPTION("Livepatch test: system state modification");