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/>.
|
2008-05-26 08:38:26 +04:00
|
|
|
*
|
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.
|
|
|
|
*
|
|
|
|
* 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/>.
|
2018-02-07 22:49:38 +03:00
|
|
|
*
|
2010-05-18 02:18:00 +04:00
|
|
|
* Solaris Porting Layer (SPL) Reader/Writer Lock Implementation.
|
2017-10-04 19:33:43 +03:00
|
|
|
*/
|
2008-05-26 08:38:26 +04:00
|
|
|
|
2008-03-01 03:45:59 +03:00
|
|
|
#include <sys/rwlock.h>
|
2018-02-16 04:53:18 +03:00
|
|
|
#include <linux/module.h>
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2016-12-17 19:09:57 +03:00
|
|
|
#if defined(CONFIG_PREEMPT_RT_FULL)
|
|
|
|
|
|
|
|
#include <linux/rtmutex.h>
|
2017-01-20 01:41:38 +03:00
|
|
|
#define RT_MUTEX_OWNER_MASKALL 1UL
|
2016-12-17 19:09:57 +03:00
|
|
|
|
|
|
|
static int
|
|
|
|
__rwsem_tryupgrade(struct rw_semaphore *rwsem)
|
|
|
|
{
|
2018-07-30 19:22:30 +03:00
|
|
|
#if defined(READER_BIAS) && defined(WRITER_BIAS)
|
|
|
|
/*
|
|
|
|
* After the 4.9.20-rt16 kernel the realtime patch series lifted the
|
|
|
|
* single reader restriction. While this could be accommodated by
|
|
|
|
* adding additional compatibility code assume the rwsem can never
|
|
|
|
* be upgraded. All caller must already cleanly handle this case.
|
|
|
|
*/
|
|
|
|
return (0);
|
|
|
|
#else
|
2017-01-20 01:41:38 +03:00
|
|
|
ASSERT((struct task_struct *)
|
|
|
|
((unsigned long)rwsem->lock.owner & ~RT_MUTEX_OWNER_MASKALL) ==
|
|
|
|
current);
|
2016-12-17 19:09:57 +03:00
|
|
|
|
|
|
|
/*
|
2018-07-30 19:22:30 +03:00
|
|
|
* Prior to 4.9.20-rt16 kernel the realtime patch series, rwsem is
|
|
|
|
* implemented as a single mutex held by readers and writers alike.
|
|
|
|
* However, this implementation would prevent a thread from taking
|
|
|
|
* a read lock twice, as the mutex would already be locked on
|
2016-12-17 19:09:57 +03:00
|
|
|
* the second attempt. Therefore the implementation allows a
|
|
|
|
* single thread to take a rwsem as read lock multiple times
|
|
|
|
* tracking that nesting as read_depth counter.
|
|
|
|
*/
|
|
|
|
if (rwsem->read_depth <= 1) {
|
|
|
|
/*
|
|
|
|
* In case, the current thread has not taken the lock
|
|
|
|
* more than once as read lock, we can allow an
|
|
|
|
* upgrade to a write lock. rwsem_rt.h implements
|
|
|
|
* write locks as read_depth == 0.
|
|
|
|
*/
|
|
|
|
rwsem->read_depth = 0;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
2018-07-30 19:22:30 +03:00
|
|
|
#endif
|
2016-12-17 19:09:57 +03:00
|
|
|
}
|
|
|
|
#elif defined(CONFIG_RWSEM_GENERIC_SPINLOCK)
|
2016-05-26 02:35:42 +03:00
|
|
|
static int
|
|
|
|
__rwsem_tryupgrade(struct rw_semaphore *rwsem)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
spl_rwsem_lock_irqsave(&rwsem->wait_lock, flags);
|
|
|
|
if (RWSEM_COUNT(rwsem) == SPL_RWSEM_SINGLE_READER_VALUE &&
|
|
|
|
list_empty(&rwsem->wait_list)) {
|
|
|
|
ret = 1;
|
|
|
|
RWSEM_COUNT(rwsem) = SPL_RWSEM_SINGLE_WRITER_VALUE;
|
|
|
|
}
|
|
|
|
spl_rwsem_unlock_irqrestore(&rwsem->wait_lock, flags);
|
|
|
|
return (ret);
|
|
|
|
}
|
2016-07-27 02:37:46 +03:00
|
|
|
#elif defined(HAVE_RWSEM_ATOMIC_LONG_COUNT)
|
|
|
|
static int
|
|
|
|
__rwsem_tryupgrade(struct rw_semaphore *rwsem)
|
|
|
|
{
|
|
|
|
long val;
|
|
|
|
val = atomic_long_cmpxchg(&rwsem->count, SPL_RWSEM_SINGLE_READER_VALUE,
|
|
|
|
SPL_RWSEM_SINGLE_WRITER_VALUE);
|
|
|
|
return (val == SPL_RWSEM_SINGLE_READER_VALUE);
|
|
|
|
}
|
2016-05-26 02:35:42 +03:00
|
|
|
#else
|
|
|
|
static int
|
|
|
|
__rwsem_tryupgrade(struct rw_semaphore *rwsem)
|
|
|
|
{
|
2018-02-07 22:49:38 +03:00
|
|
|
typeof(rwsem->count) val;
|
2016-05-26 02:35:42 +03:00
|
|
|
val = cmpxchg(&rwsem->count, SPL_RWSEM_SINGLE_READER_VALUE,
|
|
|
|
SPL_RWSEM_SINGLE_WRITER_VALUE);
|
|
|
|
return (val == SPL_RWSEM_SINGLE_READER_VALUE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
|
|
|
rwsem_tryupgrade(struct rw_semaphore *rwsem)
|
|
|
|
{
|
|
|
|
if (__rwsem_tryupgrade(rwsem)) {
|
|
|
|
rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
|
|
|
|
rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
|
|
|
|
#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
|
|
|
|
rwsem->owner = current;
|
|
|
|
#endif
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rwsem_tryupgrade);
|
|
|
|
|
2009-09-26 01:14:35 +04:00
|
|
|
int spl_rw_init(void) { return 0; }
|
|
|
|
void spl_rw_fini(void) { }
|