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