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