162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef MM_SLAB_H
362306a36Sopenharmony_ci#define MM_SLAB_H
462306a36Sopenharmony_ci/*
562306a36Sopenharmony_ci * Internal slab definitions
662306a36Sopenharmony_ci */
762306a36Sopenharmony_civoid __init kmem_cache_init(void);
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifdef CONFIG_64BIT
1062306a36Sopenharmony_ci# ifdef system_has_cmpxchg128
1162306a36Sopenharmony_ci# define system_has_freelist_aba()	system_has_cmpxchg128()
1262306a36Sopenharmony_ci# define try_cmpxchg_freelist		try_cmpxchg128
1362306a36Sopenharmony_ci# endif
1462306a36Sopenharmony_ci#define this_cpu_try_cmpxchg_freelist	this_cpu_try_cmpxchg128
1562306a36Sopenharmony_citypedef u128 freelist_full_t;
1662306a36Sopenharmony_ci#else /* CONFIG_64BIT */
1762306a36Sopenharmony_ci# ifdef system_has_cmpxchg64
1862306a36Sopenharmony_ci# define system_has_freelist_aba()	system_has_cmpxchg64()
1962306a36Sopenharmony_ci# define try_cmpxchg_freelist		try_cmpxchg64
2062306a36Sopenharmony_ci# endif
2162306a36Sopenharmony_ci#define this_cpu_try_cmpxchg_freelist	this_cpu_try_cmpxchg64
2262306a36Sopenharmony_citypedef u64 freelist_full_t;
2362306a36Sopenharmony_ci#endif /* CONFIG_64BIT */
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#if defined(system_has_freelist_aba) && !defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
2662306a36Sopenharmony_ci#undef system_has_freelist_aba
2762306a36Sopenharmony_ci#endif
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/*
3062306a36Sopenharmony_ci * Freelist pointer and counter to cmpxchg together, avoids the typical ABA
3162306a36Sopenharmony_ci * problems with cmpxchg of just a pointer.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_citypedef union {
3462306a36Sopenharmony_ci	struct {
3562306a36Sopenharmony_ci		void *freelist;
3662306a36Sopenharmony_ci		unsigned long counter;
3762306a36Sopenharmony_ci	};
3862306a36Sopenharmony_ci	freelist_full_t full;
3962306a36Sopenharmony_ci} freelist_aba_t;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/* Reuses the bits in struct page */
4262306a36Sopenharmony_cistruct slab {
4362306a36Sopenharmony_ci	unsigned long __page_flags;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#if defined(CONFIG_SLAB)
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	struct kmem_cache *slab_cache;
4862306a36Sopenharmony_ci	union {
4962306a36Sopenharmony_ci		struct {
5062306a36Sopenharmony_ci			struct list_head slab_list;
5162306a36Sopenharmony_ci			void *freelist;	/* array of free object indexes */
5262306a36Sopenharmony_ci			void *s_mem;	/* first object */
5362306a36Sopenharmony_ci		};
5462306a36Sopenharmony_ci		struct rcu_head rcu_head;
5562306a36Sopenharmony_ci	};
5662306a36Sopenharmony_ci	unsigned int active;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci#elif defined(CONFIG_SLUB)
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	struct kmem_cache *slab_cache;
6162306a36Sopenharmony_ci	union {
6262306a36Sopenharmony_ci		struct {
6362306a36Sopenharmony_ci			union {
6462306a36Sopenharmony_ci				struct list_head slab_list;
6562306a36Sopenharmony_ci#ifdef CONFIG_SLUB_CPU_PARTIAL
6662306a36Sopenharmony_ci				struct {
6762306a36Sopenharmony_ci					struct slab *next;
6862306a36Sopenharmony_ci					int slabs;	/* Nr of slabs left */
6962306a36Sopenharmony_ci				};
7062306a36Sopenharmony_ci#endif
7162306a36Sopenharmony_ci			};
7262306a36Sopenharmony_ci			/* Double-word boundary */
7362306a36Sopenharmony_ci			union {
7462306a36Sopenharmony_ci				struct {
7562306a36Sopenharmony_ci					void *freelist;		/* first free object */
7662306a36Sopenharmony_ci					union {
7762306a36Sopenharmony_ci						unsigned long counters;
7862306a36Sopenharmony_ci						struct {
7962306a36Sopenharmony_ci							unsigned inuse:16;
8062306a36Sopenharmony_ci							unsigned objects:15;
8162306a36Sopenharmony_ci							unsigned frozen:1;
8262306a36Sopenharmony_ci						};
8362306a36Sopenharmony_ci					};
8462306a36Sopenharmony_ci				};
8562306a36Sopenharmony_ci#ifdef system_has_freelist_aba
8662306a36Sopenharmony_ci				freelist_aba_t freelist_counter;
8762306a36Sopenharmony_ci#endif
8862306a36Sopenharmony_ci			};
8962306a36Sopenharmony_ci		};
9062306a36Sopenharmony_ci		struct rcu_head rcu_head;
9162306a36Sopenharmony_ci	};
9262306a36Sopenharmony_ci	unsigned int __unused;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci#else
9562306a36Sopenharmony_ci#error "Unexpected slab allocator configured"
9662306a36Sopenharmony_ci#endif
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	atomic_t __page_refcount;
9962306a36Sopenharmony_ci#ifdef CONFIG_MEMCG
10062306a36Sopenharmony_ci	unsigned long memcg_data;
10162306a36Sopenharmony_ci#endif
10262306a36Sopenharmony_ci};
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci#define SLAB_MATCH(pg, sl)						\
10562306a36Sopenharmony_ci	static_assert(offsetof(struct page, pg) == offsetof(struct slab, sl))
10662306a36Sopenharmony_ciSLAB_MATCH(flags, __page_flags);
10762306a36Sopenharmony_ciSLAB_MATCH(compound_head, slab_cache);	/* Ensure bit 0 is clear */
10862306a36Sopenharmony_ciSLAB_MATCH(_refcount, __page_refcount);
10962306a36Sopenharmony_ci#ifdef CONFIG_MEMCG
11062306a36Sopenharmony_ciSLAB_MATCH(memcg_data, memcg_data);
11162306a36Sopenharmony_ci#endif
11262306a36Sopenharmony_ci#undef SLAB_MATCH
11362306a36Sopenharmony_cistatic_assert(sizeof(struct slab) <= sizeof(struct page));
11462306a36Sopenharmony_ci#if defined(system_has_freelist_aba) && defined(CONFIG_SLUB)
11562306a36Sopenharmony_cistatic_assert(IS_ALIGNED(offsetof(struct slab, freelist), sizeof(freelist_aba_t)));
11662306a36Sopenharmony_ci#endif
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci/**
11962306a36Sopenharmony_ci * folio_slab - Converts from folio to slab.
12062306a36Sopenharmony_ci * @folio: The folio.
12162306a36Sopenharmony_ci *
12262306a36Sopenharmony_ci * Currently struct slab is a different representation of a folio where
12362306a36Sopenharmony_ci * folio_test_slab() is true.
12462306a36Sopenharmony_ci *
12562306a36Sopenharmony_ci * Return: The slab which contains this folio.
12662306a36Sopenharmony_ci */
12762306a36Sopenharmony_ci#define folio_slab(folio)	(_Generic((folio),			\
12862306a36Sopenharmony_ci	const struct folio *:	(const struct slab *)(folio),		\
12962306a36Sopenharmony_ci	struct folio *:		(struct slab *)(folio)))
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci/**
13262306a36Sopenharmony_ci * slab_folio - The folio allocated for a slab
13362306a36Sopenharmony_ci * @slab: The slab.
13462306a36Sopenharmony_ci *
13562306a36Sopenharmony_ci * Slabs are allocated as folios that contain the individual objects and are
13662306a36Sopenharmony_ci * using some fields in the first struct page of the folio - those fields are
13762306a36Sopenharmony_ci * now accessed by struct slab. It is occasionally necessary to convert back to
13862306a36Sopenharmony_ci * a folio in order to communicate with the rest of the mm.  Please use this
13962306a36Sopenharmony_ci * helper function instead of casting yourself, as the implementation may change
14062306a36Sopenharmony_ci * in the future.
14162306a36Sopenharmony_ci */
14262306a36Sopenharmony_ci#define slab_folio(s)		(_Generic((s),				\
14362306a36Sopenharmony_ci	const struct slab *:	(const struct folio *)s,		\
14462306a36Sopenharmony_ci	struct slab *:		(struct folio *)s))
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci/**
14762306a36Sopenharmony_ci * page_slab - Converts from first struct page to slab.
14862306a36Sopenharmony_ci * @p: The first (either head of compound or single) page of slab.
14962306a36Sopenharmony_ci *
15062306a36Sopenharmony_ci * A temporary wrapper to convert struct page to struct slab in situations where
15162306a36Sopenharmony_ci * we know the page is the compound head, or single order-0 page.
15262306a36Sopenharmony_ci *
15362306a36Sopenharmony_ci * Long-term ideally everything would work with struct slab directly or go
15462306a36Sopenharmony_ci * through folio to struct slab.
15562306a36Sopenharmony_ci *
15662306a36Sopenharmony_ci * Return: The slab which contains this page
15762306a36Sopenharmony_ci */
15862306a36Sopenharmony_ci#define page_slab(p)		(_Generic((p),				\
15962306a36Sopenharmony_ci	const struct page *:	(const struct slab *)(p),		\
16062306a36Sopenharmony_ci	struct page *:		(struct slab *)(p)))
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci/**
16362306a36Sopenharmony_ci * slab_page - The first struct page allocated for a slab
16462306a36Sopenharmony_ci * @slab: The slab.
16562306a36Sopenharmony_ci *
16662306a36Sopenharmony_ci * A convenience wrapper for converting slab to the first struct page of the
16762306a36Sopenharmony_ci * underlying folio, to communicate with code not yet converted to folio or
16862306a36Sopenharmony_ci * struct slab.
16962306a36Sopenharmony_ci */
17062306a36Sopenharmony_ci#define slab_page(s) folio_page(slab_folio(s), 0)
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci/*
17362306a36Sopenharmony_ci * If network-based swap is enabled, sl*b must keep track of whether pages
17462306a36Sopenharmony_ci * were allocated from pfmemalloc reserves.
17562306a36Sopenharmony_ci */
17662306a36Sopenharmony_cistatic inline bool slab_test_pfmemalloc(const struct slab *slab)
17762306a36Sopenharmony_ci{
17862306a36Sopenharmony_ci	return folio_test_active((struct folio *)slab_folio(slab));
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic inline void slab_set_pfmemalloc(struct slab *slab)
18262306a36Sopenharmony_ci{
18362306a36Sopenharmony_ci	folio_set_active(slab_folio(slab));
18462306a36Sopenharmony_ci}
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_cistatic inline void slab_clear_pfmemalloc(struct slab *slab)
18762306a36Sopenharmony_ci{
18862306a36Sopenharmony_ci	folio_clear_active(slab_folio(slab));
18962306a36Sopenharmony_ci}
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_cistatic inline void __slab_clear_pfmemalloc(struct slab *slab)
19262306a36Sopenharmony_ci{
19362306a36Sopenharmony_ci	__folio_clear_active(slab_folio(slab));
19462306a36Sopenharmony_ci}
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_cistatic inline void *slab_address(const struct slab *slab)
19762306a36Sopenharmony_ci{
19862306a36Sopenharmony_ci	return folio_address(slab_folio(slab));
19962306a36Sopenharmony_ci}
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_cistatic inline int slab_nid(const struct slab *slab)
20262306a36Sopenharmony_ci{
20362306a36Sopenharmony_ci	return folio_nid(slab_folio(slab));
20462306a36Sopenharmony_ci}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cistatic inline pg_data_t *slab_pgdat(const struct slab *slab)
20762306a36Sopenharmony_ci{
20862306a36Sopenharmony_ci	return folio_pgdat(slab_folio(slab));
20962306a36Sopenharmony_ci}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_cistatic inline struct slab *virt_to_slab(const void *addr)
21262306a36Sopenharmony_ci{
21362306a36Sopenharmony_ci	struct folio *folio = virt_to_folio(addr);
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	if (!folio_test_slab(folio))
21662306a36Sopenharmony_ci		return NULL;
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	return folio_slab(folio);
21962306a36Sopenharmony_ci}
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_cistatic inline int slab_order(const struct slab *slab)
22262306a36Sopenharmony_ci{
22362306a36Sopenharmony_ci	return folio_order((struct folio *)slab_folio(slab));
22462306a36Sopenharmony_ci}
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_cistatic inline size_t slab_size(const struct slab *slab)
22762306a36Sopenharmony_ci{
22862306a36Sopenharmony_ci	return PAGE_SIZE << slab_order(slab);
22962306a36Sopenharmony_ci}
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci#ifdef CONFIG_SLAB
23262306a36Sopenharmony_ci#include <linux/slab_def.h>
23362306a36Sopenharmony_ci#endif
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci#ifdef CONFIG_SLUB
23662306a36Sopenharmony_ci#include <linux/slub_def.h>
23762306a36Sopenharmony_ci#endif
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci#include <linux/memcontrol.h>
24062306a36Sopenharmony_ci#include <linux/fault-inject.h>
24162306a36Sopenharmony_ci#include <linux/kasan.h>
24262306a36Sopenharmony_ci#include <linux/kmemleak.h>
24362306a36Sopenharmony_ci#include <linux/random.h>
24462306a36Sopenharmony_ci#include <linux/sched/mm.h>
24562306a36Sopenharmony_ci#include <linux/list_lru.h>
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci/*
24862306a36Sopenharmony_ci * State of the slab allocator.
24962306a36Sopenharmony_ci *
25062306a36Sopenharmony_ci * This is used to describe the states of the allocator during bootup.
25162306a36Sopenharmony_ci * Allocators use this to gradually bootstrap themselves. Most allocators
25262306a36Sopenharmony_ci * have the problem that the structures used for managing slab caches are
25362306a36Sopenharmony_ci * allocated from slab caches themselves.
25462306a36Sopenharmony_ci */
25562306a36Sopenharmony_cienum slab_state {
25662306a36Sopenharmony_ci	DOWN,			/* No slab functionality yet */
25762306a36Sopenharmony_ci	PARTIAL,		/* SLUB: kmem_cache_node available */
25862306a36Sopenharmony_ci	PARTIAL_NODE,		/* SLAB: kmalloc size for node struct available */
25962306a36Sopenharmony_ci	UP,			/* Slab caches usable but not all extras yet */
26062306a36Sopenharmony_ci	FULL			/* Everything is working */
26162306a36Sopenharmony_ci};
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ciextern enum slab_state slab_state;
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci/* The slab cache mutex protects the management structures during changes */
26662306a36Sopenharmony_ciextern struct mutex slab_mutex;
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci/* The list of all slab caches on the system */
26962306a36Sopenharmony_ciextern struct list_head slab_caches;
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci/* The slab cache that manages slab cache information */
27262306a36Sopenharmony_ciextern struct kmem_cache *kmem_cache;
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci/* A table of kmalloc cache names and sizes */
27562306a36Sopenharmony_ciextern const struct kmalloc_info_struct {
27662306a36Sopenharmony_ci	const char *name[NR_KMALLOC_TYPES];
27762306a36Sopenharmony_ci	unsigned int size;
27862306a36Sopenharmony_ci} kmalloc_info[];
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci/* Kmalloc array related functions */
28162306a36Sopenharmony_civoid setup_kmalloc_cache_index_table(void);
28262306a36Sopenharmony_civoid create_kmalloc_caches(slab_flags_t);
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci/* Find the kmalloc slab corresponding for a certain size */
28562306a36Sopenharmony_cistruct kmem_cache *kmalloc_slab(size_t size, gfp_t flags, unsigned long caller);
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_civoid *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags,
28862306a36Sopenharmony_ci			      int node, size_t orig_size,
28962306a36Sopenharmony_ci			      unsigned long caller);
29062306a36Sopenharmony_civoid __kmem_cache_free(struct kmem_cache *s, void *x, unsigned long caller);
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_cigfp_t kmalloc_fix_flags(gfp_t flags);
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci/* Functions provided by the slab allocators */
29562306a36Sopenharmony_ciint __kmem_cache_create(struct kmem_cache *, slab_flags_t flags);
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_civoid __init new_kmalloc_cache(int idx, enum kmalloc_cache_type type,
29862306a36Sopenharmony_ci			      slab_flags_t flags);
29962306a36Sopenharmony_ciextern void create_boot_cache(struct kmem_cache *, const char *name,
30062306a36Sopenharmony_ci			unsigned int size, slab_flags_t flags,
30162306a36Sopenharmony_ci			unsigned int useroffset, unsigned int usersize);
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ciint slab_unmergeable(struct kmem_cache *s);
30462306a36Sopenharmony_cistruct kmem_cache *find_mergeable(unsigned size, unsigned align,
30562306a36Sopenharmony_ci		slab_flags_t flags, const char *name, void (*ctor)(void *));
30662306a36Sopenharmony_cistruct kmem_cache *
30762306a36Sopenharmony_ci__kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
30862306a36Sopenharmony_ci		   slab_flags_t flags, void (*ctor)(void *));
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_cislab_flags_t kmem_cache_flags(unsigned int object_size,
31162306a36Sopenharmony_ci	slab_flags_t flags, const char *name);
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_cistatic inline bool is_kmalloc_cache(struct kmem_cache *s)
31462306a36Sopenharmony_ci{
31562306a36Sopenharmony_ci	return (s->flags & SLAB_KMALLOC);
31662306a36Sopenharmony_ci}
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci/* Legal flag mask for kmem_cache_create(), for various configurations */
31962306a36Sopenharmony_ci#define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
32062306a36Sopenharmony_ci			 SLAB_CACHE_DMA32 | SLAB_PANIC | \
32162306a36Sopenharmony_ci			 SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS )
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci#if defined(CONFIG_DEBUG_SLAB)
32462306a36Sopenharmony_ci#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
32562306a36Sopenharmony_ci#elif defined(CONFIG_SLUB_DEBUG)
32662306a36Sopenharmony_ci#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
32762306a36Sopenharmony_ci			  SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
32862306a36Sopenharmony_ci#else
32962306a36Sopenharmony_ci#define SLAB_DEBUG_FLAGS (0)
33062306a36Sopenharmony_ci#endif
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci#if defined(CONFIG_SLAB)
33362306a36Sopenharmony_ci#define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
33462306a36Sopenharmony_ci			  SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
33562306a36Sopenharmony_ci			  SLAB_ACCOUNT | SLAB_NO_MERGE)
33662306a36Sopenharmony_ci#elif defined(CONFIG_SLUB)
33762306a36Sopenharmony_ci#define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
33862306a36Sopenharmony_ci			  SLAB_TEMPORARY | SLAB_ACCOUNT | \
33962306a36Sopenharmony_ci			  SLAB_NO_USER_FLAGS | SLAB_KMALLOC | SLAB_NO_MERGE)
34062306a36Sopenharmony_ci#else
34162306a36Sopenharmony_ci#define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
34262306a36Sopenharmony_ci#endif
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci/* Common flags available with current configuration */
34562306a36Sopenharmony_ci#define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci/* Common flags permitted for kmem_cache_create */
34862306a36Sopenharmony_ci#define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
34962306a36Sopenharmony_ci			      SLAB_RED_ZONE | \
35062306a36Sopenharmony_ci			      SLAB_POISON | \
35162306a36Sopenharmony_ci			      SLAB_STORE_USER | \
35262306a36Sopenharmony_ci			      SLAB_TRACE | \
35362306a36Sopenharmony_ci			      SLAB_CONSISTENCY_CHECKS | \
35462306a36Sopenharmony_ci			      SLAB_MEM_SPREAD | \
35562306a36Sopenharmony_ci			      SLAB_NOLEAKTRACE | \
35662306a36Sopenharmony_ci			      SLAB_RECLAIM_ACCOUNT | \
35762306a36Sopenharmony_ci			      SLAB_TEMPORARY | \
35862306a36Sopenharmony_ci			      SLAB_ACCOUNT | \
35962306a36Sopenharmony_ci			      SLAB_KMALLOC | \
36062306a36Sopenharmony_ci			      SLAB_NO_MERGE | \
36162306a36Sopenharmony_ci			      SLAB_NO_USER_FLAGS)
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_cibool __kmem_cache_empty(struct kmem_cache *);
36462306a36Sopenharmony_ciint __kmem_cache_shutdown(struct kmem_cache *);
36562306a36Sopenharmony_civoid __kmem_cache_release(struct kmem_cache *);
36662306a36Sopenharmony_ciint __kmem_cache_shrink(struct kmem_cache *);
36762306a36Sopenharmony_civoid slab_kmem_cache_release(struct kmem_cache *);
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_cistruct seq_file;
37062306a36Sopenharmony_cistruct file;
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_cistruct slabinfo {
37362306a36Sopenharmony_ci	unsigned long active_objs;
37462306a36Sopenharmony_ci	unsigned long num_objs;
37562306a36Sopenharmony_ci	unsigned long active_slabs;
37662306a36Sopenharmony_ci	unsigned long num_slabs;
37762306a36Sopenharmony_ci	unsigned long shared_avail;
37862306a36Sopenharmony_ci	unsigned int limit;
37962306a36Sopenharmony_ci	unsigned int batchcount;
38062306a36Sopenharmony_ci	unsigned int shared;
38162306a36Sopenharmony_ci	unsigned int objects_per_slab;
38262306a36Sopenharmony_ci	unsigned int cache_order;
38362306a36Sopenharmony_ci};
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_civoid get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
38662306a36Sopenharmony_civoid slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
38762306a36Sopenharmony_cissize_t slabinfo_write(struct file *file, const char __user *buffer,
38862306a36Sopenharmony_ci		       size_t count, loff_t *ppos);
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_cistatic inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s)
39162306a36Sopenharmony_ci{
39262306a36Sopenharmony_ci	return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
39362306a36Sopenharmony_ci		NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
39462306a36Sopenharmony_ci}
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci#ifdef CONFIG_SLUB_DEBUG
39762306a36Sopenharmony_ci#ifdef CONFIG_SLUB_DEBUG_ON
39862306a36Sopenharmony_ciDECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
39962306a36Sopenharmony_ci#else
40062306a36Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
40162306a36Sopenharmony_ci#endif
40262306a36Sopenharmony_ciextern void print_tracking(struct kmem_cache *s, void *object);
40362306a36Sopenharmony_cilong validate_slab_cache(struct kmem_cache *s);
40462306a36Sopenharmony_cistatic inline bool __slub_debug_enabled(void)
40562306a36Sopenharmony_ci{
40662306a36Sopenharmony_ci	return static_branch_unlikely(&slub_debug_enabled);
40762306a36Sopenharmony_ci}
40862306a36Sopenharmony_ci#else
40962306a36Sopenharmony_cistatic inline void print_tracking(struct kmem_cache *s, void *object)
41062306a36Sopenharmony_ci{
41162306a36Sopenharmony_ci}
41262306a36Sopenharmony_cistatic inline bool __slub_debug_enabled(void)
41362306a36Sopenharmony_ci{
41462306a36Sopenharmony_ci	return false;
41562306a36Sopenharmony_ci}
41662306a36Sopenharmony_ci#endif
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci/*
41962306a36Sopenharmony_ci * Returns true if any of the specified slub_debug flags is enabled for the
42062306a36Sopenharmony_ci * cache. Use only for flags parsed by setup_slub_debug() as it also enables
42162306a36Sopenharmony_ci * the static key.
42262306a36Sopenharmony_ci */
42362306a36Sopenharmony_cistatic inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
42462306a36Sopenharmony_ci{
42562306a36Sopenharmony_ci	if (IS_ENABLED(CONFIG_SLUB_DEBUG))
42662306a36Sopenharmony_ci		VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
42762306a36Sopenharmony_ci	if (__slub_debug_enabled())
42862306a36Sopenharmony_ci		return s->flags & flags;
42962306a36Sopenharmony_ci	return false;
43062306a36Sopenharmony_ci}
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci#ifdef CONFIG_MEMCG_KMEM
43362306a36Sopenharmony_ci/*
43462306a36Sopenharmony_ci * slab_objcgs - get the object cgroups vector associated with a slab
43562306a36Sopenharmony_ci * @slab: a pointer to the slab struct
43662306a36Sopenharmony_ci *
43762306a36Sopenharmony_ci * Returns a pointer to the object cgroups vector associated with the slab,
43862306a36Sopenharmony_ci * or NULL if no such vector has been associated yet.
43962306a36Sopenharmony_ci */
44062306a36Sopenharmony_cistatic inline struct obj_cgroup **slab_objcgs(struct slab *slab)
44162306a36Sopenharmony_ci{
44262306a36Sopenharmony_ci	unsigned long memcg_data = READ_ONCE(slab->memcg_data);
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci	VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS),
44562306a36Sopenharmony_ci							slab_page(slab));
44662306a36Sopenharmony_ci	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, slab_page(slab));
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ci	return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
44962306a36Sopenharmony_ci}
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ciint memcg_alloc_slab_cgroups(struct slab *slab, struct kmem_cache *s,
45262306a36Sopenharmony_ci				 gfp_t gfp, bool new_slab);
45362306a36Sopenharmony_civoid mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
45462306a36Sopenharmony_ci		     enum node_stat_item idx, int nr);
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_cistatic inline void memcg_free_slab_cgroups(struct slab *slab)
45762306a36Sopenharmony_ci{
45862306a36Sopenharmony_ci	kfree(slab_objcgs(slab));
45962306a36Sopenharmony_ci	slab->memcg_data = 0;
46062306a36Sopenharmony_ci}
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_cistatic inline size_t obj_full_size(struct kmem_cache *s)
46362306a36Sopenharmony_ci{
46462306a36Sopenharmony_ci	/*
46562306a36Sopenharmony_ci	 * For each accounted object there is an extra space which is used
46662306a36Sopenharmony_ci	 * to store obj_cgroup membership. Charge it too.
46762306a36Sopenharmony_ci	 */
46862306a36Sopenharmony_ci	return s->size + sizeof(struct obj_cgroup *);
46962306a36Sopenharmony_ci}
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci/*
47262306a36Sopenharmony_ci * Returns false if the allocation should fail.
47362306a36Sopenharmony_ci */
47462306a36Sopenharmony_cistatic inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
47562306a36Sopenharmony_ci					     struct list_lru *lru,
47662306a36Sopenharmony_ci					     struct obj_cgroup **objcgp,
47762306a36Sopenharmony_ci					     size_t objects, gfp_t flags)
47862306a36Sopenharmony_ci{
47962306a36Sopenharmony_ci	struct obj_cgroup *objcg;
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci	if (!memcg_kmem_online())
48262306a36Sopenharmony_ci		return true;
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci	if (!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT))
48562306a36Sopenharmony_ci		return true;
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci	objcg = get_obj_cgroup_from_current();
48862306a36Sopenharmony_ci	if (!objcg)
48962306a36Sopenharmony_ci		return true;
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci	if (lru) {
49262306a36Sopenharmony_ci		int ret;
49362306a36Sopenharmony_ci		struct mem_cgroup *memcg;
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci		memcg = get_mem_cgroup_from_objcg(objcg);
49662306a36Sopenharmony_ci		ret = memcg_list_lru_alloc(memcg, lru, flags);
49762306a36Sopenharmony_ci		css_put(&memcg->css);
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci		if (ret)
50062306a36Sopenharmony_ci			goto out;
50162306a36Sopenharmony_ci	}
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci	if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s)))
50462306a36Sopenharmony_ci		goto out;
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci	*objcgp = objcg;
50762306a36Sopenharmony_ci	return true;
50862306a36Sopenharmony_ciout:
50962306a36Sopenharmony_ci	obj_cgroup_put(objcg);
51062306a36Sopenharmony_ci	return false;
51162306a36Sopenharmony_ci}
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_cistatic inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
51462306a36Sopenharmony_ci					      struct obj_cgroup *objcg,
51562306a36Sopenharmony_ci					      gfp_t flags, size_t size,
51662306a36Sopenharmony_ci					      void **p)
51762306a36Sopenharmony_ci{
51862306a36Sopenharmony_ci	struct slab *slab;
51962306a36Sopenharmony_ci	unsigned long off;
52062306a36Sopenharmony_ci	size_t i;
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	if (!memcg_kmem_online() || !objcg)
52362306a36Sopenharmony_ci		return;
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci	for (i = 0; i < size; i++) {
52662306a36Sopenharmony_ci		if (likely(p[i])) {
52762306a36Sopenharmony_ci			slab = virt_to_slab(p[i]);
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci			if (!slab_objcgs(slab) &&
53062306a36Sopenharmony_ci			    memcg_alloc_slab_cgroups(slab, s, flags,
53162306a36Sopenharmony_ci							 false)) {
53262306a36Sopenharmony_ci				obj_cgroup_uncharge(objcg, obj_full_size(s));
53362306a36Sopenharmony_ci				continue;
53462306a36Sopenharmony_ci			}
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ci			off = obj_to_index(s, slab, p[i]);
53762306a36Sopenharmony_ci			obj_cgroup_get(objcg);
53862306a36Sopenharmony_ci			slab_objcgs(slab)[off] = objcg;
53962306a36Sopenharmony_ci			mod_objcg_state(objcg, slab_pgdat(slab),
54062306a36Sopenharmony_ci					cache_vmstat_idx(s), obj_full_size(s));
54162306a36Sopenharmony_ci		} else {
54262306a36Sopenharmony_ci			obj_cgroup_uncharge(objcg, obj_full_size(s));
54362306a36Sopenharmony_ci		}
54462306a36Sopenharmony_ci	}
54562306a36Sopenharmony_ci	obj_cgroup_put(objcg);
54662306a36Sopenharmony_ci}
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_cistatic inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
54962306a36Sopenharmony_ci					void **p, int objects)
55062306a36Sopenharmony_ci{
55162306a36Sopenharmony_ci	struct obj_cgroup **objcgs;
55262306a36Sopenharmony_ci	int i;
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci	if (!memcg_kmem_online())
55562306a36Sopenharmony_ci		return;
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ci	objcgs = slab_objcgs(slab);
55862306a36Sopenharmony_ci	if (!objcgs)
55962306a36Sopenharmony_ci		return;
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci	for (i = 0; i < objects; i++) {
56262306a36Sopenharmony_ci		struct obj_cgroup *objcg;
56362306a36Sopenharmony_ci		unsigned int off;
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_ci		off = obj_to_index(s, slab, p[i]);
56662306a36Sopenharmony_ci		objcg = objcgs[off];
56762306a36Sopenharmony_ci		if (!objcg)
56862306a36Sopenharmony_ci			continue;
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci		objcgs[off] = NULL;
57162306a36Sopenharmony_ci		obj_cgroup_uncharge(objcg, obj_full_size(s));
57262306a36Sopenharmony_ci		mod_objcg_state(objcg, slab_pgdat(slab), cache_vmstat_idx(s),
57362306a36Sopenharmony_ci				-obj_full_size(s));
57462306a36Sopenharmony_ci		obj_cgroup_put(objcg);
57562306a36Sopenharmony_ci	}
57662306a36Sopenharmony_ci}
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_ci#else /* CONFIG_MEMCG_KMEM */
57962306a36Sopenharmony_cistatic inline struct obj_cgroup **slab_objcgs(struct slab *slab)
58062306a36Sopenharmony_ci{
58162306a36Sopenharmony_ci	return NULL;
58262306a36Sopenharmony_ci}
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_cistatic inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
58562306a36Sopenharmony_ci{
58662306a36Sopenharmony_ci	return NULL;
58762306a36Sopenharmony_ci}
58862306a36Sopenharmony_ci
58962306a36Sopenharmony_cistatic inline int memcg_alloc_slab_cgroups(struct slab *slab,
59062306a36Sopenharmony_ci					       struct kmem_cache *s, gfp_t gfp,
59162306a36Sopenharmony_ci					       bool new_slab)
59262306a36Sopenharmony_ci{
59362306a36Sopenharmony_ci	return 0;
59462306a36Sopenharmony_ci}
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_cistatic inline void memcg_free_slab_cgroups(struct slab *slab)
59762306a36Sopenharmony_ci{
59862306a36Sopenharmony_ci}
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_cistatic inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
60162306a36Sopenharmony_ci					     struct list_lru *lru,
60262306a36Sopenharmony_ci					     struct obj_cgroup **objcgp,
60362306a36Sopenharmony_ci					     size_t objects, gfp_t flags)
60462306a36Sopenharmony_ci{
60562306a36Sopenharmony_ci	return true;
60662306a36Sopenharmony_ci}
60762306a36Sopenharmony_ci
60862306a36Sopenharmony_cistatic inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
60962306a36Sopenharmony_ci					      struct obj_cgroup *objcg,
61062306a36Sopenharmony_ci					      gfp_t flags, size_t size,
61162306a36Sopenharmony_ci					      void **p)
61262306a36Sopenharmony_ci{
61362306a36Sopenharmony_ci}
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_cistatic inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
61662306a36Sopenharmony_ci					void **p, int objects)
61762306a36Sopenharmony_ci{
61862306a36Sopenharmony_ci}
61962306a36Sopenharmony_ci#endif /* CONFIG_MEMCG_KMEM */
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_cistatic inline struct kmem_cache *virt_to_cache(const void *obj)
62262306a36Sopenharmony_ci{
62362306a36Sopenharmony_ci	struct slab *slab;
62462306a36Sopenharmony_ci
62562306a36Sopenharmony_ci	slab = virt_to_slab(obj);
62662306a36Sopenharmony_ci	if (WARN_ONCE(!slab, "%s: Object is not a Slab page!\n",
62762306a36Sopenharmony_ci					__func__))
62862306a36Sopenharmony_ci		return NULL;
62962306a36Sopenharmony_ci	return slab->slab_cache;
63062306a36Sopenharmony_ci}
63162306a36Sopenharmony_ci
63262306a36Sopenharmony_cistatic __always_inline void account_slab(struct slab *slab, int order,
63362306a36Sopenharmony_ci					 struct kmem_cache *s, gfp_t gfp)
63462306a36Sopenharmony_ci{
63562306a36Sopenharmony_ci	if (memcg_kmem_online() && (s->flags & SLAB_ACCOUNT))
63662306a36Sopenharmony_ci		memcg_alloc_slab_cgroups(slab, s, gfp, true);
63762306a36Sopenharmony_ci
63862306a36Sopenharmony_ci	mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
63962306a36Sopenharmony_ci			    PAGE_SIZE << order);
64062306a36Sopenharmony_ci}
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_cistatic __always_inline void unaccount_slab(struct slab *slab, int order,
64362306a36Sopenharmony_ci					   struct kmem_cache *s)
64462306a36Sopenharmony_ci{
64562306a36Sopenharmony_ci	if (memcg_kmem_online())
64662306a36Sopenharmony_ci		memcg_free_slab_cgroups(slab);
64762306a36Sopenharmony_ci
64862306a36Sopenharmony_ci	mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
64962306a36Sopenharmony_ci			    -(PAGE_SIZE << order));
65062306a36Sopenharmony_ci}
65162306a36Sopenharmony_ci
65262306a36Sopenharmony_cistatic inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
65362306a36Sopenharmony_ci{
65462306a36Sopenharmony_ci	struct kmem_cache *cachep;
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_ci	if (!IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
65762306a36Sopenharmony_ci	    !kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS))
65862306a36Sopenharmony_ci		return s;
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci	cachep = virt_to_cache(x);
66162306a36Sopenharmony_ci	if (WARN(cachep && cachep != s,
66262306a36Sopenharmony_ci		  "%s: Wrong slab cache. %s but object is from %s\n",
66362306a36Sopenharmony_ci		  __func__, s->name, cachep->name))
66462306a36Sopenharmony_ci		print_tracking(cachep, x);
66562306a36Sopenharmony_ci	return cachep;
66662306a36Sopenharmony_ci}
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_civoid free_large_kmalloc(struct folio *folio, void *object);
66962306a36Sopenharmony_ci
67062306a36Sopenharmony_cisize_t __ksize(const void *objp);
67162306a36Sopenharmony_ci
67262306a36Sopenharmony_cistatic inline size_t slab_ksize(const struct kmem_cache *s)
67362306a36Sopenharmony_ci{
67462306a36Sopenharmony_ci#ifndef CONFIG_SLUB
67562306a36Sopenharmony_ci	return s->object_size;
67662306a36Sopenharmony_ci
67762306a36Sopenharmony_ci#else /* CONFIG_SLUB */
67862306a36Sopenharmony_ci# ifdef CONFIG_SLUB_DEBUG
67962306a36Sopenharmony_ci	/*
68062306a36Sopenharmony_ci	 * Debugging requires use of the padding between object
68162306a36Sopenharmony_ci	 * and whatever may come after it.
68262306a36Sopenharmony_ci	 */
68362306a36Sopenharmony_ci	if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
68462306a36Sopenharmony_ci		return s->object_size;
68562306a36Sopenharmony_ci# endif
68662306a36Sopenharmony_ci	if (s->flags & SLAB_KASAN)
68762306a36Sopenharmony_ci		return s->object_size;
68862306a36Sopenharmony_ci	/*
68962306a36Sopenharmony_ci	 * If we have the need to store the freelist pointer
69062306a36Sopenharmony_ci	 * back there or track user information then we can
69162306a36Sopenharmony_ci	 * only use the space before that information.
69262306a36Sopenharmony_ci	 */
69362306a36Sopenharmony_ci	if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
69462306a36Sopenharmony_ci		return s->inuse;
69562306a36Sopenharmony_ci	/*
69662306a36Sopenharmony_ci	 * Else we can use all the padding etc for the allocation
69762306a36Sopenharmony_ci	 */
69862306a36Sopenharmony_ci	return s->size;
69962306a36Sopenharmony_ci#endif
70062306a36Sopenharmony_ci}
70162306a36Sopenharmony_ci
70262306a36Sopenharmony_cistatic inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
70362306a36Sopenharmony_ci						     struct list_lru *lru,
70462306a36Sopenharmony_ci						     struct obj_cgroup **objcgp,
70562306a36Sopenharmony_ci						     size_t size, gfp_t flags)
70662306a36Sopenharmony_ci{
70762306a36Sopenharmony_ci	flags &= gfp_allowed_mask;
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci	might_alloc(flags);
71062306a36Sopenharmony_ci
71162306a36Sopenharmony_ci	if (should_failslab(s, flags))
71262306a36Sopenharmony_ci		return NULL;
71362306a36Sopenharmony_ci
71462306a36Sopenharmony_ci	if (!memcg_slab_pre_alloc_hook(s, lru, objcgp, size, flags))
71562306a36Sopenharmony_ci		return NULL;
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_ci	return s;
71862306a36Sopenharmony_ci}
71962306a36Sopenharmony_ci
72062306a36Sopenharmony_cistatic inline void slab_post_alloc_hook(struct kmem_cache *s,
72162306a36Sopenharmony_ci					struct obj_cgroup *objcg, gfp_t flags,
72262306a36Sopenharmony_ci					size_t size, void **p, bool init,
72362306a36Sopenharmony_ci					unsigned int orig_size)
72462306a36Sopenharmony_ci{
72562306a36Sopenharmony_ci	unsigned int zero_size = s->object_size;
72662306a36Sopenharmony_ci	bool kasan_init = init;
72762306a36Sopenharmony_ci	size_t i;
72862306a36Sopenharmony_ci
72962306a36Sopenharmony_ci	flags &= gfp_allowed_mask;
73062306a36Sopenharmony_ci
73162306a36Sopenharmony_ci	/*
73262306a36Sopenharmony_ci	 * For kmalloc object, the allocated memory size(object_size) is likely
73362306a36Sopenharmony_ci	 * larger than the requested size(orig_size). If redzone check is
73462306a36Sopenharmony_ci	 * enabled for the extra space, don't zero it, as it will be redzoned
73562306a36Sopenharmony_ci	 * soon. The redzone operation for this extra space could be seen as a
73662306a36Sopenharmony_ci	 * replacement of current poisoning under certain debug option, and
73762306a36Sopenharmony_ci	 * won't break other sanity checks.
73862306a36Sopenharmony_ci	 */
73962306a36Sopenharmony_ci	if (kmem_cache_debug_flags(s, SLAB_STORE_USER | SLAB_RED_ZONE) &&
74062306a36Sopenharmony_ci	    (s->flags & SLAB_KMALLOC))
74162306a36Sopenharmony_ci		zero_size = orig_size;
74262306a36Sopenharmony_ci
74362306a36Sopenharmony_ci	/*
74462306a36Sopenharmony_ci	 * When slub_debug is enabled, avoid memory initialization integrated
74562306a36Sopenharmony_ci	 * into KASAN and instead zero out the memory via the memset below with
74662306a36Sopenharmony_ci	 * the proper size. Otherwise, KASAN might overwrite SLUB redzones and
74762306a36Sopenharmony_ci	 * cause false-positive reports. This does not lead to a performance
74862306a36Sopenharmony_ci	 * penalty on production builds, as slub_debug is not intended to be
74962306a36Sopenharmony_ci	 * enabled there.
75062306a36Sopenharmony_ci	 */
75162306a36Sopenharmony_ci	if (__slub_debug_enabled())
75262306a36Sopenharmony_ci		kasan_init = false;
75362306a36Sopenharmony_ci
75462306a36Sopenharmony_ci	/*
75562306a36Sopenharmony_ci	 * As memory initialization might be integrated into KASAN,
75662306a36Sopenharmony_ci	 * kasan_slab_alloc and initialization memset must be
75762306a36Sopenharmony_ci	 * kept together to avoid discrepancies in behavior.
75862306a36Sopenharmony_ci	 *
75962306a36Sopenharmony_ci	 * As p[i] might get tagged, memset and kmemleak hook come after KASAN.
76062306a36Sopenharmony_ci	 */
76162306a36Sopenharmony_ci	for (i = 0; i < size; i++) {
76262306a36Sopenharmony_ci		p[i] = kasan_slab_alloc(s, p[i], flags, kasan_init);
76362306a36Sopenharmony_ci		if (p[i] && init && (!kasan_init || !kasan_has_integrated_init()))
76462306a36Sopenharmony_ci			memset(p[i], 0, zero_size);
76562306a36Sopenharmony_ci		kmemleak_alloc_recursive(p[i], s->object_size, 1,
76662306a36Sopenharmony_ci					 s->flags, flags);
76762306a36Sopenharmony_ci		kmsan_slab_alloc(s, p[i], flags);
76862306a36Sopenharmony_ci	}
76962306a36Sopenharmony_ci
77062306a36Sopenharmony_ci	memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
77162306a36Sopenharmony_ci}
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci/*
77462306a36Sopenharmony_ci * The slab lists for all objects.
77562306a36Sopenharmony_ci */
77662306a36Sopenharmony_cistruct kmem_cache_node {
77762306a36Sopenharmony_ci#ifdef CONFIG_SLAB
77862306a36Sopenharmony_ci	raw_spinlock_t list_lock;
77962306a36Sopenharmony_ci	struct list_head slabs_partial;	/* partial list first, better asm code */
78062306a36Sopenharmony_ci	struct list_head slabs_full;
78162306a36Sopenharmony_ci	struct list_head slabs_free;
78262306a36Sopenharmony_ci	unsigned long total_slabs;	/* length of all slab lists */
78362306a36Sopenharmony_ci	unsigned long free_slabs;	/* length of free slab list only */
78462306a36Sopenharmony_ci	unsigned long free_objects;
78562306a36Sopenharmony_ci	unsigned int free_limit;
78662306a36Sopenharmony_ci	unsigned int colour_next;	/* Per-node cache coloring */
78762306a36Sopenharmony_ci	struct array_cache *shared;	/* shared per node */
78862306a36Sopenharmony_ci	struct alien_cache **alien;	/* on other nodes */
78962306a36Sopenharmony_ci	unsigned long next_reap;	/* updated without locking */
79062306a36Sopenharmony_ci	int free_touched;		/* updated without locking */
79162306a36Sopenharmony_ci#endif
79262306a36Sopenharmony_ci
79362306a36Sopenharmony_ci#ifdef CONFIG_SLUB
79462306a36Sopenharmony_ci	spinlock_t list_lock;
79562306a36Sopenharmony_ci	unsigned long nr_partial;
79662306a36Sopenharmony_ci	struct list_head partial;
79762306a36Sopenharmony_ci#ifdef CONFIG_SLUB_DEBUG
79862306a36Sopenharmony_ci	atomic_long_t nr_slabs;
79962306a36Sopenharmony_ci	atomic_long_t total_objects;
80062306a36Sopenharmony_ci	struct list_head full;
80162306a36Sopenharmony_ci#endif
80262306a36Sopenharmony_ci#endif
80362306a36Sopenharmony_ci
80462306a36Sopenharmony_ci};
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_cistatic inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
80762306a36Sopenharmony_ci{
80862306a36Sopenharmony_ci	return s->node[node];
80962306a36Sopenharmony_ci}
81062306a36Sopenharmony_ci
81162306a36Sopenharmony_ci/*
81262306a36Sopenharmony_ci * Iterator over all nodes. The body will be executed for each node that has
81362306a36Sopenharmony_ci * a kmem_cache_node structure allocated (which is true for all online nodes)
81462306a36Sopenharmony_ci */
81562306a36Sopenharmony_ci#define for_each_kmem_cache_node(__s, __node, __n) \
81662306a36Sopenharmony_ci	for (__node = 0; __node < nr_node_ids; __node++) \
81762306a36Sopenharmony_ci		 if ((__n = get_node(__s, __node)))
81862306a36Sopenharmony_ci
81962306a36Sopenharmony_ci
82062306a36Sopenharmony_ci#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
82162306a36Sopenharmony_civoid dump_unreclaimable_slab(void);
82262306a36Sopenharmony_ci#else
82362306a36Sopenharmony_cistatic inline void dump_unreclaimable_slab(void)
82462306a36Sopenharmony_ci{
82562306a36Sopenharmony_ci}
82662306a36Sopenharmony_ci#endif
82762306a36Sopenharmony_ci
82862306a36Sopenharmony_civoid ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
82962306a36Sopenharmony_ci
83062306a36Sopenharmony_ci#ifdef CONFIG_SLAB_FREELIST_RANDOM
83162306a36Sopenharmony_ciint cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
83262306a36Sopenharmony_ci			gfp_t gfp);
83362306a36Sopenharmony_civoid cache_random_seq_destroy(struct kmem_cache *cachep);
83462306a36Sopenharmony_ci#else
83562306a36Sopenharmony_cistatic inline int cache_random_seq_create(struct kmem_cache *cachep,
83662306a36Sopenharmony_ci					unsigned int count, gfp_t gfp)
83762306a36Sopenharmony_ci{
83862306a36Sopenharmony_ci	return 0;
83962306a36Sopenharmony_ci}
84062306a36Sopenharmony_cistatic inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
84162306a36Sopenharmony_ci#endif /* CONFIG_SLAB_FREELIST_RANDOM */
84262306a36Sopenharmony_ci
84362306a36Sopenharmony_cistatic inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
84462306a36Sopenharmony_ci{
84562306a36Sopenharmony_ci	if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
84662306a36Sopenharmony_ci				&init_on_alloc)) {
84762306a36Sopenharmony_ci		if (c->ctor)
84862306a36Sopenharmony_ci			return false;
84962306a36Sopenharmony_ci		if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
85062306a36Sopenharmony_ci			return flags & __GFP_ZERO;
85162306a36Sopenharmony_ci		return true;
85262306a36Sopenharmony_ci	}
85362306a36Sopenharmony_ci	return flags & __GFP_ZERO;
85462306a36Sopenharmony_ci}
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_cistatic inline bool slab_want_init_on_free(struct kmem_cache *c)
85762306a36Sopenharmony_ci{
85862306a36Sopenharmony_ci	if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
85962306a36Sopenharmony_ci				&init_on_free))
86062306a36Sopenharmony_ci		return !(c->ctor ||
86162306a36Sopenharmony_ci			 (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
86262306a36Sopenharmony_ci	return false;
86362306a36Sopenharmony_ci}
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_ci#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
86662306a36Sopenharmony_civoid debugfs_slab_release(struct kmem_cache *);
86762306a36Sopenharmony_ci#else
86862306a36Sopenharmony_cistatic inline void debugfs_slab_release(struct kmem_cache *s) { }
86962306a36Sopenharmony_ci#endif
87062306a36Sopenharmony_ci
87162306a36Sopenharmony_ci#ifdef CONFIG_PRINTK
87262306a36Sopenharmony_ci#define KS_ADDRS_COUNT 16
87362306a36Sopenharmony_cistruct kmem_obj_info {
87462306a36Sopenharmony_ci	void *kp_ptr;
87562306a36Sopenharmony_ci	struct slab *kp_slab;
87662306a36Sopenharmony_ci	void *kp_objp;
87762306a36Sopenharmony_ci	unsigned long kp_data_offset;
87862306a36Sopenharmony_ci	struct kmem_cache *kp_slab_cache;
87962306a36Sopenharmony_ci	void *kp_ret;
88062306a36Sopenharmony_ci	void *kp_stack[KS_ADDRS_COUNT];
88162306a36Sopenharmony_ci	void *kp_free_stack[KS_ADDRS_COUNT];
88262306a36Sopenharmony_ci};
88362306a36Sopenharmony_civoid __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
88462306a36Sopenharmony_ci#endif
88562306a36Sopenharmony_ci
88662306a36Sopenharmony_civoid __check_heap_object(const void *ptr, unsigned long n,
88762306a36Sopenharmony_ci			 const struct slab *slab, bool to_user);
88862306a36Sopenharmony_ci
88962306a36Sopenharmony_ci#ifdef CONFIG_SLUB_DEBUG
89062306a36Sopenharmony_civoid skip_orig_size_check(struct kmem_cache *s, const void *object);
89162306a36Sopenharmony_ci#endif
89262306a36Sopenharmony_ci
89362306a36Sopenharmony_ci#endif /* MM_SLAB_H */
894