1*1ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 293862e38SJoe Lawrence /* 393862e38SJoe Lawrence * Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com> 493862e38SJoe Lawrence */ 593862e38SJoe Lawrence 693862e38SJoe Lawrence /* 793862e38SJoe Lawrence * livepatch-callbacks-demo.c - (un)patching callbacks livepatch demo 893862e38SJoe Lawrence * 993862e38SJoe Lawrence * 1093862e38SJoe Lawrence * Purpose 1193862e38SJoe Lawrence * ------- 1293862e38SJoe Lawrence * 1393862e38SJoe Lawrence * Demonstration of registering livepatch (un)patching callbacks. 1493862e38SJoe Lawrence * 1593862e38SJoe Lawrence * 1693862e38SJoe Lawrence * Usage 1793862e38SJoe Lawrence * ----- 1893862e38SJoe Lawrence * 1993862e38SJoe Lawrence * Step 1 - load the simple module 2093862e38SJoe Lawrence * 2193862e38SJoe Lawrence * insmod samples/livepatch/livepatch-callbacks-mod.ko 2293862e38SJoe Lawrence * 2393862e38SJoe Lawrence * 2493862e38SJoe Lawrence * Step 2 - load the demonstration livepatch (with callbacks) 2593862e38SJoe Lawrence * 2693862e38SJoe Lawrence * insmod samples/livepatch/livepatch-callbacks-demo.ko 2793862e38SJoe Lawrence * 2893862e38SJoe Lawrence * 2993862e38SJoe Lawrence * Step 3 - cleanup 3093862e38SJoe Lawrence * 3193862e38SJoe Lawrence * echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled 3293862e38SJoe Lawrence * rmmod livepatch_callbacks_demo 3393862e38SJoe Lawrence * rmmod livepatch_callbacks_mod 3493862e38SJoe Lawrence * 3593862e38SJoe Lawrence * Watch dmesg output to see livepatch enablement, callback execution 3693862e38SJoe Lawrence * and patching operations for both vmlinux and module targets. 3793862e38SJoe Lawrence * 3893862e38SJoe Lawrence * NOTE: swap the insmod order of livepatch-callbacks-mod.ko and 3993862e38SJoe Lawrence * livepatch-callbacks-demo.ko to observe what happens when a 4093862e38SJoe Lawrence * target module is loaded after a livepatch with callbacks. 4193862e38SJoe Lawrence * 4293862e38SJoe Lawrence * NOTE: 'pre_patch_ret' is a module parameter that sets the pre-patch 4393862e38SJoe Lawrence * callback return status. Try setting up a non-zero status 4493862e38SJoe Lawrence * such as -19 (-ENODEV): 4593862e38SJoe Lawrence * 4693862e38SJoe Lawrence * # Load demo livepatch, vmlinux is patched 4793862e38SJoe Lawrence * insmod samples/livepatch/livepatch-callbacks-demo.ko 4893862e38SJoe Lawrence * 4993862e38SJoe Lawrence * # Setup next pre-patch callback to return -ENODEV 5093862e38SJoe Lawrence * echo -19 > /sys/module/livepatch_callbacks_demo/parameters/pre_patch_ret 5193862e38SJoe Lawrence * 5293862e38SJoe Lawrence * # Module loader refuses to load the target module 5393862e38SJoe Lawrence * insmod samples/livepatch/livepatch-callbacks-mod.ko 5493862e38SJoe Lawrence * insmod: ERROR: could not insert module samples/livepatch/livepatch-callbacks-mod.ko: No such device 5593862e38SJoe Lawrence * 5693862e38SJoe Lawrence * NOTE: There is a second target module, 5793862e38SJoe Lawrence * livepatch-callbacks-busymod.ko, available for experimenting 5893862e38SJoe Lawrence * with livepatch (un)patch callbacks. This module contains 5993862e38SJoe Lawrence * a 'sleep_secs' parameter that parks the module on one of the 6093862e38SJoe Lawrence * functions that the livepatch demo module wants to patch. 6193862e38SJoe Lawrence * Modifying this value and tweaking the order of module loads can 6293862e38SJoe Lawrence * effectively demonstrate stalled patch transitions: 6393862e38SJoe Lawrence * 6493862e38SJoe Lawrence * # Load a target module, let it park on 'busymod_work_func' for 6593862e38SJoe Lawrence * # thirty seconds 6693862e38SJoe Lawrence * insmod samples/livepatch/livepatch-callbacks-busymod.ko sleep_secs=30 6793862e38SJoe Lawrence * 6893862e38SJoe Lawrence * # Meanwhile load the livepatch 6993862e38SJoe Lawrence * insmod samples/livepatch/livepatch-callbacks-demo.ko 7093862e38SJoe Lawrence * 7193862e38SJoe Lawrence * # ... then load and unload another target module while the 7293862e38SJoe Lawrence * # transition is in progress 7393862e38SJoe Lawrence * insmod samples/livepatch/livepatch-callbacks-mod.ko 7493862e38SJoe Lawrence * rmmod samples/livepatch/livepatch-callbacks-mod.ko 7593862e38SJoe Lawrence * 7693862e38SJoe Lawrence * # Finally cleanup 7793862e38SJoe Lawrence * echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled 7893862e38SJoe Lawrence * rmmod samples/livepatch/livepatch-callbacks-demo.ko 7993862e38SJoe Lawrence */ 8093862e38SJoe Lawrence 8193862e38SJoe Lawrence #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8293862e38SJoe Lawrence 8393862e38SJoe Lawrence #include <linux/module.h> 8493862e38SJoe Lawrence #include <linux/kernel.h> 8593862e38SJoe Lawrence #include <linux/livepatch.h> 8693862e38SJoe Lawrence 8793862e38SJoe Lawrence static int pre_patch_ret; 8893862e38SJoe Lawrence module_param(pre_patch_ret, int, 0644); 8993862e38SJoe Lawrence MODULE_PARM_DESC(pre_patch_ret, "pre_patch_ret (default=0)"); 9093862e38SJoe Lawrence 9193862e38SJoe Lawrence static const char *const module_state[] = { 9293862e38SJoe Lawrence [MODULE_STATE_LIVE] = "[MODULE_STATE_LIVE] Normal state", 9393862e38SJoe Lawrence [MODULE_STATE_COMING] = "[MODULE_STATE_COMING] Full formed, running module_init", 9493862e38SJoe Lawrence [MODULE_STATE_GOING] = "[MODULE_STATE_GOING] Going away", 9593862e38SJoe Lawrence [MODULE_STATE_UNFORMED] = "[MODULE_STATE_UNFORMED] Still setting it up", 9693862e38SJoe Lawrence }; 9793862e38SJoe Lawrence 9893862e38SJoe Lawrence static void callback_info(const char *callback, struct klp_object *obj) 9993862e38SJoe Lawrence { 10093862e38SJoe Lawrence if (obj->mod) 10193862e38SJoe Lawrence pr_info("%s: %s -> %s\n", callback, obj->mod->name, 10293862e38SJoe Lawrence module_state[obj->mod->state]); 10393862e38SJoe Lawrence else 10493862e38SJoe Lawrence pr_info("%s: vmlinux\n", callback); 10593862e38SJoe Lawrence } 10693862e38SJoe Lawrence 10793862e38SJoe Lawrence /* Executed on object patching (ie, patch enablement) */ 10893862e38SJoe Lawrence static int pre_patch_callback(struct klp_object *obj) 10993862e38SJoe Lawrence { 11093862e38SJoe Lawrence callback_info(__func__, obj); 11193862e38SJoe Lawrence return pre_patch_ret; 11293862e38SJoe Lawrence } 11393862e38SJoe Lawrence 11493862e38SJoe Lawrence /* Executed on object unpatching (ie, patch disablement) */ 11593862e38SJoe Lawrence static void post_patch_callback(struct klp_object *obj) 11693862e38SJoe Lawrence { 11793862e38SJoe Lawrence callback_info(__func__, obj); 11893862e38SJoe Lawrence } 11993862e38SJoe Lawrence 12093862e38SJoe Lawrence /* Executed on object unpatching (ie, patch disablement) */ 12193862e38SJoe Lawrence static void pre_unpatch_callback(struct klp_object *obj) 12293862e38SJoe Lawrence { 12393862e38SJoe Lawrence callback_info(__func__, obj); 12493862e38SJoe Lawrence } 12593862e38SJoe Lawrence 12693862e38SJoe Lawrence /* Executed on object unpatching (ie, patch disablement) */ 12793862e38SJoe Lawrence static void post_unpatch_callback(struct klp_object *obj) 12893862e38SJoe Lawrence { 12993862e38SJoe Lawrence callback_info(__func__, obj); 13093862e38SJoe Lawrence } 13193862e38SJoe Lawrence 13293862e38SJoe Lawrence static void patched_work_func(struct work_struct *work) 13393862e38SJoe Lawrence { 13493862e38SJoe Lawrence pr_info("%s\n", __func__); 13593862e38SJoe Lawrence } 13693862e38SJoe Lawrence 13793862e38SJoe Lawrence static struct klp_func no_funcs[] = { 13893862e38SJoe Lawrence { } 13993862e38SJoe Lawrence }; 14093862e38SJoe Lawrence 14193862e38SJoe Lawrence static struct klp_func busymod_funcs[] = { 14293862e38SJoe Lawrence { 14393862e38SJoe Lawrence .old_name = "busymod_work_func", 14493862e38SJoe Lawrence .new_func = patched_work_func, 14593862e38SJoe Lawrence }, { } 14693862e38SJoe Lawrence }; 14793862e38SJoe Lawrence 14893862e38SJoe Lawrence static struct klp_object objs[] = { 14993862e38SJoe Lawrence { 15093862e38SJoe Lawrence .name = NULL, /* vmlinux */ 15193862e38SJoe Lawrence .funcs = no_funcs, 15293862e38SJoe Lawrence .callbacks = { 15393862e38SJoe Lawrence .pre_patch = pre_patch_callback, 15493862e38SJoe Lawrence .post_patch = post_patch_callback, 15593862e38SJoe Lawrence .pre_unpatch = pre_unpatch_callback, 15693862e38SJoe Lawrence .post_unpatch = post_unpatch_callback, 15793862e38SJoe Lawrence }, 15893862e38SJoe Lawrence }, { 15993862e38SJoe Lawrence .name = "livepatch_callbacks_mod", 16093862e38SJoe Lawrence .funcs = no_funcs, 16193862e38SJoe Lawrence .callbacks = { 16293862e38SJoe Lawrence .pre_patch = pre_patch_callback, 16393862e38SJoe Lawrence .post_patch = post_patch_callback, 16493862e38SJoe Lawrence .pre_unpatch = pre_unpatch_callback, 16593862e38SJoe Lawrence .post_unpatch = post_unpatch_callback, 16693862e38SJoe Lawrence }, 16793862e38SJoe Lawrence }, { 16893862e38SJoe Lawrence .name = "livepatch_callbacks_busymod", 16993862e38SJoe Lawrence .funcs = busymod_funcs, 17093862e38SJoe Lawrence .callbacks = { 17193862e38SJoe Lawrence .pre_patch = pre_patch_callback, 17293862e38SJoe Lawrence .post_patch = post_patch_callback, 17393862e38SJoe Lawrence .pre_unpatch = pre_unpatch_callback, 17493862e38SJoe Lawrence .post_unpatch = post_unpatch_callback, 17593862e38SJoe Lawrence }, 17693862e38SJoe Lawrence }, { } 17793862e38SJoe Lawrence }; 17893862e38SJoe Lawrence 17993862e38SJoe Lawrence static struct klp_patch patch = { 18093862e38SJoe Lawrence .mod = THIS_MODULE, 18193862e38SJoe Lawrence .objs = objs, 18293862e38SJoe Lawrence }; 18393862e38SJoe Lawrence 18493862e38SJoe Lawrence static int livepatch_callbacks_demo_init(void) 18593862e38SJoe Lawrence { 186958ef1e3SPetr Mladek return klp_enable_patch(&patch); 18793862e38SJoe Lawrence } 18893862e38SJoe Lawrence 18993862e38SJoe Lawrence static void livepatch_callbacks_demo_exit(void) 19093862e38SJoe Lawrence { 19193862e38SJoe Lawrence } 19293862e38SJoe Lawrence 19393862e38SJoe Lawrence module_init(livepatch_callbacks_demo_init); 19493862e38SJoe Lawrence module_exit(livepatch_callbacks_demo_exit); 19593862e38SJoe Lawrence MODULE_LICENSE("GPL"); 19693862e38SJoe Lawrence MODULE_INFO(livepatch, "Y"); 197