From: "Michael S. Tsirkin" <mst@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: virtio-dev@lists.oasis-open.org, kvm@vger.kernel.org,
netdev@vger.kernel.org,
virtualization@lists.linux-foundation.org, qinchuanyu@huawei.com,
davem@davemloft.net
Subject: [PATCH net 3/3] vhost: fix a theoretical race in device cleanup
Date: Wed, 12 Feb 2014 18:36:57 +0200 [thread overview]
Message-ID: <1392222846-26699-4-git-send-email-mst@redhat.com> (raw)
In-Reply-To: <1392222846-26699-1-git-send-email-mst@redhat.com>
vhost_zerocopy_callback accesses VQ right after
it drops the last ubuf reference.
In theory, this could race with device removal
which waits on the ubuf kref, and crash
on use after free.
Do all accesses within rcu read side critical
section, and all synchronize on release.
Since callbacks are always invoked from bh,
synchronize_rcu_bh seems enough and will help
release complete a bit faster.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
drivers/vhost/net.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
index 7eaf2de..78a9d42 100644
--- a/drivers/vhost/net.c
+++ b/drivers/vhost/net.c
@@ -308,6 +308,8 @@ static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
struct vhost_virtqueue *vq = ubufs->vq;
int cnt;
+ rcu_read_lock_bh();
+
/* set len to mark this desc buffers done DMA */
vq->heads[ubuf->desc].len = success ?
VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
@@ -322,6 +324,8 @@ static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
*/
if (cnt <= 1 || !(cnt % 16))
vhost_poll_queue(&vq->poll);
+
+ rcu_read_unlock_bh();
}
/* Expects to be always run from workqueue - which acts as
@@ -804,6 +808,8 @@ static int vhost_net_release(struct inode *inode, struct file *f)
fput(tx_sock->file);
if (rx_sock)
fput(rx_sock->file);
+ /* Make sure no callbacks are outstanding */
+ synchronize_rcu_bh();
/* We do an extra flush before freeing memory,
* since jobs can re-queue themselves. */
vhost_net_flush(n);
--
MST
next prev parent reply other threads:[~2014-02-12 16:36 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-12 16:37 [PATCH net 0/3] vhost fixes for 3.14, -stable Michael S. Tsirkin
2014-02-12 16:36 ` Michael S. Tsirkin [this message]
2014-02-12 16:38 ` [PATCH net 2/3] vhost: fix ref cnt checking deadlock Michael S. Tsirkin
2014-02-12 16:38 ` [PATCH net 1/3] kref: add kref_sub_return Michael S. Tsirkin
2014-02-12 16:56 ` Greg Kroah-Hartman
2014-02-12 17:35 ` Michael S. Tsirkin
[not found] ` <20140212173524.GA26860@redhat.com>
2014-02-12 18:37 ` Greg Kroah-Hartman
2014-02-12 18:39 ` Anatol Pomozov
2014-02-13 0:06 ` David Miller
2014-02-13 1:25 ` Jörn Engel
2014-02-13 1:39 ` Greg KH
2014-02-13 4:05 ` David Miller
2014-02-13 4:09 ` David Miller
2014-02-14 0:03 ` Greg KH
2014-02-14 5:10 ` David Miller
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=1392222846-26699-4-git-send-email-mst@redhat.com \
--to=mst@redhat.com \
--cc=davem@davemloft.net \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=qinchuanyu@huawei.com \
--cc=virtio-dev@lists.oasis-open.org \
--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;
as well as URLs for NNTP newsgroup(s).