slab.h 15 KB

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