devlink and mlx5: Support cross-function rate scheduling
Subsystems (8)
Tariq Toukan
maintainerv9 cover devlink and mlx5: Support cross-function rate scheduling
Hi,
This series by Cosmin adds support for cross-function rate scheduling in devlink and mlx5.
See detailed explanation by Cosmin below [0].
Regards,
Tariq
[0]
devlink objects support rate management for TX scheduling, which involves maintaining a tree of rate nodes that corresponds to TX schedulers in hardware. 'man devlink-rate' has the full details.
The tree of rate nodes is maintained per devlink object, protected by the devlink lock.
There exists hardware capable of instantiating TX scheduling trees spanning multiple functions of the same physical device (and thus devlink objects) and therefore the current API and locking scheme is insufficient.
This patch series changes the devlink rate implementation and API to allow supporting such hardware and managing TX scheduling trees across multiple functions of a physical device.
Modeling this requires having devlink rate nodes with parents in other devlink objects. A naive approach that relies on the current one-lock-per-devlink model is impossible, as it would require in some cases acquiring multiple devlink locks in the correct order.
The solution proposed in this patch series makes use of the recently introduced shared devlink instance [1] to manage rate hierarchy changes across multiple functions.
V1 of this patch series was sent a long time ago [2], using a different approach of storing rates in a shared rate domain with special locking rules. This new approach uses standard devlink instances and nesting.
The first part of the series adds support to devlink rates for maintaining the rate tree across multiple functions.
The second part changes the mlx5 implementation to make use of this (and cleans up remnants of the previous approach, involving rate domains).
The neat part about using the shared devlink object is that it works for SFs as well, which are already nested in their parent PF instances. So with this series, complex scheduling trees spanning multiple SFs across multiple PFs of the same NIC can now be supported.
Patches:
devlink rate changes for cross-device TX scheduling:
devlink: Add helpers to lock nested-in instances
devlink: Migrate from info->user_ptr to info->ctx
devlink: Decouple rate storage from associated devlink object
devlink: Add parent dev to devlink API
devlink: Allow parent dev for rate-set and rate-new
devlink: Allow rate node parents from other devlinks
mlx5 support for cross-device TX scheduling:
net/mlx5: qos: Use mlx5_lag_query_bond_speed to query LAG speed net/mlx5: qos: Expose a function to clear a vport's parent net/mlx5: qos: Model the root node in the scheduling hierarchy net/mlx5: qos: Remove qos domains and use shd lock net/mlx5: qos: Support cross-device tx scheduling
selftests: drv-net: Add test for cross-esw rate scheduling net/mlx5: Document devlink rates
[1] https://lore.kernel.org/all/[email protected]/T/#u
[2] https://lore.kernel.org/netdev/[email protected]/
V9:
- Link to V8:
https://lore.kernel.org/all/[email protected]/
- Updated netlink_gen.c policy related to index, missed during a rebase.
- Fixed off-by-one error in devlink_get_parent_from_attrs_lock.
- Fixed crash in qos.c when setting a parent to NULL.
Cosmin Ratiu (14):
devlink: Update nested instance locking comment
devlink: Add helpers to lock nested-in instances
devlink: Migrate from info->user_ptr to info->ctx
devlink: Decouple rate storage from associated devlink object
devlink: Add parent dev to devlink API
devlink: Allow parent dev for rate-set and rate-new
devlink: Allow rate node parents from other devlinks net/mlx5: qos: Use mlx5_lag_query_bond_speed to query LAG speed net/mlx5: qos: Expose a function to clear a vport's parent net/mlx5: qos: Model the root node in the scheduling hierarchy net/mlx5: qos: Remove qos domains and use shd lock net/mlx5: qos: Support cross-device tx scheduling
selftests: drv-net: Add test for cross-esw rate scheduling net/mlx5: Document devlink rates
Documentation/netlink/specs/devlink.yaml | 24 +- .../networking/devlink/devlink-port.rst | 2 + Documentation/networking/devlink/index.rst | 4 +- Documentation/networking/devlink/mlx5.rst | 33 ++ .../net/ethernet/mellanox/mlx5/core/devlink.c | 1 + .../mellanox/mlx5/core/esw/devlink_port.c | 2 +- .../net/ethernet/mellanox/mlx5/core/esw/qos.c | 532 +++++++----------- .../net/ethernet/mellanox/mlx5/core/esw/qos.h | 3 - .../net/ethernet/mellanox/mlx5/core/eswitch.c | 8 - .../net/ethernet/mellanox/mlx5/core/eswitch.h | 15 +- include/net/devlink.h | 5 + include/uapi/linux/devlink.h | 2 + net/devlink/core.c | 42 ++ net/devlink/dev.c | 16 +- net/devlink/devl_internal.h | 19 + net/devlink/dpipe.c | 14 +- net/devlink/health.c | 12 +- net/devlink/linecard.c | 4 +- net/devlink/netlink.c | 82 ++- net/devlink/netlink_gen.c | 24 +- net/devlink/netlink_gen.h | 8 + net/devlink/param.c | 4 +- net/devlink/port.c | 18 +- net/devlink/rate.c | 290 ++++++++-- net/devlink/region.c | 6 +- net/devlink/resource.c | 6 +- net/devlink/sb.c | 22 +- net/devlink/trap.c | 12 +- .../testing/selftests/drivers/net/hw/Makefile | 1 + .../drivers/net/hw/devlink_rate_cross_esw.py | 300 ++++++++++ 30 files changed, 1032 insertions(+), 479 deletions(-) create mode 100755 tools/testing/selftests/drivers/net/hw/devlink_rate_cross_esw.py
base-commit: b50a48b65cb0c955728894b24ba5ecc3cd4b8c7d
Signature · 1 line
2.44.0
Tariq Toukan
maintainerPATCH v9 1/14 devlink: Update nested instance locking comment
From: Cosmin Ratiu <[email protected]>
In commit [1] a comment about nested instance locking was updated. But there's another place where this is mentioned, so update that as well.
[1] commit 0061b5199d7c ("devlink: Reverse locking order for nested instances")
--- Documentation/networking/devlink/index.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/networking/devlink/index.rst +2 -2
@@ -31,8 +31,8 @@ sure to respect following rules: - Lock ordering should be maintained. If driver needs to take instance lock of both nested and parent instances at the same time, devlink - instance lock of the parent instance should be taken first, only then - instance lock of the nested instance could be taken. + instance lock of the nested instance should be taken first, only then + instance lock of the parent instance could be taken. - Driver should use object-specific helpers to setup the nested relationship:
Signature · 1 line
2.44.0
Tariq Toukan
maintainerPATCH v9 2/14 devlink: Add helpers to lock nested-in instances
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
Upcoming code will need to obtain a reference to locked nested-in devlink instances. Add helpers to lock, obtain an already locked reference and unlock/unref the nested-in instance.
--- net/devlink/core.c | 42 +++++++++++++++++++++++++++++++++++++ net/devlink/devl_internal.h | 3 +++ 2 files changed, 45 insertions(+)
net/devlink/core.c +42 -0
@@ -67,6 +67,48 @@ static void __devlink_rel_put(struct devlink_rel *rel)devlink_rel_free (rel); } +struct devlink *devlink_nested_in_get_lock (struct devlink_rel *rel) +{ +struct devlink *devlink; + +if (!rel) +return NULL ; + devlink =devlinks_xa_get (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 ; +} + +/* Returns the nested in devlink object and validates its lock is held. */ +struct devlink *devlink_nested_in_get_locked (struct devlink_rel *rel) +{ +struct devlink *devlink; +unsigned long index; + +if (!rel) +return NULL ; + index = rel->nested_in.devlink_index; + devlink =xa_find (&devlinks, &index, index,DEVLINK_REGISTERED ); +if (devlink) +devl_assert_locked (devlink); +return devlink; +} + +void devlink_nested_in_put_unlock (struct devlink_rel *rel) +{ +struct devlink *devlink =devlink_nested_in_get_locked (rel); + +if (devlink) { +devl_unlock (devlink); +devlink_put (devlink); + } +} +static void devlink_rel_nested_in_notify_work (struct work_struct *work) { struct devlink_rel *rel = container_of(work, struct devlink_rel,
net/devlink/devl_internal.h +3 -0
@@ -136,6 +136,9 @@ 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); +struct devlink *devlink_nested_in_get_lock (struct devlink_rel *rel); +struct devlink *devlink_nested_in_get_locked (struct devlink_rel *rel); +void devlink_nested_in_put_unlock (struct devlink_rel *rel);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,
Signature · 1 line
2.44.0
Jakub Kicinski
maintainerRe: [PATCH net-next V9 02/14] devlink: Add helpers to lock nested-in instances
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
devlink: Add helpers to lock nested-in instances
Upcoming code will need to obtain a reference to locked nested-in devlink instances. Add helpers to lock, obtain an already locked reference and unlock/unref the nested-in instance.
Quoted 4 lines
net/devlink/core.c +0
-0
[ ... ]
net/devlink/core.c static void __devlink_rel_put(struct devlink_rel *rel) · line 98
Quoted 12 lines
net/devlink/core.c +12
-0+/* Returns the nested in devlink object and validates its lock is held. */ +struct devlink *devlink_nested_in_get_locked (struct devlink_rel *rel) +{ +struct devlink *devlink; +unsigned long index; + +if (!rel) +return NULL ; + index = rel->nested_in.devlink_index; + devlink =xa_find (&devlinks, &index, index,DEVLINK_REGISTERED ); +if (devlink) +devl_assert_locked (devlink);
Can this dereference a freed pointer if the lock is not actually held?
xa_find() acquires and releases the RCU read lock internally. When it returns, the devlink pointer is no longer protected by RCU or a reference count.
If a caller invokes this helper without holding the devlink lock (which is the exact scenario the assertion is designed to detect), another thread could concurrently unregister and free the devlink.
If the RCU grace period expires before the assertion executes, devl_assert_locked() would dereference freed memory. Should the lookup and assertion be enclosed within an explicit rcu_read_lock() block to safely assert the lock?
net/devlink/core.c static void __devlink_rel_put(struct devlink_rel *rel) · line 104
Quoted 6 lines
net/devlink/core.c +6
-0+return devlink; +} + +void devlink_nested_in_put_unlock (struct devlink_rel *rel) +{ +struct devlink *devlink =devlink_nested_in_get_locked (rel);
Could this corrupt the mutex state and cause a refcount underflow?
This helper takes a devlink_rel pointer instead of the devlink pointer acquired by devlink_nested_in_get_lock(), and performs a secondary global lookup to find the devlink.
If a caller mistakenly calls this in an error cleanup path where they did not actually acquire the lock, the global xa_find() will still locate the registered devlink. This would execute devl_unlock() and devlink_put() on a devlink the current thread does not own.
Would it be safer for unlock/put helpers to take the exact pointer returned by the lock/get helper to ensure safe resource cleanup?
Quoted 6 lines
net/devlink/core.c +6
-0+ +if (devlink) { +devl_unlock (devlink); +devlink_put (devlink); + } +}
Signature · 1 line
pw-bot: cr
Re: [PATCH net-next V9 02/14] devlink: Add helpers to lock nested-in instances
Quoted 46 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
devlink: Add helpers to lock nested-in instancesUpcoming code will need to obtain a reference to locked nested-in devlink instances. Add helpers to lock, obtain an already locked reference and unlock/unref the nested-in instance.
Quoted 4 lines
net/devlink/core.c +0
-0[ ... ]
Quoted 13 lines
+/* Returns the nested in devlink object and validates its lock is held. */
+struct devlink *devlink_nested_in_get_locked(structdevlink_rel*rel)net/devlink/core.c +8
-0+{ +struct devlink *devlink; +unsigned long index; + +if (!rel) +return NULL ; + index = rel->nested_in.devlink_index; + devlink = xa_find(&devlinks, &index, index,
DEVLINK_REGISTERED);net/devlink/core.c static void __devlink_rel_put(struct devlink_rel *rel) · line 98
Quoted 2 lines
net/devlink/core.c +2
-0+if (devlink) +devl_assert_locked (devlink);Can this dereference a freed pointer if the lock is not actually held?
xa_find()acquires and releases the RCU read lock internally. When it returns,
the devlink pointer is no longer protected by RCU or a reference count.If a caller invokes this helper without holding the devlink lock (which is the
exact scenario the assertion is designed to detect), another thread could
concurrently unregister and free the devlink.
Quoted 6 lines
If the RCU grace period expires before the assertion executes,
devl_assert_locked()would dereference freed memory. Should the lookup and
assertion be enclosed within an explicitrcu_read_lock()block to safely
assert the lock?
This function is meant to be invoked by callers already holding all locks from a given instance up the nesting tree. Calling it outside this context could indeed lead to a race as described, where another entity unregisters a devlink about-to-be-asserted on.
All current callers hold the locks, but to make it more robust, I will add the rcu_read_lock/unlock section around the find+assert.
Quoted 24 lines
Quoted 8 lines
net/devlink/core.c +6
-0+return devlink; +} + +void devlink_nested_in_put_unlock (struct devlink_rel *rel) +{ + struct devlink *devlink =
devlink_nested_in_get_locked(rel);Could this corrupt the mutex state and cause a refcount underflow?
This helper takes a
devlink_relpointer instead of the devlink pointer
acquired bydevlink_nested_in_get_lock(), and performs a secondary global
lookup to find the devlink.If a caller mistakenly calls this in an error cleanup path where they did not
actually acquire the lock, the globalxa_find()will still locate the registered devlink. This would executedevl_unlock()anddevlink_put()on a
devlink the current thread does not own.
Quoted 3 lines
Would it be safer for unlock/put helpers to take the exact pointer returned by
the lock/get helper to ensure safe resource cleanup?
2 issues here:
1) Mistakenly calling this without having acquired the lock. This is akin to saying mutex_unlock is dangerous if the lock isn't held. Technically true, but moot.
2) The rel argument: It is intentional, so that all 3 functions are symmetrical.
Quoted 7 lines
Quoted 7 lines
net/devlink/core.c +6
-0+ +if (devlink) { +devl_unlock (devlink); +devlink_put (devlink); + } +}
Re: [PATCH net-next V9 02/14] devlink: Add helpers to lock nested-in instances
Quoted 54 lines · Cosmin Ratiu
Quoted 46 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
devlink: Add helpers to lock nested-in instancesUpcoming code will need to obtain a reference to locked nested-in devlink instances. Add helpers to lock, obtain an already locked reference and unlock/unref the nested-in instance.
Quoted 4 lines
net/devlink/core.c +0
-0[ ... ]
Quoted 13 lines
+/* Returns the nested in devlink object and validates its lock is held. */
+struct devlink *devlink_nested_in_get_locked(structdevlink_rel*rel)net/devlink/core.c +8
-0+{ +struct devlink *devlink; +unsigned long index; + +if (!rel) +return NULL ; + index = rel->nested_in.devlink_index; + devlink = xa_find(&devlinks, &index, index,
DEVLINK_REGISTERED);net/devlink/core.c static void __devlink_rel_put(struct devlink_rel *rel) · line 98
Quoted 2 lines
net/devlink/core.c +2
-0+if (devlink) +devl_assert_locked (devlink);Can this dereference a freed pointer if the lock is not actually held?
xa_find()acquires and releases the RCU read lock internally. When it returns,
the devlink pointer is no longer protected by RCU or a reference count.If a caller invokes this helper without holding the devlink lock (which is the
exact scenario the assertion is designed to detect), another thread could
concurrently unregister and free the devlink.Quoted 6 lines
If the RCU grace period expires before the assertion executes,
devl_assert_locked()would dereference freed memory. Should the lookup and
assertion be enclosed within an explicitrcu_read_lock()block to safely
assert the lock?
Quoted 5 lines
This function is meant to be invoked by callers already holding all locks from a given instance up the nesting tree. Calling it outside this context could indeed lead to a race as described, where another entity unregisters a devlink about-to-be-asserted on.
Hmm. I'm struggling to follow this. If you already expect the parent to hold the nested devlink's lock, it must have a pointer to this devlink instance. In that case, why would you even need devlink_nested_in_get_locked in the first place?
Quoted 3 lines
All current callers hold the locks, but to make it more robust, I will add the rcu_read_lock/unlock section around the find+assert.
You should at least do that. It is the correct pattern when dealing with reference counting data structures from the xarray. That's already how devlinks_xa_get() is implemented.
Quoted 17 lines
Quoted 17 lines
Quoted 8 lines
net/devlink/core.c +6
-0+return devlink; +} + +void devlink_nested_in_put_unlock (struct devlink_rel *rel) +{ + struct devlink *devlink =
devlink_nested_in_get_locked(rel);Could this corrupt the mutex state and cause a refcount underflow?
This helper takes a
devlink_relpointer instead of the devlink pointer
acquired bydevlink_nested_in_get_lock(), and performs a secondary global
lookup to find the devlink.
Quoted 7 lines
Quoted 7 lines
If a caller mistakenly calls this in an error cleanup path where they did not
actually acquire the lock, the globalxa_find()will still locate the registered devlink. This would executedevl_unlock()anddevlink_put()on a
devlink the current thread does not own.
If the caller already held the lock, why is devlink_nested_in_put_unlock calling the devl_unlock instead of the caller anyways? That seems confusing. Wouldn't the normal pattern be to devlink_nested_in_get_lock()? Oh, that is a separate function. Ok I see.
Quoted 11 lines
Quoted 3 lines
Would it be safer for unlock/put helpers to take the exact pointer returned by
the lock/get helper to ensure safe resource cleanup?2 issues here:
1) Mistakenly calling this without having acquired the lock. This is akin to sayingmutex_unlockis dangerous if the lock isn't held. Technically true, but moot.
2) The rel argument: It is intentional, so that all 3 functions are symmetrical.
IMO it would make more sense for the put version to be a put on the returned devlink pointer. I guess its not symmetrical, but it removes the need to perform the second lookup and it makes it easier to reason about the pointer you're releasing being the same one.
Having put take different arguments from get is the usual pattern for such a behavior.
Also devlink_nested_in_get_locked() doesn't increase the ref count so it is sort of "relying" on the caller already having a reference to it, which makes me think its not very useful. The only valid way to call this function as it exists now safely is to already hold a reference to the object, which also already requires you to have a valid pointer making me wonder why you'd ever need to call it in the first place.
The only example you have is to make devlink_nested_in_put_unlock() take a devlink_rel pointer as its argument instead of just calling it on the pointer returned by devlink_nested_in_get_lock().
This implementation seems confusing and likely to lead to errors.
Thanks,
Jake
Re: [PATCH net-next V9 02/14] devlink: Add helpers to lock nested-in instances
Quoted 67 lines · Jacob Keller
Quoted 59 lines · Cosmin Ratiu
Quoted 51 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
---
devlink: Add helpers to lock nested-in instancesUpcoming code will need to obtain a reference to locked nested-in devlink instances. Add helpers to lock, obtain an already locked reference and unlock/unref the nested-in instance.
Quoted 4 lines
net/devlink/core.c +0
-0[ ... ]
Quoted 15 lines
+/* Returns the nested in devlink object and validates its lock is
held. */
+struct devlink *devlink_nested_in_get_locked(structdevlink_rel
*rel)net/devlink/core.c +8
-0+{ +struct devlink *devlink; +unsigned long index; + +if (!rel) +return NULL ; + index = rel->nested_in.devlink_index; + devlink = xa_find(&devlinks, &index, index,
DEVLINK_REGISTERED);net/devlink/core.c static void __devlink_rel_put(struct devlink_rel *rel) · line 98
Quoted 2 lines
net/devlink/core.c +2
-0+if (devlink) +devl_assert_locked (devlink);Can this dereference a freed pointer if the lock is not actually held?
xa_find()acquires and releases the RCU read lock internally. When it
returns,
the devlink pointer is no longer protected by RCU or a reference count.If a caller invokes this helper without holding the devlink lock (which is the
exact scenario the assertion is designed to detect), another thread
could
concurrently unregister and free the devlink.Quoted 6 lines
If the RCU grace period expires before the assertion executes,
devl_assert_locked()would dereference freed memory. Should the lookup and
assertion be enclosed within an explicitrcu_read_lock()block to safely
assert the lock?Quoted 6 lines
This function is meant to be invoked by callers already holding all locks from a given instance up the nesting tree. Calling it outside this context could indeed lead to a race as described, where another
entity unregisters a devlink about-to-be-asserted on.
Quoted 6 lines
Hmm. I'm struggling to follow this. If you already expect the parent to
hold the nested devlink's lock, it must have a pointer to this devlink
instance. In that case, why would you even needdevlink_nested_in_get_lockedin the first place?
After some more intense staring, I realized that intermediate instances don't actually need to be locked, only the ancestor needs to. With that in mind, the code get simplified:
- devlink_nested_in_get_locked and devlink_nested_in_put_unlock can be removed.
- recursive unlocking in devl_rate_unlock is gone.
Quoted 80 lines
Quoted 5 lines
All current callers hold the locks, but to make it more robust, I will
add the rcu_read_lock/unlock section around the find+assert.You should at least do that. It is the correct pattern when dealing with
reference counting data structures from the xarray. That's already howdevlinks_xa_get()is implemented.Quoted 19 lines
Quoted 19 lines
Quoted 8 lines
net/devlink/core.c +6
-0+return devlink; +} + +void devlink_nested_in_put_unlock (struct devlink_rel *rel) +{ + struct devlink *devlink =
devlink_nested_in_get_locked(rel);Could this corrupt the mutex state and cause a refcount underflow?
This helper takes a
devlink_relpointer instead of the devlink pointer
acquired bydevlink_nested_in_get_lock(), and performs a secondary
global
lookup to find the devlink.Quoted 9 lines
Quoted 9 lines
If a caller mistakenly calls this in an error cleanup path where they
did not
actually acquire the lock, the globalxa_find()will still locate the
registered devlink. This would executedevl_unlock()anddevlink_put()on a
devlink the current thread does not own.If the caller already held the lock, why is
devlink_nested_in_put_unlock
calling thedevl_unlockinstead of the caller anyways? That seems confusing. Wouldn't the normal pattern be todevlink_nested_in_get_lock()? Oh, that is a separate function. Ok I see.Quoted 5 lines
Quoted 4 lines
Would it be safer for unlock/put helpers to take the exact pointer
returned by
the lock/get helper to ensure safe resource cleanup?Quoted 8 lines
2 issues here:
1) Mistakenly calling this without having acquired the lock. This is
akin to sayingmutex_unlockis dangerous if the lock isn't held. Technically true, but moot.
2) The rel argument: It is intentional, so that all 3 functions are symmetrical.IMO it would make more sense for the put version to be a put on the returned devlink pointer. I guess its not symmetrical, but it removes the need to perform the second lookup and it makes it easier to reason
about the pointer you're releasing being the same one.Having put take different arguments from get is the usual pattern for such a behavior.
Also
devlink_nested_in_get_locked()doesn't increase the ref count so it
is sort of "relying" on the caller already having a reference to it, which makes me think its not very useful. The only valid way to call this function as it exists now safely is to already hold a reference to
the object, which also already requires you to have a valid pointer making me wonder why you'd ever need to call it in the first place.
Quoted 7 lines
The only example you have is to make
devlink_nested_in_put_unlock()take
adevlink_relpointer as its argument instead of just calling it on the
pointer returned bydevlink_nested_in_get_lock().This implementation seems confusing and likely to lead to errors.
I hope the next version will be more suitable.
Thank you for the comments and suggestions.
Quoted 3 lines
Thanks,
Jake
Re: [PATCH net-next V9 02/14] devlink: Add helpers to lock nested-in instances
Quoted 75 lines · Cosmin Ratiu
Quoted 67 lines · Jacob Keller
Quoted 59 lines · Cosmin Ratiu
Quoted 51 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
---
devlink: Add helpers to lock nested-in instancesUpcoming code will need to obtain a reference to locked nested-in devlink instances. Add helpers to lock, obtain an already locked reference and unlock/unref the nested-in instance.
Quoted 4 lines
net/devlink/core.c +0
-0[ ... ]
Quoted 15 lines
+/* Returns the nested in devlink object and validates its lock is
held. */
+struct devlink *devlink_nested_in_get_locked(structdevlink_rel
*rel)net/devlink/core.c +8
-0+{ +struct devlink *devlink; +unsigned long index; + +if (!rel) +return NULL ; + index = rel->nested_in.devlink_index; + devlink = xa_find(&devlinks, &index, index,
DEVLINK_REGISTERED);net/devlink/core.c static void __devlink_rel_put(struct devlink_rel *rel) · line 98
Quoted 2 lines
net/devlink/core.c +2
-0+if (devlink) +devl_assert_locked (devlink);Can this dereference a freed pointer if the lock is not actually held?
xa_find()acquires and releases the RCU read lock internally. When it
returns,
the devlink pointer is no longer protected by RCU or a reference count.If a caller invokes this helper without holding the devlink lock (which is the
exact scenario the assertion is designed to detect), another thread
could
concurrently unregister and free the devlink.Quoted 6 lines
If the RCU grace period expires before the assertion executes,
devl_assert_locked()would dereference freed memory. Should the lookup and
assertion be enclosed within an explicitrcu_read_lock()block to safely
assert the lock?Quoted 6 lines
This function is meant to be invoked by callers already holding all locks from a given instance up the nesting tree. Calling it outside this context could indeed lead to a race as described, where another
entity unregisters a devlink about-to-be-asserted on.Quoted 6 lines
Hmm. I'm struggling to follow this. If you already expect the parent to
hold the nested devlink's lock, it must have a pointer to this devlink
instance. In that case, why would you even needdevlink_nested_in_get_lockedin the first place?
Quoted 7 lines
After some more intense staring, I realized that intermediate instances don't actually need to be locked, only the ancestor needs to. With that in mind, the code get simplified:
-devlink_nested_in_get_lockedanddevlink_nested_in_put_unlockcan be removed.
- recursive unlocking indevl_rate_unlockis gone.
That seems like it would be better, as long as we can prove correctness of every access. Thanks!
-Jake
Tariq Toukan
maintainerPATCH v9 3/14 devlink: Migrate from info->user_ptr to info->ctx
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
Replace deprecated info->user_ptr[0]/[1] with a typed devlink_nl_ctx struct stored in info->ctx. The struct aliases the same union memory, so the migration is safe.
There are no functionality changes here.
--- net/devlink/dev.c | 16 ++++++++-------- net/devlink/devl_internal.h | 13 +++++++++++++ net/devlink/dpipe.c | 14 +++++++------- net/devlink/health.c | 12 ++++++------ net/devlink/linecard.c | 4 ++-- net/devlink/netlink.c | 8 ++++---- net/devlink/param.c | 4 ++-- net/devlink/port.c | 18 +++++++++--------- net/devlink/rate.c | 8 ++++---- net/devlink/region.c | 6 +++--- net/devlink/resource.c | 6 +++--- net/devlink/sb.c | 22 +++++++++++----------- net/devlink/trap.c | 12 ++++++------ 13 files changed, 78 insertions(+), 65 deletions(-)
net/devlink/dev.c +8 -8
@@ -222,7 +222,7 @@ static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)int devlink_nl_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct sk_buff *msg;int err;
@@ -519,7 +519,7 @@ devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_perint devlink_nl_reload_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;enum devlink_reload_action action;enum devlink_reload_limit limit;struct net *dest_net =NULL ;
@@ -683,7 +683,7 @@ static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,int devlink_nl_eswitch_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct sk_buff *msg;int err;
@@ -704,7 +704,7 @@ int devlink_nl_eswitch_get_doit(struct sk_buff *skb, struct genl_info *info)int devlink_nl_eswitch_set_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;const struct devlink_ops *ops = devlink->ops;enum devlink_eswitch_encap_mode encap_mode;u8 inline_mode;
@@ -906,7 +906,7 @@ devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,int devlink_nl_info_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct sk_buff *msg;int err;
@@ -1134,7 +1134,7 @@ int devlink_nl_flash_update_doit(struct sk_buff *skb, struct genl_info *info) {struct nlattr *nla_overwrite_mask, *nla_file_name;struct devlink_flash_update_params params = {}; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;const char *file_name;u32 supported_params;int ret;
@@ -1302,7 +1302,7 @@ devlink_nl_selftests_fill(struct sk_buff *msg, struct devlink *devlink,int devlink_nl_selftests_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct sk_buff *msg;int err;
@@ -1372,7 +1372,7 @@ static const struct nla_policy devlink_selftest_nl_policy[DEVLINK_ATTR_SELFTEST_int devlink_nl_selftests_run_doit (struct sk_buff *skb,struct genl_info *info) {struct nlattr *tb[DEVLINK_ATTR_SELFTEST_ID_MAX +1 ]; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct nlattr *attrs, *selftests;struct sk_buff *msg;void *hdr;
net/devlink/devl_internal.h +13 -0
@@ -150,6 +150,19 @@ int devlink_rel_devlink_handle_put(struct sk_buff *msg, struct devlink *devlink, bool *msg_updated);/* Netlink */ +struct devlink_nl_ctx { +struct devlink *devlink; +struct devlink_port *devlink_port; +}; + +static inline struct devlink_nl_ctx * +devlink_nl_ctx (struct genl_info *info) +{ +BUILD_BUG_ON (sizeof (struct devlink_nl_ctx ) > + sizeof_field(struct genl_info, ctx)); +return (struct devlink_nl_ctx *)info->ctx; +} +enum devlink_multicast_groups {DEVLINK_MCGRP_CONFIG , };
net/devlink/dpipe.c +7 -7
@@ -213,7 +213,7 @@ static int devlink_dpipe_tables_fill(struct genl_info *info,struct list_head *dpipe_tables, const char *table_name) { - struct devlink *devlink = info->user_ptr[0]; + struct devlink *devlink = devlink_nl_ctx(info)->devlink;struct devlink_dpipe_table *table;struct nlattr *tables_attr;struct sk_buff *skb =NULL ;
@@ -290,7 +290,7 @@ static int devlink_dpipe_tables_fill(struct genl_info *info,int devlink_nl_dpipe_table_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;const char *table_name =NULL ;if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME ])
@@ -478,7 +478,7 @@ int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)if (!dump_ctx->hdr) goto nla_put_failure; - devlink = dump_ctx->info->user_ptr[0 ]; + devlink =devlink_nl_ctx (dump_ctx->info)->devlink;if (devlink_nl_put_handle (dump_ctx->skb, devlink)) goto nla_put_failure; dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
@@ -563,7 +563,7 @@ static int devlink_dpipe_entries_fill(struct genl_info *info,int devlink_nl_dpipe_entries_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_dpipe_table *table;const char *table_name;
@@ -650,7 +650,7 @@ static int devlink_dpipe_headers_fill(struct genl_info *info,struct devlink_dpipe_headers * dpipe_headers) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct nlattr *headers_attr;struct sk_buff *skb =NULL ;struct nlmsghdr *nlh;
@@ -713,7 +713,7 @@ static int devlink_dpipe_headers_fill(struct genl_info *info,int devlink_nl_dpipe_headers_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;if (!devlink->dpipe_headers)return -EOPNOTSUPP ;
@@ -747,7 +747,7 @@ static int devlink_dpipe_table_counters_set(struct devlink *devlink,int devlink_nl_dpipe_table_counters_set_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;const char *table_name;bool counters_enable;
net/devlink/health.c +6 -6
@@ -358,7 +358,7 @@ devlink_health_reporter_get_from_info(struct devlink *devlink,int devlink_nl_health_reporter_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_health_reporter *reporter;struct sk_buff *msg;int err;
@@ -456,7 +456,7 @@ int devlink_nl_health_reporter_get_dumpit(struct sk_buff *skb,int devlink_nl_health_reporter_set_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_health_reporter *reporter; reporter =devlink_health_reporter_get_from_info (devlink, info);
@@ -715,7 +715,7 @@ EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);int devlink_nl_health_reporter_recover_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_health_reporter *reporter; reporter =devlink_health_reporter_get_from_info (devlink, info);
@@ -1157,7 +1157,7 @@ static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,int devlink_nl_health_reporter_diagnose_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_health_reporter *reporter;struct devlink_fmsg *fmsg;int err;
@@ -1252,7 +1252,7 @@ int devlink_nl_health_reporter_dump_get_dumpit(struct sk_buff *skb,int devlink_nl_health_reporter_dump_clear_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_health_reporter *reporter; reporter =devlink_health_reporter_get_from_info (devlink, info);
@@ -1269,7 +1269,7 @@ int devlink_nl_health_reporter_dump_clear_doit(struct sk_buff *skb,int devlink_nl_health_reporter_test_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_health_reporter *reporter; reporter =devlink_health_reporter_get_from_info (devlink, info);
net/devlink/linecard.c +2 -2
@@ -171,7 +171,7 @@ void devlink_linecards_notify_unregister(struct devlink *devlink)int devlink_nl_linecard_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_linecard *linecard;struct sk_buff *msg;int err;
@@ -371,7 +371,7 @@ static int devlink_linecard_type_unset(struct devlink_linecard *linecard,int devlink_nl_linecard_set_doit (struct sk_buff *skb,struct genl_info *info) {struct netlink_ext_ack *extack = info->extack; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_linecard *linecard;int err;
net/devlink/netlink.c +4 -4
@@ -252,18 +252,18 @@ static int __devlink_nl_pre_doit(struct sk_buff *skb, struct genl_info *info,if (IS_ERR (devlink))return PTR_ERR (devlink); - info->user_ptr[0 ] = devlink; +devlink_nl_ctx (info)->devlink = devlink;if (flags &DEVLINK_NL_FLAG_NEED_PORT ) { devlink_port =devlink_port_get_from_info (devlink, info);if (IS_ERR (devlink_port)) { err =PTR_ERR (devlink_port); goto unlock; } - info->user_ptr[1 ] = devlink_port; +devlink_nl_ctx (info)->devlink_port = devlink_port; }else if (flags &DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT ) { devlink_port =devlink_port_get_from_info (devlink, info);if (!IS_ERR (devlink_port)) - info->user_ptr[1 ] = devlink_port; +devlink_nl_ctx (info)->devlink_port = devlink_port; }return 0 ;
@@ -304,7 +304,7 @@ static void __devlink_nl_post_doit(struct sk_buff *skb, struct genl_info *info,bool dev_lock = flags &DEVLINK_NL_FLAG_NEED_DEV_LOCK ;struct devlink *devlink; - devlink = info->user_ptr[0 ]; + devlink =devlink_nl_ctx (info)->devlink;devl_dev_unlock (devlink, dev_lock);devlink_put (devlink); }
net/devlink/param.c +2 -2
@@ -566,7 +566,7 @@ devlink_param_get_from_info(struct xarray *params, struct genl_info *info)int devlink_nl_param_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_param_item *param_item;struct sk_buff *msg;int err;
@@ -667,7 +667,7 @@ static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,int devlink_nl_param_set_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;return __devlink_nl_cmd_param_set_doit (devlink,0 , &devlink->params, info,DEVLINK_CMD_PARAM_NEW );
net/devlink/port.c +9 -9
@@ -594,7 +594,7 @@ void devlink_ports_notify_unregister(struct devlink *devlink)int devlink_nl_port_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink_port *devlink_port = info->user_ptr[1 ]; +struct devlink_port *devlink_port =devlink_nl_ctx (info)->devlink_port;struct sk_buff *msg;int err;
@@ -830,7 +830,7 @@ static int devlink_port_function_set(struct devlink_port *port,int devlink_nl_port_set_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink_port *devlink_port = info->user_ptr[1 ]; +struct devlink_port *devlink_port =devlink_nl_ctx (info)->devlink_port;int err;if (info->attrs[DEVLINK_ATTR_PORT_TYPE ]) {
@@ -856,8 +856,8 @@ int devlink_nl_port_set_doit(struct sk_buff *skb, struct genl_info *info)int devlink_nl_port_split_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink_port *devlink_port = info->user_ptr[1 ]; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink_port *devlink_port =devlink_nl_ctx (info)->devlink_port; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;u32 count;if (GENL_REQ_ATTR_CHECK (info,DEVLINK_ATTR_PORT_SPLIT_COUNT ))
@@ -887,8 +887,8 @@ int devlink_nl_port_split_doit(struct sk_buff *skb, struct genl_info *info)int devlink_nl_port_unsplit_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink_port *devlink_port = info->user_ptr[1 ]; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink_port *devlink_port =devlink_nl_ctx (info)->devlink_port; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;if (!devlink_port->ops->port_unsplit)return -EOPNOTSUPP ;
@@ -899,7 +899,7 @@ int devlink_nl_port_new_doit(struct sk_buff *skb, struct genl_info *info) {struct netlink_ext_ack *extack = info->extack;struct devlink_port_new_attrs new_attrs = {}; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_port *devlink_port;struct sk_buff *msg;int err;
@@ -961,9 +961,9 @@ int devlink_nl_port_new_doit(struct sk_buff *skb, struct genl_info *info)int devlink_nl_port_del_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink_port *devlink_port = info->user_ptr[1 ]; +struct devlink_port *devlink_port =devlink_nl_ctx (info)->devlink_port;struct netlink_ext_ack *extack = info->extack; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;if (!devlink_port->ops->port_del)return -EOPNOTSUPP ;
net/devlink/rate.c +4 -4
@@ -239,7 +239,7 @@ int devlink_nl_rate_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)int devlink_nl_rate_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_rate *devlink_rate;struct sk_buff *msg;int err;
@@ -585,7 +585,7 @@ static bool devlink_rate_set_ops_supported(const struct devlink_ops *ops,int devlink_nl_rate_set_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_rate *devlink_rate;const struct devlink_ops *ops;int err;
@@ -607,7 +607,7 @@ int devlink_nl_rate_set_doit(struct sk_buff *skb, struct genl_info *info)int devlink_nl_rate_new_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_rate *rate_node;const struct devlink_ops *ops;int err;
@@ -663,7 +663,7 @@ int devlink_nl_rate_new_doit(struct sk_buff *skb, struct genl_info *info)int devlink_nl_rate_del_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_rate *rate_node;int err;
net/devlink/region.c +3 -3
@@ -469,7 +469,7 @@ static void devlink_region_snapshot_del(struct devlink_region *region,int devlink_nl_region_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_port *port =NULL ;struct devlink_region *region;const char *region_name;
@@ -588,7 +588,7 @@ int devlink_nl_region_get_dumpit(struct sk_buff *skb,int devlink_nl_region_del_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_snapshot *snapshot;struct devlink_port *port =NULL ;struct devlink_region *region;
@@ -633,7 +633,7 @@ int devlink_nl_region_del_doit(struct sk_buff *skb, struct genl_info *info)int devlink_nl_region_new_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_snapshot *snapshot;struct devlink_port *port =NULL ;struct nlattr *snapshot_id_attr;
net/devlink/resource.c +3 -3
@@ -107,7 +107,7 @@ devlink_resource_validate_size(struct devlink_resource *resource, u64 size,int devlink_nl_resource_set_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_resource *resource;u64 resource_id;u64 size;
@@ -216,7 +216,7 @@ static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,static int devlink_resource_fill (struct genl_info *info,enum devlink_command cmd,int flags) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_resource *resource;struct nlattr *resources_attr;struct sk_buff *skb =NULL ;
@@ -284,7 +284,7 @@ static int devlink_resource_fill(struct genl_info *info,int devlink_nl_resource_dump_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;if (list_empty (&devlink->resource_list))return -EOPNOTSUPP ;
net/devlink/sb.c +11 -11
@@ -204,7 +204,7 @@ static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,int devlink_nl_sb_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_sb *devlink_sb;struct sk_buff *msg;int err;
@@ -306,7 +306,7 @@ static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,int devlink_nl_sb_pool_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_sb *devlink_sb;struct sk_buff *msg;u16 pool_index;
@@ -415,7 +415,7 @@ static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,int devlink_nl_sb_pool_set_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;enum devlink_sb_threshold_type threshold_type;struct devlink_sb *devlink_sb;u16 pool_index;
@@ -506,7 +506,7 @@ static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,int devlink_nl_sb_port_pool_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink_port *devlink_port = info->user_ptr[1 ]; +struct devlink_port *devlink_port =devlink_nl_ctx (info)->devlink_port;struct devlink *devlink = devlink_port->devlink;struct devlink_sb *devlink_sb;struct sk_buff *msg;
@@ -624,8 +624,8 @@ static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,int devlink_nl_sb_port_pool_set_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink_port *devlink_port = info->user_ptr[1 ]; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink_port *devlink_port =devlink_nl_ctx (info)->devlink_port; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_sb *devlink_sb;u16 pool_index;u32 threshold;
@@ -716,7 +716,7 @@ devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,int devlink_nl_sb_tc_pool_bind_get_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink_port *devlink_port = info->user_ptr[1 ]; +struct devlink_port *devlink_port =devlink_nl_ctx (info)->devlink_port;struct devlink *devlink = devlink_port->devlink;struct devlink_sb *devlink_sb;struct sk_buff *msg;
@@ -864,8 +864,8 @@ static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,int devlink_nl_sb_tc_pool_bind_set_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink_port *devlink_port = info->user_ptr[1 ]; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink_port *devlink_port =devlink_nl_ctx (info)->devlink_port; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;enum devlink_sb_pool_type pool_type;struct devlink_sb *devlink_sb;u16 tc_index;
@@ -902,7 +902,7 @@ int devlink_nl_sb_tc_pool_bind_set_doit(struct sk_buff *skb,int devlink_nl_sb_occ_snapshot_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;const struct devlink_ops *ops = devlink->ops;struct devlink_sb *devlink_sb;
@@ -918,7 +918,7 @@ int devlink_nl_sb_occ_snapshot_doit(struct sk_buff *skb, struct genl_info *info)int devlink_nl_sb_occ_max_clear_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;const struct devlink_ops *ops = devlink->ops;struct devlink_sb *devlink_sb;
net/devlink/trap.c +6 -6
@@ -302,7 +302,7 @@ static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,int devlink_nl_trap_get_doit (struct sk_buff *skb,struct genl_info *info) {struct netlink_ext_ack *extack = info->extack; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_trap_item *trap_item;struct sk_buff *msg;int err;
@@ -412,7 +412,7 @@ static int devlink_trap_action_set(struct devlink *devlink,int devlink_nl_trap_set_doit (struct sk_buff *skb,struct genl_info *info) {struct netlink_ext_ack *extack = info->extack; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_trap_item *trap_item;if (list_empty (&devlink->trap_list))
@@ -511,7 +511,7 @@ devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,int devlink_nl_trap_group_get_doit (struct sk_buff *skb,struct genl_info *info) {struct netlink_ext_ack *extack = info->extack; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_trap_group_item *group_item;struct sk_buff *msg;int err;
@@ -682,7 +682,7 @@ static int devlink_trap_group_set(struct devlink *devlink,int devlink_nl_trap_group_set_doit (struct sk_buff *skb,struct genl_info *info) {struct netlink_ext_ack *extack = info->extack; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct devlink_trap_group_item *group_item;bool modified = false;int err;
@@ -804,7 +804,7 @@ int devlink_nl_trap_policer_get_doit(struct sk_buff *skb, {struct devlink_trap_policer_item *policer_item;struct netlink_ext_ack *extack = info->extack; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;struct sk_buff *msg;int err;
@@ -924,7 +924,7 @@ int devlink_nl_trap_policer_set_doit(struct sk_buff *skb, {struct devlink_trap_policer_item *policer_item;struct netlink_ext_ack *extack = info->extack; -struct devlink *devlink = info->user_ptr[0 ]; +struct devlink *devlink =devlink_nl_ctx (info)->devlink;if (list_empty (&devlink->trap_policer_list))return -EOPNOTSUPP ;
Signature · 1 line
2.44.0
Tariq Toukan
maintainerPATCH v9 4/14 devlink: Decouple rate storage from associated devlink object
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
Devlink rate leafs and nodes were stored in their respective devlink objects pointed to by devlink_rate->devlink.
This patch removes that association by introducing the concept of 'rate node devlink', which is where all rates that could link to each other are stored. For now this is the same as devlink_rate->devlink.
After this patch, the devlink rates stored in this devlink instance could potentially be from multiple other devlink instances. So all rate node manipulation code was updated to:
- correctly compare the actual devlink object during iteration.
- maybe acquire additional locks (noop for now).
--- net/devlink/rate.c | 196 +++++++++++++++++++++++++++++++++------------ 1 file changed, 144 insertions(+), 52 deletions(-)
net/devlink/rate.c +144 -52
@@ -30,13 +30,31 @@ devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info)return devlink_rate ?:ERR_PTR (-ENODEV ); } +static struct devlink *devl_rate_lock (struct devlink *devlink) +{ +return devlink; +} + +static struct devlink * +devl_get_rate_node_instance_locked (struct devlink *devlink) +{ +return devlink; +} + +static void devl_rate_unlock (struct devlink *devlink) +{ +} +static struct devlink_rate *devlink_rate_node_get_by_name (struct devlink *devlink,const char *node_name) {struct devlink_rate *devlink_rate; +struct devlink *rate_devlink; -list_for_each_entry (devlink_rate, &devlink->rate_list, list) { -if (devlink_rate_is_node (devlink_rate) && + rate_devlink =devl_get_rate_node_instance_locked (devlink); +list_for_each_entry (devlink_rate, &rate_devlink->rate_list, list) { +if (devlink_rate->devlink == devlink && +devlink_rate_is_node (devlink_rate) && !strcmp (node_name, devlink_rate->name))return devlink_rate; }
@@ -190,17 +208,25 @@ static void devlink_rate_notify(struct devlink_rate *devlink_rate,void devlink_rates_notify_register (struct devlink *devlink) {struct devlink_rate *rate_node; +struct devlink *rate_devlink; -list_for_each_entry (rate_node, &devlink->rate_list, list) -devlink_rate_notify (rate_node,DEVLINK_CMD_RATE_NEW ); + rate_devlink =devl_rate_lock (devlink); +list_for_each_entry (rate_node, &rate_devlink->rate_list, list) +if (rate_node->devlink == devlink) +devlink_rate_notify (rate_node,DEVLINK_CMD_RATE_NEW ); +devl_rate_unlock (devlink); }void devlink_rates_notify_unregister (struct devlink *devlink) {struct devlink_rate *rate_node; +struct devlink *rate_devlink; -list_for_each_entry_reverse (rate_node, &devlink->rate_list, list) -devlink_rate_notify (rate_node,DEVLINK_CMD_RATE_DEL ); + rate_devlink =devl_rate_lock (devlink); +list_for_each_entry_reverse (rate_node, &rate_devlink->rate_list, list) +if (rate_node->devlink == devlink) +devlink_rate_notify (rate_node,DEVLINK_CMD_RATE_DEL ); +devl_rate_unlock (devlink); } static int
@@ -209,17 +235,20 @@ devlink_nl_rate_get_dump_one(struct sk_buff *msg, struct devlink *devlink, {struct devlink_nl_dump_state *state =devlink_dump_state (cb);struct devlink_rate *devlink_rate; +struct devlink *rate_devlink;int idx =0 ;int err =0 ; -list_for_each_entry (devlink_rate, &devlink->rate_list, list) { + rate_devlink =devl_rate_lock (devlink); +list_for_each_entry (devlink_rate, &rate_devlink->rate_list, list) {enum devlink_command cmd =DEVLINK_CMD_RATE_NEW ;u32 id =NETLINK_CB (cb->skb).portid; -if (idx < state->idx) { +if (idx < state->idx || devlink_rate->devlink != devlink) { idx++;continue ; } + err =devlink_nl_rate_fill (msg, devlink_rate, cmd, id, cb->nlh->nlmsg_seq, flags,NULL );if (err) {
@@ -228,6 +257,7 @@ devlink_nl_rate_get_dump_one(struct sk_buff *msg, struct devlink *devlink, } idx++; } +devl_rate_unlock (devlink);return err; }
@@ -244,23 +274,33 @@ int devlink_nl_rate_get_doit(struct sk_buff *skb, struct genl_info *info)struct sk_buff *msg;int err; +devl_rate_lock (devlink); devlink_rate =devlink_rate_get_from_info (devlink, info); -if (IS_ERR (devlink_rate)) -return PTR_ERR (devlink_rate); +if (IS_ERR (devlink_rate)) { + err =PTR_ERR (devlink_rate); + goto unlock; + } msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); -if (!msg) -return -ENOMEM ; +if (!msg) { + err = -ENOMEM ; + goto unlock; + } err =devlink_nl_rate_fill (msg, devlink_rate,DEVLINK_CMD_RATE_NEW , info->snd_portid, info->snd_seq,0 , info->extack); -if (err) { -nlmsg_free (msg); -return err; - } +if (err) + goto err_fill; +devl_rate_unlock (devlink);return genlmsg_reply (msg, info); + +err_fill: +nlmsg_free (msg); +unlock: +devl_rate_unlock (devlink); +return err; } static bool
@@ -590,24 +630,32 @@ int devlink_nl_rate_set_doit(struct sk_buff *skb, struct genl_info *info)const struct devlink_ops *ops;int err; +devl_rate_lock (devlink); devlink_rate =devlink_rate_get_from_info (devlink, info); -if (IS_ERR (devlink_rate)) -return PTR_ERR (devlink_rate); +if (IS_ERR (devlink_rate)) { + err =PTR_ERR (devlink_rate); + goto unlock; + } ops = devlink->ops; -if (!ops || !devlink_rate_set_ops_supported (ops, info, devlink_rate->type)) -return -EOPNOTSUPP ; +if (!ops || + !devlink_rate_set_ops_supported (ops, info, devlink_rate->type)) { + err = -EOPNOTSUPP ; + goto unlock; + } err =devlink_nl_rate_set (devlink_rate, ops, info);if (!err)devlink_rate_notify (devlink_rate,DEVLINK_CMD_RATE_NEW ); +unlock: +devl_rate_unlock (devlink);return err; }int devlink_nl_rate_new_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *devlink =devlink_nl_ctx (info)->devlink; +struct devlink *rate_devlink, *devlink =devlink_nl_ctx (info)->devlink;struct devlink_rate *rate_node;const struct devlink_ops *ops;int err;
@@ -621,15 +669,21 @@ int devlink_nl_rate_new_doit(struct sk_buff *skb, struct genl_info *info)if (!devlink_rate_set_ops_supported (ops, info,DEVLINK_RATE_TYPE_NODE ))return -EOPNOTSUPP ; + rate_devlink =devl_rate_lock (devlink); rate_node =devlink_rate_node_get_from_attrs (devlink, info->attrs); -if (!IS_ERR (rate_node)) -return -EEXIST ; -else if (rate_node ==ERR_PTR (-EINVAL )) -return -EINVAL ; +if (!IS_ERR (rate_node)) { + err = -EEXIST ; + goto unlock; + }else if (rate_node ==ERR_PTR (-EINVAL )) { + err = -EINVAL ; + goto unlock; + } rate_node =kzalloc_obj (*rate_node); -if (!rate_node) -return -ENOMEM ; +if (!rate_node) { + err = -ENOMEM ; + goto unlock; + } rate_node->devlink = devlink; rate_node->type =DEVLINK_RATE_TYPE_NODE ;
@@ -648,8 +702,9 @@ int devlink_nl_rate_new_doit(struct sk_buff *skb, struct genl_info *info) goto err_rate_set;refcount_set (&rate_node->refcnt,1 ); -list_add (&rate_node->list, &devlink->rate_list); +list_add (&rate_node->list, &rate_devlink->rate_list);devlink_rate_notify (rate_node,DEVLINK_CMD_RATE_NEW ); +devl_rate_unlock (devlink);return 0 ; err_rate_set:
@@ -658,6 +713,8 @@ int devlink_nl_rate_new_doit(struct sk_buff *skb, struct genl_info *info)kfree (rate_node->name); err_strdup:kfree (rate_node); +unlock: +devl_rate_unlock (devlink);return err; }
@@ -667,13 +724,17 @@ int devlink_nl_rate_del_doit(struct sk_buff *skb, struct genl_info *info)struct devlink_rate *rate_node;int err; +devl_rate_lock (devlink); rate_node =devlink_rate_node_get_from_info (devlink, info); -if (IS_ERR (rate_node)) -return PTR_ERR (rate_node); +if (IS_ERR (rate_node)) { + err =PTR_ERR (rate_node); + goto unlock; + }if (refcount_read (&rate_node->refcnt) >1 ) {NL_SET_ERR_MSG (info->extack,"Node has children. Cannot delete node." ); -return -EBUSY ; + err = -EBUSY ; + goto unlock; }devlink_rate_notify (rate_node,DEVLINK_CMD_RATE_DEL );
@@ -684,6 +745,8 @@ int devlink_nl_rate_del_doit(struct sk_buff *skb, struct genl_info *info)list_del (&rate_node->list);kfree (rate_node->name);kfree (rate_node); +unlock: +devl_rate_unlock (devlink);return err; }
@@ -692,14 +755,20 @@ int devlink_rates_check(struct devlink *devlink, struct netlink_ext_ack *extack) {struct devlink_rate *devlink_rate; +struct devlink *rate_devlink; +int err =0 ; -list_for_each_entry (devlink_rate, &devlink->rate_list, list) -if (!rate_filter ||rate_filter (devlink_rate)) { + rate_devlink =devl_rate_lock (devlink); +list_for_each_entry (devlink_rate, &rate_devlink->rate_list, list) +if (devlink_rate->devlink == devlink && + (!rate_filter ||rate_filter (devlink_rate))) {if (extack)NL_SET_ERR_MSG (extack,"Rate node(s) exists." ); -return -EBUSY ; + err = -EBUSY ; +break ; } -return 0 ; +devl_rate_unlock (devlink); +return err; }/**
@@ -716,14 +785,20 @@ devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name, struct devlink_rate *parent) {struct devlink_rate *rate_node; +struct devlink *rate_devlink; + rate_devlink =devl_rate_lock (devlink); rate_node =devlink_rate_node_get_by_name (devlink, node_name); -if (!IS_ERR (rate_node)) -return ERR_PTR (-EEXIST ); +if (!IS_ERR (rate_node)) { + rate_node =ERR_PTR (-EEXIST ); + goto unlock; + } rate_node =kzalloc_obj (*rate_node); -if (!rate_node) -return ERR_PTR (-ENOMEM ); +if (!rate_node) { + rate_node =ERR_PTR (-ENOMEM ); + goto unlock; + }if (parent) { rate_node->parent = parent;
@@ -737,12 +812,15 @@ devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name, rate_node->name =kstrdup (node_name,GFP_KERNEL );if (!rate_node->name) {kfree (rate_node); -return ERR_PTR (-ENOMEM ); + rate_node =ERR_PTR (-ENOMEM ); + goto unlock; }refcount_set (&rate_node->refcnt,1 ); -list_add (&rate_node->list, &devlink->rate_list); +list_add (&rate_node->list, &rate_devlink->rate_list);devlink_rate_notify (rate_node,DEVLINK_CMD_RATE_NEW ); +unlock: +devl_rate_unlock (devlink);return rate_node; }EXPORT_SYMBOL_GPL (devl_rate_node_create);
@@ -758,10 +836,10 @@ EXPORT_SYMBOL_GPL(devl_rate_node_create);int devl_rate_leaf_create (struct devlink_port *devlink_port,void *priv,struct devlink_rate *parent) { -struct devlink *devlink = devlink_port->devlink; +struct devlink *rate_devlink, *devlink = devlink_port->devlink;struct devlink_rate *devlink_rate; -devl_assert_locked (devlink_port->devlink); +devl_assert_locked (devlink);if (WARN_ON (devlink_port->devlink_rate))return -EBUSY ;
@@ -770,6 +848,7 @@ int devl_rate_leaf_create(struct devlink_port *devlink_port, void *priv,if (!devlink_rate)return -ENOMEM ; + rate_devlink =devl_rate_lock (devlink);if (parent) { devlink_rate->parent = parent;refcount_inc (&devlink_rate->parent->refcnt);
@@ -779,9 +858,10 @@ int devl_rate_leaf_create(struct devlink_port *devlink_port, void *priv, devlink_rate->devlink = devlink; devlink_rate->devlink_port = devlink_port; devlink_rate->priv = priv; -list_add_tail (&devlink_rate->list, &devlink->rate_list); +list_add_tail (&devlink_rate->list, &rate_devlink->rate_list); devlink_port->devlink_rate = devlink_rate;devlink_rate_notify (devlink_rate,DEVLINK_CMD_RATE_NEW ); +devl_rate_unlock (devlink);return 0 ; }
@@ -797,16 +877,19 @@ EXPORT_SYMBOL_GPL(devl_rate_leaf_create);void devl_rate_leaf_destroy (struct devlink_port *devlink_port) {struct devlink_rate *devlink_rate = devlink_port->devlink_rate; +struct devlink *devlink = devlink_port->devlink; -devl_assert_locked (devlink_port->devlink); +devl_assert_locked (devlink);if (!devlink_rate)return ; +devl_rate_lock (devlink);devlink_rate_notify (devlink_rate,DEVLINK_CMD_RATE_DEL );if (devlink_rate->parent)refcount_dec (&devlink_rate->parent->refcnt);list_del (&devlink_rate->list); devlink_port->devlink_rate =NULL ; +devl_rate_unlock (devlink);kfree (devlink_rate); }EXPORT_SYMBOL_GPL (devl_rate_leaf_destroy);
@@ -815,20 +898,25 @@ EXPORT_SYMBOL_GPL(devl_rate_leaf_destroy);* devl_rate_nodes_destroy - destroy all devlink rate nodes on device * @devlink: devlink instance * -* Unset parent for all rate objects and destroy all rate nodes -* on specified device. +* Unset parent for all rate objects involving this device and destroy all rate +* nodes on it. */ void devl_rate_nodes_destroy (struct devlink *devlink) { -const struct devlink_ops *ops = devlink->ops;struct devlink_rate *devlink_rate, *tmp; +const struct devlink_ops *ops; +struct devlink *rate_devlink;devl_assert_locked (devlink); + rate_devlink =devl_rate_lock (devlink); -list_for_each_entry (devlink_rate, &devlink->rate_list, list) { -if (!devlink_rate->parent) +list_for_each_entry (devlink_rate, &rate_devlink->rate_list, list) { +if (!devlink_rate->parent || + (devlink_rate->devlink != devlink && + devlink_rate->parent->devlink != devlink))continue ; + ops = devlink_rate->devlink->ops;if (devlink_rate_is_leaf (devlink_rate)) ops->rate_leaf_parent_set (devlink_rate,NULL , devlink_rate->priv,NULL ,NULL );
@@ -839,13 +927,17 @@ void devl_rate_nodes_destroy(struct devlink *devlink)refcount_dec (&devlink_rate->parent->refcnt); devlink_rate->parent =NULL ; } -list_for_each_entry_safe (devlink_rate, tmp, &devlink->rate_list, list) { -if (devlink_rate_is_node (devlink_rate)) { + ops = devlink->ops; +list_for_each_entry_safe (devlink_rate, tmp, &rate_devlink->rate_list, + list) { +if (devlink_rate->devlink == devlink && +devlink_rate_is_node (devlink_rate)) { ops->rate_node_del (devlink_rate, devlink_rate->priv,NULL );list_del (&devlink_rate->list);kfree (devlink_rate->name);kfree (devlink_rate); } } +devl_rate_unlock (devlink); }EXPORT_SYMBOL_GPL (devl_rate_nodes_destroy);
Signature · 1 line
2.44.0
Jakub Kicinski
maintainerRe: [PATCH net-next V9 04/14] devlink: Decouple rate storage from associated devlink object
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
devlink: Decouple rate storage from associated devlink object
Devlink rate leafs and nodes were stored in their respective devlink objects pointed to by devlink_rate->devlink.
Quoted 4 lines
net/devlink/rate.c +0
-0
[ ... ]
Quoted 13 lines
net/devlink/rate.c +2
-1@@ -648,8 +702,9 @@ int devlink_nl_rate_new_doit(struct sk_buff *skb, struct genl_info *info) goto err_rate_set;refcount_set (&rate_node->refcnt,1 ); -list_add (&rate_node->list, &devlink->rate_list); +list_add (&rate_node->list, &rate_devlink->rate_list);devlink_rate_notify (rate_node,DEVLINK_CMD_RATE_NEW ); +devl_rate_unlock (devlink);return 0 ; err_rate_set:net/devlink/rate.c +0
-0ops->rate_node_del (rate_node, rate_node->priv, info->extack);
net/devlink/rate.c int devlink_nl_rate_new_doit(struct sk_buff *skb, struct genl_info *info) · line 719
Quoted 7 lines
net/devlink/rate.c +2
-0@@ -658,6 +713,8 @@ int devlink_nl_rate_new_doit(struct sk_buff *skb, struct genl_info *info)kfree (rate_node->name); err_strdup:kfree (rate_node); +unlock: +devl_rate_unlock (devlink);return err; }
If devlink_nl_rate_set() fails after it successfully sets the parent node and increments the parent's refcount, does this error path permanently leak that reference since it frees the node without decrementing it?
[ ... ]
Quoted 26 lines
net/devlink/rate.c +10
-4@@ -716,14 +785,20 @@ devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name, struct devlink_rate *parent) {struct devlink_rate *rate_node; +struct devlink *rate_devlink; + rate_devlink =devl_rate_lock (devlink); rate_node =devlink_rate_node_get_by_name (devlink, node_name); -if (!IS_ERR (rate_node)) -return ERR_PTR (-EEXIST ); +if (!IS_ERR (rate_node)) { + rate_node =ERR_PTR (-EEXIST ); + goto unlock; + } rate_node =kzalloc_obj (*rate_node); -if (!rate_node) -return ERR_PTR (-ENOMEM ); +if (!rate_node) { + rate_node =ERR_PTR (-ENOMEM ); + goto unlock; + }if (parent) { rate_node->parent = parent;
net/devlink/rate.c devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name, · line 817
Quoted 7 lines
net/devlink/rate.c +2
-1@@ -737,12 +812,15 @@ devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name, rate_node->name =kstrdup (node_name,GFP_KERNEL );if (!rate_node->name) {kfree (rate_node); -return ERR_PTR (-ENOMEM ); + rate_node =ERR_PTR (-ENOMEM ); + goto unlock; }
Similarly, if kstrdup() fails here, does this leak the parent's refcount since it was already incremented earlier in the function?
Re: [PATCH net-next V9 04/14] devlink: Decouple rate storage from associated devlink object
Quoted 27 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
devlink: Decouple rate storage from associated devlink objectDevlink rate leafs and nodes were stored in their respective devlink objects pointed to by
devlink_rate->devlink.Quoted 4 lines
net/devlink/rate.c +0
-0[ ... ]
Quoted 14 lines
net/devlink/rate.c +0
-0@@ -648,8 +702,9 @@ int devlink_nl_rate_new_doit(struct sk_buff *skb, struct genl_info *info) goto err_rate_set;
refcount_set(&rate_node->refcnt, 1);net/devlink/rate.c +1
-1-list_add (&rate_node->list, &devlink->rate_list); +list_add (&rate_node->list, &rate_devlink->rate_list);
devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);net/devlink/rate.c +1
-0+devl_rate_unlock (devlink);return 0;
err_rate_set:
ops->rate_node_del(rate_node,rate_node->priv, info-Quoted 1 line
extack);
Quoted 15 lines
Quoted 9 lines
net/devlink/rate.c +0
-0@@ -658,6 +713,8 @@ int devlink_nl_rate_new_doit(struct sk_buff *skb, struct genl_info *info) kfree(rate_node->name);err_strdup:
kfree(rate_node);net/devlink/rate.c +2
-0+unlock: +devl_rate_unlock (devlink);return err;
}If
devlink_nl_rate_set()fails after it successfully sets the parent node
and increments the parent's refcount, does this error path permanently
leak that reference since it frees the node without decrementing it?
This is a preexisting bug caused by the fact that devlink_nl_rate_set doesn't rollback what it did on failure, leaving various side effects. Most are setting of various rate params, but this parent refcount is more serious. It will have to be addressed, but I don't think this series is the right place for it. (I'll send a separate patch soon).
Quoted 34 lines
[ ... ]
Quoted 32 lines
net/devlink/rate.c +1
-0@@ -716,14 +785,20 @@ devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name, struct devlink_rate *parent) { struct devlink_rate *rate_node; +struct devlink *rate_devlink;net/devlink/rate.c +1
-0+ rate_devlink =devl_rate_lock (devlink);
rate_node=devlink_rate_node_get_by_name(devlink, node_name);net/devlink/rate.c +4
-2-if (!IS_ERR (rate_node)) -return ERR_PTR (-EEXIST ); +if (!IS_ERR (rate_node)) { + rate_node =ERR_PTR (-EEXIST ); + goto unlock; + }
rate_node=kzalloc_obj(*rate_node);net/devlink/rate.c +4
-2-if (!rate_node) -return ERR_PTR (-ENOMEM ); +if (!rate_node) { + rate_node =ERR_PTR (-ENOMEM ); + goto unlock; + }
if (parent) {
rate_node->parent= parent;net/devlink/rate.c +0
-0@@ -737,12 +812,15 @@ devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name, rate_node->name = kstrdup(node_name, GFP_KERNEL); if (!rate_node->name) { kfree(rate_node);
Quoted 8 lines
net/devlink/rate.c devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name, · line 817
Quoted 4 lines
net/devlink/rate.c +2
-1-return ERR_PTR (-ENOMEM ); + rate_node =ERR_PTR (-ENOMEM ); + goto unlock; }Similarly, if
kstrdup()fails here, does this leak the parent's refcount
since it was already incremented earlier in the function?
Another preexisting issue. These code changes brought this section to Argus' Eyes attention. I made a note to send a patch to fix this.
Cosmin.
Tariq Toukan
maintainerPATCH v9 5/14 devlink: Add parent dev to devlink API
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
Upcoming changes to the rate commands need the parent devlink specified. This change adds a nested 'parent-dev' attribute to the API and helpers to obtain and put a reference to the parent devlink instance in info->ctx.
To avoid deadlocks, the parent devlink is unlocked before obtaining the main devlink instance that is the target of the request.
A reference to the parent is kept until the end of the request to avoid it suddenly disappearing.
This means that this reference is of limited use without additional protection.
--- Documentation/netlink/specs/devlink.yaml | 14 +++++++++ include/uapi/linux/devlink.h | 2 ++ net/devlink/devl_internal.h | 3 ++ net/devlink/netlink.c | 36 ++++++++++++++++++++---- 4 files changed, 50 insertions(+), 5 deletions(-)
Documentation/netlink/specs/devlink.yaml +14 -0
@@ -873,6 +873,10 @@ attribute-sets: doc: Unique devlink instance index. checks: max: u32-max + - + name: parent-dev + type: nest + nested-attributes: dl-parent-dev - name: dl-dev-stats subset-of: devlink
@@ -1295,6 +1299,16 @@ attribute-sets: Specifies the bandwidth share assigned to the Traffic Class. The bandwidth for the traffic class is determined in proportion to the sum of the shares of all configured classes. + - + name: dl-parent-dev + subset-of: devlink + attributes: + - + name: bus-name + - + name: dev-name + - + name: index operations: enum-model: directional
include/uapi/linux/devlink.h +2 -0
@@ -646,6 +646,8 @@ enum devlink_attr {DEVLINK_ATTR_INDEX ,/* uint */ + DEVLINK_ATTR_PARENT_DEV, /* nested */ +/* Add new attributes above here, update the spec in * Documentation/netlink/specs/devlink.yaml and re-generate * net/devlink/netlink_gen.c.
net/devlink/devl_internal.h +3 -0
@@ -153,6 +153,7 @@ int devlink_rel_devlink_handle_put(struct sk_buff *msg, struct devlink *devlink,struct devlink_nl_ctx {struct devlink *devlink;struct devlink_port *devlink_port; +struct devlink *parent_devlink; }; static inline struct devlink_nl_ctx *
@@ -191,6 +192,8 @@ typedef int devlink_nl_dump_one_func_t(struct sk_buff *msg,struct devlink *devlink_get_from_attrs_lock (struct net *net,struct nlattr **attrs,bool dev_lock); +struct devlink * +devlink_get_parent_from_attrs_lock (struct net *net,struct nlattr **attrs);int devlink_nl_dumpit (struct sk_buff *msg,struct netlink_callback *cb,devlink_nl_dump_one_func_t *dump_one);
net/devlink/netlink.c +31 -5
@@ -12,6 +12,7 @@#define DEVLINK_NL_FLAG_NEED_PORT BIT(0)#define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT BIT(1)#define DEVLINK_NL_FLAG_NEED_DEV_LOCK BIT(2) +#define DEVLINK_NL_FLAG_OPTIONAL_PARENT_DEV BIT(3)static const struct genl_multicast_group devlink_nl_mcgrps[] = { [DEVLINK_MCGRP_CONFIG ] = { .name =DEVLINK_GENL_MCGRP_CONFIG_NAME },
@@ -239,19 +240,39 @@ devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs,return ERR_PTR (-ENODEV ); } +struct devlink * +devlink_get_parent_from_attrs_lock (struct net *net,struct nlattr **attrs) +{ +return ERR_PTR (-EOPNOTSUPP ); +} +static int __devlink_nl_pre_doit (struct sk_buff *skb,struct genl_info *info,u8 flags) { +bool parent_dev = flags &DEVLINK_NL_FLAG_OPTIONAL_PARENT_DEV ;bool dev_lock = flags &DEVLINK_NL_FLAG_NEED_DEV_LOCK ; +struct devlink *devlink, *parent_devlink =NULL ; +struct net *net =genl_info_net (info); +struct nlattr **attrs = info->attrs;struct devlink_port *devlink_port; -struct devlink *devlink;int err; - devlink =devlink_get_from_attrs_lock (genl_info_net (info), info->attrs, - dev_lock); -if (IS_ERR (devlink)) -return PTR_ERR (devlink); +if (parent_dev && attrs[DEVLINK_ATTR_PARENT_DEV ]) { + parent_devlink =devlink_get_parent_from_attrs_lock (net, attrs); +if (IS_ERR (parent_devlink)) +return PTR_ERR (parent_devlink); +devlink_nl_ctx (info)->parent_devlink = parent_devlink; +/* Drop the parent devlink lock but don't release the reference. +* This will keep it alive until the end of the request. +*/ +devl_unlock (parent_devlink); + } + devlink =devlink_get_from_attrs_lock (net, attrs, dev_lock); +if (IS_ERR (devlink)) { + err =PTR_ERR (devlink); + goto parent_put; + }devlink_nl_ctx (info)->devlink = devlink;if (flags &DEVLINK_NL_FLAG_NEED_PORT ) { devlink_port =devlink_port_get_from_info (devlink, info);
@@ -270,6 +291,9 @@ static int __devlink_nl_pre_doit(struct sk_buff *skb, struct genl_info *info, unlock:devl_dev_unlock (devlink, dev_lock);devlink_put (devlink); +parent_put: +if (parent_dev && parent_devlink) +devlink_put (parent_devlink);return err; }
@@ -307,6 +331,8 @@ static void __devlink_nl_post_doit(struct sk_buff *skb, struct genl_info *info, devlink =devlink_nl_ctx (info)->devlink;devl_dev_unlock (devlink, dev_lock);devlink_put (devlink); +if (devlink_nl_ctx (info)->parent_devlink) +devlink_put (devlink_nl_ctx (info)->parent_devlink); } void devlink_nl_post_doit(const struct genl_split_ops *ops,
Signature · 1 line
2.44.0
Tariq Toukan
maintainerPATCH v9 6/14 devlink: Allow parent dev for rate-set and rate-new
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
Currently, a devlink rate's parent device is assumed to be the same as the one where the devlink rate is created.
This patch changes that to allow rate commands to accept an additional argument that specifies the parent dev. This will allow devlink rate groups with leafs from other devices.
Example of the new usage with ynl:
Creating a group on pci/0000:08:00.1 with a parent to an already existing pci/0000:08:00.1/group1:./tools/net/ynl/pyynl/cli.py --spec \
Documentation/netlink/specs/devlink.yaml --do rate-new --json '{
"bus-name": "pci",
"dev-name": "0000:08:00.1",
"rate-node-name": "group2",
"rate-parent-node-name": "group1",
"parent-dev": {
"bus-name": "pci",
"dev-name": "0000:08:00.1"
}}'
Setting the parent of leaf node pci/0000:08:00.1/65537 to pci/0000:08:00.0/group1:./tools/net/ynl/pyynl/cli.py --spec \
Documentation/netlink/specs/devlink.yaml --do rate-set --json '{
"bus-name": "pci",
"dev-name": "0000:08:00.1",
"port-index": 65537,
"parent-dev": {
"bus-name": "pci",
"dev-name": "0000:08:00.0"
},
"rate-parent-node-name": "group1"}'
--- Documentation/netlink/specs/devlink.yaml | 10 +++--- net/devlink/netlink.c | 40 +++++++++++++++++++++++- net/devlink/netlink_gen.c | 24 +++++++++----- net/devlink/netlink_gen.h | 8 +++++ net/devlink/rate.c | 4 ++- 5 files changed, 72 insertions(+), 14 deletions(-)
Documentation/netlink/specs/devlink.yaml +6 -4
@@ -2271,8 +2271,8 @@ operations: dont-validate: [strict] flags: [admin-perm] do: - pre: devlink-nl-pre-doit - post: devlink-nl-post-doit + pre: devlink-nl-pre-doit-parent-dev-optional + post: devlink-nl-post-doit-parent-dev-optional request: attributes: - bus-name
@@ -2285,6 +2285,7 @@ operations: - rate-tx-weight - rate-parent-node-name - rate-tc-bws + - parent-dev - name: rate-new
@@ -2293,8 +2294,8 @@ operations: dont-validate: [strict] flags: [admin-perm] do: - pre: devlink-nl-pre-doit - post: devlink-nl-post-doit + pre: devlink-nl-pre-doit-parent-dev-optional + post: devlink-nl-post-doit-parent-dev-optional request: attributes: - bus-name
@@ -2307,6 +2308,7 @@ operations: - rate-tx-weight - rate-parent-node-name - rate-tc-bws + - parent-dev - name: rate-del
net/devlink/netlink.c +39 -1
@@ -243,7 +243,29 @@ devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs,struct devlink *devlink_get_parent_from_attrs_lock (struct net *net,struct nlattr **attrs) { -return ERR_PTR (-EOPNOTSUPP ); +unsigned int maxtype =ARRAY_SIZE (devlink_dl_parent_dev_nl_policy) -1 ; +struct devlink *devlink; +struct nlattr **tb; +int err; + +if (!attrs[DEVLINK_ATTR_PARENT_DEV ]) +return ERR_PTR (-EINVAL ); + + tb =kcalloc (maxtype +1 ,sizeof (*tb),GFP_KERNEL ); +if (!tb) +return ERR_PTR (-ENOMEM ); + + err =nla_parse_nested (tb, maxtype, attrs[DEVLINK_ATTR_PARENT_DEV ], + devlink_dl_parent_dev_nl_policy,NULL ); +if (err) + goto out; + + devlink =devlink_get_from_attrs_lock (net, tb, false); +kfree (tb); +return devlink; +out: +kfree (tb); +return ERR_PTR (err); } static int __devlink_nl_pre_doit(struct sk_buff *skb, struct genl_info *info,
@@ -322,6 +344,14 @@ int devlink_nl_pre_doit_port_optional(const struct genl_split_ops *ops,return __devlink_nl_pre_doit (skb, info,DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT ); } +int devlink_nl_pre_doit_parent_dev_optional (const struct genl_split_ops *ops, +struct sk_buff *skb, +struct genl_info *info) +{ +return __devlink_nl_pre_doit (skb, info, +DEVLINK_NL_FLAG_OPTIONAL_PARENT_DEV ); +} +static void __devlink_nl_post_doit (struct sk_buff *skb,struct genl_info *info,u8 flags) {
@@ -348,6 +378,14 @@ devlink_nl_post_doit_dev_lock(const struct genl_split_ops *ops,__devlink_nl_post_doit (skb, info,DEVLINK_NL_FLAG_NEED_DEV_LOCK ); } +void +devlink_nl_post_doit_parent_dev_optional (const struct genl_split_ops *ops, +struct sk_buff *skb, +struct genl_info *info) +{ +__devlink_nl_post_doit (skb, info,DEVLINK_NL_FLAG_OPTIONAL_PARENT_DEV ); +} + static int devlink_nl_inst_single_dumpit(struct sk_buff *msg, struct netlink_callback *cb, int flags, devlink_nl_dump_one_func_t *dump_one,
net/devlink/netlink_gen.c +16 -8
@@ -44,6 +44,12 @@ devlink_attr_param_type_validate(const struct nlattr *attr, }/* Common nested types */ +const struct nla_policy devlink_dl_parent_dev_nl_policy[DEVLINK_ATTR_INDEX +1 ] = { + [DEVLINK_ATTR_BUS_NAME ] = { .type =NLA_NUL_STRING , }, + [DEVLINK_ATTR_DEV_NAME ] = { .type =NLA_NUL_STRING , }, + [DEVLINK_ATTR_INDEX ] =NLA_POLICY_FULL_RANGE (NLA_UINT , &devlink_attr_index_range), +}; +const struct nla_policy devlink_dl_port_function_nl_policy[DEVLINK_PORT_FN_ATTR_CAPS +1 ] = { [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR ] = { .type =NLA_BINARY , }, [DEVLINK_PORT_FN_ATTR_STATE ] =NLA_POLICY_MAX (NLA_U8 ,1 ),
@@ -597,7 +603,7 @@ static const struct nla_policy devlink_rate_get_dump_nl_policy[DEVLINK_ATTR_INDE };/* DEVLINK_CMD_RATE_SET - do */ -static const struct nla_policy devlink_rate_set_nl_policy[DEVLINK_ATTR_INDEX +1 ] = { +static const struct nla_policy devlink_rate_set_nl_policy[DEVLINK_ATTR_PARENT_DEV +1 ] = { [DEVLINK_ATTR_BUS_NAME ] = { .type =NLA_NUL_STRING , }, [DEVLINK_ATTR_DEV_NAME ] = { .type =NLA_NUL_STRING , }, [DEVLINK_ATTR_INDEX ] =NLA_POLICY_FULL_RANGE (NLA_UINT , &devlink_attr_index_range),
@@ -608,10 +614,11 @@ static const struct nla_policy devlink_rate_set_nl_policy[DEVLINK_ATTR_INDEX + 1 [DEVLINK_ATTR_RATE_TX_WEIGHT] = { .type = NLA_U32, }, [DEVLINK_ATTR_RATE_PARENT_NODE_NAME] = { .type = NLA_NUL_STRING, }, [DEVLINK_ATTR_RATE_TC_BWS] = NLA_POLICY_NESTED(devlink_dl_rate_tc_bws_nl_policy), + [DEVLINK_ATTR_PARENT_DEV] = NLA_POLICY_NESTED(devlink_dl_parent_dev_nl_policy), };/* DEVLINK_CMD_RATE_NEW - do */ -static const struct nla_policy devlink_rate_new_nl_policy[DEVLINK_ATTR_INDEX +1 ] = { +static const struct nla_policy devlink_rate_new_nl_policy[DEVLINK_ATTR_PARENT_DEV +1 ] = { [DEVLINK_ATTR_BUS_NAME ] = { .type =NLA_NUL_STRING , }, [DEVLINK_ATTR_DEV_NAME ] = { .type =NLA_NUL_STRING , }, [DEVLINK_ATTR_INDEX ] =NLA_POLICY_FULL_RANGE (NLA_UINT , &devlink_attr_index_range),
@@ -622,6 +629,7 @@ static const struct nla_policy devlink_rate_new_nl_policy[DEVLINK_ATTR_INDEX + 1 [DEVLINK_ATTR_RATE_TX_WEIGHT] = { .type = NLA_U32, }, [DEVLINK_ATTR_RATE_PARENT_NODE_NAME] = { .type = NLA_NUL_STRING, }, [DEVLINK_ATTR_RATE_TC_BWS] = NLA_POLICY_NESTED(devlink_dl_rate_tc_bws_nl_policy), + [DEVLINK_ATTR_PARENT_DEV] = NLA_POLICY_NESTED(devlink_dl_parent_dev_nl_policy), };/* DEVLINK_CMD_RATE_DEL - do */
@@ -1272,21 +1280,21 @@ const struct genl_split_ops devlink_nl_ops[74] = { { .cmd = DEVLINK_CMD_RATE_SET, .validate = GENL_DONT_VALIDATE_STRICT, - .pre_doit = devlink_nl_pre_doit, + .pre_doit = devlink_nl_pre_doit_parent_dev_optional, .doit = devlink_nl_rate_set_doit, - .post_doit = devlink_nl_post_doit, + .post_doit = devlink_nl_post_doit_parent_dev_optional, .policy = devlink_rate_set_nl_policy, - .maxattr = DEVLINK_ATTR_INDEX, + .maxattr = DEVLINK_ATTR_PARENT_DEV, .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO, }, { .cmd = DEVLINK_CMD_RATE_NEW, .validate = GENL_DONT_VALIDATE_STRICT, - .pre_doit = devlink_nl_pre_doit, + .pre_doit = devlink_nl_pre_doit_parent_dev_optional, .doit = devlink_nl_rate_new_doit, - .post_doit = devlink_nl_post_doit, + .post_doit = devlink_nl_post_doit_parent_dev_optional, .policy = devlink_rate_new_nl_policy, - .maxattr = DEVLINK_ATTR_INDEX, + .maxattr = DEVLINK_ATTR_PARENT_DEV, .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO, }, {
net/devlink/netlink_gen.h +8 -0
@@ -13,6 +13,7 @@#include <uapi/linux/devlink.h> /* Common nested types */ +extern const struct nla_policy devlink_dl_parent_dev_nl_policy[DEVLINK_ATTR_INDEX +1 ];extern const struct nla_policy devlink_dl_port_function_nl_policy[DEVLINK_PORT_FN_ATTR_CAPS +1 ];extern const struct nla_policy devlink_dl_rate_tc_bws_nl_policy[DEVLINK_RATE_TC_ATTR_BW +1 ];extern const struct nla_policy devlink_dl_selftest_id_nl_policy[DEVLINK_ATTR_SELFTEST_ID_FLASH +1 ];
@@ -29,12 +30,19 @@ int devlink_nl_pre_doit_dev_lock(const struct genl_split_ops *ops,int devlink_nl_pre_doit_port_optional (const struct genl_split_ops *ops,struct sk_buff *skb,struct genl_info *info); +int devlink_nl_pre_doit_parent_dev_optional (const struct genl_split_ops *ops, +struct sk_buff *skb, +struct genl_info *info);void devlink_nl_post_doit (const struct genl_split_ops *ops,struct sk_buff *skb,struct genl_info *info);void devlink_nl_post_doit_dev_lock (const struct genl_split_ops *ops,struct sk_buff *skb,struct genl_info *info); +void +devlink_nl_post_doit_parent_dev_optional (const struct genl_split_ops *ops, +struct sk_buff *skb, +struct genl_info *info);int devlink_nl_get_doit (struct sk_buff *skb,struct genl_info *info);int devlink_nl_get_dumpit (struct sk_buff *skb,struct netlink_callback *cb);
net/devlink/rate.c +3 -1
@@ -655,9 +655,11 @@ int devlink_nl_rate_set_doit(struct sk_buff *skb, struct genl_info *info)int devlink_nl_rate_new_doit (struct sk_buff *skb,struct genl_info *info) { -struct devlink *rate_devlink, *devlink =devlink_nl_ctx (info)->devlink; +struct devlink_nl_ctx *ctx =devlink_nl_ctx (info); +struct devlink *devlink = ctx->devlink;struct devlink_rate *rate_node;const struct devlink_ops *ops; +struct devlink *rate_devlink;int err; ops = devlink->ops;
Signature · 1 line
2.44.0
Tariq Toukan
maintainerPATCH v9 7/14 devlink: Allow rate node parents from other devlinks
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
This commit makes use of the building blocks previously added to implement cross-device rate nodes.
A new 'supported_cross_device_rate_nodes' bool is added to devlink_ops which lets drivers advertise support for cross-device rate objects. If enabled and if there is a common shared devlink instance, then:
- all rate objects will be stored in the top-most common nested instance and
- rate objects can have parents from other devices sharing the same common instance.
The parent devlink from info->ctx is not locked, so none of its mutable fields can be used. But parent setting only requires comparing devlink pointer comparisons. Additionally, since the shared devlink is locked, other rate operations cannot concurrently happen.
--- .../networking/devlink/devlink-port.rst | 2 + include/net/devlink.h | 5 + net/devlink/rate.c | 92 +++++++++++++++++-- 3 files changed, 90 insertions(+), 9 deletions(-)
Documentation/networking/devlink/devlink-port.rst +2 -0
@@ -417,6 +417,8 @@ API allows to configure following rate object's parameters: Parent node name. Parent node rate limits are considered as additional limits to all node children limits. ``tx_max`` is an upper limit for children. ``tx_share`` is a total bandwidth distributed among children. + If the device supports cross-function scheduling, the parent can be from a + different function of the same underlying device. ``tc_bw`` Allow users to set the bandwidth allocation per traffic class on rate
include/net/devlink.h +5 -0
@@ -1585,6 +1585,11 @@ struct devlink_ops {struct devlink_rate *parent, 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. +*/ +bool supported_cross_device_rate_nodes;/** * selftests_check() - queries if selftest is supported * @devlink: devlink instance
net/devlink/rate.c +83 -9
157 lines not on this page · raw message
Signature · 1 line
2.44.0
Jakub Kicinski
maintainerRe: [PATCH net-next V9 07/14] devlink: Allow rate node parents from other devlinks
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
devlink: Allow rate node parents from other devlinks
This commit makes use of the building blocks previously added to implement cross-device rate nodes.
include/net/devlink.h struct devlink_ops { · line 1592
Quoted 13 lines
include/net/devlink.h +5
-0@@ -1585,6 +1585,11 @@ struct devlink_ops {struct devlink_rate *parent, 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. +*/ +bool supported_cross_device_rate_nodes;
When a device acting as a cross-device rate parent is destroyed, devl_rate_nodes_destroy() iterates the shared rate_list to detach its children. If it encounters a child rate node belonging to a different device, it invokes the child driver's ops->rate_node_parent_set or ops->rate_leaf_parent_set callbacks.
Since the cleanup path only holds the parent's devlink lock and the shared root devlink lock, does this allow the child driver's rate operations to execute concurrently with other operations on the child device without holding the child's devlink instance lock?
[ ... ]
Quoted 26 lines
net/devlink/rate.c +14
-1@@ -30,19 +30,53 @@ devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info)return devlink_rate ?:ERR_PTR (-ENODEV ); } +/* Repeatedly locks the nested-in devlink instances while cross device rate +* nodes are supported. Returns the devlink instance where rates should be +* stored. +*/ static struct devlink *devl_rate_lock (struct devlink *devlink) { -return devlink; +struct devlink *rate_devlink = devlink; + +while (rate_devlink->ops && + rate_devlink->ops->supported_cross_device_rate_nodes) { + devlink =devlink_nested_in_get_lock (rate_devlink->rel); +if (!devlink) +break ; + rate_devlink = devlink; + } +return rate_devlink; }
[ ... ]
net/devlink/rate.c devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info) · line 80
Quoted 12 lines
net/devlink/rate.c +9
-0+/* Repeatedly unlocks the nested-in devlink instances of 'devlink' while cross +* device nodes are supported. +*/ static void devl_rate_unlock (struct devlink *devlink) { +if (!devlink || !devlink->ops || + !devlink->ops->supported_cross_device_rate_nodes) +return ; + +devl_rate_unlock (devlink_nested_in_get_locked (devlink->rel)); +devlink_nested_in_put_unlock (devlink->rel); }
If a parent devlink is not yet registered, devlink_nested_in_get_lock() can return NULL in devl_rate_lock() and break the loop without taking a lock.
If another thread completes registering the parent devlink concurrently, devlink_nested_in_get_locked() in devl_rate_unlock() will find it.
Could this lead to unlocking a mutex that was never acquired and dropping a reference that was never taken, resulting in a use-after-free or lock corruption?
[ ... ]
Quoted 18 lines
net/devlink/rate.c +2
-1@@ -320,13 +371,14 @@ devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate,struct genl_info *info, struct nlattr *nla_parent) { - struct devlink *devlink = devlink_rate->devlink; + struct devlink *devlink = devlink_rate->devlink, *parent_devlink;const char *parent_name =nla_data (nla_parent);const struct devlink_ops *ops = devlink->ops;size_t len =strlen (parent_name);struct devlink_rate *parent;int err = -EOPNOTSUPP ; + parent_devlink =devlink_nl_ctx (info)->parent_devlink ? : devlink; parent = devlink_rate->parent;if (parent && !len) {@@ -344,7 +396,13 @@ devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate,refcount_dec (&parent->refcnt);
net/devlink/rate.c devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate, · line 405
Quoted 10 lines
net/devlink/rate.c +7
-1devlink_rate->parent =NULL ; }else if (len) { - parent =devlink_rate_node_get_by_name (devlink, parent_name); +/* parent_devlink (when different than devlink) isn't locked, +* but the rate node devlink instance is, so nobody from the +* same group of devices sharing rates could change the used +* fields or unregister the parent. +*/ + parent =devlink_rate_node_get_by_name (parent_devlink, + parent_name);
Is parent_devlink validated to ensure it shares the same common rate root ancestor as devlink?
If an arbitrary unrelated parent_devlink is specified, devlink_rate_node_get_by_name() could traverse an unrelated parent's rate_list that was never locked by devl_rate_lock(), leading to a data race.
Additionally, if a rate node is found, its priv pointer is passed to the target driver's ops->rate_node_parent_set. Could this cause the target driver to cast a foreign priv pointer to its own private struct type, resulting in type confusion and memory corruption?
Re: [PATCH net-next V9 07/14] devlink: Allow rate node parents from other devlinks
Quoted 32 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
devlink: Allow rate node parents from other devlinksThis commit makes use of the building blocks previously added to implement cross-device rate nodes.
Quoted 13 lines
include/net/devlink.h +0
-0@@ -1585,6 +1585,11 @@ struct devlink_ops { struct devlink_rate *parent,void
*priv_child, void*priv_parent,
structnetlink_ext_ack*extack);include/net/devlink.h +2
-0+/* Indicates if cross-device rate nodes are supported. +* This also requires a shared common ancestor object all devices that
include/net/devlink.h struct devlink_ops { · line 1592
Quoted 3 lines
include/net/devlink.h +3
-0+* could share rate nodes are nested in. +*/ +bool supported_cross_device_rate_nodes;When a device acting as a cross-device rate parent is destroyed,
devl_rate_nodes_destroy()iterates the sharedrate_listto detach its children. If it encounters a child rate node belonging to a different device,
it invokes the child driver'sops->rate_node_parent_setorops->rate_leaf_parent_setcallbacks.
Quoted 7 lines
Since the cleanup path only holds the parent's devlink lock and the shared
root devlink lock, does this allow the child driver's rate operations to
execute concurrently with other operations on the child device without holding
the child's devlink instance lock?
The rate_node lock protects changes to the rate hierarchy, even if child devlink instance locks aren't held. The code works by locking everything from the affected instance A up to the instance at the root of the hierarchy R, and affects the hierarchy with R's instance lock held. I think this can be made clearer in the code/comments though, so Will tweak some things in the next version.
Quoted 56 lines
[ ... ]
Quoted 31 lines
net/devlink/rate.c +0
-0@@ -30,19 +30,53 @@ devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info) return devlink_rate ?: ERR_PTR(-ENODEV); }
+/* Repeatedly locks the nested-in devlink instances while cross device rate
+ * nodes are supported. Returns the devlink instance where rates should be
+ * stored.
+ */
static struct devlink *devl_rate_lock(struct devlink *devlink) {net/devlink/rate.c +4
-1-return devlink; +struct devlink *rate_devlink = devlink; + +while (rate_devlink->ops && + rate_devlink->ops-Quoted 1 line
supported_cross_device_rate_nodes) {net/devlink/rate.c +1
-0+ devlink = devlink_nested_in_get_lock(rate_devlink-Quoted 1 line
rel);
net/devlink/rate.c +5
-0+if (!devlink) +break ; + rate_devlink = devlink; + } +return rate_devlink;}
[ ... ]
Quoted 13 lines
+/* Repeatedly unlocks the nested-in devlink instances of 'devlink' while cross
+ * device nodes are supported.
+ */
static voiddevl_rate_unlock(struct devlink *devlink)
{net/devlink/rate.c +5
-0+if (!devlink || !devlink->ops || + !devlink->ops->supported_cross_device_rate_nodes) +return ; + + devl_rate_unlock(devlink_nested_in_get_locked(devlink-Quoted 1 line
rel));
net/devlink/rate.c +1
-0+devlink_nested_in_put_unlock (devlink->rel);Quoted 1 line
}
If a parent devlink is not yet registered,
devlink_nested_in_get_lock()can
return NULL indevl_rate_lock()and break the loop without taking a lock.
Quoted 8 lines
If another thread completes registering the parent devlink concurrently,
devlink_nested_in_get_locked()indevl_rate_unlock()will find it.Could this lead to unlocking a mutex that was never acquired and dropping a
reference that was never taken, resulting in a use-after-free or lock corruption?
A valid concern in theory, even though in practice there won't be another thread registering a shared devlink instance that would race with registration in this way.
I will make these lock/unlock helpers more robust in the next version.
Quoted 50 lines
[ ... ]
Quoted 37 lines
net/devlink/rate.c +1
-1@@ -320,13 +371,14 @@ devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate, struct genl_info *info, struct nlattr *nla_parent) { -struct devlink *devlink = devlink_rate->devlink; +struct devlink *devlink = devlink_rate->devlink, *parent_devlink; const char *parent_name = nla_data(nla_parent); const struct devlink_ops *ops = devlink->ops; size_t len = strlen(parent_name); struct devlink_rate *parent; int err = -EOPNOTSUPP;net/devlink/rate.c +1
-0+ parent_devlink = devlink_nl_ctx(info)->parent_devlink ? :devlink;
parent =devlink_rate->parent;
if (parent && !len) {net/devlink/rate.c +2
-1@@ -344,7 +396,13 @@ devlink_nl_rate_parent_node_set(structdevlink_rate *devlink_rate, refcount_dec(&parent->refcnt); devlink_rate->parent = NULL; } else if (len) { - parent =devlink_rate_node_get_by_name (devlink, parent_name); +/* parent_devlink (when different than devlink) isn't locked, +* but the rate node devlink instance is, so nobody from the
net/devlink/rate.c +1
-0+* same group of devices sharing rates could change the used
net/devlink/rate.c +3
-0+* fields or unregister the parent. +*/ + parent =devlink_rate_node_get_by_name(parent_devlink,
net/devlink/rate.c +1
-0+Quoted 1 line
parent_name);Is
parent_devlinkvalidated to ensure it shares the same common rate root
ancestor as devlink?If an arbitrary unrelated
parent_devlinkis specified,devlink_rate_node_get_by_name()could traverse an unrelated parent'srate_listthat was never locked bydevl_rate_lock(), leading to a data race.
Quoted 7 lines
Additionally, if a rate node is found, its priv pointer is passed to the
target driver'sops->rate_node_parent_set. Could this cause the target driver
to cast a foreign priv pointer to its own private struct type, resulting in
type confusion and memory corruption?
Right, there's no common ancestor validation which could lead to unpleasant results if one tries to set a rate parent across drivers which support cross-esw.
I'll add it in the next version.
Cosmin.
Tariq Toukan
maintainerPATCH v9 8/14 net/mlx5: qos: Use mlx5_lag_query_bond_speed to query LAG speed
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
Previously, the master device of the uplink netdev was queried for its maximum link speed from the QoS layer, requiring the uplink_netdev mutex and possibly the RTNL (if the call originated from the TC matchall layer).
Acquiring these locks here is risky, as lock cycles could form. The locking for the QoS layer is about to change, so to avoid issues, replace the code querying the LAG's max link speed with the existing infrastructure added in commit [1].
This simplifies this part and avoids potential lock cycles. One caveat is that there's a new edge case, when the bond device is not fully formed to represent the LAG device, the speed isn't calculated and is left at 0. This now handled explicitly.
[1] commit f0b2fde98065 ("net/mlx5: Add support for querying bond speed")
--- .../net/ethernet/mellanox/mlx5/core/esw/qos.c | 36 ++++--------------- 1 file changed, 6 insertions(+), 30 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +6 -30
@@ -1489,41 +1489,16 @@ static int esw_qos_node_enable_tc_arbitration(struct mlx5_esw_sched_node *node,return err; } -static u32 mlx5_esw_qos_lag_link_speed_get (struct mlx5_core_dev *mdev, -bool take_rtnl) -{ -struct ethtool_link_ksettings lksettings; -struct net_device *slave, *master; -u32 speed =SPEED_UNKNOWN ; - - slave =mlx5_uplink_netdev_get (mdev); -if (!slave) - goto out; - -if (take_rtnl) -rtnl_lock (); - master =netdev_master_upper_dev_get (slave); -if (master && !__ethtool_get_link_ksettings (master, &lksettings)) - speed = lksettings.base.speed; -if (take_rtnl) -rtnl_unlock (); - -out: -mlx5_uplink_netdev_put (mdev, slave); -return speed; -} -static int mlx5_esw_qos_max_link_speed_get (struct mlx5_core_dev *mdev,u32 *link_speed_max, -bool take_rtnl,struct netlink_ext_ack *extack) {int err; -if (!mlx5_lag_is_active (mdev)) +if (!mlx5_lag_is_active (mdev) || +mlx5_lag_query_bond_speed (mdev, link_speed_max) <0 || + *link_speed_max ==0 ) goto skip_lag; - *link_speed_max =mlx5_esw_qos_lag_link_speed_get (mdev, take_rtnl); -if (*link_speed_max != (u32 )SPEED_UNKNOWN )return 0 ;
@@ -1560,7 +1535,8 @@ int mlx5_esw_qos_modify_vport_rate(struct mlx5_eswitch *esw, u16 vport_num, u32return PTR_ERR (vport);if (rate_mbps) { - err =mlx5_esw_qos_max_link_speed_get (esw->dev, &link_speed_max, false,NULL ); + err =mlx5_esw_qos_max_link_speed_get (esw->dev, &link_speed_max, +NULL );if (err)return err;
@@ -1598,7 +1574,7 @@ static int esw_qos_devlink_rate_to_mbps(struct mlx5_core_dev *mdev, const char *return -EINVAL ; } - err =mlx5_esw_qos_max_link_speed_get (mdev, &link_speed_max, true, extack); + err =mlx5_esw_qos_max_link_speed_get (mdev, &link_speed_max, extack);if (err)return err;
Signature · 1 line
2.44.0
Tariq Toukan
maintainerPATCH v9 9/14 net/mlx5: qos: Expose a function to clear a vport's parent
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
Currently, clearing a vport's parent happens with a call that looks like this:
mlx5_esw_qos_vport_update_parent(vport, NULL, NULL);
Change that to something nicer that looks like this:
mlx5_esw_qos_vport_clear_parent(vport);
--- .../ethernet/mellanox/mlx5/core/esw/devlink_port.c | 2 +- drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c | 11 +++++++++-- drivers/net/ethernet/mellanox/mlx5/core/eswitch.h | 3 +-- 3 files changed, 11 insertions(+), 5 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/esw/devlink_port.c +1 -1
@@ -204,7 +204,7 @@ void mlx5_esw_offloads_devlink_port_unregister(struct mlx5_vport *vport)return ; dl_port = vport->dl_port; -mlx5_esw_qos_vport_update_parent (vport,NULL ,NULL ); +mlx5_esw_qos_vport_clear_parent (vport);devl_rate_leaf_destroy (&dl_port->dl_port);devl_port_unregister (&dl_port->dl_port);
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +9 -2
@@ -1866,8 +1866,10 @@ int mlx5_esw_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv,return 0 ; } -int mlx5_esw_qos_vport_update_parent (struct mlx5_vport *vport,struct mlx5_esw_sched_node *parent, -struct netlink_ext_ack *extack) +static int +mlx5_esw_qos_vport_update_parent (struct mlx5_vport *vport, +struct mlx5_esw_sched_node *parent, +struct netlink_ext_ack *extack) {struct mlx5_eswitch *esw = vport->dev->priv.eswitch;int err =0 ;
@@ -1892,6 +1894,11 @@ int mlx5_esw_qos_vport_update_parent(struct mlx5_vport *vport, struct mlx5_esw_sreturn err; } +void mlx5_esw_qos_vport_clear_parent (struct mlx5_vport *vport) +{ +mlx5_esw_qos_vport_update_parent (vport,NULL ,NULL ); +} + int mlx5_esw_devlink_rate_leaf_parent_set(struct devlink_rate *devlink_rate, struct devlink_rate *parent, void *priv, void *parent_priv,
drivers/net/ethernet/mellanox/mlx5/core/eswitch.h +1 -2
@@ -455,8 +455,7 @@ int mlx5_eswitch_set_vport_trust(struct mlx5_eswitch *esw, u16 vport_num, bool setting);int mlx5_eswitch_set_vport_rate (struct mlx5_eswitch *esw,u16 vport,u32 max_rate,u32 min_rate); -int mlx5_esw_qos_vport_update_parent (struct mlx5_vport *vport,struct mlx5_esw_sched_node *node, -struct netlink_ext_ack *extack); +void mlx5_esw_qos_vport_clear_parent (struct mlx5_vport *vport);int mlx5_eswitch_set_vepa (struct mlx5_eswitch *esw,u8 setting);int mlx5_eswitch_get_vepa (struct mlx5_eswitch *esw,u8 *setting); int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
Signature · 1 line
2.44.0
Tariq Toukan
maintainerPATCH v9 10/14 net/mlx5: qos: Model the root node in the scheduling hierarchy
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
In commit [1] the concept of the root node in the qos hierarchy was removed due to a bug with how tx_share worked. The side effect is that in many places, there are now corner cases related to parent handling. However, since that change, support for tc_bw was added and now, with upcoming cross-esw support, the code is about to become even more complicated, increasing the number of such corner cases.
Bring back the concept of the root node, to which all esw vports and nodes are connected to. This benefits multiple operations which can assume there's always a valid parent and don't have to do ternary gymnastics to determine the correct esw to talk to.
As side effect, there's no longer a need to store the groups in the qos domain, since normalization can simply iterate over all children of the root node. Normalization gets simplified as a result.
There should be no functionality changes as a result of this change.
[1] commit 330f0f6713a3 ("net/mlx5: Remove default QoS group and attach vports directly to root TSAR")
--- .../net/ethernet/mellanox/mlx5/core/esw/qos.c | 194 ++++++++---------- .../net/ethernet/mellanox/mlx5/core/eswitch.h | 3 +- 2 files changed, 84 insertions(+), 113 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +82 -112
@@ -15,8 +15,6 @@struct mlx5_qos_domain {/* Serializes access to all qos changes in the qos domain. */ struct mutex lock; -/* List of all mlx5_esw_sched_nodes. */ -struct list_head nodes; };static void esw_qos_lock (struct mlx5_eswitch *esw)
@@ -43,7 +41,6 @@ static struct mlx5_qos_domain *esw_qos_domain_alloc(void)return NULL ;mutex_init (&qos_domain->lock); -INIT_LIST_HEAD (&qos_domain->nodes);return qos_domain; }
@@ -62,6 +59,7 @@ static void esw_qos_domain_release(struct mlx5_eswitch *esw) }enum sched_node_type { +SCHED_NODE_TYPE_ROOT ,SCHED_NODE_TYPE_VPORTS_TSAR ,SCHED_NODE_TYPE_VPORT ,SCHED_NODE_TYPE_TC_ARBITER_TSAR ,
@@ -106,18 +104,6 @@ struct mlx5_esw_sched_node {u32 tc_bw[DEVLINK_RATE_TCS_MAX ]; }; -static void esw_qos_node_attach_to_parent (struct mlx5_esw_sched_node *node) -{ -if (!node->parent) { -/* Root children are assigned a depth level of 2. */ - node->level =2 ; -list_add_tail (&node->entry, &node->esw->qos.domain->nodes); - }else { - node->level = node->parent->level +1 ; -list_add_tail (&node->entry, &node->parent->children); - } -} -static int esw_qos_num_tcs (struct mlx5_core_dev *dev) {int num_tcs =mlx5_max_tc (dev) +1 ;
@@ -125,14 +111,14 @@ static int esw_qos_num_tcs(struct mlx5_core_dev *dev)return num_tcs <DEVLINK_RATE_TCS_MAX ? num_tcs :DEVLINK_RATE_TCS_MAX ; } -static void -esw_qos_node_set_parent (struct mlx5_esw_sched_node *node,struct mlx5_esw_sched_node *parent) +static void esw_qos_node_set_parent (struct mlx5_esw_sched_node *node, +struct mlx5_esw_sched_node *parent) { -list_del_init (&node->entry); node->parent = parent; -if (parent) - node->esw = parent->esw; -esw_qos_node_attach_to_parent (node); + node->esw = parent->esw; + node->level = parent->level +1 ; +list_del (&node->entry); +list_add_tail (&node->entry, &parent->children); } static void esw_qos_nodes_set_parent(struct list_head *nodes,
@@ -321,22 +307,19 @@ static int esw_qos_create_rate_limit_element(struct mlx5_esw_sched_node *node,return esw_qos_node_create_sched_element (node, sched_ctx, extack); } -static u32 esw_qos_calculate_min_rate_divider (struct mlx5_eswitch *esw, -struct mlx5_esw_sched_node *parent) +static u32 +esw_qos_calculate_min_rate_divider (struct mlx5_esw_sched_node *parent) { -struct list_head *nodes = parent ? &parent->children : &esw->qos.domain->nodes; -u32 fw_max_bw_share =MLX5_CAP_QOS (esw->dev, max_tsar_bw_share); +u32 fw_max_bw_share =MLX5_CAP_QOS (parent->esw->dev, max_tsar_bw_share);struct mlx5_esw_sched_node *node;u32 max_guarantee =0 ;/* Find max min_rate across all nodes. * This will correspond to fw_max_bw_share in the final bw_share calculation. */ -list_for_each_entry (node, nodes, entry) { -if (node->esw == esw && node->ix != esw->qos.root_tsar_ix && - node->min_rate > max_guarantee) +list_for_each_entry (node, &parent->children, entry) +if (node->min_rate > max_guarantee) max_guarantee = node->min_rate; - }if (max_guarantee)return max_t (u32, max_guarantee / fw_max_bw_share,1 );
@@ -368,18 +351,13 @@ static void esw_qos_update_sched_node_bw_share(struct mlx5_esw_sched_node *node,esw_qos_sched_elem_config (node, node->max_rate, bw_share, extack); } -static void esw_qos_normalize_min_rate (struct mlx5_eswitch *esw, -struct mlx5_esw_sched_node *parent, +static void esw_qos_normalize_min_rate (struct mlx5_esw_sched_node *parent,struct netlink_ext_ack *extack) { -struct list_head *nodes = parent ? &parent->children : &esw->qos.domain->nodes; -u32 divider =esw_qos_calculate_min_rate_divider (esw, parent); +u32 divider =esw_qos_calculate_min_rate_divider (parent);struct mlx5_esw_sched_node *node; -list_for_each_entry (node, nodes, entry) { -if (node->esw != esw || node->ix == esw->qos.root_tsar_ix) -continue ; - +list_for_each_entry (node, &parent->children, entry) {/* Vports TC TSARs don't have a minimum rate configured, * so there's no need to update the bw_share on them. */
@@ -391,7 +369,7 @@ static void esw_qos_normalize_min_rate(struct mlx5_eswitch *esw,if (list_empty (&node->children))continue ; -esw_qos_normalize_min_rate (node->esw, node, extack); +esw_qos_normalize_min_rate (node, extack); } }
@@ -412,14 +390,11 @@ static u32 esw_qos_calculate_tc_bw_divider(u32 *tc_bw)static int esw_qos_set_node_min_rate (struct mlx5_esw_sched_node *node,u32 min_rate,struct netlink_ext_ack *extack) { -struct mlx5_eswitch *esw = node->esw; -if (min_rate == node->min_rate)return 0 ; node->min_rate = min_rate; -esw_qos_normalize_min_rate (esw, node->parent, extack); - +esw_qos_normalize_min_rate (node->parent, extack);return 0 ; }
@@ -472,8 +447,7 @@ esw_qos_vport_create_sched_element(struct mlx5_esw_sched_node *vport_node, SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT); attr =MLX5_ADDR_OF (scheduling_context, sched_ctx, element_attributes);MLX5_SET (vport_element, attr, vport_number, vport_node->vport->vport); -MLX5_SET (scheduling_context, sched_ctx, parent_element_id, - parent ? parent->ix : vport_node->esw->qos.root_tsar_ix); +MLX5_SET (scheduling_context, sched_ctx, parent_element_id, parent->ix);MLX5_SET (scheduling_context, sched_ctx, max_average_bw, vport_node->max_rate);
@@ -513,7 +487,7 @@ esw_qos_vport_tc_create_sched_element(struct mlx5_esw_sched_node *vport_tc_node, }static struct mlx5_esw_sched_node * -__esw_qos_alloc_node (struct mlx5_eswitch *esw,u32 tsar_ix,enum sched_node_type type, +__esw_qos_alloc_node (u32 tsar_ix,enum sched_node_type type,struct mlx5_esw_sched_node *parent) {struct mlx5_esw_sched_node *node;
@@ -522,20 +496,12 @@ __esw_qos_alloc_node(struct mlx5_eswitch *esw, u32 tsar_ix, enum sched_node_typeif (!node)return NULL ; - node->esw = esw; node->ix = tsar_ix; node->type = type; - node->parent = parent;INIT_LIST_HEAD (&node->children); -esw_qos_node_attach_to_parent (node); -if (!parent) { -/* The caller is responsible for inserting the node into the -* parent list if necessary. This function can also be used with -* a NULL parent, which doesn't necessarily indicate that it -* refers to the root scheduling element. -*/ -list_del_init (&node->entry); - } +INIT_LIST_HEAD (&node->entry); +if (parent) +esw_qos_node_set_parent (node, parent);return node; }
@@ -570,7 +536,7 @@ static int esw_qos_create_vports_tc_node(struct mlx5_esw_sched_node *parent, SCHEDULING_HIERARCHY_E_SWITCH))return -EOPNOTSUPP ; - vports_tc_node =__esw_qos_alloc_node (parent->esw,0 , + vports_tc_node =__esw_qos_alloc_node (0 ,SCHED_NODE_TYPE_VPORTS_TC_TSAR , parent);if (!vports_tc_node) {
@@ -665,7 +631,6 @@ static int esw_qos_create_tc_arbiter_sched_elem( struct netlink_ext_ack *extack) { u32 tsar_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {}; - u32 tsar_parent_ix; void *attr; if (!mlx5_qos_tsar_type_supported(tc_arbiter_node->esw->dev,
@@ -678,10 +643,8 @@ static int esw_qos_create_tc_arbiter_sched_elem( attr =MLX5_ADDR_OF (scheduling_context, tsar_ctx, element_attributes);MLX5_SET (tsar_element, attr, tsar_type,TSAR_ELEMENT_TSAR_TYPE_TC_ARB ); - tsar_parent_ix = tc_arbiter_node->parent ? tc_arbiter_node->parent->ix : - tc_arbiter_node->esw->qos.root_tsar_ix;MLX5_SET (scheduling_context, tsar_ctx, parent_element_id, - tsar_parent_ix); + tc_arbiter_node->parent->ix);MLX5_SET (scheduling_context, tsar_ctx, element_type,SCHEDULING_CONTEXT_ELEMENT_TYPE_TSAR ); MLX5_SET(scheduling_context, tsar_ctx, max_average_bw,
@@ -694,37 +657,36 @@ static int esw_qos_create_tc_arbiter_sched_elem( }static struct mlx5_esw_sched_node * -__esw_qos_create_vports_sched_node (struct mlx5_eswitch *esw,struct mlx5_esw_sched_node *parent, +__esw_qos_create_vports_sched_node (struct mlx5_esw_sched_node *parent,struct netlink_ext_ack *extack) {struct mlx5_esw_sched_node *node; -u32 tsar_ix;int err; +u32 ix; - err =esw_qos_create_node_sched_elem (esw->dev, esw->qos.root_tsar_ix,0 , -0 , &tsar_ix); + err =esw_qos_create_node_sched_elem (parent->esw->dev, parent->ix,0 ,0 , + &ix);if (err) {NL_SET_ERR_MSG_MOD (extack,"E-Switch create TSAR for node failed" );return ERR_PTR (err); } - node =__esw_qos_alloc_node (esw, tsar_ix,SCHED_NODE_TYPE_VPORTS_TSAR , parent); + node =__esw_qos_alloc_node (ix,SCHED_NODE_TYPE_VPORTS_TSAR , parent);if (!node) {NL_SET_ERR_MSG_MOD (extack,"E-Switch alloc node failed" ); err = -ENOMEM ; goto err_alloc_node; } -list_add_tail (&node->entry, &esw->qos.domain->nodes); -esw_qos_normalize_min_rate (esw,NULL , extack); -trace_mlx5_esw_node_qos_create (esw->dev, node, node->ix); +esw_qos_normalize_min_rate (parent, extack); +trace_mlx5_esw_node_qos_create (parent->esw->dev, node, node->ix);return node; err_alloc_node: -if (mlx5_destroy_scheduling_element_cmd (esw->dev, +if (mlx5_destroy_scheduling_element_cmd (parent->esw->dev,SCHEDULING_HIERARCHY_E_SWITCH , - tsar_ix)) + ix))NL_SET_ERR_MSG_MOD (extack,"E-Switch destroy TSAR for node failed" );return ERR_PTR (err); }
@@ -746,7 +708,7 @@ esw_qos_create_vports_sched_node(struct mlx5_eswitch *esw, struct netlink_ext_acif (err)return ERR_PTR (err); - node =__esw_qos_create_vports_sched_node (esw,NULL , extack); + node =__esw_qos_create_vports_sched_node (esw->qos.root, extack);if (IS_ERR (node))esw_qos_put (esw);
@@ -762,38 +724,47 @@ static void __esw_qos_destroy_node(struct mlx5_esw_sched_node *node, struct netltrace_mlx5_esw_node_qos_destroy (esw->dev, node, node->ix);esw_qos_destroy_node (node, extack); -esw_qos_normalize_min_rate (esw,NULL , extack); +esw_qos_normalize_min_rate (esw->qos.root, extack); }static int esw_qos_create (struct mlx5_eswitch *esw,struct netlink_ext_ack *extack) {struct mlx5_core_dev *dev = esw->dev; +struct mlx5_esw_sched_node *root; +u32 root_ix;int err;if (!MLX5_CAP_GEN (dev, qos) || !MLX5_CAP_QOS (dev, esw_scheduling))return -EOPNOTSUPP ; - err =esw_qos_create_node_sched_elem (esw->dev,0 ,0 ,0 , - &esw->qos.root_tsar_ix); + err =esw_qos_create_node_sched_elem (esw->dev,0 ,0 ,0 , &root_ix);if (err) {esw_warn (dev,"E-Switch create root TSAR failed (%d)\n" , err);return err; } + root =__esw_qos_alloc_node (root_ix,SCHED_NODE_TYPE_ROOT ,NULL ); +if (!root) { +esw_warn (dev,"E-Switch create root node failed\n" ); + err = -ENOMEM ; + goto out_err; + } + root->esw = esw; + root->level =1 ; + esw->qos.root = root;refcount_set (&esw->qos.refcnt,1 );return 0 ; +out_err: +mlx5_destroy_scheduling_element_cmd (dev,SCHEDULING_HIERARCHY_E_SWITCH , + root_ix); +return err; }static void esw_qos_destroy (struct mlx5_eswitch *esw) { -int err; - - err =mlx5_destroy_scheduling_element_cmd (esw->dev, -SCHEDULING_HIERARCHY_E_SWITCH , - esw->qos.root_tsar_ix); -if (err) -esw_warn (esw->dev,"E-Switch destroy root TSAR failed (%d)\n" , err); +esw_qos_destroy_node (esw->qos.root,NULL ); + esw->qos.root =NULL ; }static int esw_qos_get (struct mlx5_eswitch *esw,struct netlink_ext_ack *extack)
@@ -866,8 +837,7 @@ esw_qos_create_vport_tc_sched_node(struct mlx5_vport *vport,u8 tc = vports_tc_node->tc;int err; - vport_tc_node =__esw_qos_alloc_node (vport_node->esw,0 , -SCHED_NODE_TYPE_VPORT_TC , + vport_tc_node =__esw_qos_alloc_node (0 ,SCHED_NODE_TYPE_VPORT_TC , vports_tc_node);if (!vport_tc_node)return -ENOMEM ;
@@ -959,7 +929,7 @@ esw_qos_vport_tc_enable(struct mlx5_vport *vport, enum sched_node_type type,/* Increase the parent's level by 2 to account for both the * TC arbiter and the vports TC scheduling element. */ - new_level = (parent ? parent->level :2 ) +2 ; + new_level = parent->level +2 ; max_level =1 <<MLX5_CAP_QOS (vport_node->esw->dev, log_esw_max_sched_depth);if (new_level > max_level) {
@@ -997,7 +967,7 @@ esw_qos_vport_tc_enable(struct mlx5_vport *vport, enum sched_node_type type, err_sched_nodes:if (type ==SCHED_NODE_TYPE_RATE_LIMITER ) {esw_qos_node_destroy_sched_element (vport_node,NULL ); -esw_qos_node_attach_to_parent (vport_node); +esw_qos_node_set_parent (vport_node, vport_node->parent); }else {esw_qos_tc_arbiter_scheduling_teardown (vport_node,NULL ); }
@@ -1055,7 +1025,7 @@ static void esw_qos_vport_disable(struct mlx5_vport *vport, struct netlink_ext_a vport_node->bw_share =0 ;memset (vport_node->tc_bw,0 ,sizeof (vport_node->tc_bw));list_del_init (&vport_node->entry); -esw_qos_normalize_min_rate (vport_node->esw, vport_node->parent, extack); +esw_qos_normalize_min_rate (vport_node->parent, extack);trace_mlx5_esw_vport_qos_destroy (vport_node->esw->dev, vport); }
@@ -1068,7 +1038,7 @@ static int esw_qos_vport_enable(struct mlx5_vport *vport,struct mlx5_esw_sched_node *vport_node = vport->qos.sched_node;int err; -esw_assert_qos_lock_held (vport->dev->priv.eswitch); +esw_assert_qos_lock_held (vport_node->esw);esw_qos_node_set_parent (vport_node, parent);if (type ==SCHED_NODE_TYPE_VPORT )
@@ -1079,7 +1049,7 @@ static int esw_qos_vport_enable(struct mlx5_vport *vport,return err; vport_node->type = type; -esw_qos_normalize_min_rate (vport_node->esw, parent, extack); +esw_qos_normalize_min_rate (parent, extack);trace_mlx5_esw_vport_qos_create (vport->dev, vport, vport_node->max_rate, vport_node->bw_share);
@@ -1092,7 +1062,6 @@ static int mlx5_esw_qos_vport_enable(struct mlx5_vport *vport, enum sched_node_t {struct mlx5_eswitch *esw = vport->dev->priv.eswitch;struct mlx5_esw_sched_node *sched_node; -struct mlx5_eswitch *parent_esw;int err;esw_assert_qos_lock_held (esw);
@@ -1100,14 +1069,13 @@ static int mlx5_esw_qos_vport_enable(struct mlx5_vport *vport, enum sched_node_tif (err)return err; - parent_esw = parent ? parent->esw : esw; - sched_node =__esw_qos_alloc_node (parent_esw,0 , type, parent); +if (!parent) + parent = esw->qos.root; + sched_node =__esw_qos_alloc_node (0 , type, parent);if (!sched_node) {esw_qos_put (esw);return -ENOMEM ; } -if (!parent) -list_add_tail (&sched_node->entry, &esw->qos.domain->nodes); sched_node->max_rate = max_rate; sched_node->min_rate = min_rate;
@@ -1147,7 +1115,8 @@ void mlx5_esw_qos_vport_disable(struct mlx5_vport *vport) goto unlock; parent = vport->qos.sched_node->parent; -WARN (parent,"Disabling QoS on port before detaching it from node" ); +WARN (parent != esw->qos.root, +"Disabling QoS on port before detaching it from node" );mlx5_esw_qos_vport_disable_locked (vport); unlock:
@@ -1319,11 +1288,9 @@ static int esw_qos_switch_tc_arbiter_node_to_vports(struct mlx5_esw_sched_node *node, struct netlink_ext_ack *extack) { - u32 parent_tsar_ix = node->parent ? - node->parent->ix : node->esw->qos.root_tsar_ix; int err; - err =esw_qos_create_node_sched_elem (node->esw->dev, parent_tsar_ix, + err =esw_qos_create_node_sched_elem (node->esw->dev, node->parent->ix, node->max_rate, node->bw_share, &node->ix);if (err) {
@@ -1378,8 +1345,8 @@ esw_qos_move_node(struct mlx5_esw_sched_node *curr_node) {struct mlx5_esw_sched_node *new_node; - new_node =__esw_qos_alloc_node (curr_node->esw, curr_node->ix, - curr_node->type,NULL ); + new_node =__esw_qos_alloc_node (curr_node->ix, curr_node->type, + curr_node->parent);if (!new_node)return ERR_PTR (-ENOMEM );
@@ -1888,7 +1855,9 @@ mlx5_esw_qos_vport_update_parent(struct mlx5_vport *vport, err =mlx5_esw_qos_vport_enable (vport, type, parent,0 ,0 , extack); }else if (vport->qos.sched_node) { - err =esw_qos_vport_update_parent (vport, parent, extack); + err =esw_qos_vport_update_parent (vport, + parent ? : esw->qos.root, + extack); }esw_qos_unlock (esw);return err;
@@ -1941,7 +1910,7 @@ mlx5_esw_qos_node_validate_set_parent(struct mlx5_esw_sched_node *node, {u8 new_level, max_level; -if (parent && parent->esw != node->esw) { +if (parent->esw != node->esw) {NL_SET_ERR_MSG_MOD (extack,"Cannot assign node to another E-Switch" );return -EOPNOTSUPP ;
@@ -1953,13 +1922,13 @@ mlx5_esw_qos_node_validate_set_parent(struct mlx5_esw_sched_node *node,return -EOPNOTSUPP ; } -if (parent && parent->type ==SCHED_NODE_TYPE_TC_ARBITER_TSAR ) { +if (parent->type ==SCHED_NODE_TYPE_TC_ARBITER_TSAR ) {NL_SET_ERR_MSG_MOD (extack,"Cannot attach a node to a parent with TC bandwidth configured" );return -EOPNOTSUPP ; } - new_level = parent ? parent->level +1 :2 ; + new_level = parent->level +1 ;if (node->type ==SCHED_NODE_TYPE_TC_ARBITER_TSAR ) {/* Increase by one to account for the vports TC scheduling * element.
@@ -2010,14 +1979,12 @@ static int esw_qos_vports_node_update_parent(struct mlx5_esw_sched_node *node, {struct mlx5_esw_sched_node *curr_parent = node->parent;struct mlx5_eswitch *esw = node->esw; -u32 parent_ix;int err; - parent_ix = parent ? parent->ix : node->esw->qos.root_tsar_ix;mlx5_destroy_scheduling_element_cmd (esw->dev,SCHEDULING_HIERARCHY_E_SWITCH , node->ix); - err =esw_qos_create_node_sched_elem (esw->dev, parent_ix, + err =esw_qos_create_node_sched_elem (esw->dev, parent->ix, node->max_rate,0 , &node->ix);if (err) { NL_SET_ERR_MSG_MOD(extack,
@@ -2044,12 +2011,15 @@ static int mlx5_esw_qos_node_update_parent(struct mlx5_esw_sched_node *node,struct mlx5_eswitch *esw = node->esw;int err; +esw_qos_lock (esw); + curr_parent = node->parent; +if (!parent) + parent = esw->qos.root; + err =mlx5_esw_qos_node_validate_set_parent (node, parent, extack);if (err) -return err; + goto out; -esw_qos_lock (esw); - curr_parent = node->parent;if (node->type ==SCHED_NODE_TYPE_TC_ARBITER_TSAR ) { err =esw_qos_tc_arbiter_node_update_parent (node, parent, extack);
@@ -2060,8 +2030,8 @@ static int mlx5_esw_qos_node_update_parent(struct mlx5_esw_sched_node *node,if (err) goto out; -esw_qos_normalize_min_rate (esw, curr_parent, extack); -esw_qos_normalize_min_rate (esw, parent, extack); +esw_qos_normalize_min_rate (curr_parent, extack); +esw_qos_normalize_min_rate (parent, extack); out:esw_qos_unlock (esw);
drivers/net/ethernet/mellanox/mlx5/core/eswitch.h +2 -1
@@ -390,8 +390,9 @@ struct mlx5_eswitch {struct {/* Initially 0, meaning no QoS users and QoS is disabled. */ refcount_t refcnt; -u32 root_tsar_ix;struct mlx5_qos_domain *domain; +/* The root node of the hierarchy. */ +struct mlx5_esw_sched_node *root; } qos;struct mlx5_esw_bridge_offloads *br_offloads;
Signature · 1 line
2.44.0
Tariq Toukan
maintainerPATCH v9 11/14 net/mlx5: qos: Remove qos domains and use shd lock
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
E-Switch QoS domains were added with the intention of eventually implementing shared qos domains to support cross-esw scheduling in the previous approach ([1]), but they are no longer necessary in the new approach.
Remove QoS domains and switch to using the shd lock for protecting against concurrent QoS modifications.
Enable the supported_cross_device_rate_nodes devink ops attribute so that all calls originating from devlink rate acquire the shd lock. Only the additional entry points into QoS need to acquire the shd lock.
Enabling supported_cross_device_rate_nodes now is safe, because mlx5_esw_qos_vport_update_parent rejects cross-esw parent updates. This will change in the next patch.
[1] https://lore.kernel.org/netdev/[email protected]/
--- .../net/ethernet/mellanox/mlx5/core/devlink.c | 1 + .../net/ethernet/mellanox/mlx5/core/esw/qos.c | 186 ++++-------------- .../net/ethernet/mellanox/mlx5/core/esw/qos.h | 3 - .../net/ethernet/mellanox/mlx5/core/eswitch.c | 8 - .../net/ethernet/mellanox/mlx5/core/eswitch.h | 9 +- 5 files changed, 45 insertions(+), 162 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/devlink.c +1 -0
@@ -385,6 +385,7 @@ static const struct devlink_ops mlx5_devlink_ops = { .rate_node_del = mlx5_esw_devlink_rate_node_del, .rate_leaf_parent_set = mlx5_esw_devlink_rate_leaf_parent_set, .rate_node_parent_set = mlx5_esw_devlink_rate_node_parent_set, + .supported_cross_device_rate_nodes = true,#endif #ifdef CONFIG_MLX5_SF_MANAGER .port_new = mlx5_devlink_sf_port_new,
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +40 -146
411 lines not on this page · raw message
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.h +0 -3
@@ -6,9 +6,6 @@#ifdef CONFIG_MLX5_ESWITCH -int mlx5_esw_qos_init (struct mlx5_eswitch *esw); -void mlx5_esw_qos_cleanup (struct mlx5_eswitch *esw); -int mlx5_esw_qos_set_vport_rate (struct mlx5_vport *evport,u32 max_rate,u32 min_rate);bool mlx5_esw_qos_get_vport_rate (struct mlx5_vport *vport,u32 *max_rate,u32 *min_rate);void mlx5_esw_qos_vport_disable (struct mlx5_vport *vport);
drivers/net/ethernet/mellanox/mlx5/core/eswitch.c +0 -8
@@ -1647,10 +1647,6 @@ int mlx5_eswitch_enable_locked(struct mlx5_eswitch *esw, int num_vfs)MLX5_NB_INIT (&esw->nb, eswitch_vport_event,NIC_VPORT_CHANGE );mlx5_eq_notifier_register (esw->dev, &esw->nb); - err =mlx5_esw_qos_init (esw); -if (err) - goto err_esw_init; -if (esw->mode ==MLX5_ESWITCH_LEGACY ) { err =esw_legacy_enable (esw); }else {
@@ -2057,9 +2053,6 @@ int mlx5_eswitch_init(struct mlx5_core_dev *dev) goto reps_err; esw->mode =MLX5_ESWITCH_LEGACY ; - err =mlx5_esw_qos_init (esw); -if (err) - goto reps_err;mutex_init (&esw->offloads.encap_tbl_lock);hash_init (esw->offloads.encap_tbl);
@@ -2109,7 +2102,6 @@ void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw)esw_info (esw->dev,"cleanup\n" ); -mlx5_esw_qos_cleanup (esw);destroy_workqueue (esw->work_queue);WARN_ON (refcount_read (&esw->qos.refcnt));mutex_destroy (&esw->state_lock);
drivers/net/ethernet/mellanox/mlx5/core/eswitch.h +4 -5
@@ -224,8 +224,9 @@ struct mlx5_vport {struct mlx5_vport_info info; -/* Protected with the E-Switch qos domain lock. The Vport QoS can -* either be disabled (sched_node is NULL) or in one of three states: +/* Protected by mlx5_shd_lock(). +* The Vport QoS can either be disabled (sched_node is NULL) or in one +* of three states: * 1. Regular QoS (sched_node is a vport node). * 2. TC QoS enabled on the vport (sched_node is a TC arbiter). * 3. TC QoS enabled on the vport's parent node
@@ -359,7 +360,6 @@ enum { };struct dentry ; -struct mlx5_qos_domain ;struct mlx5_eswitch {struct mlx5_core_dev *dev;
@@ -386,11 +386,10 @@ struct mlx5_eswitch {struct rw_semaphore mode_lock;atomic64_t user_count; -/* Protected with the E-Switch qos domain lock. */ +/* QoS changes are serialized with mlx5_shd_lock(). */ struct {/* Initially 0, meaning no QoS users and QoS is disabled. */ refcount_t refcnt; -struct mlx5_qos_domain *domain;/* The root node of the hierarchy. */ struct mlx5_esw_sched_node *root; } qos;
Signature · 1 line
2.44.0
Jakub Kicinski
maintainerRe: [PATCH net-next V9 11/14] net/mlx5: qos: Remove qos domains and use shd lock
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net/mlx5: qos: Remove qos domains and use shd lock
This commit removes QoS domains and switches to using the shd lock to protect against concurrent QoS modifications. It also enables the supported_cross_device_rate_nodes devlink ops attribute.
drivers/net/ethernet/mellanox/mlx5/core/devlink.c static const struct devlink_ops mlx5_devlink_ops = { · line 388
Quoted 9 lines
drivers/net/ethernet/mellanox/mlx5/core/devlink.c +1
-0@@ -385,6 +385,7 @@ static const struct devlink_ops mlx5_devlink_ops = { .rate_node_del = mlx5_esw_devlink_rate_node_del, .rate_leaf_parent_set = mlx5_esw_devlink_rate_leaf_parent_set, .rate_node_parent_set = mlx5_esw_devlink_rate_node_parent_set, + .supported_cross_device_rate_nodes = true,
The commit message states that enabling supported_cross_device_rate_nodes now is safe because mlx5_esw_qos_vport_update_parent rejects cross-esw parent updates.
When a user provides a DEVLINK_ATTR_PARENT_DEV belonging to an unrelated device, the devlink core (devlink_nl_rate_parent_node_set) drops the lock on the parent devlink and calls devlink_rate_node_get_by_name(). This function traverses the rate_list of the unlocked parent devlink instance.
Does this leave the devlink core exposed to data races and potential use-after-free vulnerabilities, since the driver's rejection callback executes only after this lockless list traversal has occurred?
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c void mlx5_esw_qos_vport_disable(struct mlx5_vport *vport) · line 1071
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +1
-1@@ -1110,7 +1068,7 @@ void mlx5_esw_qos_vport_disable(struct mlx5_vport *vport)struct mlx5_esw_sched_node *parent;lockdep_assert_held (&esw->state_lock); -esw_qos_lock (esw); +devl_lock (esw->dev->shd);
Could esw->dev->shd be NULL here?
In mlx5_shd_init(), initialization is explicitly skipped for non-PF devices, meaning Sub-Functions (SFs) acting as E-Switch managers will have a NULL shd pointer. If the device lacks VPD data, pci_vpd_alloc() returns -ENODEV, causing mlx5_shd_init() to return 0 but leaving dev->shd as NULL.
When QoS operations are attempted on these devices, calling devl_lock() dereferences the pointer, which could cause an immediate NULL pointer dereference and kernel panic.
Additionally, this acquires the devlink lock while already holding esw->state_lock, establishing the lock order:state_lock -> devlink->lock.
Conversely, the devlink core invokes driver callbacks with the devlink instance lock held, and the driver subsequently acquires esw->state_lock inside these callbacks, creating the reverse order:devlink->lock -> state_lock.
Does this introduce a circular locking dependency splat between state_lock and the devlink instance lock?
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c int mlx5_esw_qos_set_vport_rate(struct mlx5_vport *vport, u32 max_rate, u32 min_ · line 1120
Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +1
-1@@ -1159,26 +1117,25 @@ int mlx5_esw_qos_set_vport_rate(struct mlx5_vport *vport, u32 max_rate, u32 min_struct mlx5_eswitch *esw = vport->dev->priv.eswitch;int err; -esw_qos_lock (esw); +devl_lock (esw->dev->shd);
This function is invoked by mlx5_eswitch_set_vport_rate(), which acts as the handler for the ndo_set_vf_rate netdev callback. The core network stack executes ndo_set_vf_rate with the rtnl_lock held, establishing the lock
order: rtnl_lock -> devlink->lock.
However, the devlink subsystem enforces the opposite locking order (devlink->lock -> rtnl_lock). Because all devlink locks share the same lockdep class, does this inversion trigger a circular locking dependency lockdep splat and pose an ABBA deadlock risk?
[ ... ]
Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +0
-1@@ -1859,13 +1763,15 @@ mlx5_esw_qos_vport_update_parent(struct mlx5_vport *vport, parent ? : esw->qos.root, extack); } -esw_qos_unlock (esw);
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c mlx5_esw_qos_vport_update_parent(struct mlx5_vport *vport, · line 1775
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +3
-0+return err; }void mlx5_esw_qos_vport_clear_parent (struct mlx5_vport *vport) { +devl_lock (vport->dev->shd);mlx5_esw_qos_vport_update_parent (vport,NULL ,NULL ); +devl_unlock (vport->dev->shd); }
During device teardown, execution paths such as mlx5_unload_one_devl_locked() hold the primary devlink instance lock. This path invokes mlx5_eswitch_unload_vf_vports() -> mlx5_esw_offloads_devlink_port_unregister() -> mlx5_esw_qos_vport_clear_parent().
Because both the primary devlink instance lock and the shd devlink instance lock are initialized from the same site (devlink_alloc), they share the same lockdep class.
Does calling devl_lock() directly (instead of using the devlink core's mutex_lock_nested capability) while a lock of the same class is already held trigger a recursive locking lockdep warning?
Re: [PATCH net-next V9 11/14] net/mlx5: qos: Remove qos domains and use shd lock
Quoted 40 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net/mlx5: qos: Remove qos domains and use shd lockThis commit removes QoS domains and switches to using the shd lock to protect against concurrent QoS modifications. It also enables the
supported_cross_device_rate_nodesdevlink ops attribute.Link:
https://lore.kernel.org/netdev/[email protected]/drivers/net/ethernet/mellanox/mlx5/core/devlink.c static const struct devlink_ops mlx5_devlink_ops = { · line 388
Quoted 13 lines
drivers/net/ethernet/mellanox/mlx5/core/devlink.c +0
-0@@ -385,6 +385,7 @@ static const struct devlink_ops mlx5_devlink_ops = { .rate_node_del = mlx5_esw_devlink_rate_node_del,.rate_leaf_parent_set =
mlx5_esw_devlink_rate_leaf_parent_set,
.rate_node_parent_set =mlx5_esw_devlink_rate_node_parent_set,drivers/net/ethernet/mellanox/mlx5/core/devlink.c +1
-0+ .supported_cross_device_rate_nodes = true,The commit message states that enabling
supported_cross_device_rate_nodes
now is safe becausemlx5_esw_qos_vport_update_parentrejects cross- esw
parent updates.When a user provides a
DEVLINK_ATTR_PARENT_DEVbelonging to an unrelated
device, the devlink core (devlink_nl_rate_parent_node_set) drops the lock on
the parent devlink and callsdevlink_rate_node_get_by_name(). This function
traverses therate_listof the unlocked parent devlink instance.
Quoted 3 lines
Does this leave the devlink core exposed to data races and potential use-after-free vulnerabilities, since the driver's rejection callback executes only after this lockless list traversal has occurred?
I will address this by adding common ancestor validation in devlink rate core, which was missing.
Quoted 24 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c
Quoted 13 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +0
-0@@ -1110,7 +1068,7 @@ void mlx5_esw_qos_vport_disable(struct mlx5_vport *vport) struct mlx5_esw_sched_node *parent;
lockdep_assert_held(&esw->state_lock);drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +1
-1-esw_qos_lock (esw); +devl_lock (esw->dev->shd);Could
esw->dev->shdbe NULL here?In
mlx5_shd_init(), initialization is explicitly skipped for non-PF devices,
meaning Sub-Functions (SFs) acting as E-Switch managers will have a NULL
shd pointer. If the device lacks VPD data,pci_vpd_alloc()returns - ENODEV,
causingmlx5_shd_init()to return 0 but leavingdev->shdas NULL.
Quoted 4 lines
When QoS operations are attempted on these devices, calling
devl_lock()
dereferences the pointer, which could cause an immediate NULL pointer dereference and kernel panic.
Right. This is a race with Jiri's fix ([1]), which changed the assumption this code was architected with that dev->shd is always initialized. That is no longer the case, and the consequence is that this needs to 1) not unconditionally use dev->shd and 2) use a fallback lock when it isn't available.
Quoted 8 lines
Additionally, this acquires the devlink lock while already holding
esw->state_lock, establishing the lock order:state_lock->devlink->lock.Conversely, the devlink core invokes driver callbacks with the devlink
instance lock held, and the driver subsequently acquires esw-
Quoted 7 lines
Quoted 1 line
state_lockinside these callbacks, creating the reverse order:
devlink->lock->state_lock.Does this introduce a circular locking dependency splat between
state_lock
and the devlink instance lock?
This claim is based on the incorrect assumption that devlink instances share the same lock class, while in fact each is unique. The established lock order is devlink->lock -> state_lock and this doesn't conflict with state_lock -> shd->lock.
Quoted 17 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c
Quoted 8 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +0
-0@@ -1159,26 +1117,25 @@ int mlx5_esw_qos_set_vport_rate(struct
mlx5_vport*vport, u32max_rate, u32min_
structmlx5_eswitch*esw =vport->dev->priv.eswitch; int err;drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +1
-1-esw_qos_lock (esw); +devl_lock (esw->dev->shd);This function is invoked by
mlx5_eswitch_set_vport_rate(), which acts as the
handler for thendo_set_vf_ratenetdev callback. The core network stack
executesndo_set_vf_ratewith thertnl_lockheld, establishing the lock
order:rtnl_lock->devlink->lock.
Quoted 6 lines
However, the devlink subsystem enforces the opposite locking order (
devlink->lock->rtnl_lock). Because all devlink locks share the same
lockdep class, does this inversion trigger a circular locking dependency
lockdep splat and pose an ABBA deadlock risk?
Same as above, there's no ABBA here since devlink instances use different lock classes.
Perhaps Sashiko (I think) could be taught this to reduce false positives?
Quoted 34 lines
[ ... ]
Quoted 16 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +0
-0@@ -1859,13 +1763,15 @@ mlx5_esw_qos_vport_update_parent(struct mlx5_vport *vport,parent ? : esw-
Quoted 1 line
qos.root,
extack); }
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +1
-1-esw_qos_unlock (esw); +return err;
}
voidmlx5_esw_qos_vport_clear_parent(struct mlx5_vport *vport){drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +1
-0+devl_lock (vport->dev->shd);
mlx5_esw_qos_vport_update_parent(vport, NULL, NULL);drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +1
-0+devl_unlock (vport->dev->shd);Quoted 1 line
}
During device teardown, execution paths such as
mlx5_unload_one_devl_locked()
hold the primary devlink instance lock. This path invokesmlx5_eswitch_unload_vf_vports()->mlx5_esw_offloads_devlink_port_unregister()
->mlx5_esw_qos_vport_clear_parent().Because both the primary devlink instance lock and the shd devlink instance
lock are initialized from the same site (devlink_alloc), they share the same
lockdep class.
Quoted 5 lines
Does calling
devl_lock()directly (instead of using the devlink core'smutex_lock_nestedcapability) while a lock of the same class is already held
trigger a recursive locking lockdep warning?
Again, same false positive. Devlinks use unique lock classes.
Cosmin.
Re: [PATCH net-next V9 11/14] net/mlx5: qos: Remove qos domains and use shd lock
Quoted 84 lines · Cosmin Ratiu
Quoted 43 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
---
net/mlx5: qos: Remove qos domains and use shd lockThis commit removes QoS domains and switches to using the shd lock to
protect against concurrent QoS modifications. It also enables thesupported_cross_device_rate_nodesdevlink ops attribute.Link:
https://lore.kernel.org/netdev/[email protected]/drivers/net/ethernet/mellanox/mlx5/core/devlink.c static const struct devlink_ops mlx5_devlink_ops = { · line 388
Quoted 13 lines
drivers/net/ethernet/mellanox/mlx5/core/devlink.c +0
-0@@ -385,6 +385,7 @@ static const struct devlink_ops mlx5_devlink_ops = { .rate_node_del = mlx5_esw_devlink_rate_node_del,.rate_leaf_parent_set =
mlx5_esw_devlink_rate_leaf_parent_set,
.rate_node_parent_set =mlx5_esw_devlink_rate_node_parent_set,drivers/net/ethernet/mellanox/mlx5/core/devlink.c +1
-0+ .supported_cross_device_rate_nodes = true,The commit message states that enabling
supported_cross_device_rate_nodes
now is safe becausemlx5_esw_qos_vport_update_parentrejects cross- esw
parent updates.When a user provides a
DEVLINK_ATTR_PARENT_DEVbelonging to an unrelated
device, the devlink core (devlink_nl_rate_parent_node_set) drops the
lock on
the parent devlink and callsdevlink_rate_node_get_by_name(). This function
traverses therate_listof the unlocked parent devlink instance.Quoted 5 lines
Does this leave the devlink core exposed to data races and potential
use-after-free vulnerabilities, since the driver's rejection callback
executes only after this lockless list traversal has occurred?I will address this by adding common ancestor validation in devlink rate core, which was missing.
Quoted 25 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c
Quoted 13 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +0
-0@@ -1110,7 +1068,7 @@ void mlx5_esw_qos_vport_disable(struct mlx5_vport *vport) struct mlx5_esw_sched_node *parent;
lockdep_assert_held(&esw->state_lock);drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +1
-1-esw_qos_lock (esw); +devl_lock (esw->dev->shd);Could
esw->dev->shdbe NULL here?In
mlx5_shd_init(), initialization is explicitly skipped for non-PF devices,
meaning Sub-Functions (SFs) acting as E-Switch managers will have a NULL
shd pointer. If the device lacks VPD data,pci_vpd_alloc()returns -
ENODEV,
causingmlx5_shd_init()to return 0 but leavingdev->shdas NULL.Quoted 5 lines
When QoS operations are attempted on these devices, calling
devl_lock()
dereferences the pointer, which could cause an immediate NULL pointer
dereference and kernel panic.
Quoted 6 lines
Right. This is a race with Jiri's fix ([1]), which changed the assumption this code was architected with that
dev->shdis always initialized. That is no longer the case, and the consequence is that this needs to 1) not unconditionally usedev->shdand 2) use a fallback
lock when it isn't available.
Tariq Toukan
maintainerPATCH v9 12/14 net/mlx5: qos: Support cross-device tx scheduling
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
Up to now, rate groups could only contain vports from the same E-Switch. This patch relaxes that restriction if the device supports it (HCA_CAP.esw_cross_esw_sched == true) and the right conditions are met:
- Link Aggregation (LAG) is enabled.
- The E-Switches are from the same shared devlink device.
--- .../net/ethernet/mellanox/mlx5/core/esw/qos.c | 117 +++++++++++++----- 1 file changed, 83 insertions(+), 34 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +83 -34
@@ -50,7 +50,9 @@ struct mlx5_esw_sched_node {enum sched_node_type type;/* The eswitch this node belongs to. */ struct mlx5_eswitch *esw; -/* The children nodes of this node, empty list for leaf nodes. */ +/* The children nodes of this node, empty list for leaf nodes. +* Can be from multiple E-Switches. +*/ struct list_head children;/* Valid only if this node is associated with a vport. */ struct mlx5_vport *vport;
@@ -393,6 +395,7 @@ esw_qos_vport_create_sched_element(struct mlx5_esw_sched_node *vport_node,struct mlx5_esw_sched_node *parent = vport_node->parent;u32 sched_ctx[MLX5_ST_SZ_DW (scheduling_context)] = {};struct mlx5_core_dev *dev = vport_node->esw->dev; +struct mlx5_vport *vport = vport_node->vport;void *attr; if (!mlx5_qos_element_type_supported(
@@ -404,10 +407,17 @@ esw_qos_vport_create_sched_element(struct mlx5_esw_sched_node *vport_node,MLX5_SET (scheduling_context, sched_ctx, element_type,SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT ); attr =MLX5_ADDR_OF (scheduling_context, sched_ctx, element_attributes); -MLX5_SET (vport_element, attr, vport_number, vport_node->vport->vport); +MLX5_SET (vport_element, attr, vport_number, vport->vport);MLX5_SET (scheduling_context, sched_ctx, parent_element_id, parent->ix);MLX5_SET (scheduling_context, sched_ctx, max_average_bw, vport_node->max_rate); +if (vport->dev != dev) { +/* The port is assigned to a node on another eswitch. */ +MLX5_SET (vport_element, attr, eswitch_owner_vhca_id_valid, + true); +MLX5_SET (vport_element, attr, eswitch_owner_vhca_id, +MLX5_CAP_GEN (vport->dev, vhca_id)); + }return esw_qos_node_create_sched_element (vport_node, sched_ctx, extack); }
@@ -419,6 +429,7 @@ esw_qos_vport_tc_create_sched_element(struct mlx5_esw_sched_node *vport_tc_node, { u32 sched_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {}; struct mlx5_core_dev *dev = vport_tc_node->esw->dev; + struct mlx5_vport *vport = vport_tc_node->vport; void *attr; if (!mlx5_qos_element_type_supported(
@@ -430,8 +441,7 @@ esw_qos_vport_tc_create_sched_element(struct mlx5_esw_sched_node *vport_tc_node,MLX5_SET (scheduling_context, sched_ctx, element_type,SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT_TC ); attr =MLX5_ADDR_OF (scheduling_context, sched_ctx, element_attributes); -MLX5_SET (vport_tc_element, attr, vport_number, - vport_tc_node->vport->vport); +MLX5_SET (vport_tc_element, attr, vport_number, vport->vport);MLX5_SET (vport_tc_element, attr, traffic_class, vport_tc_node->tc);MLX5_SET (scheduling_context, sched_ctx, max_bw_obj_id, rate_limit_elem_ix);
@@ -439,6 +449,13 @@ esw_qos_vport_tc_create_sched_element(struct mlx5_esw_sched_node *vport_tc_node, vport_tc_node->parent->ix);MLX5_SET (scheduling_context, sched_ctx, bw_share, vport_tc_node->bw_share); +if (vport->dev != dev) { +/* The port is assigned to a node on another eswitch. */ +MLX5_SET (vport_tc_element, attr, eswitch_owner_vhca_id_valid, + true); +MLX5_SET (vport_tc_element, attr, eswitch_owner_vhca_id, +MLX5_CAP_GEN (vport->dev, vhca_id)); + }return esw_qos_node_create_sched_element (vport_tc_node, sched_ctx, extack);
@@ -1160,6 +1177,29 @@ static int esw_qos_vport_tc_check_type(enum sched_node_type curr_type,return 0 ; } +static bool esw_qos_validate_unsupported_tc_bw (struct mlx5_eswitch *esw, +u32 *tc_bw) +{ +int i, num_tcs =esw_qos_num_tcs (esw->dev); + +for (i = num_tcs; i <DEVLINK_RATE_TCS_MAX ; i++) +if (tc_bw[i]) +return false; + +return true; +} + +static bool esw_qos_vport_validate_unsupported_tc_bw (struct mlx5_vport *vport, +u32 *tc_bw) +{ +struct mlx5_esw_sched_node *node = vport->qos.sched_node; +struct mlx5_eswitch *esw = vport->dev->priv.eswitch; + + esw = (node && node->parent) ? node->parent->esw : esw; + +return esw_qos_validate_unsupported_tc_bw (esw, tc_bw); +} + static int esw_qos_vport_update(struct mlx5_vport *vport, enum sched_node_type type, struct mlx5_esw_sched_node *parent,
@@ -1179,8 +1219,15 @@ static int esw_qos_vport_update(struct mlx5_vport *vport,if (err)return err; -if (curr_type ==SCHED_NODE_TYPE_TC_ARBITER_TSAR && curr_type == type) +if (curr_type ==SCHED_NODE_TYPE_TC_ARBITER_TSAR && curr_type == type) {esw_qos_tc_arbiter_get_bw_shares (vport_node, curr_tc_bw); +if (!esw_qos_validate_unsupported_tc_bw (parent->esw, + curr_tc_bw)) { +NL_SET_ERR_MSG_MOD (extack, +"Unsupported traffic classes on the new device" ); +return -EOPNOTSUPP ; + } + }esw_qos_vport_disable (vport, extack);
@@ -1510,30 +1557,6 @@ static int esw_qos_devlink_rate_to_mbps(struct mlx5_core_dev *mdev, const char *return 0 ; } -static bool esw_qos_validate_unsupported_tc_bw (struct mlx5_eswitch *esw, -u32 *tc_bw) -{ -int i, num_tcs =esw_qos_num_tcs (esw->dev); - -for (i = num_tcs; i <DEVLINK_RATE_TCS_MAX ; i++) { -if (tc_bw[i]) -return false; - } - -return true; -} - -static bool esw_qos_vport_validate_unsupported_tc_bw (struct mlx5_vport *vport, -u32 *tc_bw) -{ -struct mlx5_esw_sched_node *node = vport->qos.sched_node; -struct mlx5_eswitch *esw = vport->dev->priv.eswitch; - - esw = (node && node->parent) ? node->parent->esw : esw; - -return esw_qos_validate_unsupported_tc_bw (esw, tc_bw); -} -static bool esw_qos_tc_bw_disabled (u32 *tc_bw) {int i;
@@ -1738,18 +1761,44 @@ int mlx5_esw_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv,return 0 ; } +static int +mlx5_esw_validate_cross_esw_scheduling (struct mlx5_eswitch *esw, +struct mlx5_esw_sched_node *parent, +struct netlink_ext_ack *extack) +{ +if (!parent || esw == parent->esw) +return 0 ; + +if (!MLX5_CAP_QOS (esw->dev, esw_cross_esw_sched)) { +NL_SET_ERR_MSG_MOD (extack, +"Cross E-Switch scheduling is not supported" ); +return -EOPNOTSUPP ; + } +if (esw->dev->shd != parent->esw->dev->shd) { +NL_SET_ERR_MSG_MOD (extack, +"Cannot add vport to a parent belonging to a different device" ); +return -EOPNOTSUPP ; + } +if (!mlx5_lag_is_active (esw->dev)) { +NL_SET_ERR_MSG_MOD (extack, +"Cross E-Switch scheduling requires LAG to be activated" ); +return -EOPNOTSUPP ; + } + +return 0 ; +} +static int mlx5_esw_qos_vport_update_parent (struct mlx5_vport *vport,struct mlx5_esw_sched_node *parent,struct netlink_ext_ack *extack) {struct mlx5_eswitch *esw = vport->dev->priv.eswitch; -int err =0 ; +int err; -if (parent && parent->esw != esw) { -NL_SET_ERR_MSG_MOD (extack,"Cross E-Switch scheduling is not supported" ); -return -EOPNOTSUPP ; - } + err =mlx5_esw_validate_cross_esw_scheduling (esw, parent, extack); +if (err) +return err;if (!vport->qos.sched_node && parent) {enum sched_node_type type;
Signature · 1 line
2.44.0
Jakub Kicinski
maintainerRe: [PATCH net-next V9 12/14] net/mlx5: qos: Support cross-device tx scheduling
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net/mlx5: qos: Support cross-device tx scheduling
This commit relaxes the restriction that rate groups can only contain vports from the same E-Switch. It enables cross-device TX scheduling if the hardware supports it, LAG is enabled, and the E-Switches share a devlink device.
Quoted 3 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +0
-0
[ ... ]
Quoted 9 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +5
-0@@ -1738,18 +1761,44 @@ int mlx5_esw_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv,return 0 ; } +static int +mlx5_esw_validate_cross_esw_scheduling (struct mlx5_eswitch *esw, +struct mlx5_esw_sched_node *parent, +struct netlink_ext_ack *extack) +{
[ ... ]
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c int mlx5_esw_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv, · line 1786
Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +5
-0+if (!mlx5_lag_is_active (esw->dev)) { +NL_SET_ERR_MSG_MOD (extack, +"Cross E-Switch scheduling requires LAG to be activated" ); +return -EOPNOTSUPP ; + }
What happens to the cross-device rate scheduling if LAG is deactivated after the configuration is applied?
When LAG is deactivated, the LAG state is torn down in hardware, but there does not appear to be a corresponding cleanup or update triggered for the cross-eswitch QoS scheduling elements.
Could this result in a regression where the hardware TSAR tree and devlink rate configuration remain pointing to a now-invalid VHCA association, bypassing the state validation and causing traffic drops or firmware errors?
Quoted 3 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +3
-0+ +return 0 ; +}
Re: [PATCH net-next V9 12/14] net/mlx5: qos: Support cross-device tx scheduling
Quoted 51 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net/mlx5: qos: Support cross-device tx schedulingThis commit relaxes the restriction that rate groups can only contain vports
from the same E-Switch. It enables cross-device TX scheduling if the hardware
supports it, LAG is enabled, and the E-Switches share a devlink device.Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +0
-0[ ... ]
Quoted 12 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +0
-0@@ -1738,18 +1761,44 @@ int mlx5_esw_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv, return 0; }drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +3
-0+static int +mlx5_esw_validate_cross_esw_scheduling (struct mlx5_eswitch *esw, + struct mlx5_esw_sched_node*parent,
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +1
-0+ struct netlink_ext_ack*extack)
+{[ ... ]
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c
Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +3
-0+if (!mlx5_lag_is_active (esw->dev)) { + NL_SET_ERR_MSG_MOD(extack, + "Cross E-Switch schedulingrequires LAG to be activated");
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +2
-0+return -EOPNOTSUPP ; + }What happens to the cross-device rate scheduling if LAG is deactivated
after the configuration is applied?When LAG is deactivated, the LAG state is torn down in hardware, but there
does not appear to be a corresponding cleanup or update triggered for the
cross-eswitch QoS scheduling elements.
Quoted 5 lines
Could this result in a regression where the hardware TSAR tree and devlink
rate configuration remain pointing to a now-invalid VHCA association, bypassing the state validation and causing traffic drops or firmware errors?
This has been considered, and FW rejects LAG deactivation while there are VFs present on any of the ports. VFs need to be removed first, and removing all VF will also remove the QoS tree entirely.
SW could perhaps make this nicer by detecting that the QoS tree spans multiple devices and issuing a warning before attempting to invoke the DESTROY_LAG FW command. But this appears to be a significant effort currently, since the LAG code isn't structured in a way that would make this easy.
Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c int mlx5_esw_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv, · line 1789
Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c +3
-0+ +return 0 ; +}
Cosmin.
Tariq Toukan
maintainerPATCH v9 13/14 selftests: drv-net: Add test for cross-esw rate scheduling
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
Adds a Python selftest using the YNL devlink API to verify the devlink rate ops. The test requires a bond device given in the config as NETIF containing two PFs. Test setup will then create 1 VF on each PF and verify the various rate commands.
./devlink_rate_cross_esw.py
TAP version 13
1..3
ok 1 devlink_rate_cross_esw.test_same_esw_parent ok 2 devlink_rate_cross_esw.test_cross_esw_parent ok 3 devlink_rate_cross_esw.test_tx_rates_on_cross_esw
Tests will be skipped when the preconditions aren't met, when the devlink API is too old or when the devices don't appear to support cross-esw scheduling (detected via EOPNOTSUPP).
--- .../testing/selftests/drivers/net/hw/Makefile | 1 + .../drivers/net/hw/devlink_rate_cross_esw.py | 300 ++++++++++++++++++ 2 files changed, 301 insertions(+) create mode 100755 tools/testing/selftests/drivers/net/hw/devlink_rate_cross_esw.py
tools/testing/selftests/drivers/net/hw/Makefile +1 -0
@@ -20,6 +20,7 @@ TEST_GEN_FILES := \ TEST_PROGS = \ csum.py \ devlink_port_split.py \ + devlink_rate_cross_esw.py \ devlink_rate_tc_bw.py \ devmem.py \ ethtool.sh \
tools/testing/selftests/drivers/net/hw/devlink_rate_cross_esw.py new file +300 -0
300 lines not on this page · raw message
Signature · 1 line
2.44.0
Tariq Toukan
maintainerPATCH v9 14/14 net/mlx5: Document devlink rates
In reply to Tariq ToukanFrom: Cosmin Ratiu <[email protected]>
It seems rates were not documented in the mlx5-specific file, so add examples on how to limit VFs and groups and also provide an example of the intended way to achieve cross-esw scheduling.
--- Documentation/networking/devlink/mlx5.rst | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+)
Documentation/networking/devlink/mlx5.rst +33 -0
@@ -419,3 +419,36 @@ User commands examples: .. note:: This command can run over all interfaces such as PF/VF and representor ports. + +Rates +===== + +mlx5 devices can limit transmission of individual VFs or a group of them via +the devlink-rate API in switchdev mode. + +User commands examples: + +- Print the existing rates:: + + $ devlink port function rate show + +- Set a max tx limit on traffic from VF0:: + + $ devlink port function rate set pci/0000:82:00.0/1 tx_max 10Gbit + +- Create a rate group with a max tx limit and adding two VFs to it:: + + $ devlink port function rate add pci/0000:82:00.0/group1 tx_max 10Gbit + $ devlink port function rate set pci/0000:82:00.0/1 parent group1 + $ devlink port function rate set pci/0000:82:00.0/2 parent group1 + +- Same scenario, with a min guarantee of 20% of the bandwidth for the first VFs:: + + $ devlink port function rate add pci/0000:82:00.0/group1 tx_max 10Gbit + $ devlink port function rate set pci/0000:82:00.0/1 parent group1 tx_share 2Gbit + $ devlink port function rate set pci/0000:82:00.0/2 parent group1 + +- Cross-device scheduling:: + + $ devlink port function rate add pci/0000:82:00.0/group1 tx_max 10Gbit + $ devlink port function rate set pci/0000:82:00.1/32769 parent pci/0000:82:00.0/group1
Signature · 1 line
2.44.0