Skip to contentSkip to conversation

locking/rwsem: Fix kernel crash when spinning on RWSEM_OWNER_UNKNOWN

PATCH Superseded Waiman Long 4 participants 8 messages Jan 14, 2020 – Jan 15, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Waiman Long

author reviewer
#0 lore raw

PATCH 1/1 locking/rwsem: Fix kernel crash when spinning on RWSEM_OWNER_UNKNOWN

The commit 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically spin on owner") will allow a recently woken up waiting writer to spin on the owner. Unfortunately, if the owner happens to be RWSEM_OWNER_UNKNOWN, the code will incorrectly spin on it leading to a kernel crash. This is fixed by passing the proper non-spinnable bits to rwsem_spin_on_owner() so that RWSEM_OWNER_UNKNOWN will be treated as a non-spinnable target.

Fixes 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically spin on owner")
---
 kernel/locking/rwsem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/locking/rwsem.c +1 -1
index 44e68761f432..1dd3d53f43c3 100644
@@ -1227,7 +1227,7 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
 		 * without sleeping.
 		 */
 		if ((wstate == WRITER_HANDOFF) &&
-		    (rwsem_spin_on_owner(sem, 0) == OWNER_NULL))
+		    rwsem_spin_on_owner(sem, RWSEM_NONSPINNABLE) == OWNER_NULL)
 			goto trylock_again;
 
 		/* Block until there are no active lockers. */
Signature · 1 line
2.18.1
Tested-by on v1 1/1
#1 lore raw
Quoted 8 lines · Waiman Long

The commit 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically spin on owner") will allow a recently woken up waiting writer to spin on the owner. Unfortunately, if the owner happens to be RWSEM_OWNER_UNKNOWN, the code will incorrectly spin on it leading to a kernel crash. This is fixed by passing the proper non-spinnable bits to rwsem_spin_on_owner() so that RWSEM_OWNER_UNKNOWN will be treated as a non-spinnable target.

Quoted 4 lines
Fixes 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically spin on owner")

This survives all the tests that showed the problems with the original code:

kernel/locking/rwsem.c rwsem_down_write_slowpath(struct rw_semaphore *sem, int state) · line 1230

Quoted 3 lines
kernel/locking/rwsem.c +1 -1
 		if ((wstate == WRITER_HANDOFF) &&
-		    (rwsem_spin_on_owner(sem, 0) == OWNER_NULL))
+		    rwsem_spin_on_owner(sem, RWSEM_NONSPINNABLE) == OWNER_NULL)

Nit: the inner braces in the first half of the conditional aren't required either.

author reviewer
#2 lore raw
Quoted 16 lines · Christoph Hellwig
Quoted 8 lines · Waiman Long

The commit 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically spin on owner") will allow a recently woken up waiting writer to spin on the owner. Unfortunately, if the owner happens to be RWSEM_OWNER_UNKNOWN, the code will incorrectly spin on it leading to a kernel crash. This is fixed by passing the proper non-spinnable bits to rwsem_spin_on_owner() so that RWSEM_OWNER_UNKNOWN will be treated as a non-spinnable target.

Quoted 4 lines
Fixes 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically spin on owner")

This survives all the tests that showed the problems with the original code:

Quoted 7 lines

kernel/locking/rwsem.c rwsem_down_write_slowpath(struct rw_semaphore *sem, int state) · line 1230

Quoted 3 lines
kernel/locking/rwsem.c +1 -1
 		if ((wstate == WRITER_HANDOFF) &&
-		    (rwsem_spin_on_owner(sem, 0) == OWNER_NULL))
+		    rwsem_spin_on_owner(sem, RWSEM_NONSPINNABLE) == OWNER_NULL)

Nit: the inner braces in the first half of the conditional aren't required either.

I typically over-parenthesize the code to make it easier to read as we don't need to think too much about operator precedence to see if it is doing the right thing. I remove the 2nd parentheses to avoid breaking the 80-colnum limit.

Cheers,
Longman

#3 lore raw

From: [email protected] <[email protected]> On Behalf Of Waiman Long

Quoted 1 line

Sent: 15 January 2020 14:27

...

Quoted 9 lines
Quoted 5 lines

kernel/locking/rwsem.c rwsem_down_write_slowpath(struct rw_semaphore *sem, int state) · line 1230

Quoted 3 lines
kernel/locking/rwsem.c +1 -1
 		if ((wstate == WRITER_HANDOFF) &&
-		    (rwsem_spin_on_owner(sem, 0) == OWNER_NULL))
+		    rwsem_spin_on_owner(sem, RWSEM_NONSPINNABLE) == OWNER_NULL)

Nit: the inner braces in the first half of the conditional aren't required either.

I typically over-parenthesize the code to make it easier to read as we don't need to think too much about operator precedence to see if it is doing the right thing.

The problem is it actually makes it harder to read.
It is difficult for the 'mark 1 eyeball' to follow lots of sets of brackets. Since == (etc) are the lowest priority operators (apart from ?:) they never need ().

David

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

author reviewer
#4 lore raw
Quoted 16 lines · David Laight

From: [email protected] <[email protected]> On Behalf Of Waiman Long

Quoted 1 line

Sent: 15 January 2020 14:27

...

Quoted 8 lines
Quoted 5 lines

kernel/locking/rwsem.c rwsem_down_write_slowpath(struct rw_semaphore *sem, int state) · line 1230

Quoted 3 lines
kernel/locking/rwsem.c +1 -1
 		if ((wstate == WRITER_HANDOFF) &&
-		    (rwsem_spin_on_owner(sem, 0) == OWNER_NULL))
+		    rwsem_spin_on_owner(sem, RWSEM_NONSPINNABLE) == OWNER_NULL)

Nit: the inner braces in the first half of the conditional aren't required either.

I typically over-parenthesize the code to make it easier to read as we don't need to think too much about operator precedence to see if it is doing the right thing.

The problem is it actually makes it harder to read.
It is difficult for the 'mark 1 eyeball' to follow lots of sets of brackets. Since == (etc) are the lowest priority operators (apart from ?:) they never need ().

Quoted 6 lines

David

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

It depends. I find it hard to read an expression with "&" and "&&" without parentheses. Anyway, I will admit that the above code is inconsistent in term of how parentheses are used. So I will change that.

Cheers,
Longman

#5 lore raw

From: Waiman Long

Quoted 1 line

Sent: 15 January 2020 15:48

...

Quoted 3 lines

It depends. I find it hard to read an expression with "&" and "&&" without parentheses. Anyway, I will admit that the above code is inconsistent in term of how parentheses are used. So I will change that.

Conditionals containing fragments like (a == b && c == d && ...) are much easier to read without any extra ().

The only problem with && is that when K&R added it to C they didn't change the priority of & to be higher than == (where it should be). At that time they could have changed all the existing code... Modern compilers do warn about (a == b & c).

David

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

author reviewer
#6 lore raw
In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig
Quoted 8 lines · Waiman Long

The commit 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically spin on owner") will allow a recently woken up waiting writer to spin on the owner. Unfortunately, if the owner happens to be RWSEM_OWNER_UNKNOWN, the code will incorrectly spin on it leading to a kernel crash. This is fixed by passing the proper non-spinnable bits to rwsem_spin_on_owner() so that RWSEM_OWNER_UNKNOWN will be treated as a non-spinnable target.

Quoted 9 lines
Quoted 4 lines
Fixes 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically spin on owner")

This survives all the tests that showed the problems with the original code:

Thanks for the testing.

Quoted 5 lines

kernel/locking/rwsem.c rwsem_down_write_slowpath(struct rw_semaphore *sem, int state) · line 1230

Quoted 3 lines
kernel/locking/rwsem.c +1 -1
 		if ((wstate == WRITER_HANDOFF) &&
-		    (rwsem_spin_on_owner(sem, 0) == OWNER_NULL))
+		    rwsem_spin_on_owner(sem, RWSEM_NONSPINNABLE) == OWNER_NULL)

Nit: the inner braces in the first half of the conditional aren't required either.

Yes, it is inconsistent and so is not good. I will post a v2 patch to fix that.

Cheers,
Longman

maintainer
#7 lore raw
In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Waiman Long

The commit 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically spin on owner") will allow a recently woken up waiting writer to spin on the owner. Unfortunately, if the owner happens to be RWSEM_OWNER_UNKNOWN, the code will incorrectly spin on it leading to a kernel crash. This is fixed by passing the proper non-spinnable bits to rwsem_spin_on_owner() so that RWSEM_OWNER_UNKNOWN will be treated as a non-spinnable target.

Fixes 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically spin on owner")
Quoted 7 lines
Quoted 2 lines

This survives all the tests that showed the problems with the original code:

Thanks!