/
kern_lock.c
334 lines (290 loc) · 8.39 KB
/
kern_lock.c
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
/* $NetBSD: kern_lock.c,v 1.165 2020/01/17 20:26:22 ad Exp $ */
/*-
* Copyright (c) 2002, 2006, 2007, 2008, 2009, 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* 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.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.165 2020/01/17 20:26:22 ad Exp $");
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/lock.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/lockdebug.h>
#include <sys/cpu.h>
#include <sys/syslog.h>
#include <sys/atomic.h>
#include <sys/lwp.h>
#include <sys/pserialize.h>
#include <machine/lock.h>
#include <dev/lockstat.h>
#define RETURN_ADDRESS (uintptr_t)__builtin_return_address(0)
bool kernel_lock_dodebug;
__cpu_simple_lock_t kernel_lock[CACHE_LINE_SIZE / sizeof(__cpu_simple_lock_t)]
__cacheline_aligned;
void
assert_sleepable(void)
{
const char *reason;
uint64_t pctr;
bool idle;
if (panicstr != NULL) {
return;
}
LOCKDEBUG_BARRIER(kernel_lock, 1);
/*
* Avoid disabling/re-enabling preemption here since this
* routine may be called in delicate situations.
*/
do {
pctr = lwp_pctr();
__insn_barrier();
idle = CURCPU_IDLE_P();
__insn_barrier();
} while (pctr != lwp_pctr());
reason = NULL;
if (idle && !cold &&
kcpuset_isset(kcpuset_running, cpu_index(curcpu()))) {
reason = "idle";
}
if (cpu_intr_p()) {
reason = "interrupt";
}
if (cpu_softintr_p()) {
reason = "softint";
}
if (!pserialize_not_in_read_section()) {
reason = "pserialize";
}
if (reason) {
panic("%s: %s caller=%p", __func__, reason,
(void *)RETURN_ADDRESS);
}
}
/*
* Functions for manipulating the kernel_lock. We put them here
* so that they show up in profiles.
*/
#define _KERNEL_LOCK_ABORT(msg) \
LOCKDEBUG_ABORT(__func__, __LINE__, kernel_lock, &_kernel_lock_ops, msg)
#ifdef LOCKDEBUG
#define _KERNEL_LOCK_ASSERT(cond) \
do { \
if (!(cond)) \
_KERNEL_LOCK_ABORT("assertion failed: " #cond); \
} while (/* CONSTCOND */ 0)
#else
#define _KERNEL_LOCK_ASSERT(cond) /* nothing */
#endif
static void _kernel_lock_dump(const volatile void *, lockop_printer_t);
lockops_t _kernel_lock_ops = {
.lo_name = "Kernel lock",
.lo_type = LOCKOPS_SPIN,
.lo_dump = _kernel_lock_dump,
};
/*
* Initialize the kernel lock.
*/
void
kernel_lock_init(void)
{
__cpu_simple_lock_init(kernel_lock);
kernel_lock_dodebug = LOCKDEBUG_ALLOC(kernel_lock, &_kernel_lock_ops,
RETURN_ADDRESS);
}
CTASSERT(CACHE_LINE_SIZE >= sizeof(__cpu_simple_lock_t));
/*
* Print debugging information about the kernel lock.
*/
static void
_kernel_lock_dump(const volatile void *junk, lockop_printer_t pr)
{
struct cpu_info *ci = curcpu();
(void)junk;
pr("curcpu holds : %18d wanted by: %#018lx\n",
ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
}
/*
* Acquire 'nlocks' holds on the kernel lock.
*/
void
_kernel_lock(int nlocks)
{
struct cpu_info *ci;
LOCKSTAT_TIMER(spintime);
LOCKSTAT_FLAG(lsflag);
struct lwp *owant;
u_int count;
#ifdef LOCKDEBUG
u_int spins = 0;
#endif
int s;
struct lwp *l = curlwp;
_KERNEL_LOCK_ASSERT(nlocks > 0);
s = splvm();
ci = curcpu();
if (ci->ci_biglock_count != 0) {
_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
ci->ci_biglock_count += nlocks;
l->l_blcnt += nlocks;
splx(s);
return;
}
_KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS,
0);
if (__predict_true(__cpu_simple_lock_try(kernel_lock))) {
ci->ci_biglock_count = nlocks;
l->l_blcnt = nlocks;
LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, NULL,
RETURN_ADDRESS, 0);
splx(s);
return;
}
/*
* To remove the ordering constraint between adaptive mutexes
* and kernel_lock we must make it appear as if this thread is
* blocking. For non-interlocked mutex release, a store fence
* is required to ensure that the result of any mutex_exit()
* by the current LWP becomes visible on the bus before the set
* of ci->ci_biglock_wanted becomes visible.
*
* However, we won't set ci_biglock_wanted until we've spun for
* a bit, as we don't want to make any lock waiters in rw_oncpu()
* or mutex_oncpu() block prematurely.
*/
membar_producer();
owant = ci->ci_biglock_wanted;
/*
* Spin until we acquire the lock. Once we have it, record the
* time spent with lockstat.
*/
LOCKSTAT_ENTER(lsflag);
LOCKSTAT_START_TIMER(lsflag, spintime);
count = SPINLOCK_BACKOFF_MIN;
do {
splx(s);
while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
#ifdef LOCKDEBUG
if (SPINLOCK_SPINOUT(spins)) {
extern int start_init_exec;
if (!start_init_exec)
_KERNEL_LOCK_ABORT("spinout");
}
#endif
SPINLOCK_BACKOFF(count);
if (count == SPINLOCK_BACKOFF_MAX) {
/* Ok, waiting for real. */
ci->ci_biglock_wanted = l;
}
}
s = splvm();
} while (!__cpu_simple_lock_try(kernel_lock));
ci->ci_biglock_count = nlocks;
l->l_blcnt = nlocks;
splx(s);
LOCKSTAT_STOP_TIMER(lsflag, spintime);
LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, NULL,
RETURN_ADDRESS, 0);
if (owant == NULL) {
LOCKSTAT_EVENT_RA(lsflag, kernel_lock,
LB_KERNEL_LOCK | LB_SPIN, 1, spintime, RETURN_ADDRESS);
}
LOCKSTAT_EXIT(lsflag);
/*
* Now that we have kernel_lock, reset ci_biglock_wanted. This
* store must be unbuffered (immediately visible on the bus) in
* order for non-interlocked mutex release to work correctly.
* It must be visible before a mutex_exit() can execute on this
* processor.
*
* Note: only where CAS is available in hardware will this be
* an unbuffered write, but non-interlocked release cannot be
* done on CPUs without CAS in hardware.
*/
(void)atomic_swap_ptr(&ci->ci_biglock_wanted, owant);
/*
* Issue a memory barrier as we have acquired a lock. This also
* prevents stores from a following mutex_exit() being reordered
* to occur before our store to ci_biglock_wanted above.
*/
#ifndef __HAVE_ATOMIC_AS_MEMBAR
membar_enter();
#endif
}
/*
* Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
* all holds.
*/
void
_kernel_unlock(int nlocks, int *countp)
{
struct cpu_info *ci;
u_int olocks;
int s;
struct lwp *l = curlwp;
_KERNEL_LOCK_ASSERT(nlocks < 2);
olocks = l->l_blcnt;
if (olocks == 0) {
_KERNEL_LOCK_ASSERT(nlocks <= 0);
if (countp != NULL)
*countp = 0;
return;
}
_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
if (nlocks == 0)
nlocks = olocks;
else if (nlocks == -1) {
nlocks = 1;
_KERNEL_LOCK_ASSERT(olocks == 1);
}
s = splvm();
ci = curcpu();
_KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
if (ci->ci_biglock_count == nlocks) {
LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, kernel_lock,
RETURN_ADDRESS, 0);
ci->ci_biglock_count = 0;
__cpu_simple_unlock(kernel_lock);
l->l_blcnt -= nlocks;
splx(s);
if (l->l_dopreempt)
kpreempt(0);
} else {
ci->ci_biglock_count -= nlocks;
l->l_blcnt -= nlocks;
splx(s);
}
if (countp != NULL)
*countp = olocks;
}
bool
_kernel_locked_p(void)
{
return __SIMPLELOCK_LOCKED_P(kernel_lock);
}