Skip to contentSkip to conversation

vfio/spapr: Fix page size calculation

PATCH Applied Alexey Kardashevskiy 5 participants 8 messages Mar 24, 2020 – Mar 26, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio/spapr: Fix page size calculation

Coverity detected an issue (CID 1421903) with potential call of clz64(0) which returns 64 which make it do "<<" with a negative number.

This checks the mask and avoids undefined behaviour.

In practice pgsizes and memory_region_iommu_get_min_page_size() always have some common page sizes and even if they did not, the resulting page size would be 0x8000.0000.0000.0000 (gcc 9.2) and ioctl(VFIO_IOMMU_SPAPR_TCE_CREATE) would fail anyway.

---
 hw/vfio/spapr.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/vfio/spapr.c +3 -3
index 33692fc86fd6..2900bd19417a 100644
@@ -147,7 +147,7 @@ int vfio_spapr_create_window(VFIOContainer *container,
 {
     int ret = 0;
     IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
-    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr);
+    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr), pgmask;
     unsigned entries, bits_total, bits_per_level, max_levels;
     struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) };
     long rampagesize = qemu_minrampagesize();
@@ -159,8 +159,8 @@ int vfio_spapr_create_window(VFIOContainer *container,
     if (pagesize > rampagesize) {
         pagesize = rampagesize;
     }
-    pagesize = 1ULL << (63 - clz64(container->pgsizes &
-                                   (pagesize | (pagesize - 1))));
+    pgmask = container->pgsizes & (pagesize | (pagesize - 1));
+    pagesize = pgmask ? (1ULL << (63 - clz64(pgmask))) : 0;
     if (!pagesize) {
         error_report("Host doesn't support page size 0x%"PRIx64
                      ", the supported mask is 0x%lx",
Signature · 1 line
2.17.1
#1 lore raw
Quoted 29 lines · Alexey Kardashevskiy

Coverity detected an issue (CID 1421903) with potential call of clz64(0) which returns 64 which make it do "<<" with a negative number.

This checks the mask and avoids undefined behaviour.

In practice pgsizes and memory_region_iommu_get_min_page_size() always have some common page sizes and even if they did not, the resulting page size would be 0x8000.0000.0000.0000 (gcc 9.2) and ioctl(VFIO_IOMMU_SPAPR_TCE_CREATE) would fail anyway.

---
  hw/vfio/spapr.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
hw/vfio/spapr.c +1 -1
index 33692fc86fd6..2900bd19417a 100644
@@ -147,7 +147,7 @@ int vfio_spapr_create_window(VFIOContainer *container,
  {
      int ret = 0;
      IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
-    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr);
+    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr), pgmask;
      unsigned entries, bits_total, bits_per_level, max_levels;
      struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) };
      long rampagesize = qemu_minrampagesize();

hw/vfio/spapr.c int vfio_spapr_create_window(VFIOContainer *container, · line 162

Quoted 6 lines
hw/vfio/spapr.c +1 -2
@@ -159,8 +159,8 @@ int vfio_spapr_create_window(VFIOContainer *container,
      if (pagesize > rampagesize) {
          pagesize = rampagesize;
      }
-    pagesize = 1ULL << (63 - clz64(container->pgsizes &
-                                   (pagesize | (pagesize - 1))));
+    pgmask = container->pgsizes & (pagesize | (pagesize - 1));

Is that ROUND_UP(container->pgsizes, pagesize)?

Quoted 5 lines
hw/vfio/spapr.c +1 -0
+    pagesize = pgmask ? (1ULL << (63 - clz64(pgmask))) : 0;
      if (!pagesize) {
          error_report("Host doesn't support page size 0x%"PRIx64
                       ", the supported mask is 0x%lx",
#2 lore raw
Quoted 32 lines · On Tue, 24 Mar 2020 14:27:35 +0100 Philippe Mathieu-Daudé <[email protected]>…
Quoted 29 lines · Alexey Kardashevskiy

Coverity detected an issue (CID 1421903) with potential call of clz64(0) which returns 64 which make it do "<<" with a negative number.

This checks the mask and avoids undefined behaviour.

In practice pgsizes and memory_region_iommu_get_min_page_size() always have some common page sizes and even if they did not, the resulting page size would be 0x8000.0000.0000.0000 (gcc 9.2) and ioctl(VFIO_IOMMU_SPAPR_TCE_CREATE) would fail anyway.

---
  hw/vfio/spapr.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
hw/vfio/spapr.c +1 -1
index 33692fc86fd6..2900bd19417a 100644
@@ -147,7 +147,7 @@ int vfio_spapr_create_window(VFIOContainer *container,
  {
      int ret = 0;
      IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
-    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr);
+    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr), pgmask;
      unsigned entries, bits_total, bits_per_level, max_levels;
      struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) };
      long rampagesize = qemu_minrampagesize();
@@ -159,8 +159,8 @@ int vfio_spapr_create_window(VFIOContainer *container,
Quoted 9 lines

hw/vfio/spapr.c int vfio_spapr_create_window(VFIOContainer *container, · line 162

Quoted 6 lines
hw/vfio/spapr.c +1 -2
      if (pagesize > rampagesize) {
          pagesize = rampagesize;
      }
-    pagesize = 1ULL << (63 - clz64(container->pgsizes &
-                                   (pagesize | (pagesize - 1))));
+    pgmask = container->pgsizes & (pagesize | (pagesize - 1));

Is that ROUND_UP(container->pgsizes, pagesize)?

This means we clip all page sizes greater than pagesize from container->pgsizes... It doesn't look like ROUND_UP() semantics to me.

Quoted 7 lines
Quoted 5 lines
hw/vfio/spapr.c +1 -0
+    pagesize = pgmask ? (1ULL << (63 - clz64(pgmask))) : 0;
      if (!pagesize) {
          error_report("Host doesn't support page size 0x%"PRIx64
                       ", the supported mask is 0x%lx",
#3 lore raw
Quoted 45 lines · Greg Kurz
Quoted 32 lines · On Tue, 24 Mar 2020 14:27:35 +0100 Philippe Mathieu-Daudé <[email protected]>…
Quoted 29 lines · Alexey Kardashevskiy

Coverity detected an issue (CID 1421903) with potential call of clz64(0) which returns 64 which make it do "<<" with a negative number.

This checks the mask and avoids undefined behaviour.

In practice pgsizes and memory_region_iommu_get_min_page_size() always have some common page sizes and even if they did not, the resulting page size would be 0x8000.0000.0000.0000 (gcc 9.2) and ioctl(VFIO_IOMMU_SPAPR_TCE_CREATE) would fail anyway.

---
   hw/vfio/spapr.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
hw/vfio/spapr.c +1 -1
index 33692fc86fd6..2900bd19417a 100644
@@ -147,7 +147,7 @@ int vfio_spapr_create_window(VFIOContainer *container,
   {
       int ret = 0;
       IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
-    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr);
+    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr), pgmask;
       unsigned entries, bits_total, bits_per_level, max_levels;
       struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) };
       long rampagesize = qemu_minrampagesize();
@@ -159,8 +159,8 @@ int vfio_spapr_create_window(VFIOContainer *container,
Quoted 9 lines

hw/vfio/spapr.c int vfio_spapr_create_window(VFIOContainer *container, · line 162

Quoted 6 lines
hw/vfio/spapr.c +1 -2
       if (pagesize > rampagesize) {
           pagesize = rampagesize;
       }
-    pagesize = 1ULL << (63 - clz64(container->pgsizes &
-                                   (pagesize | (pagesize - 1))));
+    pgmask = container->pgsizes & (pagesize | (pagesize - 1));

Is that ROUND_UP(container->pgsizes, pagesize)?

Quoted 3 lines

This means we clip all page sizes greater than pagesize from container->pgsizes... It doesn't look like ROUND_UP() semantics to me.

Ah. Extracting it as a new function with meaningful name would help code review :)

Quoted 9 lines
Quoted 8 lines
Quoted 6 lines
hw/vfio/spapr.c +1 -0
+    pagesize = pgmask ? (1ULL << (63 - clz64(pgmask))) : 0;
       if (!pagesize) {
           error_report("Host doesn't support page size 0x%"PRIx64
                        ", the supported mask is 0x%lx",
Reviewed-by on v1 1/1
#4 lore raw
In reply to Alexey Kardashevskiy
Quoted 5 lines · On Tue, 24 Mar 2020 17:39:12 +1100 Alexey Kardashevskiy <[email protected]> wrote:

Coverity detected an issue (CID 1421903) with potential call of clz64(0) which returns 64 which make it do "<<" with a negative number.

This checks the mask and avoids undefined behaviour.

Quoted 7 lines

In practice pgsizes and memory_region_iommu_get_min_page_size() always have some common page sizes and even if they did not, the resulting page size would be 0x8000.0000.0000.0000 (gcc 9.2) and ioctl(VFIO_IOMMU_SPAPR_TCE_CREATE) would fail anyway.

---

Quoted 27 lines
 hw/vfio/spapr.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/vfio/spapr.c +3 -3
index 33692fc86fd6..2900bd19417a 100644
@@ -147,7 +147,7 @@ int vfio_spapr_create_window(VFIOContainer *container,
 {
     int ret = 0;
     IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
-    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr);
+    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr), pgmask;
     unsigned entries, bits_total, bits_per_level, max_levels;
     struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) };
     long rampagesize = qemu_minrampagesize();
@@ -159,8 +159,8 @@ int vfio_spapr_create_window(VFIOContainer *container,
     if (pagesize > rampagesize) {
         pagesize = rampagesize;
     }
-    pagesize = 1ULL << (63 - clz64(container->pgsizes &
-                                   (pagesize | (pagesize - 1))));
+    pgmask = container->pgsizes & (pagesize | (pagesize - 1));
+    pagesize = pgmask ? (1ULL << (63 - clz64(pgmask))) : 0;
     if (!pagesize) {
         error_report("Host doesn't support page size 0x%"PRIx64
                      ", the supported mask is 0x%lx",
Applied
#5 lore raw
In reply to Alexey Kardashevskiy
Quoted 11 lines · Alexey Kardashevskiy

Coverity detected an issue (CID 1421903) with potential call of clz64(0) which returns 64 which make it do "<<" with a negative number.

This checks the mask and avoids undefined behaviour.

In practice pgsizes and memory_region_iommu_get_min_page_size() always have some common page sizes and even if they did not, the resulting page size would be 0x8000.0000.0000.0000 (gcc 9.2) and ioctl(VFIO_IOMMU_SPAPR_TCE_CREATE) would fail anyway.

Applied to ppc-for-5.1.

Quoted 28 lines
---
 hw/vfio/spapr.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/vfio/spapr.c +3 -3
index 33692fc86fd6..2900bd19417a 100644
@@ -147,7 +147,7 @@ int vfio_spapr_create_window(VFIOContainer *container,
 {
     int ret = 0;
     IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
-    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr);
+    uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr), pgmask;
     unsigned entries, bits_total, bits_per_level, max_levels;
     struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) };
     long rampagesize = qemu_minrampagesize();
@@ -159,8 +159,8 @@ int vfio_spapr_create_window(VFIOContainer *container,
     if (pagesize > rampagesize) {
         pagesize = rampagesize;
     }
-    pagesize = 1ULL << (63 - clz64(container->pgsizes &
-                                   (pagesize | (pagesize - 1))));
+    pgmask = container->pgsizes & (pagesize | (pagesize - 1));
+    pagesize = pgmask ? (1ULL << (63 - clz64(pgmask))) : 0;
     if (!pagesize) {
         error_report("Host doesn't support page size 0x%"PRIx64
                      ", the supported mask is 0x%lx",
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
#6 lore raw
Quoted 7 lines · David Gibson
Quoted 5 lines · Alexey Kardashevskiy

Coverity detected an issue (CID 1421903) with potential call of clz64(0) which returns 64 which make it do "<<" with a negative number.

This checks the mask and avoids undefined behaviour.

Quoted 8 lines
Quoted 6 lines

In practice pgsizes and memory_region_iommu_get_min_page_size() always have some common page sizes and even if they did not, the resulting page size would be 0x8000.0000.0000.0000 (gcc 9.2) and ioctl(VFIO_IOMMU_SPAPR_TCE_CREATE) would fail anyway.

Applied to ppc-for-5.1.

As a coverity-issue-fix it would be nice to have this in 5.0 unless you think it's particularly risky.

thanks
-- PMM

#7 lore raw
Quoted 13 lines · Peter Maydell
Quoted 12 lines · David Gibson
Quoted 10 lines · Alexey Kardashevskiy

Coverity detected an issue (CID 1421903) with potential call of clz64(0) which returns 64 which make it do "<<" with a negative number.

This checks the mask and avoids undefined behaviour.

In practice pgsizes and memory_region_iommu_get_min_page_size() always have some common page sizes and even if they did not, the resulting page size would be 0x8000.0000.0000.0000 (gcc 9.2) and ioctl(VFIO_IOMMU_SPAPR_TCE_CREATE) would fail anyway.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Applied to ppc-for-5.1.

As a coverity-issue-fix it would be nice to have this in 5.0 unless you think it's particularly risky.

In fact, I realized that shortly after and moved it.

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