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
|
.\" $NetBSD: localcount.9,v 1.7 2019/02/25 21:43:00 pgoyette Exp $
.\"
.\" Copyright (c) 2016 The NetBSD Foundation
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Taylor R. Campbell.
.\"
.\" 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 February 25, 2019
.Dt LOCALCOUNT 9
.Os
.Sh NAME
.Nm localcount ,
.Nm localcount_init ,
.Nm localcount_fini ,
.Nm localcount_acquire ,
.Nm localcount_release ,
.Nm localcount_drain
.Nd reference-count primitives
.Sh SYNOPSIS
.In sys/localcount.h
.Ft void
.Fn localcount_init "struct localcount *lc"
.Ft void
.Fn localcount_fini "struct localcount *lc"
.Ft void
.Fn localcount_acquire "struct localcount *lc"
.Ft void
.Fn localcount_release "struct localcount *lc" "struct kcondvar *cv" \
"struct kmutex *mtx"
.Ft void
.Fn localcount_drain "struct localcount *lc" "struct kcondvar *cv" \
"struct kmutex *mtx"
.Sh DESCRIPTION
Localcounts are used in the kernel to implement a medium-weight reference
counting mechanism.
During normal operations, localcounts do not need the interprocessor
synchronization associated with
.Xr atomic_ops 3
atomic memory operations, and (unlike
.Xr psref 9 )
.Nm
references can be held across sleeps and can migrate between CPUs.
Draining a
.Nm
requires more expensive interprocessor synchronization than
.Xr atomic_ops 3
(similar to
.Xr psref 9 ) .
And
.Nm
references require eight bytes of memory per object per-CPU, significantly
more than
.Xr atomic_ops 3
and almost always more than
.Xr psref 9 .
.Pp
As a rough heuristic,
.Nm
should be used for classes of objects of which there are perhaps a few dozen
instances (such as
.Xr autoconf 9
devices) but not thousands of instances (such as
network flows) and on which there may be a mixture of long-term I/O waits,
such as xyzread for a device xyz(4), and short-term fast operations, such as
.Dv xyzioctl(IOC_READ_A_CPU_REG) .
.Sh FUNCTIONS
.Bl -tag -width abcd
.It Fn localcount_init "lc"
Dynamically initialize localcount
.Ar lc
for use.
.Pp
No other operations can be performed on a localcount until it has been
initialized.
.It Fn localcount_fini "lc"
Release resources used by localcount
.Ar lc .
The caller must have already called
.Fn localcount_drain .
The localcount may not be used after
.Fn localcount_fini
has been called until it has been re-initialized by
.Fn localcount_init .
.It Fn localcount_acquire "lc"
Acquire a reference to the localcount
.Ar lc .
.Pp
The caller must ensure by some other mechanism that the localcount will
not be destroyed before the call to
.Fn localcount_acquire ;
typically this will be via a
.Xr pserialize 9
read section.
.It Fn localcount_release "lc" "cv" "mtx"
Release a reference to the localcount
.Ar lc .
If the localcount is currently being drained, the mutex
.Ar mtx
will be used to synchronize updates to the global reference count (i.e.,
the total across all CPUs).
If the reference count goes to zero,
.Fn localcount_release
will broadcast availability of the condvar
.Ar cv .
.It Fn localcount_drain "lc" "cv" "mtx"
Wait for all references to the localcount
.Ar lc
to be released.
The caller must hold the mutex
.Ar mtx ;
the mutex will be released during inter-CPU cross-calls (see
.Xr xcall 9 )
and while waiting on the condvar
.Ar cv .
The same
.Ar cv
and
.Ar mtx
must be used with
.Fn localcount_release .
.Pp
The caller must guarantee that no new references can be acquired with
.Fn localcount_acquire
before calling
.Fn localcount_drain .
For example, any object that may be found in a list and acquired must be
removed from the list before calling
.Fn localcount_drain ;
removal from the list would typically be protected by calling
.Xr pserialize_perform 9
to wait for any
.Xr pserialize 9
readers to complete.
.Pp
Once the localcount object
.Ar lc
is passed to
.Fn localcount_drain ,
it must be passed to
.Fn localcount_fini
before any other re-use.
.El
.Sh CODE REFERENCES
The core of the localcount implementation is located in
.Pa sys/kern/subr_localcount.c .
.Pp
The header file
.Pa sys/sys/localcount.h
describes the public interface, and interfaces that machine-dependent
code must provide to support localcounts.
.Sh SEE ALSO
.Xr atomic_ops 3 ,
.Xr condvar 9 ,
.Xr mutex 9 ,
.Xr psref 9
.Sh HISTORY
The localcount primitives first appeared in
.Nx 8.0 .
.Sh AUTHORS
.Nm
was designed by
.An -nosplit
.An Taylor R. Campbell ,
who also provided a draft implementation.
The implementation was completed, tested, and integrated by
.An Paul Goyette .
.Sh CAVEATS
The
.Nm
facility does not provide any way to examine the reference count without
actually waiting for the count to reach zero.
.Pp
Waiting for a
.Nm
reference count to drain (reach zero) is a one-shot operation.
Once the
.Nm
has been drained, no further operations are allowed until the
.Nm
has been re-initialized.
|