xref: /linux/mm/slab.h (revision d755441b7672c74f29d1241ffdd2b067ecab1ca0)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef MM_SLAB_H
3 #define MM_SLAB_H
4 
5 #include <linux/reciprocal_div.h>
6 #include <linux/list_lru.h>
7 #include <linux/local_lock.h>
8 #include <linux/random.h>
9 #include <linux/kobject.h>
10 #include <linux/sched/mm.h>
11 #include <linux/memcontrol.h>
12 #include <linux/kfence.h>
13 #include <linux/kasan.h>
14 #include <linux/slab.h>
15 
16 /*
17  * Internal slab definitions
18  */
19 
20 /* slab's alloc_flags definitions */
21 #define SLAB_ALLOC_DEFAULT	0x00 /* no flags */
22 #define SLAB_ALLOC_NOLOCK	0x01 /* a kmalloc_nolock() allocation */
23 #define SLAB_ALLOC_NEW_SLAB	0x02 /* a flag for alloc_slab_obj_exts() */
24 #define SLAB_ALLOC_NO_RECURSE	0x04 /* prevent kmalloc() recursion */
25 #define SLAB_ALLOC_NO_OBJ_EXT	0x08 /* prevent obj_exts array allocation */
26 
27 #define SLAB_FREE_DEFAULT	0x00 /* no flags */
28 #define SLAB_FREE_NOLOCK	0x01 /* spinning not allowed */
29 
30 static inline unsigned int to_alloc_flags(unsigned int free_flags)
31 {
32 	if (free_flags & SLAB_FREE_NOLOCK)
33 		return SLAB_ALLOC_NOLOCK;
34 	else
35 		return SLAB_ALLOC_DEFAULT;
36 }
37 
38 static inline bool alloc_flags_allow_spinning(const unsigned int alloc_flags)
39 {
40 	return !(alloc_flags & SLAB_ALLOC_NOLOCK);
41 }
42 
43 static inline bool free_flags_allow_spinning(const unsigned int free_flags)
44 {
45 	return !(free_flags & SLAB_FREE_NOLOCK);
46 }
47 
48 void *__kmalloc_flags_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags,
49 				  unsigned int alloc_flags, int node)
50 				  __assume_kmalloc_alignment __alloc_size(1);
51 
52 static __always_inline __alloc_size(1) void *_kmalloc_flags_noprof(size_t size,
53 		gfp_t flags, unsigned int alloc_flags, int node, kmalloc_token_t token)
54 {
55 	return __kmalloc_flags_noprof(PASS_TOKEN_PARAMS(size, token), flags, alloc_flags, node);
56 }
57 #define kmalloc_flags_noprof(...)	_kmalloc_flags_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
58 #define kmalloc_flags(...)		alloc_hooks(kmalloc_flags_noprof(__VA_ARGS__))
59 
60 #ifdef CONFIG_64BIT
61 # ifdef system_has_cmpxchg128
62 # define system_has_freelist_aba()	system_has_cmpxchg128()
63 # define try_cmpxchg_freelist		try_cmpxchg128
64 # endif
65 typedef u128 freelist_full_t;
66 #else /* CONFIG_64BIT */
67 # ifdef system_has_cmpxchg64
68 # define system_has_freelist_aba()	system_has_cmpxchg64()
69 # define try_cmpxchg_freelist		try_cmpxchg64
70 # endif
71 typedef u64 freelist_full_t;
72 #endif /* CONFIG_64BIT */
73 
74 #if defined(system_has_freelist_aba) && !defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
75 #undef system_has_freelist_aba
76 #endif
77 
78 /*
79  * Freelist pointer and counter to cmpxchg together, avoids the typical ABA
80  * problems with cmpxchg of just a pointer.
81  */
82 struct freelist_counters {
83 	union {
84 		struct {
85 			void *freelist;
86 			union {
87 				unsigned long counters;
88 				struct {
89 					unsigned inuse:16;
90 					unsigned objects:15;
91 					/*
92 					 * If slab debugging is enabled then the
93 					 * frozen bit can be reused to indicate
94 					 * that the slab was corrupted
95 					 */
96 					unsigned frozen:1;
97 #ifdef CONFIG_64BIT
98 					/*
99 					 * Some optimizations use free bits in 'counters' field
100 					 * to save memory or CPU. If these free bits are not
101 					 * available, such optimizations are disabled.
102 					 */
103 					unsigned obj_exts_in_object:1;
104 					unsigned obj_exts_needs_objcg:1;
105 #endif
106 				};
107 			};
108 		};
109 #ifdef system_has_freelist_aba
110 		freelist_full_t freelist_counters;
111 #endif
112 	};
113 };
114 
115 /* Reuses the bits in struct page */
116 struct slab {
117 	memdesc_flags_t flags;
118 
119 	struct kmem_cache *slab_cache;
120 	union {
121 		struct {
122 			struct list_head slab_list;
123 			/* Double-word boundary */
124 			struct freelist_counters;
125 		};
126 		struct rcu_head rcu_head;
127 	};
128 
129 	unsigned int __page_type;
130 	atomic_t __page_refcount;
131 #ifdef CONFIG_SLAB_OBJ_EXT
132 	unsigned long obj_exts;
133 #endif
134 };
135 
136 #define SLAB_MATCH(pg, sl)						\
137 	static_assert(offsetof(struct page, pg) == offsetof(struct slab, sl))
138 SLAB_MATCH(flags, flags);
139 SLAB_MATCH(compound_info, slab_cache);	/* Ensure bit 0 is clear */
140 SLAB_MATCH(_refcount, __page_refcount);
141 #ifdef CONFIG_MEMCG
142 SLAB_MATCH(memcg_data, obj_exts);
143 #elif defined(CONFIG_SLAB_OBJ_EXT)
144 SLAB_MATCH(_unused_slab_obj_exts, obj_exts);
145 #endif
146 #undef SLAB_MATCH
147 static_assert(sizeof(struct slab) <= sizeof(struct page));
148 #if defined(system_has_freelist_aba)
149 static_assert(IS_ALIGNED(offsetof(struct slab, freelist), sizeof(struct freelist_counters)));
150 #endif
151 
152 /**
153  * page_slab - Converts from struct page to its slab.
154  * @page: A page which may or may not belong to a slab.
155  *
156  * Return: The slab which contains this page or NULL if the page does
157  * not belong to a slab.  This includes pages returned from large kmalloc.
158  */
159 static inline struct slab *page_slab(const struct page *page)
160 {
161 	page = compound_head(page);
162 	if (data_race(page->page_type >> 24) != PGTY_slab)
163 		page = NULL;
164 
165 	return (struct slab *)page;
166 }
167 
168 /**
169  * slab_page - The first struct page allocated for a slab
170  * @s: The slab.
171  *
172  * A convenience wrapper for returning the first page allocated to this slab.
173  * This is for the benefit of code not yet converted to struct slab.
174  */
175 #define slab_page(s) 		(_Generic((s),				\
176 	const struct slab *:	(const struct page *)s,			\
177 	struct slab *:		(struct page *)s))
178 
179 static inline void *slab_address(const struct slab *slab)
180 {
181 	return page_address(slab_page(slab));
182 }
183 
184 static inline int slab_nid(const struct slab *slab)
185 {
186 	return memdesc_nid(&slab->flags);
187 }
188 
189 static inline pg_data_t *slab_pgdat(const struct slab *slab)
190 {
191 	return NODE_DATA(slab_nid(slab));
192 }
193 
194 static inline struct slab *virt_to_slab(const void *addr)
195 {
196 	return page_slab(virt_to_page(addr));
197 }
198 
199 static inline int slab_order(const struct slab *slab)
200 {
201 	return compound_order(slab_page(slab));
202 }
203 
204 static inline size_t slab_size(const struct slab *slab)
205 {
206 	return PAGE_SIZE << slab_order(slab);
207 }
208 
209 /*
210  * Word size structure that can be atomically updated or read and that
211  * contains both the order and the number of objects that a slab of the
212  * given order would contain.
213  */
214 struct kmem_cache_order_objects {
215 	unsigned int x;
216 };
217 
218 struct kmem_cache_per_node_ptrs {
219 	struct node_barn *barn;
220 	struct kmem_cache_node *node;
221 };
222 
223 /*
224  * Slab cache management.
225  */
226 struct kmem_cache {
227 	struct slub_percpu_sheaves __percpu *cpu_sheaves;
228 	/* Used for retrieving partial slabs, etc. */
229 	slab_flags_t flags;
230 	unsigned long min_partial;
231 	unsigned int size;		/* Object size including metadata */
232 	unsigned int object_size;	/* Object size without metadata */
233 	struct reciprocal_value reciprocal_size;
234 	unsigned int offset;		/* Free pointer offset */
235 	unsigned int sheaf_capacity;
236 	struct kmem_cache_order_objects oo;
237 
238 	/* Allocation and freeing of slabs */
239 	struct kmem_cache_order_objects min;
240 	gfp_t allocflags;		/* gfp flags to use on each alloc */
241 	int refcount;			/* Refcount for slab cache destroy */
242 	void (*ctor)(void *object);	/* Object constructor */
243 	unsigned int inuse;		/* Offset to metadata */
244 	unsigned int align;		/* Alignment */
245 	unsigned int red_left_pad;	/* Left redzone padding size */
246 	const char *name;		/* Name (only for display!) */
247 	struct list_head list;		/* List of slab caches */
248 #ifdef CONFIG_SYSFS
249 	struct kobject kobj;		/* For sysfs */
250 #endif
251 #ifdef CONFIG_SLAB_FREELIST_HARDENED
252 	unsigned long random;
253 #endif
254 
255 #ifdef CONFIG_NUMA
256 	/*
257 	 * Defragmentation by allocating from a remote node.
258 	 */
259 	unsigned int remote_node_defrag_ratio;
260 #endif
261 
262 #ifdef CONFIG_SLAB_FREELIST_RANDOM
263 	unsigned int *random_seq;
264 #endif
265 
266 #ifdef CONFIG_KASAN_GENERIC
267 	struct kasan_cache kasan_info;
268 #endif
269 
270 #ifdef CONFIG_HARDENED_USERCOPY
271 	unsigned int useroffset;	/* Usercopy region offset */
272 	unsigned int usersize;		/* Usercopy region size */
273 #endif
274 
275 #ifdef CONFIG_SLUB_STATS
276 	struct kmem_cache_stats __percpu *cpu_stats;
277 #endif
278 
279 	struct kmem_cache_per_node_ptrs per_node[MAX_NUMNODES];
280 };
281 
282 /*
283  * Every cache has !NULL s->cpu_sheaves but they may point to the
284  * bootstrap_sheaf temporarily during init, or permanently for the boot caches
285  * and caches with debugging enabled, or all caches with CONFIG_SLUB_TINY. This
286  * helper distinguishes whether cache has real non-bootstrap sheaves.
287  */
288 static inline bool cache_has_sheaves(struct kmem_cache *s)
289 {
290 	/* Test CONFIG_SLUB_TINY for code elimination purposes */
291 	return !IS_ENABLED(CONFIG_SLUB_TINY) && s->sheaf_capacity;
292 }
293 
294 #if defined(CONFIG_SYSFS) && !defined(CONFIG_SLUB_TINY)
295 #define SLAB_SUPPORTS_SYSFS 1
296 void sysfs_slab_unlink(struct kmem_cache *s);
297 void sysfs_slab_release(struct kmem_cache *s);
298 int sysfs_slab_alias(struct kmem_cache *s, const char *name);
299 #else
300 static inline void sysfs_slab_unlink(struct kmem_cache *s) { }
301 static inline void sysfs_slab_release(struct kmem_cache *s) { }
302 static inline int sysfs_slab_alias(struct kmem_cache *s, const char *name)
303 							{ return 0; }
304 #endif
305 
306 void *fixup_red_left(struct kmem_cache *s, void *p);
307 
308 static inline void *nearest_obj(struct kmem_cache *cache,
309 				const struct slab *slab, void *x)
310 {
311 	void *object = x - (x - slab_address(slab)) % cache->size;
312 	void *last_object = slab_address(slab) +
313 		(slab->objects - 1) * cache->size;
314 	void *result = (unlikely(object > last_object)) ? last_object : object;
315 
316 	result = fixup_red_left(cache, result);
317 	return result;
318 }
319 
320 /* Determine object index from a given position */
321 static inline unsigned int __obj_to_index(const struct kmem_cache *cache,
322 					  void *addr, const void *obj)
323 {
324 	return reciprocal_divide(kasan_reset_tag(obj) - addr,
325 				 cache->reciprocal_size);
326 }
327 
328 static inline unsigned int obj_to_index(const struct kmem_cache *cache,
329 					const struct slab *slab, const void *obj)
330 {
331 	if (is_kfence_address(obj))
332 		return 0;
333 	return __obj_to_index(cache, slab_address(slab), obj);
334 }
335 
336 /*
337  * kvfree_rcu_head offset can be only less than page size.
338  * Calculate the start address while preserving the KASAN tag.
339  */
340 static inline void *kvmalloc_obj_start_addr(void *head)
341 {
342 	unsigned long offset;
343 
344 	if (unlikely(is_vmalloc_addr(head))) {
345 		offset = offset_in_page(head);
346 	} else {
347 		struct slab *slab = virt_to_slab(head);
348 
349 		if (!slab) {
350 			offset = offset_in_page(head);
351 		} else if (is_kfence_address(head)) {
352 			offset = head - kfence_object_start(head);
353 		} else {
354 			struct kmem_cache *s = slab->slab_cache;
355 			unsigned int idx = __obj_to_index(s, slab_address(slab), head);
356 			void *obj = slab_address(slab) + s->size * idx;
357 
358 			obj = fixup_red_left(s, obj);
359 			obj = kasan_reset_tag(obj);
360 			offset = kasan_reset_tag(head) - obj;
361 		}
362 	}
363 
364 	return head - offset;
365 }
366 
367 /*
368  * State of the slab allocator.
369  *
370  * This is used to describe the states of the allocator during bootup.
371  * Allocators use this to gradually bootstrap themselves. Most allocators
372  * have the problem that the structures used for managing slab caches are
373  * allocated from slab caches themselves.
374  */
375 enum slab_state {
376 	DOWN,			/* No slab functionality yet */
377 	PARTIAL,		/* SLUB: kmem_cache_node available */
378 	UP,			/* Slab caches usable but not all extras yet */
379 	FULL			/* Everything is working */
380 };
381 
382 extern enum slab_state slab_state;
383 
384 /* The slab cache mutex protects the management structures during changes */
385 extern struct mutex slab_mutex;
386 
387 /* The list of all slab caches on the system */
388 extern struct list_head slab_caches;
389 
390 /* The slab cache that manages slab cache information */
391 extern struct kmem_cache *kmem_cache;
392 
393 /* A table of kmalloc cache names and sizes */
394 extern const struct kmalloc_info_struct {
395 	const char *name[NR_KMALLOC_TYPES];
396 	unsigned int size;
397 } kmalloc_info[];
398 
399 /* Kmalloc array related functions */
400 void setup_kmalloc_cache_index_table(void);
401 void create_kmalloc_caches(void);
402 
403 extern u8 kmalloc_size_index[24];
404 
405 static inline unsigned int size_index_elem(unsigned int bytes)
406 {
407 	return (bytes - 1) / 8;
408 }
409 
410 /*
411  * Find the kmem_cache structure that serves a given size of
412  * allocation
413  *
414  * This assumes size is larger than zero and not larger than
415  * KMALLOC_MAX_CACHE_SIZE and the caller must check that.
416  */
417 static inline struct kmem_cache *
418 kmalloc_slab(size_t size, kmem_buckets *b, gfp_t flags, kmalloc_token_t token,
419 	     unsigned int alloc_flags)
420 {
421 	unsigned int index;
422 	enum kmalloc_cache_type type = kmalloc_type(flags, token);
423 
424 	if (alloc_flags & SLAB_ALLOC_NO_OBJ_EXT)
425 		type = KMALLOC_NO_OBJ_EXT;
426 
427 	if (!b)
428 		b = &kmalloc_caches[type];
429 	if (size <= 192)
430 		index = kmalloc_size_index[size_index_elem(size)];
431 	else
432 		index = fls(size - 1);
433 
434 	return (*b)[index];
435 }
436 
437 gfp_t kmalloc_fix_flags(gfp_t flags);
438 
439 /* Functions provided by the slab allocators */
440 int do_kmem_cache_create(struct kmem_cache *s, const char *name,
441 			 unsigned int size, struct kmem_cache_args *args,
442 			 slab_flags_t flags);
443 
444 void __init kmem_cache_init(void);
445 extern void create_boot_cache(struct kmem_cache *, const char *name,
446 			unsigned int size, slab_flags_t flags,
447 			unsigned int useroffset, unsigned int usersize);
448 
449 int slab_unmergeable(struct kmem_cache *s);
450 bool slab_args_unmergeable(struct kmem_cache_args *args, slab_flags_t flags);
451 
452 slab_flags_t kmem_cache_flags(slab_flags_t flags, const char *name);
453 
454 static inline bool is_kmalloc_cache(struct kmem_cache *s)
455 {
456 	return (s->flags & SLAB_KMALLOC);
457 }
458 
459 static inline bool is_kmalloc_normal(struct kmem_cache *s)
460 {
461 	if (!is_kmalloc_cache(s))
462 		return false;
463 
464 	return !(s->flags & (SLAB_CACHE_DMA|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT|SLAB_NO_OBJ_EXT));
465 }
466 
467 bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, unsigned int free_flags);
468 void flush_all_rcu_sheaves(void);
469 void flush_rcu_sheaves_on_cache(struct kmem_cache *s);
470 
471 #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
472 			 SLAB_CACHE_DMA32 | SLAB_PANIC | \
473 			 SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS | \
474 			 SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
475 			 SLAB_TEMPORARY | SLAB_ACCOUNT | \
476 			 SLAB_NO_USER_FLAGS | SLAB_KMALLOC | SLAB_NO_MERGE)
477 
478 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
479 			  SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
480 
481 #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS)
482 
483 bool __kmem_cache_empty(struct kmem_cache *);
484 int __kmem_cache_shutdown(struct kmem_cache *);
485 void __kmem_cache_release(struct kmem_cache *);
486 int __kmem_cache_shrink(struct kmem_cache *);
487 void slab_kmem_cache_release(struct kmem_cache *);
488 
489 struct seq_file;
490 struct file;
491 
492 struct slabinfo {
493 	unsigned long active_objs;
494 	unsigned long num_objs;
495 	unsigned long active_slabs;
496 	unsigned long num_slabs;
497 	unsigned long shared_avail;
498 	unsigned int limit;
499 	unsigned int batchcount;
500 	unsigned int shared;
501 	unsigned int objects_per_slab;
502 	unsigned int cache_order;
503 };
504 
505 void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
506 
507 #ifdef CONFIG_SLUB_DEBUG
508 #ifdef CONFIG_SLUB_DEBUG_ON
509 DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
510 #else
511 DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
512 #endif
513 extern void print_tracking(struct kmem_cache *s, void *object);
514 long validate_slab_cache(struct kmem_cache *s);
515 static inline bool __slub_debug_enabled(void)
516 {
517 	return static_branch_unlikely(&slub_debug_enabled);
518 }
519 #else
520 static inline void print_tracking(struct kmem_cache *s, void *object)
521 {
522 }
523 static inline bool __slub_debug_enabled(void)
524 {
525 	return false;
526 }
527 #endif
528 
529 /*
530  * Returns true if any of the specified slab_debug flags is enabled for the
531  * cache. Use only for flags parsed by setup_slub_debug() as it also enables
532  * the static key.
533  */
534 static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
535 {
536 	if (IS_ENABLED(CONFIG_SLUB_DEBUG))
537 		VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
538 	if (__slub_debug_enabled())
539 		return s->flags & flags;
540 	return false;
541 }
542 
543 #if IS_ENABLED(CONFIG_SLUB_DEBUG) && IS_ENABLED(CONFIG_KUNIT)
544 bool slab_in_kunit_test(void);
545 #else
546 static inline bool slab_in_kunit_test(void) { return false; }
547 #endif
548 
549 /*
550  * slub is about to manipulate internal object metadata.  This memory lies
551  * outside the range of the allocated object, so accessing it would normally
552  * be reported by kasan as a bounds error.  metadata_access_enable() is used
553  * to tell kasan that these accesses are OK.
554  */
555 static inline void metadata_access_enable(void)
556 {
557 	kasan_disable_current();
558 	kmsan_disable_current();
559 }
560 
561 static inline void metadata_access_disable(void)
562 {
563 	kmsan_enable_current();
564 	kasan_enable_current();
565 }
566 
567 /*
568  * Return true if KMALLOC_NORMAL caches may need obj_exts arrays.
569  *
570  * Memory allocation profiling requires obj_exts for all caches.
571  * Memcg usually doesn't need them for normal kmalloc caches, but kmalloc types
572  * with a priority higher than KMALLOC_CGROUP can be aliased with KMALLOC_NORMAL.
573  */
574 static inline bool need_kmalloc_no_objext(void)
575 {
576 	if (!mem_alloc_profiling_permanently_disabled())
577 		return true;
578 
579 	if (!mem_cgroup_kmem_disabled() &&
580 			(KMALLOC_NORMAL == KMALLOC_RECLAIM))
581 		return true;
582 
583 	return false;
584 }
585 
586 /*
587  * Extended information for slab objects stored as a pointer to an array in
588  * slab->obj_exts (aliasing page->memcg_data) if MEMCG_DATA_OBJEXTS is set.
589  */
590 struct slabobj_ext {
591 	/*
592 	 * All elements of the union should be pointer-sized to avoid memory
593 	 * waste
594 	 */
595 	union {
596 #ifdef CONFIG_MEMCG
597 		struct obj_cgroup *_objcg;
598 #endif
599 #ifdef CONFIG_MEM_ALLOC_PROFILING
600 		union codetag_ref _ctref;
601 #endif
602 	};
603 } __aligned(8);
604 
605 #ifdef CONFIG_MEM_ALLOC_PROFILING
606 DECLARE_STATIC_KEY_MAYBE(CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT,
607 			 slab_obj_ext_has_codetag_key);
608 
609 static inline bool slab_obj_ext_has_codetag(void)
610 {
611 	return static_branch_maybe(CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT,
612 				   &slab_obj_ext_has_codetag_key);
613 }
614 #else
615 static inline bool slab_obj_ext_has_codetag(void)
616 {
617 	return false;
618 }
619 #endif
620 
621 #ifdef CONFIG_MEMCG
622 static inline bool cache_needs_objcg(struct kmem_cache *cache)
623 {
624 	return (cache->flags & SLAB_MAY_ACCOUNT);
625 }
626 
627 static inline bool slab_needs_objcg(struct slab *slab)
628 {
629 #ifdef CONFIG_64BIT
630 	return slab->obj_exts_needs_objcg;
631 #else
632 	return cache_needs_objcg(slab->slab_cache);
633 #endif
634 }
635 #else
636 static inline bool cache_needs_objcg(struct kmem_cache *cache)
637 {
638 	return false;
639 }
640 
641 static inline bool slab_needs_objcg(struct slab *slab)
642 {
643 	return false;
644 }
645 #endif
646 
647 static inline size_t cache_obj_ext_size(struct kmem_cache *s)
648 {
649 	size_t sz = 0;
650 
651 	if (cache_needs_objcg(s))
652 		sz += 1;
653 
654 	if (slab_obj_ext_has_codetag())
655 		sz += 1;
656 
657 	return sizeof(struct slabobj_ext) * sz;
658 }
659 
660 static inline size_t slab_obj_ext_size(struct slab *slab)
661 {
662 	size_t sz = 0;
663 
664 	if (slab_needs_objcg(slab))
665 		sz += 1;
666 
667 	if (slab_obj_ext_has_codetag())
668 		sz += 1;
669 
670 	return sizeof(struct slabobj_ext) * sz;
671 }
672 
673 #ifdef CONFIG_SLAB_OBJ_EXT
674 
675 /*
676  * slab_obj_exts - get the pointer to the slab object extension vector
677  * associated with a slab.
678  * @slab: a pointer to the slab struct
679  *
680  * Returns the address of the object extension vector associated with the slab,
681  * or zero if no such vector has been associated yet.
682  * Do not dereference the return value directly; use get/put_slab_obj_exts()
683  * pair and slab_obj_ext() to access individual elements.
684  *
685  * Example usage:
686  *
687  * obj_exts = slab_obj_exts(slab);
688  * if (obj_exts) {
689  *         get_slab_obj_exts(obj_exts);
690  *         obj_ext = slab_obj_ext(s, slab, obj_exts, obj);
691  *         // do something with obj_ext
692  *         put_slab_obj_exts(obj_exts);
693  * }
694  *
695  * Note that the get/put semantics does not involve reference counting.
696  * Instead, it updates kasan/kmsan depth so that accesses to slabobj_ext
697  * won't be reported as access violations.
698  */
699 static inline unsigned long slab_obj_exts(struct slab *slab)
700 {
701 	unsigned long obj_exts = READ_ONCE(slab->obj_exts);
702 
703 #ifdef CONFIG_MEMCG
704 	/*
705 	 * obj_exts should be either NULL, a valid pointer with
706 	 * MEMCG_DATA_OBJEXTS bit set or be equal to OBJEXTS_ALLOC_FAIL.
707 	 */
708 	VM_BUG_ON_PAGE(obj_exts && !(obj_exts & MEMCG_DATA_OBJEXTS) &&
709 		       obj_exts != OBJEXTS_ALLOC_FAIL, slab_page(slab));
710 	VM_BUG_ON_PAGE(obj_exts & MEMCG_DATA_KMEM, slab_page(slab));
711 #endif
712 
713 	return obj_exts & ~OBJEXTS_FLAGS_MASK;
714 }
715 
716 static inline void get_slab_obj_exts(unsigned long obj_exts)
717 {
718 	VM_WARN_ON_ONCE(!obj_exts);
719 	metadata_access_enable();
720 }
721 
722 static inline void put_slab_obj_exts(unsigned long obj_exts)
723 {
724 	metadata_access_disable();
725 }
726 
727 #ifdef CONFIG_64BIT
728 static inline bool obj_exts_in_object(struct slab *slab)
729 {
730 	/*
731 	 * Note we cannot rely on the SLAB_OBJ_EXT_IN_OBJ flag here and need to
732 	 * check the per-slab bit. A cache can have SLAB_OBJ_EXT_IN_OBJ set, but
733 	 * allocations within_slab_leftover are preferred. And those may be
734 	 * possible or not depending on the particular slab's size.
735 	 */
736 	return slab->obj_exts_in_object;
737 }
738 #else
739 static inline bool obj_exts_in_object(struct slab *slab)
740 {
741 	return false;
742 }
743 #endif
744 
745 /*
746  * slab_obj_ext - get the pointer to the slab object extension metadata
747  * associated with an object in a slab.
748  * @s: cache that the slab belongs to
749  * @slab: a pointer to the slab struct
750  * @obj_exts: a pointer to the object extension vector
751  * @obj: a pointer to the object
752  *
753  * Returns a pointer to the object extension associated with the object.
754  * Must be called within a section covered by get/put_slab_obj_exts().
755  */
756 static inline struct slabobj_ext *
757 slab_obj_ext(struct kmem_cache *s, struct slab *slab, unsigned long obj_exts,
758 	     const void *obj)
759 {
760 	struct slabobj_ext *obj_ext;
761 	unsigned int index;
762 	unsigned int stride;
763 
764 	VM_WARN_ON_ONCE(obj_exts != slab_obj_exts(slab));
765 
766 	/*
767 	 * KFENCE objects have NULL obj_exts and thus can't reach this
768 	 * and we don't need obj_to_index()
769 	 */
770 	index = __obj_to_index(s, slab_address(slab), obj);
771 
772 	if (!obj_exts_in_object(slab))
773 		stride = slab_obj_ext_size(slab);
774 	else
775 		stride = s->size;
776 
777 	obj_ext = (struct slabobj_ext *)(obj_exts + index * stride);
778 
779 	return kasan_reset_tag(obj_ext);
780 }
781 
782 #ifdef CONFIG_MEMCG
783 static inline struct obj_cgroup *
784 slab_obj_ext_objcg(struct slab *slab, struct slabobj_ext *obj_ext)
785 {
786 	VM_WARN_ON_ONCE(!slab_needs_objcg(slab));
787 
788 	/* if objcg exists, it comes first, so we don't need to do anything */
789 	return obj_ext->_objcg;
790 }
791 
792 static inline void
793 slab_obj_ext_set_objcg(struct slab *slab, struct slabobj_ext *obj_ext,
794 		       struct obj_cgroup *objcg)
795 {
796 	VM_WARN_ON_ONCE(!slab_needs_objcg(slab));
797 
798 	/* if objcg exists, it comes first, so we don't need to do anything */
799 	obj_ext->_objcg = objcg;
800 }
801 #endif
802 
803 #ifdef CONFIG_MEM_ALLOC_PROFILING
804 static inline union codetag_ref *
805 slab_obj_ext_codetag_ref(struct slab *slab, struct slabobj_ext *obj_ext)
806 {
807 	VM_WARN_ON_ONCE(!slab_obj_ext_has_codetag());
808 
809 	if (slab_needs_objcg(slab))
810 		obj_ext += 1;
811 
812 	return &obj_ext->_ctref;
813 }
814 #endif
815 
816 int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,
817 			gfp_t gfp, unsigned int alloc_flags);
818 
819 #else /* CONFIG_SLAB_OBJ_EXT */
820 
821 static inline unsigned long slab_obj_exts(struct slab *slab)
822 {
823 	return 0;
824 }
825 
826 static inline struct slabobj_ext *
827 slab_obj_ext(struct kmem_cache *s, struct slab *slab, unsigned long obj_exts,
828 	     const void *obj)
829 {
830 	return NULL;
831 }
832 
833 static inline bool obj_exts_in_object(struct slab *slab)
834 {
835 	return false;
836 }
837 
838 #endif /* CONFIG_SLAB_OBJ_EXT */
839 
840 static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s)
841 {
842 	return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
843 		NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
844 }
845 
846 #ifdef CONFIG_MEMCG
847 bool __memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru,
848 				  gfp_t flags, unsigned int slab_alloc_flags,
849 				  size_t size, void **p);
850 void __memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
851 			    void **p, int objects, unsigned long obj_exts);
852 #endif
853 
854 void kvfree_rcu_cb(struct rcu_head *head);
855 
856 static inline unsigned int large_kmalloc_order(const struct page *page)
857 {
858 	return page[1].flags.f & 0xff;
859 }
860 
861 static inline size_t large_kmalloc_size(const struct page *page)
862 {
863 	return PAGE_SIZE << large_kmalloc_order(page);
864 }
865 
866 #ifdef CONFIG_SLUB_DEBUG
867 void dump_unreclaimable_slab(void);
868 #else
869 static inline void dump_unreclaimable_slab(void)
870 {
871 }
872 #endif
873 
874 void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
875 
876 #ifdef CONFIG_SLAB_FREELIST_RANDOM
877 int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
878 			gfp_t gfp);
879 void cache_random_seq_destroy(struct kmem_cache *cachep);
880 #else
881 static inline int cache_random_seq_create(struct kmem_cache *cachep,
882 					unsigned int count, gfp_t gfp)
883 {
884 	return 0;
885 }
886 static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
887 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
888 
889 static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
890 {
891 	if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
892 				&init_on_alloc)) {
893 		if (c->ctor)
894 			return false;
895 		if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
896 			return flags & __GFP_ZERO;
897 		return true;
898 	}
899 	return flags & __GFP_ZERO;
900 }
901 
902 static inline bool slab_want_init_on_free(struct kmem_cache *c)
903 {
904 	if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
905 				&init_on_free))
906 		return !(c->ctor ||
907 			 (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
908 	return false;
909 }
910 
911 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
912 void debugfs_slab_release(struct kmem_cache *);
913 #else
914 static inline void debugfs_slab_release(struct kmem_cache *s) { }
915 #endif
916 
917 #ifdef CONFIG_PRINTK
918 #define KS_ADDRS_COUNT 16
919 struct kmem_obj_info {
920 	void *kp_ptr;
921 	struct slab *kp_slab;
922 	void *kp_objp;
923 	unsigned long kp_data_offset;
924 	struct kmem_cache *kp_slab_cache;
925 	void *kp_ret;
926 	void *kp_stack[KS_ADDRS_COUNT];
927 	void *kp_free_stack[KS_ADDRS_COUNT];
928 };
929 void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
930 #endif
931 
932 void __check_heap_object(const void *ptr, unsigned long n,
933 			 const struct slab *slab, bool to_user);
934 
935 void deferred_work_barrier(void);
936 void defer_kfree_rcu(struct kvfree_rcu_head *head);
937 
938 static inline bool slub_debug_orig_size(struct kmem_cache *s)
939 {
940 	return (kmem_cache_debug_flags(s, SLAB_STORE_USER) &&
941 			(s->flags & SLAB_KMALLOC));
942 }
943 
944 #ifdef CONFIG_SLUB_DEBUG
945 void skip_orig_size_check(struct kmem_cache *s, const void *object);
946 #endif
947 
948 #endif /* MM_SLAB_H */
949