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