[PATCH 1/2] locking/rwsem: Add down_write_interruptible

Zack Rusin zackr at vmware.com
Mon Mar 8 20:54:55 UTC 2021


Add an interruptible version of down_write. It's the other
side of the already implemented down_read_interruptible.
It allows drivers which used custom locking code to
support interruptible rw semaphores to switch over
to rwsem.

Cc: Peter Zijlstra <peterz at infradead.org>
Cc: Ingo Molnar <mingo at redhat.com>
Cc: Will Deacon <will at kernel.org>
Cc: linux-kernel at vger.kernel.org
Cc: dri-devel at lists.freedesktop.org
---
 include/linux/rwsem.h  |  1 +
 kernel/locking/rwsem.c | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
index 4c715be48717..753ae2cb8677 100644
--- a/include/linux/rwsem.h
+++ b/include/linux/rwsem.h
@@ -135,6 +135,7 @@ extern int down_read_trylock(struct rw_semaphore *sem);
  * lock for writing
  */
 extern void down_write(struct rw_semaphore *sem);
+extern int __must_check down_write_interruptible(struct rw_semaphore *sem);
 extern int __must_check down_write_killable(struct rw_semaphore *sem);
 
 /*
diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
index abba5df50006..0eadd20347de 100644
--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -1270,6 +1270,11 @@ static inline void __down_write(struct rw_semaphore *sem)
 	__down_write_common(sem, TASK_UNINTERRUPTIBLE);
 }
 
+static inline int __down_write_interruptible(struct rw_semaphore *sem)
+{
+	return __down_write_common(sem, TASK_INTERRUPTIBLE);
+}
+
 static inline int __down_write_killable(struct rw_semaphore *sem)
 {
 	return __down_write_common(sem, TASK_KILLABLE);
@@ -1408,6 +1413,24 @@ void __sched down_write(struct rw_semaphore *sem)
 }
 EXPORT_SYMBOL(down_write);
 
+/*
+ * interruptible lock for writing
+ */
+int __sched down_write_interruptible(struct rw_semaphore *sem)
+{
+	might_sleep();
+	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
+
+	if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock,
+				  __down_write_interruptible)) {
+		rwsem_release(&sem->dep_map, _RET_IP_);
+		return -EINTR;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(down_write_interruptible);
+
 /*
  * lock for writing
  */
-- 
2.27.0



More information about the dri-devel mailing list