Skip to contentSkip to conversation

net/mlx5: macsec: free struct mlx5_fs_id after an RCU grace period

PATCH In review Joas Antonio dos Santos 6 participants 8 messages Oct 27 – Nov 4

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net/mlx5: macsec: free struct mlx5_fs_id after an RCU grace period

macsec_fs_id_del() removes a struct mlx5_fs_id from its rhashtable with rhashtable_remove_fast() and then frees it with kfree() right away.

The TX (SCI) table is read locklessly from the transmit path:
mlx5e_macsec_handle_tx_skb() and mlx5e_macsec_tx_build_eseg() call mlx5_macsec_fs_get_fs_id_from_hashtable(), which does rhashtable_lookup() under rcu_read_lock() and then reads mlx5_fs_id->id. A transmit that looked the entry up just before it was removed reads freed memory.

Before commit d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec device inside macsec steering") the SCI table held struct mlx5e_macsec_sa directly, and mlx5e_macsec_del_txsa() freed it with kfree_rcu_mightsleep(). That commit moved the table into macsec_fs.c with the new struct mlx5_fs_id, but frees it with a plain kfree().

Add an rcu_head to struct mlx5_fs_id and free it with kfree_rcu().

The same issue was flagged by the Sashiko AI review as a pre-existing problem while reviewing the HW-GRO for PSP series.

Fixes d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec device inside macsec steering")
Assisted-by Yaga Agent HackerSec Claude:claude-opus-5-5

---
v2:
- Drop the macsec.c hunk: the sc_xarray_element free was already fixed by ad17ad50b8dc ("net: mlx5e: Fix use-after-free of the MACsec xarray entry on RX SC deletion"). (Simon)
- Embed an rcu_head and use kfree_rcu() instead of kvfree_rcu_mightsleep(). (Simon)
- Fixes: now points at the commit that introduced the kfree().
- v1: https://lore.kernel.org/all/[email protected]/

Testing: compile-tested only (arm64, W=1, CONFIG_MLX5_MACSEC=y, no new warnings). I do not have
MACsec-capable ConnectX hardware, so this was not run on a device.

 drivers/net/ethernet/mellanox/mlx5/core/lib/macsec_fs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/lib/macsec_fs.c +2 -1
@@ -94,6 +94,7 @@ struct mlx5_fs_id {
 	refcount_t refcnt;
 	sci_t sci;
 	struct rhash_head hash;
+	struct rcu_head rcu;
 };
 
 struct mlx5_macsec_device {
@@ -684,7 +685,7 @@ static void macsec_fs_id_del(struct list_head *macsec_devices_list, u32 fs_id,
 
 	xa_erase(fs_id_xa, fs_id);
 
-	kfree(fs_id_found);
+	kfree_rcu(fs_id_found, rcu);
 
 	if (macsec_fs_is_macsec_device_empty(macsec_device)) {
 		list_del(&macsec_device->macsec_devices_list_entry);

--
2.39.5

#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 16 lines

macsec_fs_id_del() removes a struct mlx5_fs_id from its rhashtable with rhashtable_remove_fast() and then frees it with kfree() right away.

The TX (SCI) table is read locklessly from the transmit path:
mlx5e_macsec_handle_tx_skb() and mlx5e_macsec_tx_build_eseg() call mlx5_macsec_fs_get_fs_id_from_hashtable(), which does rhashtable_lookup() under rcu_read_lock() and then reads mlx5_fs_id->id. A transmit that looked the entry up just before it was removed reads freed memory.

Before commit d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec device inside macsec steering") the SCI table held struct mlx5e_macsec_sa directly, and mlx5e_macsec_del_txsa() freed it with kfree_rcu_mightsleep(). That commit moved the table into macsec_fs.c with the new struct mlx5_fs_id, but frees it with a plain kfree().

Quoted 8 lines

Add an rcu_head to struct mlx5_fs_id and free it with kfree_rcu().

The same issue was flagged by the Sashiko AI review as a pre-existing problem while reviewing the HW-GRO for PSP series.

Fixes d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec device inside macsec steering")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by on v2 1/1
#2 lore raw
In reply to Joas Antonio dos Santos
Quoted 35 lines · Joas Antonio dos Santos

macsec_fs_id_del() removes a struct mlx5_fs_id from its rhashtable with rhashtable_remove_fast() and then frees it with kfree() right away.

The TX (SCI) table is read locklessly from the transmit path:
mlx5e_macsec_handle_tx_skb() and mlx5e_macsec_tx_build_eseg() call mlx5_macsec_fs_get_fs_id_from_hashtable(), which does rhashtable_lookup() under rcu_read_lock() and then reads mlx5_fs_id->id. A transmit that looked the entry up just before it was removed reads freed memory.

Before commit d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec device inside macsec steering") the SCI table held struct mlx5e_macsec_sa directly, and mlx5e_macsec_del_txsa() freed it with kfree_rcu_mightsleep(). That commit moved the table into macsec_fs.c with the new struct mlx5_fs_id, but frees it with a plain kfree().

Add an rcu_head to struct mlx5_fs_id and free it with kfree_rcu().

The same issue was flagged by the Sashiko AI review as a pre-existing problem while reviewing the HW-GRO for PSP series.

Fixes d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec device inside macsec steering")
Assisted-by Yaga Agent HackerSec Claude:claude-opus-5-5

---
v2:
- Drop the macsec.c hunk: the sc_xarray_element free was already fixed by ad17ad50b8dc ("net: mlx5e: Fix use-after-free of the MACsec xarray entry on RX SC deletion"). (Simon)
- Embed an rcu_head and use kfree_rcu() instead of kvfree_rcu_mightsleep(). (Simon)
- Fixes: now points at the commit that introduced the kfree().
- v1: https://lore.kernel.org/all/[email protected]/

Quoted 3 lines

Testing: compile-tested only (arm64, W=1, CONFIG_MLX5_MACSEC=y, no new warnings). I do not have
MACsec-capable ConnectX hardware, so this was not run on a device.

Thanks for the update, and testing and discovery information.

maintainer
#3 lore raw
In reply to Joas Antonio dos Santos
Quoted 18 lines · Joas Antonio dos Santos

macsec_fs_id_del() removes a struct mlx5_fs_id from its rhashtable with rhashtable_remove_fast() and then frees it with kfree() right away.

The TX (SCI) table is read locklessly from the transmit path:
mlx5e_macsec_handle_tx_skb() and mlx5e_macsec_tx_build_eseg() call mlx5_macsec_fs_get_fs_id_from_hashtable(), which does rhashtable_lookup() under rcu_read_lock() and then reads mlx5_fs_id->id. A transmit that looked the entry up just before it was removed reads freed memory.

Before commit d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec device inside macsec steering") the SCI table held struct mlx5e_macsec_sa directly, and mlx5e_macsec_del_txsa() freed it with kfree_rcu_mightsleep(). That commit moved the table into macsec_fs.c with the new struct mlx5_fs_id, but frees it with a plain kfree().

Add an rcu_head to struct mlx5_fs_id and free it with kfree_rcu().

Quoted 7 lines

The same issue was flagged by the Sashiko AI review as a pre-existing problem while reviewing the HW-GRO for PSP series.

Fixes d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec device inside macsec steering")
Assisted-by Yaga Agent HackerSec Claude:claude-opus-5-5

Waiting for an explicit ack from Mellanox.

/P

maintainer
#4 lore raw
Quoted 19 lines · Paolo Abeni
Quoted 18 lines · Joas Antonio dos Santos

macsec_fs_id_del() removes a struct mlx5_fs_id from its rhashtable with rhashtable_remove_fast() and then frees it with kfree() right away.

The TX (SCI) table is read locklessly from the transmit path:
mlx5e_macsec_handle_tx_skb() and mlx5e_macsec_tx_build_eseg() call mlx5_macsec_fs_get_fs_id_from_hashtable(), which does rhashtable_lookup() under rcu_read_lock() and then reads mlx5_fs_id->id. A transmit that looked the entry up just before it was removed reads freed memory.

Before commit d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec device inside macsec steering") the SCI table held struct mlx5e_macsec_sa directly, and mlx5e_macsec_del_txsa() freed it with kfree_rcu_mightsleep(). That commit moved the table into macsec_fs.c with the new struct mlx5_fs_id, but frees it with a plain kfree().

Add an rcu_head to struct mlx5_fs_id and free it with kfree_rcu().

Quoted 8 lines
Quoted 7 lines

The same issue was flagged by the Sashiko AI review as a pre-existing problem while reviewing the HW-GRO for PSP series.

Fixes d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec device inside macsec steering")
Assisted-by Yaga Agent HackerSec Claude:claude-opus-5-5

Waiting for an explicit ack from Mellanox.

Actually Dave already applied this one, but the bot did not catch-up.

Sorry for the noise.

/P

Reviewed-by on v2 1/1
#5 lore raw
In reply to Joas Antonio dos Santos
Quoted 72 lines · Joas Antonio dos Santos

macsec_fs_id_del() removes a struct mlx5_fs_id from its rhashtable with
rhashtable_remove_fast() and then frees it with kfree() right away.

The TX (SCI) table is read locklessly from the transmit path:
mlx5e_macsec_handle_tx_skb() and mlx5e_macsec_tx_build_eseg() call mlx5_macsec_fs_get_fs_id_from_hashtable(), which does rhashtable_lookup() under rcu_read_lock() and then reads mlx5_fs_id->id. A transmit that looked the entry up just before it was
removed reads freed memory.

Before commit d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec
device inside macsec steering") the SCI table held struct mlx5e_macsec_sa directly, and mlx5e_macsec_del_txsa() freed it with kfree_rcu_mightsleep(). That commit moved the table into macsec_fs.c with the new struct mlx5_fs_id, but frees it with a plain kfree().

Add an rcu_head to struct mlx5_fs_id and free it with kfree_rcu().

The same issue was flagged by the Sashiko AI review as a pre-existing problem while reviewing the HW-GRO for PSP series.

Fixes d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec

device inside macsec steering")
Link:
https://lore.kernel.org/all/[email protected]/

Assisted-by Yaga Agent HackerSec Claude:claude-opus-5-5

---
v2:
- Drop the macsec.c hunk: the sc_xarray_element free was already fixed
  by ad17ad50b8dc ("net: mlx5e: Fix use-after-free of the MACsec xarray
  entry on RX SC deletion"). (Simon)
- Embed an rcu_head and use kfree_rcu() instead of kvfree_rcu_mightsleep(). (Simon)
- Fixes: now points at the commit that introduced the kfree().
- v1:
https://lore.kernel.org/all/[email protected]/

Testing: compile-tested only (arm64, W=1, CONFIG_MLX5_MACSEC=y, no new
warnings). I do not have
MACsec-capable ConnectX hardware, so this was not run on a device.

 drivers/net/ethernet/mellanox/mlx5/core/lib/macsec_fs.c | 3 ++-

 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/lib/macsec_fs.c +2 -1
@@ -94,6 +94,7 @@ struct mlx5_fs_id {
  	refcount_t refcnt;
  	sci_t sci;
  	struct rhash_head hash;
+	struct rcu_head rcu;
  };
 
  struct mlx5_macsec_device {
@@ -684,7 +685,7 @@ static void macsec_fs_id_del(struct list_head
 *macsec_devices_list, u32 fs_id,
 
  	xa_erase(fs_id_xa, fs_id);
 
-	kfree(fs_id_found);
+	kfree_rcu(fs_id_found, rcu);
 
  	if (macsec_fs_is_macsec_device_empty(macsec_device)) {
Quoted 3 lines

  list_del(&macsec_device->macsec_devices_list_entry); --
2.39.5

Thank you for the fix, it looks good.

maintainer Acked-by
#6 lore raw
Quoted 72 lines · Cosmin Ratiu
Quoted 71 lines · Joas Antonio dos Santos

macsec_fs_id_del() removes a struct mlx5_fs_id from its rhashtable with
rhashtable_remove_fast() and then frees it with kfree() right away.

The TX (SCI) table is read locklessly from the transmit path:
mlx5e_macsec_handle_tx_skb() and mlx5e_macsec_tx_build_eseg() call mlx5_macsec_fs_get_fs_id_from_hashtable(), which does rhashtable_lookup() under rcu_read_lock() and then reads mlx5_fs_id->id. A transmit that looked the entry up just before it was
removed reads freed memory.

Before commit d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec
device inside macsec steering") the SCI table held struct mlx5e_macsec_sa directly, and mlx5e_macsec_del_txsa() freed it with kfree_rcu_mightsleep(). That commit moved the table into macsec_fs.c with the new struct mlx5_fs_id, but frees it with a plain kfree().

Add an rcu_head to struct mlx5_fs_id and free it with kfree_rcu().

The same issue was flagged by the Sashiko AI review as a pre-existing problem while reviewing the HW-GRO for PSP series.

Fixes d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec

device inside macsec steering")
Link:
https://lore.kernel.org/all/[email protected]/

Assisted-by Yaga Agent HackerSec Claude:claude-opus-5-5

---
v2:
- Drop the macsec.c hunk: the sc_xarray_element free was already fixed
  by ad17ad50b8dc ("net: mlx5e: Fix use-after-free of the MACsec
xarray
  entry on RX SC deletion"). (Simon)
- Embed an rcu_head and use kfree_rcu() instead of kvfree_rcu_mightsleep(). (Simon)
- Fixes: now points at the commit that introduced the kfree().
- v1:
https://lore.kernel.org/all/[email protected]/

Testing: compile-tested only (arm64, W=1, CONFIG_MLX5_MACSEC=y, no new
warnings). I do not have
MACsec-capable ConnectX hardware, so this was not run on a device.

  drivers/net/ethernet/mellanox/mlx5/core/lib/macsec_fs.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/lib/macsec_fs.c +2 -1
@@ -94,6 +94,7 @@ struct mlx5_fs_id {
  	refcount_t refcnt;
  	sci_t sci;
  	struct rhash_head hash;
+	struct rcu_head rcu;
  };
 
  struct mlx5_macsec_device {
@@ -684,7 +685,7 @@ static void macsec_fs_id_del(struct list_head
 *macsec_devices_list, u32 fs_id,
 
  	xa_erase(fs_id_xa, fs_id);
 
-	kfree(fs_id_found);
+	kfree_rcu(fs_id_found, rcu);
 
Quoted 8 lines
Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/macsec_fs.c +0 -0
  	if (macsec_fs_is_macsec_device_empty(macsec_device)) {

  list_del(&macsec_device->macsec_devices_list_entry);
--
2.39.5

Thank you for the fix, it looks good.

maintainer
#7 lore raw
Quoted 80 lines · Tariq Toukan
Quoted 79 lines · Cosmin Ratiu
Quoted 72 lines · Joas Antonio dos Santos

macsec_fs_id_del() removes a struct mlx5_fs_id from its rhashtable with
rhashtable_remove_fast() and then frees it with kfree() right away.

The TX (SCI) table is read locklessly from the transmit path:
mlx5e_macsec_handle_tx_skb() and mlx5e_macsec_tx_build_eseg() call mlx5_macsec_fs_get_fs_id_from_hashtable(), which does rhashtable_lookup() under rcu_read_lock() and then reads mlx5_fs_id->id. A transmit that looked the entry up just before it was
removed reads freed memory.

Before commit d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec
device inside macsec steering") the SCI table held struct mlx5e_macsec_sa directly, and mlx5e_macsec_del_txsa() freed it with kfree_rcu_mightsleep(). That commit moved the table into macsec_fs.c with the new struct mlx5_fs_id, but frees it with a plain kfree().

Add an rcu_head to struct mlx5_fs_id and free it with kfree_rcu().

The same issue was flagged by the Sashiko AI review as a pre-existing problem while reviewing the HW-GRO for PSP series.

Fixes d4ece08f4b1d ("net/mlx5: Maintain fs_id xarray per MACsec

device inside macsec steering")
Link:
https://lore.kernel.org/all/[email protected]/

Assisted-by Yaga Agent HackerSec Claude:claude-opus-5-5

---
v2:
- Drop the macsec.c hunk: the sc_xarray_element free was already fixed
   by ad17ad50b8dc ("net: mlx5e: Fix use-after-free of the MACsec xarray
   entry on RX SC deletion"). (Simon)
- Embed an rcu_head and use kfree_rcu() instead of kvfree_rcu_mightsleep(). (Simon)
- Fixes: now points at the commit that introduced the kfree().
- v1:
https://lore.kernel.org/all/[email protected]/

Testing: compile-tested only (arm64, W=1, CONFIG_MLX5_MACSEC=y, no new
warnings). I do not have
MACsec-capable ConnectX hardware, so this was not run on a device.

  drivers/net/ethernet/mellanox/mlx5/core/lib/macsec_fs.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/lib/macsec_fs.c +2 -1
@@ -94,6 +94,7 @@ struct mlx5_fs_id {
       refcount_t refcnt;
       sci_t sci;
       struct rhash_head hash;
+    struct rcu_head rcu;
   };
 
   struct mlx5_macsec_device {
@@ -684,7 +685,7 @@ static void macsec_fs_id_del(struct list_head
 *macsec_devices_list, u32 fs_id,
 
       xa_erase(fs_id_xa, fs_id);
 
-    kfree(fs_id_found);
+    kfree_rcu(fs_id_found, rcu);
 
       if (macsec_fs_is_macsec_device_empty(macsec_device)) {
Quoted 3 lines

          list_del(&macsec_device->macsec_devices_list_entry);

Signature · 1 line
2.39.5

Thank you for the fix, it looks good.

Quoted 3 lines
Quoted 1 line

While I have your attention, there is another mlx patch pending:

https://lore.kernel.org/all/[email protected]/

/P