diff mbox series

[net] devlink: Fix devlink parallel commands processing

Message ID 20240311085726.273193-1-shayd@nvidia.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net] devlink: Fix devlink parallel commands processing | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 958 this patch: 958
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 5 of 5 maintainers
netdev/build_clang success Errors and warnings before: 973 this patch: 973
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 974 this patch: 974
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 25 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest fail net-next-2024-03-11--15-00 (tests: 888)

Commit Message

Shay Drori March 11, 2024, 8:57 a.m. UTC
Commit 870c7ad4a52b ("devlink: protect devlink->dev by the instance
lock") added devlink instance locking inside a loop that iterates over
all the registered devlink instances on the machine in the pre-doit
phase. This can lead to serialization of devlink commands over
different devlink instances.

For example: While the first devlink instance is executing firmware
flash, all commands to other devlink instances on the machine are
forced to wait until the first devlink finishes.

Therefore, in the pre-doit phase, take the devlink instance lock only
for the devlink instance the command is targeting. Devlink layer is
taking a reference on the devlink instance, ensuring the devlink->dev
pointer is valid. This reference taking was introduced by commit
a380687200e0 ("devlink: take device reference for devlink object").
Without this commit, it would not be safe to access devlink->dev
lockless.

Fixes: 870c7ad4a52b ("devlink: protect devlink->dev by the instance lock")
Signed-off-by: Shay Drory <shayd@nvidia.com>
Reviewed-by: Jiri Pirko <jiri@nvidia.com>
---
 net/devlink/netlink.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

Comments

Jakub Kicinski March 11, 2024, 6:10 p.m. UTC | #1
On Mon, 11 Mar 2024 10:57:26 +0200 Shay Drory wrote:
>  	devlinks_xa_for_each_registered_get(net, index, devlink) {
> -		devl_dev_lock(devlink, dev_lock);
> -		if (devl_is_registered(devlink) &&
> -		    strcmp(devlink->dev->bus->name, busname) == 0 &&
> +		if (strcmp(devlink->dev->bus->name, busname) == 0 &&
>  		    strcmp(dev_name(devlink->dev), devname) == 0)
> -			return devlink;
> -		devl_dev_unlock(devlink, dev_lock);
> +			goto found;

there's no need for a goto here:

		if (strcmp(devlink->dev->bus->name, busname) == 0 &&
		    strcmp(dev_name(devlink->dev), devname) == 0) {
			devl_dev_lock(devlink, dev_lock);
			if (devl_is_registered(devlink))
				return devlink;
			devl_dev_unlock(devlink, dev_lock);
		}

simpler, and also no change in behavior (in case some impossible
race happens and we have 2 devlinks with the same name, one already
unregistered and one registered).

>  		devlink_put(devlink);
>  	}
> +	return ERR_PTR(-ENODEV);
> +
> +found:
> +	devl_dev_lock(devlink, dev_lock);
> +	if (devl_is_registered(devlink))
> +		return devlink;
>  
> +	devl_dev_unlock(devlink, dev_lock);
> +	devlink_put(devlink);
>  	return ERR_PTR(-ENODEV);
diff mbox series

Patch

diff --git a/net/devlink/netlink.c b/net/devlink/netlink.c
index 499885c8b9ca..cffc7274de8c 100644
--- a/net/devlink/netlink.c
+++ b/net/devlink/netlink.c
@@ -193,15 +193,20 @@  devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs,
 	devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
 
 	devlinks_xa_for_each_registered_get(net, index, devlink) {
-		devl_dev_lock(devlink, dev_lock);
-		if (devl_is_registered(devlink) &&
-		    strcmp(devlink->dev->bus->name, busname) == 0 &&
+		if (strcmp(devlink->dev->bus->name, busname) == 0 &&
 		    strcmp(dev_name(devlink->dev), devname) == 0)
-			return devlink;
-		devl_dev_unlock(devlink, dev_lock);
+			goto found;
 		devlink_put(devlink);
 	}
+	return ERR_PTR(-ENODEV);
+
+found:
+	devl_dev_lock(devlink, dev_lock);
+	if (devl_is_registered(devlink))
+		return devlink;
 
+	devl_dev_unlock(devlink, dev_lock);
+	devlink_put(devlink);
 	return ERR_PTR(-ENODEV);
 }