[PATCH net-next 4/6] devlink: Require devlink lock during device reload

From: Leon Romanovsky
Date: Sun Dec 05 2021 - 03:22:36 EST


From: Leon Romanovsky <leonro@xxxxxxxxxx>

Devlink reload was implemented as a special command which does _SET_
operation, but doesn't take devlink->lock, while recursive devlink
calls that were part of .reload_up()/.reload_down() sequence took it.

This fragile flow was possible due to holding a big devlink lock
(devlink_mutex), which effectively stopped all devlink activities,
even unrelated to reloaded devlink.

So let's make sure that devlink reload behaves as other commands.

Tested-by: Ido Schimmel <idosch@xxxxxxxxxx>
Signed-off-by: Leon Romanovsky <leonro@xxxxxxxxxx>
---
net/core/devlink.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/net/core/devlink.c b/net/core/devlink.c
index 7dd6091b97af..cbffafc1776f 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -8810,7 +8810,6 @@ static const struct genl_small_ops devlink_nl_ops[] = {
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
.doit = devlink_nl_cmd_reload,
.flags = GENL_ADMIN_PERM,
- .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
},
{
.cmd = DEVLINK_CMD_PARAM_GET,
@@ -11448,10 +11447,15 @@ static void __net_exit devlink_pernet_pre_exit(struct net *net)
goto retry;

WARN_ON(!(devlink->features & DEVLINK_F_RELOAD));
+
+ mutex_lock(&devlink->lock);
+ xa_set_mark(&devlinks, devlink->index, DEVLINK_NESTED_LOCK);
err = devlink_reload(devlink, &init_net,
DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
DEVLINK_RELOAD_LIMIT_UNSPEC,
&actions_performed, NULL);
+ xa_clear_mark(&devlinks, devlink->index, DEVLINK_NESTED_LOCK);
+ mutex_unlock(&devlink->lock);
if (err && err != -EOPNOTSUPP)
pr_warn("Failed to reload devlink instance into init_net\n");
retry:
--
2.33.1