1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Slab allocator functions that are independent of the allocator strategy 4 * 5 * (C) 2012 Christoph Lameter <cl@linux.com> 6 */ 7 #include <linux/slab.h> 8 9 #include <linux/mm.h> 10 #include <linux/poison.h> 11 #include <linux/interrupt.h> 12 #include <linux/memory.h> 13 #include <linux/cache.h> 14 #include <linux/compiler.h> 15 #include <linux/kfence.h> 16 #include <linux/module.h> 17 #include <linux/cpu.h> 18 #include <linux/uaccess.h> 19 #include <linux/seq_file.h> 20 #include <linux/dma-mapping.h> 21 #include <linux/swiotlb.h> 22 #include <linux/proc_fs.h> 23 #include <linux/debugfs.h> 24 #include <linux/kmemleak.h> 25 #include <linux/kasan.h> 26 #include <asm/cacheflush.h> 27 #include <asm/tlbflush.h> 28 #include <asm/page.h> 29 #include <linux/memcontrol.h> 30 #include <linux/stackdepot.h> 31 #include <trace/events/rcu.h> 32 33 #include "../kernel/rcu/rcu.h" 34 #include "internal.h" 35 #include "slab.h" 36 37 #define CREATE_TRACE_POINTS 38 #include <trace/events/kmem.h> 39 40 enum slab_state slab_state; 41 LIST_HEAD(slab_caches); 42 DEFINE_MUTEX(slab_mutex); 43 struct kmem_cache *kmem_cache; 44 45 /* 46 * Set of flags that will prevent slab merging 47 */ 48 #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ 49 SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \ 50 SLAB_FAILSLAB | SLAB_NO_MERGE) 51 52 #define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \ 53 SLAB_CACHE_DMA32 | SLAB_ACCOUNT) 54 55 /* 56 * Merge control. If this is set then no merging of slab caches will occur. 57 */ 58 static bool slab_nomerge = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT); 59 60 static int __init setup_slab_nomerge(char *str) 61 { 62 slab_nomerge = true; 63 return 1; 64 } 65 66 static int __init setup_slab_merge(char *str) 67 { 68 slab_nomerge = false; 69 return 1; 70 } 71 72 __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0); 73 __setup_param("slub_merge", slub_merge, setup_slab_merge, 0); 74 75 __setup("slab_nomerge", setup_slab_nomerge); 76 __setup("slab_merge", setup_slab_merge); 77 78 /* 79 * Determine the size of a slab object 80 */ 81 unsigned int kmem_cache_size(struct kmem_cache *s) 82 { 83 return s->object_size; 84 } 85 EXPORT_SYMBOL(kmem_cache_size); 86 87 #ifdef CONFIG_DEBUG_VM 88 89 static bool kmem_cache_is_duplicate_name(const char *name) 90 { 91 struct kmem_cache *s; 92 93 list_for_each_entry(s, &slab_caches, list) { 94 if (!strcmp(s->name, name)) 95 return true; 96 } 97 98 return false; 99 } 100 101 static int kmem_cache_sanity_check(const char *name, unsigned int size) 102 { 103 if (!name || in_interrupt() || size > KMALLOC_MAX_SIZE) { 104 pr_err("kmem_cache_create(%s) integrity check failed\n", name); 105 return -EINVAL; 106 } 107 108 /* Duplicate names will confuse slabtop, et al */ 109 WARN(kmem_cache_is_duplicate_name(name), 110 "kmem_cache of name '%s' already exists\n", name); 111 112 WARN_ON(strchr(name, ' ')); /* It confuses parsers */ 113 return 0; 114 } 115 #else 116 static inline int kmem_cache_sanity_check(const char *name, unsigned int size) 117 { 118 return 0; 119 } 120 #endif 121 122 /* 123 * Figure out what the alignment of the objects will be given a set of 124 * flags, a user specified alignment and the size of the objects. 125 */ 126 static unsigned int calculate_alignment(slab_flags_t flags, 127 unsigned int align, unsigned int size) 128 { 129 /* 130 * If the user wants hardware cache aligned objects then follow that 131 * suggestion if the object is sufficiently large. 132 * 133 * The hardware cache alignment cannot override the specified 134 * alignment though. If that is greater then use it. 135 */ 136 if (flags & SLAB_HWCACHE_ALIGN) { 137 unsigned int ralign; 138 139 ralign = cache_line_size(); 140 while (size <= ralign / 2) 141 ralign /= 2; 142 align = max(align, ralign); 143 } 144 145 align = max(align, arch_slab_minalign()); 146 147 return ALIGN(align, sizeof(void *)); 148 } 149 150 /* 151 * Find a mergeable slab cache 152 */ 153 int slab_unmergeable(struct kmem_cache *s) 154 { 155 if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE)) 156 return 1; 157 158 if (s->ctor) 159 return 1; 160 161 #ifdef CONFIG_HARDENED_USERCOPY 162 if (s->usersize) 163 return 1; 164 #endif 165 166 /* 167 * We may have set a slab to be unmergeable during bootstrap. 168 */ 169 if (s->refcount < 0) 170 return 1; 171 172 return 0; 173 } 174 175 struct kmem_cache *find_mergeable(unsigned int size, unsigned int align, 176 slab_flags_t flags, const char *name, void (*ctor)(void *)) 177 { 178 struct kmem_cache *s; 179 180 if (slab_nomerge) 181 return NULL; 182 183 if (ctor) 184 return NULL; 185 186 flags = kmem_cache_flags(flags, name); 187 188 if (flags & SLAB_NEVER_MERGE) 189 return NULL; 190 191 size = ALIGN(size, sizeof(void *)); 192 align = calculate_alignment(flags, align, size); 193 size = ALIGN(size, align); 194 195 list_for_each_entry_reverse(s, &slab_caches, list) { 196 if (slab_unmergeable(s)) 197 continue; 198 199 if (size > s->size) 200 continue; 201 202 if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME)) 203 continue; 204 /* 205 * Check if alignment is compatible. 206 * Courtesy of Adrian Drzewiecki 207 */ 208 if ((s->size & ~(align - 1)) != s->size) 209 continue; 210 211 if (s->size - size >= sizeof(void *)) 212 continue; 213 214 return s; 215 } 216 return NULL; 217 } 218 219 static struct kmem_cache *create_cache(const char *name, 220 unsigned int object_size, 221 struct kmem_cache_args *args, 222 slab_flags_t flags) 223 { 224 struct kmem_cache *s; 225 int err; 226 227 /* If a custom freelist pointer is requested make sure it's sane. */ 228 err = -EINVAL; 229 if (args->use_freeptr_offset && 230 (args->freeptr_offset >= object_size || 231 !(flags & SLAB_TYPESAFE_BY_RCU) || 232 !IS_ALIGNED(args->freeptr_offset, __alignof__(freeptr_t)))) 233 goto out; 234 235 err = -ENOMEM; 236 s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL); 237 if (!s) 238 goto out; 239 err = do_kmem_cache_create(s, name, object_size, args, flags); 240 if (err) 241 goto out_free_cache; 242 243 s->refcount = 1; 244 list_add(&s->list, &slab_caches); 245 return s; 246 247 out_free_cache: 248 kmem_cache_free(kmem_cache, s); 249 out: 250 return ERR_PTR(err); 251 } 252 253 /** 254 * __kmem_cache_create_args - Create a kmem cache. 255 * @name: A string which is used in /proc/slabinfo to identify this cache. 256 * @object_size: The size of objects to be created in this cache. 257 * @args: Additional arguments for the cache creation (see 258 * &struct kmem_cache_args). 259 * @flags: See the desriptions of individual flags. The common ones are listed 260 * in the description below. 261 * 262 * Not to be called directly, use the kmem_cache_create() wrapper with the same 263 * parameters. 264 * 265 * Commonly used @flags: 266 * 267 * &SLAB_ACCOUNT - Account allocations to memcg. 268 * 269 * &SLAB_HWCACHE_ALIGN - Align objects on cache line boundaries. 270 * 271 * &SLAB_RECLAIM_ACCOUNT - Objects are reclaimable. 272 * 273 * &SLAB_TYPESAFE_BY_RCU - Slab page (not individual objects) freeing delayed 274 * by a grace period - see the full description before using. 275 * 276 * Context: Cannot be called within a interrupt, but can be interrupted. 277 * 278 * Return: a pointer to the cache on success, NULL on failure. 279 */ 280 struct kmem_cache *__kmem_cache_create_args(const char *name, 281 unsigned int object_size, 282 struct kmem_cache_args *args, 283 slab_flags_t flags) 284 { 285 struct kmem_cache *s = NULL; 286 const char *cache_name; 287 int err; 288 289 #ifdef CONFIG_SLUB_DEBUG 290 /* 291 * If no slab_debug was enabled globally, the static key is not yet 292 * enabled by setup_slub_debug(). Enable it if the cache is being 293 * created with any of the debugging flags passed explicitly. 294 * It's also possible that this is the first cache created with 295 * SLAB_STORE_USER and we should init stack_depot for it. 296 */ 297 if (flags & SLAB_DEBUG_FLAGS) 298 static_branch_enable(&slub_debug_enabled); 299 if (flags & SLAB_STORE_USER) 300 stack_depot_init(); 301 #endif 302 303 mutex_lock(&slab_mutex); 304 305 err = kmem_cache_sanity_check(name, object_size); 306 if (err) { 307 goto out_unlock; 308 } 309 310 /* Refuse requests with allocator specific flags */ 311 if (flags & ~SLAB_FLAGS_PERMITTED) { 312 err = -EINVAL; 313 goto out_unlock; 314 } 315 316 /* 317 * Some allocators will constraint the set of valid flags to a subset 318 * of all flags. We expect them to define CACHE_CREATE_MASK in this 319 * case, and we'll just provide them with a sanitized version of the 320 * passed flags. 321 */ 322 flags &= CACHE_CREATE_MASK; 323 324 /* Fail closed on bad usersize of useroffset values. */ 325 if (!IS_ENABLED(CONFIG_HARDENED_USERCOPY) || 326 WARN_ON(!args->usersize && args->useroffset) || 327 WARN_ON(object_size < args->usersize || 328 object_size - args->usersize < args->useroffset)) 329 args->usersize = args->useroffset = 0; 330 331 if (!args->usersize) 332 s = __kmem_cache_alias(name, object_size, args->align, flags, 333 args->ctor); 334 if (s) 335 goto out_unlock; 336 337 cache_name = kstrdup_const(name, GFP_KERNEL); 338 if (!cache_name) { 339 err = -ENOMEM; 340 goto out_unlock; 341 } 342 343 args->align = calculate_alignment(flags, args->align, object_size); 344 s = create_cache(cache_name, object_size, args, flags); 345 if (IS_ERR(s)) { 346 err = PTR_ERR(s); 347 kfree_const(cache_name); 348 } 349 350 out_unlock: 351 mutex_unlock(&slab_mutex); 352 353 if (err) { 354 if (flags & SLAB_PANIC) 355 panic("%s: Failed to create slab '%s'. Error %d\n", 356 __func__, name, err); 357 else { 358 pr_warn("%s(%s) failed with error %d\n", 359 __func__, name, err); 360 dump_stack(); 361 } 362 return NULL; 363 } 364 return s; 365 } 366 EXPORT_SYMBOL(__kmem_cache_create_args); 367 368 static struct kmem_cache *kmem_buckets_cache __ro_after_init; 369 370 /** 371 * kmem_buckets_create - Create a set of caches that handle dynamic sized 372 * allocations via kmem_buckets_alloc() 373 * @name: A prefix string which is used in /proc/slabinfo to identify this 374 * cache. The individual caches with have their sizes as the suffix. 375 * @flags: SLAB flags (see kmem_cache_create() for details). 376 * @useroffset: Starting offset within an allocation that may be copied 377 * to/from userspace. 378 * @usersize: How many bytes, starting at @useroffset, may be copied 379 * to/from userspace. 380 * @ctor: A constructor for the objects, run when new allocations are made. 381 * 382 * Cannot be called within an interrupt, but can be interrupted. 383 * 384 * Return: a pointer to the cache on success, NULL on failure. When 385 * CONFIG_SLAB_BUCKETS is not enabled, ZERO_SIZE_PTR is returned, and 386 * subsequent calls to kmem_buckets_alloc() will fall back to kmalloc(). 387 * (i.e. callers only need to check for NULL on failure.) 388 */ 389 kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags, 390 unsigned int useroffset, 391 unsigned int usersize, 392 void (*ctor)(void *)) 393 { 394 unsigned long mask = 0; 395 unsigned int idx; 396 kmem_buckets *b; 397 398 BUILD_BUG_ON(ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]) > BITS_PER_LONG); 399 400 /* 401 * When the separate buckets API is not built in, just return 402 * a non-NULL value for the kmem_buckets pointer, which will be 403 * unused when performing allocations. 404 */ 405 if (!IS_ENABLED(CONFIG_SLAB_BUCKETS)) 406 return ZERO_SIZE_PTR; 407 408 if (WARN_ON(!kmem_buckets_cache)) 409 return NULL; 410 411 b = kmem_cache_alloc(kmem_buckets_cache, GFP_KERNEL|__GFP_ZERO); 412 if (WARN_ON(!b)) 413 return NULL; 414 415 flags |= SLAB_NO_MERGE; 416 417 for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]); idx++) { 418 char *short_size, *cache_name; 419 unsigned int cache_useroffset, cache_usersize; 420 unsigned int size, aligned_idx; 421 422 if (!kmalloc_caches[KMALLOC_NORMAL][idx]) 423 continue; 424 425 size = kmalloc_caches[KMALLOC_NORMAL][idx]->object_size; 426 if (!size) 427 continue; 428 429 short_size = strchr(kmalloc_caches[KMALLOC_NORMAL][idx]->name, '-'); 430 if (WARN_ON(!short_size)) 431 goto fail; 432 433 if (useroffset >= size) { 434 cache_useroffset = 0; 435 cache_usersize = 0; 436 } else { 437 cache_useroffset = useroffset; 438 cache_usersize = min(size - cache_useroffset, usersize); 439 } 440 441 aligned_idx = __kmalloc_index(size, false); 442 if (!(*b)[aligned_idx]) { 443 cache_name = kasprintf(GFP_KERNEL, "%s-%s", name, short_size + 1); 444 if (WARN_ON(!cache_name)) 445 goto fail; 446 (*b)[aligned_idx] = kmem_cache_create_usercopy(cache_name, size, 447 0, flags, cache_useroffset, 448 cache_usersize, ctor); 449 kfree(cache_name); 450 if (WARN_ON(!(*b)[aligned_idx])) 451 goto fail; 452 set_bit(aligned_idx, &mask); 453 } 454 if (idx != aligned_idx) 455 (*b)[idx] = (*b)[aligned_idx]; 456 } 457 458 return b; 459 460 fail: 461 for_each_set_bit(idx, &mask, ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL])) 462 kmem_cache_destroy((*b)[idx]); 463 kmem_cache_free(kmem_buckets_cache, b); 464 465 return NULL; 466 } 467 EXPORT_SYMBOL(kmem_buckets_create); 468 469 /* 470 * For a given kmem_cache, kmem_cache_destroy() should only be called 471 * once or there will be a use-after-free problem. The actual deletion 472 * and release of the kobject does not need slab_mutex or cpu_hotplug_lock 473 * protection. So they are now done without holding those locks. 474 */ 475 static void kmem_cache_release(struct kmem_cache *s) 476 { 477 kfence_shutdown_cache(s); 478 if (__is_defined(SLAB_SUPPORTS_SYSFS) && slab_state >= FULL) 479 sysfs_slab_release(s); 480 else 481 slab_kmem_cache_release(s); 482 } 483 484 void slab_kmem_cache_release(struct kmem_cache *s) 485 { 486 __kmem_cache_release(s); 487 kfree_const(s->name); 488 kmem_cache_free(kmem_cache, s); 489 } 490 491 void kmem_cache_destroy(struct kmem_cache *s) 492 { 493 int err; 494 495 if (unlikely(!s) || !kasan_check_byte(s)) 496 return; 497 498 /* in-flight kfree_rcu()'s may include objects from our cache */ 499 kvfree_rcu_barrier(); 500 501 if (IS_ENABLED(CONFIG_SLUB_RCU_DEBUG) && 502 (s->flags & SLAB_TYPESAFE_BY_RCU)) { 503 /* 504 * Under CONFIG_SLUB_RCU_DEBUG, when objects in a 505 * SLAB_TYPESAFE_BY_RCU slab are freed, SLUB will internally 506 * defer their freeing with call_rcu(). 507 * Wait for such call_rcu() invocations here before actually 508 * destroying the cache. 509 * 510 * It doesn't matter that we haven't looked at the slab refcount 511 * yet - slabs with SLAB_TYPESAFE_BY_RCU can't be merged, so 512 * the refcount should be 1 here. 513 */ 514 rcu_barrier(); 515 } 516 517 cpus_read_lock(); 518 mutex_lock(&slab_mutex); 519 520 s->refcount--; 521 if (s->refcount) { 522 mutex_unlock(&slab_mutex); 523 cpus_read_unlock(); 524 return; 525 } 526 527 /* free asan quarantined objects */ 528 kasan_cache_shutdown(s); 529 530 err = __kmem_cache_shutdown(s); 531 if (!slab_in_kunit_test()) 532 WARN(err, "%s %s: Slab cache still has objects when called from %pS", 533 __func__, s->name, (void *)_RET_IP_); 534 535 list_del(&s->list); 536 537 mutex_unlock(&slab_mutex); 538 cpus_read_unlock(); 539 540 if (slab_state >= FULL) 541 sysfs_slab_unlink(s); 542 debugfs_slab_release(s); 543 544 if (err) 545 return; 546 547 if (s->flags & SLAB_TYPESAFE_BY_RCU) 548 rcu_barrier(); 549 550 kmem_cache_release(s); 551 } 552 EXPORT_SYMBOL(kmem_cache_destroy); 553 554 /** 555 * kmem_cache_shrink - Shrink a cache. 556 * @cachep: The cache to shrink. 557 * 558 * Releases as many slabs as possible for a cache. 559 * To help debugging, a zero exit status indicates all slabs were released. 560 * 561 * Return: %0 if all slabs were released, non-zero otherwise 562 */ 563 int kmem_cache_shrink(struct kmem_cache *cachep) 564 { 565 kasan_cache_shrink(cachep); 566 567 return __kmem_cache_shrink(cachep); 568 } 569 EXPORT_SYMBOL(kmem_cache_shrink); 570 571 bool slab_is_available(void) 572 { 573 return slab_state >= UP; 574 } 575 576 #ifdef CONFIG_PRINTK 577 static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab) 578 { 579 if (__kfence_obj_info(kpp, object, slab)) 580 return; 581 __kmem_obj_info(kpp, object, slab); 582 } 583 584 /** 585 * kmem_dump_obj - Print available slab provenance information 586 * @object: slab object for which to find provenance information. 587 * 588 * This function uses pr_cont(), so that the caller is expected to have 589 * printed out whatever preamble is appropriate. The provenance information 590 * depends on the type of object and on how much debugging is enabled. 591 * For a slab-cache object, the fact that it is a slab object is printed, 592 * and, if available, the slab name, return address, and stack trace from 593 * the allocation and last free path of that object. 594 * 595 * Return: %true if the pointer is to a not-yet-freed object from 596 * kmalloc() or kmem_cache_alloc(), either %true or %false if the pointer 597 * is to an already-freed object, and %false otherwise. 598 */ 599 bool kmem_dump_obj(void *object) 600 { 601 char *cp = IS_ENABLED(CONFIG_MMU) ? "" : "/vmalloc"; 602 int i; 603 struct slab *slab; 604 unsigned long ptroffset; 605 struct kmem_obj_info kp = { }; 606 607 /* Some arches consider ZERO_SIZE_PTR to be a valid address. */ 608 if (object < (void *)PAGE_SIZE || !virt_addr_valid(object)) 609 return false; 610 slab = virt_to_slab(object); 611 if (!slab) 612 return false; 613 614 kmem_obj_info(&kp, object, slab); 615 if (kp.kp_slab_cache) 616 pr_cont(" slab%s %s", cp, kp.kp_slab_cache->name); 617 else 618 pr_cont(" slab%s", cp); 619 if (is_kfence_address(object)) 620 pr_cont(" (kfence)"); 621 if (kp.kp_objp) 622 pr_cont(" start %px", kp.kp_objp); 623 if (kp.kp_data_offset) 624 pr_cont(" data offset %lu", kp.kp_data_offset); 625 if (kp.kp_objp) { 626 ptroffset = ((char *)object - (char *)kp.kp_objp) - kp.kp_data_offset; 627 pr_cont(" pointer offset %lu", ptroffset); 628 } 629 if (kp.kp_slab_cache && kp.kp_slab_cache->object_size) 630 pr_cont(" size %u", kp.kp_slab_cache->object_size); 631 if (kp.kp_ret) 632 pr_cont(" allocated at %pS\n", kp.kp_ret); 633 else 634 pr_cont("\n"); 635 for (i = 0; i < ARRAY_SIZE(kp.kp_stack); i++) { 636 if (!kp.kp_stack[i]) 637 break; 638 pr_info(" %pS\n", kp.kp_stack[i]); 639 } 640 641 if (kp.kp_free_stack[0]) 642 pr_cont(" Free path:\n"); 643 644 for (i = 0; i < ARRAY_SIZE(kp.kp_free_stack); i++) { 645 if (!kp.kp_free_stack[i]) 646 break; 647 pr_info(" %pS\n", kp.kp_free_stack[i]); 648 } 649 650 return true; 651 } 652 EXPORT_SYMBOL_GPL(kmem_dump_obj); 653 #endif 654 655 /* Create a cache during boot when no slab services are available yet */ 656 void __init create_boot_cache(struct kmem_cache *s, const char *name, 657 unsigned int size, slab_flags_t flags, 658 unsigned int useroffset, unsigned int usersize) 659 { 660 int err; 661 unsigned int align = ARCH_KMALLOC_MINALIGN; 662 struct kmem_cache_args kmem_args = {}; 663 664 /* 665 * kmalloc caches guarantee alignment of at least the largest 666 * power-of-two divisor of the size. For power-of-two sizes, 667 * it is the size itself. 668 */ 669 if (flags & SLAB_KMALLOC) 670 align = max(align, 1U << (ffs(size) - 1)); 671 kmem_args.align = calculate_alignment(flags, align, size); 672 673 #ifdef CONFIG_HARDENED_USERCOPY 674 kmem_args.useroffset = useroffset; 675 kmem_args.usersize = usersize; 676 #endif 677 678 err = do_kmem_cache_create(s, name, size, &kmem_args, flags); 679 680 if (err) 681 panic("Creation of kmalloc slab %s size=%u failed. Reason %d\n", 682 name, size, err); 683 684 s->refcount = -1; /* Exempt from merging for now */ 685 } 686 687 static struct kmem_cache *__init create_kmalloc_cache(const char *name, 688 unsigned int size, 689 slab_flags_t flags) 690 { 691 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT); 692 693 if (!s) 694 panic("Out of memory when creating slab %s\n", name); 695 696 create_boot_cache(s, name, size, flags | SLAB_KMALLOC, 0, size); 697 list_add(&s->list, &slab_caches); 698 s->refcount = 1; 699 return s; 700 } 701 702 kmem_buckets kmalloc_caches[NR_KMALLOC_TYPES] __ro_after_init = 703 { /* initialization for https://llvm.org/pr42570 */ }; 704 EXPORT_SYMBOL(kmalloc_caches); 705 706 #ifdef CONFIG_RANDOM_KMALLOC_CACHES 707 unsigned long random_kmalloc_seed __ro_after_init; 708 EXPORT_SYMBOL(random_kmalloc_seed); 709 #endif 710 711 /* 712 * Conversion table for small slabs sizes / 8 to the index in the 713 * kmalloc array. This is necessary for slabs < 192 since we have non power 714 * of two cache sizes there. The size of larger slabs can be determined using 715 * fls. 716 */ 717 u8 kmalloc_size_index[24] __ro_after_init = { 718 3, /* 8 */ 719 4, /* 16 */ 720 5, /* 24 */ 721 5, /* 32 */ 722 6, /* 40 */ 723 6, /* 48 */ 724 6, /* 56 */ 725 6, /* 64 */ 726 1, /* 72 */ 727 1, /* 80 */ 728 1, /* 88 */ 729 1, /* 96 */ 730 7, /* 104 */ 731 7, /* 112 */ 732 7, /* 120 */ 733 7, /* 128 */ 734 2, /* 136 */ 735 2, /* 144 */ 736 2, /* 152 */ 737 2, /* 160 */ 738 2, /* 168 */ 739 2, /* 176 */ 740 2, /* 184 */ 741 2 /* 192 */ 742 }; 743 744 size_t kmalloc_size_roundup(size_t size) 745 { 746 if (size && size <= KMALLOC_MAX_CACHE_SIZE) { 747 /* 748 * The flags don't matter since size_index is common to all. 749 * Neither does the caller for just getting ->object_size. 750 */ 751 return kmalloc_slab(size, NULL, GFP_KERNEL, 0)->object_size; 752 } 753 754 /* Above the smaller buckets, size is a multiple of page size. */ 755 if (size && size <= KMALLOC_MAX_SIZE) 756 return PAGE_SIZE << get_order(size); 757 758 /* 759 * Return 'size' for 0 - kmalloc() returns ZERO_SIZE_PTR 760 * and very large size - kmalloc() may fail. 761 */ 762 return size; 763 764 } 765 EXPORT_SYMBOL(kmalloc_size_roundup); 766 767 #ifdef CONFIG_ZONE_DMA 768 #define KMALLOC_DMA_NAME(sz) .name[KMALLOC_DMA] = "dma-kmalloc-" #sz, 769 #else 770 #define KMALLOC_DMA_NAME(sz) 771 #endif 772 773 #ifdef CONFIG_MEMCG 774 #define KMALLOC_CGROUP_NAME(sz) .name[KMALLOC_CGROUP] = "kmalloc-cg-" #sz, 775 #else 776 #define KMALLOC_CGROUP_NAME(sz) 777 #endif 778 779 #ifndef CONFIG_SLUB_TINY 780 #define KMALLOC_RCL_NAME(sz) .name[KMALLOC_RECLAIM] = "kmalloc-rcl-" #sz, 781 #else 782 #define KMALLOC_RCL_NAME(sz) 783 #endif 784 785 #ifdef CONFIG_RANDOM_KMALLOC_CACHES 786 #define __KMALLOC_RANDOM_CONCAT(a, b) a ## b 787 #define KMALLOC_RANDOM_NAME(N, sz) __KMALLOC_RANDOM_CONCAT(KMA_RAND_, N)(sz) 788 #define KMA_RAND_1(sz) .name[KMALLOC_RANDOM_START + 1] = "kmalloc-rnd-01-" #sz, 789 #define KMA_RAND_2(sz) KMA_RAND_1(sz) .name[KMALLOC_RANDOM_START + 2] = "kmalloc-rnd-02-" #sz, 790 #define KMA_RAND_3(sz) KMA_RAND_2(sz) .name[KMALLOC_RANDOM_START + 3] = "kmalloc-rnd-03-" #sz, 791 #define KMA_RAND_4(sz) KMA_RAND_3(sz) .name[KMALLOC_RANDOM_START + 4] = "kmalloc-rnd-04-" #sz, 792 #define KMA_RAND_5(sz) KMA_RAND_4(sz) .name[KMALLOC_RANDOM_START + 5] = "kmalloc-rnd-05-" #sz, 793 #define KMA_RAND_6(sz) KMA_RAND_5(sz) .name[KMALLOC_RANDOM_START + 6] = "kmalloc-rnd-06-" #sz, 794 #define KMA_RAND_7(sz) KMA_RAND_6(sz) .name[KMALLOC_RANDOM_START + 7] = "kmalloc-rnd-07-" #sz, 795 #define KMA_RAND_8(sz) KMA_RAND_7(sz) .name[KMALLOC_RANDOM_START + 8] = "kmalloc-rnd-08-" #sz, 796 #define KMA_RAND_9(sz) KMA_RAND_8(sz) .name[KMALLOC_RANDOM_START + 9] = "kmalloc-rnd-09-" #sz, 797 #define KMA_RAND_10(sz) KMA_RAND_9(sz) .name[KMALLOC_RANDOM_START + 10] = "kmalloc-rnd-10-" #sz, 798 #define KMA_RAND_11(sz) KMA_RAND_10(sz) .name[KMALLOC_RANDOM_START + 11] = "kmalloc-rnd-11-" #sz, 799 #define KMA_RAND_12(sz) KMA_RAND_11(sz) .name[KMALLOC_RANDOM_START + 12] = "kmalloc-rnd-12-" #sz, 800 #define KMA_RAND_13(sz) KMA_RAND_12(sz) .name[KMALLOC_RANDOM_START + 13] = "kmalloc-rnd-13-" #sz, 801 #define KMA_RAND_14(sz) KMA_RAND_13(sz) .name[KMALLOC_RANDOM_START + 14] = "kmalloc-rnd-14-" #sz, 802 #define KMA_RAND_15(sz) KMA_RAND_14(sz) .name[KMALLOC_RANDOM_START + 15] = "kmalloc-rnd-15-" #sz, 803 #else // CONFIG_RANDOM_KMALLOC_CACHES 804 #define KMALLOC_RANDOM_NAME(N, sz) 805 #endif 806 807 #define INIT_KMALLOC_INFO(__size, __short_size) \ 808 { \ 809 .name[KMALLOC_NORMAL] = "kmalloc-" #__short_size, \ 810 KMALLOC_RCL_NAME(__short_size) \ 811 KMALLOC_CGROUP_NAME(__short_size) \ 812 KMALLOC_DMA_NAME(__short_size) \ 813 KMALLOC_RANDOM_NAME(RANDOM_KMALLOC_CACHES_NR, __short_size) \ 814 .size = __size, \ 815 } 816 817 /* 818 * kmalloc_info[] is to make slab_debug=,kmalloc-xx option work at boot time. 819 * kmalloc_index() supports up to 2^21=2MB, so the final entry of the table is 820 * kmalloc-2M. 821 */ 822 const struct kmalloc_info_struct kmalloc_info[] __initconst = { 823 INIT_KMALLOC_INFO(0, 0), 824 INIT_KMALLOC_INFO(96, 96), 825 INIT_KMALLOC_INFO(192, 192), 826 INIT_KMALLOC_INFO(8, 8), 827 INIT_KMALLOC_INFO(16, 16), 828 INIT_KMALLOC_INFO(32, 32), 829 INIT_KMALLOC_INFO(64, 64), 830 INIT_KMALLOC_INFO(128, 128), 831 INIT_KMALLOC_INFO(256, 256), 832 INIT_KMALLOC_INFO(512, 512), 833 INIT_KMALLOC_INFO(1024, 1k), 834 INIT_KMALLOC_INFO(2048, 2k), 835 INIT_KMALLOC_INFO(4096, 4k), 836 INIT_KMALLOC_INFO(8192, 8k), 837 INIT_KMALLOC_INFO(16384, 16k), 838 INIT_KMALLOC_INFO(32768, 32k), 839 INIT_KMALLOC_INFO(65536, 64k), 840 INIT_KMALLOC_INFO(131072, 128k), 841 INIT_KMALLOC_INFO(262144, 256k), 842 INIT_KMALLOC_INFO(524288, 512k), 843 INIT_KMALLOC_INFO(1048576, 1M), 844 INIT_KMALLOC_INFO(2097152, 2M) 845 }; 846 847 /* 848 * Patch up the size_index table if we have strange large alignment 849 * requirements for the kmalloc array. This is only the case for 850 * MIPS it seems. The standard arches will not generate any code here. 851 * 852 * Largest permitted alignment is 256 bytes due to the way we 853 * handle the index determination for the smaller caches. 854 * 855 * Make sure that nothing crazy happens if someone starts tinkering 856 * around with ARCH_KMALLOC_MINALIGN 857 */ 858 void __init setup_kmalloc_cache_index_table(void) 859 { 860 unsigned int i; 861 862 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 || 863 !is_power_of_2(KMALLOC_MIN_SIZE)); 864 865 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) { 866 unsigned int elem = size_index_elem(i); 867 868 if (elem >= ARRAY_SIZE(kmalloc_size_index)) 869 break; 870 kmalloc_size_index[elem] = KMALLOC_SHIFT_LOW; 871 } 872 873 if (KMALLOC_MIN_SIZE >= 64) { 874 /* 875 * The 96 byte sized cache is not used if the alignment 876 * is 64 byte. 877 */ 878 for (i = 64 + 8; i <= 96; i += 8) 879 kmalloc_size_index[size_index_elem(i)] = 7; 880 881 } 882 883 if (KMALLOC_MIN_SIZE >= 128) { 884 /* 885 * The 192 byte sized cache is not used if the alignment 886 * is 128 byte. Redirect kmalloc to use the 256 byte cache 887 * instead. 888 */ 889 for (i = 128 + 8; i <= 192; i += 8) 890 kmalloc_size_index[size_index_elem(i)] = 8; 891 } 892 } 893 894 static unsigned int __kmalloc_minalign(void) 895 { 896 unsigned int minalign = dma_get_cache_alignment(); 897 898 if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && 899 is_swiotlb_allocated()) 900 minalign = ARCH_KMALLOC_MINALIGN; 901 902 return max(minalign, arch_slab_minalign()); 903 } 904 905 static void __init 906 new_kmalloc_cache(int idx, enum kmalloc_cache_type type) 907 { 908 slab_flags_t flags = 0; 909 unsigned int minalign = __kmalloc_minalign(); 910 unsigned int aligned_size = kmalloc_info[idx].size; 911 int aligned_idx = idx; 912 913 if ((KMALLOC_RECLAIM != KMALLOC_NORMAL) && (type == KMALLOC_RECLAIM)) { 914 flags |= SLAB_RECLAIM_ACCOUNT; 915 } else if (IS_ENABLED(CONFIG_MEMCG) && (type == KMALLOC_CGROUP)) { 916 if (mem_cgroup_kmem_disabled()) { 917 kmalloc_caches[type][idx] = kmalloc_caches[KMALLOC_NORMAL][idx]; 918 return; 919 } 920 flags |= SLAB_ACCOUNT; 921 } else if (IS_ENABLED(CONFIG_ZONE_DMA) && (type == KMALLOC_DMA)) { 922 flags |= SLAB_CACHE_DMA; 923 } 924 925 #ifdef CONFIG_RANDOM_KMALLOC_CACHES 926 if (type >= KMALLOC_RANDOM_START && type <= KMALLOC_RANDOM_END) 927 flags |= SLAB_NO_MERGE; 928 #endif 929 930 /* 931 * If CONFIG_MEMCG is enabled, disable cache merging for 932 * KMALLOC_NORMAL caches. 933 */ 934 if (IS_ENABLED(CONFIG_MEMCG) && (type == KMALLOC_NORMAL)) 935 flags |= SLAB_NO_MERGE; 936 937 if (minalign > ARCH_KMALLOC_MINALIGN) { 938 aligned_size = ALIGN(aligned_size, minalign); 939 aligned_idx = __kmalloc_index(aligned_size, false); 940 } 941 942 if (!kmalloc_caches[type][aligned_idx]) 943 kmalloc_caches[type][aligned_idx] = create_kmalloc_cache( 944 kmalloc_info[aligned_idx].name[type], 945 aligned_size, flags); 946 if (idx != aligned_idx) 947 kmalloc_caches[type][idx] = kmalloc_caches[type][aligned_idx]; 948 } 949 950 /* 951 * Create the kmalloc array. Some of the regular kmalloc arrays 952 * may already have been created because they were needed to 953 * enable allocations for slab creation. 954 */ 955 void __init create_kmalloc_caches(void) 956 { 957 int i; 958 enum kmalloc_cache_type type; 959 960 /* 961 * Including KMALLOC_CGROUP if CONFIG_MEMCG defined 962 */ 963 for (type = KMALLOC_NORMAL; type < NR_KMALLOC_TYPES; type++) { 964 /* Caches that are NOT of the two-to-the-power-of size. */ 965 if (KMALLOC_MIN_SIZE <= 32) 966 new_kmalloc_cache(1, type); 967 if (KMALLOC_MIN_SIZE <= 64) 968 new_kmalloc_cache(2, type); 969 970 /* Caches that are of the two-to-the-power-of size. */ 971 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) 972 new_kmalloc_cache(i, type); 973 } 974 #ifdef CONFIG_RANDOM_KMALLOC_CACHES 975 random_kmalloc_seed = get_random_u64(); 976 #endif 977 978 /* Kmalloc array is now usable */ 979 slab_state = UP; 980 981 if (IS_ENABLED(CONFIG_SLAB_BUCKETS)) 982 kmem_buckets_cache = kmem_cache_create("kmalloc_buckets", 983 sizeof(kmem_buckets), 984 0, SLAB_NO_MERGE, NULL); 985 } 986 987 /** 988 * __ksize -- Report full size of underlying allocation 989 * @object: pointer to the object 990 * 991 * This should only be used internally to query the true size of allocations. 992 * It is not meant to be a way to discover the usable size of an allocation 993 * after the fact. Instead, use kmalloc_size_roundup(). Using memory beyond 994 * the originally requested allocation size may trigger KASAN, UBSAN_BOUNDS, 995 * and/or FORTIFY_SOURCE. 996 * 997 * Return: size of the actual memory used by @object in bytes 998 */ 999 size_t __ksize(const void *object) 1000 { 1001 struct folio *folio; 1002 1003 if (unlikely(object == ZERO_SIZE_PTR)) 1004 return 0; 1005 1006 folio = virt_to_folio(object); 1007 1008 if (unlikely(!folio_test_slab(folio))) { 1009 if (WARN_ON(folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE)) 1010 return 0; 1011 if (WARN_ON(object != folio_address(folio))) 1012 return 0; 1013 return folio_size(folio); 1014 } 1015 1016 #ifdef CONFIG_SLUB_DEBUG 1017 skip_orig_size_check(folio_slab(folio)->slab_cache, object); 1018 #endif 1019 1020 return slab_ksize(folio_slab(folio)->slab_cache); 1021 } 1022 1023 gfp_t kmalloc_fix_flags(gfp_t flags) 1024 { 1025 gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK; 1026 1027 flags &= ~GFP_SLAB_BUG_MASK; 1028 pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n", 1029 invalid_mask, &invalid_mask, flags, &flags); 1030 dump_stack(); 1031 1032 return flags; 1033 } 1034 1035 #ifdef CONFIG_SLAB_FREELIST_RANDOM 1036 /* Randomize a generic freelist */ 1037 static void freelist_randomize(unsigned int *list, 1038 unsigned int count) 1039 { 1040 unsigned int rand; 1041 unsigned int i; 1042 1043 for (i = 0; i < count; i++) 1044 list[i] = i; 1045 1046 /* Fisher-Yates shuffle */ 1047 for (i = count - 1; i > 0; i--) { 1048 rand = get_random_u32_below(i + 1); 1049 swap(list[i], list[rand]); 1050 } 1051 } 1052 1053 /* Create a random sequence per cache */ 1054 int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count, 1055 gfp_t gfp) 1056 { 1057 1058 if (count < 2 || cachep->random_seq) 1059 return 0; 1060 1061 cachep->random_seq = kcalloc(count, sizeof(unsigned int), gfp); 1062 if (!cachep->random_seq) 1063 return -ENOMEM; 1064 1065 freelist_randomize(cachep->random_seq, count); 1066 return 0; 1067 } 1068 1069 /* Destroy the per-cache random freelist sequence */ 1070 void cache_random_seq_destroy(struct kmem_cache *cachep) 1071 { 1072 kfree(cachep->random_seq); 1073 cachep->random_seq = NULL; 1074 } 1075 #endif /* CONFIG_SLAB_FREELIST_RANDOM */ 1076 1077 #ifdef CONFIG_SLUB_DEBUG 1078 #define SLABINFO_RIGHTS (0400) 1079 1080 static void print_slabinfo_header(struct seq_file *m) 1081 { 1082 /* 1083 * Output format version, so at least we can change it 1084 * without _too_ many complaints. 1085 */ 1086 seq_puts(m, "slabinfo - version: 2.1\n"); 1087 seq_puts(m, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>"); 1088 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>"); 1089 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>"); 1090 seq_putc(m, '\n'); 1091 } 1092 1093 static void *slab_start(struct seq_file *m, loff_t *pos) 1094 { 1095 mutex_lock(&slab_mutex); 1096 return seq_list_start(&slab_caches, *pos); 1097 } 1098 1099 static void *slab_next(struct seq_file *m, void *p, loff_t *pos) 1100 { 1101 return seq_list_next(p, &slab_caches, pos); 1102 } 1103 1104 static void slab_stop(struct seq_file *m, void *p) 1105 { 1106 mutex_unlock(&slab_mutex); 1107 } 1108 1109 static void cache_show(struct kmem_cache *s, struct seq_file *m) 1110 { 1111 struct slabinfo sinfo; 1112 1113 memset(&sinfo, 0, sizeof(sinfo)); 1114 get_slabinfo(s, &sinfo); 1115 1116 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", 1117 s->name, sinfo.active_objs, sinfo.num_objs, s->size, 1118 sinfo.objects_per_slab, (1 << sinfo.cache_order)); 1119 1120 seq_printf(m, " : tunables %4u %4u %4u", 1121 sinfo.limit, sinfo.batchcount, sinfo.shared); 1122 seq_printf(m, " : slabdata %6lu %6lu %6lu", 1123 sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail); 1124 seq_putc(m, '\n'); 1125 } 1126 1127 static int slab_show(struct seq_file *m, void *p) 1128 { 1129 struct kmem_cache *s = list_entry(p, struct kmem_cache, list); 1130 1131 if (p == slab_caches.next) 1132 print_slabinfo_header(m); 1133 cache_show(s, m); 1134 return 0; 1135 } 1136 1137 void dump_unreclaimable_slab(void) 1138 { 1139 struct kmem_cache *s; 1140 struct slabinfo sinfo; 1141 1142 /* 1143 * Here acquiring slab_mutex is risky since we don't prefer to get 1144 * sleep in oom path. But, without mutex hold, it may introduce a 1145 * risk of crash. 1146 * Use mutex_trylock to protect the list traverse, dump nothing 1147 * without acquiring the mutex. 1148 */ 1149 if (!mutex_trylock(&slab_mutex)) { 1150 pr_warn("excessive unreclaimable slab but cannot dump stats\n"); 1151 return; 1152 } 1153 1154 pr_info("Unreclaimable slab info:\n"); 1155 pr_info("Name Used Total\n"); 1156 1157 list_for_each_entry(s, &slab_caches, list) { 1158 if (s->flags & SLAB_RECLAIM_ACCOUNT) 1159 continue; 1160 1161 get_slabinfo(s, &sinfo); 1162 1163 if (sinfo.num_objs > 0) 1164 pr_info("%-17s %10luKB %10luKB\n", s->name, 1165 (sinfo.active_objs * s->size) / 1024, 1166 (sinfo.num_objs * s->size) / 1024); 1167 } 1168 mutex_unlock(&slab_mutex); 1169 } 1170 1171 /* 1172 * slabinfo_op - iterator that generates /proc/slabinfo 1173 * 1174 * Output layout: 1175 * cache-name 1176 * num-active-objs 1177 * total-objs 1178 * object size 1179 * num-active-slabs 1180 * total-slabs 1181 * num-pages-per-slab 1182 * + further values on SMP and with statistics enabled 1183 */ 1184 static const struct seq_operations slabinfo_op = { 1185 .start = slab_start, 1186 .next = slab_next, 1187 .stop = slab_stop, 1188 .show = slab_show, 1189 }; 1190 1191 static int slabinfo_open(struct inode *inode, struct file *file) 1192 { 1193 return seq_open(file, &slabinfo_op); 1194 } 1195 1196 static const struct proc_ops slabinfo_proc_ops = { 1197 .proc_flags = PROC_ENTRY_PERMANENT, 1198 .proc_open = slabinfo_open, 1199 .proc_read = seq_read, 1200 .proc_lseek = seq_lseek, 1201 .proc_release = seq_release, 1202 }; 1203 1204 static int __init slab_proc_init(void) 1205 { 1206 proc_create("slabinfo", SLABINFO_RIGHTS, NULL, &slabinfo_proc_ops); 1207 return 0; 1208 } 1209 module_init(slab_proc_init); 1210 1211 #endif /* CONFIG_SLUB_DEBUG */ 1212 1213 /** 1214 * kfree_sensitive - Clear sensitive information in memory before freeing 1215 * @p: object to free memory of 1216 * 1217 * The memory of the object @p points to is zeroed before freed. 1218 * If @p is %NULL, kfree_sensitive() does nothing. 1219 * 1220 * Note: this function zeroes the whole allocated buffer which can be a good 1221 * deal bigger than the requested buffer size passed to kmalloc(). So be 1222 * careful when using this function in performance sensitive code. 1223 */ 1224 void kfree_sensitive(const void *p) 1225 { 1226 size_t ks; 1227 void *mem = (void *)p; 1228 1229 ks = ksize(mem); 1230 if (ks) { 1231 kasan_unpoison_range(mem, ks); 1232 memzero_explicit(mem, ks); 1233 } 1234 kfree(mem); 1235 } 1236 EXPORT_SYMBOL(kfree_sensitive); 1237 1238 size_t ksize(const void *objp) 1239 { 1240 /* 1241 * We need to first check that the pointer to the object is valid. 1242 * The KASAN report printed from ksize() is more useful, then when 1243 * it's printed later when the behaviour could be undefined due to 1244 * a potential use-after-free or double-free. 1245 * 1246 * We use kasan_check_byte(), which is supported for the hardware 1247 * tag-based KASAN mode, unlike kasan_check_read/write(). 1248 * 1249 * If the pointed to memory is invalid, we return 0 to avoid users of 1250 * ksize() writing to and potentially corrupting the memory region. 1251 * 1252 * We want to perform the check before __ksize(), to avoid potentially 1253 * crashing in __ksize() due to accessing invalid metadata. 1254 */ 1255 if (unlikely(ZERO_OR_NULL_PTR(objp)) || !kasan_check_byte(objp)) 1256 return 0; 1257 1258 return kfence_ksize(objp) ?: __ksize(objp); 1259 } 1260 EXPORT_SYMBOL(ksize); 1261 1262 #ifdef CONFIG_BPF_SYSCALL 1263 #include <linux/btf.h> 1264 1265 __bpf_kfunc_start_defs(); 1266 1267 __bpf_kfunc struct kmem_cache *bpf_get_kmem_cache(u64 addr) 1268 { 1269 struct slab *slab; 1270 1271 if (!virt_addr_valid((void *)(long)addr)) 1272 return NULL; 1273 1274 slab = virt_to_slab((void *)(long)addr); 1275 return slab ? slab->slab_cache : NULL; 1276 } 1277 1278 __bpf_kfunc_end_defs(); 1279 #endif /* CONFIG_BPF_SYSCALL */ 1280 1281 /* Tracepoints definitions. */ 1282 EXPORT_TRACEPOINT_SYMBOL(kmalloc); 1283 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc); 1284 EXPORT_TRACEPOINT_SYMBOL(kfree); 1285 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free); 1286 1287 /* 1288 * This rcu parameter is runtime-read-only. It reflects 1289 * a minimum allowed number of objects which can be cached 1290 * per-CPU. Object size is equal to one page. This value 1291 * can be changed at boot time. 1292 */ 1293 static int rcu_min_cached_objs = 5; 1294 module_param(rcu_min_cached_objs, int, 0444); 1295 1296 // A page shrinker can ask for pages to be freed to make them 1297 // available for other parts of the system. This usually happens 1298 // under low memory conditions, and in that case we should also 1299 // defer page-cache filling for a short time period. 1300 // 1301 // The default value is 5 seconds, which is long enough to reduce 1302 // interference with the shrinker while it asks other systems to 1303 // drain their caches. 1304 static int rcu_delay_page_cache_fill_msec = 5000; 1305 module_param(rcu_delay_page_cache_fill_msec, int, 0444); 1306 1307 /* Maximum number of jiffies to wait before draining a batch. */ 1308 #define KFREE_DRAIN_JIFFIES (5 * HZ) 1309 #define KFREE_N_BATCHES 2 1310 #define FREE_N_CHANNELS 2 1311 1312 /** 1313 * struct kvfree_rcu_bulk_data - single block to store kvfree_rcu() pointers 1314 * @list: List node. All blocks are linked between each other 1315 * @gp_snap: Snapshot of RCU state for objects placed to this bulk 1316 * @nr_records: Number of active pointers in the array 1317 * @records: Array of the kvfree_rcu() pointers 1318 */ 1319 struct kvfree_rcu_bulk_data { 1320 struct list_head list; 1321 struct rcu_gp_oldstate gp_snap; 1322 unsigned long nr_records; 1323 void *records[] __counted_by(nr_records); 1324 }; 1325 1326 /* 1327 * This macro defines how many entries the "records" array 1328 * will contain. It is based on the fact that the size of 1329 * kvfree_rcu_bulk_data structure becomes exactly one page. 1330 */ 1331 #define KVFREE_BULK_MAX_ENTR \ 1332 ((PAGE_SIZE - sizeof(struct kvfree_rcu_bulk_data)) / sizeof(void *)) 1333 1334 /** 1335 * struct kfree_rcu_cpu_work - single batch of kfree_rcu() requests 1336 * @rcu_work: Let queue_rcu_work() invoke workqueue handler after grace period 1337 * @head_free: List of kfree_rcu() objects waiting for a grace period 1338 * @head_free_gp_snap: Grace-period snapshot to check for attempted premature frees. 1339 * @bulk_head_free: Bulk-List of kvfree_rcu() objects waiting for a grace period 1340 * @krcp: Pointer to @kfree_rcu_cpu structure 1341 */ 1342 1343 struct kfree_rcu_cpu_work { 1344 struct rcu_work rcu_work; 1345 struct rcu_head *head_free; 1346 struct rcu_gp_oldstate head_free_gp_snap; 1347 struct list_head bulk_head_free[FREE_N_CHANNELS]; 1348 struct kfree_rcu_cpu *krcp; 1349 }; 1350 1351 /** 1352 * struct kfree_rcu_cpu - batch up kfree_rcu() requests for RCU grace period 1353 * @head: List of kfree_rcu() objects not yet waiting for a grace period 1354 * @head_gp_snap: Snapshot of RCU state for objects placed to "@head" 1355 * @bulk_head: Bulk-List of kvfree_rcu() objects not yet waiting for a grace period 1356 * @krw_arr: Array of batches of kfree_rcu() objects waiting for a grace period 1357 * @lock: Synchronize access to this structure 1358 * @monitor_work: Promote @head to @head_free after KFREE_DRAIN_JIFFIES 1359 * @initialized: The @rcu_work fields have been initialized 1360 * @head_count: Number of objects in rcu_head singular list 1361 * @bulk_count: Number of objects in bulk-list 1362 * @bkvcache: 1363 * A simple cache list that contains objects for reuse purpose. 1364 * In order to save some per-cpu space the list is singular. 1365 * Even though it is lockless an access has to be protected by the 1366 * per-cpu lock. 1367 * @page_cache_work: A work to refill the cache when it is empty 1368 * @backoff_page_cache_fill: Delay cache refills 1369 * @work_in_progress: Indicates that page_cache_work is running 1370 * @hrtimer: A hrtimer for scheduling a page_cache_work 1371 * @nr_bkv_objs: number of allocated objects at @bkvcache. 1372 * 1373 * This is a per-CPU structure. The reason that it is not included in 1374 * the rcu_data structure is to permit this code to be extracted from 1375 * the RCU files. Such extraction could allow further optimization of 1376 * the interactions with the slab allocators. 1377 */ 1378 struct kfree_rcu_cpu { 1379 // Objects queued on a linked list 1380 // through their rcu_head structures. 1381 struct rcu_head *head; 1382 unsigned long head_gp_snap; 1383 atomic_t head_count; 1384 1385 // Objects queued on a bulk-list. 1386 struct list_head bulk_head[FREE_N_CHANNELS]; 1387 atomic_t bulk_count[FREE_N_CHANNELS]; 1388 1389 struct kfree_rcu_cpu_work krw_arr[KFREE_N_BATCHES]; 1390 raw_spinlock_t lock; 1391 struct delayed_work monitor_work; 1392 bool initialized; 1393 1394 struct delayed_work page_cache_work; 1395 atomic_t backoff_page_cache_fill; 1396 atomic_t work_in_progress; 1397 struct hrtimer hrtimer; 1398 1399 struct llist_head bkvcache; 1400 int nr_bkv_objs; 1401 }; 1402 1403 static DEFINE_PER_CPU(struct kfree_rcu_cpu, krc) = { 1404 .lock = __RAW_SPIN_LOCK_UNLOCKED(krc.lock), 1405 }; 1406 1407 static __always_inline void 1408 debug_rcu_bhead_unqueue(struct kvfree_rcu_bulk_data *bhead) 1409 { 1410 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD 1411 int i; 1412 1413 for (i = 0; i < bhead->nr_records; i++) 1414 debug_rcu_head_unqueue((struct rcu_head *)(bhead->records[i])); 1415 #endif 1416 } 1417 1418 static inline struct kfree_rcu_cpu * 1419 krc_this_cpu_lock(unsigned long *flags) 1420 { 1421 struct kfree_rcu_cpu *krcp; 1422 1423 local_irq_save(*flags); // For safely calling this_cpu_ptr(). 1424 krcp = this_cpu_ptr(&krc); 1425 raw_spin_lock(&krcp->lock); 1426 1427 return krcp; 1428 } 1429 1430 static inline void 1431 krc_this_cpu_unlock(struct kfree_rcu_cpu *krcp, unsigned long flags) 1432 { 1433 raw_spin_unlock_irqrestore(&krcp->lock, flags); 1434 } 1435 1436 static inline struct kvfree_rcu_bulk_data * 1437 get_cached_bnode(struct kfree_rcu_cpu *krcp) 1438 { 1439 if (!krcp->nr_bkv_objs) 1440 return NULL; 1441 1442 WRITE_ONCE(krcp->nr_bkv_objs, krcp->nr_bkv_objs - 1); 1443 return (struct kvfree_rcu_bulk_data *) 1444 llist_del_first(&krcp->bkvcache); 1445 } 1446 1447 static inline bool 1448 put_cached_bnode(struct kfree_rcu_cpu *krcp, 1449 struct kvfree_rcu_bulk_data *bnode) 1450 { 1451 // Check the limit. 1452 if (krcp->nr_bkv_objs >= rcu_min_cached_objs) 1453 return false; 1454 1455 llist_add((struct llist_node *) bnode, &krcp->bkvcache); 1456 WRITE_ONCE(krcp->nr_bkv_objs, krcp->nr_bkv_objs + 1); 1457 return true; 1458 } 1459 1460 static int 1461 drain_page_cache(struct kfree_rcu_cpu *krcp) 1462 { 1463 unsigned long flags; 1464 struct llist_node *page_list, *pos, *n; 1465 int freed = 0; 1466 1467 if (!rcu_min_cached_objs) 1468 return 0; 1469 1470 raw_spin_lock_irqsave(&krcp->lock, flags); 1471 page_list = llist_del_all(&krcp->bkvcache); 1472 WRITE_ONCE(krcp->nr_bkv_objs, 0); 1473 raw_spin_unlock_irqrestore(&krcp->lock, flags); 1474 1475 llist_for_each_safe(pos, n, page_list) { 1476 free_page((unsigned long)pos); 1477 freed++; 1478 } 1479 1480 return freed; 1481 } 1482 1483 static void 1484 kvfree_rcu_bulk(struct kfree_rcu_cpu *krcp, 1485 struct kvfree_rcu_bulk_data *bnode, int idx) 1486 { 1487 unsigned long flags; 1488 int i; 1489 1490 if (!WARN_ON_ONCE(!poll_state_synchronize_rcu_full(&bnode->gp_snap))) { 1491 debug_rcu_bhead_unqueue(bnode); 1492 rcu_lock_acquire(&rcu_callback_map); 1493 if (idx == 0) { // kmalloc() / kfree(). 1494 trace_rcu_invoke_kfree_bulk_callback( 1495 "slab", bnode->nr_records, 1496 bnode->records); 1497 1498 kfree_bulk(bnode->nr_records, bnode->records); 1499 } else { // vmalloc() / vfree(). 1500 for (i = 0; i < bnode->nr_records; i++) { 1501 trace_rcu_invoke_kvfree_callback( 1502 "slab", bnode->records[i], 0); 1503 1504 vfree(bnode->records[i]); 1505 } 1506 } 1507 rcu_lock_release(&rcu_callback_map); 1508 } 1509 1510 raw_spin_lock_irqsave(&krcp->lock, flags); 1511 if (put_cached_bnode(krcp, bnode)) 1512 bnode = NULL; 1513 raw_spin_unlock_irqrestore(&krcp->lock, flags); 1514 1515 if (bnode) 1516 free_page((unsigned long) bnode); 1517 1518 cond_resched_tasks_rcu_qs(); 1519 } 1520 1521 static void 1522 kvfree_rcu_list(struct rcu_head *head) 1523 { 1524 struct rcu_head *next; 1525 1526 for (; head; head = next) { 1527 void *ptr = (void *) head->func; 1528 unsigned long offset = (void *) head - ptr; 1529 1530 next = head->next; 1531 debug_rcu_head_unqueue((struct rcu_head *)ptr); 1532 rcu_lock_acquire(&rcu_callback_map); 1533 trace_rcu_invoke_kvfree_callback("slab", head, offset); 1534 1535 if (!WARN_ON_ONCE(!__is_kvfree_rcu_offset(offset))) 1536 kvfree(ptr); 1537 1538 rcu_lock_release(&rcu_callback_map); 1539 cond_resched_tasks_rcu_qs(); 1540 } 1541 } 1542 1543 /* 1544 * This function is invoked in workqueue context after a grace period. 1545 * It frees all the objects queued on ->bulk_head_free or ->head_free. 1546 */ 1547 static void kfree_rcu_work(struct work_struct *work) 1548 { 1549 unsigned long flags; 1550 struct kvfree_rcu_bulk_data *bnode, *n; 1551 struct list_head bulk_head[FREE_N_CHANNELS]; 1552 struct rcu_head *head; 1553 struct kfree_rcu_cpu *krcp; 1554 struct kfree_rcu_cpu_work *krwp; 1555 struct rcu_gp_oldstate head_gp_snap; 1556 int i; 1557 1558 krwp = container_of(to_rcu_work(work), 1559 struct kfree_rcu_cpu_work, rcu_work); 1560 krcp = krwp->krcp; 1561 1562 raw_spin_lock_irqsave(&krcp->lock, flags); 1563 // Channels 1 and 2. 1564 for (i = 0; i < FREE_N_CHANNELS; i++) 1565 list_replace_init(&krwp->bulk_head_free[i], &bulk_head[i]); 1566 1567 // Channel 3. 1568 head = krwp->head_free; 1569 krwp->head_free = NULL; 1570 head_gp_snap = krwp->head_free_gp_snap; 1571 raw_spin_unlock_irqrestore(&krcp->lock, flags); 1572 1573 // Handle the first two channels. 1574 for (i = 0; i < FREE_N_CHANNELS; i++) { 1575 // Start from the tail page, so a GP is likely passed for it. 1576 list_for_each_entry_safe(bnode, n, &bulk_head[i], list) 1577 kvfree_rcu_bulk(krcp, bnode, i); 1578 } 1579 1580 /* 1581 * This is used when the "bulk" path can not be used for the 1582 * double-argument of kvfree_rcu(). This happens when the 1583 * page-cache is empty, which means that objects are instead 1584 * queued on a linked list through their rcu_head structures. 1585 * This list is named "Channel 3". 1586 */ 1587 if (head && !WARN_ON_ONCE(!poll_state_synchronize_rcu_full(&head_gp_snap))) 1588 kvfree_rcu_list(head); 1589 } 1590 1591 static bool 1592 need_offload_krc(struct kfree_rcu_cpu *krcp) 1593 { 1594 int i; 1595 1596 for (i = 0; i < FREE_N_CHANNELS; i++) 1597 if (!list_empty(&krcp->bulk_head[i])) 1598 return true; 1599 1600 return !!READ_ONCE(krcp->head); 1601 } 1602 1603 static bool 1604 need_wait_for_krwp_work(struct kfree_rcu_cpu_work *krwp) 1605 { 1606 int i; 1607 1608 for (i = 0; i < FREE_N_CHANNELS; i++) 1609 if (!list_empty(&krwp->bulk_head_free[i])) 1610 return true; 1611 1612 return !!krwp->head_free; 1613 } 1614 1615 static int krc_count(struct kfree_rcu_cpu *krcp) 1616 { 1617 int sum = atomic_read(&krcp->head_count); 1618 int i; 1619 1620 for (i = 0; i < FREE_N_CHANNELS; i++) 1621 sum += atomic_read(&krcp->bulk_count[i]); 1622 1623 return sum; 1624 } 1625 1626 static void 1627 __schedule_delayed_monitor_work(struct kfree_rcu_cpu *krcp) 1628 { 1629 long delay, delay_left; 1630 1631 delay = krc_count(krcp) >= KVFREE_BULK_MAX_ENTR ? 1:KFREE_DRAIN_JIFFIES; 1632 if (delayed_work_pending(&krcp->monitor_work)) { 1633 delay_left = krcp->monitor_work.timer.expires - jiffies; 1634 if (delay < delay_left) 1635 mod_delayed_work(system_unbound_wq, &krcp->monitor_work, delay); 1636 return; 1637 } 1638 queue_delayed_work(system_unbound_wq, &krcp->monitor_work, delay); 1639 } 1640 1641 static void 1642 schedule_delayed_monitor_work(struct kfree_rcu_cpu *krcp) 1643 { 1644 unsigned long flags; 1645 1646 raw_spin_lock_irqsave(&krcp->lock, flags); 1647 __schedule_delayed_monitor_work(krcp); 1648 raw_spin_unlock_irqrestore(&krcp->lock, flags); 1649 } 1650 1651 static void 1652 kvfree_rcu_drain_ready(struct kfree_rcu_cpu *krcp) 1653 { 1654 struct list_head bulk_ready[FREE_N_CHANNELS]; 1655 struct kvfree_rcu_bulk_data *bnode, *n; 1656 struct rcu_head *head_ready = NULL; 1657 unsigned long flags; 1658 int i; 1659 1660 raw_spin_lock_irqsave(&krcp->lock, flags); 1661 for (i = 0; i < FREE_N_CHANNELS; i++) { 1662 INIT_LIST_HEAD(&bulk_ready[i]); 1663 1664 list_for_each_entry_safe_reverse(bnode, n, &krcp->bulk_head[i], list) { 1665 if (!poll_state_synchronize_rcu_full(&bnode->gp_snap)) 1666 break; 1667 1668 atomic_sub(bnode->nr_records, &krcp->bulk_count[i]); 1669 list_move(&bnode->list, &bulk_ready[i]); 1670 } 1671 } 1672 1673 if (krcp->head && poll_state_synchronize_rcu(krcp->head_gp_snap)) { 1674 head_ready = krcp->head; 1675 atomic_set(&krcp->head_count, 0); 1676 WRITE_ONCE(krcp->head, NULL); 1677 } 1678 raw_spin_unlock_irqrestore(&krcp->lock, flags); 1679 1680 for (i = 0; i < FREE_N_CHANNELS; i++) { 1681 list_for_each_entry_safe(bnode, n, &bulk_ready[i], list) 1682 kvfree_rcu_bulk(krcp, bnode, i); 1683 } 1684 1685 if (head_ready) 1686 kvfree_rcu_list(head_ready); 1687 } 1688 1689 /* 1690 * Return: %true if a work is queued, %false otherwise. 1691 */ 1692 static bool 1693 kvfree_rcu_queue_batch(struct kfree_rcu_cpu *krcp) 1694 { 1695 unsigned long flags; 1696 bool queued = false; 1697 int i, j; 1698 1699 raw_spin_lock_irqsave(&krcp->lock, flags); 1700 1701 // Attempt to start a new batch. 1702 for (i = 0; i < KFREE_N_BATCHES; i++) { 1703 struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]); 1704 1705 // Try to detach bulk_head or head and attach it, only when 1706 // all channels are free. Any channel is not free means at krwp 1707 // there is on-going rcu work to handle krwp's free business. 1708 if (need_wait_for_krwp_work(krwp)) 1709 continue; 1710 1711 // kvfree_rcu_drain_ready() might handle this krcp, if so give up. 1712 if (need_offload_krc(krcp)) { 1713 // Channel 1 corresponds to the SLAB-pointer bulk path. 1714 // Channel 2 corresponds to vmalloc-pointer bulk path. 1715 for (j = 0; j < FREE_N_CHANNELS; j++) { 1716 if (list_empty(&krwp->bulk_head_free[j])) { 1717 atomic_set(&krcp->bulk_count[j], 0); 1718 list_replace_init(&krcp->bulk_head[j], 1719 &krwp->bulk_head_free[j]); 1720 } 1721 } 1722 1723 // Channel 3 corresponds to both SLAB and vmalloc 1724 // objects queued on the linked list. 1725 if (!krwp->head_free) { 1726 krwp->head_free = krcp->head; 1727 get_state_synchronize_rcu_full(&krwp->head_free_gp_snap); 1728 atomic_set(&krcp->head_count, 0); 1729 WRITE_ONCE(krcp->head, NULL); 1730 } 1731 1732 // One work is per one batch, so there are three 1733 // "free channels", the batch can handle. Break 1734 // the loop since it is done with this CPU thus 1735 // queuing an RCU work is _always_ success here. 1736 queued = queue_rcu_work(system_unbound_wq, &krwp->rcu_work); 1737 WARN_ON_ONCE(!queued); 1738 break; 1739 } 1740 } 1741 1742 raw_spin_unlock_irqrestore(&krcp->lock, flags); 1743 return queued; 1744 } 1745 1746 /* 1747 * This function is invoked after the KFREE_DRAIN_JIFFIES timeout. 1748 */ 1749 static void kfree_rcu_monitor(struct work_struct *work) 1750 { 1751 struct kfree_rcu_cpu *krcp = container_of(work, 1752 struct kfree_rcu_cpu, monitor_work.work); 1753 1754 // Drain ready for reclaim. 1755 kvfree_rcu_drain_ready(krcp); 1756 1757 // Queue a batch for a rest. 1758 kvfree_rcu_queue_batch(krcp); 1759 1760 // If there is nothing to detach, it means that our job is 1761 // successfully done here. In case of having at least one 1762 // of the channels that is still busy we should rearm the 1763 // work to repeat an attempt. Because previous batches are 1764 // still in progress. 1765 if (need_offload_krc(krcp)) 1766 schedule_delayed_monitor_work(krcp); 1767 } 1768 1769 static void fill_page_cache_func(struct work_struct *work) 1770 { 1771 struct kvfree_rcu_bulk_data *bnode; 1772 struct kfree_rcu_cpu *krcp = 1773 container_of(work, struct kfree_rcu_cpu, 1774 page_cache_work.work); 1775 unsigned long flags; 1776 int nr_pages; 1777 bool pushed; 1778 int i; 1779 1780 nr_pages = atomic_read(&krcp->backoff_page_cache_fill) ? 1781 1 : rcu_min_cached_objs; 1782 1783 for (i = READ_ONCE(krcp->nr_bkv_objs); i < nr_pages; i++) { 1784 bnode = (struct kvfree_rcu_bulk_data *) 1785 __get_free_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN); 1786 1787 if (!bnode) 1788 break; 1789 1790 raw_spin_lock_irqsave(&krcp->lock, flags); 1791 pushed = put_cached_bnode(krcp, bnode); 1792 raw_spin_unlock_irqrestore(&krcp->lock, flags); 1793 1794 if (!pushed) { 1795 free_page((unsigned long) bnode); 1796 break; 1797 } 1798 } 1799 1800 atomic_set(&krcp->work_in_progress, 0); 1801 atomic_set(&krcp->backoff_page_cache_fill, 0); 1802 } 1803 1804 // Record ptr in a page managed by krcp, with the pre-krc_this_cpu_lock() 1805 // state specified by flags. If can_alloc is true, the caller must 1806 // be schedulable and not be holding any locks or mutexes that might be 1807 // acquired by the memory allocator or anything that it might invoke. 1808 // Returns true if ptr was successfully recorded, else the caller must 1809 // use a fallback. 1810 static inline bool 1811 add_ptr_to_bulk_krc_lock(struct kfree_rcu_cpu **krcp, 1812 unsigned long *flags, void *ptr, bool can_alloc) 1813 { 1814 struct kvfree_rcu_bulk_data *bnode; 1815 int idx; 1816 1817 *krcp = krc_this_cpu_lock(flags); 1818 if (unlikely(!(*krcp)->initialized)) 1819 return false; 1820 1821 idx = !!is_vmalloc_addr(ptr); 1822 bnode = list_first_entry_or_null(&(*krcp)->bulk_head[idx], 1823 struct kvfree_rcu_bulk_data, list); 1824 1825 /* Check if a new block is required. */ 1826 if (!bnode || bnode->nr_records == KVFREE_BULK_MAX_ENTR) { 1827 bnode = get_cached_bnode(*krcp); 1828 if (!bnode && can_alloc) { 1829 krc_this_cpu_unlock(*krcp, *flags); 1830 1831 // __GFP_NORETRY - allows a light-weight direct reclaim 1832 // what is OK from minimizing of fallback hitting point of 1833 // view. Apart of that it forbids any OOM invoking what is 1834 // also beneficial since we are about to release memory soon. 1835 // 1836 // __GFP_NOMEMALLOC - prevents from consuming of all the 1837 // memory reserves. Please note we have a fallback path. 1838 // 1839 // __GFP_NOWARN - it is supposed that an allocation can 1840 // be failed under low memory or high memory pressure 1841 // scenarios. 1842 bnode = (struct kvfree_rcu_bulk_data *) 1843 __get_free_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN); 1844 raw_spin_lock_irqsave(&(*krcp)->lock, *flags); 1845 } 1846 1847 if (!bnode) 1848 return false; 1849 1850 // Initialize the new block and attach it. 1851 bnode->nr_records = 0; 1852 list_add(&bnode->list, &(*krcp)->bulk_head[idx]); 1853 } 1854 1855 // Finally insert and update the GP for this page. 1856 bnode->nr_records++; 1857 bnode->records[bnode->nr_records - 1] = ptr; 1858 get_state_synchronize_rcu_full(&bnode->gp_snap); 1859 atomic_inc(&(*krcp)->bulk_count[idx]); 1860 1861 return true; 1862 } 1863 1864 #if !defined(CONFIG_TINY_RCU) 1865 1866 static enum hrtimer_restart 1867 schedule_page_work_fn(struct hrtimer *t) 1868 { 1869 struct kfree_rcu_cpu *krcp = 1870 container_of(t, struct kfree_rcu_cpu, hrtimer); 1871 1872 queue_delayed_work(system_highpri_wq, &krcp->page_cache_work, 0); 1873 return HRTIMER_NORESTART; 1874 } 1875 1876 static void 1877 run_page_cache_worker(struct kfree_rcu_cpu *krcp) 1878 { 1879 // If cache disabled, bail out. 1880 if (!rcu_min_cached_objs) 1881 return; 1882 1883 if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING && 1884 !atomic_xchg(&krcp->work_in_progress, 1)) { 1885 if (atomic_read(&krcp->backoff_page_cache_fill)) { 1886 queue_delayed_work(system_unbound_wq, 1887 &krcp->page_cache_work, 1888 msecs_to_jiffies(rcu_delay_page_cache_fill_msec)); 1889 } else { 1890 hrtimer_init(&krcp->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1891 krcp->hrtimer.function = schedule_page_work_fn; 1892 hrtimer_start(&krcp->hrtimer, 0, HRTIMER_MODE_REL); 1893 } 1894 } 1895 } 1896 1897 void __init kfree_rcu_scheduler_running(void) 1898 { 1899 int cpu; 1900 1901 for_each_possible_cpu(cpu) { 1902 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu); 1903 1904 if (need_offload_krc(krcp)) 1905 schedule_delayed_monitor_work(krcp); 1906 } 1907 } 1908 1909 /* 1910 * Queue a request for lazy invocation of the appropriate free routine 1911 * after a grace period. Please note that three paths are maintained, 1912 * two for the common case using arrays of pointers and a third one that 1913 * is used only when the main paths cannot be used, for example, due to 1914 * memory pressure. 1915 * 1916 * Each kvfree_call_rcu() request is added to a batch. The batch will be drained 1917 * every KFREE_DRAIN_JIFFIES number of jiffies. All the objects in the batch will 1918 * be free'd in workqueue context. This allows us to: batch requests together to 1919 * reduce the number of grace periods during heavy kfree_rcu()/kvfree_rcu() load. 1920 */ 1921 void kvfree_call_rcu(struct rcu_head *head, void *ptr) 1922 { 1923 unsigned long flags; 1924 struct kfree_rcu_cpu *krcp; 1925 bool success; 1926 1927 /* 1928 * Please note there is a limitation for the head-less 1929 * variant, that is why there is a clear rule for such 1930 * objects: it can be used from might_sleep() context 1931 * only. For other places please embed an rcu_head to 1932 * your data. 1933 */ 1934 if (!head) 1935 might_sleep(); 1936 1937 // Queue the object but don't yet schedule the batch. 1938 if (debug_rcu_head_queue(ptr)) { 1939 // Probable double kfree_rcu(), just leak. 1940 WARN_ONCE(1, "%s(): Double-freed call. rcu_head %p\n", 1941 __func__, head); 1942 1943 // Mark as success and leave. 1944 return; 1945 } 1946 1947 kasan_record_aux_stack_noalloc(ptr); 1948 success = add_ptr_to_bulk_krc_lock(&krcp, &flags, ptr, !head); 1949 if (!success) { 1950 run_page_cache_worker(krcp); 1951 1952 if (head == NULL) 1953 // Inline if kvfree_rcu(one_arg) call. 1954 goto unlock_return; 1955 1956 head->func = ptr; 1957 head->next = krcp->head; 1958 WRITE_ONCE(krcp->head, head); 1959 atomic_inc(&krcp->head_count); 1960 1961 // Take a snapshot for this krcp. 1962 krcp->head_gp_snap = get_state_synchronize_rcu(); 1963 success = true; 1964 } 1965 1966 /* 1967 * The kvfree_rcu() caller considers the pointer freed at this point 1968 * and likely removes any references to it. Since the actual slab 1969 * freeing (and kmemleak_free()) is deferred, tell kmemleak to ignore 1970 * this object (no scanning or false positives reporting). 1971 */ 1972 kmemleak_ignore(ptr); 1973 1974 // Set timer to drain after KFREE_DRAIN_JIFFIES. 1975 if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING) 1976 __schedule_delayed_monitor_work(krcp); 1977 1978 unlock_return: 1979 krc_this_cpu_unlock(krcp, flags); 1980 1981 /* 1982 * Inline kvfree() after synchronize_rcu(). We can do 1983 * it from might_sleep() context only, so the current 1984 * CPU can pass the QS state. 1985 */ 1986 if (!success) { 1987 debug_rcu_head_unqueue((struct rcu_head *) ptr); 1988 synchronize_rcu(); 1989 kvfree(ptr); 1990 } 1991 } 1992 EXPORT_SYMBOL_GPL(kvfree_call_rcu); 1993 1994 /** 1995 * kvfree_rcu_barrier - Wait until all in-flight kvfree_rcu() complete. 1996 * 1997 * Note that a single argument of kvfree_rcu() call has a slow path that 1998 * triggers synchronize_rcu() following by freeing a pointer. It is done 1999 * before the return from the function. Therefore for any single-argument 2000 * call that will result in a kfree() to a cache that is to be destroyed 2001 * during module exit, it is developer's responsibility to ensure that all 2002 * such calls have returned before the call to kmem_cache_destroy(). 2003 */ 2004 void kvfree_rcu_barrier(void) 2005 { 2006 struct kfree_rcu_cpu_work *krwp; 2007 struct kfree_rcu_cpu *krcp; 2008 bool queued; 2009 int i, cpu; 2010 2011 /* 2012 * Firstly we detach objects and queue them over an RCU-batch 2013 * for all CPUs. Finally queued works are flushed for each CPU. 2014 * 2015 * Please note. If there are outstanding batches for a particular 2016 * CPU, those have to be finished first following by queuing a new. 2017 */ 2018 for_each_possible_cpu(cpu) { 2019 krcp = per_cpu_ptr(&krc, cpu); 2020 2021 /* 2022 * Check if this CPU has any objects which have been queued for a 2023 * new GP completion. If not(means nothing to detach), we are done 2024 * with it. If any batch is pending/running for this "krcp", below 2025 * per-cpu flush_rcu_work() waits its completion(see last step). 2026 */ 2027 if (!need_offload_krc(krcp)) 2028 continue; 2029 2030 while (1) { 2031 /* 2032 * If we are not able to queue a new RCU work it means: 2033 * - batches for this CPU are still in flight which should 2034 * be flushed first and then repeat; 2035 * - no objects to detach, because of concurrency. 2036 */ 2037 queued = kvfree_rcu_queue_batch(krcp); 2038 2039 /* 2040 * Bail out, if there is no need to offload this "krcp" 2041 * anymore. As noted earlier it can run concurrently. 2042 */ 2043 if (queued || !need_offload_krc(krcp)) 2044 break; 2045 2046 /* There are ongoing batches. */ 2047 for (i = 0; i < KFREE_N_BATCHES; i++) { 2048 krwp = &(krcp->krw_arr[i]); 2049 flush_rcu_work(&krwp->rcu_work); 2050 } 2051 } 2052 } 2053 2054 /* 2055 * Now we guarantee that all objects are flushed. 2056 */ 2057 for_each_possible_cpu(cpu) { 2058 krcp = per_cpu_ptr(&krc, cpu); 2059 2060 /* 2061 * A monitor work can drain ready to reclaim objects 2062 * directly. Wait its completion if running or pending. 2063 */ 2064 cancel_delayed_work_sync(&krcp->monitor_work); 2065 2066 for (i = 0; i < KFREE_N_BATCHES; i++) { 2067 krwp = &(krcp->krw_arr[i]); 2068 flush_rcu_work(&krwp->rcu_work); 2069 } 2070 } 2071 } 2072 EXPORT_SYMBOL_GPL(kvfree_rcu_barrier); 2073 2074 #endif /* #if !defined(CONFIG_TINY_RCU) */ 2075 2076 static unsigned long 2077 kfree_rcu_shrink_count(struct shrinker *shrink, struct shrink_control *sc) 2078 { 2079 int cpu; 2080 unsigned long count = 0; 2081 2082 /* Snapshot count of all CPUs */ 2083 for_each_possible_cpu(cpu) { 2084 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu); 2085 2086 count += krc_count(krcp); 2087 count += READ_ONCE(krcp->nr_bkv_objs); 2088 atomic_set(&krcp->backoff_page_cache_fill, 1); 2089 } 2090 2091 return count == 0 ? SHRINK_EMPTY : count; 2092 } 2093 2094 static unsigned long 2095 kfree_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) 2096 { 2097 int cpu, freed = 0; 2098 2099 for_each_possible_cpu(cpu) { 2100 int count; 2101 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu); 2102 2103 count = krc_count(krcp); 2104 count += drain_page_cache(krcp); 2105 kfree_rcu_monitor(&krcp->monitor_work.work); 2106 2107 sc->nr_to_scan -= count; 2108 freed += count; 2109 2110 if (sc->nr_to_scan <= 0) 2111 break; 2112 } 2113 2114 return freed == 0 ? SHRINK_STOP : freed; 2115 } 2116 2117 void __init kvfree_rcu_init(void) 2118 { 2119 int cpu; 2120 int i, j; 2121 struct shrinker *kfree_rcu_shrinker; 2122 2123 /* Clamp it to [0:100] seconds interval. */ 2124 if (rcu_delay_page_cache_fill_msec < 0 || 2125 rcu_delay_page_cache_fill_msec > 100 * MSEC_PER_SEC) { 2126 2127 rcu_delay_page_cache_fill_msec = 2128 clamp(rcu_delay_page_cache_fill_msec, 0, 2129 (int) (100 * MSEC_PER_SEC)); 2130 2131 pr_info("Adjusting rcutree.rcu_delay_page_cache_fill_msec to %d ms.\n", 2132 rcu_delay_page_cache_fill_msec); 2133 } 2134 2135 for_each_possible_cpu(cpu) { 2136 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu); 2137 2138 for (i = 0; i < KFREE_N_BATCHES; i++) { 2139 INIT_RCU_WORK(&krcp->krw_arr[i].rcu_work, kfree_rcu_work); 2140 krcp->krw_arr[i].krcp = krcp; 2141 2142 for (j = 0; j < FREE_N_CHANNELS; j++) 2143 INIT_LIST_HEAD(&krcp->krw_arr[i].bulk_head_free[j]); 2144 } 2145 2146 for (i = 0; i < FREE_N_CHANNELS; i++) 2147 INIT_LIST_HEAD(&krcp->bulk_head[i]); 2148 2149 INIT_DELAYED_WORK(&krcp->monitor_work, kfree_rcu_monitor); 2150 INIT_DELAYED_WORK(&krcp->page_cache_work, fill_page_cache_func); 2151 krcp->initialized = true; 2152 } 2153 2154 kfree_rcu_shrinker = shrinker_alloc(0, "slab-kvfree-rcu"); 2155 if (!kfree_rcu_shrinker) { 2156 pr_err("Failed to allocate kfree_rcu() shrinker!\n"); 2157 return; 2158 } 2159 2160 kfree_rcu_shrinker->count_objects = kfree_rcu_shrink_count; 2161 kfree_rcu_shrinker->scan_objects = kfree_rcu_shrink_scan; 2162 2163 shrinker_register(kfree_rcu_shrinker); 2164 } 2165