3 * Written by Mark Hemment, 1996. 4 * (markhe@nextd.demon.co.uk) 7 * An implementation of the Slab Allocator as described in outline in; 8 * UNIX Internals: The New Frontiers by Uresh Vahalia 9 * Pub: Prentice Hall ISBN 0-13-101908-2 10 * or with a little more detail in; 11 * The Slab Allocator: An Object-Caching Kernel Memory Allocator 12 * Jeff Bonwick (Sun Microsystems). 13 * Presented at: USENIX Summer 1994 Technical Conference 16 #include <linux/slab.h> 18 #include <linux/interrupt.h> 19 #include <asm/system.h> 20 #include <asm/cache.h> 22 /* SLAB_MGMT_CHECKS - define to enable extra checks in 23 * kmem_cache_[create|destroy|shrink]. 24 * If you're not messing around with these funcs, then undef this. 25 * SLAB_HIGH_PACK - define to allow 'bufctl's to be stored within objs that do not 26 * have a state. This allows more objs per slab, but removes the 27 * ability to sanity check an addr on release (if the addr is 28 * within any slab, anywhere, kmem_cache_free() will accept it!). 29 * SLAB_DEBUG_SUPPORT - when defined, kmem_cache_create() will honour; SLAB_DEBUG_FREE, 30 * SLAB_DEBUG_INITIAL and SLAB_RED_ZONE. 32 #define SLAB_MGMT_CHECKS 34 #define SLAB_DEBUG_SUPPORT/* undef this when your cache is stable */ 36 #define BYTES_PER_WORD sizeof(void *) 38 /* legal flag mask for kmem_cache_create() */ 39 #if defined(SLAB_DEBUG_SUPPORT) 40 #define SLAB_C_MASK (SLAB_DEBUG_FREE|SLAB_DEBUG_INITIAL|SLAB_HWCACHE_ALIGN|SLAB_RED_ZONE) 42 #define SLAB_C_MASK (SLAB_HWCACHE_ALIGN) 43 #endif/* SLAB_DEBUG_SUPPORT */ 45 /* Magic num for red zoning. 46 * Placed in the first word after the end of an obj 48 #define SLAB_RED_MAGIC1 0x5A2CF071UL/* when obj is active */ 49 #define SLAB_RED_MAGIC2 0x170FC2A5UL/* when obj is inactive */ 51 /* Used for linking objs within a slab. How much of the struct is 52 * used, and where its placed, depends on the packing used in a cache. 53 * Don't mess with the order! 55 typedefstruct kmem_bufctl_s
{ 56 struct kmem_bufctl_s
*buf_nextp
; 57 struct kmem_slab_s
*buf_slabp
; 58 void*buf_objp
;/* start of obj */ 59 struct kmem_bufctl_s
*buf_hnextp
; 60 struct kmem_bufctl_s
**buf_hashp
; 63 /* different portions of the bufctl are used - so need some macros */ 64 #define kmem_bufctl_offset(x) ((unsigned long)&((kmem_bufctl_t *)0)->x) 65 #define kmem_bufctl_short_size (kmem_bufctl_offset(buf_objp)) 66 #define kmem_bufctl_very_short_size (kmem_bufctl_offset(buf_slabp)) 68 /* Slab management struct. 69 * Manages the objs in a slab. Placed either at the end of mem allocated 70 * for the slab, or from an internal obj cache (SLAB_CFLGS_OFF_SLAB). 71 * Slabs are chain into a partially ordered list. The linking ptrs must 72 * be first in the struct! 73 * The size of the struct is important(ish); it should align well on 76 typedefstruct kmem_slab_s
{ 77 struct kmem_slab_s
*s_nextp
; 78 struct kmem_slab_s
*s_prevp
; 79 void*s_mem
;/* addr of mem allocated for slab */ 80 unsigned long s_jiffies
; 81 kmem_bufctl_t
*s_freep
;/* ptr to first inactive obj in slab */ 82 unsigned long s_flags
; 83 unsigned long s_magic
; 84 unsigned long s_inuse
;/* num of objs active in slab */ 87 /* to test for end of slab chain */ 88 #define kmem_slab_end(x) ((kmem_slab_t*)&((x)->c_firstp)) 91 #define SLAB_MAGIC_ALLOC 0xA5C32F2BUL 92 #define SLAB_MAGIC_UNALLOC 0xB2F23C5AUL 95 #define SLAB_SFLGS_DMA 0x000001UL/* slab's mem can do DMA */ 97 /* cache struct - manages a cache. 98 * c_lastp must appear immediately after c_firstp! 100 struct kmem_cache_s
{ 101 kmem_slab_t
*c_freep
;/* first slab w. free objs */ 102 unsigned long c_flags
; 103 unsigned long c_offset
; 104 struct kmem_bufctl_s
**c_hashp
;/* ptr for off-slab bufctls */ 105 kmem_slab_t
*c_firstp
;/* first slab in chain */ 106 kmem_slab_t
*c_lastp
;/* last slab in chain */ 107 unsigned long c_hashbits
; 108 unsigned long c_num
;/* # of objs per slab */ 109 unsigned long c_gfporder
;/* order of pgs per slab (2^n) */ 110 unsigned long c_org_size
; 111 unsigned long c_magic
; 112 unsigned long c_inuse
;/* kept at zero */ 113 void(*c_ctor
)(void*,int,unsigned long);/* constructor func */ 114 void(*c_dtor
)(void*,int,unsigned long);/* de-constructor func */ 115 unsigned long c_align
;/* alignment of objs */ 116 unsigned long c_colour
;/* cache colouring range */ 117 unsigned long c_colour_next
;/* cache colouring */ 119 struct kmem_cache_s
*c_nextp
; 122 /* magic # for c_magic - used to detect out-of-slabs in __kmem_cache_alloc() */ 123 #define SLAB_C_MAGIC 0x4F17A36DUL 125 /* internal c_flags */ 126 #define SLAB_CFLGS_OFF_SLAB 0x010000UL/* slab mgmt in own cache */ 127 #define SLAB_CFLGS_BUFCTL 0x020000UL/* bufctls in own cache */ 128 #define SLAB_CFLGS_RELEASED 0x040000UL/* cache is/being destroyed */ 130 #if defined(SLAB_HIGH_PACK) 131 #define SLAB_CFLGS_PTR_IN_OBJ 0x080000UL/* free ptr in obj */ 134 #define SLAB_OFF_SLAB(x) ((x) & SLAB_CFLGS_OFF_SLAB) 135 #define SLAB_BUFCTL(x) ((x) & SLAB_CFLGS_BUFCTL) 136 #define SLAB_RELEASED(x) ((x) & SLAB_CFLGS_RELEASED) 137 #if defined(SLAB_HIGH_PACK) 138 #define SLAB_PTR_IN_OBJ(x) ((x) & SLAB_CFLGS_PTR_IN_OBJ) 140 #define SLAB_PTR_IN_OBJ(x) (0) 143 /* maximum size of an obj (in 2^order pages) */ 144 #define SLAB_OBJ_MAX_ORDER 5/* 32 pages */ 146 /* maximum num of pages for a slab (avoids trying to ask for too may contigious pages) */ 147 #define SLAB_MAX_GFP_ORDER 5/* 32 pages */ 149 /* the 'prefered' minimum num of objs per slab - maybe less for large objs */ 150 #define SLAB_MIN_OBJS_PER_SLAB 4 152 /* if the num of objs per slab is <= SLAB_MIN_OBJS_PER_SLAB, 153 * then the page order must be less than this before trying the next order 155 #define SLAB_BREAK_GFP_ORDER 2 157 /* size of hash tables for caches which use off-slab bufctls (SLAB_CFLGS_BUFCTL) */ 158 #define KMEM_HASH_SIZE 128 160 /* size description struct for general-caches */ 161 typedefstruct cache_sizes
{ 162 unsigned long cs_size
; 163 kmem_cache_t
*cs_cachep
; 166 static cache_sizes_t cache_sizes
[] = { 167 #if PAGE_SIZE == 4096 178 #if PAGE_SIZE == 8192 184 /* Names for the general-caches. 185 * Not placed into the sizes struct for a good reason; the 186 * string ptr is not needed while searching in kmem_alloc()/ 187 * kmem_free(), and would 'get-in-the-way' - think about it. 189 static char*cache_sizes_name
[] = { 190 #if PAGE_SIZE == 4096 200 #if PAGE_SIZE == 4096 202 #elif PAGE_SIZE == 8192 206 #error Your page size is not supported for the general-caches - please fix 210 static voidkmem_hash_ctor(void*ptr
,int,unsigned long);/* fwd ref */ 211 extern kmem_cache_t cache_cache
;/* fwd ref */ 213 /* internal cache of hash objs, only used when bufctls are off-slab */ 214 static kmem_cache_t cache_hash
= { 215 /* freep, flags */kmem_slab_end(&cache_hash
),0, 216 /* offset, hashp */sizeof(kmem_bufctl_t
*)*KMEM_HASH_SIZE
, NULL
, 217 /* firstp, lastp */kmem_slab_end(&cache_hash
),kmem_slab_end(&cache_hash
), 218 /* hashbits, num, gfporder */0,0,0, 219 /* org_size, magic */sizeof(kmem_bufctl_t
*)*KMEM_HASH_SIZE
, SLAB_C_MAGIC
, 220 /* inuse, ctor, dtor, align */0, kmem_hash_ctor
, NULL
, L1_CACHE_BYTES
, 221 /* colour, colour_next */0,0, 222 /* name, nextp */"hash_cache", &cache_cache
225 /* internal cache of freelist mgmnt objs, only use when bufctls are off-slab */ 226 static kmem_cache_t cache_bufctl
= { 227 /* freep, flags */kmem_slab_end(&cache_bufctl
),0, 228 /* offset, hashp */sizeof(kmem_bufctl_t
), NULL
, 229 /* firstp, lastp */kmem_slab_end(&cache_bufctl
),kmem_slab_end(&cache_bufctl
), 230 /* hashbits, num, gfporder */0,0,0, 231 /* org_size, magic */sizeof(kmem_bufctl_t
), SLAB_C_MAGIC
, 232 /* inuse, ctor, dtor, align */0, NULL
, NULL
, BYTES_PER_WORD
*2, 233 /* colour, colour_next */0,0, 234 /* name, nextp */"bufctl_cache", &cache_hash
237 /* internal cache of slab mngmnt objs, only used when slab mgmt is off-slab */ 238 static kmem_cache_t cache_slab
= { 239 /* freep, flags */kmem_slab_end(&cache_slab
),0, 240 /* offset, hashp */sizeof(kmem_slab_t
), NULL
, 241 /* firstp, lastp */kmem_slab_end(&cache_slab
),kmem_slab_end(&cache_slab
), 242 /* hashbits, num, gfporder */0,0,0, 243 /* org_size, magic */sizeof(kmem_slab_t
), SLAB_C_MAGIC
, 244 /* inuse, ctor, dtor, align */0, NULL
, NULL
, L1_CACHE_BYTES
, 245 /* colour, colour_next */0,0, 246 /* name, nextp */"slab_cache", &cache_bufctl
249 /* internal cache of cache description objs */ 250 static kmem_cache_t cache_cache
= { 251 /* freep, flags */kmem_slab_end(&cache_cache
),0, 252 /* offset, hashp */sizeof(kmem_cache_t
), NULL
, 253 /* firstp, lastp */kmem_slab_end(&cache_cache
),kmem_slab_end(&cache_cache
), 254 /* hashbits, num, gfporder */0,0,0, 255 /* org_size, magic */sizeof(kmem_cache_t
), SLAB_C_MAGIC
, 256 /* inuse, ctor, dtor, align */0, NULL
, NULL
, L1_CACHE_BYTES
, 257 /* colour, colour_next */0,0, 258 /* name */"kmem_cache", 259 /* nextp */&cache_slab
262 /* constructor for hash tables */ 263 static voidkmem_hash_ctor(void*ptr
,int size
,unsigned long flags
) 265 memset(ptr
,0,sizeof(kmem_bufctl_t
*)*KMEM_HASH_SIZE
); 268 /* place maintainer for reaping */ 269 static kmem_cache_t
*clock_searchp
= &cache_cache
; 271 /* Init an internal cache */ 273 kmem_own_cache_init(kmem_cache_t
*cachep
) 275 unsigned long size
, i
; 277 if(cachep
->c_inuse
|| cachep
->c_magic
!= SLAB_C_MAGIC
) { 278 panic("Bad init of internal cache %s", cachep
->c_name
); 281 size
= cachep
->c_offset
+ kmem_bufctl_short_size
; 282 i
= size
% cachep
->c_align
; 284 size
+= (cachep
->c_align
-i
); 285 cachep
->c_offset
= size
-kmem_bufctl_short_size
; 287 i
= ((PAGE_SIZE
<<cachep
->c_gfporder
)-sizeof(kmem_slab_t
)); 288 cachep
->c_num
= i
/ size
;/* num of objs per slab */ 290 /* cache colouring */ 291 cachep
->c_colour
=1+ (i
-(cachep
->c_num
*size
))/cachep
->c_align
; 292 cachep
->c_colour_next
= cachep
->c_colour
; 295 /* Initialisation - setup all internal caches */ 297 kmem_cache_init(long start
,long end
) 300 #define kmem_cache_offset(x) ((unsigned long)&((kmem_cache_t *)0)->x) 301 #define kmem_slab_offset(x) ((unsigned long)&((kmem_slab_t *)0)->x) 302 if(((kmem_cache_offset(c_magic
)-kmem_cache_offset(c_firstp
)) !=kmem_slab_offset(s_magic
)) || 303 ((kmem_cache_offset(c_inuse
)-kmem_cache_offset(c_firstp
)) !=kmem_slab_offset(s_inuse
))) { 304 /* Offsets to the magic are incorrect, either the structures have 305 * been incorrectly changed, or adjustments are needed for your 308 panic("kmem_cache_init(): Offsets are different - been messed with!\n"); 311 #undef kmem_cache_offset 312 #undef kmem_slab_offset 314 kmem_own_cache_init(&cache_cache
); 315 kmem_own_cache_init(&cache_slab
); 316 kmem_own_cache_init(&cache_bufctl
); 317 kmem_own_cache_init(&cache_hash
); 321 /* Initialisation - setup general caches */ 323 kmem_cache_sizes_init(void) 327 i
=sizeof(cache_sizes
)/sizeof(cache_sizes
[0])-1; 329 cache_sizes
[i
].cs_cachep
=kmem_cache_create(cache_sizes_name
[i
], 330 cache_sizes
[i
].cs_size
, 334 /* Interface to system's page allocator. 335 * dma pts to non-zero if all of the mem is suitable for DMA 338 kmem_getpages(const kmem_cache_t
*cachep
,unsigned long flags
,unsigned int*dma
) 343 addr
= (void*)__get_free_pages(flags
& SLAB_LEVEL_MASK
, \
344 cachep
->c_gfporder
, flags
& SLAB_DMA
); 345 *dma
=1<<cachep
->c_gfporder
; 346 if(!(flags
& SLAB_DMA
) && addr
) { 347 /* need to check if can dma */ 348 page
= mem_map
+MAP_NR(addr
); 360 /* Interface to system's page release */ 362 kmem_freepages(kmem_cache_t
*cachep
,void*addr
) 364 free_pages((unsigned long)addr
, cachep
->c_gfporder
); 367 /* Hashing function - used for caches with off-slab bufctls */ 369 kmem_hash(const kmem_cache_t
*cachep
,const void*objp
) 371 return(((unsigned long)objp
>> cachep
->c_hashbits
) & (KMEM_HASH_SIZE
-1)); 374 /* Link bufctl into a hash table - used for caches with off-slab bufctls 375 * - called with ints disabled 378 kmem_add_to_hash(kmem_cache_t
*cachep
, kmem_bufctl_t
*bufp
) 380 kmem_bufctl_t
**bufpp
= bufp
->buf_hashp
; 382 bufp
->buf_hnextp
= *bufpp
; 383 return(*bufpp
= bufp
)->buf_objp
; 386 /* Find bufcntl for given obj addr, and unlink. 387 * - called with ints disabled 389 staticinline kmem_bufctl_t
* 390 kmem_remove_from_hash(kmem_cache_t
*cachep
,const void*objp
) 393 kmem_bufctl_t
**bufpp
= &cachep
->c_hashp
[kmem_hash(cachep
, objp
)]; 395 for(;*bufpp
; bufpp
= &(*bufpp
)->buf_hnextp
) { 396 if((*bufpp
)->buf_objp
!= objp
) 399 *bufpp
= bufp
->buf_hnextp
; 405 /* Three slab chain funcs - all called with ints disabled */ 407 kmem_slab_unlink(kmem_slab_t
*slabp
) 409 kmem_slab_t
*prevp
= slabp
->s_prevp
; 410 kmem_slab_t
*nextp
= slabp
->s_nextp
; 412 prevp
->s_nextp
= nextp
; 413 nextp
->s_prevp
= prevp
; 417 kmem_slab_link_end(kmem_cache_t
*cachep
, kmem_slab_t
*slabp
) 419 slabp
->s_nextp
=kmem_slab_end(cachep
); 420 slabp
->s_prevp
= cachep
->c_lastp
; 421 kmem_slab_end(cachep
)->s_prevp
= slabp
; 422 slabp
->s_prevp
->s_nextp
= slabp
; 426 kmem_slab_link_free(kmem_cache_t
*cachep
, kmem_slab_t
*slabp
) 428 kmem_slab_t
*nextp
= cachep
->c_freep
; 430 slabp
->s_nextp
= nextp
; 431 cachep
->c_freep
= slabp
; 432 slabp
->s_prevp
= nextp
->s_prevp
; 433 nextp
->s_prevp
= slabp
; 434 slabp
->s_prevp
->s_nextp
= slabp
; 437 /* Cal the num objs, wastage, and bytes left over for a given slab size */ 439 kmem_cache_cal_waste(unsigned long gfporder
,unsigned long size
, 440 unsigned long extra
,unsigned long flags
, 441 unsigned long*left_over
,unsigned long*num
) 443 unsigned long wastage
; 445 wastage
= PAGE_SIZE
<< gfporder
; 447 if(!SLAB_OFF_SLAB(flags
)) 448 gfporder
=sizeof(kmem_slab_t
); 450 *num
= wastage
/ size
; 451 wastage
-= (*num
* size
); 452 *left_over
= wastage
; 454 wastage
+= (extra
* *num
); 461 * Returns a ptr to the cache on success, NULL on failure. 462 * Cannot be called within a int, but can be interrupted. 463 * NOTE: The 'name' is assumed to be memory that is _not_ going to disappear. 466 kmem_cache_create(const char*name
,unsigned long size
,unsigned long align
, 467 unsigned long flags
,void(*ctor
)(void*,int,unsigned long), 468 void(*dtor
)(void*,int,unsigned long)) 470 const char*func_nm
="kmem_create: "; 471 kmem_cache_t
*searchp
, *cachep
; 472 unsigned long words
, i
; 473 unsigned long num
, left_over
; 476 #if defined(SLAB_MGMT_CHECKS) 478 printk(KERN_ERR
"%sNULL ptr\n", func_nm
); 482 printk(KERN_ERR
"%sCalled during int - %s\n", func_nm
, name
); 486 if(size
< kmem_bufctl_very_short_size
) { 487 printk(KERN_WARNING
"%sSize too small %lu - %s\n", func_nm
, size
, name
); 488 size
= kmem_bufctl_very_short_size
; 491 if(size
> ((1<<SLAB_OBJ_MAX_ORDER
)*PAGE_SIZE
)) { 492 printk(KERN_ERR
"%sSize too large %lu - %s\n", func_nm
, size
, name
); 495 #endif/* SLAB_MGMT_CHECKS */ 497 /* always checks flags, a caller might be expecting debug support which 500 if(flags
& ~SLAB_C_MASK
) { 502 printk(KERN_WARNING
"%sIllgl flg %lX - %s\n", func_nm
, flags
, name
); 503 flags
&= SLAB_C_MASK
; 506 #if defined(SLAB_MGMT_CHECKS) 507 if(align
<0|| align
>= size
) { 508 printk(KERN_WARNING
"%sAlign weired %lu - %s\n", func_nm
, align
, name
); 513 /* Descon, but no con - doesn't make sense */ 514 printk(KERN_ERR
"%sDecon but no con - %s\n", func_nm
, name
); 518 if((flags
& SLAB_DEBUG_INITIAL
) && !ctor
) { 519 /* No constructor, but inital state check requested */ 520 printk(KERN_WARNING
"%sNo con, but init state check requested - %s\n", 522 flags
&= ~SLAB_DEBUG_INITIAL
; 524 #endif/* SLAB_MGMT_CHECKS */ 526 /* get cache's description obj */ 527 cachep
= (kmem_cache_t
*)kmem_cache_alloc(&cache_cache
, SLAB_KERNEL
); 531 /* remember original size, so can be passed to a constructor or decon. 532 * Allows the same con/decon to be used for caches of similar objs 533 * that have a different size data buffer assoicated with them 535 cachep
->c_org_size
= size
; 537 #if defined(SLAB_DEBUG_SUPPORT) 538 if(flags
& SLAB_RED_ZONE
) 539 size
+= BYTES_PER_WORD
;/* word for redzone */ 540 #endif/* SLAB_DEBUG_SUPPORT */ 542 /* Make a guess if slab mngmnt obj and/or bufctls are 'on' or 'off' slab */ 543 i
= kmem_bufctl_short_size
; 544 if(size
< (PAGE_SIZE
>>3)) { 545 /* Size is small(ish). Use format where bufctl size per 546 * obj is low, and slab mngmnt is on-slab 548 if(!ctor
&& !dtor
&& !(flags
& SLAB_RED_ZONE
)) { 549 /* the objs in this cache have no state - can store 550 * store freelist ptr within obj. (redzoning is a state) 552 #if defined(SLAB_HIGH_PACK) 554 flags
|= SLAB_CFLGS_PTR_IN_OBJ
; 556 i
= kmem_bufctl_very_short_size
; 560 /* Size is large, assume best to place the slab mngmnt obj 561 * off-slab (should allow better packing of objs) 563 flags
|= SLAB_CFLGS_OFF_SLAB
; 564 if(!(size
& ~PAGE_MASK
) || 565 size
== (PAGE_SIZE
+PAGE_SIZE
/2) || 566 size
== (PAGE_SIZE
/2) || 567 size
== (PAGE_SIZE
/4) || 568 size
== (PAGE_SIZE
/8)) { 569 /* to avoid waste the bufctls are off-slab */ 570 flags
|= SLAB_CFLGS_BUFCTL
; 571 /* get hash table for cache */ 572 cachep
->c_hashp
=kmem_cache_alloc(&cache_hash
, SLAB_KERNEL
); 573 if(cachep
->c_hashp
== NULL
) { 574 kmem_cache_free(&cache_cache
, cachep
); 578 cachep
->c_hashbits
= PAGE_SHIFT
; 579 if(size
<= (PAGE_SIZE
/2)) { 580 cachep
->c_hashbits
--; 581 if(size
<= (PAGE_SIZE
/4)) cachep
->c_hashbits
--; 582 if(size
<= (PAGE_SIZE
/8)) cachep
->c_hashbits
-=2; 584 }/* else slab mngmnt is off-slab, but freelist ptrs are on */ 588 /* Adjust the mem used for objs so they will align correctly. 589 * Force objs to start on word boundaries, but caller may specify 590 * h/w cache line boundaries. This 'alignment' is slightly different 591 * to the 'align' argument. Objs may be requested to start on h/w 592 * lines (as that is how the members of the obj have been organised), 593 * but the 'align' may be quite high (say 64) as the first 64 bytes 594 * are commonly accessed/modified within a loop (stops h/w line 595 * thrashing). The 'align' is the slab colouring. 597 words
= BYTES_PER_WORD
; 598 if(flags
& SLAB_HWCACHE_ALIGN
) 599 words
= L1_CACHE_BYTES
; 602 size
= size
& ~words
; 603 /* alignment might not be a factor of the boundary alignment - fix-up */ 605 align
= align
& ~words
; 608 /* Cal size (in pages) of slabs, and the num of objs per slab. 609 * This could be made much more intelligent. */ 610 cachep
->c_gfporder
=0; 612 unsigned long wastage
; 613 wastage
=kmem_cache_cal_waste(cachep
->c_gfporder
, size
, i
, 614 flags
, &left_over
, &num
); 617 if(SLAB_PTR_IN_OBJ(flags
)) 619 if(cachep
->c_gfporder
== SLAB_MAX_GFP_ORDER
) 621 /* large num of objs is good, but v. large slabs are bad for the 624 if(num
<= SLAB_MIN_OBJS_PER_SLAB
) { 625 if(cachep
->c_gfporder
< SLAB_BREAK_GFP_ORDER
) 628 /* stop caches with small objs having a large num of pages */ 629 if(left_over
<=sizeof(kmem_slab_t
)) 631 if((wastage
*8) <= (PAGE_SIZE
<<cachep
->c_gfporder
)) 632 break;/* acceptable wastage */ 634 cachep
->c_gfporder
++; 638 /* try with requested alignment, but reduce it if that will 639 * allow at least some alignment words 642 if(left_over
< align
) 643 align
= (left_over
/ words
) * words
; 644 else if(!align
&& words
<= left_over
) { 645 /* no alignment given, but space enough - give one */ 647 if(words
== BYTES_PER_WORD
) { 648 if(BYTES_PER_WORD
*4<= left_over
) 650 if(BYTES_PER_WORD
*8<= left_over
) 654 cachep
->c_align
= align
; 657 printk("Size:%lu Orig:%lu Left:%lu Align %lu Pages:%d - %s\n", 658 size
, cachep
->c_org_size
, left_over
, align
,1<<cachep
->c_gfporder
, name
); 659 if(SLAB_OFF_SLAB(flags
))printk("OFF SLAB\n"); 660 if(SLAB_BUFCTL(flags
))printk("BUFCTL PTRS\n"); 663 /* if the bufctl's are on-slab, c_offset does not inc the size of the bufctl */ 664 if(!SLAB_BUFCTL(flags
)) 665 size
-= kmem_bufctl_short_size
; 666 cachep
->c_freep
=kmem_slab_end(cachep
); 667 cachep
->c_flags
= flags
; 668 cachep
->c_offset
= size
; 669 cachep
->c_firstp
=kmem_slab_end(cachep
); 670 cachep
->c_lastp
=kmem_slab_end(cachep
); 671 cachep
->c_ctor
= ctor
; 672 cachep
->c_dtor
= dtor
; 673 cachep
->c_magic
= SLAB_C_MAGIC
; 674 cachep
->c_inuse
=0;/* always zero */ 675 cachep
->c_name
= name
;/* simply point to the name */ 679 cachep
->c_colour
+= (left_over
/align
); 680 cachep
->c_colour_next
= cachep
->c_colour
; 682 /* warn on dup cache names */ 683 searchp
= &cache_cache
; 685 if(!strcmp(searchp
->c_name
, name
)) { 686 printk(KERN_WARNING
"%sDup name - %s\n", func_nm
, name
); 689 searchp
= searchp
->c_nextp
; 690 }while(searchp
!= &cache_cache
); 691 cachep
->c_nextp
= cache_cache
.c_nextp
; 692 cache_cache
.c_nextp
= cachep
; 695 printk(KERN_WARNING
"%sOut of mem creating cache %s\n", func_nm
, name
); 699 /* Destroy all the objs in a slab, and release the mem back to the system. 700 * Before calling the slab must have been unlinked 703 kmem_slab_destroy(kmem_cache_t
*cachep
, kmem_slab_t
*slabp
,unsigned long flags
) 705 if(cachep
->c_dtor
||SLAB_BUFCTL(cachep
->c_flags
)) { 706 kmem_bufctl_t
*bufp
= slabp
->s_freep
; 708 /* for each obj in slab... */ 710 kmem_bufctl_t
*freep
; 712 void*objp
= ((void*)bufp
)-cachep
->c_offset
; 713 if(SLAB_BUFCTL(cachep
->c_flags
)) 714 objp
= bufp
->buf_objp
; 715 (cachep
->c_dtor
)(objp
, cachep
->c_org_size
, flags
); 718 bufp
= bufp
->buf_nextp
; 719 if(SLAB_BUFCTL(cachep
->c_flags
)) 720 kmem_cache_free(&cache_bufctl
, freep
); 724 slabp
->s_magic
= SLAB_MAGIC_UNALLOC
; 725 kmem_freepages(cachep
, slabp
->s_mem
); 726 if(SLAB_OFF_SLAB(cachep
->c_flags
)) 727 kmem_cache_free(&cache_slab
, slabp
); 730 /* Destroy (remove) a cache. 731 * All objs in the cache should be inactive 734 kmem_cache_destroy(kmem_cache_t
*cachep
) 736 kmem_cache_t
**searchp
; 738 unsigned long save_flags
; 740 #if defined(SLAB_MGMT_CHECKS) 742 printk(KERN_ERR
"kmem_dest: NULL ptr\n"); 747 printk(KERN_ERR
"kmem_dest: Called during int - %s\n", cachep
->c_name
); 751 #endif/* SLAB_MGMT_CHECKS */ 753 /* unlink the cache from the chain of active caches. 754 * Note: the chain is never modified during an int 756 searchp
= &(cache_cache
.c_nextp
); 757 for(;*searchp
!= &cache_cache
; searchp
= &((*searchp
)->c_nextp
)) { 758 if(*searchp
!= cachep
) 762 printk(KERN_ERR
"kmem_dest: Invalid cache addr %p\n", cachep
); 765 /* disable cache so attempts to allocated from an int can 768 save_flags(save_flags
); 770 if(cachep
->c_freep
!=kmem_slab_end(cachep
)) { 771 restore_flags(save_flags
); 772 printk(KERN_ERR
"kmem_dest: active cache - %s\n", cachep
->c_name
); 775 *searchp
= cachep
->c_nextp
;/* remove from cache chain */ 776 cachep
->c_flags
|= SLAB_CFLGS_RELEASED
; 777 cachep
->c_freep
=kmem_slab_end(cachep
); 778 if(cachep
== clock_searchp
) 779 clock_searchp
= cachep
->c_nextp
; 780 restore_flags(save_flags
); 782 while((slabp
= cachep
->c_firstp
) !=kmem_slab_end(cachep
)) { 783 kmem_slab_unlink(slabp
); 784 kmem_slab_destroy(cachep
, slabp
,0); 787 if(SLAB_BUFCTL(cachep
->c_flags
)) 788 kmem_cache_free(&cache_hash
, cachep
->c_hashp
); 789 kmem_cache_free(&cache_cache
, cachep
); 793 /* Shrink a cache, ie. remove _all_ inactive slabs. 794 * Can be called when a user of a cache knows they are not going to be 795 * needing any new objs for a while. 796 * NOTE: This func is probably going to disappear - let me know if you 800 kmem_cache_shrink(kmem_cache_t
*cachep
,int wait
) 803 unsigned long dtor_flags
; 804 unsigned long save_flags
, num_freed
=0; 806 #if defined(SLAB_MGMT_CHECKS) 808 printk(KERN_ERR
"kmem_shrink: NULL ptr\n"); 813 printk(KERN_ERR
"kmem_shrink: Called during int - %s\n", cachep
->c_name
); 816 #endif/* SLAB_MGMT_CHECKS */ 819 if(!wait
)/* not allowed to wait */ 820 dtor_flags
= SLAB_DTOR_ATOMIC
; 822 save_flags(save_flags
); 825 slabp
= cachep
->c_lastp
; 826 if(slabp
==kmem_slab_end(cachep
) || slabp
->s_inuse
) { 827 restore_flags(save_flags
); 830 kmem_slab_unlink(slabp
); 831 if(cachep
->c_freep
== slabp
) 832 cachep
->c_freep
=kmem_slab_end(cachep
); 833 restore_flags(save_flags
); 835 kmem_slab_destroy(cachep
, slabp
, dtor_flags
); 841 /* Search for a slab whose objs are suitable for DMA. 842 * Note: since testing the first free slab (in __kmem_cache_alloc()), 843 * ints must not have been enabled! 845 staticinline kmem_slab_t
* 846 kmem_cache_search_dma(kmem_cache_t
*cachep
) 848 kmem_slab_t
*slabp
= cachep
->c_freep
->s_nextp
; 850 for(; slabp
!=kmem_slab_end(cachep
); slabp
= slabp
->s_nextp
) { 851 if(!(slabp
->s_flags
& SLAB_SFLGS_DMA
)) 853 kmem_slab_unlink(slabp
); 854 kmem_slab_link_free(cachep
, slabp
); 860 /* get the mem for a slab mgmt obj */ 861 staticinline kmem_slab_t
* 862 kmem_cache_slabmgmt(kmem_cache_t
*cachep
,void*objp
,unsigned long local_flags
,unsigned long offset
) 866 if(SLAB_OFF_SLAB(cachep
->c_flags
)) { 867 /* slab mngmnt obj is off-slab */ 868 if(!(slabp
=kmem_cache_alloc(&cache_slab
, local_flags
))) 871 /* slab mngmnt at end of slab mem */ 872 slabp
= objp
+ (PAGE_SIZE
<< cachep
->c_gfporder
); 874 if(!SLAB_PTR_IN_OBJ(cachep
->c_flags
)) { 875 /* A bit of extra help for the L1 cache; try to position the slab 876 * mgmnt struct at different offsets within the gap at the end 877 * of a slab. This helps avoid thrashing the h/w cache lines, 878 * that map to the end of a page, too much... 880 unsigned long gap
= cachep
->c_offset
; 881 if(!SLAB_BUFCTL(cachep
->c_flags
)) 882 gap
+= kmem_bufctl_short_size
; 883 gap
= (PAGE_SIZE
<< cachep
->c_gfporder
)-((gap
*cachep
->c_num
)+offset
+sizeof(*slabp
)); 884 gap
/= (sizeof(*slabp
)/2); 885 gap
*= (sizeof(*slabp
)/2); 886 slabp
= (((void*)slabp
)-gap
); 890 slabp
->s_flags
= slabp
->s_inuse
= slabp
->s_jiffies
=0; 896 kmem_cache_init_objs(kmem_cache_t
*cachep
, kmem_slab_t
*slabp
,void*objp
, 897 unsigned long local_flags
,unsigned long ctor_flags
) 899 kmem_bufctl_t
**bufpp
= &slabp
->s_freep
; 900 unsigned long num
= cachep
->c_num
; 903 if(SLAB_BUFCTL(cachep
->c_flags
)) { 904 if(!(*bufpp
=kmem_cache_alloc(&cache_bufctl
, local_flags
))) { 905 kmem_slab_destroy(cachep
, slabp
,0); 908 (*bufpp
)->buf_objp
= objp
; 909 (*bufpp
)->buf_hashp
= &cachep
->c_hashp
[kmem_hash(cachep
, objp
)]; 913 cachep
->c_ctor(objp
, cachep
->c_org_size
, ctor_flags
); 915 #if defined(SLAB_DEBUG_SUPPORT) 916 if(cachep
->c_flags
& SLAB_RED_ZONE
) 917 *((unsigned long*)(objp
+cachep
->c_org_size
)) = SLAB_RED_MAGIC1
; 918 #endif/* SLAB_DEBUG_SUPPORT */ 920 objp
+= cachep
->c_offset
; 921 if(!SLAB_BUFCTL(cachep
->c_flags
)) { 923 objp
+= kmem_bufctl_short_size
; 925 if(!SLAB_PTR_IN_OBJ(cachep
->c_flags
)) 926 (*bufpp
)->buf_slabp
= slabp
; 927 bufpp
= &(*bufpp
)->buf_nextp
; 933 /* Grow (by 1) the number of slabs within a cache. 934 * This is called by kmem_cache_alloc() when there are no 935 * inactive objs left in a cache 938 kmem_cache_grow(kmem_cache_t
*cachep
,unsigned long flags
) 942 unsigned int offset
, dma
; 943 unsigned long ctor_flags
, local_flags
, save_flags
; 945 if(flags
& SLAB_NO_GROW
) 946 return;/* caller doesn't want us to grow */ 948 save_flags(save_flags
); 949 /* The test for missing atomic flag is performed here, rather than 950 * the more obvious place, simply to reduce the critical path length 951 * in kmem_cache_alloc(). If a caller is slightly mis-behaving, 952 * will eventually be caught here (where it matters) 954 if(in_interrupt() && (flags
& SLAB_LEVEL_MASK
) != SLAB_ATOMIC
) { 957 printk(KERN_ERR
"kmem_grow: Called nonatomically from " 958 "int - %s\n", cachep
->c_name
); 961 flags
&= ~SLAB_LEVEL_MASK
; 962 flags
|= SLAB_ATOMIC
; 964 local_flags
= (flags
& SLAB_LEVEL_MASK
); 965 ctor_flags
= SLAB_CTOR_CONSTRUCTOR
; 966 if((flags
& SLAB_LEVEL_MASK
) == SLAB_ATOMIC
) { 967 /* Not allowed to sleep. 968 * Need to tell a constructor about this - it 969 * might need to know.... 971 ctor_flags
|= SLAB_CTOR_ATOMIC
; 975 /* get mem for the objs */ 976 if(!(objp
=kmem_getpages(cachep
, flags
, &dma
))) 979 /* get colour for the slab, and cal the next value */ 981 if(!(offset
= --(cachep
->c_colour_next
))) 982 cachep
->c_colour_next
= cachep
->c_colour
; 983 restore_flags(save_flags
); 984 offset
*= cachep
->c_align
; 987 if(!(slabp
=kmem_cache_slabmgmt(cachep
, objp
, local_flags
, offset
))) 990 slabp
->s_flags
= SLAB_SFLGS_DMA
; 993 objp
+= offset
;/* address of first object */ 995 /* For on-slab bufctls, c_offset is the distance between the start of 996 * an obj and its related bufctl. For off-slab bufctls, c_offset is 997 * the distance between objs in the slab. 998 * Reason for bufctl at end of obj (when on slab), as opposed to the front; 999 * if stored within the obj (has no state), and the obj is 'used' after being 1000 * freed then (normally) most activity occurs at the beginning of the obj. 1001 * By keeping the bufctl ptr away from the front, should reduce the chance of 1002 * corruption. Also, allows easier alignment of objs onto cache lines when 1003 * bufctl is not stored with the objs. 1004 * Downsize; if, while an obj is active, a write is made past its end, then the 1005 * bufctl will be corrupted :( 1007 if(kmem_cache_init_objs(cachep
, slabp
, objp
, local_flags
, ctor_flags
)) 1011 /* make slab active */ 1012 slabp
->s_magic
= SLAB_MAGIC_ALLOC
; 1013 kmem_slab_link_end(cachep
, slabp
); 1014 if(cachep
->c_freep
==kmem_slab_end(cachep
)) 1015 cachep
->c_freep
= slabp
; 1016 restore_flags(save_flags
); 1019 kmem_freepages(cachep
, slabp
->s_mem
); 1021 kmem_freepages(cachep
, objp
); 1023 if(slabp
&&SLAB_OFF_SLAB(cachep
->c_flags
)) 1024 kmem_cache_free(&cache_slab
, slabp
); 1025 /* printk("kmem_alloc: Out of mem - %s\n", cachep->c_name); */ 1029 #if defined(SLAB_DEBUG_SUPPORT) 1030 /* Perform extra freeing checks. 1031 * Currently, this check is only for caches that use bufctl structures 1032 * within the slab. Those which use bufctl's from the internal cache 1033 * have a reasonable check when the address is searched for. 1036 kmem_extra_free_checks(const kmem_cache_t
*cachep
, kmem_bufctl_t
*search_bufp
, 1037 const kmem_bufctl_t
*bufp
,void* objp
) 1039 if(SLAB_BUFCTL(cachep
->c_flags
)) 1042 /* check slab's freelist to see if this obj is there */ 1043 for(; search_bufp
; search_bufp
= search_bufp
->buf_nextp
) { 1044 if(search_bufp
!= bufp
) 1046 printk(KERN_ERR
"kmem_free: Double free detected during checking " 1047 "%p - %s\n", objp
, cachep
->c_name
); 1053 #endif/* SLAB_DEBUG_SUPPORT */ 1056 kmem_cache_full_free(kmem_cache_t
*cachep
, kmem_slab_t
*slabp
) 1058 if(!slabp
->s_nextp
->s_inuse
) 1059 return;/* at correct position */ 1060 slabp
->s_jiffies
= jiffies
;/* set release time */ 1061 if(cachep
->c_freep
== slabp
) 1062 cachep
->c_freep
= slabp
->s_nextp
; 1063 kmem_slab_unlink(slabp
); 1064 kmem_slab_link_end(cachep
, slabp
); 1070 kmem_cache_one_free(kmem_cache_t
*cachep
, kmem_slab_t
*slabp
) 1072 if(slabp
->s_nextp
->s_inuse
!= cachep
->c_num
) { 1073 cachep
->c_freep
= slabp
; 1076 kmem_slab_unlink(slabp
); 1077 kmem_slab_link_free(cachep
, slabp
); 1081 /* Returns a ptr to an obj in the given cache. 1082 * The obj is in the initial state (if there is one) 1085 __kmem_cache_alloc(kmem_cache_t
*cachep
,unsigned long flags
) 1088 kmem_bufctl_t
*bufp
; 1090 unsigned long save_flags
; 1095 save_flags(save_flags
); 1097 /* get slab alloc is to come from */ 1098 slabp
= cachep
->c_freep
; 1100 /* magic is a sanity check _and_ says if we need a new slab */ 1101 if(slabp
->s_magic
!= SLAB_MAGIC_ALLOC
) 1102 goto alloc_new_slab
; 1104 /* DMA allocations are 'rare' - keep out of critical path */ 1105 if(flags
& SLAB_DMA
) 1109 bufp
= slabp
->s_freep
; 1110 slabp
->s_freep
= bufp
->buf_nextp
; 1111 if(!SLAB_BUFCTL(cachep
->c_flags
)) { 1112 /* Nasty - we want the 'if' to be taken in the common case */ 1113 if(slabp
->s_freep
) { 1115 objp
= ((void*)bufp
) - cachep
->c_offset
; 1116 restore_flags(save_flags
); 1117 #if defined(SLAB_DEBUG_SUPPORT) 1118 if(cachep
->c_flags
& SLAB_RED_ZONE
) 1120 #endif/* SLAB_DEBUG_SUPPORT */ 1123 cachep
->c_freep
= slabp
->s_nextp
; 1124 goto short_finished
; 1129 cachep
->c_freep
= slabp
->s_nextp
; 1131 /* link into hash chain */ 1132 objp
=kmem_add_to_hash(cachep
, bufp
); 1133 restore_flags(save_flags
); 1134 #if defined(SLAB_DEBUG_SUPPORT) 1135 if(!(cachep
->c_flags
& SLAB_RED_ZONE
)) 1136 #endif/* SLAB_DEBUG_SUPPORT */ 1139 #if defined(SLAB_DEBUG_SUPPORT) 1141 /* set alloc red-zone, and check old one */ 1142 if(xchg((unsigned long*)(objp
+cachep
->c_org_size
), SLAB_RED_MAGIC2
) != SLAB_RED_MAGIC1
) 1143 printk(KERN_ERR
"kmem_alloc: Bad redzone %p - %s\n", 1144 objp
, cachep
->c_name
); 1146 #endif/* SLAB_DEBUG_SUPPORT */ 1149 if(slabp
->s_flags
& SLAB_SFLGS_DMA
) 1151 /* need to search... */ 1152 if((slabp
=kmem_cache_search_dma(cachep
))) 1155 /* Either out of slabs, or magic number corruption */ 1156 if(slabp
!=kmem_slab_end(cachep
)) 1158 /* need a new slab */ 1159 restore_flags(save_flags
); 1160 if(SLAB_RELEASED(cachep
->c_flags
)) { 1161 printk(KERN_ERR
"kmem_alloc: destroyed cache\n"); 1165 /* Be lazy and only check for valid flags 1166 * here (keeping it out of the critical path above) 1168 if(flags
& ~(SLAB_DMA
|SLAB_LEVEL_MASK
|SLAB_NO_GROW
)) { 1169 printk(KERN_ERR
"kmem_alloc: Illegal flgs %lX (correcting) - %s\n", 1170 flags
, cachep
->c_name
); 1171 flags
&= (SLAB_DMA
|SLAB_LEVEL_MASK
|SLAB_NO_GROW
); 1174 kmem_cache_grow(cachep
, flags
); 1176 if((slabp
=cachep
->c_freep
) !=kmem_slab_end(cachep
)) 1178 restore_flags(save_flags
); 1182 /* v. serious error - maybe panic() here? */ 1183 printk(KERN_ERR
"kmem_alloc: Bad slab magic (corruption) - %s\n", 1187 printk(KERN_ERR
"kmem_alloc: NULL ptr\n"); 1191 /* Release an obj back to its cache. 1192 * If the obj has a constructed state, it should be 1193 * in this state _before_ it is released. 1196 __kmem_cache_free(kmem_cache_t
*cachep
,void*objp
) 1199 kmem_bufctl_t
*bufp
; 1200 unsigned long save_flags
; 1202 /* basic sanity checks */ 1208 save_flags(save_flags
); 1209 #if defined(SLAB_DEBUG_SUPPORT) 1210 if(cachep
->c_flags
& SLAB_DEBUG_INITIAL
) 1211 goto init_state_check
; 1213 #endif/* SLAB_DEBUG_SUPPORT */ 1215 if(SLAB_BUFCTL(cachep
->c_flags
)) 1218 bufp
= (kmem_bufctl_t
*)(objp
+cachep
->c_offset
); 1220 /* get slab for the obj */ 1221 if(SLAB_PTR_IN_OBJ(cachep
->c_flags
)) { 1222 /* if SLAB_HIGH_PACK is undef, the below is optimised away */ 1223 slabp
= (kmem_slab_t
*)((((unsigned long)objp
)&PAGE_MASK
)+PAGE_SIZE
); 1226 slabp
= (kmem_slab_t
*) bufp
->buf_slabp
; 1228 if(slabp
->s_magic
!= SLAB_MAGIC_ALLOC
)/* sanity check */ 1232 #if defined(SLAB_DEBUG_SUPPORT) 1233 if(cachep
->c_flags
& (SLAB_DEBUG_FREE
|SLAB_RED_ZONE
)) 1235 #endif/* SLAB_DEBUG_SUPPORT */ 1238 if(!slabp
->s_inuse
)/* sanity check */ 1240 bufp
->buf_nextp
= slabp
->s_freep
; 1241 slabp
->s_freep
= bufp
; 1242 if(--(slabp
->s_inuse
)) { 1243 if(bufp
->buf_nextp
) { 1244 restore_flags(save_flags
); 1247 kmem_cache_one_free(cachep
, slabp
); 1248 restore_flags(save_flags
); 1251 kmem_cache_full_free(cachep
, slabp
); 1252 restore_flags(save_flags
); 1255 /* Off-slab bufctls. Need to search hash for bufctl, and hence the slab. 1256 * No 'extra' checks are performed for objs stored this way, finding 1257 * the obj a check enough 1260 if((bufp
=kmem_remove_from_hash(cachep
, objp
))) { 1261 slabp
= (kmem_slab_t
*) bufp
->buf_slabp
; 1262 #if defined(SLAB_DEBUG_SUPPORT) 1263 if(cachep
->c_flags
& SLAB_RED_ZONE
) 1265 #endif/* SLAB_DEBUG_SUPPORT */ 1268 restore_flags(save_flags
); 1269 printk(KERN_ERR
"kmem_free: Either bad obj addr or double free: %p - %s\n", 1270 objp
, cachep
->c_name
); 1272 #if defined(SLAB_DEBUG_SUPPORT) 1274 if(xchg((unsigned long*)(objp
+cachep
->c_org_size
), SLAB_RED_MAGIC1
) != SLAB_RED_MAGIC2
) { 1275 /* Either write past end of the object, or a double free */ 1276 printk(KERN_ERR
"kmem_free: Bad redzone %p - %s\n", 1277 objp
, cachep
->c_name
); 1281 /* Need to call the slab's constructor so that 1282 * the caller can perform a verify of its state (debugging) 1284 cachep
->c_ctor(objp
, cachep
->c_org_size
, SLAB_CTOR_CONSTRUCTOR
|SLAB_CTOR_VERIFY
); 1285 goto finished_initial
; 1287 if((cachep
->c_flags
& SLAB_DEBUG_FREE
) && 1288 (objp
!=kmem_extra_free_checks(cachep
, slabp
->s_freep
, bufp
, objp
))) { 1289 restore_flags(save_flags
); 1292 if(cachep
->c_flags
& SLAB_RED_ZONE
) 1295 #endif/* SLAB_DEBUG_SUPPORT */ 1297 /* The addr of the slab doesn't contain the correct 1300 if(slabp
->s_magic
== SLAB_MAGIC_UNALLOC
) { 1301 /* magic num says this is an unalloc slab */ 1302 printk(KERN_ERR
"kmem_free: obj %p from destroyed slab - %s\n", 1303 objp
, cachep
->c_name
); 1306 printk(KERN_ERR
"kmem_free: Bad obj %p - %s\n", objp
, cachep
->c_name
); 1309 /* don't add to freelist */ 1310 restore_flags(save_flags
); 1311 printk(KERN_ERR
"kmem_free: obj free for slab with no active objs - %s\n", 1315 printk(KERN_ERR
"kmem_free: NULL obj - %s\n", cachep
->c_name
); 1318 printk(KERN_ERR
"kmem_free: NULL cache ptr\n"); 1323 kmem_cache_alloc(kmem_cache_t
*cachep
,unsigned long flags
) 1325 return__kmem_cache_alloc(cachep
, flags
); 1329 kmem_cache_free(kmem_cache_t
*cachep
,void*objp
) 1331 __kmem_cache_free(cachep
, objp
); 1335 kmem_alloc(unsigned long size
,unsigned long flags
) 1337 cache_sizes_t
*cachep
= cache_sizes
; 1339 for(; cachep
->cs_size
; cachep
++) { 1340 if(size
> cachep
->cs_size
) 1342 /* should the inline version be used here? */ 1343 returnkmem_cache_alloc(cachep
->cs_cachep
, flags
); 1345 printk(KERN_ERR
"kmem_alloc: Size (%lu) too large\n", size
); 1350 kmem_free(void*objp
,unsigned long size
) 1352 cache_sizes_t
*cachep
= cache_sizes
; 1354 for(; cachep
->cs_size
; cachep
++) { 1355 if(size
> cachep
->cs_size
) 1357 /* should the inline version be used here? */ 1358 kmem_cache_free(cachep
->cs_cachep
, objp
); 1361 printk(KERN_ERR
"kmem_free: Size (%lu) too large - strange\n", size
); 1366 /* Called from try_to_free_page(). 1367 * Ideal solution would have a weight for each cache, based on; 1368 * o num of fully free slabs 1369 * o if the objs have a constructor/deconstructor 1370 * o length of time slabs have been fully free (ie. ageing) 1371 * This function _cannot_ be called within a int, but it 1372 * can be interrupted. 1375 kmem_cache_reap(int pri
,int dma
,int wait
) 1377 unsigned long dtor_flags
=0; 1378 unsigned long best_jiffie
; 1381 kmem_slab_t
*best_slabp
= NULL
; 1382 kmem_cache_t
*best_cachep
= NULL
; 1384 kmem_cache_t
*searchp
; 1385 unsigned long save_flags
; 1387 /* 'pri' maps to the number of caches to examine, not the number of slabs. 1388 * This avoids only checking the jiffies for slabs in one cache at the 1389 * expensive spending more cycles 1392 if(!wait
)/* not allowed to wait */ 1393 dtor_flags
= SLAB_DTOR_ATOMIC
; 1394 searchp
= clock_searchp
; 1395 save_flags(save_flags
); 1397 best_jiffie
= now
- (2*HZ
);/* 2secs - avoid heavy thrashing */ 1399 kmem_slab_t
*local_slabp
; 1400 unsigned long local_jiffie
; 1401 if(searchp
== &cache_cache
) 1404 /* sanity check for corruption */ 1405 if(searchp
->c_inuse
|| searchp
->c_magic
!= SLAB_C_MAGIC
) { 1406 printk(KERN_ERR
"kmem_reap: Corrupted cache struct for %s\n", 1412 local_jiffie
= now
- (2*HZ
); 1414 /* As the fully free slabs, within a cache, have no particular 1415 * order, we need to test them all. Infact, we only check 'count' 1418 slabp
= searchp
->c_lastp
; 1419 for(;count
&& slabp
!=kmem_slab_end(searchp
) && !slabp
->s_inuse
; slabp
= slabp
->s_prevp
, count
--) { 1420 if(slabp
->s_jiffies
>= local_jiffie
) 1423 /* weight caches with a con/decon */ 1424 if((searchp
->c_ctor
|| searchp
->c_dtor
) && slabp
->s_jiffies
>= (local_jiffie
- (2*HZ
))) 1427 /* weight caches with high page orders. Avoids stressing the 1428 * VM sub-system by reducing the frequency requests for a large 1429 * num of contigious pages 1431 if(searchp
->c_gfporder
>1&& slabp
->s_jiffies
>= (local_jiffie
- (4*HZ
))) 1434 local_jiffie
= slabp
->s_jiffies
; 1435 local_slabp
= slabp
; 1436 if(!searchp
->c_gfporder
&& (now
-local_jiffie
) >= (300*HZ
)) { 1437 /* an old, one page slab. Make a quick get away... */ 1443 if(!count
|| local_jiffie
< best_jiffie
) { 1444 best_slabp
= local_slabp
; 1445 best_jiffie
= local_jiffie
; 1446 best_cachep
= searchp
; 1451 restore_flags(save_flags
); 1453 searchp
= searchp
->c_nextp
; 1454 if(searchp
== clock_searchp
) 1456 count
=8;/* # of slabs at which we force a reap */ 1459 /* only move along with we didn't find an over allocated cache */ 1461 clock_searchp
= clock_searchp
->c_nextp
; 1467 if(best_slabp
->s_inuse
) { 1468 /* an object in our selected slab has been 1469 * allocated. This souldn't happen v. often, so we 1470 * simply fail - which isn't ideal but will do. 1471 * NOTE: No test for the case where an obj has been 1472 * allocated from the slab, and then freed. While 1473 * this would change our idea of the best slab to 1474 * reap, it's not worth the re-calculation effort. 1476 restore_flags(save_flags
); 1480 if(best_cachep
->c_freep
== best_slabp
) 1481 best_cachep
->c_freep
= best_slabp
->s_nextp
; 1482 kmem_slab_unlink(best_slabp
); 1484 restore_flags(save_flags
); 1485 kmem_slab_destroy(best_cachep
, best_slabp
, dtor_flags
); 1491 * cache-name num-active-objs total-objs num-active-slabs total-slabs num-pages-per-slab 1494 get_slabinfo(char*buf
) 1496 kmem_cache_t
*cachep
; 1498 unsigned long active_objs
; 1499 unsigned long num_slabs
, active_slabs
; 1500 unsigned long save_flags
; 1503 /* output format version, so at least we can change it without _too_ 1506 len
=sprintf(buf
,"slabinfo - version: 1.0\n"); 1507 save_flags(save_flags
); 1508 cachep
= &cache_cache
; 1510 active_slabs
= num_slabs
= active_objs
=0; 1512 for(slabp
= cachep
->c_firstp
; 1513 slabp
!=kmem_slab_end(cachep
); 1514 slabp
= slabp
->s_nextp
) { 1516 active_objs
+= slabp
->s_inuse
; 1520 restore_flags(save_flags
); 1521 len
+=sprintf(buf
+len
,"%-20s%lu %lu %lu %lu %d\n", cachep
->c_name
, 1522 active_objs
, cachep
->c_num
*num_slabs
, 1523 active_slabs
, num_slabs
,1<<cachep
->c_gfporder
); 1524 }while((cachep
= cachep
->c_nextp
) != &cache_cache
);