blob: 5417573e80af5ae2a28a98374cbdf8e723bd7876 [file] [log] [blame]
Joe Lawrencea2818ee2019-01-09 13:43:29 +01001// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
3
4#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/livepatch.h>
9
10static int replace;
11module_param(replace, int, 0644);
12MODULE_PARM_DESC(replace, "replace (default=0)");
13
14static const char *const module_state[] = {
15 [MODULE_STATE_LIVE] = "[MODULE_STATE_LIVE] Normal state",
16 [MODULE_STATE_COMING] = "[MODULE_STATE_COMING] Full formed, running module_init",
17 [MODULE_STATE_GOING] = "[MODULE_STATE_GOING] Going away",
18 [MODULE_STATE_UNFORMED] = "[MODULE_STATE_UNFORMED] Still setting it up",
19};
20
21static void callback_info(const char *callback, struct klp_object *obj)
22{
23 if (obj->mod)
24 pr_info("%s: %s -> %s\n", callback, obj->mod->name,
25 module_state[obj->mod->state]);
26 else
27 pr_info("%s: vmlinux\n", callback);
28}
29
30/* Executed on object patching (ie, patch enablement) */
31static int pre_patch_callback(struct klp_object *obj)
32{
33 callback_info(__func__, obj);
34 return 0;
35}
36
37/* Executed on object unpatching (ie, patch disablement) */
38static void post_patch_callback(struct klp_object *obj)
39{
40 callback_info(__func__, obj);
41}
42
43/* Executed on object unpatching (ie, patch disablement) */
44static void pre_unpatch_callback(struct klp_object *obj)
45{
46 callback_info(__func__, obj);
47}
48
49/* Executed on object unpatching (ie, patch disablement) */
50static void post_unpatch_callback(struct klp_object *obj)
51{
52 callback_info(__func__, obj);
53}
54
55static struct klp_func no_funcs[] = {
56 { }
57};
58
59static struct klp_object objs[] = {
60 {
61 .name = NULL, /* vmlinux */
62 .funcs = no_funcs,
63 .callbacks = {
64 .pre_patch = pre_patch_callback,
65 .post_patch = post_patch_callback,
66 .pre_unpatch = pre_unpatch_callback,
67 .post_unpatch = post_unpatch_callback,
68 },
69 }, { }
70};
71
72static struct klp_patch patch = {
73 .mod = THIS_MODULE,
74 .objs = objs,
75 /* set .replace in the init function below for demo purposes */
76};
77
78static int test_klp_callbacks_demo2_init(void)
79{
80 patch.replace = replace;
81 return klp_enable_patch(&patch);
82}
83
84static void test_klp_callbacks_demo2_exit(void)
85{
86}
87
88module_init(test_klp_callbacks_demo2_init);
89module_exit(test_klp_callbacks_demo2_exit);
90MODULE_LICENSE("GPL");
91MODULE_INFO(livepatch, "Y");
92MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>");
93MODULE_DESCRIPTION("Livepatch test: livepatch demo2");