quarantine.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * KASAN quarantine.
  3. *
  4. * Author: Alexander Potapenko <glider@google.com>
  5. * Copyright (C) 2016 Google, Inc.
  6. *
  7. * Based on code by Dmitry Chernenkov.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. */
  19. #include <linux/gfp.h>
  20. #include <linux/hash.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/percpu.h>
  24. #include <linux/printk.h>
  25. #include <linux/shrinker.h>
  26. #include <linux/slab.h>
  27. #include <linux/string.h>
  28. #include <linux/types.h>
  29. #include "../slab.h"
  30. #include "kasan.h"
  31. /* Data structure and operations for quarantine queues. */
  32. /*
  33. * Each queue is a signle-linked list, which also stores the total size of
  34. * objects inside of it.
  35. */
  36. struct qlist_head {
  37. struct qlist_node *head;
  38. struct qlist_node *tail;
  39. size_t bytes;
  40. };
  41. #define QLIST_INIT { NULL, NULL, 0 }
  42. static bool qlist_empty(struct qlist_head *q)
  43. {
  44. return !q->head;
  45. }
  46. static void qlist_init(struct qlist_head *q)
  47. {
  48. q->head = q->tail = NULL;
  49. q->bytes = 0;
  50. }
  51. static void qlist_put(struct qlist_head *q, struct qlist_node *qlink,
  52. size_t size)
  53. {
  54. if (unlikely(qlist_empty(q)))
  55. q->head = qlink;
  56. else
  57. q->tail->next = qlink;
  58. q->tail = qlink;
  59. qlink->next = NULL;
  60. q->bytes += size;
  61. }
  62. static void qlist_move_all(struct qlist_head *from, struct qlist_head *to)
  63. {
  64. if (unlikely(qlist_empty(from)))
  65. return;
  66. if (qlist_empty(to)) {
  67. *to = *from;
  68. qlist_init(from);
  69. return;
  70. }
  71. to->tail->next = from->head;
  72. to->tail = from->tail;
  73. to->bytes += from->bytes;
  74. qlist_init(from);
  75. }
  76. static void qlist_move(struct qlist_head *from, struct qlist_node *last,
  77. struct qlist_head *to, size_t size)
  78. {
  79. if (unlikely(last == from->tail)) {
  80. qlist_move_all(from, to);
  81. return;
  82. }
  83. if (qlist_empty(to))
  84. to->head = from->head;
  85. else
  86. to->tail->next = from->head;
  87. to->tail = last;
  88. from->head = last->next;
  89. last->next = NULL;
  90. from->bytes -= size;
  91. to->bytes += size;
  92. }
  93. /*
  94. * The object quarantine consists of per-cpu queues and a global queue,
  95. * guarded by quarantine_lock.
  96. */
  97. static DEFINE_PER_CPU(struct qlist_head, cpu_quarantine);
  98. static struct qlist_head global_quarantine;
  99. static DEFINE_SPINLOCK(quarantine_lock);
  100. /* Maximum size of the global queue. */
  101. static unsigned long quarantine_size;
  102. /*
  103. * The fraction of physical memory the quarantine is allowed to occupy.
  104. * Quarantine doesn't support memory shrinker with SLAB allocator, so we keep
  105. * the ratio low to avoid OOM.
  106. */
  107. #define QUARANTINE_FRACTION 32
  108. #define QUARANTINE_LOW_SIZE (READ_ONCE(quarantine_size) * 3 / 4)
  109. #define QUARANTINE_PERCPU_SIZE (1 << 20)
  110. static struct kmem_cache *qlink_to_cache(struct qlist_node *qlink)
  111. {
  112. return virt_to_head_page(qlink)->slab_cache;
  113. }
  114. static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache)
  115. {
  116. struct kasan_free_meta *free_info =
  117. container_of(qlink, struct kasan_free_meta,
  118. quarantine_link);
  119. return ((void *)free_info) - cache->kasan_info.free_meta_offset;
  120. }
  121. static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)
  122. {
  123. void *object = qlink_to_object(qlink, cache);
  124. unsigned long flags;
  125. if (IS_ENABLED(CONFIG_SLAB))
  126. local_irq_save(flags);
  127. ___cache_free(cache, object, _THIS_IP_);
  128. if (IS_ENABLED(CONFIG_SLAB))
  129. local_irq_restore(flags);
  130. }
  131. static void qlist_free_all(struct qlist_head *q, struct kmem_cache *cache)
  132. {
  133. struct qlist_node *qlink;
  134. if (unlikely(qlist_empty(q)))
  135. return;
  136. qlink = q->head;
  137. while (qlink) {
  138. struct kmem_cache *obj_cache =
  139. cache ? cache : qlink_to_cache(qlink);
  140. struct qlist_node *next = qlink->next;
  141. qlink_free(qlink, obj_cache);
  142. qlink = next;
  143. }
  144. qlist_init(q);
  145. }
  146. void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
  147. {
  148. unsigned long flags;
  149. struct qlist_head *q;
  150. struct qlist_head temp = QLIST_INIT;
  151. local_irq_save(flags);
  152. q = this_cpu_ptr(&cpu_quarantine);
  153. qlist_put(q, &info->quarantine_link, cache->size);
  154. if (unlikely(q->bytes > QUARANTINE_PERCPU_SIZE))
  155. qlist_move_all(q, &temp);
  156. local_irq_restore(flags);
  157. if (unlikely(!qlist_empty(&temp))) {
  158. spin_lock_irqsave(&quarantine_lock, flags);
  159. qlist_move_all(&temp, &global_quarantine);
  160. spin_unlock_irqrestore(&quarantine_lock, flags);
  161. }
  162. }
  163. void quarantine_reduce(void)
  164. {
  165. size_t new_quarantine_size, percpu_quarantines;
  166. unsigned long flags;
  167. struct qlist_head to_free = QLIST_INIT;
  168. size_t size_to_free = 0;
  169. struct qlist_node *last;
  170. if (likely(READ_ONCE(global_quarantine.bytes) <=
  171. READ_ONCE(quarantine_size)))
  172. return;
  173. spin_lock_irqsave(&quarantine_lock, flags);
  174. /*
  175. * Update quarantine size in case of hotplug. Allocate a fraction of
  176. * the installed memory to quarantine minus per-cpu queue limits.
  177. */
  178. new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
  179. QUARANTINE_FRACTION;
  180. percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
  181. new_quarantine_size = (new_quarantine_size < percpu_quarantines) ?
  182. 0 : new_quarantine_size - percpu_quarantines;
  183. WRITE_ONCE(quarantine_size, new_quarantine_size);
  184. last = global_quarantine.head;
  185. while (last) {
  186. struct kmem_cache *cache = qlink_to_cache(last);
  187. size_to_free += cache->size;
  188. if (!last->next || size_to_free >
  189. global_quarantine.bytes - QUARANTINE_LOW_SIZE)
  190. break;
  191. last = last->next;
  192. }
  193. qlist_move(&global_quarantine, last, &to_free, size_to_free);
  194. spin_unlock_irqrestore(&quarantine_lock, flags);
  195. qlist_free_all(&to_free, NULL);
  196. }
  197. static void qlist_move_cache(struct qlist_head *from,
  198. struct qlist_head *to,
  199. struct kmem_cache *cache)
  200. {
  201. struct qlist_node *curr;
  202. if (unlikely(qlist_empty(from)))
  203. return;
  204. curr = from->head;
  205. qlist_init(from);
  206. while (curr) {
  207. struct qlist_node *next = curr->next;
  208. struct kmem_cache *obj_cache = qlink_to_cache(curr);
  209. if (obj_cache == cache)
  210. qlist_put(to, curr, obj_cache->size);
  211. else
  212. qlist_put(from, curr, obj_cache->size);
  213. curr = next;
  214. }
  215. }
  216. static void per_cpu_remove_cache(void *arg)
  217. {
  218. struct kmem_cache *cache = arg;
  219. struct qlist_head to_free = QLIST_INIT;
  220. struct qlist_head *q;
  221. q = this_cpu_ptr(&cpu_quarantine);
  222. qlist_move_cache(q, &to_free, cache);
  223. qlist_free_all(&to_free, cache);
  224. }
  225. void quarantine_remove_cache(struct kmem_cache *cache)
  226. {
  227. unsigned long flags;
  228. struct qlist_head to_free = QLIST_INIT;
  229. on_each_cpu(per_cpu_remove_cache, cache, 1);
  230. spin_lock_irqsave(&quarantine_lock, flags);
  231. qlist_move_cache(&global_quarantine, &to_free, cache);
  232. spin_unlock_irqrestore(&quarantine_lock, flags);
  233. qlist_free_all(&to_free, cache);
  234. }