1*a2818ee4SJoe Lawrence // SPDX-License-Identifier: GPL-2.0 2*a2818ee4SJoe Lawrence // Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com> 3*a2818ee4SJoe Lawrence 4*a2818ee4SJoe Lawrence #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 5*a2818ee4SJoe Lawrence 6*a2818ee4SJoe Lawrence #include <linux/module.h> 7*a2818ee4SJoe Lawrence #include <linux/kernel.h> 8*a2818ee4SJoe Lawrence #include <linux/livepatch.h> 9*a2818ee4SJoe Lawrence 10*a2818ee4SJoe Lawrence static int pre_patch_ret; 11*a2818ee4SJoe Lawrence module_param(pre_patch_ret, int, 0644); 12*a2818ee4SJoe Lawrence MODULE_PARM_DESC(pre_patch_ret, "pre_patch_ret (default=0)"); 13*a2818ee4SJoe Lawrence 14*a2818ee4SJoe Lawrence static const char *const module_state[] = { 15*a2818ee4SJoe Lawrence [MODULE_STATE_LIVE] = "[MODULE_STATE_LIVE] Normal state", 16*a2818ee4SJoe Lawrence [MODULE_STATE_COMING] = "[MODULE_STATE_COMING] Full formed, running module_init", 17*a2818ee4SJoe Lawrence [MODULE_STATE_GOING] = "[MODULE_STATE_GOING] Going away", 18*a2818ee4SJoe Lawrence [MODULE_STATE_UNFORMED] = "[MODULE_STATE_UNFORMED] Still setting it up", 19*a2818ee4SJoe Lawrence }; 20*a2818ee4SJoe Lawrence 21*a2818ee4SJoe Lawrence static void callback_info(const char *callback, struct klp_object *obj) 22*a2818ee4SJoe Lawrence { 23*a2818ee4SJoe Lawrence if (obj->mod) 24*a2818ee4SJoe Lawrence pr_info("%s: %s -> %s\n", callback, obj->mod->name, 25*a2818ee4SJoe Lawrence module_state[obj->mod->state]); 26*a2818ee4SJoe Lawrence else 27*a2818ee4SJoe Lawrence pr_info("%s: vmlinux\n", callback); 28*a2818ee4SJoe Lawrence } 29*a2818ee4SJoe Lawrence 30*a2818ee4SJoe Lawrence /* Executed on object patching (ie, patch enablement) */ 31*a2818ee4SJoe Lawrence static int pre_patch_callback(struct klp_object *obj) 32*a2818ee4SJoe Lawrence { 33*a2818ee4SJoe Lawrence callback_info(__func__, obj); 34*a2818ee4SJoe Lawrence return pre_patch_ret; 35*a2818ee4SJoe Lawrence } 36*a2818ee4SJoe Lawrence 37*a2818ee4SJoe Lawrence /* Executed on object unpatching (ie, patch disablement) */ 38*a2818ee4SJoe Lawrence static void post_patch_callback(struct klp_object *obj) 39*a2818ee4SJoe Lawrence { 40*a2818ee4SJoe Lawrence callback_info(__func__, obj); 41*a2818ee4SJoe Lawrence } 42*a2818ee4SJoe Lawrence 43*a2818ee4SJoe Lawrence /* Executed on object unpatching (ie, patch disablement) */ 44*a2818ee4SJoe Lawrence static void pre_unpatch_callback(struct klp_object *obj) 45*a2818ee4SJoe Lawrence { 46*a2818ee4SJoe Lawrence callback_info(__func__, obj); 47*a2818ee4SJoe Lawrence } 48*a2818ee4SJoe Lawrence 49*a2818ee4SJoe Lawrence /* Executed on object unpatching (ie, patch disablement) */ 50*a2818ee4SJoe Lawrence static void post_unpatch_callback(struct klp_object *obj) 51*a2818ee4SJoe Lawrence { 52*a2818ee4SJoe Lawrence callback_info(__func__, obj); 53*a2818ee4SJoe Lawrence } 54*a2818ee4SJoe Lawrence 55*a2818ee4SJoe Lawrence static void patched_work_func(struct work_struct *work) 56*a2818ee4SJoe Lawrence { 57*a2818ee4SJoe Lawrence pr_info("%s\n", __func__); 58*a2818ee4SJoe Lawrence } 59*a2818ee4SJoe Lawrence 60*a2818ee4SJoe Lawrence static struct klp_func no_funcs[] = { 61*a2818ee4SJoe Lawrence {} 62*a2818ee4SJoe Lawrence }; 63*a2818ee4SJoe Lawrence 64*a2818ee4SJoe Lawrence static struct klp_func busymod_funcs[] = { 65*a2818ee4SJoe Lawrence { 66*a2818ee4SJoe Lawrence .old_name = "busymod_work_func", 67*a2818ee4SJoe Lawrence .new_func = patched_work_func, 68*a2818ee4SJoe Lawrence }, {} 69*a2818ee4SJoe Lawrence }; 70*a2818ee4SJoe Lawrence 71*a2818ee4SJoe Lawrence static struct klp_object objs[] = { 72*a2818ee4SJoe Lawrence { 73*a2818ee4SJoe Lawrence .name = NULL, /* vmlinux */ 74*a2818ee4SJoe Lawrence .funcs = no_funcs, 75*a2818ee4SJoe Lawrence .callbacks = { 76*a2818ee4SJoe Lawrence .pre_patch = pre_patch_callback, 77*a2818ee4SJoe Lawrence .post_patch = post_patch_callback, 78*a2818ee4SJoe Lawrence .pre_unpatch = pre_unpatch_callback, 79*a2818ee4SJoe Lawrence .post_unpatch = post_unpatch_callback, 80*a2818ee4SJoe Lawrence }, 81*a2818ee4SJoe Lawrence }, { 82*a2818ee4SJoe Lawrence .name = "test_klp_callbacks_mod", 83*a2818ee4SJoe Lawrence .funcs = no_funcs, 84*a2818ee4SJoe Lawrence .callbacks = { 85*a2818ee4SJoe Lawrence .pre_patch = pre_patch_callback, 86*a2818ee4SJoe Lawrence .post_patch = post_patch_callback, 87*a2818ee4SJoe Lawrence .pre_unpatch = pre_unpatch_callback, 88*a2818ee4SJoe Lawrence .post_unpatch = post_unpatch_callback, 89*a2818ee4SJoe Lawrence }, 90*a2818ee4SJoe Lawrence }, { 91*a2818ee4SJoe Lawrence .name = "test_klp_callbacks_busy", 92*a2818ee4SJoe Lawrence .funcs = busymod_funcs, 93*a2818ee4SJoe Lawrence .callbacks = { 94*a2818ee4SJoe Lawrence .pre_patch = pre_patch_callback, 95*a2818ee4SJoe Lawrence .post_patch = post_patch_callback, 96*a2818ee4SJoe Lawrence .pre_unpatch = pre_unpatch_callback, 97*a2818ee4SJoe Lawrence .post_unpatch = post_unpatch_callback, 98*a2818ee4SJoe Lawrence }, 99*a2818ee4SJoe Lawrence }, { } 100*a2818ee4SJoe Lawrence }; 101*a2818ee4SJoe Lawrence 102*a2818ee4SJoe Lawrence static struct klp_patch patch = { 103*a2818ee4SJoe Lawrence .mod = THIS_MODULE, 104*a2818ee4SJoe Lawrence .objs = objs, 105*a2818ee4SJoe Lawrence }; 106*a2818ee4SJoe Lawrence 107*a2818ee4SJoe Lawrence static int test_klp_callbacks_demo_init(void) 108*a2818ee4SJoe Lawrence { 109*a2818ee4SJoe Lawrence return klp_enable_patch(&patch); 110*a2818ee4SJoe Lawrence } 111*a2818ee4SJoe Lawrence 112*a2818ee4SJoe Lawrence static void test_klp_callbacks_demo_exit(void) 113*a2818ee4SJoe Lawrence { 114*a2818ee4SJoe Lawrence } 115*a2818ee4SJoe Lawrence 116*a2818ee4SJoe Lawrence module_init(test_klp_callbacks_demo_init); 117*a2818ee4SJoe Lawrence module_exit(test_klp_callbacks_demo_exit); 118*a2818ee4SJoe Lawrence MODULE_LICENSE("GPL"); 119*a2818ee4SJoe Lawrence MODULE_INFO(livepatch, "Y"); 120*a2818ee4SJoe Lawrence MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>"); 121*a2818ee4SJoe Lawrence MODULE_DESCRIPTION("Livepatch test: livepatch demo"); 122