NetBSD/share/man/man9/vmem.9

415 lines
10 KiB
Groff

.\" $NetBSD: vmem.9,v 1.8 2009/12/22 11:56:14 wiz Exp $
.\"
.\" Copyright (c)2006 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.
.\"
.\" ------------------------------------------------------------
.Dd December 21, 2009
.Dt VMEM 9
.Os
.\" ------------------------------------------------------------
.Sh NAME
.Nm vmem
.Nd virtual memory allocator
.\" ------------------------------------------------------------
.Sh SYNOPSIS
.In sys/vmem.h
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Ft vmem_t *
.Fn vmem_create \
"const char *name" "vmem_addr_t base" "vmem_size_t size" "vmem_size_t quantum" \
"vmem_addr_t (*allocfn)(vmem_t *, vmem_size_t, vmem_size_t *, vm_flag_t)" \
"void (*freefn)(vmem_t *, vmem_addr_t, vmem_size_t)" \
"vmem_t *source" "vmem_size_t qcache_max" "vm_flag_t flags" "int ipl"
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Ft vmem_addr_t
.Fn vmem_add \
"vmem_t *vm" "vmem_addr_t addr" "vmem_size_t size" "vm_flag_t flags"
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Ft vmem_addr_t
.Fn vmem_xalloc \
"vmem_t *vm" "vmem_size_t size" "vmem_size_t align" \
"vmem_size_t phase" "vmem_size_t nocross" "vmem_addr_t minaddr" \
"vmem_addr_t maxaddr" "vm_flag_t flags"
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Ft void
.Fn vmem_xfree "vmem_t *vm" "vmem_addr_t addr" "vmem_size_t size"
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Ft vmem_addr_t
.Fn vmem_alloc "vmem_t *vm" "vmem_size_t size" "vm_flag_t flags"
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Ft void
.Fn vmem_free "vmem_t *vm" "vmem_addr_t addr" "vmem_size_t size"
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Ft void
.Fn vmem_destroy "vmem_t *vm"
.\" ------------------------------------------------------------
.Sh DESCRIPTION
The
.Nm
is a general purpose resource allocator.
Despite its name, it can be used for arbitrary resources
other than virtual memory.
.Pp
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Fn vmem_create
creates a new vmem arena.
.Pp
.Bl -tag -width qcache_max
.It Fa name
The string to describe the vmem.
.It Fa base
The start address of the initial span.
It can be
.Dv VMEM_ADDR_NULL
if no initial span is required.
.It Fa size
The size of the initial span.
.It Fa quantum
The smallest unit of allocation.
.It Fa allocfn
The callback function used to import spans from the backend arena.
Set both
.Fa allocfn
and
.Fa freefn
to
.Dv NULL
to disable automatic imports.
.Nm
calls
.Fo "(*allocfn)"
.Fa source
.Fa size
.Fa "\*[Am]actualsize"
.Fa flags
.Fc
to import a span of size at least
.Fa size .
.Fa allocfn
should accept the same
.Fa flags
as
.Fn vmem_alloc .
.Fa allocfn
must return
.Dv VMEM_ADDR_NULL
to indicate failure, or
else the starting address of the imported span.
If
.Fa allocfn
succeeds, it must write the actual size of the allocation to
.Fa actualsize .
The actual size will always be greater than or equal to the requested size.
.It Fa freefn
The callback function used to free spans to the backend arena.
.Fa freefn
may not be
.Dv NULL
unless
.Fa allocfn
is
.Dv NULL .
.Nm
calls
.Fn "(*freefn)" source addr size
to return to
.Fa source
a span of size
.Fa size ,
starting at
.Fa addr ,
that was previously allocated by
.Fa allocfn .
.It Fa source
The backend arena.
.Fa source
may be
.Dv NULL .
.Nm
passes
.Fa source
as the first argument of
.Fa allocfn
and
.Fa freefn .
.It Fa qcache_max
The largest size of allocations which can be served by quantum cache.
It is merely a hint and can be ignored.
.It Fa flags
Either of:
.Bl -tag -width VM_NOSLEEP
.It Dv VM_SLEEP
Can sleep until enough resources are available.
.It Dv VM_NOSLEEP
Don't sleep.
Immediately return
.Dv NULL
if there are not enough resources available.
.El
.It Fa ipl
Interrupt level to be blocked for allocating from vmem.
.El
.Pp
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Fn vmem_add
adds a span of size
.Fa size
starting at
.Fa addr
to the arena.
Returns
.Fa addr
on success.
.Fa flags
should be one of:
.Bl -tag -width VM_NOSLEEP
.It Dv VM_SLEEP
Can sleep until enough resources are available.
.It Dv VM_NOSLEEP
Don't sleep.
Immediately return
.Dv VMEM_ADDR_NULL
if there are not enough resources available.
.El
.Pp
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Fn vmem_xalloc
allocates a resource from the arena.
.Pp
.Bl -tag -width nocross
.It Fa vm
The arena which we allocate from.
.It Fa size
Specify the size of the allocation.
.It Fa align
If zero, don't care about the alignment of the allocation.
Otherwise, request a resource segment starting at
offset
.Fa phase
from an
.Fa align
aligned boundary.
.It Fa phase
See the above description of
.Fa align .
If
.Fa align
is zero,
.Fa phase
should be zero.
Otherwise,
.Fa phase
should be smaller than
.Fa align .
.It Fa nocross
Request a resource which doesn't cross
.Fa nocross
aligned boundary.
.It Fa minaddr
If non-zero, specify the minimum address which can be allocated.
.It Fa maxaddr
If non-zero, specify the maximum address + 1 which can be allocated.
.It Fa flags
A bitwise OR of an allocation strategy and a sleep flag.
.Pp
The allocation strategy is one of:
.Bl -tag -width VM_INSTANTFIT
.It Dv VM_BESTFIT
Prefer space efficiency.
.It Dv VM_INSTANTFIT
Prefer performance.
.El
.Pp
The sleep flag should be one of:
.Bl -tag -width VM_NOSLEEP
.It Dv VM_SLEEP
Can sleep until enough resources are available.
.It Dv VM_NOSLEEP
Don't sleep.
Immediately return
.Dv VMEM_ADDR_NULL
if there are not enough resources available.
.El
.El
.Pp
.\" ------------------------------------------------------------
.Fn vmem_xfree
frees resource allocated by
.Fn vmem_xalloc
to the arena.
.Pp
.Bl -tag -width addr
.It Fa vm
The arena which we free to.
.It Fa addr
The resource being freed.
It must be the one returned by
.Fn vmem_xalloc .
Notably, it must not be the one from
.Fn vmem_alloc .
Otherwise, the behaviour is undefined.
.It Fa size
The size of the resource being freed.
It must be the same as the
.Fa size
argument used for
.Fn vmem_xalloc .
.El
.Pp
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Fn vmem_alloc
allocates a resource from the arena.
.Pp
.Bl -tag -width flags
.It Fa vm
The arena which we allocate from.
.It Fa size
Specify the size of the allocation.
.It Fa flags
A bitwise OR of an allocation strategy and a sleep flag.
.Pp
The allocation strategy is one of:
.Bl -tag -width VM_INSTANTFIT
.It Dv VM_BESTFIT
Prefer space efficiency.
.It Dv VM_INSTANTFIT
Prefer performance.
.El
.Pp
The sleep flag should be one of:
.Bl -tag -width VM_NOSLEEP
.It Dv VM_SLEEP
Can sleep until enough resources are available.
.It Dv VM_NOSLEEP
Don't sleep.
Immediately return
.Dv VMEM_ADDR_NULL
if there are not enough resources available.
.El
.El
.Pp
.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Fn vmem_free
frees resource allocated by
.Fn vmem_alloc
to the arena.
.Pp
.Bl -tag -width addr
.It Fa vm
The arena which we free to.
.It Fa addr
The resource being freed.
It must be the one returned by
.Fn vmem_alloc .
Notably, it must not be the one from
.Fn vmem_xalloc .
Otherwise, the behaviour is undefined.
.It Fa size
The size of the resource being freed.
It must be the same as the
.Fa size
argument used for
.Fn vmem_alloc .
.El
.Pp
.\" ------------------------------------------------------------
.Fn vmem_destroy
destroys a vmem arena.
.Pp
.Bl -tag -width vm
.It Fa vm
The vmem arena being destroyed.
The caller should ensure that no one will use it anymore.
.El
.\" ------------------------------------------------------------
.Sh RETURN VALUES
.Fn vmem_create
return a pointer to the newly allocated vmem_t.
Otherwise, it returns
.Dv NULL .
.Pp
On success,
.Fn vmem_xalloc
and
.Fn vmem_alloc
return an allocated vmem_addr_t.
Otherwise,
.Dv VMEM_ADDR_NULL
is returned.
.\" ------------------------------------------------------------
.Sh CODE REFERENCES
This section describes places within the
.Nx
source tree where actual code implementing the
.Nm
subsystem can be found.
All pathnames are relative to
.Pa /usr/src .
.Pp
The
.Nm
subsystem is implemented within the file
.Pa sys/kern/subr_vmem.c .
.\" ------------------------------------------------------------
.Sh SEE ALSO
.Xr intro 9 ,
.Xr kmem 9 ,
.Xr memoryallocators 9 ,
.Xr uvm 9
.Rs
.%A Jeff Bonwick
.%A Jonathan Adams
.%T "Magazines and Vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resources"
.%J "2001 USENIX Annual Technical Conference"
.%D 2001
.Re
.\" ------------------------------------------------------------
.Sh AUTHORS
This implementation of
.Nm
was written by
.An YAMAMOTO Takashi .
.Sh BUGS
.Nm
cannot manage a resource that starts at 0, because it reserves the
address
.Dv VMEM_ADDR_NULL
.Pq 0
for indicating errors.
.Pp
.Nm
cannot manage a resource that ends at the maximum
.Vt vmem_addr_t .
This is an implementation limitation.
.Pp
.Nm
relies on
.Xr malloc 9 ,
.Xr pool 9 ,
and
.Xr RUN_ONCE 9 ,
so it cannot be used as early during system bootstrap as
.Xr extent 9 .