slab.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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_SLAB_DEBUG is set.
  18. */
  19. #define SLAB_DEBUG_FREE 0x00000100UL /* DEBUG: Perform (expensive) checks on 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. /* The following flags affect the page allocator grouping pages by mobility */
  83. #define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* Objects are reclaimable */
  84. #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT /* Objects are short-lived */
  85. /*
  86. * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
  87. *
  88. * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
  89. *
  90. * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
  91. * Both make kfree a no-op.
  92. */
  93. #define ZERO_SIZE_PTR ((void *)16)
  94. #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
  95. (unsigned long)ZERO_SIZE_PTR)
  96. #include <linux/kmemleak.h>
  97. struct mem_cgroup;
  98. /*
  99. * struct kmem_cache related prototypes
  100. */
  101. void __init kmem_cache_init(void);
  102. int slab_is_available(void);
  103. struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
  104. unsigned long,
  105. void (*)(void *));
  106. struct kmem_cache *
  107. kmem_cache_create_memcg(struct mem_cgroup *, const char *, size_t, size_t,
  108. unsigned long, void (*)(void *), struct kmem_cache *);
  109. void kmem_cache_destroy(struct kmem_cache *);
  110. int kmem_cache_shrink(struct kmem_cache *);
  111. void kmem_cache_free(struct kmem_cache *, void *);
  112. /*
  113. * Please use this macro to create slab caches. Simply specify the
  114. * name of the structure and maybe some flags that are listed above.
  115. *
  116. * The alignment of the struct determines object alignment. If you
  117. * f.e. add ____cacheline_aligned_in_smp to the struct declaration
  118. * then the objects will be properly aligned in SMP configurations.
  119. */
  120. #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
  121. sizeof(struct __struct), __alignof__(struct __struct),\
  122. (__flags), NULL)
  123. /*
  124. * Common kmalloc functions provided by all allocators
  125. */
  126. void * __must_check __krealloc(const void *, size_t, gfp_t);
  127. void * __must_check krealloc(const void *, size_t, gfp_t);
  128. void kfree(const void *);
  129. void kzfree(const void *);
  130. size_t ksize(const void *);
  131. /*
  132. * Some archs want to perform DMA into kmalloc caches and need a guaranteed
  133. * alignment larger than the alignment of a 64-bit integer.
  134. * Setting ARCH_KMALLOC_MINALIGN in arch headers allows that.
  135. */
  136. #if defined(ARCH_DMA_MINALIGN) && ARCH_DMA_MINALIGN > 8
  137. #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN
  138. #define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN
  139. #define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN)
  140. #else
  141. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
  142. #endif
  143. #ifdef CONFIG_SLOB
  144. /*
  145. * Common fields provided in kmem_cache by all slab allocators
  146. * This struct is either used directly by the allocator (SLOB)
  147. * or the allocator must include definitions for all fields
  148. * provided in kmem_cache_common in their definition of kmem_cache.
  149. *
  150. * Once we can do anonymous structs (C11 standard) we could put a
  151. * anonymous struct definition in these allocators so that the
  152. * separate allocations in the kmem_cache structure of SLAB and
  153. * SLUB is no longer needed.
  154. */
  155. struct kmem_cache {
  156. unsigned int object_size;/* The original size of the object */
  157. unsigned int size; /* The aligned/padded/added on size */
  158. unsigned int align; /* Alignment as calculated */
  159. unsigned long flags; /* Active flags on the slab */
  160. const char *name; /* Slab name for sysfs */
  161. int refcount; /* Use counter */
  162. void (*ctor)(void *); /* Called on object slot creation */
  163. struct list_head list; /* List of all slab caches on the system */
  164. };
  165. #endif /* CONFIG_SLOB */
  166. /*
  167. * Kmalloc array related definitions
  168. */
  169. #ifdef CONFIG_SLAB
  170. /*
  171. * The largest kmalloc size supported by the SLAB allocators is
  172. * 32 megabyte (2^25) or the maximum allocatable page order if that is
  173. * less than 32 MB.
  174. *
  175. * WARNING: Its not easy to increase this value since the allocators have
  176. * to do various tricks to work around compiler limitations in order to
  177. * ensure proper constant folding.
  178. */
  179. #define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT - 1) <= 25 ? \
  180. (MAX_ORDER + PAGE_SHIFT - 1) : 25)
  181. #define KMALLOC_SHIFT_MAX KMALLOC_SHIFT_HIGH
  182. #ifndef KMALLOC_SHIFT_LOW
  183. #define KMALLOC_SHIFT_LOW 5
  184. #endif
  185. #endif
  186. #ifdef CONFIG_SLUB
  187. /*
  188. * SLUB directly allocates requests fitting in to an order-1 page
  189. * (PAGE_SIZE*2). Larger requests are passed to the page allocator.
  190. */
  191. #define KMALLOC_SHIFT_HIGH (PAGE_SHIFT + 1)
  192. #define KMALLOC_SHIFT_MAX (MAX_ORDER + PAGE_SHIFT)
  193. #ifndef KMALLOC_SHIFT_LOW
  194. #define KMALLOC_SHIFT_LOW 3
  195. #endif
  196. #endif
  197. #ifdef CONFIG_SLOB
  198. /*
  199. * SLOB passes all requests larger than one page to the page allocator.
  200. * No kmalloc array is necessary since objects of different sizes can
  201. * be allocated from the same page.
  202. */
  203. #define KMALLOC_SHIFT_HIGH PAGE_SHIFT
  204. #define KMALLOC_SHIFT_MAX 30
  205. #ifndef KMALLOC_SHIFT_LOW
  206. #define KMALLOC_SHIFT_LOW 3
  207. #endif
  208. #endif
  209. /* Maximum allocatable size */
  210. #define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_MAX)
  211. /* Maximum size for which we actually use a slab cache */
  212. #define KMALLOC_MAX_CACHE_SIZE (1UL << KMALLOC_SHIFT_HIGH)
  213. /* Maximum order allocatable via the slab allocagtor */
  214. #define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_MAX - PAGE_SHIFT)
  215. /*
  216. * Kmalloc subsystem.
  217. */
  218. #ifndef KMALLOC_MIN_SIZE
  219. #define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW)
  220. #endif
  221. /*
  222. * This restriction comes from byte sized index implementation.
  223. * Page size is normally 2^12 bytes and, in this case, if we want to use
  224. * byte sized index which can represent 2^8 entries, the size of the object
  225. * should be equal or greater to 2^12 / 2^8 = 2^4 = 16.
  226. * If minimum size of kmalloc is less than 16, we use it as minimum object
  227. * size and give up to use byte sized index.
  228. */
  229. #define SLAB_OBJ_MIN_SIZE (KMALLOC_MIN_SIZE < 16 ? \
  230. (KMALLOC_MIN_SIZE) : 16)
  231. #ifndef CONFIG_SLOB
  232. extern struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
  233. #ifdef CONFIG_ZONE_DMA
  234. extern struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
  235. #endif
  236. /*
  237. * Figure out which kmalloc slab an allocation of a certain size
  238. * belongs to.
  239. * 0 = zero alloc
  240. * 1 = 65 .. 96 bytes
  241. * 2 = 120 .. 192 bytes
  242. * n = 2^(n-1) .. 2^n -1
  243. */
  244. static __always_inline int kmalloc_index(size_t size)
  245. {
  246. if (!size)
  247. return 0;
  248. if (size <= KMALLOC_MIN_SIZE)
  249. return KMALLOC_SHIFT_LOW;
  250. if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96)
  251. return 1;
  252. if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
  253. return 2;
  254. if (size <= 8) return 3;
  255. if (size <= 16) return 4;
  256. if (size <= 32) return 5;
  257. if (size <= 64) return 6;
  258. if (size <= 128) return 7;
  259. if (size <= 256) return 8;
  260. if (size <= 512) return 9;
  261. if (size <= 1024) return 10;
  262. if (size <= 2 * 1024) return 11;
  263. if (size <= 4 * 1024) return 12;
  264. if (size <= 8 * 1024) return 13;
  265. if (size <= 16 * 1024) return 14;
  266. if (size <= 32 * 1024) return 15;
  267. if (size <= 64 * 1024) return 16;
  268. if (size <= 128 * 1024) return 17;
  269. if (size <= 256 * 1024) return 18;
  270. if (size <= 512 * 1024) return 19;
  271. if (size <= 1024 * 1024) return 20;
  272. if (size <= 2 * 1024 * 1024) return 21;
  273. if (size <= 4 * 1024 * 1024) return 22;
  274. if (size <= 8 * 1024 * 1024) return 23;
  275. if (size <= 16 * 1024 * 1024) return 24;
  276. if (size <= 32 * 1024 * 1024) return 25;
  277. if (size <= 64 * 1024 * 1024) return 26;
  278. BUG();
  279. /* Will never be reached. Needed because the compiler may complain */
  280. return -1;
  281. }
  282. #endif /* !CONFIG_SLOB */
  283. void *__kmalloc(size_t size, gfp_t flags);
  284. void *kmem_cache_alloc(struct kmem_cache *, gfp_t flags);
  285. #ifdef CONFIG_NUMA
  286. void *__kmalloc_node(size_t size, gfp_t flags, int node);
  287. void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  288. #else
  289. static __always_inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
  290. {
  291. return __kmalloc(size, flags);
  292. }
  293. static __always_inline void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node)
  294. {
  295. return kmem_cache_alloc(s, flags);
  296. }
  297. #endif
  298. #ifdef CONFIG_TRACING
  299. extern void *kmem_cache_alloc_trace(struct kmem_cache *, gfp_t, size_t);
  300. #ifdef CONFIG_NUMA
  301. extern void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
  302. gfp_t gfpflags,
  303. int node, size_t size);
  304. #else
  305. static __always_inline void *
  306. kmem_cache_alloc_node_trace(struct kmem_cache *s,
  307. gfp_t gfpflags,
  308. int node, size_t size)
  309. {
  310. return kmem_cache_alloc_trace(s, gfpflags, size);
  311. }
  312. #endif /* CONFIG_NUMA */
  313. #else /* CONFIG_TRACING */
  314. static __always_inline void *kmem_cache_alloc_trace(struct kmem_cache *s,
  315. gfp_t flags, size_t size)
  316. {
  317. return kmem_cache_alloc(s, flags);
  318. }
  319. static __always_inline void *
  320. kmem_cache_alloc_node_trace(struct kmem_cache *s,
  321. gfp_t gfpflags,
  322. int node, size_t size)
  323. {
  324. return kmem_cache_alloc_node(s, gfpflags, node);
  325. }
  326. #endif /* CONFIG_TRACING */
  327. #ifdef CONFIG_SLAB
  328. #include <linux/slab_def.h>
  329. #endif
  330. #ifdef CONFIG_SLUB
  331. #include <linux/slub_def.h>
  332. #endif
  333. static __always_inline void *
  334. kmalloc_order(size_t size, gfp_t flags, unsigned int order)
  335. {
  336. void *ret;
  337. flags |= (__GFP_COMP | __GFP_KMEMCG);
  338. ret = (void *) __get_free_pages(flags, order);
  339. kmemleak_alloc(ret, size, 1, flags);
  340. return ret;
  341. }
  342. #ifdef CONFIG_TRACING
  343. extern void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order);
  344. #else
  345. static __always_inline void *
  346. kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  347. {
  348. return kmalloc_order(size, flags, order);
  349. }
  350. #endif
  351. static __always_inline void *kmalloc_large(size_t size, gfp_t flags)
  352. {
  353. unsigned int order = get_order(size);
  354. return kmalloc_order_trace(size, flags, order);
  355. }
  356. /**
  357. * kmalloc - allocate memory
  358. * @size: how many bytes of memory are required.
  359. * @flags: the type of memory to allocate.
  360. *
  361. * kmalloc is the normal method of allocating memory
  362. * for objects smaller than page size in the kernel.
  363. *
  364. * The @flags argument may be one of:
  365. *
  366. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  367. *
  368. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  369. *
  370. * %GFP_ATOMIC - Allocation will not sleep. May use emergency pools.
  371. * For example, use this inside interrupt handlers.
  372. *
  373. * %GFP_HIGHUSER - Allocate pages from high memory.
  374. *
  375. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  376. *
  377. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  378. *
  379. * %GFP_NOWAIT - Allocation will not sleep.
  380. *
  381. * %GFP_THISNODE - Allocate node-local memory only.
  382. *
  383. * %GFP_DMA - Allocation suitable for DMA.
  384. * Should only be used for kmalloc() caches. Otherwise, use a
  385. * slab created with SLAB_DMA.
  386. *
  387. * Also it is possible to set different flags by OR'ing
  388. * in one or more of the following additional @flags:
  389. *
  390. * %__GFP_COLD - Request cache-cold pages instead of
  391. * trying to return cache-warm pages.
  392. *
  393. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  394. *
  395. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  396. * (think twice before using).
  397. *
  398. * %__GFP_NORETRY - If memory is not immediately available,
  399. * then give up at once.
  400. *
  401. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  402. *
  403. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  404. *
  405. * There are other flags available as well, but these are not intended
  406. * for general use, and so are not documented here. For a full list of
  407. * potential flags, always refer to linux/gfp.h.
  408. */
  409. static __always_inline void *kmalloc(size_t size, gfp_t flags)
  410. {
  411. if (__builtin_constant_p(size)) {
  412. if (size > KMALLOC_MAX_CACHE_SIZE)
  413. return kmalloc_large(size, flags);
  414. #ifndef CONFIG_SLOB
  415. if (!(flags & GFP_DMA)) {
  416. int index = kmalloc_index(size);
  417. if (!index)
  418. return ZERO_SIZE_PTR;
  419. return kmem_cache_alloc_trace(kmalloc_caches[index],
  420. flags, size);
  421. }
  422. #endif
  423. }
  424. return __kmalloc(size, flags);
  425. }
  426. /*
  427. * Determine size used for the nth kmalloc cache.
  428. * return size or 0 if a kmalloc cache for that
  429. * size does not exist
  430. */
  431. static __always_inline int kmalloc_size(int n)
  432. {
  433. #ifndef CONFIG_SLOB
  434. if (n > 2)
  435. return 1 << n;
  436. if (n == 1 && KMALLOC_MIN_SIZE <= 32)
  437. return 96;
  438. if (n == 2 && KMALLOC_MIN_SIZE <= 64)
  439. return 192;
  440. #endif
  441. return 0;
  442. }
  443. static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  444. {
  445. #ifndef CONFIG_SLOB
  446. if (__builtin_constant_p(size) &&
  447. size <= KMALLOC_MAX_CACHE_SIZE && !(flags & GFP_DMA)) {
  448. int i = kmalloc_index(size);
  449. if (!i)
  450. return ZERO_SIZE_PTR;
  451. return kmem_cache_alloc_node_trace(kmalloc_caches[i],
  452. flags, node, size);
  453. }
  454. #endif
  455. return __kmalloc_node(size, flags, node);
  456. }
  457. /*
  458. * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
  459. * Intended for arches that get misalignment faults even for 64 bit integer
  460. * aligned buffers.
  461. */
  462. #ifndef ARCH_SLAB_MINALIGN
  463. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
  464. #endif
  465. /*
  466. * This is the main placeholder for memcg-related information in kmem caches.
  467. * struct kmem_cache will hold a pointer to it, so the memory cost while
  468. * disabled is 1 pointer. The runtime cost while enabled, gets bigger than it
  469. * would otherwise be if that would be bundled in kmem_cache: we'll need an
  470. * extra pointer chase. But the trade off clearly lays in favor of not
  471. * penalizing non-users.
  472. *
  473. * Both the root cache and the child caches will have it. For the root cache,
  474. * this will hold a dynamically allocated array large enough to hold
  475. * information about the currently limited memcgs in the system. To allow the
  476. * array to be accessed without taking any locks, on relocation we free the old
  477. * version only after a grace period.
  478. *
  479. * Child caches will hold extra metadata needed for its operation. Fields are:
  480. *
  481. * @memcg: pointer to the memcg this cache belongs to
  482. * @list: list_head for the list of all caches in this memcg
  483. * @root_cache: pointer to the global, root cache, this cache was derived from
  484. * @dead: set to true after the memcg dies; the cache may still be around.
  485. * @nr_pages: number of pages that belongs to this cache.
  486. * @destroy: worker to be called whenever we are ready, or believe we may be
  487. * ready, to destroy this cache.
  488. */
  489. struct memcg_cache_params {
  490. bool is_root_cache;
  491. union {
  492. struct {
  493. struct rcu_head rcu_head;
  494. struct kmem_cache *memcg_caches[0];
  495. };
  496. struct {
  497. struct mem_cgroup *memcg;
  498. struct list_head list;
  499. struct kmem_cache *root_cache;
  500. bool dead;
  501. atomic_t nr_pages;
  502. struct work_struct destroy;
  503. };
  504. };
  505. };
  506. int memcg_update_all_caches(int num_memcgs);
  507. struct seq_file;
  508. int cache_show(struct kmem_cache *s, struct seq_file *m);
  509. void print_slabinfo_header(struct seq_file *m);
  510. /**
  511. * kmalloc_array - allocate memory for an array.
  512. * @n: number of elements.
  513. * @size: element size.
  514. * @flags: the type of memory to allocate (see kmalloc).
  515. */
  516. static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
  517. {
  518. if (size != 0 && n > SIZE_MAX / size)
  519. return NULL;
  520. return __kmalloc(n * size, flags);
  521. }
  522. /**
  523. * kcalloc - allocate memory for an array. The memory is set to zero.
  524. * @n: number of elements.
  525. * @size: element size.
  526. * @flags: the type of memory to allocate (see kmalloc).
  527. */
  528. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  529. {
  530. return kmalloc_array(n, size, flags | __GFP_ZERO);
  531. }
  532. /*
  533. * kmalloc_track_caller is a special version of kmalloc that records the
  534. * calling function of the routine calling it for slab leak tracking instead
  535. * of just the calling function (confusing, eh?).
  536. * It's useful when the call to kmalloc comes from a widely-used standard
  537. * allocator where we care about the real place the memory allocation
  538. * request comes from.
  539. */
  540. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \
  541. (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \
  542. (defined(CONFIG_SLOB) && defined(CONFIG_TRACING))
  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. #else
  547. #define kmalloc_track_caller(size, flags) \
  548. __kmalloc(size, flags)
  549. #endif /* DEBUG_SLAB */
  550. #ifdef CONFIG_NUMA
  551. /*
  552. * kmalloc_node_track_caller is a special version of kmalloc_node that
  553. * records the calling function of the routine calling it for slab leak
  554. * tracking instead of just the calling function (confusing, eh?).
  555. * It's useful when the call to kmalloc_node comes from a widely-used
  556. * standard allocator where we care about the real place the memory
  557. * allocation request comes from.
  558. */
  559. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \
  560. (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \
  561. (defined(CONFIG_SLOB) && defined(CONFIG_TRACING))
  562. extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long);
  563. #define kmalloc_node_track_caller(size, flags, node) \
  564. __kmalloc_node_track_caller(size, flags, node, \
  565. _RET_IP_)
  566. #else
  567. #define kmalloc_node_track_caller(size, flags, node) \
  568. __kmalloc_node(size, flags, node)
  569. #endif
  570. #else /* CONFIG_NUMA */
  571. #define kmalloc_node_track_caller(size, flags, node) \
  572. kmalloc_track_caller(size, flags)
  573. #endif /* CONFIG_NUMA */
  574. /*
  575. * Shortcuts
  576. */
  577. static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
  578. {
  579. return kmem_cache_alloc(k, flags | __GFP_ZERO);
  580. }
  581. /**
  582. * kzalloc - allocate memory. The memory is set to zero.
  583. * @size: how many bytes of memory are required.
  584. * @flags: the type of memory to allocate (see kmalloc).
  585. */
  586. static inline void *kzalloc(size_t size, gfp_t flags)
  587. {
  588. return kmalloc(size, flags | __GFP_ZERO);
  589. }
  590. /**
  591. * kzalloc_node - allocate zeroed memory from a particular memory node.
  592. * @size: how many bytes of memory are required.
  593. * @flags: the type of memory to allocate (see kmalloc).
  594. * @node: memory node from which to allocate
  595. */
  596. static inline void *kzalloc_node(size_t size, gfp_t flags, int node)
  597. {
  598. return kmalloc_node(size, flags | __GFP_ZERO, node);
  599. }
  600. /*
  601. * Determine the size of a slab object
  602. */
  603. static inline unsigned int kmem_cache_size(struct kmem_cache *s)
  604. {
  605. return s->object_size;
  606. }
  607. void __init kmem_cache_init_late(void);
  608. #endif /* _LINUX_SLAB_H */