Skip to content

Commit

Permalink
locking/rwsem: Add down_read_killable()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Kirill Tkhai authored and Ingo Molnar committed Oct 10, 2017
1 parent 19c6092 commit 76f8507
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/asm-generic/rwsem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions include/linux/rwsem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions kernel/locking/rwsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 76f8507

Please sign in to comment.