xref: /linux/mm/slab.h (revision 30222639602c89ddc52208ac6c9d7baed376c84a)
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 
26 static inline bool alloc_flags_allow_spinning(const unsigned int alloc_flags)
27 {
28 	return !(alloc_flags & SLAB_ALLOC_NOLOCK);
29 }
30 
31 void *__kmalloc_flags_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags,
32 				  unsigned int alloc_flags, int node)
33 				  __assume_kmalloc_alignment __alloc_size(1);
34 
35 static __always_inline __alloc_size(1) void *_kmalloc_flags_noprof(size_t size,
36 		gfp_t flags, unsigned int alloc_flags, int node, kmalloc_token_t token)
37 {
38 	return __kmalloc_flags_noprof(PASS_TOKEN_PARAMS(size, token), flags, alloc_flags, node);
39 }
40 #define kmalloc_flags_noprof(...)	_kmalloc_flags_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
41 #define kmalloc_flags(...)		alloc_hooks(kmalloc_flags_noprof(__VA_ARGS__))
42 
43 #ifdef CONFIG_64BIT
44 # ifdef system_has_cmpxchg128
45 # define system_has_freelist_aba()	system_has_cmpxchg128()
46 # define try_cmpxchg_freelist		try_cmpxchg128
47 # endif
48 typedef u128 freelist_full_t;
49 #else /* CONFIG_64BIT */
50 # ifdef system_has_cmpxchg64
51 # define system_has_freelist_aba()	system_has_cmpxchg64()
52 # define try_cmpxchg_freelist		try_cmpxchg64
53 # endif
54 typedef u64 freelist_full_t;
55 #endif /* CONFIG_64BIT */
56 
57 #if defined(system_has_freelist_aba) && !defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
58 #undef system_has_freelist_aba
59 #endif
60 
61 /*
62  * Freelist pointer and counter to cmpxchg together, avoids the typical ABA
63  * problems with cmpxchg of just a pointer.
64  */
65 struct freelist_counters {
66 	union {
67 		struct {
68 			void *freelist;
69 			union {
70 				unsigned long counters;
71 				struct {
72 					unsigned inuse:16;
73 					unsigned objects:15;
74 					/*
75 					 * If slab debugging is enabled then the
76 					 * frozen bit can be reused to indicate
77 					 * that the slab was corrupted
78 					 */
79 					unsigned frozen:1;
80 #ifdef CONFIG_64BIT
81 					/*
82 					 * Some optimizations use free bits in 'counters' field
83 					 * to save memory. In case ->stride field is not available,
84 					 * such optimizations are disabled.
85 					 */
86 					unsigned int stride;
87 #endif
88 				};
89 			};
90 		};
91 #ifdef system_has_freelist_aba
92 		freelist_full_t freelist_counters;
93 #endif
94 	};
95 };
96 
97 /* Reuses the bits in struct page */
98 struct slab {
99 	memdesc_flags_t flags;
100 
101 	struct kmem_cache *slab_cache;
102 	union {
103 		struct {
104 			struct list_head slab_list;
105 			/* Double-word boundary */
106 			struct freelist_counters;
107 		};
108 		struct rcu_head rcu_head;
109 	};
110 
111 	unsigned int __page_type;
112 	atomic_t __page_refcount;
113 #ifdef CONFIG_SLAB_OBJ_EXT
114 	unsigned long obj_exts;
115 #endif
116 };
117 
118 #define SLAB_MATCH(pg, sl)						\
119 	static_assert(offsetof(struct page, pg) == offsetof(struct slab, sl))
120 SLAB_MATCH(flags, flags);
121 SLAB_MATCH(compound_info, slab_cache);	/* Ensure bit 0 is clear */
122 SLAB_MATCH(_refcount, __page_refcount);
123 #ifdef CONFIG_MEMCG
124 SLAB_MATCH(memcg_data, obj_exts);
125 #elif defined(CONFIG_SLAB_OBJ_EXT)
126 SLAB_MATCH(_unused_slab_obj_exts, obj_exts);
127 #endif
128 #undef SLAB_MATCH
129 static_assert(sizeof(struct slab) <= sizeof(struct page));
130 #if defined(system_has_freelist_aba)
131 static_assert(IS_ALIGNED(offsetof(struct slab, freelist), sizeof(struct freelist_counters)));
132 #endif
133 
134 /**
135  * slab_folio - The folio allocated for a slab
136  * @s: The slab.
137  *
138  * Slabs are allocated as folios that contain the individual objects and are
139  * using some fields in the first struct page of the folio - those fields are
140  * now accessed by struct slab. It is occasionally necessary to convert back to
141  * a folio in order to communicate with the rest of the mm.  Please use this
142  * helper function instead of casting yourself, as the implementation may change
143  * in the future.
144  */
145 #define slab_folio(s)		(_Generic((s),				\
146 	const struct slab *:	(const struct folio *)s,		\
147 	struct slab *:		(struct folio *)s))
148 
149 /**
150  * page_slab - Converts from struct page to its slab.
151  * @page: A page which may or may not belong to a slab.
152  *
153  * Return: The slab which contains this page or NULL if the page does
154  * not belong to a slab.  This includes pages returned from large kmalloc.
155  */
156 static inline struct slab *page_slab(const struct page *page)
157 {
158 	page = compound_head(page);
159 	if (data_race(page->page_type >> 24) != PGTY_slab)
160 		page = NULL;
161 
162 	return (struct slab *)page;
163 }
164 
165 /**
166  * slab_page - The first struct page allocated for a slab
167  * @s: The slab.
168  *
169  * A convenience wrapper for converting slab to the first struct page of the
170  * underlying folio, to communicate with code not yet converted to folio or
171  * struct slab.
172  */
173 #define slab_page(s) folio_page(slab_folio(s), 0)
174 
175 static inline void *slab_address(const struct slab *slab)
176 {
177 	return folio_address(slab_folio(slab));
178 }
179 
180 static inline int slab_nid(const struct slab *slab)
181 {
182 	return memdesc_nid(slab->flags);
183 }
184 
185 static inline pg_data_t *slab_pgdat(const struct slab *slab)
186 {
187 	return NODE_DATA(slab_nid(slab));
188 }
189 
190 static inline struct slab *virt_to_slab(const void *addr)
191 {
192 	return page_slab(virt_to_page(addr));
193 }
194 
195 static inline int slab_order(const struct slab *slab)
196 {
197 	return folio_order(slab_folio(slab));
198 }
199 
200 static inline size_t slab_size(const struct slab *slab)
201 {
202 	return PAGE_SIZE << slab_order(slab);
203 }
204 
205 /*
206  * Word size structure that can be atomically updated or read and that
207  * contains both the order and the number of objects that a slab of the
208  * given order would contain.
209  */
210 struct kmem_cache_order_objects {
211 	unsigned int x;
212 };
213 
214 struct kmem_cache_per_node_ptrs {
215 	struct node_barn *barn;
216 	struct kmem_cache_node *node;
217 };
218 
219 /*
220  * Slab cache management.
221  */
222 struct kmem_cache {
223 	struct slub_percpu_sheaves __percpu *cpu_sheaves;
224 	/* Used for retrieving partial slabs, etc. */
225 	slab_flags_t flags;
226 	unsigned long min_partial;
227 	unsigned int size;		/* Object size including metadata */
228 	unsigned int object_size;	/* Object size without metadata */
229 	struct reciprocal_value reciprocal_size;
230 	unsigned int offset;		/* Free pointer offset */
231 	unsigned int sheaf_capacity;
232 	struct kmem_cache_order_objects oo;
233 
234 	/* Allocation and freeing of slabs */
235 	struct kmem_cache_order_objects min;
236 	gfp_t allocflags;		/* gfp flags to use on each alloc */
237 	int refcount;			/* Refcount for slab cache destroy */
238 	void (*ctor)(void *object);	/* Object constructor */
239 	unsigned int inuse;		/* Offset to metadata */
240 	unsigned int align;		/* Alignment */
241 	unsigned int red_left_pad;	/* Left redzone padding size */
242 	const char *name;		/* Name (only for display!) */
243 	struct list_head list;		/* List of slab caches */
244 #ifdef CONFIG_SYSFS
245 	struct kobject kobj;		/* For sysfs */
246 #endif
247 #ifdef CONFIG_SLAB_FREELIST_HARDENED
248 	unsigned long random;
249 #endif
250 
251 #ifdef CONFIG_NUMA
252 	/*
253 	 * Defragmentation by allocating from a remote node.
254 	 */
255 	unsigned int remote_node_defrag_ratio;
256 #endif
257 
258 #ifdef CONFIG_SLAB_FREELIST_RANDOM
259 	unsigned int *random_seq;
260 #endif
261 
262 #ifdef CONFIG_KASAN_GENERIC
263 	struct kasan_cache kasan_info;
264 #endif
265 
266 #ifdef CONFIG_HARDENED_USERCOPY
267 	unsigned int useroffset;	/* Usercopy region offset */
268 	unsigned int usersize;		/* Usercopy region size */
269 #endif
270 
271 #ifdef CONFIG_SLUB_STATS
272 	struct kmem_cache_stats __percpu *cpu_stats;
273 #endif
274 
275 	struct kmem_cache_per_node_ptrs per_node[MAX_NUMNODES];
276 };
277 
278 /*
279  * Every cache has !NULL s->cpu_sheaves but they may point to the
280  * bootstrap_sheaf temporarily during init, or permanently for the boot caches
281  * and caches with debugging enabled, or all caches with CONFIG_SLUB_TINY. This
282  * helper distinguishes whether cache has real non-bootstrap sheaves.
283  */
284 static inline bool cache_has_sheaves(struct kmem_cache *s)
285 {
286 	/* Test CONFIG_SLUB_TINY for code elimination purposes */
287 	return !IS_ENABLED(CONFIG_SLUB_TINY) && s->sheaf_capacity;
288 }
289 
290 #if defined(CONFIG_SYSFS) && !defined(CONFIG_SLUB_TINY)
291 #define SLAB_SUPPORTS_SYSFS 1
292 void sysfs_slab_unlink(struct kmem_cache *s);
293 void sysfs_slab_release(struct kmem_cache *s);
294 int sysfs_slab_alias(struct kmem_cache *s, const char *name);
295 #else
296 static inline void sysfs_slab_unlink(struct kmem_cache *s) { }
297 static inline void sysfs_slab_release(struct kmem_cache *s) { }
298 static inline int sysfs_slab_alias(struct kmem_cache *s, const char *name)
299 							{ return 0; }
300 #endif
301 
302 void *fixup_red_left(struct kmem_cache *s, void *p);
303 
304 static inline void *nearest_obj(struct kmem_cache *cache,
305 				const struct slab *slab, void *x)
306 {
307 	void *object = x - (x - slab_address(slab)) % cache->size;
308 	void *last_object = slab_address(slab) +
309 		(slab->objects - 1) * cache->size;
310 	void *result = (unlikely(object > last_object)) ? last_object : object;
311 
312 	result = fixup_red_left(cache, result);
313 	return result;
314 }
315 
316 /* Determine object index from a given position */
317 static inline unsigned int __obj_to_index(const struct kmem_cache *cache,
318 					  void *addr, const void *obj)
319 {
320 	return reciprocal_divide(kasan_reset_tag(obj) - addr,
321 				 cache->reciprocal_size);
322 }
323 
324 static inline unsigned int obj_to_index(const struct kmem_cache *cache,
325 					const struct slab *slab, const void *obj)
326 {
327 	if (is_kfence_address(obj))
328 		return 0;
329 	return __obj_to_index(cache, slab_address(slab), obj);
330 }
331 
332 static inline int objs_per_slab(const struct kmem_cache *cache,
333 				const struct slab *slab)
334 {
335 	return slab->objects;
336 }
337 
338 /*
339  * State of the slab allocator.
340  *
341  * This is used to describe the states of the allocator during bootup.
342  * Allocators use this to gradually bootstrap themselves. Most allocators
343  * have the problem that the structures used for managing slab caches are
344  * allocated from slab caches themselves.
345  */
346 enum slab_state {
347 	DOWN,			/* No slab functionality yet */
348 	PARTIAL,		/* SLUB: kmem_cache_node available */
349 	UP,			/* Slab caches usable but not all extras yet */
350 	FULL			/* Everything is working */
351 };
352 
353 extern enum slab_state slab_state;
354 
355 /* The slab cache mutex protects the management structures during changes */
356 extern struct mutex slab_mutex;
357 
358 /* The list of all slab caches on the system */
359 extern struct list_head slab_caches;
360 
361 /* The slab cache that manages slab cache information */
362 extern struct kmem_cache *kmem_cache;
363 
364 /* A table of kmalloc cache names and sizes */
365 extern const struct kmalloc_info_struct {
366 	const char *name[NR_KMALLOC_TYPES];
367 	unsigned int size;
368 } kmalloc_info[];
369 
370 /* Kmalloc array related functions */
371 void setup_kmalloc_cache_index_table(void);
372 void create_kmalloc_caches(void);
373 
374 extern u8 kmalloc_size_index[24];
375 
376 static inline unsigned int size_index_elem(unsigned int bytes)
377 {
378 	return (bytes - 1) / 8;
379 }
380 
381 /*
382  * Find the kmem_cache structure that serves a given size of
383  * allocation
384  *
385  * This assumes size is larger than zero and not larger than
386  * KMALLOC_MAX_CACHE_SIZE and the caller must check that.
387  */
388 static inline struct kmem_cache *
389 kmalloc_slab(size_t size, kmem_buckets *b, gfp_t flags, kmalloc_token_t token)
390 {
391 	unsigned int index;
392 
393 	if (!b)
394 		b = &kmalloc_caches[kmalloc_type(flags, token)];
395 	if (size <= 192)
396 		index = kmalloc_size_index[size_index_elem(size)];
397 	else
398 		index = fls(size - 1);
399 
400 	return (*b)[index];
401 }
402 
403 gfp_t kmalloc_fix_flags(gfp_t flags);
404 
405 /* Functions provided by the slab allocators */
406 int do_kmem_cache_create(struct kmem_cache *s, const char *name,
407 			 unsigned int size, struct kmem_cache_args *args,
408 			 slab_flags_t flags);
409 
410 void __init kmem_cache_init(void);
411 extern void create_boot_cache(struct kmem_cache *, const char *name,
412 			unsigned int size, slab_flags_t flags,
413 			unsigned int useroffset, unsigned int usersize);
414 
415 int slab_unmergeable(struct kmem_cache *s);
416 bool slab_args_unmergeable(struct kmem_cache_args *args, slab_flags_t flags);
417 
418 slab_flags_t kmem_cache_flags(slab_flags_t flags, const char *name);
419 
420 static inline bool is_kmalloc_cache(struct kmem_cache *s)
421 {
422 	return (s->flags & SLAB_KMALLOC);
423 }
424 
425 static inline bool is_kmalloc_normal(struct kmem_cache *s)
426 {
427 	if (!is_kmalloc_cache(s))
428 		return false;
429 	return !(s->flags & (SLAB_CACHE_DMA|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT));
430 }
431 
432 bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj);
433 void flush_all_rcu_sheaves(void);
434 void flush_rcu_sheaves_on_cache(struct kmem_cache *s);
435 
436 #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
437 			 SLAB_CACHE_DMA32 | SLAB_PANIC | \
438 			 SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS | \
439 			 SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
440 			 SLAB_TEMPORARY | SLAB_ACCOUNT | \
441 			 SLAB_NO_USER_FLAGS | SLAB_KMALLOC | SLAB_NO_MERGE)
442 
443 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
444 			  SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
445 
446 #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS)
447 
448 bool __kmem_cache_empty(struct kmem_cache *);
449 int __kmem_cache_shutdown(struct kmem_cache *);
450 void __kmem_cache_release(struct kmem_cache *);
451 int __kmem_cache_shrink(struct kmem_cache *);
452 void slab_kmem_cache_release(struct kmem_cache *);
453 
454 struct seq_file;
455 struct file;
456 
457 struct slabinfo {
458 	unsigned long active_objs;
459 	unsigned long num_objs;
460 	unsigned long active_slabs;
461 	unsigned long num_slabs;
462 	unsigned long shared_avail;
463 	unsigned int limit;
464 	unsigned int batchcount;
465 	unsigned int shared;
466 	unsigned int objects_per_slab;
467 	unsigned int cache_order;
468 };
469 
470 void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
471 
472 #ifdef CONFIG_SLUB_DEBUG
473 #ifdef CONFIG_SLUB_DEBUG_ON
474 DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
475 #else
476 DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
477 #endif
478 extern void print_tracking(struct kmem_cache *s, void *object);
479 long validate_slab_cache(struct kmem_cache *s);
480 static inline bool __slub_debug_enabled(void)
481 {
482 	return static_branch_unlikely(&slub_debug_enabled);
483 }
484 #else
485 static inline void print_tracking(struct kmem_cache *s, void *object)
486 {
487 }
488 static inline bool __slub_debug_enabled(void)
489 {
490 	return false;
491 }
492 #endif
493 
494 /*
495  * Returns true if any of the specified slab_debug flags is enabled for the
496  * cache. Use only for flags parsed by setup_slub_debug() as it also enables
497  * the static key.
498  */
499 static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
500 {
501 	if (IS_ENABLED(CONFIG_SLUB_DEBUG))
502 		VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
503 	if (__slub_debug_enabled())
504 		return s->flags & flags;
505 	return false;
506 }
507 
508 #if IS_ENABLED(CONFIG_SLUB_DEBUG) && IS_ENABLED(CONFIG_KUNIT)
509 bool slab_in_kunit_test(void);
510 #else
511 static inline bool slab_in_kunit_test(void) { return false; }
512 #endif
513 
514 /*
515  * slub is about to manipulate internal object metadata.  This memory lies
516  * outside the range of the allocated object, so accessing it would normally
517  * be reported by kasan as a bounds error.  metadata_access_enable() is used
518  * to tell kasan that these accesses are OK.
519  */
520 static inline void metadata_access_enable(void)
521 {
522 	kasan_disable_current();
523 	kmsan_disable_current();
524 }
525 
526 static inline void metadata_access_disable(void)
527 {
528 	kmsan_enable_current();
529 	kasan_enable_current();
530 }
531 
532 #ifdef CONFIG_SLAB_OBJ_EXT
533 
534 /*
535  * slab_obj_exts - get the pointer to the slab object extension vector
536  * associated with a slab.
537  * @slab: a pointer to the slab struct
538  *
539  * Returns the address of the object extension vector associated with the slab,
540  * or zero if no such vector has been associated yet.
541  * Do not dereference the return value directly; use get/put_slab_obj_exts()
542  * pair and slab_obj_ext() to access individual elements.
543  *
544  * Example usage:
545  *
546  * obj_exts = slab_obj_exts(slab);
547  * if (obj_exts) {
548  *         get_slab_obj_exts(obj_exts);
549  *         obj_ext = slab_obj_ext(slab, obj_exts, obj_to_index(s, slab, obj));
550  *         // do something with obj_ext
551  *         put_slab_obj_exts(obj_exts);
552  * }
553  *
554  * Note that the get/put semantics does not involve reference counting.
555  * Instead, it updates kasan/kmsan depth so that accesses to slabobj_ext
556  * won't be reported as access violations.
557  */
558 static inline unsigned long slab_obj_exts(struct slab *slab)
559 {
560 	unsigned long obj_exts = READ_ONCE(slab->obj_exts);
561 
562 #ifdef CONFIG_MEMCG
563 	/*
564 	 * obj_exts should be either NULL, a valid pointer with
565 	 * MEMCG_DATA_OBJEXTS bit set or be equal to OBJEXTS_ALLOC_FAIL.
566 	 */
567 	VM_BUG_ON_PAGE(obj_exts && !(obj_exts & MEMCG_DATA_OBJEXTS) &&
568 		       obj_exts != OBJEXTS_ALLOC_FAIL, slab_page(slab));
569 	VM_BUG_ON_PAGE(obj_exts & MEMCG_DATA_KMEM, slab_page(slab));
570 #endif
571 
572 	return obj_exts & ~OBJEXTS_FLAGS_MASK;
573 }
574 
575 static inline void get_slab_obj_exts(unsigned long obj_exts)
576 {
577 	VM_WARN_ON_ONCE(!obj_exts);
578 	metadata_access_enable();
579 }
580 
581 static inline void put_slab_obj_exts(unsigned long obj_exts)
582 {
583 	metadata_access_disable();
584 }
585 
586 #ifdef CONFIG_64BIT
587 static inline void slab_set_stride(struct slab *slab, unsigned int stride)
588 {
589 	slab->stride = stride;
590 }
591 static inline unsigned int slab_get_stride(struct slab *slab)
592 {
593 	return slab->stride;
594 }
595 #else
596 static inline void slab_set_stride(struct slab *slab, unsigned int stride)
597 {
598 	VM_WARN_ON_ONCE(stride != sizeof(struct slabobj_ext));
599 }
600 static inline unsigned int slab_get_stride(struct slab *slab)
601 {
602 	return sizeof(struct slabobj_ext);
603 }
604 #endif
605 
606 /*
607  * slab_obj_ext - get the pointer to the slab object extension metadata
608  * associated with an object in a slab.
609  * @slab: a pointer to the slab struct
610  * @obj_exts: a pointer to the object extension vector
611  * @index: an index of the object
612  *
613  * Returns a pointer to the object extension associated with the object.
614  * Must be called within a section covered by get/put_slab_obj_exts().
615  */
616 static inline struct slabobj_ext *slab_obj_ext(struct slab *slab,
617 					       unsigned long obj_exts,
618 					       unsigned int index)
619 {
620 	struct slabobj_ext *obj_ext;
621 
622 	VM_WARN_ON_ONCE(obj_exts != slab_obj_exts(slab));
623 
624 	obj_ext = (struct slabobj_ext *)(obj_exts +
625 					 slab_get_stride(slab) * index);
626 	return kasan_reset_tag(obj_ext);
627 }
628 
629 int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,
630 			gfp_t gfp, unsigned int alloc_flags);
631 
632 #else /* CONFIG_SLAB_OBJ_EXT */
633 
634 static inline unsigned long slab_obj_exts(struct slab *slab)
635 {
636 	return 0;
637 }
638 
639 static inline struct slabobj_ext *slab_obj_ext(struct slab *slab,
640 					       unsigned long obj_exts,
641 					       unsigned int index)
642 {
643 	return NULL;
644 }
645 
646 static inline void slab_set_stride(struct slab *slab, unsigned int stride) { }
647 static inline unsigned int slab_get_stride(struct slab *slab) { return 0; }
648 
649 
650 #endif /* CONFIG_SLAB_OBJ_EXT */
651 
652 static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s)
653 {
654 	return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
655 		NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
656 }
657 
658 #ifdef CONFIG_MEMCG
659 bool __memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru,
660 				  gfp_t flags, unsigned int slab_alloc_flags,
661 				  size_t size, void **p);
662 void __memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
663 			    void **p, int objects, unsigned long obj_exts);
664 #endif
665 
666 void kvfree_rcu_cb(struct rcu_head *head);
667 
668 static inline unsigned int large_kmalloc_order(const struct page *page)
669 {
670 	return page[1].flags.f & 0xff;
671 }
672 
673 static inline size_t large_kmalloc_size(const struct page *page)
674 {
675 	return PAGE_SIZE << large_kmalloc_order(page);
676 }
677 
678 #ifdef CONFIG_SLUB_DEBUG
679 void dump_unreclaimable_slab(void);
680 #else
681 static inline void dump_unreclaimable_slab(void)
682 {
683 }
684 #endif
685 
686 void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
687 
688 #ifdef CONFIG_SLAB_FREELIST_RANDOM
689 int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
690 			gfp_t gfp);
691 void cache_random_seq_destroy(struct kmem_cache *cachep);
692 #else
693 static inline int cache_random_seq_create(struct kmem_cache *cachep,
694 					unsigned int count, gfp_t gfp)
695 {
696 	return 0;
697 }
698 static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
699 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
700 
701 static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
702 {
703 	if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
704 				&init_on_alloc)) {
705 		if (c->ctor)
706 			return false;
707 		if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
708 			return flags & __GFP_ZERO;
709 		return true;
710 	}
711 	return flags & __GFP_ZERO;
712 }
713 
714 static inline bool slab_want_init_on_free(struct kmem_cache *c)
715 {
716 	if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
717 				&init_on_free))
718 		return !(c->ctor ||
719 			 (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
720 	return false;
721 }
722 
723 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
724 void debugfs_slab_release(struct kmem_cache *);
725 #else
726 static inline void debugfs_slab_release(struct kmem_cache *s) { }
727 #endif
728 
729 #ifdef CONFIG_PRINTK
730 #define KS_ADDRS_COUNT 16
731 struct kmem_obj_info {
732 	void *kp_ptr;
733 	struct slab *kp_slab;
734 	void *kp_objp;
735 	unsigned long kp_data_offset;
736 	struct kmem_cache *kp_slab_cache;
737 	void *kp_ret;
738 	void *kp_stack[KS_ADDRS_COUNT];
739 	void *kp_free_stack[KS_ADDRS_COUNT];
740 };
741 void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
742 #endif
743 
744 void __check_heap_object(const void *ptr, unsigned long n,
745 			 const struct slab *slab, bool to_user);
746 
747 void defer_free_barrier(void);
748 
749 static inline bool slub_debug_orig_size(struct kmem_cache *s)
750 {
751 	return (kmem_cache_debug_flags(s, SLAB_STORE_USER) &&
752 			(s->flags & SLAB_KMALLOC));
753 }
754 
755 #ifdef CONFIG_SLUB_DEBUG
756 void skip_orig_size_check(struct kmem_cache *s, const void *object);
757 #endif
758 
759 #endif /* MM_SLAB_H */
760