1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Written by Mark Hemment, 1996 ([email protected]). 4 * 5 * (C) SGI 2006, Christoph Lameter 6 * Cleaned up and restructured to ease the addition of alternative 7 * implementations of SLAB allocators. 8 * (C) Linux Foundation 2008-2013 9 * Unified interface for all slab allocators 10 */ 11 12 #ifndef _LINUX_SLAB_H 13 #define _LINUX_SLAB_H 14 15 #include <linux/cache.h> 16 #include <linux/gfp.h> 17 #include <linux/overflow.h> 18 #include <linux/types.h> 19 #include <linux/workqueue.h> 20 #include <linux/percpu-refcount.h> 21 #include <linux/cleanup.h> 22 #include <linux/hash.h> 23 24 enum _slab_flag_bits { 25 _SLAB_CONSISTENCY_CHECKS, 26 _SLAB_RED_ZONE, 27 _SLAB_POISON, 28 _SLAB_KMALLOC, 29 _SLAB_HWCACHE_ALIGN, 30 _SLAB_CACHE_DMA, 31 _SLAB_CACHE_DMA32, 32 _SLAB_STORE_USER, 33 _SLAB_PANIC, 34 _SLAB_TYPESAFE_BY_RCU, 35 _SLAB_TRACE, 36 #ifdef CONFIG_DEBUG_OBJECTS 37 _SLAB_DEBUG_OBJECTS, 38 #endif 39 _SLAB_NOLEAKTRACE, 40 _SLAB_NO_MERGE, 41 #ifdef CONFIG_FAILSLAB 42 _SLAB_FAILSLAB, 43 #endif 44 #ifdef CONFIG_MEMCG 45 _SLAB_ACCOUNT, 46 #endif 47 #ifdef CONFIG_KASAN_GENERIC 48 _SLAB_KASAN, 49 #endif 50 _SLAB_NO_USER_FLAGS, 51 #ifdef CONFIG_KFENCE 52 _SLAB_SKIP_KFENCE, 53 #endif 54 #ifndef CONFIG_SLUB_TINY 55 _SLAB_RECLAIM_ACCOUNT, 56 #endif 57 _SLAB_OBJECT_POISON, 58 _SLAB_CMPXCHG_DOUBLE, 59 #ifdef CONFIG_SLAB_OBJ_EXT 60 _SLAB_NO_OBJ_EXT, 61 #endif 62 _SLAB_FLAGS_LAST_BIT 63 }; 64 65 #define __SLAB_FLAG_BIT(nr) ((slab_flags_t __force)(1U << (nr))) 66 #define __SLAB_FLAG_UNUSED ((slab_flags_t __force)(0U)) 67 68 /* 69 * Flags to pass to kmem_cache_create(). 70 * The ones marked DEBUG need CONFIG_SLUB_DEBUG enabled, otherwise are no-op 71 */ 72 /* DEBUG: Perform (expensive) checks on alloc/free */ 73 #define SLAB_CONSISTENCY_CHECKS __SLAB_FLAG_BIT(_SLAB_CONSISTENCY_CHECKS) 74 /* DEBUG: Red zone objs in a cache */ 75 #define SLAB_RED_ZONE __SLAB_FLAG_BIT(_SLAB_RED_ZONE) 76 /* DEBUG: Poison objects */ 77 #define SLAB_POISON __SLAB_FLAG_BIT(_SLAB_POISON) 78 /* Indicate a kmalloc slab */ 79 #define SLAB_KMALLOC __SLAB_FLAG_BIT(_SLAB_KMALLOC) 80 /* Align objs on cache lines */ 81 #define SLAB_HWCACHE_ALIGN __SLAB_FLAG_BIT(_SLAB_HWCACHE_ALIGN) 82 /* Use GFP_DMA memory */ 83 #define SLAB_CACHE_DMA __SLAB_FLAG_BIT(_SLAB_CACHE_DMA) 84 /* Use GFP_DMA32 memory */ 85 #define SLAB_CACHE_DMA32 __SLAB_FLAG_BIT(_SLAB_CACHE_DMA32) 86 /* DEBUG: Store the last owner for bug hunting */ 87 #define SLAB_STORE_USER __SLAB_FLAG_BIT(_SLAB_STORE_USER) 88 /* Panic if kmem_cache_create() fails */ 89 #define SLAB_PANIC __SLAB_FLAG_BIT(_SLAB_PANIC) 90 /* 91 * SLAB_TYPESAFE_BY_RCU - **WARNING** READ THIS! 92 * 93 * This delays freeing the SLAB page by a grace period, it does _NOT_ 94 * delay object freeing. This means that if you do kmem_cache_free() 95 * that memory location is free to be reused at any time. Thus it may 96 * be possible to see another object there in the same RCU grace period. 97 * 98 * This feature only ensures the memory location backing the object 99 * stays valid, the trick to using this is relying on an independent 100 * object validation pass. Something like: 101 * 102 * begin: 103 * rcu_read_lock(); 104 * obj = lockless_lookup(key); 105 * if (obj) { 106 * if (!try_get_ref(obj)) // might fail for free objects 107 * rcu_read_unlock(); 108 * goto begin; 109 * 110 * if (obj->key != key) { // not the object we expected 111 * put_ref(obj); 112 * rcu_read_unlock(); 113 * goto begin; 114 * } 115 * } 116 * rcu_read_unlock(); 117 * 118 * This is useful if we need to approach a kernel structure obliquely, 119 * from its address obtained without the usual locking. We can lock 120 * the structure to stabilize it and check it's still at the given address, 121 * only if we can be sure that the memory has not been meanwhile reused 122 * for some other kind of object (which our subsystem's lock might corrupt). 123 * 124 * rcu_read_lock before reading the address, then rcu_read_unlock after 125 * taking the spinlock within the structure expected at that address. 126 * 127 * Note that it is not possible to acquire a lock within a structure 128 * allocated with SLAB_TYPESAFE_BY_RCU without first acquiring a reference 129 * as described above. The reason is that SLAB_TYPESAFE_BY_RCU pages 130 * are not zeroed before being given to the slab, which means that any 131 * locks must be initialized after each and every kmem_struct_alloc(). 132 * Alternatively, make the ctor passed to kmem_cache_create() initialize 133 * the locks at page-allocation time, as is done in __i915_request_ctor(), 134 * sighand_ctor(), and anon_vma_ctor(). Such a ctor permits readers 135 * to safely acquire those ctor-initialized locks under rcu_read_lock() 136 * protection. 137 * 138 * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU. 139 */ 140 /* Defer freeing slabs to RCU */ 141 #define SLAB_TYPESAFE_BY_RCU __SLAB_FLAG_BIT(_SLAB_TYPESAFE_BY_RCU) 142 /* Trace allocations and frees */ 143 #define SLAB_TRACE __SLAB_FLAG_BIT(_SLAB_TRACE) 144 145 /* Flag to prevent checks on free */ 146 #ifdef CONFIG_DEBUG_OBJECTS 147 # define SLAB_DEBUG_OBJECTS __SLAB_FLAG_BIT(_SLAB_DEBUG_OBJECTS) 148 #else 149 # define SLAB_DEBUG_OBJECTS __SLAB_FLAG_UNUSED 150 #endif 151 152 /* Avoid kmemleak tracing */ 153 #define SLAB_NOLEAKTRACE __SLAB_FLAG_BIT(_SLAB_NOLEAKTRACE) 154 155 /* 156 * Prevent merging with compatible kmem caches. This flag should be used 157 * cautiously. Valid use cases: 158 * 159 * - caches created for self-tests (e.g. kunit) 160 * - general caches created and used by a subsystem, only when a 161 * (subsystem-specific) debug option is enabled 162 * - performance critical caches, should be very rare and consulted with slab 163 * maintainers, and not used together with CONFIG_SLUB_TINY 164 */ 165 #define SLAB_NO_MERGE __SLAB_FLAG_BIT(_SLAB_NO_MERGE) 166 167 /* Fault injection mark */ 168 #ifdef CONFIG_FAILSLAB 169 # define SLAB_FAILSLAB __SLAB_FLAG_BIT(_SLAB_FAILSLAB) 170 #else 171 # define SLAB_FAILSLAB __SLAB_FLAG_UNUSED 172 #endif 173 /* Account to memcg */ 174 #ifdef CONFIG_MEMCG 175 # define SLAB_ACCOUNT __SLAB_FLAG_BIT(_SLAB_ACCOUNT) 176 #else 177 # define SLAB_ACCOUNT __SLAB_FLAG_UNUSED 178 #endif 179 180 #ifdef CONFIG_KASAN_GENERIC 181 #define SLAB_KASAN __SLAB_FLAG_BIT(_SLAB_KASAN) 182 #else 183 #define SLAB_KASAN __SLAB_FLAG_UNUSED 184 #endif 185 186 /* 187 * Ignore user specified debugging flags. 188 * Intended for caches created for self-tests so they have only flags 189 * specified in the code and other flags are ignored. 190 */ 191 #define SLAB_NO_USER_FLAGS __SLAB_FLAG_BIT(_SLAB_NO_USER_FLAGS) 192 193 #ifdef CONFIG_KFENCE 194 #define SLAB_SKIP_KFENCE __SLAB_FLAG_BIT(_SLAB_SKIP_KFENCE) 195 #else 196 #define SLAB_SKIP_KFENCE __SLAB_FLAG_UNUSED 197 #endif 198 199 /* The following flags affect the page allocator grouping pages by mobility */ 200 /* Objects are reclaimable */ 201 #ifndef CONFIG_SLUB_TINY 202 #define SLAB_RECLAIM_ACCOUNT __SLAB_FLAG_BIT(_SLAB_RECLAIM_ACCOUNT) 203 #else 204 #define SLAB_RECLAIM_ACCOUNT __SLAB_FLAG_UNUSED 205 #endif 206 #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT /* Objects are short-lived */ 207 208 /* Slab created using create_boot_cache */ 209 #ifdef CONFIG_SLAB_OBJ_EXT 210 #define SLAB_NO_OBJ_EXT __SLAB_FLAG_BIT(_SLAB_NO_OBJ_EXT) 211 #else 212 #define SLAB_NO_OBJ_EXT __SLAB_FLAG_UNUSED 213 #endif 214 215 /* 216 * freeptr_t represents a SLUB freelist pointer, which might be encoded 217 * and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled. 218 */ 219 typedef struct { unsigned long v; } freeptr_t; 220 221 /* 222 * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. 223 * 224 * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault. 225 * 226 * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can. 227 * Both make kfree a no-op. 228 */ 229 #define ZERO_SIZE_PTR ((void *)16) 230 231 #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \ 232 (unsigned long)ZERO_SIZE_PTR) 233 234 #include <linux/kasan.h> 235 236 struct list_lru; 237 struct mem_cgroup; 238 /* 239 * struct kmem_cache related prototypes 240 */ 241 bool slab_is_available(void); 242 243 /** 244 * struct kmem_cache_args - Less common arguments for kmem_cache_create() 245 * @align: The required alignment for the objects. 246 * @useroffset: Usercopy region offset 247 * @usersize: Usercopy region size 248 * @freeptr_offset: Custom offset for the free pointer in RCU caches 249 * @use_freeptr_offset: Whether a @freeptr_offset is used 250 * @ctor: A constructor for the objects. 251 */ 252 struct kmem_cache_args { 253 unsigned int align; 254 unsigned int useroffset; 255 unsigned int usersize; 256 unsigned int freeptr_offset; 257 bool use_freeptr_offset; 258 void (*ctor)(void *); 259 }; 260 261 struct kmem_cache *__kmem_cache_create_args(const char *name, 262 unsigned int object_size, 263 struct kmem_cache_args *args, 264 slab_flags_t flags); 265 static inline struct kmem_cache * 266 __kmem_cache_create(const char *name, unsigned int size, unsigned int align, 267 slab_flags_t flags, void (*ctor)(void *)) 268 { 269 struct kmem_cache_args kmem_args = { 270 .align = align, 271 .ctor = ctor, 272 }; 273 274 return __kmem_cache_create_args(name, size, &kmem_args, flags); 275 } 276 277 /** 278 * kmem_cache_create_usercopy - Create a cache with a region suitable 279 * for copying to userspace 280 * @name: A string which is used in /proc/slabinfo to identify this cache. 281 * @size: The size of objects to be created in this cache. 282 * @align: The required alignment for the objects. 283 * @flags: SLAB flags 284 * @useroffset: Usercopy region offset 285 * @usersize: Usercopy region size 286 * @ctor: A constructor for the objects. 287 * 288 * Cannot be called within a interrupt, but can be interrupted. 289 * The @ctor is run when new pages are allocated by the cache. 290 * 291 * The flags are 292 * 293 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5) 294 * to catch references to uninitialised memory. 295 * 296 * %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check 297 * for buffer overruns. 298 * 299 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware 300 * cacheline. This can be beneficial if you're counting cycles as closely 301 * as davem. 302 * 303 * Return: a pointer to the cache on success, NULL on failure. 304 */ 305 static inline struct kmem_cache * 306 kmem_cache_create_usercopy(const char *name, unsigned int size, 307 unsigned int align, slab_flags_t flags, 308 unsigned int useroffset, unsigned int usersize, 309 void (*ctor)(void *)) 310 { 311 struct kmem_cache_args kmem_args = { 312 .align = align, 313 .ctor = ctor, 314 .useroffset = useroffset, 315 .usersize = usersize, 316 }; 317 318 return __kmem_cache_create_args(name, size, &kmem_args, flags); 319 } 320 321 /* If NULL is passed for @args, use this variant with default arguments. */ 322 static inline struct kmem_cache * 323 __kmem_cache_default_args(const char *name, unsigned int size, 324 struct kmem_cache_args *args, 325 slab_flags_t flags) 326 { 327 struct kmem_cache_args kmem_default_args = {}; 328 329 /* Make sure we don't get passed garbage. */ 330 if (WARN_ON_ONCE(args)) 331 return ERR_PTR(-EINVAL); 332 333 return __kmem_cache_create_args(name, size, &kmem_default_args, flags); 334 } 335 336 #define kmem_cache_create(__name, __object_size, __args, ...) \ 337 _Generic((__args), \ 338 struct kmem_cache_args *: __kmem_cache_create_args, \ 339 void *: __kmem_cache_default_args, \ 340 default: __kmem_cache_create)(__name, __object_size, __args, __VA_ARGS__) 341 342 void kmem_cache_destroy(struct kmem_cache *s); 343 int kmem_cache_shrink(struct kmem_cache *s); 344 345 /* 346 * Please use this macro to create slab caches. Simply specify the 347 * name of the structure and maybe some flags that are listed above. 348 * 349 * The alignment of the struct determines object alignment. If you 350 * f.e. add ____cacheline_aligned_in_smp to the struct declaration 351 * then the objects will be properly aligned in SMP configurations. 352 */ 353 #define KMEM_CACHE(__struct, __flags) \ 354 __kmem_cache_create_args(#__struct, sizeof(struct __struct), \ 355 &(struct kmem_cache_args) { \ 356 .align = __alignof__(struct __struct), \ 357 }, (__flags)) 358 359 /* 360 * To whitelist a single field for copying to/from usercopy, use this 361 * macro instead for KMEM_CACHE() above. 362 */ 363 #define KMEM_CACHE_USERCOPY(__struct, __flags, __field) \ 364 __kmem_cache_create_args(#__struct, sizeof(struct __struct), \ 365 &(struct kmem_cache_args) { \ 366 .align = __alignof__(struct __struct), \ 367 .useroffset = offsetof(struct __struct, __field), \ 368 .usersize = sizeof_field(struct __struct, __field), \ 369 }, (__flags)) 370 371 /* 372 * Common kmalloc functions provided by all allocators 373 */ 374 void * __must_check krealloc_noprof(const void *objp, size_t new_size, 375 gfp_t flags) __realloc_size(2); 376 #define krealloc(...) alloc_hooks(krealloc_noprof(__VA_ARGS__)) 377 378 void kfree(const void *objp); 379 void kfree_sensitive(const void *objp); 380 size_t __ksize(const void *objp); 381 382 DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T)) 383 384 /** 385 * ksize - Report actual allocation size of associated object 386 * 387 * @objp: Pointer returned from a prior kmalloc()-family allocation. 388 * 389 * This should not be used for writing beyond the originally requested 390 * allocation size. Either use krealloc() or round up the allocation size 391 * with kmalloc_size_roundup() prior to allocation. If this is used to 392 * access beyond the originally requested allocation size, UBSAN_BOUNDS 393 * and/or FORTIFY_SOURCE may trip, since they only know about the 394 * originally allocated size via the __alloc_size attribute. 395 */ 396 size_t ksize(const void *objp); 397 398 #ifdef CONFIG_PRINTK 399 bool kmem_dump_obj(void *object); 400 #else 401 static inline bool kmem_dump_obj(void *object) { return false; } 402 #endif 403 404 /* 405 * Some archs want to perform DMA into kmalloc caches and need a guaranteed 406 * alignment larger than the alignment of a 64-bit integer. 407 * Setting ARCH_DMA_MINALIGN in arch headers allows that. 408 */ 409 #ifdef ARCH_HAS_DMA_MINALIGN 410 #if ARCH_DMA_MINALIGN > 8 && !defined(ARCH_KMALLOC_MINALIGN) 411 #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN 412 #endif 413 #endif 414 415 #ifndef ARCH_KMALLOC_MINALIGN 416 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long) 417 #elif ARCH_KMALLOC_MINALIGN > 8 418 #define KMALLOC_MIN_SIZE ARCH_KMALLOC_MINALIGN 419 #define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE) 420 #endif 421 422 /* 423 * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment. 424 * Intended for arches that get misalignment faults even for 64 bit integer 425 * aligned buffers. 426 */ 427 #ifndef ARCH_SLAB_MINALIGN 428 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long) 429 #endif 430 431 /* 432 * Arches can define this function if they want to decide the minimum slab 433 * alignment at runtime. The value returned by the function must be a power 434 * of two and >= ARCH_SLAB_MINALIGN. 435 */ 436 #ifndef arch_slab_minalign 437 static inline unsigned int arch_slab_minalign(void) 438 { 439 return ARCH_SLAB_MINALIGN; 440 } 441 #endif 442 443 /* 444 * kmem_cache_alloc and friends return pointers aligned to ARCH_SLAB_MINALIGN. 445 * kmalloc and friends return pointers aligned to both ARCH_KMALLOC_MINALIGN 446 * and ARCH_SLAB_MINALIGN, but here we only assume the former alignment. 447 */ 448 #define __assume_kmalloc_alignment __assume_aligned(ARCH_KMALLOC_MINALIGN) 449 #define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MINALIGN) 450 #define __assume_page_alignment __assume_aligned(PAGE_SIZE) 451 452 /* 453 * Kmalloc array related definitions 454 */ 455 456 /* 457 * SLUB directly allocates requests fitting in to an order-1 page 458 * (PAGE_SIZE*2). Larger requests are passed to the page allocator. 459 */ 460 #define KMALLOC_SHIFT_HIGH (PAGE_SHIFT + 1) 461 #define KMALLOC_SHIFT_MAX (MAX_PAGE_ORDER + PAGE_SHIFT) 462 #ifndef KMALLOC_SHIFT_LOW 463 #define KMALLOC_SHIFT_LOW 3 464 #endif 465 466 /* Maximum allocatable size */ 467 #define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_MAX) 468 /* Maximum size for which we actually use a slab cache */ 469 #define KMALLOC_MAX_CACHE_SIZE (1UL << KMALLOC_SHIFT_HIGH) 470 /* Maximum order allocatable via the slab allocator */ 471 #define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_MAX - PAGE_SHIFT) 472 473 /* 474 * Kmalloc subsystem. 475 */ 476 #ifndef KMALLOC_MIN_SIZE 477 #define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW) 478 #endif 479 480 /* 481 * This restriction comes from byte sized index implementation. 482 * Page size is normally 2^12 bytes and, in this case, if we want to use 483 * byte sized index which can represent 2^8 entries, the size of the object 484 * should be equal or greater to 2^12 / 2^8 = 2^4 = 16. 485 * If minimum size of kmalloc is less than 16, we use it as minimum object 486 * size and give up to use byte sized index. 487 */ 488 #define SLAB_OBJ_MIN_SIZE (KMALLOC_MIN_SIZE < 16 ? \ 489 (KMALLOC_MIN_SIZE) : 16) 490 491 #ifdef CONFIG_RANDOM_KMALLOC_CACHES 492 #define RANDOM_KMALLOC_CACHES_NR 15 // # of cache copies 493 #else 494 #define RANDOM_KMALLOC_CACHES_NR 0 495 #endif 496 497 /* 498 * Whenever changing this, take care of that kmalloc_type() and 499 * create_kmalloc_caches() still work as intended. 500 * 501 * KMALLOC_NORMAL can contain only unaccounted objects whereas KMALLOC_CGROUP 502 * is for accounted but unreclaimable and non-dma objects. All the other 503 * kmem caches can have both accounted and unaccounted objects. 504 */ 505 enum kmalloc_cache_type { 506 KMALLOC_NORMAL = 0, 507 #ifndef CONFIG_ZONE_DMA 508 KMALLOC_DMA = KMALLOC_NORMAL, 509 #endif 510 #ifndef CONFIG_MEMCG 511 KMALLOC_CGROUP = KMALLOC_NORMAL, 512 #endif 513 KMALLOC_RANDOM_START = KMALLOC_NORMAL, 514 KMALLOC_RANDOM_END = KMALLOC_RANDOM_START + RANDOM_KMALLOC_CACHES_NR, 515 #ifdef CONFIG_SLUB_TINY 516 KMALLOC_RECLAIM = KMALLOC_NORMAL, 517 #else 518 KMALLOC_RECLAIM, 519 #endif 520 #ifdef CONFIG_ZONE_DMA 521 KMALLOC_DMA, 522 #endif 523 #ifdef CONFIG_MEMCG 524 KMALLOC_CGROUP, 525 #endif 526 NR_KMALLOC_TYPES 527 }; 528 529 typedef struct kmem_cache * kmem_buckets[KMALLOC_SHIFT_HIGH + 1]; 530 531 extern kmem_buckets kmalloc_caches[NR_KMALLOC_TYPES]; 532 533 /* 534 * Define gfp bits that should not be set for KMALLOC_NORMAL. 535 */ 536 #define KMALLOC_NOT_NORMAL_BITS \ 537 (__GFP_RECLAIMABLE | \ 538 (IS_ENABLED(CONFIG_ZONE_DMA) ? __GFP_DMA : 0) | \ 539 (IS_ENABLED(CONFIG_MEMCG) ? __GFP_ACCOUNT : 0)) 540 541 extern unsigned long random_kmalloc_seed; 542 543 static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags, unsigned long caller) 544 { 545 /* 546 * The most common case is KMALLOC_NORMAL, so test for it 547 * with a single branch for all the relevant flags. 548 */ 549 if (likely((flags & KMALLOC_NOT_NORMAL_BITS) == 0)) 550 #ifdef CONFIG_RANDOM_KMALLOC_CACHES 551 /* RANDOM_KMALLOC_CACHES_NR (=15) copies + the KMALLOC_NORMAL */ 552 return KMALLOC_RANDOM_START + hash_64(caller ^ random_kmalloc_seed, 553 ilog2(RANDOM_KMALLOC_CACHES_NR + 1)); 554 #else 555 return KMALLOC_NORMAL; 556 #endif 557 558 /* 559 * At least one of the flags has to be set. Their priorities in 560 * decreasing order are: 561 * 1) __GFP_DMA 562 * 2) __GFP_RECLAIMABLE 563 * 3) __GFP_ACCOUNT 564 */ 565 if (IS_ENABLED(CONFIG_ZONE_DMA) && (flags & __GFP_DMA)) 566 return KMALLOC_DMA; 567 if (!IS_ENABLED(CONFIG_MEMCG) || (flags & __GFP_RECLAIMABLE)) 568 return KMALLOC_RECLAIM; 569 else 570 return KMALLOC_CGROUP; 571 } 572 573 /* 574 * Figure out which kmalloc slab an allocation of a certain size 575 * belongs to. 576 * 0 = zero alloc 577 * 1 = 65 .. 96 bytes 578 * 2 = 129 .. 192 bytes 579 * n = 2^(n-1)+1 .. 2^n 580 * 581 * Note: __kmalloc_index() is compile-time optimized, and not runtime optimized; 582 * typical usage is via kmalloc_index() and therefore evaluated at compile-time. 583 * Callers where !size_is_constant should only be test modules, where runtime 584 * overheads of __kmalloc_index() can be tolerated. Also see kmalloc_slab(). 585 */ 586 static __always_inline unsigned int __kmalloc_index(size_t size, 587 bool size_is_constant) 588 { 589 if (!size) 590 return 0; 591 592 if (size <= KMALLOC_MIN_SIZE) 593 return KMALLOC_SHIFT_LOW; 594 595 if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96) 596 return 1; 597 if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192) 598 return 2; 599 if (size <= 8) return 3; 600 if (size <= 16) return 4; 601 if (size <= 32) return 5; 602 if (size <= 64) return 6; 603 if (size <= 128) return 7; 604 if (size <= 256) return 8; 605 if (size <= 512) return 9; 606 if (size <= 1024) return 10; 607 if (size <= 2 * 1024) return 11; 608 if (size <= 4 * 1024) return 12; 609 if (size <= 8 * 1024) return 13; 610 if (size <= 16 * 1024) return 14; 611 if (size <= 32 * 1024) return 15; 612 if (size <= 64 * 1024) return 16; 613 if (size <= 128 * 1024) return 17; 614 if (size <= 256 * 1024) return 18; 615 if (size <= 512 * 1024) return 19; 616 if (size <= 1024 * 1024) return 20; 617 if (size <= 2 * 1024 * 1024) return 21; 618 619 if (!IS_ENABLED(CONFIG_PROFILE_ALL_BRANCHES) && size_is_constant) 620 BUILD_BUG_ON_MSG(1, "unexpected size in kmalloc_index()"); 621 else 622 BUG(); 623 624 /* Will never be reached. Needed because the compiler may complain */ 625 return -1; 626 } 627 static_assert(PAGE_SHIFT <= 20); 628 #define kmalloc_index(s) __kmalloc_index(s, true) 629 630 #include <linux/alloc_tag.h> 631 632 /** 633 * kmem_cache_alloc - Allocate an object 634 * @cachep: The cache to allocate from. 635 * @flags: See kmalloc(). 636 * 637 * Allocate an object from this cache. 638 * See kmem_cache_zalloc() for a shortcut of adding __GFP_ZERO to flags. 639 * 640 * Return: pointer to the new object or %NULL in case of error 641 */ 642 void *kmem_cache_alloc_noprof(struct kmem_cache *cachep, 643 gfp_t flags) __assume_slab_alignment __malloc; 644 #define kmem_cache_alloc(...) alloc_hooks(kmem_cache_alloc_noprof(__VA_ARGS__)) 645 646 void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru, 647 gfp_t gfpflags) __assume_slab_alignment __malloc; 648 #define kmem_cache_alloc_lru(...) alloc_hooks(kmem_cache_alloc_lru_noprof(__VA_ARGS__)) 649 650 void kmem_cache_free(struct kmem_cache *s, void *objp); 651 652 kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags, 653 unsigned int useroffset, unsigned int usersize, 654 void (*ctor)(void *)); 655 656 /* 657 * Bulk allocation and freeing operations. These are accelerated in an 658 * allocator specific way to avoid taking locks repeatedly or building 659 * metadata structures unnecessarily. 660 * 661 * Note that interrupts must be enabled when calling these functions. 662 */ 663 void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p); 664 665 int kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags, size_t size, void **p); 666 #define kmem_cache_alloc_bulk(...) alloc_hooks(kmem_cache_alloc_bulk_noprof(__VA_ARGS__)) 667 668 static __always_inline void kfree_bulk(size_t size, void **p) 669 { 670 kmem_cache_free_bulk(NULL, size, p); 671 } 672 673 void *kmem_cache_alloc_node_noprof(struct kmem_cache *s, gfp_t flags, 674 int node) __assume_slab_alignment __malloc; 675 #define kmem_cache_alloc_node(...) alloc_hooks(kmem_cache_alloc_node_noprof(__VA_ARGS__)) 676 677 /* 678 * These macros allow declaring a kmem_buckets * parameter alongside size, which 679 * can be compiled out with CONFIG_SLAB_BUCKETS=n so that a large number of call 680 * sites don't have to pass NULL. 681 */ 682 #ifdef CONFIG_SLAB_BUCKETS 683 #define DECL_BUCKET_PARAMS(_size, _b) size_t (_size), kmem_buckets *(_b) 684 #define PASS_BUCKET_PARAMS(_size, _b) (_size), (_b) 685 #define PASS_BUCKET_PARAM(_b) (_b) 686 #else 687 #define DECL_BUCKET_PARAMS(_size, _b) size_t (_size) 688 #define PASS_BUCKET_PARAMS(_size, _b) (_size) 689 #define PASS_BUCKET_PARAM(_b) NULL 690 #endif 691 692 /* 693 * The following functions are not to be used directly and are intended only 694 * for internal use from kmalloc() and kmalloc_node() 695 * with the exception of kunit tests 696 */ 697 698 void *__kmalloc_noprof(size_t size, gfp_t flags) 699 __assume_kmalloc_alignment __alloc_size(1); 700 701 void *__kmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node) 702 __assume_kmalloc_alignment __alloc_size(1); 703 704 void *__kmalloc_cache_noprof(struct kmem_cache *s, gfp_t flags, size_t size) 705 __assume_kmalloc_alignment __alloc_size(3); 706 707 void *__kmalloc_cache_node_noprof(struct kmem_cache *s, gfp_t gfpflags, 708 int node, size_t size) 709 __assume_kmalloc_alignment __alloc_size(4); 710 711 void *__kmalloc_large_noprof(size_t size, gfp_t flags) 712 __assume_page_alignment __alloc_size(1); 713 714 void *__kmalloc_large_node_noprof(size_t size, gfp_t flags, int node) 715 __assume_page_alignment __alloc_size(1); 716 717 /** 718 * kmalloc - allocate kernel memory 719 * @size: how many bytes of memory are required. 720 * @flags: describe the allocation context 721 * 722 * kmalloc is the normal method of allocating memory 723 * for objects smaller than page size in the kernel. 724 * 725 * The allocated object address is aligned to at least ARCH_KMALLOC_MINALIGN 726 * bytes. For @size of power of two bytes, the alignment is also guaranteed 727 * to be at least to the size. For other sizes, the alignment is guaranteed to 728 * be at least the largest power-of-two divisor of @size. 729 * 730 * The @flags argument may be one of the GFP flags defined at 731 * include/linux/gfp_types.h and described at 732 * :ref:`Documentation/core-api/mm-api.rst <mm-api-gfp-flags>` 733 * 734 * The recommended usage of the @flags is described at 735 * :ref:`Documentation/core-api/memory-allocation.rst <memory_allocation>` 736 * 737 * Below is a brief outline of the most useful GFP flags 738 * 739 * %GFP_KERNEL 740 * Allocate normal kernel ram. May sleep. 741 * 742 * %GFP_NOWAIT 743 * Allocation will not sleep. 744 * 745 * %GFP_ATOMIC 746 * Allocation will not sleep. May use emergency pools. 747 * 748 * Also it is possible to set different flags by OR'ing 749 * in one or more of the following additional @flags: 750 * 751 * %__GFP_ZERO 752 * Zero the allocated memory before returning. Also see kzalloc(). 753 * 754 * %__GFP_HIGH 755 * This allocation has high priority and may use emergency pools. 756 * 757 * %__GFP_NOFAIL 758 * Indicate that this allocation is in no way allowed to fail 759 * (think twice before using). 760 * 761 * %__GFP_NORETRY 762 * If memory is not immediately available, 763 * then give up at once. 764 * 765 * %__GFP_NOWARN 766 * If allocation fails, don't issue any warnings. 767 * 768 * %__GFP_RETRY_MAYFAIL 769 * Try really hard to succeed the allocation but fail 770 * eventually. 771 */ 772 static __always_inline __alloc_size(1) void *kmalloc_noprof(size_t size, gfp_t flags) 773 { 774 if (__builtin_constant_p(size) && size) { 775 unsigned int index; 776 777 if (size > KMALLOC_MAX_CACHE_SIZE) 778 return __kmalloc_large_noprof(size, flags); 779 780 index = kmalloc_index(size); 781 return __kmalloc_cache_noprof( 782 kmalloc_caches[kmalloc_type(flags, _RET_IP_)][index], 783 flags, size); 784 } 785 return __kmalloc_noprof(size, flags); 786 } 787 #define kmalloc(...) alloc_hooks(kmalloc_noprof(__VA_ARGS__)) 788 789 #define kmem_buckets_alloc(_b, _size, _flags) \ 790 alloc_hooks(__kmalloc_node_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE)) 791 792 #define kmem_buckets_alloc_track_caller(_b, _size, _flags) \ 793 alloc_hooks(__kmalloc_node_track_caller_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE, _RET_IP_)) 794 795 static __always_inline __alloc_size(1) void *kmalloc_node_noprof(size_t size, gfp_t flags, int node) 796 { 797 if (__builtin_constant_p(size) && size) { 798 unsigned int index; 799 800 if (size > KMALLOC_MAX_CACHE_SIZE) 801 return __kmalloc_large_node_noprof(size, flags, node); 802 803 index = kmalloc_index(size); 804 return __kmalloc_cache_node_noprof( 805 kmalloc_caches[kmalloc_type(flags, _RET_IP_)][index], 806 flags, node, size); 807 } 808 return __kmalloc_node_noprof(PASS_BUCKET_PARAMS(size, NULL), flags, node); 809 } 810 #define kmalloc_node(...) alloc_hooks(kmalloc_node_noprof(__VA_ARGS__)) 811 812 /** 813 * kmalloc_array - allocate memory for an array. 814 * @n: number of elements. 815 * @size: element size. 816 * @flags: the type of memory to allocate (see kmalloc). 817 */ 818 static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t size, gfp_t flags) 819 { 820 size_t bytes; 821 822 if (unlikely(check_mul_overflow(n, size, &bytes))) 823 return NULL; 824 if (__builtin_constant_p(n) && __builtin_constant_p(size)) 825 return kmalloc_noprof(bytes, flags); 826 return kmalloc_noprof(bytes, flags); 827 } 828 #define kmalloc_array(...) alloc_hooks(kmalloc_array_noprof(__VA_ARGS__)) 829 830 /** 831 * krealloc_array - reallocate memory for an array. 832 * @p: pointer to the memory chunk to reallocate 833 * @new_n: new number of elements to alloc 834 * @new_size: new size of a single member of the array 835 * @flags: the type of memory to allocate (see kmalloc) 836 */ 837 static inline __realloc_size(2, 3) void * __must_check krealloc_array_noprof(void *p, 838 size_t new_n, 839 size_t new_size, 840 gfp_t flags) 841 { 842 size_t bytes; 843 844 if (unlikely(check_mul_overflow(new_n, new_size, &bytes))) 845 return NULL; 846 847 return krealloc_noprof(p, bytes, flags); 848 } 849 #define krealloc_array(...) alloc_hooks(krealloc_array_noprof(__VA_ARGS__)) 850 851 /** 852 * kcalloc - allocate memory for an array. The memory is set to zero. 853 * @n: number of elements. 854 * @size: element size. 855 * @flags: the type of memory to allocate (see kmalloc). 856 */ 857 #define kcalloc(n, size, flags) kmalloc_array(n, size, (flags) | __GFP_ZERO) 858 859 void *__kmalloc_node_track_caller_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node, 860 unsigned long caller) __alloc_size(1); 861 #define kmalloc_node_track_caller_noprof(size, flags, node, caller) \ 862 __kmalloc_node_track_caller_noprof(PASS_BUCKET_PARAMS(size, NULL), flags, node, caller) 863 #define kmalloc_node_track_caller(...) \ 864 alloc_hooks(kmalloc_node_track_caller_noprof(__VA_ARGS__, _RET_IP_)) 865 866 /* 867 * kmalloc_track_caller is a special version of kmalloc that records the 868 * calling function of the routine calling it for slab leak tracking instead 869 * of just the calling function (confusing, eh?). 870 * It's useful when the call to kmalloc comes from a widely-used standard 871 * allocator where we care about the real place the memory allocation 872 * request comes from. 873 */ 874 #define kmalloc_track_caller(...) kmalloc_node_track_caller(__VA_ARGS__, NUMA_NO_NODE) 875 876 #define kmalloc_track_caller_noprof(...) \ 877 kmalloc_node_track_caller_noprof(__VA_ARGS__, NUMA_NO_NODE, _RET_IP_) 878 879 static inline __alloc_size(1, 2) void *kmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, 880 int node) 881 { 882 size_t bytes; 883 884 if (unlikely(check_mul_overflow(n, size, &bytes))) 885 return NULL; 886 if (__builtin_constant_p(n) && __builtin_constant_p(size)) 887 return kmalloc_node_noprof(bytes, flags, node); 888 return __kmalloc_node_noprof(PASS_BUCKET_PARAMS(bytes, NULL), flags, node); 889 } 890 #define kmalloc_array_node(...) alloc_hooks(kmalloc_array_node_noprof(__VA_ARGS__)) 891 892 #define kcalloc_node(_n, _size, _flags, _node) \ 893 kmalloc_array_node(_n, _size, (_flags) | __GFP_ZERO, _node) 894 895 /* 896 * Shortcuts 897 */ 898 #define kmem_cache_zalloc(_k, _flags) kmem_cache_alloc(_k, (_flags)|__GFP_ZERO) 899 900 /** 901 * kzalloc - allocate memory. The memory is set to zero. 902 * @size: how many bytes of memory are required. 903 * @flags: the type of memory to allocate (see kmalloc). 904 */ 905 static inline __alloc_size(1) void *kzalloc_noprof(size_t size, gfp_t flags) 906 { 907 return kmalloc_noprof(size, flags | __GFP_ZERO); 908 } 909 #define kzalloc(...) alloc_hooks(kzalloc_noprof(__VA_ARGS__)) 910 #define kzalloc_node(_size, _flags, _node) kmalloc_node(_size, (_flags)|__GFP_ZERO, _node) 911 912 void *__kvmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node) __alloc_size(1); 913 #define kvmalloc_node_noprof(size, flags, node) \ 914 __kvmalloc_node_noprof(PASS_BUCKET_PARAMS(size, NULL), flags, node) 915 #define kvmalloc_node(...) alloc_hooks(kvmalloc_node_noprof(__VA_ARGS__)) 916 917 #define kvmalloc(_size, _flags) kvmalloc_node(_size, _flags, NUMA_NO_NODE) 918 #define kvmalloc_noprof(_size, _flags) kvmalloc_node_noprof(_size, _flags, NUMA_NO_NODE) 919 #define kvzalloc(_size, _flags) kvmalloc(_size, (_flags)|__GFP_ZERO) 920 921 #define kvzalloc_node(_size, _flags, _node) kvmalloc_node(_size, (_flags)|__GFP_ZERO, _node) 922 #define kmem_buckets_valloc(_b, _size, _flags) \ 923 alloc_hooks(__kvmalloc_node_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE)) 924 925 static inline __alloc_size(1, 2) void * 926 kvmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, int node) 927 { 928 size_t bytes; 929 930 if (unlikely(check_mul_overflow(n, size, &bytes))) 931 return NULL; 932 933 return kvmalloc_node_noprof(bytes, flags, node); 934 } 935 936 #define kvmalloc_array_noprof(...) kvmalloc_array_node_noprof(__VA_ARGS__, NUMA_NO_NODE) 937 #define kvcalloc_node_noprof(_n,_s,_f,_node) kvmalloc_array_node_noprof(_n,_s,(_f)|__GFP_ZERO,_node) 938 #define kvcalloc_noprof(...) kvcalloc_node_noprof(__VA_ARGS__, NUMA_NO_NODE) 939 940 #define kvmalloc_array(...) alloc_hooks(kvmalloc_array_noprof(__VA_ARGS__)) 941 #define kvcalloc_node(...) alloc_hooks(kvcalloc_node_noprof(__VA_ARGS__)) 942 #define kvcalloc(...) alloc_hooks(kvcalloc_noprof(__VA_ARGS__)) 943 944 extern void *kvrealloc_noprof(const void *p, size_t oldsize, size_t newsize, gfp_t flags) 945 __realloc_size(3); 946 #define kvrealloc(...) alloc_hooks(kvrealloc_noprof(__VA_ARGS__)) 947 948 extern void kvfree(const void *addr); 949 DEFINE_FREE(kvfree, void *, if (!IS_ERR_OR_NULL(_T)) kvfree(_T)) 950 951 extern void kvfree_sensitive(const void *addr, size_t len); 952 953 unsigned int kmem_cache_size(struct kmem_cache *s); 954 955 /** 956 * kmalloc_size_roundup - Report allocation bucket size for the given size 957 * 958 * @size: Number of bytes to round up from. 959 * 960 * This returns the number of bytes that would be available in a kmalloc() 961 * allocation of @size bytes. For example, a 126 byte request would be 962 * rounded up to the next sized kmalloc bucket, 128 bytes. (This is strictly 963 * for the general-purpose kmalloc()-based allocations, and is not for the 964 * pre-sized kmem_cache_alloc()-based allocations.) 965 * 966 * Use this to kmalloc() the full bucket size ahead of time instead of using 967 * ksize() to query the size after an allocation. 968 */ 969 size_t kmalloc_size_roundup(size_t size); 970 971 void __init kmem_cache_init_late(void); 972 973 #endif /* _LINUX_SLAB_H */ 974