Merge tag 'media/v6.4-6' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-block.git] / mm / slab.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
97d06609
CL
2#ifndef MM_SLAB_H
3#define MM_SLAB_H
4/*
5 * Internal slab definitions
6 */
d5d2c02a 7void __init kmem_cache_init(void);
97d06609 8
d122019b
MWO
9/* Reuses the bits in struct page */
10struct slab {
11 unsigned long __page_flags;
401fb12c
VB
12
13#if defined(CONFIG_SLAB)
14
130d4df5 15 struct kmem_cache *slab_cache;
d122019b 16 union {
130d4df5
VB
17 struct {
18 struct list_head slab_list;
19 void *freelist; /* array of free object indexes */
20 void *s_mem; /* first object */
21 };
401fb12c
VB
22 struct rcu_head rcu_head;
23 };
401fb12c
VB
24 unsigned int active;
25
26#elif defined(CONFIG_SLUB)
27
401fb12c 28 struct kmem_cache *slab_cache;
d122019b 29 union {
401fb12c 30 struct {
130d4df5
VB
31 union {
32 struct list_head slab_list;
33#ifdef CONFIG_SLUB_CPU_PARTIAL
34 struct {
35 struct slab *next;
36 int slabs; /* Nr of slabs left */
37 };
38#endif
39 };
40 /* Double-word boundary */
41 void *freelist; /* first free object */
42 union {
43 unsigned long counters;
44 struct {
45 unsigned inuse:16;
46 unsigned objects:15;
47 unsigned frozen:1;
48 };
49 };
d122019b 50 };
130d4df5 51 struct rcu_head rcu_head;
d122019b 52 };
401fb12c
VB
53 unsigned int __unused;
54
401fb12c
VB
55#else
56#error "Unexpected slab allocator configured"
57#endif
d122019b 58
d122019b
MWO
59 atomic_t __page_refcount;
60#ifdef CONFIG_MEMCG
61 unsigned long memcg_data;
62#endif
63};
64
65#define SLAB_MATCH(pg, sl) \
66 static_assert(offsetof(struct page, pg) == offsetof(struct slab, sl))
67SLAB_MATCH(flags, __page_flags);
130d4df5 68SLAB_MATCH(compound_head, slab_cache); /* Ensure bit 0 is clear */
d122019b
MWO
69SLAB_MATCH(_refcount, __page_refcount);
70#ifdef CONFIG_MEMCG
71SLAB_MATCH(memcg_data, memcg_data);
72#endif
73#undef SLAB_MATCH
74static_assert(sizeof(struct slab) <= sizeof(struct page));
130d4df5
VB
75#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && defined(CONFIG_SLUB)
76static_assert(IS_ALIGNED(offsetof(struct slab, freelist), 2*sizeof(void *)));
77#endif
d122019b
MWO
78
79/**
80 * folio_slab - Converts from folio to slab.
81 * @folio: The folio.
82 *
83 * Currently struct slab is a different representation of a folio where
84 * folio_test_slab() is true.
85 *
86 * Return: The slab which contains this folio.
87 */
88#define folio_slab(folio) (_Generic((folio), \
89 const struct folio *: (const struct slab *)(folio), \
90 struct folio *: (struct slab *)(folio)))
91
92/**
93 * slab_folio - The folio allocated for a slab
94 * @slab: The slab.
95 *
96 * Slabs are allocated as folios that contain the individual objects and are
97 * using some fields in the first struct page of the folio - those fields are
98 * now accessed by struct slab. It is occasionally necessary to convert back to
99 * a folio in order to communicate with the rest of the mm. Please use this
100 * helper function instead of casting yourself, as the implementation may change
101 * in the future.
102 */
103#define slab_folio(s) (_Generic((s), \
104 const struct slab *: (const struct folio *)s, \
105 struct slab *: (struct folio *)s))
106
107/**
108 * page_slab - Converts from first struct page to slab.
109 * @p: The first (either head of compound or single) page of slab.
110 *
111 * A temporary wrapper to convert struct page to struct slab in situations where
112 * we know the page is the compound head, or single order-0 page.
113 *
114 * Long-term ideally everything would work with struct slab directly or go
115 * through folio to struct slab.
116 *
117 * Return: The slab which contains this page
118 */
119#define page_slab(p) (_Generic((p), \
120 const struct page *: (const struct slab *)(p), \
121 struct page *: (struct slab *)(p)))
122
123/**
124 * slab_page - The first struct page allocated for a slab
125 * @slab: The slab.
126 *
127 * A convenience wrapper for converting slab to the first struct page of the
128 * underlying folio, to communicate with code not yet converted to folio or
129 * struct slab.
130 */
131#define slab_page(s) folio_page(slab_folio(s), 0)
132
133/*
134 * If network-based swap is enabled, sl*b must keep track of whether pages
135 * were allocated from pfmemalloc reserves.
136 */
137static inline bool slab_test_pfmemalloc(const struct slab *slab)
138{
139 return folio_test_active((struct folio *)slab_folio(slab));
140}
141
142static inline void slab_set_pfmemalloc(struct slab *slab)
143{
144 folio_set_active(slab_folio(slab));
145}
146
147static inline void slab_clear_pfmemalloc(struct slab *slab)
148{
149 folio_clear_active(slab_folio(slab));
150}
151
152static inline void __slab_clear_pfmemalloc(struct slab *slab)
153{
154 __folio_clear_active(slab_folio(slab));
155}
156
157static inline void *slab_address(const struct slab *slab)
158{
159 return folio_address(slab_folio(slab));
160}
161
162static inline int slab_nid(const struct slab *slab)
163{
164 return folio_nid(slab_folio(slab));
165}
166
167static inline pg_data_t *slab_pgdat(const struct slab *slab)
168{
169 return folio_pgdat(slab_folio(slab));
170}
171
172static inline struct slab *virt_to_slab(const void *addr)
173{
174 struct folio *folio = virt_to_folio(addr);
175
176 if (!folio_test_slab(folio))
177 return NULL;
178
179 return folio_slab(folio);
180}
181
182static inline int slab_order(const struct slab *slab)
183{
184 return folio_order((struct folio *)slab_folio(slab));
185}
186
187static inline size_t slab_size(const struct slab *slab)
188{
189 return PAGE_SIZE << slab_order(slab);
190}
191
07f361b2
JK
192#ifdef CONFIG_SLAB
193#include <linux/slab_def.h>
194#endif
195
196#ifdef CONFIG_SLUB
197#include <linux/slub_def.h>
198#endif
199
200#include <linux/memcontrol.h>
11c7aec2 201#include <linux/fault-inject.h>
11c7aec2
JDB
202#include <linux/kasan.h>
203#include <linux/kmemleak.h>
7c00fce9 204#include <linux/random.h>
d92a8cfc 205#include <linux/sched/mm.h>
88f2ef73 206#include <linux/list_lru.h>
07f361b2 207
97d06609
CL
208/*
209 * State of the slab allocator.
210 *
211 * This is used to describe the states of the allocator during bootup.
212 * Allocators use this to gradually bootstrap themselves. Most allocators
213 * have the problem that the structures used for managing slab caches are
214 * allocated from slab caches themselves.
215 */
216enum slab_state {
217 DOWN, /* No slab functionality yet */
218 PARTIAL, /* SLUB: kmem_cache_node available */
ce8eb6c4 219 PARTIAL_NODE, /* SLAB: kmalloc size for node struct available */
97d06609
CL
220 UP, /* Slab caches usable but not all extras yet */
221 FULL /* Everything is working */
222};
223
224extern enum slab_state slab_state;
225
18004c5d
CL
226/* The slab cache mutex protects the management structures during changes */
227extern struct mutex slab_mutex;
9b030cb8
CL
228
229/* The list of all slab caches on the system */
18004c5d
CL
230extern struct list_head slab_caches;
231
9b030cb8
CL
232/* The slab cache that manages slab cache information */
233extern struct kmem_cache *kmem_cache;
234
af3b5f87
VB
235/* A table of kmalloc cache names and sizes */
236extern const struct kmalloc_info_struct {
cb5d9fb3 237 const char *name[NR_KMALLOC_TYPES];
55de8b9c 238 unsigned int size;
af3b5f87
VB
239} kmalloc_info[];
240
f97d5f63 241/* Kmalloc array related functions */
34cc6990 242void setup_kmalloc_cache_index_table(void);
d50112ed 243void create_kmalloc_caches(slab_flags_t);
2c59dd65
CL
244
245/* Find the kmalloc slab corresponding for a certain size */
246struct kmem_cache *kmalloc_slab(size_t, gfp_t);
ed4cd17e
HY
247
248void *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags,
249 int node, size_t orig_size,
250 unsigned long caller);
251void __kmem_cache_free(struct kmem_cache *s, void *x, unsigned long caller);
f97d5f63 252
44405099 253gfp_t kmalloc_fix_flags(gfp_t flags);
f97d5f63 254
9b030cb8 255/* Functions provided by the slab allocators */
d50112ed 256int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags);
97d06609 257
55de8b9c
AD
258struct kmem_cache *create_kmalloc_cache(const char *name, unsigned int size,
259 slab_flags_t flags, unsigned int useroffset,
260 unsigned int usersize);
45530c44 261extern void create_boot_cache(struct kmem_cache *, const char *name,
361d575e
AD
262 unsigned int size, slab_flags_t flags,
263 unsigned int useroffset, unsigned int usersize);
45530c44 264
423c929c 265int slab_unmergeable(struct kmem_cache *s);
f4957d5b 266struct kmem_cache *find_mergeable(unsigned size, unsigned align,
d50112ed 267 slab_flags_t flags, const char *name, void (*ctor)(void *));
2633d7a0 268struct kmem_cache *
f4957d5b 269__kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
d50112ed 270 slab_flags_t flags, void (*ctor)(void *));
423c929c 271
0293d1fd 272slab_flags_t kmem_cache_flags(unsigned int object_size,
37540008 273 slab_flags_t flags, const char *name);
cbb79694 274
bb944290
FT
275static inline bool is_kmalloc_cache(struct kmem_cache *s)
276{
bb944290 277 return (s->flags & SLAB_KMALLOC);
bb944290 278}
cbb79694 279
d8843922 280/* Legal flag mask for kmem_cache_create(), for various configurations */
6d6ea1e9
NB
281#define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
282 SLAB_CACHE_DMA32 | SLAB_PANIC | \
5f0d5a3a 283 SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS )
d8843922
GC
284
285#if defined(CONFIG_DEBUG_SLAB)
286#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
287#elif defined(CONFIG_SLUB_DEBUG)
288#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
becfda68 289 SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
d8843922
GC
290#else
291#define SLAB_DEBUG_FLAGS (0)
292#endif
293
294#if defined(CONFIG_SLAB)
295#define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
230e9fc2 296 SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
75f296d9 297 SLAB_ACCOUNT)
d8843922
GC
298#elif defined(CONFIG_SLUB)
299#define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
6cd6d33c
FT
300 SLAB_TEMPORARY | SLAB_ACCOUNT | \
301 SLAB_NO_USER_FLAGS | SLAB_KMALLOC)
d8843922 302#else
34dbc3aa 303#define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
d8843922
GC
304#endif
305
e70954fd 306/* Common flags available with current configuration */
d8843922
GC
307#define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
308
e70954fd
TG
309/* Common flags permitted for kmem_cache_create */
310#define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
311 SLAB_RED_ZONE | \
312 SLAB_POISON | \
313 SLAB_STORE_USER | \
314 SLAB_TRACE | \
315 SLAB_CONSISTENCY_CHECKS | \
316 SLAB_MEM_SPREAD | \
317 SLAB_NOLEAKTRACE | \
318 SLAB_RECLAIM_ACCOUNT | \
319 SLAB_TEMPORARY | \
a285909f 320 SLAB_ACCOUNT | \
6cd6d33c 321 SLAB_KMALLOC | \
a285909f 322 SLAB_NO_USER_FLAGS)
e70954fd 323
f9e13c0a 324bool __kmem_cache_empty(struct kmem_cache *);
945cf2b6 325int __kmem_cache_shutdown(struct kmem_cache *);
52b4b950 326void __kmem_cache_release(struct kmem_cache *);
c9fc5864 327int __kmem_cache_shrink(struct kmem_cache *);
41a21285 328void slab_kmem_cache_release(struct kmem_cache *);
945cf2b6 329
b7454ad3
GC
330struct seq_file;
331struct file;
b7454ad3 332
0d7561c6
GC
333struct slabinfo {
334 unsigned long active_objs;
335 unsigned long num_objs;
336 unsigned long active_slabs;
337 unsigned long num_slabs;
338 unsigned long shared_avail;
339 unsigned int limit;
340 unsigned int batchcount;
341 unsigned int shared;
342 unsigned int objects_per_slab;
343 unsigned int cache_order;
344};
345
346void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
347void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
b7454ad3
GC
348ssize_t slabinfo_write(struct file *file, const char __user *buffer,
349 size_t count, loff_t *ppos);
ba6c496e 350
1a984c4e 351static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s)
6cea1d56
RG
352{
353 return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
d42f3245 354 NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
6cea1d56
RG
355}
356
e42f174e
VB
357#ifdef CONFIG_SLUB_DEBUG
358#ifdef CONFIG_SLUB_DEBUG_ON
359DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
360#else
361DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
362#endif
363extern void print_tracking(struct kmem_cache *s, void *object);
1f9f78b1 364long validate_slab_cache(struct kmem_cache *s);
0d4a062a
ME
365static inline bool __slub_debug_enabled(void)
366{
367 return static_branch_unlikely(&slub_debug_enabled);
368}
e42f174e
VB
369#else
370static inline void print_tracking(struct kmem_cache *s, void *object)
371{
372}
0d4a062a
ME
373static inline bool __slub_debug_enabled(void)
374{
375 return false;
376}
e42f174e
VB
377#endif
378
379/*
380 * Returns true if any of the specified slub_debug flags is enabled for the
381 * cache. Use only for flags parsed by setup_slub_debug() as it also enables
382 * the static key.
383 */
384static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
385{
0d4a062a
ME
386 if (IS_ENABLED(CONFIG_SLUB_DEBUG))
387 VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
388 if (__slub_debug_enabled())
e42f174e 389 return s->flags & flags;
e42f174e
VB
390 return false;
391}
392
84c07d11 393#ifdef CONFIG_MEMCG_KMEM
4b5f8d9a
VB
394/*
395 * slab_objcgs - get the object cgroups vector associated with a slab
396 * @slab: a pointer to the slab struct
397 *
398 * Returns a pointer to the object cgroups vector associated with the slab,
399 * or NULL if no such vector has been associated yet.
400 */
401static inline struct obj_cgroup **slab_objcgs(struct slab *slab)
402{
403 unsigned long memcg_data = READ_ONCE(slab->memcg_data);
404
405 VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS),
406 slab_page(slab));
407 VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, slab_page(slab));
408
409 return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
410}
411
412int memcg_alloc_slab_cgroups(struct slab *slab, struct kmem_cache *s,
413 gfp_t gfp, bool new_slab);
fdbcb2a6
WL
414void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
415 enum node_stat_item idx, int nr);
286e04b8 416
4b5f8d9a 417static inline void memcg_free_slab_cgroups(struct slab *slab)
286e04b8 418{
4b5f8d9a
VB
419 kfree(slab_objcgs(slab));
420 slab->memcg_data = 0;
286e04b8
RG
421}
422
f2fe7b09
RG
423static inline size_t obj_full_size(struct kmem_cache *s)
424{
425 /*
426 * For each accounted object there is an extra space which is used
427 * to store obj_cgroup membership. Charge it too.
428 */
429 return s->size + sizeof(struct obj_cgroup *);
430}
431
becaba65
RG
432/*
433 * Returns false if the allocation should fail.
434 */
435static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
88f2ef73 436 struct list_lru *lru,
becaba65
RG
437 struct obj_cgroup **objcgp,
438 size_t objects, gfp_t flags)
f2fe7b09 439{
9855609b
RG
440 struct obj_cgroup *objcg;
441
f7a449f7 442 if (!memcg_kmem_online())
becaba65
RG
443 return true;
444
445 if (!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT))
446 return true;
447
9855609b
RG
448 objcg = get_obj_cgroup_from_current();
449 if (!objcg)
becaba65 450 return true;
9855609b 451
88f2ef73
MS
452 if (lru) {
453 int ret;
454 struct mem_cgroup *memcg;
455
456 memcg = get_mem_cgroup_from_objcg(objcg);
457 ret = memcg_list_lru_alloc(memcg, lru, flags);
458 css_put(&memcg->css);
459
460 if (ret)
461 goto out;
f2fe7b09
RG
462 }
463
88f2ef73
MS
464 if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s)))
465 goto out;
466
becaba65
RG
467 *objcgp = objcg;
468 return true;
88f2ef73
MS
469out:
470 obj_cgroup_put(objcg);
471 return false;
f2fe7b09
RG
472}
473
964d4bd3
RG
474static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
475 struct obj_cgroup *objcg,
10befea9
RG
476 gfp_t flags, size_t size,
477 void **p)
964d4bd3 478{
4b5f8d9a 479 struct slab *slab;
964d4bd3
RG
480 unsigned long off;
481 size_t i;
482
f7a449f7 483 if (!memcg_kmem_online() || !objcg)
10befea9
RG
484 return;
485
964d4bd3
RG
486 for (i = 0; i < size; i++) {
487 if (likely(p[i])) {
4b5f8d9a 488 slab = virt_to_slab(p[i]);
10befea9 489
4b5f8d9a
VB
490 if (!slab_objcgs(slab) &&
491 memcg_alloc_slab_cgroups(slab, s, flags,
2e9bd483 492 false)) {
10befea9
RG
493 obj_cgroup_uncharge(objcg, obj_full_size(s));
494 continue;
495 }
496
4b5f8d9a 497 off = obj_to_index(s, slab, p[i]);
964d4bd3 498 obj_cgroup_get(objcg);
4b5f8d9a
VB
499 slab_objcgs(slab)[off] = objcg;
500 mod_objcg_state(objcg, slab_pgdat(slab),
f2fe7b09
RG
501 cache_vmstat_idx(s), obj_full_size(s));
502 } else {
503 obj_cgroup_uncharge(objcg, obj_full_size(s));
964d4bd3
RG
504 }
505 }
506 obj_cgroup_put(objcg);
964d4bd3
RG
507}
508
b77d5b1b 509static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
d1b2cf6c 510 void **p, int objects)
964d4bd3 511{
270c6a71 512 struct obj_cgroup **objcgs;
d1b2cf6c 513 int i;
964d4bd3 514
f7a449f7 515 if (!memcg_kmem_online())
10befea9
RG
516 return;
517
b77d5b1b
MS
518 objcgs = slab_objcgs(slab);
519 if (!objcgs)
520 return;
f2fe7b09 521
b77d5b1b
MS
522 for (i = 0; i < objects; i++) {
523 struct obj_cgroup *objcg;
524 unsigned int off;
10befea9 525
4b5f8d9a 526 off = obj_to_index(s, slab, p[i]);
270c6a71 527 objcg = objcgs[off];
d1b2cf6c
BR
528 if (!objcg)
529 continue;
f2fe7b09 530
270c6a71 531 objcgs[off] = NULL;
d1b2cf6c 532 obj_cgroup_uncharge(objcg, obj_full_size(s));
4b5f8d9a 533 mod_objcg_state(objcg, slab_pgdat(slab), cache_vmstat_idx(s),
d1b2cf6c
BR
534 -obj_full_size(s));
535 obj_cgroup_put(objcg);
536 }
964d4bd3
RG
537}
538
84c07d11 539#else /* CONFIG_MEMCG_KMEM */
4b5f8d9a
VB
540static inline struct obj_cgroup **slab_objcgs(struct slab *slab)
541{
542 return NULL;
543}
544
9855609b 545static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
4d96ba35
RG
546{
547 return NULL;
548}
549
4b5f8d9a 550static inline int memcg_alloc_slab_cgroups(struct slab *slab,
2e9bd483 551 struct kmem_cache *s, gfp_t gfp,
4b5f8d9a 552 bool new_slab)
286e04b8
RG
553{
554 return 0;
555}
556
4b5f8d9a 557static inline void memcg_free_slab_cgroups(struct slab *slab)
286e04b8
RG
558{
559}
560
becaba65 561static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
88f2ef73 562 struct list_lru *lru,
becaba65
RG
563 struct obj_cgroup **objcgp,
564 size_t objects, gfp_t flags)
f2fe7b09 565{
becaba65 566 return true;
f2fe7b09
RG
567}
568
964d4bd3
RG
569static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
570 struct obj_cgroup *objcg,
10befea9
RG
571 gfp_t flags, size_t size,
572 void **p)
964d4bd3
RG
573{
574}
575
b77d5b1b 576static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
d1b2cf6c 577 void **p, int objects)
964d4bd3
RG
578{
579}
84c07d11 580#endif /* CONFIG_MEMCG_KMEM */
b9ce5ef4 581
a64b5378
KC
582static inline struct kmem_cache *virt_to_cache(const void *obj)
583{
82c1775d 584 struct slab *slab;
a64b5378 585
82c1775d
MWO
586 slab = virt_to_slab(obj);
587 if (WARN_ONCE(!slab, "%s: Object is not a Slab page!\n",
a64b5378
KC
588 __func__))
589 return NULL;
82c1775d 590 return slab->slab_cache;
a64b5378
KC
591}
592
b918653b
MWO
593static __always_inline void account_slab(struct slab *slab, int order,
594 struct kmem_cache *s, gfp_t gfp)
6cea1d56 595{
f7a449f7 596 if (memcg_kmem_online() && (s->flags & SLAB_ACCOUNT))
4b5f8d9a 597 memcg_alloc_slab_cgroups(slab, s, gfp, true);
2e9bd483 598
b918653b 599 mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
f2fe7b09 600 PAGE_SIZE << order);
6cea1d56
RG
601}
602
b918653b
MWO
603static __always_inline void unaccount_slab(struct slab *slab, int order,
604 struct kmem_cache *s)
6cea1d56 605{
f7a449f7 606 if (memcg_kmem_online())
4b5f8d9a 607 memcg_free_slab_cgroups(slab);
9855609b 608
b918653b 609 mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
f2fe7b09 610 -(PAGE_SIZE << order));
6cea1d56
RG
611}
612
e42f174e
VB
613static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
614{
615 struct kmem_cache *cachep;
616
617 if (!IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
e42f174e
VB
618 !kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS))
619 return s;
620
621 cachep = virt_to_cache(x);
10befea9 622 if (WARN(cachep && cachep != s,
e42f174e
VB
623 "%s: Wrong slab cache. %s but object is from %s\n",
624 __func__, s->name, cachep->name))
625 print_tracking(cachep, x);
626 return cachep;
627}
d6a71648
HY
628
629void free_large_kmalloc(struct folio *folio, void *object);
630
8dfa9d55
HY
631size_t __ksize(const void *objp);
632
11c7aec2
JDB
633static inline size_t slab_ksize(const struct kmem_cache *s)
634{
635#ifndef CONFIG_SLUB
636 return s->object_size;
637
638#else /* CONFIG_SLUB */
639# ifdef CONFIG_SLUB_DEBUG
640 /*
641 * Debugging requires use of the padding between object
642 * and whatever may come after it.
643 */
644 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
645 return s->object_size;
646# endif
80a9201a
AP
647 if (s->flags & SLAB_KASAN)
648 return s->object_size;
11c7aec2
JDB
649 /*
650 * If we have the need to store the freelist pointer
651 * back there or track user information then we can
652 * only use the space before that information.
653 */
5f0d5a3a 654 if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
11c7aec2
JDB
655 return s->inuse;
656 /*
657 * Else we can use all the padding etc for the allocation
658 */
659 return s->size;
660#endif
661}
662
663static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
88f2ef73 664 struct list_lru *lru,
964d4bd3
RG
665 struct obj_cgroup **objcgp,
666 size_t size, gfp_t flags)
11c7aec2
JDB
667{
668 flags &= gfp_allowed_mask;
d92a8cfc 669
95d6c701 670 might_alloc(flags);
11c7aec2 671
fab9963a 672 if (should_failslab(s, flags))
11c7aec2
JDB
673 return NULL;
674
88f2ef73 675 if (!memcg_slab_pre_alloc_hook(s, lru, objcgp, size, flags))
becaba65 676 return NULL;
45264778
VD
677
678 return s;
11c7aec2
JDB
679}
680
964d4bd3 681static inline void slab_post_alloc_hook(struct kmem_cache *s,
da844b78 682 struct obj_cgroup *objcg, gfp_t flags,
9ce67395
FT
683 size_t size, void **p, bool init,
684 unsigned int orig_size)
11c7aec2 685{
9ce67395 686 unsigned int zero_size = s->object_size;
11c7aec2
JDB
687 size_t i;
688
689 flags &= gfp_allowed_mask;
da844b78 690
9ce67395
FT
691 /*
692 * For kmalloc object, the allocated memory size(object_size) is likely
693 * larger than the requested size(orig_size). If redzone check is
694 * enabled for the extra space, don't zero it, as it will be redzoned
695 * soon. The redzone operation for this extra space could be seen as a
696 * replacement of current poisoning under certain debug option, and
697 * won't break other sanity checks.
698 */
699 if (kmem_cache_debug_flags(s, SLAB_STORE_USER | SLAB_RED_ZONE) &&
700 (s->flags & SLAB_KMALLOC))
701 zero_size = orig_size;
702
da844b78
AK
703 /*
704 * As memory initialization might be integrated into KASAN,
705 * kasan_slab_alloc and initialization memset must be
706 * kept together to avoid discrepancies in behavior.
707 *
708 * As p[i] might get tagged, memset and kmemleak hook come after KASAN.
709 */
11c7aec2 710 for (i = 0; i < size; i++) {
da844b78
AK
711 p[i] = kasan_slab_alloc(s, p[i], flags, init);
712 if (p[i] && init && !kasan_has_integrated_init())
9ce67395 713 memset(p[i], 0, zero_size);
53128245 714 kmemleak_alloc_recursive(p[i], s->object_size, 1,
11c7aec2 715 s->flags, flags);
68ef169a 716 kmsan_slab_alloc(s, p[i], flags);
11c7aec2 717 }
45264778 718
becaba65 719 memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
11c7aec2
JDB
720}
721
ca34956b
CL
722/*
723 * The slab lists for all objects.
724 */
725struct kmem_cache_node {
ca34956b 726#ifdef CONFIG_SLAB
b539ce9f 727 raw_spinlock_t list_lock;
ca34956b
CL
728 struct list_head slabs_partial; /* partial list first, better asm code */
729 struct list_head slabs_full;
730 struct list_head slabs_free;
bf00bd34
DR
731 unsigned long total_slabs; /* length of all slab lists */
732 unsigned long free_slabs; /* length of free slab list only */
ca34956b
CL
733 unsigned long free_objects;
734 unsigned int free_limit;
735 unsigned int colour_next; /* Per-node cache coloring */
736 struct array_cache *shared; /* shared per node */
c8522a3a 737 struct alien_cache **alien; /* on other nodes */
ca34956b
CL
738 unsigned long next_reap; /* updated without locking */
739 int free_touched; /* updated without locking */
740#endif
741
742#ifdef CONFIG_SLUB
b539ce9f 743 spinlock_t list_lock;
ca34956b
CL
744 unsigned long nr_partial;
745 struct list_head partial;
746#ifdef CONFIG_SLUB_DEBUG
747 atomic_long_t nr_slabs;
748 atomic_long_t total_objects;
749 struct list_head full;
750#endif
751#endif
752
753};
e25839f6 754
44c5356f
CL
755static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
756{
757 return s->node[node];
758}
759
760/*
761 * Iterator over all nodes. The body will be executed for each node that has
762 * a kmem_cache_node structure allocated (which is true for all online nodes)
763 */
764#define for_each_kmem_cache_node(__s, __node, __n) \
9163582c
MP
765 for (__node = 0; __node < nr_node_ids; __node++) \
766 if ((__n = get_node(__s, __node)))
44c5356f 767
44c5356f 768
852d8be0
YS
769#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
770void dump_unreclaimable_slab(void);
771#else
772static inline void dump_unreclaimable_slab(void)
773{
774}
775#endif
776
55834c59
AP
777void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
778
7c00fce9
TG
779#ifdef CONFIG_SLAB_FREELIST_RANDOM
780int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
781 gfp_t gfp);
782void cache_random_seq_destroy(struct kmem_cache *cachep);
783#else
784static inline int cache_random_seq_create(struct kmem_cache *cachep,
785 unsigned int count, gfp_t gfp)
786{
787 return 0;
788}
789static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
790#endif /* CONFIG_SLAB_FREELIST_RANDOM */
791
6471384a
AP
792static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
793{
51cba1eb
KC
794 if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
795 &init_on_alloc)) {
6471384a
AP
796 if (c->ctor)
797 return false;
798 if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
799 return flags & __GFP_ZERO;
800 return true;
801 }
802 return flags & __GFP_ZERO;
803}
804
805static inline bool slab_want_init_on_free(struct kmem_cache *c)
806{
51cba1eb
KC
807 if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
808 &init_on_free))
6471384a
AP
809 return !(c->ctor ||
810 (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
811 return false;
812}
813
64dd6849
FM
814#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
815void debugfs_slab_release(struct kmem_cache *);
816#else
817static inline void debugfs_slab_release(struct kmem_cache *s) { }
818#endif
819
5bb1bb35 820#ifdef CONFIG_PRINTK
8e7f37f2
PM
821#define KS_ADDRS_COUNT 16
822struct kmem_obj_info {
823 void *kp_ptr;
7213230a 824 struct slab *kp_slab;
8e7f37f2
PM
825 void *kp_objp;
826 unsigned long kp_data_offset;
827 struct kmem_cache *kp_slab_cache;
828 void *kp_ret;
829 void *kp_stack[KS_ADDRS_COUNT];
e548eaa1 830 void *kp_free_stack[KS_ADDRS_COUNT];
8e7f37f2 831};
2dfe63e6 832void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
5bb1bb35 833#endif
8e7f37f2 834
0b3eb091
MWO
835#ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR
836void __check_heap_object(const void *ptr, unsigned long n,
837 const struct slab *slab, bool to_user);
838#else
839static inline
840void __check_heap_object(const void *ptr, unsigned long n,
841 const struct slab *slab, bool to_user)
842{
843}
844#endif
845
946fa0db
FT
846#ifdef CONFIG_SLUB_DEBUG
847void skip_orig_size_check(struct kmem_cache *s, const void *object);
848#endif
849
5240ab40 850#endif /* MM_SLAB_H */