slab.h 21 KB

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