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