From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.180 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796047375; b=JTSXHHrGpIngOllGhIk1kUYpM2UL7u9FCQjuSgR5Gp1zbIhed+VjdoT/iyZ6astT5HBa VcO7VGlEoe5Jjtzab8gaLyTDAZmGUiblOidEV5IAW3OvoEDhUfzBx5ecDNX2v5/SPuuqG 5UxM+y1yKV+HQPsbExiXuzIVWUcOwirRbNpy7Rv7iDCQFmq0DSePmQtB+pe6uFaIs+c/K GAqxQRKV6ZL3X3ySsfFYVabmuhAU1EaD+GoA4Xur4mmVFooiJh7Kt+lynxxmeqPhyXh5V PPkPGV4ZXXrT1b6nhl86q17JBCSmfVAa9gRV601OAmJS6rxTHs/LrMvdXtsAB3xcymQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796047375; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=u0TiLzUTRlHT3Sk5axpitpyWuZ3akWBYxjUVnne5sXI=; b=cDC7jWhGUVWdxH0JVwZU/gZ2XFuA8y2OHx23BzggbHPqx8EYF8WGGsBHV/Q78MQa0mGP /xkx2k0vG8hplH+AFRg+oXfuUgzC50oeRXZM4r6w/gTOrSww9wF1JwNeZuFYTFQegUMAD SW3LltgC75jW5k9l8KGL7as2/tuMY1MYWLPcn6fpK7sHp8JRnTcOh+VjrS8ettGqAtsLk OjyIEz459KuipHBqKmvPksPCOv0qVna1Z58v3VazkShr2NZ0Tk0HmcBDGog6OrOZA/VXJ QXea1WqXUEM0OAsyJFvxyZi8lk1EdFFd80dDfg6lSvTJWxvV0Dk4z/0nO5mcOXT/i6Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=hS7+73Nt; arc=none smtp.remote-ip=209.85.214.180 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 10312606A327 for ; Mon, 30 Nov 2026 14:02:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 10312606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=hS7+73Nt Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2f11b4d6484so3843275ad.3 for ; Mon, 30 Nov 2026 06:02:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796047373; x=1796652173; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=u0TiLzUTRlHT3Sk5axpitpyWuZ3akWBYxjUVnne5sXI=; b=hS7+73Nt9wtDG1PLVsGQPwnk50pL5ac9ihYuMK9HUDJlK4q4/furjOF3CXb2dyRSzM 40KkmKRDcoADEWAVNUTKFzIh7Pa+oyT5OmvpBOVFsDuUhXcOBMhFcFBJXQDcRGMIH1T3 AGn+nmsfw5sHC+5pp1VBpww+8NcsMlRcWPdEyYGnCRvKWcoUuULFr9fCeHhJ2tIOikOw os5OfkkBXJlu/PmzuYUM6NGnLPjQzJzP4WOuTRnLstkGM1Lig3RAOvMFQVmOXwQJ2/2k hqTK0VHI+MvtOkws7m59ECvA+4Qfkp/vMl8HnjxuOwA3g3EOz+1Ib5rGVOVsMkUhlbWQ GQ/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796047373; x=1796652173; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=u0TiLzUTRlHT3Sk5axpitpyWuZ3akWBYxjUVnne5sXI=; b=i6eukSpmAZny3LRpjNzCQQIJx8D6hjqTal9mDPd0brSXitaGv1MhvxIH51z1sdQ+T/ PPiZBOTSZ9G02fvoMoBxG9w3Hn8g75pVmJEScVOpYc+q/TGNsBNodBx4n/Vr5+e4emTZ 9/jt0KRZIDWOxyBdSDOrDEKbYGTrw20d+S8pyJyU5OHG5fFpMwEMTd6ZJs1NjxKXhQog IUdn7rX8CtkRRy01tjvvgCrGjbKaDw3WpaQc4VeaF9s6+ooB9Qe1xOub5gTi+QMj19Ce OWXR0NPpZIcuI0DCa1qZn8rIAvgVM8ZdKSVgQa1buZN3e08JKBJU/xheBAOnCVG4Kf29 uVlQ== X-Forwarded-Encrypted: i=1; AL+hErHqR4AASR8cjzgJ4wMt5A4/1ZjGycNd2HOfGQkM0un9712zQHxslGRv/Ld+ykbh62txVHWDJYntAH9Ayc0=@vger.kernel.org X-Gm-Message-State: APuU4Z/1TYBzTYxJI4Bob4ZQgnPExJ4BqLXRcSQ1YRUs8KshgPluk6zj GJWaCFf2Pilzc/5MYw4AYVq7hDx5DUBUMC3xBbOs+MM96gpKBckyXQBx X-Gm-Gg: AZFdTZf+UgDBK47XhKP1uy0SWbj4ZK6de5ezWbacSGR02WPCyzkbSUMoAGMU2wA+Mgw +A82fAXy0vixSzug5x+C36hvQ13FXyd5zJG8aTnsN9IFB1kK2FYH7NRIXig0pf/1H2xtNJVxSDI Le7dxBGDYFKYbCYo5L8VTCOUGWgfENIn3djmK+twh4QTkW+rU3KS7wJyygFYgvhRcTyQ2Twrko9 ryTGAcLHLUYM6Ax/IBeh/07iDuc7tMCOQPhCL+sMYkLTHqg2LAYLlIZLl6b89wM8JdI6sy83GGD +Zli6NGr+xcY4NXxyyIUpgTkzXjM5frRgXPtpiI15KxRpNJrAYavL+a1qLFGvTdR2I8GR/eiwSU Av350D4ICDLF1rdkF/V1ERVBVyVlb2oimoGOUqvnwAohu1/KfrkWoqbEgRtyS8fRDythXlzoQuN OrmegBxeTQPfy8HSfC+Y7XH109/S4HRb6quryeoVsMs7Jz51z9pDHkZuK4zHdGdjJ1Rz9bgClWa e7uk6lLrl7Yn9T3+/cv/PlZxa4IEmT8U3bk2xP4FgSF2Bger4R0ai64/QT3ayA5M9agjty+NJug UhQ6X5Q+Ob/H7Unmwy05NTWxY0J2YctJr08GhxQ= X-Received: by 2002:a17:902:ef45:b0:2ef:d8ce:83e6 with SMTP id d9443c01a7336-2f03edf6f5cmr526798915ad.0.1796047372976; Mon, 30 Nov 2026 06:02:52 -0800 (PST) Received: from gmail.com ([1.202.120.104]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f0667679d8sm149436735ad.48.2026.11.30.06.02.29 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Nov 2026 06:02:52 -0800 (PST) From: Ziyu Zhang To: saeedm@nvidia.com, tariqt@nvidia.com, mbloch@nvidia.com, leon@kernel.org Cc: netdev@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, roid@nvidia.com, baijiaju1990@gmail.com, ziyuzhang201@gmail.com Subject: [PATCH net v3] net/mlx5e: Wait for flow setup outside encap_tbl_lock Date: Mon, 30 Nov 2026 22:02:15 +0800 Message-Id: <20261130140215.66953-1-ziyuzhang201@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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)