net/mlx5e: Fix deadlock when parsing flow actions fails
Subsystems (5)
PATCH v2 1/1 net/mlx5e: Fix deadlock when parsing flow actions fails
Neighbour and route updates wait for flow->init_done while holding encap_tbl_lock. Action parsing can expose a flow to these updates by attaching post-action encap entries in alloc_flow_post_acts(). If a later allocation or validation fails, cleanup needs encap_tbl_lock to detach the entries, but the update is still waiting for init_done.
Remove any installed post-action rules first, since a woken update can free the encap headers they reference. Mark the flow as having failed action parsing and signal init_done before detaching the entries.
Skip flows that failed action parsing in mlx5e_take_tmp_flow() after waiting for init_done. A route update can encounter the same flow through more than one encap entry while holding encap_tbl_lock, before cleanup can detach the entries. Skipping the flow prevents adding its tmp_list node twice. The lock prevents detach while the collector waits and checks the result, so it can take the reference after this check.
Free the post-action attributes on the parse error path before dropping the caller's reference, so any temporary reference held elsewhere cannot defer encap detach. Use a separate parse failure flag so the existing FAILED flag retains its meaning for offload failures.
This possible bug was found by our experimental static analysis tool, which analyzes lock usage and completion-based synchronization to detect potential deadlock patterns.
---
Changes in v2:
- Skip flows that failed action parsing before adding them to tmp_list.
- Detach their post-action encaps before dropping the caller's reference.
Sashiko's other two findings concern cleanup before reoffload retries. That cleanup is being discussed separately:
https://lkml.iu.edu/2611.1/02787.html
v1: https://lore.kernel.org/r/[email protected]
.../net/ethernet/mellanox/mlx5/core/en/tc_priv.h | 1 + .../ethernet/mellanox/mlx5/core/en/tc_tun_encap.c | 6 ++++- drivers/net/ethernet/mellanox/mlx5/core/en_tc.c | 31 ++++++++++++++++++++-- 3 files changed, 35 insertions(+), 3 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en/tc_priv.h +1 -0
@@ -31,6 +31,7 @@ enum {MLX5E_TC_FLOW_FLAG_SAMPLE =MLX5E_TC_FLOW_BASE +10 ,MLX5E_TC_FLOW_FLAG_USE_ACT_STATS =MLX5E_TC_FLOW_BASE +11 ,MLX5E_TC_FLOW_FLAG_PEER =MLX5E_TC_FLOW_BASE +12 , + MLX5E_TC_FLOW_FLAG_PARSE_FAILED = MLX5E_TC_FLOW_BASE + 13, }; struct mlx5e_tc_flow_parse_attr {
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +5 -1
@@ -275,6 +275,11 @@ static void mlx5e_take_tmp_flow(struct mlx5e_tc_flow *flow,struct list_head *flow_list, int index) { +/* Detaching the flow also takes encap_tbl_lock. */ + wait_for_completion(&flow->init_done); +if (flow_flag_test (flow,PARSE_FAILED )) +return ; +if (IS_ERR (mlx5e_flow_get (flow))) {/* Flow is being deleted concurrently. Wait for it to be * unoffloaded from hardware, otherwise deleting encap will
@@ -283,7 +288,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);
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +29 -2
@@ -3748,6 +3748,31 @@ free_flow_post_acts(struct mlx5e_tc_flow *flow) } } +static void abort_flow_post_acts (struct mlx5e_tc_flow *flow) +{ +struct mlx5e_post_act *post_act =get_post_action (flow->priv); +struct mlx5_flow_attr *attr; + +if (flow_flag_test (flow,PARSE_FAILED )) +return ; + +/* Remove hardware rules before an update can free their encap headers. */ +list_for_each_entry (attr, &flow->attrs, list) { +if (list_is_last (&attr->list, &flow->attrs)) +break ; +if (!attr->post_act_handle) +continue ; + +mlx5e_tc_post_act_del (post_act, attr->post_act_handle); + attr->post_act_handle =NULL ; + } + +/* Wake updates before detaching encaps under encap_tbl_lock. */ +flow_flag_set (flow,PARSE_FAILED ); +complete_all (&flow->init_done); +free_flow_post_acts (flow); +} +int mlx5e_tc_offload_flow_post_acts (struct mlx5e_tc_flow *flow) {
@@ -3852,7 +3877,7 @@ out:return 0 ; out_free: -free_flow_post_acts (flow); +abort_flow_post_acts (flow);return err; }
@@ -4580,8 +4605,10 @@ __mlx5e_add_fdb_flow(struct mlx5e_priv *priv, goto err_free; err =parse_tc_fdb_actions (priv, &rule->action, flow, extack); -if (err) +if (err) { +abort_flow_post_acts (flow); goto err_free; + } err =mlx5e_tc_add_fdb_flow (priv, flow, extack);complete_all (&flow->init_done);
Signature · 1 line
2.34.1
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] net/mlx5e: unorted post-actions on mlx5e_tc_add_fdb_flow() failure --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] net/mlx5e: unorted post-actions on mlx5e_tc_add_fdb_flow() failure
--
commit 80bfe544864e1741e156f9fe2cf078e4de48e16b
Author: Ziyu Zhang <[email protected]>
net/mlx5e: Fix deadlock when parsing flow actions fails
This commit avoids a deadlock between flow action parsing failure cleanup and neighbour or route updates by deleting post-action rules, setting MLX5E_TC_FLOW_FLAG_PARSE_FAILED, and completing flow->init_done before detaching encap entries under encap_tbl_lock.
Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +0
-0
[ ... ]
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c __mlx5e_add_fdb_flow(struct mlx5e_priv *priv, · line 4614
Quoted 12 lines
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +3
-1@@ -4580,8 +4605,10 @@ __mlx5e_add_fdb_flow(struct mlx5e_priv *priv, goto err_free; err =parse_tc_fdb_actions (priv, &rule->action, flow, extack); -if (err) +if (err) { +abort_flow_post_acts (flow); goto err_free; + } err =mlx5e_tc_add_fdb_flow (priv, flow, extack);complete_all (&flow->init_done);
[Severity: High]
What happens when parse_tc_fdb_actions() succeeds (attaching post-action encaps and offloading post-action rules in alloc_flow_post_acts()), and then mlx5e_tc_add_fdb_flow() fails (other than the multipath -ENETUNREACH unready case)?
On failure, mlx5e_tc_add_fdb_flow() sets MLX5E_TC_FLOW_FLAG_FAILED, leaving PARSE_FAILED and OFFLOADED unset, and __mlx5e_add_fdb_flow() calls complete_all(&flow->init_done) before mlx5e_flow_put(). Because mlx5e_take_tmp_flow() now waits for init_done before mlx5e_flow_get() and only skips flows with PARSE_FAILED set:
CPU0 CPU1__mlx5e_add_fdb_flow()
parse_tc_fdb_actions()
alloc_flow_post_acts()
mlx5e_tc_fib_event_work()
mutex_lock(&esw->offloads.encap_tbl_lock)
mlx5e_take_tmp_flow()
wait_for_completion(&flow->init_done)
mlx5e_tc_add_fdb_flow() -> fails
flow_flag_set(flow, FAILED)
complete_all(&flow->init_done)
flow_flag_test(flow, PARSE_FAILED) -> false
mlx5e_flow_get(flow) -> succeeds
list_add(&flow->tmp_list, ...) mlx5e_flow_put(priv, flow)
If the route update encounters the failed flow through two encap entries before cleanup detaches them, mlx5e_take_tmp_flow() calls list_add(&flow->tmp_list, flow_list) twice on the same node, corrupting flow_list. In addition, because OFFLOADED is not set, mlx5e_tc_encap_flows_del() skips unoffloading the flow's post-action rules and calls mlx5_packet_reformat_dealloc() on e->pkt_reformat while the installed post-action hardware rules still reference it.
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 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 (1) · Medium: 1 · 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 patch fixes the init_done/encap_tbl_lock deadlock only when action parsing fails.
- [Medium] The new PARSE_FAILED early return in mlx5e_take_tmp_flow() can leave mlx5e_update_route_encaps() with an empty per-entry list for an…
Pre-existing issues:
- [High] The patch skips PARSE_FAILED flows in mlx5e_take_tmp_flow() because a route update can reach the same flow through more than one…
--
net/mlx5e: Fix deadlock when parsing flow actions fails
This patch adds an abort_flow_post_acts() helper. When action parsing fails, it removes the post-action rules, sets a new PARSE_FAILED flag and completes init_done early. mlx5e_take_tmp_flow() now waits for init_done before taking a reference and skips flows that have PARSE_FAILED set.
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c static void mlx5e_take_tmp_flow(struct mlx5e_tc_flow *flow, · line 279
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +2
-0@@ -275,6 +275,11 @@ static void mlx5e_take_tmp_flow(struct mlx5e_tc_flow *flow,struct list_head *flow_list, int index) { +/* Detaching the flow also takes encap_tbl_lock. */ + wait_for_completion(&flow->init_done);
[Severity: High]
Can this still deadlock when flow creation succeeds?
When alloc_flow_post_acts() calls post_process_attr() for a post-action attr with encap dests, mlx5e_tc_tun_encap_dests_set() puts the flow on that encap entry's e->flows list. init_done is still not complete at that point.
The add path can then take esw->offloads.encap_tbl_lock again before __mlx5e_add_fdb_flow() reaches complete_all(&flow->init_done):
- a later post-act attr with encap dests in the same alloc_flow_post_acts() loop
- mlx5e_tc_add_fdb_flow()->mlx5e_attach_decap_route() for TUN_RX flows, which takes the lock unconditionally
- mlx5e_tc_add_fdb_flow()->post_process_attr(flow->attr)-> mlx5e_tc_tun_encap_dests_set() when the main attr also has encap dests
Flow add runs without RTNL because mlx5e sets f->unlocked_driver_cb, so it can interleave with the update work:
CPU1 (flow add) CPU2 (neigh/FIB update)alloc_flow_post_acts()
post_process_attr() /* on e->flows */
rtnl_lock()
mutex_lock(&encap_tbl_lock)
mlx5e_take_tmp_flow()
wait_for_completion(&init_done)mlx5e_tc_add_fdb_flow()
mlx5e_attach_decap_route()
mutex_lock(&encap_tbl_lock) /* blocks */
CPU2 can be either mlx5e_rep_update_flows() or mlx5e_tc_fib_event_work(). Both hold RTNL and encap_tbl_lock across this wait.
The flow is not failing here, so abort_flow_post_acts() is never called and the PARSE_FAILED check does not help. Neither side can make progress, and RTNL stays held.
One example trigger is a TUN_RX rule with ct, followed by tunnel_key set and a mirred redirect to a VXLAN device, racing with a neigh update or FIB event for that encap.
Would it be better not to wait for init_done while holding encap_tbl_lock at all, instead of completing init_done early only on the parse failure path?
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c static void mlx5e_take_tmp_flow(struct mlx5e_tc_flow *flow, · line 281
Quoted 2 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +2
-0+if (flow_flag_test (flow,PARSE_FAILED )) +return ;
[Severity: Medium]
Can this early return leave an encap entry that is still in use with stale state?
Suppose the flow that failed parsing is the only flow on e->flows. It keeps its reference on e until mlx5e_detach_encap() can take encap_tbl_lock. Meanwhile mlx5e_update_route_encaps() collects an empty list for e:
mlx5e_update_route_encaps() {
...
mlx5e_take_all_encap_flows(e, &encap_flows);
if (list_empty(&encap_flows))
continue;
...
}That skips mlx5e_invalidate_encap() and mlx5e_reoffload_encap(), so MLX5_ENCAP_ENTRY_VALID and the old e->pkt_reformat stay in place.
When the FIB worker releases encap_tbl_lock, two threads compete for the mutex: the failed flow's mlx5e_detach_encap(), and a new flow with the same tunnel key in mlx5e_tc_tun_encap_dests_set(). If the new flow wins, mlx5e_attach_encap() treats the entry as valid:
if (e->flags & MLX5_ENCAP_ENTRY_VALID) {
attr->esw_attr->dests[out_index].pkt_reformat = e->pkt_reformat;It also uses the stale e->route_dev_ifindex in mlx5e_set_vf_tunnel(). The route event has already been consumed. Would the new flow then stay offloaded with encap state built for the old route?
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c static void mlx5e_take_tmp_flow(struct mlx5e_tc_flow *flow, · line 293
Quoted 12 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +1
-1+if (IS_ERR (mlx5e_flow_get (flow))) {/* Flow is being deleted concurrently. Wait for it to be * unoffloaded from hardware, otherwise deleting encap will @@ -283,7 +288,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);
[Severity: High]
This bug was not introduced by this patch. The commit message describes a double list_add hazard here, and it also seems reachable for flows that fail in mlx5e_tc_add_fdb_flow(). Those flows have only FAILED set, not PARSE_FAILED.
Take one attr with two VF-tunnel encap dests that share a tunnel source IP. mlx5e_attach_encap_route() keys the route on the source IP, so both encap entries end up on the same r->encap_entries list. mlx5e_tc_tun_encap_dests_set() attaches both and then rejects the rule while the encaps are still attached:
if (*vf_tun && esw_attr->out_count > 1) {
NL_SET_ERR_MSG_MOD(extack, "VF tunnel encap with mirroring is not supported");
err = -EOPNOTSUPP;__mlx5e_add_fdb_flow() then calls complete_all(&flow->init_done) before err_free detaches them.
Suppose mlx5e_tc_fib_event_work()->mlx5e_update_route_encaps() takes the flow for e1 before the adder's mlx5e_flow_put() drops the refcount to zero. For e2, mlx5e_flow_get() then succeeds because the worker already holds a reference.
This list_add() then relinks a tmp_list node that is still on flow_list. The list_splice() that follows leaves flow->tmp_list pointing to itself.
Would mlx5e_put_flow_list() then loop forever on the same flow while holding RTNL? It would also call mlx5e_flow_put() past zero and touch the flow after kfree_rcu().
Should the skip check here also cover FAILED flows? Alternatively, could mlx5e_update_route_encaps() remove duplicates when it collects flows from several encap entries?
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126084216.26436-1-ziyuzhang201%40gmail.com