11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2439e7271SJoe Lawrence /*
3439e7271SJoe Lawrence  * Copyright (C) 2017 Joe Lawrence <[email protected]>
4439e7271SJoe Lawrence  */
5439e7271SJoe Lawrence 
6439e7271SJoe Lawrence /*
7439e7271SJoe Lawrence  * livepatch-shadow-mod.c - Shadow variables, buggy module demo
8439e7271SJoe Lawrence  *
9439e7271SJoe Lawrence  * Purpose
10439e7271SJoe Lawrence  * -------
11439e7271SJoe Lawrence  *
12439e7271SJoe Lawrence  * As a demonstration of livepatch shadow variable API, this module
13439e7271SJoe Lawrence  * introduces memory leak behavior that livepatch modules
14439e7271SJoe Lawrence  * livepatch-shadow-fix1.ko and livepatch-shadow-fix2.ko correct and
15439e7271SJoe Lawrence  * enhance.
16439e7271SJoe Lawrence  *
17439e7271SJoe Lawrence  * WARNING - even though the livepatch-shadow-fix modules patch the
18439e7271SJoe Lawrence  * memory leak, please load these modules at your own risk -- some
19439e7271SJoe Lawrence  * amount of memory may leaked before the bug is patched.
20439e7271SJoe Lawrence  *
21439e7271SJoe Lawrence  *
22439e7271SJoe Lawrence  * Usage
23439e7271SJoe Lawrence  * -----
24439e7271SJoe Lawrence  *
25439e7271SJoe Lawrence  * Step 1 - Load the buggy demonstration module:
26439e7271SJoe Lawrence  *
27439e7271SJoe Lawrence  *   insmod samples/livepatch/livepatch-shadow-mod.ko
28439e7271SJoe Lawrence  *
29439e7271SJoe Lawrence  * Watch dmesg output for a few moments to see new dummy being allocated
30439e7271SJoe Lawrence  * and a periodic cleanup check.  (Note: a small amount of memory is
31439e7271SJoe Lawrence  * being leaked.)
32439e7271SJoe Lawrence  *
33439e7271SJoe Lawrence  *
34439e7271SJoe Lawrence  * Step 2 - Load livepatch fix1:
35439e7271SJoe Lawrence  *
36439e7271SJoe Lawrence  *   insmod samples/livepatch/livepatch-shadow-fix1.ko
37439e7271SJoe Lawrence  *
38439e7271SJoe Lawrence  * Continue watching dmesg and note that now livepatch_fix1_dummy_free()
39439e7271SJoe Lawrence  * and livepatch_fix1_dummy_alloc() are logging messages about leaked
40439e7271SJoe Lawrence  * memory and eventually leaks prevented.
41439e7271SJoe Lawrence  *
42439e7271SJoe Lawrence  *
43439e7271SJoe Lawrence  * Step 3 - Load livepatch fix2 (on top of fix1):
44439e7271SJoe Lawrence  *
45439e7271SJoe Lawrence  *   insmod samples/livepatch/livepatch-shadow-fix2.ko
46439e7271SJoe Lawrence  *
47439e7271SJoe Lawrence  * This module extends functionality through shadow variables, as a new
48439e7271SJoe Lawrence  * "check" counter is added to the dummy structure.  Periodic dmesg
49439e7271SJoe Lawrence  * messages will log these as dummies are cleaned up.
50439e7271SJoe Lawrence  *
51439e7271SJoe Lawrence  *
52439e7271SJoe Lawrence  * Step 4 - Cleanup
53439e7271SJoe Lawrence  *
54439e7271SJoe Lawrence  * Unwind the demonstration by disabling the livepatch fix modules, then
55439e7271SJoe Lawrence  * removing them and the demo module:
56439e7271SJoe Lawrence  *
57439e7271SJoe Lawrence  *   echo 0 > /sys/kernel/livepatch/livepatch_shadow_fix2/enabled
58439e7271SJoe Lawrence  *   echo 0 > /sys/kernel/livepatch/livepatch_shadow_fix1/enabled
59439e7271SJoe Lawrence  *   rmmod livepatch-shadow-fix2
60439e7271SJoe Lawrence  *   rmmod livepatch-shadow-fix1
61439e7271SJoe Lawrence  *   rmmod livepatch-shadow-mod
62439e7271SJoe Lawrence  */
63439e7271SJoe Lawrence 
64439e7271SJoe Lawrence 
65439e7271SJoe Lawrence #include <linux/kernel.h>
66439e7271SJoe Lawrence #include <linux/module.h>
67439e7271SJoe Lawrence #include <linux/sched.h>
68439e7271SJoe Lawrence #include <linux/slab.h>
69439e7271SJoe Lawrence #include <linux/stat.h>
70439e7271SJoe Lawrence #include <linux/workqueue.h>
71439e7271SJoe Lawrence 
72439e7271SJoe Lawrence MODULE_LICENSE("GPL");
73439e7271SJoe Lawrence MODULE_AUTHOR("Joe Lawrence <[email protected]>");
74439e7271SJoe Lawrence MODULE_DESCRIPTION("Buggy module for shadow variable demo");
75439e7271SJoe Lawrence 
76439e7271SJoe Lawrence /* Allocate new dummies every second */
77439e7271SJoe Lawrence #define ALLOC_PERIOD	1
78439e7271SJoe Lawrence /* Check for expired dummies after a few new ones have been allocated */
79439e7271SJoe Lawrence #define CLEANUP_PERIOD	(3 * ALLOC_PERIOD)
80439e7271SJoe Lawrence /* Dummies expire after a few cleanup instances */
81439e7271SJoe Lawrence #define EXPIRE_PERIOD	(4 * CLEANUP_PERIOD)
82439e7271SJoe Lawrence 
83439e7271SJoe Lawrence /*
84439e7271SJoe Lawrence  * Keep a list of all the dummies so we can clean up any residual ones
85439e7271SJoe Lawrence  * on module exit
86439e7271SJoe Lawrence  */
87b73d5dc7SNicholas Mc Guire static LIST_HEAD(dummy_list);
88b73d5dc7SNicholas Mc Guire static DEFINE_MUTEX(dummy_list_mutex);
89439e7271SJoe Lawrence 
90439e7271SJoe Lawrence struct dummy {
91439e7271SJoe Lawrence 	struct list_head list;
92439e7271SJoe Lawrence 	unsigned long jiffies_expire;
93439e7271SJoe Lawrence };
94439e7271SJoe Lawrence 
dummy_alloc(void)95b73d5dc7SNicholas Mc Guire static __used noinline struct dummy *dummy_alloc(void)
96439e7271SJoe Lawrence {
97439e7271SJoe Lawrence 	struct dummy *d;
988f6b8866SPetr Mladek 	int *leak;
99439e7271SJoe Lawrence 
100439e7271SJoe Lawrence 	d = kzalloc(sizeof(*d), GFP_KERNEL);
101439e7271SJoe Lawrence 	if (!d)
102439e7271SJoe Lawrence 		return NULL;
103439e7271SJoe Lawrence 
104*6d072c0bSEaswar Hariharan 	d->jiffies_expire = jiffies + secs_to_jiffies(EXPIRE_PERIOD);
105439e7271SJoe Lawrence 
106439e7271SJoe Lawrence 	/* Oops, forgot to save leak! */
1078f6b8866SPetr Mladek 	leak = kzalloc(sizeof(*leak), GFP_KERNEL);
1085f30b2e8SNicholas Mc Guire 	if (!leak) {
1095f30b2e8SNicholas Mc Guire 		kfree(d);
1105f30b2e8SNicholas Mc Guire 		return NULL;
1115f30b2e8SNicholas Mc Guire 	}
112439e7271SJoe Lawrence 
113439e7271SJoe Lawrence 	pr_info("%s: dummy @ %p, expires @ %lx\n",
114439e7271SJoe Lawrence 		__func__, d, d->jiffies_expire);
115439e7271SJoe Lawrence 
116439e7271SJoe Lawrence 	return d;
117439e7271SJoe Lawrence }
118439e7271SJoe Lawrence 
dummy_free(struct dummy * d)119b73d5dc7SNicholas Mc Guire static __used noinline void dummy_free(struct dummy *d)
120439e7271SJoe Lawrence {
121439e7271SJoe Lawrence 	pr_info("%s: dummy @ %p, expired = %lx\n",
122439e7271SJoe Lawrence 		__func__, d, d->jiffies_expire);
123439e7271SJoe Lawrence 
124439e7271SJoe Lawrence 	kfree(d);
125439e7271SJoe Lawrence }
126439e7271SJoe Lawrence 
dummy_check(struct dummy * d,unsigned long jiffies)127b73d5dc7SNicholas Mc Guire static __used noinline bool dummy_check(struct dummy *d,
128b73d5dc7SNicholas Mc Guire 					   unsigned long jiffies)
129439e7271SJoe Lawrence {
130439e7271SJoe Lawrence 	return time_after(jiffies, d->jiffies_expire);
131439e7271SJoe Lawrence }
132439e7271SJoe Lawrence 
133439e7271SJoe Lawrence /*
134439e7271SJoe Lawrence  * alloc_work_func: allocates new dummy structures, allocates additional
135439e7271SJoe Lawrence  *                  memory, aptly named "leak", but doesn't keep
136439e7271SJoe Lawrence  *                  permanent record of it.
137439e7271SJoe Lawrence  */
138439e7271SJoe Lawrence 
139439e7271SJoe Lawrence static void alloc_work_func(struct work_struct *work);
140439e7271SJoe Lawrence static DECLARE_DELAYED_WORK(alloc_dwork, alloc_work_func);
141439e7271SJoe Lawrence 
alloc_work_func(struct work_struct * work)142439e7271SJoe Lawrence static void alloc_work_func(struct work_struct *work)
143439e7271SJoe Lawrence {
144439e7271SJoe Lawrence 	struct dummy *d;
145439e7271SJoe Lawrence 
146439e7271SJoe Lawrence 	d = dummy_alloc();
147439e7271SJoe Lawrence 	if (!d)
148439e7271SJoe Lawrence 		return;
149439e7271SJoe Lawrence 
150439e7271SJoe Lawrence 	mutex_lock(&dummy_list_mutex);
151439e7271SJoe Lawrence 	list_add(&d->list, &dummy_list);
152439e7271SJoe Lawrence 	mutex_unlock(&dummy_list_mutex);
153439e7271SJoe Lawrence 
154*6d072c0bSEaswar Hariharan 	schedule_delayed_work(&alloc_dwork, secs_to_jiffies(ALLOC_PERIOD));
155439e7271SJoe Lawrence }
156439e7271SJoe Lawrence 
157439e7271SJoe Lawrence /*
158439e7271SJoe Lawrence  * cleanup_work_func: frees dummy structures.  Without knownledge of
159439e7271SJoe Lawrence  *                    "leak", it leaks the additional memory that
160439e7271SJoe Lawrence  *                    alloc_work_func created.
161439e7271SJoe Lawrence  */
162439e7271SJoe Lawrence 
163439e7271SJoe Lawrence static void cleanup_work_func(struct work_struct *work);
164439e7271SJoe Lawrence static DECLARE_DELAYED_WORK(cleanup_dwork, cleanup_work_func);
165439e7271SJoe Lawrence 
cleanup_work_func(struct work_struct * work)166439e7271SJoe Lawrence static void cleanup_work_func(struct work_struct *work)
167439e7271SJoe Lawrence {
168439e7271SJoe Lawrence 	struct dummy *d, *tmp;
169439e7271SJoe Lawrence 	unsigned long j;
170439e7271SJoe Lawrence 
171439e7271SJoe Lawrence 	j = jiffies;
172439e7271SJoe Lawrence 	pr_info("%s: jiffies = %lx\n", __func__, j);
173439e7271SJoe Lawrence 
174439e7271SJoe Lawrence 	mutex_lock(&dummy_list_mutex);
175439e7271SJoe Lawrence 	list_for_each_entry_safe(d, tmp, &dummy_list, list) {
176439e7271SJoe Lawrence 
177439e7271SJoe Lawrence 		/* Kick out and free any expired dummies */
178439e7271SJoe Lawrence 		if (dummy_check(d, j)) {
179439e7271SJoe Lawrence 			list_del(&d->list);
180439e7271SJoe Lawrence 			dummy_free(d);
181439e7271SJoe Lawrence 		}
182439e7271SJoe Lawrence 	}
183439e7271SJoe Lawrence 	mutex_unlock(&dummy_list_mutex);
184439e7271SJoe Lawrence 
185*6d072c0bSEaswar Hariharan 	schedule_delayed_work(&cleanup_dwork, secs_to_jiffies(CLEANUP_PERIOD));
186439e7271SJoe Lawrence }
187439e7271SJoe Lawrence 
livepatch_shadow_mod_init(void)188439e7271SJoe Lawrence static int livepatch_shadow_mod_init(void)
189439e7271SJoe Lawrence {
190*6d072c0bSEaswar Hariharan 	schedule_delayed_work(&alloc_dwork, secs_to_jiffies(ALLOC_PERIOD));
191*6d072c0bSEaswar Hariharan 	schedule_delayed_work(&cleanup_dwork, secs_to_jiffies(CLEANUP_PERIOD));
192439e7271SJoe Lawrence 
193439e7271SJoe Lawrence 	return 0;
194439e7271SJoe Lawrence }
195439e7271SJoe Lawrence 
livepatch_shadow_mod_exit(void)196439e7271SJoe Lawrence static void livepatch_shadow_mod_exit(void)
197439e7271SJoe Lawrence {
198439e7271SJoe Lawrence 	struct dummy *d, *tmp;
199439e7271SJoe Lawrence 
200439e7271SJoe Lawrence 	/* Wait for any dummies at work */
201439e7271SJoe Lawrence 	cancel_delayed_work_sync(&alloc_dwork);
202439e7271SJoe Lawrence 	cancel_delayed_work_sync(&cleanup_dwork);
203439e7271SJoe Lawrence 
204439e7271SJoe Lawrence 	/* Cleanup residual dummies */
205439e7271SJoe Lawrence 	list_for_each_entry_safe(d, tmp, &dummy_list, list) {
206439e7271SJoe Lawrence 		list_del(&d->list);
207439e7271SJoe Lawrence 		dummy_free(d);
208439e7271SJoe Lawrence 	}
209439e7271SJoe Lawrence }
210439e7271SJoe Lawrence 
211439e7271SJoe Lawrence module_init(livepatch_shadow_mod_init);
212439e7271SJoe Lawrence module_exit(livepatch_shadow_mod_exit);
213