Skip to content

Commit

Permalink
net/vdpa: Use readers/writers semaphore instead of cf_mutex
Browse files Browse the repository at this point in the history
Replace cf_mutex with rw_semaphore to reflect the fact that some calls
could be called concurrently but can suffice with read lock.

Suggested-by: Si-Wei Liu <[email protected]>
Signed-off-by: Eli Cohen <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Michael S. Tsirkin <[email protected]>
  • Loading branch information
elic307i authored and mstsirkin committed May 31, 2022
1 parent 0078ad9 commit a6a51ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
25 changes: 12 additions & 13 deletions drivers/vdpa/vdpa.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ static DEFINE_IDA(vdpa_index_ida);

void vdpa_set_status(struct vdpa_device *vdev, u8 status)
{
mutex_lock(&vdev->cf_mutex);
down_write(&vdev->cf_lock);
vdev->config->set_status(vdev, status);
mutex_unlock(&vdev->cf_mutex);
up_write(&vdev->cf_lock);
}
EXPORT_SYMBOL(vdpa_set_status);

Expand Down Expand Up @@ -148,7 +148,6 @@ static void vdpa_release_dev(struct device *d)
ops->free(vdev);

ida_simple_remove(&vdpa_index_ida, vdev->index);
mutex_destroy(&vdev->cf_mutex);
kfree(vdev->driver_override);
kfree(vdev);
}
Expand Down Expand Up @@ -211,7 +210,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
if (err)
goto err_name;

mutex_init(&vdev->cf_mutex);
init_rwsem(&vdev->cf_lock);
device_initialize(&vdev->dev);

return vdev;
Expand Down Expand Up @@ -407,9 +406,9 @@ static void vdpa_get_config_unlocked(struct vdpa_device *vdev,
void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
void *buf, unsigned int len)
{
mutex_lock(&vdev->cf_mutex);
down_read(&vdev->cf_lock);
vdpa_get_config_unlocked(vdev, offset, buf, len);
mutex_unlock(&vdev->cf_mutex);
up_read(&vdev->cf_lock);
}
EXPORT_SYMBOL_GPL(vdpa_get_config);

Expand All @@ -423,9 +422,9 @@ EXPORT_SYMBOL_GPL(vdpa_get_config);
void vdpa_set_config(struct vdpa_device *vdev, unsigned int offset,
const void *buf, unsigned int length)
{
mutex_lock(&vdev->cf_mutex);
down_write(&vdev->cf_lock);
vdev->config->set_config(vdev, offset, buf, length);
mutex_unlock(&vdev->cf_mutex);
up_write(&vdev->cf_lock);
}
EXPORT_SYMBOL_GPL(vdpa_set_config);

Expand Down Expand Up @@ -866,7 +865,7 @@ vdpa_dev_config_fill(struct vdpa_device *vdev, struct sk_buff *msg, u32 portid,
u8 status;
int err;

mutex_lock(&vdev->cf_mutex);
down_read(&vdev->cf_lock);
status = vdev->config->get_status(vdev);
if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
NL_SET_ERR_MSG_MOD(extack, "Features negotiation not completed");
Expand Down Expand Up @@ -903,14 +902,14 @@ vdpa_dev_config_fill(struct vdpa_device *vdev, struct sk_buff *msg, u32 portid,
if (err)
goto msg_err;

mutex_unlock(&vdev->cf_mutex);
up_read(&vdev->cf_lock);
genlmsg_end(msg, hdr);
return 0;

msg_err:
genlmsg_cancel(msg, hdr);
out:
mutex_unlock(&vdev->cf_mutex);
up_read(&vdev->cf_lock);
return err;
}

Expand Down Expand Up @@ -954,15 +953,15 @@ static int vendor_stats_fill(struct vdpa_device *vdev, struct sk_buff *msg,
{
int err;

mutex_lock(&vdev->cf_mutex);
down_read(&vdev->cf_lock);
if (!vdev->config->get_vendor_vq_stats) {
err = -EOPNOTSUPP;
goto out;
}

err = vdpa_fill_stats_rec(vdev, msg, info, index);
out:
mutex_unlock(&vdev->cf_mutex);
up_read(&vdev->cf_lock);
return err;
}

Expand Down
12 changes: 6 additions & 6 deletions include/linux/vdpa.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct vdpa_mgmt_dev;
* @dma_dev: the actual device that is performing DMA
* @driver_override: driver name to force a match
* @config: the configuration ops for this device.
* @cf_mutex: Protects get and set access to configuration layout.
* @cf_lock: Protects get and set access to configuration layout.
* @index: device index
* @features_valid: were features initialized? for legacy guests
* @use_va: indicate whether virtual address must be used by this device
Expand All @@ -79,7 +79,7 @@ struct vdpa_device {
struct device *dma_dev;
const char *driver_override;
const struct vdpa_config_ops *config;
struct mutex cf_mutex; /* Protects get/set config */
struct rw_semaphore cf_lock; /* Protects get/set config */
unsigned int index;
bool features_valid;
bool use_va;
Expand Down Expand Up @@ -398,10 +398,10 @@ static inline int vdpa_reset(struct vdpa_device *vdev)
const struct vdpa_config_ops *ops = vdev->config;
int ret;

mutex_lock(&vdev->cf_mutex);
down_write(&vdev->cf_lock);
vdev->features_valid = false;
ret = ops->reset(vdev);
mutex_unlock(&vdev->cf_mutex);
up_write(&vdev->cf_lock);
return ret;
}

Expand All @@ -420,9 +420,9 @@ static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
{
int ret;

mutex_lock(&vdev->cf_mutex);
down_write(&vdev->cf_lock);
ret = vdpa_set_features_unlocked(vdev, features);
mutex_unlock(&vdev->cf_mutex);
up_write(&vdev->cf_lock);

return ret;
}
Expand Down

0 comments on commit a6a51ad

Please sign in to comment.