IB/mlx4: Fix memory leaks [Linux 4.9.192]

This Linux kernel change "IB/mlx4: Fix memory leaks" is included in the Linux 4.9.192 release. This change is authored by Wenwen Wang <wenwen [at] cs.uga.edu> on Sun Aug 18 15:23:01 2019 -0500. The commit for this change in Linux stable tree is 3f452f4 (patch) which is from upstream commit 5c1baaa. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream 5c1baaa.

IB/mlx4: Fix memory leaks

[ Upstream commit 5c1baaa82cea2c815a5180ded402a7cd455d1810 ]

In mlx4_ib_alloc_pv_bufs(), 'tun_qp->tx_ring' is allocated through
kcalloc(). However, it is not always deallocated in the following execution
if an error occurs, leading to memory leaks. To fix this issue, free
'tun_qp->tx_ring' whenever an error occurs.

Signed-off-by: Wenwen Wang <wenwen@cs.uga.edu>
Acked-by: Leon Romanovsky <leonro@mellanox.com>
Link: https://lore.kernel.org/r/1566159781-4642-1-git-send-email-wenwen@cs.uga.edu
Signed-off-by: Doug Ledford <dledford@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>

There are 4 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 drivers/infiniband/hw/mlx4/mad.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/infiniband/hw/mlx4/mad.c b/drivers/infiniband/hw/mlx4/mad.c
index d9323d7..f32ffd7 100644
--- a/drivers/infiniband/hw/mlx4/mad.c
+++ b/drivers/infiniband/hw/mlx4/mad.c
@@ -1643,8 +1643,6 @@ static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
                    tx_buf_size, DMA_TO_DEVICE);
-   kfree(tun_qp->tx_ring);
-   tun_qp->tx_ring = NULL;
    while (i > 0) {
@@ -1653,6 +1651,8 @@ static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
                    rx_buf_size, DMA_FROM_DEVICE);
+   kfree(tun_qp->tx_ring);
+   tun_qp->tx_ring = NULL;
    tun_qp->ring = NULL;
    return -ENOMEM;

Leave a Reply

Your email address will not be published. Required fields are marked *