On 220621 1630, Peter Maydell wrote: > On Thu, 9 Jun 2022 at 14:59, Alexander Bulekov <alx...@bu.edu> wrote: > > > > Add a flag to the DeviceState, when a device is engaged in PIO/MMIO/DMA. > > This flag is set/checked prior to calling a device's MemoryRegion > > handlers, and set when device code initiates DMA. The purpose of this > > flag is to prevent two types of DMA-based reentrancy issues: > > > > 1.) mmio -> dma -> mmio case > > 2.) bh -> dma write -> mmio case > > > > These issues have led to problems such as stack-exhaustion and > > use-after-frees. > > > > Summary of the problem from Peter Maydell: > > https://lore.kernel.org/qemu-devel/cafeaca_23vc7he3iam-jva6w38lk4hjowae5kcknhprd5fp...@mail.gmail.com > > > > Resolves: https://gitlab.com/qemu-project/qemu/-/issues/62 > > Resolves: https://gitlab.com/qemu-project/qemu/-/issues/540 > > Resolves: https://gitlab.com/qemu-project/qemu/-/issues/541 > > Resolves: https://gitlab.com/qemu-project/qemu/-/issues/556 > > Resolves: https://gitlab.com/qemu-project/qemu/-/issues/557 > > Resolves: https://gitlab.com/qemu-project/qemu/-/issues/827 > > Signed-off-by: Alexander Bulekov <alx...@bu.edu> > > --- > > include/hw/pci/pci.h | 13 +++++++++++-- > > include/hw/qdev-core.h | 3 +++ > > softmmu/dma-helpers.c | 12 ++++++++++++ > > softmmu/memory.c | 15 +++++++++++++++ > > softmmu/trace-events | 1 + > > 5 files changed, 42 insertions(+), 2 deletions(-) > > > > diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h > > index 44dacfa224..ab1ad0f7a8 100644 > > --- a/include/hw/pci/pci.h > > +++ b/include/hw/pci/pci.h > > @@ -834,8 +834,17 @@ static inline MemTxResult pci_dma_rw(PCIDevice *dev, > > dma_addr_t addr, > > void *buf, dma_addr_t len, > > DMADirection dir, MemTxAttrs attrs) > > { > > - return dma_memory_rw(pci_get_address_space(dev), addr, buf, len, > > - dir, attrs); > > + bool prior_engaged_state; > > + MemTxResult result; > > + > > + prior_engaged_state = dev->qdev.engaged_in_io; > > + > > + dev->qdev.engaged_in_io = true; > > + result = dma_memory_rw(pci_get_address_space(dev), addr, buf, len, > > + dir, attrs); > > + dev->qdev.engaged_in_io = prior_engaged_state; > > + > > + return result; > > Why do we need to do something in this pci-specific function ? > I was expecting this to only need changes at the generic-to-all-devices > level.
Both of these handle the BH->DMA->MMIO case. Unlike MMIO, I don't think there is any neat way to set the engaged_in_io flag as we enter a BH. So instead, we try to set it when a device initiates DMA. The pci function lets us do that since we get a glimpse of the dev/qdev (unlike the dma_memory_... functions). > > > > diff --git a/softmmu/dma-helpers.c b/softmmu/dma-helpers.c > > index 7820fec54c..7a4f1fb9b3 100644 > > --- a/softmmu/dma-helpers.c > > +++ b/softmmu/dma-helpers.c > > @@ -288,8 +288,16 @@ static MemTxResult dma_buf_rw(void *buf, dma_addr_t > > len, dma_addr_t *residual, > > uint8_t *ptr = buf; > > dma_addr_t xresidual; > > int sg_cur_index; > > + DeviceState *dev; > > + bool prior_engaged_state; > > MemTxResult res = MEMTX_OK; > > > > + dev = sg->dev; > > + if (dev) { > > + prior_engaged_state = dev->engaged_in_io; > > + dev->engaged_in_io = true; > > + } > > + > > xresidual = sg->size; > > sg_cur_index = 0; > > len = MIN(len, xresidual); > > @@ -302,6 +310,10 @@ static MemTxResult dma_buf_rw(void *buf, dma_addr_t > > len, dma_addr_t *residual, > > xresidual -= xfer; > > } > > > > + if (dev) { > > + dev->engaged_in_io = prior_engaged_state; > > + } > > Not all DMA goes through dma_buf_rw() -- why does it need changes? This one has the same goal, but accesses the qdev through sg, instead of PCI. -Alex > > > + > > if (residual) { > > *residual = xresidual; > > } > > thanks > -- PMM