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