quarantine.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KASAN quarantine.
  4. *
  5. * Author: Alexander Potapenko <glider@google.com>
  6. * Copyright (C) 2016 Google, Inc.
  7. *
  8. * Based on code by Dmitry Chernenkov.
  9. */
  10. #define pr_fmt(fmt) "kasan: " fmt
  11. #include <linux/gfp.h>
  12. #include <linux/hash.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/percpu.h>
  16. #include <linux/printk.h>
  17. #include <linux/shrinker.h>
  18. #include <linux/slab.h>
  19. #include <linux/srcu.h>
  20. #include <linux/string.h>
  21. #include <linux/types.h>
  22. #include <linux/cpuhotplug.h>
  23. #include "../slab.h"
  24. #include "kasan.h"
  25. /* Data structure and operations for quarantine queues. */
  26. /*
  27. * Each queue is a single-linked list, which also stores the total size of
  28. * objects inside of it.
  29. */
  30. struct qlist_head {
  31. struct qlist_node *head;
  32. struct qlist_node *tail;
  33. size_t bytes;
  34. bool offline;
  35. };
  36. #define QLIST_INIT { NULL, NULL, 0 }
  37. static bool qlist_empty(struct qlist_head *q)
  38. {
  39. return !q->head;
  40. }
  41. static void qlist_init(struct qlist_head *q)
  42. {
  43. q->head = q->tail = NULL;
  44. q->bytes = 0;
  45. }
  46. static void qlist_put(struct qlist_head *q, struct qlist_node *qlink,
  47. size_t size)
  48. {
  49. if (unlikely(qlist_empty(q)))
  50. q->head = qlink;
  51. else
  52. q->tail->next = qlink;
  53. q->tail = qlink;
  54. qlink->next = NULL;
  55. q->bytes += size;
  56. }
  57. static void qlist_move_all(struct qlist_head *from, struct qlist_head *to)
  58. {
  59. if (unlikely(qlist_empty(from)))
  60. return;
  61. if (qlist_empty(to)) {
  62. *to = *from;
  63. qlist_init(from);
  64. return;
  65. }
  66. to->tail->next = from->head;
  67. to->tail = from->tail;
  68. to->bytes += from->bytes;
  69. qlist_init(from);
  70. }
  71. #define QUARANTINE_PERCPU_SIZE (1 << 20)
  72. #define QUARANTINE_BATCHES \
  73. (1024 > 4 * CONFIG_NR_CPUS ? 1024 : 4 * CONFIG_NR_CPUS)
  74. /*
  75. * The object quarantine consists of per-cpu queues and a global queue,
  76. * guarded by quarantine_lock.
  77. */
  78. static DEFINE_PER_CPU(struct qlist_head, cpu_quarantine);
  79. /* Round-robin FIFO array of batches. */
  80. static struct qlist_head global_quarantine[QUARANTINE_BATCHES];
  81. static int quarantine_head;
  82. static int quarantine_tail;
  83. /* Total size of all objects in global_quarantine across all batches. */
  84. static unsigned long quarantine_size;
  85. static DEFINE_RAW_SPINLOCK(quarantine_lock);
  86. DEFINE_STATIC_SRCU(remove_cache_srcu);
  87. struct cpu_shrink_qlist {
  88. raw_spinlock_t lock;
  89. struct qlist_head qlist;
  90. };
  91. static DEFINE_PER_CPU(struct cpu_shrink_qlist, shrink_qlist) = {
  92. .lock = __RAW_SPIN_LOCK_UNLOCKED(shrink_qlist.lock),
  93. };
  94. /* Maximum size of the global queue. */
  95. static unsigned long quarantine_max_size;
  96. /*
  97. * Target size of a batch in global_quarantine.
  98. * Usually equal to QUARANTINE_PERCPU_SIZE unless we have too much RAM.
  99. */
  100. static unsigned long quarantine_batch_size;
  101. /*
  102. * The fraction of physical memory the quarantine is allowed to occupy.
  103. * Quarantine doesn't support memory shrinker with SLAB allocator, so we keep
  104. * the ratio low to avoid OOM.
  105. */
  106. #define QUARANTINE_FRACTION 32
  107. static struct kmem_cache *qlink_to_cache(struct qlist_node *qlink)
  108. {
  109. return virt_to_slab(qlink)->slab_cache;
  110. }
  111. static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache)
  112. {
  113. struct kasan_free_meta *free_info =
  114. container_of(qlink, struct kasan_free_meta,
  115. quarantine_link);
  116. return ((void *)free_info) - cache->kasan_info.free_meta_offset;
  117. }
  118. static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)
  119. {
  120. void *object = qlink_to_object(qlink, cache);
  121. struct kasan_free_meta *free_meta = kasan_get_free_meta(cache, object);
  122. /*
  123. * Note: Keep per-object metadata to allow KASAN print stack traces for
  124. * use-after-free-before-realloc bugs.
  125. */
  126. /*
  127. * If init_on_free is enabled and KASAN's free metadata is stored in
  128. * the object, zero the metadata. Otherwise, the object's memory will
  129. * not be properly zeroed, as KASAN saves the metadata after the slab
  130. * allocator zeroes the object.
  131. */
  132. if (slab_want_init_on_free(cache) &&
  133. cache->kasan_info.free_meta_offset == 0)
  134. memzero_explicit(free_meta, sizeof(*free_meta));
  135. ___cache_free(cache, object, _THIS_IP_);
  136. }
  137. static void qlist_free_all(struct qlist_head *q, struct kmem_cache *cache)
  138. {
  139. struct qlist_node *qlink;
  140. if (unlikely(qlist_empty(q)))
  141. return;
  142. qlink = q->head;
  143. while (qlink) {
  144. struct kmem_cache *obj_cache =
  145. cache ? cache : qlink_to_cache(qlink);
  146. struct qlist_node *next = qlink->next;
  147. qlink_free(qlink, obj_cache);
  148. qlink = next;
  149. }
  150. qlist_init(q);
  151. }
  152. bool kasan_quarantine_put(struct kmem_cache *cache, void *object)
  153. {
  154. unsigned long flags;
  155. struct qlist_head *q;
  156. struct qlist_head temp = QLIST_INIT;
  157. struct kasan_free_meta *meta = kasan_get_free_meta(cache, object);
  158. /*
  159. * If there's no metadata for this object, don't put it into
  160. * quarantine.
  161. */
  162. if (!meta)
  163. return false;
  164. /*
  165. * Note: irq must be disabled until after we move the batch to the
  166. * global quarantine. Otherwise kasan_quarantine_remove_cache() can
  167. * miss some objects belonging to the cache if they are in our local
  168. * temp list. kasan_quarantine_remove_cache() executes on_each_cpu()
  169. * at the beginning which ensures that it either sees the objects in
  170. * per-cpu lists or in the global quarantine.
  171. */
  172. local_irq_save(flags);
  173. q = this_cpu_ptr(&cpu_quarantine);
  174. if (q->offline) {
  175. local_irq_restore(flags);
  176. return false;
  177. }
  178. qlist_put(q, &meta->quarantine_link, cache->size);
  179. if (unlikely(q->bytes > QUARANTINE_PERCPU_SIZE)) {
  180. qlist_move_all(q, &temp);
  181. raw_spin_lock(&quarantine_lock);
  182. WRITE_ONCE(quarantine_size, quarantine_size + temp.bytes);
  183. qlist_move_all(&temp, &global_quarantine[quarantine_tail]);
  184. if (global_quarantine[quarantine_tail].bytes >=
  185. READ_ONCE(quarantine_batch_size)) {
  186. int new_tail;
  187. new_tail = quarantine_tail + 1;
  188. if (new_tail == QUARANTINE_BATCHES)
  189. new_tail = 0;
  190. if (new_tail != quarantine_head)
  191. quarantine_tail = new_tail;
  192. }
  193. raw_spin_unlock(&quarantine_lock);
  194. }
  195. local_irq_restore(flags);
  196. return true;
  197. }
  198. void kasan_quarantine_reduce(void)
  199. {
  200. size_t total_size, new_quarantine_size, percpu_quarantines;
  201. unsigned long flags;
  202. int srcu_idx;
  203. struct qlist_head to_free = QLIST_INIT;
  204. if (likely(READ_ONCE(quarantine_size) <=
  205. READ_ONCE(quarantine_max_size)))
  206. return;
  207. /*
  208. * srcu critical section ensures that kasan_quarantine_remove_cache()
  209. * will not miss objects belonging to the cache while they are in our
  210. * local to_free list. srcu is chosen because (1) it gives us private
  211. * grace period domain that does not interfere with anything else,
  212. * and (2) it allows synchronize_srcu() to return without waiting
  213. * if there are no pending read critical sections (which is the
  214. * expected case).
  215. */
  216. srcu_idx = srcu_read_lock(&remove_cache_srcu);
  217. raw_spin_lock_irqsave(&quarantine_lock, flags);
  218. /*
  219. * Update quarantine size in case of hotplug. Allocate a fraction of
  220. * the installed memory to quarantine minus per-cpu queue limits.
  221. */
  222. total_size = (totalram_pages() << PAGE_SHIFT) /
  223. QUARANTINE_FRACTION;
  224. percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
  225. new_quarantine_size = (total_size < percpu_quarantines) ?
  226. 0 : total_size - percpu_quarantines;
  227. WRITE_ONCE(quarantine_max_size, new_quarantine_size);
  228. /* Aim at consuming at most 1/2 of slots in quarantine. */
  229. WRITE_ONCE(quarantine_batch_size, max((size_t)QUARANTINE_PERCPU_SIZE,
  230. 2 * total_size / QUARANTINE_BATCHES));
  231. if (likely(quarantine_size > quarantine_max_size)) {
  232. qlist_move_all(&global_quarantine[quarantine_head], &to_free);
  233. WRITE_ONCE(quarantine_size, quarantine_size - to_free.bytes);
  234. quarantine_head++;
  235. if (quarantine_head == QUARANTINE_BATCHES)
  236. quarantine_head = 0;
  237. }
  238. raw_spin_unlock_irqrestore(&quarantine_lock, flags);
  239. qlist_free_all(&to_free, NULL);
  240. srcu_read_unlock(&remove_cache_srcu, srcu_idx);
  241. }
  242. static void qlist_move_cache(struct qlist_head *from,
  243. struct qlist_head *to,
  244. struct kmem_cache *cache)
  245. {
  246. struct qlist_node *curr;
  247. if (unlikely(qlist_empty(from)))
  248. return;
  249. curr = from->head;
  250. qlist_init(from);
  251. while (curr) {
  252. struct qlist_node *next = curr->next;
  253. struct kmem_cache *obj_cache = qlink_to_cache(curr);
  254. if (obj_cache == cache)
  255. qlist_put(to, curr, obj_cache->size);
  256. else
  257. qlist_put(from, curr, obj_cache->size);
  258. curr = next;
  259. }
  260. }
  261. static void __per_cpu_remove_cache(struct qlist_head *q, void *arg)
  262. {
  263. struct kmem_cache *cache = arg;
  264. unsigned long flags;
  265. struct cpu_shrink_qlist *sq;
  266. sq = this_cpu_ptr(&shrink_qlist);
  267. raw_spin_lock_irqsave(&sq->lock, flags);
  268. qlist_move_cache(q, &sq->qlist, cache);
  269. raw_spin_unlock_irqrestore(&sq->lock, flags);
  270. }
  271. static void per_cpu_remove_cache(void *arg)
  272. {
  273. struct qlist_head *q;
  274. q = this_cpu_ptr(&cpu_quarantine);
  275. /*
  276. * Ensure the ordering between the writing to q->offline and
  277. * per_cpu_remove_cache. Prevent cpu_quarantine from being corrupted
  278. * by interrupt.
  279. */
  280. if (READ_ONCE(q->offline))
  281. return;
  282. __per_cpu_remove_cache(q, arg);
  283. }
  284. /* Free all quarantined objects belonging to cache. */
  285. void kasan_quarantine_remove_cache(struct kmem_cache *cache)
  286. {
  287. unsigned long flags, i;
  288. struct qlist_head to_free = QLIST_INIT;
  289. int cpu;
  290. struct cpu_shrink_qlist *sq;
  291. /*
  292. * Must be careful to not miss any objects that are being moved from
  293. * per-cpu list to the global quarantine in kasan_quarantine_put(),
  294. * nor objects being freed in kasan_quarantine_reduce(). on_each_cpu()
  295. * achieves the first goal, while synchronize_srcu() achieves the
  296. * second.
  297. */
  298. on_each_cpu(per_cpu_remove_cache, cache, 1);
  299. for_each_online_cpu(cpu) {
  300. sq = per_cpu_ptr(&shrink_qlist, cpu);
  301. raw_spin_lock_irqsave(&sq->lock, flags);
  302. qlist_move_cache(&sq->qlist, &to_free, cache);
  303. raw_spin_unlock_irqrestore(&sq->lock, flags);
  304. }
  305. qlist_free_all(&to_free, cache);
  306. raw_spin_lock_irqsave(&quarantine_lock, flags);
  307. for (i = 0; i < QUARANTINE_BATCHES; i++) {
  308. if (qlist_empty(&global_quarantine[i]))
  309. continue;
  310. qlist_move_cache(&global_quarantine[i], &to_free, cache);
  311. /* Scanning whole quarantine can take a while. */
  312. raw_spin_unlock_irqrestore(&quarantine_lock, flags);
  313. cond_resched();
  314. raw_spin_lock_irqsave(&quarantine_lock, flags);
  315. }
  316. raw_spin_unlock_irqrestore(&quarantine_lock, flags);
  317. qlist_free_all(&to_free, cache);
  318. synchronize_srcu(&remove_cache_srcu);
  319. }
  320. static int kasan_cpu_online(unsigned int cpu)
  321. {
  322. this_cpu_ptr(&cpu_quarantine)->offline = false;
  323. return 0;
  324. }
  325. static int kasan_cpu_offline(unsigned int cpu)
  326. {
  327. struct qlist_head *q;
  328. q = this_cpu_ptr(&cpu_quarantine);
  329. /* Ensure the ordering between the writing to q->offline and
  330. * qlist_free_all. Otherwise, cpu_quarantine may be corrupted
  331. * by interrupt.
  332. */
  333. WRITE_ONCE(q->offline, true);
  334. barrier();
  335. qlist_free_all(q, NULL);
  336. return 0;
  337. }
  338. static int __init kasan_cpu_quarantine_init(void)
  339. {
  340. int ret = 0;
  341. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mm/kasan:online",
  342. kasan_cpu_online, kasan_cpu_offline);
  343. if (ret < 0)
  344. pr_err("cpu quarantine register failed [%d]\n", ret);
  345. return ret;
  346. }
  347. late_initcall(kasan_cpu_quarantine_init);