2017-10-04 19:33:43 +03:00
|
|
|
/*
|
2010-05-18 02:18:00 +04: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 08:38:26 +04:00
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
2010-05-18 02:18:00 +04:00
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
2013-03-05 05:26:55 +04:00
|
|
|
* For details, see <http://zfsonlinux.org/>.
|
2010-05-18 02:18:00 +04: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 08:38:26 +04:00
|
|
|
*
|
2010-05-18 02:18:00 +04:00
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
2008-05-26 08:38:26 +04: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-18 02:18:00 +04:00
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
2017-10-04 19:33:43 +03:00
|
|
|
*/
|
2008-05-26 08:38:26 +04:00
|
|
|
|
2008-02-28 03:52:31 +03:00
|
|
|
#ifndef _SPL_RWLOCK_H
|
2018-02-07 22:49:38 +03:00
|
|
|
#define _SPL_RWLOCK_H
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-03-01 03:45:59 +03:00
|
|
|
#include <sys/types.h>
|
2009-09-26 01:14:35 +04:00
|
|
|
#include <linux/rwsem.h>
|
2018-02-16 04:53:18 +03:00
|
|
|
#include <linux/sched.h>
|
2018-02-15 04:01:15 +03: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)
|
2019-05-23 23:46:33 +03:00
|
|
|
#elif defined(RWSEM_ACTIVE_MASK)
|
2018-02-15 04:01:15 +03:00
|
|
|
#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 23:36:04 +03:00
|
|
|
|
|
|
|
typedef enum {
|
2015-09-01 19:02:55 +03:00
|
|
|
RW_DRIVER = 2,
|
2015-10-07 00:01:46 +03:00
|
|
|
RW_DEFAULT = 4,
|
|
|
|
RW_NOLOCKDEP = 5
|
2008-02-26 23:36:04 +03:00
|
|
|
} krw_type_t;
|
|
|
|
|
|
|
|
typedef enum {
|
2015-09-01 19:02:55 +03:00
|
|
|
RW_NONE = 0,
|
|
|
|
RW_WRITER = 1,
|
|
|
|
RW_READER = 2
|
2008-02-26 23:36:04 +03:00
|
|
|
} krw_t;
|
|
|
|
|
2015-09-01 18:28:02 +03: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-26 01:14:35 +04:00
|
|
|
typedef struct {
|
2015-09-01 19:02:55 +03:00
|
|
|
struct rw_semaphore rw_rwlock;
|
2015-09-01 18:28:02 +03:00
|
|
|
#ifndef CONFIG_RWSEM_SPIN_ON_OWNER
|
2015-09-01 19:02:55 +03:00
|
|
|
kthread_t *rw_owner;
|
2015-09-01 18:28:02 +03:00
|
|
|
#endif
|
2015-10-07 00:01:46 +03:00
|
|
|
#ifdef CONFIG_LOCKDEP
|
|
|
|
krw_type_t rw_type;
|
|
|
|
#endif /* CONFIG_LOCKDEP */
|
2009-09-26 01:14:35 +04:00
|
|
|
} krwlock_t;
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2018-02-07 22:49:38 +03:00
|
|
|
#define SEM(rwp) (&(rwp)->rw_rwlock)
|
2009-09-26 01:14:35 +04:00
|
|
|
|
|
|
|
static inline void
|
|
|
|
spl_rw_set_owner(krwlock_t *rwp)
|
|
|
|
{
|
2015-09-01 18:28:02 +03: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 16:55:06 +03:00
|
|
|
rwp->rw_owner = current;
|
2015-09-01 18:28:02 +03:00
|
|
|
#endif
|
2009-09-26 01:14:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
spl_rw_clear_owner(krwlock_t *rwp)
|
|
|
|
{
|
2015-09-01 18:28:02 +03:00
|
|
|
#ifndef CONFIG_RWSEM_SPIN_ON_OWNER
|
2015-09-01 16:55:06 +03:00
|
|
|
rwp->rw_owner = NULL;
|
2015-09-01 18:28:02 +03:00
|
|
|
#endif
|
2009-09-26 01:14:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline kthread_t *
|
|
|
|
rw_owner(krwlock_t *rwp)
|
|
|
|
{
|
2015-09-01 18:28:02 +03:00
|
|
|
#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
|
2018-02-07 22:49:38 +03:00
|
|
|
return (SEM(rwp)->owner);
|
2015-09-01 18:28:02 +03:00
|
|
|
#else
|
2018-02-07 22:49:38 +03:00
|
|
|
return (rwp->rw_owner);
|
2015-09-01 18:28:02 +03:00
|
|
|
#endif
|
2009-09-26 01:14:35 +04:00
|
|
|
}
|
|
|
|
|
2015-10-07 00:01:46 +03: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 22:49:38 +03:00
|
|
|
#define spl_rw_set_type(rwp, type)
|
|
|
|
#define spl_rw_lockdep_off_maybe(rwp)
|
|
|
|
#define spl_rw_lockdep_on_maybe(rwp)
|
2015-10-07 00:01:46 +03:00
|
|
|
#endif /* CONFIG_LOCKDEP */
|
|
|
|
|
2009-09-26 01:14:35 +04:00
|
|
|
|
|
|
|
static inline int
|
|
|
|
RW_WRITE_HELD(krwlock_t *rwp)
|
|
|
|
{
|
2015-09-01 16:55:06 +03:00
|
|
|
return (rw_owner(rwp) == current);
|
2009-09-26 01:14:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
RW_LOCK_HELD(krwlock_t *rwp)
|
|
|
|
{
|
2018-02-07 22:49:38 +03:00
|
|
|
return (spl_rwsem_is_locked(SEM(rwp)));
|
2009-09-26 01:14:35 +04:00
|
|
|
}
|
|
|
|
|
2019-02-15 23:37:20 +03:00
|
|
|
static inline int
|
|
|
|
RW_READ_HELD(krwlock_t *rwp)
|
|
|
|
{
|
|
|
|
if (!RW_LOCK_HELD(rwp))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rw_semaphore cheat sheet:
|
|
|
|
*
|
|
|
|
* < 3.16:
|
|
|
|
* There's no rw_semaphore.owner, so use rwp.owner instead.
|
|
|
|
* If rwp.owner == NULL then it's a reader
|
|
|
|
*
|
|
|
|
* 3.16 - 4.7:
|
|
|
|
* rw_semaphore.owner added (https://lwn.net/Articles/596656/)
|
|
|
|
* and CONFIG_RWSEM_SPIN_ON_OWNER introduced.
|
|
|
|
* If rw_semaphore.owner == NULL then it's a reader
|
|
|
|
*
|
|
|
|
* 4.8 - 4.16.16:
|
|
|
|
* RWSEM_READER_OWNED added as an internal #define.
|
|
|
|
* (https://lore.kernel.org/patchwork/patch/678590/)
|
|
|
|
* If rw_semaphore.owner == 1 then it's a reader
|
|
|
|
*
|
|
|
|
* 4.16.17 - 4.19:
|
|
|
|
* RWSEM_OWNER_UNKNOWN introduced as ((struct task_struct *)-1L)
|
|
|
|
* (https://do-db2.lkml.org/lkml/2018/5/15/985)
|
|
|
|
* If rw_semaphore.owner == 1 then it's a reader.
|
|
|
|
*
|
|
|
|
* 4.20+:
|
|
|
|
* RWSEM_OWNER_UNKNOWN changed to ((struct task_struct *)-2L)
|
|
|
|
* (https://lkml.org/lkml/2018/9/6/986)
|
|
|
|
* If rw_semaphore.owner & 1 then it's a reader, and also the reader's
|
|
|
|
* task_struct may be embedded in rw_semaphore->owner.
|
|
|
|
*/
|
|
|
|
#if defined(CONFIG_RWSEM_SPIN_ON_OWNER) && defined(RWSEM_OWNER_UNKNOWN)
|
|
|
|
if (RWSEM_OWNER_UNKNOWN == (struct task_struct *)-2L) {
|
|
|
|
/* 4.20+ kernels with CONFIG_RWSEM_SPIN_ON_OWNER */
|
|
|
|
return ((unsigned long) SEM(rwp)->owner & 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* < 4.20 kernel or !CONFIG_RWSEM_SPIN_ON_OWNER */
|
|
|
|
return (rw_owner(rwp) == NULL || (unsigned long) rw_owner(rwp) == 1);
|
|
|
|
}
|
|
|
|
|
2009-09-26 01:14:35 +04:00
|
|
|
/*
|
2018-07-30 19:22:30 +03:00
|
|
|
* The following functions must be a #define and not static inline.
|
2009-09-26 01:14:35 +04: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 22:49:38 +03:00
|
|
|
/* BEGIN CSTYLED */
|
|
|
|
#define rw_init(rwp, name, type, arg) \
|
2015-09-01 19:02:55 +03:00
|
|
|
({ \
|
|
|
|
static struct lock_class_key __key; \
|
2015-10-07 00:01:46 +03:00
|
|
|
ASSERT(type == RW_DEFAULT || type == RW_NOLOCKDEP); \
|
2015-09-01 19:02:55 +03:00
|
|
|
\
|
|
|
|
__init_rwsem(SEM(rwp), #rwp, &__key); \
|
|
|
|
spl_rw_clear_owner(rwp); \
|
2015-10-07 00:01:46 +03:00
|
|
|
spl_rw_set_type(rwp, type); \
|
2009-09-19 03:09:47 +04:00
|
|
|
})
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2018-07-30 19:22:30 +03:00
|
|
|
/*
|
|
|
|
* The Linux rwsem implementation does not require a matching destroy.
|
|
|
|
*/
|
|
|
|
#define rw_destroy(rwp) ((void) 0)
|
2008-03-07 02:42:37 +03:00
|
|
|
|
2018-02-07 22:49:38 +03:00
|
|
|
#define rw_tryenter(rwp, rw) \
|
2015-09-01 19:02:55 +03:00
|
|
|
({ \
|
|
|
|
int _rc_ = 0; \
|
|
|
|
\
|
2015-10-07 00:01:46 +03:00
|
|
|
spl_rw_lockdep_off_maybe(rwp); \
|
2015-09-01 19:02:55 +03: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-07 00:01:46 +03:00
|
|
|
spl_rw_lockdep_on_maybe(rwp); \
|
2015-09-01 19:02:55 +03:00
|
|
|
_rc_; \
|
2008-05-07 03:00:49 +04:00
|
|
|
})
|
|
|
|
|
2018-02-07 22:49:38 +03:00
|
|
|
#define rw_enter(rwp, rw) \
|
2015-09-01 19:02:55 +03:00
|
|
|
({ \
|
2015-10-07 00:01:46 +03:00
|
|
|
spl_rw_lockdep_off_maybe(rwp); \
|
2015-09-01 19:02:55 +03: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-07 00:01:46 +03:00
|
|
|
spl_rw_lockdep_on_maybe(rwp); \
|
2009-09-19 03:09:47 +04:00
|
|
|
})
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2018-02-07 22:49:38 +03:00
|
|
|
#define rw_exit(rwp) \
|
2015-09-01 19:02:55 +03:00
|
|
|
({ \
|
2015-10-07 00:01:46 +03:00
|
|
|
spl_rw_lockdep_off_maybe(rwp); \
|
2015-09-01 19:02:55 +03: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-07 00:01:46 +03:00
|
|
|
spl_rw_lockdep_on_maybe(rwp); \
|
2009-09-26 01:14:35 +04:00
|
|
|
})
|
|
|
|
|
2018-02-07 22:49:38 +03:00
|
|
|
#define rw_downgrade(rwp) \
|
2015-09-01 19:02:55 +03:00
|
|
|
({ \
|
2015-10-07 00:01:46 +03:00
|
|
|
spl_rw_lockdep_off_maybe(rwp); \
|
2015-09-01 19:02:55 +03:00
|
|
|
spl_rw_clear_owner(rwp); \
|
|
|
|
downgrade_write(SEM(rwp)); \
|
2015-10-07 00:01:46 +03:00
|
|
|
spl_rw_lockdep_on_maybe(rwp); \
|
2009-09-26 01:14:35 +04:00
|
|
|
})
|
|
|
|
|
2018-02-07 22:49:38 +03:00
|
|
|
#define rw_tryupgrade(rwp) \
|
2015-09-01 19:02:55 +03:00
|
|
|
({ \
|
|
|
|
int _rc_ = 0; \
|
|
|
|
\
|
2016-03-10 01:20:48 +03:00
|
|
|
if (RW_WRITE_HELD(rwp)) { \
|
|
|
|
_rc_ = 1; \
|
|
|
|
} else { \
|
2016-05-26 02:35:42 +03: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 19:02:55 +03:00
|
|
|
} \
|
|
|
|
_rc_; \
|
2009-09-26 01:14:35 +04:00
|
|
|
})
|
2018-02-07 22:49:38 +03:00
|
|
|
/* END CSTYLED */
|
2009-09-26 01:14:35 +04:00
|
|
|
|
|
|
|
int spl_rw_init(void);
|
|
|
|
void spl_rw_fini(void);
|
2018-02-15 04:01:15 +03:00
|
|
|
int rwsem_tryupgrade(struct rw_semaphore *rwsem);
|
2009-09-26 01:14:35 +04:00
|
|
|
|
2008-05-07 03:00:49 +04:00
|
|
|
#endif /* _SPL_RWLOCK_H */
|