SLUB: Do not use page->mapping
[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
dfb4f096 93 * freelist that allows lockless access to
894b8788
CL
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 *
81819f0f
CL
143 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
144 *
81819f0f
CL
145 * - Variable sizing of the per node arrays
146 */
147
148/* Enable to test recovery from slab corruption on boot */
149#undef SLUB_RESILIENCY_TEST
150
151#if PAGE_SHIFT <= 12
152
153/*
154 * Small page size. Make sure that we do not fragment memory
155 */
156#define DEFAULT_MAX_ORDER 1
157#define DEFAULT_MIN_OBJECTS 4
158
159#else
160
161/*
162 * Large page machines are customarily able to handle larger
163 * page orders.
164 */
165#define DEFAULT_MAX_ORDER 2
166#define DEFAULT_MIN_OBJECTS 8
167
168#endif
169
2086d26a
CL
170/*
171 * Mininum number of partial slabs. These will be left on the partial
172 * lists even if they are empty. kmem_cache_shrink may reclaim them.
173 */
e95eed57
CL
174#define MIN_PARTIAL 2
175
2086d26a
CL
176/*
177 * Maximum number of desirable partial slabs.
178 * The existence of more partial slabs makes kmem_cache_shrink
179 * sort the partial list by the number of objects in the.
180 */
181#define MAX_PARTIAL 10
182
81819f0f
CL
183#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
184 SLAB_POISON | SLAB_STORE_USER)
672bba3a 185
81819f0f
CL
186/*
187 * Set of flags that will prevent slab merging
188 */
189#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
190 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
191
192#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
193 SLAB_CACHE_DMA)
194
195#ifndef ARCH_KMALLOC_MINALIGN
47bfdc0d 196#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
81819f0f
CL
197#endif
198
199#ifndef ARCH_SLAB_MINALIGN
47bfdc0d 200#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
81819f0f
CL
201#endif
202
6300ea75
CL
203/*
204 * The page->inuse field is 16 bit thus we have this limitation
205 */
206#define MAX_OBJECTS_PER_SLAB 65535
207
81819f0f 208/* Internal SLUB flags */
1ceef402
CL
209#define __OBJECT_POISON 0x80000000 /* Poison object */
210#define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
81819f0f 211
65c02d4c
CL
212/* Not all arches define cache_line_size */
213#ifndef cache_line_size
214#define cache_line_size() L1_CACHE_BYTES
215#endif
216
81819f0f
CL
217static int kmem_size = sizeof(struct kmem_cache);
218
219#ifdef CONFIG_SMP
220static struct notifier_block slab_notifier;
221#endif
222
223static enum {
224 DOWN, /* No slab functionality available */
225 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
672bba3a 226 UP, /* Everything works but does not show up in sysfs */
81819f0f
CL
227 SYSFS /* Sysfs up */
228} slab_state = DOWN;
229
230/* A list of all slab caches on the system */
231static DECLARE_RWSEM(slub_lock);
5af328a5 232static LIST_HEAD(slab_caches);
81819f0f 233
02cbc874
CL
234/*
235 * Tracking user of a slab.
236 */
237struct track {
238 void *addr; /* Called from address */
239 int cpu; /* Was running on cpu */
240 int pid; /* Pid context */
241 unsigned long when; /* When did the operation occur */
242};
243
244enum track_item { TRACK_ALLOC, TRACK_FREE };
245
41ecc55b 246#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
81819f0f
CL
247static int sysfs_slab_add(struct kmem_cache *);
248static int sysfs_slab_alias(struct kmem_cache *, const char *);
249static void sysfs_slab_remove(struct kmem_cache *);
250#else
0c710013
CL
251static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
252static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
253 { return 0; }
254static inline void sysfs_slab_remove(struct kmem_cache *s) {}
81819f0f
CL
255#endif
256
257/********************************************************************
258 * Core slab cache functions
259 *******************************************************************/
260
261int slab_is_available(void)
262{
263 return slab_state >= UP;
264}
265
266static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
267{
268#ifdef CONFIG_NUMA
269 return s->node[node];
270#else
271 return &s->local_node;
272#endif
273}
274
dfb4f096
CL
275static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
276{
277 return &s->cpu_slab[cpu];
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
ba0268a8
CL
989static unsigned long kmem_cache_flags(unsigned long objsize,
990 unsigned long flags, const char *name,
991 void (*ctor)(void *, struct kmem_cache *, unsigned long))
41ecc55b
CL
992{
993 /*
994 * The page->offset field is only 16 bit wide. This is an offset
995 * in units of words from the beginning of an object. If the slab
996 * size is bigger then we cannot move the free pointer behind the
997 * object anymore.
998 *
999 * On 32 bit platforms the limit is 256k. On 64bit platforms
1000 * the limit is 512k.
1001 *
c59def9f 1002 * Debugging or ctor may create a need to move the free
41ecc55b
CL
1003 * pointer. Fail if this happens.
1004 */
ba0268a8
CL
1005 if (objsize >= 65535 * sizeof(void *)) {
1006 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
41ecc55b 1007 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
ba0268a8
CL
1008 BUG_ON(ctor);
1009 } else {
41ecc55b
CL
1010 /*
1011 * Enable debugging if selected on the kernel commandline.
1012 */
1013 if (slub_debug && (!slub_debug_slabs ||
ba0268a8 1014 strncmp(slub_debug_slabs, name,
41ecc55b 1015 strlen(slub_debug_slabs)) == 0))
ba0268a8
CL
1016 flags |= slub_debug;
1017 }
1018
1019 return flags;
41ecc55b
CL
1020}
1021#else
3ec09742
CL
1022static inline void setup_object_debug(struct kmem_cache *s,
1023 struct page *page, void *object) {}
41ecc55b 1024
3ec09742
CL
1025static inline int alloc_debug_processing(struct kmem_cache *s,
1026 struct page *page, void *object, void *addr) { return 0; }
41ecc55b 1027
3ec09742
CL
1028static inline int free_debug_processing(struct kmem_cache *s,
1029 struct page *page, void *object, void *addr) { return 0; }
41ecc55b 1030
41ecc55b
CL
1031static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1032 { return 1; }
1033static inline int check_object(struct kmem_cache *s, struct page *page,
1034 void *object, int active) { return 1; }
3ec09742 1035static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
ba0268a8
CL
1036static inline unsigned long kmem_cache_flags(unsigned long objsize,
1037 unsigned long flags, const char *name,
1038 void (*ctor)(void *, struct kmem_cache *, unsigned long))
1039{
1040 return flags;
1041}
41ecc55b
CL
1042#define slub_debug 0
1043#endif
81819f0f
CL
1044/*
1045 * Slab allocation and freeing
1046 */
1047static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1048{
1049 struct page * page;
1050 int pages = 1 << s->order;
1051
1052 if (s->order)
1053 flags |= __GFP_COMP;
1054
1055 if (s->flags & SLAB_CACHE_DMA)
1056 flags |= SLUB_DMA;
1057
e12ba74d
MG
1058 if (s->flags & SLAB_RECLAIM_ACCOUNT)
1059 flags |= __GFP_RECLAIMABLE;
1060
81819f0f
CL
1061 if (node == -1)
1062 page = alloc_pages(flags, s->order);
1063 else
1064 page = alloc_pages_node(node, flags, s->order);
1065
1066 if (!page)
1067 return NULL;
1068
1069 mod_zone_page_state(page_zone(page),
1070 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1071 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1072 pages);
1073
1074 return page;
1075}
1076
1077static void setup_object(struct kmem_cache *s, struct page *page,
1078 void *object)
1079{
3ec09742 1080 setup_object_debug(s, page, object);
4f104934 1081 if (unlikely(s->ctor))
a35afb83 1082 s->ctor(object, s, 0);
81819f0f
CL
1083}
1084
1085static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1086{
1087 struct page *page;
1088 struct kmem_cache_node *n;
1089 void *start;
1090 void *end;
1091 void *last;
1092 void *p;
1093
6cb06229 1094 BUG_ON(flags & GFP_SLAB_BUG_MASK);
81819f0f
CL
1095
1096 if (flags & __GFP_WAIT)
1097 local_irq_enable();
1098
6cb06229
CL
1099 page = allocate_slab(s,
1100 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
81819f0f
CL
1101 if (!page)
1102 goto out;
1103
1104 n = get_node(s, page_to_nid(page));
1105 if (n)
1106 atomic_long_inc(&n->nr_slabs);
1107 page->offset = s->offset / sizeof(void *);
1108 page->slab = s;
1109 page->flags |= 1 << PG_slab;
1110 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1111 SLAB_STORE_USER | SLAB_TRACE))
35e5d7ee 1112 SetSlabDebug(page);
81819f0f
CL
1113
1114 start = page_address(page);
1115 end = start + s->objects * s->size;
1116
1117 if (unlikely(s->flags & SLAB_POISON))
1118 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1119
1120 last = start;
7656c72b 1121 for_each_object(p, s, start) {
81819f0f
CL
1122 setup_object(s, page, last);
1123 set_freepointer(s, last, p);
1124 last = p;
1125 }
1126 setup_object(s, page, last);
1127 set_freepointer(s, last, NULL);
1128
1129 page->freelist = start;
1130 page->inuse = 0;
1131out:
1132 if (flags & __GFP_WAIT)
1133 local_irq_disable();
1134 return page;
1135}
1136
1137static void __free_slab(struct kmem_cache *s, struct page *page)
1138{
1139 int pages = 1 << s->order;
1140
c59def9f 1141 if (unlikely(SlabDebug(page))) {
81819f0f
CL
1142 void *p;
1143
1144 slab_pad_check(s, page);
c59def9f 1145 for_each_object(p, s, page_address(page))
81819f0f 1146 check_object(s, page, p, 0);
2208b764 1147 ClearSlabDebug(page);
81819f0f
CL
1148 }
1149
1150 mod_zone_page_state(page_zone(page),
1151 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1152 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1153 - pages);
1154
81819f0f
CL
1155 __free_pages(page, s->order);
1156}
1157
1158static void rcu_free_slab(struct rcu_head *h)
1159{
1160 struct page *page;
1161
1162 page = container_of((struct list_head *)h, struct page, lru);
1163 __free_slab(page->slab, page);
1164}
1165
1166static void free_slab(struct kmem_cache *s, struct page *page)
1167{
1168 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1169 /*
1170 * RCU free overloads the RCU head over the LRU
1171 */
1172 struct rcu_head *head = (void *)&page->lru;
1173
1174 call_rcu(head, rcu_free_slab);
1175 } else
1176 __free_slab(s, page);
1177}
1178
1179static void discard_slab(struct kmem_cache *s, struct page *page)
1180{
1181 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1182
1183 atomic_long_dec(&n->nr_slabs);
1184 reset_page_mapcount(page);
35e5d7ee 1185 __ClearPageSlab(page);
81819f0f
CL
1186 free_slab(s, page);
1187}
1188
1189/*
1190 * Per slab locking using the pagelock
1191 */
1192static __always_inline void slab_lock(struct page *page)
1193{
1194 bit_spin_lock(PG_locked, &page->flags);
1195}
1196
1197static __always_inline void slab_unlock(struct page *page)
1198{
1199 bit_spin_unlock(PG_locked, &page->flags);
1200}
1201
1202static __always_inline int slab_trylock(struct page *page)
1203{
1204 int rc = 1;
1205
1206 rc = bit_spin_trylock(PG_locked, &page->flags);
1207 return rc;
1208}
1209
1210/*
1211 * Management of partially allocated slabs
1212 */
e95eed57 1213static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
81819f0f 1214{
e95eed57
CL
1215 spin_lock(&n->list_lock);
1216 n->nr_partial++;
1217 list_add_tail(&page->lru, &n->partial);
1218 spin_unlock(&n->list_lock);
1219}
81819f0f 1220
e95eed57
CL
1221static void add_partial(struct kmem_cache_node *n, struct page *page)
1222{
81819f0f
CL
1223 spin_lock(&n->list_lock);
1224 n->nr_partial++;
1225 list_add(&page->lru, &n->partial);
1226 spin_unlock(&n->list_lock);
1227}
1228
1229static void remove_partial(struct kmem_cache *s,
1230 struct page *page)
1231{
1232 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1233
1234 spin_lock(&n->list_lock);
1235 list_del(&page->lru);
1236 n->nr_partial--;
1237 spin_unlock(&n->list_lock);
1238}
1239
1240/*
672bba3a 1241 * Lock slab and remove from the partial list.
81819f0f 1242 *
672bba3a 1243 * Must hold list_lock.
81819f0f 1244 */
4b6f0750 1245static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
81819f0f
CL
1246{
1247 if (slab_trylock(page)) {
1248 list_del(&page->lru);
1249 n->nr_partial--;
4b6f0750 1250 SetSlabFrozen(page);
81819f0f
CL
1251 return 1;
1252 }
1253 return 0;
1254}
1255
1256/*
672bba3a 1257 * Try to allocate a partial slab from a specific node.
81819f0f
CL
1258 */
1259static struct page *get_partial_node(struct kmem_cache_node *n)
1260{
1261 struct page *page;
1262
1263 /*
1264 * Racy check. If we mistakenly see no partial slabs then we
1265 * just allocate an empty slab. If we mistakenly try to get a
672bba3a
CL
1266 * partial slab and there is none available then get_partials()
1267 * will return NULL.
81819f0f
CL
1268 */
1269 if (!n || !n->nr_partial)
1270 return NULL;
1271
1272 spin_lock(&n->list_lock);
1273 list_for_each_entry(page, &n->partial, lru)
4b6f0750 1274 if (lock_and_freeze_slab(n, page))
81819f0f
CL
1275 goto out;
1276 page = NULL;
1277out:
1278 spin_unlock(&n->list_lock);
1279 return page;
1280}
1281
1282/*
672bba3a 1283 * Get a page from somewhere. Search in increasing NUMA distances.
81819f0f
CL
1284 */
1285static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1286{
1287#ifdef CONFIG_NUMA
1288 struct zonelist *zonelist;
1289 struct zone **z;
1290 struct page *page;
1291
1292 /*
672bba3a
CL
1293 * The defrag ratio allows a configuration of the tradeoffs between
1294 * inter node defragmentation and node local allocations. A lower
1295 * defrag_ratio increases the tendency to do local allocations
1296 * instead of attempting to obtain partial slabs from other nodes.
81819f0f 1297 *
672bba3a
CL
1298 * If the defrag_ratio is set to 0 then kmalloc() always
1299 * returns node local objects. If the ratio is higher then kmalloc()
1300 * may return off node objects because partial slabs are obtained
1301 * from other nodes and filled up.
81819f0f
CL
1302 *
1303 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
672bba3a
CL
1304 * defrag_ratio = 1000) then every (well almost) allocation will
1305 * first attempt to defrag slab caches on other nodes. This means
1306 * scanning over all nodes to look for partial slabs which may be
1307 * expensive if we do it every time we are trying to find a slab
1308 * with available objects.
81819f0f
CL
1309 */
1310 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1311 return NULL;
1312
1313 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1314 ->node_zonelists[gfp_zone(flags)];
1315 for (z = zonelist->zones; *z; z++) {
1316 struct kmem_cache_node *n;
1317
1318 n = get_node(s, zone_to_nid(*z));
1319
1320 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
e95eed57 1321 n->nr_partial > MIN_PARTIAL) {
81819f0f
CL
1322 page = get_partial_node(n);
1323 if (page)
1324 return page;
1325 }
1326 }
1327#endif
1328 return NULL;
1329}
1330
1331/*
1332 * Get a partial page, lock it and return it.
1333 */
1334static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1335{
1336 struct page *page;
1337 int searchnode = (node == -1) ? numa_node_id() : node;
1338
1339 page = get_partial_node(get_node(s, searchnode));
1340 if (page || (flags & __GFP_THISNODE))
1341 return page;
1342
1343 return get_any_partial(s, flags);
1344}
1345
1346/*
1347 * Move a page back to the lists.
1348 *
1349 * Must be called with the slab lock held.
1350 *
1351 * On exit the slab lock will have been dropped.
1352 */
4b6f0750 1353static void unfreeze_slab(struct kmem_cache *s, struct page *page)
81819f0f 1354{
e95eed57
CL
1355 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1356
4b6f0750 1357 ClearSlabFrozen(page);
81819f0f 1358 if (page->inuse) {
e95eed57 1359
81819f0f 1360 if (page->freelist)
e95eed57 1361 add_partial(n, page);
35e5d7ee 1362 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
e95eed57 1363 add_full(n, page);
81819f0f 1364 slab_unlock(page);
e95eed57 1365
81819f0f 1366 } else {
e95eed57
CL
1367 if (n->nr_partial < MIN_PARTIAL) {
1368 /*
672bba3a
CL
1369 * Adding an empty slab to the partial slabs in order
1370 * to avoid page allocator overhead. This slab needs
1371 * to come after the other slabs with objects in
1372 * order to fill them up. That way the size of the
1373 * partial list stays small. kmem_cache_shrink can
1374 * reclaim empty slabs from the partial list.
e95eed57
CL
1375 */
1376 add_partial_tail(n, page);
1377 slab_unlock(page);
1378 } else {
1379 slab_unlock(page);
1380 discard_slab(s, page);
1381 }
81819f0f
CL
1382 }
1383}
1384
1385/*
1386 * Remove the cpu slab
1387 */
dfb4f096 1388static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
81819f0f 1389{
dfb4f096 1390 struct page *page = c->page;
894b8788
CL
1391 /*
1392 * Merge cpu freelist into freelist. Typically we get here
1393 * because both freelists are empty. So this is unlikely
1394 * to occur.
1395 */
dfb4f096 1396 while (unlikely(c->freelist)) {
894b8788
CL
1397 void **object;
1398
1399 /* Retrieve object from cpu_freelist */
dfb4f096
CL
1400 object = c->freelist;
1401 c->freelist = c->freelist[page->offset];
894b8788
CL
1402
1403 /* And put onto the regular freelist */
1404 object[page->offset] = page->freelist;
1405 page->freelist = object;
1406 page->inuse--;
1407 }
dfb4f096 1408 c->page = NULL;
4b6f0750 1409 unfreeze_slab(s, page);
81819f0f
CL
1410}
1411
dfb4f096 1412static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
81819f0f 1413{
dfb4f096
CL
1414 slab_lock(c->page);
1415 deactivate_slab(s, c);
81819f0f
CL
1416}
1417
1418/*
1419 * Flush cpu slab.
1420 * Called from IPI handler with interrupts disabled.
1421 */
0c710013 1422static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
81819f0f 1423{
dfb4f096 1424 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
81819f0f 1425
dfb4f096
CL
1426 if (likely(c && c->page))
1427 flush_slab(s, c);
81819f0f
CL
1428}
1429
1430static void flush_cpu_slab(void *d)
1431{
1432 struct kmem_cache *s = d;
81819f0f 1433
dfb4f096 1434 __flush_cpu_slab(s, smp_processor_id());
81819f0f
CL
1435}
1436
1437static void flush_all(struct kmem_cache *s)
1438{
1439#ifdef CONFIG_SMP
1440 on_each_cpu(flush_cpu_slab, s, 1, 1);
1441#else
1442 unsigned long flags;
1443
1444 local_irq_save(flags);
1445 flush_cpu_slab(s);
1446 local_irq_restore(flags);
1447#endif
1448}
1449
dfb4f096
CL
1450/*
1451 * Check if the objects in a per cpu structure fit numa
1452 * locality expectations.
1453 */
1454static inline int node_match(struct kmem_cache_cpu *c, int node)
1455{
1456#ifdef CONFIG_NUMA
1457 if (node != -1 && c->node != node)
1458 return 0;
1459#endif
1460 return 1;
1461}
1462
81819f0f 1463/*
894b8788
CL
1464 * Slow path. The lockless freelist is empty or we need to perform
1465 * debugging duties.
1466 *
1467 * Interrupts are disabled.
81819f0f 1468 *
894b8788
CL
1469 * Processing is still very fast if new objects have been freed to the
1470 * regular freelist. In that case we simply take over the regular freelist
1471 * as the lockless freelist and zap the regular freelist.
81819f0f 1472 *
894b8788
CL
1473 * If that is not working then we fall back to the partial lists. We take the
1474 * first element of the freelist as the object to allocate now and move the
1475 * rest of the freelist to the lockless freelist.
81819f0f 1476 *
894b8788
CL
1477 * And if we were unable to get a new slab from the partial slab lists then
1478 * we need to allocate a new slab. This is slowest path since we may sleep.
81819f0f 1479 */
894b8788 1480static void *__slab_alloc(struct kmem_cache *s,
dfb4f096 1481 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
81819f0f 1482{
81819f0f 1483 void **object;
dfb4f096 1484 struct page *new;
81819f0f 1485
dfb4f096 1486 if (!c->page)
81819f0f
CL
1487 goto new_slab;
1488
dfb4f096
CL
1489 slab_lock(c->page);
1490 if (unlikely(!node_match(c, node)))
81819f0f 1491 goto another_slab;
894b8788 1492load_freelist:
dfb4f096 1493 object = c->page->freelist;
81819f0f
CL
1494 if (unlikely(!object))
1495 goto another_slab;
dfb4f096 1496 if (unlikely(SlabDebug(c->page)))
81819f0f
CL
1497 goto debug;
1498
dfb4f096
CL
1499 object = c->page->freelist;
1500 c->freelist = object[c->page->offset];
1501 c->page->inuse = s->objects;
1502 c->page->freelist = NULL;
1503 c->node = page_to_nid(c->page);
1504 slab_unlock(c->page);
81819f0f
CL
1505 return object;
1506
1507another_slab:
dfb4f096 1508 deactivate_slab(s, c);
81819f0f
CL
1509
1510new_slab:
dfb4f096
CL
1511 new = get_partial(s, gfpflags, node);
1512 if (new) {
1513 c->page = new;
894b8788 1514 goto load_freelist;
81819f0f
CL
1515 }
1516
dfb4f096
CL
1517 new = new_slab(s, gfpflags, node);
1518 if (new) {
1519 c = get_cpu_slab(s, smp_processor_id());
1520 if (c->page) {
81819f0f 1521 /*
672bba3a
CL
1522 * Someone else populated the cpu_slab while we
1523 * enabled interrupts, or we have gotten scheduled
1524 * on another cpu. The page may not be on the
1525 * requested node even if __GFP_THISNODE was
1526 * specified. So we need to recheck.
81819f0f 1527 */
dfb4f096 1528 if (node_match(c, node)) {
81819f0f
CL
1529 /*
1530 * Current cpuslab is acceptable and we
1531 * want the current one since its cache hot
1532 */
dfb4f096
CL
1533 discard_slab(s, new);
1534 slab_lock(c->page);
894b8788 1535 goto load_freelist;
81819f0f 1536 }
672bba3a 1537 /* New slab does not fit our expectations */
dfb4f096 1538 flush_slab(s, c);
81819f0f 1539 }
dfb4f096
CL
1540 slab_lock(new);
1541 SetSlabFrozen(new);
1542 c->page = new;
4b6f0750 1543 goto load_freelist;
81819f0f 1544 }
81819f0f
CL
1545 return NULL;
1546debug:
dfb4f096
CL
1547 object = c->page->freelist;
1548 if (!alloc_debug_processing(s, c->page, object, addr))
81819f0f 1549 goto another_slab;
894b8788 1550
dfb4f096
CL
1551 c->page->inuse++;
1552 c->page->freelist = object[c->page->offset];
1553 slab_unlock(c->page);
894b8788
CL
1554 return object;
1555}
1556
1557/*
1558 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1559 * have the fastpath folded into their functions. So no function call
1560 * overhead for requests that can be satisfied on the fastpath.
1561 *
1562 * The fastpath works by first checking if the lockless freelist can be used.
1563 * If not then __slab_alloc is called for slow processing.
1564 *
1565 * Otherwise we can simply pick the next object from the lockless free list.
1566 */
1567static void __always_inline *slab_alloc(struct kmem_cache *s,
ce15fea8 1568 gfp_t gfpflags, int node, void *addr)
894b8788 1569{
894b8788
CL
1570 void **object;
1571 unsigned long flags;
dfb4f096 1572 struct kmem_cache_cpu *c;
894b8788
CL
1573
1574 local_irq_save(flags);
dfb4f096
CL
1575 c = get_cpu_slab(s, smp_processor_id());
1576 if (unlikely(!c->page || !c->freelist ||
1577 !node_match(c, node)))
894b8788 1578
dfb4f096 1579 object = __slab_alloc(s, gfpflags, node, addr, c);
894b8788
CL
1580
1581 else {
dfb4f096
CL
1582 object = c->freelist;
1583 c->freelist = object[c->page->offset];
894b8788
CL
1584 }
1585 local_irq_restore(flags);
d07dbea4
CL
1586
1587 if (unlikely((gfpflags & __GFP_ZERO) && object))
ce15fea8 1588 memset(object, 0, s->objsize);
d07dbea4 1589
894b8788 1590 return object;
81819f0f
CL
1591}
1592
1593void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1594{
ce15fea8 1595 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
81819f0f
CL
1596}
1597EXPORT_SYMBOL(kmem_cache_alloc);
1598
1599#ifdef CONFIG_NUMA
1600void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1601{
ce15fea8 1602 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
81819f0f
CL
1603}
1604EXPORT_SYMBOL(kmem_cache_alloc_node);
1605#endif
1606
1607/*
894b8788
CL
1608 * Slow patch handling. This may still be called frequently since objects
1609 * have a longer lifetime than the cpu slabs in most processing loads.
81819f0f 1610 *
894b8788
CL
1611 * So we still attempt to reduce cache line usage. Just take the slab
1612 * lock and free the item. If there is no additional partial page
1613 * handling required then we can return immediately.
81819f0f 1614 */
894b8788 1615static void __slab_free(struct kmem_cache *s, struct page *page,
77c5e2d0 1616 void *x, void *addr)
81819f0f
CL
1617{
1618 void *prior;
1619 void **object = (void *)x;
81819f0f 1620
81819f0f
CL
1621 slab_lock(page);
1622
35e5d7ee 1623 if (unlikely(SlabDebug(page)))
81819f0f
CL
1624 goto debug;
1625checks_ok:
1626 prior = object[page->offset] = page->freelist;
1627 page->freelist = object;
1628 page->inuse--;
1629
4b6f0750 1630 if (unlikely(SlabFrozen(page)))
81819f0f
CL
1631 goto out_unlock;
1632
1633 if (unlikely(!page->inuse))
1634 goto slab_empty;
1635
1636 /*
1637 * Objects left in the slab. If it
1638 * was not on the partial list before
1639 * then add it.
1640 */
1641 if (unlikely(!prior))
e95eed57 1642 add_partial(get_node(s, page_to_nid(page)), page);
81819f0f
CL
1643
1644out_unlock:
1645 slab_unlock(page);
81819f0f
CL
1646 return;
1647
1648slab_empty:
1649 if (prior)
1650 /*
672bba3a 1651 * Slab still on the partial list.
81819f0f
CL
1652 */
1653 remove_partial(s, page);
1654
1655 slab_unlock(page);
1656 discard_slab(s, page);
81819f0f
CL
1657 return;
1658
1659debug:
3ec09742 1660 if (!free_debug_processing(s, page, x, addr))
77c5e2d0 1661 goto out_unlock;
77c5e2d0 1662 goto checks_ok;
81819f0f
CL
1663}
1664
894b8788
CL
1665/*
1666 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1667 * can perform fastpath freeing without additional function calls.
1668 *
1669 * The fastpath is only possible if we are freeing to the current cpu slab
1670 * of this processor. This typically the case if we have just allocated
1671 * the item before.
1672 *
1673 * If fastpath is not possible then fall back to __slab_free where we deal
1674 * with all sorts of special processing.
1675 */
1676static void __always_inline slab_free(struct kmem_cache *s,
1677 struct page *page, void *x, void *addr)
1678{
1679 void **object = (void *)x;
1680 unsigned long flags;
dfb4f096 1681 struct kmem_cache_cpu *c;
894b8788
CL
1682
1683 local_irq_save(flags);
02febdf7 1684 debug_check_no_locks_freed(object, s->objsize);
dfb4f096
CL
1685 c = get_cpu_slab(s, smp_processor_id());
1686 if (likely(page == c->page && !SlabDebug(page))) {
1687 object[page->offset] = c->freelist;
1688 c->freelist = object;
894b8788
CL
1689 } else
1690 __slab_free(s, page, x, addr);
1691
1692 local_irq_restore(flags);
1693}
1694
81819f0f
CL
1695void kmem_cache_free(struct kmem_cache *s, void *x)
1696{
77c5e2d0 1697 struct page *page;
81819f0f 1698
b49af68f 1699 page = virt_to_head_page(x);
81819f0f 1700
77c5e2d0 1701 slab_free(s, page, x, __builtin_return_address(0));
81819f0f
CL
1702}
1703EXPORT_SYMBOL(kmem_cache_free);
1704
1705/* Figure out on which slab object the object resides */
1706static struct page *get_object_page(const void *x)
1707{
b49af68f 1708 struct page *page = virt_to_head_page(x);
81819f0f
CL
1709
1710 if (!PageSlab(page))
1711 return NULL;
1712
1713 return page;
1714}
1715
1716/*
672bba3a
CL
1717 * Object placement in a slab is made very easy because we always start at
1718 * offset 0. If we tune the size of the object to the alignment then we can
1719 * get the required alignment by putting one properly sized object after
1720 * another.
81819f0f
CL
1721 *
1722 * Notice that the allocation order determines the sizes of the per cpu
1723 * caches. Each processor has always one slab available for allocations.
1724 * Increasing the allocation order reduces the number of times that slabs
672bba3a 1725 * must be moved on and off the partial lists and is therefore a factor in
81819f0f 1726 * locking overhead.
81819f0f
CL
1727 */
1728
1729/*
1730 * Mininum / Maximum order of slab pages. This influences locking overhead
1731 * and slab fragmentation. A higher order reduces the number of partial slabs
1732 * and increases the number of allocations possible without having to
1733 * take the list_lock.
1734 */
1735static int slub_min_order;
1736static int slub_max_order = DEFAULT_MAX_ORDER;
81819f0f
CL
1737static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1738
1739/*
1740 * Merge control. If this is set then no merging of slab caches will occur.
672bba3a 1741 * (Could be removed. This was introduced to pacify the merge skeptics.)
81819f0f
CL
1742 */
1743static int slub_nomerge;
1744
81819f0f
CL
1745/*
1746 * Calculate the order of allocation given an slab object size.
1747 *
672bba3a
CL
1748 * The order of allocation has significant impact on performance and other
1749 * system components. Generally order 0 allocations should be preferred since
1750 * order 0 does not cause fragmentation in the page allocator. Larger objects
1751 * be problematic to put into order 0 slabs because there may be too much
1752 * unused space left. We go to a higher order if more than 1/8th of the slab
1753 * would be wasted.
1754 *
1755 * In order to reach satisfactory performance we must ensure that a minimum
1756 * number of objects is in one slab. Otherwise we may generate too much
1757 * activity on the partial lists which requires taking the list_lock. This is
1758 * less a concern for large slabs though which are rarely used.
81819f0f 1759 *
672bba3a
CL
1760 * slub_max_order specifies the order where we begin to stop considering the
1761 * number of objects in a slab as critical. If we reach slub_max_order then
1762 * we try to keep the page order as low as possible. So we accept more waste
1763 * of space in favor of a small page order.
81819f0f 1764 *
672bba3a
CL
1765 * Higher order allocations also allow the placement of more objects in a
1766 * slab and thereby reduce object handling overhead. If the user has
1767 * requested a higher mininum order then we start with that one instead of
1768 * the smallest order which will fit the object.
81819f0f 1769 */
5e6d444e
CL
1770static inline int slab_order(int size, int min_objects,
1771 int max_order, int fract_leftover)
81819f0f
CL
1772{
1773 int order;
1774 int rem;
6300ea75 1775 int min_order = slub_min_order;
81819f0f 1776
6300ea75
CL
1777 /*
1778 * If we would create too many object per slab then reduce
1779 * the slab order even if it goes below slub_min_order.
1780 */
1781 while (min_order > 0 &&
1782 (PAGE_SIZE << min_order) >= MAX_OBJECTS_PER_SLAB * size)
1783 min_order--;
1784
1785 for (order = max(min_order,
5e6d444e
CL
1786 fls(min_objects * size - 1) - PAGE_SHIFT);
1787 order <= max_order; order++) {
81819f0f 1788
5e6d444e 1789 unsigned long slab_size = PAGE_SIZE << order;
81819f0f 1790
5e6d444e 1791 if (slab_size < min_objects * size)
81819f0f
CL
1792 continue;
1793
1794 rem = slab_size % size;
1795
5e6d444e 1796 if (rem <= slab_size / fract_leftover)
81819f0f
CL
1797 break;
1798
6300ea75
CL
1799 /* If the next size is too high then exit now */
1800 if (slab_size * 2 >= MAX_OBJECTS_PER_SLAB * size)
1801 break;
81819f0f 1802 }
672bba3a 1803
81819f0f
CL
1804 return order;
1805}
1806
5e6d444e
CL
1807static inline int calculate_order(int size)
1808{
1809 int order;
1810 int min_objects;
1811 int fraction;
1812
1813 /*
1814 * Attempt to find best configuration for a slab. This
1815 * works by first attempting to generate a layout with
1816 * the best configuration and backing off gradually.
1817 *
1818 * First we reduce the acceptable waste in a slab. Then
1819 * we reduce the minimum objects required in a slab.
1820 */
1821 min_objects = slub_min_objects;
1822 while (min_objects > 1) {
1823 fraction = 8;
1824 while (fraction >= 4) {
1825 order = slab_order(size, min_objects,
1826 slub_max_order, fraction);
1827 if (order <= slub_max_order)
1828 return order;
1829 fraction /= 2;
1830 }
1831 min_objects /= 2;
1832 }
1833
1834 /*
1835 * We were unable to place multiple objects in a slab. Now
1836 * lets see if we can place a single object there.
1837 */
1838 order = slab_order(size, 1, slub_max_order, 1);
1839 if (order <= slub_max_order)
1840 return order;
1841
1842 /*
1843 * Doh this slab cannot be placed using slub_max_order.
1844 */
1845 order = slab_order(size, 1, MAX_ORDER, 1);
1846 if (order <= MAX_ORDER)
1847 return order;
1848 return -ENOSYS;
1849}
1850
81819f0f 1851/*
672bba3a 1852 * Figure out what the alignment of the objects will be.
81819f0f
CL
1853 */
1854static unsigned long calculate_alignment(unsigned long flags,
1855 unsigned long align, unsigned long size)
1856{
1857 /*
1858 * If the user wants hardware cache aligned objects then
1859 * follow that suggestion if the object is sufficiently
1860 * large.
1861 *
1862 * The hardware cache alignment cannot override the
1863 * specified alignment though. If that is greater
1864 * then use it.
1865 */
5af60839 1866 if ((flags & SLAB_HWCACHE_ALIGN) &&
65c02d4c
CL
1867 size > cache_line_size() / 2)
1868 return max_t(unsigned long, align, cache_line_size());
81819f0f
CL
1869
1870 if (align < ARCH_SLAB_MINALIGN)
1871 return ARCH_SLAB_MINALIGN;
1872
1873 return ALIGN(align, sizeof(void *));
1874}
1875
dfb4f096
CL
1876static void init_kmem_cache_cpu(struct kmem_cache *s,
1877 struct kmem_cache_cpu *c)
1878{
1879 c->page = NULL;
1880 c->freelist = NULL;
1881 c->node = 0;
1882}
1883
1884static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
1885{
1886 int cpu;
1887
1888 for_each_possible_cpu(cpu)
1889 init_kmem_cache_cpu(s, get_cpu_slab(s, cpu));
1890
1891 return 1;
1892}
1893
81819f0f
CL
1894static void init_kmem_cache_node(struct kmem_cache_node *n)
1895{
1896 n->nr_partial = 0;
1897 atomic_long_set(&n->nr_slabs, 0);
1898 spin_lock_init(&n->list_lock);
1899 INIT_LIST_HEAD(&n->partial);
8ab1372f 1900#ifdef CONFIG_SLUB_DEBUG
643b1138 1901 INIT_LIST_HEAD(&n->full);
8ab1372f 1902#endif
81819f0f
CL
1903}
1904
1905#ifdef CONFIG_NUMA
1906/*
1907 * No kmalloc_node yet so do it by hand. We know that this is the first
1908 * slab on the node for this slabcache. There are no concurrent accesses
1909 * possible.
1910 *
1911 * Note that this function only works on the kmalloc_node_cache
1912 * when allocating for the kmalloc_node_cache.
1913 */
1cd7daa5
AB
1914static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
1915 int node)
81819f0f
CL
1916{
1917 struct page *page;
1918 struct kmem_cache_node *n;
1919
1920 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1921
a2f92ee7 1922 page = new_slab(kmalloc_caches, gfpflags, node);
81819f0f
CL
1923
1924 BUG_ON(!page);
a2f92ee7
CL
1925 if (page_to_nid(page) != node) {
1926 printk(KERN_ERR "SLUB: Unable to allocate memory from "
1927 "node %d\n", node);
1928 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
1929 "in order to be able to continue\n");
1930 }
1931
81819f0f
CL
1932 n = page->freelist;
1933 BUG_ON(!n);
1934 page->freelist = get_freepointer(kmalloc_caches, n);
1935 page->inuse++;
1936 kmalloc_caches->node[node] = n;
8ab1372f 1937#ifdef CONFIG_SLUB_DEBUG
d45f39cb
CL
1938 init_object(kmalloc_caches, n, 1);
1939 init_tracking(kmalloc_caches, n);
8ab1372f 1940#endif
81819f0f
CL
1941 init_kmem_cache_node(n);
1942 atomic_long_inc(&n->nr_slabs);
e95eed57 1943 add_partial(n, page);
dbc55faa
CL
1944
1945 /*
1946 * new_slab() disables interupts. If we do not reenable interrupts here
1947 * then bootup would continue with interrupts disabled.
1948 */
1949 local_irq_enable();
81819f0f
CL
1950 return n;
1951}
1952
1953static void free_kmem_cache_nodes(struct kmem_cache *s)
1954{
1955 int node;
1956
f64dc58c 1957 for_each_node_state(node, N_NORMAL_MEMORY) {
81819f0f
CL
1958 struct kmem_cache_node *n = s->node[node];
1959 if (n && n != &s->local_node)
1960 kmem_cache_free(kmalloc_caches, n);
1961 s->node[node] = NULL;
1962 }
1963}
1964
1965static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1966{
1967 int node;
1968 int local_node;
1969
1970 if (slab_state >= UP)
1971 local_node = page_to_nid(virt_to_page(s));
1972 else
1973 local_node = 0;
1974
f64dc58c 1975 for_each_node_state(node, N_NORMAL_MEMORY) {
81819f0f
CL
1976 struct kmem_cache_node *n;
1977
1978 if (local_node == node)
1979 n = &s->local_node;
1980 else {
1981 if (slab_state == DOWN) {
1982 n = early_kmem_cache_node_alloc(gfpflags,
1983 node);
1984 continue;
1985 }
1986 n = kmem_cache_alloc_node(kmalloc_caches,
1987 gfpflags, node);
1988
1989 if (!n) {
1990 free_kmem_cache_nodes(s);
1991 return 0;
1992 }
1993
1994 }
1995 s->node[node] = n;
1996 init_kmem_cache_node(n);
1997 }
1998 return 1;
1999}
2000#else
2001static void free_kmem_cache_nodes(struct kmem_cache *s)
2002{
2003}
2004
2005static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2006{
2007 init_kmem_cache_node(&s->local_node);
2008 return 1;
2009}
2010#endif
2011
2012/*
2013 * calculate_sizes() determines the order and the distribution of data within
2014 * a slab object.
2015 */
2016static int calculate_sizes(struct kmem_cache *s)
2017{
2018 unsigned long flags = s->flags;
2019 unsigned long size = s->objsize;
2020 unsigned long align = s->align;
2021
2022 /*
2023 * Determine if we can poison the object itself. If the user of
2024 * the slab may touch the object after free or before allocation
2025 * then we should never poison the object itself.
2026 */
2027 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
c59def9f 2028 !s->ctor)
81819f0f
CL
2029 s->flags |= __OBJECT_POISON;
2030 else
2031 s->flags &= ~__OBJECT_POISON;
2032
2033 /*
2034 * Round up object size to the next word boundary. We can only
2035 * place the free pointer at word boundaries and this determines
2036 * the possible location of the free pointer.
2037 */
2038 size = ALIGN(size, sizeof(void *));
2039
41ecc55b 2040#ifdef CONFIG_SLUB_DEBUG
81819f0f 2041 /*
672bba3a 2042 * If we are Redzoning then check if there is some space between the
81819f0f 2043 * end of the object and the free pointer. If not then add an
672bba3a 2044 * additional word to have some bytes to store Redzone information.
81819f0f
CL
2045 */
2046 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2047 size += sizeof(void *);
41ecc55b 2048#endif
81819f0f
CL
2049
2050 /*
672bba3a
CL
2051 * With that we have determined the number of bytes in actual use
2052 * by the object. This is the potential offset to the free pointer.
81819f0f
CL
2053 */
2054 s->inuse = size;
2055
2056 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
c59def9f 2057 s->ctor)) {
81819f0f
CL
2058 /*
2059 * Relocate free pointer after the object if it is not
2060 * permitted to overwrite the first word of the object on
2061 * kmem_cache_free.
2062 *
2063 * This is the case if we do RCU, have a constructor or
2064 * destructor or are poisoning the objects.
2065 */
2066 s->offset = size;
2067 size += sizeof(void *);
2068 }
2069
c12b3c62 2070#ifdef CONFIG_SLUB_DEBUG
81819f0f
CL
2071 if (flags & SLAB_STORE_USER)
2072 /*
2073 * Need to store information about allocs and frees after
2074 * the object.
2075 */
2076 size += 2 * sizeof(struct track);
2077
be7b3fbc 2078 if (flags & SLAB_RED_ZONE)
81819f0f
CL
2079 /*
2080 * Add some empty padding so that we can catch
2081 * overwrites from earlier objects rather than let
2082 * tracking information or the free pointer be
2083 * corrupted if an user writes before the start
2084 * of the object.
2085 */
2086 size += sizeof(void *);
41ecc55b 2087#endif
672bba3a 2088
81819f0f
CL
2089 /*
2090 * Determine the alignment based on various parameters that the
65c02d4c
CL
2091 * user specified and the dynamic determination of cache line size
2092 * on bootup.
81819f0f
CL
2093 */
2094 align = calculate_alignment(flags, align, s->objsize);
2095
2096 /*
2097 * SLUB stores one object immediately after another beginning from
2098 * offset 0. In order to align the objects we have to simply size
2099 * each object to conform to the alignment.
2100 */
2101 size = ALIGN(size, align);
2102 s->size = size;
2103
2104 s->order = calculate_order(size);
2105 if (s->order < 0)
2106 return 0;
2107
2108 /*
2109 * Determine the number of objects per slab
2110 */
2111 s->objects = (PAGE_SIZE << s->order) / size;
2112
2113 /*
2114 * Verify that the number of objects is within permitted limits.
2115 * The page->inuse field is only 16 bit wide! So we cannot have
2116 * more than 64k objects per slab.
2117 */
6300ea75 2118 if (!s->objects || s->objects > MAX_OBJECTS_PER_SLAB)
81819f0f
CL
2119 return 0;
2120 return 1;
2121
2122}
2123
81819f0f
CL
2124static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2125 const char *name, size_t size,
2126 size_t align, unsigned long flags,
c59def9f 2127 void (*ctor)(void *, struct kmem_cache *, unsigned long))
81819f0f
CL
2128{
2129 memset(s, 0, kmem_size);
2130 s->name = name;
2131 s->ctor = ctor;
81819f0f 2132 s->objsize = size;
81819f0f 2133 s->align = align;
ba0268a8 2134 s->flags = kmem_cache_flags(size, flags, name, ctor);
81819f0f
CL
2135
2136 if (!calculate_sizes(s))
2137 goto error;
2138
2139 s->refcount = 1;
2140#ifdef CONFIG_NUMA
2141 s->defrag_ratio = 100;
2142#endif
dfb4f096
CL
2143 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2144 goto error;
81819f0f 2145
dfb4f096 2146 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
81819f0f
CL
2147 return 1;
2148error:
2149 if (flags & SLAB_PANIC)
2150 panic("Cannot create slab %s size=%lu realsize=%u "
2151 "order=%u offset=%u flags=%lx\n",
2152 s->name, (unsigned long)size, s->size, s->order,
2153 s->offset, flags);
2154 return 0;
2155}
81819f0f
CL
2156
2157/*
2158 * Check if a given pointer is valid
2159 */
2160int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2161{
2162 struct page * page;
81819f0f
CL
2163
2164 page = get_object_page(object);
2165
2166 if (!page || s != page->slab)
2167 /* No slab or wrong slab */
2168 return 0;
2169
abcd08a6 2170 if (!check_valid_pointer(s, page, object))
81819f0f
CL
2171 return 0;
2172
2173 /*
2174 * We could also check if the object is on the slabs freelist.
2175 * But this would be too expensive and it seems that the main
2176 * purpose of kmem_ptr_valid is to check if the object belongs
2177 * to a certain slab.
2178 */
2179 return 1;
2180}
2181EXPORT_SYMBOL(kmem_ptr_validate);
2182
2183/*
2184 * Determine the size of a slab object
2185 */
2186unsigned int kmem_cache_size(struct kmem_cache *s)
2187{
2188 return s->objsize;
2189}
2190EXPORT_SYMBOL(kmem_cache_size);
2191
2192const char *kmem_cache_name(struct kmem_cache *s)
2193{
2194 return s->name;
2195}
2196EXPORT_SYMBOL(kmem_cache_name);
2197
2198/*
672bba3a
CL
2199 * Attempt to free all slabs on a node. Return the number of slabs we
2200 * were unable to free.
81819f0f
CL
2201 */
2202static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2203 struct list_head *list)
2204{
2205 int slabs_inuse = 0;
2206 unsigned long flags;
2207 struct page *page, *h;
2208
2209 spin_lock_irqsave(&n->list_lock, flags);
2210 list_for_each_entry_safe(page, h, list, lru)
2211 if (!page->inuse) {
2212 list_del(&page->lru);
2213 discard_slab(s, page);
2214 } else
2215 slabs_inuse++;
2216 spin_unlock_irqrestore(&n->list_lock, flags);
2217 return slabs_inuse;
2218}
2219
2220/*
672bba3a 2221 * Release all resources used by a slab cache.
81819f0f 2222 */
0c710013 2223static inline int kmem_cache_close(struct kmem_cache *s)
81819f0f
CL
2224{
2225 int node;
2226
2227 flush_all(s);
2228
2229 /* Attempt to free all objects */
f64dc58c 2230 for_each_node_state(node, N_NORMAL_MEMORY) {
81819f0f
CL
2231 struct kmem_cache_node *n = get_node(s, node);
2232
2086d26a 2233 n->nr_partial -= free_list(s, n, &n->partial);
81819f0f
CL
2234 if (atomic_long_read(&n->nr_slabs))
2235 return 1;
2236 }
2237 free_kmem_cache_nodes(s);
2238 return 0;
2239}
2240
2241/*
2242 * Close a cache and release the kmem_cache structure
2243 * (must be used for caches created using kmem_cache_create)
2244 */
2245void kmem_cache_destroy(struct kmem_cache *s)
2246{
2247 down_write(&slub_lock);
2248 s->refcount--;
2249 if (!s->refcount) {
2250 list_del(&s->list);
a0e1d1be 2251 up_write(&slub_lock);
81819f0f
CL
2252 if (kmem_cache_close(s))
2253 WARN_ON(1);
2254 sysfs_slab_remove(s);
2255 kfree(s);
a0e1d1be
CL
2256 } else
2257 up_write(&slub_lock);
81819f0f
CL
2258}
2259EXPORT_SYMBOL(kmem_cache_destroy);
2260
2261/********************************************************************
2262 * Kmalloc subsystem
2263 *******************************************************************/
2264
aadb4bc4 2265struct kmem_cache kmalloc_caches[PAGE_SHIFT] __cacheline_aligned;
81819f0f
CL
2266EXPORT_SYMBOL(kmalloc_caches);
2267
2268#ifdef CONFIG_ZONE_DMA
aadb4bc4 2269static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT];
81819f0f
CL
2270#endif
2271
2272static int __init setup_slub_min_order(char *str)
2273{
2274 get_option (&str, &slub_min_order);
2275
2276 return 1;
2277}
2278
2279__setup("slub_min_order=", setup_slub_min_order);
2280
2281static int __init setup_slub_max_order(char *str)
2282{
2283 get_option (&str, &slub_max_order);
2284
2285 return 1;
2286}
2287
2288__setup("slub_max_order=", setup_slub_max_order);
2289
2290static int __init setup_slub_min_objects(char *str)
2291{
2292 get_option (&str, &slub_min_objects);
2293
2294 return 1;
2295}
2296
2297__setup("slub_min_objects=", setup_slub_min_objects);
2298
2299static int __init setup_slub_nomerge(char *str)
2300{
2301 slub_nomerge = 1;
2302 return 1;
2303}
2304
2305__setup("slub_nomerge", setup_slub_nomerge);
2306
81819f0f
CL
2307static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2308 const char *name, int size, gfp_t gfp_flags)
2309{
2310 unsigned int flags = 0;
2311
2312 if (gfp_flags & SLUB_DMA)
2313 flags = SLAB_CACHE_DMA;
2314
2315 down_write(&slub_lock);
2316 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
c59def9f 2317 flags, NULL))
81819f0f
CL
2318 goto panic;
2319
2320 list_add(&s->list, &slab_caches);
2321 up_write(&slub_lock);
2322 if (sysfs_slab_add(s))
2323 goto panic;
2324 return s;
2325
2326panic:
2327 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2328}
2329
2e443fd0 2330#ifdef CONFIG_ZONE_DMA
1ceef402
CL
2331
2332static void sysfs_add_func(struct work_struct *w)
2333{
2334 struct kmem_cache *s;
2335
2336 down_write(&slub_lock);
2337 list_for_each_entry(s, &slab_caches, list) {
2338 if (s->flags & __SYSFS_ADD_DEFERRED) {
2339 s->flags &= ~__SYSFS_ADD_DEFERRED;
2340 sysfs_slab_add(s);
2341 }
2342 }
2343 up_write(&slub_lock);
2344}
2345
2346static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2347
2e443fd0
CL
2348static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2349{
2350 struct kmem_cache *s;
2e443fd0
CL
2351 char *text;
2352 size_t realsize;
2353
2354 s = kmalloc_caches_dma[index];
2355 if (s)
2356 return s;
2357
2358 /* Dynamically create dma cache */
1ceef402
CL
2359 if (flags & __GFP_WAIT)
2360 down_write(&slub_lock);
2361 else {
2362 if (!down_write_trylock(&slub_lock))
2363 goto out;
2364 }
2365
2366 if (kmalloc_caches_dma[index])
2367 goto unlock_out;
2e443fd0 2368
7b55f620 2369 realsize = kmalloc_caches[index].objsize;
1ceef402
CL
2370 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d", (unsigned int)realsize),
2371 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2372
2373 if (!s || !text || !kmem_cache_open(s, flags, text,
2374 realsize, ARCH_KMALLOC_MINALIGN,
2375 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2376 kfree(s);
2377 kfree(text);
2378 goto unlock_out;
dfce8648 2379 }
1ceef402
CL
2380
2381 list_add(&s->list, &slab_caches);
2382 kmalloc_caches_dma[index] = s;
2383
2384 schedule_work(&sysfs_add_work);
2385
2386unlock_out:
dfce8648 2387 up_write(&slub_lock);
1ceef402 2388out:
dfce8648 2389 return kmalloc_caches_dma[index];
2e443fd0
CL
2390}
2391#endif
2392
f1b26339
CL
2393/*
2394 * Conversion table for small slabs sizes / 8 to the index in the
2395 * kmalloc array. This is necessary for slabs < 192 since we have non power
2396 * of two cache sizes there. The size of larger slabs can be determined using
2397 * fls.
2398 */
2399static s8 size_index[24] = {
2400 3, /* 8 */
2401 4, /* 16 */
2402 5, /* 24 */
2403 5, /* 32 */
2404 6, /* 40 */
2405 6, /* 48 */
2406 6, /* 56 */
2407 6, /* 64 */
2408 1, /* 72 */
2409 1, /* 80 */
2410 1, /* 88 */
2411 1, /* 96 */
2412 7, /* 104 */
2413 7, /* 112 */
2414 7, /* 120 */
2415 7, /* 128 */
2416 2, /* 136 */
2417 2, /* 144 */
2418 2, /* 152 */
2419 2, /* 160 */
2420 2, /* 168 */
2421 2, /* 176 */
2422 2, /* 184 */
2423 2 /* 192 */
2424};
2425
81819f0f
CL
2426static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2427{
f1b26339 2428 int index;
81819f0f 2429
f1b26339
CL
2430 if (size <= 192) {
2431 if (!size)
2432 return ZERO_SIZE_PTR;
81819f0f 2433
f1b26339 2434 index = size_index[(size - 1) / 8];
aadb4bc4 2435 } else
f1b26339 2436 index = fls(size - 1);
81819f0f
CL
2437
2438#ifdef CONFIG_ZONE_DMA
f1b26339 2439 if (unlikely((flags & SLUB_DMA)))
2e443fd0 2440 return dma_kmalloc_cache(index, flags);
f1b26339 2441
81819f0f
CL
2442#endif
2443 return &kmalloc_caches[index];
2444}
2445
2446void *__kmalloc(size_t size, gfp_t flags)
2447{
aadb4bc4 2448 struct kmem_cache *s;
81819f0f 2449
aadb4bc4
CL
2450 if (unlikely(size > PAGE_SIZE / 2))
2451 return (void *)__get_free_pages(flags | __GFP_COMP,
2452 get_order(size));
2453
2454 s = get_slab(size, flags);
2455
2456 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913
CL
2457 return s;
2458
ce15fea8 2459 return slab_alloc(s, flags, -1, __builtin_return_address(0));
81819f0f
CL
2460}
2461EXPORT_SYMBOL(__kmalloc);
2462
2463#ifdef CONFIG_NUMA
2464void *__kmalloc_node(size_t size, gfp_t flags, int node)
2465{
aadb4bc4 2466 struct kmem_cache *s;
81819f0f 2467
aadb4bc4
CL
2468 if (unlikely(size > PAGE_SIZE / 2))
2469 return (void *)__get_free_pages(flags | __GFP_COMP,
2470 get_order(size));
2471
2472 s = get_slab(size, flags);
2473
2474 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913
CL
2475 return s;
2476
ce15fea8 2477 return slab_alloc(s, flags, node, __builtin_return_address(0));
81819f0f
CL
2478}
2479EXPORT_SYMBOL(__kmalloc_node);
2480#endif
2481
2482size_t ksize(const void *object)
2483{
272c1d21 2484 struct page *page;
81819f0f
CL
2485 struct kmem_cache *s;
2486
ef8b4520
CL
2487 BUG_ON(!object);
2488 if (unlikely(object == ZERO_SIZE_PTR))
272c1d21
CL
2489 return 0;
2490
2491 page = get_object_page(object);
81819f0f
CL
2492 BUG_ON(!page);
2493 s = page->slab;
2494 BUG_ON(!s);
2495
2496 /*
2497 * Debugging requires use of the padding between object
2498 * and whatever may come after it.
2499 */
2500 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2501 return s->objsize;
2502
2503 /*
2504 * If we have the need to store the freelist pointer
2505 * back there or track user information then we can
2506 * only use the space before that information.
2507 */
2508 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2509 return s->inuse;
2510
2511 /*
2512 * Else we can use all the padding etc for the allocation
2513 */
2514 return s->size;
2515}
2516EXPORT_SYMBOL(ksize);
2517
2518void kfree(const void *x)
2519{
81819f0f
CL
2520 struct page *page;
2521
2408c550 2522 if (unlikely(ZERO_OR_NULL_PTR(x)))
81819f0f
CL
2523 return;
2524
b49af68f 2525 page = virt_to_head_page(x);
aadb4bc4
CL
2526 if (unlikely(!PageSlab(page))) {
2527 put_page(page);
2528 return;
2529 }
2530 slab_free(page->slab, page, (void *)x, __builtin_return_address(0));
81819f0f
CL
2531}
2532EXPORT_SYMBOL(kfree);
2533
2086d26a 2534/*
672bba3a
CL
2535 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2536 * the remaining slabs by the number of items in use. The slabs with the
2537 * most items in use come first. New allocations will then fill those up
2538 * and thus they can be removed from the partial lists.
2539 *
2540 * The slabs with the least items are placed last. This results in them
2541 * being allocated from last increasing the chance that the last objects
2542 * are freed in them.
2086d26a
CL
2543 */
2544int kmem_cache_shrink(struct kmem_cache *s)
2545{
2546 int node;
2547 int i;
2548 struct kmem_cache_node *n;
2549 struct page *page;
2550 struct page *t;
2551 struct list_head *slabs_by_inuse =
2552 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2553 unsigned long flags;
2554
2555 if (!slabs_by_inuse)
2556 return -ENOMEM;
2557
2558 flush_all(s);
f64dc58c 2559 for_each_node_state(node, N_NORMAL_MEMORY) {
2086d26a
CL
2560 n = get_node(s, node);
2561
2562 if (!n->nr_partial)
2563 continue;
2564
2565 for (i = 0; i < s->objects; i++)
2566 INIT_LIST_HEAD(slabs_by_inuse + i);
2567
2568 spin_lock_irqsave(&n->list_lock, flags);
2569
2570 /*
672bba3a 2571 * Build lists indexed by the items in use in each slab.
2086d26a 2572 *
672bba3a
CL
2573 * Note that concurrent frees may occur while we hold the
2574 * list_lock. page->inuse here is the upper limit.
2086d26a
CL
2575 */
2576 list_for_each_entry_safe(page, t, &n->partial, lru) {
2577 if (!page->inuse && slab_trylock(page)) {
2578 /*
2579 * Must hold slab lock here because slab_free
2580 * may have freed the last object and be
2581 * waiting to release the slab.
2582 */
2583 list_del(&page->lru);
2584 n->nr_partial--;
2585 slab_unlock(page);
2586 discard_slab(s, page);
2587 } else {
fcda3d89
CL
2588 list_move(&page->lru,
2589 slabs_by_inuse + page->inuse);
2086d26a
CL
2590 }
2591 }
2592
2086d26a 2593 /*
672bba3a
CL
2594 * Rebuild the partial list with the slabs filled up most
2595 * first and the least used slabs at the end.
2086d26a
CL
2596 */
2597 for (i = s->objects - 1; i >= 0; i--)
2598 list_splice(slabs_by_inuse + i, n->partial.prev);
2599
2086d26a
CL
2600 spin_unlock_irqrestore(&n->list_lock, flags);
2601 }
2602
2603 kfree(slabs_by_inuse);
2604 return 0;
2605}
2606EXPORT_SYMBOL(kmem_cache_shrink);
2607
81819f0f
CL
2608/********************************************************************
2609 * Basic setup of slabs
2610 *******************************************************************/
2611
2612void __init kmem_cache_init(void)
2613{
2614 int i;
4b356be0 2615 int caches = 0;
81819f0f
CL
2616
2617#ifdef CONFIG_NUMA
2618 /*
2619 * Must first have the slab cache available for the allocations of the
672bba3a 2620 * struct kmem_cache_node's. There is special bootstrap code in
81819f0f
CL
2621 * kmem_cache_open for slab_state == DOWN.
2622 */
2623 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2624 sizeof(struct kmem_cache_node), GFP_KERNEL);
8ffa6875 2625 kmalloc_caches[0].refcount = -1;
4b356be0 2626 caches++;
81819f0f
CL
2627#endif
2628
2629 /* Able to allocate the per node structures */
2630 slab_state = PARTIAL;
2631
2632 /* Caches that are not of the two-to-the-power-of size */
4b356be0
CL
2633 if (KMALLOC_MIN_SIZE <= 64) {
2634 create_kmalloc_cache(&kmalloc_caches[1],
81819f0f 2635 "kmalloc-96", 96, GFP_KERNEL);
4b356be0
CL
2636 caches++;
2637 }
2638 if (KMALLOC_MIN_SIZE <= 128) {
2639 create_kmalloc_cache(&kmalloc_caches[2],
81819f0f 2640 "kmalloc-192", 192, GFP_KERNEL);
4b356be0
CL
2641 caches++;
2642 }
81819f0f 2643
aadb4bc4 2644 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) {
81819f0f
CL
2645 create_kmalloc_cache(&kmalloc_caches[i],
2646 "kmalloc", 1 << i, GFP_KERNEL);
4b356be0
CL
2647 caches++;
2648 }
81819f0f 2649
f1b26339
CL
2650
2651 /*
2652 * Patch up the size_index table if we have strange large alignment
2653 * requirements for the kmalloc array. This is only the case for
2654 * mips it seems. The standard arches will not generate any code here.
2655 *
2656 * Largest permitted alignment is 256 bytes due to the way we
2657 * handle the index determination for the smaller caches.
2658 *
2659 * Make sure that nothing crazy happens if someone starts tinkering
2660 * around with ARCH_KMALLOC_MINALIGN
2661 */
2662 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
2663 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
2664
12ad6843 2665 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
f1b26339
CL
2666 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
2667
81819f0f
CL
2668 slab_state = UP;
2669
2670 /* Provide the correct kmalloc names now that the caches are up */
aadb4bc4 2671 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++)
81819f0f
CL
2672 kmalloc_caches[i]. name =
2673 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2674
2675#ifdef CONFIG_SMP
2676 register_cpu_notifier(&slab_notifier);
2677#endif
2678
bcf889f9 2679 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
dfb4f096 2680 nr_cpu_ids * sizeof(struct kmem_cache_cpu);
81819f0f
CL
2681
2682 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
4b356be0
CL
2683 " CPUs=%d, Nodes=%d\n",
2684 caches, cache_line_size(),
81819f0f
CL
2685 slub_min_order, slub_max_order, slub_min_objects,
2686 nr_cpu_ids, nr_node_ids);
2687}
2688
2689/*
2690 * Find a mergeable slab cache
2691 */
2692static int slab_unmergeable(struct kmem_cache *s)
2693{
2694 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2695 return 1;
2696
c59def9f 2697 if (s->ctor)
81819f0f
CL
2698 return 1;
2699
8ffa6875
CL
2700 /*
2701 * We may have set a slab to be unmergeable during bootstrap.
2702 */
2703 if (s->refcount < 0)
2704 return 1;
2705
81819f0f
CL
2706 return 0;
2707}
2708
2709static struct kmem_cache *find_mergeable(size_t size,
ba0268a8 2710 size_t align, unsigned long flags, const char *name,
c59def9f 2711 void (*ctor)(void *, struct kmem_cache *, unsigned long))
81819f0f 2712{
5b95a4ac 2713 struct kmem_cache *s;
81819f0f
CL
2714
2715 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2716 return NULL;
2717
c59def9f 2718 if (ctor)
81819f0f
CL
2719 return NULL;
2720
2721 size = ALIGN(size, sizeof(void *));
2722 align = calculate_alignment(flags, align, size);
2723 size = ALIGN(size, align);
ba0268a8 2724 flags = kmem_cache_flags(size, flags, name, NULL);
81819f0f 2725
5b95a4ac 2726 list_for_each_entry(s, &slab_caches, list) {
81819f0f
CL
2727 if (slab_unmergeable(s))
2728 continue;
2729
2730 if (size > s->size)
2731 continue;
2732
ba0268a8 2733 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
81819f0f
CL
2734 continue;
2735 /*
2736 * Check if alignment is compatible.
2737 * Courtesy of Adrian Drzewiecki
2738 */
2739 if ((s->size & ~(align -1)) != s->size)
2740 continue;
2741
2742 if (s->size - size >= sizeof(void *))
2743 continue;
2744
2745 return s;
2746 }
2747 return NULL;
2748}
2749
2750struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2751 size_t align, unsigned long flags,
20c2df83 2752 void (*ctor)(void *, struct kmem_cache *, unsigned long))
81819f0f
CL
2753{
2754 struct kmem_cache *s;
2755
2756 down_write(&slub_lock);
ba0268a8 2757 s = find_mergeable(size, align, flags, name, ctor);
81819f0f
CL
2758 if (s) {
2759 s->refcount++;
2760 /*
2761 * Adjust the object sizes so that we clear
2762 * the complete object on kzalloc.
2763 */
2764 s->objsize = max(s->objsize, (int)size);
2765 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
a0e1d1be 2766 up_write(&slub_lock);
81819f0f
CL
2767 if (sysfs_slab_alias(s, name))
2768 goto err;
a0e1d1be
CL
2769 return s;
2770 }
2771 s = kmalloc(kmem_size, GFP_KERNEL);
2772 if (s) {
2773 if (kmem_cache_open(s, GFP_KERNEL, name,
c59def9f 2774 size, align, flags, ctor)) {
81819f0f 2775 list_add(&s->list, &slab_caches);
a0e1d1be
CL
2776 up_write(&slub_lock);
2777 if (sysfs_slab_add(s))
2778 goto err;
2779 return s;
2780 }
2781 kfree(s);
81819f0f
CL
2782 }
2783 up_write(&slub_lock);
81819f0f
CL
2784
2785err:
81819f0f
CL
2786 if (flags & SLAB_PANIC)
2787 panic("Cannot create slabcache %s\n", name);
2788 else
2789 s = NULL;
2790 return s;
2791}
2792EXPORT_SYMBOL(kmem_cache_create);
2793
81819f0f 2794#ifdef CONFIG_SMP
81819f0f 2795/*
672bba3a
CL
2796 * Use the cpu notifier to insure that the cpu slabs are flushed when
2797 * necessary.
81819f0f
CL
2798 */
2799static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2800 unsigned long action, void *hcpu)
2801{
2802 long cpu = (long)hcpu;
5b95a4ac
CL
2803 struct kmem_cache *s;
2804 unsigned long flags;
81819f0f
CL
2805
2806 switch (action) {
2807 case CPU_UP_CANCELED:
8bb78442 2808 case CPU_UP_CANCELED_FROZEN:
81819f0f 2809 case CPU_DEAD:
8bb78442 2810 case CPU_DEAD_FROZEN:
5b95a4ac
CL
2811 down_read(&slub_lock);
2812 list_for_each_entry(s, &slab_caches, list) {
2813 local_irq_save(flags);
2814 __flush_cpu_slab(s, cpu);
2815 local_irq_restore(flags);
2816 }
2817 up_read(&slub_lock);
81819f0f
CL
2818 break;
2819 default:
2820 break;
2821 }
2822 return NOTIFY_OK;
2823}
2824
2825static struct notifier_block __cpuinitdata slab_notifier =
2826 { &slab_cpuup_callback, NULL, 0 };
2827
2828#endif
2829
81819f0f
CL
2830void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2831{
aadb4bc4
CL
2832 struct kmem_cache *s;
2833
2834 if (unlikely(size > PAGE_SIZE / 2))
2835 return (void *)__get_free_pages(gfpflags | __GFP_COMP,
2836 get_order(size));
2837 s = get_slab(size, gfpflags);
81819f0f 2838
2408c550 2839 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913 2840 return s;
81819f0f 2841
ce15fea8 2842 return slab_alloc(s, gfpflags, -1, caller);
81819f0f
CL
2843}
2844
2845void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2846 int node, void *caller)
2847{
aadb4bc4
CL
2848 struct kmem_cache *s;
2849
2850 if (unlikely(size > PAGE_SIZE / 2))
2851 return (void *)__get_free_pages(gfpflags | __GFP_COMP,
2852 get_order(size));
2853 s = get_slab(size, gfpflags);
81819f0f 2854
2408c550 2855 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913 2856 return s;
81819f0f 2857
ce15fea8 2858 return slab_alloc(s, gfpflags, node, caller);
81819f0f
CL
2859}
2860
41ecc55b 2861#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
434e245d
CL
2862static int validate_slab(struct kmem_cache *s, struct page *page,
2863 unsigned long *map)
53e15af0
CL
2864{
2865 void *p;
2866 void *addr = page_address(page);
53e15af0
CL
2867
2868 if (!check_slab(s, page) ||
2869 !on_freelist(s, page, NULL))
2870 return 0;
2871
2872 /* Now we know that a valid freelist exists */
2873 bitmap_zero(map, s->objects);
2874
7656c72b
CL
2875 for_each_free_object(p, s, page->freelist) {
2876 set_bit(slab_index(p, s, addr), map);
53e15af0
CL
2877 if (!check_object(s, page, p, 0))
2878 return 0;
2879 }
2880
7656c72b
CL
2881 for_each_object(p, s, addr)
2882 if (!test_bit(slab_index(p, s, addr), map))
53e15af0
CL
2883 if (!check_object(s, page, p, 1))
2884 return 0;
2885 return 1;
2886}
2887
434e245d
CL
2888static void validate_slab_slab(struct kmem_cache *s, struct page *page,
2889 unsigned long *map)
53e15af0
CL
2890{
2891 if (slab_trylock(page)) {
434e245d 2892 validate_slab(s, page, map);
53e15af0
CL
2893 slab_unlock(page);
2894 } else
2895 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2896 s->name, page);
2897
2898 if (s->flags & DEBUG_DEFAULT_FLAGS) {
35e5d7ee
CL
2899 if (!SlabDebug(page))
2900 printk(KERN_ERR "SLUB %s: SlabDebug not set "
53e15af0
CL
2901 "on slab 0x%p\n", s->name, page);
2902 } else {
35e5d7ee
CL
2903 if (SlabDebug(page))
2904 printk(KERN_ERR "SLUB %s: SlabDebug set on "
53e15af0
CL
2905 "slab 0x%p\n", s->name, page);
2906 }
2907}
2908
434e245d
CL
2909static int validate_slab_node(struct kmem_cache *s,
2910 struct kmem_cache_node *n, unsigned long *map)
53e15af0
CL
2911{
2912 unsigned long count = 0;
2913 struct page *page;
2914 unsigned long flags;
2915
2916 spin_lock_irqsave(&n->list_lock, flags);
2917
2918 list_for_each_entry(page, &n->partial, lru) {
434e245d 2919 validate_slab_slab(s, page, map);
53e15af0
CL
2920 count++;
2921 }
2922 if (count != n->nr_partial)
2923 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2924 "counter=%ld\n", s->name, count, n->nr_partial);
2925
2926 if (!(s->flags & SLAB_STORE_USER))
2927 goto out;
2928
2929 list_for_each_entry(page, &n->full, lru) {
434e245d 2930 validate_slab_slab(s, page, map);
53e15af0
CL
2931 count++;
2932 }
2933 if (count != atomic_long_read(&n->nr_slabs))
2934 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2935 "counter=%ld\n", s->name, count,
2936 atomic_long_read(&n->nr_slabs));
2937
2938out:
2939 spin_unlock_irqrestore(&n->list_lock, flags);
2940 return count;
2941}
2942
434e245d 2943static long validate_slab_cache(struct kmem_cache *s)
53e15af0
CL
2944{
2945 int node;
2946 unsigned long count = 0;
434e245d
CL
2947 unsigned long *map = kmalloc(BITS_TO_LONGS(s->objects) *
2948 sizeof(unsigned long), GFP_KERNEL);
2949
2950 if (!map)
2951 return -ENOMEM;
53e15af0
CL
2952
2953 flush_all(s);
f64dc58c 2954 for_each_node_state(node, N_NORMAL_MEMORY) {
53e15af0
CL
2955 struct kmem_cache_node *n = get_node(s, node);
2956
434e245d 2957 count += validate_slab_node(s, n, map);
53e15af0 2958 }
434e245d 2959 kfree(map);
53e15af0
CL
2960 return count;
2961}
2962
b3459709
CL
2963#ifdef SLUB_RESILIENCY_TEST
2964static void resiliency_test(void)
2965{
2966 u8 *p;
2967
2968 printk(KERN_ERR "SLUB resiliency testing\n");
2969 printk(KERN_ERR "-----------------------\n");
2970 printk(KERN_ERR "A. Corruption after allocation\n");
2971
2972 p = kzalloc(16, GFP_KERNEL);
2973 p[16] = 0x12;
2974 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2975 " 0x12->0x%p\n\n", p + 16);
2976
2977 validate_slab_cache(kmalloc_caches + 4);
2978
2979 /* Hmmm... The next two are dangerous */
2980 p = kzalloc(32, GFP_KERNEL);
2981 p[32 + sizeof(void *)] = 0x34;
2982 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2983 " 0x34 -> -0x%p\n", p);
2984 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2985
2986 validate_slab_cache(kmalloc_caches + 5);
2987 p = kzalloc(64, GFP_KERNEL);
2988 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2989 *p = 0x56;
2990 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2991 p);
2992 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2993 validate_slab_cache(kmalloc_caches + 6);
2994
2995 printk(KERN_ERR "\nB. Corruption after free\n");
2996 p = kzalloc(128, GFP_KERNEL);
2997 kfree(p);
2998 *p = 0x78;
2999 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3000 validate_slab_cache(kmalloc_caches + 7);
3001
3002 p = kzalloc(256, GFP_KERNEL);
3003 kfree(p);
3004 p[50] = 0x9a;
3005 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
3006 validate_slab_cache(kmalloc_caches + 8);
3007
3008 p = kzalloc(512, GFP_KERNEL);
3009 kfree(p);
3010 p[512] = 0xab;
3011 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3012 validate_slab_cache(kmalloc_caches + 9);
3013}
3014#else
3015static void resiliency_test(void) {};
3016#endif
3017
88a420e4 3018/*
672bba3a 3019 * Generate lists of code addresses where slabcache objects are allocated
88a420e4
CL
3020 * and freed.
3021 */
3022
3023struct location {
3024 unsigned long count;
3025 void *addr;
45edfa58
CL
3026 long long sum_time;
3027 long min_time;
3028 long max_time;
3029 long min_pid;
3030 long max_pid;
3031 cpumask_t cpus;
3032 nodemask_t nodes;
88a420e4
CL
3033};
3034
3035struct loc_track {
3036 unsigned long max;
3037 unsigned long count;
3038 struct location *loc;
3039};
3040
3041static void free_loc_track(struct loc_track *t)
3042{
3043 if (t->max)
3044 free_pages((unsigned long)t->loc,
3045 get_order(sizeof(struct location) * t->max));
3046}
3047
68dff6a9 3048static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
88a420e4
CL
3049{
3050 struct location *l;
3051 int order;
3052
88a420e4
CL
3053 order = get_order(sizeof(struct location) * max);
3054
68dff6a9 3055 l = (void *)__get_free_pages(flags, order);
88a420e4
CL
3056 if (!l)
3057 return 0;
3058
3059 if (t->count) {
3060 memcpy(l, t->loc, sizeof(struct location) * t->count);
3061 free_loc_track(t);
3062 }
3063 t->max = max;
3064 t->loc = l;
3065 return 1;
3066}
3067
3068static int add_location(struct loc_track *t, struct kmem_cache *s,
45edfa58 3069 const struct track *track)
88a420e4
CL
3070{
3071 long start, end, pos;
3072 struct location *l;
3073 void *caddr;
45edfa58 3074 unsigned long age = jiffies - track->when;
88a420e4
CL
3075
3076 start = -1;
3077 end = t->count;
3078
3079 for ( ; ; ) {
3080 pos = start + (end - start + 1) / 2;
3081
3082 /*
3083 * There is nothing at "end". If we end up there
3084 * we need to add something to before end.
3085 */
3086 if (pos == end)
3087 break;
3088
3089 caddr = t->loc[pos].addr;
45edfa58
CL
3090 if (track->addr == caddr) {
3091
3092 l = &t->loc[pos];
3093 l->count++;
3094 if (track->when) {
3095 l->sum_time += age;
3096 if (age < l->min_time)
3097 l->min_time = age;
3098 if (age > l->max_time)
3099 l->max_time = age;
3100
3101 if (track->pid < l->min_pid)
3102 l->min_pid = track->pid;
3103 if (track->pid > l->max_pid)
3104 l->max_pid = track->pid;
3105
3106 cpu_set(track->cpu, l->cpus);
3107 }
3108 node_set(page_to_nid(virt_to_page(track)), l->nodes);
88a420e4
CL
3109 return 1;
3110 }
3111
45edfa58 3112 if (track->addr < caddr)
88a420e4
CL
3113 end = pos;
3114 else
3115 start = pos;
3116 }
3117
3118 /*
672bba3a 3119 * Not found. Insert new tracking element.
88a420e4 3120 */
68dff6a9 3121 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
88a420e4
CL
3122 return 0;
3123
3124 l = t->loc + pos;
3125 if (pos < t->count)
3126 memmove(l + 1, l,
3127 (t->count - pos) * sizeof(struct location));
3128 t->count++;
3129 l->count = 1;
45edfa58
CL
3130 l->addr = track->addr;
3131 l->sum_time = age;
3132 l->min_time = age;
3133 l->max_time = age;
3134 l->min_pid = track->pid;
3135 l->max_pid = track->pid;
3136 cpus_clear(l->cpus);
3137 cpu_set(track->cpu, l->cpus);
3138 nodes_clear(l->nodes);
3139 node_set(page_to_nid(virt_to_page(track)), l->nodes);
88a420e4
CL
3140 return 1;
3141}
3142
3143static void process_slab(struct loc_track *t, struct kmem_cache *s,
3144 struct page *page, enum track_item alloc)
3145{
3146 void *addr = page_address(page);
7656c72b 3147 DECLARE_BITMAP(map, s->objects);
88a420e4
CL
3148 void *p;
3149
3150 bitmap_zero(map, s->objects);
7656c72b
CL
3151 for_each_free_object(p, s, page->freelist)
3152 set_bit(slab_index(p, s, addr), map);
88a420e4 3153
7656c72b 3154 for_each_object(p, s, addr)
45edfa58
CL
3155 if (!test_bit(slab_index(p, s, addr), map))
3156 add_location(t, s, get_track(s, p, alloc));
88a420e4
CL
3157}
3158
3159static int list_locations(struct kmem_cache *s, char *buf,
3160 enum track_item alloc)
3161{
3162 int n = 0;
3163 unsigned long i;
68dff6a9 3164 struct loc_track t = { 0, 0, NULL };
88a420e4
CL
3165 int node;
3166
68dff6a9
CL
3167 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3168 GFP_KERNEL))
3169 return sprintf(buf, "Out of memory\n");
88a420e4
CL
3170
3171 /* Push back cpu slabs */
3172 flush_all(s);
3173
f64dc58c 3174 for_each_node_state(node, N_NORMAL_MEMORY) {
88a420e4
CL
3175 struct kmem_cache_node *n = get_node(s, node);
3176 unsigned long flags;
3177 struct page *page;
3178
9e86943b 3179 if (!atomic_long_read(&n->nr_slabs))
88a420e4
CL
3180 continue;
3181
3182 spin_lock_irqsave(&n->list_lock, flags);
3183 list_for_each_entry(page, &n->partial, lru)
3184 process_slab(&t, s, page, alloc);
3185 list_for_each_entry(page, &n->full, lru)
3186 process_slab(&t, s, page, alloc);
3187 spin_unlock_irqrestore(&n->list_lock, flags);
3188 }
3189
3190 for (i = 0; i < t.count; i++) {
45edfa58 3191 struct location *l = &t.loc[i];
88a420e4
CL
3192
3193 if (n > PAGE_SIZE - 100)
3194 break;
45edfa58
CL
3195 n += sprintf(buf + n, "%7ld ", l->count);
3196
3197 if (l->addr)
3198 n += sprint_symbol(buf + n, (unsigned long)l->addr);
88a420e4
CL
3199 else
3200 n += sprintf(buf + n, "<not-available>");
45edfa58
CL
3201
3202 if (l->sum_time != l->min_time) {
3203 unsigned long remainder;
3204
3205 n += sprintf(buf + n, " age=%ld/%ld/%ld",
3206 l->min_time,
3207 div_long_long_rem(l->sum_time, l->count, &remainder),
3208 l->max_time);
3209 } else
3210 n += sprintf(buf + n, " age=%ld",
3211 l->min_time);
3212
3213 if (l->min_pid != l->max_pid)
3214 n += sprintf(buf + n, " pid=%ld-%ld",
3215 l->min_pid, l->max_pid);
3216 else
3217 n += sprintf(buf + n, " pid=%ld",
3218 l->min_pid);
3219
84966343
CL
3220 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3221 n < PAGE_SIZE - 60) {
45edfa58
CL
3222 n += sprintf(buf + n, " cpus=");
3223 n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3224 l->cpus);
3225 }
3226
84966343
CL
3227 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3228 n < PAGE_SIZE - 60) {
45edfa58
CL
3229 n += sprintf(buf + n, " nodes=");
3230 n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3231 l->nodes);
3232 }
3233
88a420e4
CL
3234 n += sprintf(buf + n, "\n");
3235 }
3236
3237 free_loc_track(&t);
3238 if (!t.count)
3239 n += sprintf(buf, "No data\n");
3240 return n;
3241}
3242
81819f0f
CL
3243static unsigned long count_partial(struct kmem_cache_node *n)
3244{
3245 unsigned long flags;
3246 unsigned long x = 0;
3247 struct page *page;
3248
3249 spin_lock_irqsave(&n->list_lock, flags);
3250 list_for_each_entry(page, &n->partial, lru)
3251 x += page->inuse;
3252 spin_unlock_irqrestore(&n->list_lock, flags);
3253 return x;
3254}
3255
3256enum slab_stat_type {
3257 SL_FULL,
3258 SL_PARTIAL,
3259 SL_CPU,
3260 SL_OBJECTS
3261};
3262
3263#define SO_FULL (1 << SL_FULL)
3264#define SO_PARTIAL (1 << SL_PARTIAL)
3265#define SO_CPU (1 << SL_CPU)
3266#define SO_OBJECTS (1 << SL_OBJECTS)
3267
3268static unsigned long slab_objects(struct kmem_cache *s,
3269 char *buf, unsigned long flags)
3270{
3271 unsigned long total = 0;
3272 int cpu;
3273 int node;
3274 int x;
3275 unsigned long *nodes;
3276 unsigned long *per_cpu;
3277
3278 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3279 per_cpu = nodes + nr_node_ids;
3280
3281 for_each_possible_cpu(cpu) {
dfb4f096
CL
3282 struct page *page;
3283 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
81819f0f 3284
dfb4f096
CL
3285 if (!c)
3286 continue;
3287
3288 page = c->page;
81819f0f 3289 if (page) {
81819f0f
CL
3290 if (flags & SO_CPU) {
3291 int x = 0;
3292
3293 if (flags & SO_OBJECTS)
3294 x = page->inuse;
3295 else
3296 x = 1;
3297 total += x;
dfb4f096 3298 nodes[c->node] += x;
81819f0f 3299 }
dfb4f096 3300 per_cpu[c->node]++;
81819f0f
CL
3301 }
3302 }
3303
f64dc58c 3304 for_each_node_state(node, N_NORMAL_MEMORY) {
81819f0f
CL
3305 struct kmem_cache_node *n = get_node(s, node);
3306
3307 if (flags & SO_PARTIAL) {
3308 if (flags & SO_OBJECTS)
3309 x = count_partial(n);
3310 else
3311 x = n->nr_partial;
3312 total += x;
3313 nodes[node] += x;
3314 }
3315
3316 if (flags & SO_FULL) {
9e86943b 3317 int full_slabs = atomic_long_read(&n->nr_slabs)
81819f0f
CL
3318 - per_cpu[node]
3319 - n->nr_partial;
3320
3321 if (flags & SO_OBJECTS)
3322 x = full_slabs * s->objects;
3323 else
3324 x = full_slabs;
3325 total += x;
3326 nodes[node] += x;
3327 }
3328 }
3329
3330 x = sprintf(buf, "%lu", total);
3331#ifdef CONFIG_NUMA
f64dc58c 3332 for_each_node_state(node, N_NORMAL_MEMORY)
81819f0f
CL
3333 if (nodes[node])
3334 x += sprintf(buf + x, " N%d=%lu",
3335 node, nodes[node]);
3336#endif
3337 kfree(nodes);
3338 return x + sprintf(buf + x, "\n");
3339}
3340
3341static int any_slab_objects(struct kmem_cache *s)
3342{
3343 int node;
3344 int cpu;
3345
dfb4f096
CL
3346 for_each_possible_cpu(cpu) {
3347 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3348
3349 if (c && c->page)
81819f0f 3350 return 1;
dfb4f096 3351 }
81819f0f 3352
dfb4f096 3353 for_each_online_node(node) {
81819f0f
CL
3354 struct kmem_cache_node *n = get_node(s, node);
3355
dfb4f096
CL
3356 if (!n)
3357 continue;
3358
9e86943b 3359 if (n->nr_partial || atomic_long_read(&n->nr_slabs))
81819f0f
CL
3360 return 1;
3361 }
3362 return 0;
3363}
3364
3365#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3366#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3367
3368struct slab_attribute {
3369 struct attribute attr;
3370 ssize_t (*show)(struct kmem_cache *s, char *buf);
3371 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3372};
3373
3374#define SLAB_ATTR_RO(_name) \
3375 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3376
3377#define SLAB_ATTR(_name) \
3378 static struct slab_attribute _name##_attr = \
3379 __ATTR(_name, 0644, _name##_show, _name##_store)
3380
81819f0f
CL
3381static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3382{
3383 return sprintf(buf, "%d\n", s->size);
3384}
3385SLAB_ATTR_RO(slab_size);
3386
3387static ssize_t align_show(struct kmem_cache *s, char *buf)
3388{
3389 return sprintf(buf, "%d\n", s->align);
3390}
3391SLAB_ATTR_RO(align);
3392
3393static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3394{
3395 return sprintf(buf, "%d\n", s->objsize);
3396}
3397SLAB_ATTR_RO(object_size);
3398
3399static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3400{
3401 return sprintf(buf, "%d\n", s->objects);
3402}
3403SLAB_ATTR_RO(objs_per_slab);
3404
3405static ssize_t order_show(struct kmem_cache *s, char *buf)
3406{
3407 return sprintf(buf, "%d\n", s->order);
3408}
3409SLAB_ATTR_RO(order);
3410
3411static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3412{
3413 if (s->ctor) {
3414 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3415
3416 return n + sprintf(buf + n, "\n");
3417 }
3418 return 0;
3419}
3420SLAB_ATTR_RO(ctor);
3421
81819f0f
CL
3422static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3423{
3424 return sprintf(buf, "%d\n", s->refcount - 1);
3425}
3426SLAB_ATTR_RO(aliases);
3427
3428static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3429{
3430 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3431}
3432SLAB_ATTR_RO(slabs);
3433
3434static ssize_t partial_show(struct kmem_cache *s, char *buf)
3435{
3436 return slab_objects(s, buf, SO_PARTIAL);
3437}
3438SLAB_ATTR_RO(partial);
3439
3440static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3441{
3442 return slab_objects(s, buf, SO_CPU);
3443}
3444SLAB_ATTR_RO(cpu_slabs);
3445
3446static ssize_t objects_show(struct kmem_cache *s, char *buf)
3447{
3448 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3449}
3450SLAB_ATTR_RO(objects);
3451
3452static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3453{
3454 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3455}
3456
3457static ssize_t sanity_checks_store(struct kmem_cache *s,
3458 const char *buf, size_t length)
3459{
3460 s->flags &= ~SLAB_DEBUG_FREE;
3461 if (buf[0] == '1')
3462 s->flags |= SLAB_DEBUG_FREE;
3463 return length;
3464}
3465SLAB_ATTR(sanity_checks);
3466
3467static ssize_t trace_show(struct kmem_cache *s, char *buf)
3468{
3469 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3470}
3471
3472static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3473 size_t length)
3474{
3475 s->flags &= ~SLAB_TRACE;
3476 if (buf[0] == '1')
3477 s->flags |= SLAB_TRACE;
3478 return length;
3479}
3480SLAB_ATTR(trace);
3481
3482static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3483{
3484 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3485}
3486
3487static ssize_t reclaim_account_store(struct kmem_cache *s,
3488 const char *buf, size_t length)
3489{
3490 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3491 if (buf[0] == '1')
3492 s->flags |= SLAB_RECLAIM_ACCOUNT;
3493 return length;
3494}
3495SLAB_ATTR(reclaim_account);
3496
3497static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3498{
5af60839 3499 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
81819f0f
CL
3500}
3501SLAB_ATTR_RO(hwcache_align);
3502
3503#ifdef CONFIG_ZONE_DMA
3504static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3505{
3506 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3507}
3508SLAB_ATTR_RO(cache_dma);
3509#endif
3510
3511static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3512{
3513 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3514}
3515SLAB_ATTR_RO(destroy_by_rcu);
3516
3517static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3518{
3519 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3520}
3521
3522static ssize_t red_zone_store(struct kmem_cache *s,
3523 const char *buf, size_t length)
3524{
3525 if (any_slab_objects(s))
3526 return -EBUSY;
3527
3528 s->flags &= ~SLAB_RED_ZONE;
3529 if (buf[0] == '1')
3530 s->flags |= SLAB_RED_ZONE;
3531 calculate_sizes(s);
3532 return length;
3533}
3534SLAB_ATTR(red_zone);
3535
3536static ssize_t poison_show(struct kmem_cache *s, char *buf)
3537{
3538 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3539}
3540
3541static ssize_t poison_store(struct kmem_cache *s,
3542 const char *buf, size_t length)
3543{
3544 if (any_slab_objects(s))
3545 return -EBUSY;
3546
3547 s->flags &= ~SLAB_POISON;
3548 if (buf[0] == '1')
3549 s->flags |= SLAB_POISON;
3550 calculate_sizes(s);
3551 return length;
3552}
3553SLAB_ATTR(poison);
3554
3555static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3556{
3557 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3558}
3559
3560static ssize_t store_user_store(struct kmem_cache *s,
3561 const char *buf, size_t length)
3562{
3563 if (any_slab_objects(s))
3564 return -EBUSY;
3565
3566 s->flags &= ~SLAB_STORE_USER;
3567 if (buf[0] == '1')
3568 s->flags |= SLAB_STORE_USER;
3569 calculate_sizes(s);
3570 return length;
3571}
3572SLAB_ATTR(store_user);
3573
53e15af0
CL
3574static ssize_t validate_show(struct kmem_cache *s, char *buf)
3575{
3576 return 0;
3577}
3578
3579static ssize_t validate_store(struct kmem_cache *s,
3580 const char *buf, size_t length)
3581{
434e245d
CL
3582 int ret = -EINVAL;
3583
3584 if (buf[0] == '1') {
3585 ret = validate_slab_cache(s);
3586 if (ret >= 0)
3587 ret = length;
3588 }
3589 return ret;
53e15af0
CL
3590}
3591SLAB_ATTR(validate);
3592
2086d26a
CL
3593static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3594{
3595 return 0;
3596}
3597
3598static ssize_t shrink_store(struct kmem_cache *s,
3599 const char *buf, size_t length)
3600{
3601 if (buf[0] == '1') {
3602 int rc = kmem_cache_shrink(s);
3603
3604 if (rc)
3605 return rc;
3606 } else
3607 return -EINVAL;
3608 return length;
3609}
3610SLAB_ATTR(shrink);
3611
88a420e4
CL
3612static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3613{
3614 if (!(s->flags & SLAB_STORE_USER))
3615 return -ENOSYS;
3616 return list_locations(s, buf, TRACK_ALLOC);
3617}
3618SLAB_ATTR_RO(alloc_calls);
3619
3620static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3621{
3622 if (!(s->flags & SLAB_STORE_USER))
3623 return -ENOSYS;
3624 return list_locations(s, buf, TRACK_FREE);
3625}
3626SLAB_ATTR_RO(free_calls);
3627
81819f0f
CL
3628#ifdef CONFIG_NUMA
3629static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3630{
3631 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3632}
3633
3634static ssize_t defrag_ratio_store(struct kmem_cache *s,
3635 const char *buf, size_t length)
3636{
3637 int n = simple_strtoul(buf, NULL, 10);
3638
3639 if (n < 100)
3640 s->defrag_ratio = n * 10;
3641 return length;
3642}
3643SLAB_ATTR(defrag_ratio);
3644#endif
3645
3646static struct attribute * slab_attrs[] = {
3647 &slab_size_attr.attr,
3648 &object_size_attr.attr,
3649 &objs_per_slab_attr.attr,
3650 &order_attr.attr,
3651 &objects_attr.attr,
3652 &slabs_attr.attr,
3653 &partial_attr.attr,
3654 &cpu_slabs_attr.attr,
3655 &ctor_attr.attr,
81819f0f
CL
3656 &aliases_attr.attr,
3657 &align_attr.attr,
3658 &sanity_checks_attr.attr,
3659 &trace_attr.attr,
3660 &hwcache_align_attr.attr,
3661 &reclaim_account_attr.attr,
3662 &destroy_by_rcu_attr.attr,
3663 &red_zone_attr.attr,
3664 &poison_attr.attr,
3665 &store_user_attr.attr,
53e15af0 3666 &validate_attr.attr,
2086d26a 3667 &shrink_attr.attr,
88a420e4
CL
3668 &alloc_calls_attr.attr,
3669 &free_calls_attr.attr,
81819f0f
CL
3670#ifdef CONFIG_ZONE_DMA
3671 &cache_dma_attr.attr,
3672#endif
3673#ifdef CONFIG_NUMA
3674 &defrag_ratio_attr.attr,
3675#endif
3676 NULL
3677};
3678
3679static struct attribute_group slab_attr_group = {
3680 .attrs = slab_attrs,
3681};
3682
3683static ssize_t slab_attr_show(struct kobject *kobj,
3684 struct attribute *attr,
3685 char *buf)
3686{
3687 struct slab_attribute *attribute;
3688 struct kmem_cache *s;
3689 int err;
3690
3691 attribute = to_slab_attr(attr);
3692 s = to_slab(kobj);
3693
3694 if (!attribute->show)
3695 return -EIO;
3696
3697 err = attribute->show(s, buf);
3698
3699 return err;
3700}
3701
3702static ssize_t slab_attr_store(struct kobject *kobj,
3703 struct attribute *attr,
3704 const char *buf, size_t len)
3705{
3706 struct slab_attribute *attribute;
3707 struct kmem_cache *s;
3708 int err;
3709
3710 attribute = to_slab_attr(attr);
3711 s = to_slab(kobj);
3712
3713 if (!attribute->store)
3714 return -EIO;
3715
3716 err = attribute->store(s, buf, len);
3717
3718 return err;
3719}
3720
3721static struct sysfs_ops slab_sysfs_ops = {
3722 .show = slab_attr_show,
3723 .store = slab_attr_store,
3724};
3725
3726static struct kobj_type slab_ktype = {
3727 .sysfs_ops = &slab_sysfs_ops,
3728};
3729
3730static int uevent_filter(struct kset *kset, struct kobject *kobj)
3731{
3732 struct kobj_type *ktype = get_ktype(kobj);
3733
3734 if (ktype == &slab_ktype)
3735 return 1;
3736 return 0;
3737}
3738
3739static struct kset_uevent_ops slab_uevent_ops = {
3740 .filter = uevent_filter,
3741};
3742
5af328a5 3743static decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
81819f0f
CL
3744
3745#define ID_STR_LENGTH 64
3746
3747/* Create a unique string id for a slab cache:
3748 * format
3749 * :[flags-]size:[memory address of kmemcache]
3750 */
3751static char *create_unique_id(struct kmem_cache *s)
3752{
3753 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3754 char *p = name;
3755
3756 BUG_ON(!name);
3757
3758 *p++ = ':';
3759 /*
3760 * First flags affecting slabcache operations. We will only
3761 * get here for aliasable slabs so we do not need to support
3762 * too many flags. The flags here must cover all flags that
3763 * are matched during merging to guarantee that the id is
3764 * unique.
3765 */
3766 if (s->flags & SLAB_CACHE_DMA)
3767 *p++ = 'd';
3768 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3769 *p++ = 'a';
3770 if (s->flags & SLAB_DEBUG_FREE)
3771 *p++ = 'F';
3772 if (p != name + 1)
3773 *p++ = '-';
3774 p += sprintf(p, "%07d", s->size);
3775 BUG_ON(p > name + ID_STR_LENGTH - 1);
3776 return name;
3777}
3778
3779static int sysfs_slab_add(struct kmem_cache *s)
3780{
3781 int err;
3782 const char *name;
3783 int unmergeable;
3784
3785 if (slab_state < SYSFS)
3786 /* Defer until later */
3787 return 0;
3788
3789 unmergeable = slab_unmergeable(s);
3790 if (unmergeable) {
3791 /*
3792 * Slabcache can never be merged so we can use the name proper.
3793 * This is typically the case for debug situations. In that
3794 * case we can catch duplicate names easily.
3795 */
0f9008ef 3796 sysfs_remove_link(&slab_subsys.kobj, s->name);
81819f0f
CL
3797 name = s->name;
3798 } else {
3799 /*
3800 * Create a unique name for the slab as a target
3801 * for the symlinks.
3802 */
3803 name = create_unique_id(s);
3804 }
3805
3806 kobj_set_kset_s(s, slab_subsys);
3807 kobject_set_name(&s->kobj, name);
3808 kobject_init(&s->kobj);
3809 err = kobject_add(&s->kobj);
3810 if (err)
3811 return err;
3812
3813 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3814 if (err)
3815 return err;
3816 kobject_uevent(&s->kobj, KOBJ_ADD);
3817 if (!unmergeable) {
3818 /* Setup first alias */
3819 sysfs_slab_alias(s, s->name);
3820 kfree(name);
3821 }
3822 return 0;
3823}
3824
3825static void sysfs_slab_remove(struct kmem_cache *s)
3826{
3827 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3828 kobject_del(&s->kobj);
3829}
3830
3831/*
3832 * Need to buffer aliases during bootup until sysfs becomes
3833 * available lest we loose that information.
3834 */
3835struct saved_alias {
3836 struct kmem_cache *s;
3837 const char *name;
3838 struct saved_alias *next;
3839};
3840
5af328a5 3841static struct saved_alias *alias_list;
81819f0f
CL
3842
3843static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3844{
3845 struct saved_alias *al;
3846
3847 if (slab_state == SYSFS) {
3848 /*
3849 * If we have a leftover link then remove it.
3850 */
0f9008ef
LT
3851 sysfs_remove_link(&slab_subsys.kobj, name);
3852 return sysfs_create_link(&slab_subsys.kobj,
81819f0f
CL
3853 &s->kobj, name);
3854 }
3855
3856 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3857 if (!al)
3858 return -ENOMEM;
3859
3860 al->s = s;
3861 al->name = name;
3862 al->next = alias_list;
3863 alias_list = al;
3864 return 0;
3865}
3866
3867static int __init slab_sysfs_init(void)
3868{
5b95a4ac 3869 struct kmem_cache *s;
81819f0f
CL
3870 int err;
3871
3872 err = subsystem_register(&slab_subsys);
3873 if (err) {
3874 printk(KERN_ERR "Cannot register slab subsystem.\n");
3875 return -ENOSYS;
3876 }
3877
26a7bd03
CL
3878 slab_state = SYSFS;
3879
5b95a4ac 3880 list_for_each_entry(s, &slab_caches, list) {
26a7bd03 3881 err = sysfs_slab_add(s);
5d540fb7
CL
3882 if (err)
3883 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
3884 " to sysfs\n", s->name);
26a7bd03 3885 }
81819f0f
CL
3886
3887 while (alias_list) {
3888 struct saved_alias *al = alias_list;
3889
3890 alias_list = alias_list->next;
3891 err = sysfs_slab_alias(al->s, al->name);
5d540fb7
CL
3892 if (err)
3893 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
3894 " %s to sysfs\n", s->name);
81819f0f
CL
3895 kfree(al);
3896 }
3897
3898 resiliency_test();
3899 return 0;
3900}
3901
3902__initcall(slab_sysfs_init);
81819f0f 3903#endif