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