Skip to contentSkip to conversation

KVM: arm64: Minor page fault handler improvement

PATCH Superseded Gavin Shan 3 participants 17 messages Mar 15, 2021 – Mar 16, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

cover KVM: arm64: Minor page fault handler improvement

The series includes several minior improvements to stage-2 page fault
handler: PATCH[1/2] are cleaning up the code. PATCH[3] fixes the address range check on adding new memory slot. PATCH[4] don't retrieve the memory slot again in the page fault handler to save a bit CPU cycles.

Gavin Shan (4):
KVM: arm64: Hide kvm_mmu_wp_memory_region()
KVM: arm64: Use find_vma_intersection()
KVM: arm64: Fix address check for memory slot
KVM: arm64: Don't retrieve memory slot again in page fault handler

 arch/arm64/include/asm/kvm_host.h |  1 -
 arch/arm64/kvm/mmu.c              | 19 +++++++++++--------
 2 files changed, 11 insertions(+), 9 deletions(-)
Signature · 6 lines
2.23.0

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
author
#1 lore raw

PATCH 1/4 KVM: arm64: Hide kvm_mmu_wp_memory_region()

We needn't expose the function as it's only used by mmu.c.

---
 arch/arm64/include/asm/kvm_host.h | 1 -
 arch/arm64/kvm/mmu.c              | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -1
index 3d10e6527f7d..688f2df1957b 100644
@@ -632,7 +632,6 @@ void kvm_arm_resume_guest(struct kvm *kvm);
 	})
 
 void force_vm_exit(const cpumask_t *mask);
-void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot);
 
 int handle_exit(struct kvm_vcpu *vcpu, int exception_index);
 void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index);
arch/arm64/kvm/mmu.c +1 -1
index 77cb2d28f2a4..84e70f953de6 100644
@@ -555,7 +555,7 @@ static void stage2_wp_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_
  * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
  * serializing operations for VM memory regions.
  */
-void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
+static void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
 {
 	struct kvm_memslots *slots = kvm_memslots(kvm);
 	struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
Signature · 6 lines
2.23.0

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] KVM: arm64: Hide kvm_mmu_wp_memory_region()

Hi Gavin,

This function is only used by mmu.c in the first commit c64735554c0a, so please feel free to add:

Thanks,
Keqian

Quoted 26 lines · Gavin Shan

We needn't expose the function as it's only used by mmu.c.

---
 arch/arm64/include/asm/kvm_host.h | 1 -
 arch/arm64/kvm/mmu.c              | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -1
index 3d10e6527f7d..688f2df1957b 100644
@@ -632,7 +632,6 @@ void kvm_arm_resume_guest(struct kvm *kvm);
 	})
 
 void force_vm_exit(const cpumask_t *mask);
-void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot);
 
 int handle_exit(struct kvm_vcpu *vcpu, int exception_index);
 void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index);
arch/arm64/kvm/mmu.c +0 -0
index 77cb2d28f2a4..84e70f953de6 100644

arch/arm64/kvm/mmu.c static void stage2_wp_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_ · line 561

Quoted 8 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -555,7 +555,7 @@ static void stage2_wp_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_
  * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
  * serializing operations for VM memory regions.
  */
-void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
+static void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
 {
 	struct kvm_memslots *slots = kvm_memslots(kvm);
 	struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

author
#3 lore raw

PATCH 2/4 KVM: arm64: Use find_vma_intersection()

In reply to Gavin Shan

find_vma_intersection() has been existing to search the intersected vma. This uses the function where it's applicable, to simplify the code.

---
 arch/arm64/kvm/mmu.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +6 -4
index 84e70f953de6..286b603ed0d3 100644
@@ -421,10 +421,11 @@ static void stage2_unmap_memslot(struct kvm *kvm,
 	 *     +--------------------------------------------+
 	 */
 	do {
-		struct vm_area_struct *vma = find_vma(current->mm, hva);
+		struct vm_area_struct *vma;
 		hva_t vm_start, vm_end;
 
-		if (!vma || vma->vm_start >= reg_end)
+		vma = find_vma_intersection(current->mm, hva, reg_end);
+		if (!vma)
 			break;
 
 		/*
@@ -1330,10 +1331,11 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 	 *     +--------------------------------------------+
 	 */
 	do {
-		struct vm_area_struct *vma = find_vma(current->mm, hva);
+		struct vm_area_struct *vma;
 		hva_t vm_start, vm_end;
 
-		if (!vma || vma->vm_start >= reg_end)
+		vma = find_vma_intersection(current->mm, hva, reg_end);
+		if (!vma)
 			break;
 
 		/*
Signature · 6 lines
2.23.0

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#4 lore raw

Re: [PATCH 2/4] KVM: arm64: Use find_vma_intersection()

Hi Gavin,

Quoted 14 lines · Gavin Shan

find_vma_intersection() has been existing to search the intersected vma. This uses the function where it's applicable, to simplify the code.

---
 arch/arm64/kvm/mmu.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +0 -0
index 84e70f953de6..286b603ed0d3 100644

arch/arm64/kvm/mmu.c static void stage2_unmap_memslot(struct kvm *kvm, · line 427

Quoted 9 lines
arch/arm64/kvm/mmu.c +2 -2
@@ -421,10 +421,11 @@ static void stage2_unmap_memslot(struct kvm *kvm,
 	 *     +--------------------------------------------+
 	 */
 	do {
-		struct vm_area_struct *vma = find_vma(current->mm, hva);
+		struct vm_area_struct *vma;
 		hva_t vm_start, vm_end;
 
-		if (!vma || vma->vm_start >= reg_end)
+		vma = find_vma_intersection(current->mm, hva, reg_end);

Nit: Keep a same style may be better(Assign vma when declare it). Other looks good to me.

Thank,
Keqian

Quoted 9 lines
arch/arm64/kvm/mmu.c +1 -0
+		if (!vma)
 			break;
 
 		/*
@@ -1330,10 +1331,11 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 	 *     +--------------------------------------------+
 	 */
 	do {

arch/arm64/kvm/mmu.c int kvm_arch_prepare_memory_region(struct kvm *kvm, · line 1341

Quoted 10 lines
arch/arm64/kvm/mmu.c +3 -2
-		struct vm_area_struct *vma = find_vma(current->mm, hva);
+		struct vm_area_struct *vma;
 		hva_t vm_start, vm_end;
 
-		if (!vma || vma->vm_start >= reg_end)
+		vma = find_vma_intersection(current->mm, hva, reg_end);
+		if (!vma)
 			break;
 
 		/*

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

author
#5 lore raw

Re: [PATCH 2/4] KVM: arm64: Use find_vma_intersection()

Hi Keqian,

Quoted 22 lines · Keqian Zhu
Quoted 21 lines · Gavin Shan

find_vma_intersection() has been existing to search the intersected vma. This uses the function where it's applicable, to simplify the code.

---
  arch/arm64/kvm/mmu.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +1 -1
index 84e70f953de6..286b603ed0d3 100644
@@ -421,10 +421,11 @@ static void stage2_unmap_memslot(struct kvm *kvm,
  	 *     +--------------------------------------------+
  	 */
  	do {
-		struct vm_area_struct *vma = find_vma(current->mm, hva);
+		struct vm_area_struct *vma;
  		hva_t vm_start, vm_end;
 
Quoted 5 lines

arch/arm64/kvm/mmu.c static void stage2_unmap_memslot(struct kvm *kvm, · line 427

Quoted 2 lines
arch/arm64/kvm/mmu.c +1 -1
-		if (!vma || vma->vm_start >= reg_end)
+		vma = find_vma_intersection(current->mm, hva, reg_end);

Nit: Keep a same style may be better(Assign vma when declare it). Other looks good to me.

Yeah, I agree. I will adjust the code in v2 and included your r-b. Thanks for your time to review.

Thanks,
Gavin

Quoted 12 lines
Quoted 12 lines
arch/arm64/kvm/mmu.c +2 -1
+		if (!vma)
  			break;
 
  		/*
@@ -1330,10 +1331,11 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
  	 *     +--------------------------------------------+
  	 */
  	do {
-		struct vm_area_struct *vma = find_vma(current->mm, hva);
+		struct vm_area_struct *vma;
  		hva_t vm_start, vm_end;
 
Quoted 8 lines

arch/arm64/kvm/mmu.c static void stage2_unmap_memslot(struct kvm *kvm, · line 431

Quoted 7 lines
arch/arm64/kvm/mmu.c +2 -1
-		if (!vma || vma->vm_start >= reg_end)
+		vma = find_vma_intersection(current->mm, hva, reg_end);
+		if (!vma)
  			break;
 
  		/*

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

author
#6 lore raw

Re: [PATCH 2/4] KVM: arm64: Use find_vma_intersection()

Hi Keqian,

Quoted 28 lines · Gavin Shan
Quoted 10 lines · Keqian Zhu
Quoted 9 lines · Gavin Shan

find_vma_intersection() has been existing to search the intersected vma. This uses the function where it's applicable, to simplify the code.

---
  arch/arm64/kvm/mmu.c | 10 ++++++----

  1 file changed, 6 insertions(+), 4 deletions(-)
Quoted 16 lines

arch/arm64/kvm/mmu.c static void stage2_unmap_memslot(struct kvm *kvm, · line 426

Quoted 13 lines
arch/arm64/kvm/mmu.c +2 -2
index 84e70f953de6..286b603ed0d3 100644
@@ -421,10 +421,11 @@ static void stage2_unmap_memslot(struct kvm *kvm,
        *     +--------------------------------------------+
        */
       do {
-        struct vm_area_struct *vma = find_vma(current->mm, hva);
+        struct vm_area_struct *vma;
           hva_t vm_start, vm_end;
-        if (!vma || vma->vm_start >= reg_end)
+        vma = find_vma_intersection(current->mm, hva, reg_end);

Nit: Keep a same style may be better(Assign vma when declare it). Other looks good to me.

Quoted 3 lines

Yeah, I agree. I will adjust the code in v2 and included your r-b. Thanks for your time to review.

After rechecking the code, I think it'd better to keep current style because there is a follow-on validation on @vma. Keeping them together seems a good idea. I think it wouldn't a big deal to you. So I will keep current style with your r-b in v2.

	vma = find_vma_intersection(current->mm, hva, reg_end);
        if (!vma)
             break;

Thanks,
Gavin

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
arch/arm64/kvm/mmu.c +2 -1
+        if (!vma)
               break;
           /*
@@ -1330,10 +1331,11 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
        *     +--------------------------------------------+
        */
       do {
-        struct vm_area_struct *vma = find_vma(current->mm, hva);
+        struct vm_area_struct *vma;
Quoted 9 lines
Quoted 8 lines

arch/arm64/kvm/mmu.c static void stage2_unmap_memslot(struct kvm *kvm,

Quoted 7 lines
arch/arm64/kvm/mmu.c +2 -1
           hva_t vm_start, vm_end;
-        if (!vma || vma->vm_start >= reg_end)
+        vma = find_vma_intersection(current->mm, hva, reg_end);
+        if (!vma)
               break;
           /*
 

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#7 lore raw

Re: [PATCH 2/4] KVM: arm64: Use find_vma_intersection()

Hi Gavin,

Quoted 31 lines · Gavin Shan

Hi Keqian,

Quoted 28 lines · Gavin Shan
Quoted 26 lines · Keqian Zhu
Quoted 14 lines · Gavin Shan

find_vma_intersection() has been existing to search the intersected vma. This uses the function where it's applicable, to simplify the code.

---
  arch/arm64/kvm/mmu.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +0 -0
index 84e70f953de6..286b603ed0d3 100644

arch/arm64/kvm/mmu.c static void stage2_unmap_memslot(struct kvm *kvm, · line 426

Quoted 8 lines
arch/arm64/kvm/mmu.c +2 -2
@@ -421,10 +421,11 @@ static void stage2_unmap_memslot(struct kvm *kvm,
       *     +--------------------------------------------+
       */
      do {
-        struct vm_area_struct *vma = find_vma(current->mm, hva);
+        struct vm_area_struct *vma;
          hva_t vm_start, vm_end;
-        if (!vma || vma->vm_start >= reg_end)
+        vma = find_vma_intersection(current->mm, hva, reg_end);

Nit: Keep a same style may be better(Assign vma when declare it). Other looks good to me.

Quoted 8 lines
Quoted 3 lines

Yeah, I agree. I will adjust the code in v2 and included your r-b. Thanks for your time to review.

After rechecking the code, I think it'd better to keep current style because there is a follow-on validation on @vma. Keeping them together seems a good idea. I think it wouldn't a big deal to you. So I will keep current style with your r-b in v2.

Sure, both is OK. ;-)

Thanks,
Keqian

Quoted 26 lines
    vma = find_vma_intersection(current->mm, hva, reg_end);
        if (!vma)
             break;

Thanks,
Gavin

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
arch/arm64/kvm/mmu.c +2 -1
+        if (!vma)
              break;
          /*
@@ -1330,10 +1331,11 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
       *     +--------------------------------------------+
       */
      do {
-        struct vm_area_struct *vma = find_vma(current->mm, hva);
+        struct vm_area_struct *vma;
Quoted 9 lines
Quoted 8 lines

arch/arm64/kvm/mmu.c static void stage2_unmap_memslot(struct kvm *kvm,

Quoted 7 lines
arch/arm64/kvm/mmu.c +2 -1
          hva_t vm_start, vm_end;
-        if (!vma || vma->vm_start >= reg_end)
+        vma = find_vma_intersection(current->mm, hva, reg_end);
+        if (!vma)
              break;
          /*
 
Quoted 2 lines

.

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#8 lore raw

Re: [PATCH 2/4] KVM: arm64: Use find_vma_intersection()

In reply to Gavin Shan
Quoted 15 lines · Gavin Shan

find_vma_intersection() has been existing to search the intersected vma. This uses the function where it's applicable, to simplify the code.

---
 arch/arm64/kvm/mmu.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +0 -0
index 84e70f953de6..286b603ed0d3 100644

arch/arm64/kvm/mmu.c static void stage2_unmap_memslot(struct kvm *kvm, · line 427

Quoted 9 lines
arch/arm64/kvm/mmu.c +2 -2
@@ -421,10 +421,11 @@ static void stage2_unmap_memslot(struct kvm *kvm,
 	 *     +--------------------------------------------+
 	 */
 	do {
-		struct vm_area_struct *vma = find_vma(current->mm, hva);
+		struct vm_area_struct *vma;
 		hva_t vm_start, vm_end;
 
-		if (!vma || vma->vm_start >= reg_end)
+		vma = find_vma_intersection(current->mm, hva, reg_end);

For context, here's the definition of find_vma_intersection():

<quote>
static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)

{
	struct vm_area_struct * vma = find_vma(mm,start_addr);

	if (vma && end_addr <= vma->vm_start)
		vma = NULL;
	return vma;
}

</quote>

It seems that there is a boundary issue in either the old code or the new one in the case where (reg_end == vma->start).

Which one is which?

	M.
Signature · 5 lines
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
author
#9 lore raw

Re: [PATCH 2/4] KVM: arm64: Use find_vma_intersection()

Hi Marc,

Quoted 38 lines · Marc Zyngier
Quoted 15 lines · Gavin Shan

find_vma_intersection() has been existing to search the intersected vma. This uses the function where it's applicable, to simplify the code.

---
  arch/arm64/kvm/mmu.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +0 -0
index 84e70f953de6..286b603ed0d3 100644

arch/arm64/kvm/mmu.c static void stage2_unmap_memslot(struct kvm *kvm, · line 427

Quoted 9 lines
arch/arm64/kvm/mmu.c +2 -2
@@ -421,10 +421,11 @@ static void stage2_unmap_memslot(struct kvm *kvm,
  	 *     +--------------------------------------------+
  	 */
  	do {
-		struct vm_area_struct *vma = find_vma(current->mm, hva);
+		struct vm_area_struct *vma;
  		hva_t vm_start, vm_end;
 
-		if (!vma || vma->vm_start >= reg_end)
+		vma = find_vma_intersection(current->mm, hva, reg_end);

For context, here's the definition of find_vma_intersection():

<quote>
static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)

{
	struct vm_area_struct * vma = find_vma(mm,start_addr);

	if (vma && end_addr <= vma->vm_start)
		vma = NULL;
	return vma;
}
Quoted 7 lines

</quote>

It seems that there is a boundary issue in either the old code or the new one in the case where (reg_end == vma->start).

Which one is which?

The old and new code is interchangeable, meaning "reg_end == vma->start" is invalid in both cases. So if there is a boundary issue, the old and new code should have same issue.

According to the code, "reg_end == vma->start" is invalid. So I don't see there is a boundary issue. Hopefully, I don't miss anything :)

Thanks,
Gavin

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

author
#10 lore raw

PATCH 3/4 KVM: arm64: Fix address check for memory slot

In reply to Gavin Shan

The last (IPA) page can't be specified when a new memory slot is added. The error -EFAULT is returned when the memory slot is added with the following parameters for the VM, which has 40-bits IPA limit. The host has 4KB base page size. It's not correct because the last (IPA) page is still usable.

   struct kvm_userspace_memory_region {
      __u32 slot;               /* 1            */
      __u32 flags;              /* 0            */
      __u64 guest_phys_addr;    /* 0xfffffff000 */
      __u64 memory_size;        /* 0x1000       */
      __u64 userspace_addr;
   };
---
 arch/arm64/kvm/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/mmu.c +1 -1
index 286b603ed0d3..a5a8ade9fde4 100644
@@ -1313,7 +1313,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 	 * Prevent userspace from creating a memory region outside of the IPA
 	 * space addressable by the KVM guest IPA space.
 	 */
-	if (memslot->base_gfn + memslot->npages >=
+	if (memslot->base_gfn + memslot->npages >
 	    (kvm_phys_size(kvm) >> PAGE_SHIFT))
 		return -EFAULT;
 
Signature · 6 lines
2.23.0

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#11 lore raw

Re: [PATCH 3/4] KVM: arm64: Fix address check for memory slot

Hi Gavin,

FYI, this has been fixed by Marc in commit 262b003d059c.

Thanks,
Keqian

Quoted 25 lines · Gavin Shan

The last (IPA) page can't be specified when a new memory slot is added. The error -EFAULT is returned when the memory slot is added with the following parameters for the VM, which has 40-bits IPA limit. The host has 4KB base page size. It's not correct because the last (IPA) page is still usable.

   struct kvm_userspace_memory_region {
      __u32 slot;               /* 1            */
      __u32 flags;              /* 0            */
      __u64 guest_phys_addr;    /* 0xfffffff000 */
      __u64 memory_size;        /* 0x1000       */
      __u64 userspace_addr;
   };
---
 arch/arm64/kvm/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/mmu.c +0 -0
index 286b603ed0d3..a5a8ade9fde4 100644

arch/arm64/kvm/mmu.c int kvm_arch_prepare_memory_region(struct kvm *kvm, · line 1319

Quoted 8 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -1313,7 +1313,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 	 * Prevent userspace from creating a memory region outside of the IPA
 	 * space addressable by the KVM guest IPA space.
 	 */
-	if (memslot->base_gfn + memslot->npages >=
+	if (memslot->base_gfn + memslot->npages >
 	    (kvm_phys_size(kvm) >> PAGE_SHIFT))
 		return -EFAULT;
 

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

author
#12 lore raw

Re: [PATCH 3/4] KVM: arm64: Fix address check for memory slot

Hi Keqian,

Quoted 2 lines · Keqian Zhu

FYI, this has been fixed by Marc in commit 262b003d059c.

Yeah, I didn't check 5.12.rc3 code where the issue has been fixed. So please ignore this one and sorry for the noise.

Thanks,
Gavin

Quoted 20 lines · Gavin Shan
Quoted 19 lines

The last (IPA) page can't be specified when a new memory slot is added. The error -EFAULT is returned when the memory slot is added with the following parameters for the VM, which has 40-bits IPA limit. The host has 4KB base page size. It's not correct because the last (IPA) page is still usable.

    struct kvm_userspace_memory_region {
       __u32 slot;               /* 1            */
       __u32 flags;              /* 0            */
       __u64 guest_phys_addr;    /* 0xfffffff000 */
       __u64 memory_size;        /* 0x1000       */
       __u64 userspace_addr;
    };
---
  arch/arm64/kvm/mmu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
Quoted 6 lines
arch/arm64/kvm/mmu.c +0 -0
index 286b603ed0d3..a5a8ade9fde4 100644

arch/arm64/kvm/mmu.c int kvm_arch_prepare_memory_region(struct kvm *kvm, · line 1319

Quoted 8 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -1313,7 +1313,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
  	 * Prevent userspace from creating a memory region outside of the IPA
  	 * space addressable by the KVM guest IPA space.
  	 */
-	if (memslot->base_gfn + memslot->npages >=
+	if (memslot->base_gfn + memslot->npages >
  	    (kvm_phys_size(kvm) >> PAGE_SHIFT))
  		return -EFAULT;
 

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

author
#13 lore raw

PATCH 4/4 KVM: arm64: Don't retrieve memory slot again in page fault handler

In reply to Gavin Shan

We needn't retrieve the memory slot again in user_mem_abort() because the corresponding memory slot has been passed from the caller. This would save some CPU cycles. For example, the time used to write 1GB memory, which is backed by 2MB hugetlb pages and write-protected, is dropped by 6.8% from 928ms to 864ms.

---
 arch/arm64/kvm/mmu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/arm64/kvm/mmu.c +3 -2
index a5a8ade9fde4..4a4abcccfafb 100644
@@ -846,7 +846,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	 */
 	smp_rmb();
 
-	pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
+	pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL,
+				   write_fault, &writable, NULL);
 	if (pfn == KVM_PFN_ERR_HWPOISON) {
 		kvm_send_hwpoison_signal(hva, vma_shift);
 		return 0;
@@ -912,7 +913,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	/* Mark the page dirty only if the fault is handled successfully */
 	if (writable && !ret) {
 		kvm_set_pfn_dirty(pfn);
-		mark_page_dirty(kvm, gfn);
+		mark_page_dirty_in_slot(kvm, memslot, gfn);
 	}
 
 out_unlock:
Signature · 6 lines
2.23.0

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#14 lore raw

Re: [PATCH 4/4] KVM: arm64: Don't retrieve memory slot again in page fault handler

Hi Gavin,

Quoted 2 lines · Gavin Shan

We needn't retrieve the memory slot again in user_mem_abort() because the corresponding memory slot has been passed from the caller. This

I think you are right, though fault_ipa will be adjusted when we try to use block mapping, the fault_supports_stage2_huge_mapping() makes sure we're not trying to map anything not covered by the memslot, so the adjusted fault_ipa still belongs to the memslot.

Quoted 14 lines

would save some CPU cycles. For example, the time used to write 1GB memory, which is backed by 2MB hugetlb pages and write-protected, is dropped by 6.8% from 928ms to 864ms.

---
 arch/arm64/kvm/mmu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/arm64/kvm/mmu.c +0 -0
index a5a8ade9fde4..4a4abcccfafb 100644

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 850

Quoted 6 lines
arch/arm64/kvm/mmu.c +2 -1
@@ -846,7 +846,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	 */
 	smp_rmb();
 
-	pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
+	pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL,
+				   write_fault, &writable, NULL);

It's better to update the code comments at same time.

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 919

Quoted 13 lines
arch/arm64/kvm/mmu.c +1 -1
 	if (pfn == KVM_PFN_ERR_HWPOISON) {
 		kvm_send_hwpoison_signal(hva, vma_shift);
 		return 0;
@@ -912,7 +913,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	/* Mark the page dirty only if the fault is handled successfully */
 	if (writable && !ret) {
 		kvm_set_pfn_dirty(pfn);
-		mark_page_dirty(kvm, gfn);
+		mark_page_dirty_in_slot(kvm, memslot, gfn);
 	}
 
 out_unlock:

Thanks,
Keqian.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

author
#15 lore raw

Re: [PATCH 4/4] KVM: arm64: Don't retrieve memory slot again in page fault handler

Hi Keqian,

Quoted 7 lines · Keqian Zhu
Quoted 2 lines · Gavin Shan

We needn't retrieve the memory slot again in user_mem_abort() because the corresponding memory slot has been passed from the caller. This

I think you are right, though fault_ipa will be adjusted when we try to use block mapping, the fault_supports_stage2_huge_mapping() makes sure we're not trying to map anything not covered by the memslot, so the adjusted fault_ipa still belongs to the memslot.

Yeah, it's correct. Besides, the @logging_active is determined based on the passed memory slot. It means user_mem_abort() can't support memory range which spans multiple memory slot.

Quoted 17 lines
Quoted 17 lines

would save some CPU cycles. For example, the time used to write 1GB memory, which is backed by 2MB hugetlb pages and write-protected, is dropped by 6.8% from 928ms to 864ms.

---
  arch/arm64/kvm/mmu.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
arch/arm64/kvm/mmu.c +0 -0
index a5a8ade9fde4..4a4abcccfafb 100644
@@ -846,7 +846,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	 */
  	smp_rmb();
 
Quoted 5 lines

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 850

Quoted 3 lines
arch/arm64/kvm/mmu.c +2 -1
-	pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
+	pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL,
+				   write_fault, &writable, NULL);

It's better to update the code comments at same time.

I guess you need some comments here? If so, I would add something like below in v2:

/*
* gfn_to_pfn_prot() can be used either with unnecessary overhead
* introduced to locate the memory slot because the memory slot is
* always fixed even @gfn is adjusted for huge pages. */

Quoted 11 lines
Quoted 11 lines
arch/arm64/kvm/mmu.c +1 -1
  	if (pfn == KVM_PFN_ERR_HWPOISON) {
  		kvm_send_hwpoison_signal(hva, vma_shift);
  		return 0;
@@ -912,7 +913,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	/* Mark the page dirty only if the fault is handled successfully */
  	if (writable && !ret) {
  		kvm_set_pfn_dirty(pfn);
-		mark_page_dirty(kvm, gfn);
+		mark_page_dirty_in_slot(kvm, memslot, gfn);
  	}
 
Quoted 2 lines

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 919

Quoted 2 lines
arch/arm64/kvm/mmu.c +0 -0
  out_unlock:

Thanks,
Gavin

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#16 lore raw

Re: [PATCH 4/4] KVM: arm64: Don't retrieve memory slot again in page fault handler

Hi Gavin,

Quoted 37 lines · Gavin Shan

Hi Keqian,

Quoted 7 lines · Keqian Zhu
Quoted 2 lines · Gavin Shan

We needn't retrieve the memory slot again in user_mem_abort() because the corresponding memory slot has been passed from the caller. This

I think you are right, though fault_ipa will be adjusted when we try to use block mapping, the fault_supports_stage2_huge_mapping() makes sure we're not trying to map anything not covered by the memslot, so the adjusted fault_ipa still belongs to the memslot.

Yeah, it's correct. Besides, the @logging_active is determined based on the passed memory slot. It means user_mem_abort() can't support memory range which spans multiple memory slot.

Quoted 9 lines
Quoted 9 lines

would save some CPU cycles. For example, the time used to write 1GB memory, which is backed by 2MB hugetlb pages and write-protected, is dropped by 6.8% from 928ms to 864ms.

---
  arch/arm64/kvm/mmu.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 12 lines

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 850

Quoted 10 lines
arch/arm64/kvm/mmu.c +2 -0
index a5a8ade9fde4..4a4abcccfafb 100644
@@ -846,7 +846,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
       */
      smp_rmb();
  -    pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
+    pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL,
+                   write_fault, &writable, NULL);

It's better to update the code comments at same time.

Quoted 8 lines

I guess you need some comments here? If so, I would add something like below in v2:

/*
* gfn_to_pfn_prot() can be used either with unnecessary overhead
* introduced to locate the memory slot because the memory slot is
* always fixed even @gfn is adjusted for huge pages. */

Looks good.

See comments above "smp_rmb();", and actually my meaning is that we should change "gfn_to_pfn_prot" to "__gfn_to_pfn_memslot" :)

Thanks,
Keqian

Quoted 18 lines
Quoted 13 lines

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 919

Quoted 13 lines
arch/arm64/kvm/mmu.c +1 -1
      if (pfn == KVM_PFN_ERR_HWPOISON) {
          kvm_send_hwpoison_signal(hva, vma_shift);
          return 0;
@@ -912,7 +913,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
      /* Mark the page dirty only if the fault is handled successfully */
      if (writable && !ret) {
          kvm_set_pfn_dirty(pfn);
-        mark_page_dirty(kvm, gfn);
+        mark_page_dirty_in_slot(kvm, memslot, gfn);
      }
    out_unlock:
 

Thanks,
Gavin

Quoted 2 lines

.

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm