Commit 0078ad90 authored by Eli Cohen's avatar Eli Cohen Committed by Michael S. Tsirkin

net/vdpa: Use readers/writers semaphore instead of vdpa_dev_mutex

Use rw_semaphore instead of mutex to control access to vdpa devices.
This can be especially beneficial in case processes poll on statistics
information.
Suggested-by: default avatarSi-Wei Liu <si-wei.liu@oracle.com>
Reviewed-by: default avatarSi-Wei Liu <si-wei.liu@oracle.com>
Acked-by: default avatarJason Wang <jasowang@redhat.com>
Signed-off-by: default avatarEli Cohen <elic@nvidia.com>
Message-Id: <20220518133804.1075129-4-elic@nvidia.com>
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent 13b00b13
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
static LIST_HEAD(mdev_head); static LIST_HEAD(mdev_head);
/* A global mutex that protects vdpa management device and device level operations. */ /* A global mutex that protects vdpa management device and device level operations. */
static DEFINE_MUTEX(vdpa_dev_mutex); static DECLARE_RWSEM(vdpa_dev_lock);
static DEFINE_IDA(vdpa_index_ida); static DEFINE_IDA(vdpa_index_ida);
void vdpa_set_status(struct vdpa_device *vdev, u8 status) void vdpa_set_status(struct vdpa_device *vdev, u8 status)
...@@ -238,7 +238,7 @@ static int __vdpa_register_device(struct vdpa_device *vdev, u32 nvqs) ...@@ -238,7 +238,7 @@ static int __vdpa_register_device(struct vdpa_device *vdev, u32 nvqs)
vdev->nvqs = nvqs; vdev->nvqs = nvqs;
lockdep_assert_held(&vdpa_dev_mutex); lockdep_assert_held(&vdpa_dev_lock);
dev = bus_find_device(&vdpa_bus, NULL, dev_name(&vdev->dev), vdpa_name_match); dev = bus_find_device(&vdpa_bus, NULL, dev_name(&vdev->dev), vdpa_name_match);
if (dev) { if (dev) {
put_device(dev); put_device(dev);
...@@ -278,9 +278,9 @@ int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs) ...@@ -278,9 +278,9 @@ int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs)
{ {
int err; int err;
mutex_lock(&vdpa_dev_mutex); down_write(&vdpa_dev_lock);
err = __vdpa_register_device(vdev, nvqs); err = __vdpa_register_device(vdev, nvqs);
mutex_unlock(&vdpa_dev_mutex); up_write(&vdpa_dev_lock);
return err; return err;
} }
EXPORT_SYMBOL_GPL(vdpa_register_device); EXPORT_SYMBOL_GPL(vdpa_register_device);
...@@ -293,7 +293,7 @@ EXPORT_SYMBOL_GPL(vdpa_register_device); ...@@ -293,7 +293,7 @@ EXPORT_SYMBOL_GPL(vdpa_register_device);
*/ */
void _vdpa_unregister_device(struct vdpa_device *vdev) void _vdpa_unregister_device(struct vdpa_device *vdev)
{ {
lockdep_assert_held(&vdpa_dev_mutex); lockdep_assert_held(&vdpa_dev_lock);
WARN_ON(!vdev->mdev); WARN_ON(!vdev->mdev);
device_unregister(&vdev->dev); device_unregister(&vdev->dev);
} }
...@@ -305,9 +305,9 @@ EXPORT_SYMBOL_GPL(_vdpa_unregister_device); ...@@ -305,9 +305,9 @@ EXPORT_SYMBOL_GPL(_vdpa_unregister_device);
*/ */
void vdpa_unregister_device(struct vdpa_device *vdev) void vdpa_unregister_device(struct vdpa_device *vdev)
{ {
mutex_lock(&vdpa_dev_mutex); down_write(&vdpa_dev_lock);
device_unregister(&vdev->dev); device_unregister(&vdev->dev);
mutex_unlock(&vdpa_dev_mutex); up_write(&vdpa_dev_lock);
} }
EXPORT_SYMBOL_GPL(vdpa_unregister_device); EXPORT_SYMBOL_GPL(vdpa_unregister_device);
...@@ -352,9 +352,9 @@ int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev) ...@@ -352,9 +352,9 @@ int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev)
return -EINVAL; return -EINVAL;
INIT_LIST_HEAD(&mdev->list); INIT_LIST_HEAD(&mdev->list);
mutex_lock(&vdpa_dev_mutex); down_write(&vdpa_dev_lock);
list_add_tail(&mdev->list, &mdev_head); list_add_tail(&mdev->list, &mdev_head);
mutex_unlock(&vdpa_dev_mutex); up_write(&vdpa_dev_lock);
return 0; return 0;
} }
EXPORT_SYMBOL_GPL(vdpa_mgmtdev_register); EXPORT_SYMBOL_GPL(vdpa_mgmtdev_register);
...@@ -371,14 +371,14 @@ static int vdpa_match_remove(struct device *dev, void *data) ...@@ -371,14 +371,14 @@ static int vdpa_match_remove(struct device *dev, void *data)
void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev) void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev)
{ {
mutex_lock(&vdpa_dev_mutex); down_write(&vdpa_dev_lock);
list_del(&mdev->list); list_del(&mdev->list);
/* Filter out all the entries belong to this management device and delete it. */ /* Filter out all the entries belong to this management device and delete it. */
bus_for_each_dev(&vdpa_bus, NULL, mdev, vdpa_match_remove); bus_for_each_dev(&vdpa_bus, NULL, mdev, vdpa_match_remove);
mutex_unlock(&vdpa_dev_mutex); up_write(&vdpa_dev_lock);
} }
EXPORT_SYMBOL_GPL(vdpa_mgmtdev_unregister); EXPORT_SYMBOL_GPL(vdpa_mgmtdev_unregister);
...@@ -532,17 +532,17 @@ static int vdpa_nl_cmd_mgmtdev_get_doit(struct sk_buff *skb, struct genl_info *i ...@@ -532,17 +532,17 @@ static int vdpa_nl_cmd_mgmtdev_get_doit(struct sk_buff *skb, struct genl_info *i
if (!msg) if (!msg)
return -ENOMEM; return -ENOMEM;
mutex_lock(&vdpa_dev_mutex); down_read(&vdpa_dev_lock);
mdev = vdpa_mgmtdev_get_from_attr(info->attrs); mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
if (IS_ERR(mdev)) { if (IS_ERR(mdev)) {
mutex_unlock(&vdpa_dev_mutex); up_read(&vdpa_dev_lock);
NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified mgmt device"); NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified mgmt device");
err = PTR_ERR(mdev); err = PTR_ERR(mdev);
goto out; goto out;
} }
err = vdpa_mgmtdev_fill(mdev, msg, info->snd_portid, info->snd_seq, 0); err = vdpa_mgmtdev_fill(mdev, msg, info->snd_portid, info->snd_seq, 0);
mutex_unlock(&vdpa_dev_mutex); up_read(&vdpa_dev_lock);
if (err) if (err)
goto out; goto out;
err = genlmsg_reply(msg, info); err = genlmsg_reply(msg, info);
...@@ -561,7 +561,7 @@ vdpa_nl_cmd_mgmtdev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb) ...@@ -561,7 +561,7 @@ vdpa_nl_cmd_mgmtdev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
int idx = 0; int idx = 0;
int err; int err;
mutex_lock(&vdpa_dev_mutex); down_read(&vdpa_dev_lock);
list_for_each_entry(mdev, &mdev_head, list) { list_for_each_entry(mdev, &mdev_head, list) {
if (idx < start) { if (idx < start) {
idx++; idx++;
...@@ -574,7 +574,7 @@ vdpa_nl_cmd_mgmtdev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb) ...@@ -574,7 +574,7 @@ vdpa_nl_cmd_mgmtdev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
idx++; idx++;
} }
out: out:
mutex_unlock(&vdpa_dev_mutex); up_read(&vdpa_dev_lock);
cb->args[0] = idx; cb->args[0] = idx;
return msg->len; return msg->len;
} }
...@@ -627,7 +627,7 @@ static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff *skb, struct genl_info *i ...@@ -627,7 +627,7 @@ static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff *skb, struct genl_info *i
!netlink_capable(skb, CAP_NET_ADMIN)) !netlink_capable(skb, CAP_NET_ADMIN))
return -EPERM; return -EPERM;
mutex_lock(&vdpa_dev_mutex); down_write(&vdpa_dev_lock);
mdev = vdpa_mgmtdev_get_from_attr(info->attrs); mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
if (IS_ERR(mdev)) { if (IS_ERR(mdev)) {
NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified management device"); NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified management device");
...@@ -643,7 +643,7 @@ static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff *skb, struct genl_info *i ...@@ -643,7 +643,7 @@ static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff *skb, struct genl_info *i
err = mdev->ops->dev_add(mdev, name, &config); err = mdev->ops->dev_add(mdev, name, &config);
err: err:
mutex_unlock(&vdpa_dev_mutex); up_write(&vdpa_dev_lock);
return err; return err;
} }
...@@ -659,7 +659,7 @@ static int vdpa_nl_cmd_dev_del_set_doit(struct sk_buff *skb, struct genl_info *i ...@@ -659,7 +659,7 @@ static int vdpa_nl_cmd_dev_del_set_doit(struct sk_buff *skb, struct genl_info *i
return -EINVAL; return -EINVAL;
name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]); name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
mutex_lock(&vdpa_dev_mutex); down_write(&vdpa_dev_lock);
dev = bus_find_device(&vdpa_bus, NULL, name, vdpa_name_match); dev = bus_find_device(&vdpa_bus, NULL, name, vdpa_name_match);
if (!dev) { if (!dev) {
NL_SET_ERR_MSG_MOD(info->extack, "device not found"); NL_SET_ERR_MSG_MOD(info->extack, "device not found");
...@@ -677,7 +677,7 @@ static int vdpa_nl_cmd_dev_del_set_doit(struct sk_buff *skb, struct genl_info *i ...@@ -677,7 +677,7 @@ static int vdpa_nl_cmd_dev_del_set_doit(struct sk_buff *skb, struct genl_info *i
mdev_err: mdev_err:
put_device(dev); put_device(dev);
dev_err: dev_err:
mutex_unlock(&vdpa_dev_mutex); up_write(&vdpa_dev_lock);
return err; return err;
} }
...@@ -743,7 +743,7 @@ static int vdpa_nl_cmd_dev_get_doit(struct sk_buff *skb, struct genl_info *info) ...@@ -743,7 +743,7 @@ static int vdpa_nl_cmd_dev_get_doit(struct sk_buff *skb, struct genl_info *info)
if (!msg) if (!msg)
return -ENOMEM; return -ENOMEM;
mutex_lock(&vdpa_dev_mutex); down_read(&vdpa_dev_lock);
dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match); dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
if (!dev) { if (!dev) {
NL_SET_ERR_MSG_MOD(info->extack, "device not found"); NL_SET_ERR_MSG_MOD(info->extack, "device not found");
...@@ -761,13 +761,13 @@ static int vdpa_nl_cmd_dev_get_doit(struct sk_buff *skb, struct genl_info *info) ...@@ -761,13 +761,13 @@ static int vdpa_nl_cmd_dev_get_doit(struct sk_buff *skb, struct genl_info *info)
err = genlmsg_reply(msg, info); err = genlmsg_reply(msg, info);
put_device(dev); put_device(dev);
mutex_unlock(&vdpa_dev_mutex); up_read(&vdpa_dev_lock);
return err; return err;
mdev_err: mdev_err:
put_device(dev); put_device(dev);
err: err:
mutex_unlock(&vdpa_dev_mutex); up_read(&vdpa_dev_lock);
nlmsg_free(msg); nlmsg_free(msg);
return err; return err;
} }
...@@ -809,9 +809,9 @@ static int vdpa_nl_cmd_dev_get_dumpit(struct sk_buff *msg, struct netlink_callba ...@@ -809,9 +809,9 @@ static int vdpa_nl_cmd_dev_get_dumpit(struct sk_buff *msg, struct netlink_callba
info.start_idx = cb->args[0]; info.start_idx = cb->args[0];
info.idx = 0; info.idx = 0;
mutex_lock(&vdpa_dev_mutex); down_read(&vdpa_dev_lock);
bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_dump); bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_dump);
mutex_unlock(&vdpa_dev_mutex); up_read(&vdpa_dev_lock);
cb->args[0] = info.idx; cb->args[0] = info.idx;
return msg->len; return msg->len;
} }
...@@ -1031,7 +1031,7 @@ static int vdpa_nl_cmd_dev_config_get_doit(struct sk_buff *skb, struct genl_info ...@@ -1031,7 +1031,7 @@ static int vdpa_nl_cmd_dev_config_get_doit(struct sk_buff *skb, struct genl_info
if (!msg) if (!msg)
return -ENOMEM; return -ENOMEM;
mutex_lock(&vdpa_dev_mutex); down_read(&vdpa_dev_lock);
dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match); dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
if (!dev) { if (!dev) {
NL_SET_ERR_MSG_MOD(info->extack, "device not found"); NL_SET_ERR_MSG_MOD(info->extack, "device not found");
...@@ -1052,7 +1052,7 @@ static int vdpa_nl_cmd_dev_config_get_doit(struct sk_buff *skb, struct genl_info ...@@ -1052,7 +1052,7 @@ static int vdpa_nl_cmd_dev_config_get_doit(struct sk_buff *skb, struct genl_info
mdev_err: mdev_err:
put_device(dev); put_device(dev);
dev_err: dev_err:
mutex_unlock(&vdpa_dev_mutex); up_read(&vdpa_dev_lock);
if (err) if (err)
nlmsg_free(msg); nlmsg_free(msg);
return err; return err;
...@@ -1090,9 +1090,9 @@ vdpa_nl_cmd_dev_config_get_dumpit(struct sk_buff *msg, struct netlink_callback * ...@@ -1090,9 +1090,9 @@ vdpa_nl_cmd_dev_config_get_dumpit(struct sk_buff *msg, struct netlink_callback *
info.start_idx = cb->args[0]; info.start_idx = cb->args[0];
info.idx = 0; info.idx = 0;
mutex_lock(&vdpa_dev_mutex); down_read(&vdpa_dev_lock);
bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_config_dump); bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_config_dump);
mutex_unlock(&vdpa_dev_mutex); up_read(&vdpa_dev_lock);
cb->args[0] = info.idx; cb->args[0] = info.idx;
return msg->len; return msg->len;
} }
...@@ -1119,7 +1119,7 @@ static int vdpa_nl_cmd_dev_stats_get_doit(struct sk_buff *skb, ...@@ -1119,7 +1119,7 @@ static int vdpa_nl_cmd_dev_stats_get_doit(struct sk_buff *skb,
return -ENOMEM; return -ENOMEM;
index = nla_get_u32(info->attrs[VDPA_ATTR_DEV_QUEUE_INDEX]); index = nla_get_u32(info->attrs[VDPA_ATTR_DEV_QUEUE_INDEX]);
mutex_lock(&vdpa_dev_mutex); down_read(&vdpa_dev_lock);
dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match); dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
if (!dev) { if (!dev) {
NL_SET_ERR_MSG_MOD(info->extack, "device not found"); NL_SET_ERR_MSG_MOD(info->extack, "device not found");
...@@ -1139,7 +1139,7 @@ static int vdpa_nl_cmd_dev_stats_get_doit(struct sk_buff *skb, ...@@ -1139,7 +1139,7 @@ static int vdpa_nl_cmd_dev_stats_get_doit(struct sk_buff *skb,
err = genlmsg_reply(msg, info); err = genlmsg_reply(msg, info);
put_device(dev); put_device(dev);
mutex_unlock(&vdpa_dev_mutex); up_read(&vdpa_dev_lock);
return err; return err;
...@@ -1147,7 +1147,7 @@ static int vdpa_nl_cmd_dev_stats_get_doit(struct sk_buff *skb, ...@@ -1147,7 +1147,7 @@ static int vdpa_nl_cmd_dev_stats_get_doit(struct sk_buff *skb,
put_device(dev); put_device(dev);
dev_err: dev_err:
nlmsg_free(msg); nlmsg_free(msg);
mutex_unlock(&vdpa_dev_mutex); up_read(&vdpa_dev_lock);
return err; return err;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment