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