From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=40.107.201.0 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796218782; b=GUo283os9w0/LT06VafS9oaKxLayth1NG+G79TUdY1ZvpHECv+AZ0nawkAP9PBGz8Z8b mqWUrvd4duq8SdFsKMEFJiioVnpSPlaQvHErpYaQNVslTD6lez+ApI8dAPXugXpL8Y5rZ igCI0kxHJ/jEu3+6/n/FsFkYKjkDWLI8EaIvbedXfc/nlG8sv1VXz+Hp5rElnE41Wygws lfS23YTcxX/+sUJhvqATZN2JiauVjNdR4RI+zmCPTveuSzdqKdHg+Ze0AROj8YphROgO6 fWPlhyLc+VYDF7hwCv3KRhqj5lDIQJZGmY4z3eOi8EkU/n/6Gydn8PBQqrOFU+aMZ4Q== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796218782; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: Received:From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:Content-Type: X-Originating-IP:X-ClientProxiedBy:X-EOPAttributedMessage: X-MS-PublicTrafficType:X-MS-TrafficTypeDiagnostic: X-MS-Office365-Filtering-Correlation-Id:X-MS-Exchange-SenderADCheck: X-MS-Exchange-AntiSpam-Relay:X-Microsoft-Antispam: X-Microsoft-Antispam-Message-Info:X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=dUmxqW03i8MrUvAvbcWQ0qaKVHR6DcV8Tvq/jfoNaUs=; b=4AwH1dq96Kb2rlHpjrZD42jyT0/1SZy06M2UfQfZVIT2+xuQkv2toQuUxQ/Xkl4g/lTC 7kPymbaHsdOhqafaXN0goTmx+r6MERI2ezs8sISj3p6LGE3CCv2SPQXeUFcsZy6XhFcog gsS4u/lNq32Db/l0ZK7XNN2HTsgglMmVeHbDhp+ZsYiUNsPlOFeHmuzA6//8Oz+rPEnI8 PBYEpoNdTt4Rm5y+8Y8aoLMxiAGZwFfVEwCpUlhgUFXgCNT02+NBtLMi7NUnjZ4jFA33p odCc9OL2VOy8T93ZK1RhRyKDgFZL80GldYVA2U7NZPCkoiCXDdv1AcVqJMZHB4Pui1w== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=DI1IbYJ1; arc=pass header.oldest-pass=0 smtp.remote-ip=40.107.201.0 Received: from CH4PR04CU002.outbound.protection.outlook.com (mail-northcentralusazon11013000.outbound.protection.outlook.com [40.107.201.0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF35D606A31A; Wed, 2 Dec 2026 13:39:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DF35D606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=DI1IbYJ1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=GdUBLOgvA+Zl8bOp8RGKSHFGWuVpc1nPvYihVBDxVsyTHM30o3QzXVS//zT50Wb9+/QkZUzfTt22mMjB4KkFH5XAqi7ZVLTE96fK6AfcdM1G7DSur+COqyJA878M9Y1Nde3d+q5eY9cXDNvb+A2/9pesHIGVRqL0A8hmmosumoigf/TNFeYQINWXP/HbtAw7ZeIZthXUQ9xthDIFP0eFlpMzi9kWU5xOql5D+K5viKXmZv4lIrs5FCj4J1A6SMwrlJRdOYF0Q7IalKsAUIqRZ3rm7rm8RHSUh1m6cCTckIu+QinpxeUkTeRlgpDbzlvskVUztVujkMpjZL7aB4XLcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=dUmxqW03i8MrUvAvbcWQ0qaKVHR6DcV8Tvq/jfoNaUs=; b=pHfo9BYL0qYGxD6S6GvuUAidedWrQa+9P/epKWUUzFDgoCNTZVb29UHi1ik4h1yiEvVKUD5IsIqc9s9g9kIpl5w9933Uy/SQqL93hIVlFybio4w6sQ1f8wjRpnJABVR7sa7YIuNmG/pRj9M16FqZ83SgYlcDnu8HPWyoNBZ0pGcYHhdKJxbUcnR5SK1oWwckpTRw797ZU6n335eRAJVPEtINkUO2/LPbSypBTggZj+Ffq8/NkGG+SCddtEZ8/vglvPWg7ZyLzQnnXaTJO+sIzks68TY1xyTxWJddhLJWrHhtI7QuSoOuS76obi4X+wbYbBUA3+P/q7AYEL+x/6DSMQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=dUmxqW03i8MrUvAvbcWQ0qaKVHR6DcV8Tvq/jfoNaUs=; b=DI1IbYJ1qyZmFs73C1taODQNDHPJvLjvcG4hR6/mnYPFrEj8x0ETIWtDfYMgY1OVgLuJz6+IgnY7kb5vrQVS+32axMmaKsr6uIck0RJwJ+brlPgh803urZ8proAK54yiNKEiiQ4WB0Q09E42NyZ4n0rWQWqFH8UuIhzMM3dkAy0spPWe7NjD9+FtLZ/i5z46g9IMrmr+Le6nRKk6XiSK6vzMs+cmOiuzSUvIUA76ZpbxaaDdnDZY0dYwCVRY8QPH/42sc26Lr0w6CckJtFLx5rydeQEnqQzJMcU3ZJH6f+9vSqOjzQMTG3UkjhLFp5ppRZnYQQxfpc/d4H5UAEqqvw== Received: from CH0PR13CA0045.namprd13.prod.outlook.com (2603:10b6:610:b2::20) by SJ2PR12MB9192.namprd12.prod.outlook.com (2603:10b6:a03:55d::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.10; Wed, 2 Dec 2026 13:39:34 +0000 Received: from BN3PEPF0000B372.namprd21.prod.outlook.com (2603:10b6:610:b2:cafe::36) by CH0PR13CA0045.outlook.office365.com (2603:10b6:610:b2::20) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.709.4 via Frontend Transport; Wed, 2 Dec 2026 13:39:33 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BN3PEPF0000B372.mail.protection.outlook.com (10.167.243.169) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.0 via Frontend Transport; Wed, 2 Dec 2026 13:39:33 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Wed, 2 Dec 2026 05:39:11 -0800 Received: from c-237-113-240-247.mtl.labs.mlnx (10.126.230.37) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Wed, 2 Dec 2026 05:39:06 -0800 From: Cosmin Ratiu To: CC: Jiri Pirko , "David S . Miller" , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Willem de Bruijn , "Simon Horman" , Jonathan Corbet , Shuah Khan , Randy Dunlap , "Tariq Toukan" , Cosmin Ratiu , Carolina Jubran , Dmytro Linkin , Vlad Buslov , Subject: [PATCH net 1/2] devlink: Lock the shared instance directly for cross-device rates Date: Wed, 2 Dec 2026 15:38:37 +0200 Message-ID: <20261202133838.834971-2-cratiu@nvidia.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261202133838.834971-1-cratiu@nvidia.com> References: <20261202133838.834971-1-cratiu@nvidia.com> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: rnnvmail202.nvidia.com (10.129.68.7) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN3PEPF0000B372:EE_|SJ2PR12MB9192:EE_ X-MS-Office365-Filtering-Correlation-Id: f1a5e9dc-0de4-4167-08b7-08df5079b346 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700016|7416014|376014|1800799024|23010399003|6133799003|261009223027099003|18002099003|22082099003|10067099003|56012099006|261102130727099003|261028144727099003|11063799006|261102123227099003|261026121527099003|261021123027099003|261026140227099003|261021121427099003|261021135927099003|261022105027099003; X-Microsoft-Antispam-Message-Info: foY5D+qKuq9/7AKBdDJbhedN4VY6CeRoEhhcqqSdFLvzd1/v3DHSjV9OszjYcaCcBEFaSjz9sl3EkXeU8JspBvpI7TpdnWqLiRt4FSE1c1OL4pNiDchOvmZmxd7AEE+uijCyvw1cAQ5vWaMf5hY6UzNo0wBVl4h/gDIB0cItam/OXEjd4figCX1x4qGLjBgbyQgMWVXwie+euMisvYPPviKw6dCgjmSlqigTzORN6dckM4/FsgVFLHNb21EnKhmPPZpP82w7GTw045tkEm7mN9JrnwUe9eoLaVhHEGVFtJOUjwq06M5h0knk0SVM2hHxyCfdYtnPCz0htDzWFTJHWHGnenF5TPP+T30x5jGM8BqbO0ZsCag5txqqVGaePm6ZamUwjzk42Lj2/KCWfY0xD0D0fQAuVZqoj+WvVnGs4+2p4cI9JuXg0DcDitpxCkjAL1Yrg5wxPmt+PNAZYgA9sSZmwe0tcQnOSm9NL8RjTlGw5aBidYT8hcoiPgUbWbbP/e4cVYGHaWxhMqpSvRj/dAim0ZUM71PFAHCWcIJdkDKJ/PrPNdHA3jrHiW6ZKP6c4dtKGbJMsOFi+aaFVLRVhL11z+fdk4dSMAEKDlteOkUHXaVSNXAEH12GMZUp4naVnnuC3DkWAKAQ1vsGt2tikYTrzvp6o5yRehiSGGBkBdbfoG1IrmBv/yijJfIP2DwWGqFUux5VtUdUM0bYUxAvpA== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(82310400026)(36860700016)(7416014)(376014)(1800799024)(23010399003)(6133799003)(261009223027099003)(18002099003)(22082099003)(10067099003)(56012099006)(261102130727099003)(261028144727099003)(11063799006)(261102123227099003)(261026121527099003)(261021123027099003)(261026140227099003)(261021121427099003)(261021135927099003)(261022105027099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pQgDs3xzIO6JWKS7k6l2VS3iEwFQi8rNeWyHB+oNjnXwwjWjuTlge3XU9sp7tdMk3fHOilCxrIhS9LV+lkzk6rKMgbB30WIsk0K8t/JrkzP61CB81km4I4haVa/ZU5eqXdZl9z++4PPrDAte58SKO3erMVTWMiwbIGyRW70w15GmN2Ge3ixo02hD+FlN/v5aITaBheSS6em0R0SrNIri/qiwe8UDxBMLCes1pgYVdE5DN78Anod44u5D/Rt1WhPIvxT3ZJpRIam5/CPrXZ+xmpMPFcSXKLjIq77dsQ5t4tUzl+s3S/vzos+RX/jxrTf3BqZbkJcSXOMwZOX+jYX/x1z397fOrNDWNDeC0VHTNeLrVMW2NAJvHHTiXjC1b7AF+zVWS/BvW0gC1H1p71F5/JqIZ/rms36jWMGH6wYCIoMnakUr7cMRE3V+ySd5BXC+ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Dec 2026 13:39:33.7100 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f1a5e9dc-0de4-4167-08b7-08df5079b346 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN3PEPF0000B372.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9192 Since the referenced commit, devl_rate_lock() walks up the nested devlink chain, locking each nested-in instance in turn, to find the topmost instance where rates are stored. This deadlocks on mlx5 (where nesting is SF -> PF -> shd), when a SF add/remove concurrently runs with its parent PF unbind/reload: kworker (mlx5_vhca_event0) PF unbind / devlink reload devl_lock(SF) devl_lock(PF) devl_unregister(SF) mlx5_unload() devlink_rates_notify_unregister() mlx5_vhca_event_stop() devl_rate_lock() flush_workqueue() devl_lock(PF) <- blocks <- waits for kworker Intermediate instances are only locked to safely walk the chain, while devlink rates in shd are protected by its instance lock alone. So fix that by removing the need to walk up the chain and avoiding the deadlocks. This is done with: - making the topmost instance explicit as struct devlink::shd. - providing an accessor for it: devlink_shd(). - inheriting shd from the nested-in devlink at register time. - refactoring devl_rate_lock() to use the saved shared instance directly instead of walking up the chain. Besides avoiding the above deadlocks, this fixes a couple of latent issues and makes the devlink infrastructure more robust since the instance storing the rates is now fixed at register time instead of depending on the registration state of the chain of instances between itself and shd: - devlink_free() runs after unregistration unlinked the instance from its nested-in instance, so devlink_rates_check() only checked its own rates, missing any rates stored in shd. It now works correctly. - devl_rate_nodes_destroy() could in theory miss destroying rate nodes if called after unregistration. No in-tree driver does this currently, but it now does the correct thing since the instance storing the rates is now fixed for the lifetime of an instance. Rates created before registration (technically possible with the current API, but not done by any driver) are moved to the shared instance when it is inherited. Without a shared instance, rates are now always stored locally, so cross-device rate parents are only possible among instances belonging to the same shared instance, as originally intended. Export devlink_shd() for drivers and document the locking rule. Fixes: 6bbd1bce3099 ("devlink: Allow rate node parents from other devlinks") Signed-off-by: Cosmin Ratiu --- Documentation/networking/devlink/index.rst | 7 +++ include/net/devlink.h | 7 +-- net/devlink/core.c | 57 ++++++++++++++++------ net/devlink/devl_internal.h | 11 +++-- net/devlink/rate.c | 53 ++++++++++++-------- net/devlink/sh_dev.c | 2 + 6 files changed, 93 insertions(+), 44 deletions(-) diff --git a/Documentation/networking/devlink/index.rst b/Documentation/networking/devlink/index.rst index 03f49389a755..aebfa5c06751 100644 --- a/Documentation/networking/devlink/index.rst +++ b/Documentation/networking/devlink/index.rst @@ -46,6 +46,13 @@ sure to respect following rules: The nested devlink info is exposed to the userspace over object-specific attributes of devlink netlink. +A shared devlink instance (see ``devlink_shd_get()``) is inherited at +register time by all instances nested in it, directly or indirectly, and +can be obtained with ``devlink_shd()``. Devlink core may take the shared +instance lock while holding a nested instance lock, without taking the +locks of intermediate instances. Hence, the shared instance lock must not +be held while waiting for any nested instance to be torn down. + Interface documentation ----------------------- diff --git a/include/net/devlink.h b/include/net/devlink.h index 7abd23376319..a1d02c957219 100644 --- a/include/net/devlink.h +++ b/include/net/devlink.h @@ -1599,10 +1599,10 @@ struct devlink_ops { void *priv_child, void *priv_parent, struct netlink_ext_ack *extack); /* Indicates if cross-device rate nodes are supported. - * This also requires a shared common ancestor object all devices that - * could share rate nodes are nested in. + * This also requires a shared devlink instance (see devlink_shd()) all + * devices that could share rate nodes belong to. * If enabled, rate operations may be called on an instance with only - * the common ancestor lock held and *without that instance lock held*. + * the shared instance lock held and *without that instance lock held*. * It is the driver's responsibility to ensure proper serialization * with other operations. */ @@ -1675,6 +1675,7 @@ struct devlink *devlink_shd_get(const char *id, const struct device_driver *driver); void devlink_shd_put(struct devlink *devlink); void *devlink_shd_get_priv(struct devlink *devlink); +struct devlink *devlink_shd(const struct devlink *devlink); /** * struct devlink_port_ops - Port operations diff --git a/net/devlink/core.c b/net/devlink/core.c index 9b844e0c1bd2..07b409198e41 100644 --- a/net/devlink/core.c +++ b/net/devlink/core.c @@ -67,22 +67,6 @@ static void __devlink_rel_put(struct devlink_rel *rel) devlink_rel_free(rel); } -struct devlink *__must_check devlink_nested_in_get_lock(struct devlink *devlink) -{ - devl_assert_locked(devlink); - if (!devlink->rel) - return NULL; - devlink = devlinks_xa_get(devlink->rel->nested_in.devlink_index); - if (!devlink) - return NULL; - devl_lock(devlink); - if (devl_is_registered(devlink)) - return devlink; - devl_unlock(devlink); - devlink_put(devlink); - return NULL; -} - static void devlink_rel_nested_in_notify_work(struct work_struct *work) { struct devlink_rel *rel = container_of(work, struct devlink_rel, @@ -401,6 +385,44 @@ struct devlink *devlinks_xa_lookup_get(struct net *net, unsigned long index) return __devlinks_xa_find_get(net, &index, index); } +/* Inherit the shared instance from the nested-in instance, if any. */ +static void devlink_shd_inherit(struct devlink *devlink) +{ + struct devlink *parent, *shd; + + if (devlink->shd || !devlink->rel) + return; + + parent = devlinks_xa_get(devlink->rel->nested_in.devlink_index); + if (!parent) + return; + + shd = READ_ONCE(parent->shd); + if (shd) { + refcount_inc(&shd->refcount); + devlink_rates_shd_move(devlink, shd); + WRITE_ONCE(devlink->shd, shd); + } + devlink_put(parent); +} + +/** + * devlink_shd - Get the shared devlink instance a devlink instance belongs to + * @devlink: devlink + * + * A shared devlink instance belongs to itself. Other instances inherit the + * shared instance of the instance they are nested in when registered. + * The returned instance is immutable and valid until devlink_free() of + * @devlink. No reference is taken, so don't pair with devlink_shd_put(). + * + * Return: shared devlink instance or NULL. + */ +struct devlink *devlink_shd(const struct devlink *devlink) +{ + return READ_ONCE(devlink->shd); +} +EXPORT_SYMBOL_GPL(devlink_shd); + /** * devl_register - Register devlink instance * @devlink: devlink @@ -410,6 +432,7 @@ int devl_register(struct devlink *devlink) ASSERT_DEVLINK_NOT_REGISTERED(devlink); devl_assert_locked(devlink); + devlink_shd_inherit(devlink); xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED); devlink_notify_register(devlink); devlink_rel_nested_in_notify(devlink); @@ -544,6 +567,8 @@ void devlink_free(struct devlink *devlink) WARN_ON(devlink_rates_check(devlink, NULL, NULL)); devl_unlock(devlink); devlink_rel_put(devlink); + if (devlink->shd && devlink->shd != devlink) + devlink_put(devlink->shd); WARN_ON(!list_empty(&devlink->trap_policer_list)); WARN_ON(!list_empty(&devlink->trap_group_list)); diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h index 798e707002b9..d909b2858616 100644 --- a/net/devlink/devl_internal.h +++ b/net/devlink/devl_internal.h @@ -62,6 +62,12 @@ struct devlink { struct rcu_work rwork; struct devlink_rel *rel; struct xarray nested_rels; + /* Shared devlink instance this instance belongs to, or NULL. + * Points to self for shared instances. Otherwise, inherited from the + * nested-in instance at register time, if that is already registered. + * Immutable once set and referenced (if != self) until devlink_free(). + */ + struct devlink *shd; char priv[] __aligned(NETDEV_ALIGN); }; @@ -136,10 +142,6 @@ typedef void devlink_rel_notify_cb_t(struct devlink *devlink, u32 obj_index); typedef void devlink_rel_cleanup_cb_t(struct devlink *devlink, u32 obj_index, u32 rel_index); -/* Returns the locked+referenced nested-in instance or NULL. */ -struct devlink *__must_check -devlink_nested_in_get_lock(struct devlink *devlink); - void devlink_rel_nested_in_clear(u32 rel_index); int devlink_rel_nested_in_add(u32 *rel_index, u32 devlink_index, u32 obj_index, devlink_rel_notify_cb_t *notify_cb, @@ -306,6 +308,7 @@ void devlink_traps_notify_register(struct devlink *devlink); void devlink_traps_notify_unregister(struct devlink *devlink); void devlink_rates_notify_register(struct devlink *devlink); void devlink_rates_notify_unregister(struct devlink *devlink); +void devlink_rates_shd_move(struct devlink *devlink, struct devlink *shd); void devlink_linecards_notify_register(struct devlink *devlink); void devlink_linecards_notify_unregister(struct devlink *devlink); diff --git a/net/devlink/rate.c b/net/devlink/rate.c index e727c8b8b33e..ad6838f28092 100644 --- a/net/devlink/rate.c +++ b/net/devlink/rate.c @@ -30,34 +30,27 @@ devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info) return devlink_rate ?: ERR_PTR(-ENODEV); } -/* Repeatedly walks the nested devlink chain while cross device rate nodes are - * supported and finds the topmost instance where rates should be stored. - * That instance is locked, referenced and returned. - * When cross device rate nodes aren't supported the original devlink instance - * is returned. - */ +static bool devlink_rates_cross_dev(const struct devlink *devlink) +{ + return devlink->ops && devlink->ops->supported_cross_device_rate_nodes; +} + +/* Locks and returns the instance where rates of 'devlink' are stored. */ static struct devlink *devl_rate_lock(struct devlink *devlink) { - struct devlink *rate_devlink = devlink, *parent; + struct devlink *shd = devlink_shd(devlink); + struct devlink *rate_devlink; devl_assert_locked(devlink); - while (rate_devlink->ops && - rate_devlink->ops->supported_cross_device_rate_nodes) { - parent = devlink_nested_in_get_lock(rate_devlink); - if (!parent) - break; - if (rate_devlink != devlink) { - /* Unlock intermediate instances. */ - devl_unlock(rate_devlink); - devlink_put(rate_devlink); - } - rate_devlink = parent; - } + rate_devlink = shd && devlink_rates_cross_dev(devlink) ? shd : devlink; + + if (rate_devlink != devlink) + devl_lock(rate_devlink); return rate_devlink; } -/* Unlocks and puts 'rate devlink' if different than 'devlink'. */ +/* Unlocks 'rate devlink' if different than 'devlink'. */ static void devl_rate_unlock(struct devlink *devlink, struct devlink *rate_devlink) { @@ -65,7 +58,6 @@ static void devl_rate_unlock(struct devlink *devlink, return; devl_unlock(rate_devlink); - devlink_put(rate_devlink); } static struct devlink_rate * @@ -264,6 +256,25 @@ void devlink_rates_notify_register(struct devlink *devlink) devl_rate_unlock(devlink, rate_devlink); } +/* Called at register time, before 'devlink' starts belonging to 'shd'. + * Rates created before register are stored locally. When cross device rate + * nodes are supported, they must be moved to the shared instance, where they + * will be looked up from now on. They can only have parents among themselves, + * so the hierarchy stays intact. + */ +void devlink_rates_shd_move(struct devlink *devlink, struct devlink *shd) +{ + devl_assert_locked(devlink); + + if (!devlink_rates_cross_dev(devlink) || + list_empty(&devlink->rate_list)) + return; + + devl_lock(shd); + list_splice_tail_init(&devlink->rate_list, &shd->rate_list); + devl_unlock(shd); +} + void devlink_rates_notify_unregister(struct devlink *devlink) { struct devlink_rate *rate_node; diff --git a/net/devlink/sh_dev.c b/net/devlink/sh_dev.c index 85acce97e788..d4e376eca1f3 100644 --- a/net/devlink/sh_dev.c +++ b/net/devlink/sh_dev.c @@ -50,6 +50,8 @@ static struct devlink_shd *devlink_shd_create(const char *id, goto err_devlink_free; shd->priv_size = priv_size; refcount_set(&shd->refcount, 1); + /* Shared instances belong to themselves. */ + WRITE_ONCE(devlink->shd, devlink); devl_lock(devlink); devl_register(devlink); -- 2.53.0