]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/slub.c
Revert "Slub: UP bandaid"
[net-next-2.6.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 *
5 * The allocator synchronizes using per slab locks and only
6 * uses a centralized lock to manage a pool of partial slabs.
7 *
cde53535 8 * (C) 2007 SGI, Christoph Lameter
81819f0f
CL
9 */
10
11#include <linux/mm.h>
1eb5ac64 12#include <linux/swap.h> /* struct reclaim_state */
81819f0f
CL
13#include <linux/module.h>
14#include <linux/bit_spinlock.h>
15#include <linux/interrupt.h>
16#include <linux/bitops.h>
17#include <linux/slab.h>
7b3c3a50 18#include <linux/proc_fs.h>
81819f0f 19#include <linux/seq_file.h>
5a896d9e 20#include <linux/kmemcheck.h>
81819f0f
CL
21#include <linux/cpu.h>
22#include <linux/cpuset.h>
23#include <linux/mempolicy.h>
24#include <linux/ctype.h>
3ac7fe5a 25#include <linux/debugobjects.h>
81819f0f 26#include <linux/kallsyms.h>
b9049e23 27#include <linux/memory.h>
f8bd2258 28#include <linux/math64.h>
773ff60e 29#include <linux/fault-inject.h>
81819f0f
CL
30
31/*
32 * Lock order:
33 * 1. slab_lock(page)
34 * 2. slab->list_lock
35 *
36 * The slab_lock protects operations on the object of a particular
37 * slab and its metadata in the page struct. If the slab lock
38 * has been taken then no allocations nor frees can be performed
39 * on the objects in the slab nor can the slab be added or removed
40 * from the partial or full lists since this would mean modifying
41 * the page_struct of the slab.
42 *
43 * The list_lock protects the partial and full list on each node and
44 * the partial slab counter. If taken then no new slabs may be added or
45 * removed from the lists nor make the number of partial slabs be modified.
46 * (Note that the total number of slabs is an atomic value that may be
47 * modified without taking the list lock).
48 *
49 * The list_lock is a centralized lock and thus we avoid taking it as
50 * much as possible. As long as SLUB does not have to handle partial
51 * slabs, operations can continue without any centralized lock. F.e.
52 * allocating a long series of objects that fill up slabs does not require
53 * the list lock.
54 *
55 * The lock order is sometimes inverted when we are trying to get a slab
56 * off a list. We take the list_lock and then look for a page on the list
57 * to use. While we do that objects in the slabs may be freed. We can
58 * only operate on the slab if we have also taken the slab_lock. So we use
59 * a slab_trylock() on the slab. If trylock was successful then no frees
60 * can occur anymore and we can use the slab for allocations etc. If the
61 * slab_trylock() does not succeed then frees are in progress in the slab and
62 * we must stay away from it for a while since we may cause a bouncing
63 * cacheline if we try to acquire the lock. So go onto the next slab.
64 * If all pages are busy then we may allocate a new slab instead of reusing
65 * a partial slab. A new slab has noone operating on it and thus there is
66 * no danger of cacheline contention.
67 *
68 * Interrupts are disabled during allocation and deallocation in order to
69 * make the slab allocator safe to use in the context of an irq. In addition
70 * interrupts are disabled to ensure that the processor does not change
71 * while handling per_cpu slabs, due to kernel preemption.
72 *
73 * SLUB assigns one slab for allocation to each processor.
74 * Allocations only occur from these slabs called cpu slabs.
75 *
672bba3a
CL
76 * Slabs with free elements are kept on a partial list and during regular
77 * operations no list for full slabs is used. If an object in a full slab is
81819f0f 78 * freed then the slab will show up again on the partial lists.
672bba3a
CL
79 * We track full slabs for debugging purposes though because otherwise we
80 * cannot scan all objects.
81819f0f
CL
81 *
82 * Slabs are freed when they become empty. Teardown and setup is
83 * minimal so we rely on the page allocators per cpu caches for
84 * fast frees and allocs.
85 *
86 * Overloading of page flags that are otherwise used for LRU management.
87 *
4b6f0750
CL
88 * PageActive The slab is frozen and exempt from list processing.
89 * This means that the slab is dedicated to a purpose
90 * such as satisfying allocations for a specific
91 * processor. Objects may be freed in the slab while
92 * it is frozen but slab_free will then skip the usual
93 * list operations. It is up to the processor holding
94 * the slab to integrate the slab into the slab lists
95 * when the slab is no longer needed.
96 *
97 * One use of this flag is to mark slabs that are
98 * used for allocations. Then such a slab becomes a cpu
99 * slab. The cpu slab may be equipped with an additional
dfb4f096 100 * freelist that allows lockless access to
894b8788
CL
101 * free objects in addition to the regular freelist
102 * that requires the slab lock.
81819f0f
CL
103 *
104 * PageError Slab requires special handling due to debug
105 * options set. This moves slab handling out of
894b8788 106 * the fast path and disables lockless freelists.
81819f0f
CL
107 */
108
af537b0a
CL
109#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
110 SLAB_TRACE | SLAB_DEBUG_FREE)
111
112static inline int kmem_cache_debug(struct kmem_cache *s)
113{
5577bd8a 114#ifdef CONFIG_SLUB_DEBUG
af537b0a 115 return unlikely(s->flags & SLAB_DEBUG_FLAGS);
5577bd8a 116#else
af537b0a 117 return 0;
5577bd8a 118#endif
af537b0a 119}
5577bd8a 120
81819f0f
CL
121/*
122 * Issues still to be resolved:
123 *
81819f0f
CL
124 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
125 *
81819f0f
CL
126 * - Variable sizing of the per node arrays
127 */
128
129/* Enable to test recovery from slab corruption on boot */
130#undef SLUB_RESILIENCY_TEST
131
2086d26a
CL
132/*
133 * Mininum number of partial slabs. These will be left on the partial
134 * lists even if they are empty. kmem_cache_shrink may reclaim them.
135 */
76be8950 136#define MIN_PARTIAL 5
e95eed57 137
2086d26a
CL
138/*
139 * Maximum number of desirable partial slabs.
140 * The existence of more partial slabs makes kmem_cache_shrink
141 * sort the partial list by the number of objects in the.
142 */
143#define MAX_PARTIAL 10
144
81819f0f
CL
145#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
146 SLAB_POISON | SLAB_STORE_USER)
672bba3a 147
fa5ec8a1 148/*
3de47213
DR
149 * Debugging flags that require metadata to be stored in the slab. These get
150 * disabled when slub_debug=O is used and a cache's min order increases with
151 * metadata.
fa5ec8a1 152 */
3de47213 153#define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
fa5ec8a1 154
81819f0f
CL
155/*
156 * Set of flags that will prevent slab merging
157 */
158#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
4c13dd3b
DM
159 SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
160 SLAB_FAILSLAB)
81819f0f
CL
161
162#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
5a896d9e 163 SLAB_CACHE_DMA | SLAB_NOTRACK)
81819f0f 164
210b5c06
CG
165#define OO_SHIFT 16
166#define OO_MASK ((1 << OO_SHIFT) - 1)
167#define MAX_OBJS_PER_PAGE 65535 /* since page.objects is u16 */
168
81819f0f 169/* Internal SLUB flags */
f90ec390 170#define __OBJECT_POISON 0x80000000UL /* Poison object */
81819f0f
CL
171
172static int kmem_size = sizeof(struct kmem_cache);
173
174#ifdef CONFIG_SMP
175static struct notifier_block slab_notifier;
176#endif
177
178static enum {
179 DOWN, /* No slab functionality available */
51df1142 180 PARTIAL, /* Kmem_cache_node works */
672bba3a 181 UP, /* Everything works but does not show up in sysfs */
81819f0f
CL
182 SYSFS /* Sysfs up */
183} slab_state = DOWN;
184
185/* A list of all slab caches on the system */
186static DECLARE_RWSEM(slub_lock);
5af328a5 187static LIST_HEAD(slab_caches);
81819f0f 188
02cbc874
CL
189/*
190 * Tracking user of a slab.
191 */
192struct track {
ce71e27c 193 unsigned long addr; /* Called from address */
02cbc874
CL
194 int cpu; /* Was running on cpu */
195 int pid; /* Pid context */
196 unsigned long when; /* When did the operation occur */
197};
198
199enum track_item { TRACK_ALLOC, TRACK_FREE };
200
f6acb635 201#ifdef CONFIG_SLUB_DEBUG
81819f0f
CL
202static int sysfs_slab_add(struct kmem_cache *);
203static int sysfs_slab_alias(struct kmem_cache *, const char *);
204static void sysfs_slab_remove(struct kmem_cache *);
8ff12cfc 205
81819f0f 206#else
0c710013
CL
207static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
208static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
209 { return 0; }
151c602f
CL
210static inline void sysfs_slab_remove(struct kmem_cache *s)
211{
84c1cf62 212 kfree(s->name);
151c602f
CL
213 kfree(s);
214}
8ff12cfc 215
81819f0f
CL
216#endif
217
84e554e6 218static inline void stat(struct kmem_cache *s, enum stat_item si)
8ff12cfc
CL
219{
220#ifdef CONFIG_SLUB_STATS
84e554e6 221 __this_cpu_inc(s->cpu_slab->stat[si]);
8ff12cfc
CL
222#endif
223}
224
81819f0f
CL
225/********************************************************************
226 * Core slab cache functions
227 *******************************************************************/
228
229int slab_is_available(void)
230{
231 return slab_state >= UP;
232}
233
234static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
235{
236#ifdef CONFIG_NUMA
237 return s->node[node];
238#else
239 return &s->local_node;
240#endif
241}
242
6446faa2 243/* Verify that a pointer has an address that is valid within a slab page */
02cbc874
CL
244static inline int check_valid_pointer(struct kmem_cache *s,
245 struct page *page, const void *object)
246{
247 void *base;
248
a973e9dd 249 if (!object)
02cbc874
CL
250 return 1;
251
a973e9dd 252 base = page_address(page);
39b26464 253 if (object < base || object >= base + page->objects * s->size ||
02cbc874
CL
254 (object - base) % s->size) {
255 return 0;
256 }
257
258 return 1;
259}
260
7656c72b
CL
261static inline void *get_freepointer(struct kmem_cache *s, void *object)
262{
263 return *(void **)(object + s->offset);
264}
265
266static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
267{
268 *(void **)(object + s->offset) = fp;
269}
270
271/* Loop over all objects in a slab */
224a88be
CL
272#define for_each_object(__p, __s, __addr, __objects) \
273 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
7656c72b
CL
274 __p += (__s)->size)
275
276/* Scan freelist */
277#define for_each_free_object(__p, __s, __free) \
a973e9dd 278 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
7656c72b
CL
279
280/* Determine object index from a given position */
281static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
282{
283 return (p - addr) / s->size;
284}
285
834f3d11
CL
286static inline struct kmem_cache_order_objects oo_make(int order,
287 unsigned long size)
288{
289 struct kmem_cache_order_objects x = {
210b5c06 290 (order << OO_SHIFT) + (PAGE_SIZE << order) / size
834f3d11
CL
291 };
292
293 return x;
294}
295
296static inline int oo_order(struct kmem_cache_order_objects x)
297{
210b5c06 298 return x.x >> OO_SHIFT;
834f3d11
CL
299}
300
301static inline int oo_objects(struct kmem_cache_order_objects x)
302{
210b5c06 303 return x.x & OO_MASK;
834f3d11
CL
304}
305
41ecc55b
CL
306#ifdef CONFIG_SLUB_DEBUG
307/*
308 * Debug settings:
309 */
f0630fff
CL
310#ifdef CONFIG_SLUB_DEBUG_ON
311static int slub_debug = DEBUG_DEFAULT_FLAGS;
312#else
41ecc55b 313static int slub_debug;
f0630fff 314#endif
41ecc55b
CL
315
316static char *slub_debug_slabs;
fa5ec8a1 317static int disable_higher_order_debug;
41ecc55b 318
81819f0f
CL
319/*
320 * Object debugging
321 */
322static void print_section(char *text, u8 *addr, unsigned int length)
323{
324 int i, offset;
325 int newline = 1;
326 char ascii[17];
327
328 ascii[16] = 0;
329
330 for (i = 0; i < length; i++) {
331 if (newline) {
24922684 332 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
81819f0f
CL
333 newline = 0;
334 }
06428780 335 printk(KERN_CONT " %02x", addr[i]);
81819f0f
CL
336 offset = i % 16;
337 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
338 if (offset == 15) {
06428780 339 printk(KERN_CONT " %s\n", ascii);
81819f0f
CL
340 newline = 1;
341 }
342 }
343 if (!newline) {
344 i %= 16;
345 while (i < 16) {
06428780 346 printk(KERN_CONT " ");
81819f0f
CL
347 ascii[i] = ' ';
348 i++;
349 }
06428780 350 printk(KERN_CONT " %s\n", ascii);
81819f0f
CL
351 }
352}
353
81819f0f
CL
354static struct track *get_track(struct kmem_cache *s, void *object,
355 enum track_item alloc)
356{
357 struct track *p;
358
359 if (s->offset)
360 p = object + s->offset + sizeof(void *);
361 else
362 p = object + s->inuse;
363
364 return p + alloc;
365}
366
367static void set_track(struct kmem_cache *s, void *object,
ce71e27c 368 enum track_item alloc, unsigned long addr)
81819f0f 369{
1a00df4a 370 struct track *p = get_track(s, object, alloc);
81819f0f 371
81819f0f
CL
372 if (addr) {
373 p->addr = addr;
374 p->cpu = smp_processor_id();
88e4ccf2 375 p->pid = current->pid;
81819f0f
CL
376 p->when = jiffies;
377 } else
378 memset(p, 0, sizeof(struct track));
379}
380
81819f0f
CL
381static void init_tracking(struct kmem_cache *s, void *object)
382{
24922684
CL
383 if (!(s->flags & SLAB_STORE_USER))
384 return;
385
ce71e27c
EGM
386 set_track(s, object, TRACK_FREE, 0UL);
387 set_track(s, object, TRACK_ALLOC, 0UL);
81819f0f
CL
388}
389
390static void print_track(const char *s, struct track *t)
391{
392 if (!t->addr)
393 return;
394
7daf705f 395 printk(KERN_ERR "INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
ce71e27c 396 s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
24922684
CL
397}
398
399static void print_tracking(struct kmem_cache *s, void *object)
400{
401 if (!(s->flags & SLAB_STORE_USER))
402 return;
403
404 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
405 print_track("Freed", get_track(s, object, TRACK_FREE));
406}
407
408static void print_page_info(struct page *page)
409{
39b26464
CL
410 printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
411 page, page->objects, page->inuse, page->freelist, page->flags);
24922684
CL
412
413}
414
415static void slab_bug(struct kmem_cache *s, char *fmt, ...)
416{
417 va_list args;
418 char buf[100];
419
420 va_start(args, fmt);
421 vsnprintf(buf, sizeof(buf), fmt, args);
422 va_end(args);
423 printk(KERN_ERR "========================================"
424 "=====================================\n");
425 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
426 printk(KERN_ERR "----------------------------------------"
427 "-------------------------------------\n\n");
81819f0f
CL
428}
429
24922684
CL
430static void slab_fix(struct kmem_cache *s, char *fmt, ...)
431{
432 va_list args;
433 char buf[100];
434
435 va_start(args, fmt);
436 vsnprintf(buf, sizeof(buf), fmt, args);
437 va_end(args);
438 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
439}
440
441static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
81819f0f
CL
442{
443 unsigned int off; /* Offset of last byte */
a973e9dd 444 u8 *addr = page_address(page);
24922684
CL
445
446 print_tracking(s, p);
447
448 print_page_info(page);
449
450 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
451 p, p - addr, get_freepointer(s, p));
452
453 if (p > addr + 16)
454 print_section("Bytes b4", p - 16, 16);
455
0ebd652b 456 print_section("Object", p, min_t(unsigned long, s->objsize, PAGE_SIZE));
81819f0f
CL
457
458 if (s->flags & SLAB_RED_ZONE)
459 print_section("Redzone", p + s->objsize,
460 s->inuse - s->objsize);
461
81819f0f
CL
462 if (s->offset)
463 off = s->offset + sizeof(void *);
464 else
465 off = s->inuse;
466
24922684 467 if (s->flags & SLAB_STORE_USER)
81819f0f 468 off += 2 * sizeof(struct track);
81819f0f
CL
469
470 if (off != s->size)
471 /* Beginning of the filler is the free pointer */
24922684
CL
472 print_section("Padding", p + off, s->size - off);
473
474 dump_stack();
81819f0f
CL
475}
476
477static void object_err(struct kmem_cache *s, struct page *page,
478 u8 *object, char *reason)
479{
3dc50637 480 slab_bug(s, "%s", reason);
24922684 481 print_trailer(s, page, object);
81819f0f
CL
482}
483
24922684 484static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
81819f0f
CL
485{
486 va_list args;
487 char buf[100];
488
24922684
CL
489 va_start(args, fmt);
490 vsnprintf(buf, sizeof(buf), fmt, args);
81819f0f 491 va_end(args);
3dc50637 492 slab_bug(s, "%s", buf);
24922684 493 print_page_info(page);
81819f0f
CL
494 dump_stack();
495}
496
497static void init_object(struct kmem_cache *s, void *object, int active)
498{
499 u8 *p = object;
500
501 if (s->flags & __OBJECT_POISON) {
502 memset(p, POISON_FREE, s->objsize - 1);
06428780 503 p[s->objsize - 1] = POISON_END;
81819f0f
CL
504 }
505
506 if (s->flags & SLAB_RED_ZONE)
507 memset(p + s->objsize,
508 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
509 s->inuse - s->objsize);
510}
511
24922684 512static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
81819f0f
CL
513{
514 while (bytes) {
515 if (*start != (u8)value)
24922684 516 return start;
81819f0f
CL
517 start++;
518 bytes--;
519 }
24922684
CL
520 return NULL;
521}
522
523static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
524 void *from, void *to)
525{
526 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
527 memset(from, data, to - from);
528}
529
530static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
531 u8 *object, char *what,
06428780 532 u8 *start, unsigned int value, unsigned int bytes)
24922684
CL
533{
534 u8 *fault;
535 u8 *end;
536
537 fault = check_bytes(start, value, bytes);
538 if (!fault)
539 return 1;
540
541 end = start + bytes;
542 while (end > fault && end[-1] == value)
543 end--;
544
545 slab_bug(s, "%s overwritten", what);
546 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
547 fault, end - 1, fault[0], value);
548 print_trailer(s, page, object);
549
550 restore_bytes(s, what, value, fault, end);
551 return 0;
81819f0f
CL
552}
553
81819f0f
CL
554/*
555 * Object layout:
556 *
557 * object address
558 * Bytes of the object to be managed.
559 * If the freepointer may overlay the object then the free
560 * pointer is the first word of the object.
672bba3a 561 *
81819f0f
CL
562 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
563 * 0xa5 (POISON_END)
564 *
565 * object + s->objsize
566 * Padding to reach word boundary. This is also used for Redzoning.
672bba3a
CL
567 * Padding is extended by another word if Redzoning is enabled and
568 * objsize == inuse.
569 *
81819f0f
CL
570 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
571 * 0xcc (RED_ACTIVE) for objects in use.
572 *
573 * object + s->inuse
672bba3a
CL
574 * Meta data starts here.
575 *
81819f0f
CL
576 * A. Free pointer (if we cannot overwrite object on free)
577 * B. Tracking data for SLAB_STORE_USER
672bba3a 578 * C. Padding to reach required alignment boundary or at mininum
6446faa2 579 * one word if debugging is on to be able to detect writes
672bba3a
CL
580 * before the word boundary.
581 *
582 * Padding is done using 0x5a (POISON_INUSE)
81819f0f
CL
583 *
584 * object + s->size
672bba3a 585 * Nothing is used beyond s->size.
81819f0f 586 *
672bba3a
CL
587 * If slabcaches are merged then the objsize and inuse boundaries are mostly
588 * ignored. And therefore no slab options that rely on these boundaries
81819f0f
CL
589 * may be used with merged slabcaches.
590 */
591
81819f0f
CL
592static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
593{
594 unsigned long off = s->inuse; /* The end of info */
595
596 if (s->offset)
597 /* Freepointer is placed after the object. */
598 off += sizeof(void *);
599
600 if (s->flags & SLAB_STORE_USER)
601 /* We also have user information there */
602 off += 2 * sizeof(struct track);
603
604 if (s->size == off)
605 return 1;
606
24922684
CL
607 return check_bytes_and_report(s, page, p, "Object padding",
608 p + off, POISON_INUSE, s->size - off);
81819f0f
CL
609}
610
39b26464 611/* Check the pad bytes at the end of a slab page */
81819f0f
CL
612static int slab_pad_check(struct kmem_cache *s, struct page *page)
613{
24922684
CL
614 u8 *start;
615 u8 *fault;
616 u8 *end;
617 int length;
618 int remainder;
81819f0f
CL
619
620 if (!(s->flags & SLAB_POISON))
621 return 1;
622
a973e9dd 623 start = page_address(page);
834f3d11 624 length = (PAGE_SIZE << compound_order(page));
39b26464
CL
625 end = start + length;
626 remainder = length % s->size;
81819f0f
CL
627 if (!remainder)
628 return 1;
629
39b26464 630 fault = check_bytes(end - remainder, POISON_INUSE, remainder);
24922684
CL
631 if (!fault)
632 return 1;
633 while (end > fault && end[-1] == POISON_INUSE)
634 end--;
635
636 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
39b26464 637 print_section("Padding", end - remainder, remainder);
24922684 638
8a3d271d 639 restore_bytes(s, "slab padding", POISON_INUSE, end - remainder, end);
24922684 640 return 0;
81819f0f
CL
641}
642
643static int check_object(struct kmem_cache *s, struct page *page,
644 void *object, int active)
645{
646 u8 *p = object;
647 u8 *endobject = object + s->objsize;
648
649 if (s->flags & SLAB_RED_ZONE) {
650 unsigned int red =
651 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
652
24922684
CL
653 if (!check_bytes_and_report(s, page, object, "Redzone",
654 endobject, red, s->inuse - s->objsize))
81819f0f 655 return 0;
81819f0f 656 } else {
3adbefee
IM
657 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
658 check_bytes_and_report(s, page, p, "Alignment padding",
659 endobject, POISON_INUSE, s->inuse - s->objsize);
660 }
81819f0f
CL
661 }
662
663 if (s->flags & SLAB_POISON) {
664 if (!active && (s->flags & __OBJECT_POISON) &&
24922684
CL
665 (!check_bytes_and_report(s, page, p, "Poison", p,
666 POISON_FREE, s->objsize - 1) ||
667 !check_bytes_and_report(s, page, p, "Poison",
06428780 668 p + s->objsize - 1, POISON_END, 1)))
81819f0f 669 return 0;
81819f0f
CL
670 /*
671 * check_pad_bytes cleans up on its own.
672 */
673 check_pad_bytes(s, page, p);
674 }
675
676 if (!s->offset && active)
677 /*
678 * Object and freepointer overlap. Cannot check
679 * freepointer while object is allocated.
680 */
681 return 1;
682
683 /* Check free pointer validity */
684 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
685 object_err(s, page, p, "Freepointer corrupt");
686 /*
9f6c708e 687 * No choice but to zap it and thus lose the remainder
81819f0f 688 * of the free objects in this slab. May cause
672bba3a 689 * another error because the object count is now wrong.
81819f0f 690 */
a973e9dd 691 set_freepointer(s, p, NULL);
81819f0f
CL
692 return 0;
693 }
694 return 1;
695}
696
697static int check_slab(struct kmem_cache *s, struct page *page)
698{
39b26464
CL
699 int maxobj;
700
81819f0f
CL
701 VM_BUG_ON(!irqs_disabled());
702
703 if (!PageSlab(page)) {
24922684 704 slab_err(s, page, "Not a valid slab page");
81819f0f
CL
705 return 0;
706 }
39b26464
CL
707
708 maxobj = (PAGE_SIZE << compound_order(page)) / s->size;
709 if (page->objects > maxobj) {
710 slab_err(s, page, "objects %u > max %u",
711 s->name, page->objects, maxobj);
712 return 0;
713 }
714 if (page->inuse > page->objects) {
24922684 715 slab_err(s, page, "inuse %u > max %u",
39b26464 716 s->name, page->inuse, page->objects);
81819f0f
CL
717 return 0;
718 }
719 /* Slab_pad_check fixes things up after itself */
720 slab_pad_check(s, page);
721 return 1;
722}
723
724/*
672bba3a
CL
725 * Determine if a certain object on a page is on the freelist. Must hold the
726 * slab lock to guarantee that the chains are in a consistent state.
81819f0f
CL
727 */
728static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
729{
730 int nr = 0;
731 void *fp = page->freelist;
732 void *object = NULL;
224a88be 733 unsigned long max_objects;
81819f0f 734
39b26464 735 while (fp && nr <= page->objects) {
81819f0f
CL
736 if (fp == search)
737 return 1;
738 if (!check_valid_pointer(s, page, fp)) {
739 if (object) {
740 object_err(s, page, object,
741 "Freechain corrupt");
a973e9dd 742 set_freepointer(s, object, NULL);
81819f0f
CL
743 break;
744 } else {
24922684 745 slab_err(s, page, "Freepointer corrupt");
a973e9dd 746 page->freelist = NULL;
39b26464 747 page->inuse = page->objects;
24922684 748 slab_fix(s, "Freelist cleared");
81819f0f
CL
749 return 0;
750 }
751 break;
752 }
753 object = fp;
754 fp = get_freepointer(s, object);
755 nr++;
756 }
757
224a88be 758 max_objects = (PAGE_SIZE << compound_order(page)) / s->size;
210b5c06
CG
759 if (max_objects > MAX_OBJS_PER_PAGE)
760 max_objects = MAX_OBJS_PER_PAGE;
224a88be
CL
761
762 if (page->objects != max_objects) {
763 slab_err(s, page, "Wrong number of objects. Found %d but "
764 "should be %d", page->objects, max_objects);
765 page->objects = max_objects;
766 slab_fix(s, "Number of objects adjusted.");
767 }
39b26464 768 if (page->inuse != page->objects - nr) {
70d71228 769 slab_err(s, page, "Wrong object count. Counter is %d but "
39b26464
CL
770 "counted were %d", page->inuse, page->objects - nr);
771 page->inuse = page->objects - nr;
24922684 772 slab_fix(s, "Object count adjusted.");
81819f0f
CL
773 }
774 return search == NULL;
775}
776
0121c619
CL
777static void trace(struct kmem_cache *s, struct page *page, void *object,
778 int alloc)
3ec09742
CL
779{
780 if (s->flags & SLAB_TRACE) {
781 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
782 s->name,
783 alloc ? "alloc" : "free",
784 object, page->inuse,
785 page->freelist);
786
787 if (!alloc)
788 print_section("Object", (void *)object, s->objsize);
789
790 dump_stack();
791 }
792}
793
c016b0bd
CL
794/*
795 * Hooks for other subsystems that check memory allocations. In a typical
796 * production configuration these hooks all should produce no code at all.
797 */
798static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
799{
c1d50836 800 flags &= gfp_allowed_mask;
c016b0bd
CL
801 lockdep_trace_alloc(flags);
802 might_sleep_if(flags & __GFP_WAIT);
803
804 return should_failslab(s->objsize, flags, s->flags);
805}
806
807static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, void *object)
808{
c1d50836 809 flags &= gfp_allowed_mask;
c016b0bd
CL
810 kmemcheck_slab_alloc(s, flags, object, s->objsize);
811 kmemleak_alloc_recursive(object, s->objsize, 1, s->flags, flags);
812}
813
814static inline void slab_free_hook(struct kmem_cache *s, void *x)
815{
816 kmemleak_free_recursive(x, s->flags);
817}
818
819static inline void slab_free_hook_irq(struct kmem_cache *s, void *object)
820{
821 kmemcheck_slab_free(s, object, s->objsize);
822 debug_check_no_locks_freed(object, s->objsize);
823 if (!(s->flags & SLAB_DEBUG_OBJECTS))
824 debug_check_no_obj_freed(object, s->objsize);
825}
826
643b1138 827/*
672bba3a 828 * Tracking of fully allocated slabs for debugging purposes.
643b1138 829 */
e95eed57 830static void add_full(struct kmem_cache_node *n, struct page *page)
643b1138 831{
643b1138
CL
832 spin_lock(&n->list_lock);
833 list_add(&page->lru, &n->full);
834 spin_unlock(&n->list_lock);
835}
836
837static void remove_full(struct kmem_cache *s, struct page *page)
838{
839 struct kmem_cache_node *n;
840
841 if (!(s->flags & SLAB_STORE_USER))
842 return;
843
844 n = get_node(s, page_to_nid(page));
845
846 spin_lock(&n->list_lock);
847 list_del(&page->lru);
848 spin_unlock(&n->list_lock);
849}
850
0f389ec6
CL
851/* Tracking of the number of slabs for debugging purposes */
852static inline unsigned long slabs_node(struct kmem_cache *s, int node)
853{
854 struct kmem_cache_node *n = get_node(s, node);
855
856 return atomic_long_read(&n->nr_slabs);
857}
858
26c02cf0
AB
859static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
860{
861 return atomic_long_read(&n->nr_slabs);
862}
863
205ab99d 864static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
0f389ec6
CL
865{
866 struct kmem_cache_node *n = get_node(s, node);
867
868 /*
869 * May be called early in order to allocate a slab for the
870 * kmem_cache_node structure. Solve the chicken-egg
871 * dilemma by deferring the increment of the count during
872 * bootstrap (see early_kmem_cache_node_alloc).
873 */
205ab99d 874 if (!NUMA_BUILD || n) {
0f389ec6 875 atomic_long_inc(&n->nr_slabs);
205ab99d
CL
876 atomic_long_add(objects, &n->total_objects);
877 }
0f389ec6 878}
205ab99d 879static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
0f389ec6
CL
880{
881 struct kmem_cache_node *n = get_node(s, node);
882
883 atomic_long_dec(&n->nr_slabs);
205ab99d 884 atomic_long_sub(objects, &n->total_objects);
0f389ec6
CL
885}
886
887/* Object debug checks for alloc/free paths */
3ec09742
CL
888static void setup_object_debug(struct kmem_cache *s, struct page *page,
889 void *object)
890{
891 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
892 return;
893
894 init_object(s, object, 0);
895 init_tracking(s, object);
896}
897
1537066c 898static noinline int alloc_debug_processing(struct kmem_cache *s, struct page *page,
ce71e27c 899 void *object, unsigned long addr)
81819f0f
CL
900{
901 if (!check_slab(s, page))
902 goto bad;
903
d692ef6d 904 if (!on_freelist(s, page, object)) {
24922684 905 object_err(s, page, object, "Object already allocated");
70d71228 906 goto bad;
81819f0f
CL
907 }
908
909 if (!check_valid_pointer(s, page, object)) {
910 object_err(s, page, object, "Freelist Pointer check fails");
70d71228 911 goto bad;
81819f0f
CL
912 }
913
d692ef6d 914 if (!check_object(s, page, object, 0))
81819f0f 915 goto bad;
81819f0f 916
3ec09742
CL
917 /* Success perform special debug activities for allocs */
918 if (s->flags & SLAB_STORE_USER)
919 set_track(s, object, TRACK_ALLOC, addr);
920 trace(s, page, object, 1);
921 init_object(s, object, 1);
81819f0f 922 return 1;
3ec09742 923
81819f0f
CL
924bad:
925 if (PageSlab(page)) {
926 /*
927 * If this is a slab page then lets do the best we can
928 * to avoid issues in the future. Marking all objects
672bba3a 929 * as used avoids touching the remaining objects.
81819f0f 930 */
24922684 931 slab_fix(s, "Marking all objects used");
39b26464 932 page->inuse = page->objects;
a973e9dd 933 page->freelist = NULL;
81819f0f
CL
934 }
935 return 0;
936}
937
1537066c
CL
938static noinline int free_debug_processing(struct kmem_cache *s,
939 struct page *page, void *object, unsigned long addr)
81819f0f
CL
940{
941 if (!check_slab(s, page))
942 goto fail;
943
944 if (!check_valid_pointer(s, page, object)) {
70d71228 945 slab_err(s, page, "Invalid object pointer 0x%p", object);
81819f0f
CL
946 goto fail;
947 }
948
949 if (on_freelist(s, page, object)) {
24922684 950 object_err(s, page, object, "Object already free");
81819f0f
CL
951 goto fail;
952 }
953
954 if (!check_object(s, page, object, 1))
955 return 0;
956
957 if (unlikely(s != page->slab)) {
3adbefee 958 if (!PageSlab(page)) {
70d71228
CL
959 slab_err(s, page, "Attempt to free object(0x%p) "
960 "outside of slab", object);
3adbefee 961 } else if (!page->slab) {
81819f0f 962 printk(KERN_ERR
70d71228 963 "SLUB <none>: no slab for object 0x%p.\n",
81819f0f 964 object);
70d71228 965 dump_stack();
06428780 966 } else
24922684
CL
967 object_err(s, page, object,
968 "page slab pointer corrupt.");
81819f0f
CL
969 goto fail;
970 }
3ec09742
CL
971
972 /* Special debug activities for freeing objects */
8a38082d 973 if (!PageSlubFrozen(page) && !page->freelist)
3ec09742
CL
974 remove_full(s, page);
975 if (s->flags & SLAB_STORE_USER)
976 set_track(s, object, TRACK_FREE, addr);
977 trace(s, page, object, 0);
978 init_object(s, object, 0);
81819f0f 979 return 1;
3ec09742 980
81819f0f 981fail:
24922684 982 slab_fix(s, "Object at 0x%p not freed", object);
81819f0f
CL
983 return 0;
984}
985
41ecc55b
CL
986static int __init setup_slub_debug(char *str)
987{
f0630fff
CL
988 slub_debug = DEBUG_DEFAULT_FLAGS;
989 if (*str++ != '=' || !*str)
990 /*
991 * No options specified. Switch on full debugging.
992 */
993 goto out;
994
995 if (*str == ',')
996 /*
997 * No options but restriction on slabs. This means full
998 * debugging for slabs matching a pattern.
999 */
1000 goto check_slabs;
1001
fa5ec8a1
DR
1002 if (tolower(*str) == 'o') {
1003 /*
1004 * Avoid enabling debugging on caches if its minimum order
1005 * would increase as a result.
1006 */
1007 disable_higher_order_debug = 1;
1008 goto out;
1009 }
1010
f0630fff
CL
1011 slub_debug = 0;
1012 if (*str == '-')
1013 /*
1014 * Switch off all debugging measures.
1015 */
1016 goto out;
1017
1018 /*
1019 * Determine which debug features should be switched on
1020 */
06428780 1021 for (; *str && *str != ','; str++) {
f0630fff
CL
1022 switch (tolower(*str)) {
1023 case 'f':
1024 slub_debug |= SLAB_DEBUG_FREE;
1025 break;
1026 case 'z':
1027 slub_debug |= SLAB_RED_ZONE;
1028 break;
1029 case 'p':
1030 slub_debug |= SLAB_POISON;
1031 break;
1032 case 'u':
1033 slub_debug |= SLAB_STORE_USER;
1034 break;
1035 case 't':
1036 slub_debug |= SLAB_TRACE;
1037 break;
4c13dd3b
DM
1038 case 'a':
1039 slub_debug |= SLAB_FAILSLAB;
1040 break;
f0630fff
CL
1041 default:
1042 printk(KERN_ERR "slub_debug option '%c' "
06428780 1043 "unknown. skipped\n", *str);
f0630fff 1044 }
41ecc55b
CL
1045 }
1046
f0630fff 1047check_slabs:
41ecc55b
CL
1048 if (*str == ',')
1049 slub_debug_slabs = str + 1;
f0630fff 1050out:
41ecc55b
CL
1051 return 1;
1052}
1053
1054__setup("slub_debug", setup_slub_debug);
1055
ba0268a8
CL
1056static unsigned long kmem_cache_flags(unsigned long objsize,
1057 unsigned long flags, const char *name,
51cc5068 1058 void (*ctor)(void *))
41ecc55b
CL
1059{
1060 /*
e153362a 1061 * Enable debugging if selected on the kernel commandline.
41ecc55b 1062 */
e153362a 1063 if (slub_debug && (!slub_debug_slabs ||
3de47213
DR
1064 !strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs))))
1065 flags |= slub_debug;
ba0268a8
CL
1066
1067 return flags;
41ecc55b
CL
1068}
1069#else
3ec09742
CL
1070static inline void setup_object_debug(struct kmem_cache *s,
1071 struct page *page, void *object) {}
41ecc55b 1072
3ec09742 1073static inline int alloc_debug_processing(struct kmem_cache *s,
ce71e27c 1074 struct page *page, void *object, unsigned long addr) { return 0; }
41ecc55b 1075
3ec09742 1076static inline int free_debug_processing(struct kmem_cache *s,
ce71e27c 1077 struct page *page, void *object, unsigned long addr) { return 0; }
41ecc55b 1078
41ecc55b
CL
1079static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1080 { return 1; }
1081static inline int check_object(struct kmem_cache *s, struct page *page,
1082 void *object, int active) { return 1; }
3ec09742 1083static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
ba0268a8
CL
1084static inline unsigned long kmem_cache_flags(unsigned long objsize,
1085 unsigned long flags, const char *name,
51cc5068 1086 void (*ctor)(void *))
ba0268a8
CL
1087{
1088 return flags;
1089}
41ecc55b 1090#define slub_debug 0
0f389ec6 1091
fdaa45e9
IM
1092#define disable_higher_order_debug 0
1093
0f389ec6
CL
1094static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1095 { return 0; }
26c02cf0
AB
1096static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1097 { return 0; }
205ab99d
CL
1098static inline void inc_slabs_node(struct kmem_cache *s, int node,
1099 int objects) {}
1100static inline void dec_slabs_node(struct kmem_cache *s, int node,
1101 int objects) {}
7d550c56
CL
1102
1103static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
1104 { return 0; }
1105
1106static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
1107 void *object) {}
1108
1109static inline void slab_free_hook(struct kmem_cache *s, void *x) {}
1110
1111static inline void slab_free_hook_irq(struct kmem_cache *s,
1112 void *object) {}
1113
41ecc55b 1114#endif
205ab99d 1115
81819f0f
CL
1116/*
1117 * Slab allocation and freeing
1118 */
65c3376a
CL
1119static inline struct page *alloc_slab_page(gfp_t flags, int node,
1120 struct kmem_cache_order_objects oo)
1121{
1122 int order = oo_order(oo);
1123
b1eeab67
VN
1124 flags |= __GFP_NOTRACK;
1125
2154a336 1126 if (node == NUMA_NO_NODE)
65c3376a
CL
1127 return alloc_pages(flags, order);
1128 else
6b65aaf3 1129 return alloc_pages_exact_node(node, flags, order);
65c3376a
CL
1130}
1131
81819f0f
CL
1132static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1133{
06428780 1134 struct page *page;
834f3d11 1135 struct kmem_cache_order_objects oo = s->oo;
ba52270d 1136 gfp_t alloc_gfp;
81819f0f 1137
b7a49f0d 1138 flags |= s->allocflags;
e12ba74d 1139
ba52270d
PE
1140 /*
1141 * Let the initial higher-order allocation fail under memory pressure
1142 * so we fall-back to the minimum order allocation.
1143 */
1144 alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
1145
1146 page = alloc_slab_page(alloc_gfp, node, oo);
65c3376a
CL
1147 if (unlikely(!page)) {
1148 oo = s->min;
1149 /*
1150 * Allocation may have failed due to fragmentation.
1151 * Try a lower order alloc if possible
1152 */
1153 page = alloc_slab_page(flags, node, oo);
1154 if (!page)
1155 return NULL;
81819f0f 1156
84e554e6 1157 stat(s, ORDER_FALLBACK);
65c3376a 1158 }
5a896d9e
VN
1159
1160 if (kmemcheck_enabled
5086c389 1161 && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
b1eeab67
VN
1162 int pages = 1 << oo_order(oo);
1163
1164 kmemcheck_alloc_shadow(page, oo_order(oo), flags, node);
1165
1166 /*
1167 * Objects from caches that have a constructor don't get
1168 * cleared when they're allocated, so we need to do it here.
1169 */
1170 if (s->ctor)
1171 kmemcheck_mark_uninitialized_pages(page, pages);
1172 else
1173 kmemcheck_mark_unallocated_pages(page, pages);
5a896d9e
VN
1174 }
1175
834f3d11 1176 page->objects = oo_objects(oo);
81819f0f
CL
1177 mod_zone_page_state(page_zone(page),
1178 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1179 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
65c3376a 1180 1 << oo_order(oo));
81819f0f
CL
1181
1182 return page;
1183}
1184
1185static void setup_object(struct kmem_cache *s, struct page *page,
1186 void *object)
1187{
3ec09742 1188 setup_object_debug(s, page, object);
4f104934 1189 if (unlikely(s->ctor))
51cc5068 1190 s->ctor(object);
81819f0f
CL
1191}
1192
1193static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1194{
1195 struct page *page;
81819f0f 1196 void *start;
81819f0f
CL
1197 void *last;
1198 void *p;
1199
6cb06229 1200 BUG_ON(flags & GFP_SLAB_BUG_MASK);
81819f0f 1201
6cb06229
CL
1202 page = allocate_slab(s,
1203 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
81819f0f
CL
1204 if (!page)
1205 goto out;
1206
205ab99d 1207 inc_slabs_node(s, page_to_nid(page), page->objects);
81819f0f
CL
1208 page->slab = s;
1209 page->flags |= 1 << PG_slab;
81819f0f
CL
1210
1211 start = page_address(page);
81819f0f
CL
1212
1213 if (unlikely(s->flags & SLAB_POISON))
834f3d11 1214 memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
81819f0f
CL
1215
1216 last = start;
224a88be 1217 for_each_object(p, s, start, page->objects) {
81819f0f
CL
1218 setup_object(s, page, last);
1219 set_freepointer(s, last, p);
1220 last = p;
1221 }
1222 setup_object(s, page, last);
a973e9dd 1223 set_freepointer(s, last, NULL);
81819f0f
CL
1224
1225 page->freelist = start;
1226 page->inuse = 0;
1227out:
81819f0f
CL
1228 return page;
1229}
1230
1231static void __free_slab(struct kmem_cache *s, struct page *page)
1232{
834f3d11
CL
1233 int order = compound_order(page);
1234 int pages = 1 << order;
81819f0f 1235
af537b0a 1236 if (kmem_cache_debug(s)) {
81819f0f
CL
1237 void *p;
1238
1239 slab_pad_check(s, page);
224a88be
CL
1240 for_each_object(p, s, page_address(page),
1241 page->objects)
81819f0f 1242 check_object(s, page, p, 0);
81819f0f
CL
1243 }
1244
b1eeab67 1245 kmemcheck_free_shadow(page, compound_order(page));
5a896d9e 1246
81819f0f
CL
1247 mod_zone_page_state(page_zone(page),
1248 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1249 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
06428780 1250 -pages);
81819f0f 1251
49bd5221
CL
1252 __ClearPageSlab(page);
1253 reset_page_mapcount(page);
1eb5ac64
NP
1254 if (current->reclaim_state)
1255 current->reclaim_state->reclaimed_slab += pages;
834f3d11 1256 __free_pages(page, order);
81819f0f
CL
1257}
1258
1259static void rcu_free_slab(struct rcu_head *h)
1260{
1261 struct page *page;
1262
1263 page = container_of((struct list_head *)h, struct page, lru);
1264 __free_slab(page->slab, page);
1265}
1266
1267static void free_slab(struct kmem_cache *s, struct page *page)
1268{
1269 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1270 /*
1271 * RCU free overloads the RCU head over the LRU
1272 */
1273 struct rcu_head *head = (void *)&page->lru;
1274
1275 call_rcu(head, rcu_free_slab);
1276 } else
1277 __free_slab(s, page);
1278}
1279
1280static void discard_slab(struct kmem_cache *s, struct page *page)
1281{
205ab99d 1282 dec_slabs_node(s, page_to_nid(page), page->objects);
81819f0f
CL
1283 free_slab(s, page);
1284}
1285
1286/*
1287 * Per slab locking using the pagelock
1288 */
1289static __always_inline void slab_lock(struct page *page)
1290{
1291 bit_spin_lock(PG_locked, &page->flags);
1292}
1293
1294static __always_inline void slab_unlock(struct page *page)
1295{
a76d3546 1296 __bit_spin_unlock(PG_locked, &page->flags);
81819f0f
CL
1297}
1298
1299static __always_inline int slab_trylock(struct page *page)
1300{
1301 int rc = 1;
1302
1303 rc = bit_spin_trylock(PG_locked, &page->flags);
1304 return rc;
1305}
1306
1307/*
1308 * Management of partially allocated slabs
1309 */
7c2e132c
CL
1310static void add_partial(struct kmem_cache_node *n,
1311 struct page *page, int tail)
81819f0f 1312{
e95eed57
CL
1313 spin_lock(&n->list_lock);
1314 n->nr_partial++;
7c2e132c
CL
1315 if (tail)
1316 list_add_tail(&page->lru, &n->partial);
1317 else
1318 list_add(&page->lru, &n->partial);
81819f0f
CL
1319 spin_unlock(&n->list_lock);
1320}
1321
0121c619 1322static void remove_partial(struct kmem_cache *s, struct page *page)
81819f0f
CL
1323{
1324 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1325
1326 spin_lock(&n->list_lock);
1327 list_del(&page->lru);
1328 n->nr_partial--;
1329 spin_unlock(&n->list_lock);
1330}
1331
1332/*
672bba3a 1333 * Lock slab and remove from the partial list.
81819f0f 1334 *
672bba3a 1335 * Must hold list_lock.
81819f0f 1336 */
0121c619
CL
1337static inline int lock_and_freeze_slab(struct kmem_cache_node *n,
1338 struct page *page)
81819f0f
CL
1339{
1340 if (slab_trylock(page)) {
1341 list_del(&page->lru);
1342 n->nr_partial--;
8a38082d 1343 __SetPageSlubFrozen(page);
81819f0f
CL
1344 return 1;
1345 }
1346 return 0;
1347}
1348
1349/*
672bba3a 1350 * Try to allocate a partial slab from a specific node.
81819f0f
CL
1351 */
1352static struct page *get_partial_node(struct kmem_cache_node *n)
1353{
1354 struct page *page;
1355
1356 /*
1357 * Racy check. If we mistakenly see no partial slabs then we
1358 * just allocate an empty slab. If we mistakenly try to get a
672bba3a
CL
1359 * partial slab and there is none available then get_partials()
1360 * will return NULL.
81819f0f
CL
1361 */
1362 if (!n || !n->nr_partial)
1363 return NULL;
1364
1365 spin_lock(&n->list_lock);
1366 list_for_each_entry(page, &n->partial, lru)
4b6f0750 1367 if (lock_and_freeze_slab(n, page))
81819f0f
CL
1368 goto out;
1369 page = NULL;
1370out:
1371 spin_unlock(&n->list_lock);
1372 return page;
1373}
1374
1375/*
672bba3a 1376 * Get a page from somewhere. Search in increasing NUMA distances.
81819f0f
CL
1377 */
1378static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1379{
1380#ifdef CONFIG_NUMA
1381 struct zonelist *zonelist;
dd1a239f 1382 struct zoneref *z;
54a6eb5c
MG
1383 struct zone *zone;
1384 enum zone_type high_zoneidx = gfp_zone(flags);
81819f0f
CL
1385 struct page *page;
1386
1387 /*
672bba3a
CL
1388 * The defrag ratio allows a configuration of the tradeoffs between
1389 * inter node defragmentation and node local allocations. A lower
1390 * defrag_ratio increases the tendency to do local allocations
1391 * instead of attempting to obtain partial slabs from other nodes.
81819f0f 1392 *
672bba3a
CL
1393 * If the defrag_ratio is set to 0 then kmalloc() always
1394 * returns node local objects. If the ratio is higher then kmalloc()
1395 * may return off node objects because partial slabs are obtained
1396 * from other nodes and filled up.
81819f0f 1397 *
6446faa2 1398 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
672bba3a
CL
1399 * defrag_ratio = 1000) then every (well almost) allocation will
1400 * first attempt to defrag slab caches on other nodes. This means
1401 * scanning over all nodes to look for partial slabs which may be
1402 * expensive if we do it every time we are trying to find a slab
1403 * with available objects.
81819f0f 1404 */
9824601e
CL
1405 if (!s->remote_node_defrag_ratio ||
1406 get_cycles() % 1024 > s->remote_node_defrag_ratio)
81819f0f
CL
1407 return NULL;
1408
c0ff7453 1409 get_mems_allowed();
0e88460d 1410 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
54a6eb5c 1411 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
81819f0f
CL
1412 struct kmem_cache_node *n;
1413
54a6eb5c 1414 n = get_node(s, zone_to_nid(zone));
81819f0f 1415
54a6eb5c 1416 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
3b89d7d8 1417 n->nr_partial > s->min_partial) {
81819f0f 1418 page = get_partial_node(n);
c0ff7453
MX
1419 if (page) {
1420 put_mems_allowed();
81819f0f 1421 return page;
c0ff7453 1422 }
81819f0f
CL
1423 }
1424 }
c0ff7453 1425 put_mems_allowed();
81819f0f
CL
1426#endif
1427 return NULL;
1428}
1429
1430/*
1431 * Get a partial page, lock it and return it.
1432 */
1433static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1434{
1435 struct page *page;
2154a336 1436 int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;
81819f0f
CL
1437
1438 page = get_partial_node(get_node(s, searchnode));
bc6488e9 1439 if (page || node != -1)
81819f0f
CL
1440 return page;
1441
1442 return get_any_partial(s, flags);
1443}
1444
1445/*
1446 * Move a page back to the lists.
1447 *
1448 * Must be called with the slab lock held.
1449 *
1450 * On exit the slab lock will have been dropped.
1451 */
7c2e132c 1452static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
81819f0f 1453{
e95eed57
CL
1454 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1455
8a38082d 1456 __ClearPageSlubFrozen(page);
81819f0f 1457 if (page->inuse) {
e95eed57 1458
a973e9dd 1459 if (page->freelist) {
7c2e132c 1460 add_partial(n, page, tail);
84e554e6 1461 stat(s, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
8ff12cfc 1462 } else {
84e554e6 1463 stat(s, DEACTIVATE_FULL);
af537b0a 1464 if (kmem_cache_debug(s) && (s->flags & SLAB_STORE_USER))
8ff12cfc
CL
1465 add_full(n, page);
1466 }
81819f0f
CL
1467 slab_unlock(page);
1468 } else {
84e554e6 1469 stat(s, DEACTIVATE_EMPTY);
3b89d7d8 1470 if (n->nr_partial < s->min_partial) {
e95eed57 1471 /*
672bba3a
CL
1472 * Adding an empty slab to the partial slabs in order
1473 * to avoid page allocator overhead. This slab needs
1474 * to come after the other slabs with objects in
6446faa2
CL
1475 * so that the others get filled first. That way the
1476 * size of the partial list stays small.
1477 *
0121c619
CL
1478 * kmem_cache_shrink can reclaim any empty slabs from
1479 * the partial list.
e95eed57 1480 */
7c2e132c 1481 add_partial(n, page, 1);
e95eed57
CL
1482 slab_unlock(page);
1483 } else {
1484 slab_unlock(page);
84e554e6 1485 stat(s, FREE_SLAB);
e95eed57
CL
1486 discard_slab(s, page);
1487 }
81819f0f
CL
1488 }
1489}
1490
1491/*
1492 * Remove the cpu slab
1493 */
dfb4f096 1494static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
81819f0f 1495{
dfb4f096 1496 struct page *page = c->page;
7c2e132c 1497 int tail = 1;
8ff12cfc 1498
b773ad73 1499 if (page->freelist)
84e554e6 1500 stat(s, DEACTIVATE_REMOTE_FREES);
894b8788 1501 /*
6446faa2 1502 * Merge cpu freelist into slab freelist. Typically we get here
894b8788
CL
1503 * because both freelists are empty. So this is unlikely
1504 * to occur.
1505 */
a973e9dd 1506 while (unlikely(c->freelist)) {
894b8788
CL
1507 void **object;
1508
7c2e132c
CL
1509 tail = 0; /* Hot objects. Put the slab first */
1510
894b8788 1511 /* Retrieve object from cpu_freelist */
dfb4f096 1512 object = c->freelist;
ff12059e 1513 c->freelist = get_freepointer(s, c->freelist);
894b8788
CL
1514
1515 /* And put onto the regular freelist */
ff12059e 1516 set_freepointer(s, object, page->freelist);
894b8788
CL
1517 page->freelist = object;
1518 page->inuse--;
1519 }
dfb4f096 1520 c->page = NULL;
7c2e132c 1521 unfreeze_slab(s, page, tail);
81819f0f
CL
1522}
1523
dfb4f096 1524static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
81819f0f 1525{
84e554e6 1526 stat(s, CPUSLAB_FLUSH);
dfb4f096
CL
1527 slab_lock(c->page);
1528 deactivate_slab(s, c);
81819f0f
CL
1529}
1530
1531/*
1532 * Flush cpu slab.
6446faa2 1533 *
81819f0f
CL
1534 * Called from IPI handler with interrupts disabled.
1535 */
0c710013 1536static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
81819f0f 1537{
9dfc6e68 1538 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
81819f0f 1539
dfb4f096
CL
1540 if (likely(c && c->page))
1541 flush_slab(s, c);
81819f0f
CL
1542}
1543
1544static void flush_cpu_slab(void *d)
1545{
1546 struct kmem_cache *s = d;
81819f0f 1547
dfb4f096 1548 __flush_cpu_slab(s, smp_processor_id());
81819f0f
CL
1549}
1550
1551static void flush_all(struct kmem_cache *s)
1552{
15c8b6c1 1553 on_each_cpu(flush_cpu_slab, s, 1);
81819f0f
CL
1554}
1555
dfb4f096
CL
1556/*
1557 * Check if the objects in a per cpu structure fit numa
1558 * locality expectations.
1559 */
1560static inline int node_match(struct kmem_cache_cpu *c, int node)
1561{
1562#ifdef CONFIG_NUMA
2154a336 1563 if (node != NUMA_NO_NODE && c->node != node)
dfb4f096
CL
1564 return 0;
1565#endif
1566 return 1;
1567}
1568
781b2ba6
PE
1569static int count_free(struct page *page)
1570{
1571 return page->objects - page->inuse;
1572}
1573
1574static unsigned long count_partial(struct kmem_cache_node *n,
1575 int (*get_count)(struct page *))
1576{
1577 unsigned long flags;
1578 unsigned long x = 0;
1579 struct page *page;
1580
1581 spin_lock_irqsave(&n->list_lock, flags);
1582 list_for_each_entry(page, &n->partial, lru)
1583 x += get_count(page);
1584 spin_unlock_irqrestore(&n->list_lock, flags);
1585 return x;
1586}
1587
26c02cf0
AB
1588static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
1589{
1590#ifdef CONFIG_SLUB_DEBUG
1591 return atomic_long_read(&n->total_objects);
1592#else
1593 return 0;
1594#endif
1595}
1596
781b2ba6
PE
1597static noinline void
1598slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
1599{
1600 int node;
1601
1602 printk(KERN_WARNING
1603 "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1604 nid, gfpflags);
1605 printk(KERN_WARNING " cache: %s, object size: %d, buffer size: %d, "
1606 "default order: %d, min order: %d\n", s->name, s->objsize,
1607 s->size, oo_order(s->oo), oo_order(s->min));
1608
fa5ec8a1
DR
1609 if (oo_order(s->min) > get_order(s->objsize))
1610 printk(KERN_WARNING " %s debugging increased min order, use "
1611 "slub_debug=O to disable.\n", s->name);
1612
781b2ba6
PE
1613 for_each_online_node(node) {
1614 struct kmem_cache_node *n = get_node(s, node);
1615 unsigned long nr_slabs;
1616 unsigned long nr_objs;
1617 unsigned long nr_free;
1618
1619 if (!n)
1620 continue;
1621
26c02cf0
AB
1622 nr_free = count_partial(n, count_free);
1623 nr_slabs = node_nr_slabs(n);
1624 nr_objs = node_nr_objs(n);
781b2ba6
PE
1625
1626 printk(KERN_WARNING
1627 " node %d: slabs: %ld, objs: %ld, free: %ld\n",
1628 node, nr_slabs, nr_objs, nr_free);
1629 }
1630}
1631
81819f0f 1632/*
894b8788
CL
1633 * Slow path. The lockless freelist is empty or we need to perform
1634 * debugging duties.
1635 *
1636 * Interrupts are disabled.
81819f0f 1637 *
894b8788
CL
1638 * Processing is still very fast if new objects have been freed to the
1639 * regular freelist. In that case we simply take over the regular freelist
1640 * as the lockless freelist and zap the regular freelist.
81819f0f 1641 *
894b8788
CL
1642 * If that is not working then we fall back to the partial lists. We take the
1643 * first element of the freelist as the object to allocate now and move the
1644 * rest of the freelist to the lockless freelist.
81819f0f 1645 *
894b8788 1646 * And if we were unable to get a new slab from the partial slab lists then
6446faa2
CL
1647 * we need to allocate a new slab. This is the slowest path since it involves
1648 * a call to the page allocator and the setup of a new slab.
81819f0f 1649 */
ce71e27c
EGM
1650static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
1651 unsigned long addr, struct kmem_cache_cpu *c)
81819f0f 1652{
81819f0f 1653 void **object;
dfb4f096 1654 struct page *new;
81819f0f 1655
e72e9c23
LT
1656 /* We handle __GFP_ZERO in the caller */
1657 gfpflags &= ~__GFP_ZERO;
1658
dfb4f096 1659 if (!c->page)
81819f0f
CL
1660 goto new_slab;
1661
dfb4f096
CL
1662 slab_lock(c->page);
1663 if (unlikely(!node_match(c, node)))
81819f0f 1664 goto another_slab;
6446faa2 1665
84e554e6 1666 stat(s, ALLOC_REFILL);
6446faa2 1667
894b8788 1668load_freelist:
dfb4f096 1669 object = c->page->freelist;
a973e9dd 1670 if (unlikely(!object))
81819f0f 1671 goto another_slab;
af537b0a 1672 if (kmem_cache_debug(s))
81819f0f
CL
1673 goto debug;
1674
ff12059e 1675 c->freelist = get_freepointer(s, object);
39b26464 1676 c->page->inuse = c->page->objects;
a973e9dd 1677 c->page->freelist = NULL;
dfb4f096 1678 c->node = page_to_nid(c->page);
1f84260c 1679unlock_out:
dfb4f096 1680 slab_unlock(c->page);
84e554e6 1681 stat(s, ALLOC_SLOWPATH);
81819f0f
CL
1682 return object;
1683
1684another_slab:
dfb4f096 1685 deactivate_slab(s, c);
81819f0f
CL
1686
1687new_slab:
dfb4f096
CL
1688 new = get_partial(s, gfpflags, node);
1689 if (new) {
1690 c->page = new;
84e554e6 1691 stat(s, ALLOC_FROM_PARTIAL);
894b8788 1692 goto load_freelist;
81819f0f
CL
1693 }
1694
c1d50836 1695 gfpflags &= gfp_allowed_mask;
b811c202
CL
1696 if (gfpflags & __GFP_WAIT)
1697 local_irq_enable();
1698
dfb4f096 1699 new = new_slab(s, gfpflags, node);
b811c202
CL
1700
1701 if (gfpflags & __GFP_WAIT)
1702 local_irq_disable();
1703
dfb4f096 1704 if (new) {
9dfc6e68 1705 c = __this_cpu_ptr(s->cpu_slab);
84e554e6 1706 stat(s, ALLOC_SLAB);
05aa3450 1707 if (c->page)
dfb4f096 1708 flush_slab(s, c);
dfb4f096 1709 slab_lock(new);
8a38082d 1710 __SetPageSlubFrozen(new);
dfb4f096 1711 c->page = new;
4b6f0750 1712 goto load_freelist;
81819f0f 1713 }
95f85989
PE
1714 if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
1715 slab_out_of_memory(s, gfpflags, node);
71c7a06f 1716 return NULL;
81819f0f 1717debug:
dfb4f096 1718 if (!alloc_debug_processing(s, c->page, object, addr))
81819f0f 1719 goto another_slab;
894b8788 1720
dfb4f096 1721 c->page->inuse++;
ff12059e 1722 c->page->freelist = get_freepointer(s, object);
ee3c72a1 1723 c->node = -1;
1f84260c 1724 goto unlock_out;
894b8788
CL
1725}
1726
1727/*
1728 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1729 * have the fastpath folded into their functions. So no function call
1730 * overhead for requests that can be satisfied on the fastpath.
1731 *
1732 * The fastpath works by first checking if the lockless freelist can be used.
1733 * If not then __slab_alloc is called for slow processing.
1734 *
1735 * Otherwise we can simply pick the next object from the lockless free list.
1736 */
06428780 1737static __always_inline void *slab_alloc(struct kmem_cache *s,
ce71e27c 1738 gfp_t gfpflags, int node, unsigned long addr)
894b8788 1739{
894b8788 1740 void **object;
dfb4f096 1741 struct kmem_cache_cpu *c;
1f84260c
CL
1742 unsigned long flags;
1743
c016b0bd 1744 if (slab_pre_alloc_hook(s, gfpflags))
773ff60e 1745 return NULL;
1f84260c 1746
894b8788 1747 local_irq_save(flags);
9dfc6e68
CL
1748 c = __this_cpu_ptr(s->cpu_slab);
1749 object = c->freelist;
9dfc6e68 1750 if (unlikely(!object || !node_match(c, node)))
894b8788 1751
dfb4f096 1752 object = __slab_alloc(s, gfpflags, node, addr, c);
894b8788
CL
1753
1754 else {
ff12059e 1755 c->freelist = get_freepointer(s, object);
84e554e6 1756 stat(s, ALLOC_FASTPATH);
894b8788
CL
1757 }
1758 local_irq_restore(flags);
d07dbea4 1759
74e2134f 1760 if (unlikely(gfpflags & __GFP_ZERO) && object)
ff12059e 1761 memset(object, 0, s->objsize);
d07dbea4 1762
c016b0bd 1763 slab_post_alloc_hook(s, gfpflags, object);
5a896d9e 1764
894b8788 1765 return object;
81819f0f
CL
1766}
1767
1768void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1769{
2154a336 1770 void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
5b882be4 1771
ca2b84cb 1772 trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
5b882be4
EGM
1773
1774 return ret;
81819f0f
CL
1775}
1776EXPORT_SYMBOL(kmem_cache_alloc);
1777
0f24f128 1778#ifdef CONFIG_TRACING
5b882be4
EGM
1779void *kmem_cache_alloc_notrace(struct kmem_cache *s, gfp_t gfpflags)
1780{
2154a336 1781 return slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
5b882be4
EGM
1782}
1783EXPORT_SYMBOL(kmem_cache_alloc_notrace);
1784#endif
1785
81819f0f
CL
1786#ifdef CONFIG_NUMA
1787void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1788{
5b882be4
EGM
1789 void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
1790
ca2b84cb
EGM
1791 trace_kmem_cache_alloc_node(_RET_IP_, ret,
1792 s->objsize, s->size, gfpflags, node);
5b882be4
EGM
1793
1794 return ret;
81819f0f
CL
1795}
1796EXPORT_SYMBOL(kmem_cache_alloc_node);
1797#endif
1798
0f24f128 1799#ifdef CONFIG_TRACING
5b882be4
EGM
1800void *kmem_cache_alloc_node_notrace(struct kmem_cache *s,
1801 gfp_t gfpflags,
1802 int node)
1803{
1804 return slab_alloc(s, gfpflags, node, _RET_IP_);
1805}
1806EXPORT_SYMBOL(kmem_cache_alloc_node_notrace);
1807#endif
1808
81819f0f 1809/*
894b8788
CL
1810 * Slow patch handling. This may still be called frequently since objects
1811 * have a longer lifetime than the cpu slabs in most processing loads.
81819f0f 1812 *
894b8788
CL
1813 * So we still attempt to reduce cache line usage. Just take the slab
1814 * lock and free the item. If there is no additional partial page
1815 * handling required then we can return immediately.
81819f0f 1816 */
894b8788 1817static void __slab_free(struct kmem_cache *s, struct page *page,
ff12059e 1818 void *x, unsigned long addr)
81819f0f
CL
1819{
1820 void *prior;
1821 void **object = (void *)x;
81819f0f 1822
84e554e6 1823 stat(s, FREE_SLOWPATH);
81819f0f
CL
1824 slab_lock(page);
1825
af537b0a 1826 if (kmem_cache_debug(s))
81819f0f 1827 goto debug;
6446faa2 1828
81819f0f 1829checks_ok:
ff12059e
CL
1830 prior = page->freelist;
1831 set_freepointer(s, object, prior);
81819f0f
CL
1832 page->freelist = object;
1833 page->inuse--;
1834
8a38082d 1835 if (unlikely(PageSlubFrozen(page))) {
84e554e6 1836 stat(s, FREE_FROZEN);
81819f0f 1837 goto out_unlock;
8ff12cfc 1838 }
81819f0f
CL
1839
1840 if (unlikely(!page->inuse))
1841 goto slab_empty;
1842
1843 /*
6446faa2 1844 * Objects left in the slab. If it was not on the partial list before
81819f0f
CL
1845 * then add it.
1846 */
a973e9dd 1847 if (unlikely(!prior)) {
7c2e132c 1848 add_partial(get_node(s, page_to_nid(page)), page, 1);
84e554e6 1849 stat(s, FREE_ADD_PARTIAL);
8ff12cfc 1850 }
81819f0f
CL
1851
1852out_unlock:
1853 slab_unlock(page);
81819f0f
CL
1854 return;
1855
1856slab_empty:
a973e9dd 1857 if (prior) {
81819f0f 1858 /*
672bba3a 1859 * Slab still on the partial list.
81819f0f
CL
1860 */
1861 remove_partial(s, page);
84e554e6 1862 stat(s, FREE_REMOVE_PARTIAL);
8ff12cfc 1863 }
81819f0f 1864 slab_unlock(page);
84e554e6 1865 stat(s, FREE_SLAB);
81819f0f 1866 discard_slab(s, page);
81819f0f
CL
1867 return;
1868
1869debug:
3ec09742 1870 if (!free_debug_processing(s, page, x, addr))
77c5e2d0 1871 goto out_unlock;
77c5e2d0 1872 goto checks_ok;
81819f0f
CL
1873}
1874
894b8788
CL
1875/*
1876 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1877 * can perform fastpath freeing without additional function calls.
1878 *
1879 * The fastpath is only possible if we are freeing to the current cpu slab
1880 * of this processor. This typically the case if we have just allocated
1881 * the item before.
1882 *
1883 * If fastpath is not possible then fall back to __slab_free where we deal
1884 * with all sorts of special processing.
1885 */
06428780 1886static __always_inline void slab_free(struct kmem_cache *s,
ce71e27c 1887 struct page *page, void *x, unsigned long addr)
894b8788
CL
1888{
1889 void **object = (void *)x;
dfb4f096 1890 struct kmem_cache_cpu *c;
1f84260c
CL
1891 unsigned long flags;
1892
c016b0bd
CL
1893 slab_free_hook(s, x);
1894
894b8788 1895 local_irq_save(flags);
9dfc6e68 1896 c = __this_cpu_ptr(s->cpu_slab);
c016b0bd
CL
1897
1898 slab_free_hook_irq(s, x);
1899
ee3c72a1 1900 if (likely(page == c->page && c->node >= 0)) {
ff12059e 1901 set_freepointer(s, object, c->freelist);
dfb4f096 1902 c->freelist = object;
84e554e6 1903 stat(s, FREE_FASTPATH);
894b8788 1904 } else
ff12059e 1905 __slab_free(s, page, x, addr);
894b8788
CL
1906
1907 local_irq_restore(flags);
1908}
1909
81819f0f
CL
1910void kmem_cache_free(struct kmem_cache *s, void *x)
1911{
77c5e2d0 1912 struct page *page;
81819f0f 1913
b49af68f 1914 page = virt_to_head_page(x);
81819f0f 1915
ce71e27c 1916 slab_free(s, page, x, _RET_IP_);
5b882be4 1917
ca2b84cb 1918 trace_kmem_cache_free(_RET_IP_, x);
81819f0f
CL
1919}
1920EXPORT_SYMBOL(kmem_cache_free);
1921
e9beef18 1922/* Figure out on which slab page the object resides */
81819f0f
CL
1923static struct page *get_object_page(const void *x)
1924{
b49af68f 1925 struct page *page = virt_to_head_page(x);
81819f0f
CL
1926
1927 if (!PageSlab(page))
1928 return NULL;
1929
1930 return page;
1931}
1932
1933/*
672bba3a
CL
1934 * Object placement in a slab is made very easy because we always start at
1935 * offset 0. If we tune the size of the object to the alignment then we can
1936 * get the required alignment by putting one properly sized object after
1937 * another.
81819f0f
CL
1938 *
1939 * Notice that the allocation order determines the sizes of the per cpu
1940 * caches. Each processor has always one slab available for allocations.
1941 * Increasing the allocation order reduces the number of times that slabs
672bba3a 1942 * must be moved on and off the partial lists and is therefore a factor in
81819f0f 1943 * locking overhead.
81819f0f
CL
1944 */
1945
1946/*
1947 * Mininum / Maximum order of slab pages. This influences locking overhead
1948 * and slab fragmentation. A higher order reduces the number of partial slabs
1949 * and increases the number of allocations possible without having to
1950 * take the list_lock.
1951 */
1952static int slub_min_order;
114e9e89 1953static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
9b2cd506 1954static int slub_min_objects;
81819f0f
CL
1955
1956/*
1957 * Merge control. If this is set then no merging of slab caches will occur.
672bba3a 1958 * (Could be removed. This was introduced to pacify the merge skeptics.)
81819f0f
CL
1959 */
1960static int slub_nomerge;
1961
81819f0f
CL
1962/*
1963 * Calculate the order of allocation given an slab object size.
1964 *
672bba3a
CL
1965 * The order of allocation has significant impact on performance and other
1966 * system components. Generally order 0 allocations should be preferred since
1967 * order 0 does not cause fragmentation in the page allocator. Larger objects
1968 * be problematic to put into order 0 slabs because there may be too much
c124f5b5 1969 * unused space left. We go to a higher order if more than 1/16th of the slab
672bba3a
CL
1970 * would be wasted.
1971 *
1972 * In order to reach satisfactory performance we must ensure that a minimum
1973 * number of objects is in one slab. Otherwise we may generate too much
1974 * activity on the partial lists which requires taking the list_lock. This is
1975 * less a concern for large slabs though which are rarely used.
81819f0f 1976 *
672bba3a
CL
1977 * slub_max_order specifies the order where we begin to stop considering the
1978 * number of objects in a slab as critical. If we reach slub_max_order then
1979 * we try to keep the page order as low as possible. So we accept more waste
1980 * of space in favor of a small page order.
81819f0f 1981 *
672bba3a
CL
1982 * Higher order allocations also allow the placement of more objects in a
1983 * slab and thereby reduce object handling overhead. If the user has
1984 * requested a higher mininum order then we start with that one instead of
1985 * the smallest order which will fit the object.
81819f0f 1986 */
5e6d444e
CL
1987static inline int slab_order(int size, int min_objects,
1988 int max_order, int fract_leftover)
81819f0f
CL
1989{
1990 int order;
1991 int rem;
6300ea75 1992 int min_order = slub_min_order;
81819f0f 1993
210b5c06
CG
1994 if ((PAGE_SIZE << min_order) / size > MAX_OBJS_PER_PAGE)
1995 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
39b26464 1996
6300ea75 1997 for (order = max(min_order,
5e6d444e
CL
1998 fls(min_objects * size - 1) - PAGE_SHIFT);
1999 order <= max_order; order++) {
81819f0f 2000
5e6d444e 2001 unsigned long slab_size = PAGE_SIZE << order;
81819f0f 2002
5e6d444e 2003 if (slab_size < min_objects * size)
81819f0f
CL
2004 continue;
2005
2006 rem = slab_size % size;
2007
5e6d444e 2008 if (rem <= slab_size / fract_leftover)
81819f0f
CL
2009 break;
2010
2011 }
672bba3a 2012
81819f0f
CL
2013 return order;
2014}
2015
5e6d444e
CL
2016static inline int calculate_order(int size)
2017{
2018 int order;
2019 int min_objects;
2020 int fraction;
e8120ff1 2021 int max_objects;
5e6d444e
CL
2022
2023 /*
2024 * Attempt to find best configuration for a slab. This
2025 * works by first attempting to generate a layout with
2026 * the best configuration and backing off gradually.
2027 *
2028 * First we reduce the acceptable waste in a slab. Then
2029 * we reduce the minimum objects required in a slab.
2030 */
2031 min_objects = slub_min_objects;
9b2cd506
CL
2032 if (!min_objects)
2033 min_objects = 4 * (fls(nr_cpu_ids) + 1);
e8120ff1
ZY
2034 max_objects = (PAGE_SIZE << slub_max_order)/size;
2035 min_objects = min(min_objects, max_objects);
2036
5e6d444e 2037 while (min_objects > 1) {
c124f5b5 2038 fraction = 16;
5e6d444e
CL
2039 while (fraction >= 4) {
2040 order = slab_order(size, min_objects,
2041 slub_max_order, fraction);
2042 if (order <= slub_max_order)
2043 return order;
2044 fraction /= 2;
2045 }
5086c389 2046 min_objects--;
5e6d444e
CL
2047 }
2048
2049 /*
2050 * We were unable to place multiple objects in a slab. Now
2051 * lets see if we can place a single object there.
2052 */
2053 order = slab_order(size, 1, slub_max_order, 1);
2054 if (order <= slub_max_order)
2055 return order;
2056
2057 /*
2058 * Doh this slab cannot be placed using slub_max_order.
2059 */
2060 order = slab_order(size, 1, MAX_ORDER, 1);
818cf590 2061 if (order < MAX_ORDER)
5e6d444e
CL
2062 return order;
2063 return -ENOSYS;
2064}
2065
81819f0f 2066/*
672bba3a 2067 * Figure out what the alignment of the objects will be.
81819f0f
CL
2068 */
2069static unsigned long calculate_alignment(unsigned long flags,
2070 unsigned long align, unsigned long size)
2071{
2072 /*
6446faa2
CL
2073 * If the user wants hardware cache aligned objects then follow that
2074 * suggestion if the object is sufficiently large.
81819f0f 2075 *
6446faa2
CL
2076 * The hardware cache alignment cannot override the specified
2077 * alignment though. If that is greater then use it.
81819f0f 2078 */
b6210386
NP
2079 if (flags & SLAB_HWCACHE_ALIGN) {
2080 unsigned long ralign = cache_line_size();
2081 while (size <= ralign / 2)
2082 ralign /= 2;
2083 align = max(align, ralign);
2084 }
81819f0f
CL
2085
2086 if (align < ARCH_SLAB_MINALIGN)
b6210386 2087 align = ARCH_SLAB_MINALIGN;
81819f0f
CL
2088
2089 return ALIGN(align, sizeof(void *));
2090}
2091
5595cffc
PE
2092static void
2093init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
81819f0f
CL
2094{
2095 n->nr_partial = 0;
81819f0f
CL
2096 spin_lock_init(&n->list_lock);
2097 INIT_LIST_HEAD(&n->partial);
8ab1372f 2098#ifdef CONFIG_SLUB_DEBUG
0f389ec6 2099 atomic_long_set(&n->nr_slabs, 0);
02b71b70 2100 atomic_long_set(&n->total_objects, 0);
643b1138 2101 INIT_LIST_HEAD(&n->full);
8ab1372f 2102#endif
81819f0f
CL
2103}
2104
55136592 2105static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
4c93c355 2106{
6c182dc0
CL
2107 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
2108 SLUB_PAGE_SHIFT * sizeof(struct kmem_cache_cpu));
4c93c355 2109
6c182dc0 2110 s->cpu_slab = alloc_percpu(struct kmem_cache_cpu);
4c93c355 2111
6c182dc0 2112 return s->cpu_slab != NULL;
4c93c355 2113}
4c93c355 2114
81819f0f 2115#ifdef CONFIG_NUMA
51df1142
CL
2116static struct kmem_cache *kmem_cache_node;
2117
81819f0f
CL
2118/*
2119 * No kmalloc_node yet so do it by hand. We know that this is the first
2120 * slab on the node for this slabcache. There are no concurrent accesses
2121 * possible.
2122 *
2123 * Note that this function only works on the kmalloc_node_cache
4c93c355
CL
2124 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2125 * memory on a fresh node that has no slab structures yet.
81819f0f 2126 */
55136592 2127static void early_kmem_cache_node_alloc(int node)
81819f0f
CL
2128{
2129 struct page *page;
2130 struct kmem_cache_node *n;
ba84c73c 2131 unsigned long flags;
81819f0f 2132
51df1142 2133 BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
81819f0f 2134
51df1142 2135 page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
81819f0f
CL
2136
2137 BUG_ON(!page);
a2f92ee7
CL
2138 if (page_to_nid(page) != node) {
2139 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2140 "node %d\n", node);
2141 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2142 "in order to be able to continue\n");
2143 }
2144
81819f0f
CL
2145 n = page->freelist;
2146 BUG_ON(!n);
51df1142 2147 page->freelist = get_freepointer(kmem_cache_node, n);
81819f0f 2148 page->inuse++;
51df1142 2149 kmem_cache_node->node[node] = n;
8ab1372f 2150#ifdef CONFIG_SLUB_DEBUG
51df1142
CL
2151 init_object(kmem_cache_node, n, 1);
2152 init_tracking(kmem_cache_node, n);
8ab1372f 2153#endif
51df1142
CL
2154 init_kmem_cache_node(n, kmem_cache_node);
2155 inc_slabs_node(kmem_cache_node, node, page->objects);
6446faa2 2156
ba84c73c 2157 /*
2158 * lockdep requires consistent irq usage for each lock
2159 * so even though there cannot be a race this early in
2160 * the boot sequence, we still disable irqs.
2161 */
2162 local_irq_save(flags);
7c2e132c 2163 add_partial(n, page, 0);
ba84c73c 2164 local_irq_restore(flags);
81819f0f
CL
2165}
2166
2167static void free_kmem_cache_nodes(struct kmem_cache *s)
2168{
2169 int node;
2170
f64dc58c 2171 for_each_node_state(node, N_NORMAL_MEMORY) {
81819f0f 2172 struct kmem_cache_node *n = s->node[node];
51df1142 2173
73367bd8 2174 if (n)
51df1142
CL
2175 kmem_cache_free(kmem_cache_node, n);
2176
81819f0f
CL
2177 s->node[node] = NULL;
2178 }
2179}
2180
55136592 2181static int init_kmem_cache_nodes(struct kmem_cache *s)
81819f0f
CL
2182{
2183 int node;
81819f0f 2184
f64dc58c 2185 for_each_node_state(node, N_NORMAL_MEMORY) {
81819f0f
CL
2186 struct kmem_cache_node *n;
2187
73367bd8 2188 if (slab_state == DOWN) {
55136592 2189 early_kmem_cache_node_alloc(node);
73367bd8
AD
2190 continue;
2191 }
51df1142 2192 n = kmem_cache_alloc_node(kmem_cache_node,
55136592 2193 GFP_KERNEL, node);
81819f0f 2194
73367bd8
AD
2195 if (!n) {
2196 free_kmem_cache_nodes(s);
2197 return 0;
81819f0f 2198 }
73367bd8 2199
81819f0f 2200 s->node[node] = n;
5595cffc 2201 init_kmem_cache_node(n, s);
81819f0f
CL
2202 }
2203 return 1;
2204}
2205#else
2206static void free_kmem_cache_nodes(struct kmem_cache *s)
2207{
2208}
2209
55136592 2210static int init_kmem_cache_nodes(struct kmem_cache *s)
81819f0f 2211{
5595cffc 2212 init_kmem_cache_node(&s->local_node, s);
81819f0f
CL
2213 return 1;
2214}
2215#endif
2216
c0bdb232 2217static void set_min_partial(struct kmem_cache *s, unsigned long min)
3b89d7d8
DR
2218{
2219 if (min < MIN_PARTIAL)
2220 min = MIN_PARTIAL;
2221 else if (min > MAX_PARTIAL)
2222 min = MAX_PARTIAL;
2223 s->min_partial = min;
2224}
2225
81819f0f
CL
2226/*
2227 * calculate_sizes() determines the order and the distribution of data within
2228 * a slab object.
2229 */
06b285dc 2230static int calculate_sizes(struct kmem_cache *s, int forced_order)
81819f0f
CL
2231{
2232 unsigned long flags = s->flags;
2233 unsigned long size = s->objsize;
2234 unsigned long align = s->align;
834f3d11 2235 int order;
81819f0f 2236
d8b42bf5
CL
2237 /*
2238 * Round up object size to the next word boundary. We can only
2239 * place the free pointer at word boundaries and this determines
2240 * the possible location of the free pointer.
2241 */
2242 size = ALIGN(size, sizeof(void *));
2243
2244#ifdef CONFIG_SLUB_DEBUG
81819f0f
CL
2245 /*
2246 * Determine if we can poison the object itself. If the user of
2247 * the slab may touch the object after free or before allocation
2248 * then we should never poison the object itself.
2249 */
2250 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
c59def9f 2251 !s->ctor)
81819f0f
CL
2252 s->flags |= __OBJECT_POISON;
2253 else
2254 s->flags &= ~__OBJECT_POISON;
2255
81819f0f
CL
2256
2257 /*
672bba3a 2258 * If we are Redzoning then check if there is some space between the
81819f0f 2259 * end of the object and the free pointer. If not then add an
672bba3a 2260 * additional word to have some bytes to store Redzone information.
81819f0f
CL
2261 */
2262 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2263 size += sizeof(void *);
41ecc55b 2264#endif
81819f0f
CL
2265
2266 /*
672bba3a
CL
2267 * With that we have determined the number of bytes in actual use
2268 * by the object. This is the potential offset to the free pointer.
81819f0f
CL
2269 */
2270 s->inuse = size;
2271
2272 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
c59def9f 2273 s->ctor)) {
81819f0f
CL
2274 /*
2275 * Relocate free pointer after the object if it is not
2276 * permitted to overwrite the first word of the object on
2277 * kmem_cache_free.
2278 *
2279 * This is the case if we do RCU, have a constructor or
2280 * destructor or are poisoning the objects.
2281 */
2282 s->offset = size;
2283 size += sizeof(void *);
2284 }
2285
c12b3c62 2286#ifdef CONFIG_SLUB_DEBUG
81819f0f
CL
2287 if (flags & SLAB_STORE_USER)
2288 /*
2289 * Need to store information about allocs and frees after
2290 * the object.
2291 */
2292 size += 2 * sizeof(struct track);
2293
be7b3fbc 2294 if (flags & SLAB_RED_ZONE)
81819f0f
CL
2295 /*
2296 * Add some empty padding so that we can catch
2297 * overwrites from earlier objects rather than let
2298 * tracking information or the free pointer be
0211a9c8 2299 * corrupted if a user writes before the start
81819f0f
CL
2300 * of the object.
2301 */
2302 size += sizeof(void *);
41ecc55b 2303#endif
672bba3a 2304
81819f0f
CL
2305 /*
2306 * Determine the alignment based on various parameters that the
65c02d4c
CL
2307 * user specified and the dynamic determination of cache line size
2308 * on bootup.
81819f0f
CL
2309 */
2310 align = calculate_alignment(flags, align, s->objsize);
dcb0ce1b 2311 s->align = align;
81819f0f
CL
2312
2313 /*
2314 * SLUB stores one object immediately after another beginning from
2315 * offset 0. In order to align the objects we have to simply size
2316 * each object to conform to the alignment.
2317 */
2318 size = ALIGN(size, align);
2319 s->size = size;
06b285dc
CL
2320 if (forced_order >= 0)
2321 order = forced_order;
2322 else
2323 order = calculate_order(size);
81819f0f 2324
834f3d11 2325 if (order < 0)
81819f0f
CL
2326 return 0;
2327
b7a49f0d 2328 s->allocflags = 0;
834f3d11 2329 if (order)
b7a49f0d
CL
2330 s->allocflags |= __GFP_COMP;
2331
2332 if (s->flags & SLAB_CACHE_DMA)
2333 s->allocflags |= SLUB_DMA;
2334
2335 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2336 s->allocflags |= __GFP_RECLAIMABLE;
2337
81819f0f
CL
2338 /*
2339 * Determine the number of objects per slab
2340 */
834f3d11 2341 s->oo = oo_make(order, size);
65c3376a 2342 s->min = oo_make(get_order(size), size);
205ab99d
CL
2343 if (oo_objects(s->oo) > oo_objects(s->max))
2344 s->max = s->oo;
81819f0f 2345
834f3d11 2346 return !!oo_objects(s->oo);
81819f0f
CL
2347
2348}
2349
55136592 2350static int kmem_cache_open(struct kmem_cache *s,
81819f0f
CL
2351 const char *name, size_t size,
2352 size_t align, unsigned long flags,
51cc5068 2353 void (*ctor)(void *))
81819f0f
CL
2354{
2355 memset(s, 0, kmem_size);
2356 s->name = name;
2357 s->ctor = ctor;
81819f0f 2358 s->objsize = size;
81819f0f 2359 s->align = align;
ba0268a8 2360 s->flags = kmem_cache_flags(size, flags, name, ctor);
81819f0f 2361
06b285dc 2362 if (!calculate_sizes(s, -1))
81819f0f 2363 goto error;
3de47213
DR
2364 if (disable_higher_order_debug) {
2365 /*
2366 * Disable debugging flags that store metadata if the min slab
2367 * order increased.
2368 */
2369 if (get_order(s->size) > get_order(s->objsize)) {
2370 s->flags &= ~DEBUG_METADATA_FLAGS;
2371 s->offset = 0;
2372 if (!calculate_sizes(s, -1))
2373 goto error;
2374 }
2375 }
81819f0f 2376
3b89d7d8
DR
2377 /*
2378 * The larger the object size is, the more pages we want on the partial
2379 * list to avoid pounding the page allocator excessively.
2380 */
c0bdb232 2381 set_min_partial(s, ilog2(s->size));
81819f0f
CL
2382 s->refcount = 1;
2383#ifdef CONFIG_NUMA
e2cb96b7 2384 s->remote_node_defrag_ratio = 1000;
81819f0f 2385#endif
55136592 2386 if (!init_kmem_cache_nodes(s))
dfb4f096 2387 goto error;
81819f0f 2388
55136592 2389 if (alloc_kmem_cache_cpus(s))
81819f0f 2390 return 1;
ff12059e 2391
4c93c355 2392 free_kmem_cache_nodes(s);
81819f0f
CL
2393error:
2394 if (flags & SLAB_PANIC)
2395 panic("Cannot create slab %s size=%lu realsize=%u "
2396 "order=%u offset=%u flags=%lx\n",
834f3d11 2397 s->name, (unsigned long)size, s->size, oo_order(s->oo),
81819f0f
CL
2398 s->offset, flags);
2399 return 0;
2400}
81819f0f
CL
2401
2402/*
2403 * Check if a given pointer is valid
2404 */
2405int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2406{
06428780 2407 struct page *page;
81819f0f 2408
d3e06e2b
PE
2409 if (!kern_ptr_validate(object, s->size))
2410 return 0;
2411
81819f0f
CL
2412 page = get_object_page(object);
2413
2414 if (!page || s != page->slab)
2415 /* No slab or wrong slab */
2416 return 0;
2417
abcd08a6 2418 if (!check_valid_pointer(s, page, object))
81819f0f
CL
2419 return 0;
2420
2421 /*
2422 * We could also check if the object is on the slabs freelist.
2423 * But this would be too expensive and it seems that the main
6446faa2 2424 * purpose of kmem_ptr_valid() is to check if the object belongs
81819f0f
CL
2425 * to a certain slab.
2426 */
2427 return 1;
2428}
2429EXPORT_SYMBOL(kmem_ptr_validate);
2430
2431/*
2432 * Determine the size of a slab object
2433 */
2434unsigned int kmem_cache_size(struct kmem_cache *s)
2435{
2436 return s->objsize;
2437}
2438EXPORT_SYMBOL(kmem_cache_size);
2439
2440const char *kmem_cache_name(struct kmem_cache *s)
2441{
2442 return s->name;
2443}
2444EXPORT_SYMBOL(kmem_cache_name);
2445
33b12c38
CL
2446static void list_slab_objects(struct kmem_cache *s, struct page *page,
2447 const char *text)
2448{
2449#ifdef CONFIG_SLUB_DEBUG
2450 void *addr = page_address(page);
2451 void *p;
bbd7d57b
ED
2452 long *map = kzalloc(BITS_TO_LONGS(page->objects) * sizeof(long),
2453 GFP_ATOMIC);
33b12c38 2454
bbd7d57b
ED
2455 if (!map)
2456 return;
33b12c38
CL
2457 slab_err(s, page, "%s", text);
2458 slab_lock(page);
2459 for_each_free_object(p, s, page->freelist)
2460 set_bit(slab_index(p, s, addr), map);
2461
2462 for_each_object(p, s, addr, page->objects) {
2463
2464 if (!test_bit(slab_index(p, s, addr), map)) {
2465 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
2466 p, p - addr);
2467 print_tracking(s, p);
2468 }
2469 }
2470 slab_unlock(page);
bbd7d57b 2471 kfree(map);
33b12c38
CL
2472#endif
2473}
2474
81819f0f 2475/*
599870b1 2476 * Attempt to free all partial slabs on a node.
81819f0f 2477 */
599870b1 2478static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
81819f0f 2479{
81819f0f
CL
2480 unsigned long flags;
2481 struct page *page, *h;
2482
2483 spin_lock_irqsave(&n->list_lock, flags);
33b12c38 2484 list_for_each_entry_safe(page, h, &n->partial, lru) {
81819f0f
CL
2485 if (!page->inuse) {
2486 list_del(&page->lru);
2487 discard_slab(s, page);
599870b1 2488 n->nr_partial--;
33b12c38
CL
2489 } else {
2490 list_slab_objects(s, page,
2491 "Objects remaining on kmem_cache_close()");
599870b1 2492 }
33b12c38 2493 }
81819f0f 2494 spin_unlock_irqrestore(&n->list_lock, flags);
81819f0f
CL
2495}
2496
2497/*
672bba3a 2498 * Release all resources used by a slab cache.
81819f0f 2499 */
0c710013 2500static inline int kmem_cache_close(struct kmem_cache *s)
81819f0f
CL
2501{
2502 int node;
2503
2504 flush_all(s);
9dfc6e68 2505 free_percpu(s->cpu_slab);
81819f0f 2506 /* Attempt to free all objects */
f64dc58c 2507 for_each_node_state(node, N_NORMAL_MEMORY) {
81819f0f
CL
2508 struct kmem_cache_node *n = get_node(s, node);
2509
599870b1
CL
2510 free_partial(s, n);
2511 if (n->nr_partial || slabs_node(s, node))
81819f0f
CL
2512 return 1;
2513 }
2514 free_kmem_cache_nodes(s);
2515 return 0;
2516}
2517
2518/*
2519 * Close a cache and release the kmem_cache structure
2520 * (must be used for caches created using kmem_cache_create)
2521 */
2522void kmem_cache_destroy(struct kmem_cache *s)
2523{
2524 down_write(&slub_lock);
2525 s->refcount--;
2526 if (!s->refcount) {
2527 list_del(&s->list);
d629d819
PE
2528 if (kmem_cache_close(s)) {
2529 printk(KERN_ERR "SLUB %s: %s called for cache that "
2530 "still has objects.\n", s->name, __func__);
2531 dump_stack();
2532 }
d76b1590
ED
2533 if (s->flags & SLAB_DESTROY_BY_RCU)
2534 rcu_barrier();
81819f0f 2535 sysfs_slab_remove(s);
2bce6485
CL
2536 }
2537 up_write(&slub_lock);
81819f0f
CL
2538}
2539EXPORT_SYMBOL(kmem_cache_destroy);
2540
2541/********************************************************************
2542 * Kmalloc subsystem
2543 *******************************************************************/
2544
51df1142 2545struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
81819f0f
CL
2546EXPORT_SYMBOL(kmalloc_caches);
2547
51df1142
CL
2548static struct kmem_cache *kmem_cache;
2549
55136592 2550#ifdef CONFIG_ZONE_DMA
51df1142 2551static struct kmem_cache *kmalloc_dma_caches[SLUB_PAGE_SHIFT];
55136592
CL
2552#endif
2553
81819f0f
CL
2554static int __init setup_slub_min_order(char *str)
2555{
06428780 2556 get_option(&str, &slub_min_order);
81819f0f
CL
2557
2558 return 1;
2559}
2560
2561__setup("slub_min_order=", setup_slub_min_order);
2562
2563static int __init setup_slub_max_order(char *str)
2564{
06428780 2565 get_option(&str, &slub_max_order);
818cf590 2566 slub_max_order = min(slub_max_order, MAX_ORDER - 1);
81819f0f
CL
2567
2568 return 1;
2569}
2570
2571__setup("slub_max_order=", setup_slub_max_order);
2572
2573static int __init setup_slub_min_objects(char *str)
2574{
06428780 2575 get_option(&str, &slub_min_objects);
81819f0f
CL
2576
2577 return 1;
2578}
2579
2580__setup("slub_min_objects=", setup_slub_min_objects);
2581
2582static int __init setup_slub_nomerge(char *str)
2583{
2584 slub_nomerge = 1;
2585 return 1;
2586}
2587
2588__setup("slub_nomerge", setup_slub_nomerge);
2589
51df1142
CL
2590static struct kmem_cache *__init create_kmalloc_cache(const char *name,
2591 int size, unsigned int flags)
81819f0f 2592{
51df1142
CL
2593 struct kmem_cache *s;
2594
2595 s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
2596
83b519e8
PE
2597 /*
2598 * This function is called with IRQs disabled during early-boot on
2599 * single CPU so there's no need to take slub_lock here.
2600 */
55136592 2601 if (!kmem_cache_open(s, name, size, ARCH_KMALLOC_MINALIGN,
319d1e24 2602 flags, NULL))
81819f0f
CL
2603 goto panic;
2604
2605 list_add(&s->list, &slab_caches);
51df1142 2606 return s;
81819f0f
CL
2607
2608panic:
2609 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
51df1142 2610 return NULL;
81819f0f
CL
2611}
2612
f1b26339
CL
2613/*
2614 * Conversion table for small slabs sizes / 8 to the index in the
2615 * kmalloc array. This is necessary for slabs < 192 since we have non power
2616 * of two cache sizes there. The size of larger slabs can be determined using
2617 * fls.
2618 */
2619static s8 size_index[24] = {
2620 3, /* 8 */
2621 4, /* 16 */
2622 5, /* 24 */
2623 5, /* 32 */
2624 6, /* 40 */
2625 6, /* 48 */
2626 6, /* 56 */
2627 6, /* 64 */
2628 1, /* 72 */
2629 1, /* 80 */
2630 1, /* 88 */
2631 1, /* 96 */
2632 7, /* 104 */
2633 7, /* 112 */
2634 7, /* 120 */
2635 7, /* 128 */
2636 2, /* 136 */
2637 2, /* 144 */
2638 2, /* 152 */
2639 2, /* 160 */
2640 2, /* 168 */
2641 2, /* 176 */
2642 2, /* 184 */
2643 2 /* 192 */
2644};
2645
acdfcd04
AK
2646static inline int size_index_elem(size_t bytes)
2647{
2648 return (bytes - 1) / 8;
2649}
2650
81819f0f
CL
2651static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2652{
f1b26339 2653 int index;
81819f0f 2654
f1b26339
CL
2655 if (size <= 192) {
2656 if (!size)
2657 return ZERO_SIZE_PTR;
81819f0f 2658
acdfcd04 2659 index = size_index[size_index_elem(size)];
aadb4bc4 2660 } else
f1b26339 2661 index = fls(size - 1);
81819f0f
CL
2662
2663#ifdef CONFIG_ZONE_DMA
f1b26339 2664 if (unlikely((flags & SLUB_DMA)))
51df1142 2665 return kmalloc_dma_caches[index];
f1b26339 2666
81819f0f 2667#endif
51df1142 2668 return kmalloc_caches[index];
81819f0f
CL
2669}
2670
2671void *__kmalloc(size_t size, gfp_t flags)
2672{
aadb4bc4 2673 struct kmem_cache *s;
5b882be4 2674 void *ret;
81819f0f 2675
ffadd4d0 2676 if (unlikely(size > SLUB_MAX_SIZE))
eada35ef 2677 return kmalloc_large(size, flags);
aadb4bc4
CL
2678
2679 s = get_slab(size, flags);
2680
2681 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913
CL
2682 return s;
2683
2154a336 2684 ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
5b882be4 2685
ca2b84cb 2686 trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
5b882be4
EGM
2687
2688 return ret;
81819f0f
CL
2689}
2690EXPORT_SYMBOL(__kmalloc);
2691
f619cfe1
CL
2692static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
2693{
b1eeab67 2694 struct page *page;
e4f7c0b4 2695 void *ptr = NULL;
f619cfe1 2696
b1eeab67
VN
2697 flags |= __GFP_COMP | __GFP_NOTRACK;
2698 page = alloc_pages_node(node, flags, get_order(size));
f619cfe1 2699 if (page)
e4f7c0b4
CM
2700 ptr = page_address(page);
2701
2702 kmemleak_alloc(ptr, size, 1, flags);
2703 return ptr;
f619cfe1
CL
2704}
2705
81819f0f
CL
2706#ifdef CONFIG_NUMA
2707void *__kmalloc_node(size_t size, gfp_t flags, int node)
2708{
aadb4bc4 2709 struct kmem_cache *s;
5b882be4 2710 void *ret;
81819f0f 2711
057685cf 2712 if (unlikely(size > SLUB_MAX_SIZE)) {
5b882be4
EGM
2713 ret = kmalloc_large_node(size, flags, node);
2714
ca2b84cb
EGM
2715 trace_kmalloc_node(_RET_IP_, ret,
2716 size, PAGE_SIZE << get_order(size),
2717 flags, node);
5b882be4
EGM
2718
2719 return ret;
2720 }
aadb4bc4
CL
2721
2722 s = get_slab(size, flags);
2723
2724 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913
CL
2725 return s;
2726
5b882be4
EGM
2727 ret = slab_alloc(s, flags, node, _RET_IP_);
2728
ca2b84cb 2729 trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
5b882be4
EGM
2730
2731 return ret;
81819f0f
CL
2732}
2733EXPORT_SYMBOL(__kmalloc_node);
2734#endif
2735
2736size_t ksize(const void *object)
2737{
272c1d21 2738 struct page *page;
81819f0f
CL
2739 struct kmem_cache *s;
2740
ef8b4520 2741 if (unlikely(object == ZERO_SIZE_PTR))
272c1d21
CL
2742 return 0;
2743
294a80a8 2744 page = virt_to_head_page(object);
294a80a8 2745
76994412
PE
2746 if (unlikely(!PageSlab(page))) {
2747 WARN_ON(!PageCompound(page));
294a80a8 2748 return PAGE_SIZE << compound_order(page);
76994412 2749 }
81819f0f 2750 s = page->slab;
81819f0f 2751
ae20bfda 2752#ifdef CONFIG_SLUB_DEBUG
81819f0f
CL
2753 /*
2754 * Debugging requires use of the padding between object
2755 * and whatever may come after it.
2756 */
2757 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2758 return s->objsize;
2759
ae20bfda 2760#endif
81819f0f
CL
2761 /*
2762 * If we have the need to store the freelist pointer
2763 * back there or track user information then we can
2764 * only use the space before that information.
2765 */
2766 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2767 return s->inuse;
81819f0f
CL
2768 /*
2769 * Else we can use all the padding etc for the allocation
2770 */
2771 return s->size;
2772}
b1aabecd 2773EXPORT_SYMBOL(ksize);
81819f0f
CL
2774
2775void kfree(const void *x)
2776{
81819f0f 2777 struct page *page;
5bb983b0 2778 void *object = (void *)x;
81819f0f 2779
2121db74
PE
2780 trace_kfree(_RET_IP_, x);
2781
2408c550 2782 if (unlikely(ZERO_OR_NULL_PTR(x)))
81819f0f
CL
2783 return;
2784
b49af68f 2785 page = virt_to_head_page(x);
aadb4bc4 2786 if (unlikely(!PageSlab(page))) {
0937502a 2787 BUG_ON(!PageCompound(page));
e4f7c0b4 2788 kmemleak_free(x);
aadb4bc4
CL
2789 put_page(page);
2790 return;
2791 }
ce71e27c 2792 slab_free(page->slab, page, object, _RET_IP_);
81819f0f
CL
2793}
2794EXPORT_SYMBOL(kfree);
2795
2086d26a 2796/*
672bba3a
CL
2797 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2798 * the remaining slabs by the number of items in use. The slabs with the
2799 * most items in use come first. New allocations will then fill those up
2800 * and thus they can be removed from the partial lists.
2801 *
2802 * The slabs with the least items are placed last. This results in them
2803 * being allocated from last increasing the chance that the last objects
2804 * are freed in them.
2086d26a
CL
2805 */
2806int kmem_cache_shrink(struct kmem_cache *s)
2807{
2808 int node;
2809 int i;
2810 struct kmem_cache_node *n;
2811 struct page *page;
2812 struct page *t;
205ab99d 2813 int objects = oo_objects(s->max);
2086d26a 2814 struct list_head *slabs_by_inuse =
834f3d11 2815 kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
2086d26a
CL
2816 unsigned long flags;
2817
2818 if (!slabs_by_inuse)
2819 return -ENOMEM;
2820
2821 flush_all(s);
f64dc58c 2822 for_each_node_state(node, N_NORMAL_MEMORY) {
2086d26a
CL
2823 n = get_node(s, node);
2824
2825 if (!n->nr_partial)
2826 continue;
2827
834f3d11 2828 for (i = 0; i < objects; i++)
2086d26a
CL
2829 INIT_LIST_HEAD(slabs_by_inuse + i);
2830
2831 spin_lock_irqsave(&n->list_lock, flags);
2832
2833 /*
672bba3a 2834 * Build lists indexed by the items in use in each slab.
2086d26a 2835 *
672bba3a
CL
2836 * Note that concurrent frees may occur while we hold the
2837 * list_lock. page->inuse here is the upper limit.
2086d26a
CL
2838 */
2839 list_for_each_entry_safe(page, t, &n->partial, lru) {
2840 if (!page->inuse && slab_trylock(page)) {
2841 /*
2842 * Must hold slab lock here because slab_free
2843 * may have freed the last object and be
2844 * waiting to release the slab.
2845 */
2846 list_del(&page->lru);
2847 n->nr_partial--;
2848 slab_unlock(page);
2849 discard_slab(s, page);
2850 } else {
fcda3d89
CL
2851 list_move(&page->lru,
2852 slabs_by_inuse + page->inuse);
2086d26a
CL
2853 }
2854 }
2855
2086d26a 2856 /*
672bba3a
CL
2857 * Rebuild the partial list with the slabs filled up most
2858 * first and the least used slabs at the end.
2086d26a 2859 */
834f3d11 2860 for (i = objects - 1; i >= 0; i--)
2086d26a
CL
2861 list_splice(slabs_by_inuse + i, n->partial.prev);
2862
2086d26a
CL
2863 spin_unlock_irqrestore(&n->list_lock, flags);
2864 }
2865
2866 kfree(slabs_by_inuse);
2867 return 0;
2868}
2869EXPORT_SYMBOL(kmem_cache_shrink);
2870
b9049e23
YG
2871#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2872static int slab_mem_going_offline_callback(void *arg)
2873{
2874 struct kmem_cache *s;
2875
2876 down_read(&slub_lock);
2877 list_for_each_entry(s, &slab_caches, list)
2878 kmem_cache_shrink(s);
2879 up_read(&slub_lock);
2880
2881 return 0;
2882}
2883
2884static void slab_mem_offline_callback(void *arg)
2885{
2886 struct kmem_cache_node *n;
2887 struct kmem_cache *s;
2888 struct memory_notify *marg = arg;
2889 int offline_node;
2890
2891 offline_node = marg->status_change_nid;
2892
2893 /*
2894 * If the node still has available memory. we need kmem_cache_node
2895 * for it yet.
2896 */
2897 if (offline_node < 0)
2898 return;
2899
2900 down_read(&slub_lock);
2901 list_for_each_entry(s, &slab_caches, list) {
2902 n = get_node(s, offline_node);
2903 if (n) {
2904 /*
2905 * if n->nr_slabs > 0, slabs still exist on the node
2906 * that is going down. We were unable to free them,
c9404c9c 2907 * and offline_pages() function shouldn't call this
b9049e23
YG
2908 * callback. So, we must fail.
2909 */
0f389ec6 2910 BUG_ON(slabs_node(s, offline_node));
b9049e23
YG
2911
2912 s->node[offline_node] = NULL;
8de66a0c 2913 kmem_cache_free(kmem_cache_node, n);
b9049e23
YG
2914 }
2915 }
2916 up_read(&slub_lock);
2917}
2918
2919static int slab_mem_going_online_callback(void *arg)
2920{
2921 struct kmem_cache_node *n;
2922 struct kmem_cache *s;
2923 struct memory_notify *marg = arg;
2924 int nid = marg->status_change_nid;
2925 int ret = 0;
2926
2927 /*
2928 * If the node's memory is already available, then kmem_cache_node is
2929 * already created. Nothing to do.
2930 */
2931 if (nid < 0)
2932 return 0;
2933
2934 /*
0121c619 2935 * We are bringing a node online. No memory is available yet. We must
b9049e23
YG
2936 * allocate a kmem_cache_node structure in order to bring the node
2937 * online.
2938 */
2939 down_read(&slub_lock);
2940 list_for_each_entry(s, &slab_caches, list) {
2941 /*
2942 * XXX: kmem_cache_alloc_node will fallback to other nodes
2943 * since memory is not yet available from the node that
2944 * is brought up.
2945 */
8de66a0c 2946 n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
b9049e23
YG
2947 if (!n) {
2948 ret = -ENOMEM;
2949 goto out;
2950 }
5595cffc 2951 init_kmem_cache_node(n, s);
b9049e23
YG
2952 s->node[nid] = n;
2953 }
2954out:
2955 up_read(&slub_lock);
2956 return ret;
2957}
2958
2959static int slab_memory_callback(struct notifier_block *self,
2960 unsigned long action, void *arg)
2961{
2962 int ret = 0;
2963
2964 switch (action) {
2965 case MEM_GOING_ONLINE:
2966 ret = slab_mem_going_online_callback(arg);
2967 break;
2968 case MEM_GOING_OFFLINE:
2969 ret = slab_mem_going_offline_callback(arg);
2970 break;
2971 case MEM_OFFLINE:
2972 case MEM_CANCEL_ONLINE:
2973 slab_mem_offline_callback(arg);
2974 break;
2975 case MEM_ONLINE:
2976 case MEM_CANCEL_OFFLINE:
2977 break;
2978 }
dc19f9db
KH
2979 if (ret)
2980 ret = notifier_from_errno(ret);
2981 else
2982 ret = NOTIFY_OK;
b9049e23
YG
2983 return ret;
2984}
2985
2986#endif /* CONFIG_MEMORY_HOTPLUG */
2987
81819f0f
CL
2988/********************************************************************
2989 * Basic setup of slabs
2990 *******************************************************************/
2991
51df1142
CL
2992/*
2993 * Used for early kmem_cache structures that were allocated using
2994 * the page allocator
2995 */
2996
2997static void __init kmem_cache_bootstrap_fixup(struct kmem_cache *s)
2998{
2999 int node;
3000
3001 list_add(&s->list, &slab_caches);
3002 s->refcount = -1;
3003
3004 for_each_node_state(node, N_NORMAL_MEMORY) {
3005 struct kmem_cache_node *n = get_node(s, node);
3006 struct page *p;
3007
3008 if (n) {
3009 list_for_each_entry(p, &n->partial, lru)
3010 p->slab = s;
3011
3012#ifdef CONFIG_SLAB_DEBUG
3013 list_for_each_entry(p, &n->full, lru)
3014 p->slab = s;
3015#endif
3016 }
3017 }
3018}
3019
81819f0f
CL
3020void __init kmem_cache_init(void)
3021{
3022 int i;
4b356be0 3023 int caches = 0;
51df1142
CL
3024 struct kmem_cache *temp_kmem_cache;
3025 int order;
81819f0f
CL
3026
3027#ifdef CONFIG_NUMA
51df1142
CL
3028 struct kmem_cache *temp_kmem_cache_node;
3029 unsigned long kmalloc_size;
3030
3031 kmem_size = offsetof(struct kmem_cache, node) +
3032 nr_node_ids * sizeof(struct kmem_cache_node *);
3033
3034 /* Allocate two kmem_caches from the page allocator */
3035 kmalloc_size = ALIGN(kmem_size, cache_line_size());
3036 order = get_order(2 * kmalloc_size);
3037 kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
3038
81819f0f
CL
3039 /*
3040 * Must first have the slab cache available for the allocations of the
672bba3a 3041 * struct kmem_cache_node's. There is special bootstrap code in
81819f0f
CL
3042 * kmem_cache_open for slab_state == DOWN.
3043 */
51df1142
CL
3044 kmem_cache_node = (void *)kmem_cache + kmalloc_size;
3045
3046 kmem_cache_open(kmem_cache_node, "kmem_cache_node",
3047 sizeof(struct kmem_cache_node),
3048 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
b9049e23 3049
0c40ba4f 3050 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
51df1142
CL
3051#else
3052 /* Allocate a single kmem_cache from the page allocator */
3053 kmem_size = sizeof(struct kmem_cache);
3054 order = get_order(kmem_size);
3055 kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
81819f0f
CL
3056#endif
3057
3058 /* Able to allocate the per node structures */
3059 slab_state = PARTIAL;
3060
51df1142
CL
3061 temp_kmem_cache = kmem_cache;
3062 kmem_cache_open(kmem_cache, "kmem_cache", kmem_size,
3063 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
3064 kmem_cache = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
3065 memcpy(kmem_cache, temp_kmem_cache, kmem_size);
81819f0f 3066
51df1142
CL
3067#ifdef CONFIG_NUMA
3068 /*
3069 * Allocate kmem_cache_node properly from the kmem_cache slab.
3070 * kmem_cache_node is separately allocated so no need to
3071 * update any list pointers.
3072 */
3073 temp_kmem_cache_node = kmem_cache_node;
81819f0f 3074
51df1142
CL
3075 kmem_cache_node = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
3076 memcpy(kmem_cache_node, temp_kmem_cache_node, kmem_size);
3077
3078 kmem_cache_bootstrap_fixup(kmem_cache_node);
3079
3080 caches++;
3081#else
3082 /*
3083 * kmem_cache has kmem_cache_node embedded and we moved it!
3084 * Update the list heads
3085 */
3086 INIT_LIST_HEAD(&kmem_cache->local_node.partial);
3087 list_splice(&temp_kmem_cache->local_node.partial, &kmem_cache->local_node.partial);
3088#ifdef CONFIG_SLUB_DEBUG
3089 INIT_LIST_HEAD(&kmem_cache->local_node.full);
3090 list_splice(&temp_kmem_cache->local_node.full, &kmem_cache->local_node.full);
3091#endif
3092#endif
3093 kmem_cache_bootstrap_fixup(kmem_cache);
3094 caches++;
3095 /* Free temporary boot structure */
3096 free_pages((unsigned long)temp_kmem_cache, order);
3097
3098 /* Now we can use the kmem_cache to allocate kmalloc slabs */
f1b26339
CL
3099
3100 /*
3101 * Patch up the size_index table if we have strange large alignment
3102 * requirements for the kmalloc array. This is only the case for
6446faa2 3103 * MIPS it seems. The standard arches will not generate any code here.
f1b26339
CL
3104 *
3105 * Largest permitted alignment is 256 bytes due to the way we
3106 * handle the index determination for the smaller caches.
3107 *
3108 * Make sure that nothing crazy happens if someone starts tinkering
3109 * around with ARCH_KMALLOC_MINALIGN
3110 */
3111 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3112 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3113
acdfcd04
AK
3114 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
3115 int elem = size_index_elem(i);
3116 if (elem >= ARRAY_SIZE(size_index))
3117 break;
3118 size_index[elem] = KMALLOC_SHIFT_LOW;
3119 }
f1b26339 3120
acdfcd04
AK
3121 if (KMALLOC_MIN_SIZE == 64) {
3122 /*
3123 * The 96 byte size cache is not used if the alignment
3124 * is 64 byte.
3125 */
3126 for (i = 64 + 8; i <= 96; i += 8)
3127 size_index[size_index_elem(i)] = 7;
3128 } else if (KMALLOC_MIN_SIZE == 128) {
41d54d3b
CL
3129 /*
3130 * The 192 byte sized cache is not used if the alignment
3131 * is 128 byte. Redirect kmalloc to use the 256 byte cache
3132 * instead.
3133 */
3134 for (i = 128 + 8; i <= 192; i += 8)
acdfcd04 3135 size_index[size_index_elem(i)] = 8;
41d54d3b
CL
3136 }
3137
51df1142
CL
3138 /* Caches that are not of the two-to-the-power-of size */
3139 if (KMALLOC_MIN_SIZE <= 32) {
3140 kmalloc_caches[1] = create_kmalloc_cache("kmalloc-96", 96, 0);
3141 caches++;
3142 }
3143
3144 if (KMALLOC_MIN_SIZE <= 64) {
3145 kmalloc_caches[2] = create_kmalloc_cache("kmalloc-192", 192, 0);
3146 caches++;
3147 }
3148
3149 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
3150 kmalloc_caches[i] = create_kmalloc_cache("kmalloc", 1 << i, 0);
3151 caches++;
3152 }
3153
81819f0f
CL
3154 slab_state = UP;
3155
3156 /* Provide the correct kmalloc names now that the caches are up */
84c1cf62
PE
3157 if (KMALLOC_MIN_SIZE <= 32) {
3158 kmalloc_caches[1]->name = kstrdup(kmalloc_caches[1]->name, GFP_NOWAIT);
3159 BUG_ON(!kmalloc_caches[1]->name);
3160 }
3161
3162 if (KMALLOC_MIN_SIZE <= 64) {
3163 kmalloc_caches[2]->name = kstrdup(kmalloc_caches[2]->name, GFP_NOWAIT);
3164 BUG_ON(!kmalloc_caches[2]->name);
3165 }
3166
d7278bd7
CL
3167 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
3168 char *s = kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
3169
3170 BUG_ON(!s);
51df1142 3171 kmalloc_caches[i]->name = s;
d7278bd7 3172 }
81819f0f
CL
3173
3174#ifdef CONFIG_SMP
3175 register_cpu_notifier(&slab_notifier);
9dfc6e68 3176#endif
81819f0f 3177
55136592 3178#ifdef CONFIG_ZONE_DMA
51df1142
CL
3179 for (i = 0; i < SLUB_PAGE_SHIFT; i++) {
3180 struct kmem_cache *s = kmalloc_caches[i];
55136592 3181
51df1142 3182 if (s && s->size) {
55136592
CL
3183 char *name = kasprintf(GFP_NOWAIT,
3184 "dma-kmalloc-%d", s->objsize);
3185
3186 BUG_ON(!name);
51df1142
CL
3187 kmalloc_dma_caches[i] = create_kmalloc_cache(name,
3188 s->objsize, SLAB_CACHE_DMA);
55136592
CL
3189 }
3190 }
3191#endif
3adbefee
IM
3192 printk(KERN_INFO
3193 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
4b356be0
CL
3194 " CPUs=%d, Nodes=%d\n",
3195 caches, cache_line_size(),
81819f0f
CL
3196 slub_min_order, slub_max_order, slub_min_objects,
3197 nr_cpu_ids, nr_node_ids);
3198}
3199
7e85ee0c
PE
3200void __init kmem_cache_init_late(void)
3201{
7e85ee0c
PE
3202}
3203
81819f0f
CL
3204/*
3205 * Find a mergeable slab cache
3206 */
3207static int slab_unmergeable(struct kmem_cache *s)
3208{
3209 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3210 return 1;
3211
c59def9f 3212 if (s->ctor)
81819f0f
CL
3213 return 1;
3214
8ffa6875
CL
3215 /*
3216 * We may have set a slab to be unmergeable during bootstrap.
3217 */
3218 if (s->refcount < 0)
3219 return 1;
3220
81819f0f
CL
3221 return 0;
3222}
3223
3224static struct kmem_cache *find_mergeable(size_t size,
ba0268a8 3225 size_t align, unsigned long flags, const char *name,
51cc5068 3226 void (*ctor)(void *))
81819f0f 3227{
5b95a4ac 3228 struct kmem_cache *s;
81819f0f
CL
3229
3230 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3231 return NULL;
3232
c59def9f 3233 if (ctor)
81819f0f
CL
3234 return NULL;
3235
3236 size = ALIGN(size, sizeof(void *));
3237 align = calculate_alignment(flags, align, size);
3238 size = ALIGN(size, align);
ba0268a8 3239 flags = kmem_cache_flags(size, flags, name, NULL);
81819f0f 3240
5b95a4ac 3241 list_for_each_entry(s, &slab_caches, list) {
81819f0f
CL
3242 if (slab_unmergeable(s))
3243 continue;
3244
3245 if (size > s->size)
3246 continue;
3247
ba0268a8 3248 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
81819f0f
CL
3249 continue;
3250 /*
3251 * Check if alignment is compatible.
3252 * Courtesy of Adrian Drzewiecki
3253 */
06428780 3254 if ((s->size & ~(align - 1)) != s->size)
81819f0f
CL
3255 continue;
3256
3257 if (s->size - size >= sizeof(void *))
3258 continue;
3259
3260 return s;
3261 }
3262 return NULL;
3263}
3264
3265struct kmem_cache *kmem_cache_create(const char *name, size_t size,
51cc5068 3266 size_t align, unsigned long flags, void (*ctor)(void *))
81819f0f
CL
3267{
3268 struct kmem_cache *s;
84c1cf62 3269 char *n;
81819f0f 3270
fe1ff49d
BH
3271 if (WARN_ON(!name))
3272 return NULL;
3273
81819f0f 3274 down_write(&slub_lock);
ba0268a8 3275 s = find_mergeable(size, align, flags, name, ctor);
81819f0f
CL
3276 if (s) {
3277 s->refcount++;
3278 /*
3279 * Adjust the object sizes so that we clear
3280 * the complete object on kzalloc.
3281 */
3282 s->objsize = max(s->objsize, (int)size);
3283 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
6446faa2 3284
7b8f3b66 3285 if (sysfs_slab_alias(s, name)) {
7b8f3b66 3286 s->refcount--;
81819f0f 3287 goto err;
7b8f3b66 3288 }
2bce6485 3289 up_write(&slub_lock);
a0e1d1be
CL
3290 return s;
3291 }
6446faa2 3292
84c1cf62
PE
3293 n = kstrdup(name, GFP_KERNEL);
3294 if (!n)
3295 goto err;
3296
a0e1d1be
CL
3297 s = kmalloc(kmem_size, GFP_KERNEL);
3298 if (s) {
84c1cf62 3299 if (kmem_cache_open(s, n,
c59def9f 3300 size, align, flags, ctor)) {
81819f0f 3301 list_add(&s->list, &slab_caches);
7b8f3b66 3302 if (sysfs_slab_add(s)) {
7b8f3b66 3303 list_del(&s->list);
84c1cf62 3304 kfree(n);
7b8f3b66 3305 kfree(s);
a0e1d1be 3306 goto err;
7b8f3b66 3307 }
2bce6485 3308 up_write(&slub_lock);
a0e1d1be
CL
3309 return s;
3310 }
84c1cf62 3311 kfree(n);
a0e1d1be 3312 kfree(s);
81819f0f
CL
3313 }
3314 up_write(&slub_lock);
81819f0f
CL
3315
3316err:
81819f0f
CL
3317 if (flags & SLAB_PANIC)
3318 panic("Cannot create slabcache %s\n", name);
3319 else
3320 s = NULL;
3321 return s;
3322}
3323EXPORT_SYMBOL(kmem_cache_create);
3324
81819f0f 3325#ifdef CONFIG_SMP
81819f0f 3326/*
672bba3a
CL
3327 * Use the cpu notifier to insure that the cpu slabs are flushed when
3328 * necessary.
81819f0f
CL
3329 */
3330static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3331 unsigned long action, void *hcpu)
3332{
3333 long cpu = (long)hcpu;
5b95a4ac
CL
3334 struct kmem_cache *s;
3335 unsigned long flags;
81819f0f
CL
3336
3337 switch (action) {
3338 case CPU_UP_CANCELED:
8bb78442 3339 case CPU_UP_CANCELED_FROZEN:
81819f0f 3340 case CPU_DEAD:
8bb78442 3341 case CPU_DEAD_FROZEN:
5b95a4ac
CL
3342 down_read(&slub_lock);
3343 list_for_each_entry(s, &slab_caches, list) {
3344 local_irq_save(flags);
3345 __flush_cpu_slab(s, cpu);
3346 local_irq_restore(flags);
3347 }
3348 up_read(&slub_lock);
81819f0f
CL
3349 break;
3350 default:
3351 break;
3352 }
3353 return NOTIFY_OK;
3354}
3355
06428780 3356static struct notifier_block __cpuinitdata slab_notifier = {
3adbefee 3357 .notifier_call = slab_cpuup_callback
06428780 3358};
81819f0f
CL
3359
3360#endif
3361
ce71e27c 3362void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
81819f0f 3363{
aadb4bc4 3364 struct kmem_cache *s;
94b528d0 3365 void *ret;
aadb4bc4 3366
ffadd4d0 3367 if (unlikely(size > SLUB_MAX_SIZE))
eada35ef
PE
3368 return kmalloc_large(size, gfpflags);
3369
aadb4bc4 3370 s = get_slab(size, gfpflags);
81819f0f 3371
2408c550 3372 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913 3373 return s;
81819f0f 3374
2154a336 3375 ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, caller);
94b528d0
EGM
3376
3377 /* Honor the call site pointer we recieved. */
ca2b84cb 3378 trace_kmalloc(caller, ret, size, s->size, gfpflags);
94b528d0
EGM
3379
3380 return ret;
81819f0f
CL
3381}
3382
3383void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
ce71e27c 3384 int node, unsigned long caller)
81819f0f 3385{
aadb4bc4 3386 struct kmem_cache *s;
94b528d0 3387 void *ret;
aadb4bc4 3388
d3e14aa3
XF
3389 if (unlikely(size > SLUB_MAX_SIZE)) {
3390 ret = kmalloc_large_node(size, gfpflags, node);
3391
3392 trace_kmalloc_node(caller, ret,
3393 size, PAGE_SIZE << get_order(size),
3394 gfpflags, node);
3395
3396 return ret;
3397 }
eada35ef 3398
aadb4bc4 3399 s = get_slab(size, gfpflags);
81819f0f 3400
2408c550 3401 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913 3402 return s;
81819f0f 3403
94b528d0
EGM
3404 ret = slab_alloc(s, gfpflags, node, caller);
3405
3406 /* Honor the call site pointer we recieved. */
ca2b84cb 3407 trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
94b528d0
EGM
3408
3409 return ret;
81819f0f
CL
3410}
3411
f6acb635 3412#ifdef CONFIG_SLUB_DEBUG
205ab99d
CL
3413static int count_inuse(struct page *page)
3414{
3415 return page->inuse;
3416}
3417
3418static int count_total(struct page *page)
3419{
3420 return page->objects;
3421}
3422
434e245d
CL
3423static int validate_slab(struct kmem_cache *s, struct page *page,
3424 unsigned long *map)
53e15af0
CL
3425{
3426 void *p;
a973e9dd 3427 void *addr = page_address(page);
53e15af0
CL
3428
3429 if (!check_slab(s, page) ||
3430 !on_freelist(s, page, NULL))
3431 return 0;
3432
3433 /* Now we know that a valid freelist exists */
39b26464 3434 bitmap_zero(map, page->objects);
53e15af0 3435
7656c72b
CL
3436 for_each_free_object(p, s, page->freelist) {
3437 set_bit(slab_index(p, s, addr), map);
53e15af0
CL
3438 if (!check_object(s, page, p, 0))
3439 return 0;
3440 }
3441
224a88be 3442 for_each_object(p, s, addr, page->objects)
7656c72b 3443 if (!test_bit(slab_index(p, s, addr), map))
53e15af0
CL
3444 if (!check_object(s, page, p, 1))
3445 return 0;
3446 return 1;
3447}
3448
434e245d
CL
3449static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3450 unsigned long *map)
53e15af0
CL
3451{
3452 if (slab_trylock(page)) {
434e245d 3453 validate_slab(s, page, map);
53e15af0
CL
3454 slab_unlock(page);
3455 } else
3456 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3457 s->name, page);
53e15af0
CL
3458}
3459
434e245d
CL
3460static int validate_slab_node(struct kmem_cache *s,
3461 struct kmem_cache_node *n, unsigned long *map)
53e15af0
CL
3462{
3463 unsigned long count = 0;
3464 struct page *page;
3465 unsigned long flags;
3466
3467 spin_lock_irqsave(&n->list_lock, flags);
3468
3469 list_for_each_entry(page, &n->partial, lru) {
434e245d 3470 validate_slab_slab(s, page, map);
53e15af0
CL
3471 count++;
3472 }
3473 if (count != n->nr_partial)
3474 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3475 "counter=%ld\n", s->name, count, n->nr_partial);
3476
3477 if (!(s->flags & SLAB_STORE_USER))
3478 goto out;
3479
3480 list_for_each_entry(page, &n->full, lru) {
434e245d 3481 validate_slab_slab(s, page, map);
53e15af0
CL
3482 count++;
3483 }
3484 if (count != atomic_long_read(&n->nr_slabs))
3485 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3486 "counter=%ld\n", s->name, count,
3487 atomic_long_read(&n->nr_slabs));
3488
3489out:
3490 spin_unlock_irqrestore(&n->list_lock, flags);
3491 return count;
3492}
3493
434e245d 3494static long validate_slab_cache(struct kmem_cache *s)
53e15af0
CL
3495{
3496 int node;
3497 unsigned long count = 0;
205ab99d 3498 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
434e245d
CL
3499 sizeof(unsigned long), GFP_KERNEL);
3500
3501 if (!map)
3502 return -ENOMEM;
53e15af0
CL
3503
3504 flush_all(s);
f64dc58c 3505 for_each_node_state(node, N_NORMAL_MEMORY) {
53e15af0
CL
3506 struct kmem_cache_node *n = get_node(s, node);
3507
434e245d 3508 count += validate_slab_node(s, n, map);
53e15af0 3509 }
434e245d 3510 kfree(map);
53e15af0
CL
3511 return count;
3512}
3513
b3459709
CL
3514#ifdef SLUB_RESILIENCY_TEST
3515static void resiliency_test(void)
3516{
3517 u8 *p;
3518
a016471a
DR
3519 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || SLUB_PAGE_SHIFT < 10);
3520
b3459709
CL
3521 printk(KERN_ERR "SLUB resiliency testing\n");
3522 printk(KERN_ERR "-----------------------\n");
3523 printk(KERN_ERR "A. Corruption after allocation\n");
3524
3525 p = kzalloc(16, GFP_KERNEL);
3526 p[16] = 0x12;
3527 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3528 " 0x12->0x%p\n\n", p + 16);
3529
a016471a 3530 validate_slab_cache(kmalloc_caches[4]);
b3459709
CL
3531
3532 /* Hmmm... The next two are dangerous */
3533 p = kzalloc(32, GFP_KERNEL);
3534 p[32 + sizeof(void *)] = 0x34;
3535 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
3adbefee
IM
3536 " 0x34 -> -0x%p\n", p);
3537 printk(KERN_ERR
3538 "If allocated object is overwritten then not detectable\n\n");
b3459709 3539
a016471a 3540 validate_slab_cache(kmalloc_caches[5]);
b3459709
CL
3541 p = kzalloc(64, GFP_KERNEL);
3542 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3543 *p = 0x56;
3544 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3545 p);
3adbefee
IM
3546 printk(KERN_ERR
3547 "If allocated object is overwritten then not detectable\n\n");
a016471a 3548 validate_slab_cache(kmalloc_caches[6]);
b3459709
CL
3549
3550 printk(KERN_ERR "\nB. Corruption after free\n");
3551 p = kzalloc(128, GFP_KERNEL);
3552 kfree(p);
3553 *p = 0x78;
3554 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
a016471a 3555 validate_slab_cache(kmalloc_caches[7]);
b3459709
CL
3556
3557 p = kzalloc(256, GFP_KERNEL);
3558 kfree(p);
3559 p[50] = 0x9a;
3adbefee
IM
3560 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3561 p);
a016471a 3562 validate_slab_cache(kmalloc_caches[8]);
b3459709
CL
3563
3564 p = kzalloc(512, GFP_KERNEL);
3565 kfree(p);
3566 p[512] = 0xab;
3567 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
a016471a 3568 validate_slab_cache(kmalloc_caches[9]);
b3459709
CL
3569}
3570#else
3571static void resiliency_test(void) {};
3572#endif
3573
88a420e4 3574/*
672bba3a 3575 * Generate lists of code addresses where slabcache objects are allocated
88a420e4
CL
3576 * and freed.
3577 */
3578
3579struct location {
3580 unsigned long count;
ce71e27c 3581 unsigned long addr;
45edfa58
CL
3582 long long sum_time;
3583 long min_time;
3584 long max_time;
3585 long min_pid;
3586 long max_pid;
174596a0 3587 DECLARE_BITMAP(cpus, NR_CPUS);
45edfa58 3588 nodemask_t nodes;
88a420e4
CL
3589};
3590
3591struct loc_track {
3592 unsigned long max;
3593 unsigned long count;
3594 struct location *loc;
3595};
3596
3597static void free_loc_track(struct loc_track *t)
3598{
3599 if (t->max)
3600 free_pages((unsigned long)t->loc,
3601 get_order(sizeof(struct location) * t->max));
3602}
3603
68dff6a9 3604static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
88a420e4
CL
3605{
3606 struct location *l;
3607 int order;
3608
88a420e4
CL
3609 order = get_order(sizeof(struct location) * max);
3610
68dff6a9 3611 l = (void *)__get_free_pages(flags, order);
88a420e4
CL
3612 if (!l)
3613 return 0;
3614
3615 if (t->count) {
3616 memcpy(l, t->loc, sizeof(struct location) * t->count);
3617 free_loc_track(t);
3618 }
3619 t->max = max;
3620 t->loc = l;
3621 return 1;
3622}
3623
3624static int add_location(struct loc_track *t, struct kmem_cache *s,
45edfa58 3625 const struct track *track)
88a420e4
CL
3626{
3627 long start, end, pos;
3628 struct location *l;
ce71e27c 3629 unsigned long caddr;
45edfa58 3630 unsigned long age = jiffies - track->when;
88a420e4
CL
3631
3632 start = -1;
3633 end = t->count;
3634
3635 for ( ; ; ) {
3636 pos = start + (end - start + 1) / 2;
3637
3638 /*
3639 * There is nothing at "end". If we end up there
3640 * we need to add something to before end.
3641 */
3642 if (pos == end)
3643 break;
3644
3645 caddr = t->loc[pos].addr;
45edfa58
CL
3646 if (track->addr == caddr) {
3647
3648 l = &t->loc[pos];
3649 l->count++;
3650 if (track->when) {
3651 l->sum_time += age;
3652 if (age < l->min_time)
3653 l->min_time = age;
3654 if (age > l->max_time)
3655 l->max_time = age;
3656
3657 if (track->pid < l->min_pid)
3658 l->min_pid = track->pid;
3659 if (track->pid > l->max_pid)
3660 l->max_pid = track->pid;
3661
174596a0
RR
3662 cpumask_set_cpu(track->cpu,
3663 to_cpumask(l->cpus));
45edfa58
CL
3664 }
3665 node_set(page_to_nid(virt_to_page(track)), l->nodes);
88a420e4
CL
3666 return 1;
3667 }
3668
45edfa58 3669 if (track->addr < caddr)
88a420e4
CL
3670 end = pos;
3671 else
3672 start = pos;
3673 }
3674
3675 /*
672bba3a 3676 * Not found. Insert new tracking element.
88a420e4 3677 */
68dff6a9 3678 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
88a420e4
CL
3679 return 0;
3680
3681 l = t->loc + pos;
3682 if (pos < t->count)
3683 memmove(l + 1, l,
3684 (t->count - pos) * sizeof(struct location));
3685 t->count++;
3686 l->count = 1;
45edfa58
CL
3687 l->addr = track->addr;
3688 l->sum_time = age;
3689 l->min_time = age;
3690 l->max_time = age;
3691 l->min_pid = track->pid;
3692 l->max_pid = track->pid;
174596a0
RR
3693 cpumask_clear(to_cpumask(l->cpus));
3694 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
45edfa58
CL
3695 nodes_clear(l->nodes);
3696 node_set(page_to_nid(virt_to_page(track)), l->nodes);
88a420e4
CL
3697 return 1;
3698}
3699
3700static void process_slab(struct loc_track *t, struct kmem_cache *s,
bbd7d57b
ED
3701 struct page *page, enum track_item alloc,
3702 long *map)
88a420e4 3703{
a973e9dd 3704 void *addr = page_address(page);
88a420e4
CL
3705 void *p;
3706
39b26464 3707 bitmap_zero(map, page->objects);
7656c72b
CL
3708 for_each_free_object(p, s, page->freelist)
3709 set_bit(slab_index(p, s, addr), map);
88a420e4 3710
224a88be 3711 for_each_object(p, s, addr, page->objects)
45edfa58
CL
3712 if (!test_bit(slab_index(p, s, addr), map))
3713 add_location(t, s, get_track(s, p, alloc));
88a420e4
CL
3714}
3715
3716static int list_locations(struct kmem_cache *s, char *buf,
3717 enum track_item alloc)
3718{
e374d483 3719 int len = 0;
88a420e4 3720 unsigned long i;
68dff6a9 3721 struct loc_track t = { 0, 0, NULL };
88a420e4 3722 int node;
bbd7d57b
ED
3723 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
3724 sizeof(unsigned long), GFP_KERNEL);
88a420e4 3725
bbd7d57b
ED
3726 if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3727 GFP_TEMPORARY)) {
3728 kfree(map);
68dff6a9 3729 return sprintf(buf, "Out of memory\n");
bbd7d57b 3730 }
88a420e4
CL
3731 /* Push back cpu slabs */
3732 flush_all(s);
3733
f64dc58c 3734 for_each_node_state(node, N_NORMAL_MEMORY) {
88a420e4
CL
3735 struct kmem_cache_node *n = get_node(s, node);
3736 unsigned long flags;
3737 struct page *page;
3738
9e86943b 3739 if (!atomic_long_read(&n->nr_slabs))
88a420e4
CL
3740 continue;
3741
3742 spin_lock_irqsave(&n->list_lock, flags);
3743 list_for_each_entry(page, &n->partial, lru)
bbd7d57b 3744 process_slab(&t, s, page, alloc, map);
88a420e4 3745 list_for_each_entry(page, &n->full, lru)
bbd7d57b 3746 process_slab(&t, s, page, alloc, map);
88a420e4
CL
3747 spin_unlock_irqrestore(&n->list_lock, flags);
3748 }
3749
3750 for (i = 0; i < t.count; i++) {
45edfa58 3751 struct location *l = &t.loc[i];
88a420e4 3752
9c246247 3753 if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
88a420e4 3754 break;
e374d483 3755 len += sprintf(buf + len, "%7ld ", l->count);
45edfa58
CL
3756
3757 if (l->addr)
e374d483 3758 len += sprint_symbol(buf + len, (unsigned long)l->addr);
88a420e4 3759 else
e374d483 3760 len += sprintf(buf + len, "<not-available>");
45edfa58
CL
3761
3762 if (l->sum_time != l->min_time) {
e374d483 3763 len += sprintf(buf + len, " age=%ld/%ld/%ld",
f8bd2258
RZ
3764 l->min_time,
3765 (long)div_u64(l->sum_time, l->count),
3766 l->max_time);
45edfa58 3767 } else
e374d483 3768 len += sprintf(buf + len, " age=%ld",
45edfa58
CL
3769 l->min_time);
3770
3771 if (l->min_pid != l->max_pid)
e374d483 3772 len += sprintf(buf + len, " pid=%ld-%ld",
45edfa58
CL
3773 l->min_pid, l->max_pid);
3774 else
e374d483 3775 len += sprintf(buf + len, " pid=%ld",
45edfa58
CL
3776 l->min_pid);
3777
174596a0
RR
3778 if (num_online_cpus() > 1 &&
3779 !cpumask_empty(to_cpumask(l->cpus)) &&
e374d483
HH
3780 len < PAGE_SIZE - 60) {
3781 len += sprintf(buf + len, " cpus=");
3782 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
174596a0 3783 to_cpumask(l->cpus));
45edfa58
CL
3784 }
3785
62bc62a8 3786 if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
e374d483
HH
3787 len < PAGE_SIZE - 60) {
3788 len += sprintf(buf + len, " nodes=");
3789 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
45edfa58
CL
3790 l->nodes);
3791 }
3792
e374d483 3793 len += sprintf(buf + len, "\n");
88a420e4
CL
3794 }
3795
3796 free_loc_track(&t);
bbd7d57b 3797 kfree(map);
88a420e4 3798 if (!t.count)
e374d483
HH
3799 len += sprintf(buf, "No data\n");
3800 return len;
88a420e4
CL
3801}
3802
81819f0f 3803enum slab_stat_type {
205ab99d
CL
3804 SL_ALL, /* All slabs */
3805 SL_PARTIAL, /* Only partially allocated slabs */
3806 SL_CPU, /* Only slabs used for cpu caches */
3807 SL_OBJECTS, /* Determine allocated objects not slabs */
3808 SL_TOTAL /* Determine object capacity not slabs */
81819f0f
CL
3809};
3810
205ab99d 3811#define SO_ALL (1 << SL_ALL)
81819f0f
CL
3812#define SO_PARTIAL (1 << SL_PARTIAL)
3813#define SO_CPU (1 << SL_CPU)
3814#define SO_OBJECTS (1 << SL_OBJECTS)
205ab99d 3815#define SO_TOTAL (1 << SL_TOTAL)
81819f0f 3816
62e5c4b4
CG
3817static ssize_t show_slab_objects(struct kmem_cache *s,
3818 char *buf, unsigned long flags)
81819f0f
CL
3819{
3820 unsigned long total = 0;
81819f0f
CL
3821 int node;
3822 int x;
3823 unsigned long *nodes;
3824 unsigned long *per_cpu;
3825
3826 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
62e5c4b4
CG
3827 if (!nodes)
3828 return -ENOMEM;
81819f0f
CL
3829 per_cpu = nodes + nr_node_ids;
3830
205ab99d
CL
3831 if (flags & SO_CPU) {
3832 int cpu;
81819f0f 3833
205ab99d 3834 for_each_possible_cpu(cpu) {
9dfc6e68 3835 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
dfb4f096 3836
205ab99d
CL
3837 if (!c || c->node < 0)
3838 continue;
3839
3840 if (c->page) {
3841 if (flags & SO_TOTAL)
3842 x = c->page->objects;
3843 else if (flags & SO_OBJECTS)
3844 x = c->page->inuse;
81819f0f
CL
3845 else
3846 x = 1;
205ab99d 3847
81819f0f 3848 total += x;
205ab99d 3849 nodes[c->node] += x;
81819f0f 3850 }
205ab99d 3851 per_cpu[c->node]++;
81819f0f
CL
3852 }
3853 }
3854
205ab99d
CL
3855 if (flags & SO_ALL) {
3856 for_each_node_state(node, N_NORMAL_MEMORY) {
3857 struct kmem_cache_node *n = get_node(s, node);
3858
3859 if (flags & SO_TOTAL)
3860 x = atomic_long_read(&n->total_objects);
3861 else if (flags & SO_OBJECTS)
3862 x = atomic_long_read(&n->total_objects) -
3863 count_partial(n, count_free);
81819f0f 3864
81819f0f 3865 else
205ab99d 3866 x = atomic_long_read(&n->nr_slabs);
81819f0f
CL
3867 total += x;
3868 nodes[node] += x;
3869 }
3870
205ab99d
CL
3871 } else if (flags & SO_PARTIAL) {
3872 for_each_node_state(node, N_NORMAL_MEMORY) {
3873 struct kmem_cache_node *n = get_node(s, node);
81819f0f 3874
205ab99d
CL
3875 if (flags & SO_TOTAL)
3876 x = count_partial(n, count_total);
3877 else if (flags & SO_OBJECTS)
3878 x = count_partial(n, count_inuse);
81819f0f 3879 else
205ab99d 3880 x = n->nr_partial;
81819f0f
CL
3881 total += x;
3882 nodes[node] += x;
3883 }
3884 }
81819f0f
CL
3885 x = sprintf(buf, "%lu", total);
3886#ifdef CONFIG_NUMA
f64dc58c 3887 for_each_node_state(node, N_NORMAL_MEMORY)
81819f0f
CL
3888 if (nodes[node])
3889 x += sprintf(buf + x, " N%d=%lu",
3890 node, nodes[node]);
3891#endif
3892 kfree(nodes);
3893 return x + sprintf(buf + x, "\n");
3894}
3895
3896static int any_slab_objects(struct kmem_cache *s)
3897{
3898 int node;
81819f0f 3899
dfb4f096 3900 for_each_online_node(node) {
81819f0f
CL
3901 struct kmem_cache_node *n = get_node(s, node);
3902
dfb4f096
CL
3903 if (!n)
3904 continue;
3905
4ea33e2d 3906 if (atomic_long_read(&n->total_objects))
81819f0f
CL
3907 return 1;
3908 }
3909 return 0;
3910}
3911
3912#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3913#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3914
3915struct slab_attribute {
3916 struct attribute attr;
3917 ssize_t (*show)(struct kmem_cache *s, char *buf);
3918 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3919};
3920
3921#define SLAB_ATTR_RO(_name) \
3922 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3923
3924#define SLAB_ATTR(_name) \
3925 static struct slab_attribute _name##_attr = \
3926 __ATTR(_name, 0644, _name##_show, _name##_store)
3927
81819f0f
CL
3928static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3929{
3930 return sprintf(buf, "%d\n", s->size);
3931}
3932SLAB_ATTR_RO(slab_size);
3933
3934static ssize_t align_show(struct kmem_cache *s, char *buf)
3935{
3936 return sprintf(buf, "%d\n", s->align);
3937}
3938SLAB_ATTR_RO(align);
3939
3940static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3941{
3942 return sprintf(buf, "%d\n", s->objsize);
3943}
3944SLAB_ATTR_RO(object_size);
3945
3946static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3947{
834f3d11 3948 return sprintf(buf, "%d\n", oo_objects(s->oo));
81819f0f
CL
3949}
3950SLAB_ATTR_RO(objs_per_slab);
3951
06b285dc
CL
3952static ssize_t order_store(struct kmem_cache *s,
3953 const char *buf, size_t length)
3954{
0121c619
CL
3955 unsigned long order;
3956 int err;
3957
3958 err = strict_strtoul(buf, 10, &order);
3959 if (err)
3960 return err;
06b285dc
CL
3961
3962 if (order > slub_max_order || order < slub_min_order)
3963 return -EINVAL;
3964
3965 calculate_sizes(s, order);
3966 return length;
3967}
3968
81819f0f
CL
3969static ssize_t order_show(struct kmem_cache *s, char *buf)
3970{
834f3d11 3971 return sprintf(buf, "%d\n", oo_order(s->oo));
81819f0f 3972}
06b285dc 3973SLAB_ATTR(order);
81819f0f 3974
73d342b1
DR
3975static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
3976{
3977 return sprintf(buf, "%lu\n", s->min_partial);
3978}
3979
3980static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
3981 size_t length)
3982{
3983 unsigned long min;
3984 int err;
3985
3986 err = strict_strtoul(buf, 10, &min);
3987 if (err)
3988 return err;
3989
c0bdb232 3990 set_min_partial(s, min);
73d342b1
DR
3991 return length;
3992}
3993SLAB_ATTR(min_partial);
3994
81819f0f
CL
3995static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3996{
3997 if (s->ctor) {
3998 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3999
4000 return n + sprintf(buf + n, "\n");
4001 }
4002 return 0;
4003}
4004SLAB_ATTR_RO(ctor);
4005
81819f0f
CL
4006static ssize_t aliases_show(struct kmem_cache *s, char *buf)
4007{
4008 return sprintf(buf, "%d\n", s->refcount - 1);
4009}
4010SLAB_ATTR_RO(aliases);
4011
4012static ssize_t slabs_show(struct kmem_cache *s, char *buf)
4013{
205ab99d 4014 return show_slab_objects(s, buf, SO_ALL);
81819f0f
CL
4015}
4016SLAB_ATTR_RO(slabs);
4017
4018static ssize_t partial_show(struct kmem_cache *s, char *buf)
4019{
d9acf4b7 4020 return show_slab_objects(s, buf, SO_PARTIAL);
81819f0f
CL
4021}
4022SLAB_ATTR_RO(partial);
4023
4024static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
4025{
d9acf4b7 4026 return show_slab_objects(s, buf, SO_CPU);
81819f0f
CL
4027}
4028SLAB_ATTR_RO(cpu_slabs);
4029
4030static ssize_t objects_show(struct kmem_cache *s, char *buf)
4031{
205ab99d 4032 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
81819f0f
CL
4033}
4034SLAB_ATTR_RO(objects);
4035
205ab99d
CL
4036static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
4037{
4038 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
4039}
4040SLAB_ATTR_RO(objects_partial);
4041
4042static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
4043{
4044 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
4045}
4046SLAB_ATTR_RO(total_objects);
4047
81819f0f
CL
4048static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
4049{
4050 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
4051}
4052
4053static ssize_t sanity_checks_store(struct kmem_cache *s,
4054 const char *buf, size_t length)
4055{
4056 s->flags &= ~SLAB_DEBUG_FREE;
4057 if (buf[0] == '1')
4058 s->flags |= SLAB_DEBUG_FREE;
4059 return length;
4060}
4061SLAB_ATTR(sanity_checks);
4062
4063static ssize_t trace_show(struct kmem_cache *s, char *buf)
4064{
4065 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
4066}
4067
4068static ssize_t trace_store(struct kmem_cache *s, const char *buf,
4069 size_t length)
4070{
4071 s->flags &= ~SLAB_TRACE;
4072 if (buf[0] == '1')
4073 s->flags |= SLAB_TRACE;
4074 return length;
4075}
4076SLAB_ATTR(trace);
4077
4c13dd3b
DM
4078#ifdef CONFIG_FAILSLAB
4079static ssize_t failslab_show(struct kmem_cache *s, char *buf)
4080{
4081 return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
4082}
4083
4084static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
4085 size_t length)
4086{
4087 s->flags &= ~SLAB_FAILSLAB;
4088 if (buf[0] == '1')
4089 s->flags |= SLAB_FAILSLAB;
4090 return length;
4091}
4092SLAB_ATTR(failslab);
4093#endif
4094
81819f0f
CL
4095static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
4096{
4097 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
4098}
4099
4100static ssize_t reclaim_account_store(struct kmem_cache *s,
4101 const char *buf, size_t length)
4102{
4103 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
4104 if (buf[0] == '1')
4105 s->flags |= SLAB_RECLAIM_ACCOUNT;
4106 return length;
4107}
4108SLAB_ATTR(reclaim_account);
4109
4110static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
4111{
5af60839 4112 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
81819f0f
CL
4113}
4114SLAB_ATTR_RO(hwcache_align);
4115
4116#ifdef CONFIG_ZONE_DMA
4117static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
4118{
4119 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
4120}
4121SLAB_ATTR_RO(cache_dma);
4122#endif
4123
4124static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
4125{
4126 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
4127}
4128SLAB_ATTR_RO(destroy_by_rcu);
4129
4130static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
4131{
4132 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
4133}
4134
4135static ssize_t red_zone_store(struct kmem_cache *s,
4136 const char *buf, size_t length)
4137{
4138 if (any_slab_objects(s))
4139 return -EBUSY;
4140
4141 s->flags &= ~SLAB_RED_ZONE;
4142 if (buf[0] == '1')
4143 s->flags |= SLAB_RED_ZONE;
06b285dc 4144 calculate_sizes(s, -1);
81819f0f
CL
4145 return length;
4146}
4147SLAB_ATTR(red_zone);
4148
4149static ssize_t poison_show(struct kmem_cache *s, char *buf)
4150{
4151 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
4152}
4153
4154static ssize_t poison_store(struct kmem_cache *s,
4155 const char *buf, size_t length)
4156{
4157 if (any_slab_objects(s))
4158 return -EBUSY;
4159
4160 s->flags &= ~SLAB_POISON;
4161 if (buf[0] == '1')
4162 s->flags |= SLAB_POISON;
06b285dc 4163 calculate_sizes(s, -1);
81819f0f
CL
4164 return length;
4165}
4166SLAB_ATTR(poison);
4167
4168static ssize_t store_user_show(struct kmem_cache *s, char *buf)
4169{
4170 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
4171}
4172
4173static ssize_t store_user_store(struct kmem_cache *s,
4174 const char *buf, size_t length)
4175{
4176 if (any_slab_objects(s))
4177 return -EBUSY;
4178
4179 s->flags &= ~SLAB_STORE_USER;
4180 if (buf[0] == '1')
4181 s->flags |= SLAB_STORE_USER;
06b285dc 4182 calculate_sizes(s, -1);
81819f0f
CL
4183 return length;
4184}
4185SLAB_ATTR(store_user);
4186
53e15af0
CL
4187static ssize_t validate_show(struct kmem_cache *s, char *buf)
4188{
4189 return 0;
4190}
4191
4192static ssize_t validate_store(struct kmem_cache *s,
4193 const char *buf, size_t length)
4194{
434e245d
CL
4195 int ret = -EINVAL;
4196
4197 if (buf[0] == '1') {
4198 ret = validate_slab_cache(s);
4199 if (ret >= 0)
4200 ret = length;
4201 }
4202 return ret;
53e15af0
CL
4203}
4204SLAB_ATTR(validate);
4205
2086d26a
CL
4206static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4207{
4208 return 0;
4209}
4210
4211static ssize_t shrink_store(struct kmem_cache *s,
4212 const char *buf, size_t length)
4213{
4214 if (buf[0] == '1') {
4215 int rc = kmem_cache_shrink(s);
4216
4217 if (rc)
4218 return rc;
4219 } else
4220 return -EINVAL;
4221 return length;
4222}
4223SLAB_ATTR(shrink);
4224
88a420e4
CL
4225static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4226{
4227 if (!(s->flags & SLAB_STORE_USER))
4228 return -ENOSYS;
4229 return list_locations(s, buf, TRACK_ALLOC);
4230}
4231SLAB_ATTR_RO(alloc_calls);
4232
4233static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4234{
4235 if (!(s->flags & SLAB_STORE_USER))
4236 return -ENOSYS;
4237 return list_locations(s, buf, TRACK_FREE);
4238}
4239SLAB_ATTR_RO(free_calls);
4240
81819f0f 4241#ifdef CONFIG_NUMA
9824601e 4242static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
81819f0f 4243{
9824601e 4244 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
81819f0f
CL
4245}
4246
9824601e 4247static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
81819f0f
CL
4248 const char *buf, size_t length)
4249{
0121c619
CL
4250 unsigned long ratio;
4251 int err;
4252
4253 err = strict_strtoul(buf, 10, &ratio);
4254 if (err)
4255 return err;
4256
e2cb96b7 4257 if (ratio <= 100)
0121c619 4258 s->remote_node_defrag_ratio = ratio * 10;
81819f0f 4259
81819f0f
CL
4260 return length;
4261}
9824601e 4262SLAB_ATTR(remote_node_defrag_ratio);
81819f0f
CL
4263#endif
4264
8ff12cfc 4265#ifdef CONFIG_SLUB_STATS
8ff12cfc
CL
4266static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4267{
4268 unsigned long sum = 0;
4269 int cpu;
4270 int len;
4271 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4272
4273 if (!data)
4274 return -ENOMEM;
4275
4276 for_each_online_cpu(cpu) {
9dfc6e68 4277 unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
8ff12cfc
CL
4278
4279 data[cpu] = x;
4280 sum += x;
4281 }
4282
4283 len = sprintf(buf, "%lu", sum);
4284
50ef37b9 4285#ifdef CONFIG_SMP
8ff12cfc
CL
4286 for_each_online_cpu(cpu) {
4287 if (data[cpu] && len < PAGE_SIZE - 20)
50ef37b9 4288 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
8ff12cfc 4289 }
50ef37b9 4290#endif
8ff12cfc
CL
4291 kfree(data);
4292 return len + sprintf(buf + len, "\n");
4293}
4294
78eb00cc
DR
4295static void clear_stat(struct kmem_cache *s, enum stat_item si)
4296{
4297 int cpu;
4298
4299 for_each_online_cpu(cpu)
9dfc6e68 4300 per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
78eb00cc
DR
4301}
4302
8ff12cfc
CL
4303#define STAT_ATTR(si, text) \
4304static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4305{ \
4306 return show_stat(s, buf, si); \
4307} \
78eb00cc
DR
4308static ssize_t text##_store(struct kmem_cache *s, \
4309 const char *buf, size_t length) \
4310{ \
4311 if (buf[0] != '0') \
4312 return -EINVAL; \
4313 clear_stat(s, si); \
4314 return length; \
4315} \
4316SLAB_ATTR(text); \
8ff12cfc
CL
4317
4318STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4319STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4320STAT_ATTR(FREE_FASTPATH, free_fastpath);
4321STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4322STAT_ATTR(FREE_FROZEN, free_frozen);
4323STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4324STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4325STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4326STAT_ATTR(ALLOC_SLAB, alloc_slab);
4327STAT_ATTR(ALLOC_REFILL, alloc_refill);
4328STAT_ATTR(FREE_SLAB, free_slab);
4329STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4330STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4331STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4332STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4333STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4334STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
65c3376a 4335STAT_ATTR(ORDER_FALLBACK, order_fallback);
8ff12cfc
CL
4336#endif
4337
06428780 4338static struct attribute *slab_attrs[] = {
81819f0f
CL
4339 &slab_size_attr.attr,
4340 &object_size_attr.attr,
4341 &objs_per_slab_attr.attr,
4342 &order_attr.attr,
73d342b1 4343 &min_partial_attr.attr,
81819f0f 4344 &objects_attr.attr,
205ab99d
CL
4345 &objects_partial_attr.attr,
4346 &total_objects_attr.attr,
81819f0f
CL
4347 &slabs_attr.attr,
4348 &partial_attr.attr,
4349 &cpu_slabs_attr.attr,
4350 &ctor_attr.attr,
81819f0f
CL
4351 &aliases_attr.attr,
4352 &align_attr.attr,
4353 &sanity_checks_attr.attr,
4354 &trace_attr.attr,
4355 &hwcache_align_attr.attr,
4356 &reclaim_account_attr.attr,
4357 &destroy_by_rcu_attr.attr,
4358 &red_zone_attr.attr,
4359 &poison_attr.attr,
4360 &store_user_attr.attr,
53e15af0 4361 &validate_attr.attr,
2086d26a 4362 &shrink_attr.attr,
88a420e4
CL
4363 &alloc_calls_attr.attr,
4364 &free_calls_attr.attr,
81819f0f
CL
4365#ifdef CONFIG_ZONE_DMA
4366 &cache_dma_attr.attr,
4367#endif
4368#ifdef CONFIG_NUMA
9824601e 4369 &remote_node_defrag_ratio_attr.attr,
8ff12cfc
CL
4370#endif
4371#ifdef CONFIG_SLUB_STATS
4372 &alloc_fastpath_attr.attr,
4373 &alloc_slowpath_attr.attr,
4374 &free_fastpath_attr.attr,
4375 &free_slowpath_attr.attr,
4376 &free_frozen_attr.attr,
4377 &free_add_partial_attr.attr,
4378 &free_remove_partial_attr.attr,
4379 &alloc_from_partial_attr.attr,
4380 &alloc_slab_attr.attr,
4381 &alloc_refill_attr.attr,
4382 &free_slab_attr.attr,
4383 &cpuslab_flush_attr.attr,
4384 &deactivate_full_attr.attr,
4385 &deactivate_empty_attr.attr,
4386 &deactivate_to_head_attr.attr,
4387 &deactivate_to_tail_attr.attr,
4388 &deactivate_remote_frees_attr.attr,
65c3376a 4389 &order_fallback_attr.attr,
81819f0f 4390#endif
4c13dd3b
DM
4391#ifdef CONFIG_FAILSLAB
4392 &failslab_attr.attr,
4393#endif
4394
81819f0f
CL
4395 NULL
4396};
4397
4398static struct attribute_group slab_attr_group = {
4399 .attrs = slab_attrs,
4400};
4401
4402static ssize_t slab_attr_show(struct kobject *kobj,
4403 struct attribute *attr,
4404 char *buf)
4405{
4406 struct slab_attribute *attribute;
4407 struct kmem_cache *s;
4408 int err;
4409
4410 attribute = to_slab_attr(attr);
4411 s = to_slab(kobj);
4412
4413 if (!attribute->show)
4414 return -EIO;
4415
4416 err = attribute->show(s, buf);
4417
4418 return err;
4419}
4420
4421static ssize_t slab_attr_store(struct kobject *kobj,
4422 struct attribute *attr,
4423 const char *buf, size_t len)
4424{
4425 struct slab_attribute *attribute;
4426 struct kmem_cache *s;
4427 int err;
4428
4429 attribute = to_slab_attr(attr);
4430 s = to_slab(kobj);
4431
4432 if (!attribute->store)
4433 return -EIO;
4434
4435 err = attribute->store(s, buf, len);
4436
4437 return err;
4438}
4439
151c602f
CL
4440static void kmem_cache_release(struct kobject *kobj)
4441{
4442 struct kmem_cache *s = to_slab(kobj);
4443
84c1cf62 4444 kfree(s->name);
151c602f
CL
4445 kfree(s);
4446}
4447
52cf25d0 4448static const struct sysfs_ops slab_sysfs_ops = {
81819f0f
CL
4449 .show = slab_attr_show,
4450 .store = slab_attr_store,
4451};
4452
4453static struct kobj_type slab_ktype = {
4454 .sysfs_ops = &slab_sysfs_ops,
151c602f 4455 .release = kmem_cache_release
81819f0f
CL
4456};
4457
4458static int uevent_filter(struct kset *kset, struct kobject *kobj)
4459{
4460 struct kobj_type *ktype = get_ktype(kobj);
4461
4462 if (ktype == &slab_ktype)
4463 return 1;
4464 return 0;
4465}
4466
9cd43611 4467static const struct kset_uevent_ops slab_uevent_ops = {
81819f0f
CL
4468 .filter = uevent_filter,
4469};
4470
27c3a314 4471static struct kset *slab_kset;
81819f0f
CL
4472
4473#define ID_STR_LENGTH 64
4474
4475/* Create a unique string id for a slab cache:
6446faa2
CL
4476 *
4477 * Format :[flags-]size
81819f0f
CL
4478 */
4479static char *create_unique_id(struct kmem_cache *s)
4480{
4481 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4482 char *p = name;
4483
4484 BUG_ON(!name);
4485
4486 *p++ = ':';
4487 /*
4488 * First flags affecting slabcache operations. We will only
4489 * get here for aliasable slabs so we do not need to support
4490 * too many flags. The flags here must cover all flags that
4491 * are matched during merging to guarantee that the id is
4492 * unique.
4493 */
4494 if (s->flags & SLAB_CACHE_DMA)
4495 *p++ = 'd';
4496 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4497 *p++ = 'a';
4498 if (s->flags & SLAB_DEBUG_FREE)
4499 *p++ = 'F';
5a896d9e
VN
4500 if (!(s->flags & SLAB_NOTRACK))
4501 *p++ = 't';
81819f0f
CL
4502 if (p != name + 1)
4503 *p++ = '-';
4504 p += sprintf(p, "%07d", s->size);
4505 BUG_ON(p > name + ID_STR_LENGTH - 1);
4506 return name;
4507}
4508
4509static int sysfs_slab_add(struct kmem_cache *s)
4510{
4511 int err;
4512 const char *name;
4513 int unmergeable;
4514
4515 if (slab_state < SYSFS)
4516 /* Defer until later */
4517 return 0;
4518
4519 unmergeable = slab_unmergeable(s);
4520 if (unmergeable) {
4521 /*
4522 * Slabcache can never be merged so we can use the name proper.
4523 * This is typically the case for debug situations. In that
4524 * case we can catch duplicate names easily.
4525 */
27c3a314 4526 sysfs_remove_link(&slab_kset->kobj, s->name);
81819f0f
CL
4527 name = s->name;
4528 } else {
4529 /*
4530 * Create a unique name for the slab as a target
4531 * for the symlinks.
4532 */
4533 name = create_unique_id(s);
4534 }
4535
27c3a314 4536 s->kobj.kset = slab_kset;
1eada11c
GKH
4537 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4538 if (err) {
4539 kobject_put(&s->kobj);
81819f0f 4540 return err;
1eada11c 4541 }
81819f0f
CL
4542
4543 err = sysfs_create_group(&s->kobj, &slab_attr_group);
5788d8ad
XF
4544 if (err) {
4545 kobject_del(&s->kobj);
4546 kobject_put(&s->kobj);
81819f0f 4547 return err;
5788d8ad 4548 }
81819f0f
CL
4549 kobject_uevent(&s->kobj, KOBJ_ADD);
4550 if (!unmergeable) {
4551 /* Setup first alias */
4552 sysfs_slab_alias(s, s->name);
4553 kfree(name);
4554 }
4555 return 0;
4556}
4557
4558static void sysfs_slab_remove(struct kmem_cache *s)
4559{
2bce6485
CL
4560 if (slab_state < SYSFS)
4561 /*
4562 * Sysfs has not been setup yet so no need to remove the
4563 * cache from sysfs.
4564 */
4565 return;
4566
81819f0f
CL
4567 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4568 kobject_del(&s->kobj);
151c602f 4569 kobject_put(&s->kobj);
81819f0f
CL
4570}
4571
4572/*
4573 * Need to buffer aliases during bootup until sysfs becomes
9f6c708e 4574 * available lest we lose that information.
81819f0f
CL
4575 */
4576struct saved_alias {
4577 struct kmem_cache *s;
4578 const char *name;
4579 struct saved_alias *next;
4580};
4581
5af328a5 4582static struct saved_alias *alias_list;
81819f0f
CL
4583
4584static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4585{
4586 struct saved_alias *al;
4587
4588 if (slab_state == SYSFS) {
4589 /*
4590 * If we have a leftover link then remove it.
4591 */
27c3a314
GKH
4592 sysfs_remove_link(&slab_kset->kobj, name);
4593 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
81819f0f
CL
4594 }
4595
4596 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4597 if (!al)
4598 return -ENOMEM;
4599
4600 al->s = s;
4601 al->name = name;
4602 al->next = alias_list;
4603 alias_list = al;
4604 return 0;
4605}
4606
4607static int __init slab_sysfs_init(void)
4608{
5b95a4ac 4609 struct kmem_cache *s;
81819f0f
CL
4610 int err;
4611
2bce6485
CL
4612 down_write(&slub_lock);
4613
0ff21e46 4614 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
27c3a314 4615 if (!slab_kset) {
2bce6485 4616 up_write(&slub_lock);
81819f0f
CL
4617 printk(KERN_ERR "Cannot register slab subsystem.\n");
4618 return -ENOSYS;
4619 }
4620
26a7bd03
CL
4621 slab_state = SYSFS;
4622
5b95a4ac 4623 list_for_each_entry(s, &slab_caches, list) {
26a7bd03 4624 err = sysfs_slab_add(s);
5d540fb7
CL
4625 if (err)
4626 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4627 " to sysfs\n", s->name);
26a7bd03 4628 }
81819f0f
CL
4629
4630 while (alias_list) {
4631 struct saved_alias *al = alias_list;
4632
4633 alias_list = alias_list->next;
4634 err = sysfs_slab_alias(al->s, al->name);
5d540fb7
CL
4635 if (err)
4636 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4637 " %s to sysfs\n", s->name);
81819f0f
CL
4638 kfree(al);
4639 }
4640
2bce6485 4641 up_write(&slub_lock);
81819f0f
CL
4642 resiliency_test();
4643 return 0;
4644}
4645
4646__initcall(slab_sysfs_init);
81819f0f 4647#endif
57ed3eda
PE
4648
4649/*
4650 * The /proc/slabinfo ABI
4651 */
158a9624 4652#ifdef CONFIG_SLABINFO
57ed3eda
PE
4653static void print_slabinfo_header(struct seq_file *m)
4654{
4655 seq_puts(m, "slabinfo - version: 2.1\n");
4656 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4657 "<objperslab> <pagesperslab>");
4658 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4659 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4660 seq_putc(m, '\n');
4661}
4662
4663static void *s_start(struct seq_file *m, loff_t *pos)
4664{
4665 loff_t n = *pos;
4666
4667 down_read(&slub_lock);
4668 if (!n)
4669 print_slabinfo_header(m);
4670
4671 return seq_list_start(&slab_caches, *pos);
4672}
4673
4674static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4675{
4676 return seq_list_next(p, &slab_caches, pos);
4677}
4678
4679static void s_stop(struct seq_file *m, void *p)
4680{
4681 up_read(&slub_lock);
4682}
4683
4684static int s_show(struct seq_file *m, void *p)
4685{
4686 unsigned long nr_partials = 0;
4687 unsigned long nr_slabs = 0;
4688 unsigned long nr_inuse = 0;
205ab99d
CL
4689 unsigned long nr_objs = 0;
4690 unsigned long nr_free = 0;
57ed3eda
PE
4691 struct kmem_cache *s;
4692 int node;
4693
4694 s = list_entry(p, struct kmem_cache, list);
4695
4696 for_each_online_node(node) {
4697 struct kmem_cache_node *n = get_node(s, node);
4698
4699 if (!n)
4700 continue;
4701
4702 nr_partials += n->nr_partial;
4703 nr_slabs += atomic_long_read(&n->nr_slabs);
205ab99d
CL
4704 nr_objs += atomic_long_read(&n->total_objects);
4705 nr_free += count_partial(n, count_free);
57ed3eda
PE
4706 }
4707
205ab99d 4708 nr_inuse = nr_objs - nr_free;
57ed3eda
PE
4709
4710 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
834f3d11
CL
4711 nr_objs, s->size, oo_objects(s->oo),
4712 (1 << oo_order(s->oo)));
57ed3eda
PE
4713 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4714 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4715 0UL);
4716 seq_putc(m, '\n');
4717 return 0;
4718}
4719
7b3c3a50 4720static const struct seq_operations slabinfo_op = {
57ed3eda
PE
4721 .start = s_start,
4722 .next = s_next,
4723 .stop = s_stop,
4724 .show = s_show,
4725};
4726
7b3c3a50
AD
4727static int slabinfo_open(struct inode *inode, struct file *file)
4728{
4729 return seq_open(file, &slabinfo_op);
4730}
4731
4732static const struct file_operations proc_slabinfo_operations = {
4733 .open = slabinfo_open,
4734 .read = seq_read,
4735 .llseek = seq_lseek,
4736 .release = seq_release,
4737};
4738
4739static int __init slab_proc_init(void)
4740{
cf5d1131 4741 proc_create("slabinfo", S_IRUGO, NULL, &proc_slabinfo_operations);
7b3c3a50
AD
4742 return 0;
4743}
4744module_init(slab_proc_init);
158a9624 4745#endif /* CONFIG_SLABINFO */