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