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
                   ` (9 more replies)
  0 siblings, 10 replies; 18+ 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] 18+ 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-27 22:32   ` netdev-bot+sashiko
  2026-11-26 21:56 ` [PATCH net 02/10] net/mlx5e: Stop counting packets dropped by IPsec RX SA rules Tariq Toukan
                   ` (8 subsequent siblings)
  9 siblings, 1 reply; 18+ 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] 18+ 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-27 22:32   ` netdev-bot+sashiko
  2026-11-26 21:56 ` [PATCH net 03/10] net/mlx5e: Fix IPsec SA modify/stats update race Tariq Toukan
                   ` (7 subsequent siblings)
  9 siblings, 1 reply; 18+ 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] 18+ 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-27 22:32   ` netdev-bot+sashiko
  2026-11-26 21:56 ` [PATCH net 04/10] net/mlx5e: Drain pending hardware event work before freeing IPsec SA Tariq Toukan
                   ` (6 subsequent siblings)
  9 siblings, 1 reply; 18+ 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] 18+ 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-27 22:32   ` netdev-bot+sashiko
  2026-11-26 21:56 ` [PATCH net 05/10] net/mlx5e: Drop IPsec tunnel traffic if the neigh cannot be created Tariq Toukan
                   ` (5 subsequent siblings)
  9 siblings, 1 reply; 18+ 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] 18+ 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-27 22:32   ` netdev-bot+sashiko
  2026-11-26 21:56 ` [PATCH net 06/10] net/mlx5e: Fix IPv6 neighbour fallback for IPsec tunnels Tariq Toukan
                   ` (4 subsequent siblings)
  9 siblings, 1 reply; 18+ 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] 18+ messages in thread

* [PATCH net 06/10] net/mlx5e: Fix IPv6 neighbour fallback for IPsec tunnels
  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 07/10] net/mlx5e: Copy back only replay_esn after an IPsec ESN update Tariq Toukan
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 18+ 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_init_macs() falls back to a neighbour lookup when the route
lookup yields no neighbour, but always in the ARP table. For an IPv6 SA
the first four bytes of the address are used as an IPv4 key: the SA
takes the MAC of an unrelated ARP entry, or creates one, probes for it
and drops traffic waiting for a reply that never comes.

Look the neighbour up in the table of the SA's address family.

Found while debugging IPsec tunnel failures on a ConnectX-8 Kubernetes
cluster. Reproduced on ConnectX-7 (fw 28.48.1000): IPv6 RX SA with an
unreachable route to the peer fd00:99::2. An ARP entry for 253.0.0.153
is probed and the SA drops all packets. With the fix the peer is
resolved by ND and traffic passes.

Fixes: 37a417ca911a ("net/mlx5e: Support IPsec RX packet offload in tunnel mode")
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>
---
 .../net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c   | 10 ++++++++--
 1 file changed, 8 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 4daad648460f..4084f8a88695 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
@@ -365,9 +365,15 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,
 	return;
 
 neigh:
-	n = ipv4_neigh_lookup(netdev, pkey);
+	if (addrs->family == AF_INET)
+		n = ipv4_neigh_lookup(netdev, pkey);
+	else
+		n = ipv6_neigh_lookup(netdev, pkey);
 	if (!n) {
-		n = ipv4_neigh_create(netdev, pkey);
+		if (addrs->family == AF_INET)
+			n = ipv4_neigh_create(netdev, pkey);
+		else
+			n = ipv6_neigh_create(netdev, pkey);
 		attrs->drop = true;
 		if (IS_ERR(n))
 			return;
-- 
2.44.0


^ permalink raw reply related	[flat|nested] 18+ 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
                   ` (5 preceding siblings ...)
  2026-11-26 21:56 ` [PATCH net 06/10] net/mlx5e: Fix IPv6 neighbour fallback for IPsec tunnels 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)
  9 siblings, 0 replies; 18+ 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] 18+ 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
                   ` (6 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-27 22:32   ` netdev-bot+sashiko
  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
  9 siblings, 1 reply; 18+ 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] 18+ 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
                   ` (7 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
  9 siblings, 0 replies; 18+ 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] 18+ 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
                   ` (8 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
  2026-11-27 22:32   ` netdev-bot+sashiko
  9 siblings, 1 reply; 18+ 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] 18+ messages in thread

* Re: [PATCH net 01/10] net/mlx5e: Initialize IPsec XArrays with XA_FLAGS_LOCK_BH
  2026-11-26 21:56 ` [PATCH net 01/10] net/mlx5e: Initialize IPsec XArrays with XA_FLAGS_LOCK_BH Tariq Toukan
@ 2026-11-27 22:32   ` netdev-bot+sashiko
  0 siblings, 0 replies; 18+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 22:32 UTC (permalink / raw)
  To: tariqt
  Cc: andrew+netdev, davem, edumazet, kuba, netdev, pabeni, willemb,
	parri.andrea, borisp, cratiu, eedmunds, gal, jmassar, jianbol,
	kuniyu, leon, linux-rdma, mbloch, phaddad, raeds, saeedm, horms,
	sridhar.samudrala, steffen.klassert

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message overstates what the change to
  `ipsec_obj_id_map` fixes.

--

net/mlx5e: Initialize IPsec XArrays with XA_FLAGS_LOCK_BH

This commit adds XA_FLAGS_LOCK_BH when ipsec->sadb and
ipsec->ipsec_obj_id_map are initialized, because every mutation of
these arrays uses the _bh xarray accessors. The commit message says the
mismatch leaves bottom halves disabled while __xas_nomem() allocates a
node. It carries Fixes tags for both 403b383a3ce3 and 91bafc638ed4.

> 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.

[ ... ]

> Fixes: 403b383a3ce3 ("net/mlx5e: Store all XFRM SAs in Xarray")
> Fixes: 91bafc638ed4 ("net/mlx5e: Handle IPsec offload for RX datapath in switchdev mode")

[ ... ]

> 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 5c69e89b13a72..502d89a18f9ec 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);

[Severity: Low]
Is the Fixes: 91bafc638ed4 tag accurate for this hunk?

The description fits sadb. In mlx5e_xfrm_add_state(), xa_insert_bh() on
sadb is called with GFP_KERNEL, so __xas_nomem() drops and retakes the
lock using the configured lock type.

For ipsec_obj_id_map, the only call that allocates passes gfp == 0:

drivers/net/ethernet/mellanox/mlx5/core/esw/ipsec_fs.c:mlx5_esw_ipsec_rx_setup_modify_header() {
    ...
	err = xa_alloc_bh(&ipsec->ipsec_obj_id_map, &mapped_id,
			  xa_mk_value(sa_entry->ipsec_obj_id),
			  XA_LIMIT(1, ESW_IPSEC_RX_MAPPED_ID_MASK), 0);
    ...
}

With gfp == 0, gfpflags_allow_blocking() returns false. __xas_nomem()
then allocates with the lock still held and never calls
xas_unlock_type() or xas_lock_type():

lib/xarray.c:__xas_nomem() {
    ...
	if (gfpflags_allow_blocking(gfp)) {
		xas_unlock_type(xas, lock_type);
		xas->xa_alloc = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp);
		xas_lock_type(xas, lock_type);
	} else {
		xas->xa_alloc = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp);
	}
    ...
}

The other users of this array are xa_erase_bh() and xa_load(), and
neither of them allocates. The gfp was already 0 when 91bafc638ed4
added this code.

That would make this hunk a harmless consistency change rather than a
fix. Could the commit message say that the ipsec_obj_id_map change is
only for consistency? Alternatively, could the second Fixes tag be
dropped, so backporters don't assume 91bafc638ed4 had a live
sleep-in-atomic bug?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126215614.792598-1-tariqt%40nvidia.com

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

* Re: [PATCH net 02/10] net/mlx5e: Stop counting packets dropped by IPsec RX SA rules
  2026-11-26 21:56 ` [PATCH net 02/10] net/mlx5e: Stop counting packets dropped by IPsec RX SA rules Tariq Toukan
@ 2026-11-27 22:32   ` netdev-bot+sashiko
  0 siblings, 0 replies; 18+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 22:32 UTC (permalink / raw)
  To: tariqt
  Cc: andrew+netdev, davem, edumazet, kuba, netdev, pabeni, willemb,
	parri.andrea, borisp, cratiu, eedmunds, gal, jmassar, jianbol,
	kuniyu, leon, linux-rdma, mbloch, phaddad, raeds, saeedm, horms,
	sridhar.samudrala, steffen.klassert

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 0 · 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).

- [Medium] In rx_add_rule()
  (drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c), the…

--

net/mlx5e: Stop counting packets dropped by IPsec RX SA rules

This patch removes the COUNT action and the counter destination from the
RX SA rule used when attrs->drop is set. Packets dropped in hardware are
then no longer added to x->curlft.packets for packet offload. The goal is
to match crypto offload accounting, and the rule keeps CRYPTO_DECRYPT so
ESN state keeps updating.

> 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 502d89a18f9ec..0facc54771558 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

[ ... ]

> @@ -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);

[Severity: Medium]
Could this cause x->curlft.packets to drift away from the hardware ASO
packet-lifetime counter for packet offload RX SAs?

The drop rule still decrypts against sa_entry->ipsec_obj_id, so the ASO
context for that object keeps being processed. For any finite packet limit,
mlx5e_ipsec_packet_setup() in ipsec_offload.c arms the lifetime counter in
the same ASO context without checking the direction:

	if (attrs->lft.hard_packet_limit != XFRM_INF) {
		MLX5_SET(ipsec_aso, aso_ctx, remove_flow_pkt_cnt,
			 attrs->lft.hard_packet_limit);
		MLX5_SET(ipsec_aso, aso_ctx, hard_lft_arm, 1);
		MLX5_SET(ipsec_aso, aso_ctx, remove_flow_enable, 1);
	}

x->curlft.packets, however, is derived only from the SA rule flow counter
in mlx5e_xfrm_update_stats():

	mlx5_fc_query_cached(ipsec_rule->fc, &bytes, &packets, &lastuse);
	success_packets = packets - auth_packets - trailer_packets - replay_packets;
	x->curlft.packets += success_packets;

Before this patch, both counts included packets received in the drop
state. After it, only the hardware counter would, assuming the device
decrements remove_flow_pkt_cnt for packets that are decrypted and then
dropped by the same FTE. The code does not show whether the device does
this. Keeping DECRYPT so the ASO state keeps updating suggests that it
might.

If so, when the hardware soft lifetime event fires,
mlx5e_ipsec_handle_limits() checks curlft once and marks the soft limit
as handled:

		sa_entry->limits.soft_limit_hit = true;
		/* Notify about soft limit */
		xfrm_state_check_expire(sa_entry->x);

At that point curlft can be short by the number of packets received while
attrs->drop was set. The soft expiry (rekey) would then be missed on this
path.

In the final round, attrs->lft.soft_packet_limit is set to XFRM_INF, so
mlx5e_ipsec_handle_event() no longer calls mlx5e_ipsec_handle_limits(). Since
remove_flow_enable is set, can the hardware retire the SA while the xfrm
state is still valid, and drop traffic until curlft catches up?

The software poller mlx5e_ipsec_handle_sw_limits() returns early without
requeueing whenever attrs.drop is set. So the catch-up might also not happen.

The commit message says:

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

Crypto offload does not arm the ASO lifetime counter. Is it intended that
the hardware lifetime for packet offload RX SAs still counts these drops
while curlft no longer does?

> +	} 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);
> +	}

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126215614.792598-1-tariqt%40nvidia.com

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

* Re: [PATCH net 03/10] net/mlx5e: Fix IPsec SA modify/stats update race
  2026-11-26 21:56 ` [PATCH net 03/10] net/mlx5e: Fix IPsec SA modify/stats update race Tariq Toukan
@ 2026-11-27 22:32   ` netdev-bot+sashiko
  0 siblings, 0 replies; 18+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 22:32 UTC (permalink / raw)
  To: tariqt
  Cc: andrew+netdev, davem, edumazet, kuba, netdev, pabeni, willemb,
	parri.andrea, borisp, cratiu, eedmunds, gal, jmassar, jianbol,
	kuniyu, leon, linux-rdma, mbloch, phaddad, raeds, saeedm, horms,
	sridhar.samudrala, steffen.klassert

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 (3) · Low: 1

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).

- [Medium] This patch moves rx_mapped_id into struct mlx5e_ipsec_rule, so
  the existing `memset(&sa_entry_shadow.ipsec_rule, 0, ...)` in…
- [Low] The commit message does not match the code.

Pre-existing issues:
- [Medium] Baseline decision is false: the last patch in the series fixes
  this.
- [Medium] Baseline decision is false: pre-existing, medium severity, and
  not claimed or relied on by this patch.
- [Medium] Baseline decision is false: the last patch in the series fixes
  this.

--

net/mlx5e: Fix IPsec SA modify/stats update race

This commit moves the eswitch RX mapped id from struct
mlx5e_ipsec_sa_entry into struct mlx5e_ipsec_rule. It also changes
mlx5e_accel_ipsec_fs_modify(): the new rule is now swapped into
sa_entry under x->lock and the old rule is deleted afterwards. Before,
the old rule was deleted first and the whole shadow entry was memcpy'd
back.

> 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.

[Severity: Low]
Is "swap the pointer" accurate here? ipsec_rule is an embedded member,
not a pointer:

struct mlx5e_ipsec_sa_entry {
	...
	struct mlx5e_ipsec_rule ipsec_rule;
	...
};

So swap(sa_entry->ipsec_rule, sa_entry_shadow.ipsec_rule) copies the
whole struct by value. That covers the flow handles, modify_hdr,
pkt_reformat, the four mlx5_fc pointers and rx_mapped_id. It is a
multi-word copy, and only readers holding x->lock see it atomically.

The message also doesn't mention that the old write-back was removed:

	memcpy(sa_entry, &sa_entry_shadow, sizeof(*sa_entry));

Without it, attrs, esn_state, limits and ctx are no longer reset to the
snapshot taken at the start of mlx5e_accel_ipsec_fs_modify(). That
looks harmless, since the add_rule paths only write ipsec_rule fields.

Could the commit message describe both of these more precisely?

> 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 f4b2d7b950c00..9bbcd9e4c3197 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;
>  };

[Severity: Medium]
Now that rx_mapped_id lives in struct mlx5e_ipsec_rule, does the
existing memset in mlx5e_accel_ipsec_fs_modify() also clear the
shadow's mapped id?

mlx5e_accel_ipsec_fs_modify() {
	...
	memcpy(&sa_entry_shadow, sa_entry, sizeof(*sa_entry));
	memset(&sa_entry_shadow.ipsec_rule, 0x00, sizeof(sa_entry->ipsec_rule));

	err = mlx5e_accel_ipsec_fs_add_rule(&sa_entry_shadow);
	if (err)
		return;
	...
}

rx_add_rule() only allocates a new mapped id when the SA is not in drop
mode:

	if (!attrs->drop) {
		if (rx != ipsec->rx_esw)
			...
		else
			err = mlx5_esw_ipsec_rx_setup_modify_header(sa_entry, &flow_act);

Consider mlx5e_ipsec_handle_sw_limits() setting attrs.drop = true and
calling mlx5e_accel_ipsec_fs_modify() for an RX SA on rx_esw.
mlx5_esw_ipsec_rx_rule_add_match_obj() then builds the auth, trailer
and replay status drop rules with metadata_reg_c_1 == 0. Before this
patch, the modify path reused the SA's existing unique id.

On rx_esw, reg_c_1 is the only per-SA field in those matches. Every
drop-mode SA therefore ends up with identical FTEs that use
FLOW_ACT_NO_APPEND. SAs added in drop mode because the neighbour was
unresolved already had id 0. This patch extends that to every SA that
hits hard expiry.

Under HW steering, mlx5_cmd_hws_get_capabilities() doesn't report
MLX5_FLOW_STEERING_CAP_DUPLICATE_MATCH for FDB. add_rule_dup_match_fte()
then allows only one duplicate:

	if (!list_empty(&fte->dup->children)) {
		mlx5_core_warn(get_dev(&fte->node),
			       "Can have only a single duplicate rule\n");
		return ERR_PTR(-EEXIST);

Suppose two or more drop-mode SAs are already installed on rx_esw. Can
rx_add_rule() then fail for the expiring SA? If it does,
mlx5e_accel_ipsec_fs_modify() returns silently. Software has
attrs.drop = true, but the hardware keeps the old decrypt and forward
rule until the state is freed.

[ ... ]

> 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 0facc54771558..d0f738fdeff3d 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);

[Severity: Medium]
This is a pre-existing issue, but does taking x->lock here fully close
the race the commit message describes?

mlx5e_xfrm_update_stats() accepts other locks instead of x->lock:

	lockdep_assert(lockdep_is_held(&x->lock) ||
		       lockdep_is_held(&net->xfrm.xfrm_cfg_mutex) ||
		       lockdep_is_held(&net->xfrm.xfrm_state_lock));

Some callers hold only xfrm_state_lock or xfrm_cfg_mutex:

xfrm_state_update_stats()      /proc/net/xfrm_stat, xfrm_state_lock
  -> xfrm_dev_state_update_stats()
    -> mlx5e_xfrm_update_stats()

copy_to_user_state()           SA dump under xfrm_state_lock,
                               xfrm_get_sa under xfrm_cfg_mutex
  -> xfrm_dev_state_update_stats()
    -> mlx5e_xfrm_update_stats()

The following sequence looks possible:

1. Such a reader loads an old ipsec_rule->fc (or auth.fc, trailer.fc,
   replay.fc) pointer.
2. The worker runs mlx5e_accel_ipsec_fs_del_rule(&sa_entry_shadow),
   which calls mlx5_fc_destroy().
3. The reader's mlx5_fc_query_cached() then reads and writes
   lastbytes/lastpackets on the freed mlx5_fc.

The same race existed before this commit. The last patch in this
series, "net/mlx5e: Create IPsec flow counters once per SA", moves the
counters into sa_entry->fcs[] and stops destroying them in del_rule. It
is still reachable at this commit, though.

[Severity: Medium]
This is also a pre-existing issue, but are counter deltas lost here?

mlx5e_accel_ipsec_fs_del_rule(&sa_entry_shadow) destroys the old
ipsec_rule.fc, auth.fc, trailer.fc and replay.fc without a final
mlx5_fc_query_cached(). Any delta gathered since the last
mlx5e_xfrm_update_stats() never reaches x->curlft.packets/bytes or
x->stats.integrity_failed/replay. The replacement counters start at
zero, so each neighbour update or expiry rebuild under-counts the SA.

The same later series commit, "net/mlx5e: Create IPsec flow counters
once per SA", keeps the counters in sa_entry->fcs[] across rebuilds. Its
commit message says it fixes these lost counter values.

>  }

[ ... ]

> 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 f8c97954b6eaf..2615bbc89a16d 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;

[Severity: Medium]
This isn't a bug introduced by this patch, but does rx_add_rule() leak
this mapped id on its later error paths?

Once mlx5_esw_ipsec_rx_setup_modify_header() succeeds, it has allocated
an id in ipsec->ipsec_obj_id_map with xa_alloc_bh(). Later steps can
still fail: setup_pkt_reformat, mlx5_fc_create, mlx5_add_flow_rules,
and the sa_sel, replay, auth and trailer rule creation. Those failures
unwind through err_add_flow, err_add_cnt, err_pkt_reformat and
err_mod_header. These labels free the modify header but never erase
the xarray entry.

mlx5_esw_ipsec_rx_id_mapping_remove() is only called from
mlx5e_accel_ipsec_fs_del_rule(). In the modify path the id is held only
in the on-stack shadow, so it is lost for good when this returns:

	err = mlx5e_accel_ipsec_fs_add_rule(&sa_entry_shadow);
	if (err)
		return;

The same leak existed with sa_entry->rx_mapped_id, and it is still
present at the end of the series.

>  	flow_act->modify_hdr = modify_hdr;
>  	flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
>  

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126215614.792598-1-tariqt%40nvidia.com

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

* Re: [PATCH net 04/10] net/mlx5e: Drain pending hardware event work before freeing IPsec SA
  2026-11-26 21:56 ` [PATCH net 04/10] net/mlx5e: Drain pending hardware event work before freeing IPsec SA Tariq Toukan
@ 2026-11-27 22:32   ` netdev-bot+sashiko
  0 siblings, 0 replies; 18+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 22:32 UTC (permalink / raw)
  To: tariqt
  Cc: andrew+netdev, davem, edumazet, kuba, netdev, pabeni, willemb,
	parri.andrea, borisp, cratiu, eedmunds, gal, jmassar, jianbol,
	kuniyu, leon, linux-rdma, mbloch, phaddad, raeds, saeedm, horms,
	sridhar.samudrala, steffen.klassert

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message leaves out a second change to IPsec init and
  teardown ordering.

--

net/mlx5e: Drain pending hardware event work before freeing IPsec SA

The IPsec hardware event work item now lives in struct
mlx5e_ipsec_sa_entry instead of being allocated for each event. This lets
mlx5e_xfrm_free_state() call mlx5_eq_synchronize_async_irq() and then
cancel_work_sync() on it before the SA is freed. The patch also moves the
mlx5 notifier registration out of the ASO helpers and into
mlx5e_ipsec_init() and mlx5e_ipsec_cleanup().

> 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 a549851c83827..ea7a1ae2056fa 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c

[ ... ]

> @@ -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;
>  	}

[ ... ]

> @@ -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;
>  }

[Severity: Low]
Could the commit message also describe this init and teardown change?
Right now it only covers embedding event_work in sa_entry and cancelling
it in mlx5e_xfrm_free_state().

The patch also makes these changes:

  - mlx5_notifier_register() and mlx5_notifier_unregister() for ipsec->nb
    move out of mlx5e_ipsec_aso_init() and mlx5e_ipsec_aso_cleanup() in
    ipsec_offload.c. They now live in mlx5e_ipsec_init() and
    mlx5e_ipsec_cleanup().

  - mlx5e_ipsec_init() now checks the return value of
    mlx5_notifier_register(), which was ignored before. It also gains a
    new err_notifier unwind label.

  - mlx5e_ipsec_cleanup() now calls destroy_workqueue() before
    mlx5e_ipsec_aso_cleanup(), where it used to call it after.

The reorder looks like a fix for a separate lifetime problem. Before this
patch, event work still pending on ipsec->wq could run this path after the
ASO had been freed and ipsec->aso set to NULL:

  mlx5e_ipsec_handle_event()->mlx5e_ipsec_aso_query()

The new ordering looks correct. Without a note in the changelog, though,
someone backporting this may not see that the ordering change is needed.

Would it make sense to add a sentence explaining why the workqueue has to
be drained before the ASO is torn down?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126215614.792598-1-tariqt%40nvidia.com

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

* Re: [PATCH net 05/10] net/mlx5e: Drop IPsec tunnel traffic if the neigh cannot be created
  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-27 22:32   ` netdev-bot+sashiko
  0 siblings, 0 replies; 18+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 22:32 UTC (permalink / raw)
  To: tariqt
  Cc: andrew+netdev, davem, edumazet, kuba, netdev, pabeni, willemb,
	parri.andrea, borisp, cratiu, eedmunds, gal, jmassar, jianbol,
	kuniyu, leon, linux-rdma, mbloch, phaddad, raeds, saeedm, horms,
	sridhar.samudrala, steffen.klassert

Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 3 · 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).

- [Medium] The patch only covers the case where neighbour creation fails.
- [Medium] Setting attrs->drop = true when the neighbour cannot be
  created permanently stops the SA's software lifetime worker.
- [Medium] The new 'attrs->drop = true' on neighbour-creation failure in
  mlx5e_ipsec_init_macs() (ipsec.c:371) is not limited to the TX…

--

net/mlx5e: Drop IPsec tunnel traffic if the neigh cannot be created

This patch moves the attrs->drop assignment in mlx5e_ipsec_init_macs() so
it happens before the IS_ERR() check on the result of ipv4_neigh_create().
When the peer neighbour cannot be created, the tunnel packet offload SA
is now installed as a drop rule. Before, it forwarded traffic with an
all-zero destination MAC.

> 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 ea7a1ae2056fa..4daad648460fe 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;

[Severity: Medium]
Should this drop be limited to XFRM_DEV_OFFLOAD_OUT?

For an inbound SA, mlx5e_ipsec_init_macs() sets src = attrs->dmac (the
netdev's own MAC) and dst = attrs->smac (the peer MAC). The unresolved
peer MAC only becomes the source MAC of the L2 header that
setup_pkt_reformat() adds to the decrypted inner frame:

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c:setup_pkt_reformat() {
    ...
	ether_addr_copy(eth_hdr->h_dest, attrs->dmac);
	ether_addr_copy(eth_hdr->h_source, attrs->smac);
    ...
}

Before this patch, an RX SA in this state still delivered decrypted
traffic to the host, with h_source set to 00:00:00:00:00:00. With drop
set, rx_add_rule() now installs CRYPTO_DECRYPT|DROP:

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c:rx_add_rule() {
    ...
	if (attrs->drop) {
		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
		rule = mlx5_add_flow_rules(rx->ft.sa, spec, &flow_act, NULL, 0);
    ...
}

n is an ERR_PTR here, so neigh_event_send() is never called and no
resolution starts.

Could inbound traffic for this SA stay blackholed until some unrelated
event creates and validates the neighbour? That can't happen while the
table is still full.

The commit message only describes the TX case ("add the TX SA ...").

[Severity: Medium]
Does setting drop here permanently stop the software lifetime worker for
this SA?

mlx5e_xfrm_add_state() queues sa_entry->dwork once, MLX5_IPSEC_RESCHED
after install. mlx5e_ipsec_handle_sw_limits() returns early when drop is
set and only re-queues itself at the end:

mlx5e_ipsec_handle_sw_limits() {
	...
	if (sa_entry->attrs.drop)
		return;
	...
	xfrm_state_check_expire(x);
	spin_unlock_bh(&x->lock);

	queue_delayed_work(sa_entry->ipsec->wq, &dwork->dwork,
			   MLX5_IPSEC_RESCHED);
}

This IS_ERR() branch sends no probe. Drop is therefore almost certainly
still set when the worker first runs, and the worker is never queued
again.

If the neighbour resolves later, mlx5e_ipsec_handle_netdev_event()
clears the flag and reprograms the rules to forward:

	attrs->drop = false;
	mlx5e_accel_ipsec_fs_modify(sa_entry);

It does not re-arm the dwork. Would the SA then forward traffic without
any xfrm_state_check_expire() calls, so the soft and hard byte limits
are never enforced?

Before this patch, the IS_ERR() branch left drop false and the worker
kept running. The root cause seems to be that attrs->drop means both
"expired" and "neighbour unresolved".

A later commit in the series, "net/mlx5e: Copy back only replay_esn
after an IPsec ESN update", fixes the ESN rebuild variant of this
problem. At the end of the series, the early return in
mlx5e_ipsec_handle_sw_limits() and the missing re-arm in
mlx5e_ipsec_handle_netdev_event() are both still there.

>  		neigh_event_send(n, NULL);
> -		attrs->drop = true;
>  	} else {
>  		neigh_ha_snapshot(addr, n, netdev);
>  		ether_addr_copy(dst, addr);

[Severity: Medium]
The commit message says this makes creation failure behave "as for a
created but still unresolved neigh". Does that hold outside the create
branch of the fallback path?

In this else branch, ipv4_neigh_lookup() can return an existing
neighbour that is INCOMPLETE or FAILED. Its zero ha is copied into dmac,
drop stays false, and no probe is sent.

The route path earlier in mlx5e_ipsec_init_macs() shows the same
symptom. dst_neigh_lookup() goes through
ipv4_dst_neigh_lookup()->ip_neigh_gw4(), which calls __neigh_create()
on a miss. It returns a new unresolved neighbour instead of NULL, so the
fallback is never reached:

	n = dst_neigh_lookup(rt_dst_entry, pkey);
	...
	neigh_ha_snapshot(addr, n, netdev);
	ether_addr_copy(dst, addr);
	if (attrs->dir == XFRM_DEV_OFFLOAD_OUT &&
	    is_zero_ether_addr(addr))
		neigh_event_send(n, NULL);

attrs->drop is never set on that path, and tx_add_rule() chooses the
action from attrs->drop alone:

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c:tx_add_rule() {
    ...
	if (attrs->drop)
		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
	else
		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
    ...
}

Suppose a route to the peer exists but the next hop is not resolved yet,
for example the first SA toward a new peer. Won't the TX SA still be
installed with dmac 00:00:00:00:00:00 and forwarding enabled? That is
the symptom this commit describes.

Commit 9ab89bde13e5 describes this route path case ("the hardware rule
is programmed with an all-zero destination MAC address") but only adds a
probe, not a drop. The route path still has no drop at the end of the
series.

A later commit in the series, "net/mlx5e: Validate neighbour events
before updating IPsec SAs", fixes the related problem that gateway
neighbour updates were never matched.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126215614.792598-1-tariqt%40nvidia.com

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

* Re: [PATCH net 08/10] net/mlx5e: Validate neighbour events before updating IPsec SAs
  2026-11-26 21:56 ` [PATCH net 08/10] net/mlx5e: Validate neighbour events before updating IPsec SAs Tariq Toukan
@ 2026-11-27 22:32   ` netdev-bot+sashiko
  0 siblings, 0 replies; 18+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 22:32 UTC (permalink / raw)
  To: tariqt
  Cc: andrew+netdev, davem, edumazet, kuba, netdev, pabeni, willemb,
	parri.andrea, borisp, cratiu, eedmunds, gal, jmassar, jianbol,
	kuniyu, leon, linux-rdma, mbloch, phaddad, raeds, saeedm, horms,
	sridhar.samudrala, steffen.klassert

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 (1) · Low: 0 (1)

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).

- [Medium] With nh.ifindex == 0, a foreign neighbour on any device in the
  same netns can still overwrite the hardware MAC.

Pre-existing issues:
- [Medium] In the route-resolved branch of mlx5e_ipsec_init_macs(),
  dst_neigh_lookup() can return an unresolved neighbour with an all-zero…
- [Low] In this commit, the per-SA MAC buffer work->data->addr has no
  synchronization.

--

net/mlx5e: Validate neighbour events before updating IPsec SAs

The patch stops matching neighbour events against the SA source and
destination addresses. Instead it saves a snapshot of the resolved nexthop
(neigh table, ifindex, key) in mlx5e_ipsec_init_macs() and matches events
against that. The ifindex is saved only when the nexthop device is the SA's
offload device (xso.dev); otherwise 0 is saved and any device in the netns
matches.

> 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 4084f8a88695b..cc75fcb0aa152 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;

[Severity: Medium]
If the nexthop device is not xso.dev, nh.ifindex is saved as 0.
mlx5e_ipsec_neigh_matches() then treats 0 as "any device":

    (!attrs->nh.ifindex || attrs->nh.ifindex == n->dev->ifindex) &&

In that case, can a foreign neighbour on another device in the same netns
still overwrite the programmed MAC?

This looks like it always happens in the bonding fallback path.
bond_ipsec_add_sa() calls:

    real_dev->xfrmdev_ops->xdo_dev_state_add(real_dev, xs, extack);

so mlx5e_xfrm_add_state() sets sa_entry->dev to the slave, while xso.dev is
the bond. The neigh: and IS_ERR branches of mlx5e_ipsec_init_macs() pass
that slave netdev, so ifindex 0 is saved. The route branch ends up the same
way whenever the route's output device is not xso.dev.

For example, a container behind a host-side veth could send an ARP request
with sip set to the nexthop IP and tip set to a host address.
arp_process()->neigh_event_ns() then creates a NUD_STALE neighbour on the
host veth and raises NETEVENT_NEIGH_UPDATE.

The table, the wildcard ifindex and the key all match. neigh_ha_snapshot()
copies the attacker's MAC, and the worker reprograms the TX dmac or RX smac.

The commit message documents the ifindex 0 trade-off. However, this looks
like the case it describes as fixed:

  "a foreign neighbour (veth, other netdev or netns) sharing the SA
   address overwrites the programmed MAC"

The only difference is that the nexthop IP is the target instead of the SA
IP.

Would a tighter match close this? For example, n->dev could be accepted
only when it is xso.dev or the nexthop device, instead of using a wildcard.

> +	memcpy(attrs->nh.key, pkey, tbl->key_len);
> +}

[ ... ]

> @@ -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);

[Severity: Medium]
This is a pre-existing issue and was not introduced by this patch. Should
the route branch of mlx5e_ipsec_init_macs() set attrs->drop while the
nexthop MAC is still zero?

dst_neigh_lookup() can return an unresolved neighbour whose ha is all
zeros. For IPv4, the route's neigh_lookup creates one if it is missing.
This branch already checks for that with is_zero_ether_addr(addr).

The zero address is still copied into attrs->dmac (OUT) or attrs->smac
(IN), and attrs->drop stays false.

The TX rule adds MLX5_FLOW_CONTEXT_ACTION_DROP only when attrs->drop is
set. setup_pkt_tunnel_reformat() copies attrs->dmac into the encap header
as is:

    ether_addr_copy(eth_hdr->h_dest, attrs->dmac);

Until the neighbour resolves and the netevent worker reprograms the rule,
could encrypted tunnel frames go out with destination MAC
00:00:00:00:00:00?

The fallback branch sets attrs->drop = true for the same unresolved state.
So does the IS_ERR case added by "net/mlx5e: Drop IPsec tunnel traffic if
the neigh cannot be created".

The problem is still there at the end of the series. This patch does make
the window shorter: gateway events now match, so the rule gets reprogrammed
once the gateway resolves.

>  	dst_release(rt_dst_entry);
>  	neigh_release(n);
>  	return;

[ ... ]

> @@ -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;

[ ... ]

>  		data = sa_entry->work->data;
>  
>  		neigh_ha_snapshot(data->addr, n, sa_entry->dev);

[Severity: Low]
This isn't a bug introduced by this patch, but nothing synchronizes access
to data->addr here. mlx5e_ipsec_netevent_event() writes it from the atomic
netevent chain. mlx5e_ipsec_handle_netdev_event() reads it from the
workqueue and programs the result into hardware:

    ether_addr_copy(attrs->dmac, data->addr);

Can a worker that is already running read a torn MAC while the notifier
rewrites the buffer? With the ifindex 0 wildcard, two notifier calls on
different CPUs could also write the buffer at the same time.

The next patch in the series, "net/mlx5e: Serialize IPsec netevent MAC
snapshots", appears to fix this. It adds a spinlock_t to struct
mlx5e_ipsec_netevent_data and takes spin_lock_bh() around
neigh_ha_snapshot(). The worker then copies data->addr into a local buffer
under the same lock.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126215614.792598-1-tariqt%40nvidia.com

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

* Re: [PATCH net 10/10] net/mlx5e: Create IPsec flow counters once per SA
  2026-11-26 21:56 ` [PATCH net 10/10] net/mlx5e: Create IPsec flow counters once per SA Tariq Toukan
@ 2026-11-27 22:32   ` netdev-bot+sashiko
  0 siblings, 0 replies; 18+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 22:32 UTC (permalink / raw)
  To: tariqt
  Cc: andrew+netdev, davem, edumazet, kuba, netdev, pabeni, willemb,
	parri.andrea, borisp, cratiu, eedmunds, gal, jmassar, jianbol,
	kuniyu, leon, linux-rdma, mbloch, phaddad, raeds, saeedm, horms,
	sridhar.samudrala, steffen.klassert

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The patch leaves dead per-SA counter fields behind.

--

net/mlx5e: Create IPsec flow counters once per SA

The IPsec flow counters used to be created and destroyed together with
the flow rules, so every rule update (for example after a neighbour
update) recreated them and lost the earlier counter values. This patch
creates the counters once when the SA is added, frees them when the SA
is freed, and moves them into a new sa_entry->fcs[] array.

> 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 e70189f81ffb9..2e8c6fc216de3 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;

[Severity: Low]
This isn't a bug, but should replay, auth and trailer here still be
struct mlx5e_ipsec_drop?

After this patch nothing reads or writes
sa_entry->ipsec_rule.{replay,auth,trailer}.fc:

  rx_add_rule_drop_auth_trailer() and rx_add_rule_drop_replay() set
  only .rule.

  mlx5e_accel_ipsec_fs_del_rule() and the rx_add_rule() error path
  delete only .rule.

  mlx5e_xfrm_update_stats() now reads sa_entry->fcs[].

The only remaining user of mlx5e_ipsec_drop::fc is the shared
rx->status_checks.all rule in rx_create_status_checks(). That rule is
not per-SA.

As a result, every SA carries three counter pointers that are always
NULL, next to the real counters in sa_entry->fcs[]. A later change
that reads ipsec_rule.auth.fc, as mlx5e_xfrm_update_stats() did
before this patch, would still compile. It would then pass NULL to
mlx5_fc_query_cached(), which dereferences the counter without a
check.

Could these three members become plain struct mlx5_flow_handle
pointers, or use a rule-only type?

[ ... ]

> 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 d0f738fdeff3d..df90ae327f23f 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

[ ... ]

> @@ -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];

This is where the last write to ipsec_rule.auth.fc is removed. The same
happens to trailer.fc later in this function and to replay.fc in
rx_add_rule_drop_replay().

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126215614.792598-1-tariqt%40nvidia.com

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

end of thread, other threads:[~2026-11-27 22:32 UTC | newest]

Thread overview: 18+ 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-27 22:32   ` netdev-bot+sashiko
2026-11-26 21:56 ` [PATCH net 02/10] net/mlx5e: Stop counting packets dropped by IPsec RX SA rules Tariq Toukan
2026-11-27 22:32   ` netdev-bot+sashiko
2026-11-26 21:56 ` [PATCH net 03/10] net/mlx5e: Fix IPsec SA modify/stats update race Tariq Toukan
2026-11-27 22:32   ` netdev-bot+sashiko
2026-11-26 21:56 ` [PATCH net 04/10] net/mlx5e: Drain pending hardware event work before freeing IPsec SA Tariq Toukan
2026-11-27 22:32   ` netdev-bot+sashiko
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-27 22:32   ` netdev-bot+sashiko
2026-11-26 21:56 ` [PATCH net 06/10] net/mlx5e: Fix IPv6 neighbour fallback for IPsec tunnels 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-27 22:32   ` netdev-bot+sashiko
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
2026-11-27 22:32   ` netdev-bot+sashiko

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