mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v3] net/mlx5e: Wait for flow setup outside encap_tbl_lock
@ 2026-11-30 14:02 Ziyu Zhang
  2026-11-30 14:03 ` netdev-bot+sinfo
  0 siblings, 1 reply; 2+ messages in thread
From: Ziyu Zhang @ 2026-11-30 14:02 UTC (permalink / raw)
  To: saeedm, tariqt, mbloch, leon
  Cc: netdev, linux-rdma, linux-kernel, andrew+netdev, davem, edumazet,
	kuba, pabeni, roid, baijiaju1990, ziyuzhang201

A post action can attach an FDB flow to an encap entry before flow
setup finishes. A neighbour or FIB update can then take encap_tbl_lock
and wait for init_done. For a TUN_RX flow the add path still needs the
same mutex in mlx5e_attach_decap_route(), so even a successful add can
block indefinitely. Parse error cleanup also needs the mutex to detach
the flow.

Wait on the eswitch until all flows attached to the affected entry or
route are ready. Drop encap_tbl_lock while sleeping and rescan the lists
under the mutex before applying the update. A waiter holds no reference
to a partially initialized flow. Failed adds can therefore tear it down
and wake the waiter after detach. Successful adds publish their state
before waking waiters, as do flows retained for multipath retry.

Process the encap entry even if no flow reference could be taken. A new
flow can reuse that entry before the deleting flow detaches, so the FIB
event must invalidate or rebuild its cached header. Remove post-action
rules, including an extra split rule, before signalling del_hw_done so
the update can release the reformat object safely.

This possible bug was found by our experimental static analysis tool,
which analyzes lock usage and completion-based synchronization to detect
potential deadlock patterns.

Fixes: 8300f225268b ("net/mlx5e: Create new flow attr for multi table actions")
Signed-off-by: Ziyu Zhang <ziyuzhang201@gmail.com>
---
Changes in v3:
- Drop encap_tbl_lock while waiting for flow setup in both the neighbour
  and FIB update paths. Rescan after wakeup so failed adds can detach.
- Update the encap entry even with an empty flow list and retire
  post-action rules before signalling del_hw_done.

v2: https://lore.kernel.org/r/20261126084216.26436-1-ziyuzhang201@gmail.com

 .../ethernet/mellanox/mlx5/core/en/rep/tc.c   |  3 ++
 .../ethernet/mellanox/mlx5/core/en/tc_priv.h  |  2 +-
 .../mellanox/mlx5/core/en/tc_tun_encap.c      | 43 +++++++++++++++++--
 .../net/ethernet/mellanox/mlx5/core/en_tc.c   | 27 ++++++++++--
 .../net/ethernet/mellanox/mlx5/core/en_tc.h   |  1 +
 .../net/ethernet/mellanox/mlx5/core/eswitch.c |  1 +
 .../net/ethernet/mellanox/mlx5/core/eswitch.h |  1 +
 7 files changed, 70 insertions(+), 8 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c b/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c
index e1ebc9b2d061..1522458213bf 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c
@@ -103,6 +103,9 @@ void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
 	if (encap_connected == neigh_connected && ether_addr_equal(e->h_dest, ha))
 		goto unlock;
 
+	wait_event_cmd(esw->offloads.encap_flow_wait, mlx5e_encap_flows_ready(e),
+		       mutex_unlock(&esw->offloads.encap_tbl_lock),
+		       mutex_lock(&esw->offloads.encap_tbl_lock));
 	mlx5e_take_all_encap_flows(e, &flow_list);
 
 	if ((e->flags & MLX5_ENCAP_ENTRY_VALID) &&
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_priv.h b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_priv.h
index 1ceebab4ce08..c9a04a01243c 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_priv.h
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_priv.h
@@ -110,7 +110,7 @@ struct mlx5e_tc_flow {
 	struct list_head tmp_list; /* temporary flow list used by neigh update */
 	refcount_t refcnt;
 	struct rcu_head rcu_head;
-	struct completion init_done;
+	bool init_done;
 	struct completion del_hw_done;
 	struct mlx5_flow_attr *attr;
 	struct mlx5_flow_attr *extra_split_attr;
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c
index 4f9f8beffbc8..59fb5c15c875 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c
@@ -283,7 +283,6 @@ static void mlx5e_take_tmp_flow(struct mlx5e_tc_flow *flow,
 		wait_for_completion(&flow->del_hw_done);
 		return;
 	}
-	wait_for_completion(&flow->init_done);
 
 	flow->tmp_entry_index = index;
 	list_add(&flow->tmp_list, flow_list);
@@ -315,6 +314,40 @@ static void mlx5e_take_all_route_decap_flows(struct mlx5e_route_entry *r,
 		mlx5e_take_tmp_flow(flow, flow_list, 0);
 }
 
+static bool mlx5e_flow_init_done(struct mlx5e_tc_flow *flow)
+{
+	/* Pair with the release in __mlx5e_add_fdb_flow() before using flow state. */
+	return smp_load_acquire(&flow->init_done);
+}
+
+bool mlx5e_encap_flows_ready(struct mlx5e_encap_entry *e)
+{
+	struct encap_flow_item *efi;
+
+	list_for_each_entry(efi, &e->flows, list) {
+		struct mlx5e_tc_flow *flow;
+
+		flow = container_of(efi, struct mlx5e_tc_flow, encaps[efi->index]);
+		if (!mlx5e_flow_init_done(flow))
+			return false;
+	}
+	return true;
+}
+
+static bool mlx5e_route_flows_ready(struct mlx5e_route_entry *r)
+{
+	struct mlx5e_encap_entry *e;
+	struct mlx5e_tc_flow *flow;
+
+	list_for_each_entry(e, &r->encap_entries, route_list)
+		if (!mlx5e_encap_flows_ready(e))
+			return false;
+	list_for_each_entry(flow, &r->decap_flows, decap_routes)
+		if (!mlx5e_flow_init_done(flow))
+			return false;
+	return true;
+}
+
 typedef bool (match_cb)(struct mlx5e_encap_entry *);
 
 static struct mlx5e_encap_entry *
@@ -1594,8 +1627,6 @@ static int mlx5e_update_route_encaps(struct mlx5e_priv *priv,
 		LIST_HEAD(encap_flows);
 
 		mlx5e_take_all_encap_flows(e, &encap_flows);
-		if (list_empty(&encap_flows))
-			continue;
 
 		if (mlx5e_route_entry_valid(r))
 			mlx5e_invalidate_encap(priv, e, &encap_flows);
@@ -1704,6 +1735,12 @@ static void mlx5e_tc_fib_event_work(struct work_struct *work)
 	if (!mlx5e_route_entry_valid(r) && !replace)
 		goto out;
 
+	/* Setup and error unwind both need encap_tbl_lock. Rescan the route
+	 * after waking, before changing any of its entries.
+	 */
+	wait_event_cmd(esw->offloads.encap_flow_wait, mlx5e_route_flows_ready(r),
+		       mutex_unlock(&esw->offloads.encap_tbl_lock),
+		       mutex_lock(&esw->offloads.encap_tbl_lock));
 	err = mlx5e_update_route_encaps(priv, r, &flow_list, replace);
 	if (err)
 		mlx5_core_warn(priv->mdev, "Failed to update route encaps, %d\n",
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
index 011741553f5b..1688f8951581 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
@@ -2063,11 +2063,21 @@ static void free_branch_attr(struct mlx5e_tc_flow *flow, struct mlx5_flow_attr *
 	kfree(attr);
 }
 
+static void mlx5e_del_post_act_handle(struct mlx5e_tc_flow *flow,
+				      struct mlx5_flow_attr *attr)
+{
+	if (attr->post_act_handle) {
+		mlx5e_tc_post_act_del(get_post_action(flow->priv), attr->post_act_handle);
+		attr->post_act_handle = NULL;
+	}
+}
+
 static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
 				  struct mlx5e_tc_flow *flow)
 {
 	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
 	struct mlx5_flow_attr *attr = flow->attr;
+	struct mlx5_flow_attr *post_attr;
 
 	mlx5e_put_flow_tunnel_id(flow);
 
@@ -2079,6 +2089,11 @@ static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
 		else
 			mlx5e_tc_unoffload_fdb_rules(esw, flow, attr);
 	}
+	/* An encap update may free the reformat object once del_hw_done fires. */
+	list_for_each_entry(post_attr, &flow->attrs, list)
+		mlx5e_del_post_act_handle(flow, post_attr);
+	if (flow->extra_split_attr)
+		mlx5e_del_post_act_handle(flow, flow->extra_split_attr);
 	complete_all(&flow->del_hw_done);
 
 	if (mlx5_flow_has_geneve_opt(&attr->parse_attr->spec))
@@ -2105,6 +2120,10 @@ static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
 	kvfree(attr->esw_attr->rx_tun_attr);
 	kvfree(attr->parse_attr);
 	kfree(flow->attr);
+
+	/* An unpublished flow can disappear without completing setup. */
+	if (!READ_ONCE(flow->init_done))
+		wake_up_all(&esw->offloads.encap_flow_wait);
 }
 
 struct mlx5_fc *mlx5e_tc_get_counter(struct mlx5e_tc_flow *flow)
@@ -4430,8 +4449,7 @@ mlx5_free_flow_attr_actions(struct mlx5e_tc_flow *flow, struct mlx5_flow_attr *a
 	if (!attr)
 		return;
 
-	if (attr->post_act_handle)
-		mlx5e_tc_post_act_del(get_post_action(flow->priv), attr->post_act_handle);
+	mlx5e_del_post_act_handle(flow, attr);
 
 	mlx5e_tc_tun_encap_dests_unset(flow->priv, flow, attr);
 
@@ -4495,7 +4513,6 @@ mlx5e_alloc_flow(struct mlx5e_priv *priv, int attr_size,
 	INIT_LIST_HEAD(&flow->attrs);
 	INIT_LIST_HEAD(&flow->peer_flows);
 	refcount_set(&flow->refcnt, 1);
-	init_completion(&flow->init_done);
 	init_completion(&flow->del_hw_done);
 
 	*__flow = flow;
@@ -4584,7 +4601,6 @@ __mlx5e_add_fdb_flow(struct mlx5e_priv *priv,
 		goto err_free;
 
 	err = mlx5e_tc_add_fdb_flow(priv, flow, extack);
-	complete_all(&flow->init_done);
 	if (err) {
 		if (!(err == -ENETUNREACH && mlx5_lag_is_multipath(in_mdev)))
 			goto err_free;
@@ -4592,6 +4608,9 @@ __mlx5e_add_fdb_flow(struct mlx5e_priv *priv,
 		add_unready_flow(flow);
 	}
 
+	/* Publish the initialized attributes to neighbour and FIB updates. */
+	smp_store_release(&flow->init_done, true);
+	wake_up_all(&priv->mdev->priv.eswitch->offloads.encap_flow_wait);
 	return flow;
 
 err_free:
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.h b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.h
index e1b8cb78369f..e953a7864408 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.h
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.h
@@ -215,6 +215,7 @@ void mlx5e_tc_encap_flows_del(struct mlx5e_priv *priv,
 bool mlx5e_encap_take(struct mlx5e_encap_entry *e);
 void mlx5e_encap_put(struct mlx5e_priv *priv, struct mlx5e_encap_entry *e);
 
+bool mlx5e_encap_flows_ready(struct mlx5e_encap_entry *e);
 void mlx5e_take_all_encap_flows(struct mlx5e_encap_entry *e, struct list_head *flow_list);
 void mlx5e_put_flow_list(struct mlx5e_priv *priv, struct list_head *flow_list);
 
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/eswitch.c b/drivers/net/ethernet/mellanox/mlx5/core/eswitch.c
index 174197f69888..62005751706f 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/eswitch.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/eswitch.c
@@ -2571,6 +2571,7 @@ int mlx5_eswitch_init(struct mlx5_core_dev *dev)
 	esw->mode = MLX5_ESWITCH_LEGACY;
 
 	mutex_init(&esw->offloads.encap_tbl_lock);
+	init_waitqueue_head(&esw->offloads.encap_flow_wait);
 	hash_init(esw->offloads.encap_tbl);
 	mutex_init(&esw->offloads.decap_tbl_lock);
 	hash_init(esw->offloads.decap_tbl);
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/eswitch.h b/drivers/net/ethernet/mellanox/mlx5/core/eswitch.h
index 48eb6acc1e87..f969007fc971 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/eswitch.h
+++ b/drivers/net/ethernet/mellanox/mlx5/core/eswitch.h
@@ -321,6 +321,7 @@ struct mlx5_esw_offload {
 	struct xarray vport_reps;
 	struct xarray peer_flows;
 	struct mutex peer_mutex;
+	wait_queue_head_t encap_flow_wait;
 	struct mutex encap_tbl_lock; /* protects encap_tbl */
 	DECLARE_HASHTABLE(encap_tbl, 8);
 	struct mutex decap_tbl_lock; /* protects decap_tbl */
-- 
2.39.5 (Apple Git-154)


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 14:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 14:02 [PATCH net v3] net/mlx5e: Wait for flow setup outside encap_tbl_lock Ziyu Zhang
2026-11-30 14:03 ` netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®