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