2017-10-04 16:33:43 +00:00
|
|
|
/*
|
2010-05-17 22:18:00 +00:00
|
|
|
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
|
|
|
* Copyright (C) 2007 The Regents of the University of California.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
|
|
|
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
2008-05-26 04:38:26 +00:00
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
2013-03-05 01:26:55 +00:00
|
|
|
* For details, see <http://zfsonlinux.org/>.
|
2010-05-17 22:18:00 +00:00
|
|
|
*
|
|
|
|
* The SPL is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
2008-05-26 04:38:26 +00:00
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
2008-05-26 04:38:26 +00:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2010-05-17 22:18:00 +00:00
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
2017-10-04 16:33:43 +00:00
|
|
|
*/
|
2008-05-26 04:38:26 +00:00
|
|
|
|
2008-02-28 00:52:31 +00:00
|
|
|
#ifndef _SPL_RWLOCK_H
|
2018-02-07 19:49:38 +00:00
|
|
|
#define _SPL_RWLOCK_H
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-03-01 00:45:59 +00:00
|
|
|
#include <sys/types.h>
|
2009-09-25 21:14:35 +00:00
|
|
|
#include <linux/rwsem.h>
|
2018-02-16 01:53:18 +00:00
|
|
|
#include <linux/sched.h>
|
2018-02-15 01:01:15 +00:00
|
|
|
|
|
|
|
/* Linux kernel compatibility */
|
|
|
|
#if defined(CONFIG_PREEMPT_RT_FULL)
|
|
|
|
#define SPL_RWSEM_SINGLE_READER_VALUE (1)
|
|
|
|
#define SPL_RWSEM_SINGLE_WRITER_VALUE (0)
|
|
|
|
#elif defined(CONFIG_RWSEM_GENERIC_SPINLOCK)
|
|
|
|
#define SPL_RWSEM_SINGLE_READER_VALUE (1)
|
|
|
|
#define SPL_RWSEM_SINGLE_WRITER_VALUE (-1)
|
|
|
|
#else
|
|
|
|
#define SPL_RWSEM_SINGLE_READER_VALUE (RWSEM_ACTIVE_READ_BIAS)
|
|
|
|
#define SPL_RWSEM_SINGLE_WRITER_VALUE (RWSEM_ACTIVE_WRITE_BIAS)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Linux 3.16 changed activity to count for rwsem-spinlock */
|
|
|
|
#if defined(CONFIG_PREEMPT_RT_FULL)
|
|
|
|
#define RWSEM_COUNT(sem) sem->read_depth
|
|
|
|
#elif defined(HAVE_RWSEM_ACTIVITY)
|
|
|
|
#define RWSEM_COUNT(sem) sem->activity
|
|
|
|
/* Linux 4.8 changed count to an atomic_long_t for !rwsem-spinlock */
|
|
|
|
#elif defined(HAVE_RWSEM_ATOMIC_LONG_COUNT)
|
|
|
|
#define RWSEM_COUNT(sem) atomic_long_read(&(sem)->count)
|
|
|
|
#else
|
|
|
|
#define RWSEM_COUNT(sem) sem->count
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(RWSEM_SPINLOCK_IS_RAW)
|
|
|
|
#define spl_rwsem_lock_irqsave(lk, fl) raw_spin_lock_irqsave(lk, fl)
|
|
|
|
#define spl_rwsem_unlock_irqrestore(lk, fl) \
|
|
|
|
raw_spin_unlock_irqrestore(lk, fl)
|
|
|
|
#define spl_rwsem_trylock_irqsave(lk, fl) raw_spin_trylock_irqsave(lk, fl)
|
|
|
|
#else
|
|
|
|
#define spl_rwsem_lock_irqsave(lk, fl) spin_lock_irqsave(lk, fl)
|
|
|
|
#define spl_rwsem_unlock_irqrestore(lk, fl) spin_unlock_irqrestore(lk, fl)
|
|
|
|
#define spl_rwsem_trylock_irqsave(lk, fl) spin_trylock_irqsave(lk, fl)
|
|
|
|
#endif /* RWSEM_SPINLOCK_IS_RAW */
|
|
|
|
|
|
|
|
#define spl_rwsem_is_locked(rwsem) rwsem_is_locked(rwsem)
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
typedef enum {
|
2015-09-01 16:02:55 +00:00
|
|
|
RW_DRIVER = 2,
|
2015-10-06 21:01:46 +00:00
|
|
|
RW_DEFAULT = 4,
|
|
|
|
RW_NOLOCKDEP = 5
|
2008-02-26 20:36:04 +00:00
|
|
|
} krw_type_t;
|
|
|
|
|
|
|
|
typedef enum {
|
2015-09-01 16:02:55 +00:00
|
|
|
RW_NONE = 0,
|
|
|
|
RW_WRITER = 1,
|
|
|
|
RW_READER = 2
|
2008-02-26 20:36:04 +00:00
|
|
|
} krw_t;
|
|
|
|
|
2015-09-01 15:28:02 +00:00
|
|
|
/*
|
|
|
|
* If CONFIG_RWSEM_SPIN_ON_OWNER is defined, rw_semaphore will have an owner
|
|
|
|
* field, so we don't need our own.
|
|
|
|
*/
|
2009-09-25 21:14:35 +00:00
|
|
|
typedef struct {
|
2015-09-01 16:02:55 +00:00
|
|
|
struct rw_semaphore rw_rwlock;
|
2015-09-01 15:28:02 +00:00
|
|
|
#ifndef CONFIG_RWSEM_SPIN_ON_OWNER
|
2015-09-01 16:02:55 +00:00
|
|
|
kthread_t *rw_owner;
|
2015-09-01 15:28:02 +00:00
|
|
|
#endif
|
2015-10-06 21:01:46 +00:00
|
|
|
#ifdef CONFIG_LOCKDEP
|
|
|
|
krw_type_t rw_type;
|
|
|
|
#endif /* CONFIG_LOCKDEP */
|
2009-09-25 21:14:35 +00:00
|
|
|
} krwlock_t;
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
#define SEM(rwp) (&(rwp)->rw_rwlock)
|
2009-09-25 21:14:35 +00:00
|
|
|
|
|
|
|
static inline void
|
|
|
|
spl_rw_set_owner(krwlock_t *rwp)
|
|
|
|
{
|
2015-09-01 15:28:02 +00:00
|
|
|
/*
|
|
|
|
* If CONFIG_RWSEM_SPIN_ON_OWNER is defined, down_write, up_write,
|
|
|
|
* downgrade_write and __init_rwsem will set/clear owner for us.
|
|
|
|
*/
|
|
|
|
#ifndef CONFIG_RWSEM_SPIN_ON_OWNER
|
2015-09-01 13:55:06 +00:00
|
|
|
rwp->rw_owner = current;
|
2015-09-01 15:28:02 +00:00
|
|
|
#endif
|
2009-09-25 21:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
spl_rw_clear_owner(krwlock_t *rwp)
|
|
|
|
{
|
2015-09-01 15:28:02 +00:00
|
|
|
#ifndef CONFIG_RWSEM_SPIN_ON_OWNER
|
2015-09-01 13:55:06 +00:00
|
|
|
rwp->rw_owner = NULL;
|
2015-09-01 15:28:02 +00:00
|
|
|
#endif
|
2009-09-25 21:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline kthread_t *
|
|
|
|
rw_owner(krwlock_t *rwp)
|
|
|
|
{
|
2015-09-01 15:28:02 +00:00
|
|
|
#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
|
2018-02-07 19:49:38 +00:00
|
|
|
return (SEM(rwp)->owner);
|
2015-09-01 15:28:02 +00:00
|
|
|
#else
|
2018-02-07 19:49:38 +00:00
|
|
|
return (rwp->rw_owner);
|
2015-09-01 15:28:02 +00:00
|
|
|
#endif
|
2009-09-25 21:14:35 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 21:01:46 +00:00
|
|
|
#ifdef CONFIG_LOCKDEP
|
|
|
|
static inline void
|
|
|
|
spl_rw_set_type(krwlock_t *rwp, krw_type_t type)
|
|
|
|
{
|
|
|
|
rwp->rw_type = type;
|
|
|
|
}
|
|
|
|
static inline void
|
|
|
|
spl_rw_lockdep_off_maybe(krwlock_t *rwp) \
|
|
|
|
{ \
|
|
|
|
if (rwp && rwp->rw_type == RW_NOLOCKDEP) \
|
|
|
|
lockdep_off(); \
|
|
|
|
}
|
|
|
|
static inline void
|
|
|
|
spl_rw_lockdep_on_maybe(krwlock_t *rwp) \
|
|
|
|
{ \
|
|
|
|
if (rwp && rwp->rw_type == RW_NOLOCKDEP) \
|
|
|
|
lockdep_on(); \
|
|
|
|
}
|
|
|
|
#else /* CONFIG_LOCKDEP */
|
2018-02-07 19:49:38 +00:00
|
|
|
#define spl_rw_set_type(rwp, type)
|
|
|
|
#define spl_rw_lockdep_off_maybe(rwp)
|
|
|
|
#define spl_rw_lockdep_on_maybe(rwp)
|
2015-10-06 21:01:46 +00:00
|
|
|
#endif /* CONFIG_LOCKDEP */
|
|
|
|
|
2009-09-25 21:14:35 +00:00
|
|
|
static inline int
|
|
|
|
RW_READ_HELD(krwlock_t *rwp)
|
|
|
|
{
|
2016-10-08 03:53:58 +00:00
|
|
|
/*
|
|
|
|
* Linux 4.8 will set owner to 1 when read held instead of leave it
|
|
|
|
* NULL. So we check whether owner <= 1.
|
|
|
|
*/
|
|
|
|
return (spl_rwsem_is_locked(SEM(rwp)) &&
|
|
|
|
(unsigned long)rw_owner(rwp) <= 1);
|
2009-09-25 21:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
RW_WRITE_HELD(krwlock_t *rwp)
|
|
|
|
{
|
2015-09-01 13:55:06 +00:00
|
|
|
return (rw_owner(rwp) == current);
|
2009-09-25 21:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
RW_LOCK_HELD(krwlock_t *rwp)
|
|
|
|
{
|
2018-02-07 19:49:38 +00:00
|
|
|
return (spl_rwsem_is_locked(SEM(rwp)));
|
2009-09-25 21:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-07-30 16:22:30 +00:00
|
|
|
* The following functions must be a #define and not static inline.
|
2009-09-25 21:14:35 +00:00
|
|
|
* This ensures that the native linux semaphore functions (down/up)
|
|
|
|
* will be correctly located in the users code which is important
|
|
|
|
* for the built in kernel lock analysis tools
|
|
|
|
*/
|
2018-02-07 19:49:38 +00:00
|
|
|
/* BEGIN CSTYLED */
|
|
|
|
#define rw_init(rwp, name, type, arg) \
|
2015-09-01 16:02:55 +00:00
|
|
|
({ \
|
|
|
|
static struct lock_class_key __key; \
|
2015-10-06 21:01:46 +00:00
|
|
|
ASSERT(type == RW_DEFAULT || type == RW_NOLOCKDEP); \
|
2015-09-01 16:02:55 +00:00
|
|
|
\
|
|
|
|
__init_rwsem(SEM(rwp), #rwp, &__key); \
|
|
|
|
spl_rw_clear_owner(rwp); \
|
2015-10-06 21:01:46 +00:00
|
|
|
spl_rw_set_type(rwp, type); \
|
2009-09-18 23:09:47 +00:00
|
|
|
})
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2018-07-30 16:22:30 +00:00
|
|
|
/*
|
|
|
|
* The Linux rwsem implementation does not require a matching destroy.
|
|
|
|
*/
|
|
|
|
#define rw_destroy(rwp) ((void) 0)
|
2008-03-06 23:42:37 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
#define rw_tryenter(rwp, rw) \
|
2015-09-01 16:02:55 +00:00
|
|
|
({ \
|
|
|
|
int _rc_ = 0; \
|
|
|
|
\
|
2015-10-06 21:01:46 +00:00
|
|
|
spl_rw_lockdep_off_maybe(rwp); \
|
2015-09-01 16:02:55 +00:00
|
|
|
switch (rw) { \
|
|
|
|
case RW_READER: \
|
|
|
|
_rc_ = down_read_trylock(SEM(rwp)); \
|
|
|
|
break; \
|
|
|
|
case RW_WRITER: \
|
|
|
|
if ((_rc_ = down_write_trylock(SEM(rwp)))) \
|
|
|
|
spl_rw_set_owner(rwp); \
|
|
|
|
break; \
|
|
|
|
default: \
|
|
|
|
VERIFY(0); \
|
|
|
|
} \
|
2015-10-06 21:01:46 +00:00
|
|
|
spl_rw_lockdep_on_maybe(rwp); \
|
2015-09-01 16:02:55 +00:00
|
|
|
_rc_; \
|
2008-05-06 23:00:49 +00:00
|
|
|
})
|
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
#define rw_enter(rwp, rw) \
|
2015-09-01 16:02:55 +00:00
|
|
|
({ \
|
2015-10-06 21:01:46 +00:00
|
|
|
spl_rw_lockdep_off_maybe(rwp); \
|
2015-09-01 16:02:55 +00:00
|
|
|
switch (rw) { \
|
|
|
|
case RW_READER: \
|
|
|
|
down_read(SEM(rwp)); \
|
|
|
|
break; \
|
|
|
|
case RW_WRITER: \
|
|
|
|
down_write(SEM(rwp)); \
|
|
|
|
spl_rw_set_owner(rwp); \
|
|
|
|
break; \
|
|
|
|
default: \
|
|
|
|
VERIFY(0); \
|
|
|
|
} \
|
2015-10-06 21:01:46 +00:00
|
|
|
spl_rw_lockdep_on_maybe(rwp); \
|
2009-09-18 23:09:47 +00:00
|
|
|
})
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
#define rw_exit(rwp) \
|
2015-09-01 16:02:55 +00:00
|
|
|
({ \
|
2015-10-06 21:01:46 +00:00
|
|
|
spl_rw_lockdep_off_maybe(rwp); \
|
2015-09-01 16:02:55 +00:00
|
|
|
if (RW_WRITE_HELD(rwp)) { \
|
|
|
|
spl_rw_clear_owner(rwp); \
|
|
|
|
up_write(SEM(rwp)); \
|
|
|
|
} else { \
|
|
|
|
ASSERT(RW_READ_HELD(rwp)); \
|
|
|
|
up_read(SEM(rwp)); \
|
|
|
|
} \
|
2015-10-06 21:01:46 +00:00
|
|
|
spl_rw_lockdep_on_maybe(rwp); \
|
2009-09-25 21:14:35 +00:00
|
|
|
})
|
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
#define rw_downgrade(rwp) \
|
2015-09-01 16:02:55 +00:00
|
|
|
({ \
|
2015-10-06 21:01:46 +00:00
|
|
|
spl_rw_lockdep_off_maybe(rwp); \
|
2015-09-01 16:02:55 +00:00
|
|
|
spl_rw_clear_owner(rwp); \
|
|
|
|
downgrade_write(SEM(rwp)); \
|
2015-10-06 21:01:46 +00:00
|
|
|
spl_rw_lockdep_on_maybe(rwp); \
|
2009-09-25 21:14:35 +00:00
|
|
|
})
|
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
#define rw_tryupgrade(rwp) \
|
2015-09-01 16:02:55 +00:00
|
|
|
({ \
|
|
|
|
int _rc_ = 0; \
|
|
|
|
\
|
2016-03-09 22:20:48 +00:00
|
|
|
if (RW_WRITE_HELD(rwp)) { \
|
|
|
|
_rc_ = 1; \
|
|
|
|
} else { \
|
2016-05-25 23:35:42 +00:00
|
|
|
spl_rw_lockdep_off_maybe(rwp); \
|
|
|
|
if ((_rc_ = rwsem_tryupgrade(SEM(rwp)))) \
|
|
|
|
spl_rw_set_owner(rwp); \
|
|
|
|
spl_rw_lockdep_on_maybe(rwp); \
|
2015-09-01 16:02:55 +00:00
|
|
|
} \
|
|
|
|
_rc_; \
|
2009-09-25 21:14:35 +00:00
|
|
|
})
|
2018-02-07 19:49:38 +00:00
|
|
|
/* END CSTYLED */
|
2009-09-25 21:14:35 +00:00
|
|
|
|
|
|
|
int spl_rw_init(void);
|
|
|
|
void spl_rw_fini(void);
|
2018-02-15 01:01:15 +00:00
|
|
|
int rwsem_tryupgrade(struct rw_semaphore *rwsem);
|
2009-09-25 21:14:35 +00:00
|
|
|
|
2008-05-06 23:00:49 +00:00
|
|
|
#endif /* _SPL_RWLOCK_H */
|