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