Skip to contentSkip to conversation

Add a 'in_mmio' device flag to avoid the DMA to MMIO

RFC In review Li Qiang 7 participants 18 messages Sep 8, 2020 – Sep 21, 2020

lore
Share & tools
lore

Li Qiang

author
#0 lore raw

cover Add a 'in_mmio' device flag to avoid the DMA to MMIO

Currently the qemu device fuzzer find some DMA to MMIO issue. If the device handling MMIO currently trigger a DMA which the address is MMIO, this will reenter the device MMIO handler. As some of the device doesn't consider this it will sometimes crash the qemu.

This patch tries to solve this by adding a per-device flag 'in_mmio'. When the memory core dispatch MMIO it will check/set this flag and when it leaves it will clean this flag.

Li Qiang (4):
memory: add memory_region_init_io_with_dev interface
memory: avoid reenter the device's MMIO handler while processing MMIO
e1000e: use the new memory_region_init_io_with_dev interface
hcd-xhci: use the new memory_region_init_io_with_dev interface

 hw/net/e1000e.c        |  8 ++++----
 hw/usb/hcd-xhci.c      | 25 ++++++++++++++---------
 include/exec/memory.h  |  9 +++++++++
 include/hw/qdev-core.h |  1 +
 softmmu/memory.c       | 46 +++++++++++++++++++++++++++++++++++++++---
 5 files changed, 72 insertions(+), 17 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

RFC 1/4 memory: add memory_region_init_io_with_dev interface

Currently the MR is not explicitly connecting with its device instead of a opaque. In most situation this opaque is the deivce but it is not an enforcement. This patch adds a DeviceState member of to MemoryRegion we will use it in later patch.

---
 include/exec/memory.h |  9 +++++++++
 softmmu/memory.c      | 15 +++++++++++++++
 2 files changed, 24 insertions(+)
include/exec/memory.h +9 -0
index 0cfe987ab4..620fb12d9b 100644
@@ -404,6 +404,7 @@ struct MemoryRegion {
     const char *name;
     unsigned ioeventfd_nb;
     MemoryRegionIoeventfd *ioeventfds;
+    DeviceState *dev;
 };
 
 struct IOMMUMemoryRegion {
@@ -794,6 +795,14 @@ void memory_region_init_io(MemoryRegion *mr,
                            const char *name,
                            uint64_t size);
 
+void memory_region_init_io_with_dev(MemoryRegion *mr,
+                           struct Object *owner,
+                           const MemoryRegionOps *ops,
+                           void *opaque,
+                           const char *name,
+                           uint64_t size,
+                           DeviceState *dev);
+
 /**
  * memory_region_init_ram_nomigrate:  Initialize RAM memory region.  Accesses
  *                                    into the region will modify memory
softmmu/memory.c +15 -0
index 70b93104e8..2628c9d2d9 100644
@@ -1490,6 +1490,21 @@ void memory_region_init_io(MemoryRegion *mr,
     mr->terminates = true;
 }
 
+void memory_region_init_io_with_dev(MemoryRegion *mr,
+                           Object *owner,
+                           const MemoryRegionOps *ops,
+                           void *opaque,
+                           const char *name,
+                           uint64_t size,
+                           DeviceState *dev)
+{
+    memory_region_init(mr, owner, name, size);
+    mr->ops = ops ? ops : &unassigned_mem_ops;
+    mr->opaque = opaque;
+    mr->terminates = true;
+    mr->dev = dev;
+}
+
 void memory_region_init_ram_nomigrate(MemoryRegion *mr,
                                       Object *owner,
                                       const char *name,
Signature · 1 line
2.17.1
#2 lore raw

Re: [RFC 1/4] memory: add memory_region_init_io_with_dev interface

Quoted 4 lines · Li Qiang

Currently the MR is not explicitly connecting with its device instead of a opaque. In most situation this opaque is the deivce but it is not an enforcement. This patch adds a DeviceState member of to MemoryRegion we will use it in later patch.

I don't have a deep investigation. But I wonder whether we could make sure of owner instead of adding a new field here.

Thanks

Quoted 60 lines
---
  include/exec/memory.h |  9 +++++++++
  softmmu/memory.c      | 15 +++++++++++++++
  2 files changed, 24 insertions(+)
include/exec/memory.h +9 -0
index 0cfe987ab4..620fb12d9b 100644
@@ -404,6 +404,7 @@ struct MemoryRegion {
      const char *name;
      unsigned ioeventfd_nb;
      MemoryRegionIoeventfd *ioeventfds;
+    DeviceState *dev;
  };
 
  struct IOMMUMemoryRegion {
@@ -794,6 +795,14 @@ void memory_region_init_io(MemoryRegion *mr,
                             const char *name,
                             uint64_t size);
 
+void memory_region_init_io_with_dev(MemoryRegion *mr,
+                           struct Object *owner,
+                           const MemoryRegionOps *ops,
+                           void *opaque,
+                           const char *name,
+                           uint64_t size,
+                           DeviceState *dev);
+
  /**
   * memory_region_init_ram_nomigrate:  Initialize RAM memory region.  Accesses
   *                                    into the region will modify memory
softmmu/memory.c +15 -0
index 70b93104e8..2628c9d2d9 100644
@@ -1490,6 +1490,21 @@ void memory_region_init_io(MemoryRegion *mr,
      mr->terminates = true;
  }
 
+void memory_region_init_io_with_dev(MemoryRegion *mr,
+                           Object *owner,
+                           const MemoryRegionOps *ops,
+                           void *opaque,
+                           const char *name,
+                           uint64_t size,
+                           DeviceState *dev)
+{
+    memory_region_init(mr, owner, name, size);
+    mr->ops = ops ? ops : &unassigned_mem_ops;
+    mr->opaque = opaque;
+    mr->terminates = true;
+    mr->dev = dev;
+}
+
  void memory_region_init_ram_nomigrate(MemoryRegion *mr,
                                        Object *owner,
                                        const char *name,
#3 lore raw

Re: [RFC 1/4] memory: add memory_region_init_io_with_dev interface

Quoted 11 lines · Jason Wang <[email protected]> 于2020年9月9日周三 上午10:16写道:
Quoted 4 lines · Li Qiang

Currently the MR is not explicitly connecting with its device instead of a opaque. In most situation this opaque is the deivce but it is not an enforcement. This patch adds a DeviceState member of to MemoryRegion we will use it in later patch.

I don't have a deep investigation. But I wonder whether we could make sure of owner instead of adding a new field here.

I have did some investigation.

void memory_region_init_io(MemoryRegion *mr,
struct Object *owner,
const MemoryRegionOps *ops,
void *opaque,
const char *name,
uint64_t size);

memory_region_init_io now mostly connects to the device with an opaque member. But it has no guaranteen that this should be true. So we can't assume this.

The 'owner' is just in the 'object' context.

For the MR itself, MR may have sub-MR and alias. This will complicated the issue.

As the device emulation and MR has a clear relation. I think add such field is reasonable.

Thanks,
Li Qiang

Quoted 65 lines

Thanks

Quoted 60 lines
---
  include/exec/memory.h |  9 +++++++++
  softmmu/memory.c      | 15 +++++++++++++++
  2 files changed, 24 insertions(+)
include/exec/memory.h +9 -0
index 0cfe987ab4..620fb12d9b 100644
@@ -404,6 +404,7 @@ struct MemoryRegion {
      const char *name;
      unsigned ioeventfd_nb;
      MemoryRegionIoeventfd *ioeventfds;
+    DeviceState *dev;
  };
 
  struct IOMMUMemoryRegion {
@@ -794,6 +795,14 @@ void memory_region_init_io(MemoryRegion *mr,
                             const char *name,
                             uint64_t size);
 
+void memory_region_init_io_with_dev(MemoryRegion *mr,
+                           struct Object *owner,
+                           const MemoryRegionOps *ops,
+                           void *opaque,
+                           const char *name,
+                           uint64_t size,
+                           DeviceState *dev);
+
  /**
   * memory_region_init_ram_nomigrate:  Initialize RAM memory region.  Accesses
   *                                    into the region will modify memory
softmmu/memory.c +15 -0
index 70b93104e8..2628c9d2d9 100644
@@ -1490,6 +1490,21 @@ void memory_region_init_io(MemoryRegion *mr,
      mr->terminates = true;
  }
 
+void memory_region_init_io_with_dev(MemoryRegion *mr,
+                           Object *owner,
+                           const MemoryRegionOps *ops,
+                           void *opaque,
+                           const char *name,
+                           uint64_t size,
+                           DeviceState *dev)
+{
+    memory_region_init(mr, owner, name, size);
+    mr->ops = ops ? ops : &unassigned_mem_ops;
+    mr->opaque = opaque;
+    mr->terminates = true;
+    mr->dev = dev;
+}
+
  void memory_region_init_ram_nomigrate(MemoryRegion *mr,
                                        Object *owner,
                                        const char *name,
#4 lore raw

Re: [RFC 1/4] memory: add memory_region_init_io_with_dev interface

In reply to Jason Wang
Quoted 10 lines · Jason Wang
Quoted 4 lines · Li Qiang

Currently the MR is not explicitly connecting with its device instead of a opaque. In most situation this opaque is the deivce but it is not an enforcement. This patch adds a DeviceState member of to MemoryRegion we will use it in later patch.

I don't have a deep investigation. But I wonder whether we could make sure of owner instead of adding a new field here.

Should be possible. There is object_dynamic_cast() which can be used to figure whenever a given owner object is a device.

take care,
Gerd

#5 lore raw

Re: [RFC 1/4] memory: add memory_region_init_io_with_dev interface

Quoted 10 lines · Gerd Hoffmann <[email protected]> 于2020年9月9日周三 下午12:49写道:
Quoted 8 lines · Jason Wang
Quoted 4 lines · Li Qiang

Currently the MR is not explicitly connecting with its device instead of a opaque. In most situation this opaque is the deivce but it is not an enforcement. This patch adds a DeviceState member of to MemoryRegion we will use it in later patch.

Quoted 6 lines
Quoted 2 lines

I don't have a deep investigation. But I wonder whether we could make sure of owner instead of adding a new field here.

Should be possible. There is object_dynamic_cast() which can be used to figure whenever a given owner object is a device.

I found most caller of 'memory_region_init_io' will set the owner to the device object.
But some of them will just set it to NULL. Do will have a clear rule that the device's MR
'owner' should be the device object? If yes, we can use this field.

Thanks,
Li Qiang

Quoted 3 lines

take care,
Gerd

#6 lore raw

Re: [RFC 1/4] memory: add memory_region_init_io_with_dev interface

Quoted 18 lines · Li Qiang
Quoted 10 lines · Gerd Hoffmann <[email protected]> 于2020年9月9日周三 下午12:49写道:
Quoted 8 lines · Jason Wang
Quoted 4 lines · Li Qiang

Currently the MR is not explicitly connecting with its device instead of a opaque. In most situation this opaque is the deivce but it is not an enforcement. This patch adds a DeviceState member of to MemoryRegion we will use it in later patch.

Quoted 6 lines
Quoted 2 lines

I don't have a deep investigation. But I wonder whether we could make sure of owner instead of adding a new field here.

Should be possible. There is object_dynamic_cast() which can be used to figure whenever a given owner object is a device.

Quoted 6 lines

I found most caller of 'memory_region_init_io' will set the owner to the device object.
But some of them will just set it to NULL. Do will have a clear rule that the device's MR
'owner' should be the device object? If yes, we can use this field.

Those seem to be devices that havent't been QOM-imfied yet? Maybe those devices are unlikely to be affected by these issues, though...

For i386, it seems like parallel, port80, portF0, pckbd, and xen_pvdevice .. ? I'm guessing none of these do DMA.

+CC Stefan, since he replied to the other thread.

Quoted 6 lines

Thanks,
Li Qiang

Quoted 3 lines

take care,
Gerd

#7 lore raw

Re: [RFC 1/4] memory: add memory_region_init_io_with_dev interface

Quoted 27 lines · Alexander Bulekov <[email protected]> 于2020年9月9日周三 下午10:28写道:
Quoted 18 lines · Li Qiang
Quoted 10 lines · Gerd Hoffmann <[email protected]> 于2020年9月9日周三 下午12:49写道:
Quoted 8 lines · Jason Wang
Quoted 4 lines · Li Qiang

Currently the MR is not explicitly connecting with its device instead of a opaque. In most situation this opaque is the deivce but it is not an enforcement. This patch adds a DeviceState member of to MemoryRegion we will use it in later patch.

Quoted 6 lines
Quoted 2 lines

I don't have a deep investigation. But I wonder whether we could make sure of owner instead of adding a new field here.

Should be possible. There is object_dynamic_cast() which can be used to figure whenever a given owner object is a device.

Quoted 6 lines

I found most caller of 'memory_region_init_io' will set the owner to the device object.
But some of them will just set it to NULL. Do will have a clear rule that the device's MR
'owner' should be the device object? If yes, we can use this field.

Quoted 3 lines

Those seem to be devices that havent't been QOM-imfied yet? Maybe those devices are unlikely to be affected by these issues, though...

No it seems not related QOM-ified.

Quoted 3 lines

For i386, it seems like parallel, port80, portF0, pckbd, and xen_pvdevice .. ? I'm guessing none of these do DMA.

In fact xen_pvdevice is MMIO but the handlers does nothing.

There are some other example than i386 such as the riscv in hw/riscv/sifive_uart.c

If we have a rule to force the 'MR's owner to the device then we can fix these NULL owner MR.

Thanks,
Li Qiang

Quoted 8 lines

+CC Stefan, since he replied to the other thread.

Quoted 6 lines

Thanks,
Li Qiang

Quoted 3 lines

take care,
Gerd

#8 lore raw

Re: [RFC 1/4] memory: add memory_region_init_io_with_dev interface

Quoted 31 lines · Li Qiang
Quoted 14 lines · Alexander Bulekov <[email protected]> 于2020年9月9日周三 下午10:28写道:
Quoted 13 lines · Li Qiang
Quoted 12 lines · Gerd Hoffmann <[email protected]> 于2020年9月9日周三 下午12:49写道:
Quoted 8 lines · Jason Wang
Quoted 4 lines · Li Qiang

Currently the MR is not explicitly connecting with its device instead of a opaque. In most situation this opaque is the deivce but it is not an enforcement. This patch adds a DeviceState member of to MemoryRegion we will use it in later patch.

I don't have a deep investigation. But I wonder whether we could make sure of owner instead of adding a new field here.

Should be possible. There is object_dynamic_cast() which can be used to figure whenever a given owner object is a device.

Quoted 9 lines
Quoted 6 lines

I found most caller of 'memory_region_init_io' will set the owner to the device object.
But some of them will just set it to NULL. Do will have a clear rule that the device's MR
'owner' should be the device object? If yes, we can use this field.

Those seem to be devices that havent't been QOM-imfied yet? Maybe those devices are unlikely to be affected by these issues, though...

No it seems not related QOM-ified.

Quoted 3 lines

For i386, it seems like parallel, port80, portF0, pckbd, and xen_pvdevice .. ? I'm guessing none of these do DMA.

In fact xen_pvdevice is MMIO but the handlers does nothing.

Quoted 8 lines

There are some other example than i386 such as the riscv in hw/riscv/sifive_uart.c

If we have a rule to force the 'MR's owner to the device then we can fix these NULL owner MR.

Thanks,
Li Qiang

I guess maybe we can start from the ones whose owner is a device and convert the rest on top (if necessary)?

Thanks

Quoted 9 lines
Quoted 9 lines

+CC Stefan, since he replied to the other thread.

Quoted 6 lines

Thanks,
Li Qiang

Quoted 3 lines

take care,
Gerd

#9 lore raw

Re: [RFC 1/4] memory: add memory_region_init_io_with_dev interface

In reply to Li Qiang
Quoted 16 lines · Li Qiang

Currently the MR is not explicitly connecting with its device instead of a opaque. In most situation this opaque is the deivce but it is not an enforcement. This patch adds a DeviceState member of to MemoryRegion we will use it in later patch.

---
 include/exec/memory.h |  9 +++++++++
 softmmu/memory.c      | 15 +++++++++++++++
 2 files changed, 24 insertions(+)
include/exec/memory.h +0 -0
index 0cfe987ab4..620fb12d9b 100644

include/exec/memory.h struct MemoryRegion { · line 407

Quoted 4 lines
include/exec/memory.h +1 -0
@@ -404,6 +404,7 @@ struct MemoryRegion {
     const char *name;
     unsigned ioeventfd_nb;
     MemoryRegionIoeventfd *ioeventfds;
+    DeviceState *dev;

There is already an owner field for this.

Paolo

author
#10 lore raw

RFC 2/4 memory: avoid reenter the device's MMIO handler while processing MMIO

In reply to Li Qiang

This patch adds a 'in_mmio' flag to 'DeviceState' to indicate that the device is doing MMIO path. This can avoid the malicious guest do DMA to MMIO and crash the qemu.

---
 include/hw/qdev-core.h |  1 +
 softmmu/memory.c       | 31 ++++++++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 3 deletions(-)
include/hw/qdev-core.h +1 -0
index ea3f73a282..c6f4ebba9e 100644
@@ -177,6 +177,7 @@ struct DeviceState {
     char *canonical_path;
     bool realized;
     bool pending_deleted_event;
+    bool in_mmio;
     QemuOpts *opts;
     int hotplugged;
     bool allow_unplug_during_migration;
softmmu/memory.c +28 -3
index 2628c9d2d9..7be44f7175 100644
@@ -1410,8 +1410,20 @@ MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
         return MEMTX_DECODE_ERROR;
     }
 
+    if (mr->dev) {
+        if (mr->dev->in_mmio) {
+            return MEMTX_ERROR;
+        } else {
+            mr->dev->in_mmio = true;
+        }
+    }
+
     r = memory_region_dispatch_read1(mr, addr, pval, size, attrs);
     adjust_endianness(mr, pval, op);
+
+    if (mr->dev) {
+        mr->dev->in_mmio = false;
+    }
     return r;
 }
 
@@ -1448,6 +1460,7 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
                                          MemTxAttrs attrs)
 {
     unsigned size = memop_size(op);
+    MemTxResult ret;
 
     if (!memory_region_access_valid(mr, addr, size, true, attrs)) {
         unassigned_mem_write(mr, addr, data, size);
@@ -1461,20 +1474,32 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
         return MEMTX_OK;
     }
 
+    if (mr->dev) {
+        if (mr->dev->in_mmio) {
+            return MEMTX_ERROR;
+        } else {
+            mr->dev->in_mmio = true;
+        }
+    }
+
     if (mr->ops->write) {
-        return access_with_adjusted_size(addr, &data, size,
+        ret = access_with_adjusted_size(addr, &data, size,
                                          mr->ops->impl.min_access_size,
                                          mr->ops->impl.max_access_size,
                                          memory_region_write_accessor, mr,
                                          attrs);
     } else {
-        return
-            access_with_adjusted_size(addr, &data, size,
+        ret = access_with_adjusted_size(addr, &data, size,
                                       mr->ops->impl.min_access_size,
                                       mr->ops->impl.max_access_size,
                                       memory_region_write_with_attrs_accessor,
                                       mr, attrs);
     }
+    if (mr->dev) {
+        mr->dev->in_mmio = false;
+    }
+
+    return ret;
 }
 
 void memory_region_init_io(MemoryRegion *mr,
Signature · 1 line
2.17.1
author
#11 lore raw

RFC 3/4 e1000e: use the new memory_region_init_io_with_dev interface

In reply to Li Qiang

This can avoid the DMA to MMIO issue here:

https://bugs.launchpad.net/qemu/+bug/1886362

---
 hw/net/e1000e.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/net/e1000e.c +4 -4
index fda34518c9..0aac5cea2e 100644
@@ -439,8 +439,8 @@ static void e1000e_pci_realize(PCIDevice *pci_dev, Error **errp)
     s->subsys_used = s->subsys;
 
     /* Define IO/MMIO regions */
-    memory_region_init_io(&s->mmio, OBJECT(s), &mmio_ops, s,
-                          "e1000e-mmio", E1000E_MMIO_SIZE);
+    memory_region_init_io_with_dev(&s->mmio, OBJECT(s), &mmio_ops, s,
+                          "e1000e-mmio", E1000E_MMIO_SIZE, &pci_dev->qdev);
     pci_register_bar(pci_dev, E1000E_MMIO_IDX,
                      PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mmio);
 
@@ -453,8 +453,8 @@ static void e1000e_pci_realize(PCIDevice *pci_dev, Error **errp)
     pci_register_bar(pci_dev, E1000E_FLASH_IDX,
                      PCI_BASE_ADDRESS_SPACE_MEMORY, &s->flash);
 
-    memory_region_init_io(&s->io, OBJECT(s), &io_ops, s,
-                          "e1000e-io", E1000E_IO_SIZE);
+    memory_region_init_io_with_dev(&s->io, OBJECT(s), &io_ops, s,
+                          "e1000e-io", E1000E_IO_SIZE, &pci_dev->qdev);
     pci_register_bar(pci_dev, E1000E_IO_IDX,
                      PCI_BASE_ADDRESS_SPACE_IO, &s->io);
 
Signature · 1 line
2.17.1
author
#12 lore raw

RFC 4/4 hcd-xhci: use the new memory_region_init_io_with_dev interface

In reply to Li Qiang

This can avoid the DMA to MMIO issue here:

https://bugs.launchpad.net/qemu/+bug/1891354

---
 hw/usb/hcd-xhci.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
hw/usb/hcd-xhci.c +15 -10
index 46a2186d91..1954ae2ae7 100644
@@ -3437,14 +3437,18 @@ static void usb_xhci_realize(struct PCIDevice *dev, Error **errp)
     xhci->mfwrap_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, xhci_mfwrap_timer, xhci);
 
     memory_region_init(&xhci->mem, OBJECT(xhci), "xhci", LEN_REGS);
-    memory_region_init_io(&xhci->mem_cap, OBJECT(xhci), &xhci_cap_ops, xhci,
-                          "capabilities", LEN_CAP);
-    memory_region_init_io(&xhci->mem_oper, OBJECT(xhci), &xhci_oper_ops, xhci,
-                          "operational", 0x400);
-    memory_region_init_io(&xhci->mem_runtime, OBJECT(xhci), &xhci_runtime_ops, xhci,
-                          "runtime", LEN_RUNTIME);
-    memory_region_init_io(&xhci->mem_doorbell, OBJECT(xhci), &xhci_doorbell_ops, xhci,
-                          "doorbell", LEN_DOORBELL);
+    memory_region_init_io_with_dev(&xhci->mem_cap, OBJECT(xhci),
+                                   &xhci_cap_ops, xhci,
+                                   "capabilities", LEN_CAP, &dev->qdev);
+    memory_region_init_io_with_dev(&xhci->mem_oper, OBJECT(xhci),
+                                   &xhci_oper_ops, xhci,
+                                   "operational", 0x400, &dev->qdev);
+    memory_region_init_io_with_dev(&xhci->mem_runtime, OBJECT(xhci),
+                                   &xhci_runtime_ops, xhci,
+                                   "runtime", LEN_RUNTIME, &dev->qdev);
+    memory_region_init_io_with_dev(&xhci->mem_doorbell, OBJECT(xhci),
+                                   &xhci_doorbell_ops, xhci,
+                                   "doorbell", LEN_DOORBELL, &dev->qdev);
 
     memory_region_add_subregion(&xhci->mem, 0,            &xhci->mem_cap);
     memory_region_add_subregion(&xhci->mem, OFF_OPER,     &xhci->mem_oper);
@@ -3455,8 +3459,9 @@ static void usb_xhci_realize(struct PCIDevice *dev, Error **errp)
         XHCIPort *port = &xhci->ports[i];
         uint32_t offset = OFF_OPER + 0x400 + 0x10 * i;
         port->xhci = xhci;
-        memory_region_init_io(&port->mem, OBJECT(xhci), &xhci_port_ops, port,
-                              port->name, 0x10);
+        memory_region_init_io_with_dev(&port->mem, OBJECT(xhci),
+                                       &xhci_port_ops, port,
+                                       port->name, 0x10, &dev->qdev);
         memory_region_add_subregion(&xhci->mem, offset, &port->mem);
     }
 
Signature · 1 line
2.17.1
#13 lore raw
In reply to Li Qiang
Quoted 8 lines · Li Qiang

Currently the qemu device fuzzer find some DMA to MMIO issue. If the device handling MMIO currently trigger a DMA which the address is MMIO, this will reenter the device MMIO handler. As some of the device doesn't consider this it will sometimes crash the qemu.

This patch tries to solve this by adding a per-device flag 'in_mmio'. When the memory core dispatch MMIO it will check/set this flag and when it leaves it will clean this flag.

What's the plan for fixing the irq issues pointed out by Peter?

Thanks

Quoted 15 lines

Li Qiang (4):
memory: add memory_region_init_io_with_dev interface
memory: avoid reenter the device's MMIO handler while processing MMIO
e1000e: use the new memory_region_init_io_with_dev interface
hcd-xhci: use the new memory_region_init_io_with_dev interface

  hw/net/e1000e.c        |  8 ++++----
  hw/usb/hcd-xhci.c      | 25 ++++++++++++++---------
  include/exec/memory.h  |  9 +++++++++
  include/hw/qdev-core.h |  1 +
  softmmu/memory.c       | 46 +++++++++++++++++++++++++++++++++++++++---
  5 files changed, 72 insertions(+), 17 deletions(-)
#14 lore raw
Quoted 8 lines · Jason Wang <[email protected]> 于2020年9月9日周三 上午10:17写道:
Quoted 5 lines · Li Qiang

Currently the qemu device fuzzer find some DMA to MMIO issue. If the device handling MMIO currently trigger a DMA which the address is MMIO, this will reenter the device MMIO handler. As some of the device doesn't consider this it will sometimes crash the qemu.

Quoted 7 lines
Quoted 3 lines

This patch tries to solve this by adding a per-device flag 'in_mmio'. When the memory core dispatch MMIO it will check/set this flag and when it leaves it will clean this flag.

What's the plan for fixing the irq issues pointed out by Peter?

Just have a basic idea. Just like this we can add a per-device flag, 'in_mmio' or 'in_emulation'
or some other names. The device need solve the irq handler/mmio and anything other reenter issue by themself
or they can just check/set/clean this flag. This way we may can define a principle which Peter mentioned that the device emulation should obey.

Thanks,
Li Qiang

Quoted 19 lines

Thanks

Quoted 15 lines

Li Qiang (4):
memory: add memory_region_init_io_with_dev interface
memory: avoid reenter the device's MMIO handler while processing MMIO
e1000e: use the new memory_region_init_io_with_dev interface
hcd-xhci: use the new memory_region_init_io_with_dev interface

  hw/net/e1000e.c        |  8 ++++----
  hw/usb/hcd-xhci.c      | 25 ++++++++++++++---------
  include/exec/memory.h  |  9 +++++++++
  include/hw/qdev-core.h |  1 +
  softmmu/memory.c       | 46 +++++++++++++++++++++++++++++++++++++++---
  5 files changed, 72 insertions(+), 17 deletions(-)
#15 lore raw
In reply to Li Qiang
Quoted 16 lines · Li Qiang

Currently the qemu device fuzzer find some DMA to MMIO issue. If the device handling MMIO currently trigger a DMA which the address is MMIO, this will reenter the device MMIO handler. As some of the device doesn't consider this it will sometimes crash the qemu.

This patch tries to solve this by adding a per-device flag 'in_mmio'. When the memory core dispatch MMIO it will check/set this flag and when it leaves it will clean this flag.

Li Qiang (4):
memory: add memory_region_init_io_with_dev interface
memory: avoid reenter the device's MMIO handler while processing MMIO
e1000e: use the new memory_region_init_io_with_dev interface
hcd-xhci: use the new memory_region_init_io_with_dev interface

Quoted 7 lines
 hw/net/e1000e.c        |  8 ++++----
 hw/usb/hcd-xhci.c      | 25 ++++++++++++++---------
 include/exec/memory.h  |  9 +++++++++
 include/hw/qdev-core.h |  1 +
 softmmu/memory.c       | 46 +++++++++++++++++++++++++++++++++++++++---
 5 files changed, 72 insertions(+), 17 deletions(-)

I don't think this is a good solution. These are device bugs and they need to be fixed.

Paolo

#16 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 4 lines · Li Qiang

Currently the qemu device fuzzer find some DMA to MMIO issue. If the device handling MMIO currently trigger a DMA which the address is MMIO, this will reenter the device MMIO handler. As some of the device doesn't consider this it will sometimes crash the qemu.

I don't think this is a good solution. These are device bugs and they need to be fixed.

Do you have an opinion on what the right approach to fixing them is? It seems like a hard problem to me; my brain has been too full of cotton wool recently and I haven't felt up to sitting down and trying to think through whether there's a clean way to handle the reentrancy-into-device-code problem in the general case...

thanks
-- PMM

#17 lore raw
In reply to Paolo Bonzini
Quoted 26 lines · Paolo Bonzini <[email protected]> 于2020年9月20日周日 下午3:56写道:
Quoted 16 lines · Li Qiang

Currently the qemu device fuzzer find some DMA to MMIO issue. If the device handling MMIO currently trigger a DMA which the address is MMIO, this will reenter the device MMIO handler. As some of the device doesn't consider this it will sometimes crash the qemu.

This patch tries to solve this by adding a per-device flag 'in_mmio'. When the memory core dispatch MMIO it will check/set this flag and when it leaves it will clean this flag.

Li Qiang (4):
memory: add memory_region_init_io_with_dev interface
memory: avoid reenter the device's MMIO handler while processing MMIO
e1000e: use the new memory_region_init_io_with_dev interface
hcd-xhci: use the new memory_region_init_io_with_dev interface

Quoted 7 lines
 hw/net/e1000e.c        |  8 ++++----
 hw/usb/hcd-xhci.c      | 25 ++++++++++++++---------
 include/exec/memory.h  |  9 +++++++++
 include/hw/qdev-core.h |  1 +
 softmmu/memory.c       | 46 +++++++++++++++++++++++++++++++++++++++---
 5 files changed, 72 insertions(+), 17 deletions(-)
Quoted 2 lines

I don't think this is a good solution. These are device bugs and they need to be fixed.

I agree with this the device should finally handle their cases. But we can do something in a pattern if the device hasn't
do that.
I have posted a patchset:
-->https://lists.gnu.org/archive/html/qemu-devel/2020-09/msg00906.html

This is add flags in the Device State. And check/record the flag when doing reentrancy code.
Once the device has fixed the reentrancy issue, they can remove this flag.

Thanks,
Li QIang

Quoted 3 lines

Paolo