livepatch-shadow-mod.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com>
  3. *
  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.
  8. *
  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.
  13. *
  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/>.
  16. */
  17. /*
  18. * livepatch-shadow-mod.c - Shadow variables, buggy module demo
  19. *
  20. * Purpose
  21. * -------
  22. *
  23. * As a demonstration of livepatch shadow variable API, this module
  24. * introduces memory leak behavior that livepatch modules
  25. * livepatch-shadow-fix1.ko and livepatch-shadow-fix2.ko correct and
  26. * enhance.
  27. *
  28. * WARNING - even though the livepatch-shadow-fix modules patch the
  29. * memory leak, please load these modules at your own risk -- some
  30. * amount of memory may leaked before the bug is patched.
  31. *
  32. *
  33. * Usage
  34. * -----
  35. *
  36. * Step 1 - Load the buggy demonstration module:
  37. *
  38. * insmod samples/livepatch/livepatch-shadow-mod.ko
  39. *
  40. * Watch dmesg output for a few moments to see new dummy being allocated
  41. * and a periodic cleanup check. (Note: a small amount of memory is
  42. * being leaked.)
  43. *
  44. *
  45. * Step 2 - Load livepatch fix1:
  46. *
  47. * insmod samples/livepatch/livepatch-shadow-fix1.ko
  48. *
  49. * Continue watching dmesg and note that now livepatch_fix1_dummy_free()
  50. * and livepatch_fix1_dummy_alloc() are logging messages about leaked
  51. * memory and eventually leaks prevented.
  52. *
  53. *
  54. * Step 3 - Load livepatch fix2 (on top of fix1):
  55. *
  56. * insmod samples/livepatch/livepatch-shadow-fix2.ko
  57. *
  58. * This module extends functionality through shadow variables, as a new
  59. * "check" counter is added to the dummy structure. Periodic dmesg
  60. * messages will log these as dummies are cleaned up.
  61. *
  62. *
  63. * Step 4 - Cleanup
  64. *
  65. * Unwind the demonstration by disabling the livepatch fix modules, then
  66. * removing them and the demo module:
  67. *
  68. * echo 0 > /sys/kernel/livepatch/livepatch_shadow_fix2/enabled
  69. * echo 0 > /sys/kernel/livepatch/livepatch_shadow_fix1/enabled
  70. * rmmod livepatch-shadow-fix2
  71. * rmmod livepatch-shadow-fix1
  72. * rmmod livepatch-shadow-mod
  73. */
  74. #include <linux/kernel.h>
  75. #include <linux/module.h>
  76. #include <linux/sched.h>
  77. #include <linux/slab.h>
  78. #include <linux/stat.h>
  79. #include <linux/workqueue.h>
  80. MODULE_LICENSE("GPL");
  81. MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>");
  82. MODULE_DESCRIPTION("Buggy module for shadow variable demo");
  83. /* Allocate new dummies every second */
  84. #define ALLOC_PERIOD 1
  85. /* Check for expired dummies after a few new ones have been allocated */
  86. #define CLEANUP_PERIOD (3 * ALLOC_PERIOD)
  87. /* Dummies expire after a few cleanup instances */
  88. #define EXPIRE_PERIOD (4 * CLEANUP_PERIOD)
  89. /*
  90. * Keep a list of all the dummies so we can clean up any residual ones
  91. * on module exit
  92. */
  93. LIST_HEAD(dummy_list);
  94. DEFINE_MUTEX(dummy_list_mutex);
  95. struct dummy {
  96. struct list_head list;
  97. unsigned long jiffies_expire;
  98. };
  99. noinline struct dummy *dummy_alloc(void)
  100. {
  101. struct dummy *d;
  102. void *leak;
  103. d = kzalloc(sizeof(*d), GFP_KERNEL);
  104. if (!d)
  105. return NULL;
  106. d->jiffies_expire = jiffies +
  107. msecs_to_jiffies(1000 * EXPIRE_PERIOD);
  108. /* Oops, forgot to save leak! */
  109. leak = kzalloc(sizeof(int), GFP_KERNEL);
  110. pr_info("%s: dummy @ %p, expires @ %lx\n",
  111. __func__, d, d->jiffies_expire);
  112. return d;
  113. }
  114. noinline void dummy_free(struct dummy *d)
  115. {
  116. pr_info("%s: dummy @ %p, expired = %lx\n",
  117. __func__, d, d->jiffies_expire);
  118. kfree(d);
  119. }
  120. noinline bool dummy_check(struct dummy *d, unsigned long jiffies)
  121. {
  122. return time_after(jiffies, d->jiffies_expire);
  123. }
  124. /*
  125. * alloc_work_func: allocates new dummy structures, allocates additional
  126. * memory, aptly named "leak", but doesn't keep
  127. * permanent record of it.
  128. */
  129. static void alloc_work_func(struct work_struct *work);
  130. static DECLARE_DELAYED_WORK(alloc_dwork, alloc_work_func);
  131. static void alloc_work_func(struct work_struct *work)
  132. {
  133. struct dummy *d;
  134. d = dummy_alloc();
  135. if (!d)
  136. return;
  137. mutex_lock(&dummy_list_mutex);
  138. list_add(&d->list, &dummy_list);
  139. mutex_unlock(&dummy_list_mutex);
  140. schedule_delayed_work(&alloc_dwork,
  141. msecs_to_jiffies(1000 * ALLOC_PERIOD));
  142. }
  143. /*
  144. * cleanup_work_func: frees dummy structures. Without knownledge of
  145. * "leak", it leaks the additional memory that
  146. * alloc_work_func created.
  147. */
  148. static void cleanup_work_func(struct work_struct *work);
  149. static DECLARE_DELAYED_WORK(cleanup_dwork, cleanup_work_func);
  150. static void cleanup_work_func(struct work_struct *work)
  151. {
  152. struct dummy *d, *tmp;
  153. unsigned long j;
  154. j = jiffies;
  155. pr_info("%s: jiffies = %lx\n", __func__, j);
  156. mutex_lock(&dummy_list_mutex);
  157. list_for_each_entry_safe(d, tmp, &dummy_list, list) {
  158. /* Kick out and free any expired dummies */
  159. if (dummy_check(d, j)) {
  160. list_del(&d->list);
  161. dummy_free(d);
  162. }
  163. }
  164. mutex_unlock(&dummy_list_mutex);
  165. schedule_delayed_work(&cleanup_dwork,
  166. msecs_to_jiffies(1000 * CLEANUP_PERIOD));
  167. }
  168. static int livepatch_shadow_mod_init(void)
  169. {
  170. schedule_delayed_work(&alloc_dwork,
  171. msecs_to_jiffies(1000 * ALLOC_PERIOD));
  172. schedule_delayed_work(&cleanup_dwork,
  173. msecs_to_jiffies(1000 * CLEANUP_PERIOD));
  174. return 0;
  175. }
  176. static void livepatch_shadow_mod_exit(void)
  177. {
  178. struct dummy *d, *tmp;
  179. /* Wait for any dummies at work */
  180. cancel_delayed_work_sync(&alloc_dwork);
  181. cancel_delayed_work_sync(&cleanup_dwork);
  182. /* Cleanup residual dummies */
  183. list_for_each_entry_safe(d, tmp, &dummy_list, list) {
  184. list_del(&d->list);
  185. dummy_free(d);
  186. }
  187. }
  188. module_init(livepatch_shadow_mod_init);
  189. module_exit(livepatch_shadow_mod_exit);