]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/lock.h
Fix the NFSv4 client to safely find processes.
[FreeBSD/FreeBSD.git] / sys / sys / lock.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1997 Berkeley Software Design, Inc. 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  * 3. Berkeley Software Design Inc's name may not be used to endorse or
15  *    promote products derived from this software without specific prior
16  *    written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      from BSDI Id: mutex.h,v 2.7.2.35 2000/04/27 03:10:26 cp
31  * $FreeBSD$
32  */
33
34 #ifndef _SYS_LOCK_H_
35 #define _SYS_LOCK_H_
36
37 #include <sys/queue.h>
38 #include <sys/_lock.h>
39 #include <sys/ktr_class.h>
40
41 struct lock_list_entry;
42 struct thread;
43
44 /*
45  * Lock classes.  Each lock has a class which describes characteristics
46  * common to all types of locks of a given class.
47  *
48  * Spin locks in general must always protect against preemption, as it is
49  * an error to perform any type of context switch while holding a spin lock.
50  * Also, for an individual lock to be recursable, its class must allow
51  * recursion and the lock itself must explicitly allow recursion.
52  *
53  * The 'lc_ddb_show' function pointer is used to dump class-specific
54  * data for the 'show lock' DDB command.  The 'lc_lock' and
55  * 'lc_unlock' function pointers are used in sleep(9) and cv_wait(9)
56  * to lock and unlock locks while blocking on a sleep queue.  The
57  * return value of 'lc_unlock' will be passed to 'lc_lock' on resume
58  * to allow communication of state between the two routines.
59  */
60
61 struct lock_class {
62         const           char *lc_name;
63         u_int           lc_flags;
64         void            (*lc_assert)(const struct lock_object *lock, int what);
65         void            (*lc_ddb_show)(const struct lock_object *lock);
66         void            (*lc_lock)(struct lock_object *lock, uintptr_t how);
67         int             (*lc_owner)(const struct lock_object *lock,
68                             struct thread **owner);
69         uintptr_t       (*lc_unlock)(struct lock_object *lock);
70 };
71
72 #define LC_SLEEPLOCK    0x00000001      /* Sleep lock. */
73 #define LC_SPINLOCK     0x00000002      /* Spin lock. */
74 #define LC_SLEEPABLE    0x00000004      /* Sleeping allowed with this lock. */
75 #define LC_RECURSABLE   0x00000008      /* Locks of this type may recurse. */
76 #define LC_UPGRADABLE   0x00000010      /* Upgrades and downgrades permitted. */
77
78 #define LO_CLASSFLAGS   0x0000ffff      /* Class specific flags. */
79 #define LO_INITIALIZED  0x00010000      /* Lock has been initialized. */
80 #define LO_WITNESS      0x00020000      /* Should witness monitor this lock. */
81 #define LO_QUIET        0x00040000      /* Don't log locking operations. */
82 #define LO_RECURSABLE   0x00080000      /* Lock may recurse. */
83 #define LO_SLEEPABLE    0x00100000      /* Lock may be held while sleeping. */
84 #define LO_UPGRADABLE   0x00200000      /* Lock may be upgraded/downgraded. */
85 #define LO_DUPOK        0x00400000      /* Don't check for duplicate acquires */
86 #define LO_IS_VNODE     0x00800000      /* Tell WITNESS about a VNODE lock */
87 #define LO_CLASSMASK    0x0f000000      /* Class index bitmask. */
88 #define LO_NOPROFILE    0x10000000      /* Don't profile this lock */
89 #define LO_NEW          0x20000000      /* Don't check for double-init */
90
91 /*
92  * Lock classes are statically assigned an index into the gobal lock_classes
93  * array.  Debugging code looks up the lock class for a given lock object
94  * by indexing the array.
95  */
96 #define LO_CLASSSHIFT           24
97 #define LO_CLASSINDEX(lock)     ((((lock)->lo_flags) & LO_CLASSMASK) >> LO_CLASSSHIFT)
98 #define LOCK_CLASS(lock)        (lock_classes[LO_CLASSINDEX((lock))])
99 #define LOCK_CLASS_MAX          (LO_CLASSMASK >> LO_CLASSSHIFT)
100
101 /*
102  * Option flags passed to lock operations that witness also needs to know
103  * about or that are generic across all locks.
104  */
105 #define LOP_NEWORDER    0x00000001      /* Define a new lock order. */
106 #define LOP_QUIET       0x00000002      /* Don't log locking operations. */
107 #define LOP_TRYLOCK     0x00000004      /* Don't check lock order. */
108 #define LOP_EXCLUSIVE   0x00000008      /* Exclusive lock. */
109 #define LOP_DUPOK       0x00000010      /* Don't check for duplicate acquires */
110
111 /* Flags passed to witness_assert. */
112 #define LA_MASKASSERT   0x000000ff      /* Mask for witness defined asserts. */
113 #define LA_UNLOCKED     0x00000000      /* Lock is unlocked. */
114 #define LA_LOCKED       0x00000001      /* Lock is at least share locked. */
115 #define LA_SLOCKED      0x00000002      /* Lock is exactly share locked. */
116 #define LA_XLOCKED      0x00000004      /* Lock is exclusively locked. */
117 #define LA_RECURSED     0x00000008      /* Lock is recursed. */
118 #define LA_NOTRECURSED  0x00000010      /* Lock is not recursed. */
119
120 #ifdef _KERNEL
121 /*
122  * If any of WITNESS, INVARIANTS, or KTR_LOCK KTR tracing has been enabled,
123  * then turn on LOCK_DEBUG.  When this option is on, extra debugging
124  * facilities such as tracking the file and line number of lock operations
125  * are enabled.  Also, mutex locking operations are not inlined to avoid
126  * bloat from all the extra debugging code.  We also have to turn on all the
127  * calling conventions for this debugging code in modules so that modules can
128  * work with both debug and non-debug kernels.
129  */
130 #if (defined(KLD_MODULE) && !defined(KLD_TIED)) || defined(WITNESS) || defined(INVARIANTS) || \
131     defined(LOCK_PROFILING) || defined(KTR)
132 #define LOCK_DEBUG      1
133 #else
134 #define LOCK_DEBUG      0
135 #endif
136
137 /*
138  * In the LOCK_DEBUG case, use the filename and line numbers for debugging
139  * operations.  Otherwise, use default values to avoid the unneeded bloat.
140  */
141 #if LOCK_DEBUG > 0
142 #define LOCK_FILE_LINE_ARG_DEF  , const char *file, int line
143 #define LOCK_FILE_LINE_ARG      , file, line
144 #define LOCK_FILE       __FILE__
145 #define LOCK_LINE       __LINE__
146 #else
147 #define LOCK_FILE_LINE_ARG_DEF
148 #define LOCK_FILE_LINE_ARG
149 #define LOCK_FILE       NULL
150 #define LOCK_LINE       0
151 #endif
152
153 /*
154  * Macros for KTR_LOCK tracing.
155  *
156  * opname  - name of this operation (LOCK/UNLOCK/SLOCK, etc.)
157  * lo      - struct lock_object * for this lock
158  * flags   - flags passed to the lock operation
159  * recurse - this locks recursion level (or 0 if class is not recursable)
160  * result  - result of a try lock operation
161  * file    - file name
162  * line    - line number
163  */
164 #if LOCK_DEBUG > 0
165 #define LOCK_LOG_TEST(lo, flags)                                        \
166         (((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0)
167 #else
168 #define LOCK_LOG_TEST(lo, flags)        0
169 #endif
170
171
172 #define LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do {      \
173         if (LOCK_LOG_TEST((lo), (flags)))                               \
174                 CTR6(KTR_LOCK, opname " (%s) %s %p r = %d at %s:%d",    \
175                     LOCK_CLASS(lo)->lc_name, (lo)->lo_name,             \
176                     (lo), (u_int)(recurse), (file), (line));            \
177 } while (0)
178
179 #define LOCK_LOG_TRY(opname, lo, flags, result, file, line) do {        \
180         if (LOCK_LOG_TEST((lo), (flags)))                               \
181                 CTR6(KTR_LOCK, "TRY_" opname " (%s) %s %p result=%d at %s:%d",\
182                     LOCK_CLASS(lo)->lc_name, (lo)->lo_name,             \
183                     (lo), (u_int)(result), (file), (line));             \
184 } while (0)
185
186 #define LOCK_LOG_INIT(lo, flags) do {                                   \
187         if (LOCK_LOG_TEST((lo), (flags)))                               \
188                 CTR4(KTR_LOCK, "%s: %p (%s) %s", __func__, (lo),        \
189                     LOCK_CLASS(lo)->lc_name, (lo)->lo_name);            \
190 } while (0)
191
192 #define LOCK_LOG_DESTROY(lo, flags)     LOCK_LOG_INIT(lo, flags)
193
194 #define lock_initialized(lo)    ((lo)->lo_flags & LO_INITIALIZED)
195
196 /*
197  * Helpful macros for quickly coming up with assertions with informative
198  * panic messages.
199  */
200 #define MPASS(ex)               MPASS4(ex, #ex, __FILE__, __LINE__)
201 #define MPASS2(ex, what)        MPASS4(ex, what, __FILE__, __LINE__)
202 #define MPASS3(ex, file, line)  MPASS4(ex, #ex, file, line)
203 #define MPASS4(ex, what, file, line)                                    \
204         KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
205
206 extern struct lock_class lock_class_mtx_sleep;
207 extern struct lock_class lock_class_mtx_spin;
208 extern struct lock_class lock_class_sx;
209 extern struct lock_class lock_class_rw;
210 extern struct lock_class lock_class_rm;
211 extern struct lock_class lock_class_rm_sleepable;
212 extern struct lock_class lock_class_lockmgr;
213
214 extern struct lock_class *lock_classes[];
215
216 struct lock_delay_config {
217         u_int base;
218         u_int max;
219 };
220
221 struct lock_delay_arg {
222         struct lock_delay_config *config;
223         u_int delay;
224         u_int spin_cnt;
225 };
226
227 static inline void
228 lock_delay_arg_init(struct lock_delay_arg *la, struct lock_delay_config *lc)
229 {
230         la->config = lc;
231         la->delay = lc->base;
232         la->spin_cnt = 0;
233 }
234
235 #define lock_delay_spin(n)      do {    \
236         u_int _i;                       \
237                                         \
238         for (_i = (n); _i > 0; _i--)    \
239                 cpu_spinwait();         \
240 } while (0)
241
242 #define LOCK_DELAY_SYSINIT(func) \
243         SYSINIT(func##_ld, SI_SUB_LOCK, SI_ORDER_ANY, func, NULL)
244
245 #define LOCK_DELAY_SYSINIT_DEFAULT(lc) \
246         SYSINIT(lock_delay_##lc##_ld, SI_SUB_LOCK, SI_ORDER_ANY, \
247             lock_delay_default_init, &lc)
248
249 void    lock_init(struct lock_object *, struct lock_class *,
250             const char *, const char *, int);
251 void    lock_destroy(struct lock_object *);
252 void    lock_delay(struct lock_delay_arg *);
253 void    lock_delay_default_init(struct lock_delay_config *);
254 void    spinlock_enter(void);
255 void    spinlock_exit(void);
256 void    witness_init(struct lock_object *, const char *);
257 void    witness_destroy(struct lock_object *);
258 int     witness_defineorder(struct lock_object *, struct lock_object *);
259 void    witness_checkorder(struct lock_object *, int, const char *, int,
260             struct lock_object *);
261 void    witness_lock(struct lock_object *, int, const char *, int);
262 void    witness_upgrade(struct lock_object *, int, const char *, int);
263 void    witness_downgrade(struct lock_object *, int, const char *, int);
264 void    witness_unlock(struct lock_object *, int, const char *, int);
265 void    witness_save(struct lock_object *, const char **, int *);
266 void    witness_restore(struct lock_object *, const char *, int);
267 int     witness_list_locks(struct lock_list_entry **,
268             int (*)(const char *, ...));
269 int     witness_warn(int, struct lock_object *, const char *, ...);
270 void    witness_assert(const struct lock_object *, int, const char *, int);
271 void    witness_display_spinlock(struct lock_object *, struct thread *,
272             int (*)(const char *, ...));
273 int     witness_line(struct lock_object *);
274 void    witness_norelease(struct lock_object *);
275 void    witness_releaseok(struct lock_object *);
276 const char *witness_file(struct lock_object *);
277 void    witness_thread_exit(struct thread *);
278
279 #ifdef  WITNESS
280 int     witness_startup_count(void);
281 void    witness_startup(void *);
282
283 /* Flags for witness_warn(). */
284 #define WARN_GIANTOK    0x01    /* Giant is exempt from this check. */
285 #define WARN_PANIC      0x02    /* Panic if check fails. */
286 #define WARN_SLEEPOK    0x04    /* Sleepable locks are exempt from check. */
287
288 #define WITNESS_INIT(lock, type)                                        \
289         witness_init((lock), (type))
290
291 #define WITNESS_DESTROY(lock)                                           \
292         witness_destroy(lock)
293
294 #define WITNESS_CHECKORDER(lock, flags, file, line, interlock)          \
295         witness_checkorder((lock), (flags), (file), (line), (interlock))
296
297 #define WITNESS_DEFINEORDER(lock1, lock2)                               \
298         witness_defineorder((struct lock_object *)(lock1),              \
299             (struct lock_object *)(lock2))
300
301 #define WITNESS_LOCK(lock, flags, file, line)                           \
302         witness_lock((lock), (flags), (file), (line))
303
304 #define WITNESS_UPGRADE(lock, flags, file, line)                        \
305         witness_upgrade((lock), (flags), (file), (line))
306
307 #define WITNESS_DOWNGRADE(lock, flags, file, line)                      \
308         witness_downgrade((lock), (flags), (file), (line))
309
310 #define WITNESS_UNLOCK(lock, flags, file, line)                         \
311         witness_unlock((lock), (flags), (file), (line))
312
313 #define WITNESS_CHECK(flags, lock, fmt, ...)                            \
314         witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
315
316 #define WITNESS_WARN(flags, lock, fmt, ...)                             \
317         witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
318
319 #define WITNESS_SAVE_DECL(n)                                            \
320         const char * __CONCAT(n, __wf);                                 \
321         int __CONCAT(n, __wl)
322
323 #define WITNESS_SAVE(lock, n)                                           \
324         witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl))
325
326 #define WITNESS_RESTORE(lock, n)                                        \
327         witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl))
328
329 #define WITNESS_NORELEASE(lock)                                         \
330         witness_norelease(&(lock)->lock_object)
331
332 #define WITNESS_RELEASEOK(lock)                                         \
333         witness_releaseok(&(lock)->lock_object)
334
335 #define WITNESS_FILE(lock)                                              \
336         witness_file(lock)
337
338 #define WITNESS_LINE(lock)                                              \
339         witness_line(lock)
340
341 #else   /* WITNESS */
342 #define WITNESS_INIT(lock, type)                                (void)0
343 #define WITNESS_DESTROY(lock)                                   (void)0
344 #define WITNESS_DEFINEORDER(lock1, lock2)       0
345 #define WITNESS_CHECKORDER(lock, flags, file, line, interlock)  (void)0
346 #define WITNESS_LOCK(lock, flags, file, line)                   (void)0
347 #define WITNESS_UPGRADE(lock, flags, file, line)                (void)0
348 #define WITNESS_DOWNGRADE(lock, flags, file, line)              (void)0
349 #define WITNESS_UNLOCK(lock, flags, file, line)                 (void)0
350 #define WITNESS_CHECK(flags, lock, fmt, ...)    0
351 #define WITNESS_WARN(flags, lock, fmt, ...)                     (void)0
352 #define WITNESS_SAVE_DECL(n)                                    (void)0
353 #define WITNESS_SAVE(lock, n)                                   (void)0
354 #define WITNESS_RESTORE(lock, n)                                (void)0
355 #define WITNESS_NORELEASE(lock)                                 (void)0
356 #define WITNESS_RELEASEOK(lock)                                 (void)0
357 #define WITNESS_FILE(lock) ("?")
358 #define WITNESS_LINE(lock) (0)
359 #endif  /* WITNESS */
360
361 #endif  /* _KERNEL */
362 #endif  /* _SYS_LOCK_H_ */