mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Huang, Kai" <kai.huang@intel.com>
To: "zeming@nfschina.com" <zeming@nfschina.com>, "Christopherson,,
	Sean" <seanjc@google.com>
Cc: "x86@kernel.org" <x86@kernel.org>,
	"dave.hansen@linux.intel.com" <dave.hansen@linux.intel.com>,
	"bp@alien8.de" <bp@alien8.de>,
	"mingo@redhat.com" <mingo@redhat.com>,
	"tglx@linutronix.de" <tglx@linutronix.de>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	"pbonzini@redhat.com" <pbonzini@redhat.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] x86/kvm/mmu: Remove unnecessary ‘NULL’ values from sptep
Date: Mon, 4 Sep 2023 10:39:24 +0000	[thread overview]
Message-ID: <99cf9b5929418e8876e95a169d20ee26e126c51c.camel@intel.com> (raw)
In-Reply-To: <ZPIVzccIAiQnG4IA@google.com>

On Fri, 2023-09-01 at 09:48 -0700, Sean Christopherson wrote:
> On Sun, Sep 03, 2023, Li zeming wrote:
> > sptep is assigned first, so it does not need to initialize the assignment.
> > 
> > Signed-off-by: Li zeming <zeming@nfschina.com>
> > ---
> >  arch/x86/kvm/mmu/mmu.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> > index ec169f5c7dce..95f745aec4aa 100644
> > --- a/arch/x86/kvm/mmu/mmu.c
> > +++ b/arch/x86/kvm/mmu/mmu.c
> > @@ -3457,7 +3457,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
> >  	struct kvm_mmu_page *sp;
> >  	int ret = RET_PF_INVALID;
> >  	u64 spte = 0ull;
> > -	u64 *sptep = NULL;
> > +	u64 *sptep;
> >  	uint retry_count = 0;
> >  
> >  	if (!page_fault_can_be_fast(fault))
> 
> Hmm, this is safe, but there's some ugliness lurking.  Theoretically, it's possible
> for spte to be left untouched by the walkers.  That _shouldn't_ happen, as it means
> there's a bug somewhere in KVM.  But if that did happen, on the second or later
> iteration, it's (again, theoretically) possible to consume a stale spte.
> 
> 		if (tdp_mmu_enabled)
> 			sptep = kvm_tdp_mmu_fast_pf_get_last_sptep(vcpu, fault->addr, &spte);
> 		else
> 			sptep = fast_pf_get_last_sptep(vcpu, fault->addr, &spte);
> 
> 		if (!is_shadow_present_pte(spte)) <=== could consume stale data
> 			break;
> 
> If we're going to tidy up sptep, I think we should also give spte similar treatment
> and harden KVM in the process, e.g.
> 
> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> index 6325bb3e8c2b..ae2f87bbbf0a 100644
> --- a/arch/x86/kvm/mmu/mmu.c
> +++ b/arch/x86/kvm/mmu/mmu.c
> @@ -3430,8 +3430,8 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
>  {
>         struct kvm_mmu_page *sp;
>         int ret = RET_PF_INVALID;
> -       u64 spte = 0ull;
> -       u64 *sptep = NULL;
> +       u64 spte;
> +       u64 *sptep;
>         uint retry_count = 0;
>  
>         if (!page_fault_can_be_fast(fault))
> @@ -3447,6 +3447,14 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
>                 else
>                         sptep = fast_pf_get_last_sptep(vcpu, fault->addr, &spte);
>  
> +               /*
> +                * It's entirely possible for the mapping to have been zapped
> +                * by a different task, but the root page is should always be
> +                * available as the vCPU holds a reference to its root(s).
> +                */
> +               if (WARN_ON_ONCE(!sptep))
> +                       spte = REMOVED_SPTE;

If I recall correctly, REMOVED_SPTE is only used by TDP MMU code.  Should we use
0 (or initial SPTE value for case like TDX) instead of REMOVED_SPTE?

And I agree this code is more error proof (although theoretically for now).
  
> +
>                 if (!is_shadow_present_pte(spte))
>                         break;
>  
> 


  reply	other threads:[~2023-09-04 10:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-02 17:54 Li zeming
2023-09-01 16:48 ` Sean Christopherson
2023-09-04 10:39   ` Huang, Kai [this message]
2023-09-05 21:09     ` Sean Christopherson
2023-09-05 23:31       ` Huang, Kai

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=99cf9b5929418e8876e95a169d20ee26e126c51c.camel@intel.com \
    --to=kai.huang@intel.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    --cc=zeming@nfschina.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

all inboxes | Powered by JetHome®