Skip to content

Commit

Permalink
Add rw_tryupgrade()
Browse files Browse the repository at this point in the history
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 <behlendorf1@llnl.gov>
Signed-off-by: Tim Chase <tim@chase2k.com>
Signed-off-by: Matthew Thode <prometheanfire@gentoo.org>
Closes openzfs/zfs#4388
Closes openzfs#534
  • Loading branch information
behlendorf authored and Chunwei Chen committed Sep 7, 2016
1 parent f650547 commit b17a719
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 101 deletions.
57 changes: 22 additions & 35 deletions include/sys/rwlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,44 +167,31 @@ RW_LOCK_HELD(krwlock_t *rwp)
downgrade_write(SEM(rwp)); \
})

#if defined(CONFIG_RWSEM_GENERIC_SPINLOCK)
/*
* 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_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; \
} \
spl_rwsem_unlock_irqrestore(&SEM(rwp)->wait_lock, _flags_); \
_rc_; \
#define rw_tryupgrade(rwp) \
({ \
int _rc_ = 0; \
\
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; \
} \
} \
_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);
Expand Down
60 changes: 0 additions & 60 deletions module/spl/spl-rwlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,65 +32,5 @@

#define DEBUG_SUBSYSTEM S_RWLOCK

#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK

/*
* From lib/rwsem-spinlock.c but modified such that the caller is
* responsible for acquiring and dropping the sem->wait_lock.
*/
struct rwsem_waiter {
struct list_head list;
struct task_struct *task;
unsigned int flags;
#define RWSEM_WAITING_FOR_READ 0x00000001
#define RWSEM_WAITING_FOR_WRITE 0x00000002
};

/* wake a single writer */
static struct rw_semaphore *
__rwsem_wake_one_writer_locked(struct rw_semaphore *sem)
{
struct rwsem_waiter *waiter;
struct task_struct *tsk;

sem->activity = -1;

waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
list_del(&waiter->list);

tsk = waiter->task;
smp_mb();
waiter->task = NULL;
wake_up_process(tsk);
put_task_struct(tsk);
return sem;
}

/* release a read lock on the semaphore */
void
__up_read_locked(struct rw_semaphore *sem)
{
if (--sem->activity == 0 && !list_empty(&sem->wait_list))
(void)__rwsem_wake_one_writer_locked(sem);
}
EXPORT_SYMBOL(__up_read_locked);

/* trylock for writing -- returns 1 if successful, 0 if contention */
int
__down_write_trylock_locked(struct rw_semaphore *sem)
{
int ret = 0;

if (sem->activity == 0 && list_empty(&sem->wait_list)) {
sem->activity = -1;
ret = 1;
}

return ret;
}
EXPORT_SYMBOL(__down_write_trylock_locked);

#endif

int spl_rw_init(void) { return 0; }
void spl_rw_fini(void) { }
6 changes: 0 additions & 6 deletions module/splat/splat-rwlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,6 @@ splat_rwlock_test6(struct file *file, void *arg)
goto out;
}

#if defined(CONFIG_RWSEM_GENERIC_SPINLOCK)
/* With one reader upgrade should never fail. */
rc = rw_tryupgrade(&rwp->rw_rwlock);
if (!rc) {
Expand All @@ -610,11 +609,6 @@ splat_rwlock_test6(struct file *file, void *arg)
rc = 0;
splat_vprint(file, SPLAT_RWLOCK_TEST6_NAME, "%s",
"rwlock properly upgraded\n");
#else
rc = 0;
splat_vprint(file, SPLAT_RWLOCK_TEST6_NAME, "%s",
"rw_tryupgrade() is disabled for this arch\n");
#endif
out:
rw_exit(&rwp->rw_rwlock);
rw_destroy(&rwp->rw_rwlock);
Expand Down

0 comments on commit b17a719

Please sign in to comment.