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