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