1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Internal slab definitions
8 /* Reuses the bits in struct page */
10 unsigned long __page_flags;
12 #if defined(CONFIG_SLAB)
14 struct kmem_cache *slab_cache;
17 struct list_head slab_list;
18 void *freelist; /* array of free object indexes */
19 void *s_mem; /* first object */
21 struct rcu_head rcu_head;
25 #elif defined(CONFIG_SLUB)
27 struct kmem_cache *slab_cache;
31 struct list_head slab_list;
32 #ifdef CONFIG_SLUB_CPU_PARTIAL
35 int slabs; /* Nr of slabs left */
39 /* Double-word boundary */
40 void *freelist; /* first free object */
42 unsigned long counters;
50 struct rcu_head rcu_head;
52 unsigned int __unused;
55 #error "Unexpected slab allocator configured"
58 atomic_t __page_refcount;
60 unsigned long memcg_data;
64 #define SLAB_MATCH(pg, sl) \
65 static_assert(offsetof(struct page, pg) == offsetof(struct slab, sl))
66 SLAB_MATCH(flags, __page_flags);
67 SLAB_MATCH(compound_head, slab_cache); /* Ensure bit 0 is clear */
68 SLAB_MATCH(_refcount, __page_refcount);
70 SLAB_MATCH(memcg_data, memcg_data);
73 static_assert(sizeof(struct slab) <= sizeof(struct page));
74 #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && defined(CONFIG_SLUB)
75 static_assert(IS_ALIGNED(offsetof(struct slab, freelist), 2*sizeof(void *)));
79 * folio_slab - Converts from folio to slab.
82 * Currently struct slab is a different representation of a folio where
83 * folio_test_slab() is true.
85 * Return: The slab which contains this folio.
87 #define folio_slab(folio) (_Generic((folio), \
88 const struct folio *: (const struct slab *)(folio), \
89 struct folio *: (struct slab *)(folio)))
92 * slab_folio - The folio allocated for a slab
95 * Slabs are allocated as folios that contain the individual objects and are
96 * using some fields in the first struct page of the folio - those fields are
97 * now accessed by struct slab. It is occasionally necessary to convert back to
98 * a folio in order to communicate with the rest of the mm. Please use this
99 * helper function instead of casting yourself, as the implementation may change
102 #define slab_folio(s) (_Generic((s), \
103 const struct slab *: (const struct folio *)s, \
104 struct slab *: (struct folio *)s))
107 * page_slab - Converts from first struct page to slab.
108 * @p: The first (either head of compound or single) page of slab.
110 * A temporary wrapper to convert struct page to struct slab in situations where
111 * we know the page is the compound head, or single order-0 page.
113 * Long-term ideally everything would work with struct slab directly or go
114 * through folio to struct slab.
116 * Return: The slab which contains this page
118 #define page_slab(p) (_Generic((p), \
119 const struct page *: (const struct slab *)(p), \
120 struct page *: (struct slab *)(p)))
123 * slab_page - The first struct page allocated for a slab
126 * A convenience wrapper for converting slab to the first struct page of the
127 * underlying folio, to communicate with code not yet converted to folio or
130 #define slab_page(s) folio_page(slab_folio(s), 0)
133 * If network-based swap is enabled, sl*b must keep track of whether pages
134 * were allocated from pfmemalloc reserves.
136 static inline bool slab_test_pfmemalloc(const struct slab *slab)
138 return folio_test_active((struct folio *)slab_folio(slab));
141 static inline void slab_set_pfmemalloc(struct slab *slab)
143 folio_set_active(slab_folio(slab));
146 static inline void slab_clear_pfmemalloc(struct slab *slab)
148 folio_clear_active(slab_folio(slab));
151 static inline void __slab_clear_pfmemalloc(struct slab *slab)
153 __folio_clear_active(slab_folio(slab));
156 static inline void *slab_address(const struct slab *slab)
158 return folio_address(slab_folio(slab));
161 static inline int slab_nid(const struct slab *slab)
163 return folio_nid(slab_folio(slab));
166 static inline pg_data_t *slab_pgdat(const struct slab *slab)
168 return folio_pgdat(slab_folio(slab));
171 static inline struct slab *virt_to_slab(const void *addr)
173 struct folio *folio = virt_to_folio(addr);
175 if (!folio_test_slab(folio))
178 return folio_slab(folio);
181 static inline int slab_order(const struct slab *slab)
183 return folio_order((struct folio *)slab_folio(slab));
186 static inline size_t slab_size(const struct slab *slab)
188 return PAGE_SIZE << slab_order(slab);
192 #include <linux/slab_def.h>
196 #include <linux/slub_def.h>
199 #include <linux/memcontrol.h>
200 #include <linux/fault-inject.h>
201 #include <linux/kasan.h>
202 #include <linux/kmemleak.h>
203 #include <linux/random.h>
204 #include <linux/sched/mm.h>
205 #include <linux/list_lru.h>
208 * State of the slab allocator.
210 * This is used to describe the states of the allocator during bootup.
211 * Allocators use this to gradually bootstrap themselves. Most allocators
212 * have the problem that the structures used for managing slab caches are
213 * allocated from slab caches themselves.
216 DOWN, /* No slab functionality yet */
217 PARTIAL, /* SLUB: kmem_cache_node available */
218 PARTIAL_NODE, /* SLAB: kmalloc size for node struct available */
219 UP, /* Slab caches usable but not all extras yet */
220 FULL /* Everything is working */
223 extern enum slab_state slab_state;
225 /* The slab cache mutex protects the management structures during changes */
226 extern struct mutex slab_mutex;
228 /* The list of all slab caches on the system */
229 extern struct list_head slab_caches;
231 /* The slab cache that manages slab cache information */
232 extern struct kmem_cache *kmem_cache;
234 /* A table of kmalloc cache names and sizes */
235 extern const struct kmalloc_info_struct {
236 const char *name[NR_KMALLOC_TYPES];
240 /* Kmalloc array related functions */
241 void setup_kmalloc_cache_index_table(void);
242 void create_kmalloc_caches(slab_flags_t);
244 /* Find the kmalloc slab corresponding for a certain size */
245 struct kmem_cache *kmalloc_slab(size_t, gfp_t);
247 void *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags,
248 int node, size_t orig_size,
249 unsigned long caller);
250 void __kmem_cache_free(struct kmem_cache *s, void *x, unsigned long caller);
252 gfp_t kmalloc_fix_flags(gfp_t flags);
254 /* Functions provided by the slab allocators */
255 int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags);
257 struct kmem_cache *create_kmalloc_cache(const char *name, unsigned int size,
258 slab_flags_t flags, unsigned int useroffset,
259 unsigned int usersize);
260 extern void create_boot_cache(struct kmem_cache *, const char *name,
261 unsigned int size, slab_flags_t flags,
262 unsigned int useroffset, unsigned int usersize);
264 int slab_unmergeable(struct kmem_cache *s);
265 struct kmem_cache *find_mergeable(unsigned size, unsigned align,
266 slab_flags_t flags, const char *name, void (*ctor)(void *));
268 __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
269 slab_flags_t flags, void (*ctor)(void *));
271 slab_flags_t kmem_cache_flags(unsigned int object_size,
272 slab_flags_t flags, const char *name);
274 static inline bool is_kmalloc_cache(struct kmem_cache *s)
276 return (s->flags & SLAB_KMALLOC);
279 /* Legal flag mask for kmem_cache_create(), for various configurations */
280 #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
281 SLAB_CACHE_DMA32 | SLAB_PANIC | \
282 SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS )
284 #if defined(CONFIG_DEBUG_SLAB)
285 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
286 #elif defined(CONFIG_SLUB_DEBUG)
287 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
288 SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
290 #define SLAB_DEBUG_FLAGS (0)
293 #if defined(CONFIG_SLAB)
294 #define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
295 SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
297 #elif defined(CONFIG_SLUB)
298 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
299 SLAB_TEMPORARY | SLAB_ACCOUNT | \
300 SLAB_NO_USER_FLAGS | SLAB_KMALLOC)
302 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
305 /* Common flags available with current configuration */
306 #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
308 /* Common flags permitted for kmem_cache_create */
309 #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
314 SLAB_CONSISTENCY_CHECKS | \
317 SLAB_RECLAIM_ACCOUNT | \
323 bool __kmem_cache_empty(struct kmem_cache *);
324 int __kmem_cache_shutdown(struct kmem_cache *);
325 void __kmem_cache_release(struct kmem_cache *);
326 int __kmem_cache_shrink(struct kmem_cache *);
327 void slab_kmem_cache_release(struct kmem_cache *);
333 unsigned long active_objs;
334 unsigned long num_objs;
335 unsigned long active_slabs;
336 unsigned long num_slabs;
337 unsigned long shared_avail;
339 unsigned int batchcount;
341 unsigned int objects_per_slab;
342 unsigned int cache_order;
345 void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
346 void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
347 ssize_t slabinfo_write(struct file *file, const char __user *buffer,
348 size_t count, loff_t *ppos);
350 static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s)
352 return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
353 NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
356 #ifdef CONFIG_SLUB_DEBUG
357 #ifdef CONFIG_SLUB_DEBUG_ON
358 DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
360 DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
362 extern void print_tracking(struct kmem_cache *s, void *object);
363 long validate_slab_cache(struct kmem_cache *s);
364 static inline bool __slub_debug_enabled(void)
366 return static_branch_unlikely(&slub_debug_enabled);
369 static inline void print_tracking(struct kmem_cache *s, void *object)
372 static inline bool __slub_debug_enabled(void)
379 * Returns true if any of the specified slub_debug flags is enabled for the
380 * cache. Use only for flags parsed by setup_slub_debug() as it also enables
383 static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
385 if (IS_ENABLED(CONFIG_SLUB_DEBUG))
386 VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
387 if (__slub_debug_enabled())
388 return s->flags & flags;
392 #ifdef CONFIG_MEMCG_KMEM
394 * slab_objcgs - get the object cgroups vector associated with a slab
395 * @slab: a pointer to the slab struct
397 * Returns a pointer to the object cgroups vector associated with the slab,
398 * or NULL if no such vector has been associated yet.
400 static inline struct obj_cgroup **slab_objcgs(struct slab *slab)
402 unsigned long memcg_data = READ_ONCE(slab->memcg_data);
404 VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS),
406 VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, slab_page(slab));
408 return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
411 int memcg_alloc_slab_cgroups(struct slab *slab, struct kmem_cache *s,
412 gfp_t gfp, bool new_slab);
413 void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
414 enum node_stat_item idx, int nr);
416 static inline void memcg_free_slab_cgroups(struct slab *slab)
418 kfree(slab_objcgs(slab));
419 slab->memcg_data = 0;
422 static inline size_t obj_full_size(struct kmem_cache *s)
425 * For each accounted object there is an extra space which is used
426 * to store obj_cgroup membership. Charge it too.
428 return s->size + sizeof(struct obj_cgroup *);
432 * Returns false if the allocation should fail.
434 static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
435 struct list_lru *lru,
436 struct obj_cgroup **objcgp,
437 size_t objects, gfp_t flags)
439 struct obj_cgroup *objcg;
441 if (!memcg_kmem_online())
444 if (!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT))
447 objcg = get_obj_cgroup_from_current();
453 struct mem_cgroup *memcg;
455 memcg = get_mem_cgroup_from_objcg(objcg);
456 ret = memcg_list_lru_alloc(memcg, lru, flags);
457 css_put(&memcg->css);
463 if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s)))
469 obj_cgroup_put(objcg);
473 static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
474 struct obj_cgroup *objcg,
475 gfp_t flags, size_t size,
482 if (!memcg_kmem_online() || !objcg)
485 for (i = 0; i < size; i++) {
487 slab = virt_to_slab(p[i]);
489 if (!slab_objcgs(slab) &&
490 memcg_alloc_slab_cgroups(slab, s, flags,
492 obj_cgroup_uncharge(objcg, obj_full_size(s));
496 off = obj_to_index(s, slab, p[i]);
497 obj_cgroup_get(objcg);
498 slab_objcgs(slab)[off] = objcg;
499 mod_objcg_state(objcg, slab_pgdat(slab),
500 cache_vmstat_idx(s), obj_full_size(s));
502 obj_cgroup_uncharge(objcg, obj_full_size(s));
505 obj_cgroup_put(objcg);
508 static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
509 void **p, int objects)
511 struct obj_cgroup **objcgs;
514 if (!memcg_kmem_online())
517 objcgs = slab_objcgs(slab);
521 for (i = 0; i < objects; i++) {
522 struct obj_cgroup *objcg;
525 off = obj_to_index(s, slab, p[i]);
531 obj_cgroup_uncharge(objcg, obj_full_size(s));
532 mod_objcg_state(objcg, slab_pgdat(slab), cache_vmstat_idx(s),
534 obj_cgroup_put(objcg);
538 #else /* CONFIG_MEMCG_KMEM */
539 static inline struct obj_cgroup **slab_objcgs(struct slab *slab)
544 static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
549 static inline int memcg_alloc_slab_cgroups(struct slab *slab,
550 struct kmem_cache *s, gfp_t gfp,
556 static inline void memcg_free_slab_cgroups(struct slab *slab)
560 static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
561 struct list_lru *lru,
562 struct obj_cgroup **objcgp,
563 size_t objects, gfp_t flags)
568 static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
569 struct obj_cgroup *objcg,
570 gfp_t flags, size_t size,
575 static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
576 void **p, int objects)
579 #endif /* CONFIG_MEMCG_KMEM */
581 static inline struct kmem_cache *virt_to_cache(const void *obj)
585 slab = virt_to_slab(obj);
586 if (WARN_ONCE(!slab, "%s: Object is not a Slab page!\n",
589 return slab->slab_cache;
592 static __always_inline void account_slab(struct slab *slab, int order,
593 struct kmem_cache *s, gfp_t gfp)
595 if (memcg_kmem_online() && (s->flags & SLAB_ACCOUNT))
596 memcg_alloc_slab_cgroups(slab, s, gfp, true);
598 mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
602 static __always_inline void unaccount_slab(struct slab *slab, int order,
603 struct kmem_cache *s)
605 if (memcg_kmem_online())
606 memcg_free_slab_cgroups(slab);
608 mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
609 -(PAGE_SIZE << order));
612 static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
614 struct kmem_cache *cachep;
616 if (!IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
617 !kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS))
620 cachep = virt_to_cache(x);
621 if (WARN(cachep && cachep != s,
622 "%s: Wrong slab cache. %s but object is from %s\n",
623 __func__, s->name, cachep->name))
624 print_tracking(cachep, x);
628 void free_large_kmalloc(struct folio *folio, void *object);
630 size_t __ksize(const void *objp);
632 static inline size_t slab_ksize(const struct kmem_cache *s)
635 return s->object_size;
637 #else /* CONFIG_SLUB */
638 # ifdef CONFIG_SLUB_DEBUG
640 * Debugging requires use of the padding between object
641 * and whatever may come after it.
643 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
644 return s->object_size;
646 if (s->flags & SLAB_KASAN)
647 return s->object_size;
649 * If we have the need to store the freelist pointer
650 * back there or track user information then we can
651 * only use the space before that information.
653 if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
656 * Else we can use all the padding etc for the allocation
662 static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
663 struct list_lru *lru,
664 struct obj_cgroup **objcgp,
665 size_t size, gfp_t flags)
667 flags &= gfp_allowed_mask;
671 if (should_failslab(s, flags))
674 if (!memcg_slab_pre_alloc_hook(s, lru, objcgp, size, flags))
680 static inline void slab_post_alloc_hook(struct kmem_cache *s,
681 struct obj_cgroup *objcg, gfp_t flags,
682 size_t size, void **p, bool init,
683 unsigned int orig_size)
685 unsigned int zero_size = s->object_size;
688 flags &= gfp_allowed_mask;
691 * For kmalloc object, the allocated memory size(object_size) is likely
692 * larger than the requested size(orig_size). If redzone check is
693 * enabled for the extra space, don't zero it, as it will be redzoned
694 * soon. The redzone operation for this extra space could be seen as a
695 * replacement of current poisoning under certain debug option, and
696 * won't break other sanity checks.
698 if (kmem_cache_debug_flags(s, SLAB_STORE_USER | SLAB_RED_ZONE) &&
699 (s->flags & SLAB_KMALLOC))
700 zero_size = orig_size;
703 * As memory initialization might be integrated into KASAN,
704 * kasan_slab_alloc and initialization memset must be
705 * kept together to avoid discrepancies in behavior.
707 * As p[i] might get tagged, memset and kmemleak hook come after KASAN.
709 for (i = 0; i < size; i++) {
710 p[i] = kasan_slab_alloc(s, p[i], flags, init);
711 if (p[i] && init && !kasan_has_integrated_init())
712 memset(p[i], 0, zero_size);
713 kmemleak_alloc_recursive(p[i], s->object_size, 1,
715 kmsan_slab_alloc(s, p[i], flags);
718 memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
722 * The slab lists for all objects.
724 struct kmem_cache_node {
726 raw_spinlock_t list_lock;
727 struct list_head slabs_partial; /* partial list first, better asm code */
728 struct list_head slabs_full;
729 struct list_head slabs_free;
730 unsigned long total_slabs; /* length of all slab lists */
731 unsigned long free_slabs; /* length of free slab list only */
732 unsigned long free_objects;
733 unsigned int free_limit;
734 unsigned int colour_next; /* Per-node cache coloring */
735 struct array_cache *shared; /* shared per node */
736 struct alien_cache **alien; /* on other nodes */
737 unsigned long next_reap; /* updated without locking */
738 int free_touched; /* updated without locking */
742 spinlock_t list_lock;
743 unsigned long nr_partial;
744 struct list_head partial;
745 #ifdef CONFIG_SLUB_DEBUG
746 atomic_long_t nr_slabs;
747 atomic_long_t total_objects;
748 struct list_head full;
754 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
756 return s->node[node];
760 * Iterator over all nodes. The body will be executed for each node that has
761 * a kmem_cache_node structure allocated (which is true for all online nodes)
763 #define for_each_kmem_cache_node(__s, __node, __n) \
764 for (__node = 0; __node < nr_node_ids; __node++) \
765 if ((__n = get_node(__s, __node)))
768 #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
769 void dump_unreclaimable_slab(void);
771 static inline void dump_unreclaimable_slab(void)
776 void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
778 #ifdef CONFIG_SLAB_FREELIST_RANDOM
779 int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
781 void cache_random_seq_destroy(struct kmem_cache *cachep);
783 static inline int cache_random_seq_create(struct kmem_cache *cachep,
784 unsigned int count, gfp_t gfp)
788 static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
789 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
791 static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
793 if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
797 if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
798 return flags & __GFP_ZERO;
801 return flags & __GFP_ZERO;
804 static inline bool slab_want_init_on_free(struct kmem_cache *c)
806 if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
809 (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
813 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
814 void debugfs_slab_release(struct kmem_cache *);
816 static inline void debugfs_slab_release(struct kmem_cache *s) { }
820 #define KS_ADDRS_COUNT 16
821 struct kmem_obj_info {
823 struct slab *kp_slab;
825 unsigned long kp_data_offset;
826 struct kmem_cache *kp_slab_cache;
828 void *kp_stack[KS_ADDRS_COUNT];
829 void *kp_free_stack[KS_ADDRS_COUNT];
831 void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
834 #ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR
835 void __check_heap_object(const void *ptr, unsigned long n,
836 const struct slab *slab, bool to_user);
839 void __check_heap_object(const void *ptr, unsigned long n,
840 const struct slab *slab, bool to_user)
845 #ifdef CONFIG_SLUB_DEBUG
846 void skip_orig_size_check(struct kmem_cache *s, const void *object);
849 #endif /* MM_SLAB_H */