1.\" $NetBSD: pool_cache.9,v 1.24 2021/12/22 17:28:17 thorpej Exp $ 2.\" 3.\" Copyright (c)2003 YAMAMOTO Takashi, 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" SUCH DAMAGE. 26.\" 27.\" Copyright (c) 1997, 1999, 2000, 2007, 2008 The NetBSD Foundation, Inc. 28.\" All rights reserved. 29.\" 30.\" This code is derived from software contributed to The NetBSD Foundation 31.\" by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace 32.\" Simulation Facility, NASA Ames Research Center, and by Andrew Doran. 33.\" 34.\" Redistribution and use in source and binary forms, with or without 35.\" modification, are permitted provided that the following conditions 36.\" are met: 37.\" 1. Redistributions of source code must retain the above copyright 38.\" notice, this list of conditions and the following disclaimer. 39.\" 2. Redistributions in binary form must reproduce the above copyright 40.\" notice, this list of conditions and the following disclaimer in the 41.\" documentation and/or other materials provided with the distribution. 42.\" 43.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 44.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 45.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 46.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 47.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 48.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 49.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 50.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 51.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 52.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 53.\" POSSIBILITY OF SUCH DAMAGE. 54.\" 55.\" ------------------------------------------------------------ 56.Dd December 21, 2021 57.Dt POOL_CACHE 9 58.Os 59.\" ------------------------------------------------------------ 60.Sh NAME 61.Nm pool_cache , 62.Nm pool_cache_init , 63.Nm pool_cache_destroy , 64.Nm pool_cache_get_paddr , 65.Nm pool_cache_get , 66.Nm pool_cache_put_paddr , 67.Nm pool_cache_put , 68.Nm pool_cache_destruct_object , 69.Nm pool_cache_invalidate , 70.Nm pool_cache_sethiwat , 71.Nm pool_cache_setlowat , 72.Nm pool_cache_sethardlimit 73.Nd resource-pool cache manager 74.\" ------------------------------------------------------------ 75.Sh SYNOPSIS 76.In sys/pool.h 77.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 78.Ft pool_cache_t 79.Fn pool_cache_init \ 80"size_t size" "u_int align" "u_int align_offset" "int flags" \ 81"const char *name" "struct pool_allocator *palloc" "int ipl" \ 82"int (*ctor)(void *, void *, int)" "void (*dtor)(void *, void *)" "void *arg" 83.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 84.Ft void 85.Fn pool_cache_destroy \ 86"pool_cache_t pc" 87.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 88.Ft void * 89.Fn pool_cache_get_paddr \ 90"pool_cache_t pc" "int flags" "paddr_t *pap" 91.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 92.Ft void * 93.Fn pool_cache_get \ 94"pool_cache_t pc" "int flags" 95.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 96.Ft void 97.Fn pool_cache_put_paddr \ 98"pool_cache_t pc" "void *object" "paddr_t pa" 99.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 100.Ft void 101.Fn pool_cache_put \ 102"pool_cache_t pc" "void *object" 103.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 104.Ft void 105.Fn pool_cache_destruct_object \ 106"pool_cache_t pc" "void *object" 107.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 108.Ft void 109.Fn pool_cache_invalidate \ 110"pool_cache_t pc" 111.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 112.Ft void 113.Fn pool_cache_sethiwat \ 114"pool_cache_t pc" "int n" 115.Ft void 116.Fn pool_cache_setlowat \ 117"pool_cache_t pc" "int n" 118.Ft void 119.Fn pool_cache_sethardlimit \ 120"pool_cache_t pc" "int n" "const char *warnmess" "int ratecap" 121.\" ------------------------------------------------------------ 122.Sh DESCRIPTION 123These utility routines provide management of pools of fixed-sized 124areas of memory. 125Resource pools set aside an amount of memory for exclusive use by the resource 126pool owner. 127This can be used by applications to guarantee the availability of a minimum 128amount of memory needed to continue operation independent of the memory 129resources currently available from the system-wide memory allocator. 130.Pp 131.Nm 132follows the 133.Xr pool 9 134API closely and offers routines that are functionally equivalent to 135their 136.Xr pool 9 137counterparts. 138In addition, 139.Nm 140provides object management functions used to manipulate 141objects allocated from the pool. 142It also maintains global and per-CPU caches, both levels 143of cache work together to allow for low overhead 144allocation and release of objects, and improved L1/L2/L3 hardware 145cache locality in multiprocessor systems. 146.\" ------------------------------------------------------------ 147.Sh FUNCTIONS 148.Bl -tag -width compact 149.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 150.It Fn pool_cache_init "size" "align" "align_offset" "flags" \ 151"name" "palloc" "ipl" "ctor" "dtor" "arg" 152.Pp 153Allocate and initialize a pool cache. 154The arguments are: 155.Bl -tag -width four 156.It Fa size 157.Pp 158Specifies the size of the memory items managed by the pool. 159.It Fa align 160.Pp 161Specifies the memory address alignment of the items returned by 162.Fn pool_cache_get . 163This argument must be a power of two. 164If zero, 165the alignment defaults to an architecture-specific natural alignment. 166.It Fa align_offset 167.Pp 168The offset within an item to which the 169.Fa align 170parameter applies. 171.It Fa flags 172.Pp 173Should be set to zero, 174.Dv PR_NOTOUCH , 175or 176.Dv PR_PSERIALIZE . 177If 178.Dv PR_NOTOUCH 179is given, free items are never used to keep internal state so that 180the pool can be used for non memory backed objects. 181If 182.Dv PR_PSERIALIZE 183is given, then the allocator guarantees that a passive serialization barrier 184equivalent to 185.Dq xc_barrier(0) 186will be performed before either the object's destructor is called or 187before object's backing store is returned to the system. 188.Dv PR_PSERIALIZE 189implies 190.Dv PR_NOTOUCH . 191Because of the guarantees provided by 192.Dv PR_PSERIALIZE , 193objects must never be freed to a pool cache using this option 194from either hard or soft interrupt context, as doing so may block. 195.It Fa name 196.Pp 197The name used to identify the object in diagnostic output. 198.It Fa palloc 199.Pp 200Should be typically be set to NULL, instructing 201.Fn pool_cache_init 202to select an appropriate back-end allocator. 203Alternate allocators can be used to partition space from arbitrary sources. 204Use of alternate allocators is not documented here as it is not a stable, 205endorsed part of the API. 206.It Fa ipl 207.Pp 208Specifies an interrupt priority level that will block all interrupt 209handlers that could potentially access the pool. 210The 211.Nm 212facility provides its own synchronization. 213The users of any given 214.Nm 215need not provide additional synchronization for access to it. 216.It Fa ctor 217.Pp 218Specifies a constructor used to initialize newly allocated objects. 219If no constructor is required, specify 220.Dv NULL . 221The first argument to 222.Fa ctor 223is 224.Fa arg , 225the second is the new object, and the third is 226.Fa flags . 227.It Fa dtor 228.Pp 229Specifies a destructor used to destroy cached objects prior to 230their release to backing store. 231If no destructor is required, specify 232.Dv NULL . 233The first argument to 234.Fa dtor 235is 236.Fa arg , 237and the second is the object. 238.It Fa arg 239.Pp 240This value of this argument will be passed to both the constructor 241and destructor routines. 242.El 243.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 244.It Fn pool_cache_destroy "pc" 245.Pp 246Destroy a pool cache 247.Fa pc . 248All other access to the cache must be stopped before this call 249can be made. 250.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 251.It Fn pool_cache_get_paddr "pc" "flags" "pap" 252.Pp 253Get an object from a pool cache 254.Fa pc . 255If 256.Fa pap 257is not 258.Dv NULL , 259physical address of the object or 260.Dv POOL_PADDR_INVALID 261will be returned via it. 262.Fa flags 263will be passed to 264.Fn pool_get 265function of the backing 266.Xr pool 9 267and the object constructor specified when the pool cache is created by 268.Fn pool_cache_init . 269.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 270.It Fn pool_cache_get "pc" "flags" 271.Pp 272.Fn pool_cache_get 273is the same as 274.Fn pool_cache_get_paddr 275with 276.Dv NULL 277.Fa pap 278argument. 279It's implemented as a macro. 280.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 281.It Fn pool_cache_put_paddr "pc" "object" "pa" 282.Pp 283Put an object 284.Fa object 285back to the pool cache 286.Fa pc . 287.Fa pa 288should be physical address of the object 289.Fa object 290or 291.Dv POOL_PADDR_INVALID . 292If the number of available items in the backing pool exceeds the maximum 293pool size set by 294.Fn pool_cache_sethiwat 295and there are no outstanding requests for pool items, 296the excess items will be returned to the system. 297.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 298.It Fn pool_cache_put "pc" "object" 299.Pp 300.Fn pool_cache_put 301is the same as 302.Fn pool_cache_put_paddr 303with 304.Dv POOL_PADDR_INVALID 305.Fa pa 306argument. 307It's implemented as a macro. 308.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 309.It Fn pool_cache_destruct_object "pc" "object" 310.Pp 311Force destruction of an object 312.Fa object 313and release it back into the pool. 314.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 315.It Fn pool_cache_invalidate "pc" 316.Pp 317Invalidate a pool cache 318.Fa pc . 319All objects in the cache will be destructed and freed back to the pool 320backing the cache. 321For pool caches that vend constructed objects, consumers of this API 322must take care to provide proper synchronization between the input to 323the constructor and cache invalidation. 324.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 325.It Fn pool_cache_sethiwat "pc" "n" 326.Pp 327A pool will attempt to increase its resource usage to keep up with the demand 328for its items. 329Conversely, 330it will return unused memory to the system should the number of accumulated 331free items in the pool exceed a programmable limit. 332The limits for the minimum and maximum number of free items which a pool should 333try to keep available are known as the high and low 334.Sy watermarks . 335.Pp 336The function 337.Fn pool_cache_sethiwat 338sets the backing pool's high water mark. 339As items are freed and the number of free items in the pool is larger 340than the maximum set by this function, 341any completely unused pages are released immediately. 342If this function is not used to specify a maximum number of items, 343the pages will remain associated with the pool until the system runs low 344on memory, 345at which point the VM system will try to reclaim unused pages. 346.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 347.It Fn pool_cache_setlowat "pc" "n" 348.Pp 349Set the minimum number of free items to try to keep in the pool. 350When the number of free items in the pool drops below this threshold, 351a non-blocking attempt is made to allocate memory for more items. 352The number of free items is not guaranteed to remain above this threshold. 353.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 354.It Fn pool_cache_sethardlimit "pc" "n" "warnmess" "ratecap" 355Set the maximum number of total items (both free and allocated) for the backing 356.Xr pool 9 357to 358.Fa n . 359When the hard limit is reached, the warning message 360.Fa warnmess 361will be logged. 362.Fa ratecap 363represents the minimal interval (in seconds) after which another warning 364message is issued when the pool hits its hard limit again. 365.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 366.It Fn pool_cache_prime "pc" "n" 367Set the minimum number of total items (both free and allocated) for the backing 368.Xr pool 9 369to 370.Fa n . 371.El 372.\" ------------------------------------------------------------ 373.Sh CODE REFERENCES 374The 375.Nm 376subsystem is implemented within the file 377.Pa sys/kern/subr_pool.c . 378.Sh SEE ALSO 379.Xr intro 9 , 380.Xr kmem 9 , 381.Xr memoryallocators 9 , 382.Xr percpu 9 , 383.Xr pool 9 384