Merge tag 'tty-4.16-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
[linux/fpc-iii.git] / samples / livepatch / livepatch-shadow-fix1.c
blob830c55514f9f555055705eceeb50daf7daca20d5
1 /*
2 * Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 * livepatch-shadow-fix1.c - Shadow variables, livepatch demo
21 * Purpose
22 * -------
24 * Fixes the memory leak introduced in livepatch-shadow-mod through the
25 * use of a shadow variable. This fix demonstrates the "extending" of
26 * short-lived data structures by patching its allocation and release
27 * functions.
30 * Usage
31 * -----
33 * This module is not intended to be standalone. See the "Usage"
34 * section of livepatch-shadow-mod.c.
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/livepatch.h>
42 #include <linux/slab.h>
44 /* Shadow variable enums */
45 #define SV_LEAK 1
47 /* Allocate new dummies every second */
48 #define ALLOC_PERIOD 1
49 /* Check for expired dummies after a few new ones have been allocated */
50 #define CLEANUP_PERIOD (3 * ALLOC_PERIOD)
51 /* Dummies expire after a few cleanup instances */
52 #define EXPIRE_PERIOD (4 * CLEANUP_PERIOD)
54 struct dummy {
55 struct list_head list;
56 unsigned long jiffies_expire;
59 struct dummy *livepatch_fix1_dummy_alloc(void)
61 struct dummy *d;
62 void *leak;
64 d = kzalloc(sizeof(*d), GFP_KERNEL);
65 if (!d)
66 return NULL;
68 d->jiffies_expire = jiffies +
69 msecs_to_jiffies(1000 * EXPIRE_PERIOD);
72 * Patch: save the extra memory location into a SV_LEAK shadow
73 * variable. A patched dummy_free routine can later fetch this
74 * pointer to handle resource release.
76 leak = kzalloc(sizeof(int), GFP_KERNEL);
77 klp_shadow_alloc(d, SV_LEAK, &leak, sizeof(leak), GFP_KERNEL);
79 pr_info("%s: dummy @ %p, expires @ %lx\n",
80 __func__, d, d->jiffies_expire);
82 return d;
85 void livepatch_fix1_dummy_free(struct dummy *d)
87 void **shadow_leak, *leak;
90 * Patch: fetch the saved SV_LEAK shadow variable, detach and
91 * free it. Note: handle cases where this shadow variable does
92 * not exist (ie, dummy structures allocated before this livepatch
93 * was loaded.)
95 shadow_leak = klp_shadow_get(d, SV_LEAK);
96 if (shadow_leak) {
97 leak = *shadow_leak;
98 klp_shadow_free(d, SV_LEAK);
99 kfree(leak);
100 pr_info("%s: dummy @ %p, prevented leak @ %p\n",
101 __func__, d, leak);
102 } else {
103 pr_info("%s: dummy @ %p leaked!\n", __func__, d);
106 kfree(d);
109 static struct klp_func funcs[] = {
111 .old_name = "dummy_alloc",
112 .new_func = livepatch_fix1_dummy_alloc,
115 .old_name = "dummy_free",
116 .new_func = livepatch_fix1_dummy_free,
117 }, { }
120 static struct klp_object objs[] = {
122 .name = "livepatch_shadow_mod",
123 .funcs = funcs,
124 }, { }
127 static struct klp_patch patch = {
128 .mod = THIS_MODULE,
129 .objs = objs,
132 static int livepatch_shadow_fix1_init(void)
134 int ret;
136 ret = klp_register_patch(&patch);
137 if (ret)
138 return ret;
139 ret = klp_enable_patch(&patch);
140 if (ret) {
141 WARN_ON(klp_unregister_patch(&patch));
142 return ret;
144 return 0;
147 static void livepatch_shadow_fix1_exit(void)
149 /* Cleanup any existing SV_LEAK shadow variables */
150 klp_shadow_free_all(SV_LEAK);
152 WARN_ON(klp_unregister_patch(&patch));
155 module_init(livepatch_shadow_fix1_init);
156 module_exit(livepatch_shadow_fix1_exit);
157 MODULE_LICENSE("GPL");
158 MODULE_INFO(livepatch, "Y");