slab.h 15 KB

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