Commit aff3a925 authored by Jakub Kicinski's avatar Jakub Kicinski Committed by David S. Miller

netdevsim: replace vfs_lock with devlink instance lock

Similarly to the previous commit, use the devlink instance
lock and let it replace the vfs_lock.

nsim_esw_legacy_enable() was locked by both port lock and
vfs lock so one set of lock/unlocks goes away.

netdevsim's .eswitch_mode_set callback is now ready for
the callback to take the instance lock.
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 76eea6c2
...@@ -59,7 +59,7 @@ static struct dentry *nsim_dev_ddir; ...@@ -59,7 +59,7 @@ static struct dentry *nsim_dev_ddir;
unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev) unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev)
{ {
WARN_ON(!lockdep_rtnl_is_held() && WARN_ON(!lockdep_rtnl_is_held() &&
!lockdep_is_held(&nsim_dev->vfs_lock)); !devl_lock_is_held(priv_to_devlink(nsim_dev)));
return nsim_dev->nsim_bus_dev->num_vfs; return nsim_dev->nsim_bus_dev->num_vfs;
} }
...@@ -275,7 +275,7 @@ static ssize_t nsim_bus_dev_max_vfs_write(struct file *file, ...@@ -275,7 +275,7 @@ static ssize_t nsim_bus_dev_max_vfs_write(struct file *file,
return -ENOMEM; return -ENOMEM;
nsim_dev = file->private_data; nsim_dev = file->private_data;
mutex_lock(&nsim_dev->vfs_lock); devl_lock(priv_to_devlink(nsim_dev));
/* Reject if VFs are configured */ /* Reject if VFs are configured */
if (nsim_dev_get_vfs(nsim_dev)) { if (nsim_dev_get_vfs(nsim_dev)) {
ret = -EBUSY; ret = -EBUSY;
...@@ -285,7 +285,7 @@ static ssize_t nsim_bus_dev_max_vfs_write(struct file *file, ...@@ -285,7 +285,7 @@ static ssize_t nsim_bus_dev_max_vfs_write(struct file *file,
*ppos += count; *ppos += count;
ret = count; ret = count;
} }
mutex_unlock(&nsim_dev->vfs_lock); devl_unlock(priv_to_devlink(nsim_dev));
kfree(vfconfigs); kfree(vfconfigs);
return ret; return ret;
...@@ -339,6 +339,7 @@ static int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev) ...@@ -339,6 +339,7 @@ static int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev)
debugfs_create_bool("fail_trap_policer_counter_get", 0600, debugfs_create_bool("fail_trap_policer_counter_get", 0600,
nsim_dev->ddir, nsim_dev->ddir,
&nsim_dev->fail_trap_policer_counter_get); &nsim_dev->fail_trap_policer_counter_get);
/* caution, dev_max_vfs write takes devlink lock */
debugfs_create_file("max_vfs", 0600, nsim_dev->ddir, debugfs_create_file("max_vfs", 0600, nsim_dev->ddir,
nsim_dev, &nsim_dev_max_vfs_fops); nsim_dev, &nsim_dev_max_vfs_fops);
...@@ -567,6 +568,9 @@ static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev) ...@@ -567,6 +568,9 @@ static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev)
devlink_region_destroy(nsim_dev->dummy_region); devlink_region_destroy(nsim_dev->dummy_region);
} }
static int
__nsim_dev_port_add(struct nsim_dev *nsim_dev, enum nsim_dev_port_type type,
unsigned int port_index);
static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port); static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port);
static int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev, static int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev,
...@@ -575,12 +579,10 @@ static int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev, ...@@ -575,12 +579,10 @@ static int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev,
struct devlink *devlink = priv_to_devlink(nsim_dev); struct devlink *devlink = priv_to_devlink(nsim_dev);
struct nsim_dev_port *nsim_dev_port, *tmp; struct nsim_dev_port *nsim_dev_port, *tmp;
devlink_rate_nodes_destroy(devlink); devl_rate_nodes_destroy(devlink);
devl_lock(devlink);
list_for_each_entry_safe(nsim_dev_port, tmp, &nsim_dev->port_list, list) list_for_each_entry_safe(nsim_dev_port, tmp, &nsim_dev->port_list, list)
if (nsim_dev_port_is_vf(nsim_dev_port)) if (nsim_dev_port_is_vf(nsim_dev_port))
__nsim_dev_port_del(nsim_dev_port); __nsim_dev_port_del(nsim_dev_port);
devl_unlock(devlink);
nsim_dev->esw_mode = DEVLINK_ESWITCH_MODE_LEGACY; nsim_dev->esw_mode = DEVLINK_ESWITCH_MODE_LEGACY;
return 0; return 0;
} }
...@@ -588,11 +590,11 @@ static int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev, ...@@ -588,11 +590,11 @@ static int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev,
static int nsim_esw_switchdev_enable(struct nsim_dev *nsim_dev, static int nsim_esw_switchdev_enable(struct nsim_dev *nsim_dev,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
{ {
struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev; struct nsim_dev_port *nsim_dev_port, *tmp;
int i, err; int i, err;
for (i = 0; i < nsim_dev_get_vfs(nsim_dev); i++) { for (i = 0; i < nsim_dev_get_vfs(nsim_dev); i++) {
err = nsim_drv_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_VF, i); err = __nsim_dev_port_add(nsim_dev, NSIM_DEV_PORT_TYPE_VF, i);
if (err) { if (err) {
NL_SET_ERR_MSG_MOD(extack, "Failed to initialize VFs' netdevsim ports"); NL_SET_ERR_MSG_MOD(extack, "Failed to initialize VFs' netdevsim ports");
pr_err("Failed to initialize VF id=%d. %d.\n", i, err); pr_err("Failed to initialize VF id=%d. %d.\n", i, err);
...@@ -603,8 +605,9 @@ static int nsim_esw_switchdev_enable(struct nsim_dev *nsim_dev, ...@@ -603,8 +605,9 @@ static int nsim_esw_switchdev_enable(struct nsim_dev *nsim_dev,
return 0; return 0;
err_port_add_vfs: err_port_add_vfs:
for (i--; i >= 0; i--) list_for_each_entry_safe(nsim_dev_port, tmp, &nsim_dev->port_list, list)
nsim_drv_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_VF, i); if (nsim_dev_port_is_vf(nsim_dev_port))
__nsim_dev_port_del(nsim_dev_port);
return err; return err;
} }
...@@ -614,7 +617,7 @@ static int nsim_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode, ...@@ -614,7 +617,7 @@ static int nsim_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode,
struct nsim_dev *nsim_dev = devlink_priv(devlink); struct nsim_dev *nsim_dev = devlink_priv(devlink);
int err = 0; int err = 0;
mutex_lock(&nsim_dev->vfs_lock); devl_lock(devlink);
if (mode == nsim_dev->esw_mode) if (mode == nsim_dev->esw_mode)
goto unlock; goto unlock;
...@@ -626,7 +629,7 @@ static int nsim_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode, ...@@ -626,7 +629,7 @@ static int nsim_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode,
err = -EINVAL; err = -EINVAL;
unlock: unlock:
mutex_unlock(&nsim_dev->vfs_lock); devl_unlock(devlink);
return err; return err;
} }
...@@ -1545,7 +1548,6 @@ int nsim_drv_probe(struct nsim_bus_dev *nsim_bus_dev) ...@@ -1545,7 +1548,6 @@ int nsim_drv_probe(struct nsim_bus_dev *nsim_bus_dev)
nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id); nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id);
get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len); get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
INIT_LIST_HEAD(&nsim_dev->port_list); INIT_LIST_HEAD(&nsim_dev->port_list);
mutex_init(&nsim_dev->vfs_lock);
nsim_dev->fw_update_status = true; nsim_dev->fw_update_status = true;
nsim_dev->fw_update_overwrite_mask = 0; nsim_dev->fw_update_overwrite_mask = 0;
nsim_dev->max_macs = NSIM_DEV_MAX_MACS_DEFAULT; nsim_dev->max_macs = NSIM_DEV_MAX_MACS_DEFAULT;
...@@ -1652,13 +1654,13 @@ static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev) ...@@ -1652,13 +1654,13 @@ static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev)
return; return;
debugfs_remove(nsim_dev->take_snapshot); debugfs_remove(nsim_dev->take_snapshot);
mutex_lock(&nsim_dev->vfs_lock); devl_lock(devlink);
if (nsim_dev_get_vfs(nsim_dev)) { if (nsim_dev_get_vfs(nsim_dev)) {
nsim_bus_dev_set_vfs(nsim_dev->nsim_bus_dev, 0); nsim_bus_dev_set_vfs(nsim_dev->nsim_bus_dev, 0);
if (nsim_esw_mode_is_switchdev(nsim_dev)) if (nsim_esw_mode_is_switchdev(nsim_dev))
nsim_esw_legacy_enable(nsim_dev, NULL); nsim_esw_legacy_enable(nsim_dev, NULL);
} }
mutex_unlock(&nsim_dev->vfs_lock); devl_unlock(devlink);
nsim_dev_port_del_all(nsim_dev); nsim_dev_port_del_all(nsim_dev);
nsim_dev_hwstats_exit(nsim_dev); nsim_dev_hwstats_exit(nsim_dev);
...@@ -1736,9 +1738,10 @@ int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev, ...@@ -1736,9 +1738,10 @@ int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev,
unsigned int num_vfs) unsigned int num_vfs)
{ {
struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev); struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
struct devlink *devlink = priv_to_devlink(nsim_dev);
int ret = 0; int ret = 0;
mutex_lock(&nsim_dev->vfs_lock); devl_lock(devlink);
if (nsim_bus_dev->num_vfs == num_vfs) if (nsim_bus_dev->num_vfs == num_vfs)
goto exit_unlock; goto exit_unlock;
if (nsim_bus_dev->num_vfs && num_vfs) { if (nsim_bus_dev->num_vfs && num_vfs) {
...@@ -1764,7 +1767,7 @@ int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev, ...@@ -1764,7 +1767,7 @@ int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev,
} }
exit_unlock: exit_unlock:
mutex_unlock(&nsim_dev->vfs_lock); devl_unlock(devlink);
return ret; return ret;
} }
......
...@@ -261,7 +261,6 @@ struct nsim_dev { ...@@ -261,7 +261,6 @@ struct nsim_dev {
struct dentry *take_snapshot; struct dentry *take_snapshot;
struct dentry *nodes_ddir; struct dentry *nodes_ddir;
struct mutex vfs_lock; /* Protects vfconfigs */
struct nsim_vf_config *vfconfigs; struct nsim_vf_config *vfconfigs;
struct bpf_offload_dev *bpf_dev; struct bpf_offload_dev *bpf_dev;
......
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