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