uma.h revision 148072
1251875Speter/*- 2289166Speter * Copyright (c) 2004, 2005, 3289166Speter * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved. 4289166Speter * Copyright (c) 2002, 2003, 2004, 2005, 5289166Speter * Jeffrey Roberson <jeff@FreeBSD.org>. All rights reserved. 6289166Speter * 7289166Speter * Redistribution and use in source and binary forms, with or without 8289166Speter * modification, are permitted provided that the following conditions 9289166Speter * are met: 10289166Speter * 1. Redistributions of source code must retain the above copyright 11289166Speter * notice unmodified, this list of conditions, and the following 12289166Speter * disclaimer. 13289166Speter * 2. Redistributions in binary form must reproduce the above copyright 14289166Speter * notice, this list of conditions and the following disclaimer in the 15289166Speter * documentation and/or other materials provided with the distribution. 16289166Speter * 17289166Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18289166Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19289166Speter * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20289166Speter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21289166Speter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22289166Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23289166Speter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24289166Speter * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25289166Speter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26289166Speter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27289166Speter * 28289166Speter * $FreeBSD: head/sys/vm/uma.h 148072 2005-07-16 02:23:41Z silby $ 29289166Speter * 30289166Speter */ 31289166Speter 32289166Speter/* 33289166Speter * uma.h - External definitions for the Universal Memory Allocator 34289166Speter * 35289166Speter*/ 36289166Speter 37289166Speter#ifndef VM_UMA_H 38289166Speter#define VM_UMA_H 39289166Speter 40289166Speter#include <sys/param.h> /* For NULL */ 41289166Speter#include <sys/malloc.h> /* For M_* */ 42289166Speter 43289166Speter/* User visable parameters */ 44289166Speter#define UMA_SMALLEST_UNIT (PAGE_SIZE / 256) /* Smallest item allocated */ 45289166Speter 46289166Speter/* Types and type defs */ 47289166Speter 48289166Speterstruct uma_zone; 49289166Speter/* Opaque type used as a handle to the zone */ 50289166Spetertypedef struct uma_zone * uma_zone_t; 51289166Speter 52289166Speter/* 53289166Speter * Item constructor 54289166Speter * 55289166Speter * Arguments: 56289166Speter * item A pointer to the memory which has been allocated. 57289166Speter * arg The arg field passed to uma_zalloc_arg 58289166Speter * size The size of the allocated item 59289166Speter * flags See zalloc flags 60289166Speter * 61269847Speter * Returns: 62253734Speter * 0 on success 63269847Speter * errno on failure 64269847Speter * 65253734Speter * Discussion: 66269847Speter * The constructor is called just before the memory is returned 67269847Speter * to the user. It may block if necessary. 68269847Speter */ 69253734Spetertypedef int (*uma_ctor)(void *mem, int size, void *arg, int flags); 70269847Speter 71253734Speter/* 72269847Speter * Item destructor 73269847Speter * 74269847Speter * Arguments: 75269847Speter * item A pointer to the memory which has been allocated. 76253734Speter * size The size of the item being destructed. 77269847Speter * arg Argument passed through uma_zfree_arg 78269847Speter * 79269847Speter * Returns: 80253734Speter * Nothing 81269847Speter * 82269847Speter * Discussion: 83253734Speter * The destructor may perform operations that differ from those performed 84269847Speter * by the initializer, but it must leave the object in the same state. 85269847Speter * This IS type stable storage. This is called after EVERY zfree call. 86269847Speter */ 87253734Spetertypedef void (*uma_dtor)(void *mem, int size, void *arg); 88269847Speter 89269847Speter/* 90269847Speter * Item initializer 91269847Speter * 92253734Speter * Arguments: 93269847Speter * item A pointer to the memory which has been allocated. 94253734Speter * size The size of the item being initialized. 95253734Speter * flags See zalloc flags 96269847Speter * 97269847Speter * Returns: 98253734Speter * 0 on success 99289166Speter * errno on failure 100289166Speter * 101253734Speter * Discussion: 102269847Speter * The initializer is called when the memory is cached in the uma zone. 103253734Speter * this should be the same state that the destructor leaves the object in. 104269847Speter */ 105269847Spetertypedef int (*uma_init)(void *mem, int size, int flags); 106269847Speter 107253734Speter/* 108269847Speter * Item discard function 109269847Speter * 110269847Speter * Arguments: 111253734Speter * item A pointer to memory which has been 'freed' but has not left the 112269847Speter * zone's cache. 113269847Speter * size The size of the item being discarded. 114269847Speter * 115253734Speter * Returns: 116269847Speter * Nothing 117269847Speter * 118253734Speter * Discussion: 119269847Speter * This routine is called when memory leaves a zone and is returned to the 120251875Speter * system for other uses. It is the counter part to the init function. 121269847Speter */ 122269847Spetertypedef void (*uma_fini)(void *mem, int size); 123269847Speter 124251875Speter/* 125269847Speter * What's the difference between initializing and constructing? 126269847Speter * 127269847Speter * The item is initialized when it is cached, and this is the state that the 128269847Speter * object should be in when returned to the allocator. The purpose of this is 129269847Speter * to remove some code which would otherwise be called on each allocation by 130269847Speter * utilizing a known, stable state. This differs from the constructor which 131269847Speter * will be called on EVERY allocation. 132269847Speter * 133251875Speter * For example, in the initializer you may want to initialize embeded locks, 134269847Speter * NULL list pointers, set up initial states, magic numbers, etc. This way if 135269847Speter * the object is held in the allocator and re-used it won't be necessary to 136251875Speter * re-initialize it. 137269847Speter * 138269847Speter * The constructor may be used to lock a data structure, link it on to lists, 139251875Speter * bump reference counts or total counts of outstanding structures, etc. 140269847Speter * 141269847Speter */ 142269847Speter 143251875Speter 144269847Speter/* Function proto types */ 145269847Speter 146251875Speter/* 147269847Speter * Create a new uma zone 148251875Speter * 149269847Speter * Arguments: 150269847Speter * name The text name of the zone for debugging and stats, this memory 151251875Speter * should not be freed until the zone has been deallocated. 152269847Speter * size The size of the object that is being created. 153251875Speter * ctor The constructor that is called when the object is allocated 154269847Speter * dtor The destructor that is called when the object is freed. 155269847Speter * init An initializer that sets up the initial state of the memory. 156251875Speter * fini A discard function that undoes initialization done by init. 157269847Speter * ctor/dtor/init/fini may all be null, see notes above. 158269847Speter * align A bitmask that corisponds to the requested alignment 159251875Speter * eg 4 would be 0x3 160269847Speter * flags A set of parameters that control the behavior of the zone 161269847Speter * 162269847Speter * Returns: 163251875Speter * A pointer to a structure which is intended to be opaque to users of 164269847Speter * the interface. The value may be null if the wait flag is not set. 165269847Speter */ 166269847Speteruma_zone_t uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 167251875Speter uma_init uminit, uma_fini fini, int align, 168251875Speter u_int32_t flags); 169251875Speter 170251875Speter/* 171269847Speter * Create a secondary uma zone 172269847Speter * 173251875Speter * Arguments: 174269847Speter * name The text name of the zone for debugging and stats, this memory 175269847Speter * should not be freed until the zone has been deallocated. 176269847Speter * ctor The constructor that is called when the object is allocated 177251875Speter * dtor The destructor that is called when the object is freed. 178269847Speter * zinit An initializer that sets up the initial state of the memory 179251875Speter * as the object passes from the Keg's slab to the Zone's cache. 180269847Speter * zfini A discard function that undoes initialization done by init 181251875Speter * as the object passes from the Zone's cache to the Keg's slab. 182251875Speter * 183251875Speter * ctor/dtor/zinit/zfini may all be null, see notes above. 184251875Speter * Note that the zinit and zfini specified here are NOT 185251875Speter * exactly the same as the init/fini specified to uma_zcreate() 186251875Speter * when creating a master zone. These zinit/zfini are called 187251875Speter * on the TRANSITION from keg to zone (and vice-versa). Once 188251875Speter * these are set, the primary zone may alter its init/fini 189251875Speter * (which are called when the object passes from VM to keg) 190251875Speter * using uma_zone_set_init/fini()) as well as its own 191251875Speter * zinit/zfini (unset by default for master zone) with 192251875Speter * uma_zone_set_zinit/zfini() (note subtle 'z' prefix). 193251875Speter * 194251875Speter * master A reference to this zone's Master Zone (Primary Zone), 195251875Speter * which contains the backing Keg for the Secondary Zone 196251875Speter * being added. 197251875Speter * 198251875Speter * Returns: 199251875Speter * A pointer to a structure which is intended to be opaque to users of 200251875Speter * the interface. The value may be null if the wait flag is not set. 201 */ 202uma_zone_t uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 203 uma_init zinit, uma_fini zfini, uma_zone_t master); 204 205/* 206 * Definitions for uma_zcreate flags 207 * 208 * These flags share space with UMA_ZFLAGs in uma_int.h. Be careful not to 209 * overlap when adding new features. 0xf0000000 is in use by uma_int.h. 210 */ 211#define UMA_ZONE_PAGEABLE 0x0001 /* Return items not fully backed by 212 physical memory XXX Not yet */ 213#define UMA_ZONE_ZINIT 0x0002 /* Initialize with zeros */ 214#define UMA_ZONE_STATIC 0x0004 /* Staticly sized zone */ 215#define UMA_ZONE_OFFPAGE 0x0008 /* Force the slab structure allocation 216 off of the real memory */ 217#define UMA_ZONE_MALLOC 0x0010 /* For use by malloc(9) only! */ 218#define UMA_ZONE_NOFREE 0x0020 /* Do not free slabs of this type! */ 219#define UMA_ZONE_MTXCLASS 0x0040 /* Create a new lock class */ 220#define UMA_ZONE_VM 0x0080 /* 221 * Used for internal vm datastructures 222 * only. 223 */ 224#define UMA_ZONE_HASH 0x0100 /* 225 * Use a hash table instead of caching 226 * information in the vm_page. 227 */ 228#define UMA_ZONE_SECONDARY 0x0200 /* Zone is a Secondary Zone */ 229#define UMA_ZONE_REFCNT 0x0400 /* Allocate refcnts in slabs */ 230#define UMA_ZONE_MAXBUCKET 0x0800 /* Use largest buckets */ 231 232/* Definitions for align */ 233#define UMA_ALIGN_PTR (sizeof(void *) - 1) /* Alignment fit for ptr */ 234#define UMA_ALIGN_LONG (sizeof(long) - 1) /* "" long */ 235#define UMA_ALIGN_INT (sizeof(int) - 1) /* "" int */ 236#define UMA_ALIGN_SHORT (sizeof(short) - 1) /* "" short */ 237#define UMA_ALIGN_CHAR (sizeof(char) - 1) /* "" char */ 238#define UMA_ALIGN_CACHE (16 - 1) /* Cache line size align */ 239 240/* 241 * Destroys an empty uma zone. If the zone is not empty uma complains loudly. 242 * 243 * Arguments: 244 * zone The zone we want to destroy. 245 * 246 */ 247void uma_zdestroy(uma_zone_t zone); 248 249/* 250 * Allocates an item out of a zone 251 * 252 * Arguments: 253 * zone The zone we are allocating from 254 * arg This data is passed to the ctor function 255 * flags See sys/malloc.h for available flags. 256 * 257 * Returns: 258 * A non null pointer to an initialized element from the zone is 259 * garanteed if the wait flag is M_WAITOK, otherwise a null pointer may be 260 * returned if the zone is empty or the ctor failed. 261 */ 262 263void *uma_zalloc_arg(uma_zone_t zone, void *arg, int flags); 264 265/* 266 * Allocates an item out of a zone without supplying an argument 267 * 268 * This is just a wrapper for uma_zalloc_arg for convenience. 269 * 270 */ 271static __inline void *uma_zalloc(uma_zone_t zone, int flags); 272 273static __inline void * 274uma_zalloc(uma_zone_t zone, int flags) 275{ 276 return uma_zalloc_arg(zone, NULL, flags); 277} 278 279/* 280 * Frees an item back into the specified zone. 281 * 282 * Arguments: 283 * zone The zone the item was originally allocated out of. 284 * item The memory to be freed. 285 * arg Argument passed to the destructor 286 * 287 * Returns: 288 * Nothing. 289 */ 290 291void uma_zfree_arg(uma_zone_t zone, void *item, void *arg); 292 293/* 294 * Frees an item back to a zone without supplying an argument 295 * 296 * This is just a wrapper for uma_zfree_arg for convenience. 297 * 298 */ 299static __inline void uma_zfree(uma_zone_t zone, void *item); 300 301static __inline void 302uma_zfree(uma_zone_t zone, void *item) 303{ 304 uma_zfree_arg(zone, item, NULL); 305} 306 307/* 308 * XXX The rest of the prototypes in this header are h0h0 magic for the VM. 309 * If you think you need to use it for a normal zone you're probably incorrect. 310 */ 311 312/* 313 * Backend page supplier routines 314 * 315 * Arguments: 316 * zone The zone that is requesting pages 317 * size The number of bytes being requested 318 * pflag Flags for these memory pages, see below. 319 * wait Indicates our willingness to block. 320 * 321 * Returns: 322 * A pointer to the alloced memory or NULL on failure. 323 */ 324 325typedef void *(*uma_alloc)(uma_zone_t zone, int size, u_int8_t *pflag, int wait); 326 327/* 328 * Backend page free routines 329 * 330 * Arguments: 331 * item A pointer to the previously allocated pages 332 * size The original size of the allocation 333 * pflag The flags for the slab. See UMA_SLAB_* below 334 * 335 * Returns: 336 * None 337 */ 338typedef void (*uma_free)(void *item, int size, u_int8_t pflag); 339 340 341 342/* 343 * Sets up the uma allocator. (Called by vm_mem_init) 344 * 345 * Arguments: 346 * bootmem A pointer to memory used to bootstrap the system. 347 * 348 * Returns: 349 * Nothing 350 * 351 * Discussion: 352 * This memory is used for zones which allocate things before the 353 * backend page supplier can give us pages. It should be 354 * UMA_SLAB_SIZE * UMA_BOOT_PAGES bytes. (see uma_int.h) 355 * 356 */ 357 358void uma_startup(void *bootmem); 359 360/* 361 * Finishes starting up the allocator. This should 362 * be called when kva is ready for normal allocs. 363 * 364 * Arguments: 365 * None 366 * 367 * Returns: 368 * Nothing 369 * 370 * Discussion: 371 * uma_startup2 is called by kmeminit() to enable us of uma for malloc. 372 */ 373 374void uma_startup2(void); 375 376/* 377 * Reclaims unused memory for all zones 378 * 379 * Arguments: 380 * None 381 * Returns: 382 * None 383 * 384 * This should only be called by the page out daemon. 385 */ 386 387void uma_reclaim(void); 388 389/* 390 * Switches the backing object of a zone 391 * 392 * Arguments: 393 * zone The zone to update 394 * obj The obj to use for future allocations 395 * size The size of the object to allocate 396 * 397 * Returns: 398 * 0 if kva space can not be allocated 399 * 1 if successful 400 * 401 * Discussion: 402 * A NULL object can be used and uma will allocate one for you. Setting 403 * the size will limit the amount of memory allocated to this zone. 404 * 405 */ 406struct vm_object; 407int uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int size); 408 409/* 410 * Sets a high limit on the number of items allowed in a zone 411 * 412 * Arguments: 413 * zone The zone to limit 414 * 415 * Returns: 416 * Nothing 417 */ 418void uma_zone_set_max(uma_zone_t zone, int nitems); 419 420/* 421 * The following two routines (uma_zone_set_init/fini) 422 * are used to set the backend init/fini pair which acts on an 423 * object as it becomes allocated and is placed in a slab within 424 * the specified zone's backing keg. These should probably not 425 * be changed once allocations have already begun and only 426 * immediately upon zone creation. 427 */ 428void uma_zone_set_init(uma_zone_t zone, uma_init uminit); 429void uma_zone_set_fini(uma_zone_t zone, uma_fini fini); 430 431/* 432 * The following two routines (uma_zone_set_zinit/zfini) are 433 * used to set the zinit/zfini pair which acts on an object as 434 * it passes from the backing Keg's slab cache to the 435 * specified Zone's bucket cache. These should probably not 436 * be changed once allocations have already begun and 437 * only immediately upon zone creation. 438 */ 439void uma_zone_set_zinit(uma_zone_t zone, uma_init zinit); 440void uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini); 441 442/* 443 * Replaces the standard page_alloc or obj_alloc functions for this zone 444 * 445 * Arguments: 446 * zone The zone whos back end allocator is being changed. 447 * allocf A pointer to the allocation function 448 * 449 * Returns: 450 * Nothing 451 * 452 * Discussion: 453 * This could be used to implement pageable allocation, or perhaps 454 * even DMA allocators if used in conjunction with the OFFPAGE 455 * zone flag. 456 */ 457 458void uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf); 459 460/* 461 * Used for freeing memory provided by the allocf above 462 * 463 * Arguments: 464 * zone The zone that intends to use this free routine. 465 * freef The page freeing routine. 466 * 467 * Returns: 468 * Nothing 469 */ 470 471void uma_zone_set_freef(uma_zone_t zone, uma_free freef); 472 473/* 474 * These flags are setable in the allocf and visable in the freef. 475 */ 476#define UMA_SLAB_BOOT 0x01 /* Slab alloced from boot pages */ 477#define UMA_SLAB_KMEM 0x02 /* Slab alloced from kmem_map */ 478#define UMA_SLAB_PRIV 0x08 /* Slab alloced from priv allocator */ 479#define UMA_SLAB_OFFP 0x10 /* Slab is managed separately */ 480#define UMA_SLAB_MALLOC 0x20 /* Slab is a large malloc slab */ 481/* 0x40 and 0x80 are available */ 482 483/* 484 * Used to pre-fill a zone with some number of items 485 * 486 * Arguments: 487 * zone The zone to fill 488 * itemcnt The number of items to reserve 489 * 490 * Returns: 491 * Nothing 492 * 493 * NOTE: This is blocking and should only be done at startup 494 */ 495void uma_prealloc(uma_zone_t zone, int itemcnt); 496 497/* 498 * Used to lookup the reference counter allocated for an item 499 * from a UMA_ZONE_REFCNT zone. For UMA_ZONE_REFCNT zones, 500 * reference counters are allocated for items and stored in 501 * the underlying slab header. 502 * 503 * Arguments: 504 * zone The UMA_ZONE_REFCNT zone to which the item belongs. 505 * item The address of the item for which we want a refcnt. 506 * 507 * Returns: 508 * A pointer to a u_int32_t reference counter. 509 */ 510u_int32_t *uma_find_refcnt(uma_zone_t zone, void *item); 511 512/* 513 * Exported statistics structures to be used by user space monitoring tools. 514 * Statistics stream consusts of a uma_stream_header, followed by a series of 515 * alternative uma_type_header and uma_type_stat structures. Statistics 516 * structures 517 */ 518#define UMA_STREAM_VERSION 0x00000001 519struct uma_stream_header { 520 u_int32_t ush_version; /* Stream format version. */ 521 u_int32_t ush_maxcpus; /* Value of MAXCPU for stream. */ 522 u_int32_t ush_count; /* Number of records. */ 523 u_int32_t _ush_pad; /* Pad/reserved field. */ 524}; 525 526#define UMA_MAX_NAME 32 527struct uma_type_header { 528 /* 529 * Static per-zone data, some extracted from the supporting keg. 530 */ 531 char uth_name[UMA_MAX_NAME]; 532 u_int32_t uth_align; /* Keg: alignment. */ 533 u_int32_t uth_size; /* Keg: requested size of item. */ 534 u_int32_t uth_rsize; /* Keg: real size of item. */ 535 u_int32_t uth_maxpages; /* Keg: maximum number of pages. */ 536 u_int32_t uth_limit; /* Keg: max items to allocate. */ 537 538 /* 539 * Current dynamic zone/keg-derived statistics. 540 */ 541 u_int32_t uth_pages; /* Keg: pages allocated. */ 542 u_int32_t uth_keg_free; /* Keg: items free. */ 543 u_int32_t uth_zone_free; /* Zone: items free. */ 544 u_int32_t uth_bucketsize; /* Zone: desired bucket size. */ 545 u_int32_t _uth_reserved0; /* Reserved. */ 546 u_int64_t uth_allocs; /* Zone: number of allocations. */ 547 u_int64_t uth_frees; /* Zone: number of frees. */ 548 u_int64_t uth_fails; /* Zone: number of alloc failures. */ 549 u_int64_t _uth_reserved1[3]; /* Reserved. */ 550 551}; 552 553struct uma_percpu_stat { 554 u_int64_t ups_allocs; /* Cache: number of alloctions. */ 555 u_int64_t ups_frees; /* Cache: number of frees. */ 556 u_int64_t ups_cache_free; /* Cache: free items in cache. */ 557 u_int64_t _ups_reserved[5]; /* Reserved. */ 558}; 559 560#endif 561