xref: /openbmc/linux/mm/slab.h (revision e5242c5f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef MM_SLAB_H
3 #define MM_SLAB_H
4 /*
5  * Internal slab definitions
6  */
7 void __init kmem_cache_init(void);
8 
9 #ifdef CONFIG_64BIT
10 # ifdef system_has_cmpxchg128
11 # define system_has_freelist_aba()	system_has_cmpxchg128()
12 # define try_cmpxchg_freelist		try_cmpxchg128
13 # endif
14 #define this_cpu_try_cmpxchg_freelist	this_cpu_try_cmpxchg128
15 typedef u128 freelist_full_t;
16 #else /* CONFIG_64BIT */
17 # ifdef system_has_cmpxchg64
18 # define system_has_freelist_aba()	system_has_cmpxchg64()
19 # define try_cmpxchg_freelist		try_cmpxchg64
20 # endif
21 #define this_cpu_try_cmpxchg_freelist	this_cpu_try_cmpxchg64
22 typedef u64 freelist_full_t;
23 #endif /* CONFIG_64BIT */
24 
25 #if defined(system_has_freelist_aba) && !defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
26 #undef system_has_freelist_aba
27 #endif
28 
29 /*
30  * Freelist pointer and counter to cmpxchg together, avoids the typical ABA
31  * problems with cmpxchg of just a pointer.
32  */
33 typedef union {
34 	struct {
35 		void *freelist;
36 		unsigned long counter;
37 	};
38 	freelist_full_t full;
39 } freelist_aba_t;
40 
41 /* Reuses the bits in struct page */
42 struct slab {
43 	unsigned long __page_flags;
44 
45 #if defined(CONFIG_SLAB)
46 
47 	struct kmem_cache *slab_cache;
48 	union {
49 		struct {
50 			struct list_head slab_list;
51 			void *freelist;	/* array of free object indexes */
52 			void *s_mem;	/* first object */
53 		};
54 		struct rcu_head rcu_head;
55 	};
56 	unsigned int active;
57 
58 #elif defined(CONFIG_SLUB)
59 
60 	struct kmem_cache *slab_cache;
61 	union {
62 		struct {
63 			union {
64 				struct list_head slab_list;
65 #ifdef CONFIG_SLUB_CPU_PARTIAL
66 				struct {
67 					struct slab *next;
68 					int slabs;	/* Nr of slabs left */
69 				};
70 #endif
71 			};
72 			/* Double-word boundary */
73 			union {
74 				struct {
75 					void *freelist;		/* first free object */
76 					union {
77 						unsigned long counters;
78 						struct {
79 							unsigned inuse:16;
80 							unsigned objects:15;
81 							/*
82 							 * If slab debugging is enabled then the
83 							 * frozen bit can be reused to indicate
84 							 * that the slab was corrupted
85 							 */
86 							unsigned frozen:1;
87 						};
88 					};
89 				};
90 #ifdef system_has_freelist_aba
91 				freelist_aba_t freelist_counter;
92 #endif
93 			};
94 		};
95 		struct rcu_head rcu_head;
96 	};
97 	unsigned int __unused;
98 
99 #else
100 #error "Unexpected slab allocator configured"
101 #endif
102 
103 	atomic_t __page_refcount;
104 #ifdef CONFIG_MEMCG
105 	unsigned long memcg_data;
106 #endif
107 };
108 
109 #define SLAB_MATCH(pg, sl)						\
110 	static_assert(offsetof(struct page, pg) == offsetof(struct slab, sl))
111 SLAB_MATCH(flags, __page_flags);
112 SLAB_MATCH(compound_head, slab_cache);	/* Ensure bit 0 is clear */
113 SLAB_MATCH(_refcount, __page_refcount);
114 #ifdef CONFIG_MEMCG
115 SLAB_MATCH(memcg_data, memcg_data);
116 #endif
117 #undef SLAB_MATCH
118 static_assert(sizeof(struct slab) <= sizeof(struct page));
119 #if defined(system_has_freelist_aba) && defined(CONFIG_SLUB)
120 static_assert(IS_ALIGNED(offsetof(struct slab, freelist), sizeof(freelist_aba_t)));
121 #endif
122 
123 /**
124  * folio_slab - Converts from folio to slab.
125  * @folio: The folio.
126  *
127  * Currently struct slab is a different representation of a folio where
128  * folio_test_slab() is true.
129  *
130  * Return: The slab which contains this folio.
131  */
132 #define folio_slab(folio)	(_Generic((folio),			\
133 	const struct folio *:	(const struct slab *)(folio),		\
134 	struct folio *:		(struct slab *)(folio)))
135 
136 /**
137  * slab_folio - The folio allocated for a slab
138  * @slab: The slab.
139  *
140  * Slabs are allocated as folios that contain the individual objects and are
141  * using some fields in the first struct page of the folio - those fields are
142  * now accessed by struct slab. It is occasionally necessary to convert back to
143  * a folio in order to communicate with the rest of the mm.  Please use this
144  * helper function instead of casting yourself, as the implementation may change
145  * in the future.
146  */
147 #define slab_folio(s)		(_Generic((s),				\
148 	const struct slab *:	(const struct folio *)s,		\
149 	struct slab *:		(struct folio *)s))
150 
151 /**
152  * page_slab - Converts from first struct page to slab.
153  * @p: The first (either head of compound or single) page of slab.
154  *
155  * A temporary wrapper to convert struct page to struct slab in situations where
156  * we know the page is the compound head, or single order-0 page.
157  *
158  * Long-term ideally everything would work with struct slab directly or go
159  * through folio to struct slab.
160  *
161  * Return: The slab which contains this page
162  */
163 #define page_slab(p)		(_Generic((p),				\
164 	const struct page *:	(const struct slab *)(p),		\
165 	struct page *:		(struct slab *)(p)))
166 
167 /**
168  * slab_page - The first struct page allocated for a slab
169  * @slab: The slab.
170  *
171  * A convenience wrapper for converting slab to the first struct page of the
172  * underlying folio, to communicate with code not yet converted to folio or
173  * struct slab.
174  */
175 #define slab_page(s) folio_page(slab_folio(s), 0)
176 
177 /*
178  * If network-based swap is enabled, sl*b must keep track of whether pages
179  * were allocated from pfmemalloc reserves.
180  */
181 static inline bool slab_test_pfmemalloc(const struct slab *slab)
182 {
183 	return folio_test_active((struct folio *)slab_folio(slab));
184 }
185 
186 static inline void slab_set_pfmemalloc(struct slab *slab)
187 {
188 	folio_set_active(slab_folio(slab));
189 }
190 
191 static inline void slab_clear_pfmemalloc(struct slab *slab)
192 {
193 	folio_clear_active(slab_folio(slab));
194 }
195 
196 static inline void __slab_clear_pfmemalloc(struct slab *slab)
197 {
198 	__folio_clear_active(slab_folio(slab));
199 }
200 
201 static inline void *slab_address(const struct slab *slab)
202 {
203 	return folio_address(slab_folio(slab));
204 }
205 
206 static inline int slab_nid(const struct slab *slab)
207 {
208 	return folio_nid(slab_folio(slab));
209 }
210 
211 static inline pg_data_t *slab_pgdat(const struct slab *slab)
212 {
213 	return folio_pgdat(slab_folio(slab));
214 }
215 
216 static inline struct slab *virt_to_slab(const void *addr)
217 {
218 	struct folio *folio = virt_to_folio(addr);
219 
220 	if (!folio_test_slab(folio))
221 		return NULL;
222 
223 	return folio_slab(folio);
224 }
225 
226 static inline int slab_order(const struct slab *slab)
227 {
228 	return folio_order((struct folio *)slab_folio(slab));
229 }
230 
231 static inline size_t slab_size(const struct slab *slab)
232 {
233 	return PAGE_SIZE << slab_order(slab);
234 }
235 
236 #ifdef CONFIG_SLAB
237 #include <linux/slab_def.h>
238 #endif
239 
240 #ifdef CONFIG_SLUB
241 #include <linux/slub_def.h>
242 #endif
243 
244 #include <linux/memcontrol.h>
245 #include <linux/fault-inject.h>
246 #include <linux/kasan.h>
247 #include <linux/kmemleak.h>
248 #include <linux/random.h>
249 #include <linux/sched/mm.h>
250 #include <linux/list_lru.h>
251 
252 /*
253  * State of the slab allocator.
254  *
255  * This is used to describe the states of the allocator during bootup.
256  * Allocators use this to gradually bootstrap themselves. Most allocators
257  * have the problem that the structures used for managing slab caches are
258  * allocated from slab caches themselves.
259  */
260 enum slab_state {
261 	DOWN,			/* No slab functionality yet */
262 	PARTIAL,		/* SLUB: kmem_cache_node available */
263 	PARTIAL_NODE,		/* SLAB: kmalloc size for node struct available */
264 	UP,			/* Slab caches usable but not all extras yet */
265 	FULL			/* Everything is working */
266 };
267 
268 extern enum slab_state slab_state;
269 
270 /* The slab cache mutex protects the management structures during changes */
271 extern struct mutex slab_mutex;
272 
273 /* The list of all slab caches on the system */
274 extern struct list_head slab_caches;
275 
276 /* The slab cache that manages slab cache information */
277 extern struct kmem_cache *kmem_cache;
278 
279 /* A table of kmalloc cache names and sizes */
280 extern const struct kmalloc_info_struct {
281 	const char *name[NR_KMALLOC_TYPES];
282 	unsigned int size;
283 } kmalloc_info[];
284 
285 /* Kmalloc array related functions */
286 void setup_kmalloc_cache_index_table(void);
287 void create_kmalloc_caches(slab_flags_t);
288 
289 /* Find the kmalloc slab corresponding for a certain size */
290 struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags, unsigned long caller);
291 
292 void *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags,
293 			      int node, size_t orig_size,
294 			      unsigned long caller);
295 void __kmem_cache_free(struct kmem_cache *s, void *x, unsigned long caller);
296 
297 gfp_t kmalloc_fix_flags(gfp_t flags);
298 
299 /* Functions provided by the slab allocators */
300 int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags);
301 
302 void __init new_kmalloc_cache(int idx, enum kmalloc_cache_type type,
303 			      slab_flags_t flags);
304 extern void create_boot_cache(struct kmem_cache *, const char *name,
305 			unsigned int size, slab_flags_t flags,
306 			unsigned int useroffset, unsigned int usersize);
307 
308 int slab_unmergeable(struct kmem_cache *s);
309 struct kmem_cache *find_mergeable(unsigned size, unsigned align,
310 		slab_flags_t flags, const char *name, void (*ctor)(void *));
311 struct kmem_cache *
312 __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
313 		   slab_flags_t flags, void (*ctor)(void *));
314 
315 slab_flags_t kmem_cache_flags(unsigned int object_size,
316 	slab_flags_t flags, const char *name);
317 
318 static inline bool is_kmalloc_cache(struct kmem_cache *s)
319 {
320 	return (s->flags & SLAB_KMALLOC);
321 }
322 
323 /* Legal flag mask for kmem_cache_create(), for various configurations */
324 #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
325 			 SLAB_CACHE_DMA32 | SLAB_PANIC | \
326 			 SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS )
327 
328 #if defined(CONFIG_DEBUG_SLAB)
329 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
330 #elif defined(CONFIG_SLUB_DEBUG)
331 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
332 			  SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
333 #else
334 #define SLAB_DEBUG_FLAGS (0)
335 #endif
336 
337 #if defined(CONFIG_SLAB)
338 #define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
339 			  SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
340 			  SLAB_ACCOUNT | SLAB_NO_MERGE)
341 #elif defined(CONFIG_SLUB)
342 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
343 			  SLAB_TEMPORARY | SLAB_ACCOUNT | \
344 			  SLAB_NO_USER_FLAGS | SLAB_KMALLOC | SLAB_NO_MERGE)
345 #else
346 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
347 #endif
348 
349 /* Common flags available with current configuration */
350 #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
351 
352 /* Common flags permitted for kmem_cache_create */
353 #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
354 			      SLAB_RED_ZONE | \
355 			      SLAB_POISON | \
356 			      SLAB_STORE_USER | \
357 			      SLAB_TRACE | \
358 			      SLAB_CONSISTENCY_CHECKS | \
359 			      SLAB_MEM_SPREAD | \
360 			      SLAB_NOLEAKTRACE | \
361 			      SLAB_RECLAIM_ACCOUNT | \
362 			      SLAB_TEMPORARY | \
363 			      SLAB_ACCOUNT | \
364 			      SLAB_KMALLOC | \
365 			      SLAB_NO_MERGE | \
366 			      SLAB_NO_USER_FLAGS)
367 
368 bool __kmem_cache_empty(struct kmem_cache *);
369 int __kmem_cache_shutdown(struct kmem_cache *);
370 void __kmem_cache_release(struct kmem_cache *);
371 int __kmem_cache_shrink(struct kmem_cache *);
372 void slab_kmem_cache_release(struct kmem_cache *);
373 
374 struct seq_file;
375 struct file;
376 
377 struct slabinfo {
378 	unsigned long active_objs;
379 	unsigned long num_objs;
380 	unsigned long active_slabs;
381 	unsigned long num_slabs;
382 	unsigned long shared_avail;
383 	unsigned int limit;
384 	unsigned int batchcount;
385 	unsigned int shared;
386 	unsigned int objects_per_slab;
387 	unsigned int cache_order;
388 };
389 
390 void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
391 void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
392 ssize_t slabinfo_write(struct file *file, const char __user *buffer,
393 		       size_t count, loff_t *ppos);
394 
395 static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s)
396 {
397 	return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
398 		NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
399 }
400 
401 #ifdef CONFIG_SLUB_DEBUG
402 #ifdef CONFIG_SLUB_DEBUG_ON
403 DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
404 #else
405 DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
406 #endif
407 extern void print_tracking(struct kmem_cache *s, void *object);
408 long validate_slab_cache(struct kmem_cache *s);
409 static inline bool __slub_debug_enabled(void)
410 {
411 	return static_branch_unlikely(&slub_debug_enabled);
412 }
413 #else
414 static inline void print_tracking(struct kmem_cache *s, void *object)
415 {
416 }
417 static inline bool __slub_debug_enabled(void)
418 {
419 	return false;
420 }
421 #endif
422 
423 /*
424  * Returns true if any of the specified slub_debug flags is enabled for the
425  * cache. Use only for flags parsed by setup_slub_debug() as it also enables
426  * the static key.
427  */
428 static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
429 {
430 	if (IS_ENABLED(CONFIG_SLUB_DEBUG))
431 		VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
432 	if (__slub_debug_enabled())
433 		return s->flags & flags;
434 	return false;
435 }
436 
437 #ifdef CONFIG_MEMCG_KMEM
438 /*
439  * slab_objcgs - get the object cgroups vector associated with a slab
440  * @slab: a pointer to the slab struct
441  *
442  * Returns a pointer to the object cgroups vector associated with the slab,
443  * or NULL if no such vector has been associated yet.
444  */
445 static inline struct obj_cgroup **slab_objcgs(struct slab *slab)
446 {
447 	unsigned long memcg_data = READ_ONCE(slab->memcg_data);
448 
449 	VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS),
450 							slab_page(slab));
451 	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, slab_page(slab));
452 
453 	return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
454 }
455 
456 int memcg_alloc_slab_cgroups(struct slab *slab, struct kmem_cache *s,
457 				 gfp_t gfp, bool new_slab);
458 void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
459 		     enum node_stat_item idx, int nr);
460 
461 static inline void memcg_free_slab_cgroups(struct slab *slab)
462 {
463 	kfree(slab_objcgs(slab));
464 	slab->memcg_data = 0;
465 }
466 
467 static inline size_t obj_full_size(struct kmem_cache *s)
468 {
469 	/*
470 	 * For each accounted object there is an extra space which is used
471 	 * to store obj_cgroup membership. Charge it too.
472 	 */
473 	return s->size + sizeof(struct obj_cgroup *);
474 }
475 
476 /*
477  * Returns false if the allocation should fail.
478  */
479 static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
480 					     struct list_lru *lru,
481 					     struct obj_cgroup **objcgp,
482 					     size_t objects, gfp_t flags)
483 {
484 	struct obj_cgroup *objcg;
485 
486 	if (!memcg_kmem_online())
487 		return true;
488 
489 	if (!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT))
490 		return true;
491 
492 	objcg = get_obj_cgroup_from_current();
493 	if (!objcg)
494 		return true;
495 
496 	if (lru) {
497 		int ret;
498 		struct mem_cgroup *memcg;
499 
500 		memcg = get_mem_cgroup_from_objcg(objcg);
501 		ret = memcg_list_lru_alloc(memcg, lru, flags);
502 		css_put(&memcg->css);
503 
504 		if (ret)
505 			goto out;
506 	}
507 
508 	if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s)))
509 		goto out;
510 
511 	*objcgp = objcg;
512 	return true;
513 out:
514 	obj_cgroup_put(objcg);
515 	return false;
516 }
517 
518 static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
519 					      struct obj_cgroup *objcg,
520 					      gfp_t flags, size_t size,
521 					      void **p)
522 {
523 	struct slab *slab;
524 	unsigned long off;
525 	size_t i;
526 
527 	if (!memcg_kmem_online() || !objcg)
528 		return;
529 
530 	for (i = 0; i < size; i++) {
531 		if (likely(p[i])) {
532 			slab = virt_to_slab(p[i]);
533 
534 			if (!slab_objcgs(slab) &&
535 			    memcg_alloc_slab_cgroups(slab, s, flags,
536 							 false)) {
537 				obj_cgroup_uncharge(objcg, obj_full_size(s));
538 				continue;
539 			}
540 
541 			off = obj_to_index(s, slab, p[i]);
542 			obj_cgroup_get(objcg);
543 			slab_objcgs(slab)[off] = objcg;
544 			mod_objcg_state(objcg, slab_pgdat(slab),
545 					cache_vmstat_idx(s), obj_full_size(s));
546 		} else {
547 			obj_cgroup_uncharge(objcg, obj_full_size(s));
548 		}
549 	}
550 	obj_cgroup_put(objcg);
551 }
552 
553 static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
554 					void **p, int objects)
555 {
556 	struct obj_cgroup **objcgs;
557 	int i;
558 
559 	if (!memcg_kmem_online())
560 		return;
561 
562 	objcgs = slab_objcgs(slab);
563 	if (!objcgs)
564 		return;
565 
566 	for (i = 0; i < objects; i++) {
567 		struct obj_cgroup *objcg;
568 		unsigned int off;
569 
570 		off = obj_to_index(s, slab, p[i]);
571 		objcg = objcgs[off];
572 		if (!objcg)
573 			continue;
574 
575 		objcgs[off] = NULL;
576 		obj_cgroup_uncharge(objcg, obj_full_size(s));
577 		mod_objcg_state(objcg, slab_pgdat(slab), cache_vmstat_idx(s),
578 				-obj_full_size(s));
579 		obj_cgroup_put(objcg);
580 	}
581 }
582 
583 #else /* CONFIG_MEMCG_KMEM */
584 static inline struct obj_cgroup **slab_objcgs(struct slab *slab)
585 {
586 	return NULL;
587 }
588 
589 static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
590 {
591 	return NULL;
592 }
593 
594 static inline int memcg_alloc_slab_cgroups(struct slab *slab,
595 					       struct kmem_cache *s, gfp_t gfp,
596 					       bool new_slab)
597 {
598 	return 0;
599 }
600 
601 static inline void memcg_free_slab_cgroups(struct slab *slab)
602 {
603 }
604 
605 static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
606 					     struct list_lru *lru,
607 					     struct obj_cgroup **objcgp,
608 					     size_t objects, gfp_t flags)
609 {
610 	return true;
611 }
612 
613 static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
614 					      struct obj_cgroup *objcg,
615 					      gfp_t flags, size_t size,
616 					      void **p)
617 {
618 }
619 
620 static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
621 					void **p, int objects)
622 {
623 }
624 #endif /* CONFIG_MEMCG_KMEM */
625 
626 static inline struct kmem_cache *virt_to_cache(const void *obj)
627 {
628 	struct slab *slab;
629 
630 	slab = virt_to_slab(obj);
631 	if (WARN_ONCE(!slab, "%s: Object is not a Slab page!\n",
632 					__func__))
633 		return NULL;
634 	return slab->slab_cache;
635 }
636 
637 static __always_inline void account_slab(struct slab *slab, int order,
638 					 struct kmem_cache *s, gfp_t gfp)
639 {
640 	if (memcg_kmem_online() && (s->flags & SLAB_ACCOUNT))
641 		memcg_alloc_slab_cgroups(slab, s, gfp, true);
642 
643 	mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
644 			    PAGE_SIZE << order);
645 }
646 
647 static __always_inline void unaccount_slab(struct slab *slab, int order,
648 					   struct kmem_cache *s)
649 {
650 	if (memcg_kmem_online())
651 		memcg_free_slab_cgroups(slab);
652 
653 	mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
654 			    -(PAGE_SIZE << order));
655 }
656 
657 static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
658 {
659 	struct kmem_cache *cachep;
660 
661 	if (!IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
662 	    !kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS))
663 		return s;
664 
665 	cachep = virt_to_cache(x);
666 	if (WARN(cachep && cachep != s,
667 		  "%s: Wrong slab cache. %s but object is from %s\n",
668 		  __func__, s->name, cachep->name))
669 		print_tracking(cachep, x);
670 	return cachep;
671 }
672 
673 void free_large_kmalloc(struct folio *folio, void *object);
674 
675 size_t __ksize(const void *objp);
676 
677 static inline size_t slab_ksize(const struct kmem_cache *s)
678 {
679 #ifndef CONFIG_SLUB
680 	return s->object_size;
681 
682 #else /* CONFIG_SLUB */
683 # ifdef CONFIG_SLUB_DEBUG
684 	/*
685 	 * Debugging requires use of the padding between object
686 	 * and whatever may come after it.
687 	 */
688 	if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
689 		return s->object_size;
690 # endif
691 	if (s->flags & SLAB_KASAN)
692 		return s->object_size;
693 	/*
694 	 * If we have the need to store the freelist pointer
695 	 * back there or track user information then we can
696 	 * only use the space before that information.
697 	 */
698 	if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
699 		return s->inuse;
700 	/*
701 	 * Else we can use all the padding etc for the allocation
702 	 */
703 	return s->size;
704 #endif
705 }
706 
707 static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
708 						     struct list_lru *lru,
709 						     struct obj_cgroup **objcgp,
710 						     size_t size, gfp_t flags)
711 {
712 	flags &= gfp_allowed_mask;
713 
714 	might_alloc(flags);
715 
716 	if (should_failslab(s, flags))
717 		return NULL;
718 
719 	if (!memcg_slab_pre_alloc_hook(s, lru, objcgp, size, flags))
720 		return NULL;
721 
722 	return s;
723 }
724 
725 static inline void slab_post_alloc_hook(struct kmem_cache *s,
726 					struct obj_cgroup *objcg, gfp_t flags,
727 					size_t size, void **p, bool init,
728 					unsigned int orig_size)
729 {
730 	unsigned int zero_size = s->object_size;
731 	bool kasan_init = init;
732 	size_t i;
733 
734 	flags &= gfp_allowed_mask;
735 
736 	/*
737 	 * For kmalloc object, the allocated memory size(object_size) is likely
738 	 * larger than the requested size(orig_size). If redzone check is
739 	 * enabled for the extra space, don't zero it, as it will be redzoned
740 	 * soon. The redzone operation for this extra space could be seen as a
741 	 * replacement of current poisoning under certain debug option, and
742 	 * won't break other sanity checks.
743 	 */
744 	if (kmem_cache_debug_flags(s, SLAB_STORE_USER | SLAB_RED_ZONE) &&
745 	    (s->flags & SLAB_KMALLOC))
746 		zero_size = orig_size;
747 
748 	/*
749 	 * When slub_debug is enabled, avoid memory initialization integrated
750 	 * into KASAN and instead zero out the memory via the memset below with
751 	 * the proper size. Otherwise, KASAN might overwrite SLUB redzones and
752 	 * cause false-positive reports. This does not lead to a performance
753 	 * penalty on production builds, as slub_debug is not intended to be
754 	 * enabled there.
755 	 */
756 	if (__slub_debug_enabled())
757 		kasan_init = false;
758 
759 	/*
760 	 * As memory initialization might be integrated into KASAN,
761 	 * kasan_slab_alloc and initialization memset must be
762 	 * kept together to avoid discrepancies in behavior.
763 	 *
764 	 * As p[i] might get tagged, memset and kmemleak hook come after KASAN.
765 	 */
766 	for (i = 0; i < size; i++) {
767 		p[i] = kasan_slab_alloc(s, p[i], flags, kasan_init);
768 		if (p[i] && init && (!kasan_init || !kasan_has_integrated_init()))
769 			memset(p[i], 0, zero_size);
770 		kmemleak_alloc_recursive(p[i], s->object_size, 1,
771 					 s->flags, flags);
772 		kmsan_slab_alloc(s, p[i], flags);
773 	}
774 
775 	memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
776 }
777 
778 /*
779  * The slab lists for all objects.
780  */
781 struct kmem_cache_node {
782 #ifdef CONFIG_SLAB
783 	raw_spinlock_t list_lock;
784 	struct list_head slabs_partial;	/* partial list first, better asm code */
785 	struct list_head slabs_full;
786 	struct list_head slabs_free;
787 	unsigned long total_slabs;	/* length of all slab lists */
788 	unsigned long free_slabs;	/* length of free slab list only */
789 	unsigned long free_objects;
790 	unsigned int free_limit;
791 	unsigned int colour_next;	/* Per-node cache coloring */
792 	struct array_cache *shared;	/* shared per node */
793 	struct alien_cache **alien;	/* on other nodes */
794 	unsigned long next_reap;	/* updated without locking */
795 	int free_touched;		/* updated without locking */
796 #endif
797 
798 #ifdef CONFIG_SLUB
799 	spinlock_t list_lock;
800 	unsigned long nr_partial;
801 	struct list_head partial;
802 #ifdef CONFIG_SLUB_DEBUG
803 	atomic_long_t nr_slabs;
804 	atomic_long_t total_objects;
805 	struct list_head full;
806 #endif
807 #endif
808 
809 };
810 
811 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
812 {
813 	return s->node[node];
814 }
815 
816 /*
817  * Iterator over all nodes. The body will be executed for each node that has
818  * a kmem_cache_node structure allocated (which is true for all online nodes)
819  */
820 #define for_each_kmem_cache_node(__s, __node, __n) \
821 	for (__node = 0; __node < nr_node_ids; __node++) \
822 		 if ((__n = get_node(__s, __node)))
823 
824 
825 #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
826 void dump_unreclaimable_slab(void);
827 #else
828 static inline void dump_unreclaimable_slab(void)
829 {
830 }
831 #endif
832 
833 void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
834 
835 #ifdef CONFIG_SLAB_FREELIST_RANDOM
836 int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
837 			gfp_t gfp);
838 void cache_random_seq_destroy(struct kmem_cache *cachep);
839 #else
840 static inline int cache_random_seq_create(struct kmem_cache *cachep,
841 					unsigned int count, gfp_t gfp)
842 {
843 	return 0;
844 }
845 static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
846 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
847 
848 static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
849 {
850 	if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
851 				&init_on_alloc)) {
852 		if (c->ctor)
853 			return false;
854 		if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
855 			return flags & __GFP_ZERO;
856 		return true;
857 	}
858 	return flags & __GFP_ZERO;
859 }
860 
861 static inline bool slab_want_init_on_free(struct kmem_cache *c)
862 {
863 	if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
864 				&init_on_free))
865 		return !(c->ctor ||
866 			 (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
867 	return false;
868 }
869 
870 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
871 void debugfs_slab_release(struct kmem_cache *);
872 #else
873 static inline void debugfs_slab_release(struct kmem_cache *s) { }
874 #endif
875 
876 #ifdef CONFIG_PRINTK
877 #define KS_ADDRS_COUNT 16
878 struct kmem_obj_info {
879 	void *kp_ptr;
880 	struct slab *kp_slab;
881 	void *kp_objp;
882 	unsigned long kp_data_offset;
883 	struct kmem_cache *kp_slab_cache;
884 	void *kp_ret;
885 	void *kp_stack[KS_ADDRS_COUNT];
886 	void *kp_free_stack[KS_ADDRS_COUNT];
887 };
888 void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
889 #endif
890 
891 void __check_heap_object(const void *ptr, unsigned long n,
892 			 const struct slab *slab, bool to_user);
893 
894 #ifdef CONFIG_SLUB_DEBUG
895 void skip_orig_size_check(struct kmem_cache *s, const void *object);
896 #endif
897 
898 #endif /* MM_SLAB_H */
899