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