* [PATCH RESEND v4] locking/pvqspinlock: Fix double hash race
@ 2016-07-27 11:30 Wanpeng Li
2016-08-03 3:28 ` Waiman Long
0 siblings, 1 reply; 6+ messages in thread
From: Wanpeng Li @ 2016-07-27 11:30 UTC (permalink / raw)
To: Ingo Molnar, Peter Zijlstra, linux-kernel, kvm
Cc: Wanpeng Li, Waiman Long, Davidlohr Bueso
From: Wanpeng Li <wanpeng.li@hotmail.com>
When the lock holder vCPU is racing with the queue head vCPU:
lock holder vCPU queue head vCPU
===================== ==================
node->locked = 1;
<preemption> READ_ONCE(node->locked)
... pv_wait_head_or_lock():
SPIN_THRESHOLD loop;
pv_hash();
lock->locked = _Q_SLOW_VAL;
node->state = vcpu_hashed;
pv_kick_node():
cmpxchg(node->state,
vcpu_halted, vcpu_hashed);
lock->locked = _Q_SLOW_VAL;
pv_hash();
With preemption at the right moment, it is possible that both the
lock holder and queue head vCPUs can be racing to set node->state
which can result in hash entry race. Making sure the state is never
set to vcpu_halted will prevent this racing from happening.
This patch fix it by setting vcpu_hashed after we did all hash thing.
Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
Reviewed-by: Pan Xinhui <xinhui.pan@linux.vnet.ibm.com>
Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Waiman Long <Waiman.Long@hpe.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
---
v3 -> v4:
* update patch subject
* add code comments
v2 -> v3:
* fix typo in patch description
v1 -> v2:
* adjust patch description
kernel/locking/qspinlock_paravirt.h | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/kernel/locking/qspinlock_paravirt.h b/kernel/locking/qspinlock_paravirt.h
index 21ede57..ca96db4 100644
--- a/kernel/locking/qspinlock_paravirt.h
+++ b/kernel/locking/qspinlock_paravirt.h
@@ -450,7 +450,28 @@ pv_wait_head_or_lock(struct qspinlock *lock, struct mcs_spinlock *node)
goto gotlock;
}
}
- WRITE_ONCE(pn->state, vcpu_halted);
+ /*
+ * lock holder vCPU queue head vCPU
+ * ---------------- ---------------
+ * node->locked = 1;
+ * <preemption> READ_ONCE(node->locked)
+ * ... pv_wait_head_or_lock():
+ * SPIN_THRESHOLD loop;
+ * pv_hash();
+ * lock->locked = _Q_SLOW_VAL;
+ * node->state = vcpu_hashed;
+ * pv_kick_node():
+ * cmpxchg(node->state,
+ * vcpu_halted, vcpu_hashed);
+ * lock->locked = _Q_SLOW_VAL;
+ * pv_hash();
+ *
+ * With preemption at the right moment, it is possible that both the
+ * lock holder and queue head vCPUs can be racing to set node->state.
+ * Making sure the state is never set to vcpu_halted will prevent this
+ * racing from happening.
+ */
+ WRITE_ONCE(pn->state, vcpu_hashed);
qstat_inc(qstat_pv_wait_head, true);
qstat_inc(qstat_pv_wait_again, waitcnt);
pv_wait(&l->locked, _Q_SLOW_VAL);
--
2.1.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH RESEND v4] locking/pvqspinlock: Fix double hash race
2016-07-27 11:30 [PATCH RESEND v4] locking/pvqspinlock: Fix double hash race Wanpeng Li
@ 2016-08-03 3:28 ` Waiman Long
2016-08-03 8:10 ` Wanpeng Li
0 siblings, 1 reply; 6+ messages in thread
From: Waiman Long @ 2016-08-03 3:28 UTC (permalink / raw)
To: Wanpeng Li
Cc: Ingo Molnar, Peter Zijlstra, linux-kernel, kvm, Wanpeng Li,
Davidlohr Bueso
On 07/27/2016 07:30 AM, Wanpeng Li wrote:
> From: Wanpeng Li<wanpeng.li@hotmail.com>
>
> When the lock holder vCPU is racing with the queue head vCPU:
>
> lock holder vCPU queue head vCPU
> ===================== ==================
>
> node->locked = 1;
> <preemption> READ_ONCE(node->locked)
> ... pv_wait_head_or_lock():
> SPIN_THRESHOLD loop;
> pv_hash();
> lock->locked = _Q_SLOW_VAL;
> node->state = vcpu_hashed;
> pv_kick_node():
> cmpxchg(node->state,
> vcpu_halted, vcpu_hashed);
> lock->locked = _Q_SLOW_VAL;
> pv_hash();
>
> With preemption at the right moment, it is possible that both the
> lock holder and queue head vCPUs can be racing to set node->state
> which can result in hash entry race. Making sure the state is never
> set to vcpu_halted will prevent this racing from happening.
>
> This patch fix it by setting vcpu_hashed after we did all hash thing.
>
> Reviewed-by: Davidlohr Bueso<dave@stgolabs.net>
> Reviewed-by: Pan Xinhui<xinhui.pan@linux.vnet.ibm.com>
> Cc: Peter Zijlstra (Intel)<peterz@infradead.org>
> Cc: Ingo Molnar<mingo@kernel.org>
> Cc: Waiman Long<Waiman.Long@hpe.com>
> Cc: Davidlohr Bueso<dave@stgolabs.net>
> Signed-off-by: Wanpeng Li<wanpeng.li@hotmail.com>
> ---
> v3 -> v4:
> * update patch subject
> * add code comments
> v2 -> v3:
> * fix typo in patch description
> v1 -> v2:
> * adjust patch description
>
> kernel/locking/qspinlock_paravirt.h | 23 ++++++++++++++++++++++-
> 1 file changed, 22 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/locking/qspinlock_paravirt.h b/kernel/locking/qspinlock_paravirt.h
> index 21ede57..ca96db4 100644
> --- a/kernel/locking/qspinlock_paravirt.h
> +++ b/kernel/locking/qspinlock_paravirt.h
> @@ -450,7 +450,28 @@ pv_wait_head_or_lock(struct qspinlock *lock, struct mcs_spinlock *node)
> goto gotlock;
> }
> }
> - WRITE_ONCE(pn->state, vcpu_halted);
> + /*
> + * lock holder vCPU queue head vCPU
> + * ---------------- ---------------
> + * node->locked = 1;
> + *<preemption> READ_ONCE(node->locked)
> + * ... pv_wait_head_or_lock():
> + * SPIN_THRESHOLD loop;
> + * pv_hash();
> + * lock->locked = _Q_SLOW_VAL;
> + * node->state = vcpu_hashed;
> + * pv_kick_node():
> + * cmpxchg(node->state,
> + * vcpu_halted, vcpu_hashed);
> + * lock->locked = _Q_SLOW_VAL;
> + * pv_hash();
> + *
> + * With preemption at the right moment, it is possible that both the
> + * lock holder and queue head vCPUs can be racing to set node->state.
> + * Making sure the state is never set to vcpu_halted will prevent this
> + * racing from happening.
> + */
> + WRITE_ONCE(pn->state, vcpu_hashed);
> qstat_inc(qstat_pv_wait_head, true);
> qstat_inc(qstat_pv_wait_again, waitcnt);
> pv_wait(&l->locked, _Q_SLOW_VAL);
Acked-by: Waiman Long <Waiman.Long@hpe.com>
Cheers,
Longman
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH RESEND v4] locking/pvqspinlock: Fix double hash race
2016-08-03 3:28 ` Waiman Long
@ 2016-08-03 8:10 ` Wanpeng Li
0 siblings, 0 replies; 6+ messages in thread
From: Wanpeng Li @ 2016-08-03 8:10 UTC (permalink / raw)
To: Waiman Long
Cc: Ingo Molnar, Peter Zijlstra, linux-kernel, kvm, Wanpeng Li,
Davidlohr Bueso
2016-08-03 11:28 GMT+08:00 Waiman Long <waiman.long@hpe.com>:
> On 07/27/2016 07:30 AM, Wanpeng Li wrote:
>>
>> From: Wanpeng Li<wanpeng.li@hotmail.com>
>>
>> When the lock holder vCPU is racing with the queue head vCPU:
>>
>> lock holder vCPU queue head vCPU
>> ===================== ==================
>>
>> node->locked = 1;
>> <preemption> READ_ONCE(node->locked)
>> ... pv_wait_head_or_lock():
>> SPIN_THRESHOLD loop;
>> pv_hash();
>> lock->locked = _Q_SLOW_VAL;
>> node->state = vcpu_hashed;
>> pv_kick_node():
>> cmpxchg(node->state,
>> vcpu_halted, vcpu_hashed);
>> lock->locked = _Q_SLOW_VAL;
>> pv_hash();
>>
>> With preemption at the right moment, it is possible that both the
>> lock holder and queue head vCPUs can be racing to set node->state
>> which can result in hash entry race. Making sure the state is never
>> set to vcpu_halted will prevent this racing from happening.
>>
>> This patch fix it by setting vcpu_hashed after we did all hash thing.
>>
>> Reviewed-by: Davidlohr Bueso<dave@stgolabs.net>
>> Reviewed-by: Pan Xinhui<xinhui.pan@linux.vnet.ibm.com>
>> Cc: Peter Zijlstra (Intel)<peterz@infradead.org>
>> Cc: Ingo Molnar<mingo@kernel.org>
>> Cc: Waiman Long<Waiman.Long@hpe.com>
>> Cc: Davidlohr Bueso<dave@stgolabs.net>
>> Signed-off-by: Wanpeng Li<wanpeng.li@hotmail.com>
>> ---
>> v3 -> v4:
>> * update patch subject
>> * add code comments
>> v2 -> v3:
>> * fix typo in patch description
>> v1 -> v2:
>> * adjust patch description
>>
>> kernel/locking/qspinlock_paravirt.h | 23 ++++++++++++++++++++++-
>> 1 file changed, 22 insertions(+), 1 deletion(-)
>>
>> diff --git a/kernel/locking/qspinlock_paravirt.h
>> b/kernel/locking/qspinlock_paravirt.h
>> index 21ede57..ca96db4 100644
>> --- a/kernel/locking/qspinlock_paravirt.h
>> +++ b/kernel/locking/qspinlock_paravirt.h
>> @@ -450,7 +450,28 @@ pv_wait_head_or_lock(struct qspinlock *lock, struct
>> mcs_spinlock *node)
>> goto gotlock;
>> }
>> }
>> - WRITE_ONCE(pn->state, vcpu_halted);
>> + /*
>> + * lock holder vCPU queue head vCPU
>> + * ---------------- ---------------
>> + * node->locked = 1;
>> + *<preemption> READ_ONCE(node->locked)
>> + * ... pv_wait_head_or_lock():
>> + * SPIN_THRESHOLD loop;
>> + * pv_hash();
>> + * lock->locked =
>> _Q_SLOW_VAL;
>> + * node->state =
>> vcpu_hashed;
>> + * pv_kick_node():
>> + * cmpxchg(node->state,
>> + * vcpu_halted, vcpu_hashed);
>> + * lock->locked = _Q_SLOW_VAL;
>> + * pv_hash();
>> + *
>> + * With preemption at the right moment, it is possible
>> that both the
>> + * lock holder and queue head vCPUs can be racing to set
>> node->state.
>> + * Making sure the state is never set to vcpu_halted will
>> prevent this
>> + * racing from happening.
>> + */
>> + WRITE_ONCE(pn->state, vcpu_hashed);
>> qstat_inc(qstat_pv_wait_head, true);
>> qstat_inc(qstat_pv_wait_again, waitcnt);
>> pv_wait(&l->locked, _Q_SLOW_VAL);
>
>
> Acked-by: Waiman Long <Waiman.Long@hpe.com>
Thanks. :)
Regards,
Wanpeng Li
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH RESEND v4] locking/pvqspinlock: Fix double hash race
2016-08-09 10:49 ` Peter Zijlstra
@ 2016-08-09 11:31 ` Wanpeng Li
0 siblings, 0 replies; 6+ messages in thread
From: Wanpeng Li @ 2016-08-09 11:31 UTC (permalink / raw)
To: Peter Zijlstra
Cc: linux-kernel, kvm, Wanpeng Li, Ingo Molnar, Waiman Long, Davidlohr Bueso
2016-08-09 18:49 GMT+08:00 Peter Zijlstra <peterz@infradead.org>:
[...]
> In any case, I have an older version (possibly v1) queued up. That fixes
> the bug.
Thanks. :)
Regards,
Wanpeng Li
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH RESEND v4] locking/pvqspinlock: Fix double hash race
2016-08-09 9:37 Wanpeng Li
@ 2016-08-09 10:49 ` Peter Zijlstra
2016-08-09 11:31 ` Wanpeng Li
0 siblings, 1 reply; 6+ messages in thread
From: Peter Zijlstra @ 2016-08-09 10:49 UTC (permalink / raw)
To: Wanpeng Li
Cc: linux-kernel, kvm, Wanpeng Li, Ingo Molnar, Waiman Long, Davidlohr Bueso
On Tue, Aug 09, 2016 at 05:37:47PM +0800, Wanpeng Li wrote:
> From: Wanpeng Li <wanpeng.li@hotmail.com>
>
> When the lock holder vCPU is racing with the queue head vCPU:
>
> lock holder vCPU queue head vCPU
> ===================== ==================
>
> node->locked = 1;
> <preemption> READ_ONCE(node->locked)
> ... pv_wait_head_or_lock():
> SPIN_THRESHOLD loop;
> pv_hash();
> lock->locked = _Q_SLOW_VAL;
> node->state = vcpu_hashed;
> pv_kick_node():
> cmpxchg(node->state,
> vcpu_halted, vcpu_hashed);
> lock->locked = _Q_SLOW_VAL;
> pv_hash();
So here the example is 'wrong' in that it doesn't illustrate the fail
case, namely having vcpu_halted win while we're hashed.
> +++ b/kernel/locking/qspinlock_paravirt.h
> @@ -450,7 +450,28 @@ pv_wait_head_or_lock(struct qspinlock *lock, struct mcs_spinlock *node)
> goto gotlock;
> }
> }
> - WRITE_ONCE(pn->state, vcpu_halted);
> + /*
> + * lock holder vCPU queue head vCPU
> + * ---------------- ---------------
> + * node->locked = 1;
> + * <preemption> READ_ONCE(node->locked)
> + * ... pv_wait_head_or_lock():
> + * SPIN_THRESHOLD loop;
> + * pv_hash();
> + * lock->locked = _Q_SLOW_VAL;
> + * node->state = vcpu_hashed;
> + * pv_kick_node():
> + * cmpxchg(node->state,
> + * vcpu_halted, vcpu_hashed);
> + * lock->locked = _Q_SLOW_VAL;
> + * pv_hash();
> + *
> + * With preemption at the right moment, it is possible that both the
> + * lock holder and queue head vCPUs can be racing to set node->state.
> + * Making sure the state is never set to vcpu_halted will prevent this
> + * racing from happening.
> + */
> + WRITE_ONCE(pn->state, vcpu_hashed);
> qstat_inc(qstat_pv_wait_head, true);
> qstat_inc(qstat_pv_wait_again, waitcnt);
> pv_wait(&l->locked, _Q_SLOW_VAL);
And I completely fail to see the point of this comment, still.
Yes, if we would have used vcpu_halted, there would be a problem, but we
don't so there isn't. What does this comment tell us about the current
code?
In any case, I have an older version (possibly v1) queued up. That fixes
the bug.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH RESEND v4] locking/pvqspinlock: Fix double hash race
@ 2016-08-09 9:37 Wanpeng Li
2016-08-09 10:49 ` Peter Zijlstra
0 siblings, 1 reply; 6+ messages in thread
From: Wanpeng Li @ 2016-08-09 9:37 UTC (permalink / raw)
To: linux-kernel, kvm
Cc: Wanpeng Li, Peter Zijlstra (Intel),
Ingo Molnar, Waiman Long, Davidlohr Bueso
From: Wanpeng Li <wanpeng.li@hotmail.com>
When the lock holder vCPU is racing with the queue head vCPU:
lock holder vCPU queue head vCPU
===================== ==================
node->locked = 1;
<preemption> READ_ONCE(node->locked)
... pv_wait_head_or_lock():
SPIN_THRESHOLD loop;
pv_hash();
lock->locked = _Q_SLOW_VAL;
node->state = vcpu_hashed;
pv_kick_node():
cmpxchg(node->state,
vcpu_halted, vcpu_hashed);
lock->locked = _Q_SLOW_VAL;
pv_hash();
With preemption at the right moment, it is possible that both the
lock holder and queue head vCPUs can be racing to set node->state
which can result in hash entry race. Making sure the state is never
set to vcpu_halted will prevent this racing from happening.
This patch fix it by setting vcpu_hashed after we did all hash thing.
Acked-by: Waiman Long <Waiman.Long@hpe.com>
Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
Reviewed-by: Pan Xinhui <xinhui.pan@linux.vnet.ibm.com>
Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Waiman Long <Waiman.Long@hpe.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
---
v3 -> v4:
* update patch subject
* add code comments
v2 -> v3:
* fix typo in patch description
v1 -> v2:
* adjust patch description
kernel/locking/qspinlock_paravirt.h | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/kernel/locking/qspinlock_paravirt.h b/kernel/locking/qspinlock_paravirt.h
index 21ede57..ca96db4 100644
--- a/kernel/locking/qspinlock_paravirt.h
+++ b/kernel/locking/qspinlock_paravirt.h
@@ -450,7 +450,28 @@ pv_wait_head_or_lock(struct qspinlock *lock, struct mcs_spinlock *node)
goto gotlock;
}
}
- WRITE_ONCE(pn->state, vcpu_halted);
+ /*
+ * lock holder vCPU queue head vCPU
+ * ---------------- ---------------
+ * node->locked = 1;
+ * <preemption> READ_ONCE(node->locked)
+ * ... pv_wait_head_or_lock():
+ * SPIN_THRESHOLD loop;
+ * pv_hash();
+ * lock->locked = _Q_SLOW_VAL;
+ * node->state = vcpu_hashed;
+ * pv_kick_node():
+ * cmpxchg(node->state,
+ * vcpu_halted, vcpu_hashed);
+ * lock->locked = _Q_SLOW_VAL;
+ * pv_hash();
+ *
+ * With preemption at the right moment, it is possible that both the
+ * lock holder and queue head vCPUs can be racing to set node->state.
+ * Making sure the state is never set to vcpu_halted will prevent this
+ * racing from happening.
+ */
+ WRITE_ONCE(pn->state, vcpu_hashed);
qstat_inc(qstat_pv_wait_head, true);
qstat_inc(qstat_pv_wait_again, waitcnt);
pv_wait(&l->locked, _Q_SLOW_VAL);
--
2.1.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2016-08-09 11:31 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-27 11:30 [PATCH RESEND v4] locking/pvqspinlock: Fix double hash race Wanpeng Li
2016-08-03 3:28 ` Waiman Long
2016-08-03 8:10 ` Wanpeng Li
2016-08-09 9:37 Wanpeng Li
2016-08-09 10:49 ` Peter Zijlstra
2016-08-09 11:31 ` Wanpeng Li
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).