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