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