[tip:locking/core] locking/rwsem: Add down_read_killable()

From: tip-bot for Kirill Tkhai
Date: Tue Oct 10 2017 - 07:09:03 EST


Commit-ID: 76f8507f7a6442215df19de74f07eabca2462f1e
Gitweb: https://git.kernel.org/tip/76f8507f7a6442215df19de74f07eabca2462f1e
Author: Kirill Tkhai <ktkhai@xxxxxxxxxxxxx>
AuthorDate: Fri, 29 Sep 2017 19:06:38 +0300
Committer: Ingo Molnar <mingo@xxxxxxxxxx>
CommitDate: Tue, 10 Oct 2017 11:50:16 +0200

locking/rwsem: Add down_read_killable()

Similar to down_read() and down_write_killable(),
add killable version of down_read(), based on
__down_read_killable() function, added in previous
patches.

Signed-off-by: Kirill Tkhai <ktkhai@xxxxxxxxxxxxx>
Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: arnd@xxxxxxxx
Cc: avagin@xxxxxxxxxxxxx
Cc: davem@xxxxxxxxxxxxx
Cc: fenghua.yu@xxxxxxxxx
Cc: gorcunov@xxxxxxxxxxxxx
Cc: heiko.carstens@xxxxxxxxxx
Cc: hpa@xxxxxxxxx
Cc: ink@xxxxxxxxxxxxxxxxxxxx
Cc: mattst88@xxxxxxxxx
Cc: rientjes@xxxxxxxxxx
Cc: rth@xxxxxxxxxxx
Cc: schwidefsky@xxxxxxxxxx
Cc: tony.luck@xxxxxxxxx
Cc: viro@xxxxxxxxxxxxxxxxxx
Link: http://lkml.kernel.org/r/150670119884.23930.2585570605960763239.stgit@xxxxxxxxxxxxxxxxxxxxx
Signed-off-by: Ingo Molnar <mingo@xxxxxxxxxx>
---
include/asm-generic/rwsem.h | 10 ++++++++++
include/linux/rwsem.h | 1 +
kernel/locking/rwsem.c | 16 ++++++++++++++++
3 files changed, 27 insertions(+)

diff --git a/include/asm-generic/rwsem.h b/include/asm-generic/rwsem.h
index 6c6a214..b2d68d2 100644
--- a/include/asm-generic/rwsem.h
+++ b/include/asm-generic/rwsem.h
@@ -37,6 +37,16 @@ static inline void __down_read(struct rw_semaphore *sem)
rwsem_down_read_failed(sem);
}

+static inline int __down_read_killable(struct rw_semaphore *sem)
+{
+ if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0)) {
+ if (IS_ERR(rwsem_down_read_failed_killable(sem)))
+ return -EINTR;
+ }
+
+ return 0;
+}
+
static inline int __down_read_trylock(struct rw_semaphore *sem)
{
long tmp;
diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
index 0ad7318..6ac8ee5 100644
--- a/include/linux/rwsem.h
+++ b/include/linux/rwsem.h
@@ -111,6 +111,7 @@ static inline int rwsem_is_contended(struct rw_semaphore *sem)
* lock for reading
*/
extern void down_read(struct rw_semaphore *sem);
+extern int __must_check down_read_killable(struct rw_semaphore *sem);

/*
* trylock for reading -- returns 1 if successful, 0 if contention
diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
index 4d48b1c..e53f7746 100644
--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -28,6 +28,22 @@ void __sched down_read(struct rw_semaphore *sem)

EXPORT_SYMBOL(down_read);

+int __sched down_read_killable(struct rw_semaphore *sem)
+{
+ might_sleep();
+ rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
+
+ if (LOCK_CONTENDED_RETURN(sem, __down_read_trylock, __down_read_killable)) {
+ rwsem_release(&sem->dep_map, 1, _RET_IP_);
+ return -EINTR;
+ }
+
+ rwsem_set_reader_owned(sem);
+ return 0;
+}
+
+EXPORT_SYMBOL(down_read_killable);
+
/*
* trylock for reading -- returns 1 if successful, 0 if contention
*/