slab.h 20 KB

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