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