]> git.ipfire.org Git - thirdparty/linux.git/blame - mm/slub.c
Merge branch 'for-6.2/sony' into for-linus
[thirdparty/linux.git] / mm / slub.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
81819f0f
CL
2/*
3 * SLUB: A slab allocator that limits cache line use instead of queuing
4 * objects in per cpu and per node lists.
5 *
dc84207d 6 * The allocator synchronizes using per slab locks or atomic operations
881db7fb 7 * and only uses a centralized lock to manage a pool of partial slabs.
81819f0f 8 *
cde53535 9 * (C) 2007 SGI, Christoph Lameter
881db7fb 10 * (C) 2011 Linux Foundation, Christoph Lameter
81819f0f
CL
11 */
12
13#include <linux/mm.h>
1eb5ac64 14#include <linux/swap.h> /* struct reclaim_state */
81819f0f
CL
15#include <linux/module.h>
16#include <linux/bit_spinlock.h>
17#include <linux/interrupt.h>
1b3865d0 18#include <linux/swab.h>
81819f0f
CL
19#include <linux/bitops.h>
20#include <linux/slab.h>
97d06609 21#include "slab.h"
7b3c3a50 22#include <linux/proc_fs.h>
81819f0f 23#include <linux/seq_file.h>
a79316c6 24#include <linux/kasan.h>
68ef169a 25#include <linux/kmsan.h>
81819f0f
CL
26#include <linux/cpu.h>
27#include <linux/cpuset.h>
28#include <linux/mempolicy.h>
29#include <linux/ctype.h>
5cf909c5 30#include <linux/stackdepot.h>
3ac7fe5a 31#include <linux/debugobjects.h>
81819f0f 32#include <linux/kallsyms.h>
b89fb5ef 33#include <linux/kfence.h>
b9049e23 34#include <linux/memory.h>
f8bd2258 35#include <linux/math64.h>
773ff60e 36#include <linux/fault-inject.h>
bfa71457 37#include <linux/stacktrace.h>
4de900b4 38#include <linux/prefetch.h>
2633d7a0 39#include <linux/memcontrol.h>
2482ddec 40#include <linux/random.h>
1f9f78b1 41#include <kunit/test.h>
553c0369 42#include <linux/sort.h>
81819f0f 43
64dd6849 44#include <linux/debugfs.h>
4a92379b
RK
45#include <trace/events/kmem.h>
46
072bb0aa
MG
47#include "internal.h"
48
81819f0f
CL
49/*
50 * Lock order:
18004c5d 51 * 1. slab_mutex (Global Mutex)
bd0e7491
VB
52 * 2. node->list_lock (Spinlock)
53 * 3. kmem_cache->cpu_slab->lock (Local lock)
41bec7c3 54 * 4. slab_lock(slab) (Only on some arches)
bd0e7491 55 * 5. object_map_lock (Only for debugging)
81819f0f 56 *
18004c5d 57 * slab_mutex
881db7fb 58 *
18004c5d 59 * The role of the slab_mutex is to protect the list of all the slabs
881db7fb 60 * and to synchronize major metadata changes to slab cache structures.
bd0e7491
VB
61 * Also synchronizes memory hotplug callbacks.
62 *
63 * slab_lock
64 *
65 * The slab_lock is a wrapper around the page lock, thus it is a bit
66 * spinlock.
881db7fb 67 *
41bec7c3
VB
68 * The slab_lock is only used on arches that do not have the ability
69 * to do a cmpxchg_double. It only protects:
70 *
c2092c12
VB
71 * A. slab->freelist -> List of free objects in a slab
72 * B. slab->inuse -> Number of objects in use
73 * C. slab->objects -> Number of objects in slab
74 * D. slab->frozen -> frozen state
881db7fb 75 *
bd0e7491
VB
76 * Frozen slabs
77 *
881db7fb 78 * If a slab is frozen then it is exempt from list management. It is not
632b2ef0 79 * on any list except per cpu partial list. The processor that froze the
c2092c12 80 * slab is the one who can perform list operations on the slab. Other
632b2ef0
LX
81 * processors may put objects onto the freelist but the processor that
82 * froze the slab is the only one that can retrieve the objects from the
c2092c12 83 * slab's freelist.
81819f0f 84 *
bd0e7491
VB
85 * list_lock
86 *
81819f0f
CL
87 * The list_lock protects the partial and full list on each node and
88 * the partial slab counter. If taken then no new slabs may be added or
89 * removed from the lists nor make the number of partial slabs be modified.
90 * (Note that the total number of slabs is an atomic value that may be
91 * modified without taking the list lock).
92 *
93 * The list_lock is a centralized lock and thus we avoid taking it as
94 * much as possible. As long as SLUB does not have to handle partial
95 * slabs, operations can continue without any centralized lock. F.e.
96 * allocating a long series of objects that fill up slabs does not require
97 * the list lock.
bd0e7491 98 *
41bec7c3
VB
99 * For debug caches, all allocations are forced to go through a list_lock
100 * protected region to serialize against concurrent validation.
101 *
bd0e7491
VB
102 * cpu_slab->lock local lock
103 *
104 * This locks protect slowpath manipulation of all kmem_cache_cpu fields
105 * except the stat counters. This is a percpu structure manipulated only by
106 * the local cpu, so the lock protects against being preempted or interrupted
107 * by an irq. Fast path operations rely on lockless operations instead.
1f04b07d
TG
108 *
109 * On PREEMPT_RT, the local lock neither disables interrupts nor preemption
110 * which means the lockless fastpath cannot be used as it might interfere with
111 * an in-progress slow path operations. In this case the local lock is always
112 * taken but it still utilizes the freelist for the common operations.
bd0e7491
VB
113 *
114 * lockless fastpaths
115 *
116 * The fast path allocation (slab_alloc_node()) and freeing (do_slab_free())
117 * are fully lockless when satisfied from the percpu slab (and when
118 * cmpxchg_double is possible to use, otherwise slab_lock is taken).
119 * They also don't disable preemption or migration or irqs. They rely on
120 * the transaction id (tid) field to detect being preempted or moved to
121 * another cpu.
122 *
123 * irq, preemption, migration considerations
124 *
125 * Interrupts are disabled as part of list_lock or local_lock operations, or
126 * around the slab_lock operation, in order to make the slab allocator safe
127 * to use in the context of an irq.
128 *
129 * In addition, preemption (or migration on PREEMPT_RT) is disabled in the
130 * allocation slowpath, bulk allocation, and put_cpu_partial(), so that the
131 * local cpu doesn't change in the process and e.g. the kmem_cache_cpu pointer
132 * doesn't have to be revalidated in each section protected by the local lock.
81819f0f
CL
133 *
134 * SLUB assigns one slab for allocation to each processor.
135 * Allocations only occur from these slabs called cpu slabs.
136 *
672bba3a
CL
137 * Slabs with free elements are kept on a partial list and during regular
138 * operations no list for full slabs is used. If an object in a full slab is
81819f0f 139 * freed then the slab will show up again on the partial lists.
672bba3a
CL
140 * We track full slabs for debugging purposes though because otherwise we
141 * cannot scan all objects.
81819f0f
CL
142 *
143 * Slabs are freed when they become empty. Teardown and setup is
144 * minimal so we rely on the page allocators per cpu caches for
145 * fast frees and allocs.
146 *
c2092c12 147 * slab->frozen The slab is frozen and exempt from list processing.
4b6f0750
CL
148 * This means that the slab is dedicated to a purpose
149 * such as satisfying allocations for a specific
150 * processor. Objects may be freed in the slab while
151 * it is frozen but slab_free will then skip the usual
152 * list operations. It is up to the processor holding
153 * the slab to integrate the slab into the slab lists
154 * when the slab is no longer needed.
155 *
156 * One use of this flag is to mark slabs that are
157 * used for allocations. Then such a slab becomes a cpu
158 * slab. The cpu slab may be equipped with an additional
dfb4f096 159 * freelist that allows lockless access to
894b8788
CL
160 * free objects in addition to the regular freelist
161 * that requires the slab lock.
81819f0f 162 *
aed68148 163 * SLAB_DEBUG_FLAGS Slab requires special handling due to debug
81819f0f 164 * options set. This moves slab handling out of
894b8788 165 * the fast path and disables lockless freelists.
81819f0f
CL
166 */
167
25c00c50
VB
168/*
169 * We could simply use migrate_disable()/enable() but as long as it's a
170 * function call even on !PREEMPT_RT, use inline preempt_disable() there.
171 */
172#ifndef CONFIG_PREEMPT_RT
1f04b07d
TG
173#define slub_get_cpu_ptr(var) get_cpu_ptr(var)
174#define slub_put_cpu_ptr(var) put_cpu_ptr(var)
175#define USE_LOCKLESS_FAST_PATH() (true)
25c00c50
VB
176#else
177#define slub_get_cpu_ptr(var) \
178({ \
179 migrate_disable(); \
180 this_cpu_ptr(var); \
181})
182#define slub_put_cpu_ptr(var) \
183do { \
184 (void)(var); \
185 migrate_enable(); \
186} while (0)
1f04b07d 187#define USE_LOCKLESS_FAST_PATH() (false)
25c00c50
VB
188#endif
189
ca0cab65
VB
190#ifdef CONFIG_SLUB_DEBUG
191#ifdef CONFIG_SLUB_DEBUG_ON
192DEFINE_STATIC_KEY_TRUE(slub_debug_enabled);
193#else
194DEFINE_STATIC_KEY_FALSE(slub_debug_enabled);
195#endif
79270291 196#endif /* CONFIG_SLUB_DEBUG */
ca0cab65 197
6edf2576
FT
198/* Structure holding parameters for get_partial() call chain */
199struct partial_context {
200 struct slab **slab;
201 gfp_t flags;
202 unsigned int orig_size;
203};
204
59052e89
VB
205static inline bool kmem_cache_debug(struct kmem_cache *s)
206{
207 return kmem_cache_debug_flags(s, SLAB_DEBUG_FLAGS);
af537b0a 208}
5577bd8a 209
6edf2576
FT
210static inline bool slub_debug_orig_size(struct kmem_cache *s)
211{
212 return (kmem_cache_debug_flags(s, SLAB_STORE_USER) &&
213 (s->flags & SLAB_KMALLOC));
214}
215
117d54df 216void *fixup_red_left(struct kmem_cache *s, void *p)
d86bd1be 217{
59052e89 218 if (kmem_cache_debug_flags(s, SLAB_RED_ZONE))
d86bd1be
JK
219 p += s->red_left_pad;
220
221 return p;
222}
223
345c905d
JK
224static inline bool kmem_cache_has_cpu_partial(struct kmem_cache *s)
225{
226#ifdef CONFIG_SLUB_CPU_PARTIAL
227 return !kmem_cache_debug(s);
228#else
229 return false;
230#endif
231}
232
81819f0f
CL
233/*
234 * Issues still to be resolved:
235 *
81819f0f
CL
236 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
237 *
81819f0f
CL
238 * - Variable sizing of the per node arrays
239 */
240
b789ef51
CL
241/* Enable to log cmpxchg failures */
242#undef SLUB_DEBUG_CMPXCHG
243
2086d26a 244/*
dc84207d 245 * Minimum number of partial slabs. These will be left on the partial
2086d26a
CL
246 * lists even if they are empty. kmem_cache_shrink may reclaim them.
247 */
76be8950 248#define MIN_PARTIAL 5
e95eed57 249
2086d26a
CL
250/*
251 * Maximum number of desirable partial slabs.
252 * The existence of more partial slabs makes kmem_cache_shrink
721ae22a 253 * sort the partial list by the number of objects in use.
2086d26a
CL
254 */
255#define MAX_PARTIAL 10
256
becfda68 257#define DEBUG_DEFAULT_FLAGS (SLAB_CONSISTENCY_CHECKS | SLAB_RED_ZONE | \
81819f0f 258 SLAB_POISON | SLAB_STORE_USER)
672bba3a 259
149daaf3
LA
260/*
261 * These debug flags cannot use CMPXCHG because there might be consistency
262 * issues when checking or reading debug information
263 */
264#define SLAB_NO_CMPXCHG (SLAB_CONSISTENCY_CHECKS | SLAB_STORE_USER | \
265 SLAB_TRACE)
266
267
fa5ec8a1 268/*
3de47213
DR
269 * Debugging flags that require metadata to be stored in the slab. These get
270 * disabled when slub_debug=O is used and a cache's min order increases with
271 * metadata.
fa5ec8a1 272 */
3de47213 273#define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
fa5ec8a1 274
210b5c06
CG
275#define OO_SHIFT 16
276#define OO_MASK ((1 << OO_SHIFT) - 1)
c2092c12 277#define MAX_OBJS_PER_PAGE 32767 /* since slab.objects is u15 */
210b5c06 278
81819f0f 279/* Internal SLUB flags */
d50112ed 280/* Poison object */
4fd0b46e 281#define __OBJECT_POISON ((slab_flags_t __force)0x80000000U)
d50112ed 282/* Use cmpxchg_double */
4fd0b46e 283#define __CMPXCHG_DOUBLE ((slab_flags_t __force)0x40000000U)
81819f0f 284
02cbc874
CL
285/*
286 * Tracking user of a slab.
287 */
d6543e39 288#define TRACK_ADDRS_COUNT 16
02cbc874 289struct track {
ce71e27c 290 unsigned long addr; /* Called from address */
5cf909c5
OG
291#ifdef CONFIG_STACKDEPOT
292 depot_stack_handle_t handle;
d6543e39 293#endif
02cbc874
CL
294 int cpu; /* Was running on cpu */
295 int pid; /* Pid context */
296 unsigned long when; /* When did the operation occur */
297};
298
299enum track_item { TRACK_ALLOC, TRACK_FREE };
300
ab4d5ed5 301#ifdef CONFIG_SYSFS
81819f0f
CL
302static int sysfs_slab_add(struct kmem_cache *);
303static int sysfs_slab_alias(struct kmem_cache *, const char *);
81819f0f 304#else
0c710013
CL
305static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
306static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
307 { return 0; }
81819f0f
CL
308#endif
309
64dd6849
FM
310#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
311static void debugfs_slab_add(struct kmem_cache *);
312#else
313static inline void debugfs_slab_add(struct kmem_cache *s) { }
314#endif
315
4fdccdfb 316static inline void stat(const struct kmem_cache *s, enum stat_item si)
8ff12cfc
CL
317{
318#ifdef CONFIG_SLUB_STATS
88da03a6
CL
319 /*
320 * The rmw is racy on a preemptible kernel but this is acceptable, so
321 * avoid this_cpu_add()'s irq-disable overhead.
322 */
323 raw_cpu_inc(s->cpu_slab->stat[si]);
8ff12cfc
CL
324#endif
325}
326
7e1fa93d
VB
327/*
328 * Tracks for which NUMA nodes we have kmem_cache_nodes allocated.
329 * Corresponds to node_state[N_NORMAL_MEMORY], but can temporarily
330 * differ during memory hotplug/hotremove operations.
331 * Protected by slab_mutex.
332 */
333static nodemask_t slab_nodes;
334
e45cc288
ML
335/*
336 * Workqueue used for flush_cpu_slab().
337 */
338static struct workqueue_struct *flushwq;
339
81819f0f
CL
340/********************************************************************
341 * Core slab cache functions
342 *******************************************************************/
343
2482ddec
KC
344/*
345 * Returns freelist pointer (ptr). With hardening, this is obfuscated
346 * with an XOR of the address where the pointer is held and a per-cache
347 * random number.
348 */
349static inline void *freelist_ptr(const struct kmem_cache *s, void *ptr,
350 unsigned long ptr_addr)
351{
352#ifdef CONFIG_SLAB_FREELIST_HARDENED
d36a63a9 353 /*
aa1ef4d7 354 * When CONFIG_KASAN_SW/HW_TAGS is enabled, ptr_addr might be tagged.
d36a63a9
AK
355 * Normally, this doesn't cause any issues, as both set_freepointer()
356 * and get_freepointer() are called with a pointer with the same tag.
357 * However, there are some issues with CONFIG_SLUB_DEBUG code. For
358 * example, when __free_slub() iterates over objects in a cache, it
359 * passes untagged pointers to check_object(). check_object() in turns
360 * calls get_freepointer() with an untagged pointer, which causes the
361 * freepointer to be restored incorrectly.
362 */
363 return (void *)((unsigned long)ptr ^ s->random ^
1ad53d9f 364 swab((unsigned long)kasan_reset_tag((void *)ptr_addr)));
2482ddec
KC
365#else
366 return ptr;
367#endif
368}
369
370/* Returns the freelist pointer recorded at location ptr_addr. */
371static inline void *freelist_dereference(const struct kmem_cache *s,
372 void *ptr_addr)
373{
374 return freelist_ptr(s, (void *)*(unsigned long *)(ptr_addr),
375 (unsigned long)ptr_addr);
376}
377
7656c72b
CL
378static inline void *get_freepointer(struct kmem_cache *s, void *object)
379{
aa1ef4d7 380 object = kasan_reset_tag(object);
2482ddec 381 return freelist_dereference(s, object + s->offset);
7656c72b
CL
382}
383
0ad9500e
ED
384static void prefetch_freepointer(const struct kmem_cache *s, void *object)
385{
04b4b006 386 prefetchw(object + s->offset);
0ad9500e
ED
387}
388
68ef169a
AP
389/*
390 * When running under KMSAN, get_freepointer_safe() may return an uninitialized
391 * pointer value in the case the current thread loses the race for the next
392 * memory chunk in the freelist. In that case this_cpu_cmpxchg_double() in
393 * slab_alloc_node() will fail, so the uninitialized value won't be used, but
394 * KMSAN will still check all arguments of cmpxchg because of imperfect
395 * handling of inline assembly.
396 * To work around this problem, we apply __no_kmsan_checks to ensure that
397 * get_freepointer_safe() returns initialized memory.
398 */
399__no_kmsan_checks
1393d9a1
CL
400static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
401{
2482ddec 402 unsigned long freepointer_addr;
1393d9a1
CL
403 void *p;
404
8e57f8ac 405 if (!debug_pagealloc_enabled_static())
922d566c
JK
406 return get_freepointer(s, object);
407
f70b0049 408 object = kasan_reset_tag(object);
2482ddec 409 freepointer_addr = (unsigned long)object + s->offset;
fe557319 410 copy_from_kernel_nofault(&p, (void **)freepointer_addr, sizeof(p));
2482ddec 411 return freelist_ptr(s, p, freepointer_addr);
1393d9a1
CL
412}
413
7656c72b
CL
414static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
415{
2482ddec
KC
416 unsigned long freeptr_addr = (unsigned long)object + s->offset;
417
ce6fa91b
AP
418#ifdef CONFIG_SLAB_FREELIST_HARDENED
419 BUG_ON(object == fp); /* naive detection of double free or corruption */
420#endif
421
aa1ef4d7 422 freeptr_addr = (unsigned long)kasan_reset_tag((void *)freeptr_addr);
2482ddec 423 *(void **)freeptr_addr = freelist_ptr(s, fp, freeptr_addr);
7656c72b
CL
424}
425
426/* Loop over all objects in a slab */
224a88be 427#define for_each_object(__p, __s, __addr, __objects) \
d86bd1be
JK
428 for (__p = fixup_red_left(__s, __addr); \
429 __p < (__addr) + (__objects) * (__s)->size; \
430 __p += (__s)->size)
7656c72b 431
9736d2a9 432static inline unsigned int order_objects(unsigned int order, unsigned int size)
ab9a0f19 433{
9736d2a9 434 return ((unsigned int)PAGE_SIZE << order) / size;
ab9a0f19
LJ
435}
436
19af27af 437static inline struct kmem_cache_order_objects oo_make(unsigned int order,
9736d2a9 438 unsigned int size)
834f3d11
CL
439{
440 struct kmem_cache_order_objects x = {
9736d2a9 441 (order << OO_SHIFT) + order_objects(order, size)
834f3d11
CL
442 };
443
444 return x;
445}
446
19af27af 447static inline unsigned int oo_order(struct kmem_cache_order_objects x)
834f3d11 448{
210b5c06 449 return x.x >> OO_SHIFT;
834f3d11
CL
450}
451
19af27af 452static inline unsigned int oo_objects(struct kmem_cache_order_objects x)
834f3d11 453{
210b5c06 454 return x.x & OO_MASK;
834f3d11
CL
455}
456
b47291ef
VB
457#ifdef CONFIG_SLUB_CPU_PARTIAL
458static void slub_set_cpu_partial(struct kmem_cache *s, unsigned int nr_objects)
459{
bb192ed9 460 unsigned int nr_slabs;
b47291ef
VB
461
462 s->cpu_partial = nr_objects;
463
464 /*
465 * We take the number of objects but actually limit the number of
c2092c12
VB
466 * slabs on the per cpu partial list, in order to limit excessive
467 * growth of the list. For simplicity we assume that the slabs will
b47291ef
VB
468 * be half-full.
469 */
bb192ed9
VB
470 nr_slabs = DIV_ROUND_UP(nr_objects * 2, oo_objects(s->oo));
471 s->cpu_partial_slabs = nr_slabs;
b47291ef
VB
472}
473#else
474static inline void
475slub_set_cpu_partial(struct kmem_cache *s, unsigned int nr_objects)
476{
477}
478#endif /* CONFIG_SLUB_CPU_PARTIAL */
479
881db7fb
CL
480/*
481 * Per slab locking using the pagelock
482 */
5875e598 483static __always_inline void slab_lock(struct slab *slab)
881db7fb 484{
0393895b
VB
485 struct page *page = slab_page(slab);
486
48c935ad 487 VM_BUG_ON_PAGE(PageTail(page), page);
881db7fb
CL
488 bit_spin_lock(PG_locked, &page->flags);
489}
490
5875e598 491static __always_inline void slab_unlock(struct slab *slab)
881db7fb 492{
0393895b
VB
493 struct page *page = slab_page(slab);
494
48c935ad 495 VM_BUG_ON_PAGE(PageTail(page), page);
881db7fb
CL
496 __bit_spin_unlock(PG_locked, &page->flags);
497}
498
a2b4ae8b
VB
499/*
500 * Interrupts must be disabled (for the fallback code to work right), typically
5875e598
VB
501 * by an _irqsave() lock variant. On PREEMPT_RT the preempt_disable(), which is
502 * part of bit_spin_lock(), is sufficient because the policy is not to allow any
503 * allocation/ free operation in hardirq context. Therefore nothing can
504 * interrupt the operation.
a2b4ae8b 505 */
bb192ed9 506static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct slab *slab,
1d07171c
CL
507 void *freelist_old, unsigned long counters_old,
508 void *freelist_new, unsigned long counters_new,
509 const char *n)
510{
1f04b07d 511 if (USE_LOCKLESS_FAST_PATH())
a2b4ae8b 512 lockdep_assert_irqs_disabled();
2565409f
HC
513#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
514 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
1d07171c 515 if (s->flags & __CMPXCHG_DOUBLE) {
bb192ed9 516 if (cmpxchg_double(&slab->freelist, &slab->counters,
0aa9a13d
DC
517 freelist_old, counters_old,
518 freelist_new, counters_new))
6f6528a1 519 return true;
1d07171c
CL
520 } else
521#endif
522 {
5875e598 523 slab_lock(slab);
bb192ed9
VB
524 if (slab->freelist == freelist_old &&
525 slab->counters == counters_old) {
526 slab->freelist = freelist_new;
527 slab->counters = counters_new;
5875e598 528 slab_unlock(slab);
6f6528a1 529 return true;
1d07171c 530 }
5875e598 531 slab_unlock(slab);
1d07171c
CL
532 }
533
534 cpu_relax();
535 stat(s, CMPXCHG_DOUBLE_FAIL);
536
537#ifdef SLUB_DEBUG_CMPXCHG
f9f58285 538 pr_info("%s %s: cmpxchg double redo ", n, s->name);
1d07171c
CL
539#endif
540
6f6528a1 541 return false;
1d07171c
CL
542}
543
bb192ed9 544static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct slab *slab,
b789ef51
CL
545 void *freelist_old, unsigned long counters_old,
546 void *freelist_new, unsigned long counters_new,
547 const char *n)
548{
2565409f
HC
549#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
550 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
b789ef51 551 if (s->flags & __CMPXCHG_DOUBLE) {
bb192ed9 552 if (cmpxchg_double(&slab->freelist, &slab->counters,
0aa9a13d
DC
553 freelist_old, counters_old,
554 freelist_new, counters_new))
6f6528a1 555 return true;
b789ef51
CL
556 } else
557#endif
558 {
1d07171c
CL
559 unsigned long flags;
560
561 local_irq_save(flags);
5875e598 562 slab_lock(slab);
bb192ed9
VB
563 if (slab->freelist == freelist_old &&
564 slab->counters == counters_old) {
565 slab->freelist = freelist_new;
566 slab->counters = counters_new;
5875e598 567 slab_unlock(slab);
1d07171c 568 local_irq_restore(flags);
6f6528a1 569 return true;
b789ef51 570 }
5875e598 571 slab_unlock(slab);
1d07171c 572 local_irq_restore(flags);
b789ef51
CL
573 }
574
575 cpu_relax();
576 stat(s, CMPXCHG_DOUBLE_FAIL);
577
578#ifdef SLUB_DEBUG_CMPXCHG
f9f58285 579 pr_info("%s %s: cmpxchg double redo ", n, s->name);
b789ef51
CL
580#endif
581
6f6528a1 582 return false;
b789ef51
CL
583}
584
41ecc55b 585#ifdef CONFIG_SLUB_DEBUG
90e9f6a6 586static unsigned long object_map[BITS_TO_LONGS(MAX_OBJS_PER_PAGE)];
4ef3f5a3 587static DEFINE_SPINLOCK(object_map_lock);
90e9f6a6 588
b3fd64e1 589static void __fill_map(unsigned long *obj_map, struct kmem_cache *s,
bb192ed9 590 struct slab *slab)
b3fd64e1 591{
bb192ed9 592 void *addr = slab_address(slab);
b3fd64e1
VB
593 void *p;
594
bb192ed9 595 bitmap_zero(obj_map, slab->objects);
b3fd64e1 596
bb192ed9 597 for (p = slab->freelist; p; p = get_freepointer(s, p))
b3fd64e1
VB
598 set_bit(__obj_to_index(s, addr, p), obj_map);
599}
600
1f9f78b1
OG
601#if IS_ENABLED(CONFIG_KUNIT)
602static bool slab_add_kunit_errors(void)
603{
604 struct kunit_resource *resource;
605
606 if (likely(!current->kunit_test))
607 return false;
608
609 resource = kunit_find_named_resource(current->kunit_test, "slab_errors");
610 if (!resource)
611 return false;
612
613 (*(int *)resource->data)++;
614 kunit_put_resource(resource);
615 return true;
616}
617#else
618static inline bool slab_add_kunit_errors(void) { return false; }
619#endif
620
870b1fbb 621static inline unsigned int size_from_object(struct kmem_cache *s)
d86bd1be
JK
622{
623 if (s->flags & SLAB_RED_ZONE)
624 return s->size - s->red_left_pad;
625
626 return s->size;
627}
628
629static inline void *restore_red_left(struct kmem_cache *s, void *p)
630{
631 if (s->flags & SLAB_RED_ZONE)
632 p -= s->red_left_pad;
633
634 return p;
635}
636
41ecc55b
CL
637/*
638 * Debug settings:
639 */
89d3c87e 640#if defined(CONFIG_SLUB_DEBUG_ON)
d50112ed 641static slab_flags_t slub_debug = DEBUG_DEFAULT_FLAGS;
f0630fff 642#else
d50112ed 643static slab_flags_t slub_debug;
f0630fff 644#endif
41ecc55b 645
e17f1dfb 646static char *slub_debug_string;
fa5ec8a1 647static int disable_higher_order_debug;
41ecc55b 648
a79316c6
AR
649/*
650 * slub is about to manipulate internal object metadata. This memory lies
651 * outside the range of the allocated object, so accessing it would normally
652 * be reported by kasan as a bounds error. metadata_access_enable() is used
653 * to tell kasan that these accesses are OK.
654 */
655static inline void metadata_access_enable(void)
656{
657 kasan_disable_current();
658}
659
660static inline void metadata_access_disable(void)
661{
662 kasan_enable_current();
663}
664
81819f0f
CL
665/*
666 * Object debugging
667 */
d86bd1be
JK
668
669/* Verify that a pointer has an address that is valid within a slab page */
670static inline int check_valid_pointer(struct kmem_cache *s,
bb192ed9 671 struct slab *slab, void *object)
d86bd1be
JK
672{
673 void *base;
674
675 if (!object)
676 return 1;
677
bb192ed9 678 base = slab_address(slab);
338cfaad 679 object = kasan_reset_tag(object);
d86bd1be 680 object = restore_red_left(s, object);
bb192ed9 681 if (object < base || object >= base + slab->objects * s->size ||
d86bd1be
JK
682 (object - base) % s->size) {
683 return 0;
684 }
685
686 return 1;
687}
688
aa2efd5e
DT
689static void print_section(char *level, char *text, u8 *addr,
690 unsigned int length)
81819f0f 691{
a79316c6 692 metadata_access_enable();
340caf17
KYL
693 print_hex_dump(level, text, DUMP_PREFIX_ADDRESS,
694 16, 1, kasan_reset_tag((void *)addr), length, 1);
a79316c6 695 metadata_access_disable();
81819f0f
CL
696}
697
cbfc35a4
WL
698/*
699 * See comment in calculate_sizes().
700 */
701static inline bool freeptr_outside_object(struct kmem_cache *s)
702{
703 return s->offset >= s->inuse;
704}
705
706/*
707 * Return offset of the end of info block which is inuse + free pointer if
708 * not overlapping with object.
709 */
710static inline unsigned int get_info_end(struct kmem_cache *s)
711{
712 if (freeptr_outside_object(s))
713 return s->inuse + sizeof(void *);
714 else
715 return s->inuse;
716}
717
81819f0f
CL
718static struct track *get_track(struct kmem_cache *s, void *object,
719 enum track_item alloc)
720{
721 struct track *p;
722
cbfc35a4 723 p = object + get_info_end(s);
81819f0f 724
aa1ef4d7 725 return kasan_reset_tag(p + alloc);
81819f0f
CL
726}
727
5cf909c5 728#ifdef CONFIG_STACKDEPOT
c4cf6785
SAS
729static noinline depot_stack_handle_t set_track_prepare(void)
730{
731 depot_stack_handle_t handle;
5cf909c5 732 unsigned long entries[TRACK_ADDRS_COUNT];
0cd1a029 733 unsigned int nr_entries;
ae14c63a 734
5cf909c5 735 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 3);
c4cf6785
SAS
736 handle = stack_depot_save(entries, nr_entries, GFP_NOWAIT);
737
738 return handle;
739}
740#else
741static inline depot_stack_handle_t set_track_prepare(void)
742{
743 return 0;
744}
d6543e39 745#endif
5cf909c5 746
c4cf6785
SAS
747static void set_track_update(struct kmem_cache *s, void *object,
748 enum track_item alloc, unsigned long addr,
749 depot_stack_handle_t handle)
750{
751 struct track *p = get_track(s, object, alloc);
752
753#ifdef CONFIG_STACKDEPOT
754 p->handle = handle;
755#endif
0cd1a029
VB
756 p->addr = addr;
757 p->cpu = smp_processor_id();
758 p->pid = current->pid;
759 p->when = jiffies;
81819f0f
CL
760}
761
c4cf6785
SAS
762static __always_inline void set_track(struct kmem_cache *s, void *object,
763 enum track_item alloc, unsigned long addr)
764{
765 depot_stack_handle_t handle = set_track_prepare();
766
767 set_track_update(s, object, alloc, addr, handle);
768}
769
81819f0f
CL
770static void init_tracking(struct kmem_cache *s, void *object)
771{
0cd1a029
VB
772 struct track *p;
773
24922684
CL
774 if (!(s->flags & SLAB_STORE_USER))
775 return;
776
0cd1a029
VB
777 p = get_track(s, object, TRACK_ALLOC);
778 memset(p, 0, 2*sizeof(struct track));
81819f0f
CL
779}
780
86609d33 781static void print_track(const char *s, struct track *t, unsigned long pr_time)
81819f0f 782{
5cf909c5
OG
783 depot_stack_handle_t handle __maybe_unused;
784
81819f0f
CL
785 if (!t->addr)
786 return;
787
96b94abc 788 pr_err("%s in %pS age=%lu cpu=%u pid=%d\n",
86609d33 789 s, (void *)t->addr, pr_time - t->when, t->cpu, t->pid);
5cf909c5
OG
790#ifdef CONFIG_STACKDEPOT
791 handle = READ_ONCE(t->handle);
792 if (handle)
793 stack_depot_print(handle);
794 else
795 pr_err("object allocation/free stack trace missing\n");
d6543e39 796#endif
24922684
CL
797}
798
e42f174e 799void print_tracking(struct kmem_cache *s, void *object)
24922684 800{
86609d33 801 unsigned long pr_time = jiffies;
24922684
CL
802 if (!(s->flags & SLAB_STORE_USER))
803 return;
804
86609d33
CP
805 print_track("Allocated", get_track(s, object, TRACK_ALLOC), pr_time);
806 print_track("Freed", get_track(s, object, TRACK_FREE), pr_time);
24922684
CL
807}
808
fb012e27 809static void print_slab_info(const struct slab *slab)
24922684 810{
fb012e27 811 struct folio *folio = (struct folio *)slab_folio(slab);
24922684 812
fb012e27
MWO
813 pr_err("Slab 0x%p objects=%u used=%u fp=0x%p flags=%pGp\n",
814 slab, slab->objects, slab->inuse, slab->freelist,
815 folio_flags(folio, 0));
24922684
CL
816}
817
6edf2576
FT
818/*
819 * kmalloc caches has fixed sizes (mostly power of 2), and kmalloc() API
820 * family will round up the real request size to these fixed ones, so
821 * there could be an extra area than what is requested. Save the original
822 * request size in the meta data area, for better debug and sanity check.
823 */
824static inline void set_orig_size(struct kmem_cache *s,
825 void *object, unsigned int orig_size)
826{
827 void *p = kasan_reset_tag(object);
828
829 if (!slub_debug_orig_size(s))
830 return;
831
832 p += get_info_end(s);
833 p += sizeof(struct track) * 2;
834
835 *(unsigned int *)p = orig_size;
836}
837
838static inline unsigned int get_orig_size(struct kmem_cache *s, void *object)
839{
840 void *p = kasan_reset_tag(object);
841
842 if (!slub_debug_orig_size(s))
843 return s->object_size;
844
845 p += get_info_end(s);
846 p += sizeof(struct track) * 2;
847
848 return *(unsigned int *)p;
849}
850
24922684
CL
851static void slab_bug(struct kmem_cache *s, char *fmt, ...)
852{
ecc42fbe 853 struct va_format vaf;
24922684 854 va_list args;
24922684
CL
855
856 va_start(args, fmt);
ecc42fbe
FF
857 vaf.fmt = fmt;
858 vaf.va = &args;
f9f58285 859 pr_err("=============================================================================\n");
ecc42fbe 860 pr_err("BUG %s (%s): %pV\n", s->name, print_tainted(), &vaf);
f9f58285 861 pr_err("-----------------------------------------------------------------------------\n\n");
ecc42fbe 862 va_end(args);
81819f0f
CL
863}
864
582d1212 865__printf(2, 3)
24922684
CL
866static void slab_fix(struct kmem_cache *s, char *fmt, ...)
867{
ecc42fbe 868 struct va_format vaf;
24922684 869 va_list args;
24922684 870
1f9f78b1
OG
871 if (slab_add_kunit_errors())
872 return;
873
24922684 874 va_start(args, fmt);
ecc42fbe
FF
875 vaf.fmt = fmt;
876 vaf.va = &args;
877 pr_err("FIX %s: %pV\n", s->name, &vaf);
24922684 878 va_end(args);
24922684
CL
879}
880
bb192ed9 881static void print_trailer(struct kmem_cache *s, struct slab *slab, u8 *p)
81819f0f
CL
882{
883 unsigned int off; /* Offset of last byte */
bb192ed9 884 u8 *addr = slab_address(slab);
24922684
CL
885
886 print_tracking(s, p);
887
bb192ed9 888 print_slab_info(slab);
24922684 889
96b94abc 890 pr_err("Object 0x%p @offset=%tu fp=0x%p\n\n",
f9f58285 891 p, p - addr, get_freepointer(s, p));
24922684 892
d86bd1be 893 if (s->flags & SLAB_RED_ZONE)
8669dbab 894 print_section(KERN_ERR, "Redzone ", p - s->red_left_pad,
aa2efd5e 895 s->red_left_pad);
d86bd1be 896 else if (p > addr + 16)
aa2efd5e 897 print_section(KERN_ERR, "Bytes b4 ", p - 16, 16);
81819f0f 898
8669dbab 899 print_section(KERN_ERR, "Object ", p,
1b473f29 900 min_t(unsigned int, s->object_size, PAGE_SIZE));
81819f0f 901 if (s->flags & SLAB_RED_ZONE)
8669dbab 902 print_section(KERN_ERR, "Redzone ", p + s->object_size,
3b0efdfa 903 s->inuse - s->object_size);
81819f0f 904
cbfc35a4 905 off = get_info_end(s);
81819f0f 906
24922684 907 if (s->flags & SLAB_STORE_USER)
81819f0f 908 off += 2 * sizeof(struct track);
81819f0f 909
6edf2576
FT
910 if (slub_debug_orig_size(s))
911 off += sizeof(unsigned int);
912
80a9201a
AP
913 off += kasan_metadata_size(s);
914
d86bd1be 915 if (off != size_from_object(s))
81819f0f 916 /* Beginning of the filler is the free pointer */
8669dbab 917 print_section(KERN_ERR, "Padding ", p + off,
aa2efd5e 918 size_from_object(s) - off);
24922684
CL
919
920 dump_stack();
81819f0f
CL
921}
922
bb192ed9 923static void object_err(struct kmem_cache *s, struct slab *slab,
81819f0f
CL
924 u8 *object, char *reason)
925{
1f9f78b1
OG
926 if (slab_add_kunit_errors())
927 return;
928
3dc50637 929 slab_bug(s, "%s", reason);
bb192ed9 930 print_trailer(s, slab, object);
65ebdeef 931 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
81819f0f
CL
932}
933
bb192ed9 934static bool freelist_corrupted(struct kmem_cache *s, struct slab *slab,
ae16d059
VB
935 void **freelist, void *nextfree)
936{
937 if ((s->flags & SLAB_CONSISTENCY_CHECKS) &&
bb192ed9
VB
938 !check_valid_pointer(s, slab, nextfree) && freelist) {
939 object_err(s, slab, *freelist, "Freechain corrupt");
ae16d059
VB
940 *freelist = NULL;
941 slab_fix(s, "Isolate corrupted freechain");
942 return true;
943 }
944
945 return false;
946}
947
bb192ed9 948static __printf(3, 4) void slab_err(struct kmem_cache *s, struct slab *slab,
d0e0ac97 949 const char *fmt, ...)
81819f0f
CL
950{
951 va_list args;
952 char buf[100];
953
1f9f78b1
OG
954 if (slab_add_kunit_errors())
955 return;
956
24922684
CL
957 va_start(args, fmt);
958 vsnprintf(buf, sizeof(buf), fmt, args);
81819f0f 959 va_end(args);
3dc50637 960 slab_bug(s, "%s", buf);
bb192ed9 961 print_slab_info(slab);
81819f0f 962 dump_stack();
65ebdeef 963 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
81819f0f
CL
964}
965
f7cb1933 966static void init_object(struct kmem_cache *s, void *object, u8 val)
81819f0f 967{
aa1ef4d7 968 u8 *p = kasan_reset_tag(object);
81819f0f 969
d86bd1be
JK
970 if (s->flags & SLAB_RED_ZONE)
971 memset(p - s->red_left_pad, val, s->red_left_pad);
972
81819f0f 973 if (s->flags & __OBJECT_POISON) {
3b0efdfa
CL
974 memset(p, POISON_FREE, s->object_size - 1);
975 p[s->object_size - 1] = POISON_END;
81819f0f
CL
976 }
977
978 if (s->flags & SLAB_RED_ZONE)
3b0efdfa 979 memset(p + s->object_size, val, s->inuse - s->object_size);
81819f0f
CL
980}
981
24922684
CL
982static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
983 void *from, void *to)
984{
582d1212 985 slab_fix(s, "Restoring %s 0x%p-0x%p=0x%x", message, from, to - 1, data);
24922684
CL
986 memset(from, data, to - from);
987}
988
bb192ed9 989static int check_bytes_and_report(struct kmem_cache *s, struct slab *slab,
24922684 990 u8 *object, char *what,
06428780 991 u8 *start, unsigned int value, unsigned int bytes)
24922684
CL
992{
993 u8 *fault;
994 u8 *end;
bb192ed9 995 u8 *addr = slab_address(slab);
24922684 996
a79316c6 997 metadata_access_enable();
aa1ef4d7 998 fault = memchr_inv(kasan_reset_tag(start), value, bytes);
a79316c6 999 metadata_access_disable();
24922684
CL
1000 if (!fault)
1001 return 1;
1002
1003 end = start + bytes;
1004 while (end > fault && end[-1] == value)
1005 end--;
1006
1f9f78b1
OG
1007 if (slab_add_kunit_errors())
1008 goto skip_bug_print;
1009
24922684 1010 slab_bug(s, "%s overwritten", what);
96b94abc 1011 pr_err("0x%p-0x%p @offset=%tu. First byte 0x%x instead of 0x%x\n",
e1b70dd1
MC
1012 fault, end - 1, fault - addr,
1013 fault[0], value);
bb192ed9 1014 print_trailer(s, slab, object);
65ebdeef 1015 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
24922684 1016
1f9f78b1 1017skip_bug_print:
24922684
CL
1018 restore_bytes(s, what, value, fault, end);
1019 return 0;
81819f0f
CL
1020}
1021
81819f0f
CL
1022/*
1023 * Object layout:
1024 *
1025 * object address
1026 * Bytes of the object to be managed.
1027 * If the freepointer may overlay the object then the free
cbfc35a4 1028 * pointer is at the middle of the object.
672bba3a 1029 *
81819f0f
CL
1030 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
1031 * 0xa5 (POISON_END)
1032 *
3b0efdfa 1033 * object + s->object_size
81819f0f 1034 * Padding to reach word boundary. This is also used for Redzoning.
672bba3a 1035 * Padding is extended by another word if Redzoning is enabled and
3b0efdfa 1036 * object_size == inuse.
672bba3a 1037 *
81819f0f
CL
1038 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
1039 * 0xcc (RED_ACTIVE) for objects in use.
1040 *
1041 * object + s->inuse
672bba3a
CL
1042 * Meta data starts here.
1043 *
81819f0f
CL
1044 * A. Free pointer (if we cannot overwrite object on free)
1045 * B. Tracking data for SLAB_STORE_USER
6edf2576
FT
1046 * C. Original request size for kmalloc object (SLAB_STORE_USER enabled)
1047 * D. Padding to reach required alignment boundary or at minimum
6446faa2 1048 * one word if debugging is on to be able to detect writes
672bba3a
CL
1049 * before the word boundary.
1050 *
1051 * Padding is done using 0x5a (POISON_INUSE)
81819f0f
CL
1052 *
1053 * object + s->size
672bba3a 1054 * Nothing is used beyond s->size.
81819f0f 1055 *
3b0efdfa 1056 * If slabcaches are merged then the object_size and inuse boundaries are mostly
672bba3a 1057 * ignored. And therefore no slab options that rely on these boundaries
81819f0f
CL
1058 * may be used with merged slabcaches.
1059 */
1060
bb192ed9 1061static int check_pad_bytes(struct kmem_cache *s, struct slab *slab, u8 *p)
81819f0f 1062{
cbfc35a4 1063 unsigned long off = get_info_end(s); /* The end of info */
81819f0f 1064
6edf2576 1065 if (s->flags & SLAB_STORE_USER) {
81819f0f
CL
1066 /* We also have user information there */
1067 off += 2 * sizeof(struct track);
1068
6edf2576
FT
1069 if (s->flags & SLAB_KMALLOC)
1070 off += sizeof(unsigned int);
1071 }
1072
80a9201a
AP
1073 off += kasan_metadata_size(s);
1074
d86bd1be 1075 if (size_from_object(s) == off)
81819f0f
CL
1076 return 1;
1077
bb192ed9 1078 return check_bytes_and_report(s, slab, p, "Object padding",
d86bd1be 1079 p + off, POISON_INUSE, size_from_object(s) - off);
81819f0f
CL
1080}
1081
39b26464 1082/* Check the pad bytes at the end of a slab page */
a204e6d6 1083static void slab_pad_check(struct kmem_cache *s, struct slab *slab)
81819f0f 1084{
24922684
CL
1085 u8 *start;
1086 u8 *fault;
1087 u8 *end;
5d682681 1088 u8 *pad;
24922684
CL
1089 int length;
1090 int remainder;
81819f0f
CL
1091
1092 if (!(s->flags & SLAB_POISON))
a204e6d6 1093 return;
81819f0f 1094
bb192ed9
VB
1095 start = slab_address(slab);
1096 length = slab_size(slab);
39b26464
CL
1097 end = start + length;
1098 remainder = length % s->size;
81819f0f 1099 if (!remainder)
a204e6d6 1100 return;
81819f0f 1101
5d682681 1102 pad = end - remainder;
a79316c6 1103 metadata_access_enable();
aa1ef4d7 1104 fault = memchr_inv(kasan_reset_tag(pad), POISON_INUSE, remainder);
a79316c6 1105 metadata_access_disable();
24922684 1106 if (!fault)
a204e6d6 1107 return;
24922684
CL
1108 while (end > fault && end[-1] == POISON_INUSE)
1109 end--;
1110
bb192ed9 1111 slab_err(s, slab, "Padding overwritten. 0x%p-0x%p @offset=%tu",
e1b70dd1 1112 fault, end - 1, fault - start);
5d682681 1113 print_section(KERN_ERR, "Padding ", pad, remainder);
24922684 1114
5d682681 1115 restore_bytes(s, "slab padding", POISON_INUSE, fault, end);
81819f0f
CL
1116}
1117
bb192ed9 1118static int check_object(struct kmem_cache *s, struct slab *slab,
f7cb1933 1119 void *object, u8 val)
81819f0f
CL
1120{
1121 u8 *p = object;
3b0efdfa 1122 u8 *endobject = object + s->object_size;
81819f0f
CL
1123
1124 if (s->flags & SLAB_RED_ZONE) {
bb192ed9 1125 if (!check_bytes_and_report(s, slab, object, "Left Redzone",
d86bd1be
JK
1126 object - s->red_left_pad, val, s->red_left_pad))
1127 return 0;
1128
bb192ed9 1129 if (!check_bytes_and_report(s, slab, object, "Right Redzone",
3b0efdfa 1130 endobject, val, s->inuse - s->object_size))
81819f0f 1131 return 0;
81819f0f 1132 } else {
3b0efdfa 1133 if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
bb192ed9 1134 check_bytes_and_report(s, slab, p, "Alignment padding",
d0e0ac97
CG
1135 endobject, POISON_INUSE,
1136 s->inuse - s->object_size);
3adbefee 1137 }
81819f0f
CL
1138 }
1139
1140 if (s->flags & SLAB_POISON) {
f7cb1933 1141 if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) &&
bb192ed9 1142 (!check_bytes_and_report(s, slab, p, "Poison", p,
3b0efdfa 1143 POISON_FREE, s->object_size - 1) ||
bb192ed9 1144 !check_bytes_and_report(s, slab, p, "End Poison",
3b0efdfa 1145 p + s->object_size - 1, POISON_END, 1)))
81819f0f 1146 return 0;
81819f0f
CL
1147 /*
1148 * check_pad_bytes cleans up on its own.
1149 */
bb192ed9 1150 check_pad_bytes(s, slab, p);
81819f0f
CL
1151 }
1152
cbfc35a4 1153 if (!freeptr_outside_object(s) && val == SLUB_RED_ACTIVE)
81819f0f
CL
1154 /*
1155 * Object and freepointer overlap. Cannot check
1156 * freepointer while object is allocated.
1157 */
1158 return 1;
1159
1160 /* Check free pointer validity */
bb192ed9
VB
1161 if (!check_valid_pointer(s, slab, get_freepointer(s, p))) {
1162 object_err(s, slab, p, "Freepointer corrupt");
81819f0f 1163 /*
9f6c708e 1164 * No choice but to zap it and thus lose the remainder
81819f0f 1165 * of the free objects in this slab. May cause
672bba3a 1166 * another error because the object count is now wrong.
81819f0f 1167 */
a973e9dd 1168 set_freepointer(s, p, NULL);
81819f0f
CL
1169 return 0;
1170 }
1171 return 1;
1172}
1173
bb192ed9 1174static int check_slab(struct kmem_cache *s, struct slab *slab)
81819f0f 1175{
39b26464
CL
1176 int maxobj;
1177
bb192ed9
VB
1178 if (!folio_test_slab(slab_folio(slab))) {
1179 slab_err(s, slab, "Not a valid slab page");
81819f0f
CL
1180 return 0;
1181 }
39b26464 1182
bb192ed9
VB
1183 maxobj = order_objects(slab_order(slab), s->size);
1184 if (slab->objects > maxobj) {
1185 slab_err(s, slab, "objects %u > max %u",
1186 slab->objects, maxobj);
39b26464
CL
1187 return 0;
1188 }
bb192ed9
VB
1189 if (slab->inuse > slab->objects) {
1190 slab_err(s, slab, "inuse %u > max %u",
1191 slab->inuse, slab->objects);
81819f0f
CL
1192 return 0;
1193 }
1194 /* Slab_pad_check fixes things up after itself */
bb192ed9 1195 slab_pad_check(s, slab);
81819f0f
CL
1196 return 1;
1197}
1198
1199/*
c2092c12 1200 * Determine if a certain object in a slab is on the freelist. Must hold the
672bba3a 1201 * slab lock to guarantee that the chains are in a consistent state.
81819f0f 1202 */
bb192ed9 1203static int on_freelist(struct kmem_cache *s, struct slab *slab, void *search)
81819f0f
CL
1204{
1205 int nr = 0;
881db7fb 1206 void *fp;
81819f0f 1207 void *object = NULL;
f6edde9c 1208 int max_objects;
81819f0f 1209
bb192ed9
VB
1210 fp = slab->freelist;
1211 while (fp && nr <= slab->objects) {
81819f0f
CL
1212 if (fp == search)
1213 return 1;
bb192ed9 1214 if (!check_valid_pointer(s, slab, fp)) {
81819f0f 1215 if (object) {
bb192ed9 1216 object_err(s, slab, object,
81819f0f 1217 "Freechain corrupt");
a973e9dd 1218 set_freepointer(s, object, NULL);
81819f0f 1219 } else {
bb192ed9
VB
1220 slab_err(s, slab, "Freepointer corrupt");
1221 slab->freelist = NULL;
1222 slab->inuse = slab->objects;
24922684 1223 slab_fix(s, "Freelist cleared");
81819f0f
CL
1224 return 0;
1225 }
1226 break;
1227 }
1228 object = fp;
1229 fp = get_freepointer(s, object);
1230 nr++;
1231 }
1232
bb192ed9 1233 max_objects = order_objects(slab_order(slab), s->size);
210b5c06
CG
1234 if (max_objects > MAX_OBJS_PER_PAGE)
1235 max_objects = MAX_OBJS_PER_PAGE;
224a88be 1236
bb192ed9
VB
1237 if (slab->objects != max_objects) {
1238 slab_err(s, slab, "Wrong number of objects. Found %d but should be %d",
1239 slab->objects, max_objects);
1240 slab->objects = max_objects;
582d1212 1241 slab_fix(s, "Number of objects adjusted");
224a88be 1242 }
bb192ed9
VB
1243 if (slab->inuse != slab->objects - nr) {
1244 slab_err(s, slab, "Wrong object count. Counter is %d but counted were %d",
1245 slab->inuse, slab->objects - nr);
1246 slab->inuse = slab->objects - nr;
582d1212 1247 slab_fix(s, "Object count adjusted");
81819f0f
CL
1248 }
1249 return search == NULL;
1250}
1251
bb192ed9 1252static void trace(struct kmem_cache *s, struct slab *slab, void *object,
0121c619 1253 int alloc)
3ec09742
CL
1254{
1255 if (s->flags & SLAB_TRACE) {
f9f58285 1256 pr_info("TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
3ec09742
CL
1257 s->name,
1258 alloc ? "alloc" : "free",
bb192ed9
VB
1259 object, slab->inuse,
1260 slab->freelist);
3ec09742
CL
1261
1262 if (!alloc)
aa2efd5e 1263 print_section(KERN_INFO, "Object ", (void *)object,
d0e0ac97 1264 s->object_size);
3ec09742
CL
1265
1266 dump_stack();
1267 }
1268}
1269
643b1138 1270/*
672bba3a 1271 * Tracking of fully allocated slabs for debugging purposes.
643b1138 1272 */
5cc6eee8 1273static void add_full(struct kmem_cache *s,
bb192ed9 1274 struct kmem_cache_node *n, struct slab *slab)
643b1138 1275{
5cc6eee8
CL
1276 if (!(s->flags & SLAB_STORE_USER))
1277 return;
1278
255d0884 1279 lockdep_assert_held(&n->list_lock);
bb192ed9 1280 list_add(&slab->slab_list, &n->full);
643b1138
CL
1281}
1282
bb192ed9 1283static void remove_full(struct kmem_cache *s, struct kmem_cache_node *n, struct slab *slab)
643b1138 1284{
643b1138
CL
1285 if (!(s->flags & SLAB_STORE_USER))
1286 return;
1287
255d0884 1288 lockdep_assert_held(&n->list_lock);
bb192ed9 1289 list_del(&slab->slab_list);
643b1138
CL
1290}
1291
0f389ec6
CL
1292/* Tracking of the number of slabs for debugging purposes */
1293static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1294{
1295 struct kmem_cache_node *n = get_node(s, node);
1296
1297 return atomic_long_read(&n->nr_slabs);
1298}
1299
26c02cf0
AB
1300static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1301{
1302 return atomic_long_read(&n->nr_slabs);
1303}
1304
205ab99d 1305static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
0f389ec6
CL
1306{
1307 struct kmem_cache_node *n = get_node(s, node);
1308
1309 /*
1310 * May be called early in order to allocate a slab for the
1311 * kmem_cache_node structure. Solve the chicken-egg
1312 * dilemma by deferring the increment of the count during
1313 * bootstrap (see early_kmem_cache_node_alloc).
1314 */
338b2642 1315 if (likely(n)) {
0f389ec6 1316 atomic_long_inc(&n->nr_slabs);
205ab99d
CL
1317 atomic_long_add(objects, &n->total_objects);
1318 }
0f389ec6 1319}
205ab99d 1320static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
0f389ec6
CL
1321{
1322 struct kmem_cache_node *n = get_node(s, node);
1323
1324 atomic_long_dec(&n->nr_slabs);
205ab99d 1325 atomic_long_sub(objects, &n->total_objects);
0f389ec6
CL
1326}
1327
1328/* Object debug checks for alloc/free paths */
c0f81a94 1329static void setup_object_debug(struct kmem_cache *s, void *object)
3ec09742 1330{
8fc8d666 1331 if (!kmem_cache_debug_flags(s, SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON))
3ec09742
CL
1332 return;
1333
f7cb1933 1334 init_object(s, object, SLUB_RED_INACTIVE);
3ec09742
CL
1335 init_tracking(s, object);
1336}
1337
a50b854e 1338static
bb192ed9 1339void setup_slab_debug(struct kmem_cache *s, struct slab *slab, void *addr)
a7101224 1340{
8fc8d666 1341 if (!kmem_cache_debug_flags(s, SLAB_POISON))
a7101224
AK
1342 return;
1343
1344 metadata_access_enable();
bb192ed9 1345 memset(kasan_reset_tag(addr), POISON_INUSE, slab_size(slab));
a7101224
AK
1346 metadata_access_disable();
1347}
1348
becfda68 1349static inline int alloc_consistency_checks(struct kmem_cache *s,
bb192ed9 1350 struct slab *slab, void *object)
81819f0f 1351{
bb192ed9 1352 if (!check_slab(s, slab))
becfda68 1353 return 0;
81819f0f 1354
bb192ed9
VB
1355 if (!check_valid_pointer(s, slab, object)) {
1356 object_err(s, slab, object, "Freelist Pointer check fails");
becfda68 1357 return 0;
81819f0f
CL
1358 }
1359
bb192ed9 1360 if (!check_object(s, slab, object, SLUB_RED_INACTIVE))
becfda68
LA
1361 return 0;
1362
1363 return 1;
1364}
1365
1366static noinline int alloc_debug_processing(struct kmem_cache *s,
6edf2576 1367 struct slab *slab, void *object, int orig_size)
becfda68
LA
1368{
1369 if (s->flags & SLAB_CONSISTENCY_CHECKS) {
bb192ed9 1370 if (!alloc_consistency_checks(s, slab, object))
becfda68
LA
1371 goto bad;
1372 }
81819f0f 1373
c7323a5a 1374 /* Success. Perform special debug activities for allocs */
bb192ed9 1375 trace(s, slab, object, 1);
6edf2576 1376 set_orig_size(s, object, orig_size);
f7cb1933 1377 init_object(s, object, SLUB_RED_ACTIVE);
81819f0f 1378 return 1;
3ec09742 1379
81819f0f 1380bad:
bb192ed9 1381 if (folio_test_slab(slab_folio(slab))) {
81819f0f
CL
1382 /*
1383 * If this is a slab page then lets do the best we can
1384 * to avoid issues in the future. Marking all objects
672bba3a 1385 * as used avoids touching the remaining objects.
81819f0f 1386 */
24922684 1387 slab_fix(s, "Marking all objects used");
bb192ed9
VB
1388 slab->inuse = slab->objects;
1389 slab->freelist = NULL;
81819f0f
CL
1390 }
1391 return 0;
1392}
1393
becfda68 1394static inline int free_consistency_checks(struct kmem_cache *s,
bb192ed9 1395 struct slab *slab, void *object, unsigned long addr)
81819f0f 1396{
bb192ed9
VB
1397 if (!check_valid_pointer(s, slab, object)) {
1398 slab_err(s, slab, "Invalid object pointer 0x%p", object);
becfda68 1399 return 0;
81819f0f
CL
1400 }
1401
bb192ed9
VB
1402 if (on_freelist(s, slab, object)) {
1403 object_err(s, slab, object, "Object already free");
becfda68 1404 return 0;
81819f0f
CL
1405 }
1406
bb192ed9 1407 if (!check_object(s, slab, object, SLUB_RED_ACTIVE))
becfda68 1408 return 0;
81819f0f 1409
bb192ed9
VB
1410 if (unlikely(s != slab->slab_cache)) {
1411 if (!folio_test_slab(slab_folio(slab))) {
1412 slab_err(s, slab, "Attempt to free object(0x%p) outside of slab",
756a025f 1413 object);
bb192ed9 1414 } else if (!slab->slab_cache) {
f9f58285
FF
1415 pr_err("SLUB <none>: no slab for object 0x%p.\n",
1416 object);
70d71228 1417 dump_stack();
06428780 1418 } else
bb192ed9 1419 object_err(s, slab, object,
24922684 1420 "page slab pointer corrupt.");
becfda68
LA
1421 return 0;
1422 }
1423 return 1;
1424}
1425
e17f1dfb
VB
1426/*
1427 * Parse a block of slub_debug options. Blocks are delimited by ';'
1428 *
1429 * @str: start of block
1430 * @flags: returns parsed flags, or DEBUG_DEFAULT_FLAGS if none specified
1431 * @slabs: return start of list of slabs, or NULL when there's no list
1432 * @init: assume this is initial parsing and not per-kmem-create parsing
1433 *
1434 * returns the start of next block if there's any, or NULL
1435 */
1436static char *
1437parse_slub_debug_flags(char *str, slab_flags_t *flags, char **slabs, bool init)
41ecc55b 1438{
e17f1dfb 1439 bool higher_order_disable = false;
f0630fff 1440
e17f1dfb
VB
1441 /* Skip any completely empty blocks */
1442 while (*str && *str == ';')
1443 str++;
1444
1445 if (*str == ',') {
f0630fff
CL
1446 /*
1447 * No options but restriction on slabs. This means full
1448 * debugging for slabs matching a pattern.
1449 */
e17f1dfb 1450 *flags = DEBUG_DEFAULT_FLAGS;
f0630fff 1451 goto check_slabs;
e17f1dfb
VB
1452 }
1453 *flags = 0;
f0630fff 1454
e17f1dfb
VB
1455 /* Determine which debug features should be switched on */
1456 for (; *str && *str != ',' && *str != ';'; str++) {
f0630fff 1457 switch (tolower(*str)) {
e17f1dfb
VB
1458 case '-':
1459 *flags = 0;
1460 break;
f0630fff 1461 case 'f':
e17f1dfb 1462 *flags |= SLAB_CONSISTENCY_CHECKS;
f0630fff
CL
1463 break;
1464 case 'z':
e17f1dfb 1465 *flags |= SLAB_RED_ZONE;
f0630fff
CL
1466 break;
1467 case 'p':
e17f1dfb 1468 *flags |= SLAB_POISON;
f0630fff
CL
1469 break;
1470 case 'u':
e17f1dfb 1471 *flags |= SLAB_STORE_USER;
f0630fff
CL
1472 break;
1473 case 't':
e17f1dfb 1474 *flags |= SLAB_TRACE;
f0630fff 1475 break;
4c13dd3b 1476 case 'a':
e17f1dfb 1477 *flags |= SLAB_FAILSLAB;
4c13dd3b 1478 break;
08303a73
CA
1479 case 'o':
1480 /*
1481 * Avoid enabling debugging on caches if its minimum
1482 * order would increase as a result.
1483 */
e17f1dfb 1484 higher_order_disable = true;
08303a73 1485 break;
f0630fff 1486 default:
e17f1dfb
VB
1487 if (init)
1488 pr_err("slub_debug option '%c' unknown. skipped\n", *str);
f0630fff 1489 }
41ecc55b 1490 }
f0630fff 1491check_slabs:
41ecc55b 1492 if (*str == ',')
e17f1dfb
VB
1493 *slabs = ++str;
1494 else
1495 *slabs = NULL;
1496
1497 /* Skip over the slab list */
1498 while (*str && *str != ';')
1499 str++;
1500
1501 /* Skip any completely empty blocks */
1502 while (*str && *str == ';')
1503 str++;
1504
1505 if (init && higher_order_disable)
1506 disable_higher_order_debug = 1;
1507
1508 if (*str)
1509 return str;
1510 else
1511 return NULL;
1512}
1513
1514static int __init setup_slub_debug(char *str)
1515{
1516 slab_flags_t flags;
a7f1d485 1517 slab_flags_t global_flags;
e17f1dfb
VB
1518 char *saved_str;
1519 char *slab_list;
1520 bool global_slub_debug_changed = false;
1521 bool slab_list_specified = false;
1522
a7f1d485 1523 global_flags = DEBUG_DEFAULT_FLAGS;
e17f1dfb
VB
1524 if (*str++ != '=' || !*str)
1525 /*
1526 * No options specified. Switch on full debugging.
1527 */
1528 goto out;
1529
1530 saved_str = str;
1531 while (str) {
1532 str = parse_slub_debug_flags(str, &flags, &slab_list, true);
1533
1534 if (!slab_list) {
a7f1d485 1535 global_flags = flags;
e17f1dfb
VB
1536 global_slub_debug_changed = true;
1537 } else {
1538 slab_list_specified = true;
5cf909c5
OG
1539 if (flags & SLAB_STORE_USER)
1540 stack_depot_want_early_init();
e17f1dfb
VB
1541 }
1542 }
1543
1544 /*
1545 * For backwards compatibility, a single list of flags with list of
a7f1d485
VB
1546 * slabs means debugging is only changed for those slabs, so the global
1547 * slub_debug should be unchanged (0 or DEBUG_DEFAULT_FLAGS, depending
1548 * on CONFIG_SLUB_DEBUG_ON). We can extended that to multiple lists as
e17f1dfb
VB
1549 * long as there is no option specifying flags without a slab list.
1550 */
1551 if (slab_list_specified) {
1552 if (!global_slub_debug_changed)
a7f1d485 1553 global_flags = slub_debug;
e17f1dfb
VB
1554 slub_debug_string = saved_str;
1555 }
f0630fff 1556out:
a7f1d485 1557 slub_debug = global_flags;
5cf909c5
OG
1558 if (slub_debug & SLAB_STORE_USER)
1559 stack_depot_want_early_init();
ca0cab65
VB
1560 if (slub_debug != 0 || slub_debug_string)
1561 static_branch_enable(&slub_debug_enabled);
02ac47d0
SB
1562 else
1563 static_branch_disable(&slub_debug_enabled);
6471384a
AP
1564 if ((static_branch_unlikely(&init_on_alloc) ||
1565 static_branch_unlikely(&init_on_free)) &&
1566 (slub_debug & SLAB_POISON))
1567 pr_info("mem auto-init: SLAB_POISON will take precedence over init_on_alloc/init_on_free\n");
41ecc55b
CL
1568 return 1;
1569}
1570
1571__setup("slub_debug", setup_slub_debug);
1572
c5fd3ca0
AT
1573/*
1574 * kmem_cache_flags - apply debugging options to the cache
1575 * @object_size: the size of an object without meta data
1576 * @flags: flags to set
1577 * @name: name of the cache
c5fd3ca0
AT
1578 *
1579 * Debug option(s) are applied to @flags. In addition to the debug
1580 * option(s), if a slab name (or multiple) is specified i.e.
1581 * slub_debug=<Debug-Options>,<slab name1>,<slab name2> ...
1582 * then only the select slabs will receive the debug option(s).
1583 */
0293d1fd 1584slab_flags_t kmem_cache_flags(unsigned int object_size,
37540008 1585 slab_flags_t flags, const char *name)
41ecc55b 1586{
c5fd3ca0
AT
1587 char *iter;
1588 size_t len;
e17f1dfb
VB
1589 char *next_block;
1590 slab_flags_t block_flags;
ca220593
JB
1591 slab_flags_t slub_debug_local = slub_debug;
1592
a285909f
HY
1593 if (flags & SLAB_NO_USER_FLAGS)
1594 return flags;
1595
ca220593
JB
1596 /*
1597 * If the slab cache is for debugging (e.g. kmemleak) then
1598 * don't store user (stack trace) information by default,
1599 * but let the user enable it via the command line below.
1600 */
1601 if (flags & SLAB_NOLEAKTRACE)
1602 slub_debug_local &= ~SLAB_STORE_USER;
c5fd3ca0 1603
c5fd3ca0 1604 len = strlen(name);
e17f1dfb
VB
1605 next_block = slub_debug_string;
1606 /* Go through all blocks of debug options, see if any matches our slab's name */
1607 while (next_block) {
1608 next_block = parse_slub_debug_flags(next_block, &block_flags, &iter, false);
1609 if (!iter)
1610 continue;
1611 /* Found a block that has a slab list, search it */
1612 while (*iter) {
1613 char *end, *glob;
1614 size_t cmplen;
1615
1616 end = strchrnul(iter, ',');
1617 if (next_block && next_block < end)
1618 end = next_block - 1;
1619
1620 glob = strnchr(iter, end - iter, '*');
1621 if (glob)
1622 cmplen = glob - iter;
1623 else
1624 cmplen = max_t(size_t, len, (end - iter));
c5fd3ca0 1625
e17f1dfb
VB
1626 if (!strncmp(name, iter, cmplen)) {
1627 flags |= block_flags;
1628 return flags;
1629 }
c5fd3ca0 1630
e17f1dfb
VB
1631 if (!*end || *end == ';')
1632 break;
1633 iter = end + 1;
c5fd3ca0 1634 }
c5fd3ca0 1635 }
ba0268a8 1636
ca220593 1637 return flags | slub_debug_local;
41ecc55b 1638}
b4a64718 1639#else /* !CONFIG_SLUB_DEBUG */
c0f81a94 1640static inline void setup_object_debug(struct kmem_cache *s, void *object) {}
a50b854e 1641static inline
bb192ed9 1642void setup_slab_debug(struct kmem_cache *s, struct slab *slab, void *addr) {}
41ecc55b 1643
3ec09742 1644static inline int alloc_debug_processing(struct kmem_cache *s,
6edf2576 1645 struct slab *slab, void *object, int orig_size) { return 0; }
41ecc55b 1646
c7323a5a 1647static inline void free_debug_processing(
bb192ed9 1648 struct kmem_cache *s, struct slab *slab,
81084651 1649 void *head, void *tail, int bulk_cnt,
c7323a5a 1650 unsigned long addr) {}
41ecc55b 1651
a204e6d6 1652static inline void slab_pad_check(struct kmem_cache *s, struct slab *slab) {}
bb192ed9 1653static inline int check_object(struct kmem_cache *s, struct slab *slab,
f7cb1933 1654 void *object, u8 val) { return 1; }
c7323a5a
VB
1655static inline void set_track(struct kmem_cache *s, void *object,
1656 enum track_item alloc, unsigned long addr) {}
5cc6eee8 1657static inline void add_full(struct kmem_cache *s, struct kmem_cache_node *n,
bb192ed9 1658 struct slab *slab) {}
c65c1877 1659static inline void remove_full(struct kmem_cache *s, struct kmem_cache_node *n,
bb192ed9 1660 struct slab *slab) {}
0293d1fd 1661slab_flags_t kmem_cache_flags(unsigned int object_size,
37540008 1662 slab_flags_t flags, const char *name)
ba0268a8
CL
1663{
1664 return flags;
1665}
41ecc55b 1666#define slub_debug 0
0f389ec6 1667
fdaa45e9
IM
1668#define disable_higher_order_debug 0
1669
0f389ec6
CL
1670static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1671 { return 0; }
26c02cf0
AB
1672static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1673 { return 0; }
205ab99d
CL
1674static inline void inc_slabs_node(struct kmem_cache *s, int node,
1675 int objects) {}
1676static inline void dec_slabs_node(struct kmem_cache *s, int node,
1677 int objects) {}
7d550c56 1678
bb192ed9 1679static bool freelist_corrupted(struct kmem_cache *s, struct slab *slab,
dc07a728 1680 void **freelist, void *nextfree)
52f23478
DZ
1681{
1682 return false;
1683}
02e72cc6
AR
1684#endif /* CONFIG_SLUB_DEBUG */
1685
1686/*
1687 * Hooks for other subsystems that check memory allocations. In a typical
1688 * production configuration these hooks all should produce no code at all.
1689 */
d57a964e
AK
1690static __always_inline bool slab_free_hook(struct kmem_cache *s,
1691 void *x, bool init)
d56791b3
RB
1692{
1693 kmemleak_free_recursive(x, s->flags);
68ef169a 1694 kmsan_slab_free(s, x);
7d550c56 1695
84048039 1696 debug_check_no_locks_freed(x, s->object_size);
02e72cc6 1697
02e72cc6
AR
1698 if (!(s->flags & SLAB_DEBUG_OBJECTS))
1699 debug_check_no_obj_freed(x, s->object_size);
0316bec2 1700
cfbe1636
ME
1701 /* Use KCSAN to help debug racy use-after-free. */
1702 if (!(s->flags & SLAB_TYPESAFE_BY_RCU))
1703 __kcsan_check_access(x, s->object_size,
1704 KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT);
1705
d57a964e
AK
1706 /*
1707 * As memory initialization might be integrated into KASAN,
1708 * kasan_slab_free and initialization memset's must be
1709 * kept together to avoid discrepancies in behavior.
1710 *
1711 * The initialization memset's clear the object and the metadata,
1712 * but don't touch the SLAB redzone.
1713 */
1714 if (init) {
1715 int rsize;
1716
1717 if (!kasan_has_integrated_init())
1718 memset(kasan_reset_tag(x), 0, s->object_size);
1719 rsize = (s->flags & SLAB_RED_ZONE) ? s->red_left_pad : 0;
1720 memset((char *)kasan_reset_tag(x) + s->inuse, 0,
1721 s->size - s->inuse - rsize);
1722 }
1723 /* KASAN might put x into memory quarantine, delaying its reuse. */
1724 return kasan_slab_free(s, x, init);
02e72cc6 1725}
205ab99d 1726
c3895391 1727static inline bool slab_free_freelist_hook(struct kmem_cache *s,
899447f6
ML
1728 void **head, void **tail,
1729 int *cnt)
81084651 1730{
6471384a
AP
1731
1732 void *object;
1733 void *next = *head;
1734 void *old_tail = *tail ? *tail : *head;
6471384a 1735
b89fb5ef 1736 if (is_kfence_address(next)) {
d57a964e 1737 slab_free_hook(s, next, false);
b89fb5ef
AP
1738 return true;
1739 }
1740
aea4df4c
LA
1741 /* Head and tail of the reconstructed freelist */
1742 *head = NULL;
1743 *tail = NULL;
1b7e816f 1744
aea4df4c
LA
1745 do {
1746 object = next;
1747 next = get_freepointer(s, object);
1748
c3895391 1749 /* If object's reuse doesn't have to be delayed */
d57a964e 1750 if (!slab_free_hook(s, object, slab_want_init_on_free(s))) {
c3895391
AK
1751 /* Move object to the new freelist */
1752 set_freepointer(s, object, *head);
1753 *head = object;
1754 if (!*tail)
1755 *tail = object;
899447f6
ML
1756 } else {
1757 /*
1758 * Adjust the reconstructed freelist depth
1759 * accordingly if object's reuse is delayed.
1760 */
1761 --(*cnt);
c3895391
AK
1762 }
1763 } while (object != old_tail);
1764
1765 if (*head == *tail)
1766 *tail = NULL;
1767
1768 return *head != NULL;
81084651
JDB
1769}
1770
c0f81a94 1771static void *setup_object(struct kmem_cache *s, void *object)
588f8ba9 1772{
c0f81a94 1773 setup_object_debug(s, object);
4d176711 1774 object = kasan_init_slab_obj(s, object);
588f8ba9
TG
1775 if (unlikely(s->ctor)) {
1776 kasan_unpoison_object_data(s, object);
1777 s->ctor(object);
1778 kasan_poison_object_data(s, object);
1779 }
4d176711 1780 return object;
588f8ba9
TG
1781}
1782
81819f0f
CL
1783/*
1784 * Slab allocation and freeing
1785 */
a485e1da
XS
1786static inline struct slab *alloc_slab_page(gfp_t flags, int node,
1787 struct kmem_cache_order_objects oo)
65c3376a 1788{
45387b8c
VB
1789 struct folio *folio;
1790 struct slab *slab;
19af27af 1791 unsigned int order = oo_order(oo);
65c3376a 1792
2154a336 1793 if (node == NUMA_NO_NODE)
45387b8c 1794 folio = (struct folio *)alloc_pages(flags, order);
65c3376a 1795 else
45387b8c 1796 folio = (struct folio *)__alloc_pages_node(node, flags, order);
5dfb4175 1797
45387b8c
VB
1798 if (!folio)
1799 return NULL;
1800
1801 slab = folio_slab(folio);
1802 __folio_set_slab(folio);
1803 if (page_is_pfmemalloc(folio_page(folio, 0)))
1804 slab_set_pfmemalloc(slab);
1805
1806 return slab;
65c3376a
CL
1807}
1808
210e7a43
TG
1809#ifdef CONFIG_SLAB_FREELIST_RANDOM
1810/* Pre-initialize the random sequence cache */
1811static int init_cache_random_seq(struct kmem_cache *s)
1812{
19af27af 1813 unsigned int count = oo_objects(s->oo);
210e7a43 1814 int err;
210e7a43 1815
a810007a
SR
1816 /* Bailout if already initialised */
1817 if (s->random_seq)
1818 return 0;
1819
210e7a43
TG
1820 err = cache_random_seq_create(s, count, GFP_KERNEL);
1821 if (err) {
1822 pr_err("SLUB: Unable to initialize free list for %s\n",
1823 s->name);
1824 return err;
1825 }
1826
1827 /* Transform to an offset on the set of pages */
1828 if (s->random_seq) {
19af27af
AD
1829 unsigned int i;
1830
210e7a43
TG
1831 for (i = 0; i < count; i++)
1832 s->random_seq[i] *= s->size;
1833 }
1834 return 0;
1835}
1836
1837/* Initialize each random sequence freelist per cache */
1838static void __init init_freelist_randomization(void)
1839{
1840 struct kmem_cache *s;
1841
1842 mutex_lock(&slab_mutex);
1843
1844 list_for_each_entry(s, &slab_caches, list)
1845 init_cache_random_seq(s);
1846
1847 mutex_unlock(&slab_mutex);
1848}
1849
1850/* Get the next entry on the pre-computed freelist randomized */
bb192ed9 1851static void *next_freelist_entry(struct kmem_cache *s, struct slab *slab,
210e7a43
TG
1852 unsigned long *pos, void *start,
1853 unsigned long page_limit,
1854 unsigned long freelist_count)
1855{
1856 unsigned int idx;
1857
1858 /*
1859 * If the target page allocation failed, the number of objects on the
1860 * page might be smaller than the usual size defined by the cache.
1861 */
1862 do {
1863 idx = s->random_seq[*pos];
1864 *pos += 1;
1865 if (*pos >= freelist_count)
1866 *pos = 0;
1867 } while (unlikely(idx >= page_limit));
1868
1869 return (char *)start + idx;
1870}
1871
1872/* Shuffle the single linked freelist based on a random pre-computed sequence */
bb192ed9 1873static bool shuffle_freelist(struct kmem_cache *s, struct slab *slab)
210e7a43
TG
1874{
1875 void *start;
1876 void *cur;
1877 void *next;
1878 unsigned long idx, pos, page_limit, freelist_count;
1879
bb192ed9 1880 if (slab->objects < 2 || !s->random_seq)
210e7a43
TG
1881 return false;
1882
1883 freelist_count = oo_objects(s->oo);
81895a65 1884 pos = prandom_u32_max(freelist_count);
210e7a43 1885
bb192ed9
VB
1886 page_limit = slab->objects * s->size;
1887 start = fixup_red_left(s, slab_address(slab));
210e7a43
TG
1888
1889 /* First entry is used as the base of the freelist */
bb192ed9 1890 cur = next_freelist_entry(s, slab, &pos, start, page_limit,
210e7a43 1891 freelist_count);
c0f81a94 1892 cur = setup_object(s, cur);
bb192ed9 1893 slab->freelist = cur;
210e7a43 1894
bb192ed9
VB
1895 for (idx = 1; idx < slab->objects; idx++) {
1896 next = next_freelist_entry(s, slab, &pos, start, page_limit,
210e7a43 1897 freelist_count);
c0f81a94 1898 next = setup_object(s, next);
210e7a43
TG
1899 set_freepointer(s, cur, next);
1900 cur = next;
1901 }
210e7a43
TG
1902 set_freepointer(s, cur, NULL);
1903
1904 return true;
1905}
1906#else
1907static inline int init_cache_random_seq(struct kmem_cache *s)
1908{
1909 return 0;
1910}
1911static inline void init_freelist_randomization(void) { }
bb192ed9 1912static inline bool shuffle_freelist(struct kmem_cache *s, struct slab *slab)
210e7a43
TG
1913{
1914 return false;
1915}
1916#endif /* CONFIG_SLAB_FREELIST_RANDOM */
1917
bb192ed9 1918static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
81819f0f 1919{
bb192ed9 1920 struct slab *slab;
834f3d11 1921 struct kmem_cache_order_objects oo = s->oo;
ba52270d 1922 gfp_t alloc_gfp;
4d176711 1923 void *start, *p, *next;
a50b854e 1924 int idx;
210e7a43 1925 bool shuffle;
81819f0f 1926
7e0528da
CL
1927 flags &= gfp_allowed_mask;
1928
b7a49f0d 1929 flags |= s->allocflags;
e12ba74d 1930
ba52270d
PE
1931 /*
1932 * Let the initial higher-order allocation fail under memory pressure
1933 * so we fall-back to the minimum order allocation.
1934 */
1935 alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
d0164adc 1936 if ((alloc_gfp & __GFP_DIRECT_RECLAIM) && oo_order(oo) > oo_order(s->min))
27c08f75 1937 alloc_gfp = (alloc_gfp | __GFP_NOMEMALLOC) & ~__GFP_RECLAIM;
ba52270d 1938
a485e1da 1939 slab = alloc_slab_page(alloc_gfp, node, oo);
bb192ed9 1940 if (unlikely(!slab)) {
65c3376a 1941 oo = s->min;
80c3a998 1942 alloc_gfp = flags;
65c3376a
CL
1943 /*
1944 * Allocation may have failed due to fragmentation.
1945 * Try a lower order alloc if possible
1946 */
a485e1da 1947 slab = alloc_slab_page(alloc_gfp, node, oo);
bb192ed9 1948 if (unlikely(!slab))
c7323a5a 1949 return NULL;
588f8ba9 1950 stat(s, ORDER_FALLBACK);
65c3376a 1951 }
5a896d9e 1952
bb192ed9 1953 slab->objects = oo_objects(oo);
c7323a5a
VB
1954 slab->inuse = 0;
1955 slab->frozen = 0;
81819f0f 1956
bb192ed9 1957 account_slab(slab, oo_order(oo), s, flags);
1f3147b4 1958
bb192ed9 1959 slab->slab_cache = s;
81819f0f 1960
6e48a966 1961 kasan_poison_slab(slab);
81819f0f 1962
bb192ed9 1963 start = slab_address(slab);
81819f0f 1964
bb192ed9 1965 setup_slab_debug(s, slab, start);
0316bec2 1966
bb192ed9 1967 shuffle = shuffle_freelist(s, slab);
210e7a43
TG
1968
1969 if (!shuffle) {
4d176711 1970 start = fixup_red_left(s, start);
c0f81a94 1971 start = setup_object(s, start);
bb192ed9
VB
1972 slab->freelist = start;
1973 for (idx = 0, p = start; idx < slab->objects - 1; idx++) {
18e50661 1974 next = p + s->size;
c0f81a94 1975 next = setup_object(s, next);
18e50661
AK
1976 set_freepointer(s, p, next);
1977 p = next;
1978 }
1979 set_freepointer(s, p, NULL);
81819f0f 1980 }
81819f0f 1981
bb192ed9 1982 return slab;
81819f0f
CL
1983}
1984
bb192ed9 1985static struct slab *new_slab(struct kmem_cache *s, gfp_t flags, int node)
588f8ba9 1986{
44405099
LL
1987 if (unlikely(flags & GFP_SLAB_BUG_MASK))
1988 flags = kmalloc_fix_flags(flags);
588f8ba9 1989
53a0de06
VB
1990 WARN_ON_ONCE(s->ctor && (flags & __GFP_ZERO));
1991
588f8ba9
TG
1992 return allocate_slab(s,
1993 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
1994}
1995
4020b4a2 1996static void __free_slab(struct kmem_cache *s, struct slab *slab)
81819f0f 1997{
4020b4a2
VB
1998 struct folio *folio = slab_folio(slab);
1999 int order = folio_order(folio);
834f3d11 2000 int pages = 1 << order;
81819f0f 2001
8fc8d666 2002 if (kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS)) {
81819f0f
CL
2003 void *p;
2004
bb192ed9 2005 slab_pad_check(s, slab);
4020b4a2 2006 for_each_object(p, s, slab_address(slab), slab->objects)
bb192ed9 2007 check_object(s, slab, p, SLUB_RED_INACTIVE);
81819f0f
CL
2008 }
2009
4020b4a2
VB
2010 __slab_clear_pfmemalloc(slab);
2011 __folio_clear_slab(folio);
2012 folio->mapping = NULL;
1eb5ac64
NP
2013 if (current->reclaim_state)
2014 current->reclaim_state->reclaimed_slab += pages;
4020b4a2
VB
2015 unaccount_slab(slab, order, s);
2016 __free_pages(folio_page(folio, 0), order);
81819f0f
CL
2017}
2018
2019static void rcu_free_slab(struct rcu_head *h)
2020{
bb192ed9 2021 struct slab *slab = container_of(h, struct slab, rcu_head);
da9a638c 2022
bb192ed9 2023 __free_slab(slab->slab_cache, slab);
81819f0f
CL
2024}
2025
bb192ed9 2026static void free_slab(struct kmem_cache *s, struct slab *slab)
81819f0f 2027{
5f0d5a3a 2028 if (unlikely(s->flags & SLAB_TYPESAFE_BY_RCU)) {
bb192ed9 2029 call_rcu(&slab->rcu_head, rcu_free_slab);
81819f0f 2030 } else
bb192ed9 2031 __free_slab(s, slab);
81819f0f
CL
2032}
2033
bb192ed9 2034static void discard_slab(struct kmem_cache *s, struct slab *slab)
81819f0f 2035{
bb192ed9
VB
2036 dec_slabs_node(s, slab_nid(slab), slab->objects);
2037 free_slab(s, slab);
81819f0f
CL
2038}
2039
2040/*
5cc6eee8 2041 * Management of partially allocated slabs.
81819f0f 2042 */
1e4dd946 2043static inline void
bb192ed9 2044__add_partial(struct kmem_cache_node *n, struct slab *slab, int tail)
81819f0f 2045{
e95eed57 2046 n->nr_partial++;
136333d1 2047 if (tail == DEACTIVATE_TO_TAIL)
bb192ed9 2048 list_add_tail(&slab->slab_list, &n->partial);
7c2e132c 2049 else
bb192ed9 2050 list_add(&slab->slab_list, &n->partial);
81819f0f
CL
2051}
2052
1e4dd946 2053static inline void add_partial(struct kmem_cache_node *n,
bb192ed9 2054 struct slab *slab, int tail)
62e346a8 2055{
c65c1877 2056 lockdep_assert_held(&n->list_lock);
bb192ed9 2057 __add_partial(n, slab, tail);
1e4dd946 2058}
c65c1877 2059
1e4dd946 2060static inline void remove_partial(struct kmem_cache_node *n,
bb192ed9 2061 struct slab *slab)
1e4dd946
SR
2062{
2063 lockdep_assert_held(&n->list_lock);
bb192ed9 2064 list_del(&slab->slab_list);
52b4b950 2065 n->nr_partial--;
1e4dd946
SR
2066}
2067
c7323a5a
VB
2068/*
2069 * Called only for kmem_cache_debug() caches instead of acquire_slab(), with a
2070 * slab from the n->partial list. Remove only a single object from the slab, do
2071 * the alloc_debug_processing() checks and leave the slab on the list, or move
2072 * it to full list if it was the last free object.
2073 */
2074static void *alloc_single_from_partial(struct kmem_cache *s,
6edf2576 2075 struct kmem_cache_node *n, struct slab *slab, int orig_size)
c7323a5a
VB
2076{
2077 void *object;
2078
2079 lockdep_assert_held(&n->list_lock);
2080
2081 object = slab->freelist;
2082 slab->freelist = get_freepointer(s, object);
2083 slab->inuse++;
2084
6edf2576 2085 if (!alloc_debug_processing(s, slab, object, orig_size)) {
c7323a5a
VB
2086 remove_partial(n, slab);
2087 return NULL;
2088 }
2089
2090 if (slab->inuse == slab->objects) {
2091 remove_partial(n, slab);
2092 add_full(s, n, slab);
2093 }
2094
2095 return object;
2096}
2097
2098/*
2099 * Called only for kmem_cache_debug() caches to allocate from a freshly
2100 * allocated slab. Allocate a single object instead of whole freelist
2101 * and put the slab to the partial (or full) list.
2102 */
2103static void *alloc_single_from_new_slab(struct kmem_cache *s,
6edf2576 2104 struct slab *slab, int orig_size)
c7323a5a
VB
2105{
2106 int nid = slab_nid(slab);
2107 struct kmem_cache_node *n = get_node(s, nid);
2108 unsigned long flags;
2109 void *object;
2110
2111
2112 object = slab->freelist;
2113 slab->freelist = get_freepointer(s, object);
2114 slab->inuse = 1;
2115
6edf2576 2116 if (!alloc_debug_processing(s, slab, object, orig_size))
c7323a5a
VB
2117 /*
2118 * It's not really expected that this would fail on a
2119 * freshly allocated slab, but a concurrent memory
2120 * corruption in theory could cause that.
2121 */
2122 return NULL;
2123
2124 spin_lock_irqsave(&n->list_lock, flags);
2125
2126 if (slab->inuse == slab->objects)
2127 add_full(s, n, slab);
2128 else
2129 add_partial(n, slab, DEACTIVATE_TO_HEAD);
2130
2131 inc_slabs_node(s, nid, slab->objects);
2132 spin_unlock_irqrestore(&n->list_lock, flags);
2133
2134 return object;
2135}
2136
81819f0f 2137/*
7ced3719
CL
2138 * Remove slab from the partial list, freeze it and
2139 * return the pointer to the freelist.
81819f0f 2140 *
497b66f2 2141 * Returns a list of objects or NULL if it fails.
81819f0f 2142 */
497b66f2 2143static inline void *acquire_slab(struct kmem_cache *s,
bb192ed9 2144 struct kmem_cache_node *n, struct slab *slab,
b47291ef 2145 int mode)
81819f0f 2146{
2cfb7455
CL
2147 void *freelist;
2148 unsigned long counters;
bb192ed9 2149 struct slab new;
2cfb7455 2150
c65c1877
PZ
2151 lockdep_assert_held(&n->list_lock);
2152
2cfb7455
CL
2153 /*
2154 * Zap the freelist and set the frozen bit.
2155 * The old freelist is the list of objects for the
2156 * per cpu allocation list.
2157 */
bb192ed9
VB
2158 freelist = slab->freelist;
2159 counters = slab->counters;
7ced3719 2160 new.counters = counters;
23910c50 2161 if (mode) {
bb192ed9 2162 new.inuse = slab->objects;
23910c50
PE
2163 new.freelist = NULL;
2164 } else {
2165 new.freelist = freelist;
2166 }
2cfb7455 2167
a0132ac0 2168 VM_BUG_ON(new.frozen);
7ced3719 2169 new.frozen = 1;
2cfb7455 2170
bb192ed9 2171 if (!__cmpxchg_double_slab(s, slab,
2cfb7455 2172 freelist, counters,
02d7633f 2173 new.freelist, new.counters,
7ced3719 2174 "acquire_slab"))
7ced3719 2175 return NULL;
2cfb7455 2176
bb192ed9 2177 remove_partial(n, slab);
7ced3719 2178 WARN_ON(!freelist);
49e22585 2179 return freelist;
81819f0f
CL
2180}
2181
e0a043aa 2182#ifdef CONFIG_SLUB_CPU_PARTIAL
bb192ed9 2183static void put_cpu_partial(struct kmem_cache *s, struct slab *slab, int drain);
e0a043aa 2184#else
bb192ed9 2185static inline void put_cpu_partial(struct kmem_cache *s, struct slab *slab,
e0a043aa
VB
2186 int drain) { }
2187#endif
01b34d16 2188static inline bool pfmemalloc_match(struct slab *slab, gfp_t gfpflags);
49e22585 2189
81819f0f 2190/*
672bba3a 2191 * Try to allocate a partial slab from a specific node.
81819f0f 2192 */
8ba00bb6 2193static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
6edf2576 2194 struct partial_context *pc)
81819f0f 2195{
bb192ed9 2196 struct slab *slab, *slab2;
49e22585 2197 void *object = NULL;
4b1f449d 2198 unsigned long flags;
bb192ed9 2199 unsigned int partial_slabs = 0;
81819f0f
CL
2200
2201 /*
2202 * Racy check. If we mistakenly see no partial slabs then we
2203 * just allocate an empty slab. If we mistakenly try to get a
70b6d25e 2204 * partial slab and there is none available then get_partial()
672bba3a 2205 * will return NULL.
81819f0f
CL
2206 */
2207 if (!n || !n->nr_partial)
2208 return NULL;
2209
4b1f449d 2210 spin_lock_irqsave(&n->list_lock, flags);
bb192ed9 2211 list_for_each_entry_safe(slab, slab2, &n->partial, slab_list) {
8ba00bb6 2212 void *t;
49e22585 2213
6edf2576 2214 if (!pfmemalloc_match(slab, pc->flags))
8ba00bb6
JK
2215 continue;
2216
c7323a5a 2217 if (kmem_cache_debug(s)) {
6edf2576
FT
2218 object = alloc_single_from_partial(s, n, slab,
2219 pc->orig_size);
c7323a5a
VB
2220 if (object)
2221 break;
2222 continue;
2223 }
2224
bb192ed9 2225 t = acquire_slab(s, n, slab, object == NULL);
49e22585 2226 if (!t)
9b1ea29b 2227 break;
49e22585 2228
12d79634 2229 if (!object) {
6edf2576 2230 *pc->slab = slab;
49e22585 2231 stat(s, ALLOC_FROM_PARTIAL);
49e22585 2232 object = t;
49e22585 2233 } else {
bb192ed9 2234 put_cpu_partial(s, slab, 0);
8028dcea 2235 stat(s, CPU_PARTIAL_NODE);
bb192ed9 2236 partial_slabs++;
49e22585 2237 }
b47291ef 2238#ifdef CONFIG_SLUB_CPU_PARTIAL
345c905d 2239 if (!kmem_cache_has_cpu_partial(s)
bb192ed9 2240 || partial_slabs > s->cpu_partial_slabs / 2)
49e22585 2241 break;
b47291ef
VB
2242#else
2243 break;
2244#endif
49e22585 2245
497b66f2 2246 }
4b1f449d 2247 spin_unlock_irqrestore(&n->list_lock, flags);
497b66f2 2248 return object;
81819f0f
CL
2249}
2250
2251/*
c2092c12 2252 * Get a slab from somewhere. Search in increasing NUMA distances.
81819f0f 2253 */
6edf2576 2254static void *get_any_partial(struct kmem_cache *s, struct partial_context *pc)
81819f0f
CL
2255{
2256#ifdef CONFIG_NUMA
2257 struct zonelist *zonelist;
dd1a239f 2258 struct zoneref *z;
54a6eb5c 2259 struct zone *zone;
6edf2576 2260 enum zone_type highest_zoneidx = gfp_zone(pc->flags);
497b66f2 2261 void *object;
cc9a6c87 2262 unsigned int cpuset_mems_cookie;
81819f0f
CL
2263
2264 /*
672bba3a
CL
2265 * The defrag ratio allows a configuration of the tradeoffs between
2266 * inter node defragmentation and node local allocations. A lower
2267 * defrag_ratio increases the tendency to do local allocations
2268 * instead of attempting to obtain partial slabs from other nodes.
81819f0f 2269 *
672bba3a
CL
2270 * If the defrag_ratio is set to 0 then kmalloc() always
2271 * returns node local objects. If the ratio is higher then kmalloc()
2272 * may return off node objects because partial slabs are obtained
2273 * from other nodes and filled up.
81819f0f 2274 *
43efd3ea
LP
2275 * If /sys/kernel/slab/xx/remote_node_defrag_ratio is set to 100
2276 * (which makes defrag_ratio = 1000) then every (well almost)
2277 * allocation will first attempt to defrag slab caches on other nodes.
2278 * This means scanning over all nodes to look for partial slabs which
2279 * may be expensive if we do it every time we are trying to find a slab
672bba3a 2280 * with available objects.
81819f0f 2281 */
9824601e
CL
2282 if (!s->remote_node_defrag_ratio ||
2283 get_cycles() % 1024 > s->remote_node_defrag_ratio)
81819f0f
CL
2284 return NULL;
2285
cc9a6c87 2286 do {
d26914d1 2287 cpuset_mems_cookie = read_mems_allowed_begin();
6edf2576 2288 zonelist = node_zonelist(mempolicy_slab_node(), pc->flags);
97a225e6 2289 for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) {
cc9a6c87
MG
2290 struct kmem_cache_node *n;
2291
2292 n = get_node(s, zone_to_nid(zone));
2293
6edf2576 2294 if (n && cpuset_zone_allowed(zone, pc->flags) &&
cc9a6c87 2295 n->nr_partial > s->min_partial) {
6edf2576 2296 object = get_partial_node(s, n, pc);
cc9a6c87
MG
2297 if (object) {
2298 /*
d26914d1
MG
2299 * Don't check read_mems_allowed_retry()
2300 * here - if mems_allowed was updated in
2301 * parallel, that was a harmless race
2302 * between allocation and the cpuset
2303 * update
cc9a6c87 2304 */
cc9a6c87
MG
2305 return object;
2306 }
c0ff7453 2307 }
81819f0f 2308 }
d26914d1 2309 } while (read_mems_allowed_retry(cpuset_mems_cookie));
6dfd1b65 2310#endif /* CONFIG_NUMA */
81819f0f
CL
2311 return NULL;
2312}
2313
2314/*
c2092c12 2315 * Get a partial slab, lock it and return it.
81819f0f 2316 */
6edf2576 2317static void *get_partial(struct kmem_cache *s, int node, struct partial_context *pc)
81819f0f 2318{
497b66f2 2319 void *object;
a561ce00
JK
2320 int searchnode = node;
2321
2322 if (node == NUMA_NO_NODE)
2323 searchnode = numa_mem_id();
81819f0f 2324
6edf2576 2325 object = get_partial_node(s, get_node(s, searchnode), pc);
497b66f2
CL
2326 if (object || node != NUMA_NO_NODE)
2327 return object;
81819f0f 2328
6edf2576 2329 return get_any_partial(s, pc);
81819f0f
CL
2330}
2331
923717cb 2332#ifdef CONFIG_PREEMPTION
8a5ec0ba 2333/*
0d645ed1 2334 * Calculate the next globally unique transaction for disambiguation
8a5ec0ba
CL
2335 * during cmpxchg. The transactions start with the cpu number and are then
2336 * incremented by CONFIG_NR_CPUS.
2337 */
2338#define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
2339#else
2340/*
2341 * No preemption supported therefore also no need to check for
2342 * different cpus.
2343 */
2344#define TID_STEP 1
2345#endif
2346
2347static inline unsigned long next_tid(unsigned long tid)
2348{
2349 return tid + TID_STEP;
2350}
2351
9d5f0be0 2352#ifdef SLUB_DEBUG_CMPXCHG
8a5ec0ba
CL
2353static inline unsigned int tid_to_cpu(unsigned long tid)
2354{
2355 return tid % TID_STEP;
2356}
2357
2358static inline unsigned long tid_to_event(unsigned long tid)
2359{
2360 return tid / TID_STEP;
2361}
9d5f0be0 2362#endif
8a5ec0ba
CL
2363
2364static inline unsigned int init_tid(int cpu)
2365{
2366 return cpu;
2367}
2368
2369static inline void note_cmpxchg_failure(const char *n,
2370 const struct kmem_cache *s, unsigned long tid)
2371{
2372#ifdef SLUB_DEBUG_CMPXCHG
2373 unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
2374
f9f58285 2375 pr_info("%s %s: cmpxchg redo ", n, s->name);
8a5ec0ba 2376
923717cb 2377#ifdef CONFIG_PREEMPTION
8a5ec0ba 2378 if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
f9f58285 2379 pr_warn("due to cpu change %d -> %d\n",
8a5ec0ba
CL
2380 tid_to_cpu(tid), tid_to_cpu(actual_tid));
2381 else
2382#endif
2383 if (tid_to_event(tid) != tid_to_event(actual_tid))
f9f58285 2384 pr_warn("due to cpu running other code. Event %ld->%ld\n",
8a5ec0ba
CL
2385 tid_to_event(tid), tid_to_event(actual_tid));
2386 else
f9f58285 2387 pr_warn("for unknown reason: actual=%lx was=%lx target=%lx\n",
8a5ec0ba
CL
2388 actual_tid, tid, next_tid(tid));
2389#endif
4fdccdfb 2390 stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
8a5ec0ba
CL
2391}
2392
788e1aad 2393static void init_kmem_cache_cpus(struct kmem_cache *s)
8a5ec0ba 2394{
8a5ec0ba 2395 int cpu;
bd0e7491 2396 struct kmem_cache_cpu *c;
8a5ec0ba 2397
bd0e7491
VB
2398 for_each_possible_cpu(cpu) {
2399 c = per_cpu_ptr(s->cpu_slab, cpu);
2400 local_lock_init(&c->lock);
2401 c->tid = init_tid(cpu);
2402 }
8a5ec0ba 2403}
2cfb7455 2404
81819f0f 2405/*
c2092c12 2406 * Finishes removing the cpu slab. Merges cpu's freelist with slab's freelist,
a019d201
VB
2407 * unfreezes the slabs and puts it on the proper list.
2408 * Assumes the slab has been already safely taken away from kmem_cache_cpu
2409 * by the caller.
81819f0f 2410 */
bb192ed9 2411static void deactivate_slab(struct kmem_cache *s, struct slab *slab,
a019d201 2412 void *freelist)
81819f0f 2413{
6d3a16d0 2414 enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE, M_FULL_NOLIST };
bb192ed9 2415 struct kmem_cache_node *n = get_node(s, slab_nid(slab));
6d3a16d0
HY
2416 int free_delta = 0;
2417 enum slab_modes mode = M_NONE;
d930ff03 2418 void *nextfree, *freelist_iter, *freelist_tail;
136333d1 2419 int tail = DEACTIVATE_TO_HEAD;
3406e91b 2420 unsigned long flags = 0;
bb192ed9
VB
2421 struct slab new;
2422 struct slab old;
2cfb7455 2423
bb192ed9 2424 if (slab->freelist) {
84e554e6 2425 stat(s, DEACTIVATE_REMOTE_FREES);
136333d1 2426 tail = DEACTIVATE_TO_TAIL;
2cfb7455
CL
2427 }
2428
894b8788 2429 /*
d930ff03
VB
2430 * Stage one: Count the objects on cpu's freelist as free_delta and
2431 * remember the last object in freelist_tail for later splicing.
2cfb7455 2432 */
d930ff03
VB
2433 freelist_tail = NULL;
2434 freelist_iter = freelist;
2435 while (freelist_iter) {
2436 nextfree = get_freepointer(s, freelist_iter);
2cfb7455 2437
52f23478
DZ
2438 /*
2439 * If 'nextfree' is invalid, it is possible that the object at
d930ff03
VB
2440 * 'freelist_iter' is already corrupted. So isolate all objects
2441 * starting at 'freelist_iter' by skipping them.
52f23478 2442 */
bb192ed9 2443 if (freelist_corrupted(s, slab, &freelist_iter, nextfree))
52f23478
DZ
2444 break;
2445
d930ff03
VB
2446 freelist_tail = freelist_iter;
2447 free_delta++;
2cfb7455 2448
d930ff03 2449 freelist_iter = nextfree;
2cfb7455
CL
2450 }
2451
894b8788 2452 /*
c2092c12
VB
2453 * Stage two: Unfreeze the slab while splicing the per-cpu
2454 * freelist to the head of slab's freelist.
d930ff03 2455 *
c2092c12 2456 * Ensure that the slab is unfrozen while the list presence
d930ff03 2457 * reflects the actual number of objects during unfreeze.
2cfb7455 2458 *
6d3a16d0
HY
2459 * We first perform cmpxchg holding lock and insert to list
2460 * when it succeed. If there is mismatch then the slab is not
2461 * unfrozen and number of objects in the slab may have changed.
2462 * Then release lock and retry cmpxchg again.
894b8788 2463 */
2cfb7455 2464redo:
894b8788 2465
bb192ed9
VB
2466 old.freelist = READ_ONCE(slab->freelist);
2467 old.counters = READ_ONCE(slab->counters);
a0132ac0 2468 VM_BUG_ON(!old.frozen);
7c2e132c 2469
2cfb7455
CL
2470 /* Determine target state of the slab */
2471 new.counters = old.counters;
d930ff03
VB
2472 if (freelist_tail) {
2473 new.inuse -= free_delta;
2474 set_freepointer(s, freelist_tail, old.freelist);
2cfb7455
CL
2475 new.freelist = freelist;
2476 } else
2477 new.freelist = old.freelist;
2478
2479 new.frozen = 0;
2480
6d3a16d0
HY
2481 if (!new.inuse && n->nr_partial >= s->min_partial) {
2482 mode = M_FREE;
2483 } else if (new.freelist) {
2484 mode = M_PARTIAL;
2485 /*
2486 * Taking the spinlock removes the possibility that
2487 * acquire_slab() will see a slab that is frozen
2488 */
2489 spin_lock_irqsave(&n->list_lock, flags);
2490 } else if (kmem_cache_debug_flags(s, SLAB_STORE_USER)) {
2491 mode = M_FULL;
2492 /*
2493 * This also ensures that the scanning of full
2494 * slabs from diagnostic functions will not see
2495 * any frozen slabs.
2496 */
2497 spin_lock_irqsave(&n->list_lock, flags);
2cfb7455 2498 } else {
6d3a16d0 2499 mode = M_FULL_NOLIST;
2cfb7455
CL
2500 }
2501
2cfb7455 2502
bb192ed9 2503 if (!cmpxchg_double_slab(s, slab,
2cfb7455
CL
2504 old.freelist, old.counters,
2505 new.freelist, new.counters,
6d3a16d0
HY
2506 "unfreezing slab")) {
2507 if (mode == M_PARTIAL || mode == M_FULL)
2508 spin_unlock_irqrestore(&n->list_lock, flags);
2cfb7455 2509 goto redo;
6d3a16d0 2510 }
2cfb7455 2511
2cfb7455 2512
6d3a16d0
HY
2513 if (mode == M_PARTIAL) {
2514 add_partial(n, slab, tail);
2515 spin_unlock_irqrestore(&n->list_lock, flags);
88349a28 2516 stat(s, tail);
6d3a16d0 2517 } else if (mode == M_FREE) {
2cfb7455 2518 stat(s, DEACTIVATE_EMPTY);
bb192ed9 2519 discard_slab(s, slab);
2cfb7455 2520 stat(s, FREE_SLAB);
6d3a16d0
HY
2521 } else if (mode == M_FULL) {
2522 add_full(s, n, slab);
2523 spin_unlock_irqrestore(&n->list_lock, flags);
2524 stat(s, DEACTIVATE_FULL);
2525 } else if (mode == M_FULL_NOLIST) {
2526 stat(s, DEACTIVATE_FULL);
894b8788 2527 }
81819f0f
CL
2528}
2529
345c905d 2530#ifdef CONFIG_SLUB_CPU_PARTIAL
bb192ed9 2531static void __unfreeze_partials(struct kmem_cache *s, struct slab *partial_slab)
fc1455f4 2532{
43d77867 2533 struct kmem_cache_node *n = NULL, *n2 = NULL;
bb192ed9 2534 struct slab *slab, *slab_to_discard = NULL;
7cf9f3ba 2535 unsigned long flags = 0;
49e22585 2536
bb192ed9
VB
2537 while (partial_slab) {
2538 struct slab new;
2539 struct slab old;
49e22585 2540
bb192ed9
VB
2541 slab = partial_slab;
2542 partial_slab = slab->next;
43d77867 2543
bb192ed9 2544 n2 = get_node(s, slab_nid(slab));
43d77867
JK
2545 if (n != n2) {
2546 if (n)
7cf9f3ba 2547 spin_unlock_irqrestore(&n->list_lock, flags);
43d77867
JK
2548
2549 n = n2;
7cf9f3ba 2550 spin_lock_irqsave(&n->list_lock, flags);
43d77867 2551 }
49e22585
CL
2552
2553 do {
2554
bb192ed9
VB
2555 old.freelist = slab->freelist;
2556 old.counters = slab->counters;
a0132ac0 2557 VM_BUG_ON(!old.frozen);
49e22585
CL
2558
2559 new.counters = old.counters;
2560 new.freelist = old.freelist;
2561
2562 new.frozen = 0;
2563
bb192ed9 2564 } while (!__cmpxchg_double_slab(s, slab,
49e22585
CL
2565 old.freelist, old.counters,
2566 new.freelist, new.counters,
2567 "unfreezing slab"));
2568
8a5b20ae 2569 if (unlikely(!new.inuse && n->nr_partial >= s->min_partial)) {
bb192ed9
VB
2570 slab->next = slab_to_discard;
2571 slab_to_discard = slab;
43d77867 2572 } else {
bb192ed9 2573 add_partial(n, slab, DEACTIVATE_TO_TAIL);
43d77867 2574 stat(s, FREE_ADD_PARTIAL);
49e22585
CL
2575 }
2576 }
2577
2578 if (n)
7cf9f3ba 2579 spin_unlock_irqrestore(&n->list_lock, flags);
8de06a6f 2580
bb192ed9
VB
2581 while (slab_to_discard) {
2582 slab = slab_to_discard;
2583 slab_to_discard = slab_to_discard->next;
9ada1934
SL
2584
2585 stat(s, DEACTIVATE_EMPTY);
bb192ed9 2586 discard_slab(s, slab);
9ada1934
SL
2587 stat(s, FREE_SLAB);
2588 }
fc1455f4 2589}
f3ab8b6b 2590
fc1455f4
VB
2591/*
2592 * Unfreeze all the cpu partial slabs.
2593 */
2594static void unfreeze_partials(struct kmem_cache *s)
2595{
bb192ed9 2596 struct slab *partial_slab;
fc1455f4
VB
2597 unsigned long flags;
2598
bd0e7491 2599 local_lock_irqsave(&s->cpu_slab->lock, flags);
bb192ed9 2600 partial_slab = this_cpu_read(s->cpu_slab->partial);
fc1455f4 2601 this_cpu_write(s->cpu_slab->partial, NULL);
bd0e7491 2602 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
fc1455f4 2603
bb192ed9
VB
2604 if (partial_slab)
2605 __unfreeze_partials(s, partial_slab);
fc1455f4
VB
2606}
2607
2608static void unfreeze_partials_cpu(struct kmem_cache *s,
2609 struct kmem_cache_cpu *c)
2610{
bb192ed9 2611 struct slab *partial_slab;
fc1455f4 2612
bb192ed9 2613 partial_slab = slub_percpu_partial(c);
fc1455f4
VB
2614 c->partial = NULL;
2615
bb192ed9
VB
2616 if (partial_slab)
2617 __unfreeze_partials(s, partial_slab);
49e22585
CL
2618}
2619
2620/*
c2092c12
VB
2621 * Put a slab that was just frozen (in __slab_free|get_partial_node) into a
2622 * partial slab slot if available.
49e22585
CL
2623 *
2624 * If we did not find a slot then simply move all the partials to the
2625 * per node partial list.
2626 */
bb192ed9 2627static void put_cpu_partial(struct kmem_cache *s, struct slab *slab, int drain)
49e22585 2628{
bb192ed9
VB
2629 struct slab *oldslab;
2630 struct slab *slab_to_unfreeze = NULL;
e0a043aa 2631 unsigned long flags;
bb192ed9 2632 int slabs = 0;
49e22585 2633
bd0e7491 2634 local_lock_irqsave(&s->cpu_slab->lock, flags);
49e22585 2635
bb192ed9 2636 oldslab = this_cpu_read(s->cpu_slab->partial);
e0a043aa 2637
bb192ed9
VB
2638 if (oldslab) {
2639 if (drain && oldslab->slabs >= s->cpu_partial_slabs) {
e0a043aa
VB
2640 /*
2641 * Partial array is full. Move the existing set to the
2642 * per node partial list. Postpone the actual unfreezing
2643 * outside of the critical section.
2644 */
bb192ed9
VB
2645 slab_to_unfreeze = oldslab;
2646 oldslab = NULL;
e0a043aa 2647 } else {
bb192ed9 2648 slabs = oldslab->slabs;
49e22585 2649 }
e0a043aa 2650 }
49e22585 2651
bb192ed9 2652 slabs++;
49e22585 2653
bb192ed9
VB
2654 slab->slabs = slabs;
2655 slab->next = oldslab;
49e22585 2656
bb192ed9 2657 this_cpu_write(s->cpu_slab->partial, slab);
e0a043aa 2658
bd0e7491 2659 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
e0a043aa 2660
bb192ed9
VB
2661 if (slab_to_unfreeze) {
2662 __unfreeze_partials(s, slab_to_unfreeze);
e0a043aa
VB
2663 stat(s, CPU_PARTIAL_DRAIN);
2664 }
49e22585
CL
2665}
2666
e0a043aa
VB
2667#else /* CONFIG_SLUB_CPU_PARTIAL */
2668
2669static inline void unfreeze_partials(struct kmem_cache *s) { }
2670static inline void unfreeze_partials_cpu(struct kmem_cache *s,
2671 struct kmem_cache_cpu *c) { }
2672
2673#endif /* CONFIG_SLUB_CPU_PARTIAL */
2674
dfb4f096 2675static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
81819f0f 2676{
5a836bf6 2677 unsigned long flags;
bb192ed9 2678 struct slab *slab;
5a836bf6
SAS
2679 void *freelist;
2680
bd0e7491 2681 local_lock_irqsave(&s->cpu_slab->lock, flags);
5a836bf6 2682
bb192ed9 2683 slab = c->slab;
5a836bf6 2684 freelist = c->freelist;
c17dda40 2685
bb192ed9 2686 c->slab = NULL;
a019d201 2687 c->freelist = NULL;
c17dda40 2688 c->tid = next_tid(c->tid);
a019d201 2689
bd0e7491 2690 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
a019d201 2691
bb192ed9
VB
2692 if (slab) {
2693 deactivate_slab(s, slab, freelist);
5a836bf6
SAS
2694 stat(s, CPUSLAB_FLUSH);
2695 }
81819f0f
CL
2696}
2697
0c710013 2698static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
81819f0f 2699{
9dfc6e68 2700 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
08beb547 2701 void *freelist = c->freelist;
bb192ed9 2702 struct slab *slab = c->slab;
81819f0f 2703
bb192ed9 2704 c->slab = NULL;
08beb547
VB
2705 c->freelist = NULL;
2706 c->tid = next_tid(c->tid);
2707
bb192ed9
VB
2708 if (slab) {
2709 deactivate_slab(s, slab, freelist);
08beb547
VB
2710 stat(s, CPUSLAB_FLUSH);
2711 }
49e22585 2712
fc1455f4 2713 unfreeze_partials_cpu(s, c);
81819f0f
CL
2714}
2715
5a836bf6
SAS
2716struct slub_flush_work {
2717 struct work_struct work;
2718 struct kmem_cache *s;
2719 bool skip;
2720};
2721
fc1455f4
VB
2722/*
2723 * Flush cpu slab.
2724 *
5a836bf6 2725 * Called from CPU work handler with migration disabled.
fc1455f4 2726 */
5a836bf6 2727static void flush_cpu_slab(struct work_struct *w)
81819f0f 2728{
5a836bf6
SAS
2729 struct kmem_cache *s;
2730 struct kmem_cache_cpu *c;
2731 struct slub_flush_work *sfw;
2732
2733 sfw = container_of(w, struct slub_flush_work, work);
2734
2735 s = sfw->s;
2736 c = this_cpu_ptr(s->cpu_slab);
fc1455f4 2737
bb192ed9 2738 if (c->slab)
fc1455f4 2739 flush_slab(s, c);
81819f0f 2740
fc1455f4 2741 unfreeze_partials(s);
81819f0f
CL
2742}
2743
5a836bf6 2744static bool has_cpu_slab(int cpu, struct kmem_cache *s)
a8364d55 2745{
a8364d55
GBY
2746 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
2747
bb192ed9 2748 return c->slab || slub_percpu_partial(c);
a8364d55
GBY
2749}
2750
5a836bf6
SAS
2751static DEFINE_MUTEX(flush_lock);
2752static DEFINE_PER_CPU(struct slub_flush_work, slub_flush);
2753
2754static void flush_all_cpus_locked(struct kmem_cache *s)
2755{
2756 struct slub_flush_work *sfw;
2757 unsigned int cpu;
2758
2759 lockdep_assert_cpus_held();
2760 mutex_lock(&flush_lock);
2761
2762 for_each_online_cpu(cpu) {
2763 sfw = &per_cpu(slub_flush, cpu);
2764 if (!has_cpu_slab(cpu, s)) {
2765 sfw->skip = true;
2766 continue;
2767 }
2768 INIT_WORK(&sfw->work, flush_cpu_slab);
2769 sfw->skip = false;
2770 sfw->s = s;
e45cc288 2771 queue_work_on(cpu, flushwq, &sfw->work);
5a836bf6
SAS
2772 }
2773
2774 for_each_online_cpu(cpu) {
2775 sfw = &per_cpu(slub_flush, cpu);
2776 if (sfw->skip)
2777 continue;
2778 flush_work(&sfw->work);
2779 }
2780
2781 mutex_unlock(&flush_lock);
2782}
2783
81819f0f
CL
2784static void flush_all(struct kmem_cache *s)
2785{
5a836bf6
SAS
2786 cpus_read_lock();
2787 flush_all_cpus_locked(s);
2788 cpus_read_unlock();
81819f0f
CL
2789}
2790
a96a87bf
SAS
2791/*
2792 * Use the cpu notifier to insure that the cpu slabs are flushed when
2793 * necessary.
2794 */
2795static int slub_cpu_dead(unsigned int cpu)
2796{
2797 struct kmem_cache *s;
a96a87bf
SAS
2798
2799 mutex_lock(&slab_mutex);
0e7ac738 2800 list_for_each_entry(s, &slab_caches, list)
a96a87bf 2801 __flush_cpu_slab(s, cpu);
a96a87bf
SAS
2802 mutex_unlock(&slab_mutex);
2803 return 0;
2804}
2805
dfb4f096
CL
2806/*
2807 * Check if the objects in a per cpu structure fit numa
2808 * locality expectations.
2809 */
bb192ed9 2810static inline int node_match(struct slab *slab, int node)
dfb4f096
CL
2811{
2812#ifdef CONFIG_NUMA
bb192ed9 2813 if (node != NUMA_NO_NODE && slab_nid(slab) != node)
dfb4f096
CL
2814 return 0;
2815#endif
2816 return 1;
2817}
2818
9a02d699 2819#ifdef CONFIG_SLUB_DEBUG
bb192ed9 2820static int count_free(struct slab *slab)
781b2ba6 2821{
bb192ed9 2822 return slab->objects - slab->inuse;
781b2ba6
PE
2823}
2824
9a02d699
DR
2825static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
2826{
2827 return atomic_long_read(&n->total_objects);
2828}
a579b056
VB
2829
2830/* Supports checking bulk free of a constructed freelist */
c7323a5a 2831static noinline void free_debug_processing(
a579b056
VB
2832 struct kmem_cache *s, struct slab *slab,
2833 void *head, void *tail, int bulk_cnt,
2834 unsigned long addr)
2835{
2836 struct kmem_cache_node *n = get_node(s, slab_nid(slab));
c7323a5a 2837 struct slab *slab_free = NULL;
a579b056
VB
2838 void *object = head;
2839 int cnt = 0;
c7323a5a
VB
2840 unsigned long flags;
2841 bool checks_ok = false;
a579b056
VB
2842 depot_stack_handle_t handle = 0;
2843
2844 if (s->flags & SLAB_STORE_USER)
2845 handle = set_track_prepare();
2846
2847 spin_lock_irqsave(&n->list_lock, flags);
a579b056
VB
2848
2849 if (s->flags & SLAB_CONSISTENCY_CHECKS) {
2850 if (!check_slab(s, slab))
2851 goto out;
2852 }
2853
c7323a5a
VB
2854 if (slab->inuse < bulk_cnt) {
2855 slab_err(s, slab, "Slab has %d allocated objects but %d are to be freed\n",
2856 slab->inuse, bulk_cnt);
2857 goto out;
2858 }
2859
a579b056 2860next_object:
c7323a5a
VB
2861
2862 if (++cnt > bulk_cnt)
2863 goto out_cnt;
a579b056
VB
2864
2865 if (s->flags & SLAB_CONSISTENCY_CHECKS) {
2866 if (!free_consistency_checks(s, slab, object, addr))
2867 goto out;
2868 }
2869
2870 if (s->flags & SLAB_STORE_USER)
2871 set_track_update(s, object, TRACK_FREE, addr, handle);
2872 trace(s, slab, object, 0);
2873 /* Freepointer not overwritten by init_object(), SLAB_POISON moved it */
2874 init_object(s, object, SLUB_RED_INACTIVE);
2875
2876 /* Reached end of constructed freelist yet? */
2877 if (object != tail) {
2878 object = get_freepointer(s, object);
2879 goto next_object;
2880 }
c7323a5a 2881 checks_ok = true;
a579b056 2882
c7323a5a 2883out_cnt:
a579b056 2884 if (cnt != bulk_cnt)
c7323a5a 2885 slab_err(s, slab, "Bulk free expected %d objects but found %d\n",
a579b056
VB
2886 bulk_cnt, cnt);
2887
c7323a5a
VB
2888out:
2889 if (checks_ok) {
2890 void *prior = slab->freelist;
2891
2892 /* Perform the actual freeing while we still hold the locks */
2893 slab->inuse -= cnt;
2894 set_freepointer(s, tail, prior);
2895 slab->freelist = head;
2896
b731e357
FT
2897 /*
2898 * If the slab is empty, and node's partial list is full,
2899 * it should be discarded anyway no matter it's on full or
2900 * partial list.
2901 */
2902 if (slab->inuse == 0 && n->nr_partial >= s->min_partial)
2903 slab_free = slab;
2904
c7323a5a 2905 if (!prior) {
b731e357 2906 /* was on full list */
c7323a5a 2907 remove_full(s, n, slab);
b731e357
FT
2908 if (!slab_free) {
2909 add_partial(n, slab, DEACTIVATE_TO_TAIL);
2910 stat(s, FREE_ADD_PARTIAL);
2911 }
2912 } else if (slab_free) {
c7323a5a
VB
2913 remove_partial(n, slab);
2914 stat(s, FREE_REMOVE_PARTIAL);
2915 }
c7323a5a
VB
2916 }
2917
2918 if (slab_free) {
2919 /*
2920 * Update the counters while still holding n->list_lock to
2921 * prevent spurious validation warnings
2922 */
2923 dec_slabs_node(s, slab_nid(slab_free), slab_free->objects);
2924 }
2925
a579b056 2926 spin_unlock_irqrestore(&n->list_lock, flags);
c7323a5a
VB
2927
2928 if (!checks_ok)
a579b056 2929 slab_fix(s, "Object at 0x%p not freed", object);
c7323a5a
VB
2930
2931 if (slab_free) {
2932 stat(s, FREE_SLAB);
2933 free_slab(s, slab_free);
2934 }
a579b056 2935}
9a02d699
DR
2936#endif /* CONFIG_SLUB_DEBUG */
2937
2938#if defined(CONFIG_SLUB_DEBUG) || defined(CONFIG_SYSFS)
781b2ba6 2939static unsigned long count_partial(struct kmem_cache_node *n,
bb192ed9 2940 int (*get_count)(struct slab *))
781b2ba6
PE
2941{
2942 unsigned long flags;
2943 unsigned long x = 0;
bb192ed9 2944 struct slab *slab;
781b2ba6
PE
2945
2946 spin_lock_irqsave(&n->list_lock, flags);
bb192ed9
VB
2947 list_for_each_entry(slab, &n->partial, slab_list)
2948 x += get_count(slab);
781b2ba6
PE
2949 spin_unlock_irqrestore(&n->list_lock, flags);
2950 return x;
2951}
9a02d699 2952#endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
26c02cf0 2953
781b2ba6
PE
2954static noinline void
2955slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
2956{
9a02d699
DR
2957#ifdef CONFIG_SLUB_DEBUG
2958 static DEFINE_RATELIMIT_STATE(slub_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
2959 DEFAULT_RATELIMIT_BURST);
781b2ba6 2960 int node;
fa45dc25 2961 struct kmem_cache_node *n;
781b2ba6 2962
9a02d699
DR
2963 if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slub_oom_rs))
2964 return;
2965
5b3810e5
VB
2966 pr_warn("SLUB: Unable to allocate memory on node %d, gfp=%#x(%pGg)\n",
2967 nid, gfpflags, &gfpflags);
19af27af 2968 pr_warn(" cache: %s, object size: %u, buffer size: %u, default order: %u, min order: %u\n",
f9f58285
FF
2969 s->name, s->object_size, s->size, oo_order(s->oo),
2970 oo_order(s->min));
781b2ba6 2971
3b0efdfa 2972 if (oo_order(s->min) > get_order(s->object_size))
f9f58285
FF
2973 pr_warn(" %s debugging increased min order, use slub_debug=O to disable.\n",
2974 s->name);
fa5ec8a1 2975
fa45dc25 2976 for_each_kmem_cache_node(s, node, n) {
781b2ba6
PE
2977 unsigned long nr_slabs;
2978 unsigned long nr_objs;
2979 unsigned long nr_free;
2980
26c02cf0
AB
2981 nr_free = count_partial(n, count_free);
2982 nr_slabs = node_nr_slabs(n);
2983 nr_objs = node_nr_objs(n);
781b2ba6 2984
f9f58285 2985 pr_warn(" node %d: slabs: %ld, objs: %ld, free: %ld\n",
781b2ba6
PE
2986 node, nr_slabs, nr_objs, nr_free);
2987 }
9a02d699 2988#endif
781b2ba6
PE
2989}
2990
01b34d16 2991static inline bool pfmemalloc_match(struct slab *slab, gfp_t gfpflags)
072bb0aa 2992{
01b34d16 2993 if (unlikely(slab_test_pfmemalloc(slab)))
0b303fb4
VB
2994 return gfp_pfmemalloc_allowed(gfpflags);
2995
2996 return true;
2997}
2998
213eeb9f 2999/*
c2092c12
VB
3000 * Check the slab->freelist and either transfer the freelist to the
3001 * per cpu freelist or deactivate the slab.
213eeb9f 3002 *
c2092c12 3003 * The slab is still frozen if the return value is not NULL.
213eeb9f 3004 *
c2092c12 3005 * If this function returns NULL then the slab has been unfrozen.
213eeb9f 3006 */
bb192ed9 3007static inline void *get_freelist(struct kmem_cache *s, struct slab *slab)
213eeb9f 3008{
bb192ed9 3009 struct slab new;
213eeb9f
CL
3010 unsigned long counters;
3011 void *freelist;
3012
bd0e7491
VB
3013 lockdep_assert_held(this_cpu_ptr(&s->cpu_slab->lock));
3014
213eeb9f 3015 do {
bb192ed9
VB
3016 freelist = slab->freelist;
3017 counters = slab->counters;
6faa6833 3018
213eeb9f 3019 new.counters = counters;
a0132ac0 3020 VM_BUG_ON(!new.frozen);
213eeb9f 3021
bb192ed9 3022 new.inuse = slab->objects;
213eeb9f
CL
3023 new.frozen = freelist != NULL;
3024
bb192ed9 3025 } while (!__cmpxchg_double_slab(s, slab,
213eeb9f
CL
3026 freelist, counters,
3027 NULL, new.counters,
3028 "get_freelist"));
3029
3030 return freelist;
3031}
3032
81819f0f 3033/*
894b8788
CL
3034 * Slow path. The lockless freelist is empty or we need to perform
3035 * debugging duties.
3036 *
894b8788
CL
3037 * Processing is still very fast if new objects have been freed to the
3038 * regular freelist. In that case we simply take over the regular freelist
3039 * as the lockless freelist and zap the regular freelist.
81819f0f 3040 *
894b8788
CL
3041 * If that is not working then we fall back to the partial lists. We take the
3042 * first element of the freelist as the object to allocate now and move the
3043 * rest of the freelist to the lockless freelist.
81819f0f 3044 *
894b8788 3045 * And if we were unable to get a new slab from the partial slab lists then
6446faa2
CL
3046 * we need to allocate a new slab. This is the slowest path since it involves
3047 * a call to the page allocator and the setup of a new slab.
a380a3c7 3048 *
e500059b 3049 * Version of __slab_alloc to use when we know that preemption is
a380a3c7 3050 * already disabled (which is the case for bulk allocation).
81819f0f 3051 */
a380a3c7 3052static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
6edf2576 3053 unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
81819f0f 3054{
6faa6833 3055 void *freelist;
bb192ed9 3056 struct slab *slab;
e500059b 3057 unsigned long flags;
6edf2576 3058 struct partial_context pc;
81819f0f 3059
9f986d99
AW
3060 stat(s, ALLOC_SLOWPATH);
3061
c2092c12 3062reread_slab:
0b303fb4 3063
bb192ed9
VB
3064 slab = READ_ONCE(c->slab);
3065 if (!slab) {
0715e6c5
VB
3066 /*
3067 * if the node is not online or has no normal memory, just
3068 * ignore the node constraint
3069 */
3070 if (unlikely(node != NUMA_NO_NODE &&
7e1fa93d 3071 !node_isset(node, slab_nodes)))
0715e6c5 3072 node = NUMA_NO_NODE;
81819f0f 3073 goto new_slab;
0715e6c5 3074 }
49e22585 3075redo:
6faa6833 3076
bb192ed9 3077 if (unlikely(!node_match(slab, node))) {
0715e6c5
VB
3078 /*
3079 * same as above but node_match() being false already
3080 * implies node != NUMA_NO_NODE
3081 */
7e1fa93d 3082 if (!node_isset(node, slab_nodes)) {
0715e6c5 3083 node = NUMA_NO_NODE;
0715e6c5 3084 } else {
a561ce00 3085 stat(s, ALLOC_NODE_MISMATCH);
0b303fb4 3086 goto deactivate_slab;
a561ce00 3087 }
fc59c053 3088 }
6446faa2 3089
072bb0aa
MG
3090 /*
3091 * By rights, we should be searching for a slab page that was
3092 * PFMEMALLOC but right now, we are losing the pfmemalloc
3093 * information when the page leaves the per-cpu allocator
3094 */
bb192ed9 3095 if (unlikely(!pfmemalloc_match(slab, gfpflags)))
0b303fb4 3096 goto deactivate_slab;
072bb0aa 3097
c2092c12 3098 /* must check again c->slab in case we got preempted and it changed */
bd0e7491 3099 local_lock_irqsave(&s->cpu_slab->lock, flags);
bb192ed9 3100 if (unlikely(slab != c->slab)) {
bd0e7491 3101 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
c2092c12 3102 goto reread_slab;
0b303fb4 3103 }
6faa6833
CL
3104 freelist = c->freelist;
3105 if (freelist)
73736e03 3106 goto load_freelist;
03e404af 3107
bb192ed9 3108 freelist = get_freelist(s, slab);
6446faa2 3109
6faa6833 3110 if (!freelist) {
bb192ed9 3111 c->slab = NULL;
eeaa345e 3112 c->tid = next_tid(c->tid);
bd0e7491 3113 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
03e404af 3114 stat(s, DEACTIVATE_BYPASS);
fc59c053 3115 goto new_slab;
03e404af 3116 }
6446faa2 3117
84e554e6 3118 stat(s, ALLOC_REFILL);
6446faa2 3119
894b8788 3120load_freelist:
0b303fb4 3121
bd0e7491 3122 lockdep_assert_held(this_cpu_ptr(&s->cpu_slab->lock));
0b303fb4 3123
507effea
CL
3124 /*
3125 * freelist is pointing to the list of objects to be used.
c2092c12
VB
3126 * slab is pointing to the slab from which the objects are obtained.
3127 * That slab must be frozen for per cpu allocations to work.
507effea 3128 */
bb192ed9 3129 VM_BUG_ON(!c->slab->frozen);
6faa6833 3130 c->freelist = get_freepointer(s, freelist);
8a5ec0ba 3131 c->tid = next_tid(c->tid);
bd0e7491 3132 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
6faa6833 3133 return freelist;
81819f0f 3134
0b303fb4
VB
3135deactivate_slab:
3136
bd0e7491 3137 local_lock_irqsave(&s->cpu_slab->lock, flags);
bb192ed9 3138 if (slab != c->slab) {
bd0e7491 3139 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
c2092c12 3140 goto reread_slab;
0b303fb4 3141 }
a019d201 3142 freelist = c->freelist;
bb192ed9 3143 c->slab = NULL;
a019d201 3144 c->freelist = NULL;
eeaa345e 3145 c->tid = next_tid(c->tid);
bd0e7491 3146 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
bb192ed9 3147 deactivate_slab(s, slab, freelist);
0b303fb4 3148
81819f0f 3149new_slab:
2cfb7455 3150
a93cf07b 3151 if (slub_percpu_partial(c)) {
bd0e7491 3152 local_lock_irqsave(&s->cpu_slab->lock, flags);
bb192ed9 3153 if (unlikely(c->slab)) {
bd0e7491 3154 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
c2092c12 3155 goto reread_slab;
fa417ab7 3156 }
4b1f449d 3157 if (unlikely(!slub_percpu_partial(c))) {
bd0e7491 3158 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
25c00c50
VB
3159 /* we were preempted and partial list got empty */
3160 goto new_objects;
4b1f449d 3161 }
fa417ab7 3162
bb192ed9
VB
3163 slab = c->slab = slub_percpu_partial(c);
3164 slub_set_percpu_partial(c, slab);
bd0e7491 3165 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
49e22585 3166 stat(s, CPU_PARTIAL_ALLOC);
49e22585 3167 goto redo;
81819f0f
CL
3168 }
3169
fa417ab7
VB
3170new_objects:
3171
6edf2576
FT
3172 pc.flags = gfpflags;
3173 pc.slab = &slab;
3174 pc.orig_size = orig_size;
3175 freelist = get_partial(s, node, &pc);
3f2b77e3 3176 if (freelist)
c2092c12 3177 goto check_new_slab;
2a904905 3178
25c00c50 3179 slub_put_cpu_ptr(s->cpu_slab);
bb192ed9 3180 slab = new_slab(s, gfpflags, node);
25c00c50 3181 c = slub_get_cpu_ptr(s->cpu_slab);
01ad8a7b 3182
bb192ed9 3183 if (unlikely(!slab)) {
9a02d699 3184 slab_out_of_memory(s, gfpflags, node);
f4697436 3185 return NULL;
81819f0f 3186 }
2cfb7455 3187
c7323a5a
VB
3188 stat(s, ALLOC_SLAB);
3189
3190 if (kmem_cache_debug(s)) {
6edf2576 3191 freelist = alloc_single_from_new_slab(s, slab, orig_size);
c7323a5a
VB
3192
3193 if (unlikely(!freelist))
3194 goto new_objects;
3195
3196 if (s->flags & SLAB_STORE_USER)
3197 set_track(s, freelist, TRACK_ALLOC, addr);
3198
3199 return freelist;
3200 }
3201
53a0de06 3202 /*
c2092c12 3203 * No other reference to the slab yet so we can
53a0de06
VB
3204 * muck around with it freely without cmpxchg
3205 */
bb192ed9
VB
3206 freelist = slab->freelist;
3207 slab->freelist = NULL;
c7323a5a
VB
3208 slab->inuse = slab->objects;
3209 slab->frozen = 1;
53a0de06 3210
c7323a5a 3211 inc_slabs_node(s, slab_nid(slab), slab->objects);
53a0de06 3212
c2092c12 3213check_new_slab:
2cfb7455 3214
1572df7c 3215 if (kmem_cache_debug(s)) {
c7323a5a
VB
3216 /*
3217 * For debug caches here we had to go through
3218 * alloc_single_from_partial() so just store the tracking info
3219 * and return the object
3220 */
3221 if (s->flags & SLAB_STORE_USER)
3222 set_track(s, freelist, TRACK_ALLOC, addr);
6edf2576 3223
c7323a5a 3224 return freelist;
1572df7c
VB
3225 }
3226
c7323a5a 3227 if (unlikely(!pfmemalloc_match(slab, gfpflags))) {
1572df7c
VB
3228 /*
3229 * For !pfmemalloc_match() case we don't load freelist so that
3230 * we don't make further mismatched allocations easier.
3231 */
c7323a5a
VB
3232 deactivate_slab(s, slab, get_freepointer(s, freelist));
3233 return freelist;
3234 }
1572df7c 3235
c2092c12 3236retry_load_slab:
cfdf836e 3237
bd0e7491 3238 local_lock_irqsave(&s->cpu_slab->lock, flags);
bb192ed9 3239 if (unlikely(c->slab)) {
cfdf836e 3240 void *flush_freelist = c->freelist;
bb192ed9 3241 struct slab *flush_slab = c->slab;
cfdf836e 3242
bb192ed9 3243 c->slab = NULL;
cfdf836e
VB
3244 c->freelist = NULL;
3245 c->tid = next_tid(c->tid);
3246
bd0e7491 3247 local_unlock_irqrestore(&s->cpu_slab->lock, flags);
cfdf836e 3248
bb192ed9 3249 deactivate_slab(s, flush_slab, flush_freelist);
cfdf836e
VB
3250
3251 stat(s, CPUSLAB_FLUSH);
3252
c2092c12 3253 goto retry_load_slab;
cfdf836e 3254 }
bb192ed9 3255 c->slab = slab;
3f2b77e3 3256
1572df7c 3257 goto load_freelist;
894b8788
CL
3258}
3259
a380a3c7 3260/*
e500059b
VB
3261 * A wrapper for ___slab_alloc() for contexts where preemption is not yet
3262 * disabled. Compensates for possible cpu changes by refetching the per cpu area
3263 * pointer.
a380a3c7
CL
3264 */
3265static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
6edf2576 3266 unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
a380a3c7
CL
3267{
3268 void *p;
a380a3c7 3269
e500059b 3270#ifdef CONFIG_PREEMPT_COUNT
a380a3c7
CL
3271 /*
3272 * We may have been preempted and rescheduled on a different
e500059b 3273 * cpu before disabling preemption. Need to reload cpu area
a380a3c7
CL
3274 * pointer.
3275 */
25c00c50 3276 c = slub_get_cpu_ptr(s->cpu_slab);
a380a3c7
CL
3277#endif
3278
6edf2576 3279 p = ___slab_alloc(s, gfpflags, node, addr, c, orig_size);
e500059b 3280#ifdef CONFIG_PREEMPT_COUNT
25c00c50 3281 slub_put_cpu_ptr(s->cpu_slab);
e500059b 3282#endif
a380a3c7
CL
3283 return p;
3284}
3285
0f181f9f
AP
3286/*
3287 * If the object has been wiped upon free, make sure it's fully initialized by
3288 * zeroing out freelist pointer.
3289 */
3290static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s,
3291 void *obj)
3292{
3293 if (unlikely(slab_want_init_on_free(s)) && obj)
ce5716c6
AK
3294 memset((void *)((char *)kasan_reset_tag(obj) + s->offset),
3295 0, sizeof(void *));
0f181f9f
AP
3296}
3297
894b8788
CL
3298/*
3299 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
3300 * have the fastpath folded into their functions. So no function call
3301 * overhead for requests that can be satisfied on the fastpath.
3302 *
3303 * The fastpath works by first checking if the lockless freelist can be used.
3304 * If not then __slab_alloc is called for slow processing.
3305 *
3306 * Otherwise we can simply pick the next object from the lockless free list.
3307 */
88f2ef73 3308static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_lru *lru,
b89fb5ef 3309 gfp_t gfpflags, int node, unsigned long addr, size_t orig_size)
894b8788 3310{
03ec0ed5 3311 void *object;
dfb4f096 3312 struct kmem_cache_cpu *c;
bb192ed9 3313 struct slab *slab;
8a5ec0ba 3314 unsigned long tid;
964d4bd3 3315 struct obj_cgroup *objcg = NULL;
da844b78 3316 bool init = false;
1f84260c 3317
88f2ef73 3318 s = slab_pre_alloc_hook(s, lru, &objcg, 1, gfpflags);
8135be5a 3319 if (!s)
773ff60e 3320 return NULL;
b89fb5ef
AP
3321
3322 object = kfence_alloc(s, orig_size, gfpflags);
3323 if (unlikely(object))
3324 goto out;
3325
8a5ec0ba 3326redo:
8a5ec0ba
CL
3327 /*
3328 * Must read kmem_cache cpu data via this cpu ptr. Preemption is
3329 * enabled. We may switch back and forth between cpus while
3330 * reading from one cpu area. That does not matter as long
3331 * as we end up on the original cpu again when doing the cmpxchg.
7cccd80b 3332 *
9b4bc85a
VB
3333 * We must guarantee that tid and kmem_cache_cpu are retrieved on the
3334 * same cpu. We read first the kmem_cache_cpu pointer and use it to read
3335 * the tid. If we are preempted and switched to another cpu between the
3336 * two reads, it's OK as the two are still associated with the same cpu
3337 * and cmpxchg later will validate the cpu.
8a5ec0ba 3338 */
9b4bc85a
VB
3339 c = raw_cpu_ptr(s->cpu_slab);
3340 tid = READ_ONCE(c->tid);
9aabf810
JK
3341
3342 /*
3343 * Irqless object alloc/free algorithm used here depends on sequence
3344 * of fetching cpu_slab's data. tid should be fetched before anything
c2092c12 3345 * on c to guarantee that object and slab associated with previous tid
9aabf810 3346 * won't be used with current tid. If we fetch tid first, object and
c2092c12 3347 * slab could be one associated with next tid and our alloc/free
9aabf810
JK
3348 * request will be failed. In this case, we will retry. So, no problem.
3349 */
3350 barrier();
8a5ec0ba 3351
8a5ec0ba
CL
3352 /*
3353 * The transaction ids are globally unique per cpu and per operation on
3354 * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
3355 * occurs on the right processor and that there was no operation on the
3356 * linked list in between.
3357 */
8a5ec0ba 3358
9dfc6e68 3359 object = c->freelist;
bb192ed9 3360 slab = c->slab;
1f04b07d
TG
3361
3362 if (!USE_LOCKLESS_FAST_PATH() ||
bb192ed9 3363 unlikely(!object || !slab || !node_match(slab, node))) {
6edf2576 3364 object = __slab_alloc(s, gfpflags, node, addr, c, orig_size);
8eae1492 3365 } else {
0ad9500e
ED
3366 void *next_object = get_freepointer_safe(s, object);
3367
8a5ec0ba 3368 /*
25985edc 3369 * The cmpxchg will only match if there was no additional
8a5ec0ba
CL
3370 * operation and if we are on the right processor.
3371 *
d0e0ac97
CG
3372 * The cmpxchg does the following atomically (without lock
3373 * semantics!)
8a5ec0ba
CL
3374 * 1. Relocate first pointer to the current per cpu area.
3375 * 2. Verify that tid and freelist have not been changed
3376 * 3. If they were not changed replace tid and freelist
3377 *
d0e0ac97
CG
3378 * Since this is without lock semantics the protection is only
3379 * against code executing on this cpu *not* from access by
3380 * other cpus.
8a5ec0ba 3381 */
933393f5 3382 if (unlikely(!this_cpu_cmpxchg_double(
8a5ec0ba
CL
3383 s->cpu_slab->freelist, s->cpu_slab->tid,
3384 object, tid,
0ad9500e 3385 next_object, next_tid(tid)))) {
8a5ec0ba
CL
3386
3387 note_cmpxchg_failure("slab_alloc", s, tid);
3388 goto redo;
3389 }
0ad9500e 3390 prefetch_freepointer(s, next_object);
84e554e6 3391 stat(s, ALLOC_FASTPATH);
894b8788 3392 }
0f181f9f 3393
ce5716c6 3394 maybe_wipe_obj_freeptr(s, object);
da844b78 3395 init = slab_want_init_on_alloc(gfpflags, s);
d07dbea4 3396
b89fb5ef 3397out:
da844b78 3398 slab_post_alloc_hook(s, objcg, gfpflags, 1, &object, init);
5a896d9e 3399
894b8788 3400 return object;
81819f0f
CL
3401}
3402
88f2ef73 3403static __always_inline void *slab_alloc(struct kmem_cache *s, struct list_lru *lru,
b89fb5ef 3404 gfp_t gfpflags, unsigned long addr, size_t orig_size)
2b847c3c 3405{
88f2ef73 3406 return slab_alloc_node(s, lru, gfpflags, NUMA_NO_NODE, addr, orig_size);
2b847c3c
EG
3407}
3408
88f2ef73
MS
3409static __always_inline
3410void *__kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru,
3411 gfp_t gfpflags)
81819f0f 3412{
88f2ef73 3413 void *ret = slab_alloc(s, lru, gfpflags, _RET_IP_, s->object_size);
5b882be4 3414
2c1d697f 3415 trace_kmem_cache_alloc(_RET_IP_, ret, s, gfpflags, NUMA_NO_NODE);
5b882be4
EGM
3416
3417 return ret;
81819f0f 3418}
88f2ef73
MS
3419
3420void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
3421{
3422 return __kmem_cache_alloc_lru(s, NULL, gfpflags);
3423}
81819f0f
CL
3424EXPORT_SYMBOL(kmem_cache_alloc);
3425
88f2ef73
MS
3426void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru,
3427 gfp_t gfpflags)
3428{
3429 return __kmem_cache_alloc_lru(s, lru, gfpflags);
3430}
3431EXPORT_SYMBOL(kmem_cache_alloc_lru);
3432
ed4cd17e
HY
3433void *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags,
3434 int node, size_t orig_size,
3435 unsigned long caller)
4a92379b 3436{
ed4cd17e
HY
3437 return slab_alloc_node(s, NULL, gfpflags, node,
3438 caller, orig_size);
4a92379b 3439}
5b882be4 3440
81819f0f
CL
3441void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
3442{
88f2ef73 3443 void *ret = slab_alloc_node(s, NULL, gfpflags, node, _RET_IP_, s->object_size);
5b882be4 3444
2c1d697f 3445 trace_kmem_cache_alloc(_RET_IP_, ret, s, gfpflags, node);
5b882be4
EGM
3446
3447 return ret;
81819f0f
CL
3448}
3449EXPORT_SYMBOL(kmem_cache_alloc_node);
81819f0f
CL
3450
3451/*
94e4d712 3452 * Slow path handling. This may still be called frequently since objects
894b8788 3453 * have a longer lifetime than the cpu slabs in most processing loads.
81819f0f 3454 *
894b8788 3455 * So we still attempt to reduce cache line usage. Just take the slab
c2092c12 3456 * lock and free the item. If there is no additional partial slab
894b8788 3457 * handling required then we can return immediately.
81819f0f 3458 */
bb192ed9 3459static void __slab_free(struct kmem_cache *s, struct slab *slab,
81084651
JDB
3460 void *head, void *tail, int cnt,
3461 unsigned long addr)
3462
81819f0f
CL
3463{
3464 void *prior;
2cfb7455 3465 int was_frozen;
bb192ed9 3466 struct slab new;
2cfb7455
CL
3467 unsigned long counters;
3468 struct kmem_cache_node *n = NULL;
3f649ab7 3469 unsigned long flags;
81819f0f 3470
8a5ec0ba 3471 stat(s, FREE_SLOWPATH);
81819f0f 3472
b89fb5ef
AP
3473 if (kfence_free(head))
3474 return;
3475
c7323a5a
VB
3476 if (kmem_cache_debug(s)) {
3477 free_debug_processing(s, slab, head, tail, cnt, addr);
80f08c19 3478 return;
c7323a5a 3479 }
6446faa2 3480
2cfb7455 3481 do {
837d678d
JK
3482 if (unlikely(n)) {
3483 spin_unlock_irqrestore(&n->list_lock, flags);
3484 n = NULL;
3485 }
bb192ed9
VB
3486 prior = slab->freelist;
3487 counters = slab->counters;
81084651 3488 set_freepointer(s, tail, prior);
2cfb7455
CL
3489 new.counters = counters;
3490 was_frozen = new.frozen;
81084651 3491 new.inuse -= cnt;
837d678d 3492 if ((!new.inuse || !prior) && !was_frozen) {
49e22585 3493
c65c1877 3494 if (kmem_cache_has_cpu_partial(s) && !prior) {
49e22585
CL
3495
3496 /*
d0e0ac97
CG
3497 * Slab was on no list before and will be
3498 * partially empty
3499 * We can defer the list move and instead
3500 * freeze it.
49e22585
CL
3501 */
3502 new.frozen = 1;
3503
c65c1877 3504 } else { /* Needs to be taken off a list */
49e22585 3505
bb192ed9 3506 n = get_node(s, slab_nid(slab));
49e22585
CL
3507 /*
3508 * Speculatively acquire the list_lock.
3509 * If the cmpxchg does not succeed then we may
3510 * drop the list_lock without any processing.
3511 *
3512 * Otherwise the list_lock will synchronize with
3513 * other processors updating the list of slabs.
3514 */
3515 spin_lock_irqsave(&n->list_lock, flags);
3516
3517 }
2cfb7455 3518 }
81819f0f 3519
bb192ed9 3520 } while (!cmpxchg_double_slab(s, slab,
2cfb7455 3521 prior, counters,
81084651 3522 head, new.counters,
2cfb7455 3523 "__slab_free"));
81819f0f 3524
2cfb7455 3525 if (likely(!n)) {
49e22585 3526
c270cf30
AW
3527 if (likely(was_frozen)) {
3528 /*
3529 * The list lock was not taken therefore no list
3530 * activity can be necessary.
3531 */
3532 stat(s, FREE_FROZEN);
3533 } else if (new.frozen) {
3534 /*
c2092c12 3535 * If we just froze the slab then put it onto the
c270cf30
AW
3536 * per cpu partial list.
3537 */
bb192ed9 3538 put_cpu_partial(s, slab, 1);
8028dcea
AS
3539 stat(s, CPU_PARTIAL_FREE);
3540 }
c270cf30 3541
b455def2
L
3542 return;
3543 }
81819f0f 3544
8a5b20ae 3545 if (unlikely(!new.inuse && n->nr_partial >= s->min_partial))
837d678d
JK
3546 goto slab_empty;
3547
81819f0f 3548 /*
837d678d
JK
3549 * Objects left in the slab. If it was not on the partial list before
3550 * then add it.
81819f0f 3551 */
345c905d 3552 if (!kmem_cache_has_cpu_partial(s) && unlikely(!prior)) {
bb192ed9
VB
3553 remove_full(s, n, slab);
3554 add_partial(n, slab, DEACTIVATE_TO_TAIL);
837d678d 3555 stat(s, FREE_ADD_PARTIAL);
8ff12cfc 3556 }
80f08c19 3557 spin_unlock_irqrestore(&n->list_lock, flags);
81819f0f
CL
3558 return;
3559
3560slab_empty:
a973e9dd 3561 if (prior) {
81819f0f 3562 /*
6fbabb20 3563 * Slab on the partial list.
81819f0f 3564 */
bb192ed9 3565 remove_partial(n, slab);
84e554e6 3566 stat(s, FREE_REMOVE_PARTIAL);
c65c1877 3567 } else {
6fbabb20 3568 /* Slab must be on the full list */
bb192ed9 3569 remove_full(s, n, slab);
c65c1877 3570 }
2cfb7455 3571
80f08c19 3572 spin_unlock_irqrestore(&n->list_lock, flags);
84e554e6 3573 stat(s, FREE_SLAB);
bb192ed9 3574 discard_slab(s, slab);
81819f0f
CL
3575}
3576
894b8788
CL
3577/*
3578 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
3579 * can perform fastpath freeing without additional function calls.
3580 *
3581 * The fastpath is only possible if we are freeing to the current cpu slab
3582 * of this processor. This typically the case if we have just allocated
3583 * the item before.
3584 *
3585 * If fastpath is not possible then fall back to __slab_free where we deal
3586 * with all sorts of special processing.
81084651
JDB
3587 *
3588 * Bulk free of a freelist with several objects (all pointing to the
c2092c12 3589 * same slab) possible by specifying head and tail ptr, plus objects
81084651 3590 * count (cnt). Bulk free indicated by tail pointer being set.
894b8788 3591 */
80a9201a 3592static __always_inline void do_slab_free(struct kmem_cache *s,
bb192ed9 3593 struct slab *slab, void *head, void *tail,
80a9201a 3594 int cnt, unsigned long addr)
894b8788 3595{
81084651 3596 void *tail_obj = tail ? : head;
dfb4f096 3597 struct kmem_cache_cpu *c;
8a5ec0ba 3598 unsigned long tid;
1f04b07d 3599 void **freelist;
964d4bd3 3600
8a5ec0ba
CL
3601redo:
3602 /*
3603 * Determine the currently cpus per cpu slab.
3604 * The cpu may change afterward. However that does not matter since
3605 * data is retrieved via this pointer. If we are on the same cpu
2ae44005 3606 * during the cmpxchg then the free will succeed.
8a5ec0ba 3607 */
9b4bc85a
VB
3608 c = raw_cpu_ptr(s->cpu_slab);
3609 tid = READ_ONCE(c->tid);
c016b0bd 3610
9aabf810
JK
3611 /* Same with comment on barrier() in slab_alloc_node() */
3612 barrier();
c016b0bd 3613
1f04b07d
TG
3614 if (unlikely(slab != c->slab)) {
3615 __slab_free(s, slab, head, tail_obj, cnt, addr);
3616 return;
3617 }
3618
3619 if (USE_LOCKLESS_FAST_PATH()) {
3620 freelist = READ_ONCE(c->freelist);
5076190d
LT
3621
3622 set_freepointer(s, tail_obj, freelist);
8a5ec0ba 3623
933393f5 3624 if (unlikely(!this_cpu_cmpxchg_double(
8a5ec0ba 3625 s->cpu_slab->freelist, s->cpu_slab->tid,
5076190d 3626 freelist, tid,
81084651 3627 head, next_tid(tid)))) {
8a5ec0ba
CL
3628
3629 note_cmpxchg_failure("slab_free", s, tid);
3630 goto redo;
3631 }
1f04b07d
TG
3632 } else {
3633 /* Update the free list under the local lock */
bd0e7491
VB
3634 local_lock(&s->cpu_slab->lock);
3635 c = this_cpu_ptr(s->cpu_slab);
bb192ed9 3636 if (unlikely(slab != c->slab)) {
bd0e7491
VB
3637 local_unlock(&s->cpu_slab->lock);
3638 goto redo;
3639 }
3640 tid = c->tid;
3641 freelist = c->freelist;
3642
3643 set_freepointer(s, tail_obj, freelist);
3644 c->freelist = head;
3645 c->tid = next_tid(tid);
3646
3647 local_unlock(&s->cpu_slab->lock);
1f04b07d
TG
3648 }
3649 stat(s, FREE_FASTPATH);
894b8788
CL
3650}
3651
bb192ed9 3652static __always_inline void slab_free(struct kmem_cache *s, struct slab *slab,
b77d5b1b 3653 void *head, void *tail, void **p, int cnt,
80a9201a
AP
3654 unsigned long addr)
3655{
b77d5b1b 3656 memcg_slab_free_hook(s, slab, p, cnt);
80a9201a 3657 /*
c3895391
AK
3658 * With KASAN enabled slab_free_freelist_hook modifies the freelist
3659 * to remove objects, whose reuse must be delayed.
80a9201a 3660 */
899447f6 3661 if (slab_free_freelist_hook(s, &head, &tail, &cnt))
bb192ed9 3662 do_slab_free(s, slab, head, tail, cnt, addr);
80a9201a
AP
3663}
3664
2bd926b4 3665#ifdef CONFIG_KASAN_GENERIC
80a9201a
AP
3666void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr)
3667{
bb192ed9 3668 do_slab_free(cache, virt_to_slab(x), x, NULL, 1, addr);
80a9201a
AP
3669}
3670#endif
3671
ed4cd17e
HY
3672void __kmem_cache_free(struct kmem_cache *s, void *x, unsigned long caller)
3673{
3674 slab_free(s, virt_to_slab(x), x, NULL, &x, 1, caller);
3675}
3676
81819f0f
CL
3677void kmem_cache_free(struct kmem_cache *s, void *x)
3678{
b9ce5ef4
GC
3679 s = cache_from_obj(s, x);
3680 if (!s)
79576102 3681 return;
2c1d697f 3682 trace_kmem_cache_free(_RET_IP_, x, s);
b77d5b1b 3683 slab_free(s, virt_to_slab(x), x, NULL, &x, 1, _RET_IP_);
81819f0f
CL
3684}
3685EXPORT_SYMBOL(kmem_cache_free);
3686
d0ecd894 3687struct detached_freelist {
cc465c3b 3688 struct slab *slab;
d0ecd894
JDB
3689 void *tail;
3690 void *freelist;
3691 int cnt;
376bf125 3692 struct kmem_cache *s;
d0ecd894 3693};
fbd02630 3694
d0ecd894
JDB
3695/*
3696 * This function progressively scans the array with free objects (with
3697 * a limited look ahead) and extract objects belonging to the same
cc465c3b
MWO
3698 * slab. It builds a detached freelist directly within the given
3699 * slab/objects. This can happen without any need for
d0ecd894
JDB
3700 * synchronization, because the objects are owned by running process.
3701 * The freelist is build up as a single linked list in the objects.
3702 * The idea is, that this detached freelist can then be bulk
3703 * transferred to the real freelist(s), but only requiring a single
3704 * synchronization primitive. Look ahead in the array is limited due
3705 * to performance reasons.
3706 */
376bf125
JDB
3707static inline
3708int build_detached_freelist(struct kmem_cache *s, size_t size,
3709 void **p, struct detached_freelist *df)
d0ecd894 3710{
d0ecd894
JDB
3711 int lookahead = 3;
3712 void *object;
cc465c3b 3713 struct folio *folio;
b77d5b1b 3714 size_t same;
fbd02630 3715
b77d5b1b 3716 object = p[--size];
cc465c3b 3717 folio = virt_to_folio(object);
ca257195
JDB
3718 if (!s) {
3719 /* Handle kalloc'ed objects */
cc465c3b 3720 if (unlikely(!folio_test_slab(folio))) {
d835eef4 3721 free_large_kmalloc(folio, object);
b77d5b1b 3722 df->slab = NULL;
ca257195
JDB
3723 return size;
3724 }
3725 /* Derive kmem_cache from object */
b77d5b1b
MS
3726 df->slab = folio_slab(folio);
3727 df->s = df->slab->slab_cache;
ca257195 3728 } else {
b77d5b1b 3729 df->slab = folio_slab(folio);
ca257195
JDB
3730 df->s = cache_from_obj(s, object); /* Support for memcg */
3731 }
376bf125 3732
d0ecd894 3733 /* Start new detached freelist */
d0ecd894
JDB
3734 df->tail = object;
3735 df->freelist = object;
d0ecd894
JDB
3736 df->cnt = 1;
3737
b77d5b1b
MS
3738 if (is_kfence_address(object))
3739 return size;
3740
3741 set_freepointer(df->s, object, NULL);
3742
3743 same = size;
d0ecd894
JDB
3744 while (size) {
3745 object = p[--size];
cc465c3b
MWO
3746 /* df->slab is always set at this point */
3747 if (df->slab == virt_to_slab(object)) {
d0ecd894 3748 /* Opportunity build freelist */
376bf125 3749 set_freepointer(df->s, object, df->freelist);
d0ecd894
JDB
3750 df->freelist = object;
3751 df->cnt++;
b77d5b1b
MS
3752 same--;
3753 if (size != same)
3754 swap(p[size], p[same]);
d0ecd894 3755 continue;
fbd02630 3756 }
d0ecd894
JDB
3757
3758 /* Limit look ahead search */
3759 if (!--lookahead)
3760 break;
fbd02630 3761 }
d0ecd894 3762
b77d5b1b 3763 return same;
d0ecd894
JDB
3764}
3765
d0ecd894 3766/* Note that interrupts must be enabled when calling this function. */
376bf125 3767void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p)
d0ecd894 3768{
2055e67b 3769 if (!size)
d0ecd894
JDB
3770 return;
3771
3772 do {
3773 struct detached_freelist df;
3774
3775 size = build_detached_freelist(s, size, p, &df);
cc465c3b 3776 if (!df.slab)
d0ecd894
JDB
3777 continue;
3778
b77d5b1b
MS
3779 slab_free(df.s, df.slab, df.freelist, df.tail, &p[size], df.cnt,
3780 _RET_IP_);
d0ecd894 3781 } while (likely(size));
484748f0
CL
3782}
3783EXPORT_SYMBOL(kmem_cache_free_bulk);
3784
994eb764 3785/* Note that interrupts must be enabled when calling this function. */
865762a8
JDB
3786int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
3787 void **p)
484748f0 3788{
994eb764
JDB
3789 struct kmem_cache_cpu *c;
3790 int i;
964d4bd3 3791 struct obj_cgroup *objcg = NULL;
994eb764 3792
03ec0ed5 3793 /* memcg and kmem_cache debug support */
88f2ef73 3794 s = slab_pre_alloc_hook(s, NULL, &objcg, size, flags);
03ec0ed5
JDB
3795 if (unlikely(!s))
3796 return false;
994eb764
JDB
3797 /*
3798 * Drain objects in the per cpu slab, while disabling local
3799 * IRQs, which protects against PREEMPT and interrupts
3800 * handlers invoking normal fastpath.
3801 */
25c00c50 3802 c = slub_get_cpu_ptr(s->cpu_slab);
bd0e7491 3803 local_lock_irq(&s->cpu_slab->lock);
994eb764
JDB
3804
3805 for (i = 0; i < size; i++) {
b89fb5ef 3806 void *object = kfence_alloc(s, s->object_size, flags);
994eb764 3807
b89fb5ef
AP
3808 if (unlikely(object)) {
3809 p[i] = object;
3810 continue;
3811 }
3812
3813 object = c->freelist;
ebe909e0 3814 if (unlikely(!object)) {
fd4d9c7d
JH
3815 /*
3816 * We may have removed an object from c->freelist using
3817 * the fastpath in the previous iteration; in that case,
3818 * c->tid has not been bumped yet.
3819 * Since ___slab_alloc() may reenable interrupts while
3820 * allocating memory, we should bump c->tid now.
3821 */
3822 c->tid = next_tid(c->tid);
3823
bd0e7491 3824 local_unlock_irq(&s->cpu_slab->lock);
e500059b 3825
ebe909e0
JDB
3826 /*
3827 * Invoking slow path likely have side-effect
3828 * of re-populating per CPU c->freelist
3829 */
87098373 3830 p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
6edf2576 3831 _RET_IP_, c, s->object_size);
87098373
CL
3832 if (unlikely(!p[i]))
3833 goto error;
3834
ebe909e0 3835 c = this_cpu_ptr(s->cpu_slab);
0f181f9f
AP
3836 maybe_wipe_obj_freeptr(s, p[i]);
3837
bd0e7491 3838 local_lock_irq(&s->cpu_slab->lock);
e500059b 3839
ebe909e0
JDB
3840 continue; /* goto for-loop */
3841 }
994eb764
JDB
3842 c->freelist = get_freepointer(s, object);
3843 p[i] = object;
0f181f9f 3844 maybe_wipe_obj_freeptr(s, p[i]);
994eb764
JDB
3845 }
3846 c->tid = next_tid(c->tid);
bd0e7491 3847 local_unlock_irq(&s->cpu_slab->lock);
25c00c50 3848 slub_put_cpu_ptr(s->cpu_slab);
994eb764 3849
da844b78
AK
3850 /*
3851 * memcg and kmem_cache debug support and memory initialization.
3852 * Done outside of the IRQ disabled fastpath loop.
3853 */
3854 slab_post_alloc_hook(s, objcg, flags, size, p,
3855 slab_want_init_on_alloc(flags, s));
865762a8 3856 return i;
87098373 3857error:
25c00c50 3858 slub_put_cpu_ptr(s->cpu_slab);
da844b78 3859 slab_post_alloc_hook(s, objcg, flags, i, p, false);
2055e67b 3860 kmem_cache_free_bulk(s, i, p);
865762a8 3861 return 0;
484748f0
CL
3862}
3863EXPORT_SYMBOL(kmem_cache_alloc_bulk);
3864
3865
81819f0f 3866/*
672bba3a
CL
3867 * Object placement in a slab is made very easy because we always start at
3868 * offset 0. If we tune the size of the object to the alignment then we can
3869 * get the required alignment by putting one properly sized object after
3870 * another.
81819f0f
CL
3871 *
3872 * Notice that the allocation order determines the sizes of the per cpu
3873 * caches. Each processor has always one slab available for allocations.
3874 * Increasing the allocation order reduces the number of times that slabs
672bba3a 3875 * must be moved on and off the partial lists and is therefore a factor in
81819f0f 3876 * locking overhead.
81819f0f
CL
3877 */
3878
3879/*
f0953a1b 3880 * Minimum / Maximum order of slab pages. This influences locking overhead
81819f0f
CL
3881 * and slab fragmentation. A higher order reduces the number of partial slabs
3882 * and increases the number of allocations possible without having to
3883 * take the list_lock.
3884 */
19af27af
AD
3885static unsigned int slub_min_order;
3886static unsigned int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
3887static unsigned int slub_min_objects;
81819f0f 3888
81819f0f
CL
3889/*
3890 * Calculate the order of allocation given an slab object size.
3891 *
672bba3a
CL
3892 * The order of allocation has significant impact on performance and other
3893 * system components. Generally order 0 allocations should be preferred since
3894 * order 0 does not cause fragmentation in the page allocator. Larger objects
3895 * be problematic to put into order 0 slabs because there may be too much
c124f5b5 3896 * unused space left. We go to a higher order if more than 1/16th of the slab
672bba3a
CL
3897 * would be wasted.
3898 *
3899 * In order to reach satisfactory performance we must ensure that a minimum
3900 * number of objects is in one slab. Otherwise we may generate too much
3901 * activity on the partial lists which requires taking the list_lock. This is
3902 * less a concern for large slabs though which are rarely used.
81819f0f 3903 *
672bba3a
CL
3904 * slub_max_order specifies the order where we begin to stop considering the
3905 * number of objects in a slab as critical. If we reach slub_max_order then
3906 * we try to keep the page order as low as possible. So we accept more waste
3907 * of space in favor of a small page order.
81819f0f 3908 *
672bba3a
CL
3909 * Higher order allocations also allow the placement of more objects in a
3910 * slab and thereby reduce object handling overhead. If the user has
dc84207d 3911 * requested a higher minimum order then we start with that one instead of
672bba3a 3912 * the smallest order which will fit the object.
81819f0f 3913 */
d122019b 3914static inline unsigned int calc_slab_order(unsigned int size,
19af27af 3915 unsigned int min_objects, unsigned int max_order,
9736d2a9 3916 unsigned int fract_leftover)
81819f0f 3917{
19af27af
AD
3918 unsigned int min_order = slub_min_order;
3919 unsigned int order;
81819f0f 3920
9736d2a9 3921 if (order_objects(min_order, size) > MAX_OBJS_PER_PAGE)
210b5c06 3922 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
39b26464 3923
9736d2a9 3924 for (order = max(min_order, (unsigned int)get_order(min_objects * size));
5e6d444e 3925 order <= max_order; order++) {
81819f0f 3926
19af27af
AD
3927 unsigned int slab_size = (unsigned int)PAGE_SIZE << order;
3928 unsigned int rem;
81819f0f 3929
9736d2a9 3930 rem = slab_size % size;
81819f0f 3931
5e6d444e 3932 if (rem <= slab_size / fract_leftover)
81819f0f 3933 break;
81819f0f 3934 }
672bba3a 3935
81819f0f
CL
3936 return order;
3937}
3938
9736d2a9 3939static inline int calculate_order(unsigned int size)
5e6d444e 3940{
19af27af
AD
3941 unsigned int order;
3942 unsigned int min_objects;
3943 unsigned int max_objects;
3286222f 3944 unsigned int nr_cpus;
5e6d444e
CL
3945
3946 /*
3947 * Attempt to find best configuration for a slab. This
3948 * works by first attempting to generate a layout with
3949 * the best configuration and backing off gradually.
3950 *
422ff4d7 3951 * First we increase the acceptable waste in a slab. Then
5e6d444e
CL
3952 * we reduce the minimum objects required in a slab.
3953 */
3954 min_objects = slub_min_objects;
3286222f
VB
3955 if (!min_objects) {
3956 /*
3957 * Some architectures will only update present cpus when
3958 * onlining them, so don't trust the number if it's just 1. But
3959 * we also don't want to use nr_cpu_ids always, as on some other
3960 * architectures, there can be many possible cpus, but never
3961 * onlined. Here we compromise between trying to avoid too high
3962 * order on systems that appear larger than they are, and too
3963 * low order on systems that appear smaller than they are.
3964 */
3965 nr_cpus = num_present_cpus();
3966 if (nr_cpus <= 1)
3967 nr_cpus = nr_cpu_ids;
3968 min_objects = 4 * (fls(nr_cpus) + 1);
3969 }
9736d2a9 3970 max_objects = order_objects(slub_max_order, size);
e8120ff1
ZY
3971 min_objects = min(min_objects, max_objects);
3972
5e6d444e 3973 while (min_objects > 1) {
19af27af
AD
3974 unsigned int fraction;
3975
c124f5b5 3976 fraction = 16;
5e6d444e 3977 while (fraction >= 4) {
d122019b 3978 order = calc_slab_order(size, min_objects,
9736d2a9 3979 slub_max_order, fraction);
5e6d444e
CL
3980 if (order <= slub_max_order)
3981 return order;
3982 fraction /= 2;
3983 }
5086c389 3984 min_objects--;
5e6d444e
CL
3985 }
3986
3987 /*
3988 * We were unable to place multiple objects in a slab. Now
3989 * lets see if we can place a single object there.
3990 */
d122019b 3991 order = calc_slab_order(size, 1, slub_max_order, 1);
5e6d444e
CL
3992 if (order <= slub_max_order)
3993 return order;
3994
3995 /*
3996 * Doh this slab cannot be placed using slub_max_order.
3997 */
d122019b 3998 order = calc_slab_order(size, 1, MAX_ORDER, 1);
818cf590 3999 if (order < MAX_ORDER)
5e6d444e
CL
4000 return order;
4001 return -ENOSYS;
4002}
4003
5595cffc 4004static void
4053497d 4005init_kmem_cache_node(struct kmem_cache_node *n)
81819f0f
CL
4006{
4007 n->nr_partial = 0;
81819f0f
CL
4008 spin_lock_init(&n->list_lock);
4009 INIT_LIST_HEAD(&n->partial);
8ab1372f 4010#ifdef CONFIG_SLUB_DEBUG
0f389ec6 4011 atomic_long_set(&n->nr_slabs, 0);
02b71b70 4012 atomic_long_set(&n->total_objects, 0);
643b1138 4013 INIT_LIST_HEAD(&n->full);
8ab1372f 4014#endif
81819f0f
CL
4015}
4016
55136592 4017static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
4c93c355 4018{
6c182dc0 4019 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
95a05b42 4020 KMALLOC_SHIFT_HIGH * sizeof(struct kmem_cache_cpu));
4c93c355 4021
8a5ec0ba 4022 /*
d4d84fef
CM
4023 * Must align to double word boundary for the double cmpxchg
4024 * instructions to work; see __pcpu_double_call_return_bool().
8a5ec0ba 4025 */
d4d84fef
CM
4026 s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
4027 2 * sizeof(void *));
8a5ec0ba
CL
4028
4029 if (!s->cpu_slab)
4030 return 0;
4031
4032 init_kmem_cache_cpus(s);
4c93c355 4033
8a5ec0ba 4034 return 1;
4c93c355 4035}
4c93c355 4036
51df1142
CL
4037static struct kmem_cache *kmem_cache_node;
4038
81819f0f
CL
4039/*
4040 * No kmalloc_node yet so do it by hand. We know that this is the first
4041 * slab on the node for this slabcache. There are no concurrent accesses
4042 * possible.
4043 *
721ae22a
ZYW
4044 * Note that this function only works on the kmem_cache_node
4045 * when allocating for the kmem_cache_node. This is used for bootstrapping
4c93c355 4046 * memory on a fresh node that has no slab structures yet.
81819f0f 4047 */
55136592 4048static void early_kmem_cache_node_alloc(int node)
81819f0f 4049{
bb192ed9 4050 struct slab *slab;
81819f0f
CL
4051 struct kmem_cache_node *n;
4052
51df1142 4053 BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
81819f0f 4054
bb192ed9 4055 slab = new_slab(kmem_cache_node, GFP_NOWAIT, node);
81819f0f 4056
bb192ed9 4057 BUG_ON(!slab);
c7323a5a 4058 inc_slabs_node(kmem_cache_node, slab_nid(slab), slab->objects);
bb192ed9 4059 if (slab_nid(slab) != node) {
f9f58285
FF
4060 pr_err("SLUB: Unable to allocate memory from node %d\n", node);
4061 pr_err("SLUB: Allocating a useless per node structure in order to be able to continue\n");
a2f92ee7
CL
4062 }
4063
bb192ed9 4064 n = slab->freelist;
81819f0f 4065 BUG_ON(!n);
8ab1372f 4066#ifdef CONFIG_SLUB_DEBUG
f7cb1933 4067 init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
51df1142 4068 init_tracking(kmem_cache_node, n);
8ab1372f 4069#endif
da844b78 4070 n = kasan_slab_alloc(kmem_cache_node, n, GFP_KERNEL, false);
bb192ed9
VB
4071 slab->freelist = get_freepointer(kmem_cache_node, n);
4072 slab->inuse = 1;
12b22386 4073 kmem_cache_node->node[node] = n;
4053497d 4074 init_kmem_cache_node(n);
bb192ed9 4075 inc_slabs_node(kmem_cache_node, node, slab->objects);
6446faa2 4076
67b6c900 4077 /*
1e4dd946
SR
4078 * No locks need to be taken here as it has just been
4079 * initialized and there is no concurrent access.
67b6c900 4080 */
bb192ed9 4081 __add_partial(n, slab, DEACTIVATE_TO_HEAD);
81819f0f
CL
4082}
4083
4084static void free_kmem_cache_nodes(struct kmem_cache *s)
4085{
4086 int node;
fa45dc25 4087 struct kmem_cache_node *n;
81819f0f 4088
fa45dc25 4089 for_each_kmem_cache_node(s, node, n) {
81819f0f 4090 s->node[node] = NULL;
ea37df54 4091 kmem_cache_free(kmem_cache_node, n);
81819f0f
CL
4092 }
4093}
4094
52b4b950
DS
4095void __kmem_cache_release(struct kmem_cache *s)
4096{
210e7a43 4097 cache_random_seq_destroy(s);
52b4b950
DS
4098 free_percpu(s->cpu_slab);
4099 free_kmem_cache_nodes(s);
4100}
4101
55136592 4102static int init_kmem_cache_nodes(struct kmem_cache *s)
81819f0f
CL
4103{
4104 int node;
81819f0f 4105
7e1fa93d 4106 for_each_node_mask(node, slab_nodes) {
81819f0f
CL
4107 struct kmem_cache_node *n;
4108
73367bd8 4109 if (slab_state == DOWN) {
55136592 4110 early_kmem_cache_node_alloc(node);
73367bd8
AD
4111 continue;
4112 }
51df1142 4113 n = kmem_cache_alloc_node(kmem_cache_node,
55136592 4114 GFP_KERNEL, node);
81819f0f 4115
73367bd8
AD
4116 if (!n) {
4117 free_kmem_cache_nodes(s);
4118 return 0;
81819f0f 4119 }
73367bd8 4120
4053497d 4121 init_kmem_cache_node(n);
ea37df54 4122 s->node[node] = n;
81819f0f
CL
4123 }
4124 return 1;
4125}
81819f0f 4126
e6d0e1dc
WY
4127static void set_cpu_partial(struct kmem_cache *s)
4128{
4129#ifdef CONFIG_SLUB_CPU_PARTIAL
b47291ef
VB
4130 unsigned int nr_objects;
4131
e6d0e1dc
WY
4132 /*
4133 * cpu_partial determined the maximum number of objects kept in the
4134 * per cpu partial lists of a processor.
4135 *
4136 * Per cpu partial lists mainly contain slabs that just have one
4137 * object freed. If they are used for allocation then they can be
4138 * filled up again with minimal effort. The slab will never hit the
4139 * per node partial lists and therefore no locking will be required.
4140 *
b47291ef
VB
4141 * For backwards compatibility reasons, this is determined as number
4142 * of objects, even though we now limit maximum number of pages, see
4143 * slub_set_cpu_partial()
e6d0e1dc
WY
4144 */
4145 if (!kmem_cache_has_cpu_partial(s))
b47291ef 4146 nr_objects = 0;
e6d0e1dc 4147 else if (s->size >= PAGE_SIZE)
b47291ef 4148 nr_objects = 6;
e6d0e1dc 4149 else if (s->size >= 1024)
23e98ad1 4150 nr_objects = 24;
e6d0e1dc 4151 else if (s->size >= 256)
23e98ad1 4152 nr_objects = 52;
e6d0e1dc 4153 else
23e98ad1 4154 nr_objects = 120;
b47291ef
VB
4155
4156 slub_set_cpu_partial(s, nr_objects);
e6d0e1dc
WY
4157#endif
4158}
4159
81819f0f
CL
4160/*
4161 * calculate_sizes() determines the order and the distribution of data within
4162 * a slab object.
4163 */
ae44d81d 4164static int calculate_sizes(struct kmem_cache *s)
81819f0f 4165{
d50112ed 4166 slab_flags_t flags = s->flags;
be4a7988 4167 unsigned int size = s->object_size;
19af27af 4168 unsigned int order;
81819f0f 4169
d8b42bf5
CL
4170 /*
4171 * Round up object size to the next word boundary. We can only
4172 * place the free pointer at word boundaries and this determines
4173 * the possible location of the free pointer.
4174 */
4175 size = ALIGN(size, sizeof(void *));
4176
4177#ifdef CONFIG_SLUB_DEBUG
81819f0f
CL
4178 /*
4179 * Determine if we can poison the object itself. If the user of
4180 * the slab may touch the object after free or before allocation
4181 * then we should never poison the object itself.
4182 */
5f0d5a3a 4183 if ((flags & SLAB_POISON) && !(flags & SLAB_TYPESAFE_BY_RCU) &&
c59def9f 4184 !s->ctor)
81819f0f
CL
4185 s->flags |= __OBJECT_POISON;
4186 else
4187 s->flags &= ~__OBJECT_POISON;
4188
81819f0f
CL
4189
4190 /*
672bba3a 4191 * If we are Redzoning then check if there is some space between the
81819f0f 4192 * end of the object and the free pointer. If not then add an
672bba3a 4193 * additional word to have some bytes to store Redzone information.
81819f0f 4194 */
3b0efdfa 4195 if ((flags & SLAB_RED_ZONE) && size == s->object_size)
81819f0f 4196 size += sizeof(void *);
41ecc55b 4197#endif
81819f0f
CL
4198
4199 /*
672bba3a 4200 * With that we have determined the number of bytes in actual use
e41a49fa 4201 * by the object and redzoning.
81819f0f
CL
4202 */
4203 s->inuse = size;
4204
74c1d3e0
KC
4205 if ((flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) ||
4206 ((flags & SLAB_RED_ZONE) && s->object_size < sizeof(void *)) ||
4207 s->ctor) {
81819f0f
CL
4208 /*
4209 * Relocate free pointer after the object if it is not
4210 * permitted to overwrite the first word of the object on
4211 * kmem_cache_free.
4212 *
4213 * This is the case if we do RCU, have a constructor or
74c1d3e0
KC
4214 * destructor, are poisoning the objects, or are
4215 * redzoning an object smaller than sizeof(void *).
cbfc35a4
WL
4216 *
4217 * The assumption that s->offset >= s->inuse means free
4218 * pointer is outside of the object is used in the
4219 * freeptr_outside_object() function. If that is no
4220 * longer true, the function needs to be modified.
81819f0f
CL
4221 */
4222 s->offset = size;
4223 size += sizeof(void *);
e41a49fa 4224 } else {
3202fa62
KC
4225 /*
4226 * Store freelist pointer near middle of object to keep
4227 * it away from the edges of the object to avoid small
4228 * sized over/underflows from neighboring allocations.
4229 */
e41a49fa 4230 s->offset = ALIGN_DOWN(s->object_size / 2, sizeof(void *));
81819f0f
CL
4231 }
4232
c12b3c62 4233#ifdef CONFIG_SLUB_DEBUG
6edf2576 4234 if (flags & SLAB_STORE_USER) {
81819f0f
CL
4235 /*
4236 * Need to store information about allocs and frees after
4237 * the object.
4238 */
4239 size += 2 * sizeof(struct track);
6edf2576
FT
4240
4241 /* Save the original kmalloc request size */
4242 if (flags & SLAB_KMALLOC)
4243 size += sizeof(unsigned int);
4244 }
80a9201a 4245#endif
81819f0f 4246
80a9201a
AP
4247 kasan_cache_create(s, &size, &s->flags);
4248#ifdef CONFIG_SLUB_DEBUG
d86bd1be 4249 if (flags & SLAB_RED_ZONE) {
81819f0f
CL
4250 /*
4251 * Add some empty padding so that we can catch
4252 * overwrites from earlier objects rather than let
4253 * tracking information or the free pointer be
0211a9c8 4254 * corrupted if a user writes before the start
81819f0f
CL
4255 * of the object.
4256 */
4257 size += sizeof(void *);
d86bd1be
JK
4258
4259 s->red_left_pad = sizeof(void *);
4260 s->red_left_pad = ALIGN(s->red_left_pad, s->align);
4261 size += s->red_left_pad;
4262 }
41ecc55b 4263#endif
672bba3a 4264
81819f0f
CL
4265 /*
4266 * SLUB stores one object immediately after another beginning from
4267 * offset 0. In order to align the objects we have to simply size
4268 * each object to conform to the alignment.
4269 */
45906855 4270 size = ALIGN(size, s->align);
81819f0f 4271 s->size = size;
4138fdfc 4272 s->reciprocal_size = reciprocal_value(size);
ae44d81d 4273 order = calculate_order(size);
81819f0f 4274
19af27af 4275 if ((int)order < 0)
81819f0f
CL
4276 return 0;
4277
b7a49f0d 4278 s->allocflags = 0;
834f3d11 4279 if (order)
b7a49f0d
CL
4280 s->allocflags |= __GFP_COMP;
4281
4282 if (s->flags & SLAB_CACHE_DMA)
2c59dd65 4283 s->allocflags |= GFP_DMA;
b7a49f0d 4284
6d6ea1e9
NB
4285 if (s->flags & SLAB_CACHE_DMA32)
4286 s->allocflags |= GFP_DMA32;
4287
b7a49f0d
CL
4288 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4289 s->allocflags |= __GFP_RECLAIMABLE;
4290
81819f0f
CL
4291 /*
4292 * Determine the number of objects per slab
4293 */
9736d2a9
MW
4294 s->oo = oo_make(order, size);
4295 s->min = oo_make(get_order(size), size);
81819f0f 4296
834f3d11 4297 return !!oo_objects(s->oo);
81819f0f
CL
4298}
4299
d50112ed 4300static int kmem_cache_open(struct kmem_cache *s, slab_flags_t flags)
81819f0f 4301{
37540008 4302 s->flags = kmem_cache_flags(s->size, flags, s->name);
2482ddec
KC
4303#ifdef CONFIG_SLAB_FREELIST_HARDENED
4304 s->random = get_random_long();
4305#endif
81819f0f 4306
ae44d81d 4307 if (!calculate_sizes(s))
81819f0f 4308 goto error;
3de47213
DR
4309 if (disable_higher_order_debug) {
4310 /*
4311 * Disable debugging flags that store metadata if the min slab
4312 * order increased.
4313 */
3b0efdfa 4314 if (get_order(s->size) > get_order(s->object_size)) {
3de47213
DR
4315 s->flags &= ~DEBUG_METADATA_FLAGS;
4316 s->offset = 0;
ae44d81d 4317 if (!calculate_sizes(s))
3de47213
DR
4318 goto error;
4319 }
4320 }
81819f0f 4321
2565409f
HC
4322#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
4323 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
149daaf3 4324 if (system_has_cmpxchg_double() && (s->flags & SLAB_NO_CMPXCHG) == 0)
b789ef51
CL
4325 /* Enable fast mode */
4326 s->flags |= __CMPXCHG_DOUBLE;
4327#endif
4328
3b89d7d8 4329 /*
c2092c12 4330 * The larger the object size is, the more slabs we want on the partial
3b89d7d8
DR
4331 * list to avoid pounding the page allocator excessively.
4332 */
5182f3c9
HY
4333 s->min_partial = min_t(unsigned long, MAX_PARTIAL, ilog2(s->size) / 2);
4334 s->min_partial = max_t(unsigned long, MIN_PARTIAL, s->min_partial);
49e22585 4335
e6d0e1dc 4336 set_cpu_partial(s);
49e22585 4337
81819f0f 4338#ifdef CONFIG_NUMA
e2cb96b7 4339 s->remote_node_defrag_ratio = 1000;
81819f0f 4340#endif
210e7a43
TG
4341
4342 /* Initialize the pre-computed randomized freelist if slab is up */
4343 if (slab_state >= UP) {
4344 if (init_cache_random_seq(s))
4345 goto error;
4346 }
4347
55136592 4348 if (!init_kmem_cache_nodes(s))
dfb4f096 4349 goto error;
81819f0f 4350
55136592 4351 if (alloc_kmem_cache_cpus(s))
278b1bb1 4352 return 0;
ff12059e 4353
81819f0f 4354error:
9037c576 4355 __kmem_cache_release(s);
278b1bb1 4356 return -EINVAL;
81819f0f 4357}
81819f0f 4358
bb192ed9 4359static void list_slab_objects(struct kmem_cache *s, struct slab *slab,
55860d96 4360 const char *text)
33b12c38
CL
4361{
4362#ifdef CONFIG_SLUB_DEBUG
bb192ed9 4363 void *addr = slab_address(slab);
33b12c38 4364 void *p;
aa456c7a 4365
bb192ed9 4366 slab_err(s, slab, text, s->name);
33b12c38 4367
4ef3f5a3
VB
4368 spin_lock(&object_map_lock);
4369 __fill_map(object_map, s, slab);
4370
bb192ed9 4371 for_each_object(p, s, addr, slab->objects) {
33b12c38 4372
4ef3f5a3 4373 if (!test_bit(__obj_to_index(s, addr, p), object_map)) {
96b94abc 4374 pr_err("Object 0x%p @offset=%tu\n", p, p - addr);
33b12c38
CL
4375 print_tracking(s, p);
4376 }
4377 }
4ef3f5a3 4378 spin_unlock(&object_map_lock);
33b12c38
CL
4379#endif
4380}
4381
81819f0f 4382/*
599870b1 4383 * Attempt to free all partial slabs on a node.
52b4b950
DS
4384 * This is called from __kmem_cache_shutdown(). We must take list_lock
4385 * because sysfs file might still access partial list after the shutdowning.
81819f0f 4386 */
599870b1 4387static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
81819f0f 4388{
60398923 4389 LIST_HEAD(discard);
bb192ed9 4390 struct slab *slab, *h;
81819f0f 4391
52b4b950
DS
4392 BUG_ON(irqs_disabled());
4393 spin_lock_irq(&n->list_lock);
bb192ed9
VB
4394 list_for_each_entry_safe(slab, h, &n->partial, slab_list) {
4395 if (!slab->inuse) {
4396 remove_partial(n, slab);
4397 list_add(&slab->slab_list, &discard);
33b12c38 4398 } else {
bb192ed9 4399 list_slab_objects(s, slab,
55860d96 4400 "Objects remaining in %s on __kmem_cache_shutdown()");
599870b1 4401 }
33b12c38 4402 }
52b4b950 4403 spin_unlock_irq(&n->list_lock);
60398923 4404
bb192ed9
VB
4405 list_for_each_entry_safe(slab, h, &discard, slab_list)
4406 discard_slab(s, slab);
81819f0f
CL
4407}
4408
f9e13c0a
SB
4409bool __kmem_cache_empty(struct kmem_cache *s)
4410{
4411 int node;
4412 struct kmem_cache_node *n;
4413
4414 for_each_kmem_cache_node(s, node, n)
4415 if (n->nr_partial || slabs_node(s, node))
4416 return false;
4417 return true;
4418}
4419
81819f0f 4420/*
672bba3a 4421 * Release all resources used by a slab cache.
81819f0f 4422 */
52b4b950 4423int __kmem_cache_shutdown(struct kmem_cache *s)
81819f0f
CL
4424{
4425 int node;
fa45dc25 4426 struct kmem_cache_node *n;
81819f0f 4427
5a836bf6 4428 flush_all_cpus_locked(s);
81819f0f 4429 /* Attempt to free all objects */
fa45dc25 4430 for_each_kmem_cache_node(s, node, n) {
599870b1
CL
4431 free_partial(s, n);
4432 if (n->nr_partial || slabs_node(s, node))
81819f0f
CL
4433 return 1;
4434 }
81819f0f
CL
4435 return 0;
4436}
4437
5bb1bb35 4438#ifdef CONFIG_PRINTK
2dfe63e6 4439void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
8e7f37f2
PM
4440{
4441 void *base;
4442 int __maybe_unused i;
4443 unsigned int objnr;
4444 void *objp;
4445 void *objp0;
7213230a 4446 struct kmem_cache *s = slab->slab_cache;
8e7f37f2
PM
4447 struct track __maybe_unused *trackp;
4448
4449 kpp->kp_ptr = object;
7213230a 4450 kpp->kp_slab = slab;
8e7f37f2 4451 kpp->kp_slab_cache = s;
7213230a 4452 base = slab_address(slab);
8e7f37f2
PM
4453 objp0 = kasan_reset_tag(object);
4454#ifdef CONFIG_SLUB_DEBUG
4455 objp = restore_red_left(s, objp0);
4456#else
4457 objp = objp0;
4458#endif
40f3bf0c 4459 objnr = obj_to_index(s, slab, objp);
8e7f37f2
PM
4460 kpp->kp_data_offset = (unsigned long)((char *)objp0 - (char *)objp);
4461 objp = base + s->size * objnr;
4462 kpp->kp_objp = objp;
7213230a
MWO
4463 if (WARN_ON_ONCE(objp < base || objp >= base + slab->objects * s->size
4464 || (objp - base) % s->size) ||
8e7f37f2
PM
4465 !(s->flags & SLAB_STORE_USER))
4466 return;
4467#ifdef CONFIG_SLUB_DEBUG
0cbc124b 4468 objp = fixup_red_left(s, objp);
8e7f37f2
PM
4469 trackp = get_track(s, objp, TRACK_ALLOC);
4470 kpp->kp_ret = (void *)trackp->addr;
5cf909c5
OG
4471#ifdef CONFIG_STACKDEPOT
4472 {
4473 depot_stack_handle_t handle;
4474 unsigned long *entries;
4475 unsigned int nr_entries;
78869146 4476
5cf909c5
OG
4477 handle = READ_ONCE(trackp->handle);
4478 if (handle) {
4479 nr_entries = stack_depot_fetch(handle, &entries);
4480 for (i = 0; i < KS_ADDRS_COUNT && i < nr_entries; i++)
4481 kpp->kp_stack[i] = (void *)entries[i];
4482 }
78869146 4483
5cf909c5
OG
4484 trackp = get_track(s, objp, TRACK_FREE);
4485 handle = READ_ONCE(trackp->handle);
4486 if (handle) {
4487 nr_entries = stack_depot_fetch(handle, &entries);
4488 for (i = 0; i < KS_ADDRS_COUNT && i < nr_entries; i++)
4489 kpp->kp_free_stack[i] = (void *)entries[i];
4490 }
e548eaa1 4491 }
8e7f37f2
PM
4492#endif
4493#endif
4494}
5bb1bb35 4495#endif
8e7f37f2 4496
81819f0f
CL
4497/********************************************************************
4498 * Kmalloc subsystem
4499 *******************************************************************/
4500
81819f0f
CL
4501static int __init setup_slub_min_order(char *str)
4502{
19af27af 4503 get_option(&str, (int *)&slub_min_order);
81819f0f
CL
4504
4505 return 1;
4506}
4507
4508__setup("slub_min_order=", setup_slub_min_order);
4509
4510static int __init setup_slub_max_order(char *str)
4511{
19af27af
AD
4512 get_option(&str, (int *)&slub_max_order);
4513 slub_max_order = min(slub_max_order, (unsigned int)MAX_ORDER - 1);
81819f0f
CL
4514
4515 return 1;
4516}
4517
4518__setup("slub_max_order=", setup_slub_max_order);
4519
4520static int __init setup_slub_min_objects(char *str)
4521{
19af27af 4522 get_option(&str, (int *)&slub_min_objects);
81819f0f
CL
4523
4524 return 1;
4525}
4526
4527__setup("slub_min_objects=", setup_slub_min_objects);
4528
ed18adc1
KC
4529#ifdef CONFIG_HARDENED_USERCOPY
4530/*
afcc90f8
KC
4531 * Rejects incorrectly sized objects and objects that are to be copied
4532 * to/from userspace but do not fall entirely within the containing slab
4533 * cache's usercopy region.
ed18adc1
KC
4534 *
4535 * Returns NULL if check passes, otherwise const char * to name of cache
4536 * to indicate an error.
4537 */
0b3eb091
MWO
4538void __check_heap_object(const void *ptr, unsigned long n,
4539 const struct slab *slab, bool to_user)
ed18adc1
KC
4540{
4541 struct kmem_cache *s;
44065b2e 4542 unsigned int offset;
b89fb5ef 4543 bool is_kfence = is_kfence_address(ptr);
ed18adc1 4544
96fedce2
AK
4545 ptr = kasan_reset_tag(ptr);
4546
ed18adc1 4547 /* Find object and usable object size. */
0b3eb091 4548 s = slab->slab_cache;
ed18adc1
KC
4549
4550 /* Reject impossible pointers. */
0b3eb091 4551 if (ptr < slab_address(slab))
f4e6e289
KC
4552 usercopy_abort("SLUB object not in SLUB page?!", NULL,
4553 to_user, 0, n);
ed18adc1
KC
4554
4555 /* Find offset within object. */
b89fb5ef
AP
4556 if (is_kfence)
4557 offset = ptr - kfence_object_start(ptr);
4558 else
0b3eb091 4559 offset = (ptr - slab_address(slab)) % s->size;
ed18adc1
KC
4560
4561 /* Adjust for redzone and reject if within the redzone. */
b89fb5ef 4562 if (!is_kfence && kmem_cache_debug_flags(s, SLAB_RED_ZONE)) {
ed18adc1 4563 if (offset < s->red_left_pad)
f4e6e289
KC
4564 usercopy_abort("SLUB object in left red zone",
4565 s->name, to_user, offset, n);
ed18adc1
KC
4566 offset -= s->red_left_pad;
4567 }
4568
afcc90f8
KC
4569 /* Allow address range falling entirely within usercopy region. */
4570 if (offset >= s->useroffset &&
4571 offset - s->useroffset <= s->usersize &&
4572 n <= s->useroffset - offset + s->usersize)
f4e6e289 4573 return;
ed18adc1 4574
f4e6e289 4575 usercopy_abort("SLUB object", s->name, to_user, offset, n);
ed18adc1
KC
4576}
4577#endif /* CONFIG_HARDENED_USERCOPY */
4578
832f37f5
VD
4579#define SHRINK_PROMOTE_MAX 32
4580
2086d26a 4581/*
832f37f5
VD
4582 * kmem_cache_shrink discards empty slabs and promotes the slabs filled
4583 * up most to the head of the partial lists. New allocations will then
4584 * fill those up and thus they can be removed from the partial lists.
672bba3a
CL
4585 *
4586 * The slabs with the least items are placed last. This results in them
4587 * being allocated from last increasing the chance that the last objects
4588 * are freed in them.
2086d26a 4589 */
5a836bf6 4590static int __kmem_cache_do_shrink(struct kmem_cache *s)
2086d26a
CL
4591{
4592 int node;
4593 int i;
4594 struct kmem_cache_node *n;
bb192ed9
VB
4595 struct slab *slab;
4596 struct slab *t;
832f37f5
VD
4597 struct list_head discard;
4598 struct list_head promote[SHRINK_PROMOTE_MAX];
2086d26a 4599 unsigned long flags;
ce3712d7 4600 int ret = 0;
2086d26a 4601
fa45dc25 4602 for_each_kmem_cache_node(s, node, n) {
832f37f5
VD
4603 INIT_LIST_HEAD(&discard);
4604 for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
4605 INIT_LIST_HEAD(promote + i);
2086d26a
CL
4606
4607 spin_lock_irqsave(&n->list_lock, flags);
4608
4609 /*
832f37f5 4610 * Build lists of slabs to discard or promote.
2086d26a 4611 *
672bba3a 4612 * Note that concurrent frees may occur while we hold the
c2092c12 4613 * list_lock. slab->inuse here is the upper limit.
2086d26a 4614 */
bb192ed9
VB
4615 list_for_each_entry_safe(slab, t, &n->partial, slab_list) {
4616 int free = slab->objects - slab->inuse;
832f37f5 4617
c2092c12 4618 /* Do not reread slab->inuse */
832f37f5
VD
4619 barrier();
4620
4621 /* We do not keep full slabs on the list */
4622 BUG_ON(free <= 0);
4623
bb192ed9
VB
4624 if (free == slab->objects) {
4625 list_move(&slab->slab_list, &discard);
69cb8e6b 4626 n->nr_partial--;
c7323a5a 4627 dec_slabs_node(s, node, slab->objects);
832f37f5 4628 } else if (free <= SHRINK_PROMOTE_MAX)
bb192ed9 4629 list_move(&slab->slab_list, promote + free - 1);
2086d26a
CL
4630 }
4631
2086d26a 4632 /*
832f37f5
VD
4633 * Promote the slabs filled up most to the head of the
4634 * partial list.
2086d26a 4635 */
832f37f5
VD
4636 for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
4637 list_splice(promote + i, &n->partial);
2086d26a 4638
2086d26a 4639 spin_unlock_irqrestore(&n->list_lock, flags);
69cb8e6b
CL
4640
4641 /* Release empty slabs */
bb192ed9 4642 list_for_each_entry_safe(slab, t, &discard, slab_list)
c7323a5a 4643 free_slab(s, slab);
ce3712d7
VD
4644
4645 if (slabs_node(s, node))
4646 ret = 1;
2086d26a
CL
4647 }
4648
ce3712d7 4649 return ret;
2086d26a 4650}
2086d26a 4651
5a836bf6
SAS
4652int __kmem_cache_shrink(struct kmem_cache *s)
4653{
4654 flush_all(s);
4655 return __kmem_cache_do_shrink(s);
4656}
4657
b9049e23
YG
4658static int slab_mem_going_offline_callback(void *arg)
4659{
4660 struct kmem_cache *s;
4661
18004c5d 4662 mutex_lock(&slab_mutex);
5a836bf6
SAS
4663 list_for_each_entry(s, &slab_caches, list) {
4664 flush_all_cpus_locked(s);
4665 __kmem_cache_do_shrink(s);
4666 }
18004c5d 4667 mutex_unlock(&slab_mutex);
b9049e23
YG
4668
4669 return 0;
4670}
4671
4672static void slab_mem_offline_callback(void *arg)
4673{
b9049e23
YG
4674 struct memory_notify *marg = arg;
4675 int offline_node;
4676
b9d5ab25 4677 offline_node = marg->status_change_nid_normal;
b9049e23
YG
4678
4679 /*
4680 * If the node still has available memory. we need kmem_cache_node
4681 * for it yet.
4682 */
4683 if (offline_node < 0)
4684 return;
4685
18004c5d 4686 mutex_lock(&slab_mutex);
7e1fa93d 4687 node_clear(offline_node, slab_nodes);
666716fd
VB
4688 /*
4689 * We no longer free kmem_cache_node structures here, as it would be
4690 * racy with all get_node() users, and infeasible to protect them with
4691 * slab_mutex.
4692 */
18004c5d 4693 mutex_unlock(&slab_mutex);
b9049e23
YG
4694}
4695
4696static int slab_mem_going_online_callback(void *arg)
4697{
4698 struct kmem_cache_node *n;
4699 struct kmem_cache *s;
4700 struct memory_notify *marg = arg;
b9d5ab25 4701 int nid = marg->status_change_nid_normal;
b9049e23
YG
4702 int ret = 0;
4703
4704 /*
4705 * If the node's memory is already available, then kmem_cache_node is
4706 * already created. Nothing to do.
4707 */
4708 if (nid < 0)
4709 return 0;
4710
4711 /*
0121c619 4712 * We are bringing a node online. No memory is available yet. We must
b9049e23
YG
4713 * allocate a kmem_cache_node structure in order to bring the node
4714 * online.
4715 */
18004c5d 4716 mutex_lock(&slab_mutex);
b9049e23 4717 list_for_each_entry(s, &slab_caches, list) {
666716fd
VB
4718 /*
4719 * The structure may already exist if the node was previously
4720 * onlined and offlined.
4721 */
4722 if (get_node(s, nid))
4723 continue;
b9049e23
YG
4724 /*
4725 * XXX: kmem_cache_alloc_node will fallback to other nodes
4726 * since memory is not yet available from the node that
4727 * is brought up.
4728 */
8de66a0c 4729 n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
b9049e23
YG
4730 if (!n) {
4731 ret = -ENOMEM;
4732 goto out;
4733 }
4053497d 4734 init_kmem_cache_node(n);
b9049e23
YG
4735 s->node[nid] = n;
4736 }
7e1fa93d
VB
4737 /*
4738 * Any cache created after this point will also have kmem_cache_node
4739 * initialized for the new node.
4740 */
4741 node_set(nid, slab_nodes);
b9049e23 4742out:
18004c5d 4743 mutex_unlock(&slab_mutex);
b9049e23
YG
4744 return ret;
4745}
4746
4747static int slab_memory_callback(struct notifier_block *self,
4748 unsigned long action, void *arg)
4749{
4750 int ret = 0;
4751
4752 switch (action) {
4753 case MEM_GOING_ONLINE:
4754 ret = slab_mem_going_online_callback(arg);
4755 break;
4756 case MEM_GOING_OFFLINE:
4757 ret = slab_mem_going_offline_callback(arg);
4758 break;
4759 case MEM_OFFLINE:
4760 case MEM_CANCEL_ONLINE:
4761 slab_mem_offline_callback(arg);
4762 break;
4763 case MEM_ONLINE:
4764 case MEM_CANCEL_OFFLINE:
4765 break;
4766 }
dc19f9db
KH
4767 if (ret)
4768 ret = notifier_from_errno(ret);
4769 else
4770 ret = NOTIFY_OK;
b9049e23
YG
4771 return ret;
4772}
4773
3ac38faa
AM
4774static struct notifier_block slab_memory_callback_nb = {
4775 .notifier_call = slab_memory_callback,
4776 .priority = SLAB_CALLBACK_PRI,
4777};
b9049e23 4778
81819f0f
CL
4779/********************************************************************
4780 * Basic setup of slabs
4781 *******************************************************************/
4782
51df1142
CL
4783/*
4784 * Used for early kmem_cache structures that were allocated using
dffb4d60
CL
4785 * the page allocator. Allocate them properly then fix up the pointers
4786 * that may be pointing to the wrong kmem_cache structure.
51df1142
CL
4787 */
4788
dffb4d60 4789static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache)
51df1142
CL
4790{
4791 int node;
dffb4d60 4792 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
fa45dc25 4793 struct kmem_cache_node *n;
51df1142 4794
dffb4d60 4795 memcpy(s, static_cache, kmem_cache->object_size);
51df1142 4796
7d557b3c
GC
4797 /*
4798 * This runs very early, and only the boot processor is supposed to be
4799 * up. Even if it weren't true, IRQs are not up so we couldn't fire
4800 * IPIs around.
4801 */
4802 __flush_cpu_slab(s, smp_processor_id());
fa45dc25 4803 for_each_kmem_cache_node(s, node, n) {
bb192ed9 4804 struct slab *p;
51df1142 4805
916ac052 4806 list_for_each_entry(p, &n->partial, slab_list)
fa45dc25 4807 p->slab_cache = s;
51df1142 4808
607bf324 4809#ifdef CONFIG_SLUB_DEBUG
916ac052 4810 list_for_each_entry(p, &n->full, slab_list)
fa45dc25 4811 p->slab_cache = s;
51df1142 4812#endif
51df1142 4813 }
dffb4d60
CL
4814 list_add(&s->list, &slab_caches);
4815 return s;
51df1142
CL
4816}
4817
81819f0f
CL
4818void __init kmem_cache_init(void)
4819{
dffb4d60
CL
4820 static __initdata struct kmem_cache boot_kmem_cache,
4821 boot_kmem_cache_node;
7e1fa93d 4822 int node;
51df1142 4823
fc8d8620
SG
4824 if (debug_guardpage_minorder())
4825 slub_max_order = 0;
4826
79270291
SB
4827 /* Print slub debugging pointers without hashing */
4828 if (__slub_debug_enabled())
4829 no_hash_pointers_enable(NULL);
4830
dffb4d60
CL
4831 kmem_cache_node = &boot_kmem_cache_node;
4832 kmem_cache = &boot_kmem_cache;
51df1142 4833
7e1fa93d
VB
4834 /*
4835 * Initialize the nodemask for which we will allocate per node
4836 * structures. Here we don't need taking slab_mutex yet.
4837 */
4838 for_each_node_state(node, N_NORMAL_MEMORY)
4839 node_set(node, slab_nodes);
4840
dffb4d60 4841 create_boot_cache(kmem_cache_node, "kmem_cache_node",
8eb8284b 4842 sizeof(struct kmem_cache_node), SLAB_HWCACHE_ALIGN, 0, 0);
b9049e23 4843
3ac38faa 4844 register_hotmemory_notifier(&slab_memory_callback_nb);
81819f0f
CL
4845
4846 /* Able to allocate the per node structures */
4847 slab_state = PARTIAL;
4848
dffb4d60
CL
4849 create_boot_cache(kmem_cache, "kmem_cache",
4850 offsetof(struct kmem_cache, node) +
4851 nr_node_ids * sizeof(struct kmem_cache_node *),
8eb8284b 4852 SLAB_HWCACHE_ALIGN, 0, 0);
8a13a4cc 4853
dffb4d60 4854 kmem_cache = bootstrap(&boot_kmem_cache);
dffb4d60 4855 kmem_cache_node = bootstrap(&boot_kmem_cache_node);
51df1142
CL
4856
4857 /* Now we can use the kmem_cache to allocate kmalloc slabs */
34cc6990 4858 setup_kmalloc_cache_index_table();
f97d5f63 4859 create_kmalloc_caches(0);
81819f0f 4860
210e7a43
TG
4861 /* Setup random freelists for each cache */
4862 init_freelist_randomization();
4863
a96a87bf
SAS
4864 cpuhp_setup_state_nocalls(CPUHP_SLUB_DEAD, "slub:dead", NULL,
4865 slub_cpu_dead);
81819f0f 4866
b9726c26 4867 pr_info("SLUB: HWalign=%d, Order=%u-%u, MinObjects=%u, CPUs=%u, Nodes=%u\n",
f97d5f63 4868 cache_line_size(),
81819f0f
CL
4869 slub_min_order, slub_max_order, slub_min_objects,
4870 nr_cpu_ids, nr_node_ids);
4871}
4872
7e85ee0c
PE
4873void __init kmem_cache_init_late(void)
4874{
e45cc288
ML
4875 flushwq = alloc_workqueue("slub_flushwq", WQ_MEM_RECLAIM, 0);
4876 WARN_ON(!flushwq);
7e85ee0c
PE
4877}
4878
2633d7a0 4879struct kmem_cache *
f4957d5b 4880__kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
d50112ed 4881 slab_flags_t flags, void (*ctor)(void *))
81819f0f 4882{
10befea9 4883 struct kmem_cache *s;
81819f0f 4884
a44cb944 4885 s = find_mergeable(size, align, flags, name, ctor);
81819f0f 4886 if (s) {
efb93527
XS
4887 if (sysfs_slab_alias(s, name))
4888 return NULL;
4889
81819f0f 4890 s->refcount++;
84d0ddd6 4891
81819f0f
CL
4892 /*
4893 * Adjust the object sizes so that we clear
4894 * the complete object on kzalloc.
4895 */
1b473f29 4896 s->object_size = max(s->object_size, size);
52ee6d74 4897 s->inuse = max(s->inuse, ALIGN(size, sizeof(void *)));
a0e1d1be 4898 }
6446faa2 4899
cbb79694
CL
4900 return s;
4901}
84c1cf62 4902
d50112ed 4903int __kmem_cache_create(struct kmem_cache *s, slab_flags_t flags)
cbb79694 4904{
aac3a166
PE
4905 int err;
4906
4907 err = kmem_cache_open(s, flags);
4908 if (err)
4909 return err;
20cea968 4910
45530c44
CL
4911 /* Mutex is not taken during early boot */
4912 if (slab_state <= UP)
4913 return 0;
4914
aac3a166 4915 err = sysfs_slab_add(s);
67823a54 4916 if (err) {
52b4b950 4917 __kmem_cache_release(s);
67823a54
ML
4918 return err;
4919 }
20cea968 4920
64dd6849
FM
4921 if (s->flags & SLAB_STORE_USER)
4922 debugfs_slab_add(s);
4923
67823a54 4924 return 0;
81819f0f 4925}
81819f0f 4926
ab4d5ed5 4927#ifdef CONFIG_SYSFS
bb192ed9 4928static int count_inuse(struct slab *slab)
205ab99d 4929{
bb192ed9 4930 return slab->inuse;
205ab99d
CL
4931}
4932
bb192ed9 4933static int count_total(struct slab *slab)
205ab99d 4934{
bb192ed9 4935 return slab->objects;
205ab99d 4936}
ab4d5ed5 4937#endif
205ab99d 4938
ab4d5ed5 4939#ifdef CONFIG_SLUB_DEBUG
bb192ed9 4940static void validate_slab(struct kmem_cache *s, struct slab *slab,
0a19e7dd 4941 unsigned long *obj_map)
53e15af0
CL
4942{
4943 void *p;
bb192ed9 4944 void *addr = slab_address(slab);
53e15af0 4945
bb192ed9 4946 if (!check_slab(s, slab) || !on_freelist(s, slab, NULL))
41bec7c3 4947 return;
53e15af0
CL
4948
4949 /* Now we know that a valid freelist exists */
bb192ed9
VB
4950 __fill_map(obj_map, s, slab);
4951 for_each_object(p, s, addr, slab->objects) {
0a19e7dd 4952 u8 val = test_bit(__obj_to_index(s, addr, p), obj_map) ?
dd98afd4 4953 SLUB_RED_INACTIVE : SLUB_RED_ACTIVE;
53e15af0 4954
bb192ed9 4955 if (!check_object(s, slab, p, val))
dd98afd4
YZ
4956 break;
4957 }
53e15af0
CL
4958}
4959
434e245d 4960static int validate_slab_node(struct kmem_cache *s,
0a19e7dd 4961 struct kmem_cache_node *n, unsigned long *obj_map)
53e15af0
CL
4962{
4963 unsigned long count = 0;
bb192ed9 4964 struct slab *slab;
53e15af0
CL
4965 unsigned long flags;
4966
4967 spin_lock_irqsave(&n->list_lock, flags);
4968
bb192ed9
VB
4969 list_for_each_entry(slab, &n->partial, slab_list) {
4970 validate_slab(s, slab, obj_map);
53e15af0
CL
4971 count++;
4972 }
1f9f78b1 4973 if (count != n->nr_partial) {
f9f58285
FF
4974 pr_err("SLUB %s: %ld partial slabs counted but counter=%ld\n",
4975 s->name, count, n->nr_partial);
1f9f78b1
OG
4976 slab_add_kunit_errors();
4977 }
53e15af0
CL
4978
4979 if (!(s->flags & SLAB_STORE_USER))
4980 goto out;
4981
bb192ed9
VB
4982 list_for_each_entry(slab, &n->full, slab_list) {
4983 validate_slab(s, slab, obj_map);
53e15af0
CL
4984 count++;
4985 }
1f9f78b1 4986 if (count != atomic_long_read(&n->nr_slabs)) {
f9f58285
FF
4987 pr_err("SLUB: %s %ld slabs counted but counter=%ld\n",
4988 s->name, count, atomic_long_read(&n->nr_slabs));
1f9f78b1
OG
4989 slab_add_kunit_errors();
4990 }
53e15af0
CL
4991
4992out:
4993 spin_unlock_irqrestore(&n->list_lock, flags);
4994 return count;
4995}
4996
1f9f78b1 4997long validate_slab_cache(struct kmem_cache *s)
53e15af0
CL
4998{
4999 int node;
5000 unsigned long count = 0;
fa45dc25 5001 struct kmem_cache_node *n;
0a19e7dd
VB
5002 unsigned long *obj_map;
5003
5004 obj_map = bitmap_alloc(oo_objects(s->oo), GFP_KERNEL);
5005 if (!obj_map)
5006 return -ENOMEM;
53e15af0
CL
5007
5008 flush_all(s);
fa45dc25 5009 for_each_kmem_cache_node(s, node, n)
0a19e7dd
VB
5010 count += validate_slab_node(s, n, obj_map);
5011
5012 bitmap_free(obj_map);
90e9f6a6 5013
53e15af0
CL
5014 return count;
5015}
1f9f78b1
OG
5016EXPORT_SYMBOL(validate_slab_cache);
5017
64dd6849 5018#ifdef CONFIG_DEBUG_FS
88a420e4 5019/*
672bba3a 5020 * Generate lists of code addresses where slabcache objects are allocated
88a420e4
CL
5021 * and freed.
5022 */
5023
5024struct location {
8ea9fb92 5025 depot_stack_handle_t handle;
88a420e4 5026 unsigned long count;
ce71e27c 5027 unsigned long addr;
6edf2576 5028 unsigned long waste;
45edfa58
CL
5029 long long sum_time;
5030 long min_time;
5031 long max_time;
5032 long min_pid;
5033 long max_pid;
174596a0 5034 DECLARE_BITMAP(cpus, NR_CPUS);
45edfa58 5035 nodemask_t nodes;
88a420e4
CL
5036};
5037
5038struct loc_track {
5039 unsigned long max;
5040 unsigned long count;
5041 struct location *loc;
005a79e5 5042 loff_t idx;
88a420e4
CL
5043};
5044
64dd6849
FM
5045static struct dentry *slab_debugfs_root;
5046
88a420e4
CL
5047static void free_loc_track(struct loc_track *t)
5048{
5049 if (t->max)
5050 free_pages((unsigned long)t->loc,
5051 get_order(sizeof(struct location) * t->max));
5052}
5053
68dff6a9 5054static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
88a420e4
CL
5055{
5056 struct location *l;
5057 int order;
5058
88a420e4
CL
5059 order = get_order(sizeof(struct location) * max);
5060
68dff6a9 5061 l = (void *)__get_free_pages(flags, order);
88a420e4
CL
5062 if (!l)
5063 return 0;
5064
5065 if (t->count) {
5066 memcpy(l, t->loc, sizeof(struct location) * t->count);
5067 free_loc_track(t);
5068 }
5069 t->max = max;
5070 t->loc = l;
5071 return 1;
5072}
5073
5074static int add_location(struct loc_track *t, struct kmem_cache *s,
6edf2576
FT
5075 const struct track *track,
5076 unsigned int orig_size)
88a420e4
CL
5077{
5078 long start, end, pos;
5079 struct location *l;
6edf2576 5080 unsigned long caddr, chandle, cwaste;
45edfa58 5081 unsigned long age = jiffies - track->when;
8ea9fb92 5082 depot_stack_handle_t handle = 0;
6edf2576 5083 unsigned int waste = s->object_size - orig_size;
88a420e4 5084
8ea9fb92
OG
5085#ifdef CONFIG_STACKDEPOT
5086 handle = READ_ONCE(track->handle);
5087#endif
88a420e4
CL
5088 start = -1;
5089 end = t->count;
5090
5091 for ( ; ; ) {
5092 pos = start + (end - start + 1) / 2;
5093
5094 /*
5095 * There is nothing at "end". If we end up there
5096 * we need to add something to before end.
5097 */
5098 if (pos == end)
5099 break;
5100
6edf2576
FT
5101 l = &t->loc[pos];
5102 caddr = l->addr;
5103 chandle = l->handle;
5104 cwaste = l->waste;
5105 if ((track->addr == caddr) && (handle == chandle) &&
5106 (waste == cwaste)) {
45edfa58 5107
45edfa58
CL
5108 l->count++;
5109 if (track->when) {
5110 l->sum_time += age;
5111 if (age < l->min_time)
5112 l->min_time = age;
5113 if (age > l->max_time)
5114 l->max_time = age;
5115
5116 if (track->pid < l->min_pid)
5117 l->min_pid = track->pid;
5118 if (track->pid > l->max_pid)
5119 l->max_pid = track->pid;
5120
174596a0
RR
5121 cpumask_set_cpu(track->cpu,
5122 to_cpumask(l->cpus));
45edfa58
CL
5123 }
5124 node_set(page_to_nid(virt_to_page(track)), l->nodes);
88a420e4
CL
5125 return 1;
5126 }
5127
45edfa58 5128 if (track->addr < caddr)
88a420e4 5129 end = pos;
8ea9fb92
OG
5130 else if (track->addr == caddr && handle < chandle)
5131 end = pos;
6edf2576
FT
5132 else if (track->addr == caddr && handle == chandle &&
5133 waste < cwaste)
5134 end = pos;
88a420e4
CL
5135 else
5136 start = pos;
5137 }
5138
5139 /*
672bba3a 5140 * Not found. Insert new tracking element.
88a420e4 5141 */
68dff6a9 5142 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
88a420e4
CL
5143 return 0;
5144
5145 l = t->loc + pos;
5146 if (pos < t->count)
5147 memmove(l + 1, l,
5148 (t->count - pos) * sizeof(struct location));
5149 t->count++;
5150 l->count = 1;
45edfa58
CL
5151 l->addr = track->addr;
5152 l->sum_time = age;
5153 l->min_time = age;
5154 l->max_time = age;
5155 l->min_pid = track->pid;
5156 l->max_pid = track->pid;
8ea9fb92 5157 l->handle = handle;
6edf2576 5158 l->waste = waste;
174596a0
RR
5159 cpumask_clear(to_cpumask(l->cpus));
5160 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
45edfa58
CL
5161 nodes_clear(l->nodes);
5162 node_set(page_to_nid(virt_to_page(track)), l->nodes);
88a420e4
CL
5163 return 1;
5164}
5165
5166static void process_slab(struct loc_track *t, struct kmem_cache *s,
bb192ed9 5167 struct slab *slab, enum track_item alloc,
b3fd64e1 5168 unsigned long *obj_map)
88a420e4 5169{
bb192ed9 5170 void *addr = slab_address(slab);
6edf2576 5171 bool is_alloc = (alloc == TRACK_ALLOC);
88a420e4
CL
5172 void *p;
5173
bb192ed9 5174 __fill_map(obj_map, s, slab);
b3fd64e1 5175
bb192ed9 5176 for_each_object(p, s, addr, slab->objects)
b3fd64e1 5177 if (!test_bit(__obj_to_index(s, addr, p), obj_map))
6edf2576
FT
5178 add_location(t, s, get_track(s, p, alloc),
5179 is_alloc ? get_orig_size(s, p) :
5180 s->object_size);
88a420e4 5181}
64dd6849 5182#endif /* CONFIG_DEBUG_FS */
6dfd1b65 5183#endif /* CONFIG_SLUB_DEBUG */
88a420e4 5184
ab4d5ed5 5185#ifdef CONFIG_SYSFS
81819f0f 5186enum slab_stat_type {
205ab99d
CL
5187 SL_ALL, /* All slabs */
5188 SL_PARTIAL, /* Only partially allocated slabs */
5189 SL_CPU, /* Only slabs used for cpu caches */
5190 SL_OBJECTS, /* Determine allocated objects not slabs */
5191 SL_TOTAL /* Determine object capacity not slabs */
81819f0f
CL
5192};
5193
205ab99d 5194#define SO_ALL (1 << SL_ALL)
81819f0f
CL
5195#define SO_PARTIAL (1 << SL_PARTIAL)
5196#define SO_CPU (1 << SL_CPU)
5197#define SO_OBJECTS (1 << SL_OBJECTS)
205ab99d 5198#define SO_TOTAL (1 << SL_TOTAL)
81819f0f 5199
62e5c4b4 5200static ssize_t show_slab_objects(struct kmem_cache *s,
bf16d19a 5201 char *buf, unsigned long flags)
81819f0f
CL
5202{
5203 unsigned long total = 0;
81819f0f
CL
5204 int node;
5205 int x;
5206 unsigned long *nodes;
bf16d19a 5207 int len = 0;
81819f0f 5208
6396bb22 5209 nodes = kcalloc(nr_node_ids, sizeof(unsigned long), GFP_KERNEL);
62e5c4b4
CG
5210 if (!nodes)
5211 return -ENOMEM;
81819f0f 5212
205ab99d
CL
5213 if (flags & SO_CPU) {
5214 int cpu;
81819f0f 5215
205ab99d 5216 for_each_possible_cpu(cpu) {
d0e0ac97
CG
5217 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab,
5218 cpu);
ec3ab083 5219 int node;
bb192ed9 5220 struct slab *slab;
dfb4f096 5221
bb192ed9
VB
5222 slab = READ_ONCE(c->slab);
5223 if (!slab)
ec3ab083 5224 continue;
205ab99d 5225
bb192ed9 5226 node = slab_nid(slab);
ec3ab083 5227 if (flags & SO_TOTAL)
bb192ed9 5228 x = slab->objects;
ec3ab083 5229 else if (flags & SO_OBJECTS)
bb192ed9 5230 x = slab->inuse;
ec3ab083
CL
5231 else
5232 x = 1;
49e22585 5233
ec3ab083
CL
5234 total += x;
5235 nodes[node] += x;
5236
9c01e9af 5237#ifdef CONFIG_SLUB_CPU_PARTIAL
bb192ed9
VB
5238 slab = slub_percpu_partial_read_once(c);
5239 if (slab) {
5240 node = slab_nid(slab);
8afb1474
LZ
5241 if (flags & SO_TOTAL)
5242 WARN_ON_ONCE(1);
5243 else if (flags & SO_OBJECTS)
5244 WARN_ON_ONCE(1);
5245 else
bb192ed9 5246 x = slab->slabs;
bc6697d8
ED
5247 total += x;
5248 nodes[node] += x;
49e22585 5249 }
9c01e9af 5250#endif
81819f0f
CL
5251 }
5252 }
5253
e4f8e513
QC
5254 /*
5255 * It is impossible to take "mem_hotplug_lock" here with "kernfs_mutex"
5256 * already held which will conflict with an existing lock order:
5257 *
5258 * mem_hotplug_lock->slab_mutex->kernfs_mutex
5259 *
5260 * We don't really need mem_hotplug_lock (to hold off
5261 * slab_mem_going_offline_callback) here because slab's memory hot
5262 * unplug code doesn't destroy the kmem_cache->node[] data.
5263 */
5264
ab4d5ed5 5265#ifdef CONFIG_SLUB_DEBUG
205ab99d 5266 if (flags & SO_ALL) {
fa45dc25
CL
5267 struct kmem_cache_node *n;
5268
5269 for_each_kmem_cache_node(s, node, n) {
205ab99d 5270
d0e0ac97
CG
5271 if (flags & SO_TOTAL)
5272 x = atomic_long_read(&n->total_objects);
5273 else if (flags & SO_OBJECTS)
5274 x = atomic_long_read(&n->total_objects) -
5275 count_partial(n, count_free);
81819f0f 5276 else
205ab99d 5277 x = atomic_long_read(&n->nr_slabs);
81819f0f
CL
5278 total += x;
5279 nodes[node] += x;
5280 }
5281
ab4d5ed5
CL
5282 } else
5283#endif
5284 if (flags & SO_PARTIAL) {
fa45dc25 5285 struct kmem_cache_node *n;
81819f0f 5286
fa45dc25 5287 for_each_kmem_cache_node(s, node, n) {
205ab99d
CL
5288 if (flags & SO_TOTAL)
5289 x = count_partial(n, count_total);
5290 else if (flags & SO_OBJECTS)
5291 x = count_partial(n, count_inuse);
81819f0f 5292 else
205ab99d 5293 x = n->nr_partial;
81819f0f
CL
5294 total += x;
5295 nodes[node] += x;
5296 }
5297 }
bf16d19a
JP
5298
5299 len += sysfs_emit_at(buf, len, "%lu", total);
81819f0f 5300#ifdef CONFIG_NUMA
bf16d19a 5301 for (node = 0; node < nr_node_ids; node++) {
81819f0f 5302 if (nodes[node])
bf16d19a
JP
5303 len += sysfs_emit_at(buf, len, " N%d=%lu",
5304 node, nodes[node]);
5305 }
81819f0f 5306#endif
bf16d19a 5307 len += sysfs_emit_at(buf, len, "\n");
81819f0f 5308 kfree(nodes);
bf16d19a
JP
5309
5310 return len;
81819f0f
CL
5311}
5312
81819f0f 5313#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
497888cf 5314#define to_slab(n) container_of(n, struct kmem_cache, kobj)
81819f0f
CL
5315
5316struct slab_attribute {
5317 struct attribute attr;
5318 ssize_t (*show)(struct kmem_cache *s, char *buf);
5319 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
5320};
5321
5322#define SLAB_ATTR_RO(_name) \
d1d28bd9 5323 static struct slab_attribute _name##_attr = __ATTR_RO_MODE(_name, 0400)
81819f0f
CL
5324
5325#define SLAB_ATTR(_name) \
d1d28bd9 5326 static struct slab_attribute _name##_attr = __ATTR_RW_MODE(_name, 0600)
81819f0f 5327
81819f0f
CL
5328static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
5329{
bf16d19a 5330 return sysfs_emit(buf, "%u\n", s->size);
81819f0f
CL
5331}
5332SLAB_ATTR_RO(slab_size);
5333
5334static ssize_t align_show(struct kmem_cache *s, char *buf)
5335{
bf16d19a 5336 return sysfs_emit(buf, "%u\n", s->align);
81819f0f
CL
5337}
5338SLAB_ATTR_RO(align);
5339
5340static ssize_t object_size_show(struct kmem_cache *s, char *buf)
5341{
bf16d19a 5342 return sysfs_emit(buf, "%u\n", s->object_size);
81819f0f
CL
5343}
5344SLAB_ATTR_RO(object_size);
5345
5346static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
5347{
bf16d19a 5348 return sysfs_emit(buf, "%u\n", oo_objects(s->oo));
81819f0f
CL
5349}
5350SLAB_ATTR_RO(objs_per_slab);
5351
5352static ssize_t order_show(struct kmem_cache *s, char *buf)
5353{
bf16d19a 5354 return sysfs_emit(buf, "%u\n", oo_order(s->oo));
81819f0f 5355}
32a6f409 5356SLAB_ATTR_RO(order);
81819f0f 5357
73d342b1
DR
5358static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
5359{
bf16d19a 5360 return sysfs_emit(buf, "%lu\n", s->min_partial);
73d342b1
DR
5361}
5362
5363static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
5364 size_t length)
5365{
5366 unsigned long min;
5367 int err;
5368
3dbb95f7 5369 err = kstrtoul(buf, 10, &min);
73d342b1
DR
5370 if (err)
5371 return err;
5372
5182f3c9 5373 s->min_partial = min;
73d342b1
DR
5374 return length;
5375}
5376SLAB_ATTR(min_partial);
5377
49e22585
CL
5378static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf)
5379{
b47291ef
VB
5380 unsigned int nr_partial = 0;
5381#ifdef CONFIG_SLUB_CPU_PARTIAL
5382 nr_partial = s->cpu_partial;
5383#endif
5384
5385 return sysfs_emit(buf, "%u\n", nr_partial);
49e22585
CL
5386}
5387
5388static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf,
5389 size_t length)
5390{
e5d9998f 5391 unsigned int objects;
49e22585
CL
5392 int err;
5393
e5d9998f 5394 err = kstrtouint(buf, 10, &objects);
49e22585
CL
5395 if (err)
5396 return err;
345c905d 5397 if (objects && !kmem_cache_has_cpu_partial(s))
74ee4ef1 5398 return -EINVAL;
49e22585 5399
e6d0e1dc 5400 slub_set_cpu_partial(s, objects);
49e22585
CL
5401 flush_all(s);
5402 return length;
5403}
5404SLAB_ATTR(cpu_partial);
5405
81819f0f
CL
5406static ssize_t ctor_show(struct kmem_cache *s, char *buf)
5407{
62c70bce
JP
5408 if (!s->ctor)
5409 return 0;
bf16d19a 5410 return sysfs_emit(buf, "%pS\n", s->ctor);
81819f0f
CL
5411}
5412SLAB_ATTR_RO(ctor);
5413
81819f0f
CL
5414static ssize_t aliases_show(struct kmem_cache *s, char *buf)
5415{
bf16d19a 5416 return sysfs_emit(buf, "%d\n", s->refcount < 0 ? 0 : s->refcount - 1);
81819f0f
CL
5417}
5418SLAB_ATTR_RO(aliases);
5419
81819f0f
CL
5420static ssize_t partial_show(struct kmem_cache *s, char *buf)
5421{
d9acf4b7 5422 return show_slab_objects(s, buf, SO_PARTIAL);
81819f0f
CL
5423}
5424SLAB_ATTR_RO(partial);
5425
5426static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
5427{
d9acf4b7 5428 return show_slab_objects(s, buf, SO_CPU);
81819f0f
CL
5429}
5430SLAB_ATTR_RO(cpu_slabs);
5431
5432static ssize_t objects_show(struct kmem_cache *s, char *buf)
5433{
205ab99d 5434 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
81819f0f
CL
5435}
5436SLAB_ATTR_RO(objects);
5437
205ab99d
CL
5438static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
5439{
5440 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
5441}
5442SLAB_ATTR_RO(objects_partial);
5443
49e22585
CL
5444static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf)
5445{
5446 int objects = 0;
bb192ed9 5447 int slabs = 0;
9c01e9af 5448 int cpu __maybe_unused;
bf16d19a 5449 int len = 0;
49e22585 5450
9c01e9af 5451#ifdef CONFIG_SLUB_CPU_PARTIAL
49e22585 5452 for_each_online_cpu(cpu) {
bb192ed9 5453 struct slab *slab;
a93cf07b 5454
bb192ed9 5455 slab = slub_percpu_partial(per_cpu_ptr(s->cpu_slab, cpu));
49e22585 5456
bb192ed9
VB
5457 if (slab)
5458 slabs += slab->slabs;
49e22585 5459 }
9c01e9af 5460#endif
49e22585 5461
c2092c12 5462 /* Approximate half-full slabs, see slub_set_cpu_partial() */
bb192ed9
VB
5463 objects = (slabs * oo_objects(s->oo)) / 2;
5464 len += sysfs_emit_at(buf, len, "%d(%d)", objects, slabs);
49e22585 5465
9c01e9af 5466#if defined(CONFIG_SLUB_CPU_PARTIAL) && defined(CONFIG_SMP)
49e22585 5467 for_each_online_cpu(cpu) {
bb192ed9 5468 struct slab *slab;
a93cf07b 5469
bb192ed9
VB
5470 slab = slub_percpu_partial(per_cpu_ptr(s->cpu_slab, cpu));
5471 if (slab) {
5472 slabs = READ_ONCE(slab->slabs);
5473 objects = (slabs * oo_objects(s->oo)) / 2;
bf16d19a 5474 len += sysfs_emit_at(buf, len, " C%d=%d(%d)",
bb192ed9 5475 cpu, objects, slabs);
b47291ef 5476 }
49e22585
CL
5477 }
5478#endif
bf16d19a
JP
5479 len += sysfs_emit_at(buf, len, "\n");
5480
5481 return len;
49e22585
CL
5482}
5483SLAB_ATTR_RO(slabs_cpu_partial);
5484
a5a84755
CL
5485static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
5486{
bf16d19a 5487 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
a5a84755 5488}
8f58119a 5489SLAB_ATTR_RO(reclaim_account);
a5a84755
CL
5490
5491static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
5492{
bf16d19a 5493 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
a5a84755
CL
5494}
5495SLAB_ATTR_RO(hwcache_align);
5496
5497#ifdef CONFIG_ZONE_DMA
5498static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
5499{
bf16d19a 5500 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
a5a84755
CL
5501}
5502SLAB_ATTR_RO(cache_dma);
5503#endif
5504
8eb8284b
DW
5505static ssize_t usersize_show(struct kmem_cache *s, char *buf)
5506{
bf16d19a 5507 return sysfs_emit(buf, "%u\n", s->usersize);
8eb8284b
DW
5508}
5509SLAB_ATTR_RO(usersize);
5510
a5a84755
CL
5511static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
5512{
bf16d19a 5513 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_TYPESAFE_BY_RCU));
a5a84755
CL
5514}
5515SLAB_ATTR_RO(destroy_by_rcu);
5516
ab4d5ed5 5517#ifdef CONFIG_SLUB_DEBUG
a5a84755
CL
5518static ssize_t slabs_show(struct kmem_cache *s, char *buf)
5519{
5520 return show_slab_objects(s, buf, SO_ALL);
5521}
5522SLAB_ATTR_RO(slabs);
5523
205ab99d
CL
5524static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
5525{
5526 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
5527}
5528SLAB_ATTR_RO(total_objects);
5529
81819f0f
CL
5530static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
5531{
bf16d19a 5532 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_CONSISTENCY_CHECKS));
81819f0f 5533}
060807f8 5534SLAB_ATTR_RO(sanity_checks);
81819f0f
CL
5535
5536static ssize_t trace_show(struct kmem_cache *s, char *buf)
5537{
bf16d19a 5538 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_TRACE));
81819f0f 5539}
060807f8 5540SLAB_ATTR_RO(trace);
81819f0f 5541
81819f0f
CL
5542static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
5543{
bf16d19a 5544 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
81819f0f
CL
5545}
5546
ad38b5b1 5547SLAB_ATTR_RO(red_zone);
81819f0f
CL
5548
5549static ssize_t poison_show(struct kmem_cache *s, char *buf)
5550{
bf16d19a 5551 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_POISON));
81819f0f
CL
5552}
5553
ad38b5b1 5554SLAB_ATTR_RO(poison);
81819f0f
CL
5555
5556static ssize_t store_user_show(struct kmem_cache *s, char *buf)
5557{
bf16d19a 5558 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
81819f0f
CL
5559}
5560
ad38b5b1 5561SLAB_ATTR_RO(store_user);
81819f0f 5562
53e15af0
CL
5563static ssize_t validate_show(struct kmem_cache *s, char *buf)
5564{
5565 return 0;
5566}
5567
5568static ssize_t validate_store(struct kmem_cache *s,
5569 const char *buf, size_t length)
5570{
434e245d
CL
5571 int ret = -EINVAL;
5572
c7323a5a 5573 if (buf[0] == '1' && kmem_cache_debug(s)) {
434e245d
CL
5574 ret = validate_slab_cache(s);
5575 if (ret >= 0)
5576 ret = length;
5577 }
5578 return ret;
53e15af0
CL
5579}
5580SLAB_ATTR(validate);
a5a84755 5581
a5a84755
CL
5582#endif /* CONFIG_SLUB_DEBUG */
5583
5584#ifdef CONFIG_FAILSLAB
5585static ssize_t failslab_show(struct kmem_cache *s, char *buf)
5586{
bf16d19a 5587 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
a5a84755 5588}
060807f8 5589SLAB_ATTR_RO(failslab);
ab4d5ed5 5590#endif
53e15af0 5591
2086d26a
CL
5592static ssize_t shrink_show(struct kmem_cache *s, char *buf)
5593{
5594 return 0;
5595}
5596
5597static ssize_t shrink_store(struct kmem_cache *s,
5598 const char *buf, size_t length)
5599{
832f37f5 5600 if (buf[0] == '1')
10befea9 5601 kmem_cache_shrink(s);
832f37f5 5602 else
2086d26a
CL
5603 return -EINVAL;
5604 return length;
5605}
5606SLAB_ATTR(shrink);
5607
81819f0f 5608#ifdef CONFIG_NUMA
9824601e 5609static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
81819f0f 5610{
bf16d19a 5611 return sysfs_emit(buf, "%u\n", s->remote_node_defrag_ratio / 10);
81819f0f
CL
5612}
5613
9824601e 5614static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
81819f0f
CL
5615 const char *buf, size_t length)
5616{
eb7235eb 5617 unsigned int ratio;
0121c619
CL
5618 int err;
5619
eb7235eb 5620 err = kstrtouint(buf, 10, &ratio);
0121c619
CL
5621 if (err)
5622 return err;
eb7235eb
AD
5623 if (ratio > 100)
5624 return -ERANGE;
0121c619 5625
eb7235eb 5626 s->remote_node_defrag_ratio = ratio * 10;
81819f0f 5627
81819f0f
CL
5628 return length;
5629}
9824601e 5630SLAB_ATTR(remote_node_defrag_ratio);
81819f0f
CL
5631#endif
5632
8ff12cfc 5633#ifdef CONFIG_SLUB_STATS
8ff12cfc
CL
5634static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
5635{
5636 unsigned long sum = 0;
5637 int cpu;
bf16d19a 5638 int len = 0;
6da2ec56 5639 int *data = kmalloc_array(nr_cpu_ids, sizeof(int), GFP_KERNEL);
8ff12cfc
CL
5640
5641 if (!data)
5642 return -ENOMEM;
5643
5644 for_each_online_cpu(cpu) {
9dfc6e68 5645 unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
8ff12cfc
CL
5646
5647 data[cpu] = x;
5648 sum += x;
5649 }
5650
bf16d19a 5651 len += sysfs_emit_at(buf, len, "%lu", sum);
8ff12cfc 5652
50ef37b9 5653#ifdef CONFIG_SMP
8ff12cfc 5654 for_each_online_cpu(cpu) {
bf16d19a
JP
5655 if (data[cpu])
5656 len += sysfs_emit_at(buf, len, " C%d=%u",
5657 cpu, data[cpu]);
8ff12cfc 5658 }
50ef37b9 5659#endif
8ff12cfc 5660 kfree(data);
bf16d19a
JP
5661 len += sysfs_emit_at(buf, len, "\n");
5662
5663 return len;
8ff12cfc
CL
5664}
5665
78eb00cc
DR
5666static void clear_stat(struct kmem_cache *s, enum stat_item si)
5667{
5668 int cpu;
5669
5670 for_each_online_cpu(cpu)
9dfc6e68 5671 per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
78eb00cc
DR
5672}
5673
8ff12cfc
CL
5674#define STAT_ATTR(si, text) \
5675static ssize_t text##_show(struct kmem_cache *s, char *buf) \
5676{ \
5677 return show_stat(s, buf, si); \
5678} \
78eb00cc
DR
5679static ssize_t text##_store(struct kmem_cache *s, \
5680 const char *buf, size_t length) \
5681{ \
5682 if (buf[0] != '0') \
5683 return -EINVAL; \
5684 clear_stat(s, si); \
5685 return length; \
5686} \
5687SLAB_ATTR(text); \
8ff12cfc
CL
5688
5689STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
5690STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
5691STAT_ATTR(FREE_FASTPATH, free_fastpath);
5692STAT_ATTR(FREE_SLOWPATH, free_slowpath);
5693STAT_ATTR(FREE_FROZEN, free_frozen);
5694STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
5695STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
5696STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
5697STAT_ATTR(ALLOC_SLAB, alloc_slab);
5698STAT_ATTR(ALLOC_REFILL, alloc_refill);
e36a2652 5699STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
8ff12cfc
CL
5700STAT_ATTR(FREE_SLAB, free_slab);
5701STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
5702STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
5703STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
5704STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
5705STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
5706STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
03e404af 5707STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass);
65c3376a 5708STAT_ATTR(ORDER_FALLBACK, order_fallback);
b789ef51
CL
5709STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
5710STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
49e22585
CL
5711STAT_ATTR(CPU_PARTIAL_ALLOC, cpu_partial_alloc);
5712STAT_ATTR(CPU_PARTIAL_FREE, cpu_partial_free);
8028dcea
AS
5713STAT_ATTR(CPU_PARTIAL_NODE, cpu_partial_node);
5714STAT_ATTR(CPU_PARTIAL_DRAIN, cpu_partial_drain);
6dfd1b65 5715#endif /* CONFIG_SLUB_STATS */
8ff12cfc 5716
b84e04f1
IK
5717#ifdef CONFIG_KFENCE
5718static ssize_t skip_kfence_show(struct kmem_cache *s, char *buf)
5719{
5720 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_SKIP_KFENCE));
5721}
5722
5723static ssize_t skip_kfence_store(struct kmem_cache *s,
5724 const char *buf, size_t length)
5725{
5726 int ret = length;
5727
5728 if (buf[0] == '0')
5729 s->flags &= ~SLAB_SKIP_KFENCE;
5730 else if (buf[0] == '1')
5731 s->flags |= SLAB_SKIP_KFENCE;
5732 else
5733 ret = -EINVAL;
5734
5735 return ret;
5736}
5737SLAB_ATTR(skip_kfence);
5738#endif
5739
06428780 5740static struct attribute *slab_attrs[] = {
81819f0f
CL
5741 &slab_size_attr.attr,
5742 &object_size_attr.attr,
5743 &objs_per_slab_attr.attr,
5744 &order_attr.attr,
73d342b1 5745 &min_partial_attr.attr,
49e22585 5746 &cpu_partial_attr.attr,
81819f0f 5747 &objects_attr.attr,
205ab99d 5748 &objects_partial_attr.attr,
81819f0f
CL
5749 &partial_attr.attr,
5750 &cpu_slabs_attr.attr,
5751 &ctor_attr.attr,
81819f0f
CL
5752 &aliases_attr.attr,
5753 &align_attr.attr,
81819f0f
CL
5754 &hwcache_align_attr.attr,
5755 &reclaim_account_attr.attr,
5756 &destroy_by_rcu_attr.attr,
a5a84755 5757 &shrink_attr.attr,
49e22585 5758 &slabs_cpu_partial_attr.attr,
ab4d5ed5 5759#ifdef CONFIG_SLUB_DEBUG
a5a84755
CL
5760 &total_objects_attr.attr,
5761 &slabs_attr.attr,
5762 &sanity_checks_attr.attr,
5763 &trace_attr.attr,
81819f0f
CL
5764 &red_zone_attr.attr,
5765 &poison_attr.attr,
5766 &store_user_attr.attr,
53e15af0 5767 &validate_attr.attr,
ab4d5ed5 5768#endif
81819f0f
CL
5769#ifdef CONFIG_ZONE_DMA
5770 &cache_dma_attr.attr,
5771#endif
5772#ifdef CONFIG_NUMA
9824601e 5773 &remote_node_defrag_ratio_attr.attr,
8ff12cfc
CL
5774#endif
5775#ifdef CONFIG_SLUB_STATS
5776 &alloc_fastpath_attr.attr,
5777 &alloc_slowpath_attr.attr,
5778 &free_fastpath_attr.attr,
5779 &free_slowpath_attr.attr,
5780 &free_frozen_attr.attr,
5781 &free_add_partial_attr.attr,
5782 &free_remove_partial_attr.attr,
5783 &alloc_from_partial_attr.attr,
5784 &alloc_slab_attr.attr,
5785 &alloc_refill_attr.attr,
e36a2652 5786 &alloc_node_mismatch_attr.attr,
8ff12cfc
CL
5787 &free_slab_attr.attr,
5788 &cpuslab_flush_attr.attr,
5789 &deactivate_full_attr.attr,
5790 &deactivate_empty_attr.attr,
5791 &deactivate_to_head_attr.attr,
5792 &deactivate_to_tail_attr.attr,
5793 &deactivate_remote_frees_attr.attr,
03e404af 5794 &deactivate_bypass_attr.attr,
65c3376a 5795 &order_fallback_attr.attr,
b789ef51
CL
5796 &cmpxchg_double_fail_attr.attr,
5797 &cmpxchg_double_cpu_fail_attr.attr,
49e22585
CL
5798 &cpu_partial_alloc_attr.attr,
5799 &cpu_partial_free_attr.attr,
8028dcea
AS
5800 &cpu_partial_node_attr.attr,
5801 &cpu_partial_drain_attr.attr,
81819f0f 5802#endif
4c13dd3b
DM
5803#ifdef CONFIG_FAILSLAB
5804 &failslab_attr.attr,
5805#endif
8eb8284b 5806 &usersize_attr.attr,
b84e04f1
IK
5807#ifdef CONFIG_KFENCE
5808 &skip_kfence_attr.attr,
5809#endif
4c13dd3b 5810
81819f0f
CL
5811 NULL
5812};
5813
1fdaaa23 5814static const struct attribute_group slab_attr_group = {
81819f0f
CL
5815 .attrs = slab_attrs,
5816};
5817
5818static ssize_t slab_attr_show(struct kobject *kobj,
5819 struct attribute *attr,
5820 char *buf)
5821{
5822 struct slab_attribute *attribute;
5823 struct kmem_cache *s;
81819f0f
CL
5824
5825 attribute = to_slab_attr(attr);
5826 s = to_slab(kobj);
5827
5828 if (!attribute->show)
5829 return -EIO;
5830
2bfbb027 5831 return attribute->show(s, buf);
81819f0f
CL
5832}
5833
5834static ssize_t slab_attr_store(struct kobject *kobj,
5835 struct attribute *attr,
5836 const char *buf, size_t len)
5837{
5838 struct slab_attribute *attribute;
5839 struct kmem_cache *s;
81819f0f
CL
5840
5841 attribute = to_slab_attr(attr);
5842 s = to_slab(kobj);
5843
5844 if (!attribute->store)
5845 return -EIO;
5846
2bfbb027 5847 return attribute->store(s, buf, len);
81819f0f
CL
5848}
5849
41a21285
CL
5850static void kmem_cache_release(struct kobject *k)
5851{
5852 slab_kmem_cache_release(to_slab(k));
5853}
5854
52cf25d0 5855static const struct sysfs_ops slab_sysfs_ops = {
81819f0f
CL
5856 .show = slab_attr_show,
5857 .store = slab_attr_store,
5858};
5859
5860static struct kobj_type slab_ktype = {
5861 .sysfs_ops = &slab_sysfs_ops,
41a21285 5862 .release = kmem_cache_release,
81819f0f
CL
5863};
5864
27c3a314 5865static struct kset *slab_kset;
81819f0f 5866
9a41707b
VD
5867static inline struct kset *cache_kset(struct kmem_cache *s)
5868{
9a41707b
VD
5869 return slab_kset;
5870}
5871
d65360f2 5872#define ID_STR_LENGTH 32
81819f0f
CL
5873
5874/* Create a unique string id for a slab cache:
6446faa2
CL
5875 *
5876 * Format :[flags-]size
81819f0f
CL
5877 */
5878static char *create_unique_id(struct kmem_cache *s)
5879{
5880 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
5881 char *p = name;
5882
7e9c323c
CY
5883 if (!name)
5884 return ERR_PTR(-ENOMEM);
81819f0f
CL
5885
5886 *p++ = ':';
5887 /*
5888 * First flags affecting slabcache operations. We will only
5889 * get here for aliasable slabs so we do not need to support
5890 * too many flags. The flags here must cover all flags that
5891 * are matched during merging to guarantee that the id is
5892 * unique.
5893 */
5894 if (s->flags & SLAB_CACHE_DMA)
5895 *p++ = 'd';
6d6ea1e9
NB
5896 if (s->flags & SLAB_CACHE_DMA32)
5897 *p++ = 'D';
81819f0f
CL
5898 if (s->flags & SLAB_RECLAIM_ACCOUNT)
5899 *p++ = 'a';
becfda68 5900 if (s->flags & SLAB_CONSISTENCY_CHECKS)
81819f0f 5901 *p++ = 'F';
230e9fc2
VD
5902 if (s->flags & SLAB_ACCOUNT)
5903 *p++ = 'A';
81819f0f
CL
5904 if (p != name + 1)
5905 *p++ = '-';
d65360f2 5906 p += snprintf(p, ID_STR_LENGTH - (p - name), "%07u", s->size);
2633d7a0 5907
d65360f2
CY
5908 if (WARN_ON(p > name + ID_STR_LENGTH - 1)) {
5909 kfree(name);
5910 return ERR_PTR(-EINVAL);
5911 }
68ef169a 5912 kmsan_unpoison_memory(name, p - name);
81819f0f
CL
5913 return name;
5914}
5915
5916static int sysfs_slab_add(struct kmem_cache *s)
5917{
5918 int err;
5919 const char *name;
1663f26d 5920 struct kset *kset = cache_kset(s);
45530c44 5921 int unmergeable = slab_unmergeable(s);
81819f0f 5922
1663f26d
TH
5923 if (!kset) {
5924 kobject_init(&s->kobj, &slab_ktype);
5925 return 0;
5926 }
5927
11066386
MC
5928 if (!unmergeable && disable_higher_order_debug &&
5929 (slub_debug & DEBUG_METADATA_FLAGS))
5930 unmergeable = 1;
5931
81819f0f
CL
5932 if (unmergeable) {
5933 /*
5934 * Slabcache can never be merged so we can use the name proper.
5935 * This is typically the case for debug situations. In that
5936 * case we can catch duplicate names easily.
5937 */
27c3a314 5938 sysfs_remove_link(&slab_kset->kobj, s->name);
81819f0f
CL
5939 name = s->name;
5940 } else {
5941 /*
5942 * Create a unique name for the slab as a target
5943 * for the symlinks.
5944 */
5945 name = create_unique_id(s);
7e9c323c
CY
5946 if (IS_ERR(name))
5947 return PTR_ERR(name);
81819f0f
CL
5948 }
5949
1663f26d 5950 s->kobj.kset = kset;
26e4f205 5951 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name);
757fed1d 5952 if (err)
80da026a 5953 goto out;
81819f0f
CL
5954
5955 err = sysfs_create_group(&s->kobj, &slab_attr_group);
54b6a731
DJ
5956 if (err)
5957 goto out_del_kobj;
9a41707b 5958
81819f0f
CL
5959 if (!unmergeable) {
5960 /* Setup first alias */
5961 sysfs_slab_alias(s, s->name);
81819f0f 5962 }
54b6a731
DJ
5963out:
5964 if (!unmergeable)
5965 kfree(name);
5966 return err;
5967out_del_kobj:
5968 kobject_del(&s->kobj);
54b6a731 5969 goto out;
81819f0f
CL
5970}
5971
d50d82fa
MP
5972void sysfs_slab_unlink(struct kmem_cache *s)
5973{
5974 if (slab_state >= FULL)
5975 kobject_del(&s->kobj);
5976}
5977
bf5eb3de
TH
5978void sysfs_slab_release(struct kmem_cache *s)
5979{
5980 if (slab_state >= FULL)
5981 kobject_put(&s->kobj);
81819f0f
CL
5982}
5983
5984/*
5985 * Need to buffer aliases during bootup until sysfs becomes
9f6c708e 5986 * available lest we lose that information.
81819f0f
CL
5987 */
5988struct saved_alias {
5989 struct kmem_cache *s;
5990 const char *name;
5991 struct saved_alias *next;
5992};
5993
5af328a5 5994static struct saved_alias *alias_list;
81819f0f
CL
5995
5996static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
5997{
5998 struct saved_alias *al;
5999
97d06609 6000 if (slab_state == FULL) {
81819f0f
CL
6001 /*
6002 * If we have a leftover link then remove it.
6003 */
27c3a314
GKH
6004 sysfs_remove_link(&slab_kset->kobj, name);
6005 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
81819f0f
CL
6006 }
6007
6008 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
6009 if (!al)
6010 return -ENOMEM;
6011
6012 al->s = s;
6013 al->name = name;
6014 al->next = alias_list;
6015 alias_list = al;
68ef169a 6016 kmsan_unpoison_memory(al, sizeof(*al));
81819f0f
CL
6017 return 0;
6018}
6019
6020static int __init slab_sysfs_init(void)
6021{
5b95a4ac 6022 struct kmem_cache *s;
81819f0f
CL
6023 int err;
6024
18004c5d 6025 mutex_lock(&slab_mutex);
2bce6485 6026
d7660ce5 6027 slab_kset = kset_create_and_add("slab", NULL, kernel_kobj);
27c3a314 6028 if (!slab_kset) {
18004c5d 6029 mutex_unlock(&slab_mutex);
f9f58285 6030 pr_err("Cannot register slab subsystem.\n");
81819f0f
CL
6031 return -ENOSYS;
6032 }
6033
97d06609 6034 slab_state = FULL;
26a7bd03 6035
5b95a4ac 6036 list_for_each_entry(s, &slab_caches, list) {
26a7bd03 6037 err = sysfs_slab_add(s);
5d540fb7 6038 if (err)
f9f58285
FF
6039 pr_err("SLUB: Unable to add boot slab %s to sysfs\n",
6040 s->name);
26a7bd03 6041 }
81819f0f
CL
6042
6043 while (alias_list) {
6044 struct saved_alias *al = alias_list;
6045
6046 alias_list = alias_list->next;
6047 err = sysfs_slab_alias(al->s, al->name);
5d540fb7 6048 if (err)
f9f58285
FF
6049 pr_err("SLUB: Unable to add boot slab alias %s to sysfs\n",
6050 al->name);
81819f0f
CL
6051 kfree(al);
6052 }
6053
18004c5d 6054 mutex_unlock(&slab_mutex);
81819f0f
CL
6055 return 0;
6056}
6057
6058__initcall(slab_sysfs_init);
ab4d5ed5 6059#endif /* CONFIG_SYSFS */
57ed3eda 6060
64dd6849
FM
6061#if defined(CONFIG_SLUB_DEBUG) && defined(CONFIG_DEBUG_FS)
6062static int slab_debugfs_show(struct seq_file *seq, void *v)
6063{
64dd6849 6064 struct loc_track *t = seq->private;
005a79e5
GS
6065 struct location *l;
6066 unsigned long idx;
64dd6849 6067
005a79e5 6068 idx = (unsigned long) t->idx;
64dd6849
FM
6069 if (idx < t->count) {
6070 l = &t->loc[idx];
6071
6072 seq_printf(seq, "%7ld ", l->count);
6073
6074 if (l->addr)
6075 seq_printf(seq, "%pS", (void *)l->addr);
6076 else
6077 seq_puts(seq, "<not-available>");
6078
6edf2576
FT
6079 if (l->waste)
6080 seq_printf(seq, " waste=%lu/%lu",
6081 l->count * l->waste, l->waste);
6082
64dd6849
FM
6083 if (l->sum_time != l->min_time) {
6084 seq_printf(seq, " age=%ld/%llu/%ld",
6085 l->min_time, div_u64(l->sum_time, l->count),
6086 l->max_time);
6087 } else
6088 seq_printf(seq, " age=%ld", l->min_time);
6089
6090 if (l->min_pid != l->max_pid)
6091 seq_printf(seq, " pid=%ld-%ld", l->min_pid, l->max_pid);
6092 else
6093 seq_printf(seq, " pid=%ld",
6094 l->min_pid);
6095
6096 if (num_online_cpus() > 1 && !cpumask_empty(to_cpumask(l->cpus)))
6097 seq_printf(seq, " cpus=%*pbl",
6098 cpumask_pr_args(to_cpumask(l->cpus)));
6099
6100 if (nr_online_nodes > 1 && !nodes_empty(l->nodes))
6101 seq_printf(seq, " nodes=%*pbl",
6102 nodemask_pr_args(&l->nodes));
6103
8ea9fb92
OG
6104#ifdef CONFIG_STACKDEPOT
6105 {
6106 depot_stack_handle_t handle;
6107 unsigned long *entries;
6108 unsigned int nr_entries, j;
6109
6110 handle = READ_ONCE(l->handle);
6111 if (handle) {
6112 nr_entries = stack_depot_fetch(handle, &entries);
6113 seq_puts(seq, "\n");
6114 for (j = 0; j < nr_entries; j++)
6115 seq_printf(seq, " %pS\n", (void *)entries[j]);
6116 }
6117 }
6118#endif
64dd6849
FM
6119 seq_puts(seq, "\n");
6120 }
6121
6122 if (!idx && !t->count)
6123 seq_puts(seq, "No data\n");
6124
6125 return 0;
6126}
6127
6128static void slab_debugfs_stop(struct seq_file *seq, void *v)
6129{
6130}
6131
6132static void *slab_debugfs_next(struct seq_file *seq, void *v, loff_t *ppos)
6133{
6134 struct loc_track *t = seq->private;
6135
005a79e5 6136 t->idx = ++(*ppos);
64dd6849 6137 if (*ppos <= t->count)
005a79e5 6138 return ppos;
64dd6849
FM
6139
6140 return NULL;
6141}
6142
553c0369
OG
6143static int cmp_loc_by_count(const void *a, const void *b, const void *data)
6144{
6145 struct location *loc1 = (struct location *)a;
6146 struct location *loc2 = (struct location *)b;
6147
6148 if (loc1->count > loc2->count)
6149 return -1;
6150 else
6151 return 1;
6152}
6153
64dd6849
FM
6154static void *slab_debugfs_start(struct seq_file *seq, loff_t *ppos)
6155{
005a79e5
GS
6156 struct loc_track *t = seq->private;
6157
6158 t->idx = *ppos;
64dd6849
FM
6159 return ppos;
6160}
6161
6162static const struct seq_operations slab_debugfs_sops = {
6163 .start = slab_debugfs_start,
6164 .next = slab_debugfs_next,
6165 .stop = slab_debugfs_stop,
6166 .show = slab_debugfs_show,
6167};
6168
6169static int slab_debug_trace_open(struct inode *inode, struct file *filep)
6170{
6171
6172 struct kmem_cache_node *n;
6173 enum track_item alloc;
6174 int node;
6175 struct loc_track *t = __seq_open_private(filep, &slab_debugfs_sops,
6176 sizeof(struct loc_track));
6177 struct kmem_cache *s = file_inode(filep)->i_private;
b3fd64e1
VB
6178 unsigned long *obj_map;
6179
2127d225
ML
6180 if (!t)
6181 return -ENOMEM;
6182
b3fd64e1 6183 obj_map = bitmap_alloc(oo_objects(s->oo), GFP_KERNEL);
2127d225
ML
6184 if (!obj_map) {
6185 seq_release_private(inode, filep);
b3fd64e1 6186 return -ENOMEM;
2127d225 6187 }
64dd6849
FM
6188
6189 if (strcmp(filep->f_path.dentry->d_name.name, "alloc_traces") == 0)
6190 alloc = TRACK_ALLOC;
6191 else
6192 alloc = TRACK_FREE;
6193
b3fd64e1
VB
6194 if (!alloc_loc_track(t, PAGE_SIZE / sizeof(struct location), GFP_KERNEL)) {
6195 bitmap_free(obj_map);
2127d225 6196 seq_release_private(inode, filep);
64dd6849 6197 return -ENOMEM;
b3fd64e1 6198 }
64dd6849 6199
64dd6849
FM
6200 for_each_kmem_cache_node(s, node, n) {
6201 unsigned long flags;
bb192ed9 6202 struct slab *slab;
64dd6849
FM
6203
6204 if (!atomic_long_read(&n->nr_slabs))
6205 continue;
6206
6207 spin_lock_irqsave(&n->list_lock, flags);
bb192ed9
VB
6208 list_for_each_entry(slab, &n->partial, slab_list)
6209 process_slab(t, s, slab, alloc, obj_map);
6210 list_for_each_entry(slab, &n->full, slab_list)
6211 process_slab(t, s, slab, alloc, obj_map);
64dd6849
FM
6212 spin_unlock_irqrestore(&n->list_lock, flags);
6213 }
6214
553c0369
OG
6215 /* Sort locations by count */
6216 sort_r(t->loc, t->count, sizeof(struct location),
6217 cmp_loc_by_count, NULL, NULL);
6218
b3fd64e1 6219 bitmap_free(obj_map);
64dd6849
FM
6220 return 0;
6221}
6222
6223static int slab_debug_trace_release(struct inode *inode, struct file *file)
6224{
6225 struct seq_file *seq = file->private_data;
6226 struct loc_track *t = seq->private;
6227
6228 free_loc_track(t);
6229 return seq_release_private(inode, file);
6230}
6231
6232static const struct file_operations slab_debugfs_fops = {
6233 .open = slab_debug_trace_open,
6234 .read = seq_read,
6235 .llseek = seq_lseek,
6236 .release = slab_debug_trace_release,
6237};
6238
6239static void debugfs_slab_add(struct kmem_cache *s)
6240{
6241 struct dentry *slab_cache_dir;
6242
6243 if (unlikely(!slab_debugfs_root))
6244 return;
6245
6246 slab_cache_dir = debugfs_create_dir(s->name, slab_debugfs_root);
6247
6248 debugfs_create_file("alloc_traces", 0400,
6249 slab_cache_dir, s, &slab_debugfs_fops);
6250
6251 debugfs_create_file("free_traces", 0400,
6252 slab_cache_dir, s, &slab_debugfs_fops);
6253}
6254
6255void debugfs_slab_release(struct kmem_cache *s)
6256{
6257 debugfs_remove_recursive(debugfs_lookup(s->name, slab_debugfs_root));
6258}
6259
6260static int __init slab_debugfs_init(void)
6261{
6262 struct kmem_cache *s;
6263
6264 slab_debugfs_root = debugfs_create_dir("slab", NULL);
6265
6266 list_for_each_entry(s, &slab_caches, list)
6267 if (s->flags & SLAB_STORE_USER)
6268 debugfs_slab_add(s);
6269
6270 return 0;
6271
6272}
6273__initcall(slab_debugfs_init);
6274#endif
57ed3eda
PE
6275/*
6276 * The /proc/slabinfo ABI
6277 */
5b365771 6278#ifdef CONFIG_SLUB_DEBUG
0d7561c6 6279void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo)
57ed3eda 6280{
57ed3eda 6281 unsigned long nr_slabs = 0;
205ab99d
CL
6282 unsigned long nr_objs = 0;
6283 unsigned long nr_free = 0;
57ed3eda 6284 int node;
fa45dc25 6285 struct kmem_cache_node *n;
57ed3eda 6286
fa45dc25 6287 for_each_kmem_cache_node(s, node, n) {
c17fd13e
WL
6288 nr_slabs += node_nr_slabs(n);
6289 nr_objs += node_nr_objs(n);
205ab99d 6290 nr_free += count_partial(n, count_free);
57ed3eda
PE
6291 }
6292
0d7561c6
GC
6293 sinfo->active_objs = nr_objs - nr_free;
6294 sinfo->num_objs = nr_objs;
6295 sinfo->active_slabs = nr_slabs;
6296 sinfo->num_slabs = nr_slabs;
6297 sinfo->objects_per_slab = oo_objects(s->oo);
6298 sinfo->cache_order = oo_order(s->oo);
57ed3eda
PE
6299}
6300
0d7561c6 6301void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s)
7b3c3a50 6302{
7b3c3a50
AD
6303}
6304
b7454ad3
GC
6305ssize_t slabinfo_write(struct file *file, const char __user *buffer,
6306 size_t count, loff_t *ppos)
7b3c3a50 6307{
b7454ad3 6308 return -EIO;
7b3c3a50 6309}
5b365771 6310#endif /* CONFIG_SLUB_DEBUG */