locking/rwbase: Assorted fixes
Subsystems (2)
Peter Zijlstra
maintainercover locking/rwbase: Assorted fixes
Hai, these here patches are what I've got based on Boqun's review. The plan is to stuff in them in locking/urgent somewhere tomorrow.
Please holler if you see anything that ought to make me reconsider this :-)
Peter Zijlstra
maintainerPATCH 1/4 sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
While looking at current_save_and_set_rtlock_wait_state() I'm thinking it really ought to use smp_store_mb(), because something like:
current_save_and_set_rtlock_wait_state();
for (;;) {
if (try_lock())
break;
raw_spin_unlock_irq(&lock->wait_lock);
schedule();
raw_spin_lock_irq(&lock->wait_lock);
set_current_state(TASK_RTLOCK_WAIT);
}
current_restore_rtlock_saved_state();which is the advertised usage in the comment, is actually broken, since trylock() will only need a load-acquire in general and that could be re-ordered against the state store, which could lead to a missed wakeup -> BAD (tm).
While there, make them consistent with the IRQ usage in set_special_state().
--- include/linux/sched.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
include/linux/sched.h +11 -8
@@ -245,7 +245,8 @@ struct task_group;* if (try_lock()) * break; * raw_spin_unlock_irq(&lock->wait_lock); -* schedule_rtlock(); +* if (!cond) +* schedule_rtlock(); * raw_spin_lock_irq(&lock->wait_lock); * set_current_state(TASK_RTLOCK_WAIT); * }
@@ -253,22 +254,24 @@ struct task_group;*/ #define current_save_and_set_rtlock_wait_state () \ do { \ - lockdep_assert_irqs_disabled(); \ - raw_spin_lock(¤t->pi_lock); \ + unsigned long flags;/* may shadow */ \ + \ +raw_spin_lock_irqsave (¤t->pi_lock, flags); \ current->saved_state = current->__state; \debug_rtlock_wait_set_state (); \ - WRITE_ONCE(current->__state, TASK_RTLOCK_WAIT); \ - raw_spin_unlock(¤t->pi_lock); \ +smp_store_mb (current->__state,TASK_RTLOCK_WAIT ); \ +raw_spin_unlock_irqrestore (¤t->pi_lock, flags); \ }while (0 );#define current_restore_rtlock_saved_state () \ do { \ - lockdep_assert_irqs_disabled(); \ - raw_spin_lock(¤t->pi_lock); \ + unsigned long flags;/* may shadow */ \ + \ +raw_spin_lock_irqsave (¤t->pi_lock, flags); \debug_rtlock_wait_restore_state (); \WRITE_ONCE (current->__state, current->saved_state); \ current->saved_state =TASK_RUNNING ; \ - raw_spin_unlock(¤t->pi_lock); \ +raw_spin_unlock_irqrestore (¤t->pi_lock, flags); \ } while (0);#define get_current_state () READ_ONCE(current->__state)
Will Deacon
maintainerRe: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
Quoted 16 lines · Peter Zijlstra
While looking at
current_save_and_set_rtlock_wait_state()I'm thinking it really ought to usesmp_store_mb(), because something like:current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();
Quoted 4 lines
which is the advertised usage in the comment, is actually broken, since
trylock()will only need a load-acquire in general and that could be re-ordered against the state store, which could lead to a missed wakeup -> BAD (tm).
Why doesn't the UNLOCK of pi_lock in current_save_and_set_rtlock_wait_state() order the state change before the successful try_lock? I'm just struggling to envisage how this actually goes wrong.
Will
Peter Zijlstra
maintainerRe: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
Quoted 17 lines · Will Deacon
Quoted 16 lines · Peter Zijlstra
While looking at
current_save_and_set_rtlock_wait_state()I'm thinking it really ought to usesmp_store_mb(), because something like:current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();
Quoted 8 lines
Quoted 4 lines
which is the advertised usage in the comment, is actually broken, since
trylock()will only need a load-acquire in general and that could be re-ordered against the state store, which could lead to a missed wakeup -> BAD (tm).Why doesn't the UNLOCK of
pi_lockincurrent_save_and_set_rtlock_wait_state()order the state change before the successfultry_lock? I'm just struggling to envisage how this actually goes wrong.
Moo yes, so the earlier changelog I wrote was something like:
current_save_and_set_rtlock_wait_state();
for (;;) {
if (try_lock())
break;
raw_spin_unlock_irq(&lock->wait_lock);
if (!cond)
schedule();
raw_spin_lock_irq(&lock->wait_lock);
set_current_state(TASK_RTLOCK_WAIT);
}
current_restore_rtlock_saved_state();which is more what the code looks like before these patches, and in that case the @cond load can be lifted before __state.
It all sorta works in the current application because most things are serialized by ->wait_lock, but given the 'normal' wait pattern I got highly suspicious of there not being a full barrier around.
Will Deacon
maintainerRe: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
Quoted 43 lines · Peter Zijlstra
Quoted 17 lines · Will Deacon
Quoted 16 lines · Peter Zijlstra
While looking at
current_save_and_set_rtlock_wait_state()I'm thinking it really ought to usesmp_store_mb(), because something like:current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();Quoted 8 lines
Quoted 4 lines
which is the advertised usage in the comment, is actually broken, since
trylock()will only need a load-acquire in general and that could be re-ordered against the state store, which could lead to a missed wakeup -> BAD (tm).Why doesn't the UNLOCK of
pi_lockincurrent_save_and_set_rtlock_wait_state()order the state change before the successfultry_lock? I'm just struggling to envisage how this actually goes wrong.Moo yes, so the earlier changelog I wrote was something like:
current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); if (!cond) schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();
Quoted 2 lines
which is more what the code looks like before these patches, and in that case the @cond load can be lifted before
__state.
Ah, so that makes more sense, thanks. I can't see how the try_lock() could be reordered though, as it's going to have to do an atomic rmw.
Will
Peter Zijlstra
maintainerRe: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
Quoted 17 lines · Will Deacon
Quoted 16 lines · Peter Zijlstra
Moo yes, so the earlier changelog I wrote was something like:
current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); if (!cond) schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();
Quoted 5 lines
Quoted 2 lines
which is more what the code looks like before these patches, and in that case the @cond load can be lifted before
__state.Ah, so that makes more sense, thanks. I can't see how the
try_lock()could be reordered though, as it's going to have to do an atomic rmw.
OK, lemme go update the Changelog and make it __flags for bigeasy :-)
Peter Zijlstra
maintainerRe: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
Quoted 18 lines · Peter Zijlstra
Quoted 17 lines · Will Deacon
Quoted 16 lines · Peter Zijlstra
Moo yes, so the earlier changelog I wrote was something like:
current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); if (!cond) schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
which is more what the code looks like before these patches, and in that case the @cond load can be lifted before
__state.Ah, so that makes more sense, thanks. I can't see how the
try_lock()could be reordered though, as it's going to have to do an atomic rmw.OK, lemme go update the Changelog and make it
__flagsfor bigeasy :-)
The patch now reads:
---
Subject: sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
From: Peter Zijlstra <[email protected]>
Date: Thu, 09 Sep 2021 12:59:16 +0200
While looking at current_save_and_set_rtlock_wait_state() I'm thinking it really ought to use smp_store_mb(), because using it for a more traditional wait loop like:
current_save_and_set_rtlock_wait_state();
for (;;) {
if (cond)
schedule();
set_current_state(TASK_RTLOCK_WAIT);
}
current_restore_rtlock_saved_state();is actually broken, since the cond load could be re-ordered against the state store, which could lead to a missed wakeup -> BAD (tm).
While there, make them consistent with the IRQ usage in set_special_state().
--- include/linux/sched.h | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
include/linux/sched.h +12 -9
@@ -218,7 +218,7 @@ struct task_group;*/ #define set_special_state (state_value) \ do { \ - unsigned long flags;/* may shadow */ \ + unsigned long __flags;/* may shadow */ \ \raw_spin_lock_irqsave (¤t->pi_lock, flags); \debug_special_state_change ((state_value)); \
@@ -245,7 +245,8 @@ struct task_group;* if (try_lock()) * break; * raw_spin_unlock_irq(&lock->wait_lock); -* schedule_rtlock(); +* if (!cond) +* schedule_rtlock(); * raw_spin_lock_irq(&lock->wait_lock); * set_current_state(TASK_RTLOCK_WAIT); * }
@@ -253,22 +254,24 @@ struct task_group;*/ #define current_save_and_set_rtlock_wait_state () \ do { \ - lockdep_assert_irqs_disabled(); \ - raw_spin_lock(¤t->pi_lock); \ + unsigned long __flags;/* may shadow */ \ + \ +raw_spin_lock_irqsave (¤t->pi_lock, flags); \ current->saved_state = current->__state; \debug_rtlock_wait_set_state (); \ - WRITE_ONCE(current->__state, TASK_RTLOCK_WAIT); \ - raw_spin_unlock(¤t->pi_lock); \ +smp_store_mb (current->__state,TASK_RTLOCK_WAIT ); \ +raw_spin_unlock_irqrestore (¤t->pi_lock, flags); \ }while (0 );#define current_restore_rtlock_saved_state () \ do { \ - lockdep_assert_irqs_disabled(); \ - raw_spin_lock(¤t->pi_lock); \ + unsigned long __flags;/* may shadow */ \ + \ +raw_spin_lock_irqsave (¤t->pi_lock, flags); \debug_rtlock_wait_restore_state (); \WRITE_ONCE (current->__state, current->saved_state); \ current->saved_state =TASK_RUNNING ; \ - raw_spin_unlock(¤t->pi_lock); \ +raw_spin_unlock_irqrestore (¤t->pi_lock, flags); \ } while (0);#define get_current_state () READ_ONCE(current->__state)
Re: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
Quoted 47 lines · Peter Zijlstra
Quoted 18 lines · Peter Zijlstra
Quoted 17 lines · Will Deacon
Quoted 16 lines · Peter Zijlstra
Moo yes, so the earlier changelog I wrote was something like:
current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); if (!cond) schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
which is more what the code looks like before these patches, and in that case the @cond load can be lifted before
__state.Ah, so that makes more sense, thanks. I can't see how the
try_lock()could be reordered though, as it's going to have to do an atomic rmw.OK, lemme go update the Changelog and make it
__flagsfor bigeasy :-)The patch now reads:
---
Subject: sched/wakeup: Strengthencurrent_save_and_set_rtlock_wait_state()
From: Peter Zijlstra <[email protected]>
Date: Thu, 09 Sep 2021 12:59:16 +0200While looking at
current_save_and_set_rtlock_wait_state()I'm thinking it really ought to usesmp_store_mb(), because using it for a more traditional wait loop like:current_save_and_set_rtlock_wait_state(); for (;;) { if (cond) schedule(); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();
Quoted 5 lines
is actually broken, since the cond load could be re-ordered against the state store, which could lead to a missed wakeup -> BAD (tm).
While there, make them consistent with the IRQ usage in
set_special_state().
Cheers, that's much better:
Will
Re: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
In reply to Peter ZijlstraQuoted 60 lines · Peter Zijlstra
Quoted 18 lines · Peter Zijlstra
Quoted 17 lines · Will Deacon
Quoted 16 lines · Peter Zijlstra
Moo yes, so the earlier changelog I wrote was something like:
current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); if (!cond) schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
which is more what the code looks like before these patches, and in that case the @cond load can be lifted before
__state.Ah, so that makes more sense, thanks. I can't see how the
try_lock()could be reordered though, as it's going to have to do an atomic rmw.OK, lemme go update the Changelog and make it
__flagsfor bigeasy :-)The patch now reads:
---
Subject: sched/wakeup: Strengthencurrent_save_and_set_rtlock_wait_state()
From: Peter Zijlstra <[email protected]>
Date: Thu, 09 Sep 2021 12:59:16 +0200While looking at
current_save_and_set_rtlock_wait_state()I'm thinking it really ought to usesmp_store_mb(), because using it for a more traditional wait loop like:current_save_and_set_rtlock_wait_state(); for (;;) { if (cond) schedule(); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();is actually broken, since the cond load could be re-ordered against the state store, which could lead to a missed wakeup -> BAD (tm).
While there, make them consistent with the IRQ usage in
set_special_state().Fixes 5f220be21418 ("sched/wakeup: Prepare for RT sleeping spin/rwlocks")Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- include/linux/sched.h | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
include/linux/sched.h struct task_group; · line 221
Quoted 5 lines
include/linux/sched.h +1
-1@@ -218,7 +218,7 @@ struct task_group;*/ #define set_special_state (state_value) \ do { \ - unsigned long flags;/* may shadow */ \ + unsigned long __flags;/* may shadow */ \
Do you still need the "may shadow" comment?
Cheers,
Longman
Peter Zijlstra
maintainerRe: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
Quoted 6 lines · Waiman Long
include/linux/sched.h struct task_group; · line 221
Quoted 4 lines
#define set_special_state(state_value) \ do { \include/linux/sched.h +1
-1-unsigned long flags;/* may shadow */ \ +unsigned long __flags;/* may shadow */ \Do you still need the "may shadow" comment?
Strictly speaking yes, there _could_ be a local variable called __flags, however unlikely.
At some point someone was going around and doing dodgy patches based on compiler warnings about variables being shadowed, this it to preempt anybody trying to 'fix' this (again).
Re: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
In reply to Peter ZijlstraQuoted 43 lines · Peter Zijlstra
Quoted 17 lines · Will Deacon
Quoted 16 lines · Peter Zijlstra
While looking at
current_save_and_set_rtlock_wait_state()I'm thinking it really ought to usesmp_store_mb(), because something like:current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();Quoted 8 lines
Quoted 4 lines
which is the advertised usage in the comment, is actually broken, since
trylock()will only need a load-acquire in general and that could be re-ordered against the state store, which could lead to a missed wakeup -> BAD (tm).Why doesn't the UNLOCK of
pi_lockincurrent_save_and_set_rtlock_wait_state()order the state change before the successfultry_lock? I'm just struggling to envisage how this actually goes wrong.Moo yes, so the earlier changelog I wrote was something like:
current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); if (!cond) schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();
Quoted 6 lines
which is more what the code looks like before these patches, and in that case the @cond load can be lifted before
__state.It all sorta works in the current application because most things are serialized by ->wait_lock, but given the 'normal' wait pattern I got highly suspicious of there not being a full barrier around.
Hmm.. I think ->pi_lock actually protects us here. IIUC, a mising wake-up would happen if try_to_wake_up() failed to observe the __state change by the about-to-wait task, and the about-to-wait task didn't observe the condition set by the waker task, for example:
TASK 0 TASK 1 ====== ====== cond = 1; ... try_to_wake_up(t0, TASK_RTLOCK_WAIT, ..): ttwu_state_match(...) if (t0->__state & TASK_RTLOCK_WAIT) // false .. return false; // don't wake up ... current->__state = TASK_RTLOCK_WAIT ... if (!cond) // !cond is true because of memory reordering schedule(); // sleep, and may not be waken up again.
But let's add ->pi_lock critical sections into the example:
TASK 0 TASK 1 ====== ====== cond = 1; ... try_to_wake_up(t0, TASK_RTLOCK_WAIT, ..): raw_spin_lock_irqsave(->pi_lock,...); ttwu_state_match(...) if (t0->__state & TASK_RTLOCK_WAIT) // false .. return false; // don't wake up raw_spin_unlock_irqrestore(->pi_lock,...); // A ... raw_spin_lock_irqsave(->pi_lock, ...); // B current->__state = TASK_RTLOCK_WAIT raw_spin_unlock_irqrestore(->pi_lock, ...); if (!cond) schedule();
Now the read of cond on TASK0 must observe the store of cond on TASK1, because accesses to __state is serialized by ->pi_lock, so if TASK1's read to __state didn't observe the write of TASK0 to __state, then the lock B must read from the unlock A (or another unlock co-after A), then we have a release-acquire pair to guarantee that the read of cond on TASK0 sees the write of cond on TASK1. Simplify this by a litmus test below:
C unlock-lock
{
}
P0(spinlock_t *s, int *cond, int *state)
{
int r1;
spin_lock(s);
WRITE_ONCE(*state, 1);
spin_unlock(s);
r1 = READ_ONCE(*cond);
}
P1(spinlock_t *s, int *cond, int *state)
{
int r1;
WRITE_ONCE(*cond, 1);
spin_lock(s);
r1 = READ_ONCE(*state);
spin_unlock(s);
}
exists (0:r1=0 /\ 1:r1=0)and result is:
Test unlock-lock Allowed States 3 0:r1=0; 1:r1=1; 0:r1=1; 1:r1=0; 0:r1=1; 1:r1=1; No Witnesses Positive: 0 Negative: 3 Condition exists (0:r1=0 /\ 1:r1=0) Observation unlock-lock Never 0 3 Time unlock-lock 0.01 Hash=e1f914505f07e380405f65d3b0fb6940
In short, since we write to the __state with ->pi_lock held, I don't think we need to smp_store_mb() for __state. But maybe I'm missing something subtle here ;-)
Regards,
Boqun
Re: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
In reply to Peter Zijlstrainclude/linux/sched.h struct task_group; · line 257
Quoted 9 lines · Peter Zijlstra
include/linux/sched.h +1
-2@@ -253,22 +254,24 @@ struct task_group;*/ #define current_save_and_set_rtlock_wait_state () \ do { \ - lockdep_assert_irqs_disabled(); \ - raw_spin_lock(¤t->pi_lock); \ + unsigned long flags;/* may shadow */ \
could we haz __flags so no shadow?
include/linux/sched.h struct task_group; · line 263
Quoted 8 lines
include/linux/sched.h +4
-2+ \ +raw_spin_lock_irqsave (¤t->pi_lock, flags); \ current->saved_state = current->__state; \debug_rtlock_wait_set_state (); \ -WRITE_ONCE (current->__state,TASK_RTLOCK_WAIT ); \ -raw_spin_unlock (¤t->pi_lock); \ +smp_store_mb (current->__state,TASK_RTLOCK_WAIT ); \ +raw_spin_unlock_irqrestore (¤t->pi_lock, flags); \
Sebastian
Re: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
In reply to Peter ZijlstraQuoted 16 lines · Peter Zijlstra
While looking at
current_save_and_set_rtlock_wait_state()I'm thinking it really ought to usesmp_store_mb(), because something like:current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();
Quoted 4 lines
which is the advertised usage in the comment, is actually broken, since
trylock()will only need a load-acquire in general and that could be re-ordered against the state store, which could lead to a missed wakeup -> BAD (tm).
I don't think so because both the state store and the wakeup are serialized via tsk->pi_lock.
Quoted 12 lines
While there, make them consistent with the IRQ usage in
set_special_state().Fixes 5f220be21418 ("sched/wakeup: Prepare for RT sleeping spin/rwlocks")Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- include/linux/sched.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
include/linux/sched.h struct task_group; · line 249
Quoted 6 lines
include/linux/sched.h +2
-1@@ -245,7 +245,8 @@ struct task_group;* if (try_lock()) * break; * raw_spin_unlock_irq(&lock->wait_lock); -* schedule_rtlock(); +* if (!cond) +* schedule_rtlock();
cond is not really relevant here.
include/linux/sched.h struct task_group; · line 259
Quoted 12 lines
include/linux/sched.h +3
-2* raw_spin_lock_irq(&lock->wait_lock); * set_current_state(TASK_RTLOCK_WAIT); * } @@ -253,22 +254,24 @@ struct task_group;*/ #define current_save_and_set_rtlock_wait_state () \ do { \ - lockdep_assert_irqs_disabled(); \ - raw_spin_lock(¤t->pi_lock); \ + unsigned long flags;/* may shadow */ \ + \ +raw_spin_lock_irqsave (¤t->pi_lock, flags); \
Why? This is solely for the rtlock use case which invokes this with interrupts disabled. So why do we need that irqsave() overhead here?
include/linux/sched.h struct task_group; · line 262
Quoted 5 lines
include/linux/sched.h +1
-2current->saved_state = current->__state; \debug_rtlock_wait_set_state (); \ -WRITE_ONCE (current->__state,TASK_RTLOCK_WAIT ); \ -raw_spin_unlock (¤t->pi_lock); \ +smp_store_mb (current->__state,TASK_RTLOCK_WAIT ); \
The try_lock() does not matter at all here, really. All what matters is that the unlocker cannot observe the wrong state and that's fully serialized via tsk::pi_lock.
Thanks,
tglx
Re: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
Quoted 62 lines · Thomas Gleixner
Quoted 16 lines · Peter Zijlstra
While looking at
current_save_and_set_rtlock_wait_state()I'm thinking it really ought to usesmp_store_mb(), because something like:current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state();Quoted 4 lines
which is the advertised usage in the comment, is actually broken, since
trylock()will only need a load-acquire in general and that could be re-ordered against the state store, which could lead to a missed wakeup -> BAD (tm).I don't think so because both the state store and the wakeup are serialized via
tsk->pi_lock.Quoted 12 lines
While there, make them consistent with the IRQ usage in
set_special_state().Fixes 5f220be21418 ("sched/wakeup: Prepare for RT sleeping spin/rwlocks")Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- include/linux/sched.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)include/linux/sched.h struct task_group; · line 249
Quoted 6 lines
include/linux/sched.h +2
-1@@ -245,7 +245,8 @@ struct task_group;* if (try_lock()) * break; * raw_spin_unlock_irq(&lock->wait_lock); -* schedule_rtlock(); +* if (!cond) +* schedule_rtlock(); cond is not really relevant here.
include/linux/sched.h struct task_group; · line 259
Quoted 12 lines
include/linux/sched.h +3
-2* raw_spin_lock_irq(&lock->wait_lock); * set_current_state(TASK_RTLOCK_WAIT); * } @@ -253,22 +254,24 @@ struct task_group;*/ #define current_save_and_set_rtlock_wait_state () \ do { \ - lockdep_assert_irqs_disabled(); \ - raw_spin_lock(¤t->pi_lock); \ + unsigned long flags;/* may shadow */ \ + \ +raw_spin_lock_irqsave (¤t->pi_lock, flags); \Why? This is solely for the rtlock use case which invokes this with interrupts disabled. So why do we need that
irqsave()overhead here?
Quoted 9 lines
include/linux/sched.h struct task_group; · line 262
Quoted 5 lines
include/linux/sched.h +1
-2current->saved_state = current->__state; \debug_rtlock_wait_set_state (); \ -WRITE_ONCE (current->__state,TASK_RTLOCK_WAIT ); \ -raw_spin_unlock (¤t->pi_lock); \ +smp_store_mb (current->__state,TASK_RTLOCK_WAIT ); \The
try_lock()does not matter at all here, really. All what matters is that the unlocker cannot observe the wrong state and that's fully serialized viatsk::pi_lock.
If your reasoning would be correct, then set_special_state() would be broken as well.
Thanks,
tglx
Peter Zijlstra
maintainerRe: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
Quoted 4 lines · Thomas Gleixner
On Tue, Sep 14 2021 at 00:08, Thomas Gleixner wrote:
If your reasoning would be correct, then
set_special_state()would be broken as well.
I've dropped the patch.
Peter Zijlstra
maintainerPATCH 2/4 locking/rwbase: Properly match set_and_save_state() to restore_state()
In reply to Peter ZijlstraNoticed while looking at the readers race.
--- kernel/locking/rwbase_rt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/locking/rwbase_rt.c +1 -1
@@ -220,7 +220,7 @@ static int __sched rwbase_write_lock(strfor (;atomic_read (&rwb->readers);) {/* Optimized out for rwlocks */ if (rwbase_signal_pending_state (state, current)) { -__set_current_state (TASK_RUNNING ); +rwbase_restore_current_state ();__rwbase_write_unlock (rwb,0 , flags);return -EINTR ; }
Re: [PATCH 2/4] locking/rwbase: Properly match set_and_save_state() to restore_state()
Quoted 10 lines · Peter Zijlstra
Noticed while looking at the readers race.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- kernel/locking/rwbase_rt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/locking/rwbase_rt.c static int __sched rwbase_write_lock(str · line 225
Quoted 7 lines
kernel/locking/rwbase_rt.c +1
-1@@ -220,7 +220,7 @@ static int __sched rwbase_write_lock(strfor (;atomic_read (&rwb->readers);) {/* Optimized out for rwlocks */ if (rwbase_signal_pending_state (state, current)) { -__set_current_state (TASK_RUNNING ); +rwbase_restore_current_state ();__rwbase_write_unlock (rwb,0 , flags);return -EINTR ;
Will
Re: [PATCH 2/4] locking/rwbase: Properly match set_and_save_state() to restore_state()
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
Noticed while looking at the readers race.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- kernel/locking/rwbase_rt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/locking/rwbase_rt.c static int __sched rwbase_write_lock(str · line 223
Quoted 5 lines
kernel/locking/rwbase_rt.c +1
-1@@ -220,7 +220,7 @@ static int __sched rwbase_write_lock(strfor (;atomic_read (&rwb->readers);) {/* Optimized out for rwlocks */ if (rwbase_signal_pending_state (state, current)) { -__set_current_state (TASK_RUNNING ); +rwbase_restore_current_state ();
Right, that's functionally equivalent and makes the code more consistent.
[tip: locking/urgent] locking/rwbase: Properly match set_and_save_state() to restore_state()
In reply to Peter ZijlstraThe following commit has been merged into the locking/urgent branch of tip:
The following commit has been merged into the locking/urgent branch of tip:
Commit-ID: 7687201e37fabf2b7cf2b828f7ca46bf30e2948f Gitweb: https://git.kernel.org/tip/7687201e37fabf2b7cf2b828f7ca46bf30e2948f Author: Peter Zijlstra <[email protected]> AuthorDate: Thu, 09 Sep 2021 12:59:17 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 15 Sep 2021 17:49:15 +02:00
locking/rwbase: Properly match set_and_save_state() to restore_state()
Noticed while looking at the readers race.
--- kernel/locking/rwbase_rt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/locking/rwbase_rt.c +1 -1
@@ -220,7 +220,7 @@ static int __sched rwbase_write_lock(struct rwbase_rt *rwb,for (;atomic_read (&rwb->readers);) {/* Optimized out for rwlocks */ if (rwbase_signal_pending_state (state, current)) { -__set_current_state (TASK_RUNNING ); +rwbase_restore_current_state ();__rwbase_write_unlock (rwb,0 , flags);return -EINTR ; }
Peter Zijlstra
maintainerPATCH 3/4 locking/rwbase: Fix rwbase_write_lock() vs __rwbase_read_lock()
In reply to Peter ZijlstraBoqun noticed that the write-trylock sequence of load+set is broken in rwbase_write_lock()'s wait-loop since they're not both under the same wait_lock instance.
Restructure the code to make this more obvious and correct.
--- kernel/locking/rwbase_rt.c | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-)
kernel/locking/rwbase_rt.c +26 -18
@@ -196,6 +196,19 @@ static inline void rwbase_write_downgrad__rwbase_write_unlock (rwb,WRITER_BIAS -1 , flags); } +static inline bool __rwbase_write_trylock (struct rwbase_rt *rwb) +{ +/* Can do without CAS because we're serialized by wait_lock. */ +lockdep_assert_held (&rwb->rtmutex.wait_lock); + +if (!atomic_read (&rwb->readers)) { +atomic_set (&rwb->readers,WRITER_BIAS ); +return 1 ; + } + +return 0 ; +} + static int __sched rwbase_write_lock(struct rwbase_rt *rwb,unsigned int state) {
@@ -210,34 +223,30 @@ static int __sched rwbase_write_lock(stratomic_sub (READER_BIAS , &rwb->readers);raw_spin_lock_irqsave (&rtm->wait_lock, flags); -/* -* set_current_state() for rw_semaphore -* current_save_and_set_rtlock_wait_state() for rwlock -*/ -rwbase_set_and_save_current_state (state); +if (__rwbase_write_trylock (rwb)) + goto out_unlock; -/* Block until all readers have left the critical section. */ -for (;atomic_read (&rwb->readers);) { +rwbase_set_and_save_current_state (state); +for (;;) {/* Optimized out for rwlocks */ if (rwbase_signal_pending_state (state, current)) {rwbase_restore_current_state ();__rwbase_write_unlock (rwb,0 , flags);return -EINTR ; } + +if (__rwbase_write_trylock (rwb)) +break ; +raw_spin_unlock_irqrestore (&rtm->wait_lock, flags); +rwbase_schedule (); +raw_spin_lock_irqsave (&rtm->wait_lock, flags); -/* -* Schedule and wait for the readers to leave the critical -* section. The last reader leaving it wakes the waiter. -*/ -if (atomic_read (&rwb->readers) !=0 ) -rwbase_schedule ();set_current_state (state); -raw_spin_lock_irqsave (&rtm->wait_lock, flags); } - -atomic_set (&rwb->readers,WRITER_BIAS );rwbase_restore_current_state (); + +out_unlock:raw_spin_unlock_irqrestore (&rtm->wait_lock, flags);return 0 ; }
@@ -253,8 +262,7 @@ static inline int rwbase_write_trylock(satomic_sub (READER_BIAS , &rwb->readers);raw_spin_lock_irqsave (&rtm->wait_lock, flags); -if (!atomic_read (&rwb->readers)) { -atomic_set (&rwb->readers,WRITER_BIAS ); +if (__rwbase_write_trylock (rwb)) {raw_spin_unlock_irqrestore (&rtm->wait_lock, flags);return 1 ; }
Re: [PATCH 3/4] locking/rwbase: Fix rwbase_write_lock() vs __rwbase_read_lock()
Quoted 3 lines · Peter Zijlstra
Boqun noticed that the write-trylock sequence of load+set is broken in rwbase_write_lock()'s wait-loop since they're not both under the same
wait_lockinstance.
Confused.
lock(); A
for (; atomic_read(readers);) {
...
unlock();
..
lock(); B
}
atomic_set();unlock(); A or B
The read/set is always in the same lock instance.
Quoted 1 line
Restructure the code to make this more obvious and correct.
I agree that it's easier to read, but I disagree that it makes the code more correct.
Thanks,
tglx
Peter Zijlstra
maintainerRe: [PATCH 3/4] locking/rwbase: Fix rwbase_write_lock() vs __rwbase_read_lock()
Quoted 17 lines · Thomas Gleixner
Quoted 3 lines · Peter Zijlstra
Boqun noticed that the write-trylock sequence of load+set is broken in rwbase_write_lock()'s wait-loop since they're not both under the same
wait_lockinstance.Confused.
lock(); Afor (; atomic_read(readers);) { ... unlock(); .. lock(); B }
Quoted 4 lines
atomic_set();unlock(); A or BThe read/set is always in the same lock instance.
I really did make a mess of things didn't I :-/ It was some intermediate state that was broken.
How's this then?
---
Subject: locking/rwbase: Extract __rwbase_write_trylock()
From: Peter Zijlstra <[email protected]>
Date: Thu, 09 Sep 2021 12:59:18 +0200
The code in rwbase_write_lock() is a little non-obvious vs the read+set 'trylock', extract the sequence into a helper function to clarify the code.
This also provides a single site to fix fast-path ordering.
--- kernel/locking/rwbase_rt.c | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-)
kernel/locking/rwbase_rt.c +26 -18
@@ -196,6 +196,19 @@ static inline void rwbase_write_downgrad__rwbase_write_unlock (rwb,WRITER_BIAS -1 , flags); } +static inline bool __rwbase_write_trylock (struct rwbase_rt *rwb) +{ +/* Can do without CAS because we're serialized by wait_lock. */ +lockdep_assert_held (&rwb->rtmutex.wait_lock); + +if (!atomic_read (&rwb->readers)) { +atomic_set (&rwb->readers,WRITER_BIAS ); +return 1 ; + } + +return 0 ; +} + static int __sched rwbase_write_lock(struct rwbase_rt *rwb,unsigned int state) {
@@ -210,34 +223,30 @@ static int __sched rwbase_write_lock(stratomic_sub (READER_BIAS , &rwb->readers);raw_spin_lock_irqsave (&rtm->wait_lock, flags); -/* -* set_current_state() for rw_semaphore -* current_save_and_set_rtlock_wait_state() for rwlock -*/ -rwbase_set_and_save_current_state (state); +if (__rwbase_write_trylock (rwb)) + goto out_unlock; -/* Block until all readers have left the critical section. */ -for (;atomic_read (&rwb->readers);) { +rwbase_set_and_save_current_state (state); +for (;;) {/* Optimized out for rwlocks */ if (rwbase_signal_pending_state (state, current)) {rwbase_restore_current_state ();__rwbase_write_unlock (rwb,0 , flags);return -EINTR ; } + +if (__rwbase_write_trylock (rwb)) +break ; +raw_spin_unlock_irqrestore (&rtm->wait_lock, flags); +rwbase_schedule (); +raw_spin_lock_irqsave (&rtm->wait_lock, flags); -/* -* Schedule and wait for the readers to leave the critical -* section. The last reader leaving it wakes the waiter. -*/ -if (atomic_read (&rwb->readers) !=0 ) -rwbase_schedule ();set_current_state (state); -raw_spin_lock_irqsave (&rtm->wait_lock, flags); } - -atomic_set (&rwb->readers,WRITER_BIAS );rwbase_restore_current_state (); + +out_unlock:raw_spin_unlock_irqrestore (&rtm->wait_lock, flags);return 0 ; }
@@ -253,8 +262,7 @@ static inline int rwbase_write_trylock(satomic_sub (READER_BIAS , &rwb->readers);raw_spin_lock_irqsave (&rtm->wait_lock, flags); -if (!atomic_read (&rwb->readers)) { -atomic_set (&rwb->readers,WRITER_BIAS ); +if (__rwbase_write_trylock (rwb)) {raw_spin_unlock_irqrestore (&rtm->wait_lock, flags);return 1 ; }
Thomas Gleixner
Reviewed-byRe: [PATCH 3/4] locking/rwbase: Fix rwbase_write_lock() vs __rwbase_read_lock()
Quoted 5 lines · Peter Zijlstra
Quoted 1 line · Thomas Gleixner
The read/set is always in the same lock instance.
I really did make a mess of things didn't I :-/ It was some intermediate state that was broken.
Thinking about memory ordering can reorder your memory :)
Quoted 7 lines
How's this then?
---
Subject: locking/rwbase: Extract__rwbase_write_trylock()
From: Peter Zijlstra <[email protected]>
Date: Thu, 09 Sep 2021 12:59:18 +0200
Quoted 7 lines
The code in
rwbase_write_lock()is a little non-obvious vs the read+set 'trylock', extract the sequence into a helper function to clarify the code.This also provides a single site to fix fast-path ordering.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>
[tip: locking/urgent] locking/rwbase: Extract __rwbase_write_trylock()
In reply to Peter ZijlstraThe following commit has been merged into the locking/urgent branch of tip:
The following commit has been merged into the locking/urgent branch of tip:
Commit-ID: 616be87eac9fa2ab2dca1069712f7236e50f3bf6 Gitweb: https://git.kernel.org/tip/616be87eac9fa2ab2dca1069712f7236e50f3bf6 Author: Peter Zijlstra <[email protected]> AuthorDate: Thu, 09 Sep 2021 12:59:18 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 15 Sep 2021 17:49:15 +02:00
locking/rwbase: Extract __rwbase_write_trylock()
The code in rwbase_write_lock() is a little non-obvious vs the read+set 'trylock', extract the sequence into a helper function to clarify the code.
This also provides a single site to fix fast-path ordering.
--- kernel/locking/rwbase_rt.c | 44 +++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-)
kernel/locking/rwbase_rt.c +26 -18
@@ -196,6 +196,19 @@ static inline void rwbase_write_downgrade(struct rwbase_rt *rwb)__rwbase_write_unlock (rwb,WRITER_BIAS -1 , flags); } +static inline bool __rwbase_write_trylock (struct rwbase_rt *rwb) +{ +/* Can do without CAS because we're serialized by wait_lock. */ +lockdep_assert_held (&rwb->rtmutex.wait_lock); + +if (!atomic_read (&rwb->readers)) { +atomic_set (&rwb->readers,WRITER_BIAS ); +return 1 ; + } + +return 0 ; +} + static int __sched rwbase_write_lock(struct rwbase_rt *rwb,unsigned int state) {
@@ -210,34 +223,30 @@ static int __sched rwbase_write_lock(struct rwbase_rt *rwb,atomic_sub (READER_BIAS , &rwb->readers);raw_spin_lock_irqsave (&rtm->wait_lock, flags); -/* -* set_current_state() for rw_semaphore -* current_save_and_set_rtlock_wait_state() for rwlock -*/ -rwbase_set_and_save_current_state (state); +if (__rwbase_write_trylock (rwb)) + goto out_unlock; -/* Block until all readers have left the critical section. */ -for (;atomic_read (&rwb->readers);) { +rwbase_set_and_save_current_state (state); +for (;;) {/* Optimized out for rwlocks */ if (rwbase_signal_pending_state (state, current)) {rwbase_restore_current_state ();__rwbase_write_unlock (rwb,0 , flags);return -EINTR ; } + +if (__rwbase_write_trylock (rwb)) +break ; +raw_spin_unlock_irqrestore (&rtm->wait_lock, flags); +rwbase_schedule (); +raw_spin_lock_irqsave (&rtm->wait_lock, flags); -/* -* Schedule and wait for the readers to leave the critical -* section. The last reader leaving it wakes the waiter. -*/ -if (atomic_read (&rwb->readers) !=0 ) -rwbase_schedule ();set_current_state (state); -raw_spin_lock_irqsave (&rtm->wait_lock, flags); } - -atomic_set (&rwb->readers,WRITER_BIAS );rwbase_restore_current_state (); + +out_unlock:raw_spin_unlock_irqrestore (&rtm->wait_lock, flags);return 0 ; }
@@ -253,8 +262,7 @@ static inline int rwbase_write_trylock(struct rwbase_rt *rwb)atomic_sub (READER_BIAS , &rwb->readers);raw_spin_lock_irqsave (&rtm->wait_lock, flags); -if (!atomic_read (&rwb->readers)) { -atomic_set (&rwb->readers,WRITER_BIAS ); +if (__rwbase_write_trylock (rwb)) {raw_spin_unlock_irqrestore (&rtm->wait_lock, flags);return 1 ; }
Peter Zijlstra
maintainerPATCH 4/4 locking/rwbase: Take care of ordering guarantee for fastpath reader
In reply to Peter ZijlstraFrom: Boqun Feng <[email protected]>
Readers of rwbase can lock and unlock without taking any inner lock, if that happens, we need the ordering provided by atomic operations to satisfy the ordering semantics of lock/unlock. Without that, considering the follow case:
{ X = 0 initially }
CPU 0 CPU 1
===== =====
rt_write_lock();
X = 1
rt_write_unlock():
atomic_add(READER_BIAS - WRITER_BIAS, ->readers);
// ->readers is READER_BIAS.
rt_read_lock():
if ((r = atomic_read(->readers)) < 0) // True
atomic_try_cmpxchg(->readers, r, r + 1); // succeed.
<acquire the read lock via fast path>
r1 = X; // r1 may be 0, because nothing prevent the reordering
// of "X=1" and atomic_add() on CPU 1.Therefore audit every usage of atomic operations that may happen in a fast path, and add necessary barriers.
--- kernel/locking/rwbase_rt.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
kernel/locking/rwbase_rt.c +19 -2
@@ -41,6 +41,12 @@* The risk of writer starvation is there, but the pathological use cases * which trigger it are not necessarily the typical RT workloads. * +* Fast-path orderings: +* The lock/unlock of readers can run in fast paths: lock and unlock are only +* atomic ops, and there is no inner lock to provide ACQUIRE and RELEASE +* semantics of rwbase_rt. Atomic ops should thus provide _acquire() +* and _release() (or stronger). +* * Common code shared between RT rw_semaphore and rwlock */
@@ -53,6 +59,7 @@ static __always_inline int rwbase_read_t* set. */ for (r =atomic_read (&rwb->readers); r <0 ;) { +/* Fully-ordered if cmpxchg() succeeds, provides ACQUIRE */ if (likely (atomic_try_cmpxchg (&rwb->readers, &r, r +1 )))return 1 ; }
@@ -162,6 +169,8 @@ static __always_inline void rwbase_read_/* * rwb->readers can only hit 0 when a writer is waiting for the * active readers to leave the critical section. +* +* dec_and_test() is fully ordered, provides RELEASE. */ if (unlikely (atomic_dec_and_test (&rwb->readers)))__rwbase_read_unlock (rwb, state);
@@ -172,7 +181,11 @@ static inline void __rwbase_write_unlock {struct rt_mutex_base *rtm = &rwb->rtmutex; -atomic_add (READER_BIAS - bias, &rwb->readers); +/* +* _release() is needed in case that reader is in fast path, pairing +* with atomic_try_cmpxchg() in rwbase_read_trylock(), provides RELEASE +*/ + (void )atomic_add_return_release (READER_BIAS - bias, &rwb->readers);raw_spin_unlock_irqrestore (&rtm->wait_lock, flags);rwbase_rtmutex_unlock (rtm); }
@@ -201,7 +214,11 @@ static inline bool __rwbase_write_tryloc/* Can do without CAS because we're serialized by wait_lock. */ lockdep_assert_held (&rwb->rtmutex.wait_lock); -if (!atomic_read (&rwb->readers)) { +/* +* _acquire is needed in case the reader is in the fast path, pairing +* with rwbase_read_unlock(), provides ACQUIRE. +*/ +if (!atomic_read_acquire (&rwb->readers)) {atomic_set (&rwb->readers,WRITER_BIAS );return 1 ; }
Re: [PATCH 4/4] locking/rwbase: Take care of ordering guarantee for fastpath reader
Quoted 81 lines · Peter Zijlstra
From: Boqun Feng <[email protected]>
Readers of rwbase can lock and unlock without taking any inner lock, if that happens, we need the ordering provided by atomic operations to satisfy the ordering semantics of lock/unlock. Without that, considering the follow case:
{ X = 0 initially } CPU 0 CPU 1 ===== ===== rt_write_lock(); X = 1 rt_write_unlock(): atomic_add(READER_BIAS - WRITER_BIAS, ->readers); // ->readers is READER_BIAS. rt_read_lock(): if ((r = atomic_read(->readers)) < 0) // True atomic_try_cmpxchg(->readers, r, r + 1); // succeed. <acquire the read lock via fast path> r1 = X; // r1 may be 0, because nothing prevent the reordering // of "X=1" and atomic_add() on CPU 1.Therefore audit every usage of atomic operations that may happen in a fast path, and add necessary barriers.
Signed-off-by Boqun Feng <[email protected]>Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- kernel/locking/rwbase_rt.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)kernel/locking/rwbase_rt.c +14
-1@@ -41,6 +41,12 @@* The risk of writer starvation is there, but the pathological use cases * which trigger it are not necessarily the typical RT workloads. * +* Fast-path orderings: +* The lock/unlock of readers can run in fast paths: lock and unlock are only +* atomic ops, and there is no inner lock to provide ACQUIRE and RELEASE +* semantics of rwbase_rt. Atomic ops should thus provide _acquire() +* and _release() (or stronger). +* * Common code shared between RT rw_semaphore and rwlock */ @@ -53,6 +59,7 @@ static __always_inline int rwbase_read_t* set. */ for (r =atomic_read (&rwb->readers); r <0 ;) { +/* Fully-ordered if cmpxchg() succeeds, provides ACQUIRE */ if (likely (atomic_try_cmpxchg (&rwb->readers, &r, r +1 )))return 1 ; }@@ -162,6 +169,8 @@ static __always_inline void rwbase_read_/* * rwb->readers can only hit 0 when a writer is waiting for the * active readers to leave the critical section. +* +* dec_and_test() is fully ordered, provides RELEASE. */ if (unlikely (atomic_dec_and_test (&rwb->readers)))__rwbase_read_unlock (rwb, state);@@ -172,7 +181,11 @@ static inline void __rwbase_write_unlock {struct rt_mutex_base *rtm = &rwb->rtmutex; -atomic_add (READER_BIAS - bias, &rwb->readers); +/* +* _release() is needed in case that reader is in fast path, pairing +* with atomic_try_cmpxchg() in rwbase_read_trylock(), provides RELEASE +*/ + (void )atomic_add_return_release (READER_BIAS - bias, &rwb->readers);raw_spin_unlock_irqrestore (&rtm->wait_lock, flags);rwbase_rtmutex_unlock (rtm); }@@ -201,7 +214,11 @@ static inline bool __rwbase_write_tryloc/* Can do without CAS because we're serialized by wait_lock. */ lockdep_assert_held (&rwb->rtmutex.wait_lock);
kernel/locking/rwbase_rt.c static inline bool __rwbase_write_tryloc · line 224
Quoted 10 lines
kernel/locking/rwbase_rt.c +5
-1-if (!atomic_read (&rwb->readers)) { +/* +* _acquire is needed in case the reader is in the fast path, pairing +* with rwbase_read_unlock(), provides ACQUIRE. +*/ +if (!atomic_read_acquire (&rwb->readers)) {atomic_set (&rwb->readers,WRITER_BIAS );return 1 ; }
[tip: locking/urgent] locking/rwbase: Take care of ordering guarantee for fastpath reader
In reply to Peter ZijlstraThe following commit has been merged into the locking/urgent branch of tip:
The following commit has been merged into the locking/urgent branch of tip:
Commit-ID: 81121524f1c798c9481bd7900450b72ee7ac2eef Gitweb: https://git.kernel.org/tip/81121524f1c798c9481bd7900450b72ee7ac2eef Author: Boqun Feng <[email protected]> AuthorDate: Thu, 09 Sep 2021 12:59:19 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 15 Sep 2021 17:49:16 +02:00
locking/rwbase: Take care of ordering guarantee for fastpath reader
Readers of rwbase can lock and unlock without taking any inner lock, if that happens, we need the ordering provided by atomic operations to satisfy the ordering semantics of lock/unlock. Without that, considering the follow case:
{ X = 0 initially }
CPU 0 CPU 1
===== =====
rt_write_lock();
X = 1
rt_write_unlock():
atomic_add(READER_BIAS - WRITER_BIAS, ->readers);
// ->readers is READER_BIAS.
rt_read_lock():
if ((r = atomic_read(->readers)) < 0) // True
atomic_try_cmpxchg(->readers, r, r + 1); // succeed.
<acquire the read lock via fast path>
r1 = X; // r1 may be 0, because nothing prevent the reordering
// of "X=1" and atomic_add() on CPU 1.Therefore audit every usage of atomic operations that may happen in a fast path, and add necessary barriers.
--- kernel/locking/rwbase_rt.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
kernel/locking/rwbase_rt.c +19 -2
@@ -41,6 +41,12 @@* The risk of writer starvation is there, but the pathological use cases * which trigger it are not necessarily the typical RT workloads. * +* Fast-path orderings: +* The lock/unlock of readers can run in fast paths: lock and unlock are only +* atomic ops, and there is no inner lock to provide ACQUIRE and RELEASE +* semantics of rwbase_rt. Atomic ops should thus provide _acquire() +* and _release() (or stronger). +* * Common code shared between RT rw_semaphore and rwlock */
@@ -53,6 +59,7 @@ static __always_inline int rwbase_read_trylock(struct rwbase_rt *rwb)* set. */ for (r =atomic_read (&rwb->readers); r <0 ;) { +/* Fully-ordered if cmpxchg() succeeds, provides ACQUIRE */ if (likely (atomic_try_cmpxchg (&rwb->readers, &r, r +1 )))return 1 ; }
@@ -162,6 +169,8 @@ static __always_inline void rwbase_read_unlock(struct rwbase_rt *rwb,/* * rwb->readers can only hit 0 when a writer is waiting for the * active readers to leave the critical section. +* +* dec_and_test() is fully ordered, provides RELEASE. */ if (unlikely (atomic_dec_and_test (&rwb->readers)))__rwbase_read_unlock (rwb, state);
@@ -172,7 +181,11 @@ static inline void __rwbase_write_unlock(struct rwbase_rt *rwb, int bias, {struct rt_mutex_base *rtm = &rwb->rtmutex; -atomic_add (READER_BIAS - bias, &rwb->readers); +/* +* _release() is needed in case that reader is in fast path, pairing +* with atomic_try_cmpxchg() in rwbase_read_trylock(), provides RELEASE +*/ + (void )atomic_add_return_release (READER_BIAS - bias, &rwb->readers);raw_spin_unlock_irqrestore (&rtm->wait_lock, flags);rwbase_rtmutex_unlock (rtm); }
@@ -201,7 +214,11 @@ static inline bool __rwbase_write_trylock(struct rwbase_rt *rwb)/* Can do without CAS because we're serialized by wait_lock. */ lockdep_assert_held (&rwb->rtmutex.wait_lock); -if (!atomic_read (&rwb->readers)) { +/* +* _acquire is needed in case the reader is in the fast path, pairing +* with rwbase_read_unlock(), provides ACQUIRE. +*/ +if (!atomic_read_acquire (&rwb->readers)) {atomic_set (&rwb->readers,WRITER_BIAS );return 1 ; }