xref: /netbsd-src/share/man/man9/vmem.9 (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1.\"	$NetBSD: vmem.9,v 1.15 2013/01/29 22:02:17 wiz Exp $
2.\"
3.\" Copyright (c)2006 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.\" ------------------------------------------------------------
28.Dd January 29, 2013
29.Dt VMEM 9
30.Os
31.\" ------------------------------------------------------------
32.Sh NAME
33.Nm vmem
34.Nd virtual memory allocator
35.\" ------------------------------------------------------------
36.Sh SYNOPSIS
37.In sys/vmem.h
38.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39.Ft vmem_t *
40.Fn vmem_create \
41"const char *name" "vmem_addr_t base" "vmem_size_t size" "vmem_size_t quantum" \
42"int (*allocfn)(void *, vmem_size_t, vm_flag_t, vmem_addr_t *)" \
43"void (*freefn)(void *, vmem_addr_t, vmem_size_t)" \
44"void *arg" "vmem_size_t qcache_max" "vm_flag_t flags" "int ipl"
45.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46.Ft vmem_t *
47.Fn vmem_xcreate \
48"const char *name" "vmem_addr_t base" "vmem_size_t size" "vmem_size_t quantum" \
49"int (*allocfn)(void *, vmem_size_t, vmem_size_t *, vm_flag_t, vmem_addr_t *)" \
50"void (*freefn)(void *, vmem_addr_t, vmem_size_t)" \
51"void *arg" "vmem_size_t qcache_max" "vm_flag_t flags" "int ipl"
52.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53.Ft int
54.Fn vmem_add \
55"vmem_t *vm" "vmem_addr_t addr" "vmem_size_t size" "vm_flag_t flags" "vmem_addr_t *addrp"
56.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57.Ft int
58.Fn vmem_xalloc \
59"vmem_t *vm" "vmem_size_t size" "vmem_size_t align" \
60"vmem_size_t phase" "vmem_size_t nocross" "vmem_addr_t minaddr" \
61"vmem_addr_t maxaddr" "vm_flag_t flags" "vmem_addr_t *addrp"
62.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
63.Ft void
64.Fn vmem_xfree "vmem_t *vm" "vmem_addr_t addr" "vmem_size_t size"
65.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
66.Ft int
67.Fn vmem_alloc "vmem_t *vm" "vmem_size_t size" "vm_flag_t flags" "vmem_addr_t *addrp"
68.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
69.Ft void
70.Fn vmem_free "vmem_t *vm" "vmem_addr_t addr" "vmem_size_t size"
71.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72.Ft void
73.Fn vmem_destroy "vmem_t *vm"
74.\" ------------------------------------------------------------
75.Sh DESCRIPTION
76The
77.Nm
78is a general purpose resource allocator.
79Despite its name, it can be used for arbitrary resources
80other than virtual memory.
81.Pp
82.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
83.Fn vmem_create
84creates a new vmem arena.
85.Pp
86.Bl -tag -width qcache_max
87.It Fa name
88The string to describe the vmem.
89.It Fa base
90The start address of the initial span.
91Pass
92.Dv 0
93if no initial span is required.
94.It Fa size
95The size of the initial span.
96Pass
97.Dv 0
98if no initial span is required.
99.It Fa quantum
100The smallest unit of allocation.
101.It Fa allocfn
102The callback function used to import spans from the backend arena.
103Set both
104.Fa allocfn
105and
106.Fa freefn
107to
108.Dv NULL
109to disable automatic imports.
110.Nm
111calls
112.Fo "(*allocfn)"
113.Fa arg
114.Fa size
115.Fa flags
116.Fa "\*[Am]addrp"
117.Fc
118to import a span of size at least
119.Fa size .
120.Fa allocfn
121should accept the same
122.Fa flags
123as
124.Fn vmem_alloc .
125.Fa allocfn
126must return
127.Dv ENOMEM
128to indicate failure, or 0 on success.
129If
130.Fa allocfn
131succeeds, it must write the starting address of the imported span to
132.Fa addrp .
133.It Fa freefn
134The callback function used to free spans to the backend arena.
135.Fa freefn
136may be
137.Dv NULL
138even if
139.Fa allocfn
140is not
141.Dv NULL .
142.Nm
143calls
144.Fn "(*freefn)" arg addr size
145to return to
146.Fa arg
147a span of size
148.Fa size ,
149starting at
150.Fa addr ,
151that was previously allocated by
152.Fa allocfn .
153.It Fa arg
154The backend arena.
155.Fa arg
156may be
157.Dv NULL .
158.Nm
159passes
160.Fa arg
161as the first argument of
162.Fa allocfn
163and
164.Fa freefn .
165.It Fa qcache_max
166The largest size of allocations which can be served by quantum cache.
167It is merely a hint and can be ignored.
168.It Fa flags
169Either of:
170.Bl -tag -width VM_NOSLEEP
171.It Dv VM_SLEEP
172Can sleep until enough resources are available.
173.It Dv VM_NOSLEEP
174Don't sleep.
175Immediately return
176.Dv NULL
177if there are not enough resources available.
178.El
179.It Fa ipl
180Interrupt level to be blocked for allocating from vmem.
181.El
182.Pp
183.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
184.Fn vmem_xcreate
185creates a new vmem arena.
186.Pp
187.Bl -tag -width qcache_max
188.It Fa name
189The string to describe the vmem.
190.It Fa base
191The start address of the initial span.
192Pass
193.Dv 0
194if no initial span is required.
195.It Fa size
196The size of the initial span.
197Pass
198.Dv 0
199if no initial span is required.
200.It Fa quantum
201The smallest unit of allocation.
202.It Fa allocfn
203The callback function used to import spans from the backend arena.
204Set both
205.Fa allocfn
206and
207.Fa freefn
208to
209.Dv NULL
210to disable automatic imports.
211.Nm
212calls
213.Fo "(*allocfn)"
214.Fa arg
215.Fa size
216.Fa "\*[Am]actualsize"
217.Fa flags
218.Fa "\*[Am]addrp"
219.Fc
220to import a span of size at least
221.Fa size .
222.Fa allocfn
223should accept the same
224.Fa flags
225as
226.Fn vmem_alloc .
227.Fa allocfn
228must return
229.Dv ENOMEM
230to indicate failure, or 0 on success.
231If
232.Fa allocfn
233succeeds, it must write the actual size of the allocation to
234.Fa actualsize
235and the starting address of the imported span to
236.Fa addrp .
237The actual size will always be greater than or equal to the requested size.
238.It Fa freefn
239The callback function used to free spans to the backend arena.
240.Fa freefn
241may be
242.Dv NULL
243even if
244.Fa allocfn
245is not
246.Dv NULL .
247.Nm
248calls
249.Fn "(*freefn)" arg addr size
250to return to
251.Fa arg
252a span of size
253.Fa size ,
254starting at
255.Fa addr ,
256that was previously allocated by
257.Fa allocfn .
258.It Fa arg
259The backend arena.
260.Fa arg
261may be
262.Dv NULL .
263.Nm
264passes
265.Fa arg
266as the first argument of
267.Fa allocfn
268and
269.Fa freefn .
270.It Fa qcache_max
271The largest size of allocations which can be served by quantum cache.
272It is merely a hint and can be ignored.
273.It Fa flags
274Either of:
275.Bl -tag -width VM_NOSLEEP
276.It Dv VM_SLEEP
277Can sleep until enough resources are available.
278.It Dv VM_NOSLEEP
279Don't sleep.
280Immediately return
281.Dv NULL
282if there are not enough resources available.
283.El
284.It Fa ipl
285Interrupt level to be blocked for allocating from vmem.
286.El
287.Pp
288.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
289.Fn vmem_add
290adds a span of size
291.Fa size
292starting at
293.Fa addr
294to the arena.
295Returns
2960
297on success,
298.Dv ENOMEM
299on failure.
300.Fa flags
301should be one of:
302.Bl -tag -width VM_NOSLEEP
303.It Dv VM_SLEEP
304Can sleep until enough resources are available.
305.It Dv VM_NOSLEEP
306Don't sleep.
307Immediately return
308.Dv ENOMEM
309if there are not enough resources available.
310.El
311.Pp
312.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
313.Fn vmem_xalloc
314allocates a resource from the arena.
315.Pp
316.Bl -tag -width nocross
317.It Fa vm
318The arena which we allocate from.
319.It Fa size
320Specify the size of the allocation.
321.It Fa align
322If zero, don't care about the alignment of the allocation.
323Otherwise, request a resource segment starting at
324offset
325.Fa phase
326from an
327.Fa align
328aligned boundary.
329.It Fa phase
330See the above description of
331.Fa align .
332If
333.Fa align
334is zero,
335.Fa phase
336should be zero.
337Otherwise,
338.Fa phase
339should be smaller than
340.Fa align .
341.It Fa nocross
342Request a resource which doesn't cross
343.Fa nocross
344aligned boundary.
345.It Fa minaddr
346Specify the minimum address which can be allocated, or
347.Dv VMEM_ADDR_MIN
348if the caller does not care.
349.It Fa maxaddr
350Specify the maximum address which can be allocated, or
351.Dv VMEM_ADDR_MAX
352if the caller does not care.
353.It Fa flags
354A bitwise OR of an allocation strategy and a sleep flag.
355.Pp
356The allocation strategy is one of:
357.Bl -tag -width VM_INSTANTFIT
358.It Dv VM_BESTFIT
359Prefer space efficiency.
360.It Dv VM_INSTANTFIT
361Prefer performance.
362.El
363.Pp
364The sleep flag should be one of:
365.Bl -tag -width VM_NOSLEEP
366.It Dv VM_SLEEP
367Can sleep until enough resources are available.
368.It Dv VM_NOSLEEP
369Don't sleep.
370Immediately return
371.Dv ENOMEM
372if there are not enough resources available.
373.El
374.It Fa addrp
375On success, if
376.Fa addrp
377is not
378.Dv NULL ,
379.Fn vmem_xalloc
380overwrites it with the start address of the allocated span.
381.El
382.Pp
383.\" ------------------------------------------------------------
384.Fn vmem_xfree
385frees resource allocated by
386.Fn vmem_xalloc
387to the arena.
388.Pp
389.Bl -tag -width addr
390.It Fa vm
391The arena which we free to.
392.It Fa addr
393The resource being freed.
394It must be the one returned by
395.Fn vmem_xalloc .
396Notably, it must not be the one from
397.Fn vmem_alloc .
398Otherwise, the behaviour is undefined.
399.It Fa size
400The size of the resource being freed.
401It must be the same as the
402.Fa size
403argument used for
404.Fn vmem_xalloc .
405.El
406.Pp
407.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
408.Fn vmem_alloc
409allocates a resource from the arena.
410.Pp
411.Bl -tag -width flags
412.It Fa vm
413The arena which we allocate from.
414.It Fa size
415Specify the size of the allocation.
416.It Fa flags
417A bitwise OR of an allocation strategy and a sleep flag.
418.Pp
419The allocation strategy is one of:
420.Bl -tag -width VM_INSTANTFIT
421.It Dv VM_BESTFIT
422Prefer space efficiency.
423.It Dv VM_INSTANTFIT
424Prefer performance.
425.El
426.Pp
427The sleep flag should be one of:
428.Bl -tag -width VM_NOSLEEP
429.It Dv VM_SLEEP
430Can sleep until enough resources are available.
431.It Dv VM_NOSLEEP
432Don't sleep.
433Immediately return
434.Dv ENOMEM
435if there are not enough resources available.
436.El
437.It Fa addrp
438On success, if
439.Fa addrp
440is not
441.Dv NULL ,
442.Fn vmem_alloc
443overwrites it with the start address of the allocated span.
444.El
445.Pp
446.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
447.Fn vmem_free
448frees resource allocated by
449.Fn vmem_alloc
450to the arena.
451.Pp
452.Bl -tag -width addr
453.It Fa vm
454The arena which we free to.
455.It Fa addr
456The resource being freed.
457It must be the one returned by
458.Fn vmem_alloc .
459Notably, it must not be the one from
460.Fn vmem_xalloc .
461Otherwise, the behaviour is undefined.
462.It Fa size
463The size of the resource being freed.
464It must be the same as the
465.Fa size
466argument used for
467.Fn vmem_alloc .
468.El
469.Pp
470.\" ------------------------------------------------------------
471.Fn vmem_destroy
472destroys a vmem arena.
473.Pp
474.Bl -tag -width vm
475.It Fa vm
476The vmem arena being destroyed.
477The caller should ensure that no one will use it anymore.
478.El
479.\" ------------------------------------------------------------
480.Sh RETURN VALUES
481.Fn vmem_create
482return a pointer to the newly allocated vmem_t.
483Otherwise, it returns
484.Dv NULL .
485.Pp
486On success,
487.Fn vmem_xalloc
488and
489.Fn vmem_alloc
490return 0.
491Otherwise,
492.Dv ENOMEM
493is returned.
494.\" ------------------------------------------------------------
495.Sh CODE REFERENCES
496The
497.Nm
498subsystem is implemented within the file
499.Pa sys/kern/subr_vmem.c .
500.\" ------------------------------------------------------------
501.Sh SEE ALSO
502.Xr intro 9 ,
503.Xr kmem 9 ,
504.Xr memoryallocators 9 ,
505.Xr uvm 9
506.Rs
507.%A Jeff Bonwick
508.%A Jonathan Adams
509.%T "Magazines and Vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resources"
510.%J "2001 USENIX Annual Technical Conference"
511.%D 2001
512.Re
513.\" ------------------------------------------------------------
514.Sh AUTHORS
515This implementation of
516.Nm
517was written by
518.An YAMAMOTO Takashi .
519.Sh BUGS
520.Pp
521.Nm
522relies on
523.Xr malloc 9 ,
524.Xr pool 9 ,
525and
526.Xr RUN_ONCE 9 ,
527so it cannot be used as early during system bootstrap as
528.Xr extent 9 .
529