On Fri, Feb 10, 2023 at 09:32:38AM -0500, Stefan Hajnoczi wrote: > When an IOThread is configured, the ctrl virtqueue is processed in the > IOThread. TMFs that reset SCSI devices are currently called directly > from the IOThread and trigger an assertion failure in blk_drain(): > > ../block/block-backend.c:1780: void blk_drain(BlockBackend *): Assertion > `qemu_in_main_thread()' failed. > > The blk_drain() function is not designed to be called from an IOThread > because it needs the Big QEMU Lock (BQL). > > This patch defers TMFs that reset SCSI devices to a Bottom Half (BH) > that runs in the main loop thread under the BQL. This way it's safe to > call blk_drain() and the assertion failure is avoided. > > Introduce s->tmf_bh_list for tracking TMF requests that have been > deferred to the BH. When the BH runs it will grab the entire list and > process all requests. Care must be taken to clear the list when the > virtio-scsi device is reset or unrealized. Otherwise deferred TMF > requests could execute later and lead to use-after-free or other > undefined behavior. > > The s->resetting counter that's used by TMFs that reset SCSI devices is > accessed from multiple threads. This patch makes that explicit by using > atomic accessor functions. With this patch applied the counter is only > modified by the main loop thread under the BQL but can be read by any > thread. > > Reported-by: Qing Wang <qinw...@redhat.com> > Cc: Paolo Bonzini <pbonz...@redhat.com> > Signed-off-by: Stefan Hajnoczi <stefa...@redhat.com> > --- > include/hw/virtio/virtio-scsi.h | 11 ++- > hw/scsi/virtio-scsi.c | 169 +++++++++++++++++++++++++------- > 2 files changed, 143 insertions(+), 37 deletions(-)
This one is trickier than the first two, so if you want a second reviewer, I understand. That said, although the number of lines of code grows, the code motion into separate handlers was fairly easy to follow, and the logic for why they must be in a separate BH context is correct. Reviewed-by: Eric Blake <ebl...@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org