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