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