[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v2] memory: prevent dma-reentracy issues
From: |
Peter Maydell |
Subject: |
Re: [PATCH v2] memory: prevent dma-reentracy issues |
Date: |
Tue, 21 Jun 2022 16:30:40 +0100 |
On Thu, 9 Jun 2022 at 14:59, Alexander Bulekov <alxndr@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-JVA6W38LK4hJoWae5KcknhPRD5fPBZA@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 <alxndr@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.
> 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?
> +
> if (residual) {
> *residual = xresidual;
> }
thanks
-- PMM