slab.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. #ifdef CONFIG_SLOB
  122. /*
  123. * Common fields provided in kmem_cache by all slab allocators
  124. * This struct is either used directly by the allocator (SLOB)
  125. * or the allocator must include definitions for all fields
  126. * provided in kmem_cache_common in their definition of kmem_cache.
  127. *
  128. * Once we can do anonymous structs (C11 standard) we could put a
  129. * anonymous struct definition in these allocators so that the
  130. * separate allocations in the kmem_cache structure of SLAB and
  131. * SLUB is no longer needed.
  132. */
  133. struct kmem_cache {
  134. unsigned int object_size;/* The original size of the object */
  135. unsigned int size; /* The aligned/padded/added on size */
  136. unsigned int align; /* Alignment as calculated */
  137. unsigned long flags; /* Active flags on the slab */
  138. const char *name; /* Slab name for sysfs */
  139. int refcount; /* Use counter */
  140. void (*ctor)(void *); /* Called on object slot creation */
  141. struct list_head list; /* List of all slab caches on the system */
  142. };
  143. #define KMALLOC_MAX_SIZE (1UL << 30)
  144. #include <linux/slob_def.h>
  145. #else /* CONFIG_SLOB */
  146. /*
  147. * Kmalloc array related definitions
  148. */
  149. #ifdef CONFIG_SLAB
  150. /*
  151. * The largest kmalloc size supported by the SLAB allocators is
  152. * 32 megabyte (2^25) or the maximum allocatable page order if that is
  153. * less than 32 MB.
  154. *
  155. * WARNING: Its not easy to increase this value since the allocators have
  156. * to do various tricks to work around compiler limitations in order to
  157. * ensure proper constant folding.
  158. */
  159. #define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT - 1) <= 25 ? \
  160. (MAX_ORDER + PAGE_SHIFT - 1) : 25)
  161. #define KMALLOC_SHIFT_MAX KMALLOC_SHIFT_HIGH
  162. #define KMALLOC_SHIFT_LOW 5
  163. #else
  164. /*
  165. * SLUB allocates up to order 2 pages directly and otherwise
  166. * passes the request to the page allocator.
  167. */
  168. #define KMALLOC_SHIFT_HIGH (PAGE_SHIFT + 1)
  169. #define KMALLOC_SHIFT_MAX (MAX_ORDER + PAGE_SHIFT)
  170. #define KMALLOC_SHIFT_LOW 3
  171. #endif
  172. /* Maximum allocatable size */
  173. #define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_MAX)
  174. /* Maximum size for which we actually use a slab cache */
  175. #define KMALLOC_MAX_CACHE_SIZE (1UL << KMALLOC_SHIFT_HIGH)
  176. /* Maximum order allocatable via the slab allocagtor */
  177. #define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_MAX - PAGE_SHIFT)
  178. /*
  179. * Kmalloc subsystem.
  180. */
  181. #if defined(ARCH_DMA_MINALIGN) && ARCH_DMA_MINALIGN > 8
  182. #define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN
  183. #else
  184. #define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW)
  185. #endif
  186. /*
  187. * Figure out which kmalloc slab an allocation of a certain size
  188. * belongs to.
  189. * 0 = zero alloc
  190. * 1 = 65 .. 96 bytes
  191. * 2 = 120 .. 192 bytes
  192. * n = 2^(n-1) .. 2^n -1
  193. */
  194. static __always_inline int kmalloc_index(size_t size)
  195. {
  196. if (!size)
  197. return 0;
  198. if (size <= KMALLOC_MIN_SIZE)
  199. return KMALLOC_SHIFT_LOW;
  200. if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96)
  201. return 1;
  202. if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
  203. return 2;
  204. if (size <= 8) return 3;
  205. if (size <= 16) return 4;
  206. if (size <= 32) return 5;
  207. if (size <= 64) return 6;
  208. if (size <= 128) return 7;
  209. if (size <= 256) return 8;
  210. if (size <= 512) return 9;
  211. if (size <= 1024) return 10;
  212. if (size <= 2 * 1024) return 11;
  213. if (size <= 4 * 1024) return 12;
  214. if (size <= 8 * 1024) return 13;
  215. if (size <= 16 * 1024) return 14;
  216. if (size <= 32 * 1024) return 15;
  217. if (size <= 64 * 1024) return 16;
  218. if (size <= 128 * 1024) return 17;
  219. if (size <= 256 * 1024) return 18;
  220. if (size <= 512 * 1024) return 19;
  221. if (size <= 1024 * 1024) return 20;
  222. if (size <= 2 * 1024 * 1024) return 21;
  223. if (size <= 4 * 1024 * 1024) return 22;
  224. if (size <= 8 * 1024 * 1024) return 23;
  225. if (size <= 16 * 1024 * 1024) return 24;
  226. if (size <= 32 * 1024 * 1024) return 25;
  227. if (size <= 64 * 1024 * 1024) return 26;
  228. BUG();
  229. /* Will never be reached. Needed because the compiler may complain */
  230. return -1;
  231. }
  232. #ifdef CONFIG_SLAB
  233. #include <linux/slab_def.h>
  234. #elif defined(CONFIG_SLUB)
  235. #include <linux/slub_def.h>
  236. #else
  237. #error "Unknown slab allocator"
  238. #endif
  239. /*
  240. * Determine size used for the nth kmalloc cache.
  241. * return size or 0 if a kmalloc cache for that
  242. * size does not exist
  243. */
  244. static __always_inline int kmalloc_size(int n)
  245. {
  246. if (n > 2)
  247. return 1 << n;
  248. if (n == 1 && KMALLOC_MIN_SIZE <= 32)
  249. return 96;
  250. if (n == 2 && KMALLOC_MIN_SIZE <= 64)
  251. return 192;
  252. return 0;
  253. }
  254. #endif /* !CONFIG_SLOB */
  255. /*
  256. * Some archs want to perform DMA into kmalloc caches and need a guaranteed
  257. * alignment larger than the alignment of a 64-bit integer.
  258. * Setting ARCH_KMALLOC_MINALIGN in arch headers allows that.
  259. */
  260. #ifdef ARCH_DMA_MINALIGN
  261. #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN
  262. #else
  263. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
  264. #endif
  265. /*
  266. * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
  267. * Intended for arches that get misalignment faults even for 64 bit integer
  268. * aligned buffers.
  269. */
  270. #ifndef ARCH_SLAB_MINALIGN
  271. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
  272. #endif
  273. /*
  274. * This is the main placeholder for memcg-related information in kmem caches.
  275. * struct kmem_cache will hold a pointer to it, so the memory cost while
  276. * disabled is 1 pointer. The runtime cost while enabled, gets bigger than it
  277. * would otherwise be if that would be bundled in kmem_cache: we'll need an
  278. * extra pointer chase. But the trade off clearly lays in favor of not
  279. * penalizing non-users.
  280. *
  281. * Both the root cache and the child caches will have it. For the root cache,
  282. * this will hold a dynamically allocated array large enough to hold
  283. * information about the currently limited memcgs in the system.
  284. *
  285. * Child caches will hold extra metadata needed for its operation. Fields are:
  286. *
  287. * @memcg: pointer to the memcg this cache belongs to
  288. * @list: list_head for the list of all caches in this memcg
  289. * @root_cache: pointer to the global, root cache, this cache was derived from
  290. * @dead: set to true after the memcg dies; the cache may still be around.
  291. * @nr_pages: number of pages that belongs to this cache.
  292. * @destroy: worker to be called whenever we are ready, or believe we may be
  293. * ready, to destroy this cache.
  294. */
  295. struct memcg_cache_params {
  296. bool is_root_cache;
  297. union {
  298. struct kmem_cache *memcg_caches[0];
  299. struct {
  300. struct mem_cgroup *memcg;
  301. struct list_head list;
  302. struct kmem_cache *root_cache;
  303. bool dead;
  304. atomic_t nr_pages;
  305. struct work_struct destroy;
  306. };
  307. };
  308. };
  309. int memcg_update_all_caches(int num_memcgs);
  310. struct seq_file;
  311. int cache_show(struct kmem_cache *s, struct seq_file *m);
  312. void print_slabinfo_header(struct seq_file *m);
  313. /**
  314. * kmalloc_array - allocate memory for an array.
  315. * @n: number of elements.
  316. * @size: element size.
  317. * @flags: the type of memory to allocate.
  318. *
  319. * The @flags argument may be one of:
  320. *
  321. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  322. *
  323. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  324. *
  325. * %GFP_ATOMIC - Allocation will not sleep. May use emergency pools.
  326. * For example, use this inside interrupt handlers.
  327. *
  328. * %GFP_HIGHUSER - Allocate pages from high memory.
  329. *
  330. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  331. *
  332. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  333. *
  334. * %GFP_NOWAIT - Allocation will not sleep.
  335. *
  336. * %GFP_THISNODE - Allocate node-local memory only.
  337. *
  338. * %GFP_DMA - Allocation suitable for DMA.
  339. * Should only be used for kmalloc() caches. Otherwise, use a
  340. * slab created with SLAB_DMA.
  341. *
  342. * Also it is possible to set different flags by OR'ing
  343. * in one or more of the following additional @flags:
  344. *
  345. * %__GFP_COLD - Request cache-cold pages instead of
  346. * trying to return cache-warm pages.
  347. *
  348. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  349. *
  350. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  351. * (think twice before using).
  352. *
  353. * %__GFP_NORETRY - If memory is not immediately available,
  354. * then give up at once.
  355. *
  356. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  357. *
  358. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  359. *
  360. * There are other flags available as well, but these are not intended
  361. * for general use, and so are not documented here. For a full list of
  362. * potential flags, always refer to linux/gfp.h.
  363. */
  364. static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
  365. {
  366. if (size != 0 && n > SIZE_MAX / size)
  367. return NULL;
  368. return __kmalloc(n * size, flags);
  369. }
  370. /**
  371. * kcalloc - allocate memory for an array. The memory is set to zero.
  372. * @n: number of elements.
  373. * @size: element size.
  374. * @flags: the type of memory to allocate (see kmalloc).
  375. */
  376. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  377. {
  378. return kmalloc_array(n, size, flags | __GFP_ZERO);
  379. }
  380. #if !defined(CONFIG_NUMA) && !defined(CONFIG_SLOB)
  381. /**
  382. * kmalloc_node - allocate memory from a specific node
  383. * @size: how many bytes of memory are required.
  384. * @flags: the type of memory to allocate (see kcalloc).
  385. * @node: node to allocate from.
  386. *
  387. * kmalloc() for non-local nodes, used to allocate from a specific node
  388. * if available. Equivalent to kmalloc() in the non-NUMA single-node
  389. * case.
  390. */
  391. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  392. {
  393. return kmalloc(size, flags);
  394. }
  395. static inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
  396. {
  397. return __kmalloc(size, flags);
  398. }
  399. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  400. static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
  401. gfp_t flags, int node)
  402. {
  403. return kmem_cache_alloc(cachep, flags);
  404. }
  405. #endif /* !CONFIG_NUMA && !CONFIG_SLOB */
  406. /*
  407. * kmalloc_track_caller is a special version of kmalloc that records the
  408. * calling function of the routine calling it for slab leak tracking instead
  409. * of just the calling function (confusing, eh?).
  410. * It's useful when the call to kmalloc comes from a widely-used standard
  411. * allocator where we care about the real place the memory allocation
  412. * request comes from.
  413. */
  414. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \
  415. (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \
  416. (defined(CONFIG_SLOB) && defined(CONFIG_TRACING))
  417. extern void *__kmalloc_track_caller(size_t, gfp_t, unsigned long);
  418. #define kmalloc_track_caller(size, flags) \
  419. __kmalloc_track_caller(size, flags, _RET_IP_)
  420. #else
  421. #define kmalloc_track_caller(size, flags) \
  422. __kmalloc(size, flags)
  423. #endif /* DEBUG_SLAB */
  424. #ifdef CONFIG_NUMA
  425. /*
  426. * kmalloc_node_track_caller is a special version of kmalloc_node that
  427. * records the calling function of the routine calling it for slab leak
  428. * tracking instead of just the calling function (confusing, eh?).
  429. * It's useful when the call to kmalloc_node comes from a widely-used
  430. * standard allocator where we care about the real place the memory
  431. * allocation request comes from.
  432. */
  433. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \
  434. (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \
  435. (defined(CONFIG_SLOB) && defined(CONFIG_TRACING))
  436. extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long);
  437. #define kmalloc_node_track_caller(size, flags, node) \
  438. __kmalloc_node_track_caller(size, flags, node, \
  439. _RET_IP_)
  440. #else
  441. #define kmalloc_node_track_caller(size, flags, node) \
  442. __kmalloc_node(size, flags, node)
  443. #endif
  444. #else /* CONFIG_NUMA */
  445. #define kmalloc_node_track_caller(size, flags, node) \
  446. kmalloc_track_caller(size, flags)
  447. #endif /* CONFIG_NUMA */
  448. /*
  449. * Shortcuts
  450. */
  451. static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
  452. {
  453. return kmem_cache_alloc(k, flags | __GFP_ZERO);
  454. }
  455. /**
  456. * kzalloc - allocate memory. The memory is set to zero.
  457. * @size: how many bytes of memory are required.
  458. * @flags: the type of memory to allocate (see kmalloc).
  459. */
  460. static inline void *kzalloc(size_t size, gfp_t flags)
  461. {
  462. return kmalloc(size, flags | __GFP_ZERO);
  463. }
  464. /**
  465. * kzalloc_node - allocate zeroed memory from a particular memory node.
  466. * @size: how many bytes of memory are required.
  467. * @flags: the type of memory to allocate (see kmalloc).
  468. * @node: memory node from which to allocate
  469. */
  470. static inline void *kzalloc_node(size_t size, gfp_t flags, int node)
  471. {
  472. return kmalloc_node(size, flags | __GFP_ZERO, node);
  473. }
  474. /*
  475. * Determine the size of a slab object
  476. */
  477. static inline unsigned int kmem_cache_size(struct kmem_cache *s)
  478. {
  479. return s->object_size;
  480. }
  481. void __init kmem_cache_init_late(void);
  482. #endif /* _LINUX_SLAB_H */