Netdev List
 help / color / mirror / Atom feed
* [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling
@ 2026-11-26 21:56 Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 01/10] net/mlx5e: Initialize IPsec XArrays with XA_FLAGS_LOCK_BH Tariq Toukan
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: Tariq Toukan @ 2026-11-26 21:56 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn
  Cc: Andrea Parri, Boris Pismenny, Cosmin Ratiu, Elliot Edmunds,
	Gal Pressman, Jeroen Massar, Jianbo Liu, Kuniyuki Iwashima,
	Leon Romanovsky, linux-rdma, Mark Bloch, Patrisious Haddad,
	Raed Salem, Saeed Mahameed, Simon Horman, Sridhar Samudrala,
	Steffen Klassert, Tariq Toukan

Hi,

This series by Jianbo fixes a number of bugs related to IPsec, neighbour
handling and hardware event work.

These bugs were found by Sashiko, code inspection, code development and
a field report, and tested on ConnectX-7 with a reproducer where the
bug can be triggered.

Please see individual fixes for details.

Regards,
Tariq

Jianbo Liu (10):
  net/mlx5e: Initialize IPsec XArrays with XA_FLAGS_LOCK_BH
  net/mlx5e: Stop counting packets dropped by IPsec RX SA rules
  net/mlx5e: Fix IPsec SA modify/stats update race
  net/mlx5e: Drain pending hardware event work before freeing IPsec SA
  net/mlx5e: Drop IPsec tunnel traffic if the neigh cannot be created
  net/mlx5e: Fix IPv6 neighbour fallback for IPsec tunnels
  net/mlx5e: Copy back only replay_esn after an IPsec ESN update
  net/mlx5e: Validate neighbour events before updating IPsec SAs
  net/mlx5e: Serialize IPsec netevent MAC snapshots
  net/mlx5e: Create IPsec flow counters once per SA

 .../mellanox/mlx5/core/en_accel/ipsec.c       | 130 +++++++++----
 .../mellanox/mlx5/core/en_accel/ipsec.h       |  29 ++-
 .../mellanox/mlx5/core/en_accel/ipsec_fs.c    | 171 +++++++++---------
 .../mlx5/core/en_accel/ipsec_offload.c        |  27 +--
 .../mellanox/mlx5/core/esw/ipsec_fs.c         |   8 +-
 5 files changed, 223 insertions(+), 142 deletions(-)


base-commit: e1055cbfd6eb578417f3ce983cf44462e8465267
-- 
2.44.0


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

* [PATCH net 01/10] net/mlx5e: Initialize IPsec XArrays with XA_FLAGS_LOCK_BH
  2026-11-26 21:56 [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling Tariq Toukan
@ 2026-11-26 21:56 ` Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 02/10] net/mlx5e: Stop counting packets dropped by IPsec RX SA rules Tariq Toukan
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tariq Toukan @ 2026-11-26 21:56 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn
  Cc: Andrea Parri, Boris Pismenny, Cosmin Ratiu, Elliot Edmunds,
	Gal Pressman, Jeroen Massar, Jianbo Liu, Kuniyuki Iwashima,
	Leon Romanovsky, linux-rdma, Mark Bloch, Patrisious Haddad,
	Raed Salem, Saeed Mahameed, Simon Horman, Sridhar Samudrala,
	Steffen Klassert, Tariq Toukan

From: Jianbo Liu <jianbol@nvidia.com>

ipsec->sadb and ipsec->ipsec_obj_id_map are created without
XA_FLAGS_LOCK_BH, so their lock type is a plain spinlock, but every
mutation uses the _bh accessors.

Node insertion drops+reacquires the lock with the lock variants
corresponding to the configured type. Using the wrong type therefore
leaves bottom halves disabled during the allocation.

Fix this mismatch by using the correct locking type.

Discovered by Sashiko. Reproduced on ConnectX-7 (fw 28.48.1000) with
failslab failing the GFP_NOWAIT node allocation of the SA insert:

  BUG: sleeping function called from invalid context
  preempt_count: 200, expected: 0
   __xas_nomem
   __xa_insert
   mlx5e_xfrm_add_state

No splat with the fix.

Fixes: 403b383a3ce3 ("net/mlx5e: Store all XFRM SAs in Xarray")
Fixes: 91bafc638ed4 ("net/mlx5e: Handle IPsec offload for RX datapath in switchdev mode")
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
---
 drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c    | 2 +-
 drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
index fa6cb51fdb93..a549851c8382 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
@@ -1014,7 +1014,7 @@ void mlx5e_ipsec_init(struct mlx5e_priv *priv)
 	if (!ipsec)
 		return;
 
-	xa_init_flags(&ipsec->sadb, XA_FLAGS_ALLOC);
+	xa_init_flags(&ipsec->sadb, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_BH);
 	ipsec->mdev = priv->mdev;
 	init_completion(&ipsec->comp);
 	ipsec->wq = alloc_workqueue("mlx5e_ipsec: %s", WQ_UNBOUND, 0,
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
index 5c69e89b13a7..502d89a18f9e 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
@@ -2804,7 +2804,8 @@ int mlx5e_accel_ipsec_fs_init(struct mlx5e_ipsec *ipsec,
 		mutex_init(&ipsec->tx_esw->ft.mutex);
 		mutex_init(&ipsec->rx_esw->ft.mutex);
 		ipsec->tx_esw->ns = ns_esw;
-		xa_init_flags(&ipsec->ipsec_obj_id_map, XA_FLAGS_ALLOC1);
+		xa_init_flags(&ipsec->ipsec_obj_id_map,
+			      XA_FLAGS_ALLOC1 | XA_FLAGS_LOCK_BH);
 	} else if (mlx5_ipsec_device_caps(mdev) & MLX5_IPSEC_CAP_ROCE) {
 		ipsec->roce = mlx5_ipsec_fs_roce_init(mdev, devcom);
 	} else {
-- 
2.44.0


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

* [PATCH net 02/10] net/mlx5e: Stop counting packets dropped by IPsec RX SA rules
  2026-11-26 21:56 [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 01/10] net/mlx5e: Initialize IPsec XArrays with XA_FLAGS_LOCK_BH Tariq Toukan
@ 2026-11-26 21:56 ` Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 03/10] net/mlx5e: Fix IPsec SA modify/stats update race Tariq Toukan
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tariq Toukan @ 2026-11-26 21:56 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn
  Cc: Andrea Parri, Boris Pismenny, Cosmin Ratiu, Elliot Edmunds,
	Gal Pressman, Jeroen Massar, Jianbo Liu, Kuniyuki Iwashima,
	Leon Romanovsky, linux-rdma, Mark Bloch, Patrisious Haddad,
	Raed Salem, Saeed Mahameed, Simon Horman, Sridhar Samudrala,
	Steffen Klassert, Tariq Toukan

From: Jianbo Liu <jianbol@nvidia.com>

x->curlft.packets differs between crypto offload and packet offload
for the same traffic because packet offload counts RX SA drops.

With crypto offload, XFRM updates x->curlft.packets in the software
receive path, so packets dropped in hardware are not counted. With
packet offload, mlx5e_xfrm_update_stats() derives the lifetime from
the SA flow counter. When attrs->drop is set, the RX SA rule drops
packets in hardware but still increments that counter.

Remove the count action and counter destination from the drop rule
so these dropped packets stay out of the lifetime, matching crypto
offload accounting.

Triggered during code development. Reproduced on ConnectX-7 (fw
28.48.1000): 1000 pings to a tunnel RX SA in the drop state (peer
neighbour not resolved) give x->curlft.packets = 1000. With the fix it
stays 0, and counts again once the SA forwards.

Fixes: b2f7b01d36a9 ("net/mlx5e: Simulate missing IPsec TX limits hardware functionality")
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
---
 .../mellanox/mlx5/core/en_accel/ipsec_fs.c    | 28 +++++++++++--------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
index 502d89a18f9e..0facc5477155 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
@@ -2045,7 +2045,6 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 	struct mlx5_accel_esp_xfrm_attrs *attrs = &sa_entry->attrs;
 	struct mlx5_core_dev *mdev = mlx5e_ipsec_sa2dev(sa_entry);
 	struct mlx5e_ipsec *ipsec = sa_entry->ipsec;
-	struct mlx5_flow_destination dest[2];
 	struct mlx5_flow_act flow_act = {};
 	struct mlx5_flow_handle *rule;
 	struct mlx5_flow_spec *spec;
@@ -2103,17 +2102,24 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 	flow_act.crypto.type = MLX5_FLOW_CONTEXT_ENCRYPT_DECRYPT_TYPE_IPSEC;
 	flow_act.crypto.obj_id = sa_entry->ipsec_obj_id;
 	flow_act.flags |= FLOW_ACT_NO_APPEND;
-	flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_CRYPTO_DECRYPT |
-			   MLX5_FLOW_CONTEXT_ACTION_COUNT;
-	if (attrs->drop)
+	/* Keep decrypting packets even when dropped, to allow ESN state
+	 * updates.
+	 */
+	flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_CRYPTO_DECRYPT;
+	if (attrs->drop) {
 		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
-	else
-		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
-	dest[0].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
-	dest[0].ft = rx->ft.status;
-	dest[1].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
-	dest[1].counter = counter;
-	rule = mlx5_add_flow_rules(rx->ft.sa, spec, &flow_act, dest, 2);
+		rule = mlx5_add_flow_rules(rx->ft.sa, spec, &flow_act, NULL, 0);
+	} else {
+		struct mlx5_flow_destination dest[2];
+
+		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
+				   MLX5_FLOW_CONTEXT_ACTION_COUNT;
+		dest[0].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
+		dest[0].ft = rx->ft.status;
+		dest[1].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
+		dest[1].counter = counter;
+		rule = mlx5_add_flow_rules(rx->ft.sa, spec, &flow_act, dest, 2);
+	}
 	if (IS_ERR(rule)) {
 		err = PTR_ERR(rule);
 		mlx5_core_err(mdev, "fail to add RX ipsec rule err=%d\n", err);
-- 
2.44.0


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

* [PATCH net 03/10] net/mlx5e: Fix IPsec SA modify/stats update race
  2026-11-26 21:56 [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 01/10] net/mlx5e: Initialize IPsec XArrays with XA_FLAGS_LOCK_BH Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 02/10] net/mlx5e: Stop counting packets dropped by IPsec RX SA rules Tariq Toukan
@ 2026-11-26 21:56 ` Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 04/10] net/mlx5e: Drain pending hardware event work before freeing IPsec SA Tariq Toukan
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tariq Toukan @ 2026-11-26 21:56 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn
  Cc: Andrea Parri, Boris Pismenny, Cosmin Ratiu, Elliot Edmunds,
	Gal Pressman, Jeroen Massar, Jianbo Liu, Kuniyuki Iwashima,
	Leon Romanovsky, linux-rdma, Mark Bloch, Patrisious Haddad,
	Raed Salem, Saeed Mahameed, Simon Horman, Sridhar Samudrala,
	Steffen Klassert, Tariq Toukan

From: Jianbo Liu <jianbol@nvidia.com>

mlx5e_accel_ipsec_fs_modify() rule update races with
mlx5e_xfrm_update_stats(): The former removes and recreates steering
counters while the latter reads them.

Fix that by:
- moving the eswitch RX mapped id into struct mlx5e_ipsec_rule so
  everything related to rule creation is in the same struct.
- swap the pointer to it under x->lock to avoid the race.

Discovered by Sashiko. Not reproduced. Tested on ConnectX-7 (fw
28.48.1000) under KASAN and lockdep: 10k neighbour updates, each one
rebuilding the rules, with concurrent SA dumps and traffic. No splat,
no packet loss.

Fixes: b2f7b01d36a9 ("net/mlx5e: Simulate missing IPsec TX limits hardware functionality")
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
---
 drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h  | 2 +-
 .../net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c   | 7 +++++--
 drivers/net/ethernet/mellanox/mlx5/core/esw/ipsec_fs.c    | 8 ++++----
 3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
index f4b2d7b950c0..9bbcd9e4c319 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
@@ -215,6 +215,7 @@ struct mlx5e_ipsec_rule {
 	struct mlx5e_ipsec_drop replay;
 	struct mlx5e_ipsec_drop auth;
 	struct mlx5e_ipsec_drop trailer;
+	u32 rx_mapped_id;
 };
 
 struct mlx5e_ipsec_miss {
@@ -286,7 +287,6 @@ struct mlx5e_ipsec_sa_entry {
 	struct mlx5e_ipsec_work *work;
 	struct mlx5e_ipsec_dwork *dwork;
 	struct mlx5e_ipsec_limits limits;
-	u32 rx_mapped_id;
 	u8 ctx[MLX5_ST_SZ_BYTES(ipsec_aso)];
 };
 
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
index 0facc5477155..d0f738fdeff3 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
@@ -2845,8 +2845,11 @@ void mlx5e_accel_ipsec_fs_modify(struct mlx5e_ipsec_sa_entry *sa_entry)
 	if (err)
 		return;
 
-	mlx5e_accel_ipsec_fs_del_rule(sa_entry);
-	memcpy(sa_entry, &sa_entry_shadow, sizeof(*sa_entry));
+	spin_lock_bh(&sa_entry->x->lock);
+	swap(sa_entry->ipsec_rule, sa_entry_shadow.ipsec_rule);
+	spin_unlock_bh(&sa_entry->x->lock);
+
+	mlx5e_accel_ipsec_fs_del_rule(&sa_entry_shadow);
 }
 
 int mlx5e_ipsec_fs_tunnel_allowed(struct mlx5e_ipsec_sa_entry *sa_entry)
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/esw/ipsec_fs.c b/drivers/net/ethernet/mellanox/mlx5/core/esw/ipsec_fs.c
index f8c97954b6ea..2615bbc89a16 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/esw/ipsec_fs.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/esw/ipsec_fs.c
@@ -85,7 +85,7 @@ int mlx5_esw_ipsec_rx_setup_modify_header(struct mlx5e_ipsec_sa_entry *sa_entry,
 		goto err_header_alloc;
 	}
 
-	sa_entry->rx_mapped_id = mapped_id;
+	sa_entry->ipsec_rule.rx_mapped_id = mapped_id;
 	flow_act->modify_hdr = modify_hdr;
 	flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
 
@@ -104,7 +104,7 @@ void mlx5_esw_ipsec_rx_rule_add_match_obj(struct mlx5e_ipsec_sa_entry *sa_entry,
 		 ESW_IPSEC_RX_MAPPED_ID_MATCH_MASK);
 	MLX5_SET(fte_match_param, spec->match_value,
 		 misc_parameters_2.metadata_reg_c_1,
-		 sa_entry->rx_mapped_id << ESW_ZONE_ID_BITS);
+		 sa_entry->ipsec_rule.rx_mapped_id << ESW_ZONE_ID_BITS);
 
 	spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS_2;
 }
@@ -113,9 +113,9 @@ void mlx5_esw_ipsec_rx_id_mapping_remove(struct mlx5e_ipsec_sa_entry *sa_entry)
 {
 	struct mlx5e_ipsec *ipsec = sa_entry->ipsec;
 
-	if (sa_entry->rx_mapped_id)
+	if (sa_entry->ipsec_rule.rx_mapped_id)
 		xa_erase_bh(&ipsec->ipsec_obj_id_map,
-			    sa_entry->rx_mapped_id);
+			    sa_entry->ipsec_rule.rx_mapped_id);
 }
 
 int mlx5_esw_ipsec_rx_ipsec_obj_id_search(struct mlx5e_priv *priv, u32 id,
-- 
2.44.0


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

* [PATCH net 04/10] net/mlx5e: Drain pending hardware event work before freeing IPsec SA
  2026-11-26 21:56 [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling Tariq Toukan
                   ` (2 preceding siblings ...)
  2026-11-26 21:56 ` [PATCH net 03/10] net/mlx5e: Fix IPsec SA modify/stats update race Tariq Toukan
@ 2026-11-26 21:56 ` Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 05/10] net/mlx5e: Drop IPsec tunnel traffic if the neigh cannot be created Tariq Toukan
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tariq Toukan @ 2026-11-26 21:56 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn
  Cc: Andrea Parri, Boris Pismenny, Cosmin Ratiu, Elliot Edmunds,
	Gal Pressman, Jeroen Massar, Jianbo Liu, Kuniyuki Iwashima,
	Leon Romanovsky, linux-rdma, Mark Bloch, Patrisious Haddad,
	Raed Salem, Saeed Mahameed, Simon Horman, Sridhar Samudrala,
	Steffen Klassert, Tariq Toukan

From: Jianbo Liu <jianbol@nvidia.com>

mlx5e_ipsec_event() queues a per-event work item. This is untracked by
the SA, so mlx5e_xfrm_free_state() cannot cancel it, potentially
resulting in a use-after-free.

Embed the work in sa_entry so SA destruction can cancel it. The event
handler looks the SA up without a lock and a device flush frees the SA
right after deleting it, so wait for a running handler before
cancelling the work.

Discovered by manual code inspection. Reproduced on ConnectX-7 (fw
28.48.1000) under KASAN, with a 3 s delay added at the start of
mlx5e_ipsec_handle_event() and the SAs flushed right after a soft
limit event:

  BUG: KASAN: slab-use-after-free in mlx5e_ipsec_handle_event

With the fix the flush waits for the handler, no report.

Fixes: 8c582ddfbb47 ("net/mlx5e: Handle hardware IPsec limits events")
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
---
 .../mellanox/mlx5/core/en_accel/ipsec.c       | 34 ++++++++++++++-----
 .../mellanox/mlx5/core/en_accel/ipsec.h       |  5 +++
 .../mlx5/core/en_accel/ipsec_offload.c        | 25 ++++----------
 3 files changed, 37 insertions(+), 27 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
index a549851c8382..ea7a1ae2056f 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
@@ -42,6 +42,7 @@
 #include "ipsec.h"
 #include "ipsec_rxtx.h"
 #include "en_rep.h"
+#include "lib/eq.h"
 
 #define MLX5_IPSEC_RESCHED msecs_to_jiffies(1000)
 #define MLX5E_IPSEC_TUNNEL_SA XA_MARK_1
@@ -869,6 +870,8 @@ static int mlx5e_xfrm_add_state(struct net_device *dev,
 	if (err)
 		goto err_hw_ctx;
 
+	INIT_WORK(&sa_entry->event_work, mlx5e_ipsec_handle_event);
+
 	/* We use *_bh() variant because xfrm_timer_handler(), which runs
 	 * in softirq context, can reach our state delete logic and we need
 	 * xa_erase_bh() there.
@@ -944,6 +947,9 @@ static void mlx5e_xfrm_free_state(struct net_device *dev, struct xfrm_state *x)
 	if (x->xso.flags & XFRM_DEV_OFFLOAD_FLAG_ACQ)
 		goto sa_entry_free;
 
+	mlx5_eq_synchronize_async_irq(ipsec->mdev);
+	cancel_work_sync(&sa_entry->event_work);
+
 	if (sa_entry->work)
 		cancel_work_sync(&sa_entry->work->work);
 
@@ -1002,10 +1008,13 @@ static int mlx5e_ipsec_netevent_event(struct notifier_block *nb,
 
 void mlx5e_ipsec_init(struct mlx5e_priv *priv)
 {
+	u32 caps = mlx5_ipsec_device_caps(priv->mdev);
+	bool pkt_offload = caps & MLX5_IPSEC_CAP_PACKET_OFFLOAD;
+	bool tunnel = caps & MLX5_IPSEC_CAP_TUNNEL;
 	struct mlx5e_ipsec *ipsec;
 	int ret = -ENOMEM;
 
-	if (!mlx5_ipsec_device_caps(priv->mdev)) {
+	if (!caps) {
 		netdev_dbg(priv->netdev, "Not an IPSec offload device\n");
 		return;
 	}
@@ -1022,18 +1031,22 @@ void mlx5e_ipsec_init(struct mlx5e_priv *priv)
 	if (!ipsec->wq)
 		goto err_wq;
 
-	if (mlx5_ipsec_device_caps(priv->mdev) &
-	    MLX5_IPSEC_CAP_PACKET_OFFLOAD) {
+	if (pkt_offload) {
 		ret = mlx5e_ipsec_aso_init(ipsec);
 		if (ret)
 			goto err_aso;
+
+		ipsec->nb.notifier_call = mlx5e_ipsec_event;
+		ret = mlx5_notifier_register(priv->mdev, &ipsec->nb);
+		if (ret)
+			goto clear_aso;
 	}
 
-	if (mlx5_ipsec_device_caps(priv->mdev) & MLX5_IPSEC_CAP_TUNNEL) {
+	if (tunnel) {
 		ipsec->netevent_nb.notifier_call = mlx5e_ipsec_netevent_event;
 		ret = register_netevent_notifier(&ipsec->netevent_nb);
 		if (ret)
-			goto clear_aso;
+			goto err_notifier;
 	}
 
 	ipsec->is_uplink_rep = mlx5e_is_uplink_rep(priv);
@@ -1047,10 +1060,13 @@ void mlx5e_ipsec_init(struct mlx5e_priv *priv)
 	return;
 
 err_fs_init:
-	if (mlx5_ipsec_device_caps(priv->mdev) & MLX5_IPSEC_CAP_TUNNEL)
+	if (tunnel)
 		unregister_netevent_notifier(&ipsec->netevent_nb);
+err_notifier:
+	if (pkt_offload)
+		mlx5_notifier_unregister(priv->mdev, &ipsec->nb);
 clear_aso:
-	if (mlx5_ipsec_device_caps(priv->mdev) & MLX5_IPSEC_CAP_PACKET_OFFLOAD)
+	if (pkt_offload)
 		mlx5e_ipsec_aso_cleanup(ipsec);
 err_aso:
 	destroy_workqueue(ipsec->wq);
@@ -1073,8 +1089,10 @@ void mlx5e_ipsec_cleanup(struct mlx5e_priv *priv)
 		ipsec->netevent_nb.notifier_call = NULL;
 	}
 	if (ipsec->aso)
-		mlx5e_ipsec_aso_cleanup(ipsec);
+		mlx5_notifier_unregister(ipsec->mdev, &ipsec->nb);
 	destroy_workqueue(ipsec->wq);
+	if (ipsec->aso)
+		mlx5e_ipsec_aso_cleanup(ipsec);
 	kfree(ipsec);
 	priv->ipsec = NULL;
 }
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
index 9bbcd9e4c319..6c17bd997a0c 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
@@ -37,6 +37,7 @@
 #include <linux/mlx5/device.h>
 #include <net/xfrm.h>
 #include <linux/idr.h>
+#include <linux/workqueue.h>
 #include "lib/aso.h"
 #include "lib/devcom.h"
 
@@ -286,6 +287,7 @@ struct mlx5e_ipsec_sa_entry {
 	struct mlx5e_ipsec_rule ipsec_rule;
 	struct mlx5e_ipsec_work *work;
 	struct mlx5e_ipsec_dwork *dwork;
+	struct work_struct event_work;
 	struct mlx5e_ipsec_limits limits;
 	u8 ctx[MLX5_ST_SZ_BYTES(ipsec_aso)];
 };
@@ -332,6 +334,9 @@ void mlx5_accel_esp_modify_xfrm(struct mlx5e_ipsec_sa_entry *sa_entry,
 
 int mlx5e_ipsec_aso_init(struct mlx5e_ipsec *ipsec);
 void mlx5e_ipsec_aso_cleanup(struct mlx5e_ipsec *ipsec);
+void mlx5e_ipsec_handle_event(struct work_struct *work);
+int mlx5e_ipsec_event(struct notifier_block *nb, unsigned long event,
+		      void *data);
 
 int mlx5e_ipsec_aso_query(struct mlx5e_ipsec_sa_entry *sa_entry,
 			  struct mlx5_wqe_aso_ctrl_seg *data);
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c
index 145677ce9640..1f03465762e0 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c
@@ -438,11 +438,10 @@ static void mlx5e_ipsec_handle_limits(struct mlx5e_ipsec_sa_entry *sa_entry)
 	}
 }
 
-static void mlx5e_ipsec_handle_event(struct work_struct *_work)
+void mlx5e_ipsec_handle_event(struct work_struct *work)
 {
-	struct mlx5e_ipsec_work *work =
-		container_of(_work, struct mlx5e_ipsec_work, work);
-	struct mlx5e_ipsec_sa_entry *sa_entry = work->data;
+	struct mlx5e_ipsec_sa_entry *sa_entry =
+		container_of(work, struct mlx5e_ipsec_sa_entry, event_work);
 	struct mlx5_accel_esp_xfrm_attrs tmp = {};
 	struct mlx5_accel_esp_xfrm_attrs *attrs;
 	bool need_modify = false;
@@ -471,16 +470,14 @@ static void mlx5e_ipsec_handle_event(struct work_struct *_work)
 	spin_unlock_bh(&sa_entry->x->lock);
 	if (need_modify)
 		mlx5_accel_esp_modify_xfrm(sa_entry, &tmp);
-	kfree(work);
 }
 
-static int mlx5e_ipsec_event(struct notifier_block *nb, unsigned long event,
-			     void *data)
+int mlx5e_ipsec_event(struct notifier_block *nb, unsigned long event,
+		      void *data)
 {
 	struct mlx5e_ipsec *ipsec = container_of(nb, struct mlx5e_ipsec, nb);
 	struct mlx5e_ipsec_sa_entry *sa_entry;
 	struct mlx5_eqe_obj_change *object;
-	struct mlx5e_ipsec_work *work;
 	struct mlx5_eqe *eqe = data;
 	u16 type;
 
@@ -497,14 +494,7 @@ static int mlx5e_ipsec_event(struct notifier_block *nb, unsigned long event,
 	if (!sa_entry)
 		return NOTIFY_DONE;
 
-	work = kmalloc_obj(*work, GFP_ATOMIC);
-	if (!work)
-		return NOTIFY_DONE;
-
-	INIT_WORK(&work->work, mlx5e_ipsec_handle_event);
-	work->data = sa_entry;
-
-	queue_work(ipsec->wq, &work->work);
+	queue_work(ipsec->wq, &sa_entry->event_work);
 	return NOTIFY_OK;
 }
 
@@ -536,8 +526,6 @@ int mlx5e_ipsec_aso_init(struct mlx5e_ipsec *ipsec)
 	}
 
 	spin_lock_init(&aso->lock);
-	ipsec->nb.notifier_call = mlx5e_ipsec_event;
-	mlx5_notifier_register(mdev, &ipsec->nb);
 
 	ipsec->aso = aso;
 	return 0;
@@ -559,7 +547,6 @@ void mlx5e_ipsec_aso_cleanup(struct mlx5e_ipsec *ipsec)
 	aso = ipsec->aso;
 	pdev = mlx5_core_dma_dev(mdev);
 
-	mlx5_notifier_unregister(mdev, &ipsec->nb);
 	mlx5_aso_destroy(aso->aso);
 	dma_unmap_single(pdev, aso->dma_addr, sizeof(aso->ctx),
 			 DMA_BIDIRECTIONAL);
-- 
2.44.0


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

* [PATCH net 05/10] net/mlx5e: Drop IPsec tunnel traffic if the neigh cannot be created
  2026-11-26 21:56 [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling Tariq Toukan
                   ` (3 preceding siblings ...)
  2026-11-26 21:56 ` [PATCH net 04/10] net/mlx5e: Drain pending hardware event work before freeing IPsec SA Tariq Toukan
@ 2026-11-26 21:56 ` Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 07/10] net/mlx5e: Copy back only replay_esn after an IPsec ESN update Tariq Toukan
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tariq Toukan @ 2026-11-26 21:56 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn
  Cc: Andrea Parri, Boris Pismenny, Cosmin Ratiu, Elliot Edmunds,
	Gal Pressman, Jeroen Massar, Jianbo Liu, Kuniyuki Iwashima,
	Leon Romanovsky, linux-rdma, Mark Bloch, Patrisious Haddad,
	Raed Salem, Saeed Mahameed, Simon Horman, Sridhar Samudrala,
	Steffen Klassert, Tariq Toukan

From: Jianbo Liu <jianbol@nvidia.com>

When no route resolves the peer, mlx5e_ipsec_init_macs() creates a
neigh for it. If that fails the SA is installed with an all-zero
destination MAC and forwarding enabled. Drop instead, as for a created
but still unresolved neigh.

Triggered during code development. Reproduced on ConnectX-7 (fw
28.48.1000): add the TX SA while the ARP table is full (gc_thresh1-3 =
0), then forward packets through it. All of them leave with
destination MAC 00:00:00:00:00:00. With the fix none leave until the
neigh resolves.

Fixes: 4c24272b4e2b ("net/mlx5e: Listen to ARP events to update IPsec L2 headers in tunnel mode")
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
---
 drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
index ea7a1ae2056f..4daad648460f 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
@@ -368,10 +368,10 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,
 	n = ipv4_neigh_lookup(netdev, pkey);
 	if (!n) {
 		n = ipv4_neigh_create(netdev, pkey);
+		attrs->drop = true;
 		if (IS_ERR(n))
 			return;
 		neigh_event_send(n, NULL);
-		attrs->drop = true;
 	} else {
 		neigh_ha_snapshot(addr, n, netdev);
 		ether_addr_copy(dst, addr);
-- 
2.44.0


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

* [PATCH net 07/10] net/mlx5e: Copy back only replay_esn after an IPsec ESN update
  2026-11-26 21:56 [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling Tariq Toukan
                   ` (4 preceding siblings ...)
  2026-11-26 21:56 ` [PATCH net 05/10] net/mlx5e: Drop IPsec tunnel traffic if the neigh cannot be created Tariq Toukan
@ 2026-11-26 21:56 ` Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 08/10] net/mlx5e: Validate neighbour events before updating IPsec SAs Tariq Toukan
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tariq Toukan @ 2026-11-26 21:56 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn
  Cc: Andrea Parri, Boris Pismenny, Cosmin Ratiu, Elliot Edmunds,
	Gal Pressman, Jeroen Massar, Jianbo Liu, Kuniyuki Iwashima,
	Leon Romanovsky, linux-rdma, Mark Bloch, Patrisious Haddad,
	Raed Salem, Saeed Mahameed, Simon Horman, Sridhar Samudrala,
	Steffen Klassert, Tariq Toukan

From: Jianbo Liu <jianbol@nvidia.com>

The ESN paths rebuild a full set of attrs from the xfrm state, and
mlx5_accel_esp_modify_xfrm() copies all of them over sa_entry->attrs
although the hardware modify changes only the ESN fields. The rebuilt
attrs do not carry what changed at run time, so an ESN update undoes:

- attrs.drop, set when the lifetime worker expires the SA;
- lft.soft_packet_limit, reset by mlx5e_ipsec_handle_limits();
- the MACs and the drop state programmed by the neighbour event
  worker, which runs on the same unbound workqueue and can also race
  the copy.

The software state then differs from the installed rules.

Copy back only replay_esn.

Discovered by code inspection in review of the next patch. Not
reproduced. The ESN update was tested on ConnectX-7 (fw 28.48.1000)
with SAs starting just below sequence number 2^31 and 2^32: no loss
across either boundary.

Fixes: b2f7b01d36a9 ("net/mlx5e: Simulate missing IPsec TX limits hardware functionality")
Fixes: d05971a413d3 ("net/mlx5e: Add SW implementation to support IPsec 64 bit soft and hard limits")
Fixes: 4c24272b4e2b ("net/mlx5e: Listen to ARP events to update IPsec L2 headers in tunnel mode")
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
---
 .../net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c
index 1f03465762e0..5182a79b0e99 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c
@@ -299,7 +299,7 @@ void mlx5_accel_esp_modify_xfrm(struct mlx5e_ipsec_sa_entry *sa_entry,
 	if (err)
 		return;
 
-	memcpy(&sa_entry->attrs, attrs, sizeof(sa_entry->attrs));
+	sa_entry->attrs.replay_esn = attrs->replay_esn;
 }
 
 static void mlx5e_ipsec_aso_update(struct mlx5e_ipsec_sa_entry *sa_entry,
-- 
2.44.0


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

* [PATCH net 08/10] net/mlx5e: Validate neighbour events before updating IPsec SAs
  2026-11-26 21:56 [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling Tariq Toukan
                   ` (5 preceding siblings ...)
  2026-11-26 21:56 ` [PATCH net 07/10] net/mlx5e: Copy back only replay_esn after an IPsec ESN update Tariq Toukan
@ 2026-11-26 21:56 ` Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 09/10] net/mlx5e: Serialize IPsec netevent MAC snapshots Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 10/10] net/mlx5e: Create IPsec flow counters once per SA Tariq Toukan
  8 siblings, 0 replies; 10+ messages in thread
From: Tariq Toukan @ 2026-11-26 21:56 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn
  Cc: Andrea Parri, Boris Pismenny, Cosmin Ratiu, Elliot Edmunds,
	Gal Pressman, Jeroen Massar, Jianbo Liu, Kuniyuki Iwashima,
	Leon Romanovsky, linux-rdma, Mark Bloch, Patrisious Haddad,
	Raed Salem, Saeed Mahameed, Simon Horman, Sridhar Samudrala,
	Steffen Klassert, Tariq Toukan

From: Jianbo Liu <jianbol@nvidia.com>

mlx5e_ipsec_netevent_event() runs for every valid neighbour update on
the global netevent chain. It matched the event by the SA source or
destination address, which is wrong:

- a foreign neighbour (veth, other netdev or netns) sharing the SA
  address overwrites the programmed MAC;
- for a routed peer the MAC is taken from the nexthop gateway, whose
  address never equals the SA address, so gateway MAC changes are
  missed.

Snapshot the resolved nexthop (table, ifindex, key) when its MAC is
read in mlx5e_ipsec_init_macs(), and match events against it. The
neighbour tables are per netns, so matching the table also keeps
neighbours of other namespaces out. The ifindex tells apart one gateway
IP on several netdevs in a netns. Save it only when the nexthop is on
the SA's offload device (xso.dev): xfrm flushes the SA when that device
goes down, so the saved ifindex can never be reused by another device.
For any other device, e.g. a bond slave when the fallback path is used,
save ifindex 0 and match the key on any device in the netns.

This depends on "net/mlx5e: Copy back only replay_esn after an IPsec
ESN update": without it an ESN update replaces nh with a fresh lookup
while the hardware keeps the old MACs, and writes it while
mlx5e_ipsec_netevent_event() reads it without a lock.

Observed on a ConnectX-8 Kubernetes cluster: a pod learns its node IP
with the host veth MAC, that MAC is programmed and the peer drops the
tunnel traffic. Reproduced on ConnectX-7 (fw 28.48.1000):
- neighbour for the peer address added on a dummy netdev: ping loss
  100%, 0% with the fix;
- routed peer, wrong gateway MAC corrected after the SA was added:
  ping loss 100%, 0% with the fix.

Fixes: 4c24272b4e2b ("net/mlx5e: Listen to ARP events to update IPsec L2 headers in tunnel mode")
Co-developed-by: Elliot Edmunds <eedmunds@nvidia.com>
Signed-off-by: Elliot Edmunds <eedmunds@nvidia.com>
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Saeed Mahameed <saeedm@nvidia.com>
Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
---
 .../mellanox/mlx5/core/en_accel/ipsec.c       | 48 +++++++++++++------
 .../mellanox/mlx5/core/en_accel/ipsec.h       |  8 ++++
 2 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
index 4084f8a88695..cc75fcb0aa15 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
@@ -256,6 +256,17 @@ static void mlx5e_ipsec_init_limits(struct mlx5e_ipsec_sa_entry *sa_entry,
 	attrs->lft.numb_rounds_soft = (u64)n;
 }
 
+static void mlx5e_ipsec_set_tnl_nh(struct mlx5e_ipsec_sa_entry *sa_entry,
+				   struct mlx5_accel_esp_xfrm_attrs *attrs,
+				   const struct neigh_table *tbl,
+				   const struct net_device *dev,
+				   const void *pkey)
+{
+	attrs->nh.tbl = tbl;
+	attrs->nh.ifindex = dev == sa_entry->x->xso.dev ? dev->ifindex : 0;
+	memcpy(attrs->nh.key, pkey, tbl->key_len);
+}
+
 static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,
 				  struct mlx5_accel_esp_xfrm_attrs *attrs)
 {
@@ -263,6 +274,7 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,
 	struct net_device *netdev = sa_entry->dev;
 	struct xfrm_state *x = sa_entry->x;
 	struct dst_entry *rt_dst_entry;
+	const struct neigh_table *tbl;
 	struct flowi4 fl4 = {};
 	struct flowi6 fl6 = {};
 	struct neighbour *n;
@@ -360,6 +372,7 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,
 	if (attrs->dir == XFRM_DEV_OFFLOAD_OUT &&
 	    is_zero_ether_addr(addr))
 		neigh_event_send(n, NULL);
+	mlx5e_ipsec_set_tnl_nh(sa_entry, attrs, n->tbl, n->dev, n->primary_key);
 	dst_release(rt_dst_entry);
 	neigh_release(n);
 	return;
@@ -375,13 +388,20 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,
 		else
 			n = ipv6_neigh_create(netdev, pkey);
 		attrs->drop = true;
-		if (IS_ERR(n))
+		if (IS_ERR(n)) {
+			tbl = addrs->family == AF_INET ?
+			      arp_table(dev_net(netdev)) :
+			      nd_table(dev_net(netdev));
+			mlx5e_ipsec_set_tnl_nh(sa_entry, attrs, tbl, netdev,
+					       pkey);
 			return;
+		}
 		neigh_event_send(n, NULL);
 	} else {
 		neigh_ha_snapshot(addr, n, netdev);
 		ether_addr_copy(dst, addr);
 	}
+	mlx5e_ipsec_set_tnl_nh(sa_entry, attrs, n->tbl, n->dev, n->primary_key);
 	neigh_release(n);
 }
 
@@ -973,10 +993,20 @@ static void mlx5e_xfrm_free_state(struct net_device *dev, struct xfrm_state *x)
 	kfree(sa_entry);
 }
 
+static bool
+mlx5e_ipsec_neigh_matches(const struct mlx5e_ipsec_sa_entry *sa_entry,
+			  const struct neighbour *n)
+{
+	const struct mlx5_accel_esp_xfrm_attrs *attrs = &sa_entry->attrs;
+
+	return attrs->nh.tbl == n->tbl &&
+	       (!attrs->nh.ifindex || attrs->nh.ifindex == n->dev->ifindex) &&
+	       !memcmp(attrs->nh.key, n->primary_key, n->tbl->key_len);
+}
+
 static int mlx5e_ipsec_netevent_event(struct notifier_block *nb,
 				      unsigned long event, void *ptr)
 {
-	struct mlx5_accel_esp_xfrm_attrs *attrs;
 	struct mlx5e_ipsec_netevent_data *data;
 	struct mlx5e_ipsec_sa_entry *sa_entry;
 	struct mlx5e_ipsec *ipsec;
@@ -988,21 +1018,9 @@ static int mlx5e_ipsec_netevent_event(struct notifier_block *nb,
 
 	ipsec = container_of(nb, struct mlx5e_ipsec, netevent_nb);
 	xa_for_each_marked(&ipsec->sadb, idx, sa_entry, MLX5E_IPSEC_TUNNEL_SA) {
-		attrs = &sa_entry->attrs;
-
-		if (attrs->addrs.family != n->tbl->family)
+		if (!mlx5e_ipsec_neigh_matches(sa_entry, n))
 			continue;
 
-		if (attrs->addrs.family == AF_INET) {
-			if (!neigh_key_eq32(n, &attrs->addrs.saddr.a4) &&
-			    !neigh_key_eq32(n, &attrs->addrs.daddr.a4))
-				continue;
-		} else {
-			if (!neigh_key_eq128(n, &attrs->addrs.saddr.a4) &&
-			    !neigh_key_eq128(n, &attrs->addrs.daddr.a4))
-				continue;
-		}
-
 		data = sa_entry->work->data;
 
 		neigh_ha_snapshot(data->addr, n, sa_entry->dev);
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
index 6c17bd997a0c..d60c72df0bf1 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
@@ -119,6 +119,14 @@ struct mlx5_accel_esp_xfrm_attrs {
 		u8 dmac[ETH_ALEN];
 		__be16 dport;
 	};
+	struct {
+		/* Neighbour tables are per netns, so matching the table
+		 * also keeps neighbours of other namespaces out.
+		 */
+		const struct neigh_table *tbl;
+		int ifindex;
+		u8 key[sizeof(struct in6_addr)];
+	} nh;
 };
 
 enum mlx5_ipsec_cap {
-- 
2.44.0


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

* [PATCH net 09/10] net/mlx5e: Serialize IPsec netevent MAC snapshots
  2026-11-26 21:56 [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling Tariq Toukan
                   ` (6 preceding siblings ...)
  2026-11-26 21:56 ` [PATCH net 08/10] net/mlx5e: Validate neighbour events before updating IPsec SAs Tariq Toukan
@ 2026-11-26 21:56 ` Tariq Toukan
  2026-11-26 21:56 ` [PATCH net 10/10] net/mlx5e: Create IPsec flow counters once per SA Tariq Toukan
  8 siblings, 0 replies; 10+ messages in thread
From: Tariq Toukan @ 2026-11-26 21:56 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn
  Cc: Andrea Parri, Boris Pismenny, Cosmin Ratiu, Elliot Edmunds,
	Gal Pressman, Jeroen Massar, Jianbo Liu, Kuniyuki Iwashima,
	Leon Romanovsky, linux-rdma, Mark Bloch, Patrisious Haddad,
	Raed Salem, Saeed Mahameed, Simon Horman, Sridhar Samudrala,
	Steffen Klassert, Tariq Toukan

From: Jianbo Liu <jianbol@nvidia.com>

mlx5e_ipsec_netevent_event() runs in atomic context on the global
netevent chain and snapshots the neighbour MAC into the per-SA work
data, which mlx5e_ipsec_handle_netdev_event() then reads from a
workqueue. The two are unsynchronized and can tear the address.

Protect the shared address with a dedicated spinlock in struct
mlx5e_ipsec_netevent_data. x->lock can't be taken here, it may
deadlock with the ASO event path.

Discovered by Sashiko. Not reproduced, the window is a few
instructions wide. Tested on ConnectX-7 (fw 28.48.1000) under KASAN
and lockdep with 10k neighbour updates during traffic, no splat.

Fixes: 4c24272b4e2b ("net/mlx5e: Listen to ARP events to update IPsec L2 headers in tunnel mode")
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
---
 .../ethernet/mellanox/mlx5/core/en_accel/ipsec.c   | 14 ++++++++++++--
 .../ethernet/mellanox/mlx5/core/en_accel/ipsec.h   |  2 ++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
index cc75fcb0aa15..60f7dc3df355 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
@@ -701,15 +701,20 @@ static void mlx5e_ipsec_handle_netdev_event(struct work_struct *_work)
 	struct mlx5e_ipsec_sa_entry *sa_entry = work->sa_entry;
 	struct mlx5e_ipsec_netevent_data *data = work->data;
 	struct mlx5_accel_esp_xfrm_attrs *attrs;
+	u8 addr[ETH_ALEN] __aligned(2);
 
 	attrs = &sa_entry->attrs;
 
+	spin_lock_bh(&data->lock);
+	ether_addr_copy(addr, data->addr);
+	spin_unlock_bh(&data->lock);
+
 	switch (attrs->dir) {
 	case XFRM_DEV_OFFLOAD_IN:
-		ether_addr_copy(attrs->smac, data->addr);
+		ether_addr_copy(attrs->smac, addr);
 		break;
 	case XFRM_DEV_OFFLOAD_OUT:
-		ether_addr_copy(attrs->dmac, data->addr);
+		ether_addr_copy(attrs->dmac, addr);
 		break;
 	default:
 		WARN_ON_ONCE(true);
@@ -720,6 +725,7 @@ static void mlx5e_ipsec_handle_netdev_event(struct work_struct *_work)
 
 static int mlx5_ipsec_create_work(struct mlx5e_ipsec_sa_entry *sa_entry)
 {
+	struct mlx5e_ipsec_netevent_data *ndata;
 	struct xfrm_state *x = sa_entry->x;
 	struct mlx5e_ipsec_work *work;
 	void *data = NULL;
@@ -754,6 +760,8 @@ static int mlx5_ipsec_create_work(struct mlx5e_ipsec_sa_entry *sa_entry)
 		if (!data)
 			goto free_work;
 
+		ndata = data;
+		spin_lock_init(&ndata->lock);
 		INIT_WORK(&work->work, mlx5e_ipsec_handle_netdev_event);
 		break;
 	default:
@@ -1023,7 +1031,9 @@ static int mlx5e_ipsec_netevent_event(struct notifier_block *nb,
 
 		data = sa_entry->work->data;
 
+		spin_lock_bh(&data->lock);
 		neigh_ha_snapshot(data->addr, n, sa_entry->dev);
+		spin_unlock_bh(&data->lock);
 		queue_work(ipsec->wq, &sa_entry->work->work);
 	}
 
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
index d60c72df0bf1..e70189f81ffb 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
@@ -172,6 +172,8 @@ struct mlx5e_ipsec_work {
 };
 
 struct mlx5e_ipsec_netevent_data {
+	/* Protects addr shared by the notifier and workqueue. */
+	spinlock_t lock;
 	u8 addr[ETH_ALEN];
 };
 
-- 
2.44.0


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

* [PATCH net 10/10] net/mlx5e: Create IPsec flow counters once per SA
  2026-11-26 21:56 [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling Tariq Toukan
                   ` (7 preceding siblings ...)
  2026-11-26 21:56 ` [PATCH net 09/10] net/mlx5e: Serialize IPsec netevent MAC snapshots Tariq Toukan
@ 2026-11-26 21:56 ` Tariq Toukan
  8 siblings, 0 replies; 10+ messages in thread
From: Tariq Toukan @ 2026-11-26 21:56 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn
  Cc: Andrea Parri, Boris Pismenny, Cosmin Ratiu, Elliot Edmunds,
	Gal Pressman, Jeroen Massar, Jianbo Liu, Kuniyuki Iwashima,
	Leon Romanovsky, linux-rdma, Mark Bloch, Patrisious Haddad,
	Raed Salem, Saeed Mahameed, Simon Horman, Sridhar Samudrala,
	Steffen Klassert, Tariq Toukan

From: Jianbo Liu <jianbol@nvidia.com>

IPsec counters are recreated on rule updates (e.g. due to a neighbour
update), which loses the previous counter values and may race with the
stats update itself.

Create the counters once when the SA is added and destroy them when it
is freed. Move the counters to the SA itself now that they're no longer
owned by the rule.

Triggered in CI testing. Reproduced on ConnectX-7 (fw 28.48.1000):
800 pings through tunnel mode SAs while about 3900 "ip neigh replace"
commands switch the peer neighbour between reachable, stale and delay
(same MAC). x->curlft.packets shows 180 on the TX SA and 88 on the RX
SA. With the fix both show 800.

Fixes: 4c24272b4e2b ("net/mlx5e: Listen to ARP events to update IPsec L2 headers in tunnel mode")
Fixes: dddb49b63d86 ("net/mlx5e: Add IPsec and ASO syndromes check in HW")
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Saeed Mahameed <saeedm@nvidia.com>
Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
---
 .../mellanox/mlx5/core/en_accel/ipsec.c       |  20 ++-
 .../mellanox/mlx5/core/en_accel/ipsec.h       |  12 +-
 .../mellanox/mlx5/core/en_accel/ipsec_fs.c    | 135 +++++++++---------
 3 files changed, 92 insertions(+), 75 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
index 60f7dc3df355..d37c1deca307 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
@@ -900,12 +900,16 @@ static int mlx5e_xfrm_add_state(struct net_device *dev,
 	if (err)
 		goto release_dwork;
 
-	err = mlx5e_accel_ipsec_fs_add_rule(sa_entry);
+	err = mlx5e_accel_ipsec_fs_alloc_counters(sa_entry);
 	if (err)
 		goto err_hw_ctx;
 
 	INIT_WORK(&sa_entry->event_work, mlx5e_ipsec_handle_event);
 
+	err = mlx5e_accel_ipsec_fs_add_rule(sa_entry);
+	if (err)
+		goto err_counters;
+
 	/* We use *_bh() variant because xfrm_timer_handler(), which runs
 	 * in softirq context, can reach our state delete logic and we need
 	 * xa_erase_bh() there.
@@ -938,6 +942,8 @@ static int mlx5e_xfrm_add_state(struct net_device *dev,
 
 err_add_rule:
 	mlx5e_accel_ipsec_fs_del_rule(sa_entry);
+err_counters:
+	mlx5e_accel_ipsec_fs_free_counters(sa_entry);
 err_hw_ctx:
 	mlx5_ipsec_free_sa_ctx(sa_entry);
 release_dwork:
@@ -991,6 +997,7 @@ static void mlx5e_xfrm_free_state(struct net_device *dev, struct xfrm_state *x)
 		cancel_delayed_work_sync(&sa_entry->dwork->dwork);
 
 	mlx5e_accel_ipsec_fs_del_rule(sa_entry);
+	mlx5e_accel_ipsec_fs_free_counters(sa_entry);
 	mlx5_ipsec_free_sa_ctx(sa_entry);
 	kfree(sa_entry->dwork);
 	if (sa_entry->work)
@@ -1151,8 +1158,8 @@ static void mlx5e_xfrm_advance_esn_state(struct xfrm_state *x)
 static void mlx5e_xfrm_update_stats(struct xfrm_state *x)
 {
 	struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x);
-	struct mlx5e_ipsec_rule *ipsec_rule = &sa_entry->ipsec_rule;
 	struct net *net = dev_net(x->xso.dev);
+	struct mlx5_fc **fcs = sa_entry->fcs;
 	u64 trailer_packets = 0, trailer_bytes = 0;
 	u64 replay_packets = 0, replay_bytes = 0;
 	u64 auth_packets = 0, auth_bytes = 0;
@@ -1168,12 +1175,12 @@ static void mlx5e_xfrm_update_stats(struct xfrm_state *x)
 		return;
 
 	if (sa_entry->attrs.dir == XFRM_DEV_OFFLOAD_IN) {
-		mlx5_fc_query_cached(ipsec_rule->auth.fc, &auth_bytes,
+		mlx5_fc_query_cached(fcs[MLX5_IPSEC_FC_AUTH], &auth_bytes,
 				     &auth_packets, &lastuse);
 		x->stats.integrity_failed += auth_packets;
 		XFRM_ADD_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR, auth_packets);
 
-		mlx5_fc_query_cached(ipsec_rule->trailer.fc, &trailer_bytes,
+		mlx5_fc_query_cached(fcs[MLX5_IPSEC_FC_TRAILER], &trailer_bytes,
 				     &trailer_packets, &lastuse);
 		XFRM_ADD_STATS(net, LINUX_MIB_XFRMINHDRERROR, trailer_packets);
 	}
@@ -1182,13 +1189,14 @@ static void mlx5e_xfrm_update_stats(struct xfrm_state *x)
 		return;
 
 	if (sa_entry->attrs.dir == XFRM_DEV_OFFLOAD_IN) {
-		mlx5_fc_query_cached(ipsec_rule->replay.fc, &replay_bytes,
+		mlx5_fc_query_cached(fcs[MLX5_IPSEC_FC_REPLAY], &replay_bytes,
 				     &replay_packets, &lastuse);
 		x->stats.replay += replay_packets;
 		XFRM_ADD_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR, replay_packets);
 	}
 
-	mlx5_fc_query_cached(ipsec_rule->fc, &bytes, &packets, &lastuse);
+	mlx5_fc_query_cached(fcs[MLX5_IPSEC_FC_RULE], &bytes, &packets,
+			     &lastuse);
 	success_packets = packets - auth_packets - trailer_packets - replay_packets;
 	x->curlft.packets += success_packets;
 	/* NIC counts all bytes passed through flow steering and doesn't have
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
index e70189f81ffb..2e8c6fc216de 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
@@ -216,13 +216,20 @@ struct mlx5e_ipsec_drop {
 	struct mlx5_fc *fc;
 };
 
+enum {
+	MLX5_IPSEC_FC_RULE,
+	MLX5_IPSEC_FC_REPLAY,
+	MLX5_IPSEC_FC_AUTH,
+	MLX5_IPSEC_FC_TRAILER,
+	MLX5_IPSEC_FC_NUM,
+};
+
 struct mlx5e_ipsec_rule {
 	struct mlx5_flow_handle *rule;
 	struct mlx5_flow_handle *status_pass;
 	struct mlx5_flow_handle *sa_sel;
 	struct mlx5_modify_hdr *modify_hdr;
 	struct mlx5_pkt_reformat *pkt_reformat;
-	struct mlx5_fc *fc;
 	struct mlx5e_ipsec_drop replay;
 	struct mlx5e_ipsec_drop auth;
 	struct mlx5e_ipsec_drop trailer;
@@ -295,6 +302,7 @@ struct mlx5e_ipsec_sa_entry {
 	u32 ipsec_obj_id;
 	u32 enc_key_id;
 	struct mlx5e_ipsec_rule ipsec_rule;
+	struct mlx5_fc *fcs[MLX5_IPSEC_FC_NUM];
 	struct mlx5e_ipsec_work *work;
 	struct mlx5e_ipsec_dwork *dwork;
 	struct work_struct event_work;
@@ -329,6 +337,8 @@ void mlx5e_accel_ipsec_fs_cleanup(struct mlx5e_ipsec *ipsec);
 int mlx5e_accel_ipsec_fs_init(struct mlx5e_ipsec *ipsec, struct mlx5_devcom_comp_dev **devcom);
 int mlx5e_accel_ipsec_fs_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry);
 void mlx5e_accel_ipsec_fs_del_rule(struct mlx5e_ipsec_sa_entry *sa_entry);
+int mlx5e_accel_ipsec_fs_alloc_counters(struct mlx5e_ipsec_sa_entry *sa_entry);
+void mlx5e_accel_ipsec_fs_free_counters(struct mlx5e_ipsec_sa_entry *sa_entry);
 int mlx5e_accel_ipsec_fs_add_pol(struct mlx5e_ipsec_pol_entry *pol_entry);
 void mlx5e_accel_ipsec_fs_del_pol(struct mlx5e_ipsec_pol_entry *pol_entry);
 void mlx5e_accel_ipsec_fs_modify(struct mlx5e_ipsec_sa_entry *sa_entry);
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
index d0f738fdeff3..df90ae327f23 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c
@@ -195,6 +195,39 @@ static void ipsec_rx_rule_add_match_obj(struct mlx5e_ipsec_sa_entry *sa_entry,
 	}
 }
 
+static const char * const fc_names[MLX5_IPSEC_FC_NUM] = {
+	[MLX5_IPSEC_FC_RULE] = "decrypt",
+	[MLX5_IPSEC_FC_REPLAY] = "replay drop",
+	[MLX5_IPSEC_FC_AUTH] = "auth drop",
+	[MLX5_IPSEC_FC_TRAILER] = "trailer drop",
+};
+
+static int rx_create_counters(struct mlx5e_ipsec_sa_entry *sa_entry)
+{
+	bool packet = sa_entry->attrs.type == XFRM_DEV_OFFLOAD_PACKET;
+	struct mlx5_core_dev *mdev = mlx5e_ipsec_sa2dev(sa_entry);
+	struct mlx5_fc *fc;
+	int i, err;
+
+	for (i = 0; i < MLX5_IPSEC_FC_NUM; i++) {
+		if (i == MLX5_IPSEC_FC_REPLAY && !packet)
+			continue;
+
+		fc = mlx5_fc_create(mdev, true);
+		if (IS_ERR(fc)) {
+			err = PTR_ERR(fc);
+			mlx5_core_err(mdev,
+				      "Failed to create ipsec rx %s counter, err=%d\n",
+				      fc_names[i], err);
+			mlx5e_accel_ipsec_fs_free_counters(sa_entry);
+			return err;
+		}
+		sa_entry->fcs[i] = fc;
+	}
+
+	return 0;
+}
+
 static int rx_add_rule_drop_auth_trailer(struct mlx5e_ipsec_sa_entry *sa_entry,
 					 struct mlx5e_ipsec_rx *rx)
 {
@@ -204,7 +237,6 @@ static int rx_add_rule_drop_auth_trailer(struct mlx5e_ipsec_sa_entry *sa_entry,
 	struct mlx5_flow_destination dest = {};
 	struct mlx5_flow_act flow_act = {};
 	struct mlx5_flow_handle *rule;
-	struct mlx5_fc *flow_counter;
 	struct mlx5_flow_spec *spec;
 	int err;
 
@@ -212,19 +244,10 @@ static int rx_add_rule_drop_auth_trailer(struct mlx5e_ipsec_sa_entry *sa_entry,
 	if (!spec)
 		return -ENOMEM;
 
-	flow_counter = mlx5_fc_create(mdev, true);
-	if (IS_ERR(flow_counter)) {
-		err = PTR_ERR(flow_counter);
-		mlx5_core_err(mdev,
-			      "Failed to add ipsec rx status drop rule counter, err=%d\n", err);
-		goto err_cnt;
-	}
-	sa_entry->ipsec_rule.auth.fc = flow_counter;
-
 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP | MLX5_FLOW_CONTEXT_ACTION_COUNT;
 	flow_act.flags = FLOW_ACT_NO_APPEND;
 	dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
-	dest.counter = flow_counter;
+	dest.counter = sa_entry->fcs[MLX5_IPSEC_FC_AUTH];
 	if (rx == ipsec->rx_esw)
 		spec->flow_context.flow_source = MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK;
 
@@ -241,16 +264,7 @@ static int rx_add_rule_drop_auth_trailer(struct mlx5e_ipsec_sa_entry *sa_entry,
 	}
 	sa_entry->ipsec_rule.auth.rule = rule;
 
-	flow_counter = mlx5_fc_create(mdev, true);
-	if (IS_ERR(flow_counter)) {
-		err = PTR_ERR(flow_counter);
-		mlx5_core_err(mdev,
-			      "Failed to add ipsec rx status drop rule counter, err=%d\n", err);
-		goto err_cnt_2;
-	}
-	sa_entry->ipsec_rule.trailer.fc = flow_counter;
-
-	dest.counter = flow_counter;
+	dest.counter = sa_entry->fcs[MLX5_IPSEC_FC_TRAILER];
 	MLX5_SET(fte_match_param, spec->match_value, misc_parameters_2.ipsec_syndrome, 2);
 	rule = mlx5_add_flow_rules(ft, spec, &flow_act, &dest, 1);
 	if (IS_ERR(rule)) {
@@ -265,12 +279,8 @@ static int rx_add_rule_drop_auth_trailer(struct mlx5e_ipsec_sa_entry *sa_entry,
 	return 0;
 
 err_rule_2:
-	mlx5_fc_destroy(mdev, sa_entry->ipsec_rule.trailer.fc);
-err_cnt_2:
 	mlx5_del_flow_rules(sa_entry->ipsec_rule.auth.rule);
 err_rule:
-	mlx5_fc_destroy(mdev, sa_entry->ipsec_rule.auth.fc);
-err_cnt:
 	kvfree(spec);
 	return err;
 }
@@ -283,7 +293,6 @@ static int rx_add_rule_drop_replay(struct mlx5e_ipsec_sa_entry *sa_entry, struct
 	struct mlx5_flow_destination dest = {};
 	struct mlx5_flow_act flow_act = {};
 	struct mlx5_flow_handle *rule;
-	struct mlx5_fc *flow_counter;
 	struct mlx5_flow_spec *spec;
 	int err;
 
@@ -291,18 +300,10 @@ static int rx_add_rule_drop_replay(struct mlx5e_ipsec_sa_entry *sa_entry, struct
 	if (!spec)
 		return -ENOMEM;
 
-	flow_counter = mlx5_fc_create(mdev, true);
-	if (IS_ERR(flow_counter)) {
-		err = PTR_ERR(flow_counter);
-		mlx5_core_err(mdev,
-			      "Failed to add ipsec rx status drop rule counter, err=%d\n", err);
-		goto err_cnt;
-	}
-
 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP | MLX5_FLOW_CONTEXT_ACTION_COUNT;
 	flow_act.flags = FLOW_ACT_NO_APPEND;
 	dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
-	dest.counter = flow_counter;
+	dest.counter = sa_entry->fcs[MLX5_IPSEC_FC_REPLAY];
 	if (rx == ipsec->rx_esw)
 		spec->flow_context.flow_source = MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK;
 
@@ -319,14 +320,11 @@ static int rx_add_rule_drop_replay(struct mlx5e_ipsec_sa_entry *sa_entry, struct
 	}
 
 	sa_entry->ipsec_rule.replay.rule = rule;
-	sa_entry->ipsec_rule.replay.fc = flow_counter;
 
 	kvfree(spec);
 	return 0;
 
 err_rule:
-	mlx5_fc_destroy(mdev, flow_counter);
-err_cnt:
 	kvfree(spec);
 	return err;
 }
@@ -2049,7 +2047,6 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 	struct mlx5_flow_handle *rule;
 	struct mlx5_flow_spec *spec;
 	struct mlx5e_ipsec_rx *rx;
-	struct mlx5_fc *counter;
 	int err = 0;
 
 	rx = rx_ft_get(mdev, ipsec, attrs->addrs.family, attrs->type);
@@ -2094,11 +2091,6 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 		break;
 	}
 
-	counter = mlx5_fc_create(mdev, true);
-	if (IS_ERR(counter)) {
-		err = PTR_ERR(counter);
-		goto err_add_cnt;
-	}
 	flow_act.crypto.type = MLX5_FLOW_CONTEXT_ENCRYPT_DECRYPT_TYPE_IPSEC;
 	flow_act.crypto.obj_id = sa_entry->ipsec_obj_id;
 	flow_act.flags |= FLOW_ACT_NO_APPEND;
@@ -2117,7 +2109,7 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 		dest[0].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
 		dest[0].ft = rx->ft.status;
 		dest[1].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
-		dest[1].counter = counter;
+		dest[1].counter = sa_entry->fcs[MLX5_IPSEC_FC_RULE];
 		rule = mlx5_add_flow_rules(rx->ft.sa, spec, &flow_act, dest, 2);
 	}
 	if (IS_ERR(rule)) {
@@ -2145,15 +2137,12 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 
 	sa_entry->ipsec_rule.rule = rule;
 	sa_entry->ipsec_rule.modify_hdr = flow_act.modify_hdr;
-	sa_entry->ipsec_rule.fc = counter;
 	sa_entry->ipsec_rule.pkt_reformat = flow_act.pkt_reformat;
 	return 0;
 
 err_drop_reason:
-	if (sa_entry->ipsec_rule.replay.rule) {
+	if (sa_entry->ipsec_rule.replay.rule)
 		mlx5_del_flow_rules(sa_entry->ipsec_rule.replay.rule);
-		mlx5_fc_destroy(mdev, sa_entry->ipsec_rule.replay.fc);
-	}
 err_add_replay:
 	if (sa_entry->ipsec_rule.sa_sel) {
 		mlx5_del_flow_rules(sa_entry->ipsec_rule.sa_sel);
@@ -2162,8 +2151,6 @@ static int rx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 err_add_sa_sel:
 	mlx5_del_flow_rules(rule);
 err_add_flow:
-	mlx5_fc_destroy(mdev, counter);
-err_add_cnt:
 	if (flow_act.pkt_reformat)
 		mlx5_packet_reformat_dealloc(mdev, flow_act.pkt_reformat);
 err_pkt_reformat:
@@ -2186,7 +2173,6 @@ static int tx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 	struct mlx5_flow_handle *rule;
 	struct mlx5_flow_spec *spec;
 	struct mlx5e_ipsec_tx *tx;
-	struct mlx5_fc *counter;
 	int err;
 
 	tx = tx_ft_get(mdev, ipsec, attrs->type);
@@ -2222,12 +2208,6 @@ static int tx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 		break;
 	}
 
-	counter = mlx5_fc_create(mdev, true);
-	if (IS_ERR(counter)) {
-		err = PTR_ERR(counter);
-		goto err_add_cnt;
-	}
-
 	flow_act.crypto.type = MLX5_FLOW_CONTEXT_ENCRYPT_DECRYPT_TYPE_IPSEC;
 	flow_act.crypto.obj_id = sa_entry->ipsec_obj_id;
 	flow_act.flags |= FLOW_ACT_NO_APPEND;
@@ -2241,7 +2221,7 @@ static int tx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 	dest[0].ft = tx->ft.status;
 	dest[0].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
 	dest[1].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
-	dest[1].counter = counter;
+	dest[1].counter = sa_entry->fcs[MLX5_IPSEC_FC_RULE];
 	rule = mlx5_add_flow_rules(tx->ft.sa, spec, &flow_act, dest, 2);
 	if (IS_ERR(rule)) {
 		err = PTR_ERR(rule);
@@ -2251,13 +2231,10 @@ static int tx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 
 	kvfree(spec);
 	sa_entry->ipsec_rule.rule = rule;
-	sa_entry->ipsec_rule.fc = counter;
 	sa_entry->ipsec_rule.pkt_reformat = flow_act.pkt_reformat;
 	return 0;
 
 err_add_flow:
-	mlx5_fc_destroy(mdev, counter);
-err_add_cnt:
 	if (flow_act.pkt_reformat)
 		mlx5_packet_reformat_dealloc(mdev, flow_act.pkt_reformat);
 err_pkt_reformat:
@@ -2645,7 +2622,6 @@ void mlx5e_accel_ipsec_fs_del_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 	struct mlx5_core_dev *mdev = mlx5e_ipsec_sa2dev(sa_entry);
 
 	mlx5_del_flow_rules(ipsec_rule->rule);
-	mlx5_fc_destroy(mdev, ipsec_rule->fc);
 	if (ipsec_rule->pkt_reformat)
 		mlx5_packet_reformat_dealloc(mdev, ipsec_rule->pkt_reformat);
 
@@ -2661,25 +2637,48 @@ void mlx5e_accel_ipsec_fs_del_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
 		mlx5_modify_header_dealloc(mdev, ipsec_rule->modify_hdr);
 
 	mlx5_del_flow_rules(ipsec_rule->trailer.rule);
-	mlx5_fc_destroy(mdev, ipsec_rule->trailer.fc);
 
 	mlx5_del_flow_rules(ipsec_rule->auth.rule);
-	mlx5_fc_destroy(mdev, ipsec_rule->auth.fc);
 
 	if (ipsec_rule->sa_sel) {
 		mlx5_del_flow_rules(ipsec_rule->sa_sel);
 		mlx5_del_flow_rules(ipsec_rule->status_pass);
 	}
 
-	if (ipsec_rule->replay.rule) {
+	if (ipsec_rule->replay.rule)
 		mlx5_del_flow_rules(ipsec_rule->replay.rule);
-		mlx5_fc_destroy(mdev, ipsec_rule->replay.fc);
-	}
 	mlx5_esw_ipsec_rx_id_mapping_remove(sa_entry);
 	rx_ft_put(sa_entry->ipsec, sa_entry->attrs.addrs.family,
 		  sa_entry->attrs.type);
 }
 
+int mlx5e_accel_ipsec_fs_alloc_counters(struct mlx5e_ipsec_sa_entry *sa_entry)
+{
+	struct mlx5_core_dev *mdev = mlx5e_ipsec_sa2dev(sa_entry);
+	struct mlx5_fc *fc;
+
+	if (sa_entry->attrs.dir == XFRM_DEV_OFFLOAD_IN)
+		return rx_create_counters(sa_entry);
+
+	fc = mlx5_fc_create(mdev, true);
+	if (IS_ERR(fc))
+		return PTR_ERR(fc);
+
+	sa_entry->fcs[MLX5_IPSEC_FC_RULE] = fc;
+	return 0;
+}
+
+void mlx5e_accel_ipsec_fs_free_counters(struct mlx5e_ipsec_sa_entry *sa_entry)
+{
+	struct mlx5_core_dev *mdev = mlx5e_ipsec_sa2dev(sa_entry);
+	int i;
+
+	for (i = 0; i < MLX5_IPSEC_FC_NUM; i++) {
+		mlx5_fc_destroy(mdev, sa_entry->fcs[i]);
+		sa_entry->fcs[i] = NULL;
+	}
+}
+
 int mlx5e_accel_ipsec_fs_add_pol(struct mlx5e_ipsec_pol_entry *pol_entry)
 {
 	int err;
-- 
2.44.0


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

end of thread, other threads:[~2026-11-26 21:57 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 21:56 [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling Tariq Toukan
2026-11-26 21:56 ` [PATCH net 01/10] net/mlx5e: Initialize IPsec XArrays with XA_FLAGS_LOCK_BH Tariq Toukan
2026-11-26 21:56 ` [PATCH net 02/10] net/mlx5e: Stop counting packets dropped by IPsec RX SA rules Tariq Toukan
2026-11-26 21:56 ` [PATCH net 03/10] net/mlx5e: Fix IPsec SA modify/stats update race Tariq Toukan
2026-11-26 21:56 ` [PATCH net 04/10] net/mlx5e: Drain pending hardware event work before freeing IPsec SA Tariq Toukan
2026-11-26 21:56 ` [PATCH net 05/10] net/mlx5e: Drop IPsec tunnel traffic if the neigh cannot be created Tariq Toukan
2026-11-26 21:56 ` [PATCH net 07/10] net/mlx5e: Copy back only replay_esn after an IPsec ESN update Tariq Toukan
2026-11-26 21:56 ` [PATCH net 08/10] net/mlx5e: Validate neighbour events before updating IPsec SAs Tariq Toukan
2026-11-26 21:56 ` [PATCH net 09/10] net/mlx5e: Serialize IPsec netevent MAC snapshots Tariq Toukan
2026-11-26 21:56 ` [PATCH net 10/10] net/mlx5e: Create IPsec flow counters once per SA Tariq Toukan

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