Re: [PATCH 1/4] sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state()
From: Thomas Gleixner
Date: Mon Sep 13 2021 - 19:01:32 EST
On Tue, Sep 14 2021 at 00:08, Thomas Gleixner wrote:
> On Thu, Sep 09 2021 at 12:59, Peter Zijlstra wrote:
>> 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).
>
> I don't think so because both the state store and the wakeup are
> serialized via tsk->pi_lock.
>
>> 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) <peterz@xxxxxxxxxxxxx>
>> ---
>> include/linux/sched.h | 19 +++++++++++--------
>> 1 file changed, 11 insertions(+), 8 deletions(-)
>>
>> --- a/include/linux/sched.h
>> +++ b/include/linux/sched.h
>> @@ -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.
>
>> * 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?
>
>> 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); \
>
> 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.
If your reasoning would be correct, then set_special_state() would be
broken as well.
Thanks,
tglx