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