160727d8bSWarner Losh /*- 208ecce74SRobert Watson * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org> 308ecce74SRobert Watson * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org> 408ecce74SRobert Watson * All rights reserved. 58355f576SJeff Roberson * 68355f576SJeff Roberson * Redistribution and use in source and binary forms, with or without 78355f576SJeff Roberson * modification, are permitted provided that the following conditions 88355f576SJeff Roberson * are met: 98355f576SJeff Roberson * 1. Redistributions of source code must retain the above copyright 108355f576SJeff Roberson * notice unmodified, this list of conditions, and the following 118355f576SJeff Roberson * disclaimer. 128355f576SJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 138355f576SJeff Roberson * notice, this list of conditions and the following disclaimer in the 148355f576SJeff Roberson * documentation and/or other materials provided with the distribution. 158355f576SJeff Roberson * 168355f576SJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 178355f576SJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 188355f576SJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 198355f576SJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 208355f576SJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 218355f576SJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 228355f576SJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 238355f576SJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 248355f576SJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 258355f576SJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 268355f576SJeff Roberson * 278355f576SJeff Roberson * $FreeBSD$ 288355f576SJeff Roberson * 298355f576SJeff Roberson */ 308355f576SJeff Roberson 318355f576SJeff Roberson /* 328355f576SJeff Roberson * uma.h - External definitions for the Universal Memory Allocator 338355f576SJeff Roberson * 348355f576SJeff Roberson */ 358355f576SJeff Roberson 368355f576SJeff Roberson #ifndef VM_UMA_H 378355f576SJeff Roberson #define VM_UMA_H 388355f576SJeff Roberson 398355f576SJeff Roberson #include <sys/param.h> /* For NULL */ 408355f576SJeff Roberson #include <sys/malloc.h> /* For M_* */ 418355f576SJeff Roberson 422db63c5eSGiorgos Keramidas /* User visible parameters */ 438355f576SJeff Roberson #define UMA_SMALLEST_UNIT (PAGE_SIZE / 256) /* Smallest item allocated */ 448355f576SJeff Roberson 458355f576SJeff Roberson /* Types and type defs */ 468355f576SJeff Roberson 478355f576SJeff Roberson struct uma_zone; 488355f576SJeff Roberson /* Opaque type used as a handle to the zone */ 498355f576SJeff Roberson typedef struct uma_zone * uma_zone_t; 508355f576SJeff Roberson 516c125b8dSMohan Srinivasan void zone_drain(uma_zone_t); 526c125b8dSMohan Srinivasan 538355f576SJeff Roberson /* 548355f576SJeff Roberson * Item constructor 558355f576SJeff Roberson * 568355f576SJeff Roberson * Arguments: 578355f576SJeff Roberson * item A pointer to the memory which has been allocated. 588355f576SJeff Roberson * arg The arg field passed to uma_zalloc_arg 598355f576SJeff Roberson * size The size of the allocated item 60b23f72e9SBrian Feldman * flags See zalloc flags 618355f576SJeff Roberson * 628355f576SJeff Roberson * Returns: 63b23f72e9SBrian Feldman * 0 on success 64b23f72e9SBrian Feldman * errno on failure 658355f576SJeff Roberson * 668355f576SJeff Roberson * Discussion: 678355f576SJeff Roberson * The constructor is called just before the memory is returned 6829b4d526SSheldon Hearn * to the user. It may block if necessary. 698355f576SJeff Roberson */ 70b23f72e9SBrian Feldman typedef int (*uma_ctor)(void *mem, int size, void *arg, int flags); 718355f576SJeff Roberson 728355f576SJeff Roberson /* 738355f576SJeff Roberson * Item destructor 748355f576SJeff Roberson * 758355f576SJeff Roberson * Arguments: 768355f576SJeff Roberson * item A pointer to the memory which has been allocated. 778355f576SJeff Roberson * size The size of the item being destructed. 788355f576SJeff Roberson * arg Argument passed through uma_zfree_arg 798355f576SJeff Roberson * 808355f576SJeff Roberson * Returns: 818355f576SJeff Roberson * Nothing 828355f576SJeff Roberson * 838355f576SJeff Roberson * Discussion: 848355f576SJeff Roberson * The destructor may perform operations that differ from those performed 858355f576SJeff Roberson * by the initializer, but it must leave the object in the same state. 868355f576SJeff Roberson * This IS type stable storage. This is called after EVERY zfree call. 878355f576SJeff Roberson */ 888355f576SJeff Roberson typedef void (*uma_dtor)(void *mem, int size, void *arg); 898355f576SJeff Roberson 908355f576SJeff Roberson /* 918355f576SJeff Roberson * Item initializer 928355f576SJeff Roberson * 938355f576SJeff Roberson * Arguments: 948355f576SJeff Roberson * item A pointer to the memory which has been allocated. 958355f576SJeff Roberson * size The size of the item being initialized. 96b23f72e9SBrian Feldman * flags See zalloc flags 978355f576SJeff Roberson * 988355f576SJeff Roberson * Returns: 99b23f72e9SBrian Feldman * 0 on success 100b23f72e9SBrian Feldman * errno on failure 1018355f576SJeff Roberson * 1028355f576SJeff Roberson * Discussion: 1038355f576SJeff Roberson * The initializer is called when the memory is cached in the uma zone. 1042db63c5eSGiorgos Keramidas * The initializer and the destructor should leave the object in the same 1052db63c5eSGiorgos Keramidas * state. 1068355f576SJeff Roberson */ 107b23f72e9SBrian Feldman typedef int (*uma_init)(void *mem, int size, int flags); 1088355f576SJeff Roberson 1098355f576SJeff Roberson /* 1108355f576SJeff Roberson * Item discard function 1118355f576SJeff Roberson * 1128355f576SJeff Roberson * Arguments: 1138355f576SJeff Roberson * item A pointer to memory which has been 'freed' but has not left the 1148355f576SJeff Roberson * zone's cache. 1158355f576SJeff Roberson * size The size of the item being discarded. 1168355f576SJeff Roberson * 1178355f576SJeff Roberson * Returns: 1188355f576SJeff Roberson * Nothing 1198355f576SJeff Roberson * 1208355f576SJeff Roberson * Discussion: 1218355f576SJeff Roberson * This routine is called when memory leaves a zone and is returned to the 1222db63c5eSGiorgos Keramidas * system for other uses. It is the counter-part to the init function. 1238355f576SJeff Roberson */ 1248355f576SJeff Roberson typedef void (*uma_fini)(void *mem, int size); 1258355f576SJeff Roberson 1268355f576SJeff Roberson /* 1278355f576SJeff Roberson * What's the difference between initializing and constructing? 1288355f576SJeff Roberson * 1298355f576SJeff Roberson * The item is initialized when it is cached, and this is the state that the 1308355f576SJeff Roberson * object should be in when returned to the allocator. The purpose of this is 1318355f576SJeff Roberson * to remove some code which would otherwise be called on each allocation by 1328355f576SJeff Roberson * utilizing a known, stable state. This differs from the constructor which 1338355f576SJeff Roberson * will be called on EVERY allocation. 1348355f576SJeff Roberson * 1352db63c5eSGiorgos Keramidas * For example, in the initializer you may want to initialize embedded locks, 1368355f576SJeff Roberson * NULL list pointers, set up initial states, magic numbers, etc. This way if 13729b4d526SSheldon Hearn * the object is held in the allocator and re-used it won't be necessary to 1388355f576SJeff Roberson * re-initialize it. 1398355f576SJeff Roberson * 1408355f576SJeff Roberson * The constructor may be used to lock a data structure, link it on to lists, 1418355f576SJeff Roberson * bump reference counts or total counts of outstanding structures, etc. 1428355f576SJeff Roberson * 1438355f576SJeff Roberson */ 1448355f576SJeff Roberson 1458355f576SJeff Roberson 1468355f576SJeff Roberson /* Function proto types */ 1478355f576SJeff Roberson 1488355f576SJeff Roberson /* 1498355f576SJeff Roberson * Create a new uma zone 1508355f576SJeff Roberson * 1518355f576SJeff Roberson * Arguments: 1522db63c5eSGiorgos Keramidas * name The text name of the zone for debugging and stats. This memory 1538355f576SJeff Roberson * should not be freed until the zone has been deallocated. 1548355f576SJeff Roberson * size The size of the object that is being created. 1552db63c5eSGiorgos Keramidas * ctor The constructor that is called when the object is allocated. 1568355f576SJeff Roberson * dtor The destructor that is called when the object is freed. 1578355f576SJeff Roberson * init An initializer that sets up the initial state of the memory. 1588355f576SJeff Roberson * fini A discard function that undoes initialization done by init. 1598355f576SJeff Roberson * ctor/dtor/init/fini may all be null, see notes above. 1602db63c5eSGiorgos Keramidas * align A bitmask that corresponds to the requested alignment 1618355f576SJeff Roberson * eg 4 would be 0x3 1622db63c5eSGiorgos Keramidas * flags A set of parameters that control the behavior of the zone. 1638355f576SJeff Roberson * 1648355f576SJeff Roberson * Returns: 1658355f576SJeff Roberson * A pointer to a structure which is intended to be opaque to users of 1668355f576SJeff Roberson * the interface. The value may be null if the wait flag is not set. 1678355f576SJeff Roberson */ 168c3bdc05fSAndrew R. Reiter uma_zone_t uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 1698355f576SJeff Roberson uma_init uminit, uma_fini fini, int align, 1702018f30cSMike Silbersack u_int32_t flags); 1718355f576SJeff Roberson 172b60f5b79SJeff Roberson /* 173099a0e58SBosko Milekic * Create a secondary uma zone 174099a0e58SBosko Milekic * 175099a0e58SBosko Milekic * Arguments: 1762db63c5eSGiorgos Keramidas * name The text name of the zone for debugging and stats. This memory 177099a0e58SBosko Milekic * should not be freed until the zone has been deallocated. 1782db63c5eSGiorgos Keramidas * ctor The constructor that is called when the object is allocated. 179099a0e58SBosko Milekic * dtor The destructor that is called when the object is freed. 180099a0e58SBosko Milekic * zinit An initializer that sets up the initial state of the memory 181099a0e58SBosko Milekic * as the object passes from the Keg's slab to the Zone's cache. 182099a0e58SBosko Milekic * zfini A discard function that undoes initialization done by init 183099a0e58SBosko Milekic * as the object passes from the Zone's cache to the Keg's slab. 184099a0e58SBosko Milekic * 185099a0e58SBosko Milekic * ctor/dtor/zinit/zfini may all be null, see notes above. 186099a0e58SBosko Milekic * Note that the zinit and zfini specified here are NOT 187099a0e58SBosko Milekic * exactly the same as the init/fini specified to uma_zcreate() 188099a0e58SBosko Milekic * when creating a master zone. These zinit/zfini are called 189099a0e58SBosko Milekic * on the TRANSITION from keg to zone (and vice-versa). Once 190099a0e58SBosko Milekic * these are set, the primary zone may alter its init/fini 191099a0e58SBosko Milekic * (which are called when the object passes from VM to keg) 192099a0e58SBosko Milekic * using uma_zone_set_init/fini()) as well as its own 193099a0e58SBosko Milekic * zinit/zfini (unset by default for master zone) with 194099a0e58SBosko Milekic * uma_zone_set_zinit/zfini() (note subtle 'z' prefix). 195099a0e58SBosko Milekic * 196b83e441bSBosko Milekic * master A reference to this zone's Master Zone (Primary Zone), 197b83e441bSBosko Milekic * which contains the backing Keg for the Secondary Zone 198b83e441bSBosko Milekic * being added. 199099a0e58SBosko Milekic * 200099a0e58SBosko Milekic * Returns: 201099a0e58SBosko Milekic * A pointer to a structure which is intended to be opaque to users of 202099a0e58SBosko Milekic * the interface. The value may be null if the wait flag is not set. 203099a0e58SBosko Milekic */ 204099a0e58SBosko Milekic uma_zone_t uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 205099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master); 206099a0e58SBosko Milekic 207099a0e58SBosko Milekic /* 208e20a199fSJeff Roberson * Add a second master to a secondary zone. This provides multiple data 209e20a199fSJeff Roberson * backends for objects with the same size. Both masters must have 210e20a199fSJeff Roberson * compatible allocation flags. Presently, UMA_ZONE_MALLOC type zones are 211e20a199fSJeff Roberson * the only supported. 212e20a199fSJeff Roberson * 213e20a199fSJeff Roberson * Returns: 214e20a199fSJeff Roberson * Error on failure, 0 on success. 215e20a199fSJeff Roberson */ 216e20a199fSJeff Roberson int uma_zsecond_add(uma_zone_t zone, uma_zone_t master); 217e20a199fSJeff Roberson 218e20a199fSJeff Roberson /* 219b60f5b79SJeff Roberson * Definitions for uma_zcreate flags 220b60f5b79SJeff Roberson * 221b60f5b79SJeff Roberson * These flags share space with UMA_ZFLAGs in uma_int.h. Be careful not to 2222018f30cSMike Silbersack * overlap when adding new features. 0xf0000000 is in use by uma_int.h. 223b60f5b79SJeff Roberson */ 2248355f576SJeff Roberson #define UMA_ZONE_PAGEABLE 0x0001 /* Return items not fully backed by 2258355f576SJeff Roberson physical memory XXX Not yet */ 2268355f576SJeff Roberson #define UMA_ZONE_ZINIT 0x0002 /* Initialize with zeros */ 2272db63c5eSGiorgos Keramidas #define UMA_ZONE_STATIC 0x0004 /* Statically sized zone */ 2288355f576SJeff Roberson #define UMA_ZONE_OFFPAGE 0x0008 /* Force the slab structure allocation 2298355f576SJeff Roberson off of the real memory */ 2308355f576SJeff Roberson #define UMA_ZONE_MALLOC 0x0010 /* For use by malloc(9) only! */ 2318355f576SJeff Roberson #define UMA_ZONE_NOFREE 0x0020 /* Do not free slabs of this type! */ 23228bc4419SJeff Roberson #define UMA_ZONE_MTXCLASS 0x0040 /* Create a new lock class */ 23399571dc3SJeff Roberson #define UMA_ZONE_VM 0x0080 /* 23499571dc3SJeff Roberson * Used for internal vm datastructures 23599571dc3SJeff Roberson * only. 23699571dc3SJeff Roberson */ 23799571dc3SJeff Roberson #define UMA_ZONE_HASH 0x0100 /* 23899571dc3SJeff Roberson * Use a hash table instead of caching 23999571dc3SJeff Roberson * information in the vm_page. 24099571dc3SJeff Roberson */ 241099a0e58SBosko Milekic #define UMA_ZONE_SECONDARY 0x0200 /* Zone is a Secondary Zone */ 242099a0e58SBosko Milekic #define UMA_ZONE_REFCNT 0x0400 /* Allocate refcnts in slabs */ 243099a0e58SBosko Milekic #define UMA_ZONE_MAXBUCKET 0x0800 /* Use largest buckets */ 244e20a199fSJeff Roberson #define UMA_ZONE_CACHESPREAD 0x1000 /* 245e20a199fSJeff Roberson * Spread memory start locations across 246e20a199fSJeff Roberson * all possible cache lines. May 247e20a199fSJeff Roberson * require many virtually contiguous 248e20a199fSJeff Roberson * backend pages and can fail early. 249e20a199fSJeff Roberson */ 250e20a199fSJeff Roberson #define UMA_ZONE_VTOSLAB 0x2000 /* Zone uses vtoslab for lookup. */ 251e20a199fSJeff Roberson 252e20a199fSJeff Roberson /* 253e20a199fSJeff Roberson * These flags are shared between the keg and zone. In zones wishing to add 254e20a199fSJeff Roberson * new kegs these flags must be compatible. Some are determined based on 255e20a199fSJeff Roberson * physical parameters of the request and may not be provided by the consumer. 256e20a199fSJeff Roberson */ 257e20a199fSJeff Roberson #define UMA_ZONE_INHERIT \ 258e20a199fSJeff Roberson (UMA_ZONE_OFFPAGE | UMA_ZONE_MALLOC | UMA_ZONE_HASH | \ 259e20a199fSJeff Roberson UMA_ZONE_REFCNT | UMA_ZONE_VTOSLAB) 2608355f576SJeff Roberson 2618355f576SJeff Roberson /* Definitions for align */ 2628355f576SJeff Roberson #define UMA_ALIGN_PTR (sizeof(void *) - 1) /* Alignment fit for ptr */ 2638355f576SJeff Roberson #define UMA_ALIGN_LONG (sizeof(long) - 1) /* "" long */ 2648355f576SJeff Roberson #define UMA_ALIGN_INT (sizeof(int) - 1) /* "" int */ 2658355f576SJeff Roberson #define UMA_ALIGN_SHORT (sizeof(short) - 1) /* "" short */ 2668355f576SJeff Roberson #define UMA_ALIGN_CHAR (sizeof(char) - 1) /* "" char */ 2671e319f6dSRobert Watson #define UMA_ALIGN_CACHE (0 - 1) /* Cache line size align */ 2688355f576SJeff Roberson 2698355f576SJeff Roberson /* 2709c2cd7e5SJeff Roberson * Destroys an empty uma zone. If the zone is not empty uma complains loudly. 2718355f576SJeff Roberson * 2728355f576SJeff Roberson * Arguments: 2738355f576SJeff Roberson * zone The zone we want to destroy. 2748355f576SJeff Roberson * 2758355f576SJeff Roberson */ 2769c2cd7e5SJeff Roberson void uma_zdestroy(uma_zone_t zone); 2778355f576SJeff Roberson 2788355f576SJeff Roberson /* 2798355f576SJeff Roberson * Allocates an item out of a zone 2808355f576SJeff Roberson * 2818355f576SJeff Roberson * Arguments: 2828355f576SJeff Roberson * zone The zone we are allocating from 2838355f576SJeff Roberson * arg This data is passed to the ctor function 2842cc35ff9SJeff Roberson * flags See sys/malloc.h for available flags. 2858355f576SJeff Roberson * 2868355f576SJeff Roberson * Returns: 2872db63c5eSGiorgos Keramidas * A non-null pointer to an initialized element from the zone is 2882db63c5eSGiorgos Keramidas * guaranteed if the wait flag is M_WAITOK. Otherwise a null pointer 2892db63c5eSGiorgos Keramidas * may be returned if the zone is empty or the ctor failed. 2908355f576SJeff Roberson */ 2918355f576SJeff Roberson 2922cc35ff9SJeff Roberson void *uma_zalloc_arg(uma_zone_t zone, void *arg, int flags); 2938355f576SJeff Roberson 2948355f576SJeff Roberson /* 2958355f576SJeff Roberson * Allocates an item out of a zone without supplying an argument 2968355f576SJeff Roberson * 2978355f576SJeff Roberson * This is just a wrapper for uma_zalloc_arg for convenience. 2988355f576SJeff Roberson * 2998355f576SJeff Roberson */ 3002cc35ff9SJeff Roberson static __inline void *uma_zalloc(uma_zone_t zone, int flags); 3018355f576SJeff Roberson 3028355f576SJeff Roberson static __inline void * 3032cc35ff9SJeff Roberson uma_zalloc(uma_zone_t zone, int flags) 3048355f576SJeff Roberson { 3052cc35ff9SJeff Roberson return uma_zalloc_arg(zone, NULL, flags); 3068355f576SJeff Roberson } 3078355f576SJeff Roberson 3088355f576SJeff Roberson /* 3098355f576SJeff Roberson * Frees an item back into the specified zone. 3108355f576SJeff Roberson * 3118355f576SJeff Roberson * Arguments: 3128355f576SJeff Roberson * zone The zone the item was originally allocated out of. 3138355f576SJeff Roberson * item The memory to be freed. 3148355f576SJeff Roberson * arg Argument passed to the destructor 3158355f576SJeff Roberson * 3168355f576SJeff Roberson * Returns: 3178355f576SJeff Roberson * Nothing. 3188355f576SJeff Roberson */ 3198355f576SJeff Roberson 3208355f576SJeff Roberson void uma_zfree_arg(uma_zone_t zone, void *item, void *arg); 3218355f576SJeff Roberson 3228355f576SJeff Roberson /* 3238355f576SJeff Roberson * Frees an item back to a zone without supplying an argument 3248355f576SJeff Roberson * 3258355f576SJeff Roberson * This is just a wrapper for uma_zfree_arg for convenience. 3268355f576SJeff Roberson * 3278355f576SJeff Roberson */ 3288355f576SJeff Roberson static __inline void uma_zfree(uma_zone_t zone, void *item); 3298355f576SJeff Roberson 3308355f576SJeff Roberson static __inline void 3318355f576SJeff Roberson uma_zfree(uma_zone_t zone, void *item) 3328355f576SJeff Roberson { 333f6e34b82SMark Murray uma_zfree_arg(zone, item, NULL); 3348355f576SJeff Roberson } 3358355f576SJeff Roberson 3368355f576SJeff Roberson /* 3378355f576SJeff Roberson * XXX The rest of the prototypes in this header are h0h0 magic for the VM. 3388355f576SJeff Roberson * If you think you need to use it for a normal zone you're probably incorrect. 3398355f576SJeff Roberson */ 3408355f576SJeff Roberson 3418355f576SJeff Roberson /* 3428355f576SJeff Roberson * Backend page supplier routines 3438355f576SJeff Roberson * 3448355f576SJeff Roberson * Arguments: 3452db63c5eSGiorgos Keramidas * zone The zone that is requesting pages. 3462db63c5eSGiorgos Keramidas * size The number of bytes being requested. 3478355f576SJeff Roberson * pflag Flags for these memory pages, see below. 3488355f576SJeff Roberson * wait Indicates our willingness to block. 3498355f576SJeff Roberson * 3508355f576SJeff Roberson * Returns: 3512db63c5eSGiorgos Keramidas * A pointer to the allocated memory or NULL on failure. 3528355f576SJeff Roberson */ 3538355f576SJeff Roberson 3548355f576SJeff Roberson typedef void *(*uma_alloc)(uma_zone_t zone, int size, u_int8_t *pflag, int wait); 3558355f576SJeff Roberson 3568355f576SJeff Roberson /* 3578355f576SJeff Roberson * Backend page free routines 3588355f576SJeff Roberson * 3598355f576SJeff Roberson * Arguments: 3602db63c5eSGiorgos Keramidas * item A pointer to the previously allocated pages. 3612db63c5eSGiorgos Keramidas * size The original size of the allocation. 3622db63c5eSGiorgos Keramidas * pflag The flags for the slab. See UMA_SLAB_* below. 3638355f576SJeff Roberson * 3648355f576SJeff Roberson * Returns: 3658355f576SJeff Roberson * None 3668355f576SJeff Roberson */ 3678355f576SJeff Roberson typedef void (*uma_free)(void *item, int size, u_int8_t pflag); 3688355f576SJeff Roberson 3698355f576SJeff Roberson 3708355f576SJeff Roberson 3718355f576SJeff Roberson /* 3728355f576SJeff Roberson * Sets up the uma allocator. (Called by vm_mem_init) 3738355f576SJeff Roberson * 3748355f576SJeff Roberson * Arguments: 3758355f576SJeff Roberson * bootmem A pointer to memory used to bootstrap the system. 3768355f576SJeff Roberson * 3778355f576SJeff Roberson * Returns: 3788355f576SJeff Roberson * Nothing 3798355f576SJeff Roberson * 3808355f576SJeff Roberson * Discussion: 3818355f576SJeff Roberson * This memory is used for zones which allocate things before the 3828355f576SJeff Roberson * backend page supplier can give us pages. It should be 3833803b26bSDag-Erling Smørgrav * UMA_SLAB_SIZE * boot_pages bytes. (see uma_int.h) 3848355f576SJeff Roberson * 3858355f576SJeff Roberson */ 3868355f576SJeff Roberson 3873803b26bSDag-Erling Smørgrav void uma_startup(void *bootmem, int boot_pages); 3888355f576SJeff Roberson 3898355f576SJeff Roberson /* 3908355f576SJeff Roberson * Finishes starting up the allocator. This should 3918355f576SJeff Roberson * be called when kva is ready for normal allocs. 3928355f576SJeff Roberson * 3938355f576SJeff Roberson * Arguments: 39499571dc3SJeff Roberson * None 3958355f576SJeff Roberson * 3968355f576SJeff Roberson * Returns: 3978355f576SJeff Roberson * Nothing 3988355f576SJeff Roberson * 3998355f576SJeff Roberson * Discussion: 40099571dc3SJeff Roberson * uma_startup2 is called by kmeminit() to enable us of uma for malloc. 4018355f576SJeff Roberson */ 4028355f576SJeff Roberson 40399571dc3SJeff Roberson void uma_startup2(void); 4048355f576SJeff Roberson 4058355f576SJeff Roberson /* 4068355f576SJeff Roberson * Reclaims unused memory for all zones 4078355f576SJeff Roberson * 4088355f576SJeff Roberson * Arguments: 4098355f576SJeff Roberson * None 4108355f576SJeff Roberson * Returns: 4118355f576SJeff Roberson * None 4128355f576SJeff Roberson * 4138355f576SJeff Roberson * This should only be called by the page out daemon. 4148355f576SJeff Roberson */ 4158355f576SJeff Roberson 4168355f576SJeff Roberson void uma_reclaim(void); 4178355f576SJeff Roberson 4188355f576SJeff Roberson /* 4191e319f6dSRobert Watson * Sets the alignment mask to be used for all zones requesting cache 4201e319f6dSRobert Watson * alignment. Should be called by MD boot code prior to starting VM/UMA. 4211e319f6dSRobert Watson * 4221e319f6dSRobert Watson * Arguments: 4231e319f6dSRobert Watson * align The alignment mask 4241e319f6dSRobert Watson * 4251e319f6dSRobert Watson * Returns: 4261e319f6dSRobert Watson * Nothing 4271e319f6dSRobert Watson */ 4281e319f6dSRobert Watson void uma_set_align(int align); 4291e319f6dSRobert Watson 4301e319f6dSRobert Watson /* 4318355f576SJeff Roberson * Switches the backing object of a zone 4328355f576SJeff Roberson * 4338355f576SJeff Roberson * Arguments: 4342db63c5eSGiorgos Keramidas * zone The zone to update. 4352db63c5eSGiorgos Keramidas * obj The VM object to use for future allocations. 4362db63c5eSGiorgos Keramidas * size The size of the object to allocate. 4378355f576SJeff Roberson * 4388355f576SJeff Roberson * Returns: 4398355f576SJeff Roberson * 0 if kva space can not be allocated 4408355f576SJeff Roberson * 1 if successful 4418355f576SJeff Roberson * 4428355f576SJeff Roberson * Discussion: 4438355f576SJeff Roberson * A NULL object can be used and uma will allocate one for you. Setting 4448355f576SJeff Roberson * the size will limit the amount of memory allocated to this zone. 4458355f576SJeff Roberson * 4468355f576SJeff Roberson */ 4478355f576SJeff Roberson struct vm_object; 4488355f576SJeff Roberson int uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int size); 4498355f576SJeff Roberson 450736ee590SJeff Roberson /* 451736ee590SJeff Roberson * Sets a high limit on the number of items allowed in a zone 452736ee590SJeff Roberson * 453736ee590SJeff Roberson * Arguments: 454736ee590SJeff Roberson * zone The zone to limit 455*1c6cae97SLawrence Stewart * nitems The requested upper limit on the number of items allowed 456736ee590SJeff Roberson * 457736ee590SJeff Roberson * Returns: 458*1c6cae97SLawrence Stewart * int The effective value of nitems after rounding up based on page size 459736ee590SJeff Roberson */ 460*1c6cae97SLawrence Stewart int uma_zone_set_max(uma_zone_t zone, int nitems); 4618355f576SJeff Roberson 4628355f576SJeff Roberson /* 463e49471b0SAndre Oppermann * Obtains the effective limit on the number of items in a zone 464e49471b0SAndre Oppermann * 465e49471b0SAndre Oppermann * Arguments: 466e49471b0SAndre Oppermann * zone The zone to obtain the effective limit from 467e49471b0SAndre Oppermann * 468e49471b0SAndre Oppermann * Return: 469e49471b0SAndre Oppermann * 0 No limit 470e49471b0SAndre Oppermann * int The effective limit of the zone 471e49471b0SAndre Oppermann */ 472e49471b0SAndre Oppermann int uma_zone_get_max(uma_zone_t zone); 473e49471b0SAndre Oppermann 474e49471b0SAndre Oppermann /* 475c4ae7908SLawrence Stewart * Obtains the approximate current number of items allocated from a zone 476c4ae7908SLawrence Stewart * 477c4ae7908SLawrence Stewart * Arguments: 478c4ae7908SLawrence Stewart * zone The zone to obtain the current allocation count from 479c4ae7908SLawrence Stewart * 480c4ae7908SLawrence Stewart * Return: 481c4ae7908SLawrence Stewart * int The approximate current number of items allocated from the zone 482c4ae7908SLawrence Stewart */ 483c4ae7908SLawrence Stewart int uma_zone_get_cur(uma_zone_t zone); 484c4ae7908SLawrence Stewart 485c4ae7908SLawrence Stewart /* 486099a0e58SBosko Milekic * The following two routines (uma_zone_set_init/fini) 487099a0e58SBosko Milekic * are used to set the backend init/fini pair which acts on an 488099a0e58SBosko Milekic * object as it becomes allocated and is placed in a slab within 489099a0e58SBosko Milekic * the specified zone's backing keg. These should probably not 4902db63c5eSGiorgos Keramidas * be changed once allocations have already begun, but only be set 491099a0e58SBosko Milekic * immediately upon zone creation. 492099a0e58SBosko Milekic */ 493099a0e58SBosko Milekic void uma_zone_set_init(uma_zone_t zone, uma_init uminit); 494099a0e58SBosko Milekic void uma_zone_set_fini(uma_zone_t zone, uma_fini fini); 495099a0e58SBosko Milekic 496099a0e58SBosko Milekic /* 497099a0e58SBosko Milekic * The following two routines (uma_zone_set_zinit/zfini) are 498099a0e58SBosko Milekic * used to set the zinit/zfini pair which acts on an object as 499099a0e58SBosko Milekic * it passes from the backing Keg's slab cache to the 500099a0e58SBosko Milekic * specified Zone's bucket cache. These should probably not 5012db63c5eSGiorgos Keramidas * be changed once allocations have already begun, but only be set 5022db63c5eSGiorgos Keramidas * immediately upon zone creation. 503099a0e58SBosko Milekic */ 504099a0e58SBosko Milekic void uma_zone_set_zinit(uma_zone_t zone, uma_init zinit); 505099a0e58SBosko Milekic void uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini); 506099a0e58SBosko Milekic 507099a0e58SBosko Milekic /* 5088355f576SJeff Roberson * Replaces the standard page_alloc or obj_alloc functions for this zone 5098355f576SJeff Roberson * 5108355f576SJeff Roberson * Arguments: 5112db63c5eSGiorgos Keramidas * zone The zone whose backend allocator is being changed. 5128355f576SJeff Roberson * allocf A pointer to the allocation function 5138355f576SJeff Roberson * 5148355f576SJeff Roberson * Returns: 5158355f576SJeff Roberson * Nothing 5168355f576SJeff Roberson * 5178355f576SJeff Roberson * Discussion: 5188355f576SJeff Roberson * This could be used to implement pageable allocation, or perhaps 5198355f576SJeff Roberson * even DMA allocators if used in conjunction with the OFFPAGE 5208355f576SJeff Roberson * zone flag. 5218355f576SJeff Roberson */ 5228355f576SJeff Roberson 5238355f576SJeff Roberson void uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf); 5248355f576SJeff Roberson 5258355f576SJeff Roberson /* 5268355f576SJeff Roberson * Used for freeing memory provided by the allocf above 5278355f576SJeff Roberson * 5288355f576SJeff Roberson * Arguments: 5298355f576SJeff Roberson * zone The zone that intends to use this free routine. 5308355f576SJeff Roberson * freef The page freeing routine. 5318355f576SJeff Roberson * 5328355f576SJeff Roberson * Returns: 5338355f576SJeff Roberson * Nothing 5348355f576SJeff Roberson */ 5358355f576SJeff Roberson 5368355f576SJeff Roberson void uma_zone_set_freef(uma_zone_t zone, uma_free freef); 5378355f576SJeff Roberson 5388355f576SJeff Roberson /* 5392db63c5eSGiorgos Keramidas * These flags are setable in the allocf and visible in the freef. 5408355f576SJeff Roberson */ 5418355f576SJeff Roberson #define UMA_SLAB_BOOT 0x01 /* Slab alloced from boot pages */ 5428355f576SJeff Roberson #define UMA_SLAB_KMEM 0x02 /* Slab alloced from kmem_map */ 5437630c265SAlan Cox #define UMA_SLAB_KERNEL 0x04 /* Slab alloced from kernel_map */ 5448355f576SJeff Roberson #define UMA_SLAB_PRIV 0x08 /* Slab alloced from priv allocator */ 545c235bfa5SJeff Roberson #define UMA_SLAB_OFFP 0x10 /* Slab is managed separately */ 5468355f576SJeff Roberson #define UMA_SLAB_MALLOC 0x20 /* Slab is a large malloc slab */ 5478355f576SJeff Roberson /* 0x40 and 0x80 are available */ 5488355f576SJeff Roberson 5498355f576SJeff Roberson /* 5508355f576SJeff Roberson * Used to pre-fill a zone with some number of items 5518355f576SJeff Roberson * 5528355f576SJeff Roberson * Arguments: 5538355f576SJeff Roberson * zone The zone to fill 5548355f576SJeff Roberson * itemcnt The number of items to reserve 5558355f576SJeff Roberson * 5568355f576SJeff Roberson * Returns: 5578355f576SJeff Roberson * Nothing 5588355f576SJeff Roberson * 5598355f576SJeff Roberson * NOTE: This is blocking and should only be done at startup 5608355f576SJeff Roberson */ 5618355f576SJeff Roberson void uma_prealloc(uma_zone_t zone, int itemcnt); 5628355f576SJeff Roberson 563099a0e58SBosko Milekic /* 564099a0e58SBosko Milekic * Used to lookup the reference counter allocated for an item 565099a0e58SBosko Milekic * from a UMA_ZONE_REFCNT zone. For UMA_ZONE_REFCNT zones, 566099a0e58SBosko Milekic * reference counters are allocated for items and stored in 567099a0e58SBosko Milekic * the underlying slab header. 568099a0e58SBosko Milekic * 569099a0e58SBosko Milekic * Arguments: 570099a0e58SBosko Milekic * zone The UMA_ZONE_REFCNT zone to which the item belongs. 571099a0e58SBosko Milekic * item The address of the item for which we want a refcnt. 572099a0e58SBosko Milekic * 573099a0e58SBosko Milekic * Returns: 574099a0e58SBosko Milekic * A pointer to a u_int32_t reference counter. 575099a0e58SBosko Milekic */ 576099a0e58SBosko Milekic u_int32_t *uma_find_refcnt(uma_zone_t zone, void *item); 5778355f576SJeff Roberson 5787a52a97eSRobert Watson /* 579663b416fSJohn Baldwin * Used to determine if a fixed-size zone is exhausted. 580663b416fSJohn Baldwin * 581663b416fSJohn Baldwin * Arguments: 582663b416fSJohn Baldwin * zone The zone to check 583663b416fSJohn Baldwin * 584663b416fSJohn Baldwin * Returns: 585663b416fSJohn Baldwin * Non-zero if zone is exhausted. 586663b416fSJohn Baldwin */ 587663b416fSJohn Baldwin int uma_zone_exhausted(uma_zone_t zone); 5886c125b8dSMohan Srinivasan int uma_zone_exhausted_nolock(uma_zone_t zone); 589663b416fSJohn Baldwin 590663b416fSJohn Baldwin /* 5917a52a97eSRobert Watson * Exported statistics structures to be used by user space monitoring tools. 5922db63c5eSGiorgos Keramidas * Statistics stream consists of a uma_stream_header, followed by a series of 5932db63c5eSGiorgos Keramidas * alternative uma_type_header and uma_type_stat structures. 5947a52a97eSRobert Watson */ 5957a52a97eSRobert Watson #define UMA_STREAM_VERSION 0x00000001 5967a52a97eSRobert Watson struct uma_stream_header { 5977a52a97eSRobert Watson u_int32_t ush_version; /* Stream format version. */ 5987a52a97eSRobert Watson u_int32_t ush_maxcpus; /* Value of MAXCPU for stream. */ 5997a52a97eSRobert Watson u_int32_t ush_count; /* Number of records. */ 6007a52a97eSRobert Watson u_int32_t _ush_pad; /* Pad/reserved field. */ 6017a52a97eSRobert Watson }; 6027a52a97eSRobert Watson 603cbbb4a00SRobert Watson #define UTH_MAX_NAME 32 604cbbb4a00SRobert Watson #define UTH_ZONE_SECONDARY 0x00000001 6057a52a97eSRobert Watson struct uma_type_header { 6067a52a97eSRobert Watson /* 6077a52a97eSRobert Watson * Static per-zone data, some extracted from the supporting keg. 6087a52a97eSRobert Watson */ 609cbbb4a00SRobert Watson char uth_name[UTH_MAX_NAME]; 6107a52a97eSRobert Watson u_int32_t uth_align; /* Keg: alignment. */ 6117a52a97eSRobert Watson u_int32_t uth_size; /* Keg: requested size of item. */ 6127a52a97eSRobert Watson u_int32_t uth_rsize; /* Keg: real size of item. */ 6137a52a97eSRobert Watson u_int32_t uth_maxpages; /* Keg: maximum number of pages. */ 6147a52a97eSRobert Watson u_int32_t uth_limit; /* Keg: max items to allocate. */ 6157a52a97eSRobert Watson 6167a52a97eSRobert Watson /* 6177a52a97eSRobert Watson * Current dynamic zone/keg-derived statistics. 6187a52a97eSRobert Watson */ 6197a52a97eSRobert Watson u_int32_t uth_pages; /* Keg: pages allocated. */ 6207a52a97eSRobert Watson u_int32_t uth_keg_free; /* Keg: items free. */ 6217a52a97eSRobert Watson u_int32_t uth_zone_free; /* Zone: items free. */ 6227a52a97eSRobert Watson u_int32_t uth_bucketsize; /* Zone: desired bucket size. */ 623cbbb4a00SRobert Watson u_int32_t uth_zone_flags; /* Zone: flags. */ 6247a52a97eSRobert Watson u_int64_t uth_allocs; /* Zone: number of allocations. */ 6257a52a97eSRobert Watson u_int64_t uth_frees; /* Zone: number of frees. */ 6262019094aSRobert Watson u_int64_t uth_fails; /* Zone: number of alloc failures. */ 627bf965959SSean Bruno u_int64_t uth_sleeps; /* Zone: number of alloc sleeps. */ 628bf965959SSean Bruno u_int64_t _uth_reserved1[2]; /* Reserved. */ 6297a52a97eSRobert Watson }; 6307a52a97eSRobert Watson 6317a52a97eSRobert Watson struct uma_percpu_stat { 6322db63c5eSGiorgos Keramidas u_int64_t ups_allocs; /* Cache: number of allocations. */ 6337a52a97eSRobert Watson u_int64_t ups_frees; /* Cache: number of frees. */ 6347a52a97eSRobert Watson u_int64_t ups_cache_free; /* Cache: free items in cache. */ 6357a52a97eSRobert Watson u_int64_t _ups_reserved[5]; /* Reserved. */ 6367a52a97eSRobert Watson }; 6377a52a97eSRobert Watson 6388355f576SJeff Roberson #endif 639