ia64/linux-2.6.18-xen.hg

annotate mm/slab.c @ 871:9cbcc9008446

xen/x86: don't initialize cpu_data[]'s apicid field on generic code

Afaict, this is not only redundant with the intialization done in
drivers/xen/core/smpboot.c, but actually results - at least for
secondary CPUs - in the Xen-specific value written to be later
overwritten with whatever the generic code determines (with no
guarantee that the two values are identical).

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu May 14 10:09:15 2009 +0100 (2009-05-14)
parents 3e8752eb6d9c
children
rev   line source
ian@0 1 /*
ian@0 2 * linux/mm/slab.c
ian@0 3 * Written by Mark Hemment, 1996/97.
ian@0 4 * (markhe@nextd.demon.co.uk)
ian@0 5 *
ian@0 6 * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
ian@0 7 *
ian@0 8 * Major cleanup, different bufctl logic, per-cpu arrays
ian@0 9 * (c) 2000 Manfred Spraul
ian@0 10 *
ian@0 11 * Cleanup, make the head arrays unconditional, preparation for NUMA
ian@0 12 * (c) 2002 Manfred Spraul
ian@0 13 *
ian@0 14 * An implementation of the Slab Allocator as described in outline in;
ian@0 15 * UNIX Internals: The New Frontiers by Uresh Vahalia
ian@0 16 * Pub: Prentice Hall ISBN 0-13-101908-2
ian@0 17 * or with a little more detail in;
ian@0 18 * The Slab Allocator: An Object-Caching Kernel Memory Allocator
ian@0 19 * Jeff Bonwick (Sun Microsystems).
ian@0 20 * Presented at: USENIX Summer 1994 Technical Conference
ian@0 21 *
ian@0 22 * The memory is organized in caches, one cache for each object type.
ian@0 23 * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
ian@0 24 * Each cache consists out of many slabs (they are small (usually one
ian@0 25 * page long) and always contiguous), and each slab contains multiple
ian@0 26 * initialized objects.
ian@0 27 *
ian@0 28 * This means, that your constructor is used only for newly allocated
ian@0 29 * slabs and you must pass objects with the same intializations to
ian@0 30 * kmem_cache_free.
ian@0 31 *
ian@0 32 * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
ian@0 33 * normal). If you need a special memory type, then must create a new
ian@0 34 * cache for that memory type.
ian@0 35 *
ian@0 36 * In order to reduce fragmentation, the slabs are sorted in 3 groups:
ian@0 37 * full slabs with 0 free objects
ian@0 38 * partial slabs
ian@0 39 * empty slabs with no allocated objects
ian@0 40 *
ian@0 41 * If partial slabs exist, then new allocations come from these slabs,
ian@0 42 * otherwise from empty slabs or new slabs are allocated.
ian@0 43 *
ian@0 44 * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
ian@0 45 * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
ian@0 46 *
ian@0 47 * Each cache has a short per-cpu head array, most allocs
ian@0 48 * and frees go into that array, and if that array overflows, then 1/2
ian@0 49 * of the entries in the array are given back into the global cache.
ian@0 50 * The head array is strictly LIFO and should improve the cache hit rates.
ian@0 51 * On SMP, it additionally reduces the spinlock operations.
ian@0 52 *
ian@0 53 * The c_cpuarray may not be read with enabled local interrupts -
ian@0 54 * it's changed with a smp_call_function().
ian@0 55 *
ian@0 56 * SMP synchronization:
ian@0 57 * constructors and destructors are called without any locking.
ian@0 58 * Several members in struct kmem_cache and struct slab never change, they
ian@0 59 * are accessed without any locking.
ian@0 60 * The per-cpu arrays are never accessed from the wrong cpu, no locking,
ian@0 61 * and local interrupts are disabled so slab code is preempt-safe.
ian@0 62 * The non-constant members are protected with a per-cache irq spinlock.
ian@0 63 *
ian@0 64 * Many thanks to Mark Hemment, who wrote another per-cpu slab patch
ian@0 65 * in 2000 - many ideas in the current implementation are derived from
ian@0 66 * his patch.
ian@0 67 *
ian@0 68 * Further notes from the original documentation:
ian@0 69 *
ian@0 70 * 11 April '97. Started multi-threading - markhe
ian@0 71 * The global cache-chain is protected by the mutex 'cache_chain_mutex'.
ian@0 72 * The sem is only needed when accessing/extending the cache-chain, which
ian@0 73 * can never happen inside an interrupt (kmem_cache_create(),
ian@0 74 * kmem_cache_shrink() and kmem_cache_reap()).
ian@0 75 *
ian@0 76 * At present, each engine can be growing a cache. This should be blocked.
ian@0 77 *
ian@0 78 * 15 March 2005. NUMA slab allocator.
ian@0 79 * Shai Fultheim <shai@scalex86.org>.
ian@0 80 * Shobhit Dayal <shobhit@calsoftinc.com>
ian@0 81 * Alok N Kataria <alokk@calsoftinc.com>
ian@0 82 * Christoph Lameter <christoph@lameter.com>
ian@0 83 *
ian@0 84 * Modified the slab allocator to be node aware on NUMA systems.
ian@0 85 * Each node has its own list of partial, free and full slabs.
ian@0 86 * All object allocations for a node occur from node specific slab lists.
ian@0 87 */
ian@0 88
ian@0 89 #include <linux/config.h>
ian@0 90 #include <linux/slab.h>
ian@0 91 #include <linux/mm.h>
ian@0 92 #include <linux/poison.h>
ian@0 93 #include <linux/swap.h>
ian@0 94 #include <linux/cache.h>
ian@0 95 #include <linux/interrupt.h>
ian@0 96 #include <linux/init.h>
ian@0 97 #include <linux/compiler.h>
ian@0 98 #include <linux/cpuset.h>
ian@0 99 #include <linux/seq_file.h>
ian@0 100 #include <linux/notifier.h>
ian@0 101 #include <linux/kallsyms.h>
ian@0 102 #include <linux/cpu.h>
ian@0 103 #include <linux/sysctl.h>
ian@0 104 #include <linux/module.h>
ian@0 105 #include <linux/rcupdate.h>
ian@0 106 #include <linux/string.h>
ian@0 107 #include <linux/nodemask.h>
ian@0 108 #include <linux/mempolicy.h>
ian@0 109 #include <linux/mutex.h>
ian@0 110 #include <linux/rtmutex.h>
ian@0 111
ian@0 112 #include <asm/uaccess.h>
ian@0 113 #include <asm/cacheflush.h>
ian@0 114 #include <asm/tlbflush.h>
ian@0 115 #include <asm/page.h>
ian@0 116
ian@0 117 /*
ian@0 118 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_DEBUG_INITIAL,
ian@0 119 * SLAB_RED_ZONE & SLAB_POISON.
ian@0 120 * 0 for faster, smaller code (especially in the critical paths).
ian@0 121 *
ian@0 122 * STATS - 1 to collect stats for /proc/slabinfo.
ian@0 123 * 0 for faster, smaller code (especially in the critical paths).
ian@0 124 *
ian@0 125 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
ian@0 126 */
ian@0 127
ian@0 128 #ifdef CONFIG_DEBUG_SLAB
ian@0 129 #define DEBUG 1
ian@0 130 #define STATS 1
ian@0 131 #define FORCED_DEBUG 1
ian@0 132 #else
ian@0 133 #define DEBUG 0
ian@0 134 #define STATS 0
ian@0 135 #define FORCED_DEBUG 0
ian@0 136 #endif
ian@0 137
ian@0 138 /* Shouldn't this be in a header file somewhere? */
ian@0 139 #define BYTES_PER_WORD sizeof(void *)
ian@0 140
ian@0 141 #ifndef cache_line_size
ian@0 142 #define cache_line_size() L1_CACHE_BYTES
ian@0 143 #endif
ian@0 144
ian@0 145 #ifndef ARCH_KMALLOC_MINALIGN
ian@0 146 /*
ian@0 147 * Enforce a minimum alignment for the kmalloc caches.
ian@0 148 * Usually, the kmalloc caches are cache_line_size() aligned, except when
ian@0 149 * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned.
ian@0 150 * Some archs want to perform DMA into kmalloc caches and need a guaranteed
ian@0 151 * alignment larger than BYTES_PER_WORD. ARCH_KMALLOC_MINALIGN allows that.
ian@0 152 * Note that this flag disables some debug features.
ian@0 153 */
ian@0 154 #define ARCH_KMALLOC_MINALIGN 0
ian@0 155 #endif
ian@0 156
ian@0 157 #ifndef ARCH_SLAB_MINALIGN
ian@0 158 /*
ian@0 159 * Enforce a minimum alignment for all caches.
ian@0 160 * Intended for archs that get misalignment faults even for BYTES_PER_WORD
ian@0 161 * aligned buffers. Includes ARCH_KMALLOC_MINALIGN.
ian@0 162 * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables
ian@0 163 * some debug features.
ian@0 164 */
ian@0 165 #define ARCH_SLAB_MINALIGN 0
ian@0 166 #endif
ian@0 167
ian@0 168 #ifndef ARCH_KMALLOC_FLAGS
ian@0 169 #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
ian@0 170 #endif
ian@0 171
ian@0 172 /* Legal flag mask for kmem_cache_create(). */
ian@0 173 #if DEBUG
ian@0 174 # define CREATE_MASK (SLAB_DEBUG_INITIAL | SLAB_RED_ZONE | \
ian@0 175 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
ian@0 176 SLAB_CACHE_DMA | \
ian@0 177 SLAB_MUST_HWCACHE_ALIGN | SLAB_STORE_USER | \
ian@0 178 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
ian@0 179 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD)
ian@0 180 #else
ian@0 181 # define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
ian@0 182 SLAB_CACHE_DMA | SLAB_MUST_HWCACHE_ALIGN | \
ian@0 183 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
ian@0 184 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD)
ian@0 185 #endif
ian@0 186
ian@0 187 /*
ian@0 188 * kmem_bufctl_t:
ian@0 189 *
ian@0 190 * Bufctl's are used for linking objs within a slab
ian@0 191 * linked offsets.
ian@0 192 *
ian@0 193 * This implementation relies on "struct page" for locating the cache &
ian@0 194 * slab an object belongs to.
ian@0 195 * This allows the bufctl structure to be small (one int), but limits
ian@0 196 * the number of objects a slab (not a cache) can contain when off-slab
ian@0 197 * bufctls are used. The limit is the size of the largest general cache
ian@0 198 * that does not use off-slab slabs.
ian@0 199 * For 32bit archs with 4 kB pages, is this 56.
ian@0 200 * This is not serious, as it is only for large objects, when it is unwise
ian@0 201 * to have too many per slab.
ian@0 202 * Note: This limit can be raised by introducing a general cache whose size
ian@0 203 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
ian@0 204 */
ian@0 205
ian@0 206 typedef unsigned int kmem_bufctl_t;
ian@0 207 #define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
ian@0 208 #define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
ian@0 209 #define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
ian@0 210 #define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
ian@0 211
ian@0 212 /*
ian@0 213 * struct slab
ian@0 214 *
ian@0 215 * Manages the objs in a slab. Placed either at the beginning of mem allocated
ian@0 216 * for a slab, or allocated from an general cache.
ian@0 217 * Slabs are chained into three list: fully used, partial, fully free slabs.
ian@0 218 */
ian@0 219 struct slab {
ian@0 220 struct list_head list;
ian@0 221 unsigned long colouroff;
ian@0 222 void *s_mem; /* including colour offset */
ian@0 223 unsigned int inuse; /* num of objs active in slab */
ian@0 224 kmem_bufctl_t free;
ian@0 225 unsigned short nodeid;
ian@0 226 };
ian@0 227
ian@0 228 /*
ian@0 229 * struct slab_rcu
ian@0 230 *
ian@0 231 * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
ian@0 232 * arrange for kmem_freepages to be called via RCU. This is useful if
ian@0 233 * we need to approach a kernel structure obliquely, from its address
ian@0 234 * obtained without the usual locking. We can lock the structure to
ian@0 235 * stabilize it and check it's still at the given address, only if we
ian@0 236 * can be sure that the memory has not been meanwhile reused for some
ian@0 237 * other kind of object (which our subsystem's lock might corrupt).
ian@0 238 *
ian@0 239 * rcu_read_lock before reading the address, then rcu_read_unlock after
ian@0 240 * taking the spinlock within the structure expected at that address.
ian@0 241 *
ian@0 242 * We assume struct slab_rcu can overlay struct slab when destroying.
ian@0 243 */
ian@0 244 struct slab_rcu {
ian@0 245 struct rcu_head head;
ian@0 246 struct kmem_cache *cachep;
ian@0 247 void *addr;
ian@0 248 };
ian@0 249
ian@0 250 /*
ian@0 251 * struct array_cache
ian@0 252 *
ian@0 253 * Purpose:
ian@0 254 * - LIFO ordering, to hand out cache-warm objects from _alloc
ian@0 255 * - reduce the number of linked list operations
ian@0 256 * - reduce spinlock operations
ian@0 257 *
ian@0 258 * The limit is stored in the per-cpu structure to reduce the data cache
ian@0 259 * footprint.
ian@0 260 *
ian@0 261 */
ian@0 262 struct array_cache {
ian@0 263 unsigned int avail;
ian@0 264 unsigned int limit;
ian@0 265 unsigned int batchcount;
ian@0 266 unsigned int touched;
ian@0 267 spinlock_t lock;
ian@0 268 void *entry[0]; /*
ian@0 269 * Must have this definition in here for the proper
ian@0 270 * alignment of array_cache. Also simplifies accessing
ian@0 271 * the entries.
ian@0 272 * [0] is for gcc 2.95. It should really be [].
ian@0 273 */
ian@0 274 };
ian@0 275
ian@0 276 /*
ian@0 277 * bootstrap: The caches do not work without cpuarrays anymore, but the
ian@0 278 * cpuarrays are allocated from the generic caches...
ian@0 279 */
ian@0 280 #define BOOT_CPUCACHE_ENTRIES 1
ian@0 281 struct arraycache_init {
ian@0 282 struct array_cache cache;
ian@0 283 void *entries[BOOT_CPUCACHE_ENTRIES];
ian@0 284 };
ian@0 285
ian@0 286 /*
ian@0 287 * The slab lists for all objects.
ian@0 288 */
ian@0 289 struct kmem_list3 {
ian@0 290 struct list_head slabs_partial; /* partial list first, better asm code */
ian@0 291 struct list_head slabs_full;
ian@0 292 struct list_head slabs_free;
ian@0 293 unsigned long free_objects;
ian@0 294 unsigned int free_limit;
ian@0 295 unsigned int colour_next; /* Per-node cache coloring */
ian@0 296 spinlock_t list_lock;
ian@0 297 struct array_cache *shared; /* shared per node */
ian@0 298 struct array_cache **alien; /* on other nodes */
ian@0 299 unsigned long next_reap; /* updated without locking */
ian@0 300 int free_touched; /* updated without locking */
ian@0 301 };
ian@0 302
ian@0 303 /*
ian@0 304 * Need this for bootstrapping a per node allocator.
ian@0 305 */
ian@0 306 #define NUM_INIT_LISTS (2 * MAX_NUMNODES + 1)
ian@0 307 struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
ian@0 308 #define CACHE_CACHE 0
ian@0 309 #define SIZE_AC 1
ian@0 310 #define SIZE_L3 (1 + MAX_NUMNODES)
ian@0 311
ian@0 312 static int drain_freelist(struct kmem_cache *cache,
ian@0 313 struct kmem_list3 *l3, int tofree);
ian@0 314 static void free_block(struct kmem_cache *cachep, void **objpp, int len,
ian@0 315 int node);
ian@0 316 static void enable_cpucache(struct kmem_cache *cachep);
ian@0 317 static void cache_reap(void *unused);
ian@0 318
ian@0 319 /*
ian@0 320 * This function must be completely optimized away if a constant is passed to
ian@0 321 * it. Mostly the same as what is in linux/slab.h except it returns an index.
ian@0 322 */
ian@0 323 static __always_inline int index_of(const size_t size)
ian@0 324 {
ian@0 325 extern void __bad_size(void);
ian@0 326
ian@0 327 if (__builtin_constant_p(size)) {
ian@0 328 int i = 0;
ian@0 329
ian@0 330 #define CACHE(x) \
ian@0 331 if (size <=x) \
ian@0 332 return i; \
ian@0 333 else \
ian@0 334 i++;
ian@0 335 #include "linux/kmalloc_sizes.h"
ian@0 336 #undef CACHE
ian@0 337 __bad_size();
ian@0 338 } else
ian@0 339 __bad_size();
ian@0 340 return 0;
ian@0 341 }
ian@0 342
ian@0 343 static int slab_early_init = 1;
ian@0 344
ian@0 345 #define INDEX_AC index_of(sizeof(struct arraycache_init))
ian@0 346 #define INDEX_L3 index_of(sizeof(struct kmem_list3))
ian@0 347
ian@0 348 static void kmem_list3_init(struct kmem_list3 *parent)
ian@0 349 {
ian@0 350 INIT_LIST_HEAD(&parent->slabs_full);
ian@0 351 INIT_LIST_HEAD(&parent->slabs_partial);
ian@0 352 INIT_LIST_HEAD(&parent->slabs_free);
ian@0 353 parent->shared = NULL;
ian@0 354 parent->alien = NULL;
ian@0 355 parent->colour_next = 0;
ian@0 356 spin_lock_init(&parent->list_lock);
ian@0 357 parent->free_objects = 0;
ian@0 358 parent->free_touched = 0;
ian@0 359 }
ian@0 360
ian@0 361 #define MAKE_LIST(cachep, listp, slab, nodeid) \
ian@0 362 do { \
ian@0 363 INIT_LIST_HEAD(listp); \
ian@0 364 list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
ian@0 365 } while (0)
ian@0 366
ian@0 367 #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
ian@0 368 do { \
ian@0 369 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
ian@0 370 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
ian@0 371 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
ian@0 372 } while (0)
ian@0 373
ian@0 374 /*
ian@0 375 * struct kmem_cache
ian@0 376 *
ian@0 377 * manages a cache.
ian@0 378 */
ian@0 379
ian@0 380 struct kmem_cache {
ian@0 381 /* 1) per-cpu data, touched during every alloc/free */
ian@0 382 struct array_cache *array[NR_CPUS];
ian@0 383 /* 2) Cache tunables. Protected by cache_chain_mutex */
ian@0 384 unsigned int batchcount;
ian@0 385 unsigned int limit;
ian@0 386 unsigned int shared;
ian@0 387
ian@0 388 unsigned int buffer_size;
ian@0 389 /* 3) touched by every alloc & free from the backend */
ian@0 390 struct kmem_list3 *nodelists[MAX_NUMNODES];
ian@0 391
ian@0 392 unsigned int flags; /* constant flags */
ian@0 393 unsigned int num; /* # of objs per slab */
ian@0 394
ian@0 395 /* 4) cache_grow/shrink */
ian@0 396 /* order of pgs per slab (2^n) */
ian@0 397 unsigned int gfporder;
ian@0 398
ian@0 399 /* force GFP flags, e.g. GFP_DMA */
ian@0 400 gfp_t gfpflags;
ian@0 401
ian@0 402 size_t colour; /* cache colouring range */
ian@0 403 unsigned int colour_off; /* colour offset */
ian@0 404 struct kmem_cache *slabp_cache;
ian@0 405 unsigned int slab_size;
ian@0 406 unsigned int dflags; /* dynamic flags */
ian@0 407
ian@0 408 /* constructor func */
ian@0 409 void (*ctor) (void *, struct kmem_cache *, unsigned long);
ian@0 410
ian@0 411 /* de-constructor func */
ian@0 412 void (*dtor) (void *, struct kmem_cache *, unsigned long);
ian@0 413
ian@0 414 /* 5) cache creation/removal */
ian@0 415 const char *name;
ian@0 416 struct list_head next;
ian@0 417
ian@0 418 /* 6) statistics */
ian@0 419 #if STATS
ian@0 420 unsigned long num_active;
ian@0 421 unsigned long num_allocations;
ian@0 422 unsigned long high_mark;
ian@0 423 unsigned long grown;
ian@0 424 unsigned long reaped;
ian@0 425 unsigned long errors;
ian@0 426 unsigned long max_freeable;
ian@0 427 unsigned long node_allocs;
ian@0 428 unsigned long node_frees;
ian@0 429 unsigned long node_overflow;
ian@0 430 atomic_t allochit;
ian@0 431 atomic_t allocmiss;
ian@0 432 atomic_t freehit;
ian@0 433 atomic_t freemiss;
ian@0 434 #endif
ian@0 435 #if DEBUG
ian@0 436 /*
ian@0 437 * If debugging is enabled, then the allocator can add additional
ian@0 438 * fields and/or padding to every object. buffer_size contains the total
ian@0 439 * object size including these internal fields, the following two
ian@0 440 * variables contain the offset to the user object and its size.
ian@0 441 */
ian@0 442 int obj_offset;
ian@0 443 int obj_size;
ian@0 444 #endif
ian@0 445 };
ian@0 446
ian@0 447 #define CFLGS_OFF_SLAB (0x80000000UL)
ian@0 448 #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
ian@0 449
ian@0 450 #define BATCHREFILL_LIMIT 16
ian@0 451 /*
ian@0 452 * Optimization question: fewer reaps means less probability for unnessary
ian@0 453 * cpucache drain/refill cycles.
ian@0 454 *
ian@0 455 * OTOH the cpuarrays can contain lots of objects,
ian@0 456 * which could lock up otherwise freeable slabs.
ian@0 457 */
ian@0 458 #define REAPTIMEOUT_CPUC (2*HZ)
ian@0 459 #define REAPTIMEOUT_LIST3 (4*HZ)
ian@0 460
ian@0 461 #if STATS
ian@0 462 #define STATS_INC_ACTIVE(x) ((x)->num_active++)
ian@0 463 #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
ian@0 464 #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
ian@0 465 #define STATS_INC_GROWN(x) ((x)->grown++)
ian@0 466 #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
ian@0 467 #define STATS_SET_HIGH(x) \
ian@0 468 do { \
ian@0 469 if ((x)->num_active > (x)->high_mark) \
ian@0 470 (x)->high_mark = (x)->num_active; \
ian@0 471 } while (0)
ian@0 472 #define STATS_INC_ERR(x) ((x)->errors++)
ian@0 473 #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
ian@0 474 #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
ian@0 475 #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
ian@0 476 #define STATS_SET_FREEABLE(x, i) \
ian@0 477 do { \
ian@0 478 if ((x)->max_freeable < i) \
ian@0 479 (x)->max_freeable = i; \
ian@0 480 } while (0)
ian@0 481 #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
ian@0 482 #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
ian@0 483 #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
ian@0 484 #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
ian@0 485 #else
ian@0 486 #define STATS_INC_ACTIVE(x) do { } while (0)
ian@0 487 #define STATS_DEC_ACTIVE(x) do { } while (0)
ian@0 488 #define STATS_INC_ALLOCED(x) do { } while (0)
ian@0 489 #define STATS_INC_GROWN(x) do { } while (0)
ian@0 490 #define STATS_ADD_REAPED(x,y) do { } while (0)
ian@0 491 #define STATS_SET_HIGH(x) do { } while (0)
ian@0 492 #define STATS_INC_ERR(x) do { } while (0)
ian@0 493 #define STATS_INC_NODEALLOCS(x) do { } while (0)
ian@0 494 #define STATS_INC_NODEFREES(x) do { } while (0)
ian@0 495 #define STATS_INC_ACOVERFLOW(x) do { } while (0)
ian@0 496 #define STATS_SET_FREEABLE(x, i) do { } while (0)
ian@0 497 #define STATS_INC_ALLOCHIT(x) do { } while (0)
ian@0 498 #define STATS_INC_ALLOCMISS(x) do { } while (0)
ian@0 499 #define STATS_INC_FREEHIT(x) do { } while (0)
ian@0 500 #define STATS_INC_FREEMISS(x) do { } while (0)
ian@0 501 #endif
ian@0 502
ian@0 503 #if DEBUG
ian@0 504
ian@0 505 /*
ian@0 506 * memory layout of objects:
ian@0 507 * 0 : objp
ian@0 508 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
ian@0 509 * the end of an object is aligned with the end of the real
ian@0 510 * allocation. Catches writes behind the end of the allocation.
ian@0 511 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
ian@0 512 * redzone word.
ian@0 513 * cachep->obj_offset: The real object.
ian@0 514 * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
ian@0 515 * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
ian@0 516 * [BYTES_PER_WORD long]
ian@0 517 */
ian@0 518 static int obj_offset(struct kmem_cache *cachep)
ian@0 519 {
ian@0 520 return cachep->obj_offset;
ian@0 521 }
ian@0 522
ian@0 523 static int obj_size(struct kmem_cache *cachep)
ian@0 524 {
ian@0 525 return cachep->obj_size;
ian@0 526 }
ian@0 527
ian@0 528 static unsigned long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
ian@0 529 {
ian@0 530 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
ian@0 531 return (unsigned long*) (objp+obj_offset(cachep)-BYTES_PER_WORD);
ian@0 532 }
ian@0 533
ian@0 534 static unsigned long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
ian@0 535 {
ian@0 536 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
ian@0 537 if (cachep->flags & SLAB_STORE_USER)
ian@0 538 return (unsigned long *)(objp + cachep->buffer_size -
ian@0 539 2 * BYTES_PER_WORD);
ian@0 540 return (unsigned long *)(objp + cachep->buffer_size - BYTES_PER_WORD);
ian@0 541 }
ian@0 542
ian@0 543 static void **dbg_userword(struct kmem_cache *cachep, void *objp)
ian@0 544 {
ian@0 545 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
ian@0 546 return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
ian@0 547 }
ian@0 548
ian@0 549 #else
ian@0 550
ian@0 551 #define obj_offset(x) 0
ian@0 552 #define obj_size(cachep) (cachep->buffer_size)
ian@0 553 #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long *)NULL;})
ian@0 554 #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long *)NULL;})
ian@0 555 #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
ian@0 556
ian@0 557 #endif
ian@0 558
ian@0 559 /*
ian@0 560 * Maximum size of an obj (in 2^order pages) and absolute limit for the gfp
ian@0 561 * order.
ian@0 562 */
ian@0 563 #if defined(CONFIG_LARGE_ALLOCS)
ian@0 564 #define MAX_OBJ_ORDER 13 /* up to 32Mb */
ian@0 565 #define MAX_GFP_ORDER 13 /* up to 32Mb */
ian@0 566 #elif defined(CONFIG_MMU)
ian@0 567 #define MAX_OBJ_ORDER 5 /* 32 pages */
ian@0 568 #define MAX_GFP_ORDER 5 /* 32 pages */
ian@0 569 #else
ian@0 570 #define MAX_OBJ_ORDER 8 /* up to 1Mb */
ian@0 571 #define MAX_GFP_ORDER 8 /* up to 1Mb */
ian@0 572 #endif
ian@0 573
ian@0 574 /*
ian@0 575 * Do not go above this order unless 0 objects fit into the slab.
ian@0 576 */
ian@0 577 #define BREAK_GFP_ORDER_HI 1
ian@0 578 #define BREAK_GFP_ORDER_LO 0
ian@0 579 static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
ian@0 580
ian@0 581 /*
ian@0 582 * Functions for storing/retrieving the cachep and or slab from the page
ian@0 583 * allocator. These are used to find the slab an obj belongs to. With kfree(),
ian@0 584 * these are used to find the cache which an obj belongs to.
ian@0 585 */
ian@0 586 static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
ian@0 587 {
ian@0 588 page->lru.next = (struct list_head *)cache;
ian@0 589 }
ian@0 590
ian@0 591 static inline struct kmem_cache *page_get_cache(struct page *page)
ian@0 592 {
ian@0 593 if (unlikely(PageCompound(page)))
ian@0 594 page = (struct page *)page_private(page);
ian@0 595 BUG_ON(!PageSlab(page));
ian@0 596 return (struct kmem_cache *)page->lru.next;
ian@0 597 }
ian@0 598
ian@0 599 static inline void page_set_slab(struct page *page, struct slab *slab)
ian@0 600 {
ian@0 601 page->lru.prev = (struct list_head *)slab;
ian@0 602 }
ian@0 603
ian@0 604 static inline struct slab *page_get_slab(struct page *page)
ian@0 605 {
ian@0 606 if (unlikely(PageCompound(page)))
ian@0 607 page = (struct page *)page_private(page);
ian@0 608 BUG_ON(!PageSlab(page));
ian@0 609 return (struct slab *)page->lru.prev;
ian@0 610 }
ian@0 611
ian@0 612 static inline struct kmem_cache *virt_to_cache(const void *obj)
ian@0 613 {
ian@0 614 struct page *page = virt_to_page(obj);
ian@0 615 return page_get_cache(page);
ian@0 616 }
ian@0 617
ian@0 618 static inline struct slab *virt_to_slab(const void *obj)
ian@0 619 {
ian@0 620 struct page *page = virt_to_page(obj);
ian@0 621 return page_get_slab(page);
ian@0 622 }
ian@0 623
ian@0 624 static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
ian@0 625 unsigned int idx)
ian@0 626 {
ian@0 627 return slab->s_mem + cache->buffer_size * idx;
ian@0 628 }
ian@0 629
ian@0 630 static inline unsigned int obj_to_index(struct kmem_cache *cache,
ian@0 631 struct slab *slab, void *obj)
ian@0 632 {
ian@0 633 return (unsigned)(obj - slab->s_mem) / cache->buffer_size;
ian@0 634 }
ian@0 635
ian@0 636 /*
ian@0 637 * These are the default caches for kmalloc. Custom caches can have other sizes.
ian@0 638 */
ian@0 639 struct cache_sizes malloc_sizes[] = {
ian@0 640 #define CACHE(x) { .cs_size = (x) },
ian@0 641 #include <linux/kmalloc_sizes.h>
ian@0 642 CACHE(ULONG_MAX)
ian@0 643 #undef CACHE
ian@0 644 };
ian@0 645 EXPORT_SYMBOL(malloc_sizes);
ian@0 646
ian@0 647 /* Must match cache_sizes above. Out of line to keep cache footprint low. */
ian@0 648 struct cache_names {
ian@0 649 char *name;
ian@0 650 char *name_dma;
ian@0 651 };
ian@0 652
ian@0 653 static struct cache_names __initdata cache_names[] = {
ian@0 654 #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
ian@0 655 #include <linux/kmalloc_sizes.h>
ian@0 656 {NULL,}
ian@0 657 #undef CACHE
ian@0 658 };
ian@0 659
ian@0 660 static struct arraycache_init initarray_cache __initdata =
ian@0 661 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
ian@0 662 static struct arraycache_init initarray_generic =
ian@0 663 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
ian@0 664
ian@0 665 /* internal cache of cache description objs */
ian@0 666 static struct kmem_cache cache_cache = {
ian@0 667 .batchcount = 1,
ian@0 668 .limit = BOOT_CPUCACHE_ENTRIES,
ian@0 669 .shared = 1,
ian@0 670 .buffer_size = sizeof(struct kmem_cache),
ian@0 671 .name = "kmem_cache",
ian@0 672 #if DEBUG
ian@0 673 .obj_size = sizeof(struct kmem_cache),
ian@0 674 #endif
ian@0 675 };
ian@0 676
ian@0 677 #ifdef CONFIG_LOCKDEP
ian@0 678
ian@0 679 /*
ian@0 680 * Slab sometimes uses the kmalloc slabs to store the slab headers
ian@0 681 * for other slabs "off slab".
ian@0 682 * The locking for this is tricky in that it nests within the locks
ian@0 683 * of all other slabs in a few places; to deal with this special
ian@0 684 * locking we put on-slab caches into a separate lock-class.
ian@0 685 */
ian@0 686 static struct lock_class_key on_slab_key;
ian@0 687
ian@0 688 static inline void init_lock_keys(struct cache_sizes *s)
ian@0 689 {
ian@0 690 int q;
ian@0 691
ian@0 692 for (q = 0; q < MAX_NUMNODES; q++) {
ian@0 693 if (!s->cs_cachep->nodelists[q] || OFF_SLAB(s->cs_cachep))
ian@0 694 continue;
ian@0 695 lockdep_set_class(&s->cs_cachep->nodelists[q]->list_lock,
ian@0 696 &on_slab_key);
ian@0 697 }
ian@0 698 }
ian@0 699
ian@0 700 #else
ian@0 701 static inline void init_lock_keys(struct cache_sizes *s)
ian@0 702 {
ian@0 703 }
ian@0 704 #endif
ian@0 705
ian@0 706
ian@0 707
ian@0 708 /* Guard access to the cache-chain. */
ian@0 709 static DEFINE_MUTEX(cache_chain_mutex);
ian@0 710 static struct list_head cache_chain;
ian@0 711
ian@0 712 /*
ian@0 713 * vm_enough_memory() looks at this to determine how many slab-allocated pages
ian@0 714 * are possibly freeable under pressure
ian@0 715 *
ian@0 716 * SLAB_RECLAIM_ACCOUNT turns this on per-slab
ian@0 717 */
ian@0 718 atomic_t slab_reclaim_pages;
ian@0 719
ian@0 720 /*
ian@0 721 * chicken and egg problem: delay the per-cpu array allocation
ian@0 722 * until the general caches are up.
ian@0 723 */
ian@0 724 static enum {
ian@0 725 NONE,
ian@0 726 PARTIAL_AC,
ian@0 727 PARTIAL_L3,
ian@0 728 FULL
ian@0 729 } g_cpucache_up;
ian@0 730
ian@0 731 /*
ian@0 732 * used by boot code to determine if it can use slab based allocator
ian@0 733 */
ian@0 734 int slab_is_available(void)
ian@0 735 {
ian@0 736 return g_cpucache_up == FULL;
ian@0 737 }
ian@0 738
ian@0 739 static DEFINE_PER_CPU(struct work_struct, reap_work);
ian@0 740
ian@0 741 static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
ian@0 742 {
ian@0 743 return cachep->array[smp_processor_id()];
ian@0 744 }
ian@0 745
ian@0 746 static inline struct kmem_cache *__find_general_cachep(size_t size,
ian@0 747 gfp_t gfpflags)
ian@0 748 {
ian@0 749 struct cache_sizes *csizep = malloc_sizes;
ian@0 750
ian@0 751 #if DEBUG
ian@0 752 /* This happens if someone tries to call
ian@0 753 * kmem_cache_create(), or __kmalloc(), before
ian@0 754 * the generic caches are initialized.
ian@0 755 */
ian@0 756 BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
ian@0 757 #endif
ian@0 758 while (size > csizep->cs_size)
ian@0 759 csizep++;
ian@0 760
ian@0 761 /*
ian@0 762 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
ian@0 763 * has cs_{dma,}cachep==NULL. Thus no special case
ian@0 764 * for large kmalloc calls required.
ian@0 765 */
ian@0 766 if (unlikely(gfpflags & GFP_DMA))
ian@0 767 return csizep->cs_dmacachep;
ian@0 768 return csizep->cs_cachep;
ian@0 769 }
ian@0 770
ian@0 771 struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
ian@0 772 {
ian@0 773 return __find_general_cachep(size, gfpflags);
ian@0 774 }
ian@0 775 EXPORT_SYMBOL(kmem_find_general_cachep);
ian@0 776
ian@0 777 static size_t slab_mgmt_size(size_t nr_objs, size_t align)
ian@0 778 {
ian@0 779 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
ian@0 780 }
ian@0 781
ian@0 782 /*
ian@0 783 * Calculate the number of objects and left-over bytes for a given buffer size.
ian@0 784 */
ian@0 785 static void cache_estimate(unsigned long gfporder, size_t buffer_size,
ian@0 786 size_t align, int flags, size_t *left_over,
ian@0 787 unsigned int *num)
ian@0 788 {
ian@0 789 int nr_objs;
ian@0 790 size_t mgmt_size;
ian@0 791 size_t slab_size = PAGE_SIZE << gfporder;
ian@0 792
ian@0 793 /*
ian@0 794 * The slab management structure can be either off the slab or
ian@0 795 * on it. For the latter case, the memory allocated for a
ian@0 796 * slab is used for:
ian@0 797 *
ian@0 798 * - The struct slab
ian@0 799 * - One kmem_bufctl_t for each object
ian@0 800 * - Padding to respect alignment of @align
ian@0 801 * - @buffer_size bytes for each object
ian@0 802 *
ian@0 803 * If the slab management structure is off the slab, then the
ian@0 804 * alignment will already be calculated into the size. Because
ian@0 805 * the slabs are all pages aligned, the objects will be at the
ian@0 806 * correct alignment when allocated.
ian@0 807 */
ian@0 808 if (flags & CFLGS_OFF_SLAB) {
ian@0 809 mgmt_size = 0;
ian@0 810 nr_objs = slab_size / buffer_size;
ian@0 811
ian@0 812 if (nr_objs > SLAB_LIMIT)
ian@0 813 nr_objs = SLAB_LIMIT;
ian@0 814 } else {
ian@0 815 /*
ian@0 816 * Ignore padding for the initial guess. The padding
ian@0 817 * is at most @align-1 bytes, and @buffer_size is at
ian@0 818 * least @align. In the worst case, this result will
ian@0 819 * be one greater than the number of objects that fit
ian@0 820 * into the memory allocation when taking the padding
ian@0 821 * into account.
ian@0 822 */
ian@0 823 nr_objs = (slab_size - sizeof(struct slab)) /
ian@0 824 (buffer_size + sizeof(kmem_bufctl_t));
ian@0 825
ian@0 826 /*
ian@0 827 * This calculated number will be either the right
ian@0 828 * amount, or one greater than what we want.
ian@0 829 */
ian@0 830 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
ian@0 831 > slab_size)
ian@0 832 nr_objs--;
ian@0 833
ian@0 834 if (nr_objs > SLAB_LIMIT)
ian@0 835 nr_objs = SLAB_LIMIT;
ian@0 836
ian@0 837 mgmt_size = slab_mgmt_size(nr_objs, align);
ian@0 838 }
ian@0 839 *num = nr_objs;
ian@0 840 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
ian@0 841 }
ian@0 842
ian@0 843 #define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
ian@0 844
ian@0 845 static void __slab_error(const char *function, struct kmem_cache *cachep,
ian@0 846 char *msg)
ian@0 847 {
ian@0 848 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
ian@0 849 function, cachep->name, msg);
ian@0 850 dump_stack();
ian@0 851 }
ian@0 852
ian@0 853 #ifdef CONFIG_NUMA
ian@0 854 /*
ian@0 855 * Special reaping functions for NUMA systems called from cache_reap().
ian@0 856 * These take care of doing round robin flushing of alien caches (containing
ian@0 857 * objects freed on different nodes from which they were allocated) and the
ian@0 858 * flushing of remote pcps by calling drain_node_pages.
ian@0 859 */
ian@0 860 static DEFINE_PER_CPU(unsigned long, reap_node);
ian@0 861
ian@0 862 static void init_reap_node(int cpu)
ian@0 863 {
ian@0 864 int node;
ian@0 865
ian@0 866 node = next_node(cpu_to_node(cpu), node_online_map);
ian@0 867 if (node == MAX_NUMNODES)
ian@0 868 node = first_node(node_online_map);
ian@0 869
ian@240 870 per_cpu(reap_node, cpu) = node;
ian@0 871 }
ian@0 872
ian@0 873 static void next_reap_node(void)
ian@0 874 {
ian@0 875 int node = __get_cpu_var(reap_node);
ian@0 876
ian@0 877 /*
ian@0 878 * Also drain per cpu pages on remote zones
ian@0 879 */
ian@0 880 if (node != numa_node_id())
ian@0 881 drain_node_pages(node);
ian@0 882
ian@0 883 node = next_node(node, node_online_map);
ian@0 884 if (unlikely(node >= MAX_NUMNODES))
ian@0 885 node = first_node(node_online_map);
ian@0 886 __get_cpu_var(reap_node) = node;
ian@0 887 }
ian@0 888
ian@0 889 #else
ian@0 890 #define init_reap_node(cpu) do { } while (0)
ian@0 891 #define next_reap_node(void) do { } while (0)
ian@0 892 #endif
ian@0 893
ian@0 894 /*
ian@0 895 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
ian@0 896 * via the workqueue/eventd.
ian@0 897 * Add the CPU number into the expiration time to minimize the possibility of
ian@0 898 * the CPUs getting into lockstep and contending for the global cache chain
ian@0 899 * lock.
ian@0 900 */
ian@0 901 static void __devinit start_cpu_timer(int cpu)
ian@0 902 {
ian@0 903 struct work_struct *reap_work = &per_cpu(reap_work, cpu);
ian@0 904
ian@0 905 /*
ian@0 906 * When this gets called from do_initcalls via cpucache_init(),
ian@0 907 * init_workqueues() has already run, so keventd will be setup
ian@0 908 * at that time.
ian@0 909 */
ian@0 910 if (keventd_up() && reap_work->func == NULL) {
ian@0 911 init_reap_node(cpu);
ian@0 912 INIT_WORK(reap_work, cache_reap, NULL);
ian@0 913 schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
ian@0 914 }
ian@0 915 }
ian@0 916
ian@0 917 static struct array_cache *alloc_arraycache(int node, int entries,
ian@0 918 int batchcount)
ian@0 919 {
ian@0 920 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
ian@0 921 struct array_cache *nc = NULL;
ian@0 922
ian@0 923 nc = kmalloc_node(memsize, GFP_KERNEL, node);
ian@0 924 if (nc) {
ian@0 925 nc->avail = 0;
ian@0 926 nc->limit = entries;
ian@0 927 nc->batchcount = batchcount;
ian@0 928 nc->touched = 0;
ian@0 929 spin_lock_init(&nc->lock);
ian@0 930 }
ian@0 931 return nc;
ian@0 932 }
ian@0 933
ian@0 934 /*
ian@0 935 * Transfer objects in one arraycache to another.
ian@0 936 * Locking must be handled by the caller.
ian@0 937 *
ian@0 938 * Return the number of entries transferred.
ian@0 939 */
ian@0 940 static int transfer_objects(struct array_cache *to,
ian@0 941 struct array_cache *from, unsigned int max)
ian@0 942 {
ian@0 943 /* Figure out how many entries to transfer */
ian@0 944 int nr = min(min(from->avail, max), to->limit - to->avail);
ian@0 945
ian@0 946 if (!nr)
ian@0 947 return 0;
ian@0 948
ian@0 949 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
ian@0 950 sizeof(void *) *nr);
ian@0 951
ian@0 952 from->avail -= nr;
ian@0 953 to->avail += nr;
ian@0 954 to->touched = 1;
ian@0 955 return nr;
ian@0 956 }
ian@0 957
ian@0 958 #ifdef CONFIG_NUMA
ian@0 959 static void *__cache_alloc_node(struct kmem_cache *, gfp_t, int);
ian@0 960 static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
ian@0 961
ian@0 962 static struct array_cache **alloc_alien_cache(int node, int limit)
ian@0 963 {
ian@0 964 struct array_cache **ac_ptr;
ian@0 965 int memsize = sizeof(void *) * MAX_NUMNODES;
ian@0 966 int i;
ian@0 967
ian@0 968 if (limit > 1)
ian@0 969 limit = 12;
ian@0 970 ac_ptr = kmalloc_node(memsize, GFP_KERNEL, node);
ian@0 971 if (ac_ptr) {
ian@0 972 for_each_node(i) {
ian@0 973 if (i == node || !node_online(i)) {
ian@0 974 ac_ptr[i] = NULL;
ian@0 975 continue;
ian@0 976 }
ian@0 977 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d);
ian@0 978 if (!ac_ptr[i]) {
ian@0 979 for (i--; i <= 0; i--)
ian@0 980 kfree(ac_ptr[i]);
ian@0 981 kfree(ac_ptr);
ian@0 982 return NULL;
ian@0 983 }
ian@0 984 }
ian@0 985 }
ian@0 986 return ac_ptr;
ian@0 987 }
ian@0 988
ian@0 989 static void free_alien_cache(struct array_cache **ac_ptr)
ian@0 990 {
ian@0 991 int i;
ian@0 992
ian@0 993 if (!ac_ptr)
ian@0 994 return;
ian@0 995 for_each_node(i)
ian@0 996 kfree(ac_ptr[i]);
ian@0 997 kfree(ac_ptr);
ian@0 998 }
ian@0 999
ian@0 1000 static void __drain_alien_cache(struct kmem_cache *cachep,
ian@0 1001 struct array_cache *ac, int node)
ian@0 1002 {
ian@0 1003 struct kmem_list3 *rl3 = cachep->nodelists[node];
ian@0 1004
ian@0 1005 if (ac->avail) {
ian@0 1006 spin_lock(&rl3->list_lock);
ian@0 1007 /*
ian@0 1008 * Stuff objects into the remote nodes shared array first.
ian@0 1009 * That way we could avoid the overhead of putting the objects
ian@0 1010 * into the free lists and getting them back later.
ian@0 1011 */
ian@0 1012 if (rl3->shared)
ian@0 1013 transfer_objects(rl3->shared, ac, ac->limit);
ian@0 1014
ian@0 1015 free_block(cachep, ac->entry, ac->avail, node);
ian@0 1016 ac->avail = 0;
ian@0 1017 spin_unlock(&rl3->list_lock);
ian@0 1018 }
ian@0 1019 }
ian@0 1020
ian@0 1021 /*
ian@0 1022 * Called from cache_reap() to regularly drain alien caches round robin.
ian@0 1023 */
ian@0 1024 static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
ian@0 1025 {
ian@0 1026 int node = __get_cpu_var(reap_node);
ian@0 1027
ian@0 1028 if (l3->alien) {
ian@0 1029 struct array_cache *ac = l3->alien[node];
ian@0 1030
ian@0 1031 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
ian@0 1032 __drain_alien_cache(cachep, ac, node);
ian@0 1033 spin_unlock_irq(&ac->lock);
ian@0 1034 }
ian@0 1035 }
ian@0 1036 }
ian@0 1037
ian@0 1038 static void drain_alien_cache(struct kmem_cache *cachep,
ian@0 1039 struct array_cache **alien)
ian@0 1040 {
ian@0 1041 int i = 0;
ian@0 1042 struct array_cache *ac;
ian@0 1043 unsigned long flags;
ian@0 1044
ian@0 1045 for_each_online_node(i) {
ian@0 1046 ac = alien[i];
ian@0 1047 if (ac) {
ian@0 1048 spin_lock_irqsave(&ac->lock, flags);
ian@0 1049 __drain_alien_cache(cachep, ac, i);
ian@0 1050 spin_unlock_irqrestore(&ac->lock, flags);
ian@0 1051 }
ian@0 1052 }
ian@0 1053 }
ian@0 1054
ian@0 1055 static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
ian@0 1056 {
ian@0 1057 struct slab *slabp = virt_to_slab(objp);
ian@0 1058 int nodeid = slabp->nodeid;
ian@0 1059 struct kmem_list3 *l3;
ian@0 1060 struct array_cache *alien = NULL;
ian@0 1061
ian@0 1062 /*
ian@0 1063 * Make sure we are not freeing a object from another node to the array
ian@0 1064 * cache on this cpu.
ian@0 1065 */
ian@0 1066 if (likely(slabp->nodeid == numa_node_id()))
ian@0 1067 return 0;
ian@0 1068
ian@0 1069 l3 = cachep->nodelists[numa_node_id()];
ian@0 1070 STATS_INC_NODEFREES(cachep);
ian@0 1071 if (l3->alien && l3->alien[nodeid]) {
ian@0 1072 alien = l3->alien[nodeid];
ian@0 1073 spin_lock(&alien->lock);
ian@0 1074 if (unlikely(alien->avail == alien->limit)) {
ian@0 1075 STATS_INC_ACOVERFLOW(cachep);
ian@0 1076 __drain_alien_cache(cachep, alien, nodeid);
ian@0 1077 }
ian@0 1078 alien->entry[alien->avail++] = objp;
ian@0 1079 spin_unlock(&alien->lock);
ian@0 1080 } else {
ian@0 1081 spin_lock(&(cachep->nodelists[nodeid])->list_lock);
ian@0 1082 free_block(cachep, &objp, 1, nodeid);
ian@0 1083 spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
ian@0 1084 }
ian@0 1085 return 1;
ian@0 1086 }
ian@0 1087
ian@0 1088 #else
ian@0 1089
ian@0 1090 #define drain_alien_cache(cachep, alien) do { } while (0)
ian@0 1091 #define reap_alien(cachep, l3) do { } while (0)
ian@0 1092
ian@0 1093 static inline struct array_cache **alloc_alien_cache(int node, int limit)
ian@0 1094 {
ian@0 1095 return (struct array_cache **) 0x01020304ul;
ian@0 1096 }
ian@0 1097
ian@0 1098 static inline void free_alien_cache(struct array_cache **ac_ptr)
ian@0 1099 {
ian@0 1100 }
ian@0 1101
ian@0 1102 static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
ian@0 1103 {
ian@0 1104 return 0;
ian@0 1105 }
ian@0 1106
ian@0 1107 #endif
ian@0 1108
ian@0 1109 static int __cpuinit cpuup_callback(struct notifier_block *nfb,
ian@0 1110 unsigned long action, void *hcpu)
ian@0 1111 {
ian@0 1112 long cpu = (long)hcpu;
ian@0 1113 struct kmem_cache *cachep;
ian@0 1114 struct kmem_list3 *l3 = NULL;
ian@0 1115 int node = cpu_to_node(cpu);
ian@0 1116 int memsize = sizeof(struct kmem_list3);
ian@0 1117
ian@0 1118 switch (action) {
ian@0 1119 case CPU_UP_PREPARE:
ian@0 1120 mutex_lock(&cache_chain_mutex);
ian@0 1121 /*
ian@0 1122 * We need to do this right in the beginning since
ian@0 1123 * alloc_arraycache's are going to use this list.
ian@0 1124 * kmalloc_node allows us to add the slab to the right
ian@0 1125 * kmem_list3 and not this cpu's kmem_list3
ian@0 1126 */
ian@0 1127
ian@0 1128 list_for_each_entry(cachep, &cache_chain, next) {
ian@0 1129 /*
ian@0 1130 * Set up the size64 kmemlist for cpu before we can
ian@0 1131 * begin anything. Make sure some other cpu on this
ian@0 1132 * node has not already allocated this
ian@0 1133 */
ian@0 1134 if (!cachep->nodelists[node]) {
ian@0 1135 l3 = kmalloc_node(memsize, GFP_KERNEL, node);
ian@0 1136 if (!l3)
ian@0 1137 goto bad;
ian@0 1138 kmem_list3_init(l3);
ian@0 1139 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
ian@0 1140 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
ian@0 1141
ian@0 1142 /*
ian@0 1143 * The l3s don't come and go as CPUs come and
ian@0 1144 * go. cache_chain_mutex is sufficient
ian@0 1145 * protection here.
ian@0 1146 */
ian@0 1147 cachep->nodelists[node] = l3;
ian@0 1148 }
ian@0 1149
ian@0 1150 spin_lock_irq(&cachep->nodelists[node]->list_lock);
ian@0 1151 cachep->nodelists[node]->free_limit =
ian@0 1152 (1 + nr_cpus_node(node)) *
ian@0 1153 cachep->batchcount + cachep->num;
ian@0 1154 spin_unlock_irq(&cachep->nodelists[node]->list_lock);
ian@0 1155 }
ian@0 1156
ian@0 1157 /*
ian@0 1158 * Now we can go ahead with allocating the shared arrays and
ian@0 1159 * array caches
ian@0 1160 */
ian@0 1161 list_for_each_entry(cachep, &cache_chain, next) {
ian@0 1162 struct array_cache *nc;
ian@0 1163 struct array_cache *shared;
ian@0 1164 struct array_cache **alien;
ian@0 1165
ian@0 1166 nc = alloc_arraycache(node, cachep->limit,
ian@0 1167 cachep->batchcount);
ian@0 1168 if (!nc)
ian@0 1169 goto bad;
ian@0 1170 shared = alloc_arraycache(node,
ian@0 1171 cachep->shared * cachep->batchcount,
ian@0 1172 0xbaadf00d);
ian@0 1173 if (!shared)
ian@0 1174 goto bad;
ian@0 1175
ian@0 1176 alien = alloc_alien_cache(node, cachep->limit);
ian@0 1177 if (!alien)
ian@0 1178 goto bad;
ian@0 1179 cachep->array[cpu] = nc;
ian@0 1180 l3 = cachep->nodelists[node];
ian@0 1181 BUG_ON(!l3);
ian@0 1182
ian@0 1183 spin_lock_irq(&l3->list_lock);
ian@0 1184 if (!l3->shared) {
ian@0 1185 /*
ian@0 1186 * We are serialised from CPU_DEAD or
ian@0 1187 * CPU_UP_CANCELLED by the cpucontrol lock
ian@0 1188 */
ian@0 1189 l3->shared = shared;
ian@0 1190 shared = NULL;
ian@0 1191 }
ian@0 1192 #ifdef CONFIG_NUMA
ian@0 1193 if (!l3->alien) {
ian@0 1194 l3->alien = alien;
ian@0 1195 alien = NULL;
ian@0 1196 }
ian@0 1197 #endif
ian@0 1198 spin_unlock_irq(&l3->list_lock);
ian@0 1199 kfree(shared);
ian@0 1200 free_alien_cache(alien);
ian@0 1201 }
ian@0 1202 mutex_unlock(&cache_chain_mutex);
ian@0 1203 break;
ian@0 1204 case CPU_ONLINE:
ian@0 1205 start_cpu_timer(cpu);
ian@0 1206 break;
ian@0 1207 #ifdef CONFIG_HOTPLUG_CPU
ian@0 1208 case CPU_DEAD:
ian@0 1209 /*
ian@0 1210 * Even if all the cpus of a node are down, we don't free the
ian@0 1211 * kmem_list3 of any cache. This to avoid a race between
ian@0 1212 * cpu_down, and a kmalloc allocation from another cpu for
ian@0 1213 * memory from the node of the cpu going down. The list3
ian@0 1214 * structure is usually allocated from kmem_cache_create() and
ian@0 1215 * gets destroyed at kmem_cache_destroy().
ian@0 1216 */
ian@0 1217 /* fall thru */
ian@0 1218 case CPU_UP_CANCELED:
ian@0 1219 mutex_lock(&cache_chain_mutex);
ian@0 1220 list_for_each_entry(cachep, &cache_chain, next) {
ian@0 1221 struct array_cache *nc;
ian@0 1222 struct array_cache *shared;
ian@0 1223 struct array_cache **alien;
ian@0 1224 cpumask_t mask;
ian@0 1225
ian@0 1226 mask = node_to_cpumask(node);
ian@0 1227 /* cpu is dead; no one can alloc from it. */
ian@0 1228 nc = cachep->array[cpu];
ian@0 1229 cachep->array[cpu] = NULL;
ian@0 1230 l3 = cachep->nodelists[node];
ian@0 1231
ian@0 1232 if (!l3)
ian@0 1233 goto free_array_cache;
ian@0 1234
ian@0 1235 spin_lock_irq(&l3->list_lock);
ian@0 1236
ian@0 1237 /* Free limit for this kmem_list3 */
ian@0 1238 l3->free_limit -= cachep->batchcount;
ian@0 1239 if (nc)
ian@0 1240 free_block(cachep, nc->entry, nc->avail, node);
ian@0 1241
ian@0 1242 if (!cpus_empty(mask)) {
ian@0 1243 spin_unlock_irq(&l3->list_lock);
ian@0 1244 goto free_array_cache;
ian@0 1245 }
ian@0 1246
ian@0 1247 shared = l3->shared;
ian@0 1248 if (shared) {
ian@0 1249 free_block(cachep, l3->shared->entry,
ian@0 1250 l3->shared->avail, node);
ian@0 1251 l3->shared = NULL;
ian@0 1252 }
ian@0 1253
ian@0 1254 alien = l3->alien;
ian@0 1255 l3->alien = NULL;
ian@0 1256
ian@0 1257 spin_unlock_irq(&l3->list_lock);
ian@0 1258
ian@0 1259 kfree(shared);
ian@0 1260 if (alien) {
ian@0 1261 drain_alien_cache(cachep, alien);
ian@0 1262 free_alien_cache(alien);
ian@0 1263 }
ian@0 1264 free_array_cache:
ian@0 1265 kfree(nc);
ian@0 1266 }
ian@0 1267 /*
ian@0 1268 * In the previous loop, all the objects were freed to
ian@0 1269 * the respective cache's slabs, now we can go ahead and
ian@0 1270 * shrink each nodelist to its limit.
ian@0 1271 */
ian@0 1272 list_for_each_entry(cachep, &cache_chain, next) {
ian@0 1273 l3 = cachep->nodelists[node];
ian@0 1274 if (!l3)
ian@0 1275 continue;
ian@0 1276 drain_freelist(cachep, l3, l3->free_objects);
ian@0 1277 }
ian@0 1278 mutex_unlock(&cache_chain_mutex);
ian@0 1279 break;
ian@0 1280 #endif
ian@0 1281 }
ian@0 1282 return NOTIFY_OK;
ian@0 1283 bad:
ian@0 1284 mutex_unlock(&cache_chain_mutex);
ian@0 1285 return NOTIFY_BAD;
ian@0 1286 }
ian@0 1287
ian@0 1288 static struct notifier_block __cpuinitdata cpucache_notifier = {
ian@0 1289 &cpuup_callback, NULL, 0
ian@0 1290 };
ian@0 1291
ian@0 1292 /*
ian@0 1293 * swap the static kmem_list3 with kmalloced memory
ian@0 1294 */
ian@0 1295 static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
ian@0 1296 int nodeid)
ian@0 1297 {
ian@0 1298 struct kmem_list3 *ptr;
ian@0 1299
ian@0 1300 BUG_ON(cachep->nodelists[nodeid] != list);
ian@0 1301 ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
ian@0 1302 BUG_ON(!ptr);
ian@0 1303
ian@0 1304 local_irq_disable();
ian@0 1305 memcpy(ptr, list, sizeof(struct kmem_list3));
ian@0 1306 /*
ian@0 1307 * Do not assume that spinlocks can be initialized via memcpy:
ian@0 1308 */
ian@0 1309 spin_lock_init(&ptr->list_lock);
ian@0 1310
ian@0 1311 MAKE_ALL_LISTS(cachep, ptr, nodeid);
ian@0 1312 cachep->nodelists[nodeid] = ptr;
ian@0 1313 local_irq_enable();
ian@0 1314 }
ian@0 1315
ian@0 1316 /*
ian@0 1317 * Initialisation. Called after the page allocator have been initialised and
ian@0 1318 * before smp_init().
ian@0 1319 */
ian@0 1320 void __init kmem_cache_init(void)
ian@0 1321 {
ian@0 1322 size_t left_over;
ian@0 1323 struct cache_sizes *sizes;
ian@0 1324 struct cache_names *names;
ian@0 1325 int i;
ian@0 1326 int order;
ian@0 1327
ian@0 1328 for (i = 0; i < NUM_INIT_LISTS; i++) {
ian@0 1329 kmem_list3_init(&initkmem_list3[i]);
ian@0 1330 if (i < MAX_NUMNODES)
ian@0 1331 cache_cache.nodelists[i] = NULL;
ian@0 1332 }
ian@0 1333
ian@0 1334 /*
ian@0 1335 * Fragmentation resistance on low memory - only use bigger
ian@0 1336 * page orders on machines with more than 32MB of memory.
ian@0 1337 */
ian@0 1338 if (num_physpages > (32 << 20) >> PAGE_SHIFT)
ian@0 1339 slab_break_gfp_order = BREAK_GFP_ORDER_HI;
ian@0 1340
ian@0 1341 /* Bootstrap is tricky, because several objects are allocated
ian@0 1342 * from caches that do not exist yet:
ian@0 1343 * 1) initialize the cache_cache cache: it contains the struct
ian@0 1344 * kmem_cache structures of all caches, except cache_cache itself:
ian@0 1345 * cache_cache is statically allocated.
ian@0 1346 * Initially an __init data area is used for the head array and the
ian@0 1347 * kmem_list3 structures, it's replaced with a kmalloc allocated
ian@0 1348 * array at the end of the bootstrap.
ian@0 1349 * 2) Create the first kmalloc cache.
ian@0 1350 * The struct kmem_cache for the new cache is allocated normally.
ian@0 1351 * An __init data area is used for the head array.
ian@0 1352 * 3) Create the remaining kmalloc caches, with minimally sized
ian@0 1353 * head arrays.
ian@0 1354 * 4) Replace the __init data head arrays for cache_cache and the first
ian@0 1355 * kmalloc cache with kmalloc allocated arrays.
ian@0 1356 * 5) Replace the __init data for kmem_list3 for cache_cache and
ian@0 1357 * the other cache's with kmalloc allocated memory.
ian@0 1358 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
ian@0 1359 */
ian@0 1360
ian@0 1361 /* 1) create the cache_cache */
ian@0 1362 INIT_LIST_HEAD(&cache_chain);
ian@0 1363 list_add(&cache_cache.next, &cache_chain);
ian@0 1364 cache_cache.colour_off = cache_line_size();
ian@0 1365 cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
ian@0 1366 cache_cache.nodelists[numa_node_id()] = &initkmem_list3[CACHE_CACHE];
ian@0 1367
ian@0 1368 cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
ian@0 1369 cache_line_size());
ian@0 1370
ian@0 1371 for (order = 0; order < MAX_ORDER; order++) {
ian@0 1372 cache_estimate(order, cache_cache.buffer_size,
ian@0 1373 cache_line_size(), 0, &left_over, &cache_cache.num);
ian@0 1374 if (cache_cache.num)
ian@0 1375 break;
ian@0 1376 }
ian@0 1377 BUG_ON(!cache_cache.num);
ian@0 1378 cache_cache.gfporder = order;
ian@0 1379 cache_cache.colour = left_over / cache_cache.colour_off;
ian@0 1380 cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
ian@0 1381 sizeof(struct slab), cache_line_size());
ian@0 1382
ian@0 1383 /* 2+3) create the kmalloc caches */
ian@0 1384 sizes = malloc_sizes;
ian@0 1385 names = cache_names;
ian@0 1386
ian@0 1387 /*
ian@0 1388 * Initialize the caches that provide memory for the array cache and the
ian@0 1389 * kmem_list3 structures first. Without this, further allocations will
ian@0 1390 * bug.
ian@0 1391 */
ian@0 1392
ian@0 1393 sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
ian@0 1394 sizes[INDEX_AC].cs_size,
ian@0 1395 ARCH_KMALLOC_MINALIGN,
ian@0 1396 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
ian@0 1397 NULL, NULL);
ian@0 1398
ian@0 1399 if (INDEX_AC != INDEX_L3) {
ian@0 1400 sizes[INDEX_L3].cs_cachep =
ian@0 1401 kmem_cache_create(names[INDEX_L3].name,
ian@0 1402 sizes[INDEX_L3].cs_size,
ian@0 1403 ARCH_KMALLOC_MINALIGN,
ian@0 1404 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
ian@0 1405 NULL, NULL);
ian@0 1406 }
ian@0 1407
ian@0 1408 slab_early_init = 0;
ian@0 1409
ian@0 1410 while (sizes->cs_size != ULONG_MAX) {
ian@0 1411 /*
ian@0 1412 * For performance, all the general caches are L1 aligned.
ian@0 1413 * This should be particularly beneficial on SMP boxes, as it
ian@0 1414 * eliminates "false sharing".
ian@0 1415 * Note for systems short on memory removing the alignment will
ian@0 1416 * allow tighter packing of the smaller caches.
ian@0 1417 */
ian@0 1418 if (!sizes->cs_cachep) {
ian@0 1419 sizes->cs_cachep = kmem_cache_create(names->name,
ian@0 1420 sizes->cs_size,
ian@0 1421 ARCH_KMALLOC_MINALIGN,
ian@0 1422 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
ian@0 1423 NULL, NULL);
ian@0 1424 }
ian@0 1425 init_lock_keys(sizes);
ian@0 1426
ian@0 1427 sizes->cs_dmacachep = kmem_cache_create(names->name_dma,
ian@0 1428 sizes->cs_size,
ian@0 1429 ARCH_KMALLOC_MINALIGN,
ian@0 1430 ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
ian@0 1431 SLAB_PANIC,
ian@0 1432 NULL, NULL);
ian@0 1433 sizes++;
ian@0 1434 names++;
ian@0 1435 }
ian@0 1436 /* 4) Replace the bootstrap head arrays */
ian@0 1437 {
ian@0 1438 struct array_cache *ptr;
ian@0 1439
ian@0 1440 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
ian@0 1441
ian@0 1442 local_irq_disable();
ian@0 1443 BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
ian@0 1444 memcpy(ptr, cpu_cache_get(&cache_cache),
ian@0 1445 sizeof(struct arraycache_init));
ian@0 1446 /*
ian@0 1447 * Do not assume that spinlocks can be initialized via memcpy:
ian@0 1448 */
ian@0 1449 spin_lock_init(&ptr->lock);
ian@0 1450
ian@0 1451 cache_cache.array[smp_processor_id()] = ptr;
ian@0 1452 local_irq_enable();
ian@0 1453
ian@0 1454 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
ian@0 1455
ian@0 1456 local_irq_disable();
ian@0 1457 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
ian@0 1458 != &initarray_generic.cache);
ian@0 1459 memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
ian@0 1460 sizeof(struct arraycache_init));
ian@0 1461 /*
ian@0 1462 * Do not assume that spinlocks can be initialized via memcpy:
ian@0 1463 */
ian@0 1464 spin_lock_init(&ptr->lock);
ian@0 1465
ian@0 1466 malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
ian@0 1467 ptr;
ian@0 1468 local_irq_enable();
ian@0 1469 }
ian@0 1470 /* 5) Replace the bootstrap kmem_list3's */
ian@0 1471 {
ian@0 1472 int node;
ian@0 1473 /* Replace the static kmem_list3 structures for the boot cpu */
ian@0 1474 init_list(&cache_cache, &initkmem_list3[CACHE_CACHE],
ian@0 1475 numa_node_id());
ian@0 1476
ian@0 1477 for_each_online_node(node) {
ian@0 1478 init_list(malloc_sizes[INDEX_AC].cs_cachep,
ian@0 1479 &initkmem_list3[SIZE_AC + node], node);
ian@0 1480
ian@0 1481 if (INDEX_AC != INDEX_L3) {
ian@0 1482 init_list(malloc_sizes[INDEX_L3].cs_cachep,
ian@0 1483 &initkmem_list3[SIZE_L3 + node],
ian@0 1484 node);
ian@0 1485 }
ian@0 1486 }
ian@0 1487 }
ian@0 1488
ian@0 1489 /* 6) resize the head arrays to their final sizes */
ian@0 1490 {
ian@0 1491 struct kmem_cache *cachep;
ian@0 1492 mutex_lock(&cache_chain_mutex);
ian@0 1493 list_for_each_entry(cachep, &cache_chain, next)
ian@0 1494 enable_cpucache(cachep);
ian@0 1495 mutex_unlock(&cache_chain_mutex);
ian@0 1496 }
ian@0 1497
ian@0 1498 /* Done! */
ian@0 1499 g_cpucache_up = FULL;
ian@0 1500
ian@0 1501 /*
ian@0 1502 * Register a cpu startup notifier callback that initializes
ian@0 1503 * cpu_cache_get for all new cpus
ian@0 1504 */
ian@0 1505 register_cpu_notifier(&cpucache_notifier);
ian@0 1506
ian@0 1507 /*
ian@0 1508 * The reap timers are started later, with a module init call: That part
ian@0 1509 * of the kernel is not yet operational.
ian@0 1510 */
ian@0 1511 }
ian@0 1512
ian@0 1513 static int __init cpucache_init(void)
ian@0 1514 {
ian@0 1515 int cpu;
ian@0 1516
ian@0 1517 /*
ian@0 1518 * Register the timers that return unneeded pages to the page allocator
ian@0 1519 */
ian@0 1520 for_each_online_cpu(cpu)
ian@0 1521 start_cpu_timer(cpu);
ian@0 1522 return 0;
ian@0 1523 }
ian@0 1524 __initcall(cpucache_init);
ian@0 1525
ian@0 1526 /*
ian@0 1527 * Interface to system's page allocator. No need to hold the cache-lock.
ian@0 1528 *
ian@0 1529 * If we requested dmaable memory, we will get it. Even if we
ian@0 1530 * did not request dmaable memory, we might get it, but that
ian@0 1531 * would be relatively rare and ignorable.
ian@0 1532 */
ian@0 1533 static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
ian@0 1534 {
ian@0 1535 struct page *page;
ian@0 1536 int nr_pages;
ian@0 1537 int i;
ian@0 1538
ian@0 1539 #ifndef CONFIG_MMU
ian@0 1540 /*
ian@0 1541 * Nommu uses slab's for process anonymous memory allocations, and thus
ian@0 1542 * requires __GFP_COMP to properly refcount higher order allocations
ian@0 1543 */
ian@0 1544 flags |= __GFP_COMP;
ian@0 1545 #endif
ian@0 1546 flags |= cachep->gfpflags;
ian@0 1547
ian@0 1548 page = alloc_pages_node(nodeid, flags, cachep->gfporder);
ian@0 1549 if (!page)
ian@0 1550 return NULL;
ian@0 1551
ian@0 1552 nr_pages = (1 << cachep->gfporder);
ian@0 1553 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
ian@0 1554 atomic_add(nr_pages, &slab_reclaim_pages);
ian@0 1555 add_zone_page_state(page_zone(page), NR_SLAB, nr_pages);
ian@0 1556 for (i = 0; i < nr_pages; i++)
ian@0 1557 __SetPageSlab(page + i);
ian@0 1558 return page_address(page);
ian@0 1559 }
ian@0 1560
ian@0 1561 /*
ian@0 1562 * Interface to system's page release.
ian@0 1563 */
ian@0 1564 static void kmem_freepages(struct kmem_cache *cachep, void *addr)
ian@0 1565 {
ian@0 1566 unsigned long i = (1 << cachep->gfporder);
ian@0 1567 struct page *page = virt_to_page(addr);
ian@0 1568 const unsigned long nr_freed = i;
ian@0 1569
ian@0 1570 sub_zone_page_state(page_zone(page), NR_SLAB, nr_freed);
ian@0 1571 while (i--) {
ian@0 1572 BUG_ON(!PageSlab(page));
ian@0 1573 __ClearPageSlab(page);
ian@0 1574 page++;
ian@0 1575 }
ian@0 1576 if (current->reclaim_state)
ian@0 1577 current->reclaim_state->reclaimed_slab += nr_freed;
ian@0 1578 free_pages((unsigned long)addr, cachep->gfporder);
ian@0 1579 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
ian@0 1580 atomic_sub(1 << cachep->gfporder, &slab_reclaim_pages);
ian@0 1581 }
ian@0 1582
ian@0 1583 static void kmem_rcu_free(struct rcu_head *head)
ian@0 1584 {
ian@0 1585 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
ian@0 1586 struct kmem_cache *cachep = slab_rcu->cachep;
ian@0 1587
ian@0 1588 kmem_freepages(cachep, slab_rcu->addr);
ian@0 1589 if (OFF_SLAB(cachep))
ian@0 1590 kmem_cache_free(cachep->slabp_cache, slab_rcu);
ian@0 1591 }
ian@0 1592
ian@0 1593 #if DEBUG
ian@0 1594
ian@0 1595 #ifdef CONFIG_DEBUG_PAGEALLOC
ian@0 1596 static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
ian@0 1597 unsigned long caller)
ian@0 1598 {
ian@0 1599 int size = obj_size(cachep);
ian@0 1600
ian@0 1601 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
ian@0 1602
ian@0 1603 if (size < 5 * sizeof(unsigned long))
ian@0 1604 return;
ian@0 1605
ian@0 1606 *addr++ = 0x12345678;
ian@0 1607 *addr++ = caller;
ian@0 1608 *addr++ = smp_processor_id();
ian@0 1609 size -= 3 * sizeof(unsigned long);
ian@0 1610 {
ian@0 1611 unsigned long *sptr = &caller;
ian@0 1612 unsigned long svalue;
ian@0 1613
ian@0 1614 while (!kstack_end(sptr)) {
ian@0 1615 svalue = *sptr++;
ian@0 1616 if (kernel_text_address(svalue)) {
ian@0 1617 *addr++ = svalue;
ian@0 1618 size -= sizeof(unsigned long);
ian@0 1619 if (size <= sizeof(unsigned long))
ian@0 1620 break;
ian@0 1621 }
ian@0 1622 }
ian@0 1623
ian@0 1624 }
ian@0 1625 *addr++ = 0x87654321;
ian@0 1626 }
ian@0 1627 #endif
ian@0 1628
ian@0 1629 static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
ian@0 1630 {
ian@0 1631 int size = obj_size(cachep);
ian@0 1632 addr = &((char *)addr)[obj_offset(cachep)];
ian@0 1633
ian@0 1634 memset(addr, val, size);
ian@0 1635 *(unsigned char *)(addr + size - 1) = POISON_END;
ian@0 1636 }
ian@0 1637
ian@0 1638 static void dump_line(char *data, int offset, int limit)
ian@0 1639 {
ian@0 1640 int i;
ian@0 1641 printk(KERN_ERR "%03x:", offset);
ian@0 1642 for (i = 0; i < limit; i++)
ian@0 1643 printk(" %02x", (unsigned char)data[offset + i]);
ian@0 1644 printk("\n");
ian@0 1645 }
ian@0 1646 #endif
ian@0 1647
ian@0 1648 #if DEBUG
ian@0 1649
ian@0 1650 static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
ian@0 1651 {
ian@0 1652 int i, size;
ian@0 1653 char *realobj;
ian@0 1654
ian@0 1655 if (cachep->flags & SLAB_RED_ZONE) {
ian@0 1656 printk(KERN_ERR "Redzone: 0x%lx/0x%lx.\n",
ian@0 1657 *dbg_redzone1(cachep, objp),
ian@0 1658 *dbg_redzone2(cachep, objp));
ian@0 1659 }
ian@0 1660
ian@0 1661 if (cachep->flags & SLAB_STORE_USER) {
ian@0 1662 printk(KERN_ERR "Last user: [<%p>]",
ian@0 1663 *dbg_userword(cachep, objp));
ian@0 1664 print_symbol("(%s)",
ian@0 1665 (unsigned long)*dbg_userword(cachep, objp));
ian@0 1666 printk("\n");
ian@0 1667 }
ian@0 1668 realobj = (char *)objp + obj_offset(cachep);
ian@0 1669 size = obj_size(cachep);
ian@0 1670 for (i = 0; i < size && lines; i += 16, lines--) {
ian@0 1671 int limit;
ian@0 1672 limit = 16;
ian@0 1673 if (i + limit > size)
ian@0 1674 limit = size - i;
ian@0 1675 dump_line(realobj, i, limit);
ian@0 1676 }
ian@0 1677 }
ian@0 1678
ian@0 1679 static void check_poison_obj(struct kmem_cache *cachep, void *objp)
ian@0 1680 {
ian@0 1681 char *realobj;
ian@0 1682 int size, i;
ian@0 1683 int lines = 0;
ian@0 1684
ian@0 1685 realobj = (char *)objp + obj_offset(cachep);
ian@0 1686 size = obj_size(cachep);
ian@0 1687
ian@0 1688 for (i = 0; i < size; i++) {
ian@0 1689 char exp = POISON_FREE;
ian@0 1690 if (i == size - 1)
ian@0 1691 exp = POISON_END;
ian@0 1692 if (realobj[i] != exp) {
ian@0 1693 int limit;
ian@0 1694 /* Mismatch ! */
ian@0 1695 /* Print header */
ian@0 1696 if (lines == 0) {
ian@0 1697 printk(KERN_ERR
ian@0 1698 "Slab corruption: start=%p, len=%d\n",
ian@0 1699 realobj, size);
ian@0 1700 print_objinfo(cachep, objp, 0);
ian@0 1701 }
ian@0 1702 /* Hexdump the affected line */
ian@0 1703 i = (i / 16) * 16;
ian@0 1704 limit = 16;
ian@0 1705 if (i + limit > size)
ian@0 1706 limit = size - i;
ian@0 1707 dump_line(realobj, i, limit);
ian@0 1708 i += 16;
ian@0 1709 lines++;
ian@0 1710 /* Limit to 5 lines */
ian@0 1711 if (lines > 5)
ian@0 1712 break;
ian@0 1713 }
ian@0 1714 }
ian@0 1715 if (lines != 0) {
ian@0 1716 /* Print some data about the neighboring objects, if they
ian@0 1717 * exist:
ian@0 1718 */
ian@0 1719 struct slab *slabp = virt_to_slab(objp);
ian@0 1720 unsigned int objnr;
ian@0 1721
ian@0 1722 objnr = obj_to_index(cachep, slabp, objp);
ian@0 1723 if (objnr) {
ian@0 1724 objp = index_to_obj(cachep, slabp, objnr - 1);
ian@0 1725 realobj = (char *)objp + obj_offset(cachep);
ian@0 1726 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
ian@0 1727 realobj, size);
ian@0 1728 print_objinfo(cachep, objp, 2);
ian@0 1729 }
ian@0 1730 if (objnr + 1 < cachep->num) {
ian@0 1731 objp = index_to_obj(cachep, slabp, objnr + 1);
ian@0 1732 realobj = (char *)objp + obj_offset(cachep);
ian@0 1733 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
ian@0 1734 realobj, size);
ian@0 1735 print_objinfo(cachep, objp, 2);
ian@0 1736 }
ian@0 1737 }
ian@0 1738 }
ian@0 1739 #endif
ian@0 1740
ian@0 1741 #if DEBUG
ian@0 1742 /**
ian@0 1743 * slab_destroy_objs - destroy a slab and its objects
ian@0 1744 * @cachep: cache pointer being destroyed
ian@0 1745 * @slabp: slab pointer being destroyed
ian@0 1746 *
ian@0 1747 * Call the registered destructor for each object in a slab that is being
ian@0 1748 * destroyed.
ian@0 1749 */
ian@0 1750 static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
ian@0 1751 {
ian@0 1752 int i;
ian@0 1753 for (i = 0; i < cachep->num; i++) {
ian@0 1754 void *objp = index_to_obj(cachep, slabp, i);
ian@0 1755
ian@0 1756 if (cachep->flags & SLAB_POISON) {
ian@0 1757 #ifdef CONFIG_DEBUG_PAGEALLOC
ian@0 1758 if (cachep->buffer_size % PAGE_SIZE == 0 &&
ian@0 1759 OFF_SLAB(cachep))
ian@0 1760 kernel_map_pages(virt_to_page(objp),
ian@0 1761 cachep->buffer_size / PAGE_SIZE, 1);
ian@0 1762 else
ian@0 1763 check_poison_obj(cachep, objp);
ian@0 1764 #else
ian@0 1765 check_poison_obj(cachep, objp);
ian@0 1766 #endif
ian@0 1767 }
ian@0 1768 if (cachep->flags & SLAB_RED_ZONE) {
ian@0 1769 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
ian@0 1770 slab_error(cachep, "start of a freed object "
ian@0 1771 "was overwritten");
ian@0 1772 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
ian@0 1773 slab_error(cachep, "end of a freed object "
ian@0 1774 "was overwritten");
ian@0 1775 }
ian@0 1776 if (cachep->dtor && !(cachep->flags & SLAB_POISON))
ian@0 1777 (cachep->dtor) (objp + obj_offset(cachep), cachep, 0);
ian@0 1778 }
ian@0 1779 }
ian@0 1780 #else
ian@0 1781 static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
ian@0 1782 {
ian@0 1783 if (cachep->dtor) {
ian@0 1784 int i;
ian@0 1785 for (i = 0; i < cachep->num; i++) {
ian@0 1786 void *objp = index_to_obj(cachep, slabp, i);
ian@0 1787 (cachep->dtor) (objp, cachep, 0);
ian@0 1788 }
ian@0 1789 }
ian@0 1790 }
ian@0 1791 #endif
ian@0 1792
ian@0 1793 /**
ian@0 1794 * slab_destroy - destroy and release all objects in a slab
ian@0 1795 * @cachep: cache pointer being destroyed
ian@0 1796 * @slabp: slab pointer being destroyed
ian@0 1797 *
ian@0 1798 * Destroy all the objs in a slab, and release the mem back to the system.
ian@0 1799 * Before calling the slab must have been unlinked from the cache. The
ian@0 1800 * cache-lock is not held/needed.
ian@0 1801 */
ian@0 1802 static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
ian@0 1803 {
ian@0 1804 void *addr = slabp->s_mem - slabp->colouroff;
ian@0 1805
ian@0 1806 slab_destroy_objs(cachep, slabp);
ian@0 1807 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
ian@0 1808 struct slab_rcu *slab_rcu;
ian@0 1809
ian@0 1810 slab_rcu = (struct slab_rcu *)slabp;
ian@0 1811 slab_rcu->cachep = cachep;
ian@0 1812 slab_rcu->addr = addr;
ian@0 1813 call_rcu(&slab_rcu->head, kmem_rcu_free);
ian@0 1814 } else {
ian@0 1815 kmem_freepages(cachep, addr);
ian@0 1816 if (OFF_SLAB(cachep))
ian@0 1817 kmem_cache_free(cachep->slabp_cache, slabp);
ian@0 1818 }
ian@0 1819 }
ian@0 1820
ian@0 1821 /*
ian@0 1822 * For setting up all the kmem_list3s for cache whose buffer_size is same as
ian@0 1823 * size of kmem_list3.
ian@0 1824 */
ian@0 1825 static void set_up_list3s(struct kmem_cache *cachep, int index)
ian@0 1826 {
ian@0 1827 int node;
ian@0 1828
ian@0 1829 for_each_online_node(node) {
ian@0 1830 cachep->nodelists[node] = &initkmem_list3[index + node];
ian@0 1831 cachep->nodelists[node]->next_reap = jiffies +
ian@0 1832 REAPTIMEOUT_LIST3 +
ian@0 1833 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
ian@0 1834 }
ian@0 1835 }
ian@0 1836
ian@0 1837 /**
ian@0 1838 * calculate_slab_order - calculate size (page order) of slabs
ian@0 1839 * @cachep: pointer to the cache that is being created
ian@0 1840 * @size: size of objects to be created in this cache.
ian@0 1841 * @align: required alignment for the objects.
ian@0 1842 * @flags: slab allocation flags
ian@0 1843 *
ian@0 1844 * Also calculates the number of objects per slab.
ian@0 1845 *
ian@0 1846 * This could be made much more intelligent. For now, try to avoid using
ian@0 1847 * high order pages for slabs. When the gfp() functions are more friendly
ian@0 1848 * towards high-order requests, this should be changed.
ian@0 1849 */
ian@0 1850 static size_t calculate_slab_order(struct kmem_cache *cachep,
ian@0 1851 size_t size, size_t align, unsigned long flags)
ian@0 1852 {
ian@0 1853 unsigned long offslab_limit;
ian@0 1854 size_t left_over = 0;
ian@0 1855 int gfporder;
ian@0 1856
ian@0 1857 for (gfporder = 0; gfporder <= MAX_GFP_ORDER; gfporder++) {
ian@0 1858 unsigned int num;
ian@0 1859 size_t remainder;
ian@0 1860
ian@0 1861 cache_estimate(gfporder, size, align, flags, &remainder, &num);
ian@0 1862 if (!num)
ian@0 1863 continue;
ian@0 1864
ian@0 1865 if (flags & CFLGS_OFF_SLAB) {
ian@0 1866 /*
ian@0 1867 * Max number of objs-per-slab for caches which
ian@0 1868 * use off-slab slabs. Needed to avoid a possible
ian@0 1869 * looping condition in cache_grow().
ian@0 1870 */
ian@0 1871 offslab_limit = size - sizeof(struct slab);
ian@0 1872 offslab_limit /= sizeof(kmem_bufctl_t);
ian@0 1873
ian@0 1874 if (num > offslab_limit)
ian@0 1875 break;
ian@0 1876 }
ian@0 1877
ian@0 1878 /* Found something acceptable - save it away */
ian@0 1879 cachep->num = num;
ian@0 1880 cachep->gfporder = gfporder;
ian@0 1881 left_over = remainder;
ian@0 1882
ian@0 1883 /*
ian@0 1884 * A VFS-reclaimable slab tends to have most allocations
ian@0 1885 * as GFP_NOFS and we really don't want to have to be allocating
ian@0 1886 * higher-order pages when we are unable to shrink dcache.
ian@0 1887 */
ian@0 1888 if (flags & SLAB_RECLAIM_ACCOUNT)
ian@0 1889 break;
ian@0 1890
ian@0 1891 /*
ian@0 1892 * Large number of objects is good, but very large slabs are
ian@0 1893 * currently bad for the gfp()s.
ian@0 1894 */
ian@0 1895 if (gfporder >= slab_break_gfp_order)
ian@0 1896 break;
ian@0 1897
ian@0 1898 /*
ian@0 1899 * Acceptable internal fragmentation?
ian@0 1900 */
ian@0 1901 if (left_over * 8 <= (PAGE_SIZE << gfporder))
ian@0 1902 break;
ian@0 1903 }
ian@0 1904 return left_over;
ian@0 1905 }
ian@0 1906
ian@0 1907 static void setup_cpu_cache(struct kmem_cache *cachep)
ian@0 1908 {
ian@0 1909 if (g_cpucache_up == FULL) {
ian@0 1910 enable_cpucache(cachep);
ian@0 1911 return;
ian@0 1912 }
ian@0 1913 if (g_cpucache_up == NONE) {
ian@0 1914 /*
ian@0 1915 * Note: the first kmem_cache_create must create the cache
ian@0 1916 * that's used by kmalloc(24), otherwise the creation of
ian@0 1917 * further caches will BUG().
ian@0 1918 */
ian@0 1919 cachep->array[smp_processor_id()] = &initarray_generic.cache;
ian@0 1920
ian@0 1921 /*
ian@0 1922 * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
ian@0 1923 * the first cache, then we need to set up all its list3s,
ian@0 1924 * otherwise the creation of further caches will BUG().
ian@0 1925 */
ian@0 1926 set_up_list3s(cachep, SIZE_AC);
ian@0 1927 if (INDEX_AC == INDEX_L3)
ian@0 1928 g_cpucache_up = PARTIAL_L3;
ian@0 1929 else
ian@0 1930 g_cpucache_up = PARTIAL_AC;
ian@0 1931 } else {
ian@0 1932 cachep->array[smp_processor_id()] =
ian@0 1933 kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
ian@0 1934
ian@0 1935 if (g_cpucache_up == PARTIAL_AC) {
ian@0 1936 set_up_list3s(cachep, SIZE_L3);
ian@0 1937 g_cpucache_up = PARTIAL_L3;
ian@0 1938 } else {
ian@0 1939 int node;
ian@0 1940 for_each_online_node(node) {
ian@0 1941 cachep->nodelists[node] =
ian@0 1942 kmalloc_node(sizeof(struct kmem_list3),
ian@0 1943 GFP_KERNEL, node);
ian@0 1944 BUG_ON(!cachep->nodelists[node]);
ian@0 1945 kmem_list3_init(cachep->nodelists[node]);
ian@0 1946 }
ian@0 1947 }
ian@0 1948 }
ian@0 1949 cachep->nodelists[numa_node_id()]->next_reap =
ian@0 1950 jiffies + REAPTIMEOUT_LIST3 +
ian@0 1951 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
ian@0 1952
ian@0 1953 cpu_cache_get(cachep)->avail = 0;
ian@0 1954 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
ian@0 1955 cpu_cache_get(cachep)->batchcount = 1;
ian@0 1956 cpu_cache_get(cachep)->touched = 0;
ian@0 1957 cachep->batchcount = 1;
ian@0 1958 cachep->limit = BOOT_CPUCACHE_ENTRIES;
ian@0 1959 }
ian@0 1960
ian@0 1961 /**
ian@0 1962 * kmem_cache_create - Create a cache.
ian@0 1963 * @name: A string which is used in /proc/slabinfo to identify this cache.
ian@0 1964 * @size: The size of objects to be created in this cache.
ian@0 1965 * @align: The required alignment for the objects.
ian@0 1966 * @flags: SLAB flags
ian@0 1967 * @ctor: A constructor for the objects.
ian@0 1968 * @dtor: A destructor for the objects.
ian@0 1969 *
ian@0 1970 * Returns a ptr to the cache on success, NULL on failure.
ian@0 1971 * Cannot be called within a int, but can be interrupted.
ian@0 1972 * The @ctor is run when new pages are allocated by the cache
ian@0 1973 * and the @dtor is run before the pages are handed back.
ian@0 1974 *
ian@0 1975 * @name must be valid until the cache is destroyed. This implies that
ian@0 1976 * the module calling this has to destroy the cache before getting unloaded.
ian@0 1977 *
ian@0 1978 * The flags are
ian@0 1979 *
ian@0 1980 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
ian@0 1981 * to catch references to uninitialised memory.
ian@0 1982 *
ian@0 1983 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
ian@0 1984 * for buffer overruns.
ian@0 1985 *
ian@0 1986 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
ian@0 1987 * cacheline. This can be beneficial if you're counting cycles as closely
ian@0 1988 * as davem.
ian@0 1989 */
ian@0 1990 struct kmem_cache *
ian@0 1991 kmem_cache_create (const char *name, size_t size, size_t align,
ian@0 1992 unsigned long flags,
ian@0 1993 void (*ctor)(void*, struct kmem_cache *, unsigned long),
ian@0 1994 void (*dtor)(void*, struct kmem_cache *, unsigned long))
ian@0 1995 {
ian@0 1996 size_t left_over, slab_size, ralign;
ian@0 1997 struct kmem_cache *cachep = NULL, *pc;
ian@0 1998
ian@0 1999 /*
ian@0 2000 * Sanity checks... these are all serious usage bugs.
ian@0 2001 */
ian@0 2002 if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
ian@0 2003 (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) {
ian@0 2004 printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__,
ian@0 2005 name);
ian@0 2006 BUG();
ian@0 2007 }
ian@0 2008
ian@0 2009 /*
ian@0 2010 * Prevent CPUs from coming and going.
ian@0 2011 * lock_cpu_hotplug() nests outside cache_chain_mutex
ian@0 2012 */
ian@0 2013 lock_cpu_hotplug();
ian@0 2014
ian@0 2015 mutex_lock(&cache_chain_mutex);
ian@0 2016
ian@0 2017 list_for_each_entry(pc, &cache_chain, next) {
ian@0 2018 mm_segment_t old_fs = get_fs();
ian@0 2019 char tmp;
ian@0 2020 int res;
ian@0 2021
ian@0 2022 /*
ian@0 2023 * This happens when the module gets unloaded and doesn't
ian@0 2024 * destroy its slab cache and no-one else reuses the vmalloc
ian@0 2025 * area of the module. Print a warning.
ian@0 2026 */
ian@0 2027 set_fs(KERNEL_DS);
ian@0 2028 res = __get_user(tmp, pc->name);
ian@0 2029 set_fs(old_fs);
ian@0 2030 if (res) {
ian@0 2031 printk("SLAB: cache with size %d has lost its name\n",
ian@0 2032 pc->buffer_size);
ian@0 2033 continue;
ian@0 2034 }
ian@0 2035
ian@0 2036 if (!strcmp(pc->name, name)) {
ian@0 2037 printk("kmem_cache_create: duplicate cache %s\n", name);
ian@0 2038 dump_stack();
ian@0 2039 goto oops;
ian@0 2040 }
ian@0 2041 }
ian@0 2042
ian@0 2043 #if DEBUG
ian@0 2044 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
ian@0 2045 if ((flags & SLAB_DEBUG_INITIAL) && !ctor) {
ian@0 2046 /* No constructor, but inital state check requested */
ian@0 2047 printk(KERN_ERR "%s: No con, but init state check "
ian@0 2048 "requested - %s\n", __FUNCTION__, name);
ian@0 2049 flags &= ~SLAB_DEBUG_INITIAL;
ian@0 2050 }
ian@0 2051 #if FORCED_DEBUG
ian@0 2052 /*
ian@0 2053 * Enable redzoning and last user accounting, except for caches with
ian@0 2054 * large objects, if the increased size would increase the object size
ian@0 2055 * above the next power of two: caches with object sizes just above a
ian@0 2056 * power of two have a significant amount of internal fragmentation.
ian@0 2057 */
ian@0 2058 if (size < 4096 || fls(size - 1) == fls(size-1 + 3 * BYTES_PER_WORD))
ian@0 2059 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
ian@0 2060 if (!(flags & SLAB_DESTROY_BY_RCU))
ian@0 2061 flags |= SLAB_POISON;
ian@0 2062 #endif
ian@0 2063 if (flags & SLAB_DESTROY_BY_RCU)
ian@0 2064 BUG_ON(flags & SLAB_POISON);
ian@0 2065 #endif
ian@0 2066 if (flags & SLAB_DESTROY_BY_RCU)
ian@0 2067 BUG_ON(dtor);
ian@0 2068
ian@0 2069 /*
ian@0 2070 * Always checks flags, a caller might be expecting debug support which
ian@0 2071 * isn't available.
ian@0 2072 */
ian@0 2073 BUG_ON(flags & ~CREATE_MASK);
ian@0 2074
ian@0 2075 /*
ian@0 2076 * Check that size is in terms of words. This is needed to avoid
ian@0 2077 * unaligned accesses for some archs when redzoning is used, and makes
ian@0 2078 * sure any on-slab bufctl's are also correctly aligned.
ian@0 2079 */
ian@0 2080 if (size & (BYTES_PER_WORD - 1)) {
ian@0 2081 size += (BYTES_PER_WORD - 1);
ian@0 2082 size &= ~(BYTES_PER_WORD - 1);
ian@0 2083 }
ian@0 2084
ian@0 2085 /* calculate the final buffer alignment: */
ian@0 2086
ian@0 2087 /* 1) arch recommendation: can be overridden for debug */
ian@0 2088 if (flags & SLAB_HWCACHE_ALIGN) {
ian@0 2089 /*
ian@0 2090 * Default alignment: as specified by the arch code. Except if
ian@0 2091 * an object is really small, then squeeze multiple objects into
ian@0 2092 * one cacheline.
ian@0 2093 */
ian@0 2094 ralign = cache_line_size();
ian@0 2095 while (size <= ralign / 2)
ian@0 2096 ralign /= 2;
ian@0 2097 } else {
ian@0 2098 ralign = BYTES_PER_WORD;
ian@0 2099 }
ian@0 2100 /* 2) arch mandated alignment: disables debug if necessary */
ian@0 2101 if (ralign < ARCH_SLAB_MINALIGN) {
ian@0 2102 ralign = ARCH_SLAB_MINALIGN;
ian@0 2103 if (ralign > BYTES_PER_WORD)
ian@0 2104 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
ian@0 2105 }
ian@0 2106 /* 3) caller mandated alignment: disables debug if necessary */
ian@0 2107 if (ralign < align) {
ian@0 2108 ralign = align;
ian@0 2109 if (ralign > BYTES_PER_WORD)
ian@0 2110 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
ian@0 2111 }
ian@0 2112 /*
ian@0 2113 * 4) Store it. Note that the debug code below can reduce
ian@0 2114 * the alignment to BYTES_PER_WORD.
ian@0 2115 */
ian@0 2116 align = ralign;
ian@0 2117
ian@0 2118 /* Get cache's description obj. */
ian@0 2119 cachep = kmem_cache_zalloc(&cache_cache, SLAB_KERNEL);
ian@0 2120 if (!cachep)
ian@0 2121 goto oops;
ian@0 2122
ian@0 2123 #if DEBUG
ian@0 2124 cachep->obj_size = size;
ian@0 2125
ian@0 2126 if (flags & SLAB_RED_ZONE) {
ian@0 2127 /* redzoning only works with word aligned caches */
ian@0 2128 align = BYTES_PER_WORD;
ian@0 2129
ian@0 2130 /* add space for red zone words */
ian@0 2131 cachep->obj_offset += BYTES_PER_WORD;
ian@0 2132 size += 2 * BYTES_PER_WORD;
ian@0 2133 }
ian@0 2134 if (flags & SLAB_STORE_USER) {
ian@0 2135 /* user store requires word alignment and
ian@0 2136 * one word storage behind the end of the real
ian@0 2137 * object.
ian@0 2138 */
ian@0 2139 align = BYTES_PER_WORD;
ian@0 2140 size += BYTES_PER_WORD;
ian@0 2141 }
ian@0 2142 #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
ian@0 2143 if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
ian@0 2144 && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) {
ian@0 2145 cachep->obj_offset += PAGE_SIZE - size;
ian@0 2146 size = PAGE_SIZE;
ian@0 2147 }
ian@0 2148 #endif
ian@0 2149 #endif
ian@0 2150
ian@0 2151 /*
ian@0 2152 * Determine if the slab management is 'on' or 'off' slab.
ian@0 2153 * (bootstrapping cannot cope with offslab caches so don't do
ian@0 2154 * it too early on.)
ian@0 2155 */
ian@0 2156 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init)
ian@0 2157 /*
ian@0 2158 * Size is large, assume best to place the slab management obj
ian@0 2159 * off-slab (should allow better packing of objs).
ian@0 2160 */
ian@0 2161 flags |= CFLGS_OFF_SLAB;
ian@0 2162
ian@0 2163 size = ALIGN(size, align);
ian@0 2164
ian@0 2165 left_over = calculate_slab_order(cachep, size, align, flags);
ian@0 2166
ian@0 2167 if (!cachep->num) {
ian@0 2168 printk("kmem_cache_create: couldn't create cache %s.\n", name);
ian@0 2169 kmem_cache_free(&cache_cache, cachep);
ian@0 2170 cachep = NULL;
ian@0 2171 goto oops;
ian@0 2172 }
ian@0 2173 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
ian@0 2174 + sizeof(struct slab), align);
ian@0 2175
ian@0 2176 /*
ian@0 2177 * If the slab has been placed off-slab, and we have enough space then
ian@0 2178 * move it on-slab. This is at the expense of any extra colouring.
ian@0 2179 */
ian@0 2180 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
ian@0 2181 flags &= ~CFLGS_OFF_SLAB;
ian@0 2182 left_over -= slab_size;
ian@0 2183 }
ian@0 2184
ian@0 2185 if (flags & CFLGS_OFF_SLAB) {
ian@0 2186 /* really off slab. No need for manual alignment */
ian@0 2187 slab_size =
ian@0 2188 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
ian@0 2189 }
ian@0 2190
ian@0 2191 cachep->colour_off = cache_line_size();
ian@0 2192 /* Offset must be a multiple of the alignment. */
ian@0 2193 if (cachep->colour_off < align)
ian@0 2194 cachep->colour_off = align;
ian@0 2195 cachep->colour = left_over / cachep->colour_off;
ian@0 2196 cachep->slab_size = slab_size;
ian@0 2197 cachep->flags = flags;
ian@0 2198 cachep->gfpflags = 0;
ian@0 2199 if (flags & SLAB_CACHE_DMA)
ian@0 2200 cachep->gfpflags |= GFP_DMA;
ian@0 2201 cachep->buffer_size = size;
ian@0 2202
ian@0 2203 if (flags & CFLGS_OFF_SLAB)
ian@0 2204 cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
ian@0 2205 cachep->ctor = ctor;
ian@0 2206 cachep->dtor = dtor;
ian@0 2207 cachep->name = name;
ian@0 2208
ian@0 2209
ian@0 2210 setup_cpu_cache(cachep);
ian@0 2211
ian@0 2212 /* cache setup completed, link it into the list */
ian@0 2213 list_add(&cachep->next, &cache_chain);
ian@0 2214 oops:
ian@0 2215 if (!cachep && (flags & SLAB_PANIC))
ian@0 2216 panic("kmem_cache_create(): failed to create slab `%s'\n",
ian@0 2217 name);
ian@0 2218 mutex_unlock(&cache_chain_mutex);
ian@0 2219 unlock_cpu_hotplug();
ian@0 2220 return cachep;
ian@0 2221 }
ian@0 2222 EXPORT_SYMBOL(kmem_cache_create);
ian@0 2223
ian@0 2224 #if DEBUG
ian@0 2225 static void check_irq_off(void)
ian@0 2226 {
ian@0 2227 BUG_ON(!irqs_disabled());
ian@0 2228 }
ian@0 2229
ian@0 2230 static void check_irq_on(void)
ian@0 2231 {
ian@0 2232 BUG_ON(irqs_disabled());
ian@0 2233 }
ian@0 2234
ian@0 2235 static void check_spinlock_acquired(struct kmem_cache *cachep)
ian@0 2236 {
ian@0 2237 #ifdef CONFIG_SMP
ian@0 2238 check_irq_off();
ian@0 2239 assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
ian@0 2240 #endif
ian@0 2241 }
ian@0 2242
ian@0 2243 static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
ian@0 2244 {
ian@0 2245 #ifdef CONFIG_SMP
ian@0 2246 check_irq_off();
ian@0 2247 assert_spin_locked(&cachep->nodelists[node]->list_lock);
ian@0 2248 #endif
ian@0 2249 }
ian@0 2250
ian@0 2251 #else
ian@0 2252 #define check_irq_off() do { } while(0)
ian@0 2253 #define check_irq_on() do { } while(0)
ian@0 2254 #define check_spinlock_acquired(x) do { } while(0)
ian@0 2255 #define check_spinlock_acquired_node(x, y) do { } while(0)
ian@0 2256 #endif
ian@0 2257
ian@0 2258 static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
ian@0 2259 struct array_cache *ac,
ian@0 2260 int force, int node);
ian@0 2261
ian@0 2262 static void do_drain(void *arg)
ian@0 2263 {
ian@0 2264 struct kmem_cache *cachep = arg;
ian@0 2265 struct array_cache *ac;
ian@0 2266 int node = numa_node_id();
ian@0 2267
ian@0 2268 check_irq_off();
ian@0 2269 ac = cpu_cache_get(cachep);
ian@0 2270 spin_lock(&cachep->nodelists[node]->list_lock);
ian@0 2271 free_block(cachep, ac->entry, ac->avail, node);
ian@0 2272 spin_unlock(&cachep->nodelists[node]->list_lock);
ian@0 2273 ac->avail = 0;
ian@0 2274 }
ian@0 2275
ian@0 2276 static void drain_cpu_caches(struct kmem_cache *cachep)
ian@0 2277 {
ian@0 2278 struct kmem_list3 *l3;
ian@0 2279 int node;
ian@0 2280
ian@0 2281 on_each_cpu(do_drain, cachep, 1, 1);
ian@0 2282 check_irq_on();
ian@0 2283 for_each_online_node(node) {
ian@0 2284 l3 = cachep->nodelists[node];
ian@0 2285 if (l3 && l3->alien)
ian@0 2286 drain_alien_cache(cachep, l3->alien);
ian@0 2287 }
ian@0 2288
ian@0 2289 for_each_online_node(node) {
ian@0 2290 l3 = cachep->nodelists[node];
ian@0 2291 if (l3)
ian@0 2292 drain_array(cachep, l3, l3->shared, 1, node);
ian@0 2293 }
ian@0 2294 }
ian@0 2295
ian@0 2296 /*
ian@0 2297 * Remove slabs from the list of free slabs.
ian@0 2298 * Specify the number of slabs to drain in tofree.
ian@0 2299 *
ian@0 2300 * Returns the actual number of slabs released.
ian@0 2301 */
ian@0 2302 static int drain_freelist(struct kmem_cache *cache,
ian@0 2303 struct kmem_list3 *l3, int tofree)
ian@0 2304 {
ian@0 2305 struct list_head *p;
ian@0 2306 int nr_freed;
ian@0 2307 struct slab *slabp;
ian@0 2308
ian@0 2309 nr_freed = 0;
ian@0 2310 while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
ian@0 2311
ian@0 2312 spin_lock_irq(&l3->list_lock);
ian@0 2313 p = l3->slabs_free.prev;
ian@0 2314 if (p == &l3->slabs_free) {
ian@0 2315 spin_unlock_irq(&l3->list_lock);
ian@0 2316 goto out;
ian@0 2317 }
ian@0 2318
ian@0 2319 slabp = list_entry(p, struct slab, list);
ian@0 2320 #if DEBUG
ian@0 2321 BUG_ON(slabp->inuse);
ian@0 2322 #endif
ian@0 2323 list_del(&slabp->list);
ian@0 2324 /*
ian@0 2325 * Safe to drop the lock. The slab is no longer linked
ian@0 2326 * to the cache.
ian@0 2327 */
ian@0 2328 l3->free_objects -= cache->num;
ian@0 2329 spin_unlock_irq(&l3->list_lock);
ian@0 2330 slab_destroy(cache, slabp);
ian@0 2331 nr_freed++;
ian@0 2332 }
ian@0 2333 out:
ian@0 2334 return nr_freed;
ian@0 2335 }
ian@0 2336
ian@0 2337 static int __cache_shrink(struct kmem_cache *cachep)
ian@0 2338 {
ian@0 2339 int ret = 0, i = 0;
ian@0 2340 struct kmem_list3 *l3;
ian@0 2341
ian@0 2342 drain_cpu_caches(cachep);
ian@0 2343
ian@0 2344 check_irq_on();
ian@0 2345 for_each_online_node(i) {
ian@0 2346 l3 = cachep->nodelists[i];
ian@0 2347 if (!l3)
ian@0 2348 continue;
ian@0 2349
ian@0 2350 drain_freelist(cachep, l3, l3->free_objects);
ian@0 2351
ian@0 2352 ret += !list_empty(&l3->slabs_full) ||
ian@0 2353 !list_empty(&l3->slabs_partial);
ian@0 2354 }
ian@0 2355 return (ret ? 1 : 0);
ian@0 2356 }
ian@0 2357
ian@0 2358 /**
ian@0 2359 * kmem_cache_shrink - Shrink a cache.
ian@0 2360 * @cachep: The cache to shrink.
ian@0 2361 *
ian@0 2362 * Releases as many slabs as possible for a cache.
ian@0 2363 * To help debugging, a zero exit status indicates all slabs were released.
ian@0 2364 */
ian@0 2365 int kmem_cache_shrink(struct kmem_cache *cachep)
ian@0 2366 {
ian@0 2367 BUG_ON(!cachep || in_interrupt());
ian@0 2368
ian@0 2369 return __cache_shrink(cachep);
ian@0 2370 }
ian@0 2371 EXPORT_SYMBOL(kmem_cache_shrink);
ian@0 2372
ian@0 2373 /**
ian@0 2374 * kmem_cache_destroy - delete a cache
ian@0 2375 * @cachep: the cache to destroy
ian@0 2376 *
ian@0 2377 * Remove a struct kmem_cache object from the slab cache.
ian@0 2378 * Returns 0 on success.
ian@0 2379 *
ian@0 2380 * It is expected this function will be called by a module when it is
ian@0 2381 * unloaded. This will remove the cache completely, and avoid a duplicate
ian@0 2382 * cache being allocated each time a module is loaded and unloaded, if the
ian@0 2383 * module doesn't have persistent in-kernel storage across loads and unloads.
ian@0 2384 *
ian@0 2385 * The cache must be empty before calling this function.
ian@0 2386 *
ian@0 2387 * The caller must guarantee that noone will allocate memory from the cache
ian@0 2388 * during the kmem_cache_destroy().
ian@0 2389 */
ian@0 2390 int kmem_cache_destroy(struct kmem_cache *cachep)
ian@0 2391 {
ian@0 2392 int i;
ian@0 2393 struct kmem_list3 *l3;
ian@0 2394
ian@0 2395 BUG_ON(!cachep || in_interrupt());
ian@0 2396
ian@0 2397 /* Don't let CPUs to come and go */
ian@0 2398 lock_cpu_hotplug();
ian@0 2399
ian@0 2400 /* Find the cache in the chain of caches. */
ian@0 2401 mutex_lock(&cache_chain_mutex);
ian@0 2402 /*
ian@0 2403 * the chain is never empty, cache_cache is never destroyed
ian@0 2404 */
ian@0 2405 list_del(&cachep->next);
ian@0 2406 mutex_unlock(&cache_chain_mutex);
ian@0 2407
ian@0 2408 if (__cache_shrink(cachep)) {
ian@0 2409 slab_error(cachep, "Can't free all objects");
ian@0 2410 mutex_lock(&cache_chain_mutex);
ian@0 2411 list_add(&cachep->next, &cache_chain);
ian@0 2412 mutex_unlock(&cache_chain_mutex);
ian@0 2413 unlock_cpu_hotplug();
ian@0 2414 return 1;
ian@0 2415 }
ian@0 2416
ian@0 2417 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
ian@0 2418 synchronize_rcu();
ian@0 2419
ian@0 2420 for_each_online_cpu(i)
ian@0 2421 kfree(cachep->array[i]);
ian@0 2422
ian@0 2423 /* NUMA: free the list3 structures */
ian@0 2424 for_each_online_node(i) {
ian@0 2425 l3 = cachep->nodelists[i];
ian@0 2426 if (l3) {
ian@0 2427 kfree(l3->shared);
ian@0 2428 free_alien_cache(l3->alien);
ian@0 2429 kfree(l3);
ian@0 2430 }
ian@0 2431 }
ian@0 2432 kmem_cache_free(&cache_cache, cachep);
ian@0 2433 unlock_cpu_hotplug();
ian@0 2434 return 0;
ian@0 2435 }
ian@0 2436 EXPORT_SYMBOL(kmem_cache_destroy);
ian@0 2437
ian@0 2438 /* Get the memory for a slab management obj. */
ian@0 2439 static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
ian@0 2440 int colour_off, gfp_t local_flags,
ian@0 2441 int nodeid)
ian@0 2442 {
ian@0 2443 struct slab *slabp;
ian@0 2444
ian@0 2445 if (OFF_SLAB(cachep)) {
ian@0 2446 /* Slab management obj is off-slab. */
ian@0 2447 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
ian@0 2448 local_flags, nodeid);
ian@0 2449 if (!slabp)
ian@0 2450 return NULL;
ian@0 2451 } else {
ian@0 2452 slabp = objp + colour_off;
ian@0 2453 colour_off += cachep->slab_size;
ian@0 2454 }
ian@0 2455 slabp->inuse = 0;
ian@0 2456 slabp->colouroff = colour_off;
ian@0 2457 slabp->s_mem = objp + colour_off;
ian@0 2458 slabp->nodeid = nodeid;
ian@0 2459 return slabp;
ian@0 2460 }
ian@0 2461
ian@0 2462 static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
ian@0 2463 {
ian@0 2464 return (kmem_bufctl_t *) (slabp + 1);
ian@0 2465 }
ian@0 2466
ian@0 2467 static void cache_init_objs(struct kmem_cache *cachep,
ian@0 2468 struct slab *slabp, unsigned long ctor_flags)
ian@0 2469 {
ian@0 2470 int i;
ian@0 2471
ian@0 2472 for (i = 0; i < cachep->num; i++) {
ian@0 2473 void *objp = index_to_obj(cachep, slabp, i);
ian@0 2474 #if DEBUG
ian@0 2475 /* need to poison the objs? */
ian@0 2476 if (cachep->flags & SLAB_POISON)
ian@0 2477 poison_obj(cachep, objp, POISON_FREE);
ian@0 2478 if (cachep->flags & SLAB_STORE_USER)
ian@0 2479 *dbg_userword(cachep, objp) = NULL;
ian@0 2480
ian@0 2481 if (cachep->flags & SLAB_RED_ZONE) {
ian@0 2482 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
ian@0 2483 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
ian@0 2484 }
ian@0 2485 /*
ian@0 2486 * Constructors are not allowed to allocate memory from the same
ian@0 2487 * cache which they are a constructor for. Otherwise, deadlock.
ian@0 2488 * They must also be threaded.
ian@0 2489 */
ian@0 2490 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
ian@0 2491 cachep->ctor(objp + obj_offset(cachep), cachep,
ian@0 2492 ctor_flags);
ian@0 2493
ian@0 2494 if (cachep->flags & SLAB_RED_ZONE) {
ian@0 2495 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
ian@0 2496 slab_error(cachep, "constructor overwrote the"
ian@0 2497 " end of an object");
ian@0 2498 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
ian@0 2499 slab_error(cachep, "constructor overwrote the"
ian@0 2500 " start of an object");
ian@0 2501 }
ian@0 2502 if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
ian@0 2503 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
ian@0 2504 kernel_map_pages(virt_to_page(objp),
ian@0 2505 cachep->buffer_size / PAGE_SIZE, 0);
ian@0 2506 #else
ian@0 2507 if (cachep->ctor)
ian@0 2508 cachep->ctor(objp, cachep, ctor_flags);
ian@0 2509 #endif
ian@0 2510 slab_bufctl(slabp)[i] = i + 1;
ian@0 2511 }
ian@0 2512 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
ian@0 2513 slabp->free = 0;
ian@0 2514 }
ian@0 2515
ian@0 2516 static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
ian@0 2517 {
ian@0 2518 if (flags & SLAB_DMA)
ian@0 2519 BUG_ON(!(cachep->gfpflags & GFP_DMA));
ian@0 2520 else
ian@0 2521 BUG_ON(cachep->gfpflags & GFP_DMA);
ian@0 2522 }
ian@0 2523
ian@0 2524 static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
ian@0 2525 int nodeid)
ian@0 2526 {
ian@0 2527 void *objp = index_to_obj(cachep, slabp, slabp->free);
ian@0 2528 kmem_bufctl_t next;
ian@0 2529
ian@0 2530 slabp->inuse++;
ian@0 2531 next = slab_bufctl(slabp)[slabp->free];
ian@0 2532 #if DEBUG
ian@0 2533 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
ian@0 2534 WARN_ON(slabp->nodeid != nodeid);
ian@0 2535 #endif
ian@0 2536 slabp->free = next;
ian@0 2537
ian@0 2538 return objp;
ian@0 2539 }
ian@0 2540
ian@0 2541 static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
ian@0 2542 void *objp, int nodeid)
ian@0 2543 {
ian@0 2544 unsigned int objnr = obj_to_index(cachep, slabp, objp);
ian@0 2545
ian@0 2546 #if DEBUG
ian@0 2547 /* Verify that the slab belongs to the intended node */
ian@0 2548 WARN_ON(slabp->nodeid != nodeid);
ian@0 2549
ian@0 2550 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
ian@0 2551 printk(KERN_ERR "slab: double free detected in cache "
ian@0 2552 "'%s', objp %p\n", cachep->name, objp);
ian@0 2553 BUG();
ian@0 2554 }
ian@0 2555 #endif
ian@0 2556 slab_bufctl(slabp)[objnr] = slabp->free;
ian@0 2557 slabp->free = objnr;
ian@0 2558 slabp->inuse--;
ian@0 2559 }
ian@0 2560
ian@0 2561 /*
ian@0 2562 * Map pages beginning at addr to the given cache and slab. This is required
ian@0 2563 * for the slab allocator to be able to lookup the cache and slab of a
ian@0 2564 * virtual address for kfree, ksize, kmem_ptr_validate, and slab debugging.
ian@0 2565 */
ian@0 2566 static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
ian@0 2567 void *addr)
ian@0 2568 {
ian@0 2569 int nr_pages;
ian@0 2570 struct page *page;
ian@0 2571
ian@0 2572 page = virt_to_page(addr);
ian@0 2573
ian@0 2574 nr_pages = 1;
ian@0 2575 if (likely(!PageCompound(page)))
ian@0 2576 nr_pages <<= cache->gfporder;
ian@0 2577
ian@0 2578 do {
ian@0 2579 page_set_cache(page, cache);
ian@0 2580 page_set_slab(page, slab);
ian@0 2581 page++;
ian@0 2582 } while (--nr_pages);
ian@0 2583 }
ian@0 2584
ian@0 2585 /*
ian@0 2586 * Grow (by 1) the number of slabs within a cache. This is called by
ian@0 2587 * kmem_cache_alloc() when there are no active objs left in a cache.
ian@0 2588 */
ian@0 2589 static int cache_grow(struct kmem_cache *cachep, gfp_t flags, int nodeid)
ian@0 2590 {
ian@0 2591 struct slab *slabp;
ian@0 2592 void *objp;
ian@0 2593 size_t offset;
ian@0 2594 gfp_t local_flags;
ian@0 2595 unsigned long ctor_flags;
ian@0 2596 struct kmem_list3 *l3;
ian@0 2597
ian@0 2598 /*
ian@0 2599 * Be lazy and only check for valid flags here, keeping it out of the
ian@0 2600 * critical path in kmem_cache_alloc().
ian@0 2601 */
ian@0 2602 BUG_ON(flags & ~(SLAB_DMA | SLAB_LEVEL_MASK | SLAB_NO_GROW));
ian@0 2603 if (flags & SLAB_NO_GROW)
ian@0 2604 return 0;
ian@0 2605
ian@0 2606 ctor_flags = SLAB_CTOR_CONSTRUCTOR;
ian@0 2607 local_flags = (flags & SLAB_LEVEL_MASK);
ian@0 2608 if (!(local_flags & __GFP_WAIT))
ian@0 2609 /*
ian@0 2610 * Not allowed to sleep. Need to tell a constructor about
ian@0 2611 * this - it might need to know...
ian@0 2612 */
ian@0 2613 ctor_flags |= SLAB_CTOR_ATOMIC;
ian@0 2614
ian@0 2615 /* Take the l3 list lock to change the colour_next on this node */
ian@0 2616 check_irq_off();
ian@0 2617 l3 = cachep->nodelists[nodeid];
ian@0 2618 spin_lock(&l3->list_lock);
ian@0 2619
ian@0 2620 /* Get colour for the slab, and cal the next value. */
ian@0 2621 offset = l3->colour_next;
ian@0 2622 l3->colour_next++;
ian@0 2623 if (l3->colour_next >= cachep->colour)
ian@0 2624 l3->colour_next = 0;
ian@0 2625 spin_unlock(&l3->list_lock);
ian@0 2626
ian@0 2627 offset *= cachep->colour_off;
ian@0 2628
ian@0 2629 if (local_flags & __GFP_WAIT)
ian@0 2630 local_irq_enable();
ian@0 2631
ian@0 2632 /*
ian@0 2633 * The test for missing atomic flag is performed here, rather than
ian@0 2634 * the more obvious place, simply to reduce the critical path length
ian@0 2635 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
ian@0 2636 * will eventually be caught here (where it matters).
ian@0 2637 */
ian@0 2638 kmem_flagcheck(cachep, flags);
ian@0 2639
ian@0 2640 /*
ian@0 2641 * Get mem for the objs. Attempt to allocate a physical page from
ian@0 2642 * 'nodeid'.
ian@0 2643 */
ian@0 2644 objp = kmem_getpages(cachep, flags, nodeid);
ian@0 2645 if (!objp)
ian@0 2646 goto failed;
ian@0 2647
ian@0 2648 /* Get slab management. */
ian@0 2649 slabp = alloc_slabmgmt(cachep, objp, offset, local_flags, nodeid);
ian@0 2650 if (!slabp)
ian@0 2651 goto opps1;
ian@0 2652
ian@0 2653 slabp->nodeid = nodeid;
ian@0 2654 slab_map_pages(cachep, slabp, objp);
ian@0 2655
ian@0 2656 cache_init_objs(cachep, slabp, ctor_flags);
ian@0 2657
ian@0 2658 if (local_flags & __GFP_WAIT)
ian@0 2659 local_irq_disable();
ian@0 2660 check_irq_off();
ian@0 2661 spin_lock(&l3->list_lock);
ian@0 2662
ian@0 2663 /* Make slab active. */
ian@0 2664 list_add_tail(&slabp->list, &(l3->slabs_free));
ian@0 2665 STATS_INC_GROWN(cachep);
ian@0 2666 l3->free_objects += cachep->num;
ian@0 2667 spin_unlock(&l3->list_lock);
ian@0 2668 return 1;
ian@0 2669 opps1:
ian@0 2670 kmem_freepages(cachep, objp);
ian@0 2671 failed:
ian@0 2672 if (local_flags & __GFP_WAIT)
ian@0 2673 local_irq_disable();
ian@0 2674 return 0;
ian@0 2675 }
ian@0 2676
ian@0 2677 #if DEBUG
ian@0 2678
ian@0 2679 /*
ian@0 2680 * Perform extra freeing checks:
ian@0 2681 * - detect bad pointers.
ian@0 2682 * - POISON/RED_ZONE checking
ian@0 2683 * - destructor calls, for caches with POISON+dtor
ian@0 2684 */
ian@0 2685 static void kfree_debugcheck(const void *objp)
ian@0 2686 {
ian@0 2687 struct page *page;
ian@0 2688
ian@0 2689 if (!virt_addr_valid(objp)) {
ian@0 2690 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
ian@0 2691 (unsigned long)objp);
ian@0 2692 BUG();
ian@0 2693 }
ian@0 2694 page = virt_to_page(objp);
ian@0 2695 if (!PageSlab(page)) {
ian@0 2696 printk(KERN_ERR "kfree_debugcheck: bad ptr %lxh.\n",
ian@0 2697 (unsigned long)objp);
ian@0 2698 BUG();
ian@0 2699 }
ian@0 2700 }
ian@0 2701
ian@0 2702 static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
ian@0 2703 {
ian@0 2704 unsigned long redzone1, redzone2;
ian@0 2705
ian@0 2706 redzone1 = *dbg_redzone1(cache, obj);
ian@0 2707 redzone2 = *dbg_redzone2(cache, obj);
ian@0 2708
ian@0 2709 /*
ian@0 2710 * Redzone is ok.
ian@0 2711 */
ian@0 2712 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
ian@0 2713 return;
ian@0 2714
ian@0 2715 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
ian@0 2716 slab_error(cache, "double free detected");
ian@0 2717 else
ian@0 2718 slab_error(cache, "memory outside object was overwritten");
ian@0 2719
ian@0 2720 printk(KERN_ERR "%p: redzone 1:0x%lx, redzone 2:0x%lx.\n",
ian@0 2721 obj, redzone1, redzone2);
ian@0 2722 }
ian@0 2723
ian@0 2724 static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
ian@0 2725 void *caller)
ian@0 2726 {
ian@0 2727 struct page *page;
ian@0 2728 unsigned int objnr;
ian@0 2729 struct slab *slabp;
ian@0 2730
ian@0 2731 objp -= obj_offset(cachep);
ian@0 2732 kfree_debugcheck(objp);
ian@0 2733 page = virt_to_page(objp);
ian@0 2734
ian@0 2735 slabp = page_get_slab(page);
ian@0 2736
ian@0 2737 if (cachep->flags & SLAB_RED_ZONE) {
ian@0 2738 verify_redzone_free(cachep, objp);
ian@0 2739 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
ian@0 2740 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
ian@0 2741 }
ian@0 2742 if (cachep->flags & SLAB_STORE_USER)
ian@0 2743 *dbg_userword(cachep, objp) = caller;
ian@0 2744
ian@0 2745 objnr = obj_to_index(cachep, slabp, objp);
ian@0 2746
ian@0 2747 BUG_ON(objnr >= cachep->num);
ian@0 2748 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
ian@0 2749
ian@0 2750 if (cachep->flags & SLAB_DEBUG_INITIAL) {
ian@0 2751 /*
ian@0 2752 * Need to call the slab's constructor so the caller can
ian@0 2753 * perform a verify of its state (debugging). Called without
ian@0 2754 * the cache-lock held.
ian@0 2755 */
ian@0 2756 cachep->ctor(objp + obj_offset(cachep),
ian@0 2757 cachep, SLAB_CTOR_CONSTRUCTOR | SLAB_CTOR_VERIFY);
ian@0 2758 }
ian@0 2759 if (cachep->flags & SLAB_POISON && cachep->dtor) {
ian@0 2760 /* we want to cache poison the object,
ian@0 2761 * call the destruction callback
ian@0 2762 */
ian@0 2763 cachep->dtor(objp + obj_offset(cachep), cachep, 0);
ian@0 2764 }
ian@0 2765 #ifdef CONFIG_DEBUG_SLAB_LEAK
ian@0 2766 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
ian@0 2767 #endif
ian@0 2768 if (cachep->flags & SLAB_POISON) {
ian@0 2769 #ifdef CONFIG_DEBUG_PAGEALLOC
ian@0 2770 if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
ian@0 2771 store_stackinfo(cachep, objp, (unsigned long)caller);
ian@0 2772 kernel_map_pages(virt_to_page(objp),
ian@0 2773 cachep->buffer_size / PAGE_SIZE, 0);
ian@0 2774 } else {
ian@0 2775 poison_obj(cachep, objp, POISON_FREE);
ian@0 2776 }
ian@0 2777 #else
ian@0 2778 poison_obj(cachep, objp, POISON_FREE);
ian@0 2779 #endif
ian@0 2780 }
ian@0 2781 return objp;
ian@0 2782 }
ian@0 2783
ian@0 2784 static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
ian@0 2785 {
ian@0 2786 kmem_bufctl_t i;
ian@0 2787 int entries = 0;
ian@0 2788
ian@0 2789 /* Check slab's freelist to see if this obj is there. */
ian@0 2790 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
ian@0 2791 entries++;
ian@0 2792 if (entries > cachep->num || i >= cachep->num)
ian@0 2793 goto bad;
ian@0 2794 }
ian@0 2795 if (entries != cachep->num - slabp->inuse) {
ian@0 2796 bad:
ian@0 2797 printk(KERN_ERR "slab: Internal list corruption detected in "
ian@0 2798 "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
ian@0 2799 cachep->name, cachep->num, slabp, slabp->inuse);
ian@0 2800 for (i = 0;
ian@0 2801 i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t);
ian@0 2802 i++) {
ian@0 2803 if (i % 16 == 0)
ian@0 2804 printk("\n%03x:", i);
ian@0 2805 printk(" %02x", ((unsigned char *)slabp)[i]);
ian@0 2806 }
ian@0 2807 printk("\n");
ian@0 2808 BUG();
ian@0 2809 }
ian@0 2810 }
ian@0 2811 #else
ian@0 2812 #define kfree_debugcheck(x) do { } while(0)
ian@0 2813 #define cache_free_debugcheck(x,objp,z) (objp)
ian@0 2814 #define check_slabp(x,y) do { } while(0)
ian@0 2815 #endif
ian@0 2816
ian@0 2817 static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
ian@0 2818 {
ian@0 2819 int batchcount;
ian@0 2820 struct kmem_list3 *l3;
ian@0 2821 struct array_cache *ac;
ian@0 2822
ian@0 2823 check_irq_off();
ian@0 2824 ac = cpu_cache_get(cachep);
ian@0 2825 retry:
ian@0 2826 batchcount = ac->batchcount;
ian@0 2827 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
ian@0 2828 /*
ian@0 2829 * If there was little recent activity on this cache, then
ian@0 2830 * perform only a partial refill. Otherwise we could generate
ian@0 2831 * refill bouncing.
ian@0 2832 */
ian@0 2833 batchcount = BATCHREFILL_LIMIT;
ian@0 2834 }
ian@0 2835 l3 = cachep->nodelists[numa_node_id()];
ian@0 2836
ian@0 2837 BUG_ON(ac->avail > 0 || !l3);
ian@0 2838 spin_lock(&l3->list_lock);
ian@0 2839
ian@0 2840 /* See if we can refill from the shared array */
ian@0 2841 if (l3->shared && transfer_objects(ac, l3->shared, batchcount))
ian@0 2842 goto alloc_done;
ian@0 2843
ian@0 2844 while (batchcount > 0) {
ian@0 2845 struct list_head *entry;
ian@0 2846 struct slab *slabp;
ian@0 2847 /* Get slab alloc is to come from. */
ian@0 2848 entry = l3->slabs_partial.next;
ian@0 2849 if (entry == &l3->slabs_partial) {
ian@0 2850 l3->free_touched = 1;
ian@0 2851 entry = l3->slabs_free.next;
ian@0 2852 if (entry == &l3->slabs_free)
ian@0 2853 goto must_grow;
ian@0 2854 }
ian@0 2855
ian@0 2856 slabp = list_entry(entry, struct slab, list);
ian@0 2857 check_slabp(cachep, slabp);
ian@0 2858 check_spinlock_acquired(cachep);
ian@0 2859 while (slabp->inuse < cachep->num && batchcount--) {
ian@0 2860 STATS_INC_ALLOCED(cachep);
ian@0 2861 STATS_INC_ACTIVE(cachep);
ian@0 2862 STATS_SET_HIGH(cachep);
ian@0 2863
ian@0 2864 ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
ian@0 2865 numa_node_id());
ian@0 2866 }
ian@0 2867 check_slabp(cachep, slabp);
ian@0 2868
ian@0 2869 /* move slabp to correct slabp list: */
ian@0 2870 list_del(&slabp->list);
ian@0 2871 if (slabp->free == BUFCTL_END)
ian@0 2872 list_add(&slabp->list, &l3->slabs_full);
ian@0 2873 else
ian@0 2874 list_add(&slabp->list, &l3->slabs_partial);
ian@0 2875 }
ian@0 2876
ian@0 2877 must_grow:
ian@0 2878 l3->free_objects -= ac->avail;
ian@0 2879 alloc_done:
ian@0 2880 spin_unlock(&l3->list_lock);
ian@0 2881
ian@0 2882 if (unlikely(!ac->avail)) {
ian@0 2883 int x;
ian@0 2884 x = cache_grow(cachep, flags, numa_node_id());
ian@0 2885
ian@0 2886 /* cache_grow can reenable interrupts, then ac could change. */
ian@0 2887 ac = cpu_cache_get(cachep);
ian@0 2888 if (!x && ac->avail == 0) /* no objects in sight? abort */
ian@0 2889 return NULL;
ian@0 2890
ian@0 2891 if (!ac->avail) /* objects refilled by interrupt? */
ian@0 2892 goto retry;
ian@0 2893 }
ian@0 2894 ac->touched = 1;
ian@0 2895 return ac->entry[--ac->avail];
ian@0 2896 }
ian@0 2897
ian@0 2898 static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
ian@0 2899 gfp_t flags)
ian@0 2900 {
ian@0 2901 might_sleep_if(flags & __GFP_WAIT);
ian@0 2902 #if DEBUG
ian@0 2903 kmem_flagcheck(cachep, flags);
ian@0 2904 #endif
ian@0 2905 }
ian@0 2906
ian@0 2907 #if DEBUG
ian@0 2908 static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
ian@0 2909 gfp_t flags, void *objp, void *caller)
ian@0 2910 {
ian@0 2911 if (!objp)
ian@0 2912 return objp;
ian@0 2913 if (cachep->flags & SLAB_POISON) {
ian@0 2914 #ifdef CONFIG_DEBUG_PAGEALLOC
ian@0 2915 if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
ian@0 2916 kernel_map_pages(virt_to_page(objp),
ian@0 2917 cachep->buffer_size / PAGE_SIZE, 1);
ian@0 2918 else
ian@0 2919 check_poison_obj(cachep, objp);
ian@0 2920 #else
ian@0 2921 check_poison_obj(cachep, objp);
ian@0 2922 #endif
ian@0 2923 poison_obj(cachep, objp, POISON_INUSE);
ian@0 2924 }
ian@0 2925 if (cachep->flags & SLAB_STORE_USER)
ian@0 2926 *dbg_userword(cachep, objp) = caller;
ian@0 2927
ian@0 2928 if (cachep->flags & SLAB_RED_ZONE) {
ian@0 2929 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
ian@0 2930 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
ian@0 2931 slab_error(cachep, "double free, or memory outside"
ian@0 2932 " object was overwritten");
ian@0 2933 printk(KERN_ERR
ian@0 2934 "%p: redzone 1:0x%lx, redzone 2:0x%lx\n",
ian@0 2935 objp, *dbg_redzone1(cachep, objp),
ian@0 2936 *dbg_redzone2(cachep, objp));
ian@0 2937 }
ian@0 2938 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
ian@0 2939 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
ian@0 2940 }
ian@0 2941 #ifdef CONFIG_DEBUG_SLAB_LEAK
ian@0 2942 {
ian@0 2943 struct slab *slabp;
ian@0 2944 unsigned objnr;
ian@0 2945
ian@0 2946 slabp = page_get_slab(virt_to_page(objp));
ian@0 2947 objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
ian@0 2948 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
ian@0 2949 }
ian@0 2950 #endif
ian@0 2951 objp += obj_offset(cachep);
ian@0 2952 if (cachep->ctor && cachep->flags & SLAB_POISON) {
ian@0 2953 unsigned long ctor_flags = SLAB_CTOR_CONSTRUCTOR;
ian@0 2954
ian@0 2955 if (!(flags & __GFP_WAIT))
ian@0 2956 ctor_flags |= SLAB_CTOR_ATOMIC;
ian@0 2957
ian@0 2958 cachep->ctor(objp, cachep, ctor_flags);
ian@0 2959 }
ian@0 2960 return objp;
ian@0 2961 }
ian@0 2962 #else
ian@0 2963 #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
ian@0 2964 #endif
ian@0 2965
ian@0 2966 static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
ian@0 2967 {
ian@0 2968 void *objp;
ian@0 2969 struct array_cache *ac;
ian@0 2970
ian@0 2971 #ifdef CONFIG_NUMA
ian@0 2972 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
ian@0 2973 objp = alternate_node_alloc(cachep, flags);
ian@0 2974 if (objp != NULL)
ian@0 2975 return objp;
ian@0 2976 }
ian@0 2977 #endif
ian@0 2978
ian@0 2979 check_irq_off();
ian@0 2980 ac = cpu_cache_get(cachep);
ian@0 2981 if (likely(ac->avail)) {
ian@0 2982 STATS_INC_ALLOCHIT(cachep);
ian@0 2983 ac->touched = 1;
ian@0 2984 objp = ac->entry[--ac->avail];
ian@0 2985 } else {
ian@0 2986 STATS_INC_ALLOCMISS(cachep);
ian@0 2987 objp = cache_alloc_refill(cachep, flags);
ian@0 2988 }
ian@0 2989 return objp;
ian@0 2990 }
ian@0 2991
ian@0 2992 static __always_inline void *__cache_alloc(struct kmem_cache *cachep,
ian@0 2993 gfp_t flags, void *caller)
ian@0 2994 {
ian@0 2995 unsigned long save_flags;
ian@0 2996 void *objp;
ian@0 2997
ian@0 2998 cache_alloc_debugcheck_before(cachep, flags);
ian@0 2999
ian@0 3000 local_irq_save(save_flags);
ian@0 3001 objp = ____cache_alloc(cachep, flags);
ian@0 3002 local_irq_restore(save_flags);
ian@0 3003 objp = cache_alloc_debugcheck_after(cachep, flags, objp,
ian@0 3004 caller);
ian@0 3005 prefetchw(objp);
ian@0 3006 return objp;
ian@0 3007 }
ian@0 3008
ian@0 3009 #ifdef CONFIG_NUMA
ian@0 3010 /*
ian@0 3011 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
ian@0 3012 *
ian@0 3013 * If we are in_interrupt, then process context, including cpusets and
ian@0 3014 * mempolicy, may not apply and should not be used for allocation policy.
ian@0 3015 */
ian@0 3016 static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
ian@0 3017 {
ian@0 3018 int nid_alloc, nid_here;
ian@0 3019
ian@0 3020 if (in_interrupt())
ian@0 3021 return NULL;
ian@0 3022 nid_alloc = nid_here = numa_node_id();
ian@0 3023 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
ian@0 3024 nid_alloc = cpuset_mem_spread_node();
ian@0 3025 else if (current->mempolicy)
ian@0 3026 nid_alloc = slab_node(current->mempolicy);
ian@0 3027 if (nid_alloc != nid_here)
ian@0 3028 return __cache_alloc_node(cachep, flags, nid_alloc);
ian@0 3029 return NULL;
ian@0 3030 }
ian@0 3031
ian@0 3032 /*
ian@0 3033 * A interface to enable slab creation on nodeid
ian@0 3034 */
ian@0 3035 static void *__cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
ian@0 3036 int nodeid)
ian@0 3037 {
ian@0 3038 struct list_head *entry;
ian@0 3039 struct slab *slabp;
ian@0 3040 struct kmem_list3 *l3;
ian@0 3041 void *obj;
ian@0 3042 int x;
ian@0 3043
ian@0 3044 l3 = cachep->nodelists[nodeid];
ian@0 3045 BUG_ON(!l3);
ian@0 3046
ian@0 3047 retry:
ian@0 3048 check_irq_off();
ian@0 3049 spin_lock(&l3->list_lock);
ian@0 3050 entry = l3->slabs_partial.next;
ian@0 3051 if (entry == &l3->slabs_partial) {
ian@0 3052 l3->free_touched = 1;
ian@0 3053 entry = l3->slabs_free.next;
ian@0 3054 if (entry == &l3->slabs_free)
ian@0 3055 goto must_grow;
ian@0 3056 }
ian@0 3057
ian@0 3058 slabp = list_entry(entry, struct slab, list);
ian@0 3059 check_spinlock_acquired_node(cachep, nodeid);
ian@0 3060 check_slabp(cachep, slabp);
ian@0 3061
ian@0 3062 STATS_INC_NODEALLOCS(cachep);
ian@0 3063 STATS_INC_ACTIVE(cachep);
ian@0 3064 STATS_SET_HIGH(cachep);
ian@0 3065
ian@0 3066 BUG_ON(slabp->inuse == cachep->num);
ian@0 3067
ian@0 3068 obj = slab_get_obj(cachep, slabp, nodeid);
ian@0 3069 check_slabp(cachep, slabp);
ian@0 3070 l3->free_objects--;
ian@0 3071 /* move slabp to correct slabp list: */
ian@0 3072 list_del(&slabp->list);
ian@0 3073
ian@0 3074 if (slabp->free == BUFCTL_END)
ian@0 3075 list_add(&slabp->list, &l3->slabs_full);
ian@0 3076 else
ian@0 3077 list_add(&slabp->list, &l3->slabs_partial);
ian@0 3078
ian@0 3079 spin_unlock(&l3->list_lock);
ian@0 3080 goto done;
ian@0 3081
ian@0 3082 must_grow:
ian@0 3083 spin_unlock(&l3->list_lock);
ian@0 3084 x = cache_grow(cachep, flags, nodeid);
ian@0 3085
ian@0 3086 if (!x)
ian@0 3087 return NULL;
ian@0 3088
ian@0 3089 goto retry;
ian@0 3090 done:
ian@0 3091 return obj;
ian@0 3092 }
ian@0 3093 #endif
ian@0 3094
ian@0 3095 /*
ian@0 3096 * Caller needs to acquire correct kmem_list's list_lock
ian@0 3097 */
ian@0 3098 static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
ian@0 3099 int node)
ian@0 3100 {
ian@0 3101 int i;
ian@0 3102 struct kmem_list3 *l3;
ian@0 3103
ian@0 3104 for (i = 0; i < nr_objects; i++) {
ian@0 3105 void *objp = objpp[i];
ian@0 3106 struct slab *slabp;
ian@0 3107
ian@0 3108 slabp = virt_to_slab(objp);
ian@0 3109 l3 = cachep->nodelists[node];
ian@0 3110 list_del(&slabp->list);
ian@0 3111 check_spinlock_acquired_node(cachep, node);
ian@0 3112 check_slabp(cachep, slabp);
ian@0 3113 slab_put_obj(cachep, slabp, objp, node);
ian@0 3114 STATS_DEC_ACTIVE(cachep);
ian@0 3115 l3->free_objects++;
ian@0 3116 check_slabp(cachep, slabp);
ian@0 3117
ian@0 3118 /* fixup slab chains */
ian@0 3119 if (slabp->inuse == 0) {
ian@0 3120 if (l3->free_objects > l3->free_limit) {
ian@0 3121 l3->free_objects -= cachep->num;
ian@0 3122 slab_destroy(cachep, slabp);
ian@0 3123 } else {
ian@0 3124 list_add(&slabp->list, &l3->slabs_free);
ian@0 3125 }
ian@0 3126 } else {
ian@0 3127 /* Unconditionally move a slab to the end of the
ian@0 3128 * partial list on free - maximum time for the
ian@0 3129 * other objects to be freed, too.
ian@0 3130 */
ian@0 3131 list_add_tail(&slabp->list, &l3->slabs_partial);
ian@0 3132 }
ian@0 3133 }
ian@0 3134 }
ian@0 3135
ian@0 3136 static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
ian@0 3137 {
ian@0 3138 int batchcount;
ian@0 3139 struct kmem_list3 *l3;
ian@0 3140 int node = numa_node_id();
ian@0 3141
ian@0 3142 batchcount = ac->batchcount;
ian@0 3143 #if DEBUG
ian@0 3144 BUG_ON(!batchcount || batchcount > ac->avail);
ian@0 3145 #endif
ian@0 3146 check_irq_off();
ian@0 3147 l3 = cachep->nodelists[node];
ian@0 3148 spin_lock(&l3->list_lock);
ian@0 3149 if (l3->shared) {
ian@0 3150 struct array_cache *shared_array = l3->shared;
ian@0 3151 int max = shared_array->limit - shared_array->avail;
ian@0 3152 if (max) {
ian@0 3153 if (batchcount > max)
ian@0 3154 batchcount = max;
ian@0 3155 memcpy(&(shared_array->entry[shared_array->avail]),
ian@0 3156 ac->entry, sizeof(void *) * batchcount);
ian@0 3157 shared_array->avail += batchcount;
ian@0 3158 goto free_done;
ian@0 3159 }
ian@0 3160 }
ian@0 3161
ian@0 3162 free_block(cachep, ac->entry, batchcount, node);
ian@0 3163 free_done:
ian@0 3164 #if STATS
ian@0 3165 {
ian@0 3166 int i = 0;
ian@0 3167 struct list_head *p;
ian@0 3168
ian@0 3169 p = l3->slabs_free.next;
ian@0 3170 while (p != &(l3->slabs_free)) {
ian@0 3171 struct slab *slabp;
ian@0 3172
ian@0 3173 slabp = list_entry(p, struct slab, list);
ian@0 3174 BUG_ON(slabp->inuse);
ian@0 3175
ian@0 3176 i++;
ian@0 3177 p = p->next;
ian@0 3178 }
ian@0 3179 STATS_SET_FREEABLE(cachep, i);
ian@0 3180 }
ian@0 3181 #endif
ian@0 3182 spin_unlock(&l3->list_lock);
ian@0 3183 ac->avail -= batchcount;
ian@0 3184 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
ian@0 3185 }
ian@0 3186
ian@0 3187 /*
ian@0 3188 * Release an obj back to its cache. If the obj has a constructed state, it must
ian@0 3189 * be in this state _before_ it is released. Called with disabled ints.
ian@0 3190 */
ian@0 3191 static inline void __cache_free(struct kmem_cache *cachep, void *objp)
ian@0 3192 {
ian@0 3193 struct array_cache *ac = cpu_cache_get(cachep);
ian@0 3194
ian@0 3195 check_irq_off();
ian@0 3196 objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
ian@0 3197
ian@0 3198 if (cache_free_alien(cachep, objp))
ian@0 3199 return;
ian@0 3200
ian@0 3201 if (likely(ac->avail < ac->limit)) {
ian@0 3202 STATS_INC_FREEHIT(cachep);
ian@0 3203 ac->entry[ac->avail++] = objp;
ian@0 3204 return;
ian@0 3205 } else {
ian@0 3206 STATS_INC_FREEMISS(cachep);
ian@0 3207 cache_flusharray(cachep, ac);
ian@0 3208 ac->entry[ac->avail++] = objp;
ian@0 3209 }
ian@0 3210 }
ian@0 3211
ian@0 3212 /**
ian@0 3213 * kmem_cache_alloc - Allocate an object
ian@0 3214 * @cachep: The cache to allocate from.
ian@0 3215 * @flags: See kmalloc().
ian@0 3216 *
ian@0 3217 * Allocate an object from this cache. The flags are only relevant
ian@0 3218 * if the cache has no available objects.
ian@0 3219 */
ian@0 3220 void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
ian@0 3221 {
ian@0 3222 return __cache_alloc(cachep, flags, __builtin_return_address(0));
ian@0 3223 }
ian@0 3224 EXPORT_SYMBOL(kmem_cache_alloc);
ian@0 3225
ian@0 3226 /**
ian@0 3227 * kmem_cache_zalloc - Allocate an object. The memory is set to zero.
ian@0 3228 * @cache: The cache to allocate from.
ian@0 3229 * @flags: See kmalloc().
ian@0 3230 *
ian@0 3231 * Allocate an object from this cache and set the allocated memory to zero.
ian@0 3232 * The flags are only relevant if the cache has no available objects.
ian@0 3233 */
ian@0 3234 void *kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags)
ian@0 3235 {
ian@0 3236 void *ret = __cache_alloc(cache, flags, __builtin_return_address(0));
ian@0 3237 if (ret)
ian@0 3238 memset(ret, 0, obj_size(cache));
ian@0 3239 return ret;
ian@0 3240 }
ian@0 3241 EXPORT_SYMBOL(kmem_cache_zalloc);
ian@0 3242
ian@0 3243 /**
ian@0 3244 * kmem_ptr_validate - check if an untrusted pointer might
ian@0 3245 * be a slab entry.
ian@0 3246 * @cachep: the cache we're checking against
ian@0 3247 * @ptr: pointer to validate
ian@0 3248 *
ian@0 3249 * This verifies that the untrusted pointer looks sane:
ian@0 3250 * it is _not_ a guarantee that the pointer is actually
ian@0 3251 * part of the slab cache in question, but it at least
ian@0 3252 * validates that the pointer can be dereferenced and
ian@0 3253 * looks half-way sane.
ian@0 3254 *
ian@0 3255 * Currently only used for dentry validation.
ian@0 3256 */
ian@0 3257 int fastcall kmem_ptr_validate(struct kmem_cache *cachep, void *ptr)
ian@0 3258 {
ian@0 3259 unsigned long addr = (unsigned long)ptr;
ian@0 3260 unsigned long min_addr = PAGE_OFFSET;
ian@0 3261 unsigned long align_mask = BYTES_PER_WORD - 1;
ian@0 3262 unsigned long size = cachep->buffer_size;
ian@0 3263 struct page *page;
ian@0 3264
ian@0 3265 if (unlikely(addr < min_addr))
ian@0 3266 goto out;
ian@0 3267 if (unlikely(addr > (unsigned long)high_memory - size))
ian@0 3268 goto out;
ian@0 3269 if (unlikely(addr & align_mask))
ian@0 3270 goto out;
ian@0 3271 if (unlikely(!kern_addr_valid(addr)))
ian@0 3272 goto out;
ian@0 3273 if (unlikely(!kern_addr_valid(addr + size - 1)))
ian@0 3274 goto out;
ian@0 3275 page = virt_to_page(ptr);
ian@0 3276 if (unlikely(!PageSlab(page)))
ian@0 3277 goto out;
ian@0 3278 if (unlikely(page_get_cache(page) != cachep))
ian@0 3279 goto out;
ian@0 3280 return 1;
ian@0 3281 out:
ian@0 3282 return 0;
ian@0 3283 }
ian@0 3284
ian@0 3285 #ifdef CONFIG_NUMA
ian@0 3286 /**
ian@0 3287 * kmem_cache_alloc_node - Allocate an object on the specified node
ian@0 3288 * @cachep: The cache to allocate from.
ian@0 3289 * @flags: See kmalloc().
ian@0 3290 * @nodeid: node number of the target node.
ian@0 3291 *
ian@0 3292 * Identical to kmem_cache_alloc, except that this function is slow
ian@0 3293 * and can sleep. And it will allocate memory on the given node, which
ian@0 3294 * can improve the performance for cpu bound structures.
ian@0 3295 * New and improved: it will now make sure that the object gets
ian@0 3296 * put on the correct node list so that there is no false sharing.
ian@0 3297 */
ian@0 3298 void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
ian@0 3299 {
ian@0 3300 unsigned long save_flags;
ian@0 3301 void *ptr;
ian@0 3302
ian@0 3303 cache_alloc_debugcheck_before(cachep, flags);
ian@0 3304 local_irq_save(save_flags);
ian@0 3305
ian@0 3306 if (nodeid == -1 || nodeid == numa_node_id() ||
ian@0 3307 !cachep->nodelists[nodeid])
ian@0 3308 ptr = ____cache_alloc(cachep, flags);
ian@0 3309 else
ian@0 3310 ptr = __cache_alloc_node(cachep, flags, nodeid);
ian@0 3311 local_irq_restore(save_flags);
ian@0 3312
ian@0 3313 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr,
ian@0 3314 __builtin_return_address(0));
ian@0 3315
ian@0 3316 return ptr;
ian@0 3317 }
ian@0 3318 EXPORT_SYMBOL(kmem_cache_alloc_node);
ian@0 3319
ian@0 3320 void *kmalloc_node(size_t size, gfp_t flags, int node)
ian@0 3321 {
ian@0 3322 struct kmem_cache *cachep;
ian@0 3323
ian@0 3324 cachep = kmem_find_general_cachep(size, flags);
ian@0 3325 if (unlikely(cachep == NULL))
ian@0 3326 return NULL;
ian@0 3327 return kmem_cache_alloc_node(cachep, flags, node);
ian@0 3328 }
ian@0 3329 EXPORT_SYMBOL(kmalloc_node);
ian@0 3330 #endif
ian@0 3331
ian@0 3332 /**
ian@0 3333 * __do_kmalloc - allocate memory
ian@0 3334 * @size: how many bytes of memory are required.
ian@0 3335 * @flags: the type of memory to allocate (see kmalloc).
ian@0 3336 * @caller: function caller for debug tracking of the caller
ian@0 3337 */
ian@0 3338 static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
ian@0 3339 void *caller)
ian@0 3340 {
ian@0 3341 struct kmem_cache *cachep;
ian@0 3342
ian@0 3343 /* If you want to save a few bytes .text space: replace
ian@0 3344 * __ with kmem_.
ian@0 3345 * Then kmalloc uses the uninlined functions instead of the inline
ian@0 3346 * functions.
ian@0 3347 */
ian@0 3348 cachep = __find_general_cachep(size, flags);
ian@0 3349 if (unlikely(cachep == NULL))
ian@0 3350 return NULL;
ian@0 3351 return __cache_alloc(cachep, flags, caller);
ian@0 3352 }
ian@0 3353
ian@0 3354
ian@0 3355 void *__kmalloc(size_t size, gfp_t flags)
ian@0 3356 {
ian@0 3357 #ifndef CONFIG_DEBUG_SLAB
ian@0 3358 return __do_kmalloc(size, flags, NULL);
ian@0 3359 #else
ian@0 3360 return __do_kmalloc(size, flags, __builtin_return_address(0));
ian@0 3361 #endif
ian@0 3362 }
ian@0 3363 EXPORT_SYMBOL(__kmalloc);
ian@0 3364
ian@0 3365 #ifdef CONFIG_DEBUG_SLAB
ian@0 3366 void *__kmalloc_track_caller(size_t size, gfp_t flags, void *caller)
ian@0 3367 {
ian@0 3368 return __do_kmalloc(size, flags, caller);
ian@0 3369 }
ian@0 3370 EXPORT_SYMBOL(__kmalloc_track_caller);
ian@0 3371 #endif
ian@0 3372
ian@0 3373 #ifdef CONFIG_SMP
ian@0 3374 /**
ian@0 3375 * __alloc_percpu - allocate one copy of the object for every present
ian@0 3376 * cpu in the system, zeroing them.
ian@0 3377 * Objects should be dereferenced using the per_cpu_ptr macro only.
ian@0 3378 *
ian@0 3379 * @size: how many bytes of memory are required.
ian@0 3380 */
ian@0 3381 void *__alloc_percpu(size_t size)
ian@0 3382 {
ian@0 3383 int i;
ian@0 3384 struct percpu_data *pdata = kmalloc(sizeof(*pdata), GFP_KERNEL);
ian@0 3385
ian@0 3386 if (!pdata)
ian@0 3387 return NULL;
ian@0 3388
ian@0 3389 /*
ian@0 3390 * Cannot use for_each_online_cpu since a cpu may come online
ian@0 3391 * and we have no way of figuring out how to fix the array
ian@0 3392 * that we have allocated then....
ian@0 3393 */
ian@0 3394 for_each_possible_cpu(i) {
ian@0 3395 int node = cpu_to_node(i);
ian@0 3396
ian@0 3397 if (node_online(node))
ian@0 3398 pdata->ptrs[i] = kmalloc_node(size, GFP_KERNEL, node);
ian@0 3399 else
ian@0 3400 pdata->ptrs[i] = kmalloc(size, GFP_KERNEL);
ian@0 3401
ian@0 3402 if (!pdata->ptrs[i])
ian@0 3403 goto unwind_oom;
ian@0 3404 memset(pdata->ptrs[i], 0, size);
ian@0 3405 }
ian@0 3406
ian@0 3407 /* Catch derefs w/o wrappers */
ian@0 3408 return (void *)(~(unsigned long)pdata);
ian@0 3409
ian@0 3410 unwind_oom:
ian@0 3411 while (--i >= 0) {
ian@0 3412 if (!cpu_possible(i))
ian@0 3413 continue;
ian@0 3414 kfree(pdata->ptrs[i]);
ian@0 3415 }
ian@0 3416 kfree(pdata);
ian@0 3417 return NULL;
ian@0 3418 }
ian@0 3419 EXPORT_SYMBOL(__alloc_percpu);
ian@0 3420 #endif
ian@0 3421
ian@0 3422 /**
ian@0 3423 * kmem_cache_free - Deallocate an object
ian@0 3424 * @cachep: The cache the allocation was from.
ian@0 3425 * @objp: The previously allocated object.
ian@0 3426 *
ian@0 3427 * Free an object which was previously allocated from this
ian@0 3428 * cache.
ian@0 3429 */
ian@0 3430 void kmem_cache_free(struct kmem_cache *cachep, void *objp)
ian@0 3431 {
ian@0 3432 unsigned long flags;
ian@0 3433
ian@0 3434 BUG_ON(virt_to_cache(objp) != cachep);
ian@0 3435
ian@0 3436 local_irq_save(flags);
ian@0 3437 __cache_free(cachep, objp);
ian@0 3438 local_irq_restore(flags);
ian@0 3439 }
ian@0 3440 EXPORT_SYMBOL(kmem_cache_free);
ian@0 3441
ian@0 3442 /**
ian@0 3443 * kfree - free previously allocated memory
ian@0 3444 * @objp: pointer returned by kmalloc.
ian@0 3445 *
ian@0 3446 * If @objp is NULL, no operation is performed.
ian@0 3447 *
ian@0 3448 * Don't free memory not originally allocated by kmalloc()
ian@0 3449 * or you will run into trouble.
ian@0 3450 */
ian@0 3451 void kfree(const void *objp)
ian@0 3452 {
ian@0 3453 struct kmem_cache *c;
ian@0 3454 unsigned long flags;
ian@0 3455
ian@0 3456 if (unlikely(!objp))
ian@0 3457 return;
ian@0 3458 local_irq_save(flags);
ian@0 3459 kfree_debugcheck(objp);
ian@0 3460 c = virt_to_cache(objp);
ian@0 3461 debug_check_no_locks_freed(objp, obj_size(c));
ian@0 3462 __cache_free(c, (void *)objp);
ian@0 3463 local_irq_restore(flags);
ian@0 3464 }
ian@0 3465 EXPORT_SYMBOL(kfree);
ian@0 3466
ian@0 3467 #ifdef CONFIG_SMP
ian@0 3468 /**
ian@0 3469 * free_percpu - free previously allocated percpu memory
ian@0 3470 * @objp: pointer returned by alloc_percpu.
ian@0 3471 *
ian@0 3472 * Don't free memory not originally allocated by alloc_percpu()
ian@0 3473 * The complemented objp is to check for that.
ian@0 3474 */
ian@0 3475 void free_percpu(const void *objp)
ian@0 3476 {
ian@0 3477 int i;
ian@0 3478 struct percpu_data *p = (struct percpu_data *)(~(unsigned long)objp);
ian@0 3479
ian@0 3480 /*
ian@0 3481 * We allocate for all cpus so we cannot use for online cpu here.
ian@0 3482 */
ian@0 3483 for_each_possible_cpu(i)
ian@0 3484 kfree(p->ptrs[i]);
ian@0 3485 kfree(p);
ian@0 3486 }
ian@0 3487 EXPORT_SYMBOL(free_percpu);
ian@0 3488 #endif
ian@0 3489
ian@0 3490 unsigned int kmem_cache_size(struct kmem_cache *cachep)
ian@0 3491 {
ian@0 3492 return obj_size(cachep);
ian@0 3493 }
ian@0 3494 EXPORT_SYMBOL(kmem_cache_size);
ian@0 3495
ian@0 3496 const char *kmem_cache_name(struct kmem_cache *cachep)
ian@0 3497 {
ian@0 3498 return cachep->name;
ian@0 3499 }
ian@0 3500 EXPORT_SYMBOL_GPL(kmem_cache_name);
ian@0 3501
ian@0 3502 /*
ian@0 3503 * This initializes kmem_list3 or resizes varioius caches for all nodes.
ian@0 3504 */
ian@0 3505 static int alloc_kmemlist(struct kmem_cache *cachep)
ian@0 3506 {
ian@0 3507 int node;
ian@0 3508 struct kmem_list3 *l3;
ian@0 3509 struct array_cache *new_shared;
ian@0 3510 struct array_cache **new_alien;
ian@0 3511
ian@0 3512 for_each_online_node(node) {
ian@0 3513
ian@0 3514 new_alien = alloc_alien_cache(node, cachep->limit);
ian@0 3515 if (!new_alien)
ian@0 3516 goto fail;
ian@0 3517
ian@0 3518 new_shared = alloc_arraycache(node,
ian@0 3519 cachep->shared*cachep->batchcount,
ian@0 3520 0xbaadf00d);
ian@0 3521 if (!new_shared) {
ian@0 3522 free_alien_cache(new_alien);
ian@0 3523 goto fail;
ian@0 3524 }
ian@0 3525
ian@0 3526 l3 = cachep->nodelists[node];
ian@0 3527 if (l3) {
ian@0 3528 struct array_cache *shared = l3->shared;
ian@0 3529
ian@0 3530 spin_lock_irq(&l3->list_lock);
ian@0 3531
ian@0 3532 if (shared)
ian@0 3533 free_block(cachep, shared->entry,
ian@0 3534 shared->avail, node);
ian@0 3535
ian@0 3536 l3->shared = new_shared;
ian@0 3537 if (!l3->alien) {
ian@0 3538 l3->alien = new_alien;
ian@0 3539 new_alien = NULL;
ian@0 3540 }
ian@0 3541 l3->free_limit = (1 + nr_cpus_node(node)) *
ian@0 3542 cachep->batchcount + cachep->num;
ian@0 3543 spin_unlock_irq(&l3->list_lock);
ian@0 3544 kfree(shared);
ian@0 3545 free_alien_cache(new_alien);
ian@0 3546 continue;
ian@0 3547 }
ian@0 3548 l3 = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, node);
ian@0 3549 if (!l3) {
ian@0 3550 free_alien_cache(new_alien);
ian@0 3551 kfree(new_shared);
ian@0 3552 goto fail;
ian@0 3553 }
ian@0 3554
ian@0 3555 kmem_list3_init(l3);
ian@0 3556 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
ian@0 3557 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
ian@0 3558 l3->shared = new_shared;
ian@0 3559 l3->alien = new_alien;
ian@0 3560 l3->free_limit = (1 + nr_cpus_node(node)) *