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