From: Paul Mackerras <paulus@ozlabs.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Daniel Wagner <daniel.wagner@bmw-carit.de>,
linux-kernel@vger.kernel.org, linux-rt-users@vger.kernel.org,
Marcelo Tosatti <mtosatti@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>,
"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
Paul Gortmaker <paul.gortmaker@windriver.com>,
Thomas Gleixner <tglx@linutronix.de>
Subject: Re: [PATCH v3 2/4] KVM: use simple waitqueue for vcpu->wq
Date: Wed, 21 Oct 2015 19:55:00 +1100 [thread overview]
Message-ID: <20151021085500.GB15591@fergus.ozlabs.ibm.com> (raw)
In-Reply-To: <20151020140031.GG17308@twins.programming.kicks-ass.net>
On Tue, Oct 20, 2015 at 04:00:31PM +0200, Peter Zijlstra wrote:
> On Tue, Oct 20, 2015 at 09:28:08AM +0200, Daniel Wagner wrote:
> > diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
> > index 2280497..f534e15 100644
> > --- a/arch/powerpc/kvm/book3s_hv.c
> > +++ b/arch/powerpc/kvm/book3s_hv.c
> > @@ -2560,10 +2560,9 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
> > {
> > struct kvm_vcpu *vcpu;
> > int do_sleep = 1;
> > + DECLARE_SWAITQUEUE(wait);
> >
> > - DEFINE_WAIT(wait);
> > -
> > - prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
> > + prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
> >
> > /*
> > * Check one last time for pending exceptions and ceded state after
> > @@ -2577,7 +2576,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
> > }
> >
> > if (!do_sleep) {
> > - finish_wait(&vc->wq, &wait);
> > + finish_swait(&vc->wq, &wait);
> > return;
> > }
> >
> > @@ -2585,7 +2584,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
> > trace_kvmppc_vcore_blocked(vc, 0);
> > spin_unlock(&vc->lock);
> > schedule();
> > - finish_wait(&vc->wq, &wait);
> > + finish_swait(&vc->wq, &wait);
> > spin_lock(&vc->lock);
> > vc->vcore_state = VCORE_INACTIVE;
> > trace_kvmppc_vcore_blocked(vc, 1);
>
> This one looks buggy, one should _NOT_ assume that your blocking
> condition is true after schedule().
Do you mean it's buggy in calling finish_swait there, or it's buggy in
not immediately re-checking the condition? If the latter, then it's
OK because the sole caller of this function calls it in a loop and
checks the condition (all runnable vcpus in this vcore are idle) each
time around the loop.
> > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> > index 8db1d93..45ab55f 100644
> > --- a/virt/kvm/kvm_main.c
> > +++ b/virt/kvm/kvm_main.c
> > @@ -2019,7 +2018,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
> > }
> >
> > for (;;) {
> > - prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
> > + prepare_to_swait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
> >
> > if (kvm_vcpu_check_block(vcpu) < 0)
> > break;
> > @@ -2028,7 +2027,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
> > schedule();
> > }
> >
> > - finish_wait(&vcpu->wq, &wait);
> > + finish_swait(&vcpu->wq, &wait);
> > cur = ktime_get();
> >
> > out:
>
> Should we not take this opportunity to get rid of these open-coded wait
> loops?
>
>
> Does this work?
>
> ---
> arch/powerpc/kvm/book3s_hv.c | 33 +++++++++++++++++----------------
> virt/kvm/kvm_main.c | 13 ++-----------
> 2 files changed, 19 insertions(+), 27 deletions(-)
>
> diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
> index 228049786888..b5b8bcad5105 100644
> --- a/arch/powerpc/kvm/book3s_hv.c
> +++ b/arch/powerpc/kvm/book3s_hv.c
> @@ -2552,18 +2552,10 @@ static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
> finish_wait(&vcpu->arch.cpu_run, &wait);
> }
>
> -/*
> - * All the vcpus in this vcore are idle, so wait for a decrementer
> - * or external interrupt to one of the vcpus. vc->lock is held.
> - */
> -static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
> +static inline bool kvmppc_vcore_should_sleep(struct kvmppc_vcore *vc)
This function could also be used in kvmppc_run_vcpu().
> {
> struct kvm_vcpu *vcpu;
> - int do_sleep = 1;
> -
> - DEFINE_WAIT(wait);
> -
> - prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
> + bool sleep = true;
>
> /*
> * Check one last time for pending exceptions and ceded state after
> @@ -2571,26 +2563,35 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
> */
> list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
> if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
> - do_sleep = 0;
> + sleep = false;
> break;
> }
> }
>
> - if (!do_sleep) {
> - finish_wait(&vc->wq, &wait);
> - return;
> - }
> + return sleep;
> +}
>
> +static inline void kvmppc_vcore_schedule(struct kvmppc_vcore *vc)
> +{
> vc->vcore_state = VCORE_SLEEPING;
> trace_kvmppc_vcore_blocked(vc, 0);
> spin_unlock(&vc->lock);
> schedule();
> - finish_wait(&vc->wq, &wait);
> spin_lock(&vc->lock);
> vc->vcore_state = VCORE_INACTIVE;
> trace_kvmppc_vcore_blocked(vc, 1);
> }
>
> +/*
> + * All the vcpus in this vcore are idle, so wait for a decrementer
> + * or external interrupt to one of the vcpus. vc->lock is held.
> + */
> +static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
> +{
> + ___wait_event(vc->wq, !kvmppc_vcore_should_sleep(vc), TASK_IDLE, 0, 0,
> + kvmppc_vcore_schedule(vc));
Wow, triple underscores, that must be an ultra-trendy function. :)
> +}
> +
> static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
> {
> int n_ceded;
That all looks OK at a first glance, I'll give it a whirl.
Paul.
next prev parent reply other threads:[~2015-10-21 8:55 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-20 7:28 [PATCH v3 0/4] Simple wait queue support Daniel Wagner
2015-10-20 7:28 ` [PATCH v3 1/4] wait.[ch]: Introduce the simple waitqueue (swait) implementation Daniel Wagner
2015-10-26 12:04 ` Boqun Feng
2015-10-26 12:28 ` Peter Zijlstra
2015-10-26 12:59 ` Daniel Wagner
2015-10-26 13:26 ` Peter Zijlstra
2015-10-26 14:19 ` Boqun Feng
2015-11-04 10:33 ` Thomas Gleixner
2015-11-04 12:12 ` Daniel Wagner
2015-11-18 10:33 ` Peter Zijlstra
2015-11-18 15:07 ` Thomas Gleixner
2015-10-20 7:28 ` [PATCH v3 2/4] KVM: use simple waitqueue for vcpu->wq Daniel Wagner
2015-10-20 13:11 ` Paolo Bonzini
2015-10-20 14:00 ` Peter Zijlstra
2015-10-20 15:40 ` Paolo Bonzini
2015-10-20 16:06 ` Peter Zijlstra
2015-10-21 8:55 ` Paul Mackerras [this message]
2015-10-21 9:05 ` Peter Zijlstra
2015-10-21 9:10 ` Paul Mackerras
2015-10-21 9:24 ` Paul Mackerras
2015-10-21 11:13 ` Peter Zijlstra
2015-10-23 11:51 ` Daniel Wagner
2015-10-20 7:28 ` [PATCH v3 3/4] rcu: Do not call rcu_nocb_gp_cleanup() while holding rnp->lock Daniel Wagner
2015-10-20 7:28 ` [PATCH v3 4/4] rcu: use simple wait queues where possible in rcutree Daniel Wagner
2015-10-25 20:10 ` [PATCH v3 0/4] Simple wait queue support Paul E. McKenney
2015-10-26 6:34 ` Daniel Wagner
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=20151021085500.GB15591@fergus.ozlabs.ibm.com \
--to=paulus@ozlabs.org \
--cc=daniel.wagner@bmw-carit.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rt-users@vger.kernel.org \
--cc=mtosatti@redhat.com \
--cc=paul.gortmaker@windriver.com \
--cc=paulmck@linux.vnet.ibm.com \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
/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;
as well as URLs for NNTP newsgroup(s).