RE: [PATCH-tip v7 08/20] locking/rwsem: Implement lock handoff to prevent lock starvation

From: David Laight
Date: Fri May 03 2019 - 10:37:56 EST


From: Peter Zijlstra
> Sent: 03 May 2019 14:11
> To: Waiman Long
...
> I've changed that like so.
>
> --- a/kernel/locking/rwsem.c
> +++ b/kernel/locking/rwsem.c
> @@ -406,19 +406,23 @@ static inline bool rwsem_try_write_lock(
> long new;
>
> lockdep_assert_held(&sem->wait_lock);
> +
> do {
> bool has_handoff = !!(count & RWSEM_FLAG_HANDOFF);
>
> if (has_handoff && wstate == WRITER_NOT_FIRST)
> return false;
>
> + new = count;
> +
> if (count & RWSEM_LOCK_MASK) {
> if (has_handoff || (wstate != WRITER_HANDOFF))
> return false;
> - new = count | RWSEM_FLAG_HANDOFF;
> +
> + new |= RWSEM_FLAG_HANDOFF;
> } else {
> - new = (count | RWSEM_WRITER_LOCKED) &
> - ~RWSEM_FLAG_HANDOFF;
> + new |= RWSEM_WRITER_LOCKED;
> + new &= ~RWSEM_FLAG_HANDOFF;
>
> if (list_is_singular(&sem->wait_list))
> new &= ~RWSEM_FLAG_WAITERS;

The compiler will probably convert it back :-)

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)