Skip to content

Commit 76f8507

Browse files
Kirill TkhaiIngo Molnar
authored andcommitted
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 <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Link: http://lkml.kernel.org/r/150670119884.23930.2585570605960763239.stgit@localhost.localdomain Signed-off-by: Ingo Molnar <[email protected]>
1 parent 19c6092 commit 76f8507

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

include/asm-generic/rwsem.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ static inline void __down_read(struct rw_semaphore *sem)
3737
rwsem_down_read_failed(sem);
3838
}
3939

40+
static inline int __down_read_killable(struct rw_semaphore *sem)
41+
{
42+
if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0)) {
43+
if (IS_ERR(rwsem_down_read_failed_killable(sem)))
44+
return -EINTR;
45+
}
46+
47+
return 0;
48+
}
49+
4050
static inline int __down_read_trylock(struct rw_semaphore *sem)
4151
{
4252
long tmp;

include/linux/rwsem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ static inline int rwsem_is_contended(struct rw_semaphore *sem)
111111
* lock for reading
112112
*/
113113
extern void down_read(struct rw_semaphore *sem);
114+
extern int __must_check down_read_killable(struct rw_semaphore *sem);
114115

115116
/*
116117
* trylock for reading -- returns 1 if successful, 0 if contention

kernel/locking/rwsem.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ void __sched down_read(struct rw_semaphore *sem)
2828

2929
EXPORT_SYMBOL(down_read);
3030

31+
int __sched down_read_killable(struct rw_semaphore *sem)
32+
{
33+
might_sleep();
34+
rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
35+
36+
if (LOCK_CONTENDED_RETURN(sem, __down_read_trylock, __down_read_killable)) {
37+
rwsem_release(&sem->dep_map, 1, _RET_IP_);
38+
return -EINTR;
39+
}
40+
41+
rwsem_set_reader_owned(sem);
42+
return 0;
43+
}
44+
45+
EXPORT_SYMBOL(down_read_killable);
46+
3147
/*
3248
* trylock for reading -- returns 1 if successful, 0 if contention
3349
*/

0 commit comments

Comments
 (0)