15326Sek110237 /*
25326Sek110237 * CDDL HEADER START
35326Sek110237 *
45326Sek110237 * The contents of this file are subject to the terms of the
55326Sek110237 * Common Development and Distribution License (the "License").
65326Sek110237 * You may not use this file except in compliance with the License.
75326Sek110237 *
85326Sek110237 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95326Sek110237 * or http://www.opensolaris.org/os/licensing.
105326Sek110237 * See the License for the specific language governing permissions
115326Sek110237 * and limitations under the License.
125326Sek110237 *
135326Sek110237 * When distributing Covered Code, include this CDDL HEADER in each
145326Sek110237 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155326Sek110237 * If applicable, add the following below this CDDL HEADER, with the
165326Sek110237 * fields enclosed by brackets "[]" replaced with your own identifying
175326Sek110237 * information: Portions Copyright [yyyy] [name of copyright owner]
185326Sek110237 *
195326Sek110237 * CDDL HEADER END
205326Sek110237 */
215326Sek110237 /*
22*9981STim.Haley@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
235326Sek110237 * Use is subject to license terms.
245326Sek110237 */
255326Sek110237
265326Sek110237 #include <sys/refcount.h>
275326Sek110237 #include <sys/rrwlock.h>
285326Sek110237
295326Sek110237 /*
305326Sek110237 * This file contains the implementation of a re-entrant read
315326Sek110237 * reader/writer lock (aka "rrwlock").
325326Sek110237 *
335326Sek110237 * This is a normal reader/writer lock with the additional feature
345326Sek110237 * of allowing threads who have already obtained a read lock to
355326Sek110237 * re-enter another read lock (re-entrant read) - even if there are
365326Sek110237 * waiting writers.
375326Sek110237 *
385326Sek110237 * Callers who have not obtained a read lock give waiting writers priority.
395326Sek110237 *
405326Sek110237 * The rrwlock_t lock does not allow re-entrant writers, nor does it
415326Sek110237 * allow a re-entrant mix of reads and writes (that is, it does not
425326Sek110237 * allow a caller who has already obtained a read lock to be able to
435326Sek110237 * then grab a write lock without first dropping all read locks, and
445326Sek110237 * vice versa).
455326Sek110237 *
465326Sek110237 * The rrwlock_t uses tsd (thread specific data) to keep a list of
475326Sek110237 * nodes (rrw_node_t), where each node keeps track of which specific
485326Sek110237 * lock (rrw_node_t::rn_rrl) the thread has grabbed. Since re-entering
495326Sek110237 * should be rare, a thread that grabs multiple reads on the same rrwlock_t
505326Sek110237 * will store multiple rrw_node_ts of the same 'rrn_rrl'. Nodes on the
515326Sek110237 * tsd list can represent a different rrwlock_t. This allows a thread
525326Sek110237 * to enter multiple and unique rrwlock_ts for read locks at the same time.
535326Sek110237 *
545326Sek110237 * Since using tsd exposes some overhead, the rrwlock_t only needs to
555326Sek110237 * keep tsd data when writers are waiting. If no writers are waiting, then
565326Sek110237 * a reader just bumps the anonymous read count (rr_anon_rcount) - no tsd
575326Sek110237 * is needed. Once a writer attempts to grab the lock, readers then
585326Sek110237 * keep tsd data and bump the linked readers count (rr_linked_rcount).
595326Sek110237 *
605326Sek110237 * If there are waiting writers and there are anonymous readers, then a
615326Sek110237 * reader doesn't know if it is a re-entrant lock. But since it may be one,
625326Sek110237 * we allow the read to proceed (otherwise it could deadlock). Since once
635326Sek110237 * waiting writers are active, readers no longer bump the anonymous count,
645326Sek110237 * the anonymous readers will eventually flush themselves out. At this point,
655326Sek110237 * readers will be able to tell if they are a re-entrant lock (have a
665326Sek110237 * rrw_node_t entry for the lock) or not. If they are a re-entrant lock, then
675326Sek110237 * we must let the proceed. If they are not, then the reader blocks for the
685326Sek110237 * waiting writers. Hence, we do not starve writers.
695326Sek110237 */
705326Sek110237
715326Sek110237 /* global key for TSD */
725326Sek110237 uint_t rrw_tsd_key;
735326Sek110237
745326Sek110237 typedef struct rrw_node {
755326Sek110237 struct rrw_node *rn_next;
765326Sek110237 rrwlock_t *rn_rrl;
775326Sek110237 } rrw_node_t;
785326Sek110237
795326Sek110237 static rrw_node_t *
rrn_find(rrwlock_t * rrl)805326Sek110237 rrn_find(rrwlock_t *rrl)
815326Sek110237 {
825326Sek110237 rrw_node_t *rn;
835326Sek110237
845326Sek110237 if (refcount_count(&rrl->rr_linked_rcount) == 0)
855326Sek110237 return (NULL);
865326Sek110237
875326Sek110237 for (rn = tsd_get(rrw_tsd_key); rn != NULL; rn = rn->rn_next) {
885326Sek110237 if (rn->rn_rrl == rrl)
895326Sek110237 return (rn);
905326Sek110237 }
915326Sek110237 return (NULL);
925326Sek110237 }
935326Sek110237
945326Sek110237 /*
955326Sek110237 * Add a node to the head of the singly linked list.
965326Sek110237 */
975326Sek110237 static void
rrn_add(rrwlock_t * rrl)985326Sek110237 rrn_add(rrwlock_t *rrl)
995326Sek110237 {
1005326Sek110237 rrw_node_t *rn;
1015326Sek110237
1025326Sek110237 rn = kmem_alloc(sizeof (*rn), KM_SLEEP);
1035326Sek110237 rn->rn_rrl = rrl;
1045326Sek110237 rn->rn_next = tsd_get(rrw_tsd_key);
1055326Sek110237 VERIFY(tsd_set(rrw_tsd_key, rn) == 0);
1065326Sek110237 }
1075326Sek110237
1085326Sek110237 /*
1095326Sek110237 * If a node is found for 'rrl', then remove the node from this
1105326Sek110237 * thread's list and return TRUE; otherwise return FALSE.
1115326Sek110237 */
1125326Sek110237 static boolean_t
rrn_find_and_remove(rrwlock_t * rrl)1135326Sek110237 rrn_find_and_remove(rrwlock_t *rrl)
1145326Sek110237 {
1155326Sek110237 rrw_node_t *rn;
1165326Sek110237 rrw_node_t *prev = NULL;
1175326Sek110237
1185326Sek110237 if (refcount_count(&rrl->rr_linked_rcount) == 0)
119*9981STim.Haley@Sun.COM return (B_FALSE);
1205326Sek110237
1215326Sek110237 for (rn = tsd_get(rrw_tsd_key); rn != NULL; rn = rn->rn_next) {
1225326Sek110237 if (rn->rn_rrl == rrl) {
1235326Sek110237 if (prev)
1245326Sek110237 prev->rn_next = rn->rn_next;
1255326Sek110237 else
1265326Sek110237 VERIFY(tsd_set(rrw_tsd_key, rn->rn_next) == 0);
1275326Sek110237 kmem_free(rn, sizeof (*rn));
1285326Sek110237 return (B_TRUE);
1295326Sek110237 }
1305326Sek110237 prev = rn;
1315326Sek110237 }
1325326Sek110237 return (B_FALSE);
1335326Sek110237 }
1345326Sek110237
1355326Sek110237 void
rrw_init(rrwlock_t * rrl)1365326Sek110237 rrw_init(rrwlock_t *rrl)
1375326Sek110237 {
1385326Sek110237 mutex_init(&rrl->rr_lock, NULL, MUTEX_DEFAULT, NULL);
1395326Sek110237 cv_init(&rrl->rr_cv, NULL, CV_DEFAULT, NULL);
1405326Sek110237 rrl->rr_writer = NULL;
1415326Sek110237 refcount_create(&rrl->rr_anon_rcount);
1425326Sek110237 refcount_create(&rrl->rr_linked_rcount);
1435326Sek110237 rrl->rr_writer_wanted = B_FALSE;
1445326Sek110237 }
1455326Sek110237
1465326Sek110237 void
rrw_destroy(rrwlock_t * rrl)1475326Sek110237 rrw_destroy(rrwlock_t *rrl)
1485326Sek110237 {
1495326Sek110237 mutex_destroy(&rrl->rr_lock);
1505326Sek110237 cv_destroy(&rrl->rr_cv);
1515326Sek110237 ASSERT(rrl->rr_writer == NULL);
1525326Sek110237 refcount_destroy(&rrl->rr_anon_rcount);
1535326Sek110237 refcount_destroy(&rrl->rr_linked_rcount);
1545326Sek110237 }
1555326Sek110237
1565326Sek110237 static void
rrw_enter_read(rrwlock_t * rrl,void * tag)1575326Sek110237 rrw_enter_read(rrwlock_t *rrl, void *tag)
1585326Sek110237 {
1595326Sek110237 mutex_enter(&rrl->rr_lock);
160*9981STim.Haley@Sun.COM #if !defined(DEBUG) && defined(_KERNEL)
161*9981STim.Haley@Sun.COM if (!rrl->rr_writer && !rrl->rr_writer_wanted) {
162*9981STim.Haley@Sun.COM rrl->rr_anon_rcount.rc_count++;
163*9981STim.Haley@Sun.COM mutex_exit(&rrl->rr_lock);
164*9981STim.Haley@Sun.COM return;
165*9981STim.Haley@Sun.COM }
166*9981STim.Haley@Sun.COM DTRACE_PROBE(zfs__rrwfastpath__rdmiss);
167*9981STim.Haley@Sun.COM #endif
1685326Sek110237 ASSERT(rrl->rr_writer != curthread);
1695326Sek110237 ASSERT(refcount_count(&rrl->rr_anon_rcount) >= 0);
1705326Sek110237
1715326Sek110237 while (rrl->rr_writer || (rrl->rr_writer_wanted &&
1725326Sek110237 refcount_is_zero(&rrl->rr_anon_rcount) &&
1735326Sek110237 rrn_find(rrl) == NULL))
1745326Sek110237 cv_wait(&rrl->rr_cv, &rrl->rr_lock);
1755326Sek110237
1765326Sek110237 if (rrl->rr_writer_wanted) {
1775326Sek110237 /* may or may not be a re-entrant enter */
1785326Sek110237 rrn_add(rrl);
1795326Sek110237 (void) refcount_add(&rrl->rr_linked_rcount, tag);
1805326Sek110237 } else {
1815326Sek110237 (void) refcount_add(&rrl->rr_anon_rcount, tag);
1825326Sek110237 }
1835326Sek110237 ASSERT(rrl->rr_writer == NULL);
1845326Sek110237 mutex_exit(&rrl->rr_lock);
1855326Sek110237 }
1865326Sek110237
1875326Sek110237 static void
rrw_enter_write(rrwlock_t * rrl)1885326Sek110237 rrw_enter_write(rrwlock_t *rrl)
1895326Sek110237 {
1905326Sek110237 mutex_enter(&rrl->rr_lock);
1915326Sek110237 ASSERT(rrl->rr_writer != curthread);
1925326Sek110237
1935326Sek110237 while (refcount_count(&rrl->rr_anon_rcount) > 0 ||
1945326Sek110237 refcount_count(&rrl->rr_linked_rcount) > 0 ||
1955326Sek110237 rrl->rr_writer != NULL) {
1965326Sek110237 rrl->rr_writer_wanted = B_TRUE;
1975326Sek110237 cv_wait(&rrl->rr_cv, &rrl->rr_lock);
1985326Sek110237 }
1995326Sek110237 rrl->rr_writer_wanted = B_FALSE;
2005326Sek110237 rrl->rr_writer = curthread;
2015326Sek110237 mutex_exit(&rrl->rr_lock);
2025326Sek110237 }
2035326Sek110237
2045326Sek110237 void
rrw_enter(rrwlock_t * rrl,krw_t rw,void * tag)2055326Sek110237 rrw_enter(rrwlock_t *rrl, krw_t rw, void *tag)
2065326Sek110237 {
2075326Sek110237 if (rw == RW_READER)
2085326Sek110237 rrw_enter_read(rrl, tag);
2095326Sek110237 else
2105326Sek110237 rrw_enter_write(rrl);
2115326Sek110237 }
2125326Sek110237
2135326Sek110237 void
rrw_exit(rrwlock_t * rrl,void * tag)2145326Sek110237 rrw_exit(rrwlock_t *rrl, void *tag)
2155326Sek110237 {
2165326Sek110237 mutex_enter(&rrl->rr_lock);
217*9981STim.Haley@Sun.COM #if !defined(DEBUG) && defined(_KERNEL)
218*9981STim.Haley@Sun.COM if (!rrl->rr_writer && rrl->rr_linked_rcount.rc_count == 0) {
219*9981STim.Haley@Sun.COM rrl->rr_anon_rcount.rc_count--;
220*9981STim.Haley@Sun.COM if (rrl->rr_anon_rcount.rc_count == 0)
221*9981STim.Haley@Sun.COM cv_broadcast(&rrl->rr_cv);
222*9981STim.Haley@Sun.COM mutex_exit(&rrl->rr_lock);
223*9981STim.Haley@Sun.COM return;
224*9981STim.Haley@Sun.COM }
225*9981STim.Haley@Sun.COM DTRACE_PROBE(zfs__rrwfastpath__exitmiss);
226*9981STim.Haley@Sun.COM #endif
2275326Sek110237 ASSERT(!refcount_is_zero(&rrl->rr_anon_rcount) ||
2285326Sek110237 !refcount_is_zero(&rrl->rr_linked_rcount) ||
2295326Sek110237 rrl->rr_writer != NULL);
2305326Sek110237
2315326Sek110237 if (rrl->rr_writer == NULL) {
232*9981STim.Haley@Sun.COM int64_t count;
233*9981STim.Haley@Sun.COM if (rrn_find_and_remove(rrl))
234*9981STim.Haley@Sun.COM count = refcount_remove(&rrl->rr_linked_rcount, tag);
235*9981STim.Haley@Sun.COM else
236*9981STim.Haley@Sun.COM count = refcount_remove(&rrl->rr_anon_rcount, tag);
237*9981STim.Haley@Sun.COM if (count == 0)
238*9981STim.Haley@Sun.COM cv_broadcast(&rrl->rr_cv);
2395326Sek110237 } else {
2405326Sek110237 ASSERT(rrl->rr_writer == curthread);
2415326Sek110237 ASSERT(refcount_is_zero(&rrl->rr_anon_rcount) &&
2425326Sek110237 refcount_is_zero(&rrl->rr_linked_rcount));
2435326Sek110237 rrl->rr_writer = NULL;
2445326Sek110237 cv_broadcast(&rrl->rr_cv);
2455326Sek110237 }
2465326Sek110237 mutex_exit(&rrl->rr_lock);
2475326Sek110237 }
2485326Sek110237
2495326Sek110237 boolean_t
rrw_held(rrwlock_t * rrl,krw_t rw)2505326Sek110237 rrw_held(rrwlock_t *rrl, krw_t rw)
2515326Sek110237 {
2525326Sek110237 boolean_t held;
2535326Sek110237
2545326Sek110237 mutex_enter(&rrl->rr_lock);
2555326Sek110237 if (rw == RW_WRITER) {
2565326Sek110237 held = (rrl->rr_writer == curthread);
2575326Sek110237 } else {
2585326Sek110237 held = (!refcount_is_zero(&rrl->rr_anon_rcount) ||
2595326Sek110237 !refcount_is_zero(&rrl->rr_linked_rcount));
2605326Sek110237 }
2615326Sek110237 mutex_exit(&rrl->rr_lock);
2625326Sek110237
2635326Sek110237 return (held);
2645326Sek110237 }
265