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