slab.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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 allocates up to order 2 pages directly and otherwise
  189. * passes the request 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 page size and larger requests 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_MAX 30
  204. #define KMALLOC_SHIFT_HIGH PAGE_SHIFT
  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. #ifndef CONFIG_SLOB
  222. extern struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
  223. #ifdef CONFIG_ZONE_DMA
  224. extern struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
  225. #endif
  226. /*
  227. * Figure out which kmalloc slab an allocation of a certain size
  228. * belongs to.
  229. * 0 = zero alloc
  230. * 1 = 65 .. 96 bytes
  231. * 2 = 120 .. 192 bytes
  232. * n = 2^(n-1) .. 2^n -1
  233. */
  234. static __always_inline int kmalloc_index(size_t size)
  235. {
  236. if (!size)
  237. return 0;
  238. if (size <= KMALLOC_MIN_SIZE)
  239. return KMALLOC_SHIFT_LOW;
  240. if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96)
  241. return 1;
  242. if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
  243. return 2;
  244. if (size <= 8) return 3;
  245. if (size <= 16) return 4;
  246. if (size <= 32) return 5;
  247. if (size <= 64) return 6;
  248. if (size <= 128) return 7;
  249. if (size <= 256) return 8;
  250. if (size <= 512) return 9;
  251. if (size <= 1024) return 10;
  252. if (size <= 2 * 1024) return 11;
  253. if (size <= 4 * 1024) return 12;
  254. if (size <= 8 * 1024) return 13;
  255. if (size <= 16 * 1024) return 14;
  256. if (size <= 32 * 1024) return 15;
  257. if (size <= 64 * 1024) return 16;
  258. if (size <= 128 * 1024) return 17;
  259. if (size <= 256 * 1024) return 18;
  260. if (size <= 512 * 1024) return 19;
  261. if (size <= 1024 * 1024) return 20;
  262. if (size <= 2 * 1024 * 1024) return 21;
  263. if (size <= 4 * 1024 * 1024) return 22;
  264. if (size <= 8 * 1024 * 1024) return 23;
  265. if (size <= 16 * 1024 * 1024) return 24;
  266. if (size <= 32 * 1024 * 1024) return 25;
  267. if (size <= 64 * 1024 * 1024) return 26;
  268. BUG();
  269. /* Will never be reached. Needed because the compiler may complain */
  270. return -1;
  271. }
  272. #endif /* !CONFIG_SLOB */
  273. void *__kmalloc(size_t size, gfp_t flags);
  274. void *kmem_cache_alloc(struct kmem_cache *, gfp_t flags);
  275. #ifdef CONFIG_NUMA
  276. void *__kmalloc_node(size_t size, gfp_t flags, int node);
  277. void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  278. #else
  279. static __always_inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
  280. {
  281. return __kmalloc(size, flags);
  282. }
  283. static __always_inline void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node)
  284. {
  285. return kmem_cache_alloc(s, flags);
  286. }
  287. #endif
  288. #ifdef CONFIG_TRACING
  289. extern void *kmem_cache_alloc_trace(struct kmem_cache *, gfp_t, size_t);
  290. #ifdef CONFIG_NUMA
  291. extern void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
  292. gfp_t gfpflags,
  293. int node, size_t size);
  294. #else
  295. static __always_inline void *
  296. kmem_cache_alloc_node_trace(struct kmem_cache *s,
  297. gfp_t gfpflags,
  298. int node, size_t size)
  299. {
  300. return kmem_cache_alloc_trace(s, gfpflags, size);
  301. }
  302. #endif /* CONFIG_NUMA */
  303. #else /* CONFIG_TRACING */
  304. static __always_inline void *kmem_cache_alloc_trace(struct kmem_cache *s,
  305. gfp_t flags, size_t size)
  306. {
  307. return kmem_cache_alloc(s, flags);
  308. }
  309. static __always_inline void *
  310. kmem_cache_alloc_node_trace(struct kmem_cache *s,
  311. gfp_t gfpflags,
  312. int node, size_t size)
  313. {
  314. return kmem_cache_alloc_node(s, gfpflags, node);
  315. }
  316. #endif /* CONFIG_TRACING */
  317. #ifdef CONFIG_SLAB
  318. #include <linux/slab_def.h>
  319. #endif
  320. #ifdef CONFIG_SLUB
  321. #include <linux/slub_def.h>
  322. #endif
  323. static __always_inline void *
  324. kmalloc_order(size_t size, gfp_t flags, unsigned int order)
  325. {
  326. void *ret;
  327. flags |= (__GFP_COMP | __GFP_KMEMCG);
  328. ret = (void *) __get_free_pages(flags, order);
  329. kmemleak_alloc(ret, size, 1, flags);
  330. return ret;
  331. }
  332. #ifdef CONFIG_TRACING
  333. extern void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order);
  334. #else
  335. static __always_inline void *
  336. kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  337. {
  338. return kmalloc_order(size, flags, order);
  339. }
  340. #endif
  341. static __always_inline void *kmalloc_large(size_t size, gfp_t flags)
  342. {
  343. unsigned int order = get_order(size);
  344. return kmalloc_order_trace(size, flags, order);
  345. }
  346. /**
  347. * kmalloc - allocate memory
  348. * @size: how many bytes of memory are required.
  349. * @flags: the type of memory to allocate (see kcalloc).
  350. *
  351. * kmalloc is the normal method of allocating memory
  352. * for objects smaller than page size in the kernel.
  353. */
  354. static __always_inline void *kmalloc(size_t size, gfp_t flags)
  355. {
  356. if (__builtin_constant_p(size)) {
  357. if (size > KMALLOC_MAX_CACHE_SIZE)
  358. return kmalloc_large(size, flags);
  359. #ifndef CONFIG_SLOB
  360. if (!(flags & GFP_DMA)) {
  361. int index = kmalloc_index(size);
  362. if (!index)
  363. return ZERO_SIZE_PTR;
  364. return kmem_cache_alloc_trace(kmalloc_caches[index],
  365. flags, size);
  366. }
  367. #endif
  368. }
  369. return __kmalloc(size, flags);
  370. }
  371. /*
  372. * Determine size used for the nth kmalloc cache.
  373. * return size or 0 if a kmalloc cache for that
  374. * size does not exist
  375. */
  376. static __always_inline int kmalloc_size(int n)
  377. {
  378. #ifndef CONFIG_SLOB
  379. if (n > 2)
  380. return 1 << n;
  381. if (n == 1 && KMALLOC_MIN_SIZE <= 32)
  382. return 96;
  383. if (n == 2 && KMALLOC_MIN_SIZE <= 64)
  384. return 192;
  385. #endif
  386. return 0;
  387. }
  388. static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  389. {
  390. #ifndef CONFIG_SLOB
  391. if (__builtin_constant_p(size) &&
  392. size <= KMALLOC_MAX_CACHE_SIZE && !(flags & GFP_DMA)) {
  393. int i = kmalloc_index(size);
  394. if (!i)
  395. return ZERO_SIZE_PTR;
  396. return kmem_cache_alloc_node_trace(kmalloc_caches[i],
  397. flags, node, size);
  398. }
  399. #endif
  400. return __kmalloc_node(size, flags, node);
  401. }
  402. /*
  403. * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
  404. * Intended for arches that get misalignment faults even for 64 bit integer
  405. * aligned buffers.
  406. */
  407. #ifndef ARCH_SLAB_MINALIGN
  408. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
  409. #endif
  410. /*
  411. * This is the main placeholder for memcg-related information in kmem caches.
  412. * struct kmem_cache will hold a pointer to it, so the memory cost while
  413. * disabled is 1 pointer. The runtime cost while enabled, gets bigger than it
  414. * would otherwise be if that would be bundled in kmem_cache: we'll need an
  415. * extra pointer chase. But the trade off clearly lays in favor of not
  416. * penalizing non-users.
  417. *
  418. * Both the root cache and the child caches will have it. For the root cache,
  419. * this will hold a dynamically allocated array large enough to hold
  420. * information about the currently limited memcgs in the system.
  421. *
  422. * Child caches will hold extra metadata needed for its operation. Fields are:
  423. *
  424. * @memcg: pointer to the memcg this cache belongs to
  425. * @list: list_head for the list of all caches in this memcg
  426. * @root_cache: pointer to the global, root cache, this cache was derived from
  427. * @dead: set to true after the memcg dies; the cache may still be around.
  428. * @nr_pages: number of pages that belongs to this cache.
  429. * @destroy: worker to be called whenever we are ready, or believe we may be
  430. * ready, to destroy this cache.
  431. */
  432. struct memcg_cache_params {
  433. bool is_root_cache;
  434. union {
  435. struct kmem_cache *memcg_caches[0];
  436. struct {
  437. struct mem_cgroup *memcg;
  438. struct list_head list;
  439. struct kmem_cache *root_cache;
  440. bool dead;
  441. atomic_t nr_pages;
  442. struct work_struct destroy;
  443. };
  444. };
  445. };
  446. int memcg_update_all_caches(int num_memcgs);
  447. struct seq_file;
  448. int cache_show(struct kmem_cache *s, struct seq_file *m);
  449. void print_slabinfo_header(struct seq_file *m);
  450. /**
  451. * kmalloc - allocate memory
  452. * @size: how many bytes of memory are required.
  453. * @flags: the type of memory to allocate.
  454. *
  455. * The @flags argument may be one of:
  456. *
  457. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  458. *
  459. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  460. *
  461. * %GFP_ATOMIC - Allocation will not sleep. May use emergency pools.
  462. * For example, use this inside interrupt handlers.
  463. *
  464. * %GFP_HIGHUSER - Allocate pages from high memory.
  465. *
  466. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  467. *
  468. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  469. *
  470. * %GFP_NOWAIT - Allocation will not sleep.
  471. *
  472. * %GFP_THISNODE - Allocate node-local memory only.
  473. *
  474. * %GFP_DMA - Allocation suitable for DMA.
  475. * Should only be used for kmalloc() caches. Otherwise, use a
  476. * slab created with SLAB_DMA.
  477. *
  478. * Also it is possible to set different flags by OR'ing
  479. * in one or more of the following additional @flags:
  480. *
  481. * %__GFP_COLD - Request cache-cold pages instead of
  482. * trying to return cache-warm pages.
  483. *
  484. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  485. *
  486. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  487. * (think twice before using).
  488. *
  489. * %__GFP_NORETRY - If memory is not immediately available,
  490. * then give up at once.
  491. *
  492. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  493. *
  494. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  495. *
  496. * There are other flags available as well, but these are not intended
  497. * for general use, and so are not documented here. For a full list of
  498. * potential flags, always refer to linux/gfp.h.
  499. *
  500. * kmalloc is the normal method of allocating memory
  501. * in the kernel.
  502. */
  503. static __always_inline void *kmalloc(size_t size, gfp_t flags);
  504. /**
  505. * kmalloc_array - allocate memory for an array.
  506. * @n: number of elements.
  507. * @size: element size.
  508. * @flags: the type of memory to allocate (see kmalloc).
  509. */
  510. static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
  511. {
  512. if (size != 0 && n > SIZE_MAX / size)
  513. return NULL;
  514. return __kmalloc(n * size, flags);
  515. }
  516. /**
  517. * kcalloc - allocate memory for an array. The memory is set to zero.
  518. * @n: number of elements.
  519. * @size: element size.
  520. * @flags: the type of memory to allocate (see kmalloc).
  521. */
  522. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  523. {
  524. return kmalloc_array(n, size, flags | __GFP_ZERO);
  525. }
  526. /*
  527. * kmalloc_track_caller is a special version of kmalloc that records the
  528. * calling function of the routine calling it for slab leak tracking instead
  529. * of just the calling function (confusing, eh?).
  530. * It's useful when the call to kmalloc comes from a widely-used standard
  531. * allocator where we care about the real place the memory allocation
  532. * request comes from.
  533. */
  534. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \
  535. (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \
  536. (defined(CONFIG_SLOB) && defined(CONFIG_TRACING))
  537. extern void *__kmalloc_track_caller(size_t, gfp_t, unsigned long);
  538. #define kmalloc_track_caller(size, flags) \
  539. __kmalloc_track_caller(size, flags, _RET_IP_)
  540. #else
  541. #define kmalloc_track_caller(size, flags) \
  542. __kmalloc(size, flags)
  543. #endif /* DEBUG_SLAB */
  544. #ifdef CONFIG_NUMA
  545. /*
  546. * kmalloc_node_track_caller is a special version of kmalloc_node that
  547. * records the calling function of the routine calling it for slab leak
  548. * tracking instead of just the calling function (confusing, eh?).
  549. * It's useful when the call to kmalloc_node comes from a widely-used
  550. * standard allocator where we care about the real place the memory
  551. * allocation request comes from.
  552. */
  553. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \
  554. (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \
  555. (defined(CONFIG_SLOB) && defined(CONFIG_TRACING))
  556. extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long);
  557. #define kmalloc_node_track_caller(size, flags, node) \
  558. __kmalloc_node_track_caller(size, flags, node, \
  559. _RET_IP_)
  560. #else
  561. #define kmalloc_node_track_caller(size, flags, node) \
  562. __kmalloc_node(size, flags, node)
  563. #endif
  564. #else /* CONFIG_NUMA */
  565. #define kmalloc_node_track_caller(size, flags, node) \
  566. kmalloc_track_caller(size, flags)
  567. #endif /* CONFIG_NUMA */
  568. /*
  569. * Shortcuts
  570. */
  571. static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
  572. {
  573. return kmem_cache_alloc(k, flags | __GFP_ZERO);
  574. }
  575. /**
  576. * kzalloc - allocate memory. The memory is set to zero.
  577. * @size: how many bytes of memory are required.
  578. * @flags: the type of memory to allocate (see kmalloc).
  579. */
  580. static inline void *kzalloc(size_t size, gfp_t flags)
  581. {
  582. return kmalloc(size, flags | __GFP_ZERO);
  583. }
  584. /**
  585. * kzalloc_node - allocate zeroed memory from a particular memory node.
  586. * @size: how many bytes of memory are required.
  587. * @flags: the type of memory to allocate (see kmalloc).
  588. * @node: memory node from which to allocate
  589. */
  590. static inline void *kzalloc_node(size_t size, gfp_t flags, int node)
  591. {
  592. return kmalloc_node(size, flags | __GFP_ZERO, node);
  593. }
  594. /*
  595. * Determine the size of a slab object
  596. */
  597. static inline unsigned int kmem_cache_size(struct kmem_cache *s)
  598. {
  599. return s->object_size;
  600. }
  601. void __init kmem_cache_init_late(void);
  602. #endif /* _LINUX_SLAB_H */