public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Wanpeng Li <kernellwp@gmail.com>
Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
	Wanpeng Li <wanpeng.li@hotmail.com>,
	Ingo Molnar <mingo@kernel.org>, Waiman Long <Waiman.Long@hpe.com>,
	Davidlohr Bueso <dave@stgolabs.net>
Subject: Re: [PATCH RESEND v4] locking/pvqspinlock: Fix double hash race
Date: Tue, 9 Aug 2016 12:49:02 +0200	[thread overview]
Message-ID: <20160809104902.GH30192@twins.programming.kicks-ass.net> (raw)
In-Reply-To: <1470735467-4370-1-git-send-email-wanpeng.li@hotmail.com>

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.

  reply	other threads:[~2016-08-09 10:49 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-09  9:37 [PATCH RESEND v4] locking/pvqspinlock: Fix double hash race Wanpeng Li
2016-08-09 10:49 ` Peter Zijlstra [this message]
2016-08-09 11:31   ` Wanpeng Li
  -- strict thread matches above, loose matches on Subject: below --
2016-07-27 11:30 Wanpeng Li
2016-08-03  3:28 ` Waiman Long
2016-08-03  8:10   ` Wanpeng Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160809104902.GH30192@twins.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=Waiman.Long@hpe.com \
    --cc=dave@stgolabs.net \
    --cc=kernellwp@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=wanpeng.li@hotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox