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