memory: prevent dma-reentracy issues
Subsystems (2)
v5 cover memory: prevent dma-reentracy issues
These patches aim to solve two types of DMA-reentrancy issues:
1.) mmio -> dma -> mmio case
To solve this, we track whether the device is engaged in io by checking/setting a reentrancy-guard within APIs used for MMIO access.
2.) bh -> dma write -> mmio case
This case is trickier, since we dont have a generic way to associate a bh with the underlying Device/DeviceState. Thus, this version allows a device to associate a reentrancy-guard with a bh, when creating it. (Instead of calling qemu_bh_new, you call qemu_bh_new_guarded)
I replaced most of the qemu_bh_new invocations with the guarded analog, except for the ones where the DeviceState was not trivially accessible
v4-> v5:
- Add corresponding checkpatch checks
- Save/restore reentrancy-flag when entering/exiting BHs
- Improve documentation
- Check object_dynamic_cast return value
v3 -> v4: Instead of changing all of the DMA APIs, instead add an
optional reentrancy guard to the BH API.
v2 -> v3: Bite the bullet and modify the DMA APIs, rather than
attempting to guess DeviceStates in BHs.
Alexander Bulekov (4):
memory: prevent dma-reentracy issues
async: Add an optional reentrancy guard to the BH API
checkpatch: add qemu_bh_new/aio_bh_new checks
hw: replace most qemu_bh_new calls with qemu_bh_new_guarded
docs/devel/multiple-iothreads.txt | 7 +++++++ hw/9pfs/xen-9p-backend.c | 4 +++- hw/block/dataplane/virtio-blk.c | 3 ++- hw/block/dataplane/xen-block.c | 5 +++-- hw/block/virtio-blk.c | 5 +++-- hw/char/virtio-serial-bus.c | 3 ++- hw/display/qxl.c | 9 ++++++--- hw/display/virtio-gpu.c | 6 ++++-- hw/ide/ahci.c | 3 ++- hw/ide/core.c | 3 ++- hw/misc/imx_rngc.c | 6 ++++-- hw/misc/macio/mac_dbdma.c | 2 +- hw/net/virtio-net.c | 3 ++- hw/nvme/ctrl.c | 6 ++++-- hw/scsi/mptsas.c | 3 ++- hw/scsi/scsi-bus.c | 3 ++- hw/scsi/vmw_pvscsi.c | 3 ++- hw/usb/dev-uas.c | 3 ++- hw/usb/hcd-dwc2.c | 3 ++- hw/usb/hcd-ehci.c | 3 ++- hw/usb/hcd-uhci.c | 2 +- hw/usb/host-libusb.c | 6 ++++-- hw/usb/redirect.c | 6 ++++-- hw/usb/xen-usb.c | 3 ++- hw/virtio/virtio-balloon.c | 5 +++-- hw/virtio/virtio-crypto.c | 3 ++- include/block/aio.h | 18 ++++++++++++++++-- include/hw/qdev-core.h | 7 +++++++ include/qemu/main-loop.h | 7 +++++-- scripts/checkpatch.pl | 8 ++++++++ softmmu/memory.c | 17 +++++++++++++++++ softmmu/trace-events | 1 + tests/unit/ptimer-test-stubs.c | 3 ++- util/async.c | 18 +++++++++++++++++- util/main-loop.c | 5 +++-- util/trace-events | 1 + 36 files changed, 150 insertions(+), 43 deletions(-)
Signature · 1 line
2.39.0
PATCH v5 1/4 memory: prevent dma-reentracy issues
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
--- include/hw/qdev-core.h | 7 +++++++ softmmu/memory.c | 17 +++++++++++++++++ softmmu/trace-events | 1 + 3 files changed, 25 insertions(+)
include/hw/qdev-core.h +7 -0
@@ -162,6 +162,10 @@ struct NamedClockList {QLIST_ENTRY (NamedClockList) node; }; +typedef struct { +bool engaged_in_io; +}MemReentrancyGuard ; +/** * DeviceState: * @realized: Indicates whether the device has been fully constructed.
@@ -194,6 +198,9 @@ struct DeviceState {int alias_required_for_version;ResettableState reset;GSList *unplug_blockers; + +/* Is the device currently in mmio/pio/dma? Used to prevent re-entrancy */ +MemReentrancyGuard mem_reentrancy_guard; };struct DeviceListener {
softmmu/memory.c +17 -0
@@ -533,6 +533,7 @@ static MemTxResult access_with_adjusted_size(hwaddr addr,uint64_t access_mask;unsigned access_size;unsigned i; +DeviceState *dev =NULL ;MemTxResult r =MEMTX_OK ;if (!access_size_min) {
@@ -542,6 +543,19 @@ static MemTxResult access_with_adjusted_size(hwaddr addr, access_size_max =4 ; } +/* Do not allow more than one simultanous access to a device's IO Regions */ +if (mr->owner && + !mr->ram_device && !mr->ram && !mr->rom_device && !mr->readonly) { + dev = (DeviceState *)object_dynamic_cast (mr->owner,TYPE_DEVICE ); +if (dev) { +if (dev->mem_reentrancy_guard.engaged_in_io) { +trace_memory_region_reentrant_io (get_cpu_index (), mr, addr, size); +return MEMTX_ERROR ; + } + dev->mem_reentrancy_guard.engaged_in_io = true; + } + } +/* FIXME: support unaligned access? */ access_size =MAX (MIN (size, access_size_max), access_size_min); access_mask =MAKE_64BIT_MASK (0 , access_size *8 );
@@ -556,6 +570,9 @@ static MemTxResult access_with_adjusted_size(hwaddr addr, access_mask, attrs); } } +if (dev) { + dev->mem_reentrancy_guard.engaged_in_io = false; + }return r; }
softmmu/trace-events +1 -0
@@ -13,6 +13,7 @@ memory_region_ops_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, u memory_region_ops_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size, const char *name) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u name '%s'" memory_region_subpage_read(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u" memory_region_subpage_write(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u" +memory_region_reentrant_io(int cpu_index, void *mr, uint64_t offset, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" size %u" memory_region_ram_device_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u" memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u" memory_region_sync_dirty(const char *mr, const char *listener, int global) "mr '%s' listener '%s' synced (global=%d)"
Signature · 1 line
2.39.0
Quoted 21 lines · Alexander Bulekov
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 caseThese 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.comResolves: 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
Quoted 2 lines
Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Alexander Bulekov <[email protected]>
Signature · 1 line
Peter Xu
PATCH v5 2/4 async: Add an optional reentrancy guard to the BH API
In reply to Alexander BulekovDevices can pass their MemoryReentrancyGuard (from their DeviceState), when creating new BHes. Then, the async API will toggle the guard before/after calling the BH call-back. This prevents bh->mmio reentrancy issues.
--- docs/devel/multiple-iothreads.txt | 7 +++++++ include/block/aio.h | 18 ++++++++++++++++-- include/qemu/main-loop.h | 7 +++++-- tests/unit/ptimer-test-stubs.c | 3 ++- util/async.c | 18 +++++++++++++++++- util/main-loop.c | 5 +++-- util/trace-events | 1 + 7 files changed, 51 insertions(+), 8 deletions(-)
docs/devel/multiple-iothreads.txt +7 -0
@@ -61,6 +61,7 @@ There are several old APIs that use the main loop AioContext: * LEGACY qemu_aio_set_event_notifier() - monitor an event notifier * LEGACY timer_new_ms() - create a timer * LEGACY qemu_bh_new() - create a BH + * LEGACY qemu_bh_new_guarded() - create a BH with a device re-entrancy guard * LEGACY qemu_aio_wait() - run an event loop iteration Since they implicitly work on the main loop they cannot be used in code that
@@ -72,8 +73,14 @@ Instead, use the AioContext functions directly (see include/block/aio.h): * aio_set_event_notifier() - monitor an event notifier * aio_timer_new() - create a timer * aio_bh_new() - create a BH + * aio_bh_new_guarded() - create a BH with a device re-entrancy guard * aio_poll() - run an event loop iteration +The qemu_bh_new_guarded/aio_bh_new_guarded APIs accept a "MemReentrancyGuard" +argument, which is used to check for and prevent re-entrancy problems. For +BHs associated with devices, the reentrancy-guard is contained in the +corresponding DeviceState and named "mem_reentrancy_guard". + The AioContext can be obtained from the IOThread using iothread_get_aio_context() or for the main loop using qemu_get_aio_context(). Code that takes an AioContext argument works both in IOThreads or the main
include/block/aio.h +16 -2
@@ -23,6 +23,8 @@#include "qemu/thread.h" #include "qemu/timer.h" #include "block/graph-lock.h" +#include "hw/qdev-core.h" +typedef struct BlockAIOCB BlockAIOCB ;typedef void BlockCompletionFunc (void *opaque,int ret);
@@ -332,9 +334,11 @@ void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,* is opaque and must be allocated prior to its use. * * @name: A human-readable identifier for debugging purposes. +* @reentrancy_guard: A guard set when entering a cb to prevent +* device-reentrancy issues */ QEMUBH *aio_bh_new_full (AioContext *ctx,QEMUBHFunc *cb,void *opaque, -const char *name); +const char *name,MemReentrancyGuard *reentrancy_guard);/** * aio_bh_new: Allocate a new bottom half structure
@@ -343,7 +347,17 @@ QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,* string. */ #define aio_bh_new (ctx, cb, opaque) \ - aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb))) + aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb)), NULL) + +/** +* aio_bh_new_guarded: Allocate a new bottom half structure with a +* reentrancy_guard +* +* A convenience wrapper for aio_bh_new_full() that uses the cb as the name +* string. +*/ +#define aio_bh_new_guarded (ctx, cb, opaque, guard) \ + aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb)), guard)/** * aio_notify: Force processing of pending events.
include/qemu/main-loop.h +5 -2
@@ -389,9 +389,12 @@ void qemu_cond_timedwait_iothread(QemuCond *cond, int ms);void qemu_fd_register (int fd); +#define qemu_bh_new_guarded (cb, opaque, guard) \ + qemu_bh_new_full((cb), (opaque), (stringify(cb)), guard)#define qemu_bh_new (cb, opaque) \ - qemu_bh_new_full((cb), (opaque), (stringify(cb))) -QEMUBH *qemu_bh_new_full (QEMUBHFunc *cb,void *opaque,const char *name); + qemu_bh_new_full((cb), (opaque), (stringify(cb)), NULL) +QEMUBH *qemu_bh_new_full (QEMUBHFunc *cb,void *opaque,const char *name, +MemReentrancyGuard *reentrancy_guard);void qemu_bh_schedule_idle (QEMUBH *bh);enum {
tests/unit/ptimer-test-stubs.c +2 -1
@@ -107,7 +107,8 @@ int64_t qemu_clock_deadline_ns_all(QEMUClockType type, int attr_mask)return deadline; } -QEMUBH *qemu_bh_new_full (QEMUBHFunc *cb,void *opaque,const char *name) +QEMUBH *qemu_bh_new_full (QEMUBHFunc *cb,void *opaque,const char *name, +MemReentrancyGuard *reentrancy_guard) {QEMUBH *bh =g_new (QEMUBH ,1 );
util/async.c +17 -1
@@ -65,6 +65,7 @@ struct QEMUBH {void *opaque;QSLIST_ENTRY (QEMUBH) next;unsigned flags; +MemReentrancyGuard *reentrancy_guard; };/* Called concurrently from any thread */
@@ -133,7 +134,7 @@ void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb, }QEMUBH *aio_bh_new_full (AioContext *ctx,QEMUBHFunc *cb,void *opaque, -const char *name) +const char *name,MemReentrancyGuard *reentrancy_guard) {QEMUBH *bh; bh =g_new (QEMUBH ,1 );
@@ -142,13 +143,28 @@ QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque, .cb = cb, .opaque = opaque, .name = name, + .reentrancy_guard = reentrancy_guard, };return bh; }void aio_bh_call (QEMUBH *bh) { +bool last_engaged_in_io = false; + +if (bh->reentrancy_guard) { + last_engaged_in_io = bh->reentrancy_guard->engaged_in_io; +if (bh->reentrancy_guard->engaged_in_io) { +trace_reentrant_aio (bh->ctx, bh->name); + } + bh->reentrancy_guard->engaged_in_io = true; + } + bh->cb (bh->opaque); + +if (bh->reentrancy_guard) { + bh->reentrancy_guard->engaged_in_io = last_engaged_in_io; + } }/* Multiple occurrences of aio_bh_poll cannot be called concurrently. */
util/main-loop.c +3 -2
@@ -617,9 +617,10 @@ void main_loop_wait(int nonblocking)/* Functions to operate on the main QEMU AioContext. */ -QEMUBH *qemu_bh_new_full (QEMUBHFunc *cb,void *opaque,const char *name) +QEMUBH *qemu_bh_new_full (QEMUBHFunc *cb,void *opaque,const char *name,MemReentrancyGuard *reentrancy_guard) { -return aio_bh_new_full (qemu_aio_context, cb, opaque, name); +return aio_bh_new_full (qemu_aio_context, cb, opaque, name, + reentrancy_guard); }/*
util/trace-events +1 -0
@@ -11,6 +11,7 @@ poll_remove(void *ctx, void *node, int fd) "ctx %p node %p fd %d" # async.c aio_co_schedule(void *ctx, void *co) "ctx %p co %p" aio_co_schedule_bh_cb(void *ctx, void *co) "ctx %p co %p" +reentrant_aio(void *ctx, const char *name) "ctx %p name %s" # thread-pool.c thread_pool_submit(void *pool, void *req, void *opaque) "pool %p req %p opaque %p"
Signature · 1 line
2.39.0
PATCH v5 3/4 checkpatch: add qemu_bh_new/aio_bh_new checks
In reply to Alexander BulekovAdvise authors to use the _guarded versions of the APIs, instead.
--- scripts/checkpatch.pl | 8 ++++++++ 1 file changed, 8 insertions(+)
scripts/checkpatch.pl +8 -0
@@ -2865,6 +2865,14 @@ sub process { if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) { ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr); } +# recommend qemu_bh_new_guarded instead of qemu_bh_new + if ($line =~ /\bqemu_bh_new\s*\(/) { + ERROR("use qemu_bh_new_guarded() instead of qemu_bh_new() to avoid reentrancy problems\n" . $herecurr); + } +# recommend aio_bh_new_guarded instead of aio_bh_new + if ($line =~ /\baio_bh_new\s*\(/) { + ERROR("use aio_bh_new_guarded() instead of aio_bh_new() to avoid reentrancy problems\n" . $herecurr); + } # check for module_init(), use category-specific init macros explicitly please if ($line =~ /^module_init\s*\(/) { ERROR("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
Signature · 1 line
2.39.0
Re: [PATCH v5 3/4] checkpatch: add qemu_bh_new/aio_bh_new checks
Quoted 13 lines · Alexander Bulekov
Advise authors to use the
_guardedversions of the APIs, instead.Signed-off-by Alexander Bulekov <[email protected]>--- scripts/checkpatch.pl | 8 ++++++++ 1 file changed, 8 insertions(+)scripts/checkpatch.pl +0
-0@@ -2865,6 +2865,14 @@ sub process { if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
scripts/checkpatch.pl sub process { · line 2875
Quoted 10 lines
scripts/checkpatch.pl +8
-0ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr); } +# recommend qemu_bh_new_guarded instead of qemu_bh_new + if ($line =~ /\bqemu_bh_new\s*\(/) { + ERROR("use qemu_bh_new_guarded() instead of qemu_bh_new() to avoid reentrancy problems\n" . $herecurr); + } +# recommend aio_bh_new_guarded instead of aio_bh_new + if ($line =~ /\baio_bh_new\s*\(/) { + ERROR("use aio_bh_new_guarded() instead of aio_bh_new() to avoid reentrancy problems\n" . $herecurr); + }
These checks should be limited to source code in hw/. Other directories can use BHs without MemReentrancyGuard.
Stefan
PATCH v5 4/4 hw: replace most qemu_bh_new calls with qemu_bh_new_guarded
In reply to Alexander BulekovThis protects devices from bh->mmio reentrancy issues.
--- hw/9pfs/xen-9p-backend.c | 4 +++- hw/block/dataplane/virtio-blk.c | 3 ++- hw/block/dataplane/xen-block.c | 5 +++-- hw/block/virtio-blk.c | 5 +++-- hw/char/virtio-serial-bus.c | 3 ++- hw/display/qxl.c | 9 ++++++--- hw/display/virtio-gpu.c | 6 ++++-- hw/ide/ahci.c | 3 ++- hw/ide/core.c | 3 ++- hw/misc/imx_rngc.c | 6 ++++-- hw/misc/macio/mac_dbdma.c | 2 +- hw/net/virtio-net.c | 3 ++- hw/nvme/ctrl.c | 6 ++++-- hw/scsi/mptsas.c | 3 ++- hw/scsi/scsi-bus.c | 3 ++- hw/scsi/vmw_pvscsi.c | 3 ++- hw/usb/dev-uas.c | 3 ++- hw/usb/hcd-dwc2.c | 3 ++- hw/usb/hcd-ehci.c | 3 ++- hw/usb/hcd-uhci.c | 2 +- hw/usb/host-libusb.c | 6 ++++-- hw/usb/redirect.c | 6 ++++-- hw/usb/xen-usb.c | 3 ++- hw/virtio/virtio-balloon.c | 5 +++-- hw/virtio/virtio-crypto.c | 3 ++- 25 files changed, 66 insertions(+), 35 deletions(-)
hw/9pfs/xen-9p-backend.c +3 -1
@@ -441,7 +441,9 @@ static int xen_9pfs_connect(struct XenLegacyDevice *xendev) xen_9pdev->rings[i].ring.out = xen_9pdev->rings[i].data +XEN_FLEX_RING_SIZE (ring_order); - xen_9pdev->rings[i].bh =qemu_bh_new (xen_9pfs_bh, &xen_9pdev->rings[i]); + xen_9pdev->rings[i].bh =qemu_bh_new_guarded (xen_9pfs_bh, + &xen_9pdev->rings[i], + &DEVICE (xen_9pdev)->mem_reentrancy_guard); xen_9pdev->rings[i].out_cons =0 ; xen_9pdev->rings[i].out_size =0 ; xen_9pdev->rings[i].inprogress = false;
hw/block/dataplane/virtio-blk.c +2 -1
@@ -127,7 +127,8 @@ bool virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf, }else { s->ctx =qemu_get_aio_context (); } - s->bh =aio_bh_new (s->ctx, notify_guest_bh, s); + s->bh =aio_bh_new_guarded (s->ctx, notify_guest_bh, s, + &DEVICE (s)->mem_reentrancy_guard); s->batch_notify_vqs =bitmap_new (conf->num_queues); *dataplane = s;
hw/block/dataplane/xen-block.c +3 -2
@@ -632,8 +632,9 @@ XenBlockDataPlane *xen_block_dataplane_create(XenDevice *xendev, }else { dataplane->ctx =qemu_get_aio_context (); } - dataplane->bh =aio_bh_new (dataplane->ctx, xen_block_dataplane_bh, - dataplane); + dataplane->bh =aio_bh_new_guarded (dataplane->ctx, xen_block_dataplane_bh, + dataplane, + &DEVICE (xendev)->mem_reentrancy_guard);return dataplane; }
hw/block/virtio-blk.c +3 -2
@@ -866,8 +866,9 @@ static void virtio_blk_dma_restart_cb(void *opaque, bool running,* requests will be processed while starting the data plane. */ if (!s->bh && !virtio_bus_ioeventfd_enabled (bus)) { - s->bh =aio_bh_new (blk_get_aio_context (s->conf.conf.blk), - virtio_blk_dma_restart_bh, s); + s->bh =aio_bh_new_guarded (blk_get_aio_context (s->conf.conf.blk), + virtio_blk_dma_restart_bh, s, + &DEVICE (s)->mem_reentrancy_guard);blk_inc_in_flight (s->conf.conf.blk);qemu_bh_schedule (s->bh); }
hw/char/virtio-serial-bus.c +2 -1
@@ -985,7 +985,8 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)return ; } - port->bh =qemu_bh_new (flush_queued_data_bh, port); + port->bh =qemu_bh_new_guarded (flush_queued_data_bh, port, + &dev->mem_reentrancy_guard); port->elem =NULL ; }
hw/display/qxl.c +6 -3
@@ -2223,11 +2223,14 @@ static void qxl_realize_common(PCIQXLDevice *qxl, Error **errp)qemu_add_vm_change_state_handler (qxl_vm_change_state_handler, qxl); - qxl->update_irq =qemu_bh_new (qxl_update_irq_bh, qxl); + qxl->update_irq =qemu_bh_new_guarded (qxl_update_irq_bh, qxl, + &DEVICE (qxl)->mem_reentrancy_guard);qxl_reset_state (qxl); - qxl->update_area_bh =qemu_bh_new (qxl_render_update_area_bh, qxl); - qxl->ssd.cursor_bh =qemu_bh_new (qemu_spice_cursor_refresh_bh, &qxl->ssd); + qxl->update_area_bh =qemu_bh_new_guarded (qxl_render_update_area_bh, qxl, + &DEVICE (qxl)->mem_reentrancy_guard); + qxl->ssd.cursor_bh =qemu_bh_new_guarded (qemu_spice_cursor_refresh_bh, &qxl->ssd, + &DEVICE (qxl)->mem_reentrancy_guard); }static void qxl_realize_primary (PCIDevice *dev,Error **errp)
hw/display/virtio-gpu.c +4 -2
@@ -1339,8 +1339,10 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp) g->ctrl_vq =virtio_get_queue (vdev,0 ); g->cursor_vq =virtio_get_queue (vdev,1 ); - g->ctrl_bh =qemu_bh_new (virtio_gpu_ctrl_bh, g); - g->cursor_bh =qemu_bh_new (virtio_gpu_cursor_bh, g); + g->ctrl_bh =qemu_bh_new_guarded (virtio_gpu_ctrl_bh, g, + &qdev->mem_reentrancy_guard); + g->cursor_bh =qemu_bh_new_guarded (virtio_gpu_cursor_bh, g, + &qdev->mem_reentrancy_guard);QTAILQ_INIT (&g->reslist);QTAILQ_INIT (&g->cmdq);QTAILQ_INIT (&g->fenceq);
hw/ide/ahci.c +2 -1
@@ -1508,7 +1508,8 @@ static void ahci_cmd_done(const IDEDMA *dma)ahci_write_fis_d2h (ad);if (ad->port_regs.cmd_issue && !ad->check_bh) { - ad->check_bh =qemu_bh_new (ahci_check_cmd_bh, ad); + ad->check_bh =qemu_bh_new_guarded (ahci_check_cmd_bh, ad, + &DEVICE (ad)->mem_reentrancy_guard);qemu_bh_schedule (ad->check_bh); } }
hw/ide/core.c +2 -1
@@ -519,7 +519,8 @@ BlockAIOCB *ide_issue_trim( iocb =blk_aio_get (&trim_aiocb_info, s->blk, cb, cb_opaque); iocb->s = s; - iocb->bh =qemu_bh_new (ide_trim_bh_cb, iocb); + iocb->bh =qemu_bh_new_guarded (ide_trim_bh_cb, iocb, + &DEVICE (s)->mem_reentrancy_guard); iocb->ret =0 ; iocb->qiov = qiov; iocb->i =-1 ;
hw/misc/imx_rngc.c +4 -2
@@ -228,8 +228,10 @@ static void imx_rngc_realize(DeviceState *dev, Error **errp)sysbus_init_mmio (sbd, &s->iomem);sysbus_init_irq (sbd, &s->irq); - s->self_test_bh =qemu_bh_new (imx_rngc_self_test, s); - s->seed_bh =qemu_bh_new (imx_rngc_seed, s); + s->self_test_bh =qemu_bh_new_guarded (imx_rngc_self_test, s, + &dev->mem_reentrancy_guard); + s->seed_bh =qemu_bh_new_guarded (imx_rngc_seed, s, + &dev->mem_reentrancy_guard); }static void imx_rngc_reset (DeviceState *dev)
hw/misc/macio/mac_dbdma.c +1 -1
@@ -914,7 +914,7 @@ static void mac_dbdma_realize(DeviceState *dev, Error **errp) {DBDMAState *s =MAC_DBDMA (dev); - s->bh =qemu_bh_new (DBDMA_run_bh, s); + s->bh =qemu_bh_new_guarded (DBDMA_run_bh, s, &dev->mem_reentrancy_guard); }static void mac_dbdma_class_init (ObjectClass *oc,void *data)
hw/net/virtio-net.c +2 -1
@@ -2885,7 +2885,8 @@ static void virtio_net_add_queue(VirtIONet *n, int index) n->vqs[index].tx_vq =virtio_add_queue (vdev, n->net_conf.tx_queue_size, virtio_net_handle_tx_bh); - n->vqs[index].tx_bh =qemu_bh_new (virtio_net_tx_bh, &n->vqs[index]); + n->vqs[index].tx_bh =qemu_bh_new_guarded (virtio_net_tx_bh, &n->vqs[index], + &DEVICE (vdev)->mem_reentrancy_guard); } n->vqs[index].tx_waiting =0 ;
hw/nvme/ctrl.c +4 -2
@@ -4318,7 +4318,8 @@ static void nvme_init_sq(NvmeSQueue *sq, NvmeCtrl *n, uint64_t dma_addr,QTAILQ_INSERT_TAIL (&(sq->req_list), &sq->io_req[i], entry); } - sq->bh =qemu_bh_new (nvme_process_sq, sq); + sq->bh =qemu_bh_new_guarded (nvme_process_sq, sq, + &DEVICE (sq->ctrl)->mem_reentrancy_guard);if (n->dbbuf_enabled) { sq->db_addr = n->dbbuf_dbs + (sqid <<3 );
@@ -4708,7 +4709,8 @@ static void nvme_init_cq(NvmeCQueue *cq, NvmeCtrl *n, uint64_t dma_addr, } } n->cq[cqid] = cq; - cq->bh =qemu_bh_new (nvme_post_cqes, cq); + cq->bh =qemu_bh_new_guarded (nvme_post_cqes, cq, + &DEVICE (cq->ctrl)->mem_reentrancy_guard); }static uint16_t nvme_create_cq (NvmeCtrl *n,NvmeRequest *req)
hw/scsi/mptsas.c +2 -1
@@ -1322,7 +1322,8 @@ static void mptsas_scsi_realize(PCIDevice *dev, Error **errp) } s->max_devices =MPTSAS_NUM_PORTS ; - s->request_bh =qemu_bh_new (mptsas_fetch_requests, s); + s->request_bh =qemu_bh_new_guarded (mptsas_fetch_requests, s, + &DEVICE (dev)->mem_reentrancy_guard);scsi_bus_init (&s->bus,sizeof (s->bus), &dev->qdev, &mptsas_scsi_info); }
hw/scsi/scsi-bus.c +2 -1
@@ -193,7 +193,8 @@ static void scsi_dma_restart_cb(void *opaque, bool running, RunState state)AioContext *ctx =blk_get_aio_context (s->conf.blk);/* The reference is dropped in scsi_dma_restart_bh.*/ object_ref (OBJECT (s)); - s->bh =aio_bh_new (ctx, scsi_dma_restart_bh, s); + s->bh =aio_bh_new_guarded (ctx, scsi_dma_restart_bh, s, + &DEVICE (s)->mem_reentrancy_guard);qemu_bh_schedule (s->bh); } }
hw/scsi/vmw_pvscsi.c +2 -1
@@ -1184,7 +1184,8 @@ pvscsi_realizefn(PCIDevice *pci_dev, Error **errp)pcie_endpoint_cap_init (pci_dev,PVSCSI_EXP_EP_OFFSET ); } - s->completion_worker =qemu_bh_new (pvscsi_process_completion_queue, s); + s->completion_worker =qemu_bh_new_guarded (pvscsi_process_completion_queue, s, + &DEVICE (pci_dev)->mem_reentrancy_guard);scsi_bus_init (&s->bus,sizeof (s->bus),DEVICE (pci_dev), &pvscsi_scsi_info);/* override default SCSI bus hotplug-handler, with pvscsi's one */
hw/usb/dev-uas.c +2 -1
@@ -937,7 +937,8 @@ static void usb_uas_realize(USBDevice *dev, Error **errp)QTAILQ_INIT (&uas->results);QTAILQ_INIT (&uas->requests); - uas->status_bh =qemu_bh_new (usb_uas_send_status_bh, uas); + uas->status_bh =qemu_bh_new_guarded (usb_uas_send_status_bh, uas, + &d->mem_reentrancy_guard); dev->flags |= (1 <<USB_DEV_FLAG_IS_SCSI_STORAGE );scsi_bus_init (&uas->bus,sizeof (uas->bus),DEVICE (dev), &usb_uas_scsi_info);
hw/usb/hcd-dwc2.c +2 -1
@@ -1364,7 +1364,8 @@ static void dwc2_realize(DeviceState *dev, Error **errp) s->fi =USB_FRMINTVL -1 ; s->eof_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , dwc2_frame_boundary, s); s->frame_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , dwc2_work_timer, s); - s->async_bh =qemu_bh_new (dwc2_work_bh, s); + s->async_bh =qemu_bh_new_guarded (dwc2_work_bh, s, + &dev->mem_reentrancy_guard);sysbus_init_irq (sbd, &s->irq); }
hw/usb/hcd-ehci.c +2 -1
@@ -2533,7 +2533,8 @@ void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp) } s->frame_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , ehci_work_timer, s); - s->async_bh =qemu_bh_new (ehci_work_bh, s); + s->async_bh =qemu_bh_new_guarded (ehci_work_bh, s, + &dev->mem_reentrancy_guard); s->device = dev; s->vmstate =qemu_add_vm_change_state_handler (usb_ehci_vm_state_change, s);
hw/usb/hcd-uhci.c +1 -1
@@ -1193,7 +1193,7 @@ void usb_uhci_common_realize(PCIDevice *dev, Error **errp) USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL); } } - s->bh =qemu_bh_new (uhci_bh, s); + s->bh =qemu_bh_new_guarded (uhci_bh, s, &DEVICE (dev)->mem_reentrancy_guard); s->frame_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , uhci_frame_timer, s); s->num_ports_vmstate =NB_PORTS ;QTAILQ_INIT (&s->queues);
hw/usb/host-libusb.c +4 -2
@@ -1141,7 +1141,8 @@ static void usb_host_nodev_bh(void *opaque)static void usb_host_nodev (USBHostDevice *s) {if (!s->bh_nodev) { - s->bh_nodev =qemu_bh_new (usb_host_nodev_bh, s); + s->bh_nodev =qemu_bh_new_guarded (usb_host_nodev_bh, s, + &DEVICE (s)->mem_reentrancy_guard); }qemu_bh_schedule (s->bh_nodev); }
@@ -1739,7 +1740,8 @@ static int usb_host_post_load(void *opaque, int version_id)USBHostDevice *dev = opaque;if (!dev->bh_postld) { - dev->bh_postld =qemu_bh_new (usb_host_post_load_bh, dev); + dev->bh_postld =qemu_bh_new_guarded (usb_host_post_load_bh, dev, + &DEVICE (dev)->mem_reentrancy_guard); }qemu_bh_schedule (dev->bh_postld); dev->bh_postld_pending = true;
hw/usb/redirect.c +4 -2
@@ -1441,8 +1441,10 @@ static void usbredir_realize(USBDevice *udev, Error **errp) } } - dev->chardev_close_bh =qemu_bh_new (usbredir_chardev_close_bh, dev); - dev->device_reject_bh =qemu_bh_new (usbredir_device_reject_bh, dev); + dev->chardev_close_bh =qemu_bh_new_guarded (usbredir_chardev_close_bh, dev, + &DEVICE (dev)->mem_reentrancy_guard); + dev->device_reject_bh =qemu_bh_new_guarded (usbredir_device_reject_bh, dev, + &DEVICE (dev)->mem_reentrancy_guard); dev->attach_timer =timer_new_ms (QEMU_CLOCK_VIRTUAL , usbredir_do_attach, dev);packet_id_queue_init (&dev->cancelled, dev,"cancelled" );
hw/usb/xen-usb.c +2 -1
@@ -1021,7 +1021,8 @@ static void usbback_alloc(struct XenLegacyDevice *xendev)QTAILQ_INIT (&usbif->req_free_q);QSIMPLEQ_INIT (&usbif->hotplug_q); - usbif->bh =qemu_bh_new (usbback_bh, usbif); + usbif->bh =qemu_bh_new_guarded (usbback_bh, usbif, + &DEVICE (xendev)->mem_reentrancy_guard); }static int usbback_free (struct XenLegacyDevice *xendev)
hw/virtio/virtio-balloon.c +3 -2
@@ -908,8 +908,9 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)precopy_add_notifier (&s->free_page_hint_notify);object_ref (OBJECT (s->iothread)); - s->free_page_bh =aio_bh_new (iothread_get_aio_context (s->iothread), - virtio_ballloon_get_free_page_hints, s); + s->free_page_bh =aio_bh_new_guarded (iothread_get_aio_context (s->iothread), + virtio_ballloon_get_free_page_hints, s, + &DEVICE (s)->mem_reentrancy_guard); }if (virtio_has_feature (s->host_features,VIRTIO_BALLOON_F_REPORTING )) {
hw/virtio/virtio-crypto.c +2 -1
@@ -1050,7 +1050,8 @@ static void virtio_crypto_device_realize(DeviceState *dev, Error **errp) vcrypto->vqs[i].dataq =virtio_add_queue (vdev,1024 , virtio_crypto_handle_dataq_bh); vcrypto->vqs[i].dataq_bh = -qemu_bh_new (virtio_crypto_dataq_bh, &vcrypto->vqs[i]); +qemu_bh_new_guarded (virtio_crypto_dataq_bh, &vcrypto->vqs[i], + &dev->mem_reentrancy_guard); vcrypto->vqs[i].vcrypto = vcrypto; }
Signature · 1 line
2.39.0
Hi Alex,
From my perspective, I'm happy with the changes made, so from me at least:
Thanks,
Darren.
Quoted 73 lines · Alexander Bulekov
These patches aim to solve two types of DMA-reentrancy issues:
1.) mmio -> dma -> mmio case
To solve this, we track whether the device is engaged in io by checking/setting a reentrancy-guard within APIs used for MMIO access.2.) bh -> dma write -> mmio case
This case is trickier, since we dont have a generic way to associate a bh with the underlying Device/DeviceState. Thus, this version allows a device to associate a reentrancy-guard with a bh, when creating it. (Instead of callingqemu_bh_new, you callqemu_bh_new_guarded)I replaced most of the
qemu_bh_newinvocations with the guarded analog, except for the ones where the DeviceState was not trivially accessiblev4-> v5:
- Add corresponding checkpatch checks
- Save/restore reentrancy-flag when entering/exiting BHs
- Improve documentation
- Checkobject_dynamic_castreturn valuev3 -> v4: Instead of changing all of the DMA APIs, instead add an
optional reentrancy guard to the BH API.v2 -> v3: Bite the bullet and modify the DMA APIs, rather than
attempting to guess DeviceStates in BHs.Alexander Bulekov (4):
memory: prevent dma-reentracy issues
async: Add an optional reentrancy guard to the BH API
checkpatch: add qemu_bh_new/aio_bh_new checks
hw: replace mostqemu_bh_newcalls withqemu_bh_new_guardeddocs/devel/multiple-iothreads.txt | 7 +++++++ hw/9pfs/xen-9p-backend.c | 4 +++- hw/block/dataplane/virtio-blk.c | 3 ++- hw/block/dataplane/xen-block.c | 5 +++-- hw/block/virtio-blk.c | 5 +++-- hw/char/virtio-serial-bus.c | 3 ++- hw/display/qxl.c | 9 ++++++--- hw/display/virtio-gpu.c | 6 ++++-- hw/ide/ahci.c | 3 ++- hw/ide/core.c | 3 ++- hw/misc/imx_rngc.c | 6 ++++-- hw/misc/macio/mac_dbdma.c | 2 +- hw/net/virtio-net.c | 3 ++- hw/nvme/ctrl.c | 6 ++++-- hw/scsi/mptsas.c | 3 ++- hw/scsi/scsi-bus.c | 3 ++- hw/scsi/vmw_pvscsi.c | 3 ++- hw/usb/dev-uas.c | 3 ++- hw/usb/hcd-dwc2.c | 3 ++- hw/usb/hcd-ehci.c | 3 ++- hw/usb/hcd-uhci.c | 2 +- hw/usb/host-libusb.c | 6 ++++-- hw/usb/redirect.c | 6 ++++-- hw/usb/xen-usb.c | 3 ++- hw/virtio/virtio-balloon.c | 5 +++-- hw/virtio/virtio-crypto.c | 3 ++- include/block/aio.h | 18 ++++++++++++++++-- include/hw/qdev-core.h | 7 +++++++ include/qemu/main-loop.h | 7 +++++-- scripts/checkpatch.pl | 8 ++++++++ softmmu/memory.c | 17 +++++++++++++++++ softmmu/trace-events | 1 + tests/unit/ptimer-test-stubs.c | 3 ++- util/async.c | 18 +++++++++++++++++- util/main-loop.c | 5 +++-- util/trace-events | 1 + 36 files changed, 150 insertions(+), 43 deletions(-)Signature · 1 line
2.39.0