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