net/mlx5e: IPOIB, Fix QPN lookup race
Subsystems (4)
PATCH v2 1/1 net/mlx5e: IPOIB, Fix QPN lookup race
The parent RX completion path can load a QPN hash entry before mlx5i_pkey_del_qpn() unlinks and frees it, then dereference freed memory.
Use RCU hlist operations and defer freeing with kfree_rcu(). The existing RCU read-side section in mlx5e_napi_poll() covers the lookup. Keep ht_lock for writers and acquire it before looking up the entry to delete.
---
Changes in v2:
- Replace the RX spinlock with RCU traversal and deferred freeing.
- Narrow the description to the parent CQE lookup race.
v1: https://lore.kernel.org/all/[email protected]/
Review: https://lore.kernel.org/all/[email protected]/
.../ethernet/mellanox/mlx5/core/ipoib/ipoib.h | 2 +- .../mellanox/mlx5/core/ipoib/ipoib_vlan.c | 26 +++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/ipoib/ipoib.h +1 -1
@@ -82,7 +82,7 @@ void mlx5i_pkey_qpn_ht_cleanup(struct net_device *netdev);int mlx5i_pkey_add_qpn (struct net_device *netdev,u32 qpn);int mlx5i_pkey_del_qpn (struct net_device *netdev,u32 qpn); -/* Get the net-device corresponding to the given underlay QPN */ +/* Get the net-device for a QPN. Caller must hold rcu_read_lock(). */ struct net_device *mlx5i_pkey_get_netdev (struct net_device *netdev,u32 qpn);/* Shared ndo functions */
drivers/net/ethernet/mellanox/mlx5/core/ipoib/ipoib_vlan.c +15 -11
@@ -31,6 +31,7 @@*/ #include <linux/hash.h> +#include <linux/rculist.h> #include "ipoib.h" #define MLX5I_MAX_LOG_PKEY_SUP 7
@@ -39,11 +40,12 @@ struct qpn_to_netdev {struct net_device *netdev;struct hlist_node hlist;u32 underlay_qpn; +struct rcu_head rcu; };struct mlx5i_pkey_qpn_ht {struct hlist_head buckets[1 <<MLX5I_MAX_LOG_PKEY_SUP ]; -spinlock_t ht_lock;/* Synchronise with NAPI */ +spinlock_t ht_lock;/* Serialize hash table updates */ };int mlx5i_pkey_qpn_ht_init (struct net_device *netdev)
@@ -68,13 +70,14 @@ void mlx5i_pkey_qpn_ht_cleanup(struct net_device *netdev)kfree (ipriv->qpn_htbl); } -static struct qpn_to_netdev *mlx5i_find_qpn_to_netdev_node (struct hlist_head *buckets, -u32 qpn) +static struct qpn_to_netdev * +mlx5i_find_qpn_to_netdev_node (struct mlx5i_pkey_qpn_ht *ht,u32 qpn) { -struct hlist_head *h = &buckets[hash_32 (qpn,MLX5I_MAX_LOG_PKEY_SUP )]; +struct hlist_head *h = &ht->buckets[hash_32 (qpn,MLX5I_MAX_LOG_PKEY_SUP )];struct qpn_to_netdev *node; -hlist_for_each_entry (node, h, hlist) { +hlist_for_each_entry_rcu (node, h, hlist, +lockdep_is_held (&ht->ht_lock)) {if (node->underlay_qpn == qpn)return node; }
@@ -96,7 +99,7 @@ int mlx5i_pkey_add_qpn(struct net_device *netdev, u32 qpn) new_node->netdev = netdev; new_node->underlay_qpn = qpn;spin_lock_bh (&ht->ht_lock); -hlist_add_head (&new_node->hlist, &ht->buckets[key]); +hlist_add_head_rcu (&new_node->hlist, &ht->buckets[key]);spin_unlock_bh (&ht->ht_lock);return 0 ;
@@ -109,16 +112,17 @@ int mlx5i_pkey_del_qpn(struct net_device *netdev, u32 qpn)struct mlx5i_pkey_qpn_ht *ht = ipriv->qpn_htbl;struct qpn_to_netdev *node; - node =mlx5i_find_qpn_to_netdev_node (ht->buckets, qpn); +spin_lock_bh (&ht->ht_lock); + node =mlx5i_find_qpn_to_netdev_node (ht, qpn);if (!node) { +spin_unlock_bh (&ht->ht_lock);mlx5_core_warn (epriv->mdev,"QPN to netdev delete from HT failed\n" );return -EINVAL ; } -spin_lock_bh (&ht->ht_lock); -hlist_del_init (&node->hlist); +hlist_del_rcu (&node->hlist);spin_unlock_bh (&ht->ht_lock); -kfree (node); +kfree_rcu (node, rcu);return 0 ; }
@@ -128,7 +132,7 @@ struct net_device *mlx5i_pkey_get_netdev(struct net_device *netdev, u32 qpn)struct mlx5i_priv *ipriv =netdev_priv (netdev);struct qpn_to_netdev *node; - node =mlx5i_find_qpn_to_netdev_node (ipriv->qpn_htbl->buckets, qpn); + node =mlx5i_find_qpn_to_netdev_node (ipriv->qpn_htbl, qpn);if (!node)return NULL ;
Signature · 1 line
2.43.0
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.
- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Quoted 9 lines · Prathamesh Deshpande
The parent RX completion path can load a QPN hash entry before
mlx5i_pkey_del_qpn()unlinks and frees it, then dereference freed memory.Use RCU hlist operations and defer freeing with
kfree_rcu(). The existing RCU read-side section inmlx5e_napi_poll()covers the lookup. Keepht_lockfor writers and acquire it before looking up the entry to delete.Fixes 7e7f4780c340 ("net/mlx5e: IPoIB, Use hash-table to map between QPN to child netdev")Signed-off-by Prathamesh Deshpande <[email protected]>
Quoted 9 lines
---
Changes in v2:
- Replace the RX spinlock with RCU traversal and deferred freeing.
- Narrow the description to the parent CQE lookup race.v1: https://lore.kernel.org/all/[email protected]/
Review: https://lore.kernel.org/all/[email protected]/
nit: Convention is IPoIB, not IPOIB. Maybe can be modified while applying.
Thanks.
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Sat, 31 Oct 2026 22:57:13 +0000 you wrote:
The parent RX completion path can load a QPN hash entry before
mlx5i_pkey_del_qpn()unlinks and frees it, then dereference freed memory.Use RCU hlist operations and defer freeing with
kfree_rcu(). The existing RCU read-side section inmlx5e_napi_poll()covers the lookup. Keepht_lockfor writers and acquire it before looking up the entry to delete.[...]
Here is the summary with links:
- [net,v2] net/mlx5e: IPOIB, Fix QPN lookup race
https://git.kernel.org/netdev/net/c/29066b0fc046
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html