It will be used in a few more scenarios read-only so make it more
scalable.

Suggested-by: Xie Yongji <xieyon...@bytedance.com>
Signed-off-by: Eugenio Pérez <epere...@redhat.com>
---
v2: New in v2
---
 drivers/vdpa/vdpa_user/vduse_dev.c | 41 +++++++++++++++---------------
 1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c 
b/drivers/vdpa/vdpa_user/vduse_dev.c
index e7bced0b5542..2b6a8958ffe0 100644
--- a/drivers/vdpa/vdpa_user/vduse_dev.c
+++ b/drivers/vdpa/vdpa_user/vduse_dev.c
@@ -14,6 +14,7 @@
 #include <linux/cdev.h>
 #include <linux/device.h>
 #include <linux/eventfd.h>
+#include <linux/rwlock.h>
 #include <linux/slab.h>
 #include <linux/wait.h>
 #include <linux/dma-map-ops.h>
@@ -117,7 +118,7 @@ struct vduse_dev {
        struct vduse_umem *umem;
        struct mutex mem_lock;
        unsigned int bounce_size;
-       struct mutex domain_lock;
+       rwlock_t domain_lock;
 };
 
 struct vduse_dev_msg {
@@ -1176,9 +1177,9 @@ static long vduse_dev_ioctl(struct file *file, unsigned 
int cmd,
                if (entry.start > entry.last)
                        break;
 
-               mutex_lock(&dev->domain_lock);
+               read_lock(&dev->domain_lock);
                if (!dev->domain) {
-                       mutex_unlock(&dev->domain_lock);
+                       read_unlock(&dev->domain_lock);
                        break;
                }
                spin_lock(&dev->domain->iotlb_lock);
@@ -1193,7 +1194,7 @@ static long vduse_dev_ioctl(struct file *file, unsigned 
int cmd,
                        entry.perm = map->perm;
                }
                spin_unlock(&dev->domain->iotlb_lock);
-               mutex_unlock(&dev->domain_lock);
+               read_unlock(&dev->domain_lock);
                ret = -EINVAL;
                if (!f)
                        break;
@@ -1346,10 +1347,10 @@ static long vduse_dev_ioctl(struct file *file, unsigned 
int cmd,
                                 sizeof(umem.reserved)))
                        break;
 
-               mutex_lock(&dev->domain_lock);
+               write_lock(&dev->domain_lock);
                ret = vduse_dev_reg_umem(dev, umem.iova,
                                         umem.uaddr, umem.size);
-               mutex_unlock(&dev->domain_lock);
+               write_unlock(&dev->domain_lock);
                break;
        }
        case VDUSE_IOTLB_DEREG_UMEM: {
@@ -1363,10 +1364,10 @@ static long vduse_dev_ioctl(struct file *file, unsigned 
int cmd,
                if (!is_mem_zero((const char *)umem.reserved,
                                 sizeof(umem.reserved)))
                        break;
-               mutex_lock(&dev->domain_lock);
+               write_lock(&dev->domain_lock);
                ret = vduse_dev_dereg_umem(dev, umem.iova,
                                           umem.size);
-               mutex_unlock(&dev->domain_lock);
+               write_unlock(&dev->domain_lock);
                break;
        }
        case VDUSE_IOTLB_GET_INFO: {
@@ -1385,9 +1386,9 @@ static long vduse_dev_ioctl(struct file *file, unsigned 
int cmd,
                                 sizeof(info.reserved)))
                        break;
 
-               mutex_lock(&dev->domain_lock);
+               read_lock(&dev->domain_lock);
                if (!dev->domain) {
-                       mutex_unlock(&dev->domain_lock);
+                       read_unlock(&dev->domain_lock);
                        break;
                }
                spin_lock(&dev->domain->iotlb_lock);
@@ -1402,7 +1403,7 @@ static long vduse_dev_ioctl(struct file *file, unsigned 
int cmd,
                                info.capability |= VDUSE_IOVA_CAP_UMEM;
                }
                spin_unlock(&dev->domain->iotlb_lock);
-               mutex_unlock(&dev->domain_lock);
+               read_unlock(&dev->domain_lock);
                if (!map)
                        break;
 
@@ -1425,10 +1426,10 @@ static int vduse_dev_release(struct inode *inode, 
struct file *file)
 {
        struct vduse_dev *dev = file->private_data;
 
-       mutex_lock(&dev->domain_lock);
+       write_lock(&dev->domain_lock);
        if (dev->domain)
                vduse_dev_dereg_umem(dev, 0, dev->domain->bounce_size);
-       mutex_unlock(&dev->domain_lock);
+       write_unlock(&dev->domain_lock);
        spin_lock(&dev->msg_lock);
        /* Make sure the inflight messages can processed after reconncection */
        list_splice_init(&dev->recv_list, &dev->send_list);
@@ -1647,7 +1648,7 @@ static struct vduse_dev *vduse_dev_create(void)
 
        mutex_init(&dev->lock);
        mutex_init(&dev->mem_lock);
-       mutex_init(&dev->domain_lock);
+       rwlock_init(&dev->domain_lock);
        spin_lock_init(&dev->msg_lock);
        INIT_LIST_HEAD(&dev->send_list);
        INIT_LIST_HEAD(&dev->recv_list);
@@ -1805,7 +1806,7 @@ static ssize_t bounce_size_store(struct device *device,
        int ret;
 
        ret = -EPERM;
-       mutex_lock(&dev->domain_lock);
+       write_lock(&dev->domain_lock);
        if (dev->domain)
                goto unlock;
 
@@ -1821,7 +1822,7 @@ static ssize_t bounce_size_store(struct device *device,
        dev->bounce_size = bounce_size & PAGE_MASK;
        ret = count;
 unlock:
-       mutex_unlock(&dev->domain_lock);
+       write_unlock(&dev->domain_lock);
        return ret;
 }
 
@@ -2045,11 +2046,11 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev, 
const char *name,
        if (ret)
                return ret;
 
-       mutex_lock(&dev->domain_lock);
+       write_lock(&dev->domain_lock);
        if (!dev->domain)
                dev->domain = vduse_domain_create(VDUSE_IOVA_SIZE - 1,
                                                  dev->bounce_size);
-       mutex_unlock(&dev->domain_lock);
+       write_unlock(&dev->domain_lock);
        if (!dev->domain) {
                put_device(&dev->vdev->vdpa.dev);
                return -ENOMEM;
@@ -2059,10 +2060,10 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev, 
const char *name,
        ret = _vdpa_register_device(&dev->vdev->vdpa, dev->vq_num);
        if (ret) {
                put_device(&dev->vdev->vdpa.dev);
-               mutex_lock(&dev->domain_lock);
+               write_lock(&dev->domain_lock);
                vduse_domain_destroy(dev->domain);
                dev->domain = NULL;
-               mutex_unlock(&dev->domain_lock);
+               write_unlock(&dev->domain_lock);
                return ret;
        }
 
-- 
2.51.0


Reply via email to