blob: 728fb24c5683d7cb533f924b8b0e973483b6bef7 [file] [log] [blame]
Andrey Konovalove886bf92018-12-28 00:31:14 -08001// SPDX-License-Identifier: GPL-2.0
Alexander Potapenko55834c52016-05-20 16:59:11 -07002/*
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.
Alexander Potapenko55834c52016-05-20 16:59:11 -07009 */
10
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>
Dmitry Vyukovce5bec52017-03-09 16:17:32 -080019#include <linux/srcu.h>
Alexander Potapenko55834c52016-05-20 16:59:11 -070020#include <linux/string.h>
21#include <linux/types.h>
Kuan-Ying Lee6c82d452020-12-11 13:36:49 -080022#include <linux/cpuhotplug.h>
Alexander Potapenko55834c52016-05-20 16:59:11 -070023
24#include "../slab.h"
25#include "kasan.h"
26
27/* Data structure and operations for quarantine queues. */
28
29/*
30 * Each queue is a signle-linked list, which also stores the total size of
31 * objects inside of it.
32 */
33struct qlist_head {
34 struct qlist_node *head;
35 struct qlist_node *tail;
36 size_t bytes;
Kuan-Ying Lee6c82d452020-12-11 13:36:49 -080037 bool offline;
Alexander Potapenko55834c52016-05-20 16:59:11 -070038};
39
40#define QLIST_INIT { NULL, NULL, 0 }
41
42static bool qlist_empty(struct qlist_head *q)
43{
44 return !q->head;
45}
46
47static void qlist_init(struct qlist_head *q)
48{
49 q->head = q->tail = NULL;
50 q->bytes = 0;
51}
52
53static void qlist_put(struct qlist_head *q, struct qlist_node *qlink,
54 size_t size)
55{
56 if (unlikely(qlist_empty(q)))
57 q->head = qlink;
58 else
59 q->tail->next = qlink;
60 q->tail = qlink;
61 qlink->next = NULL;
62 q->bytes += size;
63}
64
65static void qlist_move_all(struct qlist_head *from, struct qlist_head *to)
66{
67 if (unlikely(qlist_empty(from)))
68 return;
69
70 if (qlist_empty(to)) {
71 *to = *from;
72 qlist_init(from);
73 return;
74 }
75
76 to->tail->next = from->head;
77 to->tail = from->tail;
78 to->bytes += from->bytes;
79
80 qlist_init(from);
81}
82
Dmitry Vyukov64abdcb2016-12-12 16:44:56 -080083#define QUARANTINE_PERCPU_SIZE (1 << 20)
84#define QUARANTINE_BATCHES \
85 (1024 > 4 * CONFIG_NR_CPUS ? 1024 : 4 * CONFIG_NR_CPUS)
Alexander Potapenko55834c52016-05-20 16:59:11 -070086
87/*
88 * The object quarantine consists of per-cpu queues and a global queue,
89 * guarded by quarantine_lock.
90 */
91static DEFINE_PER_CPU(struct qlist_head, cpu_quarantine);
92
Dmitry Vyukov64abdcb2016-12-12 16:44:56 -080093/* Round-robin FIFO array of batches. */
94static struct qlist_head global_quarantine[QUARANTINE_BATCHES];
95static int quarantine_head;
96static int quarantine_tail;
97/* Total size of all objects in global_quarantine across all batches. */
98static unsigned long quarantine_size;
Clark Williams026d1ea2018-10-26 15:10:32 -070099static DEFINE_RAW_SPINLOCK(quarantine_lock);
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800100DEFINE_STATIC_SRCU(remove_cache_srcu);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700101
102/* Maximum size of the global queue. */
Dmitry Vyukov64abdcb2016-12-12 16:44:56 -0800103static unsigned long quarantine_max_size;
104
105/*
106 * Target size of a batch in global_quarantine.
107 * Usually equal to QUARANTINE_PERCPU_SIZE unless we have too much RAM.
108 */
109static unsigned long quarantine_batch_size;
Alexander Potapenko55834c52016-05-20 16:59:11 -0700110
111/*
112 * The fraction of physical memory the quarantine is allowed to occupy.
113 * Quarantine doesn't support memory shrinker with SLAB allocator, so we keep
114 * the ratio low to avoid OOM.
115 */
116#define QUARANTINE_FRACTION 32
117
Alexander Potapenko55834c52016-05-20 16:59:11 -0700118static struct kmem_cache *qlink_to_cache(struct qlist_node *qlink)
119{
120 return virt_to_head_page(qlink)->slab_cache;
121}
122
123static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache)
124{
125 struct kasan_free_meta *free_info =
126 container_of(qlink, struct kasan_free_meta,
127 quarantine_link);
128
129 return ((void *)free_info) - cache->kasan_info.free_meta_offset;
130}
131
132static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)
133{
134 void *object = qlink_to_object(qlink, cache);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700135 unsigned long flags;
136
Andrey Ryabininf7376ae2016-08-02 14:02:46 -0700137 if (IS_ENABLED(CONFIG_SLAB))
138 local_irq_save(flags);
139
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800140 /*
141 * As the object now gets freed from the quaratine, assume that its
142 * free track is no longer valid.
143 */
Walter Wue4b78182020-08-06 23:24:39 -0700144 *(u8 *)kasan_mem_to_shadow(object) = KASAN_KMALLOC_FREE;
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800145
Alexander Potapenko55834c52016-05-20 16:59:11 -0700146 ___cache_free(cache, object, _THIS_IP_);
Andrey Ryabininf7376ae2016-08-02 14:02:46 -0700147
148 if (IS_ENABLED(CONFIG_SLAB))
149 local_irq_restore(flags);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700150}
151
152static void qlist_free_all(struct qlist_head *q, struct kmem_cache *cache)
153{
154 struct qlist_node *qlink;
155
156 if (unlikely(qlist_empty(q)))
157 return;
158
159 qlink = q->head;
160 while (qlink) {
161 struct kmem_cache *obj_cache =
162 cache ? cache : qlink_to_cache(qlink);
163 struct qlist_node *next = qlink->next;
164
165 qlink_free(qlink, obj_cache);
166 qlink = next;
167 }
168 qlist_init(q);
169}
170
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800171bool kasan_quarantine_put(struct kmem_cache *cache, void *object)
Alexander Potapenko55834c52016-05-20 16:59:11 -0700172{
173 unsigned long flags;
174 struct qlist_head *q;
175 struct qlist_head temp = QLIST_INIT;
Andrey Konovalov64767922020-12-22 12:02:34 -0800176 struct kasan_free_meta *meta = kasan_get_free_meta(cache, object);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700177
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800178 /*
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800179 * If there's no metadata for this object, don't put it into
180 * quarantine.
181 */
182 if (!meta)
183 return false;
184
185 /*
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800186 * Note: irq must be disabled until after we move the batch to the
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800187 * global quarantine. Otherwise kasan_quarantine_remove_cache() can
188 * miss some objects belonging to the cache if they are in our local
189 * temp list. kasan_quarantine_remove_cache() executes on_each_cpu()
190 * at the beginning which ensures that it either sees the objects in
191 * per-cpu lists or in the global quarantine.
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800192 */
Alexander Potapenko55834c52016-05-20 16:59:11 -0700193 local_irq_save(flags);
194
195 q = this_cpu_ptr(&cpu_quarantine);
Kuan-Ying Lee6c82d452020-12-11 13:36:49 -0800196 if (q->offline) {
197 local_irq_restore(flags);
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800198 return false;
Kuan-Ying Lee6c82d452020-12-11 13:36:49 -0800199 }
Andrey Konovalov64767922020-12-22 12:02:34 -0800200 qlist_put(q, &meta->quarantine_link, cache->size);
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800201 if (unlikely(q->bytes > QUARANTINE_PERCPU_SIZE)) {
Alexander Potapenko55834c52016-05-20 16:59:11 -0700202 qlist_move_all(q, &temp);
203
Clark Williams026d1ea2018-10-26 15:10:32 -0700204 raw_spin_lock(&quarantine_lock);
Dmitry Vyukov64abdcb2016-12-12 16:44:56 -0800205 WRITE_ONCE(quarantine_size, quarantine_size + temp.bytes);
206 qlist_move_all(&temp, &global_quarantine[quarantine_tail]);
207 if (global_quarantine[quarantine_tail].bytes >=
208 READ_ONCE(quarantine_batch_size)) {
209 int new_tail;
210
211 new_tail = quarantine_tail + 1;
212 if (new_tail == QUARANTINE_BATCHES)
213 new_tail = 0;
214 if (new_tail != quarantine_head)
215 quarantine_tail = new_tail;
216 }
Clark Williams026d1ea2018-10-26 15:10:32 -0700217 raw_spin_unlock(&quarantine_lock);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700218 }
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800219
220 local_irq_restore(flags);
Andrey Konovalov97593ca2020-12-22 12:03:28 -0800221
222 return true;
Alexander Potapenko55834c52016-05-20 16:59:11 -0700223}
224
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800225void kasan_quarantine_reduce(void)
Alexander Potapenko55834c52016-05-20 16:59:11 -0700226{
Dmitry Vyukov64abdcb2016-12-12 16:44:56 -0800227 size_t total_size, new_quarantine_size, percpu_quarantines;
Alexander Potapenko55834c52016-05-20 16:59:11 -0700228 unsigned long flags;
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800229 int srcu_idx;
Alexander Potapenko55834c52016-05-20 16:59:11 -0700230 struct qlist_head to_free = QLIST_INIT;
Alexander Potapenko55834c52016-05-20 16:59:11 -0700231
Dmitry Vyukov64abdcb2016-12-12 16:44:56 -0800232 if (likely(READ_ONCE(quarantine_size) <=
233 READ_ONCE(quarantine_max_size)))
Alexander Potapenko55834c52016-05-20 16:59:11 -0700234 return;
235
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800236 /*
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800237 * srcu critical section ensures that kasan_quarantine_remove_cache()
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800238 * will not miss objects belonging to the cache while they are in our
239 * local to_free list. srcu is chosen because (1) it gives us private
240 * grace period domain that does not interfere with anything else,
241 * and (2) it allows synchronize_srcu() to return without waiting
242 * if there are no pending read critical sections (which is the
243 * expected case).
244 */
245 srcu_idx = srcu_read_lock(&remove_cache_srcu);
Clark Williams026d1ea2018-10-26 15:10:32 -0700246 raw_spin_lock_irqsave(&quarantine_lock, flags);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700247
248 /*
249 * Update quarantine size in case of hotplug. Allocate a fraction of
250 * the installed memory to quarantine minus per-cpu queue limits.
251 */
Arun KSca79b0c2018-12-28 00:34:29 -0800252 total_size = (totalram_pages() << PAGE_SHIFT) /
Alexander Potapenko55834c52016-05-20 16:59:11 -0700253 QUARANTINE_FRACTION;
Alexander Potapenkoc3cee372016-08-02 14:02:58 -0700254 percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
Dmitry Vyukov64abdcb2016-12-12 16:44:56 -0800255 new_quarantine_size = (total_size < percpu_quarantines) ?
256 0 : total_size - percpu_quarantines;
257 WRITE_ONCE(quarantine_max_size, new_quarantine_size);
258 /* Aim at consuming at most 1/2 of slots in quarantine. */
259 WRITE_ONCE(quarantine_batch_size, max((size_t)QUARANTINE_PERCPU_SIZE,
260 2 * total_size / QUARANTINE_BATCHES));
Alexander Potapenko55834c52016-05-20 16:59:11 -0700261
Dmitry Vyukov64abdcb2016-12-12 16:44:56 -0800262 if (likely(quarantine_size > quarantine_max_size)) {
263 qlist_move_all(&global_quarantine[quarantine_head], &to_free);
264 WRITE_ONCE(quarantine_size, quarantine_size - to_free.bytes);
265 quarantine_head++;
266 if (quarantine_head == QUARANTINE_BATCHES)
267 quarantine_head = 0;
Alexander Potapenko55834c52016-05-20 16:59:11 -0700268 }
Alexander Potapenko55834c52016-05-20 16:59:11 -0700269
Clark Williams026d1ea2018-10-26 15:10:32 -0700270 raw_spin_unlock_irqrestore(&quarantine_lock, flags);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700271
272 qlist_free_all(&to_free, NULL);
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800273 srcu_read_unlock(&remove_cache_srcu, srcu_idx);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700274}
275
276static void qlist_move_cache(struct qlist_head *from,
277 struct qlist_head *to,
278 struct kmem_cache *cache)
279{
Joonsoo Kim0ab686d2016-07-14 12:07:17 -0700280 struct qlist_node *curr;
Alexander Potapenko55834c52016-05-20 16:59:11 -0700281
282 if (unlikely(qlist_empty(from)))
283 return;
284
285 curr = from->head;
Joonsoo Kim0ab686d2016-07-14 12:07:17 -0700286 qlist_init(from);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700287 while (curr) {
Joonsoo Kim0ab686d2016-07-14 12:07:17 -0700288 struct qlist_node *next = curr->next;
289 struct kmem_cache *obj_cache = qlink_to_cache(curr);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700290
Joonsoo Kim0ab686d2016-07-14 12:07:17 -0700291 if (obj_cache == cache)
292 qlist_put(to, curr, obj_cache->size);
293 else
294 qlist_put(from, curr, obj_cache->size);
295
296 curr = next;
Alexander Potapenko55834c52016-05-20 16:59:11 -0700297 }
298}
299
300static void per_cpu_remove_cache(void *arg)
301{
302 struct kmem_cache *cache = arg;
303 struct qlist_head to_free = QLIST_INIT;
304 struct qlist_head *q;
305
306 q = this_cpu_ptr(&cpu_quarantine);
307 qlist_move_cache(q, &to_free, cache);
308 qlist_free_all(&to_free, cache);
309}
310
Greg Thelenf9fa1d92017-02-24 15:00:05 -0800311/* Free all quarantined objects belonging to cache. */
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800312void kasan_quarantine_remove_cache(struct kmem_cache *cache)
Alexander Potapenko55834c52016-05-20 16:59:11 -0700313{
Dmitry Vyukov64abdcb2016-12-12 16:44:56 -0800314 unsigned long flags, i;
Alexander Potapenko55834c52016-05-20 16:59:11 -0700315 struct qlist_head to_free = QLIST_INIT;
316
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800317 /*
318 * Must be careful to not miss any objects that are being moved from
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800319 * per-cpu list to the global quarantine in kasan_quarantine_put(),
320 * nor objects being freed in kasan_quarantine_reduce(). on_each_cpu()
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800321 * achieves the first goal, while synchronize_srcu() achieves the
322 * second.
323 */
Alexander Potapenko55834c52016-05-20 16:59:11 -0700324 on_each_cpu(per_cpu_remove_cache, cache, 1);
325
Clark Williams026d1ea2018-10-26 15:10:32 -0700326 raw_spin_lock_irqsave(&quarantine_lock, flags);
Dmitry Vyukov68fd8142017-03-09 16:17:28 -0800327 for (i = 0; i < QUARANTINE_BATCHES; i++) {
328 if (qlist_empty(&global_quarantine[i]))
329 continue;
Dmitry Vyukov64abdcb2016-12-12 16:44:56 -0800330 qlist_move_cache(&global_quarantine[i], &to_free, cache);
Dmitry Vyukov68fd8142017-03-09 16:17:28 -0800331 /* Scanning whole quarantine can take a while. */
Clark Williams026d1ea2018-10-26 15:10:32 -0700332 raw_spin_unlock_irqrestore(&quarantine_lock, flags);
Dmitry Vyukov68fd8142017-03-09 16:17:28 -0800333 cond_resched();
Clark Williams026d1ea2018-10-26 15:10:32 -0700334 raw_spin_lock_irqsave(&quarantine_lock, flags);
Dmitry Vyukov68fd8142017-03-09 16:17:28 -0800335 }
Clark Williams026d1ea2018-10-26 15:10:32 -0700336 raw_spin_unlock_irqrestore(&quarantine_lock, flags);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700337
338 qlist_free_all(&to_free, cache);
Dmitry Vyukovce5bec52017-03-09 16:17:32 -0800339
340 synchronize_srcu(&remove_cache_srcu);
Alexander Potapenko55834c52016-05-20 16:59:11 -0700341}
Kuan-Ying Lee6c82d452020-12-11 13:36:49 -0800342
343static int kasan_cpu_online(unsigned int cpu)
344{
345 this_cpu_ptr(&cpu_quarantine)->offline = false;
346 return 0;
347}
348
349static int kasan_cpu_offline(unsigned int cpu)
350{
351 struct qlist_head *q;
352
353 q = this_cpu_ptr(&cpu_quarantine);
354 /* Ensure the ordering between the writing to q->offline and
355 * qlist_free_all. Otherwise, cpu_quarantine may be corrupted
356 * by interrupt.
357 */
358 WRITE_ONCE(q->offline, true);
359 barrier();
360 qlist_free_all(q, NULL);
361 return 0;
362}
363
364static int __init kasan_cpu_quarantine_init(void)
365{
366 int ret = 0;
367
368 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mm/kasan:online",
369 kasan_cpu_online, kasan_cpu_offline);
370 if (ret < 0)
371 pr_err("kasan cpu quarantine register failed [%d]\n", ret);
372 return ret;
373}
374late_initcall(kasan_cpu_quarantine_init);