xref: /freebsd/sys/kern/kern_rmlock.c (revision 00a5db46de56179184c0f000eaacad695e2b0859)
1 /*-
2  * Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * Machine independent bits of reader/writer lock implementation.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_ddb.h"
38 #include "opt_kdtrace.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/proc.h>
48 #include <sys/rmlock.h>
49 #include <sys/sched.h>
50 #include <sys/smp.h>
51 #include <sys/systm.h>
52 #include <sys/turnstile.h>
53 #include <sys/lock_profile.h>
54 #include <machine/cpu.h>
55 
56 #ifdef DDB
57 #include <ddb/ddb.h>
58 #endif
59 
60 #define RMPF_ONQUEUE	1
61 #define RMPF_SIGNAL	2
62 
63 /*
64  * To support usage of rmlock in CVs and msleep yet another list for the
65  * priority tracker would be needed.  Using this lock for cv and msleep also
66  * does not seem very useful
67  */
68 
69 static __inline void compiler_memory_barrier(void) {
70 	__asm __volatile("":::"memory");
71 }
72 
73 static void	assert_rm(struct lock_object *lock, int what);
74 static void	lock_rm(struct lock_object *lock, int how);
75 #ifdef KDTRACE_HOOKS
76 static int	owner_rm(struct lock_object *lock, struct thread **owner);
77 #endif
78 static int	unlock_rm(struct lock_object *lock);
79 
80 struct lock_class lock_class_rm = {
81 	.lc_name = "rm",
82 	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
83 	.lc_assert = assert_rm,
84 #if 0
85 #ifdef DDB
86 	.lc_ddb_show = db_show_rwlock,
87 #endif
88 #endif
89 	.lc_lock = lock_rm,
90 	.lc_unlock = unlock_rm,
91 #ifdef KDTRACE_HOOKS
92 	.lc_owner = owner_rm,
93 #endif
94 };
95 
96 static void
97 assert_rm(struct lock_object *lock, int what)
98 {
99 
100 	panic("assert_rm called");
101 }
102 
103 static void
104 lock_rm(struct lock_object *lock, int how)
105 {
106 
107 	panic("lock_rm called");
108 }
109 
110 static int
111 unlock_rm(struct lock_object *lock)
112 {
113 
114 	panic("unlock_rm called");
115 }
116 
117 #ifdef KDTRACE_HOOKS
118 static int
119 owner_rm(struct lock_object *lock, struct thread **owner)
120 {
121 
122 	panic("owner_rm called");
123 }
124 #endif
125 
126 static struct mtx rm_spinlock;
127 
128 MTX_SYSINIT(rm_spinlock, &rm_spinlock, "rm_spinlock", MTX_SPIN);
129 
130 /*
131  * Add or remove tracker from per cpu list.
132  *
133  * The per cpu list can be traversed at any time in forward direction from an
134  * interrupt on the *local* cpu.
135  */
136 static void inline
137 rm_tracker_add(struct pcpu *pc, struct rm_priotracker *tracker)
138 {
139 	struct rm_queue *next;
140 
141 	/* Initialize all tracker pointers */
142 	tracker->rmp_cpuQueue.rmq_prev = &pc->pc_rm_queue;
143 	next = pc->pc_rm_queue.rmq_next;
144 	tracker->rmp_cpuQueue.rmq_next = next;
145 
146 	/* rmq_prev is not used during froward traversal. */
147 	next->rmq_prev = &tracker->rmp_cpuQueue;
148 
149 	/* Update pointer to first element. */
150 	pc->pc_rm_queue.rmq_next =  &tracker->rmp_cpuQueue;
151 }
152 
153 static void inline
154 rm_tracker_remove(struct pcpu *pc, struct rm_priotracker *tracker)
155 {
156 	struct rm_queue *next, *prev;
157 
158 	next = tracker->rmp_cpuQueue.rmq_next;
159 	prev = tracker->rmp_cpuQueue.rmq_prev;
160 
161 	/* Not used during forward traversal. */
162 	next->rmq_prev = prev;
163 
164 	/* Remove from list. */
165 	prev->rmq_next = next;
166 }
167 
168 static void
169 rm_cleanIPI(void *arg)
170 {
171 	struct pcpu *pc;
172 	struct rmlock *rm = arg;
173 	struct rm_priotracker *tracker;
174 	struct rm_queue *queue;
175 	pc = pcpu_find(curcpu);
176 
177 	for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue;
178 	    queue = queue->rmq_next) {
179 		tracker = (struct rm_priotracker *)queue;
180 		if (tracker->rmp_rmlock == rm && tracker->rmp_flags == 0) {
181 			tracker->rmp_flags = RMPF_ONQUEUE;
182 			mtx_lock_spin(&rm_spinlock);
183 			LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker,
184 			    rmp_qentry);
185 			mtx_unlock_spin(&rm_spinlock);
186 		}
187 	}
188 }
189 
190 void
191 rm_init(struct rmlock *rm, const char *name, int opts)
192 {
193 
194 	rm->rm_noreadtoken = 1;
195 	LIST_INIT(&rm->rm_activeReaders);
196 	mtx_init(&rm->rm_lock, name, "RM_MTX",MTX_NOWITNESS);
197 	lock_init(&rm->lock_object, &lock_class_rm, name, NULL,
198 	    (opts & LO_RECURSABLE)| LO_WITNESS);
199 }
200 
201 void
202 rm_destroy(struct rmlock *rm)
203 {
204 
205 	mtx_destroy(&rm->rm_lock);
206 	lock_destroy(&rm->lock_object);
207 }
208 
209 int
210 rm_wowned(struct rmlock *rm)
211 {
212 
213 	return (mtx_owned(&rm->rm_lock));
214 }
215 
216 void
217 rm_sysinit(void *arg)
218 {
219 
220 	struct rm_args *args = arg;
221 	rm_init(args->ra_rm, args->ra_desc, args->ra_opts);
222 }
223 
224 static void
225 _rm_rlock_hard(struct rmlock *rm, struct rm_priotracker *tracker)
226 {
227 	struct pcpu *pc;
228 	struct rm_queue *queue;
229 	struct rm_priotracker *atracker;
230 
231 	critical_enter();
232 	pc = pcpu_find(curcpu);
233 
234 	/* Check if we just need to do a proper critical_exit. */
235 	if (0 == rm->rm_noreadtoken) {
236 		critical_exit();
237 		return;
238 	}
239 
240 	/* Remove our tracker from the per cpu list. */
241 	rm_tracker_remove(pc, tracker);
242 
243 	/* Check to see if the IPI granted us the lock after all. */
244 	if (tracker->rmp_flags) {
245 		/* Just add back tracker - we hold the lock. */
246 		rm_tracker_add(pc, tracker);
247 		critical_exit();
248 		return;
249 	}
250 
251 	/*
252 	 * We allow readers to aquire a lock even if a writer is blocked if
253 	 * the lock is recursive and the reader already holds the lock.
254 	 */
255 	if ((rm->lock_object.lo_flags & LO_RECURSABLE) != 0) {
256 		/*
257 		 * Just grand the lock if this thread already have a tracker
258 		 * for this lock on the per cpu queue.
259 		 */
260 		for (queue = pc->pc_rm_queue.rmq_next;
261 		    queue !=  &pc->pc_rm_queue; queue = queue->rmq_next) {
262 			atracker = (struct rm_priotracker *)queue;
263 			if ((atracker->rmp_rmlock == rm) &&
264 			    (atracker->rmp_thread == tracker->rmp_thread)) {
265 				mtx_lock_spin(&rm_spinlock);
266 				LIST_INSERT_HEAD(&rm->rm_activeReaders,
267 				    tracker, rmp_qentry);
268 				tracker->rmp_flags = RMPF_ONQUEUE;
269 				mtx_unlock_spin(&rm_spinlock);
270 				rm_tracker_add(pc, tracker);
271 				critical_exit();
272 				return;
273 			}
274 		}
275 	}
276 
277 	sched_unpin();
278 	critical_exit();
279 
280 	mtx_lock(&rm->rm_lock);
281 	rm->rm_noreadtoken = 0;
282 	critical_enter();
283 
284 	pc = pcpu_find(curcpu);
285 	rm_tracker_add(pc, tracker);
286 	sched_pin();
287 	critical_exit();
288 
289 	mtx_unlock(&rm->rm_lock);
290 }
291 
292 void
293 _rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker)
294 {
295 	struct thread *td = curthread;
296 	struct pcpu *pc;
297 
298 	tracker->rmp_flags  = 0;
299 	tracker->rmp_thread = td;
300 	tracker->rmp_rmlock = rm;
301 
302 	td->td_critnest++;	/* critical_enter(); */
303 
304 	compiler_memory_barrier();
305 
306 	pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */
307 
308 	rm_tracker_add(pc, tracker);
309 
310 	td->td_pinned++; /*  sched_pin(); */
311 
312 	compiler_memory_barrier();
313 
314 	td->td_critnest--;
315 
316 	/*
317 	 * Fast path to combine two common conditions into a single
318 	 * conditional jump.
319 	 */
320 	if (0 == (td->td_owepreempt |  rm->rm_noreadtoken))
321 		return;
322 
323 	/* We do not have a read token and need to acquire one. */
324 	_rm_rlock_hard(rm, tracker);
325 }
326 
327 static void
328 _rm_unlock_hard(struct thread *td,struct rm_priotracker *tracker)
329 {
330 
331 	if (td->td_owepreempt) {
332 		td->td_critnest++;
333 		critical_exit();
334 	}
335 
336 	if (!tracker->rmp_flags)
337 		return;
338 
339 	mtx_lock_spin(&rm_spinlock);
340 	LIST_REMOVE(tracker, rmp_qentry);
341 
342 	if (tracker->rmp_flags & RMPF_SIGNAL) {
343 		struct rmlock *rm;
344 		struct turnstile *ts;
345 
346 		rm = tracker->rmp_rmlock;
347 
348 		turnstile_chain_lock(&rm->lock_object);
349 		mtx_unlock_spin(&rm_spinlock);
350 
351 		ts = turnstile_lookup(&rm->lock_object);
352 
353 		turnstile_signal(ts, TS_EXCLUSIVE_QUEUE);
354 		turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
355 		turnstile_chain_unlock(&rm->lock_object);
356 	} else
357 		mtx_unlock_spin(&rm_spinlock);
358 }
359 
360 void
361 _rm_runlock(struct rmlock *rm, struct rm_priotracker *tracker)
362 {
363 	struct pcpu *pc;
364 	struct thread *td = tracker->rmp_thread;
365 
366 	td->td_critnest++;	/* critical_enter(); */
367 	pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */
368 	rm_tracker_remove(pc, tracker);
369 	td->td_critnest--;
370 	td->td_pinned--; /*  sched_unpin(); */
371 
372 	if (0 == (td->td_owepreempt | tracker->rmp_flags))
373 		return;
374 
375 	_rm_unlock_hard(td, tracker);
376 }
377 
378 void
379 _rm_wlock(struct rmlock *rm)
380 {
381 	struct rm_priotracker *prio;
382 	struct turnstile *ts;
383 
384 	mtx_lock(&rm->rm_lock);
385 
386 	if (rm->rm_noreadtoken == 0) {
387 		/* Get all read tokens back */
388 
389 		rm->rm_noreadtoken = 1;
390 
391 		/*
392 		 * Assumes rm->rm_noreadtoken update is visible on other CPUs
393 		 * before rm_cleanIPI is called.
394 		 */
395 #ifdef SMP
396    		smp_rendezvous(smp_no_rendevous_barrier,
397 		    rm_cleanIPI,
398 		    smp_no_rendevous_barrier,
399 		    rm);
400 
401 #else
402 		rm_cleanIPI(rm);
403 #endif
404 
405 		mtx_lock_spin(&rm_spinlock);
406 		while ((prio = LIST_FIRST(&rm->rm_activeReaders)) != NULL) {
407 			ts = turnstile_trywait(&rm->lock_object);
408 			prio->rmp_flags = RMPF_ONQUEUE | RMPF_SIGNAL;
409 			mtx_unlock_spin(&rm_spinlock);
410 			turnstile_wait(ts, prio->rmp_thread,
411 			    TS_EXCLUSIVE_QUEUE);
412 			mtx_lock_spin(&rm_spinlock);
413 		}
414 		mtx_unlock_spin(&rm_spinlock);
415 	}
416 }
417 
418 void
419 _rm_wunlock(struct rmlock *rm)
420 {
421 
422 	mtx_unlock(&rm->rm_lock);
423 }
424 
425 #ifdef LOCK_DEBUG
426 
427 void _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
428 {
429 
430 	WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE,
431 	    file, line, NULL);
432 
433 	_rm_wlock(rm);
434 
435 	LOCK_LOG_LOCK("RMWLOCK", &rm->lock_object, 0, 0, file, line);
436 
437 	WITNESS_LOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
438 
439 	curthread->td_locks++;
440 
441 }
442 
443 void
444 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
445 {
446 
447 	curthread->td_locks--;
448 	WITNESS_UNLOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
449 	LOCK_LOG_LOCK("RMWUNLOCK", &rm->lock_object, 0, 0, file, line);
450 	_rm_wunlock(rm);
451 }
452 
453 void
454 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
455     const char *file, int line)
456 {
457 
458 	WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER, file, line, NULL);
459 
460 	_rm_rlock(rm, tracker);
461 
462 	LOCK_LOG_LOCK("RMRLOCK", &rm->lock_object, 0, 0, file, line);
463 
464 	WITNESS_LOCK(&rm->lock_object, 0, file, line);
465 
466 	curthread->td_locks++;
467 }
468 
469 void
470 _rm_runlock_debug(struct rmlock *rm,  struct rm_priotracker *tracker,
471     const char *file, int line)
472 {
473 
474 	curthread->td_locks--;
475 	WITNESS_UNLOCK(&rm->lock_object, 0, file, line);
476 	LOCK_LOG_LOCK("RMRUNLOCK", &rm->lock_object, 0, 0, file, line);
477 	_rm_runlock(rm, tracker);
478 }
479 
480 #else
481 
482 /*
483  * Just strip out file and line arguments if no lock debugging is enabled in
484  * the kernel - we are called from a kernel module.
485  */
486 void
487 _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
488 {
489 
490 	_rm_wlock(rm);
491 }
492 
493 void
494 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
495 {
496 
497 	_rm_wunlock(rm);
498 }
499 
500 void
501 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
502     const char *file, int line)
503 {
504 
505 	_rm_rlock(rm, tracker);
506 }
507 
508 void
509 _rm_runlock_debug(struct rmlock *rm,  struct rm_priotracker *tracker,
510     const char *file, int line) {
511 
512 	_rm_runlock(rm, tracker);
513 }
514 
515 #endif
516