Skip to contentSkip to conversation

target/ppc: Yet another fix for KVM-HV HPTE accessors

PATCH Applied Alexey Kardashevskiy 3 participants 4 messages Jan 11, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/ppc: Yet another fix for KVM-HV HPTE accessors

As stated in the 1ad9f0a464fe commit log, the returned entries are not a while PTEG. It was not a problem before 1ad9f0a464fe as it would read a single record assuming it contains a whole PTEG but now the code tries reading the entire PTEG and "if ((n - i) < invalid)" produces negative values which then are converted to size_t for memset() and that throws seg fault.

This fixes the math.

While here, fix the last @i increment as well.

Fixes 1ad9f0a464fe "target/ppc: Fix KVM-HV HPTE accessors"

---

Record #0:
(gdb) p *hdr
$13 = {
index = <ptr>,
n_valid = 0x1,
n_invalid = 0x6
}

Record #1:
(gdb) p *hdr
$18 = {
index = <ptr>,
n_valid = 0x2,
n_invalid = 0x6
}

i.e. in the second iteration of the loop right before "if ((n - i) < invalid)":
(gdb) p n
$16 = 0x8
(gdb) p i
$17 = 0x9

and @invalid becomes -1.

---
 target/ppc/kvm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
target/ppc/kvm.c +7 -4
index 0566af7..c2dea81 100644
@@ -2657,21 +2657,24 @@ void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n)
 
         hdr = (struct kvm_get_htab_header *)buf;
         while ((i < n) && ((char *)hdr < (buf + rc))) {
-            int invalid = hdr->n_invalid;
+            int invalid = hdr->n_invalid, valid = hdr->n_valid;
 
             if (hdr->index != (ptex + i)) {
                 hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32
                          " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i);
             }
 
-            memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * hdr->n_valid);
-            i += hdr->n_valid;
+            if (n - i < valid) {
+                valid = n - i;
+            }
+            memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * valid);
+            i += valid;
 
             if ((n - i) < invalid) {
                 invalid = n - i;
             }
             memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64);
-            i += hdr->n_invalid;
+            i += invalid;
 
             hdr = (struct kvm_get_htab_header *)
                 ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid);
Signature · 1 line
2.11.0
Applied
#1 lore raw
Quoted 7 lines · Alexey Kardashevskiy

As stated in the 1ad9f0a464fe commit log, the returned entries are not a while PTEG. It was not a problem before 1ad9f0a464fe as it would read a single record assuming it contains a whole PTEG but now the code tries reading the entire PTEG and "if ((n - i) < invalid)" produces negative values which then are converted to size_t for memset() and that throws seg fault.

Quoted 6 lines

This fixes the math.

While here, fix the last @i increment as well.

Fixes 1ad9f0a464fe "target/ppc: Fix KVM-HV HPTE accessors"

Applied, thanks.

Quoted 65 lines

---

Record #0:
(gdb) p *hdr
$13 = {
index = <ptr>,
n_valid = 0x1,
n_invalid = 0x6
}

Record #1:
(gdb) p *hdr
$18 = {
index = <ptr>,
n_valid = 0x2,
n_invalid = 0x6
}

i.e. in the second iteration of the loop right before "if ((n - i) < invalid)":
(gdb) p n
$16 = 0x8
(gdb) p i
$17 = 0x9

and @invalid becomes -1.

---
 target/ppc/kvm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
target/ppc/kvm.c +7 -4
index 0566af7..c2dea81 100644
@@ -2657,21 +2657,24 @@ void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n)
 
         hdr = (struct kvm_get_htab_header *)buf;
         while ((i < n) && ((char *)hdr < (buf + rc))) {
-            int invalid = hdr->n_invalid;
+            int invalid = hdr->n_invalid, valid = hdr->n_valid;
 
             if (hdr->index != (ptex + i)) {
                 hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32
                          " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i);
             }
 
-            memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * hdr->n_valid);
-            i += hdr->n_valid;
+            if (n - i < valid) {
+                valid = n - i;
+            }
+            memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * valid);
+            i += valid;
 
             if ((n - i) < invalid) {
                 invalid = n - i;
             }
             memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64);
-            i += hdr->n_invalid;
+            i += invalid;
 
             hdr = (struct kvm_get_htab_header *)
                 ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid);
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#2 lore raw
In reply to Alexey Kardashevskiy
Quoted 2 lines · Alexey Kardashevskiy

As stated in the 1ad9f0a464fe commit log, the returned entries are not a while PTEG. It was not a problem before 1ad9f0a464fe as it would read

s/while/whole/?

Quoted 67 lines

a single record assuming it contains a whole PTEG but now the code tries reading the entire PTEG and "if ((n - i) < invalid)" produces negative values which then are converted to size_t for memset() and that throws seg fault.

This fixes the math.

While here, fix the last @i increment as well.

Fixes 1ad9f0a464fe "target/ppc: Fix KVM-HV HPTE accessors"

---

Record #0:
(gdb) p *hdr

$13 = {
   index = <ptr>,
   n_valid = 0x1,
   n_invalid = 0x6
}

Record #1:
(gdb) p *hdr

$18 = {
   index = <ptr>,
   n_valid = 0x2,
   n_invalid = 0x6
}

i.e. in the second iteration of the loop right before "if ((n - i) < invalid)":
(gdb) p n
$16 = 0x8
(gdb) p i
$17 = 0x9

and @invalid becomes -1.

---
  target/ppc/kvm.c | 11 +++++++----
  1 file changed, 7 insertions(+), 4 deletions(-)
target/ppc/kvm.c +6 -3
index 0566af7..c2dea81 100644
@@ -2657,21 +2657,24 @@ void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n)
 
          hdr = (struct kvm_get_htab_header *)buf;
          while ((i < n) && ((char *)hdr < (buf + rc))) {
-            int invalid = hdr->n_invalid;
+            int invalid = hdr->n_invalid, valid = hdr->n_valid;
 
              if (hdr->index != (ptex + i)) {
                  hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32
                           " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i);
              }
 
-            memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * hdr->n_valid);
-            i += hdr->n_valid;
+            if (n - i < valid) {
+                valid = n - i;
+            }
+            memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * valid);
+            i += valid;

target/ppc/kvm.c void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n) · line 2680

Quoted 10 lines
target/ppc/kvm.c +1 -1
 
              if ((n - i) < invalid) {
                  invalid = n - i;
              }
              memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64);
-            i += hdr->n_invalid;
+            i += invalid;
 
              hdr = (struct kvm_get_htab_header *)
                  ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid);

ATB,

Mark.

#3 lore raw
Quoted 6 lines · Mark Cave-Ayland
Quoted 2 lines · Alexey Kardashevskiy

As stated in the 1ad9f0a464fe commit log, the returned entries are not a while PTEG. It was not a problem before 1ad9f0a464fe as it would read

s/while/whole/?

I already merged, so I adjusted this in my tree. Come to that although the fix is correct, I don't think the current message explains it very well. I'll think about adjusting it.

Quoted 79 lines
Quoted 63 lines

a single record assuming it contains a whole PTEG but now the code tries reading the entire PTEG and "if ((n - i) < invalid)" produces negative values which then are converted to size_t for memset() and that throws seg fault.

This fixes the math.

While here, fix the last @i increment as well.

Fixes 1ad9f0a464fe "target/ppc: Fix KVM-HV HPTE accessors"

---

Record #0:
(gdb) p *hdr

$13 = {
   index = <ptr>,
   n_valid = 0x1,
   n_invalid = 0x6
}

Record #1:
(gdb) p *hdr

$18 = {
   index = <ptr>,
   n_valid = 0x2,
   n_invalid = 0x6
}

i.e. in the second iteration of the loop right before "if ((n - i) < invalid)":
(gdb) p n
$16 = 0x8
(gdb) p i
$17 = 0x9

and @invalid becomes -1.

---
  target/ppc/kvm.c | 11 +++++++----
  1 file changed, 7 insertions(+), 4 deletions(-)
target/ppc/kvm.c +5 -3
index 0566af7..c2dea81 100644
@@ -2657,21 +2657,24 @@ void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n)
          hdr = (struct kvm_get_htab_header *)buf;
          while ((i < n) && ((char *)hdr < (buf + rc))) {
-            int invalid = hdr->n_invalid;
+            int invalid = hdr->n_invalid, valid = hdr->n_valid;
              if (hdr->index != (ptex + i)) {
                  hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32
                           " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i);
              }
-            memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * hdr->n_valid);
-            i += hdr->n_valid;
+            if (n - i < valid) {
+                valid = n - i;
+            }
+            memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * valid);

target/ppc/kvm.c void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n) · line 2676

Quoted 10 lines
target/ppc/kvm.c +2 -1
+            i += valid;
              if ((n - i) < invalid) {
                  invalid = n - i;
              }
              memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64);
-            i += hdr->n_invalid;
+            i += invalid;
              hdr = (struct kvm_get_htab_header *)
                  ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid);
 

ATB,

Mark.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson