Loading...
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 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 */
20
21#include <linux/gfp.h>
22#include <linux/hash.h>
23#include <linux/kernel.h>
24#include <linux/mm.h>
25#include <linux/percpu.h>
26#include <linux/printk.h>
27#include <linux/shrinker.h>
28#include <linux/slab.h>
29#include <linux/srcu.h>
30#include <linux/string.h>
31#include <linux/types.h>
32
33#include "../slab.h"
34#include "kasan.h"
35
36/* Data structure and operations for quarantine queues. */
37
38/*
39 * Each queue is a signle-linked list, which also stores the total size of
40 * objects inside of it.
41 */
42struct qlist_head {
43 struct qlist_node *head;
44 struct qlist_node *tail;
45 size_t bytes;
46};
47
48#define QLIST_INIT { NULL, NULL, 0 }
49
50static bool qlist_empty(struct qlist_head *q)
51{
52 return !q->head;
53}
54
55static void qlist_init(struct qlist_head *q)
56{
57 q->head = q->tail = NULL;
58 q->bytes = 0;
59}
60
61static void qlist_put(struct qlist_head *q, struct qlist_node *qlink,
62 size_t size)
63{
64 if (unlikely(qlist_empty(q)))
65 q->head = qlink;
66 else
67 q->tail->next = qlink;
68 q->tail = qlink;
69 qlink->next = NULL;
70 q->bytes += size;
71}
72
73static void qlist_move_all(struct qlist_head *from, struct qlist_head *to)
74{
75 if (unlikely(qlist_empty(from)))
76 return;
77
78 if (qlist_empty(to)) {
79 *to = *from;
80 qlist_init(from);
81 return;
82 }
83
84 to->tail->next = from->head;
85 to->tail = from->tail;
86 to->bytes += from->bytes;
87
88 qlist_init(from);
89}
90
91#define QUARANTINE_PERCPU_SIZE (1 << 20)
92#define QUARANTINE_BATCHES \
93 (1024 > 4 * CONFIG_NR_CPUS ? 1024 : 4 * CONFIG_NR_CPUS)
94
95/*
96 * The object quarantine consists of per-cpu queues and a global queue,
97 * guarded by quarantine_lock.
98 */
99static DEFINE_PER_CPU(struct qlist_head, cpu_quarantine);
100
101/* Round-robin FIFO array of batches. */
102static struct qlist_head global_quarantine[QUARANTINE_BATCHES];
103static int quarantine_head;
104static int quarantine_tail;
105/* Total size of all objects in global_quarantine across all batches. */
106static unsigned long quarantine_size;
107static DEFINE_RAW_SPINLOCK(quarantine_lock);
108DEFINE_STATIC_SRCU(remove_cache_srcu);
109
110/* Maximum size of the global queue. */
111static unsigned long quarantine_max_size;
112
113/*
114 * Target size of a batch in global_quarantine.
115 * Usually equal to QUARANTINE_PERCPU_SIZE unless we have too much RAM.
116 */
117static unsigned long quarantine_batch_size;
118
119/*
120 * The fraction of physical memory the quarantine is allowed to occupy.
121 * Quarantine doesn't support memory shrinker with SLAB allocator, so we keep
122 * the ratio low to avoid OOM.
123 */
124#define QUARANTINE_FRACTION 32
125
126static struct kmem_cache *qlink_to_cache(struct qlist_node *qlink)
127{
128 return virt_to_head_page(qlink)->slab_cache;
129}
130
131static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache)
132{
133 struct kasan_free_meta *free_info =
134 container_of(qlink, struct kasan_free_meta,
135 quarantine_link);
136
137 return ((void *)free_info) - cache->kasan_info.free_meta_offset;
138}
139
140static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)
141{
142 void *object = qlink_to_object(qlink, cache);
143 unsigned long flags;
144
145 if (IS_ENABLED(CONFIG_SLAB))
146 local_irq_save(flags);
147
148 *(u8 *)kasan_mem_to_shadow(object) = KASAN_KMALLOC_FREE;
149 ___cache_free(cache, object, _THIS_IP_);
150
151 if (IS_ENABLED(CONFIG_SLAB))
152 local_irq_restore(flags);
153}
154
155static void qlist_free_all(struct qlist_head *q, struct kmem_cache *cache)
156{
157 struct qlist_node *qlink;
158
159 if (unlikely(qlist_empty(q)))
160 return;
161
162 qlink = q->head;
163 while (qlink) {
164 struct kmem_cache *obj_cache =
165 cache ? cache : qlink_to_cache(qlink);
166 struct qlist_node *next = qlink->next;
167
168 qlink_free(qlink, obj_cache);
169 qlink = next;
170 }
171 qlist_init(q);
172}
173
174void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
175{
176 unsigned long flags;
177 struct qlist_head *q;
178 struct qlist_head temp = QLIST_INIT;
179
180 /*
181 * Note: irq must be disabled until after we move the batch to the
182 * global quarantine. Otherwise quarantine_remove_cache() can miss
183 * some objects belonging to the cache if they are in our local temp
184 * list. quarantine_remove_cache() executes on_each_cpu() at the
185 * beginning which ensures that it either sees the objects in per-cpu
186 * lists or in the global quarantine.
187 */
188 local_irq_save(flags);
189
190 q = this_cpu_ptr(&cpu_quarantine);
191 qlist_put(q, &info->quarantine_link, cache->size);
192 if (unlikely(q->bytes > QUARANTINE_PERCPU_SIZE)) {
193 qlist_move_all(q, &temp);
194
195 raw_spin_lock(&quarantine_lock);
196 WRITE_ONCE(quarantine_size, quarantine_size + temp.bytes);
197 qlist_move_all(&temp, &global_quarantine[quarantine_tail]);
198 if (global_quarantine[quarantine_tail].bytes >=
199 READ_ONCE(quarantine_batch_size)) {
200 int new_tail;
201
202 new_tail = quarantine_tail + 1;
203 if (new_tail == QUARANTINE_BATCHES)
204 new_tail = 0;
205 if (new_tail != quarantine_head)
206 quarantine_tail = new_tail;
207 }
208 raw_spin_unlock(&quarantine_lock);
209 }
210
211 local_irq_restore(flags);
212}
213
214void quarantine_reduce(void)
215{
216 size_t total_size, new_quarantine_size, percpu_quarantines;
217 unsigned long flags;
218 int srcu_idx;
219 struct qlist_head to_free = QLIST_INIT;
220
221 if (likely(READ_ONCE(quarantine_size) <=
222 READ_ONCE(quarantine_max_size)))
223 return;
224
225 /*
226 * srcu critical section ensures that quarantine_remove_cache()
227 * will not miss objects belonging to the cache while they are in our
228 * local to_free list. srcu is chosen because (1) it gives us private
229 * grace period domain that does not interfere with anything else,
230 * and (2) it allows synchronize_srcu() to return without waiting
231 * if there are no pending read critical sections (which is the
232 * expected case).
233 */
234 srcu_idx = srcu_read_lock(&remove_cache_srcu);
235 raw_spin_lock_irqsave(&quarantine_lock, flags);
236
237 /*
238 * Update quarantine size in case of hotplug. Allocate a fraction of
239 * the installed memory to quarantine minus per-cpu queue limits.
240 */
241 total_size = (totalram_pages() << PAGE_SHIFT) /
242 QUARANTINE_FRACTION;
243 percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
244 new_quarantine_size = (total_size < percpu_quarantines) ?
245 0 : total_size - percpu_quarantines;
246 WRITE_ONCE(quarantine_max_size, new_quarantine_size);
247 /* Aim at consuming at most 1/2 of slots in quarantine. */
248 WRITE_ONCE(quarantine_batch_size, max((size_t)QUARANTINE_PERCPU_SIZE,
249 2 * total_size / QUARANTINE_BATCHES));
250
251 if (likely(quarantine_size > quarantine_max_size)) {
252 qlist_move_all(&global_quarantine[quarantine_head], &to_free);
253 WRITE_ONCE(quarantine_size, quarantine_size - to_free.bytes);
254 quarantine_head++;
255 if (quarantine_head == QUARANTINE_BATCHES)
256 quarantine_head = 0;
257 }
258
259 raw_spin_unlock_irqrestore(&quarantine_lock, flags);
260
261 qlist_free_all(&to_free, NULL);
262 srcu_read_unlock(&remove_cache_srcu, srcu_idx);
263}
264
265static void qlist_move_cache(struct qlist_head *from,
266 struct qlist_head *to,
267 struct kmem_cache *cache)
268{
269 struct qlist_node *curr;
270
271 if (unlikely(qlist_empty(from)))
272 return;
273
274 curr = from->head;
275 qlist_init(from);
276 while (curr) {
277 struct qlist_node *next = curr->next;
278 struct kmem_cache *obj_cache = qlink_to_cache(curr);
279
280 if (obj_cache == cache)
281 qlist_put(to, curr, obj_cache->size);
282 else
283 qlist_put(from, curr, obj_cache->size);
284
285 curr = next;
286 }
287}
288
289static void per_cpu_remove_cache(void *arg)
290{
291 struct kmem_cache *cache = arg;
292 struct qlist_head to_free = QLIST_INIT;
293 struct qlist_head *q;
294
295 q = this_cpu_ptr(&cpu_quarantine);
296 qlist_move_cache(q, &to_free, cache);
297 qlist_free_all(&to_free, cache);
298}
299
300/* Free all quarantined objects belonging to cache. */
301void quarantine_remove_cache(struct kmem_cache *cache)
302{
303 unsigned long flags, i;
304 struct qlist_head to_free = QLIST_INIT;
305
306 /*
307 * Must be careful to not miss any objects that are being moved from
308 * per-cpu list to the global quarantine in quarantine_put(),
309 * nor objects being freed in quarantine_reduce(). on_each_cpu()
310 * achieves the first goal, while synchronize_srcu() achieves the
311 * second.
312 */
313 on_each_cpu(per_cpu_remove_cache, cache, 1);
314
315 raw_spin_lock_irqsave(&quarantine_lock, flags);
316 for (i = 0; i < QUARANTINE_BATCHES; i++) {
317 if (qlist_empty(&global_quarantine[i]))
318 continue;
319 qlist_move_cache(&global_quarantine[i], &to_free, cache);
320 /* Scanning whole quarantine can take a while. */
321 raw_spin_unlock_irqrestore(&quarantine_lock, flags);
322 cond_resched();
323 raw_spin_lock_irqsave(&quarantine_lock, flags);
324 }
325 raw_spin_unlock_irqrestore(&quarantine_lock, flags);
326
327 qlist_free_all(&to_free, cache);
328
329 synchronize_srcu(&remove_cache_srcu);
330}
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
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
24#include "../slab.h"
25#include "kasan.h"
26
27/* Data structure and operations for quarantine queues. */
28
29/*
30 * Each queue is a single-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;
37 bool offline;
38};
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
83#define QUARANTINE_PERCPU_SIZE (1 << 20)
84#define QUARANTINE_BATCHES \
85 (1024 > 4 * CONFIG_NR_CPUS ? 1024 : 4 * CONFIG_NR_CPUS)
86
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
93/* 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;
99static DEFINE_RAW_SPINLOCK(quarantine_lock);
100DEFINE_STATIC_SRCU(remove_cache_srcu);
101
102#ifdef CONFIG_PREEMPT_RT
103struct cpu_shrink_qlist {
104 raw_spinlock_t lock;
105 struct qlist_head qlist;
106};
107
108static DEFINE_PER_CPU(struct cpu_shrink_qlist, shrink_qlist) = {
109 .lock = __RAW_SPIN_LOCK_UNLOCKED(shrink_qlist.lock),
110};
111#endif
112
113/* Maximum size of the global queue. */
114static unsigned long quarantine_max_size;
115
116/*
117 * Target size of a batch in global_quarantine.
118 * Usually equal to QUARANTINE_PERCPU_SIZE unless we have too much RAM.
119 */
120static unsigned long quarantine_batch_size;
121
122/*
123 * The fraction of physical memory the quarantine is allowed to occupy.
124 * Quarantine doesn't support memory shrinker with SLAB allocator, so we keep
125 * the ratio low to avoid OOM.
126 */
127#define QUARANTINE_FRACTION 32
128
129static struct kmem_cache *qlink_to_cache(struct qlist_node *qlink)
130{
131 return virt_to_slab(qlink)->slab_cache;
132}
133
134static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache)
135{
136 struct kasan_free_meta *free_info =
137 container_of(qlink, struct kasan_free_meta,
138 quarantine_link);
139
140 return ((void *)free_info) - cache->kasan_info.free_meta_offset;
141}
142
143static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)
144{
145 void *object = qlink_to_object(qlink, cache);
146 struct kasan_free_meta *meta = kasan_get_free_meta(cache, object);
147 unsigned long flags;
148
149 if (IS_ENABLED(CONFIG_SLAB))
150 local_irq_save(flags);
151
152 /*
153 * If init_on_free is enabled and KASAN's free metadata is stored in
154 * the object, zero the metadata. Otherwise, the object's memory will
155 * not be properly zeroed, as KASAN saves the metadata after the slab
156 * allocator zeroes the object.
157 */
158 if (slab_want_init_on_free(cache) &&
159 cache->kasan_info.free_meta_offset == 0)
160 memzero_explicit(meta, sizeof(*meta));
161
162 /*
163 * As the object now gets freed from the quarantine, assume that its
164 * free track is no longer valid.
165 */
166 *(u8 *)kasan_mem_to_shadow(object) = KASAN_SLAB_FREE;
167
168 ___cache_free(cache, object, _THIS_IP_);
169
170 if (IS_ENABLED(CONFIG_SLAB))
171 local_irq_restore(flags);
172}
173
174static void qlist_free_all(struct qlist_head *q, struct kmem_cache *cache)
175{
176 struct qlist_node *qlink;
177
178 if (unlikely(qlist_empty(q)))
179 return;
180
181 qlink = q->head;
182 while (qlink) {
183 struct kmem_cache *obj_cache =
184 cache ? cache : qlink_to_cache(qlink);
185 struct qlist_node *next = qlink->next;
186
187 qlink_free(qlink, obj_cache);
188 qlink = next;
189 }
190 qlist_init(q);
191}
192
193bool kasan_quarantine_put(struct kmem_cache *cache, void *object)
194{
195 unsigned long flags;
196 struct qlist_head *q;
197 struct qlist_head temp = QLIST_INIT;
198 struct kasan_free_meta *meta = kasan_get_free_meta(cache, object);
199
200 /*
201 * If there's no metadata for this object, don't put it into
202 * quarantine.
203 */
204 if (!meta)
205 return false;
206
207 /*
208 * Note: irq must be disabled until after we move the batch to the
209 * global quarantine. Otherwise kasan_quarantine_remove_cache() can
210 * miss some objects belonging to the cache if they are in our local
211 * temp list. kasan_quarantine_remove_cache() executes on_each_cpu()
212 * at the beginning which ensures that it either sees the objects in
213 * per-cpu lists or in the global quarantine.
214 */
215 local_irq_save(flags);
216
217 q = this_cpu_ptr(&cpu_quarantine);
218 if (q->offline) {
219 local_irq_restore(flags);
220 return false;
221 }
222 qlist_put(q, &meta->quarantine_link, cache->size);
223 if (unlikely(q->bytes > QUARANTINE_PERCPU_SIZE)) {
224 qlist_move_all(q, &temp);
225
226 raw_spin_lock(&quarantine_lock);
227 WRITE_ONCE(quarantine_size, quarantine_size + temp.bytes);
228 qlist_move_all(&temp, &global_quarantine[quarantine_tail]);
229 if (global_quarantine[quarantine_tail].bytes >=
230 READ_ONCE(quarantine_batch_size)) {
231 int new_tail;
232
233 new_tail = quarantine_tail + 1;
234 if (new_tail == QUARANTINE_BATCHES)
235 new_tail = 0;
236 if (new_tail != quarantine_head)
237 quarantine_tail = new_tail;
238 }
239 raw_spin_unlock(&quarantine_lock);
240 }
241
242 local_irq_restore(flags);
243
244 return true;
245}
246
247void kasan_quarantine_reduce(void)
248{
249 size_t total_size, new_quarantine_size, percpu_quarantines;
250 unsigned long flags;
251 int srcu_idx;
252 struct qlist_head to_free = QLIST_INIT;
253
254 if (likely(READ_ONCE(quarantine_size) <=
255 READ_ONCE(quarantine_max_size)))
256 return;
257
258 /*
259 * srcu critical section ensures that kasan_quarantine_remove_cache()
260 * will not miss objects belonging to the cache while they are in our
261 * local to_free list. srcu is chosen because (1) it gives us private
262 * grace period domain that does not interfere with anything else,
263 * and (2) it allows synchronize_srcu() to return without waiting
264 * if there are no pending read critical sections (which is the
265 * expected case).
266 */
267 srcu_idx = srcu_read_lock(&remove_cache_srcu);
268 raw_spin_lock_irqsave(&quarantine_lock, flags);
269
270 /*
271 * Update quarantine size in case of hotplug. Allocate a fraction of
272 * the installed memory to quarantine minus per-cpu queue limits.
273 */
274 total_size = (totalram_pages() << PAGE_SHIFT) /
275 QUARANTINE_FRACTION;
276 percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
277 new_quarantine_size = (total_size < percpu_quarantines) ?
278 0 : total_size - percpu_quarantines;
279 WRITE_ONCE(quarantine_max_size, new_quarantine_size);
280 /* Aim at consuming at most 1/2 of slots in quarantine. */
281 WRITE_ONCE(quarantine_batch_size, max((size_t)QUARANTINE_PERCPU_SIZE,
282 2 * total_size / QUARANTINE_BATCHES));
283
284 if (likely(quarantine_size > quarantine_max_size)) {
285 qlist_move_all(&global_quarantine[quarantine_head], &to_free);
286 WRITE_ONCE(quarantine_size, quarantine_size - to_free.bytes);
287 quarantine_head++;
288 if (quarantine_head == QUARANTINE_BATCHES)
289 quarantine_head = 0;
290 }
291
292 raw_spin_unlock_irqrestore(&quarantine_lock, flags);
293
294 qlist_free_all(&to_free, NULL);
295 srcu_read_unlock(&remove_cache_srcu, srcu_idx);
296}
297
298static void qlist_move_cache(struct qlist_head *from,
299 struct qlist_head *to,
300 struct kmem_cache *cache)
301{
302 struct qlist_node *curr;
303
304 if (unlikely(qlist_empty(from)))
305 return;
306
307 curr = from->head;
308 qlist_init(from);
309 while (curr) {
310 struct qlist_node *next = curr->next;
311 struct kmem_cache *obj_cache = qlink_to_cache(curr);
312
313 if (obj_cache == cache)
314 qlist_put(to, curr, obj_cache->size);
315 else
316 qlist_put(from, curr, obj_cache->size);
317
318 curr = next;
319 }
320}
321
322#ifndef CONFIG_PREEMPT_RT
323static void __per_cpu_remove_cache(struct qlist_head *q, void *arg)
324{
325 struct kmem_cache *cache = arg;
326 struct qlist_head to_free = QLIST_INIT;
327
328 qlist_move_cache(q, &to_free, cache);
329 qlist_free_all(&to_free, cache);
330}
331#else
332static void __per_cpu_remove_cache(struct qlist_head *q, void *arg)
333{
334 struct kmem_cache *cache = arg;
335 unsigned long flags;
336 struct cpu_shrink_qlist *sq;
337
338 sq = this_cpu_ptr(&shrink_qlist);
339 raw_spin_lock_irqsave(&sq->lock, flags);
340 qlist_move_cache(q, &sq->qlist, cache);
341 raw_spin_unlock_irqrestore(&sq->lock, flags);
342}
343#endif
344
345static void per_cpu_remove_cache(void *arg)
346{
347 struct qlist_head *q;
348
349 q = this_cpu_ptr(&cpu_quarantine);
350 /*
351 * Ensure the ordering between the writing to q->offline and
352 * per_cpu_remove_cache. Prevent cpu_quarantine from being corrupted
353 * by interrupt.
354 */
355 if (READ_ONCE(q->offline))
356 return;
357 __per_cpu_remove_cache(q, arg);
358}
359
360/* Free all quarantined objects belonging to cache. */
361void kasan_quarantine_remove_cache(struct kmem_cache *cache)
362{
363 unsigned long flags, i;
364 struct qlist_head to_free = QLIST_INIT;
365
366 /*
367 * Must be careful to not miss any objects that are being moved from
368 * per-cpu list to the global quarantine in kasan_quarantine_put(),
369 * nor objects being freed in kasan_quarantine_reduce(). on_each_cpu()
370 * achieves the first goal, while synchronize_srcu() achieves the
371 * second.
372 */
373 on_each_cpu(per_cpu_remove_cache, cache, 1);
374
375#ifdef CONFIG_PREEMPT_RT
376 {
377 int cpu;
378 struct cpu_shrink_qlist *sq;
379
380 for_each_online_cpu(cpu) {
381 sq = per_cpu_ptr(&shrink_qlist, cpu);
382 raw_spin_lock_irqsave(&sq->lock, flags);
383 qlist_move_cache(&sq->qlist, &to_free, cache);
384 raw_spin_unlock_irqrestore(&sq->lock, flags);
385 }
386 qlist_free_all(&to_free, cache);
387 }
388#endif
389
390 raw_spin_lock_irqsave(&quarantine_lock, flags);
391 for (i = 0; i < QUARANTINE_BATCHES; i++) {
392 if (qlist_empty(&global_quarantine[i]))
393 continue;
394 qlist_move_cache(&global_quarantine[i], &to_free, cache);
395 /* Scanning whole quarantine can take a while. */
396 raw_spin_unlock_irqrestore(&quarantine_lock, flags);
397 cond_resched();
398 raw_spin_lock_irqsave(&quarantine_lock, flags);
399 }
400 raw_spin_unlock_irqrestore(&quarantine_lock, flags);
401
402 qlist_free_all(&to_free, cache);
403
404 synchronize_srcu(&remove_cache_srcu);
405}
406
407static int kasan_cpu_online(unsigned int cpu)
408{
409 this_cpu_ptr(&cpu_quarantine)->offline = false;
410 return 0;
411}
412
413static int kasan_cpu_offline(unsigned int cpu)
414{
415 struct qlist_head *q;
416
417 q = this_cpu_ptr(&cpu_quarantine);
418 /* Ensure the ordering between the writing to q->offline and
419 * qlist_free_all. Otherwise, cpu_quarantine may be corrupted
420 * by interrupt.
421 */
422 WRITE_ONCE(q->offline, true);
423 barrier();
424 qlist_free_all(q, NULL);
425 return 0;
426}
427
428static int __init kasan_cpu_quarantine_init(void)
429{
430 int ret = 0;
431
432 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mm/kasan:online",
433 kasan_cpu_online, kasan_cpu_offline);
434 if (ret < 0)
435 pr_err("kasan cpu quarantine register failed [%d]\n", ret);
436 return ret;
437}
438late_initcall(kasan_cpu_quarantine_init);