diff options
author | Brian Behlendorf <[email protected]> | 2016-03-09 14:20:48 -0800 |
---|---|---|
committer | Brian Behlendorf <[email protected]> | 2016-03-10 13:05:25 -0800 |
commit | a6ae97caed620b0e9e0575346062c751f6f15483 (patch) | |
tree | 15c8b923a044403a5bda9c8e207ebe48f8af5902 /include | |
parent | 47f98247814fff50d02ddc0772197842c57cdc3a (diff) |
Add rw_tryupgrade()
This implementation of rw_tryupgrade() behaves slightly differently
from its counterparts on other platforms. It drops the RW_READER lock
and then acquires the RW_WRITER lock leaving a small window where no
lock is held. On other platforms the lock is never released during
the upgrade process. This is necessary under Linux because the kernel
does not provide an upgrade function.
There are currently no callers in the ZFS code where this change in
behavior is a problem. In fact, in most cases the code is already
written such that if the upgrade fails the RW_READER lock is dropped
and the caller blocks waiting to acquire the lock as RW_WRITER.
Signed-off-by: Brian Behlendorf <[email protected]>
Signed-off-by: Tim Chase <[email protected]>
Signed-off-by: Matthew Thode <[email protected]>
Closes zfsonlinux/zfs#4388
Closes #534
Diffstat (limited to 'include')
-rw-r--r-- | include/sys/rwlock.h | 50 |
1 files changed, 16 insertions, 34 deletions
diff --git a/include/sys/rwlock.h b/include/sys/rwlock.h index c82764ce9..14d097b01 100644 --- a/include/sys/rwlock.h +++ b/include/sys/rwlock.h @@ -208,49 +208,31 @@ RW_LOCK_HELD(krwlock_t *rwp) spl_rw_lockdep_on_maybe(rwp); \ }) -#if defined(CONFIG_RWSEM_GENERIC_SPINLOCK) -#ifdef CONFIG_RWSEM_SPIN_ON_OWNER -#error spinlock rwsem should not have spin on owner -#endif /* - * For the generic implementations of rw-semaphores the following is - * true. If your semaphore implementation internally represents the - * semaphore state differently then special case handling is required. - * - if activity/count is 0 then there are no active readers or writers - * - if activity/count is +ve then that is the number of active readers - * - if activity/count is -1 then there is one active writer + * This implementation of rw_tryupgrade() behaves slightly differently + * from its counterparts on other platforms. It drops the RW_READER lock + * and then acquires the RW_WRITER lock leaving a small window where no + * lock is held. On other platforms the lock is never released during + * the upgrade process. This is necessary under Linux because the kernel + * does not provide an upgrade function. */ - -extern void __up_read_locked(struct rw_semaphore *); -extern int __down_write_trylock_locked(struct rw_semaphore *); - #define rw_tryupgrade(rwp) \ ({ \ - unsigned long _flags_; \ int _rc_ = 0; \ \ - spl_rw_lockdep_off_maybe(rwp); \ - spl_rwsem_lock_irqsave(&SEM(rwp)->wait_lock, _flags_); \ - if ((list_empty(&SEM(rwp)->wait_list)) && \ - (SEM(rwp)->activity == 1)) { \ - __up_read_locked(SEM(rwp)); \ - VERIFY(_rc_ = __down_write_trylock_locked(SEM(rwp))); \ - (rwp)->rw_owner = current; \ + if (RW_WRITE_HELD(rwp)) { \ + _rc_ = 1; \ + } else { \ + rw_exit(rwp); \ + if (rw_tryenter(rwp, RW_WRITER)) { \ + _rc_ = 1; \ + } else { \ + rw_enter(rwp, RW_READER); \ + _rc_ = 0; \ + } \ } \ - spl_rwsem_unlock_irqrestore(&SEM(rwp)->wait_lock, _flags_); \ - spl_rw_lockdep_on_maybe(rwp); \ _rc_; \ }) -#else -/* - * rw_tryupgrade() can be implemented correctly but for each supported - * arch we will need a custom implementation. For the x86 implementation - * it looks like a custom cmpxchg() to atomically check and promote the - * rwsem would be safe. For now that's not worth the trouble so in this - * case rw_tryupgrade() has just been disabled. - */ -#define rw_tryupgrade(rwp) ({ 0; }) -#endif int spl_rw_init(void); void spl_rw_fini(void); |