slab.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. #ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR
  142. const char *__check_heap_object(const void *ptr, unsigned long n,
  143. struct page *page);
  144. #else
  145. static inline const char *__check_heap_object(const void *ptr,
  146. unsigned long n,
  147. struct page *page)
  148. {
  149. return NULL;
  150. }
  151. #endif
  152. /*
  153. * Some archs want to perform DMA into kmalloc caches and need a guaranteed
  154. * alignment larger than the alignment of a 64-bit integer.
  155. * Setting ARCH_KMALLOC_MINALIGN in arch headers allows that.
  156. */
  157. #if defined(ARCH_DMA_MINALIGN) && ARCH_DMA_MINALIGN > 8
  158. #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN
  159. #define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN
  160. #define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN)
  161. #else
  162. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
  163. #endif
  164. /*
  165. * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
  166. * Intended for arches that get misalignment faults even for 64 bit integer
  167. * aligned buffers.
  168. */
  169. #ifndef ARCH_SLAB_MINALIGN
  170. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
  171. #endif
  172. /*
  173. * kmalloc and friends return ARCH_KMALLOC_MINALIGN aligned
  174. * pointers. kmem_cache_alloc and friends return ARCH_SLAB_MINALIGN
  175. * aligned pointers.
  176. */
  177. #define __assume_kmalloc_alignment __assume_aligned(ARCH_KMALLOC_MINALIGN)
  178. #define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MINALIGN)
  179. #define __assume_page_alignment __assume_aligned(PAGE_SIZE)
  180. /*
  181. * Kmalloc array related definitions
  182. */
  183. #ifdef CONFIG_SLAB
  184. /*
  185. * The largest kmalloc size supported by the SLAB allocators is
  186. * 32 megabyte (2^25) or the maximum allocatable page order if that is
  187. * less than 32 MB.
  188. *
  189. * WARNING: Its not easy to increase this value since the allocators have
  190. * to do various tricks to work around compiler limitations in order to
  191. * ensure proper constant folding.
  192. */
  193. #define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT - 1) <= 25 ? \
  194. (MAX_ORDER + PAGE_SHIFT - 1) : 25)
  195. #define KMALLOC_SHIFT_MAX KMALLOC_SHIFT_HIGH
  196. #ifndef KMALLOC_SHIFT_LOW
  197. #define KMALLOC_SHIFT_LOW 5
  198. #endif
  199. #endif
  200. #ifdef CONFIG_SLUB
  201. /*
  202. * SLUB directly allocates requests fitting in to an order-1 page
  203. * (PAGE_SIZE*2). Larger requests are passed to the page allocator.
  204. */
  205. #define KMALLOC_SHIFT_HIGH (PAGE_SHIFT + 1)
  206. #define KMALLOC_SHIFT_MAX (MAX_ORDER + PAGE_SHIFT - 1)
  207. #ifndef KMALLOC_SHIFT_LOW
  208. #define KMALLOC_SHIFT_LOW 3
  209. #endif
  210. #endif
  211. #ifdef CONFIG_SLOB
  212. /*
  213. * SLOB passes all requests larger than one page to the page allocator.
  214. * No kmalloc array is necessary since objects of different sizes can
  215. * be allocated from the same page.
  216. */
  217. #define KMALLOC_SHIFT_HIGH PAGE_SHIFT
  218. #define KMALLOC_SHIFT_MAX (MAX_ORDER + PAGE_SHIFT - 1)
  219. #ifndef KMALLOC_SHIFT_LOW
  220. #define KMALLOC_SHIFT_LOW 3
  221. #endif
  222. #endif
  223. /* Maximum allocatable size */
  224. #define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_MAX)
  225. /* Maximum size for which we actually use a slab cache */
  226. #define KMALLOC_MAX_CACHE_SIZE (1UL << KMALLOC_SHIFT_HIGH)
  227. /* Maximum order allocatable via the slab allocagtor */
  228. #define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_MAX - PAGE_SHIFT)
  229. /*
  230. * Kmalloc subsystem.
  231. */
  232. #ifndef KMALLOC_MIN_SIZE
  233. #define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW)
  234. #endif
  235. /*
  236. * This restriction comes from byte sized index implementation.
  237. * Page size is normally 2^12 bytes and, in this case, if we want to use
  238. * byte sized index which can represent 2^8 entries, the size of the object
  239. * should be equal or greater to 2^12 / 2^8 = 2^4 = 16.
  240. * If minimum size of kmalloc is less than 16, we use it as minimum object
  241. * size and give up to use byte sized index.
  242. */
  243. #define SLAB_OBJ_MIN_SIZE (KMALLOC_MIN_SIZE < 16 ? \
  244. (KMALLOC_MIN_SIZE) : 16)
  245. #ifndef CONFIG_SLOB
  246. extern struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
  247. #ifdef CONFIG_ZONE_DMA
  248. extern struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
  249. #endif
  250. /*
  251. * Figure out which kmalloc slab an allocation of a certain size
  252. * belongs to.
  253. * 0 = zero alloc
  254. * 1 = 65 .. 96 bytes
  255. * 2 = 129 .. 192 bytes
  256. * n = 2^(n-1)+1 .. 2^n
  257. */
  258. static __always_inline int kmalloc_index(size_t size)
  259. {
  260. if (!size)
  261. return 0;
  262. if (size <= KMALLOC_MIN_SIZE)
  263. return KMALLOC_SHIFT_LOW;
  264. if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96)
  265. return 1;
  266. if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
  267. return 2;
  268. if (size <= 8) return 3;
  269. if (size <= 16) return 4;
  270. if (size <= 32) return 5;
  271. if (size <= 64) return 6;
  272. if (size <= 128) return 7;
  273. if (size <= 256) return 8;
  274. if (size <= 512) return 9;
  275. if (size <= 1024) return 10;
  276. if (size <= 2 * 1024) return 11;
  277. if (size <= 4 * 1024) return 12;
  278. if (size <= 8 * 1024) return 13;
  279. if (size <= 16 * 1024) return 14;
  280. if (size <= 32 * 1024) return 15;
  281. if (size <= 64 * 1024) return 16;
  282. if (size <= 128 * 1024) return 17;
  283. if (size <= 256 * 1024) return 18;
  284. if (size <= 512 * 1024) return 19;
  285. if (size <= 1024 * 1024) return 20;
  286. if (size <= 2 * 1024 * 1024) return 21;
  287. if (size <= 4 * 1024 * 1024) return 22;
  288. if (size <= 8 * 1024 * 1024) return 23;
  289. if (size <= 16 * 1024 * 1024) return 24;
  290. if (size <= 32 * 1024 * 1024) return 25;
  291. if (size <= 64 * 1024 * 1024) return 26;
  292. BUG();
  293. /* Will never be reached. Needed because the compiler may complain */
  294. return -1;
  295. }
  296. #endif /* !CONFIG_SLOB */
  297. void *__kmalloc(size_t size, gfp_t flags) __assume_kmalloc_alignment __malloc;
  298. void *kmem_cache_alloc(struct kmem_cache *, gfp_t flags) __assume_slab_alignment __malloc;
  299. void kmem_cache_free(struct kmem_cache *, void *);
  300. /*
  301. * Bulk allocation and freeing operations. These are accelerated in an
  302. * allocator specific way to avoid taking locks repeatedly or building
  303. * metadata structures unnecessarily.
  304. *
  305. * Note that interrupts must be enabled when calling these functions.
  306. */
  307. void kmem_cache_free_bulk(struct kmem_cache *, size_t, void **);
  308. int kmem_cache_alloc_bulk(struct kmem_cache *, gfp_t, size_t, void **);
  309. /*
  310. * Caller must not use kfree_bulk() on memory not originally allocated
  311. * by kmalloc(), because the SLOB allocator cannot handle this.
  312. */
  313. static __always_inline void kfree_bulk(size_t size, void **p)
  314. {
  315. kmem_cache_free_bulk(NULL, size, p);
  316. }
  317. #ifdef CONFIG_NUMA
  318. void *__kmalloc_node(size_t size, gfp_t flags, int node) __assume_kmalloc_alignment __malloc;
  319. void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node) __assume_slab_alignment __malloc;
  320. #else
  321. static __always_inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
  322. {
  323. return __kmalloc(size, flags);
  324. }
  325. static __always_inline void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node)
  326. {
  327. return kmem_cache_alloc(s, flags);
  328. }
  329. #endif
  330. #ifdef CONFIG_TRACING
  331. extern void *kmem_cache_alloc_trace(struct kmem_cache *, gfp_t, size_t) __assume_slab_alignment __malloc;
  332. #ifdef CONFIG_NUMA
  333. extern void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
  334. gfp_t gfpflags,
  335. int node, size_t size) __assume_slab_alignment __malloc;
  336. #else
  337. static __always_inline void *
  338. kmem_cache_alloc_node_trace(struct kmem_cache *s,
  339. gfp_t gfpflags,
  340. int node, size_t size)
  341. {
  342. return kmem_cache_alloc_trace(s, gfpflags, size);
  343. }
  344. #endif /* CONFIG_NUMA */
  345. #else /* CONFIG_TRACING */
  346. static __always_inline void *kmem_cache_alloc_trace(struct kmem_cache *s,
  347. gfp_t flags, size_t size)
  348. {
  349. void *ret = kmem_cache_alloc(s, flags);
  350. kasan_kmalloc(s, ret, size, flags);
  351. return ret;
  352. }
  353. static __always_inline void *
  354. kmem_cache_alloc_node_trace(struct kmem_cache *s,
  355. gfp_t gfpflags,
  356. int node, size_t size)
  357. {
  358. void *ret = kmem_cache_alloc_node(s, gfpflags, node);
  359. kasan_kmalloc(s, ret, size, gfpflags);
  360. return ret;
  361. }
  362. #endif /* CONFIG_TRACING */
  363. extern void *kmalloc_order(size_t size, gfp_t flags, unsigned int order) __assume_page_alignment __malloc;
  364. #ifdef CONFIG_TRACING
  365. extern void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order) __assume_page_alignment __malloc;
  366. #else
  367. static __always_inline void *
  368. kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  369. {
  370. return kmalloc_order(size, flags, order);
  371. }
  372. #endif
  373. static __always_inline void *kmalloc_large(size_t size, gfp_t flags)
  374. {
  375. unsigned int order = get_order(size);
  376. return kmalloc_order_trace(size, flags, order);
  377. }
  378. /**
  379. * kmalloc - allocate memory
  380. * @size: how many bytes of memory are required.
  381. * @flags: the type of memory to allocate.
  382. *
  383. * kmalloc is the normal method of allocating memory
  384. * for objects smaller than page size in the kernel.
  385. *
  386. * The @flags argument may be one of:
  387. *
  388. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  389. *
  390. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  391. *
  392. * %GFP_ATOMIC - Allocation will not sleep. May use emergency pools.
  393. * For example, use this inside interrupt handlers.
  394. *
  395. * %GFP_HIGHUSER - Allocate pages from high memory.
  396. *
  397. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  398. *
  399. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  400. *
  401. * %GFP_NOWAIT - Allocation will not sleep.
  402. *
  403. * %__GFP_THISNODE - Allocate node-local memory only.
  404. *
  405. * %GFP_DMA - Allocation suitable for DMA.
  406. * Should only be used for kmalloc() caches. Otherwise, use a
  407. * slab created with SLAB_DMA.
  408. *
  409. * Also it is possible to set different flags by OR'ing
  410. * in one or more of the following additional @flags:
  411. *
  412. * %__GFP_COLD - Request cache-cold pages instead of
  413. * trying to return cache-warm pages.
  414. *
  415. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  416. *
  417. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  418. * (think twice before using).
  419. *
  420. * %__GFP_NORETRY - If memory is not immediately available,
  421. * then give up at once.
  422. *
  423. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  424. *
  425. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  426. *
  427. * There are other flags available as well, but these are not intended
  428. * for general use, and so are not documented here. For a full list of
  429. * potential flags, always refer to linux/gfp.h.
  430. */
  431. static __always_inline void *kmalloc(size_t size, gfp_t flags)
  432. {
  433. if (__builtin_constant_p(size)) {
  434. if (size > KMALLOC_MAX_CACHE_SIZE)
  435. return kmalloc_large(size, flags);
  436. #ifndef CONFIG_SLOB
  437. if (!(flags & GFP_DMA)) {
  438. int index = kmalloc_index(size);
  439. if (!index)
  440. return ZERO_SIZE_PTR;
  441. return kmem_cache_alloc_trace(kmalloc_caches[index],
  442. flags, size);
  443. }
  444. #endif
  445. }
  446. return __kmalloc(size, flags);
  447. }
  448. /*
  449. * Determine size used for the nth kmalloc cache.
  450. * return size or 0 if a kmalloc cache for that
  451. * size does not exist
  452. */
  453. static __always_inline int kmalloc_size(int n)
  454. {
  455. #ifndef CONFIG_SLOB
  456. if (n > 2)
  457. return 1 << n;
  458. if (n == 1 && KMALLOC_MIN_SIZE <= 32)
  459. return 96;
  460. if (n == 2 && KMALLOC_MIN_SIZE <= 64)
  461. return 192;
  462. #endif
  463. return 0;
  464. }
  465. static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  466. {
  467. #ifndef CONFIG_SLOB
  468. if (__builtin_constant_p(size) &&
  469. size <= KMALLOC_MAX_CACHE_SIZE && !(flags & GFP_DMA)) {
  470. int i = kmalloc_index(size);
  471. if (!i)
  472. return ZERO_SIZE_PTR;
  473. return kmem_cache_alloc_node_trace(kmalloc_caches[i],
  474. flags, node, size);
  475. }
  476. #endif
  477. return __kmalloc_node(size, flags, node);
  478. }
  479. struct memcg_cache_array {
  480. struct rcu_head rcu;
  481. struct kmem_cache *entries[0];
  482. };
  483. /*
  484. * This is the main placeholder for memcg-related information in kmem caches.
  485. * Both the root cache and the child caches will have it. For the root cache,
  486. * this will hold a dynamically allocated array large enough to hold
  487. * information about the currently limited memcgs in the system. To allow the
  488. * array to be accessed without taking any locks, on relocation we free the old
  489. * version only after a grace period.
  490. *
  491. * Child caches will hold extra metadata needed for its operation. Fields are:
  492. *
  493. * @memcg: pointer to the memcg this cache belongs to
  494. * @root_cache: pointer to the global, root cache, this cache was derived from
  495. *
  496. * Both root and child caches of the same kind are linked into a list chained
  497. * through @list.
  498. */
  499. struct memcg_cache_params {
  500. bool is_root_cache;
  501. struct list_head list;
  502. union {
  503. struct memcg_cache_array __rcu *memcg_caches;
  504. struct {
  505. struct mem_cgroup *memcg;
  506. struct kmem_cache *root_cache;
  507. };
  508. };
  509. };
  510. int memcg_update_all_caches(int num_memcgs);
  511. /**
  512. * kmalloc_array - allocate memory for an array.
  513. * @n: number of elements.
  514. * @size: element size.
  515. * @flags: the type of memory to allocate (see kmalloc).
  516. */
  517. static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
  518. {
  519. if (size != 0 && n > SIZE_MAX / size)
  520. return NULL;
  521. if (__builtin_constant_p(n) && __builtin_constant_p(size))
  522. return kmalloc(n * size, flags);
  523. return __kmalloc(n * size, flags);
  524. }
  525. /**
  526. * kcalloc - allocate memory for an array. The memory is set to zero.
  527. * @n: number of elements.
  528. * @size: element size.
  529. * @flags: the type of memory to allocate (see kmalloc).
  530. */
  531. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  532. {
  533. return kmalloc_array(n, size, flags | __GFP_ZERO);
  534. }
  535. /*
  536. * kmalloc_track_caller is a special version of kmalloc that records the
  537. * calling function of the routine calling it for slab leak tracking instead
  538. * of just the calling function (confusing, eh?).
  539. * It's useful when the call to kmalloc comes from a widely-used standard
  540. * allocator where we care about the real place the memory allocation
  541. * request comes from.
  542. */
  543. extern void *__kmalloc_track_caller(size_t, gfp_t, unsigned long);
  544. #define kmalloc_track_caller(size, flags) \
  545. __kmalloc_track_caller(size, flags, _RET_IP_)
  546. #ifdef CONFIG_NUMA
  547. extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long);
  548. #define kmalloc_node_track_caller(size, flags, node) \
  549. __kmalloc_node_track_caller(size, flags, node, \
  550. _RET_IP_)
  551. #else /* CONFIG_NUMA */
  552. #define kmalloc_node_track_caller(size, flags, node) \
  553. kmalloc_track_caller(size, flags)
  554. #endif /* CONFIG_NUMA */
  555. /*
  556. * Shortcuts
  557. */
  558. static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
  559. {
  560. return kmem_cache_alloc(k, flags | __GFP_ZERO);
  561. }
  562. /**
  563. * kzalloc - allocate memory. The memory is set to zero.
  564. * @size: how many bytes of memory are required.
  565. * @flags: the type of memory to allocate (see kmalloc).
  566. */
  567. static inline void *kzalloc(size_t size, gfp_t flags)
  568. {
  569. return kmalloc(size, flags | __GFP_ZERO);
  570. }
  571. /**
  572. * kzalloc_node - allocate zeroed memory from a particular memory node.
  573. * @size: how many bytes of memory are required.
  574. * @flags: the type of memory to allocate (see kmalloc).
  575. * @node: memory node from which to allocate
  576. */
  577. static inline void *kzalloc_node(size_t size, gfp_t flags, int node)
  578. {
  579. return kmalloc_node(size, flags | __GFP_ZERO, node);
  580. }
  581. unsigned int kmem_cache_size(struct kmem_cache *s);
  582. void __init kmem_cache_init_late(void);
  583. #if defined(CONFIG_SMP) && defined(CONFIG_SLAB)
  584. int slab_prepare_cpu(unsigned int cpu);
  585. int slab_dead_cpu(unsigned int cpu);
  586. #else
  587. #define slab_prepare_cpu NULL
  588. #define slab_dead_cpu NULL
  589. #endif
  590. #endif /* _LINUX_SLAB_H */