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