Re: [PATCH] locking/rwsem: Prevent non-first waiter from spinning in down_write() slowpath

From: Waiman Long
Date: Wed Oct 12 2022 - 09:20:31 EST


On 10/12/22 00:04, Hillf Danton wrote:
you mean, you want to check and change waiter->handoff_set on every run
rwsem_try_write_lock().

Yes, with RWSEM_FLAG_HANDOFF set, it is too late for non first waiters to
spin, and with both RWSEM_LOCK_MASK and RWSEM_FLAG_HANDOFF set, the rivals
in the RWSEM_LOCK_MASK have an uphand over the first waiter wrt acquiring
the lock, and it is not a bad option for the first waiter to take a step
back off.

if (count & RWSEM_LOCK_MASK) {
if (has_handoff || (!rt_task(waiter->task) &&
!time_after(jiffies, waiter->timeout)))
return false;

new |= RWSEM_FLAG_HANDOFF;
} else {

But does it break optimistic spinning ? @waiman ?
Waiters spin for acquiring lock instead of lockup and your report shows
spinning too much makes trouble. The key is stop spinning neither too
late nor too early. My proposal is a simple one with as few heuristics
added as possible.

Yes, too much spinning is bad if we have RT tasks in the mix, otherwise it should be fine.

Cheers,
Longman