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