mlx4: Misc bug fixes after reinitializing queues
Subsystems (4)
v2 cover mlx4: Misc bug fixes after reinitializing queues
This patchset fixes misc bugs after reinitializing queues (e.g. by ethtool -L).
v2:
* Add another fix to mem leak in tx_ring[t] and tx_cq[t]
* In mlx4_en_try_alloc_resources(),
move all xdp_prog logic after calling mlx4_en_alloc_resources()
Martin KaFai Lau (2):
mlx4: Fix memory leak after mlx4_en_update_priv()
mlx4: xdp_prog becomes inactive after mlx4_en_try_alloc_resources()
drivers/net/ethernet/mellanox/mlx4/en_ethtool.c | 4 +-- drivers/net/ethernet/mellanox/mlx4/en_netdev.c | 35 ++++++++++++++++++------- drivers/net/ethernet/mellanox/mlx4/mlx4_en.h | 3 ++- 3 files changed, 29 insertions(+), 13 deletions(-)
Signature · 1 line
2.5.1
PATCH v2 1/2 mlx4: Fix memory leak after mlx4_en_update_priv()
In mlx4_en_update_priv(), dst->tx_ring[t] and dst->tx_cq[t] are over-written by src->tx_ring[t] and src->tx_cq[t] without first calling kfree.
One of the reproducible code paths is by doing 'ethtool -L'.
The fix is to do the kfree in mlx4_en_free_resources().
Here is the kmemleak report:
unreferenced object 0xffff880841211800 (size 2048):
comm "ethtool", pid 3096, jiffies 4294716940 (age 528.353s) hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
backtrace:
[<ffffffff81930718>] kmemleak_alloc+0x28/0x50
[<ffffffff8120b213>] kmem_cache_alloc_trace+0x103/0x260
[<ffffffff8170e0a8>] mlx4_en_try_alloc_resources+0x118/0x1a0
[<ffffffff817065a9>] mlx4_en_set_ringparam+0x169/0x210
[<ffffffff818040c5>] dev_ethtool+0xae5/0x2190
[<ffffffff8181b898>] dev_ioctl+0x168/0x6f0
[<ffffffff817d7a72>] sock_do_ioctl+0x42/0x50
[<ffffffff817d819b>] sock_ioctl+0x21b/0x2d0
[<ffffffff81247a73>] do_vfs_ioctl+0x93/0x6a0
[<ffffffff812480f9>] SyS_ioctl+0x79/0x90
[<ffffffff8193d7ea>] entry_SYSCALL_64_fastpath+0x18/0xad
[<ffffffffffffffff>] 0xffffffffffffffff
unreferenced object 0xffff880841213000 (size 2048):
comm "ethtool", pid 3096, jiffies 4294716940 (age 528.353s)
hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
backtrace:
[<ffffffff81930718>] kmemleak_alloc+0x28/0x50
[<ffffffff8120b213>] kmem_cache_alloc_trace+0x103/0x260
[<ffffffff8170e0cb>] mlx4_en_try_alloc_resources+0x13b/0x1a0
[<ffffffff817065a9>] mlx4_en_set_ringparam+0x169/0x210
[<ffffffff818040c5>] dev_ethtool+0xae5/0x2190
[<ffffffff8181b898>] dev_ioctl+0x168/0x6f0
[<ffffffff817d7a72>] sock_do_ioctl+0x42/0x50
[<ffffffff817d819b>] sock_ioctl+0x21b/0x2d0
[<ffffffff81247a73>] do_vfs_ioctl+0x93/0x6a0
[<ffffffff812480f9>] SyS_ioctl+0x79/0x90
[<ffffffff8193d7ea>] entry_SYSCALL_64_fastpath+0x18/0xad
[<ffffffffffffffff>] 0xffffffffffffffff(gdb) list *mlx4_en_try_alloc_resources+0x1180xffffffff8170e0a8 is in mlx4_en_try_alloc_resources (drivers/net/ethernet/mellanox/mlx4/en_netdev.c:2145).
2140 if (!dst->tx_ring_num[t])
2141 continue;
2142
2143 dst->tx_ring[t] = kzalloc(sizeof(struct mlx4_en_tx_ring *) *
2144 MAX_TX_RINGS, GFP_KERNEL);
2145 if (!dst->tx_ring[t])
2146 goto err_free_tx;
2147
2148 dst->tx_cq[t] = kzalloc(sizeof(struct mlx4_en_cq *) *
2149 MAX_TX_RINGS, GFP_KERNEL);
(gdb) list *mlx4_en_try_alloc_resources+0x13b0xffffffff8170e0cb is in mlx4_en_try_alloc_resources (drivers/net/ethernet/mellanox/mlx4/en_netdev.c:2150).
2145 if (!dst->tx_ring[t])
2146 goto err_free_tx;
2147
2148 dst->tx_cq[t] = kzalloc(sizeof(struct mlx4_en_cq *) *
2149 MAX_TX_RINGS, GFP_KERNEL);
2150 if (!dst->tx_cq[t]) {
2151 kfree(dst->tx_ring[t]);
2152 goto err_free_tx;
2153 }
2154 }
--- drivers/net/ethernet/mellanox/mlx4/en_netdev.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +2 -6
@@ -2042,6 +2042,8 @@ static void mlx4_en_free_resources(struct mlx4_en_priv *priv)if (priv->tx_cq[t] && priv->tx_cq[t][i])mlx4_en_destroy_cq (priv, &priv->tx_cq[t][i]); } +kfree (priv->tx_ring[t]); +kfree (priv->tx_cq[t]); }for (i =0 ; i < priv->rx_ring_num; i++) {
@@ -2214,7 +2216,6 @@ void mlx4_en_destroy_netdev(struct net_device *dev) {struct mlx4_en_priv *priv =netdev_priv (dev);struct mlx4_en_dev *mdev = priv->mdev; -int t;en_dbg (DRV , priv,"Destroying netdev on port:%d\n" , priv->port);
@@ -2248,11 +2249,6 @@ void mlx4_en_destroy_netdev(struct net_device *dev)mlx4_en_free_resources (priv);mutex_unlock (&mdev->state_lock); -for (t =0 ; t <MLX4_EN_NUM_TX_TYPES ; t++) { -kfree (priv->tx_ring[t]); -kfree (priv->tx_cq[t]); - } -free_netdev (dev); }
Signature · 1 line
2.5.1
Re: [PATCH net v2 1/2] mlx4: Fix memory leak after mlx4_en_update_priv()
Hi Martin,
Thanks for your fix!
Quoted 8 lines · Martin KaFai Lau
In
mlx4_en_update_priv(), dst->tx_ring[t] and dst->tx_cq[t] are over-written by src->tx_ring[t] and src->tx_cq[t] without first calling kfree.One of the reproducible code paths is by doing 'ethtool -L'.
The fix is to do the kfree in
mlx4_en_free_resources().
...
Quoted 32 lines
Fixes ec25bc04ed8e ("net/mlx4_en: Add resilience in low memory systems")Cc Eugenia Emantayev <[email protected]>Cc Saeed Mahameed <[email protected]>Cc Tariq Toukan <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>--- drivers/net/ethernet/mellanox/mlx4/en_netdev.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)drivers/net/ethernet/mellanox/mlx4/en_netdev.c +2
-1@@ -2042,6 +2042,8 @@ static void mlx4_en_free_resources(struct mlx4_en_priv *priv)if (priv->tx_cq[t] && priv->tx_cq[t][i])mlx4_en_destroy_cq (priv, &priv->tx_cq[t][i]); } +kfree (priv->tx_ring[t]); +kfree (priv->tx_cq[t]); }for (i =0 ; i < priv->rx_ring_num; i++) {@@ -2214,7 +2216,6 @@ void mlx4_en_destroy_netdev(struct net_device *dev) {struct mlx4_en_priv *priv =netdev_priv (dev);struct mlx4_en_dev *mdev = priv->mdev; -int t;en_dbg (DRV , priv,"Destroying netdev on port:%d\n" , priv->port);@@ -2248,11 +2249,6 @@ void mlx4_en_destroy_netdev(struct net_device *dev)mlx4_en_free_resources (priv);
drivers/net/ethernet/mellanox/mlx4/en_netdev.c void mlx4_en_destroy_netdev(struct net_device *dev) · line 2254
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +0
-5mutex_unlock (&mdev->state_lock); -for (t =0 ; t <MLX4_EN_NUM_TX_TYPES ; t++) { -kfree (priv->tx_ring[t]); -kfree (priv->tx_cq[t]); - } -free_netdev (dev); }
PATCH v2 2/2 mlx4: xdp_prog becomes inactive after ethtool '-L' or '-G'
In reply to Martin KaFai LauAfter calling mlx4_en_try_alloc_resources (e.g. by changing the number of rx-queues with ethtool -L), the existing xdp_prog becomes inactive.
The bug is that the xdp_prog ptr has not been carried over from the old rx-queues to the new rx-queues
--- drivers/net/ethernet/mellanox/mlx4/en_ethtool.c | 4 ++-- drivers/net/ethernet/mellanox/mlx4/en_netdev.c | 27 +++++++++++++++++++++---- drivers/net/ethernet/mellanox/mlx4/mlx4_en.h | 3 ++- 3 files changed, 27 insertions(+), 7 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_ethtool.c +2 -2
@@ -1099,7 +1099,7 @@ static int mlx4_en_set_ringparam(struct net_device *dev,memcpy (&new_prof, priv->prof,sizeof (struct mlx4_en_port_profile )); new_prof.tx_ring_size = tx_size; new_prof.rx_ring_size = rx_size; - err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof); + err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof, true);if (err) goto out;
@@ -1774,7 +1774,7 @@ static int mlx4_en_set_channels(struct net_device *dev, new_prof.tx_ring_num[TX_XDP ] = xdp_count; new_prof.rx_ring_num = channel->rx_count; - err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof); + err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof, true);if (err) goto out;
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +23 -4
@@ -2186,9 +2186,11 @@ static void mlx4_en_update_priv(struct mlx4_en_priv *dst,int mlx4_en_try_alloc_resources (struct mlx4_en_priv *priv,struct mlx4_en_priv *tmp, -struct mlx4_en_port_profile *prof) +struct mlx4_en_port_profile *prof, +bool carry_xdp_prog) { -int t; +struct bpf_prog *xdp_prog; +int i, t;mlx4_en_copy_priv (tmp, priv, prof);
@@ -2202,6 +2204,23 @@ int mlx4_en_try_alloc_resources(struct mlx4_en_priv *priv, }return -ENOMEM ; } + +/* All rx_rings has the same xdp_prog. Pick the first one. */ + xdp_prog =rcu_dereference_protected ( + priv->rx_ring[0 ]->xdp_prog, +lockdep_is_held (&priv->mdev->state_lock)); + +if (xdp_prog && carry_xdp_prog) { + xdp_prog =bpf_prog_add (xdp_prog, tmp->rx_ring_num); +if (IS_ERR (xdp_prog)) { +mlx4_en_free_resources (tmp); +return PTR_ERR (xdp_prog); + } +for (i =0 ; i < tmp->rx_ring_num; i++) +rcu_assign_pointer (tmp->rx_ring[i]->xdp_prog, + xdp_prog); + } +return 0 ; }
@@ -2751,7 +2770,7 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)en_warn (priv,"Reducing the number of TX rings, to not exceed the max total rings number.\n" ); } - err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof); + err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof, false);if (err) {if (prog)bpf_prog_sub (prog, priv->rx_ring_num -1 );
@@ -3495,7 +3514,7 @@ int mlx4_en_reset_config(struct net_device *dev,memcpy (&new_prof, priv->prof,sizeof (struct mlx4_en_port_profile ));memcpy (&new_prof.hwtstamp_config, &ts_config,sizeof (ts_config)); - err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof); + err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof, true);if (err) goto out;
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +2 -1
@@ -679,7 +679,8 @@ void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev,int mlx4_en_try_alloc_resources (struct mlx4_en_priv *priv,struct mlx4_en_priv *tmp, -struct mlx4_en_port_profile *prof); +struct mlx4_en_port_profile *prof, +bool carry_xdp_prog);void mlx4_en_safe_replace_resources (struct mlx4_en_priv *priv,struct mlx4_en_priv *tmp);
Signature · 1 line
2.5.1
Re: [PATCH net v2 2/2] mlx4: xdp_prog becomes inactive after ethtool '-L' or '-G'
Quoted 105 lines · Martin KaFai Lau
After calling
mlx4_en_try_alloc_resources(e.g. by changing the number of rx-queues with ethtool -L), the existingxdp_progbecomes inactive.The bug is that the
xdp_progptr has not been carried over from the old rx-queues to the new rx-queuesFixes 47a38e155037 ("net/mlx4_en: add support for fast rx drop bpf program")Cc Brenden Blanco <[email protected]>Cc Saeed Mahameed <[email protected]>Cc Tariq Toukan <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>--- drivers/net/ethernet/mellanox/mlx4/en_ethtool.c | 4 ++-- drivers/net/ethernet/mellanox/mlx4/en_netdev.c | 27 +++++++++++++++++++++---- drivers/net/ethernet/mellanox/mlx4/mlx4_en.h | 3 ++- 3 files changed, 27 insertions(+), 7 deletions(-)drivers/net/ethernet/mellanox/mlx4/en_ethtool.c +2
-2@@ -1099,7 +1099,7 @@ static int mlx4_en_set_ringparam(struct net_device *dev,memcpy (&new_prof, priv->prof,sizeof (struct mlx4_en_port_profile )); new_prof.tx_ring_size = tx_size; new_prof.rx_ring_size = rx_size; - err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof); + err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof, true);if (err) goto out;@@ -1774,7 +1774,7 @@ static int mlx4_en_set_channels(struct net_device *dev, new_prof.tx_ring_num[TX_XDP ] = xdp_count; new_prof.rx_ring_num = channel->rx_count; - err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof); + err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof, true);if (err) goto out;drivers/net/ethernet/mellanox/mlx4/en_netdev.c +23
-4@@ -2186,9 +2186,11 @@ static void mlx4_en_update_priv(struct mlx4_en_priv *dst,int mlx4_en_try_alloc_resources (struct mlx4_en_priv *priv,struct mlx4_en_priv *tmp, -struct mlx4_en_port_profile *prof) +struct mlx4_en_port_profile *prof, +bool carry_xdp_prog) { -int t; +struct bpf_prog *xdp_prog; +int i, t;mlx4_en_copy_priv (tmp, priv, prof);@@ -2202,6 +2204,23 @@ int mlx4_en_try_alloc_resources(struct mlx4_en_priv *priv, }return -ENOMEM ; } + +/* All rx_rings has the same xdp_prog. Pick the first one. */ + xdp_prog =rcu_dereference_protected ( + priv->rx_ring[0 ]->xdp_prog, +lockdep_is_held (&priv->mdev->state_lock)); + +if (xdp_prog && carry_xdp_prog) { + xdp_prog =bpf_prog_add (xdp_prog, tmp->rx_ring_num); +if (IS_ERR (xdp_prog)) { +mlx4_en_free_resources (tmp); +return PTR_ERR (xdp_prog); + } +for (i =0 ; i < tmp->rx_ring_num; i++) +rcu_assign_pointer (tmp->rx_ring[i]->xdp_prog, + xdp_prog); + } +return 0 ; }@@ -2751,7 +2770,7 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)en_warn (priv,"Reducing the number of TX rings, to not exceed the max total rings number.\n" ); } - err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof); + err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof, false);if (err) {if (prog)bpf_prog_sub (prog, priv->rx_ring_num -1 );@@ -3495,7 +3514,7 @@ int mlx4_en_reset_config(struct net_device *dev,memcpy (&new_prof, priv->prof,sizeof (struct mlx4_en_port_profile ));memcpy (&new_prof.hwtstamp_config, &ts_config,sizeof (ts_config)); - err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof); + err =mlx4_en_try_alloc_resources (priv, tmp, &new_prof, true);if (err) goto out;drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +0
-0
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev, · line 686
Quoted 9 lines
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +2
-1@@ -679,7 +679,8 @@ void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev,int mlx4_en_try_alloc_resources (struct mlx4_en_priv *priv,struct mlx4_en_priv *tmp, -struct mlx4_en_port_profile *prof); +struct mlx4_en_port_profile *prof, +bool carry_xdp_prog);void mlx4_en_safe_replace_resources (struct mlx4_en_priv *priv,struct mlx4_en_priv *tmp);
Thanks.
David Miller
maintainerFrom: Martin KaFai Lau <[email protected]>
Date: Tue, 31 Jan 2017 22:35:31 -0800
Quoted 7 lines
This patchset fixes misc bugs after reinitializing queues (e.g. by ethtool -L).
v2:
* Add another fix to mem leak in tx_ring[t] and tx_cq[t]
* Inmlx4_en_try_alloc_resources(),
move allxdp_proglogic after callingmlx4_en_alloc_resources()
Series applied, thanks.