slab.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. #ifndef _LINUX_SLAB_H
  12. #define _LINUX_SLAB_H
  13. #include <linux/gfp.h>
  14. #include <linux/overflow.h>
  15. #include <linux/types.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/percpu-refcount.h>
  18. /*
  19. * Flags to pass to kmem_cache_create().
  20. * The ones marked DEBUG are only valid if CONFIG_DEBUG_SLAB is set.
  21. */
  22. /* DEBUG: Perform (expensive) checks on alloc/free */
  23. #define SLAB_CONSISTENCY_CHECKS ((slab_flags_t __force)0x00000100U)
  24. /* DEBUG: Red zone objs in a cache */
  25. #define SLAB_RED_ZONE ((slab_flags_t __force)0x00000400U)
  26. /* DEBUG: Poison objects */
  27. #define SLAB_POISON ((slab_flags_t __force)0x00000800U)
  28. /* Indicate a kmalloc slab */
  29. #define SLAB_KMALLOC ((slab_flags_t __force)0x00001000U)
  30. /* Align objs on cache lines */
  31. #define SLAB_HWCACHE_ALIGN ((slab_flags_t __force)0x00002000U)
  32. /* Use GFP_DMA memory */
  33. #define SLAB_CACHE_DMA ((slab_flags_t __force)0x00004000U)
  34. /* Use GFP_DMA32 memory */
  35. #define SLAB_CACHE_DMA32 ((slab_flags_t __force)0x00008000U)
  36. /* DEBUG: Store the last owner for bug hunting */
  37. #define SLAB_STORE_USER ((slab_flags_t __force)0x00010000U)
  38. /* Panic if kmem_cache_create() fails */
  39. #define SLAB_PANIC ((slab_flags_t __force)0x00040000U)
  40. /*
  41. * SLAB_TYPESAFE_BY_RCU - **WARNING** READ THIS!
  42. *
  43. * This delays freeing the SLAB page by a grace period, it does _NOT_
  44. * delay object freeing. This means that if you do kmem_cache_free()
  45. * that memory location is free to be reused at any time. Thus it may
  46. * be possible to see another object there in the same RCU grace period.
  47. *
  48. * This feature only ensures the memory location backing the object
  49. * stays valid, the trick to using this is relying on an independent
  50. * object validation pass. Something like:
  51. *
  52. * rcu_read_lock()
  53. * again:
  54. * obj = lockless_lookup(key);
  55. * if (obj) {
  56. * if (!try_get_ref(obj)) // might fail for free objects
  57. * goto again;
  58. *
  59. * if (obj->key != key) { // not the object we expected
  60. * put_ref(obj);
  61. * goto again;
  62. * }
  63. * }
  64. * rcu_read_unlock();
  65. *
  66. * This is useful if we need to approach a kernel structure obliquely,
  67. * from its address obtained without the usual locking. We can lock
  68. * the structure to stabilize it and check it's still at the given address,
  69. * only if we can be sure that the memory has not been meanwhile reused
  70. * for some other kind of object (which our subsystem's lock might corrupt).
  71. *
  72. * rcu_read_lock before reading the address, then rcu_read_unlock after
  73. * taking the spinlock within the structure expected at that address.
  74. *
  75. * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU.
  76. */
  77. /* Defer freeing slabs to RCU */
  78. #define SLAB_TYPESAFE_BY_RCU ((slab_flags_t __force)0x00080000U)
  79. /* Spread some memory over cpuset */
  80. #define SLAB_MEM_SPREAD ((slab_flags_t __force)0x00100000U)
  81. /* Trace allocations and frees */
  82. #define SLAB_TRACE ((slab_flags_t __force)0x00200000U)
  83. /* Flag to prevent checks on free */
  84. #ifdef CONFIG_DEBUG_OBJECTS
  85. # define SLAB_DEBUG_OBJECTS ((slab_flags_t __force)0x00400000U)
  86. #else
  87. # define SLAB_DEBUG_OBJECTS 0
  88. #endif
  89. /* Avoid kmemleak tracing */
  90. #define SLAB_NOLEAKTRACE ((slab_flags_t __force)0x00800000U)
  91. /* Fault injection mark */
  92. #ifdef CONFIG_FAILSLAB
  93. # define SLAB_FAILSLAB ((slab_flags_t __force)0x02000000U)
  94. #else
  95. # define SLAB_FAILSLAB 0
  96. #endif
  97. /* Account to memcg */
  98. #ifdef CONFIG_MEMCG_KMEM
  99. # define SLAB_ACCOUNT ((slab_flags_t __force)0x04000000U)
  100. #else
  101. # define SLAB_ACCOUNT 0
  102. #endif
  103. #ifdef CONFIG_KASAN_GENERIC
  104. #define SLAB_KASAN ((slab_flags_t __force)0x08000000U)
  105. #else
  106. #define SLAB_KASAN 0
  107. #endif
  108. /*
  109. * Ignore user specified debugging flags.
  110. * Intended for caches created for self-tests so they have only flags
  111. * specified in the code and other flags are ignored.
  112. */
  113. #define SLAB_NO_USER_FLAGS ((slab_flags_t __force)0x10000000U)
  114. #ifdef CONFIG_KFENCE
  115. #define SLAB_SKIP_KFENCE ((slab_flags_t __force)0x20000000U)
  116. #else
  117. #define SLAB_SKIP_KFENCE 0
  118. #endif
  119. /* The following flags affect the page allocator grouping pages by mobility */
  120. /* Objects are reclaimable */
  121. #define SLAB_RECLAIM_ACCOUNT ((slab_flags_t __force)0x00020000U)
  122. #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT /* Objects are short-lived */
  123. /*
  124. * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
  125. *
  126. * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
  127. *
  128. * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
  129. * Both make kfree a no-op.
  130. */
  131. #define ZERO_SIZE_PTR ((void *)16)
  132. #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
  133. (unsigned long)ZERO_SIZE_PTR)
  134. #include <linux/kasan.h>
  135. struct list_lru;
  136. struct mem_cgroup;
  137. /*
  138. * struct kmem_cache related prototypes
  139. */
  140. void __init kmem_cache_init(void);
  141. bool slab_is_available(void);
  142. struct kmem_cache *kmem_cache_create(const char *name, unsigned int size,
  143. unsigned int align, slab_flags_t flags,
  144. void (*ctor)(void *));
  145. struct kmem_cache *kmem_cache_create_usercopy(const char *name,
  146. unsigned int size, unsigned int align,
  147. slab_flags_t flags,
  148. unsigned int useroffset, unsigned int usersize,
  149. void (*ctor)(void *));
  150. void kmem_cache_destroy(struct kmem_cache *s);
  151. int kmem_cache_shrink(struct kmem_cache *s);
  152. /*
  153. * Please use this macro to create slab caches. Simply specify the
  154. * name of the structure and maybe some flags that are listed above.
  155. *
  156. * The alignment of the struct determines object alignment. If you
  157. * f.e. add ____cacheline_aligned_in_smp to the struct declaration
  158. * then the objects will be properly aligned in SMP configurations.
  159. */
  160. #define KMEM_CACHE(__struct, __flags) \
  161. kmem_cache_create(#__struct, sizeof(struct __struct), \
  162. __alignof__(struct __struct), (__flags), NULL)
  163. /*
  164. * To whitelist a single field for copying to/from usercopy, use this
  165. * macro instead for KMEM_CACHE() above.
  166. */
  167. #define KMEM_CACHE_USERCOPY(__struct, __flags, __field) \
  168. kmem_cache_create_usercopy(#__struct, \
  169. sizeof(struct __struct), \
  170. __alignof__(struct __struct), (__flags), \
  171. offsetof(struct __struct, __field), \
  172. sizeof_field(struct __struct, __field), NULL)
  173. /*
  174. * Common kmalloc functions provided by all allocators
  175. */
  176. void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __realloc_size(2);
  177. void kfree(const void *objp);
  178. void kfree_sensitive(const void *objp);
  179. size_t __ksize(const void *objp);
  180. /**
  181. * ksize - Report actual allocation size of associated object
  182. *
  183. * @objp: Pointer returned from a prior kmalloc()-family allocation.
  184. *
  185. * This should not be used for writing beyond the originally requested
  186. * allocation size. Either use krealloc() or round up the allocation size
  187. * with kmalloc_size_roundup() prior to allocation. If this is used to
  188. * access beyond the originally requested allocation size, UBSAN_BOUNDS
  189. * and/or FORTIFY_SOURCE may trip, since they only know about the
  190. * originally allocated size via the __alloc_size attribute.
  191. */
  192. size_t ksize(const void *objp);
  193. #ifdef CONFIG_PRINTK
  194. bool kmem_valid_obj(void *object);
  195. void kmem_dump_obj(void *object);
  196. #endif
  197. /*
  198. * Some archs want to perform DMA into kmalloc caches and need a guaranteed
  199. * alignment larger than the alignment of a 64-bit integer.
  200. * Setting ARCH_DMA_MINALIGN in arch headers allows that.
  201. */
  202. #if defined(ARCH_DMA_MINALIGN) && ARCH_DMA_MINALIGN > 8
  203. #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN
  204. #define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN
  205. #define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN)
  206. #else
  207. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
  208. #endif
  209. /*
  210. * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
  211. * Intended for arches that get misalignment faults even for 64 bit integer
  212. * aligned buffers.
  213. */
  214. #ifndef ARCH_SLAB_MINALIGN
  215. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
  216. #endif
  217. /*
  218. * Arches can define this function if they want to decide the minimum slab
  219. * alignment at runtime. The value returned by the function must be a power
  220. * of two and >= ARCH_SLAB_MINALIGN.
  221. */
  222. #ifndef arch_slab_minalign
  223. static inline unsigned int arch_slab_minalign(void)
  224. {
  225. return ARCH_SLAB_MINALIGN;
  226. }
  227. #endif
  228. /*
  229. * kmem_cache_alloc and friends return pointers aligned to ARCH_SLAB_MINALIGN.
  230. * kmalloc and friends return pointers aligned to both ARCH_KMALLOC_MINALIGN
  231. * and ARCH_SLAB_MINALIGN, but here we only assume the former alignment.
  232. */
  233. #define __assume_kmalloc_alignment __assume_aligned(ARCH_KMALLOC_MINALIGN)
  234. #define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MINALIGN)
  235. #define __assume_page_alignment __assume_aligned(PAGE_SIZE)
  236. /*
  237. * Kmalloc array related definitions
  238. */
  239. #ifdef CONFIG_SLAB
  240. /*
  241. * SLAB and SLUB directly allocates requests fitting in to an order-1 page
  242. * (PAGE_SIZE*2). Larger requests are passed to the page allocator.
  243. */
  244. #define KMALLOC_SHIFT_HIGH (PAGE_SHIFT + 1)
  245. #define KMALLOC_SHIFT_MAX (MAX_ORDER + PAGE_SHIFT - 1)
  246. #ifndef KMALLOC_SHIFT_LOW
  247. #define KMALLOC_SHIFT_LOW 5
  248. #endif
  249. #endif
  250. #ifdef CONFIG_SLUB
  251. #define KMALLOC_SHIFT_HIGH (PAGE_SHIFT + 1)
  252. #define KMALLOC_SHIFT_MAX (MAX_ORDER + PAGE_SHIFT - 1)
  253. #ifndef KMALLOC_SHIFT_LOW
  254. #define KMALLOC_SHIFT_LOW 3
  255. #endif
  256. #endif
  257. #ifdef CONFIG_SLOB
  258. /*
  259. * SLOB passes all requests larger than one page to the page allocator.
  260. * No kmalloc array is necessary since objects of different sizes can
  261. * be allocated from the same page.
  262. */
  263. #define KMALLOC_SHIFT_HIGH PAGE_SHIFT
  264. #define KMALLOC_SHIFT_MAX (MAX_ORDER + PAGE_SHIFT - 1)
  265. #ifndef KMALLOC_SHIFT_LOW
  266. #define KMALLOC_SHIFT_LOW 3
  267. #endif
  268. #endif
  269. /* Maximum allocatable size */
  270. #define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_MAX)
  271. /* Maximum size for which we actually use a slab cache */
  272. #define KMALLOC_MAX_CACHE_SIZE (1UL << KMALLOC_SHIFT_HIGH)
  273. /* Maximum order allocatable via the slab allocator */
  274. #define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_MAX - PAGE_SHIFT)
  275. /*
  276. * Kmalloc subsystem.
  277. */
  278. #ifndef KMALLOC_MIN_SIZE
  279. #define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW)
  280. #endif
  281. /*
  282. * This restriction comes from byte sized index implementation.
  283. * Page size is normally 2^12 bytes and, in this case, if we want to use
  284. * byte sized index which can represent 2^8 entries, the size of the object
  285. * should be equal or greater to 2^12 / 2^8 = 2^4 = 16.
  286. * If minimum size of kmalloc is less than 16, we use it as minimum object
  287. * size and give up to use byte sized index.
  288. */
  289. #define SLAB_OBJ_MIN_SIZE (KMALLOC_MIN_SIZE < 16 ? \
  290. (KMALLOC_MIN_SIZE) : 16)
  291. /*
  292. * Whenever changing this, take care of that kmalloc_type() and
  293. * create_kmalloc_caches() still work as intended.
  294. *
  295. * KMALLOC_NORMAL can contain only unaccounted objects whereas KMALLOC_CGROUP
  296. * is for accounted but unreclaimable and non-dma objects. All the other
  297. * kmem caches can have both accounted and unaccounted objects.
  298. */
  299. enum kmalloc_cache_type {
  300. KMALLOC_NORMAL = 0,
  301. #ifndef CONFIG_ZONE_DMA
  302. KMALLOC_DMA = KMALLOC_NORMAL,
  303. #endif
  304. #ifndef CONFIG_MEMCG_KMEM
  305. KMALLOC_CGROUP = KMALLOC_NORMAL,
  306. #else
  307. KMALLOC_CGROUP,
  308. #endif
  309. KMALLOC_RECLAIM,
  310. #ifdef CONFIG_ZONE_DMA
  311. KMALLOC_DMA,
  312. #endif
  313. NR_KMALLOC_TYPES
  314. };
  315. #ifndef CONFIG_SLOB
  316. extern struct kmem_cache *
  317. kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1];
  318. /*
  319. * Define gfp bits that should not be set for KMALLOC_NORMAL.
  320. */
  321. #define KMALLOC_NOT_NORMAL_BITS \
  322. (__GFP_RECLAIMABLE | \
  323. (IS_ENABLED(CONFIG_ZONE_DMA) ? __GFP_DMA : 0) | \
  324. (IS_ENABLED(CONFIG_MEMCG_KMEM) ? __GFP_ACCOUNT : 0))
  325. static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
  326. {
  327. /*
  328. * The most common case is KMALLOC_NORMAL, so test for it
  329. * with a single branch for all the relevant flags.
  330. */
  331. if (likely((flags & KMALLOC_NOT_NORMAL_BITS) == 0))
  332. return KMALLOC_NORMAL;
  333. /*
  334. * At least one of the flags has to be set. Their priorities in
  335. * decreasing order are:
  336. * 1) __GFP_DMA
  337. * 2) __GFP_RECLAIMABLE
  338. * 3) __GFP_ACCOUNT
  339. */
  340. if (IS_ENABLED(CONFIG_ZONE_DMA) && (flags & __GFP_DMA))
  341. return KMALLOC_DMA;
  342. if (!IS_ENABLED(CONFIG_MEMCG_KMEM) || (flags & __GFP_RECLAIMABLE))
  343. return KMALLOC_RECLAIM;
  344. else
  345. return KMALLOC_CGROUP;
  346. }
  347. /*
  348. * Figure out which kmalloc slab an allocation of a certain size
  349. * belongs to.
  350. * 0 = zero alloc
  351. * 1 = 65 .. 96 bytes
  352. * 2 = 129 .. 192 bytes
  353. * n = 2^(n-1)+1 .. 2^n
  354. *
  355. * Note: __kmalloc_index() is compile-time optimized, and not runtime optimized;
  356. * typical usage is via kmalloc_index() and therefore evaluated at compile-time.
  357. * Callers where !size_is_constant should only be test modules, where runtime
  358. * overheads of __kmalloc_index() can be tolerated. Also see kmalloc_slab().
  359. */
  360. static __always_inline unsigned int __kmalloc_index(size_t size,
  361. bool size_is_constant)
  362. {
  363. if (!size)
  364. return 0;
  365. if (size <= KMALLOC_MIN_SIZE)
  366. return KMALLOC_SHIFT_LOW;
  367. if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96)
  368. return 1;
  369. if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
  370. return 2;
  371. if (size <= 8) return 3;
  372. if (size <= 16) return 4;
  373. if (size <= 32) return 5;
  374. if (size <= 64) return 6;
  375. if (size <= 128) return 7;
  376. if (size <= 256) return 8;
  377. if (size <= 512) return 9;
  378. if (size <= 1024) return 10;
  379. if (size <= 2 * 1024) return 11;
  380. if (size <= 4 * 1024) return 12;
  381. if (size <= 8 * 1024) return 13;
  382. if (size <= 16 * 1024) return 14;
  383. if (size <= 32 * 1024) return 15;
  384. if (size <= 64 * 1024) return 16;
  385. if (size <= 128 * 1024) return 17;
  386. if (size <= 256 * 1024) return 18;
  387. if (size <= 512 * 1024) return 19;
  388. if (size <= 1024 * 1024) return 20;
  389. if (size <= 2 * 1024 * 1024) return 21;
  390. if (!IS_ENABLED(CONFIG_PROFILE_ALL_BRANCHES) && size_is_constant)
  391. BUILD_BUG_ON_MSG(1, "unexpected size in kmalloc_index()");
  392. else
  393. BUG();
  394. /* Will never be reached. Needed because the compiler may complain */
  395. return -1;
  396. }
  397. static_assert(PAGE_SHIFT <= 20);
  398. #define kmalloc_index(s) __kmalloc_index(s, true)
  399. #endif /* !CONFIG_SLOB */
  400. void *__kmalloc(size_t size, gfp_t flags) __assume_kmalloc_alignment __alloc_size(1);
  401. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t flags) __assume_slab_alignment __malloc;
  402. void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru,
  403. gfp_t gfpflags) __assume_slab_alignment __malloc;
  404. void kmem_cache_free(struct kmem_cache *s, void *objp);
  405. /*
  406. * Bulk allocation and freeing operations. These are accelerated in an
  407. * allocator specific way to avoid taking locks repeatedly or building
  408. * metadata structures unnecessarily.
  409. *
  410. * Note that interrupts must be enabled when calling these functions.
  411. */
  412. void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p);
  413. int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, void **p);
  414. /*
  415. * Caller must not use kfree_bulk() on memory not originally allocated
  416. * by kmalloc(), because the SLOB allocator cannot handle this.
  417. */
  418. static __always_inline void kfree_bulk(size_t size, void **p)
  419. {
  420. kmem_cache_free_bulk(NULL, size, p);
  421. }
  422. void *__kmalloc_node(size_t size, gfp_t flags, int node) __assume_kmalloc_alignment
  423. __alloc_size(1);
  424. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node) __assume_slab_alignment
  425. __malloc;
  426. void *kmalloc_trace(struct kmem_cache *s, gfp_t flags, size_t size)
  427. __assume_kmalloc_alignment __alloc_size(3);
  428. void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags,
  429. int node, size_t size) __assume_kmalloc_alignment
  430. __alloc_size(4);
  431. void *kmalloc_large(size_t size, gfp_t flags) __assume_page_alignment
  432. __alloc_size(1);
  433. void *kmalloc_large_node(size_t size, gfp_t flags, int node) __assume_page_alignment
  434. __alloc_size(1);
  435. /**
  436. * kmalloc - allocate memory
  437. * @size: how many bytes of memory are required.
  438. * @flags: the type of memory to allocate.
  439. *
  440. * kmalloc is the normal method of allocating memory
  441. * for objects smaller than page size in the kernel.
  442. *
  443. * The allocated object address is aligned to at least ARCH_KMALLOC_MINALIGN
  444. * bytes. For @size of power of two bytes, the alignment is also guaranteed
  445. * to be at least to the size.
  446. *
  447. * The @flags argument may be one of the GFP flags defined at
  448. * include/linux/gfp.h and described at
  449. * :ref:`Documentation/core-api/mm-api.rst <mm-api-gfp-flags>`
  450. *
  451. * The recommended usage of the @flags is described at
  452. * :ref:`Documentation/core-api/memory-allocation.rst <memory_allocation>`
  453. *
  454. * Below is a brief outline of the most useful GFP flags
  455. *
  456. * %GFP_KERNEL
  457. * Allocate normal kernel ram. May sleep.
  458. *
  459. * %GFP_NOWAIT
  460. * Allocation will not sleep.
  461. *
  462. * %GFP_ATOMIC
  463. * Allocation will not sleep. May use emergency pools.
  464. *
  465. * %GFP_HIGHUSER
  466. * Allocate memory from high memory on behalf of user.
  467. *
  468. * Also it is possible to set different flags by OR'ing
  469. * in one or more of the following additional @flags:
  470. *
  471. * %__GFP_HIGH
  472. * This allocation has high priority and may use emergency pools.
  473. *
  474. * %__GFP_NOFAIL
  475. * Indicate that this allocation is in no way allowed to fail
  476. * (think twice before using).
  477. *
  478. * %__GFP_NORETRY
  479. * If memory is not immediately available,
  480. * then give up at once.
  481. *
  482. * %__GFP_NOWARN
  483. * If allocation fails, don't issue any warnings.
  484. *
  485. * %__GFP_RETRY_MAYFAIL
  486. * Try really hard to succeed the allocation but fail
  487. * eventually.
  488. */
  489. static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags)
  490. {
  491. if (__builtin_constant_p(size)) {
  492. #ifndef CONFIG_SLOB
  493. unsigned int index;
  494. #endif
  495. if (size > KMALLOC_MAX_CACHE_SIZE)
  496. return kmalloc_large(size, flags);
  497. #ifndef CONFIG_SLOB
  498. index = kmalloc_index(size);
  499. if (!index)
  500. return ZERO_SIZE_PTR;
  501. return kmalloc_trace(
  502. kmalloc_caches[kmalloc_type(flags)][index],
  503. flags, size);
  504. #endif
  505. }
  506. return __kmalloc(size, flags);
  507. }
  508. #ifndef CONFIG_SLOB
  509. static __always_inline __alloc_size(1) void *kmalloc_node(size_t size, gfp_t flags, int node)
  510. {
  511. if (__builtin_constant_p(size)) {
  512. unsigned int index;
  513. if (size > KMALLOC_MAX_CACHE_SIZE)
  514. return kmalloc_large_node(size, flags, node);
  515. index = kmalloc_index(size);
  516. if (!index)
  517. return ZERO_SIZE_PTR;
  518. return kmalloc_node_trace(
  519. kmalloc_caches[kmalloc_type(flags)][index],
  520. flags, node, size);
  521. }
  522. return __kmalloc_node(size, flags, node);
  523. }
  524. #else
  525. static __always_inline __alloc_size(1) void *kmalloc_node(size_t size, gfp_t flags, int node)
  526. {
  527. if (__builtin_constant_p(size) && size > KMALLOC_MAX_CACHE_SIZE)
  528. return kmalloc_large_node(size, flags, node);
  529. return __kmalloc_node(size, flags, node);
  530. }
  531. #endif
  532. /**
  533. * kmalloc_array - allocate memory for an array.
  534. * @n: number of elements.
  535. * @size: element size.
  536. * @flags: the type of memory to allocate (see kmalloc).
  537. */
  538. static inline __alloc_size(1, 2) void *kmalloc_array(size_t n, size_t size, gfp_t flags)
  539. {
  540. size_t bytes;
  541. if (unlikely(check_mul_overflow(n, size, &bytes)))
  542. return NULL;
  543. if (__builtin_constant_p(n) && __builtin_constant_p(size))
  544. return kmalloc(bytes, flags);
  545. return __kmalloc(bytes, flags);
  546. }
  547. /**
  548. * krealloc_array - reallocate memory for an array.
  549. * @p: pointer to the memory chunk to reallocate
  550. * @new_n: new number of elements to alloc
  551. * @new_size: new size of a single member of the array
  552. * @flags: the type of memory to allocate (see kmalloc)
  553. */
  554. static inline __realloc_size(2, 3) void * __must_check krealloc_array(void *p,
  555. size_t new_n,
  556. size_t new_size,
  557. gfp_t flags)
  558. {
  559. size_t bytes;
  560. if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
  561. return NULL;
  562. return krealloc(p, bytes, flags);
  563. }
  564. /**
  565. * kcalloc - allocate memory for an array. The memory is set to zero.
  566. * @n: number of elements.
  567. * @size: element size.
  568. * @flags: the type of memory to allocate (see kmalloc).
  569. */
  570. static inline __alloc_size(1, 2) void *kcalloc(size_t n, size_t size, gfp_t flags)
  571. {
  572. return kmalloc_array(n, size, flags | __GFP_ZERO);
  573. }
  574. void *__kmalloc_node_track_caller(size_t size, gfp_t flags, int node,
  575. unsigned long caller);
  576. #define kmalloc_node_track_caller(size, flags, node) \
  577. __kmalloc_node_track_caller(size, flags, node, \
  578. _RET_IP_)
  579. /*
  580. * kmalloc_track_caller is a special version of kmalloc that records the
  581. * calling function of the routine calling it for slab leak tracking instead
  582. * of just the calling function (confusing, eh?).
  583. * It's useful when the call to kmalloc comes from a widely-used standard
  584. * allocator where we care about the real place the memory allocation
  585. * request comes from.
  586. */
  587. #define kmalloc_track_caller(size, flags) \
  588. __kmalloc_node_track_caller(size, flags, \
  589. NUMA_NO_NODE, _RET_IP_)
  590. static inline __alloc_size(1, 2) void *kmalloc_array_node(size_t n, size_t size, gfp_t flags,
  591. int node)
  592. {
  593. size_t bytes;
  594. if (unlikely(check_mul_overflow(n, size, &bytes)))
  595. return NULL;
  596. if (__builtin_constant_p(n) && __builtin_constant_p(size))
  597. return kmalloc_node(bytes, flags, node);
  598. return __kmalloc_node(bytes, flags, node);
  599. }
  600. static inline __alloc_size(1, 2) void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node)
  601. {
  602. return kmalloc_array_node(n, size, flags | __GFP_ZERO, node);
  603. }
  604. /*
  605. * Shortcuts
  606. */
  607. static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
  608. {
  609. return kmem_cache_alloc(k, flags | __GFP_ZERO);
  610. }
  611. /**
  612. * kzalloc - allocate memory. The memory is set to zero.
  613. * @size: how many bytes of memory are required.
  614. * @flags: the type of memory to allocate (see kmalloc).
  615. */
  616. static inline __alloc_size(1) void *kzalloc(size_t size, gfp_t flags)
  617. {
  618. return kmalloc(size, flags | __GFP_ZERO);
  619. }
  620. /**
  621. * kzalloc_node - allocate zeroed memory from a particular memory node.
  622. * @size: how many bytes of memory are required.
  623. * @flags: the type of memory to allocate (see kmalloc).
  624. * @node: memory node from which to allocate
  625. */
  626. static inline __alloc_size(1) void *kzalloc_node(size_t size, gfp_t flags, int node)
  627. {
  628. return kmalloc_node(size, flags | __GFP_ZERO, node);
  629. }
  630. extern void *kvmalloc_node(size_t size, gfp_t flags, int node) __alloc_size(1);
  631. static inline __alloc_size(1) void *kvmalloc(size_t size, gfp_t flags)
  632. {
  633. return kvmalloc_node(size, flags, NUMA_NO_NODE);
  634. }
  635. static inline __alloc_size(1) void *kvzalloc_node(size_t size, gfp_t flags, int node)
  636. {
  637. return kvmalloc_node(size, flags | __GFP_ZERO, node);
  638. }
  639. static inline __alloc_size(1) void *kvzalloc(size_t size, gfp_t flags)
  640. {
  641. return kvmalloc(size, flags | __GFP_ZERO);
  642. }
  643. static inline __alloc_size(1, 2) void *kvmalloc_array(size_t n, size_t size, gfp_t flags)
  644. {
  645. size_t bytes;
  646. if (unlikely(check_mul_overflow(n, size, &bytes)))
  647. return NULL;
  648. return kvmalloc(bytes, flags);
  649. }
  650. static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t flags)
  651. {
  652. return kvmalloc_array(n, size, flags | __GFP_ZERO);
  653. }
  654. extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
  655. __realloc_size(3);
  656. extern void kvfree(const void *addr);
  657. extern void kvfree_sensitive(const void *addr, size_t len);
  658. unsigned int kmem_cache_size(struct kmem_cache *s);
  659. /**
  660. * kmalloc_size_roundup - Report allocation bucket size for the given size
  661. *
  662. * @size: Number of bytes to round up from.
  663. *
  664. * This returns the number of bytes that would be available in a kmalloc()
  665. * allocation of @size bytes. For example, a 126 byte request would be
  666. * rounded up to the next sized kmalloc bucket, 128 bytes. (This is strictly
  667. * for the general-purpose kmalloc()-based allocations, and is not for the
  668. * pre-sized kmem_cache_alloc()-based allocations.)
  669. *
  670. * Use this to kmalloc() the full bucket size ahead of time instead of using
  671. * ksize() to query the size after an allocation.
  672. */
  673. size_t kmalloc_size_roundup(size_t size);
  674. void __init kmem_cache_init_late(void);
  675. #if defined(CONFIG_SMP) && defined(CONFIG_SLAB)
  676. int slab_prepare_cpu(unsigned int cpu);
  677. int slab_dead_cpu(unsigned int cpu);
  678. #else
  679. #define slab_prepare_cpu NULL
  680. #define slab_dead_cpu NULL
  681. #endif
  682. #endif /* _LINUX_SLAB_H */