Re: [PATCH] locking/rwlocks: do not starve writers

From: Linus Torvalds
Date: Fri Jun 17 2022 - 15:09:13 EST


On Fri, Jun 17, 2022 at 12:45 PM Eric Dumazet <edumazet@xxxxxxxxxx> wrote:
>
> Were rwlocks always unfair, and we have been lucky ?

Yes, rwlocks have always been unfair.

And the unfairness is very fundamental: it is what allows you to take
a read-lock without disabling interrupts (or softirqs), and still
allowing interrupts to *also* take the lock for reading.

That's actually a big deal for some things, with the tasklist lock
being the traditional thing, but there could be others.

Because with a fair lock, you'd have deadlocks when CPU1 gets the lock
for reading, then another CPU blocks on writing (with interrupts
disabled), and then CPU1 takes an interrupt and wants to read again.

This is not going to change.

If you want fair rwlocks, you have a couple of options:

- add a new explicitlly fair version.

- don't use rwlocks from irq or softirq context

- use another lock entirely (spinlocks are fair, and often perform better)

You do *not* get to change behavior that has been there since day#1
and that very core code very much depends on.

In fact, the fact that you use a read_lock from softirq context makes
me suspect that you yourself might be in danger of deadlocking due to
that "look, another CPU is trying to get the write lock" deadlock
situation.

Because the only way to fix that deadlock is

(a) unfair rwlocks like we have now

(b) having to disable interrupts around all read-locked regions

and (b) often basically means that the whole point of using a rwlock
goes away, because it is now much more expensive.

Linus