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