slab.h 22 KB

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