1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com>
7 * livepatch-shadow-fix2.c - Shadow variables, livepatch demo
12 * Adds functionality to livepatch-shadow-mod's in-flight data
13 * structures through a shadow variable. The livepatch patches a
14 * routine that periodically inspects data structures, incrementing a
15 * per-data-structure counter, creating the counter if needed.
21 * This module is not intended to be standalone. See the "Usage"
22 * section of livepatch-shadow-mod.c.
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/livepatch.h>
30 #include <linux/slab.h>
32 /* Shadow variable enums */
37 struct list_head list
;
38 unsigned long jiffies_expire
;
41 static bool livepatch_fix2_dummy_check(struct dummy
*d
, unsigned long jiffies
)
46 * Patch: handle in-flight dummy structures, if they do not
47 * already have a SV_COUNTER shadow variable, then attach a
50 shadow_count
= klp_shadow_get_or_alloc(d
, SV_COUNTER
,
51 sizeof(*shadow_count
), GFP_NOWAIT
,
56 return time_after(jiffies
, d
->jiffies_expire
);
59 static void livepatch_fix2_dummy_leak_dtor(void *obj
, void *shadow_data
)
62 int **shadow_leak
= shadow_data
;
65 pr_info("%s: dummy @ %p, prevented leak @ %p\n",
66 __func__
, d
, *shadow_leak
);
69 static void livepatch_fix2_dummy_free(struct dummy
*d
)
74 /* Patch: copy the memory leak patch from the fix1 module. */
75 shadow_leak
= klp_shadow_get(d
, SV_LEAK
);
77 klp_shadow_free(d
, SV_LEAK
, livepatch_fix2_dummy_leak_dtor
);
79 pr_info("%s: dummy @ %p leaked!\n", __func__
, d
);
82 * Patch: fetch the SV_COUNTER shadow variable and display
83 * the final count. Detach the shadow variable.
85 shadow_count
= klp_shadow_get(d
, SV_COUNTER
);
87 pr_info("%s: dummy @ %p, check counter = %d\n",
88 __func__
, d
, *shadow_count
);
89 klp_shadow_free(d
, SV_COUNTER
, NULL
);
95 static struct klp_func funcs
[] = {
97 .old_name
= "dummy_check",
98 .new_func
= livepatch_fix2_dummy_check
,
101 .old_name
= "dummy_free",
102 .new_func
= livepatch_fix2_dummy_free
,
106 static struct klp_object objs
[] = {
108 .name
= "livepatch_shadow_mod",
113 static struct klp_patch patch
= {
118 static int livepatch_shadow_fix2_init(void)
120 return klp_enable_patch(&patch
);
123 static void livepatch_shadow_fix2_exit(void)
125 /* Cleanup any existing SV_COUNTER shadow variables */
126 klp_shadow_free_all(SV_COUNTER
, NULL
);
129 module_init(livepatch_shadow_fix2_init
);
130 module_exit(livepatch_shadow_fix2_exit
);
131 MODULE_LICENSE("GPL");
132 MODULE_INFO(livepatch
, "Y");