From: Paolo Bonzini <pbonzini@redhat.com>
To: Stefan Hajnoczi <stefanha@redhat.com>, qemu-devel@nongnu.org
Cc: sgarzare@redhat.com, kwolf@redhat.com,
Emanuele Giuseppe Esposito <eesposit@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>, Fam Zheng <fam@euphon.net>,
Qing Wang <qinwang@redhat.com>
Subject: Re: [PATCH] virtio-scsi: fix race in virtio_scsi_dataplane_start()
Date: Fri, 5 Aug 2022 11:40:38 +0200 [thread overview]
Message-ID: <17d794db-4c17-b438-b71b-f6224791a496@redhat.com> (raw)
In-Reply-To: <20220803162824.948023-1-stefanha@redhat.com>
On 8/3/22 18:28, Stefan Hajnoczi wrote:
> diff --git a/hw/scsi/virtio-scsi-dataplane.c b/hw/scsi/virtio-scsi-dataplane.c
> index 8bb6e6acfc..a575c3f0cd 100644
> --- a/hw/scsi/virtio-scsi-dataplane.c
> +++ b/hw/scsi/virtio-scsi-dataplane.c
> @@ -66,6 +66,21 @@ static int virtio_scsi_set_host_notifier(VirtIOSCSI *s, VirtQueue *vq, int n)
> return 0;
> }
>
> +/* Context: BH in IOThread */
> +static void virtio_scsi_dataplane_start_bh(void *opaque)
> +{
> + VirtIOSCSI *s = opaque;
> + VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
> + int i;
> +
> + virtio_queue_aio_attach_host_notifier(vs->ctrl_vq, s->ctx);
> + virtio_queue_aio_attach_host_notifier_no_poll(vs->event_vq, s->ctx);
> +
> + for (i = 0; i < vs->conf.num_queues; i++) {
> + virtio_queue_aio_attach_host_notifier(vs->cmd_vqs[i], s->ctx);
> + }
> +}
> +
> /* Context: BH in IOThread */
> static void virtio_scsi_dataplane_stop_bh(void *opaque)
> {
> @@ -136,16 +151,18 @@ int virtio_scsi_dataplane_start(VirtIODevice *vdev)
>
> memory_region_transaction_commit();
>
> - aio_context_acquire(s->ctx);
> - virtio_queue_aio_attach_host_notifier(vs->ctrl_vq, s->ctx);
> - virtio_queue_aio_attach_host_notifier_no_poll(vs->event_vq, s->ctx);
> -
> - for (i = 0; i < vs->conf.num_queues; i++) {
> - virtio_queue_aio_attach_host_notifier(vs->cmd_vqs[i], s->ctx);
> - }
> -
> s->dataplane_starting = false;
> s->dataplane_started = true;
> +
> + /*
> + * Attach notifiers from within the IOThread. It's possible to attach
> + * notifiers from our thread directly but this approach has the advantages
> + * that virtio_scsi_dataplane_start_bh() is symmetric with
> + * virtio_scsi_dataplane_stop_bh() and the s->dataplane_started assignment
> + * above doesn't require explicit synchronization.
> + */
> + aio_context_acquire(s->ctx);
> + aio_wait_bh_oneshot(s->ctx, virtio_scsi_dataplane_start_bh, s);
> aio_context_release(s->ctx);
Taking the AioContext lock for code that is running in another thread
seems wrong. But really there is no need to take the lock: I think it
wanted to protect against the handler running before
s->dataplane_starting/started were set, but it's not needed now because
the iothread is busy running the bottom half.
Also, please do the same in virtio-blk as well.
Thanks,
Paolo
next prev parent reply other threads:[~2022-08-05 10:02 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-03 16:28 [PATCH] virtio-scsi: fix race in virtio_scsi_dataplane_start() Stefan Hajnoczi
2022-08-04 7:48 ` Stefano Garzarella
2022-08-04 18:56 ` [PATCH 7.1] " Stefan Hajnoczi
2022-08-05 7:03 ` Michael S. Tsirkin
2022-08-05 7:04 ` [PATCH] " Michael S. Tsirkin
2022-08-05 9:41 ` Paolo Bonzini
2022-08-08 16:01 ` Stefan Hajnoczi
2022-08-05 8:31 ` Emanuele Giuseppe Esposito
2022-08-05 9:40 ` Paolo Bonzini [this message]
2022-08-05 10:49 ` Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=17d794db-4c17-b438-b71b-f6224791a496@redhat.com \
--to=pbonzini@redhat.com \
--cc=eesposit@redhat.com \
--cc=fam@euphon.net \
--cc=kwolf@redhat.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qinwang@redhat.com \
--cc=sgarzare@redhat.com \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).