slab.h 22 KB

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