net/mlx5e: TC, use the owner eswitch for encap neigh updates
Subsystems (5)
Tariq Toukan
maintainerPATCH 1/1 net/mlx5e: TC, use the owner eswitch for encap neigh updates
From: Shay Drory <[email protected]>
Encap entries are kept in the encap table of the eswitch of the flows that use them, while the neigh hash entry is attached to the out_dev uplink. The neigh update and neigh-used paths took the eswitch from netdev_priv(e->out_dev), assuming out_dev is always the uplink of the owner eswitch.
Since MPESW peer flows were added, an encap entry of one eswitch can route through the uplink of another eswitch. A neigh update on that uplink then:
- takes the wrong encap_tbl_lock,
- adds the slow path rule to the other eswitch's FDB chains, where it shares a match with the origin flow's rule, while the flow later removes it from its own eswitch chains. This unbalances the fs_chains refcount ("Couldn't find table", leaked flow tables on unload) and with HWS, which emulates same-match NO_APPEND rules with a single pending rule, fails the origin's slow path update with -EEXIST,
- allocates the packet reformat on the other device, which the owner eswitch FDB rejects on HWS.
Record the owner eswitch uplink priv in the encap entry and use it for flow updates and entry release, keeping out_dev for neigh handling.
--- drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c | 2 +- drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c | 3 ++- drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c | 7 ++++--- drivers/net/ethernet/mellanox/mlx5/core/en_rep.h | 1 + 4 files changed, 8 insertions(+), 5 deletions(-)
---
Answer to an internal sashiko comment:
Could mlx5e_attach_encap() preserve the owner's initialized profile resources until all foreign neighbour users have finished?
When an mpesw encap owned by uplink B routes through uplink A, unbinding B's eth-rep auxiliary device can overlap A's neighbour work. In drivers/net/ethernet/mellanox/mlx5/core/en_rep.c, mlx5e_rep_remove() reaches owner profile teardown through:
mlx5_eswitch_unregister_vport_reps()
-> mlx5e_vport_rep_unload()
-> mlx5e_vport_uplink_rep_unload()
-> mlx5e_netdev_change_profile()Unlike normal eswitch disable, this path does not first synchronously unpair peers. The peer flow created by mlx5e_tc_add_fdb_peer_flow() in drivers/net/ethernet/mellanox/mlx5/core/en_tc.c is stored on the originating flow's peer_flows list, rather than B's tc_ht, so B's table cleanup need not remove the encap.
In drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c, mlx5e_rep_neigh_cleanup() drains B's work, not A's callbacks. mlx5e_rep_neigh_update() on A now passes this borrowed owner to mlx5e_rep_update_flows() in drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c, which dereferences priv->mdev->priv.eswitch.
The callback holds the rtnl lock, but mlx5e_netdev_change_profile() in drivers/net/ethernet/mellanox/mlx5/core/en_main.c performs profile cleanup after mlx5e_uplink_rep_disable() releases that lock. mlx5e_priv_cleanup() then clears priv with memset(priv, 0, sizeof(*priv)).
Can A's callback therefore dereference a cleared owner->mdev and oops, or access destroyed owner tc resources during profile replacement? mlx5e_tc_update_neigh_used_value() and mlx5e_get_next_matching_encap() in drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c also now use this owner, including for iterator release. An encap reference or a retained netdev allocation does not preserve those initialized profile resources. Could owner teardown synchronously remove these foreign encap users and wait for their callbacks before resetting the profile?
[SD] The stored priv does not add a new lifetime dependency. It has the same lifetime as flow->priv of the peer flows attached to the entry, which mlx5e_tc_add_fdb_peer_flow() creates on the peer uplink priv, and which the neigh update path already dereferenced before this patch, e.g. in mlx5e_tc_unoffload_fdb_rules() and get_mod_hdr_table(). All teardown paths (unload, reload, eswitch mode change, module exit) unpair the eswitches in esw_offloads_disable(), which removes the peer flows, before the owner uplink representor is unloaded. The window is only reachable by a manual eth-rep auxiliary unbind while paired, which this patch does not change.
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c +1 -1
@@ -167,7 +167,7 @@ static void mlx5e_rep_neigh_update(struct work_struct *work)* the next one. */ while ((e =mlx5e_get_next_init_encap (nhe, e)) !=NULL ) -mlx5e_rep_update_flows (netdev_priv (e->out_dev), e, neigh_connected, ha); +mlx5e_rep_update_flows (e->priv, e, neigh_connected, ha); out:rtnl_unlock ();
drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c +2 -1
@@ -117,7 +117,8 @@ void mlx5e_rep_update_flows(struct mlx5e_priv *priv,/* Update the encap source mac, in case that we delete * the flows when encap source mac changed. */ - route_dev =__dev_get_by_index (dev_net (priv->netdev), e->route_dev_ifindex); + route_dev =__dev_get_by_index (dev_net (e->out_dev), + e->route_dev_ifindex);if (route_dev)ether_addr_copy (eth->h_source, route_dev->dev_addr);
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +4 -3
@@ -177,7 +177,7 @@ void mlx5e_tc_encap_flows_add(struct mlx5e_priv *priv,return ; } e->flags |=MLX5_ENCAP_ENTRY_VALID ; -mlx5e_rep_queue_neigh_stats_work (priv); +mlx5e_rep_queue_neigh_stats_work (netdev_priv (e->out_dev));list_for_each_entry (flow, flow_list, tmp_list) {if (!mlx5e_is_offloaded_flow (flow) || !flow_flag_test (flow,SLOW ))
@@ -348,7 +348,7 @@ mlx5e_get_next_matching_encap(struct mlx5e_neigh_hash_entry *nhe,/* release starting encap */ if (e) -mlx5e_encap_put (netdev_priv (e->out_dev), e); +mlx5e_encap_put (e->priv, e);if (!next)return next;
@@ -401,7 +401,7 @@ void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)* next one. */ while ((e =mlx5e_get_next_valid_encap (nhe, e)) !=NULL ) { -struct mlx5e_priv *priv =netdev_priv (e->out_dev); +struct mlx5e_priv *priv = e->priv;struct encap_flow_item *efi, *tmp;struct mlx5_eswitch *esw;LIST_HEAD (flow_list);
@@ -871,6 +871,7 @@ int mlx5e_attach_encap(struct mlx5e_priv *priv,init_completion (&e->res_ready); entry_created = true;INIT_LIST_HEAD (&e->route_list); + e->priv =netdev_priv (mlx5_eswitch_uplink_get_proto_dev (esw,REP_ETH )); tun_info =mlx5e_dup_tun_info (tun_info);if (!tun_info) {
drivers/net/ethernet/mellanox/mlx5/core/en_rep.h +1 -0
@@ -211,6 +211,7 @@ struct mlx5e_encap_entry {struct mlx5e_mpls_info mpls_info;unsigned char h_dest[ETH_ALEN ];/* destination eth addr */ +struct mlx5e_priv *priv;struct net_device *out_dev;int route_dev_ifindex;struct mlx5e_tc_tunnel *tunnel;
base-commit: cc7fe6e741730fef549e484379281736d32746f0
Signature · 1 line
2.44.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] net/mlx5e: wrong owner eswitch in mlx5e_attach_encap() on SD secondary --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] net/mlx5e: wrong owner eswitch in mlx5e_attach_encap() on SD secondary
--
commit 96c6db1d17691b26e167336b35cba55741af989b
Author: Shay Drory <[email protected]>
net/mlx5e: TC, use the owner eswitch for encap neigh updates
This commit stores the owner eswitch uplink priv in struct mlx5e_encap_entry and uses it for encap neighbour flow updates and entry release instead of deriving priv from e->out_dev.
Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +0
-0
[ ... ]
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c int mlx5e_attach_encap(struct mlx5e_priv *priv, · line 877
Quoted 8 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +1
-0@@ -871,6 +871,7 @@ int mlx5e_attach_encap(struct mlx5e_priv *priv,init_completion (&e->res_ready); entry_created = true;INIT_LIST_HEAD (&e->route_list); + e->priv =netdev_priv (mlx5_eswitch_uplink_get_proto_dev (esw,REP_ETH )); tun_info =mlx5e_dup_tun_info (tun_info);if (!tun_info) {
[Severity: High]
Does mlx5_eswitch_uplink_get_proto_dev() set e->priv to the primary eswitch when mlx5e_attach_encap() is called on a Socket Direct secondary eswitch?
drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads.c:mlx5_eswitch_uplink_get_proto_dev() {
struct mlx5_core_dev *primary = mlx5_sd_get_primary(esw->dev);
if (primary)
esw = primary->priv.eswitch;
return mlx5_eswitch_get_proto_dev(esw, MLX5_VPORT_UPLINK, rep_type);
}When a tunnel encap flow is offloaded on an SD secondary representor, mlx5e_attach_encap() inserts e into the secondary esw->offloads.encap_tbl, allocates e->pkt_reformat on the secondary mdev, and installs the origin flow on the secondary eswitch, while e->priv points to the primary uplink priv. A subsequent neighbour update or encap release via e->priv will lock the primary encap_tbl_lock, update slow-path FDB rules on the primary eswitch (colliding with the peer flow and unbalancing fs_chains), and allocate or free e->pkt_reformat on the primary mdev.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix does not record the owner eswitch for encap entries created on a Socket Direct (SD) secondary eswitch, so the neigh-update…
--
net/mlx5e: TC, use the owner eswitch for encap neigh updates
The neigh update and neigh-used paths used to derive the eswitch from netdev_priv(e->out_dev). In MPESW, an encap entry of one eswitch can route through another eswitch's uplink, so that eswitch was not always the owner. This patch records the owner eswitch uplink priv in the encap entry, uses it for flow updates and entry release, and keeps out_dev for neigh handling.
Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +0
-0
[ ... ]
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c int mlx5e_attach_encap(struct mlx5e_priv *priv, · line 874
Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +1
-0@@ -871,6 +871,7 @@ int mlx5e_attach_encap(struct mlx5e_priv *priv,init_completion (&e->res_ready); entry_created = true;INIT_LIST_HEAD (&e->route_list); + e->priv =netdev_priv (mlx5_eswitch_uplink_get_proto_dev (esw,REP_ETH ));
[Severity: High]
Does this record the owner eswitch for encap entries created on a Socket Direct secondary eswitch? mlx5_eswitch_uplink_get_proto_dev() redirects to the SD primary:
void *mlx5_eswitch_uplink_get_proto_dev(struct mlx5_eswitch *esw, u8 rep_type)
{
struct mlx5_core_dev *primary = mlx5_sd_get_primary(esw->dev);
if (primary)
esw = primary->priv.eswitch;
return mlx5_eswitch_get_proto_dev(esw, MLX5_VPORT_UPLINK, rep_type);
}SD secondaries also have no uplink representor of their own, as the comment in mlx5_eswitch_reload_ib_reps() says:
/* SD secondary devices share the primary's uplink and do not * have their own uplink representor. Only load VF/SF vports. */
Take a tc flower encap rule added on a VF representor of an SD secondary:
- is_peer_flow_needed() handles this case explicitly via mlx5_lag_is_sd(in_mdev) && !mlx5_sd_is_primary(in_mdev).
- mlx5e_tc_add_fdb_flow() installs the original flow in the secondary's chains.
- e goes into the secondary's encap_tbl under the secondary's encap_tbl_lock.
- mlx5e_tc_tun_create_header_ipv4() allocates e->pkt_reformat on the secondary mdev.
But e->priv->mdev->priv.eswitch is the SD primary's eswitch.
On a neighbour change, mlx5e_rep_neigh_update() calls mlx5e_rep_update_flows(e->priv, ...). It looks like that would:
- take the primary's encap_tbl_lock while walking e->flows in mlx5e_take_all_encap_flows(). Meanwhile mlx5e_attach_encap() and mlx5e_detach_encap() change the same list under the secondary's lock.
- add and remove the slow path and encap rules through the primary's fs_chains, for flows whose rules live in the secondary's chains.
- allocate the replacement pkt_reformat on the primary mdev. Later, mlx5e_detach_encap() frees it through mlx5e_encap_dealloc(priv, e) with the secondary's priv.
The other converted paths have the same problem. When mlx5e_get_next_matching_encap() calls mlx5e_encap_put(e->priv, e) on the last reference, it would take the primary's lock and call hash_del_rcu() on an entry in the secondary's encap_tbl. mlx5e_tc_update_neigh_used_value() also walks e->flows under the primary's lock.
For SD, the old netdev_priv(e->out_dev) resolved to the same SD primary uplink, so this behavior is not new. Still, the problem the commit message describes stays reachable for SD secondary owners, including SD under MPESW.
Would it work better to record the owner eswitch or mdev directly, for example priv->mdev->priv.eswitch without the SD primary redirection, instead of an uplink priv?
Quoted 3 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +0
-0tun_info =mlx5e_dup_tun_info (tun_info);if (!tun_info) {
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125210629.239095-1-tariqt%40nvidia.com
Quoted 96 lines · On 26/11/2026 23:45, [email protected] wrote:
External email: Use caution opening links or attachments
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix does not record the owner eswitch for encap entries created on a Socket Direct (SD) secondary eswitch, so the neigh-update…
--
net/mlx5e: TC, use the owner eswitch for encap neigh updates
The neigh update and neigh-used paths used to derive the eswitch from
netdev_priv(e->out_dev). In MPESW, an encap entry of one eswitch can route through another eswitch's uplink, so that eswitch was not always the owner. This patch records the owner eswitch uplink priv in the encap entry, uses it for flow updates and entry release, and keepsout_devfor neigh handling.Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +0
-0[ ... ]
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c int mlx5e_attach_encap(struct mlx5e_priv *priv, · line 874
Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +1
-0@@ -871,6 +871,7 @@ int mlx5e_attach_encap(struct mlx5e_priv *priv,init_completion (&e->res_ready); entry_created = true;INIT_LIST_HEAD (&e->route_list); + e->priv =netdev_priv (mlx5_eswitch_uplink_get_proto_dev (esw,REP_ETH ));[Severity: High]
Does this record the owner eswitch for encap entries created on a Socket Direct secondary eswitch?
mlx5_eswitch_uplink_get_proto_dev()redirects to the SD primary:void *mlx5_eswitch_uplink_get_proto_dev(struct
mlx5_eswitch*esw, u8rep_type){ struct mlx5_core_dev *primary = mlx5_sd_get_primary(esw->dev); if (primary) esw = primary->priv.eswitch; return mlx5_eswitch_get_proto_dev(esw, MLX5_VPORT_UPLINK, rep_type); }SD secondaries also have no uplink representor of their own, as the comment in
mlx5_eswitch_reload_ib_reps()says:/* SD secondary devices share the primary's uplink and do not * have their own uplink representor. Only load VF/SF vports. */Take a tc flower encap rule added on a VF representor of an SD secondary:
-
is_peer_flow_needed()handles this case explicitly viamlx5_lag_is_sd(in_mdev)&& !mlx5_sd_is_primary(in_mdev).
-mlx5e_tc_add_fdb_flow()installs the original flow in the secondary's chains.
- e goes into the secondary'sencap_tblunder the secondary'sencap_tbl_lock.
-mlx5e_tc_tun_create_header_ipv4()allocatese->pkt_reformaton the secondary mdev.But
e->priv->mdev->priv.eswitchis the SD primary's eswitch.On a neighbour change,
mlx5e_rep_neigh_update()callsmlx5e_rep_update_flows(e->priv, ...). It looks like that would:- take the primary's
encap_tbl_lockwhile walkinge->flowsinmlx5e_take_all_encap_flows(). Meanwhilemlx5e_attach_encap()andmlx5e_detach_encap()change the same list under the secondary's lock.- add and remove the slow path and encap rules through the primary's
fs_chains, for flows whose rules live in the secondary's chains.- allocate the replacement
pkt_reformaton the primary mdev. Later,mlx5e_detach_encap()frees it throughmlx5e_encap_dealloc(priv, e)with the secondary's priv.The other converted paths have the same problem. When
mlx5e_get_next_matching_encap()callsmlx5e_encap_put(e->priv, e)on the last reference, it would take the primary's lock and callhash_del_rcu()on an entry in the secondary'sencap_tbl.mlx5e_tc_update_neigh_used_value()also walkse->flowsunder the primary's lock.
Quoted 7 lines
For SD, the old
netdev_priv(e->out_dev)resolved to the same SD primary uplink, so this behavior is not new. Still, the problem the commit message describes stays reachable for SD secondary owners, including SD under MPESW.Would it work better to record the owner eswitch or mdev directly, for example
priv->mdev->priv.eswitchwithout the SD primary redirection, instead of an uplink priv?
This is correct, but this is a pre-existing issue; this patch is fixing the MPESW case. a fix for the Socket-Direct will be sent separately
Quoted 9 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c int mlx5e_attach_encap(struct mlx5e_priv *priv, · line 877
Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +0
-0tun_info =mlx5e_dup_tun_info (tun_info);if (!tun_info) {[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125210629.239095-1-tariqt%40nvidia.com
This patch was applied to netdev/net.git (main) by Willem de Bruijn <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Willem de Bruijn <[email protected]>:
Quoted 9 lines · On Wed, 25 Nov 2026 23:06:29 +0200 you wrote:
From: Shay Drory <[email protected]>
Encap entries are kept in the encap table of the eswitch of the flows that use them, while the neigh hash entry is attached to the
out_devuplink. The neigh update and neigh-used paths took the eswitch fromnetdev_priv(e->out_dev), assumingout_devis always the uplink of the owner eswitch.[...]
Here is the summary with links:
- [net] net/mlx5e: TC, use the owner eswitch for encap neigh updates
https://git.kernel.org/netdev/net/c/3de4ad91efcd
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html