From: Jason Wang <jasowang@redhat.com>
To: Stefano Garzarella <sgarzare@redhat.com>, netdev@vger.kernel.org
Cc: kvm@vger.kernel.org, virtualization@lists.linux-foundation.org,
Stefan Hajnoczi <stefanha@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"David S. Miller" <davem@davemloft.net>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/3] vsock/virtio: stop workers during the .remove()
Date: Thu, 4 Jul 2019 12:00:11 +0800 [thread overview]
Message-ID: <04b93975-1cce-1e7e-61ad-246601d4216c@redhat.com> (raw)
In-Reply-To: <20190628123659.139576-3-sgarzare@redhat.com>
On 2019/6/28 下午8:36, Stefano Garzarella wrote:
> Before to call vdev->config->reset(vdev) we need to be sure that
> no one is accessing the device, for this reason, we add new variables
> in the struct virtio_vsock to stop the workers during the .remove().
>
> This patch also add few comments before vdev->config->reset(vdev)
> and vdev->config->del_vqs(vdev).
>
> Suggested-by: Stefan Hajnoczi <stefanha@redhat.com>
> Suggested-by: Michael S. Tsirkin <mst@redhat.com>
> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
> ---
> net/vmw_vsock/virtio_transport.c | 51 +++++++++++++++++++++++++++++++-
> 1 file changed, 50 insertions(+), 1 deletion(-)
This should work. But we may consider to convert the_virtio_vosck to
socket object and use socket refcnt and destructor in the future instead
of inventing something new by ourselves.
Thanks
>
> diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
> index 7ad510ec12e0..1b44ec6f3f6c 100644
> --- a/net/vmw_vsock/virtio_transport.c
> +++ b/net/vmw_vsock/virtio_transport.c
> @@ -38,6 +38,7 @@ struct virtio_vsock {
> * must be accessed with tx_lock held.
> */
> struct mutex tx_lock;
> + bool tx_run;
>
> struct work_struct send_pkt_work;
> spinlock_t send_pkt_list_lock;
> @@ -53,6 +54,7 @@ struct virtio_vsock {
> * must be accessed with rx_lock held.
> */
> struct mutex rx_lock;
> + bool rx_run;
> int rx_buf_nr;
> int rx_buf_max_nr;
>
> @@ -60,6 +62,7 @@ struct virtio_vsock {
> * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
> */
> struct mutex event_lock;
> + bool event_run;
> struct virtio_vsock_event event_list[8];
>
> u32 guest_cid;
> @@ -94,6 +97,10 @@ static void virtio_transport_loopback_work(struct work_struct *work)
> spin_unlock_bh(&vsock->loopback_list_lock);
>
> mutex_lock(&vsock->rx_lock);
> +
> + if (!vsock->rx_run)
> + goto out;
> +
> while (!list_empty(&pkts)) {
> struct virtio_vsock_pkt *pkt;
>
> @@ -102,6 +109,7 @@ static void virtio_transport_loopback_work(struct work_struct *work)
>
> virtio_transport_recv_pkt(pkt);
> }
> +out:
> mutex_unlock(&vsock->rx_lock);
> }
>
> @@ -130,6 +138,9 @@ virtio_transport_send_pkt_work(struct work_struct *work)
>
> mutex_lock(&vsock->tx_lock);
>
> + if (!vsock->tx_run)
> + goto out;
> +
> vq = vsock->vqs[VSOCK_VQ_TX];
>
> for (;;) {
> @@ -188,6 +199,7 @@ virtio_transport_send_pkt_work(struct work_struct *work)
> if (added)
> virtqueue_kick(vq);
>
> +out:
> mutex_unlock(&vsock->tx_lock);
>
> if (restart_rx)
> @@ -323,6 +335,10 @@ static void virtio_transport_tx_work(struct work_struct *work)
>
> vq = vsock->vqs[VSOCK_VQ_TX];
> mutex_lock(&vsock->tx_lock);
> +
> + if (!vsock->tx_run)
> + goto out;
> +
> do {
> struct virtio_vsock_pkt *pkt;
> unsigned int len;
> @@ -333,6 +349,8 @@ static void virtio_transport_tx_work(struct work_struct *work)
> added = true;
> }
> } while (!virtqueue_enable_cb(vq));
> +
> +out:
> mutex_unlock(&vsock->tx_lock);
>
> if (added)
> @@ -361,6 +379,9 @@ static void virtio_transport_rx_work(struct work_struct *work)
>
> mutex_lock(&vsock->rx_lock);
>
> + if (!vsock->rx_run)
> + goto out;
> +
> do {
> virtqueue_disable_cb(vq);
> for (;;) {
> @@ -470,6 +491,9 @@ static void virtio_transport_event_work(struct work_struct *work)
>
> mutex_lock(&vsock->event_lock);
>
> + if (!vsock->event_run)
> + goto out;
> +
> do {
> struct virtio_vsock_event *event;
> unsigned int len;
> @@ -484,7 +508,7 @@ static void virtio_transport_event_work(struct work_struct *work)
> } while (!virtqueue_enable_cb(vq));
>
> virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
> -
> +out:
> mutex_unlock(&vsock->event_lock);
> }
>
> @@ -619,12 +643,18 @@ static int virtio_vsock_probe(struct virtio_device *vdev)
> INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
> INIT_WORK(&vsock->loopback_work, virtio_transport_loopback_work);
>
> + mutex_lock(&vsock->tx_lock);
> + vsock->tx_run = true;
> + mutex_unlock(&vsock->tx_lock);
> +
> mutex_lock(&vsock->rx_lock);
> virtio_vsock_rx_fill(vsock);
> + vsock->rx_run = true;
> mutex_unlock(&vsock->rx_lock);
>
> mutex_lock(&vsock->event_lock);
> virtio_vsock_event_fill(vsock);
> + vsock->event_run = true;
> mutex_unlock(&vsock->event_lock);
>
> vdev->priv = vsock;
> @@ -659,6 +689,24 @@ static void virtio_vsock_remove(struct virtio_device *vdev)
> /* Reset all connected sockets when the device disappear */
> vsock_for_each_connected_socket(virtio_vsock_reset_sock);
>
> + /* Stop all work handlers to make sure no one is accessing the device,
> + * so we can safely call vdev->config->reset().
> + */
> + mutex_lock(&vsock->rx_lock);
> + vsock->rx_run = false;
> + mutex_unlock(&vsock->rx_lock);
> +
> + mutex_lock(&vsock->tx_lock);
> + vsock->tx_run = false;
> + mutex_unlock(&vsock->tx_lock);
> +
> + mutex_lock(&vsock->event_lock);
> + vsock->event_run = false;
> + mutex_unlock(&vsock->event_lock);
> +
> + /* Flush all device writes and interrupts, device will not use any
> + * more buffers.
> + */
> vdev->config->reset(vdev);
>
> mutex_lock(&vsock->rx_lock);
> @@ -689,6 +737,7 @@ static void virtio_vsock_remove(struct virtio_device *vdev)
> }
> spin_unlock_bh(&vsock->loopback_list_lock);
>
> + /* Delete virtqueues and flush outstanding callbacks if any */
> vdev->config->del_vqs(vdev);
>
> mutex_unlock(&the_virtio_vsock_mutex);
next prev parent reply other threads:[~2019-07-04 4:00 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-28 12:36 [PATCH v2 0/3] vsock/virtio: several fixes in the .probe() and .remove() Stefano Garzarella
2019-06-28 12:36 ` [PATCH v2 1/3] vsock/virtio: use RCU to avoid use-after-free on the_virtio_vsock Stefano Garzarella
2019-07-01 14:54 ` Stefan Hajnoczi
2019-07-01 15:10 ` Stefan Hajnoczi
2019-07-03 9:53 ` Jason Wang
2019-07-03 10:41 ` Stefano Garzarella
2019-07-04 3:58 ` Jason Wang
2019-07-04 9:20 ` Stefano Garzarella
2019-07-05 0:18 ` Jason Wang
2019-07-04 10:17 ` Stefan Hajnoczi
2019-06-28 12:36 ` [PATCH v2 2/3] vsock/virtio: stop workers during the .remove() Stefano Garzarella
2019-07-04 4:00 ` Jason Wang [this message]
2019-06-28 12:36 ` [PATCH v2 3/3] vsock/virtio: fix flush of works " Stefano Garzarella
2019-07-01 15:08 ` Stefan Hajnoczi
2019-07-01 15:09 ` Stefan Hajnoczi
2019-07-01 15:11 ` [PATCH v2 0/3] vsock/virtio: several fixes in the .probe() and .remove() Stefan Hajnoczi
2019-07-01 17:03 ` Stefano Garzarella
2019-07-03 9:14 ` Stefan Hajnoczi
2019-07-03 10:07 ` Stefano Garzarella
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=04b93975-1cce-1e7e-61ad-246601d4216c@redhat.com \
--to=jasowang@redhat.com \
--cc=davem@davemloft.net \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=sgarzare@redhat.com \
--cc=stefanha@redhat.com \
--cc=virtualization@lists.linux-foundation.org \
/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
all inboxes | Powered by JetHome®