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