slab.h 19 KB

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