Skip to contentSkip to conversation

KVM: s390: Some gaccess cleanup

PATCH Superseded Janis Schoetterl-Glausch 5 participants 13 messages Aug 16, 2021 – Aug 19, 2021

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover KVM: s390: Some gaccess cleanup

Cleanup s390 guest access code a bit, getting rid of some code duplication and improving readability.

Janis Schoetterl-Glausch (2):
KVM: s390: gaccess: Cleanup access to guest frames
KVM: s390: gaccess: Refactor access address range check

 arch/s390/kvm/gaccess.c | 129 ++++++++++++++++++++--------------------
 1 file changed, 63 insertions(+), 66 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/2 KVM: s390: gaccess: Cleanup access to guest frames

Introduce a helper function for guest frame access. Rewrite the calculation of the gpa and the length of the segment to be more readable.

---
 arch/s390/kvm/gaccess.c | 48 +++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 19 deletions(-)
arch/s390/kvm/gaccess.c +29 -19
index b9f85b2dc053..df83de0843de 100644
@@ -827,11 +827,26 @@ static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
 	return 0;
 }
 
+static int access_guest_frame(struct kvm *kvm, enum gacc_mode mode, gpa_t gpa,
+			      void *data, unsigned int len)
+{
+	gfn_t gfn = gpa_to_gfn(gpa);
+	unsigned int offset = offset_in_page(gpa);
+	int rc;
+
+	if (mode == GACC_STORE)
+		rc = kvm_write_guest_page(kvm, gfn, data, offset, len);
+	else
+		rc = kvm_read_guest_page(kvm, gfn, data, offset, len);
+	return rc;
+}
+
 int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
 		 unsigned long len, enum gacc_mode mode)
 {
 	psw_t *psw = &vcpu->arch.sie_block->gpsw;
-	unsigned long _len, nr_pages, gpa, idx;
+	unsigned long nr_pages, gpa, idx;
+	unsigned int seg;
 	unsigned long pages_array[2];
 	unsigned long *pages;
 	int need_ipte_lock;
@@ -855,15 +870,12 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
 		ipte_lock(vcpu);
 	rc = guest_page_range(vcpu, ga, ar, pages, nr_pages, asce, mode);
 	for (idx = 0; idx < nr_pages && !rc; idx++) {
-		gpa = *(pages + idx) + (ga & ~PAGE_MASK);
-		_len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-		if (mode == GACC_STORE)
-			rc = kvm_write_guest(vcpu->kvm, gpa, data, _len);
-		else
-			rc = kvm_read_guest(vcpu->kvm, gpa, data, _len);
-		len -= _len;
-		ga += _len;
-		data += _len;
+		gpa = pages[idx] + offset_in_page(ga);
+		seg = min(PAGE_SIZE - offset_in_page(gpa), len);
+		rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+		len -= seg;
+		ga += seg;
+		data += seg;
 	}
 	if (need_ipte_lock)
 		ipte_unlock(vcpu);
@@ -875,19 +887,17 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
 int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
 		      void *data, unsigned long len, enum gacc_mode mode)
 {
-	unsigned long _len, gpa;
+	unsigned long gpa;
+	unsigned int seg;
 	int rc = 0;
 
 	while (len && !rc) {
 		gpa = kvm_s390_real_to_abs(vcpu, gra);
-		_len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-		if (mode)
-			rc = write_guest_abs(vcpu, gpa, data, _len);
-		else
-			rc = read_guest_abs(vcpu, gpa, data, _len);
-		len -= _len;
-		gra += _len;
-		data += _len;
+		seg = min(PAGE_SIZE - offset_in_page(gpa), len);
+		rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+		len -= seg;
+		gra += seg;
+		data += seg;
 	}
 	return rc;
 }
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] KVM: s390: gaccess: Cleanup access to guest frames

Quoted 5 lines · Janis Schoetterl-Glausch

Introduce a helper function for guest frame access. Rewrite the calculation of the gpa and the length of the segment to be more readable.

Quoted 86 lines
---
 arch/s390/kvm/gaccess.c | 48 +++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 19 deletions(-)
arch/s390/kvm/gaccess.c +29 -19
index b9f85b2dc053..df83de0843de 100644
@@ -827,11 +827,26 @@ static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
 	return 0;
 }
 
+static int access_guest_frame(struct kvm *kvm, enum gacc_mode mode, gpa_t gpa,
+			      void *data, unsigned int len)
+{
+	gfn_t gfn = gpa_to_gfn(gpa);
+	unsigned int offset = offset_in_page(gpa);
+	int rc;
+
+	if (mode == GACC_STORE)
+		rc = kvm_write_guest_page(kvm, gfn, data, offset, len);
+	else
+		rc = kvm_read_guest_page(kvm, gfn, data, offset, len);
+	return rc;
+}
+
 int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
 		 unsigned long len, enum gacc_mode mode)
 {
 	psw_t *psw = &vcpu->arch.sie_block->gpsw;
-	unsigned long _len, nr_pages, gpa, idx;
+	unsigned long nr_pages, gpa, idx;
+	unsigned int seg;
 	unsigned long pages_array[2];
 	unsigned long *pages;
 	int need_ipte_lock;
@@ -855,15 +870,12 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
 		ipte_lock(vcpu);
 	rc = guest_page_range(vcpu, ga, ar, pages, nr_pages, asce, mode);
 	for (idx = 0; idx < nr_pages && !rc; idx++) {
-		gpa = *(pages + idx) + (ga & ~PAGE_MASK);
-		_len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-		if (mode == GACC_STORE)
-			rc = kvm_write_guest(vcpu->kvm, gpa, data, _len);
-		else
-			rc = kvm_read_guest(vcpu->kvm, gpa, data, _len);
-		len -= _len;
-		ga += _len;
-		data += _len;
+		gpa = pages[idx] + offset_in_page(ga);
+		seg = min(PAGE_SIZE - offset_in_page(gpa), len);
+		rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+		len -= seg;
+		ga += seg;
+		data += seg;
 	}
 	if (need_ipte_lock)
 		ipte_unlock(vcpu);
@@ -875,19 +887,17 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
 int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
 		      void *data, unsigned long len, enum gacc_mode mode)
 {
-	unsigned long _len, gpa;
+	unsigned long gpa;
+	unsigned int seg;
 	int rc = 0;
 
 	while (len && !rc) {
 		gpa = kvm_s390_real_to_abs(vcpu, gra);
-		_len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-		if (mode)
-			rc = write_guest_abs(vcpu, gpa, data, _len);
-		else
-			rc = read_guest_abs(vcpu, gpa, data, _len);
-		len -= _len;
-		gra += _len;
-		data += _len;
+		seg = min(PAGE_SIZE - offset_in_page(gpa), len);
+		rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+		len -= seg;
+		gra += seg;
+		data += seg;
 	}
 	return rc;
 }
#3 lore raw

Re: [PATCH 1/2] KVM: s390: gaccess: Cleanup access to guest frames

In reply to Janis Schoetterl-Glausch
Quoted 14 lines · Janis Schoetterl-Glausch

Introduce a helper function for guest frame access. Rewrite the calculation of the gpa and the length of the segment to be more readable.

---
  arch/s390/kvm/gaccess.c | 48 +++++++++++++++++++++++++----------------
  1 file changed, 29 insertions(+), 19 deletions(-)
arch/s390/kvm/gaccess.c +0 -0
index b9f85b2dc053..df83de0843de 100644

arch/s390/kvm/gaccess.c static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, · line 831

Quoted 5 lines
arch/s390/kvm/gaccess.c +2 -0
@@ -827,11 +827,26 @@ static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
  	return 0;
  }
 
+static int access_guest_frame(struct kvm *kvm, enum gacc_mode mode, gpa_t gpa,
+			      void *data, unsigned int len)

I know, "frame" is a beautiful term for "page" -- can we just avoid using it because we're not using it anywhere else here? :)

What's wrong with "access_guest_page()" ?

arch/s390/kvm/gaccess.c static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, · line 835

Quoted 4 lines
arch/s390/kvm/gaccess.c +4 -0
+{
+	gfn_t gfn = gpa_to_gfn(gpa);
+	unsigned int offset = offset_in_page(gpa);
+	int rc;

You could turn both const. You might want to consider reverse-christmas-treeing this.

arch/s390/kvm/gaccess.c static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, · line 840

Quoted 5 lines
arch/s390/kvm/gaccess.c +5 -0
+
+	if (mode == GACC_STORE)
+		rc = kvm_write_guest_page(kvm, gfn, data, offset, len);
+	else
+		rc = kvm_read_guest_page(kvm, gfn, data, offset, len);

Personally, I prefer passing in pfn + offset instead of a gpa. Also avoids having to convert back and forth.

arch/s390/kvm/gaccess.c static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, · line 849

Quoted 10 lines
arch/s390/kvm/gaccess.c +5 -1
+	return rc;
+}
+
  int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
  		 unsigned long len, enum gacc_mode mode)
  {
  	psw_t *psw = &vcpu->arch.sie_block->gpsw;
-	unsigned long _len, nr_pages, gpa, idx;
+	unsigned long nr_pages, gpa, idx;
+	unsigned int seg;

Dito, reverse christmas tree might be worth keeping.

Quoted 35 lines
arch/s390/kvm/gaccess.c +8 -10
  	unsigned long pages_array[2];
  	unsigned long *pages;
  	int need_ipte_lock;
@@ -855,15 +870,12 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
  		ipte_lock(vcpu);
  	rc = guest_page_range(vcpu, ga, ar, pages, nr_pages, asce, mode);
  	for (idx = 0; idx < nr_pages && !rc; idx++) {
-		gpa = *(pages + idx) + (ga & ~PAGE_MASK);
-		_len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-		if (mode == GACC_STORE)
-			rc = kvm_write_guest(vcpu->kvm, gpa, data, _len);
-		else
-			rc = kvm_read_guest(vcpu->kvm, gpa, data, _len);
-		len -= _len;
-		ga += _len;
-		data += _len;
+		gpa = pages[idx] + offset_in_page(ga);
+		seg = min(PAGE_SIZE - offset_in_page(gpa), len);
+		rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+		len -= seg;
+		ga += seg;
+		data += seg;
  	}
  	if (need_ipte_lock)
  		ipte_unlock(vcpu);
@@ -875,19 +887,17 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
  int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
  		      void *data, unsigned long len, enum gacc_mode mode)
  {
-	unsigned long _len, gpa;
+	unsigned long gpa;
+	unsigned int seg;
  	int rc = 0;
 
  	while (len && !rc) {

arch/s390/kvm/gaccess.c int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data, · line 896

Quoted 10 lines
arch/s390/kvm/gaccess.c +1 -8
  		gpa = kvm_s390_real_to_abs(vcpu, gra);
-		_len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-		if (mode)
-			rc = write_guest_abs(vcpu, gpa, data, _len);
-		else
-			rc = read_guest_abs(vcpu, gpa, data, _len);
-		len -= _len;
-		gra += _len;
-		data += _len;
+		seg = min(PAGE_SIZE - offset_in_page(gpa), len);

What does "seg" mean? I certainly know when "len" means -- which is also what the function eats.

Quoted 8 lines
arch/s390/kvm/gaccess.c +4 -0
+		rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+		len -= seg;
+		gra += seg;
+		data += seg;
  	}
  	return rc;
  }
Signature · 3 lines
Thanks,

David / dhildenb
maintainer
#4 lore raw

Re: [PATCH 1/2] KVM: s390: gaccess: Cleanup access to guest frames

Quoted 6 lines · David Hildenbrand
Quoted 5 lines · Janis Schoetterl-Glausch

Introduce a helper function for guest frame access. Rewrite the calculation of the gpa and the length of the segment to be more readable.

[...]

Quoted 5 lines
Quoted 5 lines
arch/s390/kvm/gaccess.c +2 -1
-	unsigned long _len, gpa;
+	unsigned long gpa;
+	unsigned int seg;
  	int rc = 0;
Quoted 14 lines

arch/s390/kvm/gaccess.c int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data, · line 896

Quoted 11 lines
  	while (len && !rc) {
  		gpa = kvm_s390_real_to_abs(vcpu, gra);
arch/s390/kvm/gaccess.c +1 -8
-		_len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-		if (mode)
-			rc = write_guest_abs(vcpu, gpa, data, _len);
-		else
-			rc = read_guest_abs(vcpu, gpa, data, _len);
-		len -= _len;
-		gra += _len;
-		data += _len;
+		seg = min(PAGE_SIZE - offset_in_page(gpa), len);

What does "seg" mean? I certainly know when "len" means -- which is also what the function eats.

What does "_len" mean especially in contrast to "len"?

"seg" is used in the common kvm guest access functions so it's at least consistent although I share the sentiment that it's not a great name for the length we access inside the page.

Originally I suggested "len_in_page" if you have a better name I'd expect we'll both be happy to discuss it :-)

Quoted 11 lines
Quoted 9 lines
arch/s390/kvm/gaccess.c +4 -0
+		rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+		len -= seg;
+		gra += seg;
+		data += seg;
  	}
  	return rc;
  }
#5 lore raw

Re: [PATCH 1/2] KVM: s390: gaccess: Cleanup access to guest frames

Quoted 28 lines · Janosch Frank
Quoted 6 lines · David Hildenbrand
Quoted 5 lines · Janis Schoetterl-Glausch

Introduce a helper function for guest frame access. Rewrite the calculation of the gpa and the length of the segment to be more readable.

[...]

Quoted 5 lines
Quoted 5 lines
arch/s390/kvm/gaccess.c +2 -1
-	unsigned long _len, gpa;
+	unsigned long gpa;
+	unsigned int seg;
   	int rc = 0;
Quoted 14 lines

arch/s390/kvm/gaccess.c int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data, · line 896

Quoted 11 lines
   	while (len && !rc) {
   		gpa = kvm_s390_real_to_abs(vcpu, gra);
arch/s390/kvm/gaccess.c +1 -8
-		_len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-		if (mode)
-			rc = write_guest_abs(vcpu, gpa, data, _len);
-		else
-			rc = read_guest_abs(vcpu, gpa, data, _len);
-		len -= _len;
-		gra += _len;
-		data += _len;
+		seg = min(PAGE_SIZE - offset_in_page(gpa), len);

What does "seg" mean? I certainly know when "len" means -- which is also what the function eats.

Quoted 8 lines

What does "_len" mean especially in contrast to "len"?

"seg" is used in the common kvm guest access functions so it's at least consistent although I share the sentiment that it's not a great name for the length we access inside the page.

Originally I suggested "len_in_page" if you have a better name I'd expect we'll both be happy to discuss it :-)

Similar code I encountered in other places uses "len" vs "cur_len" or "total_len" vs. "cur_len". I agree that everything is better than "len" vs. "_len".

I just cannot come up with a proper word for "seg" that would make sense. "Segment" ? Maybe my uneducated mind is missing some important English words that just fit perfectly here.

Anyhow, just my 2 cents, you maintain this code :)

Signature · 3 lines
Thanks,

David / dhildenb
#6 lore raw

Re: [PATCH 1/2] KVM: s390: gaccess: Cleanup access to guest frames

Quoted 34 lines · David Hildenbrand
Quoted 33 lines · Janosch Frank
Quoted 6 lines · David Hildenbrand
Quoted 5 lines · Janis Schoetterl-Glausch

Introduce a helper function for guest frame access. Rewrite the calculation of the gpa and the length of the segment to be more readable.

[...]

Quoted 16 lines
Quoted 7 lines

-    unsigned long _len, gpa;
+    unsigned long gpa;
+    unsigned int seg;
       int rc = 0;
          while (len && !rc) {
           gpa = kvm_s390_real_to_abs(vcpu, gra); -        _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);

Quoted 8 lines

-        if (mode)
-            rc = write_guest_abs(vcpu, gpa, data, _len); -        else
-            rc = read_guest_abs(vcpu, gpa, data, _len); -        len -= _len;
-        gra += _len;
-        data += _len;
+        seg = min(PAGE_SIZE - offset_in_page(gpa), len);

Quoted 2 lines

What does "seg" mean? I certainly know when "len" means -- which is also what the function eats.

What does "_len" mean especially in contrast to "len"?

"seg" is used in the common kvm guest access functions so it's at least consistent although I share the sentiment that it's not a great name for the length we access inside the page.

Quoted 6 lines
Quoted 2 lines

Originally I suggested "len_in_page" if you have a better name I'd expect we'll both be happy to discuss it :-)

Similar code I encountered in other places uses "len" vs "cur_len" or "total_len" vs. "cur_len". I agree that everything is better than "len" vs. "_len".

I just cannot come up with a proper word for "seg" that would make sense. "Segment" ? Maybe my uneducated mind is missing some important English words that just fit perfectly here.

Yes, it's short for segment, kvm_main has a function next_segment to calculate it. I don't like the naming scheme much either.

Quoted 3 lines

Anyhow, just my 2 cents, you maintain this code :)

#7 lore raw

Re: [PATCH 1/2] KVM: s390: gaccess: Cleanup access to guest frames

In reply to Janosch Frank
Quoted 28 lines · Janosch Frank
Quoted 6 lines · David Hildenbrand
Quoted 5 lines · Janis Schoetterl-Glausch

Introduce a helper function for guest frame access. Rewrite the calculation of the gpa and the length of the segment to be more readable.

[...]

Quoted 5 lines
Quoted 5 lines
arch/s390/kvm/gaccess.c +2 -1
-	unsigned long _len, gpa;
+	unsigned long gpa;
+	unsigned int seg;
  	int rc = 0;
Quoted 14 lines

arch/s390/kvm/gaccess.c int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data, · line 896

Quoted 11 lines
  	while (len && !rc) {
  		gpa = kvm_s390_real_to_abs(vcpu, gra);
arch/s390/kvm/gaccess.c +1 -8
-		_len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-		if (mode)
-			rc = write_guest_abs(vcpu, gpa, data, _len);
-		else
-			rc = read_guest_abs(vcpu, gpa, data, _len);
-		len -= _len;
-		gra += _len;
-		data += _len;
+		seg = min(PAGE_SIZE - offset_in_page(gpa), len);

What does "seg" mean? I certainly know when "len" means -- which is also what the function eats.

Quoted 8 lines

What does "_len" mean especially in contrast to "len"?

"seg" is used in the common kvm guest access functions so it's at least consistent although I share the sentiment that it's not a great name for the length we access inside the page.

Originally I suggested "len_in_page" if you have a better name I'd expect we'll both be happy to discuss it :-)

fragment_len ?

Quoted 13 lines
Quoted 12 lines
Quoted 10 lines
arch/s390/kvm/gaccess.c +4 -0
+		rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+		len -= seg;
+		gra += seg;
+		data += seg;
  	}
  	return rc;
  }
maintainer
#8 lore raw

Re: [PATCH 1/2] KVM: s390: gaccess: Cleanup access to guest frames

Quoted 31 lines · Janis Schoetterl-Glausch
Quoted 30 lines · Janosch Frank
Quoted 6 lines · David Hildenbrand
Quoted 5 lines · Janis Schoetterl-Glausch

Introduce a helper function for guest frame access. Rewrite the calculation of the gpa and the length of the segment to be more readable.

[...]

Quoted 5 lines
Quoted 5 lines
arch/s390/kvm/gaccess.c +2 -1
-	unsigned long _len, gpa;
+	unsigned long gpa;
+	unsigned int seg;
  	int rc = 0;
Quoted 14 lines

arch/s390/kvm/gaccess.c int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data, · line 896

Quoted 11 lines
  	while (len && !rc) {
  		gpa = kvm_s390_real_to_abs(vcpu, gra);
arch/s390/kvm/gaccess.c +1 -8
-		_len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-		if (mode)
-			rc = write_guest_abs(vcpu, gpa, data, _len);
-		else
-			rc = read_guest_abs(vcpu, gpa, data, _len);
-		len -= _len;
-		gra += _len;
-		data += _len;
+		seg = min(PAGE_SIZE - offset_in_page(gpa), len);

What does "seg" mean? I certainly know when "len" means -- which is also what the function eats.

What does "_len" mean especially in contrast to "len"?

Quoted 8 lines
Quoted 6 lines

"seg" is used in the common kvm guest access functions so it's at least consistent although I share the sentiment that it's not a great name for the length we access inside the page.

Originally I suggested "len_in_page" if you have a better name I'd expect we'll both be happy to discuss it :-)

fragment_len ?

Sounds good to me

Quoted 14 lines
Quoted 13 lines
Quoted 12 lines
Quoted 10 lines
arch/s390/kvm/gaccess.c +4 -0
+		rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+		len -= seg;
+		gra += seg;
+		data += seg;
  	}
  	return rc;
  }
#9 lore raw

Re: [PATCH 1/2] KVM: s390: gaccess: Cleanup access to guest frames

In reply to David Hildenbrand
Quoted 22 lines · David Hildenbrand
Quoted 14 lines · Janis Schoetterl-Glausch

Introduce a helper function for guest frame access. Rewrite the calculation of the gpa and the length of the segment to be more readable.

---
  arch/s390/kvm/gaccess.c | 48 +++++++++++++++++++++++++----------------

  1 file changed, 29 insertions(+), 19 deletions(-)
arch/s390/kvm/gaccess.c +0 -0
index b9f85b2dc053..df83de0843de 100644

arch/s390/kvm/gaccess.c static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, · line 830

Quoted 4 lines
arch/s390/kvm/gaccess.c +1 -0
@@ -827,11 +827,26 @@ static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
       return 0;
   }
   +static int access_guest_frame(struct kvm *kvm, enum gacc_mode mode, gpa_t gpa,
+                  void *data, unsigned int len)

I know, "frame" is a beautiful term for "page" -- can we just avoid using it because we're not using it anywhere else here? :)

Quoted 1 line

What's wrong with "access_guest_page()" ?

Ok, I'll use page for consistency's sake.

Quoted 8 lines

arch/s390/kvm/gaccess.c static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, · line 835

Quoted 6 lines
arch/s390/kvm/gaccess.c +4 -0
+{
+    gfn_t gfn = gpa_to_gfn(gpa);
+    unsigned int offset = offset_in_page(gpa);
+    int rc;

You could turn both const. You might want to consider reverse-christmas-treeing this.

Ok

Quoted 8 lines

arch/s390/kvm/gaccess.c static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, · line 840

Quoted 6 lines
arch/s390/kvm/gaccess.c +5 -0
+
+    if (mode == GACC_STORE)
+        rc = kvm_write_guest_page(kvm, gfn, data, offset, len);
+    else
+        rc = kvm_read_guest_page(kvm, gfn, data, offset, len);

Personally, I prefer passing in pfn + offset instead of a gpa. Also avoids having to convert back and forth.

In access_guest_real we get back the gpa directly from the translation function.
After the next patch the same is true for access_guest.
So using gpas everywhere is nicer.
And if we were to introduce a len_in_page function the offset would not even show up as an intermediary.

Quoted 71 lines

arch/s390/kvm/gaccess.c static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, · line 849

Quoted 11 lines
arch/s390/kvm/gaccess.c +5 -1
+    return rc;
+}
+
   int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
            unsigned long len, enum gacc_mode mode)
   {
       psw_t *psw = &vcpu->arch.sie_block->gpsw;
-    unsigned long _len, nr_pages, gpa, idx;
+    unsigned long nr_pages, gpa, idx;
+    unsigned int seg;

Dito, reverse christmas tree might be worth keeping.

Quoted 34 lines
arch/s390/kvm/gaccess.c +8 -10
       unsigned long pages_array[2];
       unsigned long *pages;
       int need_ipte_lock;
@@ -855,15 +870,12 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
           ipte_lock(vcpu);
       rc = guest_page_range(vcpu, ga, ar, pages, nr_pages, asce, mode);
       for (idx = 0; idx < nr_pages && !rc; idx++) {
-        gpa = *(pages + idx) + (ga & ~PAGE_MASK);
-        _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-        if (mode == GACC_STORE)
-            rc = kvm_write_guest(vcpu->kvm, gpa, data, _len);
-        else
-            rc = kvm_read_guest(vcpu->kvm, gpa, data, _len);
-        len -= _len;
-        ga += _len;
-        data += _len;
+        gpa = pages[idx] + offset_in_page(ga);
+        seg = min(PAGE_SIZE - offset_in_page(gpa), len);
+        rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+        len -= seg;
+        ga += seg;
+        data += seg;
       }
       if (need_ipte_lock)
           ipte_unlock(vcpu);
@@ -875,19 +887,17 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
   int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
                 void *data, unsigned long len, enum gacc_mode mode)
   {
-    unsigned long _len, gpa;
+    unsigned long gpa;
+    unsigned int seg;
       int rc = 0;
         while (len && !rc) {

arch/s390/kvm/gaccess.c int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data, · line 895

Quoted 10 lines
arch/s390/kvm/gaccess.c +1 -8
           gpa = kvm_s390_real_to_abs(vcpu, gra);
-        _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
-        if (mode)
-            rc = write_guest_abs(vcpu, gpa, data, _len);
-        else
-            rc = read_guest_abs(vcpu, gpa, data, _len);
-        len -= _len;
-        gra += _len;
-        data += _len;
+        seg = min(PAGE_SIZE - offset_in_page(gpa), len);

What does "seg" mean? I certainly know when "len" means -- which is also what the function eats.

Quoted 8 lines
arch/s390/kvm/gaccess.c +4 -0
+        rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+        len -= seg;
+        gra += seg;
+        data += seg;
       }
       return rc;
   }
 
#10 lore raw

PATCH 2/2 KVM: s390: gaccess: Refactor access address range check

In reply to Janis Schoetterl-Glausch

Do not round down the first address to the page boundary, just translate it normally, which gives the value we care about in the first place. Given this, translating a single address is just the special case of translating a range spanning a single page.

Make the output optional, so the function can be used to just check a range.

---
 arch/s390/kvm/gaccess.c | 91 ++++++++++++++++++-----------------------
 1 file changed, 39 insertions(+), 52 deletions(-)
arch/s390/kvm/gaccess.c +39 -52
index df83de0843de..e5a19d8b30e2 100644
@@ -794,35 +794,45 @@ static int low_address_protection_enabled(struct kvm_vcpu *vcpu,
 	return 1;
 }
 
-static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
-			    unsigned long *pages, unsigned long nr_pages,
-			    const union asce asce, enum gacc_mode mode)
+/* Stores the gpas for each page in a real/virtual range into @gpas
+ * Modifies the 'struct kvm_s390_pgm_info pgm' member of @vcpu in the same
+ * way read_guest/write_guest do, the meaning of the return value is likewise
+ * the same.
+ * If @gpas is NULL only the checks are performed.
+ */
+static int guest_range_to_gpas(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
+			       unsigned long *gpas, unsigned long len,
+			       const union asce asce, enum gacc_mode mode)
 {
 	psw_t *psw = &vcpu->arch.sie_block->gpsw;
+	unsigned long gpa;
+	unsigned int seg;
+	unsigned int offset = offset_in_page(ga);
 	int lap_enabled, rc = 0;
 	enum prot_type prot;
 
 	lap_enabled = low_address_protection_enabled(vcpu, asce);
-	while (nr_pages) {
+	while ((seg = min(PAGE_SIZE - offset, len)) != 0) {
 		ga = kvm_s390_logical_to_effective(vcpu, ga);
 		if (mode == GACC_STORE && lap_enabled && is_low_address(ga))
 			return trans_exc(vcpu, PGM_PROTECTION, ga, ar, mode,
 					 PROT_TYPE_LA);
-		ga &= PAGE_MASK;
 		if (psw_bits(*psw).dat) {
-			rc = guest_translate(vcpu, ga, pages, asce, mode, &prot);
+			rc = guest_translate(vcpu, ga, &gpa, asce, mode, &prot);
 			if (rc < 0)
 				return rc;
 		} else {
-			*pages = kvm_s390_real_to_abs(vcpu, ga);
-			if (kvm_is_error_gpa(vcpu->kvm, *pages))
+			gpa = kvm_s390_real_to_abs(vcpu, ga);
+			if (kvm_is_error_gpa(vcpu->kvm, gpa))
 				rc = PGM_ADDRESSING;
 		}
 		if (rc)
 			return trans_exc(vcpu, rc, ga, ar, mode, prot);
-		ga += PAGE_SIZE;
-		pages++;
-		nr_pages--;
+		if (gpas)
+			*gpas++ = gpa;
+		offset = 0;
+		ga += seg;
+		len -= seg;
 	}
 	return 0;
 }
@@ -845,10 +855,10 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
 		 unsigned long len, enum gacc_mode mode)
 {
 	psw_t *psw = &vcpu->arch.sie_block->gpsw;
-	unsigned long nr_pages, gpa, idx;
+	unsigned long nr_pages, idx;
 	unsigned int seg;
-	unsigned long pages_array[2];
-	unsigned long *pages;
+	unsigned long gpa_array[2];
+	unsigned long *gpas;
 	int need_ipte_lock;
 	union asce asce;
 	int rc;
@@ -860,27 +870,25 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
 	if (rc)
 		return rc;
 	nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1;
-	pages = pages_array;
-	if (nr_pages > ARRAY_SIZE(pages_array))
-		pages = vmalloc(array_size(nr_pages, sizeof(unsigned long)));
-	if (!pages)
+	gpas = gpa_array;
+	if (nr_pages > ARRAY_SIZE(gpa_array))
+		gpas = vmalloc(array_size(nr_pages, sizeof(unsigned long)));
+	if (!gpas)
 		return -ENOMEM;
 	need_ipte_lock = psw_bits(*psw).dat && !asce.r;
 	if (need_ipte_lock)
 		ipte_lock(vcpu);
-	rc = guest_page_range(vcpu, ga, ar, pages, nr_pages, asce, mode);
+	rc = guest_range_to_gpas(vcpu, ga, ar, gpas, len, asce, mode);
 	for (idx = 0; idx < nr_pages && !rc; idx++) {
-		gpa = pages[idx] + offset_in_page(ga);
-		seg = min(PAGE_SIZE - offset_in_page(gpa), len);
-		rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+		seg = min(PAGE_SIZE - offset_in_page(gpas[idx]), len);
+		rc = access_guest_frame(vcpu->kvm, mode, gpas[idx], data, seg);
 		len -= seg;
-		ga += seg;
 		data += seg;
 	}
 	if (need_ipte_lock)
 		ipte_unlock(vcpu);
-	if (nr_pages > ARRAY_SIZE(pages_array))
-		vfree(pages);
+	if (nr_pages > ARRAY_SIZE(gpa_array))
+		vfree(gpas);
 	return rc;
 }
 
@@ -914,8 +922,6 @@ int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
 int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
 			    unsigned long *gpa, enum gacc_mode mode)
 {
-	psw_t *psw = &vcpu->arch.sie_block->gpsw;
-	enum prot_type prot;
 	union asce asce;
 	int rc;
 
@@ -923,23 +929,7 @@ int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
 	rc = get_vcpu_asce(vcpu, &asce, gva, ar, mode);
 	if (rc)
 		return rc;
-	if (is_low_address(gva) && low_address_protection_enabled(vcpu, asce)) {
-		if (mode == GACC_STORE)
-			return trans_exc(vcpu, PGM_PROTECTION, gva, 0,
-					 mode, PROT_TYPE_LA);
-	}
-
-	if (psw_bits(*psw).dat && !asce.r) {	/* Use DAT? */
-		rc = guest_translate(vcpu, gva, gpa, asce, mode, &prot);
-		if (rc > 0)
-			return trans_exc(vcpu, rc, gva, 0, mode, prot);
-	} else {
-		*gpa = kvm_s390_real_to_abs(vcpu, gva);
-		if (kvm_is_error_gpa(vcpu->kvm, *gpa))
-			return trans_exc(vcpu, rc, gva, PGM_ADDRESSING, mode, 0);
-	}
-
-	return rc;
+	return guest_range_to_gpas(vcpu, gva, ar, gpa, 1, asce, mode);
 }
 
 /**
@@ -948,17 +938,14 @@ int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
 int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
 		    unsigned long length, enum gacc_mode mode)
 {
-	unsigned long gpa;
-	unsigned long currlen;
+	union asce asce;
 	int rc = 0;
 
+	rc = get_vcpu_asce(vcpu, &asce, gva, ar, mode);
+	if (rc)
+		return rc;
 	ipte_lock(vcpu);
-	while (length > 0 && !rc) {
-		currlen = min(length, PAGE_SIZE - (gva % PAGE_SIZE));
-		rc = guest_translate_address(vcpu, gva, ar, &gpa, mode);
-		gva += currlen;
-		length -= currlen;
-	}
+	rc = guest_range_to_gpas(vcpu, gva, ar, NULL, length, asce, mode);
 	ipte_unlock(vcpu);
 
 	return rc;
Signature · 1 line
2.25.1
maintainer
#11 lore raw

Re: [PATCH 2/2] KVM: s390: gaccess: Refactor access address range check

Quoted 7 lines · On Mon, 16 Aug 2021 17:07:17 +0200 Janis Schoetterl-Glausch…

Do not round down the first address to the page boundary, just translate it normally, which gives the value we care about in the first place. Given this, translating a single address is just the special case of translating a range spanning a single page.

Make the output optional, so the function can be used to just check a range.

I like the idea, but see a few nits below

Quoted 11 lines
---
 arch/s390/kvm/gaccess.c | 91 ++++++++++++++++++-----------------------
 1 file changed, 39 insertions(+), 52 deletions(-)
arch/s390/kvm/gaccess.c +0 -0
index df83de0843de..e5a19d8b30e2 100644

arch/s390/kvm/gaccess.c static int low_address_protection_enabled(struct kvm_vcpu *vcpu, · line 799

Quoted 9 lines
arch/s390/kvm/gaccess.c +3 -3
@@ -794,35 +794,45 @@ static int low_address_protection_enabled(struct kvm_vcpu *vcpu,
 	return 1;
 }
 
-static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
-			    unsigned long *pages, unsigned long nr_pages,
-			    const union asce asce, enum gacc_mode mode)
+/* Stores the gpas for each page in a real/virtual range into @gpas
+ * Modifies the 'struct kvm_s390_pgm_info pgm' member of @vcpu in the same
+ * way read_guest/write_guest do, the meaning of the return value is likewise

this comment is a bit confusing; why telling us to look what a different function is doing?

either don't mention this at all (since it's more or less the expected behaviour), or explain in full what's going on

Quoted 7 lines
arch/s390/kvm/gaccess.c +6 -0
+ * the same.
+ * If @gpas is NULL only the checks are performed.
+ */
+static int guest_range_to_gpas(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
+			       unsigned long *gpas, unsigned long len,
+			       const union asce asce, enum gacc_mode mode)
 {

arch/s390/kvm/gaccess.c static int low_address_protection_enabled(struct kvm_vcpu *vcpu, · line 815

Quoted 10 lines
arch/s390/kvm/gaccess.c +4 -1
 	psw_t *psw = &vcpu->arch.sie_block->gpsw;
+	unsigned long gpa;
+	unsigned int seg;
+	unsigned int offset = offset_in_page(ga);
 	int lap_enabled, rc = 0;
 	enum prot_type prot;
 
 	lap_enabled = low_address_protection_enabled(vcpu, asce);
-	while (nr_pages) {
+	while ((seg = min(PAGE_SIZE - offset, len)) != 0) {

I'm not terribly fond of assignments-as-values; moreover offset is used only once.

why not something like:

	seg = min(PAGE_SIZE - offset, len);
	while (seg) {

		...

		seg = min(PAGE_SIZE, len);
	}

or maybe even:

	seg = min(PAGE_SIZE - offset, len);
	for (; seg; seg = min(PAGE_SIZE, len)) {

(although the one with the while is perhaps more readable)

Quoted 31 lines
arch/s390/kvm/gaccess.c +8 -7
 		ga = kvm_s390_logical_to_effective(vcpu, ga);
 		if (mode == GACC_STORE && lap_enabled && is_low_address(ga))
 			return trans_exc(vcpu, PGM_PROTECTION, ga, ar, mode,
 					 PROT_TYPE_LA);
-		ga &= PAGE_MASK;
 		if (psw_bits(*psw).dat) {
-			rc = guest_translate(vcpu, ga, pages, asce, mode, &prot);
+			rc = guest_translate(vcpu, ga, &gpa, asce, mode, &prot);
 			if (rc < 0)
 				return rc;
 		} else {
-			*pages = kvm_s390_real_to_abs(vcpu, ga);
-			if (kvm_is_error_gpa(vcpu->kvm, *pages))
+			gpa = kvm_s390_real_to_abs(vcpu, ga);
+			if (kvm_is_error_gpa(vcpu->kvm, gpa))
 				rc = PGM_ADDRESSING;
 		}
 		if (rc)
 			return trans_exc(vcpu, rc, ga, ar, mode, prot);
-		ga += PAGE_SIZE;
-		pages++;
-		nr_pages--;
+		if (gpas)
+			*gpas++ = gpa;
+		offset = 0;
+		ga += seg;
+		len -= seg;
 	}
 	return 0;
 }

arch/s390/kvm/gaccess.c int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data, · line 861

Quoted 10 lines
arch/s390/kvm/gaccess.c +3 -3
@@ -845,10 +855,10 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
 		 unsigned long len, enum gacc_mode mode)
 {
 	psw_t *psw = &vcpu->arch.sie_block->gpsw;
-	unsigned long nr_pages, gpa, idx;
+	unsigned long nr_pages, idx;
 	unsigned int seg;
-	unsigned long pages_array[2];
-	unsigned long *pages;
+	unsigned long gpa_array[2];
+	unsigned long *gpas;

reverse Christmas tree?

also, since you're touching this: have you checked if a different size for the array would bring any benefit?
2 seems a little too small, but I have no idea if anything bigger would bring any advantages.

Quoted 97 lines
arch/s390/kvm/gaccess.c +15 -38
 	int need_ipte_lock;
 	union asce asce;
 	int rc;
@@ -860,27 +870,25 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
 	if (rc)
 		return rc;
 	nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1;
-	pages = pages_array;
-	if (nr_pages > ARRAY_SIZE(pages_array))
-		pages = vmalloc(array_size(nr_pages, sizeof(unsigned long)));
-	if (!pages)
+	gpas = gpa_array;
+	if (nr_pages > ARRAY_SIZE(gpa_array))
+		gpas = vmalloc(array_size(nr_pages, sizeof(unsigned long)));
+	if (!gpas)
 		return -ENOMEM;
 	need_ipte_lock = psw_bits(*psw).dat && !asce.r;
 	if (need_ipte_lock)
 		ipte_lock(vcpu);
-	rc = guest_page_range(vcpu, ga, ar, pages, nr_pages, asce, mode);
+	rc = guest_range_to_gpas(vcpu, ga, ar, gpas, len, asce, mode);
 	for (idx = 0; idx < nr_pages && !rc; idx++) {
-		gpa = pages[idx] + offset_in_page(ga);
-		seg = min(PAGE_SIZE - offset_in_page(gpa), len);
-		rc = access_guest_frame(vcpu->kvm, mode, gpa, data, seg);
+		seg = min(PAGE_SIZE - offset_in_page(gpas[idx]), len);
+		rc = access_guest_frame(vcpu->kvm, mode, gpas[idx], data, seg);
 		len -= seg;
-		ga += seg;
 		data += seg;
 	}
 	if (need_ipte_lock)
 		ipte_unlock(vcpu);
-	if (nr_pages > ARRAY_SIZE(pages_array))
-		vfree(pages);
+	if (nr_pages > ARRAY_SIZE(gpa_array))
+		vfree(gpas);
 	return rc;
 }
 
@@ -914,8 +922,6 @@ int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
 int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
 			    unsigned long *gpa, enum gacc_mode mode)
 {
-	psw_t *psw = &vcpu->arch.sie_block->gpsw;
-	enum prot_type prot;
 	union asce asce;
 	int rc;
 
@@ -923,23 +929,7 @@ int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
 	rc = get_vcpu_asce(vcpu, &asce, gva, ar, mode);
 	if (rc)
 		return rc;
-	if (is_low_address(gva) && low_address_protection_enabled(vcpu, asce)) {
-		if (mode == GACC_STORE)
-			return trans_exc(vcpu, PGM_PROTECTION, gva, 0,
-					 mode, PROT_TYPE_LA);
-	}
-
-	if (psw_bits(*psw).dat && !asce.r) {	/* Use DAT? */
-		rc = guest_translate(vcpu, gva, gpa, asce, mode, &prot);
-		if (rc > 0)
-			return trans_exc(vcpu, rc, gva, 0, mode, prot);
-	} else {
-		*gpa = kvm_s390_real_to_abs(vcpu, gva);
-		if (kvm_is_error_gpa(vcpu->kvm, *gpa))
-			return trans_exc(vcpu, rc, gva, PGM_ADDRESSING, mode, 0);
-	}
-
-	return rc;
+	return guest_range_to_gpas(vcpu, gva, ar, gpa, 1, asce, mode);
 }
 
 /**
@@ -948,17 +938,14 @@ int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
 int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
 		    unsigned long length, enum gacc_mode mode)
 {
-	unsigned long gpa;
-	unsigned long currlen;
+	union asce asce;
 	int rc = 0;
 
+	rc = get_vcpu_asce(vcpu, &asce, gva, ar, mode);
+	if (rc)
+		return rc;
 	ipte_lock(vcpu);
-	while (length > 0 && !rc) {
-		currlen = min(length, PAGE_SIZE - (gva % PAGE_SIZE));
-		rc = guest_translate_address(vcpu, gva, ar, &gpa, mode);
-		gva += currlen;
-		length -= currlen;
-	}
+	rc = guest_range_to_gpas(vcpu, gva, ar, NULL, length, asce, mode);
 	ipte_unlock(vcpu);
 
 	return rc;
#12 lore raw

Re: [PATCH 2/2] KVM: s390: gaccess: Refactor access address range check

Quoted 37 lines · Claudio Imbrenda
Quoted 7 lines · On Mon, 16 Aug 2021 17:07:17 +0200 Janis Schoetterl-Glausch…

Do not round down the first address to the page boundary, just translate it normally, which gives the value we care about in the first place. Given this, translating a single address is just the special case of translating a range spanning a single page.

Make the output optional, so the function can be used to just check a range.

I like the idea, but see a few nits below

Quoted 11 lines
---
 arch/s390/kvm/gaccess.c | 91 ++++++++++++++++++-----------------------
 1 file changed, 39 insertions(+), 52 deletions(-)
arch/s390/kvm/gaccess.c +0 -0
index df83de0843de..e5a19d8b30e2 100644

arch/s390/kvm/gaccess.c static int low_address_protection_enabled(struct kvm_vcpu *vcpu, · line 799

Quoted 9 lines
arch/s390/kvm/gaccess.c +3 -3
@@ -794,35 +794,45 @@ static int low_address_protection_enabled(struct kvm_vcpu *vcpu,
 	return 1;
 }
 
-static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
-			    unsigned long *pages, unsigned long nr_pages,
-			    const union asce asce, enum gacc_mode mode)
+/* Stores the gpas for each page in a real/virtual range into @gpas
+ * Modifies the 'struct kvm_s390_pgm_info pgm' member of @vcpu in the same
+ * way read_guest/write_guest do, the meaning of the return value is likewise

this comment is a bit confusing; why telling us to look what a different function is doing?

Quoted 2 lines

either don't mention this at all (since it's more or less the expected behaviour), or explain in full what's going on

Yeah, it's not ideal. I haven't decided yet what I'll do.
I think a comment would be helpful, and it may be expected behavior only if one has looked at the code for long enough :).

Quoted 32 lines
Quoted 8 lines
arch/s390/kvm/gaccess.c +6 -0
+ * the same.
+ * If @gpas is NULL only the checks are performed.
+ */
+static int guest_range_to_gpas(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
+			       unsigned long *gpas, unsigned long len,
+			       const union asce asce, enum gacc_mode mode)
 {

arch/s390/kvm/gaccess.c static int low_address_protection_enabled(struct kvm_vcpu *vcpu, · line 815

Quoted 10 lines
arch/s390/kvm/gaccess.c +4 -1
 	psw_t *psw = &vcpu->arch.sie_block->gpsw;
+	unsigned long gpa;
+	unsigned int seg;
+	unsigned int offset = offset_in_page(ga);
 	int lap_enabled, rc = 0;
 	enum prot_type prot;
 
 	lap_enabled = low_address_protection_enabled(vcpu, asce);
-	while (nr_pages) {
+	while ((seg = min(PAGE_SIZE - offset, len)) != 0) {

I'm not terribly fond of assignments-as-values; moreover offset is used only once.

why not something like:

	seg = min(PAGE_SIZE - offset, len);
	while (seg) {

		...

		seg = min(PAGE_SIZE, len);
	}
Quoted 6 lines

or maybe even:

	seg = min(PAGE_SIZE - offset, len);
	for (; seg; seg = min(PAGE_SIZE, len)) {

(although the one with the while is perhaps more readable)

That code pattern is not entirely uncommon, but I'll change it to:

	while(min(PAGE_SIZE - offset, len) > 0) {
		seg = min(PAGE_SIZE - offset, len);
		...
	}

which I think reads better than having the assignment at the end. I assume the compiler gets rid of the redundancy.

Quoted 1 line

[...]

Quoted 14 lines

arch/s390/kvm/gaccess.c int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data, · line 861

Quoted 11 lines
arch/s390/kvm/gaccess.c +3 -3
@@ -845,10 +855,10 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
 		 unsigned long len, enum gacc_mode mode)
 {
 	psw_t *psw = &vcpu->arch.sie_block->gpsw;
-	unsigned long nr_pages, gpa, idx;
+	unsigned long nr_pages, idx;
 	unsigned int seg;
-	unsigned long pages_array[2];
-	unsigned long *pages;
+	unsigned long gpa_array[2];
+	unsigned long *gpas;

reverse Christmas tree?

Quoted 4 lines

also, since you're touching this: have you checked if a different size for the array would bring any benefit?
2 seems a little too small, but I have no idea if anything bigger would bring any advantages.

I have not checked it, no. When emulating instructions, you would only need >2 entries if an operand is >8k or >4k and weirdly aligned, hardly seems like a common occurrence. On the other hand, bumping it up should not have any negative consequences.
I'll leave it as is.

[...]