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