quarantine.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/srcu.h>
  28. #include <linux/string.h>
  29. #include <linux/types.h>
  30. #include "../slab.h"
  31. #include "kasan.h"
  32. /* Data structure and operations for quarantine queues. */
  33. /*
  34. * Each queue is a signle-linked list, which also stores the total size of
  35. * objects inside of it.
  36. */
  37. struct qlist_head {
  38. struct qlist_node *head;
  39. struct qlist_node *tail;
  40. size_t bytes;
  41. };
  42. #define QLIST_INIT { NULL, NULL, 0 }
  43. static bool qlist_empty(struct qlist_head *q)
  44. {
  45. return !q->head;
  46. }
  47. static void qlist_init(struct qlist_head *q)
  48. {
  49. q->head = q->tail = NULL;
  50. q->bytes = 0;
  51. }
  52. static void qlist_put(struct qlist_head *q, struct qlist_node *qlink,
  53. size_t size)
  54. {
  55. if (unlikely(qlist_empty(q)))
  56. q->head = qlink;
  57. else
  58. q->tail->next = qlink;
  59. q->tail = qlink;
  60. qlink->next = NULL;
  61. q->bytes += size;
  62. }
  63. static void qlist_move_all(struct qlist_head *from, struct qlist_head *to)
  64. {
  65. if (unlikely(qlist_empty(from)))
  66. return;
  67. if (qlist_empty(to)) {
  68. *to = *from;
  69. qlist_init(from);
  70. return;
  71. }
  72. to->tail->next = from->head;
  73. to->tail = from->tail;
  74. to->bytes += from->bytes;
  75. qlist_init(from);
  76. }
  77. #define QUARANTINE_PERCPU_SIZE (1 << 20)
  78. #define QUARANTINE_BATCHES \
  79. (1024 > 4 * CONFIG_NR_CPUS ? 1024 : 4 * CONFIG_NR_CPUS)
  80. /*
  81. * The object quarantine consists of per-cpu queues and a global queue,
  82. * guarded by quarantine_lock.
  83. */
  84. static DEFINE_PER_CPU(struct qlist_head, cpu_quarantine);
  85. /* Round-robin FIFO array of batches. */
  86. static struct qlist_head global_quarantine[QUARANTINE_BATCHES];
  87. static int quarantine_head;
  88. static int quarantine_tail;
  89. /* Total size of all objects in global_quarantine across all batches. */
  90. static unsigned long quarantine_size;
  91. static DEFINE_SPINLOCK(quarantine_lock);
  92. DEFINE_STATIC_SRCU(remove_cache_srcu);
  93. /* Maximum size of the global queue. */
  94. static unsigned long quarantine_max_size;
  95. /*
  96. * Target size of a batch in global_quarantine.
  97. * Usually equal to QUARANTINE_PERCPU_SIZE unless we have too much RAM.
  98. */
  99. static unsigned long quarantine_batch_size;
  100. /*
  101. * The fraction of physical memory the quarantine is allowed to occupy.
  102. * Quarantine doesn't support memory shrinker with SLAB allocator, so we keep
  103. * the ratio low to avoid OOM.
  104. */
  105. #define QUARANTINE_FRACTION 32
  106. static struct kmem_cache *qlink_to_cache(struct qlist_node *qlink)
  107. {
  108. return virt_to_head_page(qlink)->slab_cache;
  109. }
  110. static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache)
  111. {
  112. struct kasan_free_meta *free_info =
  113. container_of(qlink, struct kasan_free_meta,
  114. quarantine_link);
  115. return ((void *)free_info) - cache->kasan_info.free_meta_offset;
  116. }
  117. static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)
  118. {
  119. void *object = qlink_to_object(qlink, cache);
  120. unsigned long flags;
  121. if (IS_ENABLED(CONFIG_SLAB))
  122. local_irq_save(flags);
  123. ___cache_free(cache, object, _THIS_IP_);
  124. if (IS_ENABLED(CONFIG_SLAB))
  125. local_irq_restore(flags);
  126. }
  127. static void qlist_free_all(struct qlist_head *q, struct kmem_cache *cache)
  128. {
  129. struct qlist_node *qlink;
  130. if (unlikely(qlist_empty(q)))
  131. return;
  132. qlink = q->head;
  133. while (qlink) {
  134. struct kmem_cache *obj_cache =
  135. cache ? cache : qlink_to_cache(qlink);
  136. struct qlist_node *next = qlink->next;
  137. qlink_free(qlink, obj_cache);
  138. qlink = next;
  139. }
  140. qlist_init(q);
  141. }
  142. void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
  143. {
  144. unsigned long flags;
  145. struct qlist_head *q;
  146. struct qlist_head temp = QLIST_INIT;
  147. /*
  148. * Note: irq must be disabled until after we move the batch to the
  149. * global quarantine. Otherwise quarantine_remove_cache() can miss
  150. * some objects belonging to the cache if they are in our local temp
  151. * list. quarantine_remove_cache() executes on_each_cpu() at the
  152. * beginning which ensures that it either sees the objects in per-cpu
  153. * lists or in the global quarantine.
  154. */
  155. local_irq_save(flags);
  156. q = this_cpu_ptr(&cpu_quarantine);
  157. qlist_put(q, &info->quarantine_link, cache->size);
  158. if (unlikely(q->bytes > QUARANTINE_PERCPU_SIZE)) {
  159. qlist_move_all(q, &temp);
  160. spin_lock(&quarantine_lock);
  161. WRITE_ONCE(quarantine_size, quarantine_size + temp.bytes);
  162. qlist_move_all(&temp, &global_quarantine[quarantine_tail]);
  163. if (global_quarantine[quarantine_tail].bytes >=
  164. READ_ONCE(quarantine_batch_size)) {
  165. int new_tail;
  166. new_tail = quarantine_tail + 1;
  167. if (new_tail == QUARANTINE_BATCHES)
  168. new_tail = 0;
  169. if (new_tail != quarantine_head)
  170. quarantine_tail = new_tail;
  171. }
  172. spin_unlock(&quarantine_lock);
  173. }
  174. local_irq_restore(flags);
  175. }
  176. void quarantine_reduce(void)
  177. {
  178. size_t total_size, new_quarantine_size, percpu_quarantines;
  179. unsigned long flags;
  180. int srcu_idx;
  181. struct qlist_head to_free = QLIST_INIT;
  182. if (likely(READ_ONCE(quarantine_size) <=
  183. READ_ONCE(quarantine_max_size)))
  184. return;
  185. /*
  186. * srcu critical section ensures that quarantine_remove_cache()
  187. * will not miss objects belonging to the cache while they are in our
  188. * local to_free list. srcu is chosen because (1) it gives us private
  189. * grace period domain that does not interfere with anything else,
  190. * and (2) it allows synchronize_srcu() to return without waiting
  191. * if there are no pending read critical sections (which is the
  192. * expected case).
  193. */
  194. srcu_idx = srcu_read_lock(&remove_cache_srcu);
  195. spin_lock_irqsave(&quarantine_lock, flags);
  196. /*
  197. * Update quarantine size in case of hotplug. Allocate a fraction of
  198. * the installed memory to quarantine minus per-cpu queue limits.
  199. */
  200. total_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
  201. QUARANTINE_FRACTION;
  202. percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
  203. new_quarantine_size = (total_size < percpu_quarantines) ?
  204. 0 : total_size - percpu_quarantines;
  205. WRITE_ONCE(quarantine_max_size, new_quarantine_size);
  206. /* Aim at consuming at most 1/2 of slots in quarantine. */
  207. WRITE_ONCE(quarantine_batch_size, max((size_t)QUARANTINE_PERCPU_SIZE,
  208. 2 * total_size / QUARANTINE_BATCHES));
  209. if (likely(quarantine_size > quarantine_max_size)) {
  210. qlist_move_all(&global_quarantine[quarantine_head], &to_free);
  211. WRITE_ONCE(quarantine_size, quarantine_size - to_free.bytes);
  212. quarantine_head++;
  213. if (quarantine_head == QUARANTINE_BATCHES)
  214. quarantine_head = 0;
  215. }
  216. spin_unlock_irqrestore(&quarantine_lock, flags);
  217. qlist_free_all(&to_free, NULL);
  218. srcu_read_unlock(&remove_cache_srcu, srcu_idx);
  219. }
  220. static void qlist_move_cache(struct qlist_head *from,
  221. struct qlist_head *to,
  222. struct kmem_cache *cache)
  223. {
  224. struct qlist_node *curr;
  225. if (unlikely(qlist_empty(from)))
  226. return;
  227. curr = from->head;
  228. qlist_init(from);
  229. while (curr) {
  230. struct qlist_node *next = curr->next;
  231. struct kmem_cache *obj_cache = qlink_to_cache(curr);
  232. if (obj_cache == cache)
  233. qlist_put(to, curr, obj_cache->size);
  234. else
  235. qlist_put(from, curr, obj_cache->size);
  236. curr = next;
  237. }
  238. }
  239. static void per_cpu_remove_cache(void *arg)
  240. {
  241. struct kmem_cache *cache = arg;
  242. struct qlist_head to_free = QLIST_INIT;
  243. struct qlist_head *q;
  244. q = this_cpu_ptr(&cpu_quarantine);
  245. qlist_move_cache(q, &to_free, cache);
  246. qlist_free_all(&to_free, cache);
  247. }
  248. /* Free all quarantined objects belonging to cache. */
  249. void quarantine_remove_cache(struct kmem_cache *cache)
  250. {
  251. unsigned long flags, i;
  252. struct qlist_head to_free = QLIST_INIT;
  253. /*
  254. * Must be careful to not miss any objects that are being moved from
  255. * per-cpu list to the global quarantine in quarantine_put(),
  256. * nor objects being freed in quarantine_reduce(). on_each_cpu()
  257. * achieves the first goal, while synchronize_srcu() achieves the
  258. * second.
  259. */
  260. on_each_cpu(per_cpu_remove_cache, cache, 1);
  261. spin_lock_irqsave(&quarantine_lock, flags);
  262. for (i = 0; i < QUARANTINE_BATCHES; i++) {
  263. if (qlist_empty(&global_quarantine[i]))
  264. continue;
  265. qlist_move_cache(&global_quarantine[i], &to_free, cache);
  266. /* Scanning whole quarantine can take a while. */
  267. spin_unlock_irqrestore(&quarantine_lock, flags);
  268. cond_resched();
  269. spin_lock_irqsave(&quarantine_lock, flags);
  270. }
  271. spin_unlock_irqrestore(&quarantine_lock, flags);
  272. qlist_free_all(&to_free, cache);
  273. synchronize_srcu(&remove_cache_srcu);
  274. }