All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Vihas Mak <makvihas@gmail.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org, Vitaly Kuznetsov <vkuznets@redhat.com>,
	Wanpeng Li <wanpengli@tencent.com>,
	Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>
Subject: Re: [PATCH] KVM: x86: move the can_unsync and prefetch checks outside of the loop
Date: Fri, 7 Jan 2022 21:55:21 +0000	[thread overview]
Message-ID: <Ydi2yTVpPGR9Qb+F@google.com> (raw)
In-Reply-To: <CAH1kMwSNhQMVLany4u+1tOZpa3KFr93OcwJGhnWN66gKWimaZA@mail.gmail.com>

On Sat, Jan 08, 2022, Vihas Mak wrote:
> On Fri, Jan 07, 2022, Sean Christopherson wrote:
> >> NAK, this change is functionally wrong.  The checks are inside the loop because
> >> the flow fails if and only if there is at least one indirect, valid shadow pages
> >> at the target gfn.  The @prefetch check is even more restrictive as it bails if
> >> there is at least one indirect, valid, synchronized shadow page.
> >> The can_unsync check could be "optimized" to
> >>
> >>       if (!can_unsync && kvm_gfn_has_indirect_valid_sp())
> >>
> >> but identifying whether or not there's a valid SP requires walking the list of
> >> shadow pages for the gfn, so it's simpler to just handle the check in the loop.
> >> And "optimized" in quotes because both checks will be well-predicted single-uop
> >> macrofused TEST+Jcc on modern CPUs, whereas walking the list twice would be
> >> relatively expensive if there are shadow pages for the gfn.
> 
> 
> So this change isn't safe. I will look into the optimization suggested
> by Sean. Sorry for this patch.

Heh, I wasn't actually suggesting we do the "optimization".  I was pointing out
what the code would look like _if_ we wanted to move the checks out of the loop,
but I do not actually think we should make any changes, quite the opposite.  The
theoretical worst case if there no indirect valid SPs, but lots of direct and/or
invalid SPs is far worse than burning a few uops per loop.

The compiler is smart enough to handle the checks out of line, and I'm sure there
are other optimizations being made as well.  In other words, odds are very good
that trying to optimize the code will do more harm than good.

      reply	other threads:[~2022-01-07 21:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-07  8:25 [PATCH] KVM: x86: move the can_unsync and prefetch checks outside of the loop Vihas Mak
2022-01-07 18:16 ` Paolo Bonzini
2022-01-07 19:26   ` Vihas Mak
2022-01-07 21:55     ` Sean Christopherson [this message]

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=Ydi2yTVpPGR9Qb+F@google.com \
    --to=seanjc@google.com \
    --cc=jmattson@google.com \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    --cc=makvihas@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=vkuznets@redhat.com \
    --cc=wanpengli@tencent.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.