slab.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #ifndef MM_SLAB_H
  2. #define MM_SLAB_H
  3. /*
  4. * Internal slab definitions
  5. */
  6. #ifdef CONFIG_SLOB
  7. /*
  8. * Common fields provided in kmem_cache by all slab allocators
  9. * This struct is either used directly by the allocator (SLOB)
  10. * or the allocator must include definitions for all fields
  11. * provided in kmem_cache_common in their definition of kmem_cache.
  12. *
  13. * Once we can do anonymous structs (C11 standard) we could put a
  14. * anonymous struct definition in these allocators so that the
  15. * separate allocations in the kmem_cache structure of SLAB and
  16. * SLUB is no longer needed.
  17. */
  18. struct kmem_cache {
  19. unsigned int object_size;/* The original size of the object */
  20. unsigned int size; /* The aligned/padded/added on size */
  21. unsigned int align; /* Alignment as calculated */
  22. unsigned long flags; /* Active flags on the slab */
  23. const char *name; /* Slab name for sysfs */
  24. int refcount; /* Use counter */
  25. void (*ctor)(void *); /* Called on object slot creation */
  26. struct list_head list; /* List of all slab caches on the system */
  27. };
  28. #endif /* CONFIG_SLOB */
  29. #ifdef CONFIG_SLAB
  30. #include <linux/slab_def.h>
  31. #endif
  32. #ifdef CONFIG_SLUB
  33. #include <linux/slub_def.h>
  34. #endif
  35. #include <linux/memcontrol.h>
  36. #include <linux/fault-inject.h>
  37. #include <linux/kmemcheck.h>
  38. #include <linux/kasan.h>
  39. #include <linux/kmemleak.h>
  40. /*
  41. * State of the slab allocator.
  42. *
  43. * This is used to describe the states of the allocator during bootup.
  44. * Allocators use this to gradually bootstrap themselves. Most allocators
  45. * have the problem that the structures used for managing slab caches are
  46. * allocated from slab caches themselves.
  47. */
  48. enum slab_state {
  49. DOWN, /* No slab functionality yet */
  50. PARTIAL, /* SLUB: kmem_cache_node available */
  51. PARTIAL_NODE, /* SLAB: kmalloc size for node struct available */
  52. UP, /* Slab caches usable but not all extras yet */
  53. FULL /* Everything is working */
  54. };
  55. extern enum slab_state slab_state;
  56. /* The slab cache mutex protects the management structures during changes */
  57. extern struct mutex slab_mutex;
  58. /* The list of all slab caches on the system */
  59. extern struct list_head slab_caches;
  60. /* The slab cache that manages slab cache information */
  61. extern struct kmem_cache *kmem_cache;
  62. unsigned long calculate_alignment(unsigned long flags,
  63. unsigned long align, unsigned long size);
  64. #ifndef CONFIG_SLOB
  65. /* Kmalloc array related functions */
  66. void setup_kmalloc_cache_index_table(void);
  67. void create_kmalloc_caches(unsigned long);
  68. /* Find the kmalloc slab corresponding for a certain size */
  69. struct kmem_cache *kmalloc_slab(size_t, gfp_t);
  70. #endif
  71. /* Functions provided by the slab allocators */
  72. extern int __kmem_cache_create(struct kmem_cache *, unsigned long flags);
  73. extern struct kmem_cache *create_kmalloc_cache(const char *name, size_t size,
  74. unsigned long flags);
  75. extern void create_boot_cache(struct kmem_cache *, const char *name,
  76. size_t size, unsigned long flags);
  77. int slab_unmergeable(struct kmem_cache *s);
  78. struct kmem_cache *find_mergeable(size_t size, size_t align,
  79. unsigned long flags, const char *name, void (*ctor)(void *));
  80. #ifndef CONFIG_SLOB
  81. struct kmem_cache *
  82. __kmem_cache_alias(const char *name, size_t size, size_t align,
  83. unsigned long flags, void (*ctor)(void *));
  84. unsigned long kmem_cache_flags(unsigned long object_size,
  85. unsigned long flags, const char *name,
  86. void (*ctor)(void *));
  87. #else
  88. static inline struct kmem_cache *
  89. __kmem_cache_alias(const char *name, size_t size, size_t align,
  90. unsigned long flags, void (*ctor)(void *))
  91. { return NULL; }
  92. static inline unsigned long kmem_cache_flags(unsigned long object_size,
  93. unsigned long flags, const char *name,
  94. void (*ctor)(void *))
  95. {
  96. return flags;
  97. }
  98. #endif
  99. /* Legal flag mask for kmem_cache_create(), for various configurations */
  100. #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | SLAB_PANIC | \
  101. SLAB_DESTROY_BY_RCU | SLAB_DEBUG_OBJECTS )
  102. #if defined(CONFIG_DEBUG_SLAB)
  103. #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
  104. #elif defined(CONFIG_SLUB_DEBUG)
  105. #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  106. SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
  107. #else
  108. #define SLAB_DEBUG_FLAGS (0)
  109. #endif
  110. #if defined(CONFIG_SLAB)
  111. #define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
  112. SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
  113. SLAB_NOTRACK | SLAB_ACCOUNT)
  114. #elif defined(CONFIG_SLUB)
  115. #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
  116. SLAB_TEMPORARY | SLAB_NOTRACK | SLAB_ACCOUNT)
  117. #else
  118. #define SLAB_CACHE_FLAGS (0)
  119. #endif
  120. #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
  121. int __kmem_cache_shutdown(struct kmem_cache *);
  122. void __kmem_cache_release(struct kmem_cache *);
  123. int __kmem_cache_shrink(struct kmem_cache *, bool);
  124. void slab_kmem_cache_release(struct kmem_cache *);
  125. struct seq_file;
  126. struct file;
  127. struct slabinfo {
  128. unsigned long active_objs;
  129. unsigned long num_objs;
  130. unsigned long active_slabs;
  131. unsigned long num_slabs;
  132. unsigned long shared_avail;
  133. unsigned int limit;
  134. unsigned int batchcount;
  135. unsigned int shared;
  136. unsigned int objects_per_slab;
  137. unsigned int cache_order;
  138. };
  139. void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
  140. void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
  141. ssize_t slabinfo_write(struct file *file, const char __user *buffer,
  142. size_t count, loff_t *ppos);
  143. /*
  144. * Generic implementation of bulk operations
  145. * These are useful for situations in which the allocator cannot
  146. * perform optimizations. In that case segments of the object listed
  147. * may be allocated or freed using these operations.
  148. */
  149. void __kmem_cache_free_bulk(struct kmem_cache *, size_t, void **);
  150. int __kmem_cache_alloc_bulk(struct kmem_cache *, gfp_t, size_t, void **);
  151. #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
  152. /*
  153. * Iterate over all memcg caches of the given root cache. The caller must hold
  154. * slab_mutex.
  155. */
  156. #define for_each_memcg_cache(iter, root) \
  157. list_for_each_entry(iter, &(root)->memcg_params.list, \
  158. memcg_params.list)
  159. static inline bool is_root_cache(struct kmem_cache *s)
  160. {
  161. return s->memcg_params.is_root_cache;
  162. }
  163. static inline bool slab_equal_or_root(struct kmem_cache *s,
  164. struct kmem_cache *p)
  165. {
  166. return p == s || p == s->memcg_params.root_cache;
  167. }
  168. /*
  169. * We use suffixes to the name in memcg because we can't have caches
  170. * created in the system with the same name. But when we print them
  171. * locally, better refer to them with the base name
  172. */
  173. static inline const char *cache_name(struct kmem_cache *s)
  174. {
  175. if (!is_root_cache(s))
  176. s = s->memcg_params.root_cache;
  177. return s->name;
  178. }
  179. /*
  180. * Note, we protect with RCU only the memcg_caches array, not per-memcg caches.
  181. * That said the caller must assure the memcg's cache won't go away by either
  182. * taking a css reference to the owner cgroup, or holding the slab_mutex.
  183. */
  184. static inline struct kmem_cache *
  185. cache_from_memcg_idx(struct kmem_cache *s, int idx)
  186. {
  187. struct kmem_cache *cachep;
  188. struct memcg_cache_array *arr;
  189. rcu_read_lock();
  190. arr = rcu_dereference(s->memcg_params.memcg_caches);
  191. /*
  192. * Make sure we will access the up-to-date value. The code updating
  193. * memcg_caches issues a write barrier to match this (see
  194. * memcg_create_kmem_cache()).
  195. */
  196. cachep = lockless_dereference(arr->entries[idx]);
  197. rcu_read_unlock();
  198. return cachep;
  199. }
  200. static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s)
  201. {
  202. if (is_root_cache(s))
  203. return s;
  204. return s->memcg_params.root_cache;
  205. }
  206. static __always_inline int memcg_charge_slab(struct page *page,
  207. gfp_t gfp, int order,
  208. struct kmem_cache *s)
  209. {
  210. int ret;
  211. if (!memcg_kmem_enabled())
  212. return 0;
  213. if (is_root_cache(s))
  214. return 0;
  215. ret = __memcg_kmem_charge_memcg(page, gfp, order,
  216. s->memcg_params.memcg);
  217. if (ret)
  218. return ret;
  219. memcg_kmem_update_page_stat(page,
  220. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  221. MEMCG_SLAB_RECLAIMABLE : MEMCG_SLAB_UNRECLAIMABLE,
  222. 1 << order);
  223. return 0;
  224. }
  225. static __always_inline void memcg_uncharge_slab(struct page *page, int order,
  226. struct kmem_cache *s)
  227. {
  228. memcg_kmem_update_page_stat(page,
  229. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  230. MEMCG_SLAB_RECLAIMABLE : MEMCG_SLAB_UNRECLAIMABLE,
  231. -(1 << order));
  232. memcg_kmem_uncharge(page, order);
  233. }
  234. extern void slab_init_memcg_params(struct kmem_cache *);
  235. #else /* CONFIG_MEMCG && !CONFIG_SLOB */
  236. #define for_each_memcg_cache(iter, root) \
  237. for ((void)(iter), (void)(root); 0; )
  238. static inline bool is_root_cache(struct kmem_cache *s)
  239. {
  240. return true;
  241. }
  242. static inline bool slab_equal_or_root(struct kmem_cache *s,
  243. struct kmem_cache *p)
  244. {
  245. return true;
  246. }
  247. static inline const char *cache_name(struct kmem_cache *s)
  248. {
  249. return s->name;
  250. }
  251. static inline struct kmem_cache *
  252. cache_from_memcg_idx(struct kmem_cache *s, int idx)
  253. {
  254. return NULL;
  255. }
  256. static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s)
  257. {
  258. return s;
  259. }
  260. static inline int memcg_charge_slab(struct page *page, gfp_t gfp, int order,
  261. struct kmem_cache *s)
  262. {
  263. return 0;
  264. }
  265. static inline void memcg_uncharge_slab(struct page *page, int order,
  266. struct kmem_cache *s)
  267. {
  268. }
  269. static inline void slab_init_memcg_params(struct kmem_cache *s)
  270. {
  271. }
  272. #endif /* CONFIG_MEMCG && !CONFIG_SLOB */
  273. static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
  274. {
  275. struct kmem_cache *cachep;
  276. struct page *page;
  277. /*
  278. * When kmemcg is not being used, both assignments should return the
  279. * same value. but we don't want to pay the assignment price in that
  280. * case. If it is not compiled in, the compiler should be smart enough
  281. * to not do even the assignment. In that case, slab_equal_or_root
  282. * will also be a constant.
  283. */
  284. if (!memcg_kmem_enabled() &&
  285. !unlikely(s->flags & SLAB_CONSISTENCY_CHECKS))
  286. return s;
  287. page = virt_to_head_page(x);
  288. cachep = page->slab_cache;
  289. if (slab_equal_or_root(cachep, s))
  290. return cachep;
  291. pr_err("%s: Wrong slab cache. %s but object is from %s\n",
  292. __func__, s->name, cachep->name);
  293. WARN_ON_ONCE(1);
  294. return s;
  295. }
  296. static inline size_t slab_ksize(const struct kmem_cache *s)
  297. {
  298. #ifndef CONFIG_SLUB
  299. return s->object_size;
  300. #else /* CONFIG_SLUB */
  301. # ifdef CONFIG_SLUB_DEBUG
  302. /*
  303. * Debugging requires use of the padding between object
  304. * and whatever may come after it.
  305. */
  306. if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
  307. return s->object_size;
  308. # endif
  309. /*
  310. * If we have the need to store the freelist pointer
  311. * back there or track user information then we can
  312. * only use the space before that information.
  313. */
  314. if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
  315. return s->inuse;
  316. /*
  317. * Else we can use all the padding etc for the allocation
  318. */
  319. return s->size;
  320. #endif
  321. }
  322. static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
  323. gfp_t flags)
  324. {
  325. flags &= gfp_allowed_mask;
  326. lockdep_trace_alloc(flags);
  327. might_sleep_if(gfpflags_allow_blocking(flags));
  328. if (should_failslab(s, flags))
  329. return NULL;
  330. return memcg_kmem_get_cache(s, flags);
  331. }
  332. static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
  333. size_t size, void **p)
  334. {
  335. size_t i;
  336. flags &= gfp_allowed_mask;
  337. for (i = 0; i < size; i++) {
  338. void *object = p[i];
  339. kmemcheck_slab_alloc(s, flags, object, slab_ksize(s));
  340. kmemleak_alloc_recursive(object, s->object_size, 1,
  341. s->flags, flags);
  342. kasan_slab_alloc(s, object, flags);
  343. }
  344. memcg_kmem_put_cache(s);
  345. }
  346. #ifndef CONFIG_SLOB
  347. /*
  348. * The slab lists for all objects.
  349. */
  350. struct kmem_cache_node {
  351. spinlock_t list_lock;
  352. #ifdef CONFIG_SLAB
  353. struct list_head slabs_partial; /* partial list first, better asm code */
  354. struct list_head slabs_full;
  355. struct list_head slabs_free;
  356. unsigned long free_objects;
  357. unsigned int free_limit;
  358. unsigned int colour_next; /* Per-node cache coloring */
  359. struct array_cache *shared; /* shared per node */
  360. struct alien_cache **alien; /* on other nodes */
  361. unsigned long next_reap; /* updated without locking */
  362. int free_touched; /* updated without locking */
  363. #endif
  364. #ifdef CONFIG_SLUB
  365. unsigned long nr_partial;
  366. struct list_head partial;
  367. #ifdef CONFIG_SLUB_DEBUG
  368. atomic_long_t nr_slabs;
  369. atomic_long_t total_objects;
  370. struct list_head full;
  371. #endif
  372. #endif
  373. };
  374. static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
  375. {
  376. return s->node[node];
  377. }
  378. /*
  379. * Iterator over all nodes. The body will be executed for each node that has
  380. * a kmem_cache_node structure allocated (which is true for all online nodes)
  381. */
  382. #define for_each_kmem_cache_node(__s, __node, __n) \
  383. for (__node = 0; __node < nr_node_ids; __node++) \
  384. if ((__n = get_node(__s, __node)))
  385. #endif
  386. void *slab_start(struct seq_file *m, loff_t *pos);
  387. void *slab_next(struct seq_file *m, void *p, loff_t *pos);
  388. void slab_stop(struct seq_file *m, void *p);
  389. int memcg_slab_show(struct seq_file *m, void *p);
  390. #endif /* MM_SLAB_H */