slab.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 | \
  110. SLAB_CACHE_DMA32 | SLAB_PANIC | \
  111. SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS )
  112. #if defined(CONFIG_DEBUG_SLAB)
  113. #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
  114. #elif defined(CONFIG_SLUB_DEBUG)
  115. #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  116. SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
  117. #else
  118. #define SLAB_DEBUG_FLAGS (0)
  119. #endif
  120. #if defined(CONFIG_SLAB)
  121. #define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
  122. SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
  123. SLAB_ACCOUNT)
  124. #elif defined(CONFIG_SLUB)
  125. #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
  126. SLAB_TEMPORARY | SLAB_ACCOUNT)
  127. #else
  128. #define SLAB_CACHE_FLAGS (0)
  129. #endif
  130. /* Common flags available with current configuration */
  131. #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
  132. /* Common flags permitted for kmem_cache_create */
  133. #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
  134. SLAB_RED_ZONE | \
  135. SLAB_POISON | \
  136. SLAB_STORE_USER | \
  137. SLAB_TRACE | \
  138. SLAB_CONSISTENCY_CHECKS | \
  139. SLAB_MEM_SPREAD | \
  140. SLAB_NOLEAKTRACE | \
  141. SLAB_RECLAIM_ACCOUNT | \
  142. SLAB_TEMPORARY | \
  143. SLAB_ACCOUNT)
  144. bool __kmem_cache_empty(struct kmem_cache *);
  145. int __kmem_cache_shutdown(struct kmem_cache *);
  146. void __kmem_cache_release(struct kmem_cache *);
  147. int __kmem_cache_shrink(struct kmem_cache *);
  148. void __kmemcg_cache_deactivate(struct kmem_cache *s);
  149. void slab_kmem_cache_release(struct kmem_cache *);
  150. struct seq_file;
  151. struct file;
  152. struct slabinfo {
  153. unsigned long active_objs;
  154. unsigned long num_objs;
  155. unsigned long active_slabs;
  156. unsigned long num_slabs;
  157. unsigned long shared_avail;
  158. unsigned int limit;
  159. unsigned int batchcount;
  160. unsigned int shared;
  161. unsigned int objects_per_slab;
  162. unsigned int cache_order;
  163. };
  164. void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
  165. void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
  166. ssize_t slabinfo_write(struct file *file, const char __user *buffer,
  167. size_t count, loff_t *ppos);
  168. /*
  169. * Generic implementation of bulk operations
  170. * These are useful for situations in which the allocator cannot
  171. * perform optimizations. In that case segments of the object listed
  172. * may be allocated or freed using these operations.
  173. */
  174. void __kmem_cache_free_bulk(struct kmem_cache *, size_t, void **);
  175. int __kmem_cache_alloc_bulk(struct kmem_cache *, gfp_t, size_t, void **);
  176. #ifdef CONFIG_MEMCG_KMEM
  177. /* List of all root caches. */
  178. extern struct list_head slab_root_caches;
  179. #define root_caches_node memcg_params.__root_caches_node
  180. /*
  181. * Iterate over all memcg caches of the given root cache. The caller must hold
  182. * slab_mutex.
  183. */
  184. #define for_each_memcg_cache(iter, root) \
  185. list_for_each_entry(iter, &(root)->memcg_params.children, \
  186. memcg_params.children_node)
  187. static inline bool is_root_cache(struct kmem_cache *s)
  188. {
  189. return !s->memcg_params.root_cache;
  190. }
  191. static inline bool slab_equal_or_root(struct kmem_cache *s,
  192. struct kmem_cache *p)
  193. {
  194. return p == s || p == s->memcg_params.root_cache;
  195. }
  196. /*
  197. * We use suffixes to the name in memcg because we can't have caches
  198. * created in the system with the same name. But when we print them
  199. * locally, better refer to them with the base name
  200. */
  201. static inline const char *cache_name(struct kmem_cache *s)
  202. {
  203. if (!is_root_cache(s))
  204. s = s->memcg_params.root_cache;
  205. return s->name;
  206. }
  207. /*
  208. * Note, we protect with RCU only the memcg_caches array, not per-memcg caches.
  209. * That said the caller must assure the memcg's cache won't go away by either
  210. * taking a css reference to the owner cgroup, or holding the slab_mutex.
  211. */
  212. static inline struct kmem_cache *
  213. cache_from_memcg_idx(struct kmem_cache *s, int idx)
  214. {
  215. struct kmem_cache *cachep;
  216. struct memcg_cache_array *arr;
  217. rcu_read_lock();
  218. arr = rcu_dereference(s->memcg_params.memcg_caches);
  219. /*
  220. * Make sure we will access the up-to-date value. The code updating
  221. * memcg_caches issues a write barrier to match this (see
  222. * memcg_create_kmem_cache()).
  223. */
  224. cachep = READ_ONCE(arr->entries[idx]);
  225. rcu_read_unlock();
  226. return cachep;
  227. }
  228. static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s)
  229. {
  230. if (is_root_cache(s))
  231. return s;
  232. return s->memcg_params.root_cache;
  233. }
  234. static __always_inline int memcg_charge_slab(struct page *page,
  235. gfp_t gfp, int order,
  236. struct kmem_cache *s)
  237. {
  238. if (!memcg_kmem_enabled())
  239. return 0;
  240. if (is_root_cache(s))
  241. return 0;
  242. return memcg_kmem_charge_memcg(page, gfp, order, s->memcg_params.memcg);
  243. }
  244. static __always_inline void memcg_uncharge_slab(struct page *page, int order,
  245. struct kmem_cache *s)
  246. {
  247. if (!memcg_kmem_enabled())
  248. return;
  249. memcg_kmem_uncharge(page, order);
  250. }
  251. extern void slab_init_memcg_params(struct kmem_cache *);
  252. extern void memcg_link_cache(struct kmem_cache *s);
  253. extern void slab_deactivate_memcg_cache_rcu_sched(struct kmem_cache *s,
  254. void (*deact_fn)(struct kmem_cache *));
  255. #else /* CONFIG_MEMCG_KMEM */
  256. /* If !memcg, all caches are root. */
  257. #define slab_root_caches slab_caches
  258. #define root_caches_node list
  259. #define for_each_memcg_cache(iter, root) \
  260. for ((void)(iter), (void)(root); 0; )
  261. static inline bool is_root_cache(struct kmem_cache *s)
  262. {
  263. return true;
  264. }
  265. static inline bool slab_equal_or_root(struct kmem_cache *s,
  266. struct kmem_cache *p)
  267. {
  268. return true;
  269. }
  270. static inline const char *cache_name(struct kmem_cache *s)
  271. {
  272. return s->name;
  273. }
  274. static inline struct kmem_cache *
  275. cache_from_memcg_idx(struct kmem_cache *s, int idx)
  276. {
  277. return NULL;
  278. }
  279. static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s)
  280. {
  281. return s;
  282. }
  283. static inline int memcg_charge_slab(struct page *page, gfp_t gfp, int order,
  284. struct kmem_cache *s)
  285. {
  286. return 0;
  287. }
  288. static inline void memcg_uncharge_slab(struct page *page, int order,
  289. struct kmem_cache *s)
  290. {
  291. }
  292. static inline void slab_init_memcg_params(struct kmem_cache *s)
  293. {
  294. }
  295. static inline void memcg_link_cache(struct kmem_cache *s)
  296. {
  297. }
  298. #endif /* CONFIG_MEMCG_KMEM */
  299. static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
  300. {
  301. struct kmem_cache *cachep;
  302. struct page *page;
  303. /*
  304. * When kmemcg is not being used, both assignments should return the
  305. * same value. but we don't want to pay the assignment price in that
  306. * case. If it is not compiled in, the compiler should be smart enough
  307. * to not do even the assignment. In that case, slab_equal_or_root
  308. * will also be a constant.
  309. */
  310. if (!memcg_kmem_enabled() &&
  311. !unlikely(s->flags & SLAB_CONSISTENCY_CHECKS))
  312. return s;
  313. page = virt_to_head_page(x);
  314. cachep = page->slab_cache;
  315. if (slab_equal_or_root(cachep, s))
  316. return cachep;
  317. pr_err("%s: Wrong slab cache. %s but object is from %s\n",
  318. __func__, s->name, cachep->name);
  319. WARN_ON_ONCE(1);
  320. return s;
  321. }
  322. static inline size_t slab_ksize(const struct kmem_cache *s)
  323. {
  324. #ifndef CONFIG_SLUB
  325. return s->object_size;
  326. #else /* CONFIG_SLUB */
  327. # ifdef CONFIG_SLUB_DEBUG
  328. /*
  329. * Debugging requires use of the padding between object
  330. * and whatever may come after it.
  331. */
  332. if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
  333. return s->object_size;
  334. # endif
  335. if (s->flags & SLAB_KASAN)
  336. return s->object_size;
  337. /*
  338. * If we have the need to store the freelist pointer
  339. * back there or track user information then we can
  340. * only use the space before that information.
  341. */
  342. if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
  343. return s->inuse;
  344. /*
  345. * Else we can use all the padding etc for the allocation
  346. */
  347. return s->size;
  348. #endif
  349. }
  350. static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
  351. gfp_t flags)
  352. {
  353. flags &= gfp_allowed_mask;
  354. fs_reclaim_acquire(flags);
  355. fs_reclaim_release(flags);
  356. might_sleep_if(gfpflags_allow_blocking(flags));
  357. if (should_failslab(s, flags))
  358. return NULL;
  359. if (memcg_kmem_enabled() &&
  360. ((flags & __GFP_ACCOUNT) || (s->flags & SLAB_ACCOUNT)))
  361. return memcg_kmem_get_cache(s);
  362. return s;
  363. }
  364. static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
  365. size_t size, void **p)
  366. {
  367. size_t i;
  368. flags &= gfp_allowed_mask;
  369. for (i = 0; i < size; i++) {
  370. void *object = p[i];
  371. kmemleak_alloc_recursive(object, s->object_size, 1,
  372. s->flags, flags);
  373. kasan_slab_alloc(s, object, flags);
  374. }
  375. if (memcg_kmem_enabled())
  376. memcg_kmem_put_cache(s);
  377. }
  378. #ifndef CONFIG_SLOB
  379. /*
  380. * The slab lists for all objects.
  381. */
  382. struct kmem_cache_node {
  383. spinlock_t list_lock;
  384. #ifdef CONFIG_SLAB
  385. struct list_head slabs_partial; /* partial list first, better asm code */
  386. struct list_head slabs_full;
  387. struct list_head slabs_free;
  388. unsigned long total_slabs; /* length of all slab lists */
  389. unsigned long free_slabs; /* length of free slab list only */
  390. unsigned long free_objects;
  391. unsigned int free_limit;
  392. unsigned int colour_next; /* Per-node cache coloring */
  393. struct array_cache *shared; /* shared per node */
  394. struct alien_cache **alien; /* on other nodes */
  395. unsigned long next_reap; /* updated without locking */
  396. int free_touched; /* updated without locking */
  397. #endif
  398. #ifdef CONFIG_SLUB
  399. unsigned long nr_partial;
  400. struct list_head partial;
  401. #ifdef CONFIG_SLUB_DEBUG
  402. atomic_long_t nr_slabs;
  403. atomic_long_t total_objects;
  404. struct list_head full;
  405. #endif
  406. #endif
  407. };
  408. static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
  409. {
  410. return s->node[node];
  411. }
  412. /*
  413. * Iterator over all nodes. The body will be executed for each node that has
  414. * a kmem_cache_node structure allocated (which is true for all online nodes)
  415. */
  416. #define for_each_kmem_cache_node(__s, __node, __n) \
  417. for (__node = 0; __node < nr_node_ids; __node++) \
  418. if ((__n = get_node(__s, __node)))
  419. #endif
  420. void *slab_start(struct seq_file *m, loff_t *pos);
  421. void *slab_next(struct seq_file *m, void *p, loff_t *pos);
  422. void slab_stop(struct seq_file *m, void *p);
  423. void *memcg_slab_start(struct seq_file *m, loff_t *pos);
  424. void *memcg_slab_next(struct seq_file *m, void *p, loff_t *pos);
  425. void memcg_slab_stop(struct seq_file *m, void *p);
  426. int memcg_slab_show(struct seq_file *m, void *p);
  427. #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
  428. void dump_unreclaimable_slab(void);
  429. #else
  430. static inline void dump_unreclaimable_slab(void)
  431. {
  432. }
  433. #endif
  434. void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
  435. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  436. int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
  437. gfp_t gfp);
  438. void cache_random_seq_destroy(struct kmem_cache *cachep);
  439. #else
  440. static inline int cache_random_seq_create(struct kmem_cache *cachep,
  441. unsigned int count, gfp_t gfp)
  442. {
  443. return 0;
  444. }
  445. static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
  446. #endif /* CONFIG_SLAB_FREELIST_RANDOM */
  447. #endif /* MM_SLAB_H */