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