slab.h 17 KB

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