xref: /openbmc/linux/mm/slab.h (revision ec788f7e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef MM_SLAB_H
3 #define MM_SLAB_H
4 /*
5  * Internal slab definitions
6  */
7 
8 /* Reuses the bits in struct page */
9 struct slab {
10 	unsigned long __page_flags;
11 
12 #if defined(CONFIG_SLAB)
13 
14 	struct kmem_cache *slab_cache;
15 	union {
16 		struct {
17 			struct list_head slab_list;
18 			void *freelist;	/* array of free object indexes */
19 			void *s_mem;	/* first object */
20 		};
21 		struct rcu_head rcu_head;
22 	};
23 	unsigned int active;
24 
25 #elif defined(CONFIG_SLUB)
26 
27 	struct kmem_cache *slab_cache;
28 	union {
29 		struct {
30 			union {
31 				struct list_head slab_list;
32 #ifdef CONFIG_SLUB_CPU_PARTIAL
33 				struct {
34 					struct slab *next;
35 					int slabs;	/* Nr of slabs left */
36 				};
37 #endif
38 			};
39 			/* Double-word boundary */
40 			void *freelist;		/* first free object */
41 			union {
42 				unsigned long counters;
43 				struct {
44 					unsigned inuse:16;
45 					unsigned objects:15;
46 					unsigned frozen:1;
47 				};
48 			};
49 		};
50 		struct rcu_head rcu_head;
51 	};
52 	unsigned int __unused;
53 
54 #else
55 #error "Unexpected slab allocator configured"
56 #endif
57 
58 	atomic_t __page_refcount;
59 #ifdef CONFIG_MEMCG
60 	unsigned long memcg_data;
61 #endif
62 };
63 
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);
69 #ifdef CONFIG_MEMCG
70 SLAB_MATCH(memcg_data, memcg_data);
71 #endif
72 #undef SLAB_MATCH
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 *)));
76 #endif
77 
78 /**
79  * folio_slab - Converts from folio to slab.
80  * @folio: The folio.
81  *
82  * Currently struct slab is a different representation of a folio where
83  * folio_test_slab() is true.
84  *
85  * Return: The slab which contains this folio.
86  */
87 #define folio_slab(folio)	(_Generic((folio),			\
88 	const struct folio *:	(const struct slab *)(folio),		\
89 	struct folio *:		(struct slab *)(folio)))
90 
91 /**
92  * slab_folio - The folio allocated for a slab
93  * @slab: The slab.
94  *
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
100  * in the future.
101  */
102 #define slab_folio(s)		(_Generic((s),				\
103 	const struct slab *:	(const struct folio *)s,		\
104 	struct slab *:		(struct folio *)s))
105 
106 /**
107  * page_slab - Converts from first struct page to slab.
108  * @p: The first (either head of compound or single) page of slab.
109  *
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.
112  *
113  * Long-term ideally everything would work with struct slab directly or go
114  * through folio to struct slab.
115  *
116  * Return: The slab which contains this page
117  */
118 #define page_slab(p)		(_Generic((p),				\
119 	const struct page *:	(const struct slab *)(p),		\
120 	struct page *:		(struct slab *)(p)))
121 
122 /**
123  * slab_page - The first struct page allocated for a slab
124  * @slab: The slab.
125  *
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
128  * struct slab.
129  */
130 #define slab_page(s) folio_page(slab_folio(s), 0)
131 
132 /*
133  * If network-based swap is enabled, sl*b must keep track of whether pages
134  * were allocated from pfmemalloc reserves.
135  */
136 static inline bool slab_test_pfmemalloc(const struct slab *slab)
137 {
138 	return folio_test_active((struct folio *)slab_folio(slab));
139 }
140 
141 static inline void slab_set_pfmemalloc(struct slab *slab)
142 {
143 	folio_set_active(slab_folio(slab));
144 }
145 
146 static inline void slab_clear_pfmemalloc(struct slab *slab)
147 {
148 	folio_clear_active(slab_folio(slab));
149 }
150 
151 static inline void __slab_clear_pfmemalloc(struct slab *slab)
152 {
153 	__folio_clear_active(slab_folio(slab));
154 }
155 
156 static inline void *slab_address(const struct slab *slab)
157 {
158 	return folio_address(slab_folio(slab));
159 }
160 
161 static inline int slab_nid(const struct slab *slab)
162 {
163 	return folio_nid(slab_folio(slab));
164 }
165 
166 static inline pg_data_t *slab_pgdat(const struct slab *slab)
167 {
168 	return folio_pgdat(slab_folio(slab));
169 }
170 
171 static inline struct slab *virt_to_slab(const void *addr)
172 {
173 	struct folio *folio = virt_to_folio(addr);
174 
175 	if (!folio_test_slab(folio))
176 		return NULL;
177 
178 	return folio_slab(folio);
179 }
180 
181 static inline int slab_order(const struct slab *slab)
182 {
183 	return folio_order((struct folio *)slab_folio(slab));
184 }
185 
186 static inline size_t slab_size(const struct slab *slab)
187 {
188 	return PAGE_SIZE << slab_order(slab);
189 }
190 
191 #ifdef CONFIG_SLAB
192 #include <linux/slab_def.h>
193 #endif
194 
195 #ifdef CONFIG_SLUB
196 #include <linux/slub_def.h>
197 #endif
198 
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>
206 
207 /*
208  * State of the slab allocator.
209  *
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.
214  */
215 enum slab_state {
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 */
221 };
222 
223 extern enum slab_state slab_state;
224 
225 /* The slab cache mutex protects the management structures during changes */
226 extern struct mutex slab_mutex;
227 
228 /* The list of all slab caches on the system */
229 extern struct list_head slab_caches;
230 
231 /* The slab cache that manages slab cache information */
232 extern struct kmem_cache *kmem_cache;
233 
234 /* A table of kmalloc cache names and sizes */
235 extern const struct kmalloc_info_struct {
236 	const char *name[NR_KMALLOC_TYPES];
237 	unsigned int size;
238 } kmalloc_info[];
239 
240 /* Kmalloc array related functions */
241 void setup_kmalloc_cache_index_table(void);
242 void create_kmalloc_caches(slab_flags_t);
243 
244 /* Find the kmalloc slab corresponding for a certain size */
245 struct kmem_cache *kmalloc_slab(size_t, gfp_t);
246 
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);
251 
252 gfp_t kmalloc_fix_flags(gfp_t flags);
253 
254 /* Functions provided by the slab allocators */
255 int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags);
256 
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);
263 
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 *));
267 struct kmem_cache *
268 __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
269 		   slab_flags_t flags, void (*ctor)(void *));
270 
271 slab_flags_t kmem_cache_flags(unsigned int object_size,
272 	slab_flags_t flags, const char *name);
273 
274 static inline bool is_kmalloc_cache(struct kmem_cache *s)
275 {
276 	return (s->flags & SLAB_KMALLOC);
277 }
278 
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 )
283 
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)
289 #else
290 #define SLAB_DEBUG_FLAGS (0)
291 #endif
292 
293 #if defined(CONFIG_SLAB)
294 #define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
295 			  SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
296 			  SLAB_ACCOUNT)
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)
301 #else
302 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
303 #endif
304 
305 /* Common flags available with current configuration */
306 #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
307 
308 /* Common flags permitted for kmem_cache_create */
309 #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
310 			      SLAB_RED_ZONE | \
311 			      SLAB_POISON | \
312 			      SLAB_STORE_USER | \
313 			      SLAB_TRACE | \
314 			      SLAB_CONSISTENCY_CHECKS | \
315 			      SLAB_MEM_SPREAD | \
316 			      SLAB_NOLEAKTRACE | \
317 			      SLAB_RECLAIM_ACCOUNT | \
318 			      SLAB_TEMPORARY | \
319 			      SLAB_ACCOUNT | \
320 			      SLAB_KMALLOC | \
321 			      SLAB_NO_USER_FLAGS)
322 
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 *);
328 
329 struct seq_file;
330 struct file;
331 
332 struct slabinfo {
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;
338 	unsigned int limit;
339 	unsigned int batchcount;
340 	unsigned int shared;
341 	unsigned int objects_per_slab;
342 	unsigned int cache_order;
343 };
344 
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);
349 
350 static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s)
351 {
352 	return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
353 		NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
354 }
355 
356 #ifdef CONFIG_SLUB_DEBUG
357 #ifdef CONFIG_SLUB_DEBUG_ON
358 DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
359 #else
360 DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
361 #endif
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)
365 {
366 	return static_branch_unlikely(&slub_debug_enabled);
367 }
368 #else
369 static inline void print_tracking(struct kmem_cache *s, void *object)
370 {
371 }
372 static inline bool __slub_debug_enabled(void)
373 {
374 	return false;
375 }
376 #endif
377 
378 /*
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
381  * the static key.
382  */
383 static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
384 {
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;
389 	return false;
390 }
391 
392 #ifdef CONFIG_MEMCG_KMEM
393 /*
394  * slab_objcgs - get the object cgroups vector associated with a slab
395  * @slab: a pointer to the slab struct
396  *
397  * Returns a pointer to the object cgroups vector associated with the slab,
398  * or NULL if no such vector has been associated yet.
399  */
400 static inline struct obj_cgroup **slab_objcgs(struct slab *slab)
401 {
402 	unsigned long memcg_data = READ_ONCE(slab->memcg_data);
403 
404 	VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS),
405 							slab_page(slab));
406 	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, slab_page(slab));
407 
408 	return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
409 }
410 
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);
415 
416 static inline void memcg_free_slab_cgroups(struct slab *slab)
417 {
418 	kfree(slab_objcgs(slab));
419 	slab->memcg_data = 0;
420 }
421 
422 static inline size_t obj_full_size(struct kmem_cache *s)
423 {
424 	/*
425 	 * For each accounted object there is an extra space which is used
426 	 * to store obj_cgroup membership. Charge it too.
427 	 */
428 	return s->size + sizeof(struct obj_cgroup *);
429 }
430 
431 /*
432  * Returns false if the allocation should fail.
433  */
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)
438 {
439 	struct obj_cgroup *objcg;
440 
441 	if (!memcg_kmem_online())
442 		return true;
443 
444 	if (!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT))
445 		return true;
446 
447 	objcg = get_obj_cgroup_from_current();
448 	if (!objcg)
449 		return true;
450 
451 	if (lru) {
452 		int ret;
453 		struct mem_cgroup *memcg;
454 
455 		memcg = get_mem_cgroup_from_objcg(objcg);
456 		ret = memcg_list_lru_alloc(memcg, lru, flags);
457 		css_put(&memcg->css);
458 
459 		if (ret)
460 			goto out;
461 	}
462 
463 	if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s)))
464 		goto out;
465 
466 	*objcgp = objcg;
467 	return true;
468 out:
469 	obj_cgroup_put(objcg);
470 	return false;
471 }
472 
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,
476 					      void **p)
477 {
478 	struct slab *slab;
479 	unsigned long off;
480 	size_t i;
481 
482 	if (!memcg_kmem_online() || !objcg)
483 		return;
484 
485 	for (i = 0; i < size; i++) {
486 		if (likely(p[i])) {
487 			slab = virt_to_slab(p[i]);
488 
489 			if (!slab_objcgs(slab) &&
490 			    memcg_alloc_slab_cgroups(slab, s, flags,
491 							 false)) {
492 				obj_cgroup_uncharge(objcg, obj_full_size(s));
493 				continue;
494 			}
495 
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));
501 		} else {
502 			obj_cgroup_uncharge(objcg, obj_full_size(s));
503 		}
504 	}
505 	obj_cgroup_put(objcg);
506 }
507 
508 static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
509 					void **p, int objects)
510 {
511 	struct obj_cgroup **objcgs;
512 	int i;
513 
514 	if (!memcg_kmem_online())
515 		return;
516 
517 	objcgs = slab_objcgs(slab);
518 	if (!objcgs)
519 		return;
520 
521 	for (i = 0; i < objects; i++) {
522 		struct obj_cgroup *objcg;
523 		unsigned int off;
524 
525 		off = obj_to_index(s, slab, p[i]);
526 		objcg = objcgs[off];
527 		if (!objcg)
528 			continue;
529 
530 		objcgs[off] = NULL;
531 		obj_cgroup_uncharge(objcg, obj_full_size(s));
532 		mod_objcg_state(objcg, slab_pgdat(slab), cache_vmstat_idx(s),
533 				-obj_full_size(s));
534 		obj_cgroup_put(objcg);
535 	}
536 }
537 
538 #else /* CONFIG_MEMCG_KMEM */
539 static inline struct obj_cgroup **slab_objcgs(struct slab *slab)
540 {
541 	return NULL;
542 }
543 
544 static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
545 {
546 	return NULL;
547 }
548 
549 static inline int memcg_alloc_slab_cgroups(struct slab *slab,
550 					       struct kmem_cache *s, gfp_t gfp,
551 					       bool new_slab)
552 {
553 	return 0;
554 }
555 
556 static inline void memcg_free_slab_cgroups(struct slab *slab)
557 {
558 }
559 
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)
564 {
565 	return true;
566 }
567 
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,
571 					      void **p)
572 {
573 }
574 
575 static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
576 					void **p, int objects)
577 {
578 }
579 #endif /* CONFIG_MEMCG_KMEM */
580 
581 static inline struct kmem_cache *virt_to_cache(const void *obj)
582 {
583 	struct slab *slab;
584 
585 	slab = virt_to_slab(obj);
586 	if (WARN_ONCE(!slab, "%s: Object is not a Slab page!\n",
587 					__func__))
588 		return NULL;
589 	return slab->slab_cache;
590 }
591 
592 static __always_inline void account_slab(struct slab *slab, int order,
593 					 struct kmem_cache *s, gfp_t gfp)
594 {
595 	if (memcg_kmem_online() && (s->flags & SLAB_ACCOUNT))
596 		memcg_alloc_slab_cgroups(slab, s, gfp, true);
597 
598 	mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
599 			    PAGE_SIZE << order);
600 }
601 
602 static __always_inline void unaccount_slab(struct slab *slab, int order,
603 					   struct kmem_cache *s)
604 {
605 	if (memcg_kmem_online())
606 		memcg_free_slab_cgroups(slab);
607 
608 	mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
609 			    -(PAGE_SIZE << order));
610 }
611 
612 static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
613 {
614 	struct kmem_cache *cachep;
615 
616 	if (!IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
617 	    !kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS))
618 		return s;
619 
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);
625 	return cachep;
626 }
627 
628 void free_large_kmalloc(struct folio *folio, void *object);
629 
630 size_t __ksize(const void *objp);
631 
632 static inline size_t slab_ksize(const struct kmem_cache *s)
633 {
634 #ifndef CONFIG_SLUB
635 	return s->object_size;
636 
637 #else /* CONFIG_SLUB */
638 # ifdef CONFIG_SLUB_DEBUG
639 	/*
640 	 * Debugging requires use of the padding between object
641 	 * and whatever may come after it.
642 	 */
643 	if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
644 		return s->object_size;
645 # endif
646 	if (s->flags & SLAB_KASAN)
647 		return s->object_size;
648 	/*
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.
652 	 */
653 	if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
654 		return s->inuse;
655 	/*
656 	 * Else we can use all the padding etc for the allocation
657 	 */
658 	return s->size;
659 #endif
660 }
661 
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)
666 {
667 	flags &= gfp_allowed_mask;
668 
669 	might_alloc(flags);
670 
671 	if (should_failslab(s, flags))
672 		return NULL;
673 
674 	if (!memcg_slab_pre_alloc_hook(s, lru, objcgp, size, flags))
675 		return NULL;
676 
677 	return s;
678 }
679 
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)
684 {
685 	unsigned int zero_size = s->object_size;
686 	size_t i;
687 
688 	flags &= gfp_allowed_mask;
689 
690 	/*
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.
697 	 */
698 	if (kmem_cache_debug_flags(s, SLAB_STORE_USER | SLAB_RED_ZONE) &&
699 	    (s->flags & SLAB_KMALLOC))
700 		zero_size = orig_size;
701 
702 	/*
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.
706 	 *
707 	 * As p[i] might get tagged, memset and kmemleak hook come after KASAN.
708 	 */
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,
714 					 s->flags, flags);
715 		kmsan_slab_alloc(s, p[i], flags);
716 	}
717 
718 	memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
719 }
720 
721 /*
722  * The slab lists for all objects.
723  */
724 struct kmem_cache_node {
725 #ifdef CONFIG_SLAB
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 */
739 #endif
740 
741 #ifdef CONFIG_SLUB
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;
749 #endif
750 #endif
751 
752 };
753 
754 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
755 {
756 	return s->node[node];
757 }
758 
759 /*
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)
762  */
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)))
766 
767 
768 #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
769 void dump_unreclaimable_slab(void);
770 #else
771 static inline void dump_unreclaimable_slab(void)
772 {
773 }
774 #endif
775 
776 void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
777 
778 #ifdef CONFIG_SLAB_FREELIST_RANDOM
779 int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
780 			gfp_t gfp);
781 void cache_random_seq_destroy(struct kmem_cache *cachep);
782 #else
783 static inline int cache_random_seq_create(struct kmem_cache *cachep,
784 					unsigned int count, gfp_t gfp)
785 {
786 	return 0;
787 }
788 static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
789 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
790 
791 static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
792 {
793 	if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
794 				&init_on_alloc)) {
795 		if (c->ctor)
796 			return false;
797 		if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
798 			return flags & __GFP_ZERO;
799 		return true;
800 	}
801 	return flags & __GFP_ZERO;
802 }
803 
804 static inline bool slab_want_init_on_free(struct kmem_cache *c)
805 {
806 	if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
807 				&init_on_free))
808 		return !(c->ctor ||
809 			 (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
810 	return false;
811 }
812 
813 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
814 void debugfs_slab_release(struct kmem_cache *);
815 #else
816 static inline void debugfs_slab_release(struct kmem_cache *s) { }
817 #endif
818 
819 #ifdef CONFIG_PRINTK
820 #define KS_ADDRS_COUNT 16
821 struct kmem_obj_info {
822 	void *kp_ptr;
823 	struct slab *kp_slab;
824 	void *kp_objp;
825 	unsigned long kp_data_offset;
826 	struct kmem_cache *kp_slab_cache;
827 	void *kp_ret;
828 	void *kp_stack[KS_ADDRS_COUNT];
829 	void *kp_free_stack[KS_ADDRS_COUNT];
830 };
831 void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
832 #endif
833 
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);
837 #else
838 static inline
839 void __check_heap_object(const void *ptr, unsigned long n,
840 			 const struct slab *slab, bool to_user)
841 {
842 }
843 #endif
844 
845 #ifdef CONFIG_SLUB_DEBUG
846 void skip_orig_size_check(struct kmem_cache *s, const void *object);
847 #endif
848 
849 #endif /* MM_SLAB_H */
850