slab.h 22 KB

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