Skip to contentSkip to conversation

KVM: x86/mmu: avoid accidentally go to shadow path for 0 count tdp root

PATCH In review Yan Zhao 2 participants 3 messages Nov 10, 2022 – Nov 11, 2022

Subsystems (3)
lore
Share & tools
lore

Yan Zhao

author
#0 lore raw

PATCH 1/1 KVM: x86/mmu: avoid accidentally go to shadow path for 0 count tdp root

kvm mmu uses "if (is_tdp_mmu(vcpu->arch.mmu))" to choose between tdp mmu and shadow path.
If a root is a tdp mmu page while its root_count is 0, it's not valid to go to the shadow path.

So, return true and add a warn on zero root count.

---
 arch/x86/kvm/mmu/tdp_mmu.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/tdp_mmu.h +5 -1
index c163f7cc23ca..58b4881654a9 100644
@@ -74,6 +74,7 @@ static inline bool is_tdp_mmu(struct kvm_mmu *mmu)
 {
 	struct kvm_mmu_page *sp;
 	hpa_t hpa = mmu->root.hpa;
+	bool is_tdp;
 
 	if (WARN_ON(!VALID_PAGE(hpa)))
 		return false;
@@ -84,7 +85,10 @@ static inline bool is_tdp_mmu(struct kvm_mmu *mmu)
 	 * pae_root page, not a shadow page.
 	 */
 	sp = to_shadow_page(hpa);
-	return sp && is_tdp_mmu_page(sp) && sp->root_count;
+	is_tdp = sp && is_tdp_mmu_page(sp);
+	WARN_ON(is_tdp && !refcount_read(&sp->tdp_mmu_root_count));
+
+	return is_tdp;
 }
 #else
 static inline int kvm_mmu_init_tdp_mmu(struct kvm *kvm) { return 0; }
Signature · 1 line
2.17.1
#1 lore raw
Quoted 25 lines · Yan Zhao

kvm mmu uses "if (is_tdp_mmu(vcpu->arch.mmu))" to choose between tdp mmu and shadow path.
If a root is a tdp mmu page while its root_count is 0, it's not valid to go to the shadow path.

So, return true and add a warn on zero root count.

---
 arch/x86/kvm/mmu/tdp_mmu.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/tdp_mmu.h +1 -0
index c163f7cc23ca..58b4881654a9 100644
@@ -74,6 +74,7 @@ static inline bool is_tdp_mmu(struct kvm_mmu *mmu)
 {
 	struct kvm_mmu_page *sp;
 	hpa_t hpa = mmu->root.hpa;
+	bool is_tdp;
 
 	if (WARN_ON(!VALID_PAGE(hpa)))
 		return false;

arch/x86/kvm/mmu/tdp_mmu.h static inline bool is_tdp_mmu(struct kvm_mmu *mmu) · line 91

Quoted 8 lines
arch/x86/kvm/mmu/tdp_mmu.h +4 -1
@@ -84,7 +85,10 @@ static inline bool is_tdp_mmu(struct kvm_mmu *mmu)
 	 * pae_root page, not a shadow page.
 	 */
 	sp = to_shadow_page(hpa);
-	return sp && is_tdp_mmu_page(sp) && sp->root_count;
+	is_tdp = sp && is_tdp_mmu_page(sp);
+	WARN_ON(is_tdp && !refcount_read(&sp->tdp_mmu_root_count));
+
+	return is_tdp;

I have a series/patch that drops this code entirely, I would rather just go that route directly.

https://lore.kernel.org/all/[email protected]

author
#2 lore raw
Quoted 38 lines · Sean Christopherson
Quoted 25 lines · Yan Zhao

kvm mmu uses "if (is_tdp_mmu(vcpu->arch.mmu))" to choose between tdp mmu and shadow path.
If a root is a tdp mmu page while its root_count is 0, it's not valid to go to the shadow path.

So, return true and add a warn on zero root count.

---
 arch/x86/kvm/mmu/tdp_mmu.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/tdp_mmu.h +1 -0
index c163f7cc23ca..58b4881654a9 100644
@@ -74,6 +74,7 @@ static inline bool is_tdp_mmu(struct kvm_mmu *mmu)
 {
 	struct kvm_mmu_page *sp;
 	hpa_t hpa = mmu->root.hpa;
+	bool is_tdp;
 
 	if (WARN_ON(!VALID_PAGE(hpa)))
 		return false;

arch/x86/kvm/mmu/tdp_mmu.h static inline bool is_tdp_mmu(struct kvm_mmu *mmu) · line 91

Quoted 8 lines
arch/x86/kvm/mmu/tdp_mmu.h +4 -1
@@ -84,7 +85,10 @@ static inline bool is_tdp_mmu(struct kvm_mmu *mmu)
 	 * pae_root page, not a shadow page.
 	 */
 	sp = to_shadow_page(hpa);
-	return sp && is_tdp_mmu_page(sp) && sp->root_count;
+	is_tdp = sp && is_tdp_mmu_page(sp);
+	WARN_ON(is_tdp && !refcount_read(&sp->tdp_mmu_root_count));
+
+	return is_tdp;

I have a series/patch that drops this code entirely, I would rather just go that route directly.

Quoted 1 line

https://lore.kernel.org/all/[email protected]

Thanks for pointing me to this link.
Yes, it's a much better fix!

Thanks
Yan