Skip to contentSkip to conversation

s390x/pci: some translation fixes

PATCH Superseded Matthew Rosato 3 participants 9 messages Sep 28 – Sep 29

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover s390x/pci: some translation fixes

While reviewing Konstantin's migration series, a few edge cases have been identified in the existing code. Let's tighten them up first.

Matthew Rosato (3):
s390x/pci: Fix frame stepping in rpcit_service_call() s390x/pci: Use qemu_log_mask() for guest-triggered errors in

    reg_ioat()

s390x/pci: Reject apertures beyond IOAT capacity in translation mode

 hw/s390x/s390-pci-inst.c        | 24 +++++++++++++++++++++---
 include/hw/s390x/s390-pci-bus.h |  1 +
 2 files changed, 22 insertions(+), 3 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/3 s390x/pci: Fix frame stepping in rpcit_service_call()

When walking the guest IO address table, an invalid segment or region entry causes table_translate() to return entry.len equal to the frame size for that table level (1 MiB for ST, 2 GiB for RT). Adding entry.len directly to start only advances to the correct next frame boundary if start is already frame-aligned at that level -- which is true for a well-behaved Linux guest but not a general requirement.

Add logic to advance start to the next frame boundary rather than simply adding entry.len. For a frame-aligned start (the typical, well-behaved case) the result is identical.

Fixes 0125861eacc3 ("s390x/pci: fixup the code walking IOMMU tables")
---
 hw/s390x/s390-pci-inst.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/s390x/s390-pci-inst.c +2 -1
index 652e1712ec..a51543df44 100644
@@ -808,7 +808,8 @@ int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra)
             coalesce = 0;
         }
 
-        start += entry.len;
+        /* Advance to next frame boundary if start was not frame-aligned */
+        start = (start & ~(entry.len - 1)) + entry.len;
         while (entry.iova < start && entry.iova < end) {
             if (dma_avail > 0 || entry.perm == IOMMU_NONE) {
                 dma_avail = s390_pci_update_iotlb(iommu, &entry);
Signature · 1 line
2.55.0
Acked-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] s390x/pci: Fix frame stepping in rpcit_service_call()

Am 28.09.26 um 22:23 schrieb Matthew Rosato:

Quoted 7 lines

When walking the guest IO address table, an invalid segment or region entry causes table_translate() to return entry.len equal to the frame size for that table level (1 MiB for ST, 2 GiB for RT). Adding entry.len directly to start only advances to the correct next frame boundary if start is already frame-aligned at that level -- which is true for a well-behaved Linux guest but not a general requirement.

Quoted 7 lines

Add logic to advance start to the next frame boundary rather than simply adding entry.len. For a frame-aligned start (the typical, well-behaved case) the result is identical.

Fixes 0125861eacc3 ("s390x/pci: fixup the code walking IOMMU tables")

Looks good to me.

Quoted 18 lines
---
  hw/s390x/s390-pci-inst.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
hw/s390x/s390-pci-inst.c +2 -1
index 652e1712ec..a51543df44 100644
@@ -808,7 +808,8 @@ int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra)
              coalesce = 0;
          }
 
-        start += entry.len;
+        /* Advance to next frame boundary if start was not frame-aligned */
+        start = (start & ~(entry.len - 1)) + entry.len;
          while (entry.iova < start && entry.iova < end) {
              if (dma_avail > 0 || entry.perm == IOMMU_NONE) {
                  dma_avail = s390_pci_update_iotlb(iommu, &entry);
#3 lore raw

Re: [PATCH 1/3] s390x/pci: Fix frame stepping in rpcit_service_call()

In reply to Matthew Rosato

Hi Matthew,

Quoted 23 lines · Matthew Rosato

When walking the guest IO address table, an invalid segment or region entry causes table_translate() to return entry.len equal to the frame size for that table level (1 MiB for ST, 2 GiB for RT). Adding entry.len directly to start only advances to the correct next frame boundary if start is already frame-aligned at that level -- which is true for a well-behaved Linux guest but not a general requirement.

Add logic to advance start to the next frame boundary rather than simply adding entry.len. For a frame-aligned start (the typical, well-behaved case) the result is identical.

Fixes 0125861eacc3 ("s390x/pci: fixup the code walking IOMMU tables")
---
  hw/s390x/s390-pci-inst.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
hw/s390x/s390-pci-inst.c +0 -0
index 652e1712ec..a51543df44 100644

hw/s390x/s390-pci-inst.c int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra) · line 812

Quoted 6 lines
hw/s390x/s390-pci-inst.c +2 -1
@@ -808,7 +808,8 @@ int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra)
              coalesce = 0;
          }
 
-        start += entry.len;
+        /* Advance to next frame boundary if start was not frame-aligned */
+        start = (start & ~(entry.len - 1)) + entry.len;

Could that be QEMU_ALIGN_UP()?

Quoted 3 lines
hw/s390x/s390-pci-inst.c +0 -0
          while (entry.iova < start && entry.iova < end) {
              if (dma_avail > 0 || entry.perm == IOMMU_NONE) {
                  dma_avail = s390_pci_update_iotlb(iommu, &entry);
#4 lore raw

Re: [PATCH 1/3] s390x/pci: Fix frame stepping in rpcit_service_call()

Quoted 6 lines
Quoted 4 lines

+        /* Advance to next frame boundary if start was not frame- aligned */
+        start = (start & ~(entry.len - 1)) + entry.len;

Could that be QEMU_ALIGN_UP()?

Oh, good idea. I think

start = QEMU_ALIGN_UP(start + 1, entry.len);

would work to ensure we always move up.

I'll test it out.

Thanks,
Matt

#5 lore raw

PATCH 2/3 s390x/pci: Use qemu_log_mask() for guest-triggered errors in reg_ioat()

In reply to Matthew Rosato

The error paths in reg_ioat() that are triggered by a guest providing invalid MPCIFC arguments use error_report(), which writes unconditionally to stderr. A misbehaving guest can exploit this to flood the host log by repeatedly issuing the instruction with bad arguments.

Replace these with qemu_log_mask(LOG_GUEST_ERROR, ...) so that the messages are suppressible.

Fixes 863f6f52b713 ("s390: implement pci instructions")
Fixes dfcee1ea4c52 ("s390x/pci: add support for guests that request direct mapping")
---
 hw/s390x/s390-pci-inst.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/s390x/s390-pci-inst.c +4 -2
index a51543df44..c6da3b4c07 100644
@@ -17,6 +17,7 @@
 #include "system/memory.h"
 #include "qemu/error-report.h"
 #include "qemu/bswap.h"
+#include "qemu/log.h"
 #include "system/hw_accel.h"
 #include "hw/core/boards.h"
 #include "hw/pci/pci_device.h"
@@ -1044,11 +1045,12 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib,
 
     /* currently we only support designation type 1 with translation */
     if (t && dt != ZPCI_IOTA_RTTO) {
-        error_report("unsupported ioat dt %d t %d", dt, t);
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "unsupported ioat dt %d t %d", dt, t);
         s390_program_interrupt(env, PGM_OPERAND, ra);
         return -EINVAL;
     } else if (!t && !pbdev->rtr_avail) {
-        error_report("relaxed translation not allowed");
+        qemu_log_mask(LOG_GUEST_ERROR, "relaxed translation not allowed");
         s390_program_interrupt(env, PGM_OPERAND, ra);
         return -EINVAL;
     }
Signature · 1 line
2.55.0
#6 lore raw

Re: [PATCH 2/3] s390x/pci: Use qemu_log_mask() for guest-triggered errors in reg_ioat()

Am 28.09.26 um 22:23 schrieb Matthew Rosato:

Quoted 21 lines

The error paths in reg_ioat() that are triggered by a guest providing invalid MPCIFC arguments use error_report(), which writes unconditionally to stderr. A misbehaving guest can exploit this to flood the host log by repeatedly issuing the instruction with bad arguments.

Replace these with qemu_log_mask(LOG_GUEST_ERROR, ...) so that the messages are suppressible.

Fixes 863f6f52b713 ("s390: implement pci instructions")
Fixes dfcee1ea4c52 ("s390x/pci: add support for guests that request direct mapping")
---
  hw/s390x/s390-pci-inst.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
hw/s390x/s390-pci-inst.c +0 -0
index a51543df44..c6da3b4c07 100644

hw/s390x/s390-pci-inst.c line 18

Quoted 2 lines
hw/s390x/s390-pci-inst.c +0 -0
@@ -17,6 +17,7 @@
  #include "system/memory.h"
  #include "qemu/error-report.h"

can we drop that now?

Quoted 7 lines
hw/s390x/s390-pci-inst.c +1 -0
  #include "qemu/bswap.h"
+#include "qemu/log.h"
  #include "system/hw_accel.h"
  #include "hw/core/boards.h"
  #include "hw/pci/pci_device.h"
@@ -1044,11 +1045,12 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib,
 

hw/s390x/s390-pci-inst.c static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib, · line 1053

Quoted 10 lines
hw/s390x/s390-pci-inst.c +3 -2
      /* currently we only support designation type 1 with translation */
      if (t && dt != ZPCI_IOTA_RTTO) {
-        error_report("unsupported ioat dt %d t %d", dt, t);
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "unsupported ioat dt %d t %d", dt, t);
          s390_program_interrupt(env, PGM_OPERAND, ra);
          return -EINVAL;
      } else if (!t && !pbdev->rtr_avail) {
-        error_report("relaxed translation not allowed");
+        qemu_log_mask(LOG_GUEST_ERROR, "relaxed translation not allowed");

I think we need "\n" here. error_report does add it, qemu_log_mask not.

Quoted 3 lines
hw/s390x/s390-pci-inst.c +0 -0
          s390_program_interrupt(env, PGM_OPERAND, ra);
          return -EINVAL;
      }
#7 lore raw

Re: [PATCH 2/3] s390x/pci: Use qemu_log_mask() for guest-triggered errors in reg_ioat()

Quoted 20 lines · Christian Borntraeger

Am 28.09.26 um 22:23 schrieb Matthew Rosato:

Quoted 17 lines

The error paths in reg_ioat() that are triggered by a guest providing invalid MPCIFC arguments use error_report(), which writes unconditionally to stderr.  A misbehaving guest can exploit this to flood the host log by repeatedly issuing the instruction with bad arguments.

Replace these with qemu_log_mask(LOG_GUEST_ERROR, ...) so that the messages are suppressible.

Fixes 863f6f52b713 ("s390: implement pci instructions")
Fixes dfcee1ea4c52 ("s390x/pci: add support for guests that request

direct mapping")

---
  hw/s390x/s390-pci-inst.c | 6 ++++--

  1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 9 lines

hw/s390x/s390-pci-inst.c line 18

Quoted 7 lines
hw/s390x/s390-pci-inst.c +0 -0
index a51543df44..c6da3b4c07 100644
@@ -17,6 +17,7 @@
   #include "system/memory.h"
   #include z

can we drop that now?

Looks like yes, will drop

Quoted 14 lines
Quoted 14 lines
hw/s390x/s390-pci-inst.c +1 -0
   #include "qemu/bswap.h"
+#include "qemu/log.h"
   #include "system/hw_accel.h"
   #include "hw/core/boards.h"
   #include "hw/pci/pci_device.h"
@@ -1044,11 +1045,12 @@ static int reg_ioat(CPUS390XState *env,
 S390PCIBusDevice *pbdev, ZpciFib fib,
         /* currently we only support designation type 1 with

translation */
      if (t && dt != ZPCI_IOTA_RTTO) {
-        error_report("unsupported ioat dt %d t %d", dt, t); +        qemu_log_mask(LOG_GUEST_ERROR,
+                      "unsupported ioat dt %d t %d", dt, t);

Quoted 9 lines
Quoted 6 lines

          s390_program_interrupt(env, PGM_OPERAND, ra); return -EINVAL;
      } else if (!t && !pbdev->rtr_avail) {
-        error_report("relaxed translation not allowed"); +        qemu_log_mask(LOG_GUEST_ERROR, "relaxed translation not allowed");

I think we need "\n" here. error_report does add it, qemu_log_mask not.

ACK, just verified - will fix here and in patch 3.

Quoted 5 lines
Quoted 4 lines

          s390_program_interrupt(env, PGM_OPERAND, ra); return -EINVAL;
      }

#8 lore raw

PATCH 3/3 s390x/pci: Reject apertures beyond IOAT capacity in translation mode

In reply to Matthew Rosato

In order to support direct mapping (which is only bound by the aperture size of the underlying host EDMA reported via vfio) QEMU reports the host-provided EDMA to the guest. However, when using translation mode QEMU only supports up to 4 TiB currently (the size of a single RT table).

Reject attempts by a guest to register a translation aperture that would exceed what QEMU can support by giving an operand exception on the MPCIFC instruction that requested the large aperture. This restriction can be removed in the future if/when support for additional IOAT regions is added.

---
 hw/s390x/s390-pci-inst.c        | 15 +++++++++++++++
 include/hw/s390x/s390-pci-bus.h |  1 +
 2 files changed, 16 insertions(+)
hw/s390x/s390-pci-inst.c +15 -0
index c6da3b4c07..c7658e7eca 100644
@@ -1055,6 +1055,21 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib,
         return -EINVAL;
     }
 
+    /*
+     * We report an EDMA that may exceed what QEMU can handle in support
+     * of direct-mapping.  If the guest attempts to register an IOAT that
+     * is too large, reject it with an informative message.  Only direct
+     * mapping can be used for guests of this size until support is added
+     * to QEMU for additional IOAT regions.
+     */
+    if (t && pal >= ZPCI_TABLE_SIZE_RT) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "ioat pal 0x%"PRIx64" exceeds max translatable address",
+                      pal);
+        s390_program_interrupt(env, PGM_OPERAND, ra);
+        return -EINVAL;
+    }
+
     iommu->pba = pba;
     iommu->pal = pal;
     iommu->g_iota = g_iota;
include/hw/s390x/s390-pci-bus.h +1 -0
index 9228523ce8..f182bab246 100644
@@ -140,6 +140,7 @@ enum ZpciIoatDtype {
 #define ZPCI_PT_BITS            8
 #define ZPCI_ST_SHIFT           (ZPCI_PT_BITS + TARGET_PAGE_BITS)
 #define ZPCI_RT_SHIFT           (ZPCI_ST_SHIFT + ZPCI_TABLE_BITS)
+#define ZPCI_TABLE_SIZE_RT      (1ULL << (ZPCI_RT_SHIFT + ZPCI_TABLE_BITS))
 
 #define ZPCI_RTE_FLAG_MASK      0x3fffULL
 #define ZPCI_RTE_ADDR_MASK      (~ZPCI_RTE_FLAG_MASK)
Signature · 1 line
2.55.0