diff mbox series

[12/18] KVM: MIPS/MMU: Convert to the gfn-based MMU notifier callbacks

Message ID 20210326021957.1424875-13-seanjc@google.com
State Not Applicable
Headers show
Series KVM: Consolidate and optimize MMU notifiers | expand

Commit Message

Sean Christopherson March 26, 2021, 2:19 a.m. UTC
Move MIPS to the gfn-based MMU notifier APIs, which do the hva->gfn
lookup in common code, and whose code is nearly identical to MIPS'
lookup.

No meaningful functional change intended, though the exact order of
operations is slightly different since the memslot lookups occur before
calling into arch code.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/mips/include/asm/kvm_host.h |  1 +
 arch/mips/kvm/mmu.c              | 97 ++++++--------------------------
 2 files changed, 17 insertions(+), 81 deletions(-)

Comments

Paolo Bonzini March 31, 2021, 7:41 a.m. UTC | #1
On 26/03/21 03:19, Sean Christopherson wrote:
> Move MIPS to the gfn-based MMU notifier APIs, which do the hva->gfn
> lookup in common code, and whose code is nearly identical to MIPS'
> lookup.
> 
> No meaningful functional change intended, though the exact order of
> operations is slightly different since the memslot lookups occur before
> calling into arch code.
> 
> Signed-off-by: Sean Christopherson <seanjc@google.com>

I'll post a couple patches to enable more coalescing of the flushes, but 
this particular patch is okay.

Paolo

> ---
>   arch/mips/include/asm/kvm_host.h |  1 +
>   arch/mips/kvm/mmu.c              | 97 ++++++--------------------------
>   2 files changed, 17 insertions(+), 81 deletions(-)
> 
> diff --git a/arch/mips/include/asm/kvm_host.h b/arch/mips/include/asm/kvm_host.h
> index feaa77036b67..374a3c8806e8 100644
> --- a/arch/mips/include/asm/kvm_host.h
> +++ b/arch/mips/include/asm/kvm_host.h
> @@ -967,6 +967,7 @@ enum kvm_mips_fault_result kvm_trap_emul_gva_fault(struct kvm_vcpu *vcpu,
>   						   bool write);
>   
>   #define KVM_ARCH_WANT_MMU_NOTIFIER
> +#define KVM_ARCH_WANT_NEW_MMU_NOTIFIER_APIS
>   
>   /* Emulation */
>   int kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu, u32 *out);
> diff --git a/arch/mips/kvm/mmu.c b/arch/mips/kvm/mmu.c
> index 3dabeda82458..3dc885df2e32 100644
> --- a/arch/mips/kvm/mmu.c
> +++ b/arch/mips/kvm/mmu.c
> @@ -439,85 +439,36 @@ static int kvm_mips_mkold_gpa_pt(struct kvm *kvm, gfn_t start_gfn,
>   				  end_gfn << PAGE_SHIFT);
>   }
>   
> -static int handle_hva_to_gpa(struct kvm *kvm,
> -			     unsigned long start,
> -			     unsigned long end,
> -			     int (*handler)(struct kvm *kvm, gfn_t gfn,
> -					    gpa_t gfn_end,
> -					    struct kvm_memory_slot *memslot,
> -					    void *data),
> -			     void *data)
> +bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
>   {
> -	struct kvm_memslots *slots;
> -	struct kvm_memory_slot *memslot;
> -	int ret = 0;
> -
> -	slots = kvm_memslots(kvm);
> -
> -	/* we only care about the pages that the guest sees */
> -	kvm_for_each_memslot(memslot, slots) {
> -		unsigned long hva_start, hva_end;
> -		gfn_t gfn, gfn_end;
> -
> -		hva_start = max(start, memslot->userspace_addr);
> -		hva_end = min(end, memslot->userspace_addr +
> -					(memslot->npages << PAGE_SHIFT));
> -		if (hva_start >= hva_end)
> -			continue;
> -
> -		/*
> -		 * {gfn(page) | page intersects with [hva_start, hva_end)} =
> -		 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
> -		 */
> -		gfn = hva_to_gfn_memslot(hva_start, memslot);
> -		gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
> -
> -		ret |= handler(kvm, gfn, gfn_end, memslot, data);
> -	}
> -
> -	return ret;
> -}
> -
> -
> -static int kvm_unmap_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
> -				 struct kvm_memory_slot *memslot, void *data)
> -{
> -	kvm_mips_flush_gpa_pt(kvm, gfn, gfn_end);
> -	return 1;
> -}
> -
> -int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end,
> -			unsigned flags)
> -{
> -	handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
> +	kvm_mips_flush_gpa_pt(kvm, range->start, range->end);
>   
>   	kvm_mips_callbacks->flush_shadow_all(kvm);
>   	return 0;
>   }
>   
> -static int kvm_set_spte_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
> -				struct kvm_memory_slot *memslot, void *data)
> +static bool __kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
>   {
> -	gpa_t gpa = gfn << PAGE_SHIFT;
> -	pte_t hva_pte = *(pte_t *)data;
> +	gpa_t gpa = range->start << PAGE_SHIFT;
> +	pte_t hva_pte = range->pte;
>   	pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
>   	pte_t old_pte;
>   
>   	if (!gpa_pte)
> -		return 0;
> +		return false;
>   
>   	/* Mapping may need adjusting depending on memslot flags */
>   	old_pte = *gpa_pte;
> -	if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES && !pte_dirty(old_pte))
> +	if (range->slot->flags & KVM_MEM_LOG_DIRTY_PAGES && !pte_dirty(old_pte))
>   		hva_pte = pte_mkclean(hva_pte);
> -	else if (memslot->flags & KVM_MEM_READONLY)
> +	else if (range->slot->flags & KVM_MEM_READONLY)
>   		hva_pte = pte_wrprotect(hva_pte);
>   
>   	set_pte(gpa_pte, hva_pte);
>   
>   	/* Replacing an absent or old page doesn't need flushes */
>   	if (!pte_present(old_pte) || !pte_young(old_pte))
> -		return 0;
> +		return false;
>   
>   	/* Pages swapped, aged, moved, or cleaned require flushes */
>   	return !pte_present(hva_pte) ||
> @@ -526,27 +477,21 @@ static int kvm_set_spte_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
>   	       (pte_dirty(old_pte) && !pte_dirty(hva_pte));
>   }
>   
> -int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
> +bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
>   {
> -	unsigned long end = hva + PAGE_SIZE;
> -	int ret;
> -
> -	ret = handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &pte);
> -	if (ret)
> +	if (__kvm_set_spte_gfn(kvm, range))
>   		kvm_mips_callbacks->flush_shadow_all(kvm);
> -	return 0;
> +	return false;
>   }
>   
> -static int kvm_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
> -			       struct kvm_memory_slot *memslot, void *data)
> +bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
>   {
> -	return kvm_mips_mkold_gpa_pt(kvm, gfn, gfn_end);
> +	return kvm_mips_mkold_gpa_pt(kvm, range->start, range->end);
>   }
>   
> -static int kvm_test_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
> -				    struct kvm_memory_slot *memslot, void *data)
> +bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
>   {
> -	gpa_t gpa = gfn << PAGE_SHIFT;
> +	gpa_t gpa = range->start << PAGE_SHIFT;
>   	pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
>   
>   	if (!gpa_pte)
> @@ -554,16 +499,6 @@ static int kvm_test_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
>   	return pte_young(*gpa_pte);
>   }
>   
> -int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
> -{
> -	return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
> -}
> -
> -int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
> -{
> -	return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
> -}
> -
>   /**
>    * _kvm_mips_map_page_fast() - Fast path GPA fault handler.
>    * @vcpu:		VCPU pointer.
>
diff mbox series

Patch

diff --git a/arch/mips/include/asm/kvm_host.h b/arch/mips/include/asm/kvm_host.h
index feaa77036b67..374a3c8806e8 100644
--- a/arch/mips/include/asm/kvm_host.h
+++ b/arch/mips/include/asm/kvm_host.h
@@ -967,6 +967,7 @@  enum kvm_mips_fault_result kvm_trap_emul_gva_fault(struct kvm_vcpu *vcpu,
 						   bool write);
 
 #define KVM_ARCH_WANT_MMU_NOTIFIER
+#define KVM_ARCH_WANT_NEW_MMU_NOTIFIER_APIS
 
 /* Emulation */
 int kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu, u32 *out);
diff --git a/arch/mips/kvm/mmu.c b/arch/mips/kvm/mmu.c
index 3dabeda82458..3dc885df2e32 100644
--- a/arch/mips/kvm/mmu.c
+++ b/arch/mips/kvm/mmu.c
@@ -439,85 +439,36 @@  static int kvm_mips_mkold_gpa_pt(struct kvm *kvm, gfn_t start_gfn,
 				  end_gfn << PAGE_SHIFT);
 }
 
-static int handle_hva_to_gpa(struct kvm *kvm,
-			     unsigned long start,
-			     unsigned long end,
-			     int (*handler)(struct kvm *kvm, gfn_t gfn,
-					    gpa_t gfn_end,
-					    struct kvm_memory_slot *memslot,
-					    void *data),
-			     void *data)
+bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 {
-	struct kvm_memslots *slots;
-	struct kvm_memory_slot *memslot;
-	int ret = 0;
-
-	slots = kvm_memslots(kvm);
-
-	/* we only care about the pages that the guest sees */
-	kvm_for_each_memslot(memslot, slots) {
-		unsigned long hva_start, hva_end;
-		gfn_t gfn, gfn_end;
-
-		hva_start = max(start, memslot->userspace_addr);
-		hva_end = min(end, memslot->userspace_addr +
-					(memslot->npages << PAGE_SHIFT));
-		if (hva_start >= hva_end)
-			continue;
-
-		/*
-		 * {gfn(page) | page intersects with [hva_start, hva_end)} =
-		 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
-		 */
-		gfn = hva_to_gfn_memslot(hva_start, memslot);
-		gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
-
-		ret |= handler(kvm, gfn, gfn_end, memslot, data);
-	}
-
-	return ret;
-}
-
-
-static int kvm_unmap_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
-				 struct kvm_memory_slot *memslot, void *data)
-{
-	kvm_mips_flush_gpa_pt(kvm, gfn, gfn_end);
-	return 1;
-}
-
-int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end,
-			unsigned flags)
-{
-	handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
+	kvm_mips_flush_gpa_pt(kvm, range->start, range->end);
 
 	kvm_mips_callbacks->flush_shadow_all(kvm);
 	return 0;
 }
 
-static int kvm_set_spte_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
-				struct kvm_memory_slot *memslot, void *data)
+static bool __kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
 {
-	gpa_t gpa = gfn << PAGE_SHIFT;
-	pte_t hva_pte = *(pte_t *)data;
+	gpa_t gpa = range->start << PAGE_SHIFT;
+	pte_t hva_pte = range->pte;
 	pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
 	pte_t old_pte;
 
 	if (!gpa_pte)
-		return 0;
+		return false;
 
 	/* Mapping may need adjusting depending on memslot flags */
 	old_pte = *gpa_pte;
-	if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES && !pte_dirty(old_pte))
+	if (range->slot->flags & KVM_MEM_LOG_DIRTY_PAGES && !pte_dirty(old_pte))
 		hva_pte = pte_mkclean(hva_pte);
-	else if (memslot->flags & KVM_MEM_READONLY)
+	else if (range->slot->flags & KVM_MEM_READONLY)
 		hva_pte = pte_wrprotect(hva_pte);
 
 	set_pte(gpa_pte, hva_pte);
 
 	/* Replacing an absent or old page doesn't need flushes */
 	if (!pte_present(old_pte) || !pte_young(old_pte))
-		return 0;
+		return false;
 
 	/* Pages swapped, aged, moved, or cleaned require flushes */
 	return !pte_present(hva_pte) ||
@@ -526,27 +477,21 @@  static int kvm_set_spte_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
 	       (pte_dirty(old_pte) && !pte_dirty(hva_pte));
 }
 
-int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
+bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
 {
-	unsigned long end = hva + PAGE_SIZE;
-	int ret;
-
-	ret = handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &pte);
-	if (ret)
+	if (__kvm_set_spte_gfn(kvm, range))
 		kvm_mips_callbacks->flush_shadow_all(kvm);
-	return 0;
+	return false;
 }
 
-static int kvm_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
-			       struct kvm_memory_slot *memslot, void *data)
+bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
 {
-	return kvm_mips_mkold_gpa_pt(kvm, gfn, gfn_end);
+	return kvm_mips_mkold_gpa_pt(kvm, range->start, range->end);
 }
 
-static int kvm_test_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
-				    struct kvm_memory_slot *memslot, void *data)
+bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
 {
-	gpa_t gpa = gfn << PAGE_SHIFT;
+	gpa_t gpa = range->start << PAGE_SHIFT;
 	pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
 
 	if (!gpa_pte)
@@ -554,16 +499,6 @@  static int kvm_test_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
 	return pte_young(*gpa_pte);
 }
 
-int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
-{
-	return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
-}
-
-int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
-{
-	return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
-}
-
 /**
  * _kvm_mips_map_page_fast() - Fast path GPA fault handler.
  * @vcpu:		VCPU pointer.