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