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