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
|
.\" $OpenBSD: cpumem_get.9,v 1.7 2016/10/25 00:07:29 dlg Exp $
.\"
.\" Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate: October 25 2016 $
.Dt CPUMEM_GET 9
.Os
.Sh NAME
.Nm cpumem_get ,
.Nm cpumem_put ,
.Nm cpumem_malloc ,
.Nm cpumem_malloc_ncpus ,
.Nm cpumem_free ,
.Nm CPUMEM_BOOT_MEMORY ,
.Nm CPUMEM_BOOT_INITIALIZER ,
.Nm cpumem_enter ,
.Nm cpumem_leave ,
.Nm cpumem_first ,
.Nm cpumem_next
.Nd per CPU memory allocations
.Sh SYNOPSIS
.In sys/percpu.h
.Ft struct cpumem *
.Fn cpumem_get "struct pool *pp"
.Ft void
.Fn cpumem_put "struct pool *pp" "struct cpumem *cm"
.Ft struct cpumem *
.Fn cpumem_malloc "size_t sz" "int type"
.Ft void
.Fn cpumem_free "struct cpumem *cm" "int type" "size_t sz"
.Fn CPUMEM_BOOT_MEMORY "NAME" "size_t sz"
.Fn CPUMEM_BOOT_INITIALIZER "NAME"
.Ft struct cpumem *
.Fn cpumem_malloc_ncpus "struct cpumem *cm" "size_t sz" "int type"
.Ft void *
.Fn cpumem_enter "struct cpumem *cm"
.Ft void
.Fn cpumem_leave "struct cpumem *cm" "void *m"
.Ft void *
.Fn cpumem_first "struct cpumem_iter *ci" "struct cpumem *cm"
.Ft void *
.Fn cpumem_next "struct cpumem_iter *ci" "struct cpumem *cm"
.Fn CPUMEM_FOREACH "VARNAME" "struct cpumem_iter *ci" "struct cpumem *cm"
.Sh DESCRIPTION
The per CPU memory API provides wrappers around the allocation of
and access to per CPU memory.
.Pp
An alternate implemention of the API is provided on uni-processor
(i.e. when the kernel is not built with
.Dv MULTIPROCESSOR
defined)
systems that provides no overhead compared to direct access to a
data structure.
This allows the API to be used without affecting the performance
uni-processor systems.
.Ss Per CPU Memory Allocation and Deallocation
.Fn cpumem_get
allocates memory for each CPU from the
.Fa pp
pool.
The memory will be zeroed on allocation.
.Pp
.Fn cpumem_put
returns each CPUs memory allocation referenced by
.Fa cm
to the
.Fa pp
pool.
.Pp
.Fn cpumem_malloc
allocates
.Fa sz
bytes of
.Fa type
memory for each CPU using
.Xr malloc 9 .
The memory will be zeroed on allocation.
.Pp
.Fn cpumem_free
returns each CPU's memory allocation referenced by
.Fa cm
to the system using
.Xr free 9 .
The same object size and type originally provided to
.Fn cpumem_malloc
must be specified by
.Fa sz
and
.Fa type
respectively.
.Pp
.Fn cpumem_get
and
.Fn cpumem_malloc
may only be used after all the CPUs in the system have been attached.
If per CPU memory needs to be available during early boot,
a cpumem pointer and memory for the boot CPU may be statically
allocated.
.Pp
.Fn CPUMEM_BOOT_MEMORY
statically allocates memory for use on the boot CPU
before the other CPUs in the system have been attached.
The allocation is identified by
.Fa NAME ,
and provides the number of bytes specified by the
.Fa sz
argument.
The memory will be initialised to zeros.
.Pp
.Fn CPUMEM_BOOT_INITIALIZER
is used to initialise a cpumem pointer with the memory that was previously
allocated using
.Fn CPUMEM_BOOT_MEMORY
and identified by
.Fa NAME .
.Pp
.Fn cpumem_malloc_ncpus
allocates additional memory for the CPUs that were attached during boot.
The cpumem structure
.Fa cm
must have been initialised with
.Fn CPUMEM_BOOT_INITIALIZER .
The same number of bytes originally passed to
.Fa COUNTERS_BOOT_MEMORY
must be specified by
.Fa sz .
The
.Fa type
argument specifies the type of memory that the counters will be
allocated as via
.Xr malloc 9 .
The memory will be zeroed on allocation.
.Pp
Per CPU memory that has been allocated with
.Fn CPUMEM_BOOT_MEMORY
and
.Fn cpumem_malloc_ncpus
cannot be deallocated with
.Fa cpumem_free .
Any attempt to do so will lead to undefined behaviour.
.Ss Per CPU Memory Access
.Fn cpumem_enter
provides access to the current CPU's memory allocation referenced by
.Fa cm .
.Pp
.Fn cpumem_leave
indicates the end of access to the current CPU's memory allocation referenced by
.Fa cm .
.Ss Per CPU Memory Iterators
.Fn cpumem_first
provides access to the first CPU's memory allocation referenced by
.Fa cm .
The iterator
.Fa ci
may be used in subsequent calls to
.Fn cpumem_next .
.Pp
.Fn cpumem_next
provides access to the next CPU's memory allocation referenced by
.Fa cm
and
.Fa ci .
.Pp
The
.Fn CPUMEM_FOREACH
macro iterates over each CPU's memory allocation referenced by
.Fa cm
using the iterator
.Fa ci ,
setting
.Fa VARNAME
to each CPU's allocation in turn.
.Sh CONTEXT
.Fn cpumem_get ,
.Fn cpumem_put ,
.Fn cpumem_malloc ,
.Fn cpumem_free ,
and
.Fn cpumem_malloc_ncpus
may be called during autoconf, or from process context.
.Pp
.Fn cpumem_enter ,
.Fn cpumem_leave ,
.Fn cpumem_first ,
.Fn cpumem_next ,
and
.Fn CPUMEM_FOREACH
may be called during autoconf, from process context, or from interrupt
context.
The per CPU memory API does not provide any locking or serialisation
of access to each CPU's memory allocation.
It is up to the caller to provide appropriate locking or serialisation
around calls to these functions to prevent concurrent access to the
relevant data structures.
.Sh RETURN VALUES
.Fn cpumem_get ,
.Fn cpumem_malloc ,
and
.Fn cpumem_malloc_ncpus
will return an opaque cpumem pointer that references each CPU's
memory allocation.
.Pp
.Fn cpumem_enter
returns a reference to the current CPU's memory allocation.
.Pp
.Fn cpumem_first
returns a reference to the first CPU's memory allocation.
.Pp
.Fn cpumem_next
returns a reference to the next CPU's memory allocation according to the
iterator
.Fa ci ,
or
.Dv NULL
if the iterator has run out of CPUs.
.Sh SEE ALSO
.Xr counters_alloc 9 ,
.Xr malloc 9 ,
.Xr pool_get 9
.Sh HISTORY
The per CPU memory API first appeared in
.Ox 6.1 .
.Sh AUTHORS
The per CPU memory API was written by
.An David Gwynne Aq Mt dlg@openbsd.org .
|