mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vincent Whitchurch <vincent.whitchurch@axis.com>
To: <mst@redhat.com>, <jasowang@redhat.com>
Cc: <kernel@axis.com>, <kvm@vger.kernel.org>,
	<virtualization@lists.linux-foundation.org>,
	<netdev@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<pbonzini@redhat.com>, <stefanha@redhat.com>,
	<sgarzare@redhat.com>,
	Vincent Whitchurch <vincent.whitchurch@axis.com>
Subject: [RFC PATCH 06/10] vhost: extract ioctl locking to common code
Date: Wed, 29 Sep 2021 17:11:15 +0200	[thread overview]
Message-ID: <20210929151119.14778-7-vincent.whitchurch@axis.com> (raw)
In-Reply-To: <20210929151119.14778-1-vincent.whitchurch@axis.com>

Extract the mutex locking for the vhost ioctl into common code.  This
will allow the common code to easily add some extra handling required
for adding a kernel API to control vhost.

Signed-off-by: Vincent Whitchurch <vincent.whitchurch@axis.com>
---
 drivers/vhost/common.c |  7 ++++++-
 drivers/vhost/net.c    | 14 +-------------
 drivers/vhost/vhost.c  | 10 ++++++++--
 drivers/vhost/vhost.h  |  1 +
 drivers/vhost/vsock.c  | 12 ------------
 5 files changed, 16 insertions(+), 28 deletions(-)

diff --git a/drivers/vhost/common.c b/drivers/vhost/common.c
index 27d4672b15d3..a5722ad65e24 100644
--- a/drivers/vhost/common.c
+++ b/drivers/vhost/common.c
@@ -60,8 +60,13 @@ static long vhost_ioctl(struct file *file, unsigned int ioctl, unsigned long arg
 {
 	struct vhost_dev *dev = file->private_data;
 	struct vhost *vhost = dev->vhost;
+	long ret;
 
-	return vhost->ops->ioctl(dev, ioctl, arg);
+	mutex_lock(&dev->mutex);
+	ret = vhost->ops->ioctl(dev, ioctl, arg);
+	mutex_unlock(&dev->mutex);
+
+	return ret;
 }
 
 static ssize_t vhost_read_iter(struct kiocb *iocb, struct iov_iter *to)
diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
index 8910d9e2a74e..b5590b7862a9 100644
--- a/drivers/vhost/net.c
+++ b/drivers/vhost/net.c
@@ -1505,7 +1505,6 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
 	struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
 	int r;
 
-	mutex_lock(&n->dev.mutex);
 	r = vhost_dev_check_owner(&n->dev);
 	if (r)
 		goto err;
@@ -1573,7 +1572,6 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
 		sockfd_put(oldsock);
 	}
 
-	mutex_unlock(&n->dev.mutex);
 	return 0;
 
 err_used:
@@ -1587,7 +1585,6 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
 err_vq:
 	mutex_unlock(&vq->mutex);
 err:
-	mutex_unlock(&n->dev.mutex);
 	return r;
 }
 
@@ -1598,7 +1595,6 @@ static long vhost_net_reset_owner(struct vhost_net *n)
 	long err;
 	struct vhost_iotlb *umem;
 
-	mutex_lock(&n->dev.mutex);
 	err = vhost_dev_check_owner(&n->dev);
 	if (err)
 		goto done;
@@ -1613,7 +1609,6 @@ static long vhost_net_reset_owner(struct vhost_net *n)
 	vhost_dev_reset_owner(&n->dev, umem);
 	vhost_net_vq_reset(n);
 done:
-	mutex_unlock(&n->dev.mutex);
 	if (tx_sock)
 		sockfd_put(tx_sock);
 	if (rx_sock)
@@ -1639,7 +1634,6 @@ static int vhost_net_set_features(struct vhost_net *n, u64 features)
 		vhost_hlen = 0;
 		sock_hlen = hdr_len;
 	}
-	mutex_lock(&n->dev.mutex);
 	if ((features & (1 << VHOST_F_LOG_ALL)) &&
 	    !vhost_log_access_ok(&n->dev))
 		goto out_unlock;
@@ -1656,11 +1650,9 @@ static int vhost_net_set_features(struct vhost_net *n, u64 features)
 		n->vqs[i].sock_hlen = sock_hlen;
 		mutex_unlock(&n->vqs[i].vq.mutex);
 	}
-	mutex_unlock(&n->dev.mutex);
 	return 0;
 
 out_unlock:
-	mutex_unlock(&n->dev.mutex);
 	return -EFAULT;
 }
 
@@ -1668,7 +1660,6 @@ static long vhost_net_set_owner(struct vhost_net *n)
 {
 	int r;
 
-	mutex_lock(&n->dev.mutex);
 	if (vhost_dev_has_owner(&n->dev)) {
 		r = -EBUSY;
 		goto out;
@@ -1681,7 +1672,6 @@ static long vhost_net_set_owner(struct vhost_net *n)
 		vhost_net_clear_ubuf_info(n);
 	vhost_net_flush(n);
 out:
-	mutex_unlock(&n->dev.mutex);
 	return r;
 }
 
@@ -1721,20 +1711,18 @@ static long vhost_net_ioctl(struct vhost_dev *dev, unsigned int ioctl,
 			return -EFAULT;
 		if (features & ~VHOST_NET_BACKEND_FEATURES)
 			return -EOPNOTSUPP;
-		vhost_set_backend_features(&n->dev, features);
+		__vhost_set_backend_features(&n->dev, features);
 		return 0;
 	case VHOST_RESET_OWNER:
 		return vhost_net_reset_owner(n);
 	case VHOST_SET_OWNER:
 		return vhost_net_set_owner(n);
 	default:
-		mutex_lock(&n->dev.mutex);
 		r = vhost_dev_ioctl(&n->dev, ioctl, argp);
 		if (r == -ENOIOCTLCMD)
 			r = vhost_vring_ioctl(&n->dev, ioctl, argp);
 		else
 			vhost_net_flush(n);
-		mutex_unlock(&n->dev.mutex);
 		return r;
 	}
 }
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 9354061ce75e..9d6496b7ad85 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -2821,18 +2821,24 @@ struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
 }
 EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
 
-void vhost_set_backend_features(struct vhost_dev *dev, u64 features)
+void __vhost_set_backend_features(struct vhost_dev *dev, u64 features)
 {
 	struct vhost_virtqueue *vq;
 	int i;
 
-	mutex_lock(&dev->mutex);
 	for (i = 0; i < dev->nvqs; ++i) {
 		vq = dev->vqs[i];
 		mutex_lock(&vq->mutex);
 		vq->acked_backend_features = features;
 		mutex_unlock(&vq->mutex);
 	}
+}
+EXPORT_SYMBOL_GPL(__vhost_set_backend_features);
+
+void vhost_set_backend_features(struct vhost_dev *dev, u64 features)
+{
+	mutex_lock(&dev->mutex);
+	__vhost_set_backend_features(dev, features);
 	mutex_unlock(&dev->mutex);
 }
 EXPORT_SYMBOL_GPL(vhost_set_backend_features);
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
index 562387b92730..408ff243ed31 100644
--- a/drivers/vhost/vhost.h
+++ b/drivers/vhost/vhost.h
@@ -302,6 +302,7 @@ void vhost_enqueue_msg(struct vhost_dev *dev,
 struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
 					 struct list_head *head);
 void vhost_set_backend_features(struct vhost_dev *dev, u64 features);
+void __vhost_set_backend_features(struct vhost_dev *dev, u64 features);
 
 __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
 			    poll_table *wait);
diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index 93f74a0010d5..062767636226 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -583,8 +583,6 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
 	size_t i;
 	int ret;
 
-	mutex_lock(&vsock->dev.mutex);
-
 	ret = vhost_dev_check_owner(&vsock->dev);
 	if (ret)
 		goto err;
@@ -614,7 +612,6 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
 	 */
 	vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
 
-	mutex_unlock(&vsock->dev.mutex);
 	return 0;
 
 err_vq:
@@ -629,7 +626,6 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
 		mutex_unlock(&vq->mutex);
 	}
 err:
-	mutex_unlock(&vsock->dev.mutex);
 	return ret;
 }
 
@@ -638,8 +634,6 @@ static int vhost_vsock_stop(struct vhost_vsock *vsock)
 	size_t i;
 	int ret;
 
-	mutex_lock(&vsock->dev.mutex);
-
 	ret = vhost_dev_check_owner(&vsock->dev);
 	if (ret)
 		goto err;
@@ -653,7 +647,6 @@ static int vhost_vsock_stop(struct vhost_vsock *vsock)
 	}
 
 err:
-	mutex_unlock(&vsock->dev.mutex);
 	return ret;
 }
 
@@ -821,7 +814,6 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 	if (features & ~VHOST_VSOCK_FEATURES)
 		return -EOPNOTSUPP;
 
-	mutex_lock(&vsock->dev.mutex);
 	if ((features & (1 << VHOST_F_LOG_ALL)) &&
 	    !vhost_log_access_ok(&vsock->dev)) {
 		goto err;
@@ -841,11 +833,9 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 		vq->acked_features = features;
 		mutex_unlock(&vq->mutex);
 	}
-	mutex_unlock(&vsock->dev.mutex);
 	return 0;
 
 err:
-	mutex_unlock(&vsock->dev.mutex);
 	return -EFAULT;
 }
 
@@ -893,13 +883,11 @@ static long vhost_vsock_dev_ioctl(struct vhost_dev *dev, unsigned int ioctl,
 		vhost_set_backend_features(&vsock->dev, features);
 		return 0;
 	default:
-		mutex_lock(&vsock->dev.mutex);
 		r = vhost_dev_ioctl(&vsock->dev, ioctl, argp);
 		if (r == -ENOIOCTLCMD)
 			r = vhost_vring_ioctl(&vsock->dev, ioctl, argp);
 		else
 			vhost_vsock_flush(vsock);
-		mutex_unlock(&vsock->dev.mutex);
 		return r;
 	}
 }
-- 
2.28.0


  parent reply	other threads:[~2021-09-29 15:11 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-29 15:11 [RFC PATCH 00/10] Support kernel buffers in vhost Vincent Whitchurch
2021-09-29 15:11 ` [RFC PATCH 01/10] vhost: scsi: use copy_to_iter() Vincent Whitchurch
2021-09-29 15:11 ` [RFC PATCH 02/10] vhost: push virtqueue area pointers into a user struct Vincent Whitchurch
2021-09-29 15:11 ` [RFC PATCH 03/10] vhost: add iov wrapper Vincent Whitchurch
2021-09-29 15:11 ` [RFC PATCH 04/10] vhost: add support for kernel buffers Vincent Whitchurch
2021-09-29 15:11 ` [RFC PATCH 05/10] vhost: extract common code for file_operations handling Vincent Whitchurch
2021-09-29 15:11 ` Vincent Whitchurch [this message]
2021-09-29 15:11 ` [RFC PATCH 07/10] vhost: add support for kernel control Vincent Whitchurch
2021-09-29 15:11 ` [RFC PATCH 08/10] vhost: net: " Vincent Whitchurch
2021-09-29 15:11 ` [RFC PATCH 09/10] vdpa: add test driver for kernel buffers in vhost Vincent Whitchurch
2021-09-29 15:11 ` [RFC PATCH 10/10] selftests: add vhost_kernel tests Vincent Whitchurch

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=20210929151119.14778-7-vincent.whitchurch@axis.com \
    --to=vincent.whitchurch@axis.com \
    --cc=jasowang@redhat.com \
    --cc=kernel@axis.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --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®