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