Skip to contentSkip to conversation

KVM: Trivial cleanups

PATCH Applied Lai Jiangshan 3 participants 10 messages Jun 5, 2022 – Jun 14, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover KVM: Trivial cleanups

From: Lai Jiangshan <[email protected]>

A small collection of trivial cleanups.

Lai Jiangshan (6):
KVM: X86/MMU: Remove unused macros from paging_tmpl.h
KVM: X86/MMU: Remove unused PT32_DIR_BASE_ADDR_MASK from mmu.c
KVM: X86/MMU: Update comments in paging_tmpl.h for the kinds of guest
PTEs
KVM: Rename ack_flush() to ack_kick()
KVM: X86/MMU: Remove useless mmu_topup_memory_caches() in

    kvm_mmu_pte_write()

KVM: X86/SVM: Use root_level in svm_load_mmu_pgd()

 arch/x86/kvm/mmu/mmu.c         |  9 ---------
 arch/x86/kvm/mmu/paging_tmpl.h | 16 ++--------------
 arch/x86/kvm/svm/svm.c         |  2 +-
 virt/kvm/kvm_main.c            |  4 ++--
 4 files changed, 5 insertions(+), 26 deletions(-)
Signature · 1 line
2.19.1.6.gb485710b
#1 lore raw

PATCH 1/6 KVM: X86/MMU: Remove unused macros from paging_tmpl.h

From: Lai Jiangshan <[email protected]>

Remove unused PT_BASE_ADDR_MASK, PT_LEVEL_BITS, and CMPXCHG.

---
 arch/x86/kvm/mmu/paging_tmpl.h | 12 ------------
 1 file changed, 12 deletions(-)
arch/x86/kvm/mmu/paging_tmpl.h +0 -12
index db80f7ccaa4e..2375bd5fd9f4 100644
@@ -24,17 +24,14 @@
 	#define pt_element_t u64
 	#define guest_walker guest_walker64
 	#define FNAME(name) paging##64_##name
-	#define PT_BASE_ADDR_MASK GUEST_PT64_BASE_ADDR_MASK
 	#define PT_LVL_ADDR_MASK(lvl) PT64_LVL_ADDR_MASK(lvl)
 	#define PT_LVL_OFFSET_MASK(lvl) PT64_LVL_OFFSET_MASK(lvl)
 	#define PT_INDEX(addr, level) PT64_INDEX(addr, level)
-	#define PT_LEVEL_BITS PT64_LEVEL_BITS
 	#define PT_GUEST_DIRTY_SHIFT PT_DIRTY_SHIFT
 	#define PT_GUEST_ACCESSED_SHIFT PT_ACCESSED_SHIFT
 	#define PT_HAVE_ACCESSED_DIRTY(mmu) true
 	#ifdef CONFIG_X86_64
 	#define PT_MAX_FULL_LEVELS PT64_ROOT_MAX_LEVEL
-	#define CMPXCHG "cmpxchgq"
 	#else
 	#define PT_MAX_FULL_LEVELS 2
 	#endif
@@ -42,30 +39,24 @@
 	#define pt_element_t u32
 	#define guest_walker guest_walker32
 	#define FNAME(name) paging##32_##name
-	#define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK
 	#define PT_LVL_ADDR_MASK(lvl) PT32_LVL_ADDR_MASK(lvl)
 	#define PT_LVL_OFFSET_MASK(lvl) PT32_LVL_OFFSET_MASK(lvl)
 	#define PT_INDEX(addr, level) PT32_INDEX(addr, level)
-	#define PT_LEVEL_BITS PT32_LEVEL_BITS
 	#define PT_MAX_FULL_LEVELS 2
 	#define PT_GUEST_DIRTY_SHIFT PT_DIRTY_SHIFT
 	#define PT_GUEST_ACCESSED_SHIFT PT_ACCESSED_SHIFT
 	#define PT_HAVE_ACCESSED_DIRTY(mmu) true
-	#define CMPXCHG "cmpxchgl"
 #elif PTTYPE == PTTYPE_EPT
 	#define pt_element_t u64
 	#define guest_walker guest_walkerEPT
 	#define FNAME(name) ept_##name
-	#define PT_BASE_ADDR_MASK GUEST_PT64_BASE_ADDR_MASK
 	#define PT_LVL_ADDR_MASK(lvl) PT64_LVL_ADDR_MASK(lvl)
 	#define PT_LVL_OFFSET_MASK(lvl) PT64_LVL_OFFSET_MASK(lvl)
 	#define PT_INDEX(addr, level) PT64_INDEX(addr, level)
-	#define PT_LEVEL_BITS PT64_LEVEL_BITS
 	#define PT_GUEST_DIRTY_SHIFT 9
 	#define PT_GUEST_ACCESSED_SHIFT 8
 	#define PT_HAVE_ACCESSED_DIRTY(mmu) (!(mmu)->cpu_role.base.ad_disabled)
 	#ifdef CONFIG_X86_64
-	#define CMPXCHG "cmpxchgq"
 	#endif
 	#define PT_MAX_FULL_LEVELS PT64_ROOT_MAX_LEVEL
 #else
@@ -1076,15 +1067,12 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
 #undef pt_element_t
 #undef guest_walker
 #undef FNAME
-#undef PT_BASE_ADDR_MASK
 #undef PT_INDEX
 #undef PT_LVL_ADDR_MASK
 #undef PT_LVL_OFFSET_MASK
-#undef PT_LEVEL_BITS
 #undef PT_MAX_FULL_LEVELS
 #undef gpte_to_gfn
 #undef gpte_to_gfn_lvl
-#undef CMPXCHG
 #undef PT_GUEST_ACCESSED_MASK
 #undef PT_GUEST_DIRTY_MASK
 #undef PT_GUEST_DIRTY_SHIFT
Signature · 1 line
2.19.1.6.gb485710b
#2 lore raw

PATCH 2/6 KVM: X86/MMU: Remove unused PT32_DIR_BASE_ADDR_MASK from mmu.c

In reply to Lai Jiangshan

From: Lai Jiangshan <[email protected]>

It is unused.

---
 arch/x86/kvm/mmu/mmu.c | 2 --
 1 file changed, 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +0 -2
index efe5a3dca1e0..c935fdfc2544 100644
@@ -125,8 +125,6 @@ module_param(dbg, bool, 0644);
 
 
 #define PT32_BASE_ADDR_MASK PAGE_MASK
-#define PT32_DIR_BASE_ADDR_MASK \
-	(PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
 #define PT32_LVL_ADDR_MASK(level) \
 	(PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
 					    * PT32_LEVEL_BITS))) - 1))
Signature · 1 line
2.19.1.6.gb485710b
#3 lore raw

PATCH 3/6 KVM: X86/MMU: Update comments in paging_tmpl.h for the kinds of guest PTEs

In reply to Lai Jiangshan

From: Lai Jiangshan <[email protected]>

Add EPT PTEs into the comments.

---
 arch/x86/kvm/mmu/paging_tmpl.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/paging_tmpl.h +2 -2
index 2375bd5fd9f4..f4e02ba04744 100644
@@ -16,8 +16,8 @@
  */
 
 /*
- * We need the mmu code to access both 32-bit and 64-bit guest ptes,
- * so the code in this file is compiled twice, once per pte size.
+ * We need the MMU code to access 32-bit, 64-bit PTEs and 64-bit EPT PTEs,
+ * so the code in this file is compiled three times, once per a kind of PTE.
  */
 
 #if PTTYPE == 64
Signature · 1 line
2.19.1.6.gb485710b
#4 lore raw

PATCH 4/6 KVM: Rename ack_flush() to ack_kick()

In reply to Lai Jiangshan

From: Lai Jiangshan <[email protected]>

Make it use the same verb as in kvm_kick_many_cpus().

---
 virt/kvm/kvm_main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +2 -2
index e089db822c12..00c899393418 100644
@@ -235,7 +235,7 @@ static bool kvm_request_needs_ipi(struct kvm_vcpu *vcpu, unsigned req)
 	return mode == IN_GUEST_MODE;
 }
 
-static void ack_flush(void *_completed)
+static void ack_kick(void *_completed)
 {
 }
 
@@ -244,7 +244,7 @@ static inline bool kvm_kick_many_cpus(struct cpumask *cpus, bool wait)
 	if (cpumask_empty(cpus))
 		return false;
 
-	smp_call_function_many(cpus, ack_flush, NULL, wait);
+	smp_call_function_many(cpus, ack_kick, NULL, wait);
 	return true;
 }
 
Signature · 1 line
2.19.1.6.gb485710b
#5 lore raw

PATCH 5/6 KVM: X86/MMU: Remove useless mmu_topup_memory_caches() in kvm_mmu_pte_write()

In reply to Lai Jiangshan

From: Lai Jiangshan <[email protected]>

Since the commit c5e2184d1544("KVM: x86/mmu: Remove the defunct update_pte() paging hook"), kvm_mmu_pte_write() no longer uses the rmap cache.

So remove mmu_topup_memory_caches() in it.

Cc Sean Christopherson <[email protected]>
---
 arch/x86/kvm/mmu/mmu.c | 7 -------
 1 file changed, 7 deletions(-)
arch/x86/kvm/mmu/mmu.c +0 -7
index c935fdfc2544..086f32dffdbe 100644
@@ -5326,13 +5326,6 @@ static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
 
 	pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
 
-	/*
-	 * No need to care whether allocation memory is successful
-	 * or not since pte prefetch is skipped if it does not have
-	 * enough objects in the cache.
-	 */
-	mmu_topup_memory_caches(vcpu, true);
-
 	write_lock(&vcpu->kvm->mmu_lock);
 
 	gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, &bytes);
Signature · 1 line
2.19.1.6.gb485710b
#6 lore raw

PATCH 6/6 KVM: X86/SVM: Use root_level in svm_load_mmu_pgd()

In reply to Lai Jiangshan

From: Lai Jiangshan <[email protected]>

The caller always call it with root_level = vcpu->arch.mmu->root_role.level.

---
 arch/x86/kvm/svm/svm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +1 -1
index 3b49337998ec..f45d11739314 100644
@@ -3951,7 +3951,7 @@ static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
 		hv_track_root_tdp(vcpu, root_hpa);
 
 		cr3 = vcpu->arch.cr3;
-	} else if (vcpu->arch.mmu->root_role.level >= PT64_ROOT_4LEVEL) {
+	} else if (root_level >= PT64_ROOT_4LEVEL) {
 		cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu);
 	} else {
 		/* PCID in the guest should be impossible with a 32-bit MMU. */
Signature · 1 line
2.19.1.6.gb485710b
#7 lore raw

Re: [PATCH 6/6] KVM: X86/SVM: Use root_level in svm_load_mmu_pgd()

Quoted 3 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

The caller always call it with root_level = vcpu->arch.mmu->root_role.level.

It'd be helpful to be more specific about the caller. e.g.

Use root_level in svm_load_mmu_pg() rather that looking up the root level in vcpu->arch.mmu->root_role.level. svm_load_mmu_pgd() has only one caller, kvm_mmu_load_pgd(), which always passes vcpu->arch.mmu->root_role.level as root_level.

No functional change intended.

Quoted 22 lines
---
 arch/x86/kvm/svm/svm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +1 -1
index 3b49337998ec..f45d11739314 100644
@@ -3951,7 +3951,7 @@ static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
 		hv_track_root_tdp(vcpu, root_hpa);
 
 		cr3 = vcpu->arch.cr3;
-	} else if (vcpu->arch.mmu->root_role.level >= PT64_ROOT_4LEVEL) {
+	} else if (root_level >= PT64_ROOT_4LEVEL) {
 		cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu);
 	} else {
 		/* PCID in the guest should be impossible with a 32-bit MMU. */
Signature · 1 line
2.19.1.6.gb485710b
Reviewed-by on the v1 series
#8 lore raw
In reply to Lai Jiangshan
Quoted 3 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

A small collection of trivial cleanups.

Nice cleanups. My only feedback is on the commit messages, which are a bit terse. Here's what I would recommend:

- Explain what the commit does in the first sentence/paragraph of the commit message and then explain why/background info.

- Include "No functional change intended." for commits that are expected to be no-ops. It's pretty obvious for most of these changes but it's still nice that have to convey your intent.

Commit messages aside:

Quoted 20 lines

Lai Jiangshan (6):
KVM: X86/MMU: Remove unused macros from paging_tmpl.h
KVM: X86/MMU: Remove unused PT32_DIR_BASE_ADDR_MASK from mmu.c
KVM: X86/MMU: Update comments in paging_tmpl.h for the kinds of guest
PTEs
KVM: Rename ack_flush() to ack_kick()
KVM: X86/MMU: Remove useless mmu_topup_memory_caches() in

    kvm_mmu_pte_write()

KVM: X86/SVM: Use root_level in svm_load_mmu_pgd()

 arch/x86/kvm/mmu/mmu.c         |  9 ---------
 arch/x86/kvm/mmu/paging_tmpl.h | 16 ++--------------
 arch/x86/kvm/svm/svm.c         |  2 +-
 virt/kvm/kvm_main.c            |  4 ++--
 4 files changed, 5 insertions(+), 26 deletions(-)
Signature · 1 line
2.19.1.6.gb485710b