Re: [PATCH-tip v6 01/20] locking/rwsem: Prevent decrement of reader count before increment

From: Linus Torvalds
Date: Sun Apr 28 2019 - 13:59:30 EST


On Sun, Apr 28, 2019 at 10:41 AM Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> It's the *first* loop that you could play games with, because you hold
> the lock, and the list is stable during that loop. So the *first* loop
> could just walk the list, and then do one list splitting operation
> instead of doing that "list_move_tail()" thing for each entry.

.. having looked at that, I would suggest against it.

I _think_ this short and sweet code snippet might just work fine for
the first loop:

list_for_each_entry(waiter, &sem->wait_list, list) {
if (waiter->type == RWSEM_WAITING_FOR_WRITE)
break;
woken++;
}
list_cut_before(&wlist, &sem->wait_list, waiter);

and if it *does* work it would be both smaller and more efficient. But
it looks a bit too subtle to my taste. Somebody would need to go
through that with a fine comb, and double-check that it gets the
"whole list" case right, for example.

So the "phase 1" loop could be perhaps simplified to the above cute things.

But the "phase 2" loop absolutely has to be changed to use
list_for_each_entry_safe().

Linus