• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef CONFIG_SLOB
9 /*
10  * Common fields provided in kmem_cache by all slab allocators
11  * This struct is either used directly by the allocator (SLOB)
12  * or the allocator must include definitions for all fields
13  * provided in kmem_cache_common in their definition of kmem_cache.
14  *
15  * Once we can do anonymous structs (C11 standard) we could put a
16  * anonymous struct definition in these allocators so that the
17  * separate allocations in the kmem_cache structure of SLAB and
18  * SLUB is no longer needed.
19  */
20 struct kmem_cache {
21 	unsigned int object_size;/* The original size of the object */
22 	unsigned int size;	/* The aligned/padded/added on size  */
23 	unsigned int align;	/* Alignment as calculated */
24 	slab_flags_t flags;	/* Active flags on the slab */
25 	unsigned int useroffset;/* Usercopy region offset */
26 	unsigned int usersize;	/* Usercopy region size */
27 	const char *name;	/* Slab name for sysfs */
28 	int refcount;		/* Use counter */
29 	void (*ctor)(void *);	/* Called on object slot creation */
30 	struct list_head list;	/* List of all slab caches on the system */
31 };
32 
33 #endif /* CONFIG_SLOB */
34 
35 #ifdef CONFIG_SLAB
36 #include <linux/slab_def.h>
37 #endif
38 
39 #ifdef CONFIG_SLUB
40 #include <linux/slub_def.h>
41 #endif
42 
43 #include <linux/memcontrol.h>
44 #include <linux/fault-inject.h>
45 #include <linux/kasan.h>
46 #include <linux/kmemleak.h>
47 #include <linux/random.h>
48 #include <linux/sched/mm.h>
49 
50 /*
51  * State of the slab allocator.
52  *
53  * This is used to describe the states of the allocator during bootup.
54  * Allocators use this to gradually bootstrap themselves. Most allocators
55  * have the problem that the structures used for managing slab caches are
56  * allocated from slab caches themselves.
57  */
58 enum slab_state {
59 	DOWN,			/* No slab functionality yet */
60 	PARTIAL,		/* SLUB: kmem_cache_node available */
61 	PARTIAL_NODE,		/* SLAB: kmalloc size for node struct available */
62 	UP,			/* Slab caches usable but not all extras yet */
63 	FULL			/* Everything is working */
64 };
65 
66 extern enum slab_state slab_state;
67 
68 /* The slab cache mutex protects the management structures during changes */
69 extern struct mutex slab_mutex;
70 
71 /* The list of all slab caches on the system */
72 extern struct list_head slab_caches;
73 
74 /* The slab cache that manages slab cache information */
75 extern struct kmem_cache *kmem_cache;
76 
77 /* A table of kmalloc cache names and sizes */
78 extern const struct kmalloc_info_struct {
79 	const char *name[NR_KMALLOC_TYPES];
80 	unsigned int size;
81 } kmalloc_info[];
82 
83 #ifndef CONFIG_SLOB
84 /* Kmalloc array related functions */
85 void setup_kmalloc_cache_index_table(void);
86 void create_kmalloc_caches(slab_flags_t);
87 
88 /* Find the kmalloc slab corresponding for a certain size */
89 struct kmem_cache *kmalloc_slab(size_t, gfp_t);
90 #endif
91 
92 gfp_t kmalloc_fix_flags(gfp_t flags);
93 
94 #ifdef CONFIG_SLUB
95 /*
96  * Tracking user of a slab.
97  */
98 #define TRACK_ADDRS_COUNT 16
99 struct track {
100 	unsigned long addr;	/* Called from address */
101 #ifdef CONFIG_STACKTRACE
102 	unsigned long addrs[TRACK_ADDRS_COUNT]; /* Called from address */
103 #endif
104 	int cpu;		/* Was running on cpu */
105 	int pid;		/* Pid context */
106 	unsigned long when;	/* When did the operation occur */
107 };
108 
109 enum track_item { TRACK_ALLOC, TRACK_FREE };
110 #endif
111 
112 /* Functions provided by the slab allocators */
113 int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags);
114 
115 struct kmem_cache *create_kmalloc_cache(const char *name, unsigned int size,
116 			slab_flags_t flags, unsigned int useroffset,
117 			unsigned int usersize);
118 extern void create_boot_cache(struct kmem_cache *, const char *name,
119 			unsigned int size, slab_flags_t flags,
120 			unsigned int useroffset, unsigned int usersize);
121 
122 int slab_unmergeable(struct kmem_cache *s);
123 struct kmem_cache *find_mergeable(unsigned size, unsigned align,
124 		slab_flags_t flags, const char *name, void (*ctor)(void *));
125 #ifndef CONFIG_SLOB
126 struct kmem_cache *
127 __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
128 		   slab_flags_t flags, void (*ctor)(void *));
129 
130 slab_flags_t kmem_cache_flags(unsigned int object_size,
131 	slab_flags_t flags, const char *name);
132 #else
133 static inline struct kmem_cache *
__kmem_cache_alias(const char * name,unsigned int size,unsigned int align,slab_flags_t flags,void (* ctor)(void *))134 __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
135 		   slab_flags_t flags, void (*ctor)(void *))
136 { return NULL; }
137 
kmem_cache_flags(unsigned int object_size,slab_flags_t flags,const char * name)138 static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
139 	slab_flags_t flags, const char *name)
140 {
141 	return flags;
142 }
143 #endif
144 
145 
146 /* Legal flag mask for kmem_cache_create(), for various configurations */
147 #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
148 			 SLAB_CACHE_DMA32 | SLAB_PANIC | \
149 			 SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS )
150 
151 #if defined(CONFIG_DEBUG_SLAB)
152 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
153 #elif defined(CONFIG_SLUB_DEBUG)
154 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
155 			  SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
156 #else
157 #define SLAB_DEBUG_FLAGS (0)
158 #endif
159 
160 #if defined(CONFIG_SLAB)
161 #define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
162 			  SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
163 			  SLAB_ACCOUNT)
164 #elif defined(CONFIG_SLUB)
165 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
166 			  SLAB_TEMPORARY | SLAB_ACCOUNT)
167 #else
168 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
169 #endif
170 
171 /* Common flags available with current configuration */
172 #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
173 
174 /* Common flags permitted for kmem_cache_create */
175 #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
176 			      SLAB_RED_ZONE | \
177 			      SLAB_POISON | \
178 			      SLAB_STORE_USER | \
179 			      SLAB_TRACE | \
180 			      SLAB_CONSISTENCY_CHECKS | \
181 			      SLAB_MEM_SPREAD | \
182 			      SLAB_NOLEAKTRACE | \
183 			      SLAB_RECLAIM_ACCOUNT | \
184 			      SLAB_TEMPORARY | \
185 			      SLAB_ACCOUNT)
186 
187 bool __kmem_cache_empty(struct kmem_cache *);
188 int __kmem_cache_shutdown(struct kmem_cache *);
189 void __kmem_cache_release(struct kmem_cache *);
190 int __kmem_cache_shrink(struct kmem_cache *);
191 void slab_kmem_cache_release(struct kmem_cache *);
192 
193 struct seq_file;
194 struct file;
195 
196 struct slabinfo {
197 	unsigned long active_objs;
198 	unsigned long num_objs;
199 	unsigned long active_slabs;
200 	unsigned long num_slabs;
201 	unsigned long shared_avail;
202 	unsigned int limit;
203 	unsigned int batchcount;
204 	unsigned int shared;
205 	unsigned int objects_per_slab;
206 	unsigned int cache_order;
207 };
208 
209 void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
210 void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
211 ssize_t slabinfo_write(struct file *file, const char __user *buffer,
212 		       size_t count, loff_t *ppos);
213 
214 /*
215  * Generic implementation of bulk operations
216  * These are useful for situations in which the allocator cannot
217  * perform optimizations. In that case segments of the object listed
218  * may be allocated or freed using these operations.
219  */
220 void __kmem_cache_free_bulk(struct kmem_cache *, size_t, void **);
221 int __kmem_cache_alloc_bulk(struct kmem_cache *, gfp_t, size_t, void **);
222 
cache_vmstat_idx(struct kmem_cache * s)223 static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s)
224 {
225 	return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
226 		NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
227 }
228 
229 #ifdef CONFIG_SLUB_DEBUG
230 #ifdef CONFIG_SLUB_DEBUG_ON
231 DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
232 #else
233 DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
234 #endif
235 extern void print_tracking(struct kmem_cache *s, void *object);
236 long validate_slab_cache(struct kmem_cache *s);
237 extern unsigned long get_each_object_track(struct kmem_cache *s,
238 		struct page *page, enum track_item alloc,
239 		int (*fn)(const struct kmem_cache *, const void *,
240 		const struct track *, void *), void *private);
__slub_debug_enabled(void)241 static inline bool __slub_debug_enabled(void)
242 {
243 	return static_branch_unlikely(&slub_debug_enabled);
244 }
245 #else
print_tracking(struct kmem_cache * s,void * object)246 static inline void print_tracking(struct kmem_cache *s, void *object)
247 {
248 }
249 #ifdef CONFIG_SLUB
get_each_object_track(struct kmem_cache * s,struct page * page,enum track_item alloc,int (* fn)(const struct kmem_cache *,const void *,const struct track *,void *),void * private)250 static inline unsigned long get_each_object_track(struct kmem_cache *s,
251 		struct page *page, enum track_item alloc,
252 		int (*fn)(const struct kmem_cache *, const void *,
253 		const struct track *, void *), void *private)
254 {
255 	return 0;
256 }
257 #endif
__slub_debug_enabled(void)258 static inline bool __slub_debug_enabled(void)
259 {
260 	return false;
261 }
262 #endif
263 
264 /*
265  * Returns true if any of the specified slub_debug flags is enabled for the
266  * cache. Use only for flags parsed by setup_slub_debug() as it also enables
267  * the static key.
268  */
kmem_cache_debug_flags(struct kmem_cache * s,slab_flags_t flags)269 static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
270 {
271 	if (IS_ENABLED(CONFIG_SLUB_DEBUG))
272 		VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
273 	if (__slub_debug_enabled())
274 		return s->flags & flags;
275 	return false;
276 }
277 
278 #ifdef CONFIG_MEMCG_KMEM
279 int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
280 				 gfp_t gfp, bool new_page);
281 void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
282 		     enum node_stat_item idx, int nr);
283 
memcg_free_page_obj_cgroups(struct page * page)284 static inline void memcg_free_page_obj_cgroups(struct page *page)
285 {
286 	kfree(page_objcgs(page));
287 	page->memcg_data = 0;
288 }
289 
obj_full_size(struct kmem_cache * s)290 static inline size_t obj_full_size(struct kmem_cache *s)
291 {
292 	/*
293 	 * For each accounted object there is an extra space which is used
294 	 * to store obj_cgroup membership. Charge it too.
295 	 */
296 	return s->size + sizeof(struct obj_cgroup *);
297 }
298 
299 /*
300  * Returns false if the allocation should fail.
301  */
memcg_slab_pre_alloc_hook(struct kmem_cache * s,struct obj_cgroup ** objcgp,size_t objects,gfp_t flags)302 static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
303 					     struct obj_cgroup **objcgp,
304 					     size_t objects, gfp_t flags)
305 {
306 	struct obj_cgroup *objcg;
307 
308 	if (!memcg_kmem_enabled())
309 		return true;
310 
311 	if (!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT))
312 		return true;
313 
314 	objcg = get_obj_cgroup_from_current();
315 	if (!objcg)
316 		return true;
317 
318 	if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s))) {
319 		obj_cgroup_put(objcg);
320 		return false;
321 	}
322 
323 	*objcgp = objcg;
324 	return true;
325 }
326 
memcg_slab_post_alloc_hook(struct kmem_cache * s,struct obj_cgroup * objcg,gfp_t flags,size_t size,void ** p)327 static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
328 					      struct obj_cgroup *objcg,
329 					      gfp_t flags, size_t size,
330 					      void **p)
331 {
332 	struct page *page;
333 	unsigned long off;
334 	size_t i;
335 
336 	if (!memcg_kmem_enabled() || !objcg)
337 		return;
338 
339 	for (i = 0; i < size; i++) {
340 		if (likely(p[i])) {
341 			page = virt_to_head_page(p[i]);
342 
343 			if (!page_objcgs(page) &&
344 			    memcg_alloc_page_obj_cgroups(page, s, flags,
345 							 false)) {
346 				obj_cgroup_uncharge(objcg, obj_full_size(s));
347 				continue;
348 			}
349 
350 			off = obj_to_index(s, page, p[i]);
351 			obj_cgroup_get(objcg);
352 			page_objcgs(page)[off] = objcg;
353 			mod_objcg_state(objcg, page_pgdat(page),
354 					cache_vmstat_idx(s), obj_full_size(s));
355 		} else {
356 			obj_cgroup_uncharge(objcg, obj_full_size(s));
357 		}
358 	}
359 	obj_cgroup_put(objcg);
360 }
361 
memcg_slab_free_hook(struct kmem_cache * s_orig,void ** p,int objects)362 static inline void memcg_slab_free_hook(struct kmem_cache *s_orig,
363 					void **p, int objects)
364 {
365 	struct kmem_cache *s;
366 	struct obj_cgroup **objcgs;
367 	struct obj_cgroup *objcg;
368 	struct page *page;
369 	unsigned int off;
370 	int i;
371 
372 	if (!memcg_kmem_enabled())
373 		return;
374 
375 	for (i = 0; i < objects; i++) {
376 		if (unlikely(!p[i]))
377 			continue;
378 
379 		page = virt_to_head_page(p[i]);
380 		objcgs = page_objcgs_check(page);
381 		if (!objcgs)
382 			continue;
383 
384 		if (!s_orig)
385 			s = page->slab_cache;
386 		else
387 			s = s_orig;
388 
389 		off = obj_to_index(s, page, p[i]);
390 		objcg = objcgs[off];
391 		if (!objcg)
392 			continue;
393 
394 		objcgs[off] = NULL;
395 		obj_cgroup_uncharge(objcg, obj_full_size(s));
396 		mod_objcg_state(objcg, page_pgdat(page), cache_vmstat_idx(s),
397 				-obj_full_size(s));
398 		obj_cgroup_put(objcg);
399 	}
400 }
401 
402 #else /* CONFIG_MEMCG_KMEM */
memcg_from_slab_obj(void * ptr)403 static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
404 {
405 	return NULL;
406 }
407 
memcg_alloc_page_obj_cgroups(struct page * page,struct kmem_cache * s,gfp_t gfp,bool new_page)408 static inline int memcg_alloc_page_obj_cgroups(struct page *page,
409 					       struct kmem_cache *s, gfp_t gfp,
410 					       bool new_page)
411 {
412 	return 0;
413 }
414 
memcg_free_page_obj_cgroups(struct page * page)415 static inline void memcg_free_page_obj_cgroups(struct page *page)
416 {
417 }
418 
memcg_slab_pre_alloc_hook(struct kmem_cache * s,struct obj_cgroup ** objcgp,size_t objects,gfp_t flags)419 static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
420 					     struct obj_cgroup **objcgp,
421 					     size_t objects, gfp_t flags)
422 {
423 	return true;
424 }
425 
memcg_slab_post_alloc_hook(struct kmem_cache * s,struct obj_cgroup * objcg,gfp_t flags,size_t size,void ** p)426 static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
427 					      struct obj_cgroup *objcg,
428 					      gfp_t flags, size_t size,
429 					      void **p)
430 {
431 }
432 
memcg_slab_free_hook(struct kmem_cache * s,void ** p,int objects)433 static inline void memcg_slab_free_hook(struct kmem_cache *s,
434 					void **p, int objects)
435 {
436 }
437 #endif /* CONFIG_MEMCG_KMEM */
438 
virt_to_cache(const void * obj)439 static inline struct kmem_cache *virt_to_cache(const void *obj)
440 {
441 	struct page *page;
442 
443 	page = virt_to_head_page(obj);
444 	if (WARN_ONCE(!PageSlab(page), "%s: Object is not a Slab page!\n",
445 					__func__))
446 		return NULL;
447 	return page->slab_cache;
448 }
449 
account_slab_page(struct page * page,int order,struct kmem_cache * s,gfp_t gfp)450 static __always_inline void account_slab_page(struct page *page, int order,
451 					      struct kmem_cache *s,
452 					      gfp_t gfp)
453 {
454 	if (memcg_kmem_enabled() && (s->flags & SLAB_ACCOUNT))
455 		memcg_alloc_page_obj_cgroups(page, s, gfp, true);
456 
457 	mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
458 			    PAGE_SIZE << order);
459 }
460 
unaccount_slab_page(struct page * page,int order,struct kmem_cache * s)461 static __always_inline void unaccount_slab_page(struct page *page, int order,
462 						struct kmem_cache *s)
463 {
464 	if (memcg_kmem_enabled())
465 		memcg_free_page_obj_cgroups(page);
466 
467 	mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
468 			    -(PAGE_SIZE << order));
469 }
470 
cache_from_obj(struct kmem_cache * s,void * x)471 static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
472 {
473 	struct kmem_cache *cachep;
474 
475 	if (!IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
476 	    !kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS))
477 		return s;
478 
479 	cachep = virt_to_cache(x);
480 	if (WARN(cachep && cachep != s,
481 		  "%s: Wrong slab cache. %s but object is from %s\n",
482 		  __func__, s->name, cachep->name))
483 		print_tracking(cachep, x);
484 	return cachep;
485 }
486 
slab_ksize(const struct kmem_cache * s)487 static inline size_t slab_ksize(const struct kmem_cache *s)
488 {
489 #ifndef CONFIG_SLUB
490 	return s->object_size;
491 
492 #else /* CONFIG_SLUB */
493 # ifdef CONFIG_SLUB_DEBUG
494 	/*
495 	 * Debugging requires use of the padding between object
496 	 * and whatever may come after it.
497 	 */
498 	if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
499 		return s->object_size;
500 # endif
501 	if (s->flags & SLAB_KASAN)
502 		return s->object_size;
503 	/*
504 	 * If we have the need to store the freelist pointer
505 	 * back there or track user information then we can
506 	 * only use the space before that information.
507 	 */
508 	if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
509 		return s->inuse;
510 	/*
511 	 * Else we can use all the padding etc for the allocation
512 	 */
513 	return s->size;
514 #endif
515 }
516 
slab_pre_alloc_hook(struct kmem_cache * s,struct obj_cgroup ** objcgp,size_t size,gfp_t flags)517 static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
518 						     struct obj_cgroup **objcgp,
519 						     size_t size, gfp_t flags)
520 {
521 	flags &= gfp_allowed_mask;
522 
523 	might_alloc(flags);
524 
525 	if (should_failslab(s, flags))
526 		return NULL;
527 
528 	if (!memcg_slab_pre_alloc_hook(s, objcgp, size, flags))
529 		return NULL;
530 
531 	return s;
532 }
533 
slab_post_alloc_hook(struct kmem_cache * s,struct obj_cgroup * objcg,gfp_t flags,size_t size,void ** p,bool init)534 static inline void slab_post_alloc_hook(struct kmem_cache *s,
535 					struct obj_cgroup *objcg, gfp_t flags,
536 					size_t size, void **p, bool init)
537 {
538 	size_t i;
539 
540 	flags &= gfp_allowed_mask;
541 
542 	/*
543 	 * As memory initialization might be integrated into KASAN,
544 	 * kasan_slab_alloc and initialization memset must be
545 	 * kept together to avoid discrepancies in behavior.
546 	 *
547 	 * As p[i] might get tagged, memset and kmemleak hook come after KASAN.
548 	 */
549 	for (i = 0; i < size; i++) {
550 		p[i] = kasan_slab_alloc(s, p[i], flags, init);
551 		if (p[i] && init && !kasan_has_integrated_init())
552 			memset(p[i], 0, s->object_size);
553 		kmemleak_alloc_recursive(p[i], s->object_size, 1,
554 					 s->flags, flags);
555 	}
556 
557 	memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
558 }
559 
560 #ifndef CONFIG_SLOB
561 /*
562  * The slab lists for all objects.
563  */
564 struct kmem_cache_node {
565 	spinlock_t list_lock;
566 
567 #ifdef CONFIG_SLAB
568 	struct list_head slabs_partial;	/* partial list first, better asm code */
569 	struct list_head slabs_full;
570 	struct list_head slabs_free;
571 	unsigned long total_slabs;	/* length of all slab lists */
572 	unsigned long free_slabs;	/* length of free slab list only */
573 	unsigned long free_objects;
574 	unsigned int free_limit;
575 	unsigned int colour_next;	/* Per-node cache coloring */
576 	struct array_cache *shared;	/* shared per node */
577 	struct alien_cache **alien;	/* on other nodes */
578 	unsigned long next_reap;	/* updated without locking */
579 	int free_touched;		/* updated without locking */
580 #endif
581 
582 #ifdef CONFIG_SLUB
583 	unsigned long nr_partial;
584 	struct list_head partial;
585 #ifdef CONFIG_SLUB_DEBUG
586 	atomic_long_t nr_slabs;
587 	atomic_long_t total_objects;
588 	struct list_head full;
589 #endif
590 #endif
591 
592 };
593 
get_node(struct kmem_cache * s,int node)594 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
595 {
596 	return s->node[node];
597 }
598 
599 /*
600  * Iterator over all nodes. The body will be executed for each node that has
601  * a kmem_cache_node structure allocated (which is true for all online nodes)
602  */
603 #define for_each_kmem_cache_node(__s, __node, __n) \
604 	for (__node = 0; __node < nr_node_ids; __node++) \
605 		 if ((__n = get_node(__s, __node)))
606 
607 #endif
608 
609 void *slab_start(struct seq_file *m, loff_t *pos);
610 void *slab_next(struct seq_file *m, void *p, loff_t *pos);
611 void slab_stop(struct seq_file *m, void *p);
612 int memcg_slab_show(struct seq_file *m, void *p);
613 
614 #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
615 void dump_unreclaimable_slab(void);
616 #else
dump_unreclaimable_slab(void)617 static inline void dump_unreclaimable_slab(void)
618 {
619 }
620 #endif
621 
622 void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
623 
624 #ifdef CONFIG_SLAB_FREELIST_RANDOM
625 int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
626 			gfp_t gfp);
627 void cache_random_seq_destroy(struct kmem_cache *cachep);
628 #else
cache_random_seq_create(struct kmem_cache * cachep,unsigned int count,gfp_t gfp)629 static inline int cache_random_seq_create(struct kmem_cache *cachep,
630 					unsigned int count, gfp_t gfp)
631 {
632 	return 0;
633 }
cache_random_seq_destroy(struct kmem_cache * cachep)634 static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
635 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
636 
slab_want_init_on_alloc(gfp_t flags,struct kmem_cache * c)637 static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
638 {
639 	if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
640 				&init_on_alloc)) {
641 		if (c->ctor)
642 			return false;
643 		if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
644 			return flags & __GFP_ZERO;
645 		return true;
646 	}
647 	return flags & __GFP_ZERO;
648 }
649 
slab_want_init_on_free(struct kmem_cache * c)650 static inline bool slab_want_init_on_free(struct kmem_cache *c)
651 {
652 	if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
653 				&init_on_free))
654 		return !(c->ctor ||
655 			 (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
656 	return false;
657 }
658 
659 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
660 void debugfs_slab_release(struct kmem_cache *);
661 #else
debugfs_slab_release(struct kmem_cache * s)662 static inline void debugfs_slab_release(struct kmem_cache *s) { }
663 #endif
664 
665 #ifdef CONFIG_PRINTK
666 #define KS_ADDRS_COUNT 16
667 struct kmem_obj_info {
668 	void *kp_ptr;
669 	struct page *kp_page;
670 	void *kp_objp;
671 	unsigned long kp_data_offset;
672 	struct kmem_cache *kp_slab_cache;
673 	void *kp_ret;
674 	void *kp_stack[KS_ADDRS_COUNT];
675 	void *kp_free_stack[KS_ADDRS_COUNT];
676 };
677 void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct page *page);
678 #endif
679 
680 #endif /* MM_SLAB_H */
681