All of lore.kernel.org
 help / color / mirror / Atom feed
From: Leon Romanovsky <leon@kernel.org>
To: Miaoqian Lin <linmq006@gmail.com>
Cc: Christian Benvenuti <benve@cisco.com>,
	Nelson Escobar <neescoba@cisco.com>,
	Jason Gunthorpe <jgg@ziepe.ca>, Upinder Malhi <umalhi@cisco.com>,
	Roland Dreier <roland@purestorage.com>,
	linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] IB/usnic: Fix memory leak in usnic_ib_sysfs_qpn_add
Date: Wed, 26 Jan 2022 11:47:15 +0200	[thread overview]
Message-ID: <YfEYo2jDo2Y8P1dL@unreal> (raw)
In-Reply-To: <20220126060425.11124-1-linmq006@gmail.com>

On Wed, Jan 26, 2022 at 06:04:25AM +0000, Miaoqian Lin wrote:
> kobject_init_and_add() takes reference even when it fails.
> According to the doc of kobject_init_and_add():
> 
>    If this function returns an error, kobject_put() must be called to
>    properly clean up the memory associated with the object.
> 
> Fix memory leak by calling kobject_put().

There is no real memory leak here, this kobject will be released in
usnic_ib_sysfs_qpn_remove(). Another possible solution is to delete
"if (err)" completely.

diff --git a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
index fdb63a8fb997..11723f54e200 100644
--- a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
+++ b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
@@ -271,17 +271,15 @@ void usnic_ib_sysfs_unregister_usdev(struct usnic_ib_dev *us_ibdev)
 void usnic_ib_sysfs_qpn_add(struct usnic_ib_qp_grp *qp_grp)
 {
        struct usnic_ib_dev *us_ibdev;
-       int err;
 
        us_ibdev = qp_grp->vf->pf;
 
-       err = kobject_init_and_add(&qp_grp->kobj, &usnic_ib_qpn_type,
+       kobject_init_and_add(&qp_grp->kobj, &usnic_ib_qpn_type,
                        kobject_get(us_ibdev->qpn_kobj),
                        "%d", qp_grp->grp_id);
-       if (err) {
-               kobject_put(us_ibdev->qpn_kobj);
-               return;
-       }
+       /* We don't care about failure here, the release will be performed in
+        * usnic_ib_sysfs_qpn_remove() anyway.
+        */
 }
 
 void usnic_ib_sysfs_qpn_remove(struct usnic_ib_qp_grp *qp_grp)


> 
> Fixes: e3cf00d0a87f ("IB/usnic: Add Cisco VIC low-level hardware driver")
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
>  drivers/infiniband/hw/usnic/usnic_ib_sysfs.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
> index 7d868f033bbf..69c5854deebc 100644
> --- a/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
> +++ b/drivers/infiniband/hw/usnic/usnic_ib_sysfs.c
> @@ -280,6 +280,7 @@ void usnic_ib_sysfs_qpn_add(struct usnic_ib_qp_grp *qp_grp)
>  			kobject_get(us_ibdev->qpn_kobj),
>  			"%d", qp_grp->grp_id);
>  	if (err) {
> +		kobject_put(&qp_grp->kobj);
>  		kobject_put(us_ibdev->qpn_kobj);
>  		return;
>  	}
> -- 
> 2.17.1
> 

      reply	other threads:[~2022-01-26  9:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-26  6:04 [PATCH] IB/usnic: Fix memory leak in usnic_ib_sysfs_qpn_add Miaoqian Lin
2022-01-26  9:47 ` Leon Romanovsky [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YfEYo2jDo2Y8P1dL@unreal \
    --to=leon@kernel.org \
    --cc=benve@cisco.com \
    --cc=jgg@ziepe.ca \
    --cc=linmq006@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=neescoba@cisco.com \
    --cc=roland@purestorage.com \
    --cc=umalhi@cisco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.