Skip to contentSkip to conversation

x86/kvm/mmu: Remove unnecessary ‘NULL’ values from sptep

PATCH Superseded Li zeming 3 participants 5 messages Sep 1, 2023 – Sep 5, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Li zeming

author
#0 lore raw

PATCH 1/1 x86/kvm/mmu: Remove unnecessary ‘NULL’ values from sptep

sptep is assigned first, so it does not need to initialize the assignment.

---
 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -1
index ec169f5c7dce..95f745aec4aa 100644
@@ -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))
Signature · 1 line
2.18.2
#1 lore raw
Quoted 12 lines · Li zeming

sptep is assigned first, so it does not need to initialize the assignment.

---
 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index ec169f5c7dce..95f745aec4aa 100644

arch/x86/kvm/mmu/mmu.c static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 3463

Quoted 8 lines
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -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.

arch/x86/kvm/mmu/mmu.c +10 -2
index 6325bb3e8c2b..ae2f87bbbf0a 100644
@@ -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 (!is_shadow_present_pte(spte))
                        break;
 
#2 lore raw
Quoted 54 lines · Sean Christopherson
Quoted 12 lines · Li zeming

sptep is assigned first, so it does not need to initialize the assignment.

---
 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index ec169f5c7dce..95f745aec4aa 100644

arch/x86/kvm/mmu/mmu.c static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 3463

Quoted 8 lines
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -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.

arch/x86/kvm/mmu/mmu.c +2 -2
index 6325bb3e8c2b..ae2f87bbbf0a 100644
@@ -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))

arch/x86/kvm/mmu/mmu.c static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 3456

Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +7 -0
@@ -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).

Quoted 5 lines
arch/x86/kvm/mmu/mmu.c +1 -0
+
                if (!is_shadow_present_pte(spte))
                        break;
 
#3 lore raw
Quoted 5 lines · Kai Huang
Quoted 4 lines · Sean Christopherson
arch/x86/kvm/mmu/mmu.c +0 -0
@@ -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);
 
Quoted 10 lines

arch/x86/kvm/mmu/mmu.c static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 3456

Quoted 7 lines
arch/x86/kvm/mmu/mmu.c +7 -0
+               /*
+                * 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?

I deliberately suggested REMOVED_SPTE in part because of TDX introducing "initial SPTE"; finding/remembering '0' initialization of SPTEs is hard. Though FWIW, '0' would be totally fine for TDX because the value is never exposed to hardware.

I think it's totally fine to use REMOVED_SPTE like this in common code, I would be quite surprised if it causes confusion. Even though REMOVED_SPTE was introduced by the TDP MMU, its value/semantics are generic.

#4 lore raw
Quoted 17 lines · Sean Christopherson
Quoted 5 lines · Kai Huang
Quoted 4 lines · Sean Christopherson
arch/x86/kvm/mmu/mmu.c +0 -0
@@ -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);
 
Quoted 10 lines

arch/x86/kvm/mmu/mmu.c static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 3456

Quoted 7 lines
arch/x86/kvm/mmu/mmu.c +7 -0
+               /*
+                * 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?

Quoted 7 lines

I deliberately suggested REMOVED_SPTE in part because of TDX introducing "initial SPTE"; finding/remembering '0' initialization of SPTEs is hard. Though FWIW, '0' would be totally fine for TDX because the value is never exposed to hardware.

I think it's totally fine to use REMOVED_SPTE like this in common code, I would be quite surprised if it causes confusion. Even though REMOVED_SPTE was introduced by the TDP MMU, its value/semantics are generic.

Yeah certainly no harm here. :-)

My thinking was REMOVED_SPTE is supposed to be an intermediate state for one SPTE, which is actually "in the page table", while multiple threads can operate on the page table entry concurrently. Slightly mismatch the case here IIUC. But I guess it also depends on how we view this case here.