public inbox for kernel-janitors@vger.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: kernel-janitors@vger.kernel.org
Subject: [patch] usb: gadget: f_tcm: memory leak on error in tcm_alloc_inst()
Date: Wed, 09 Dec 2015 10:23:01 +0000	[thread overview]
Message-ID: <20151209102301.GC3173@mwanda> (raw)

We need to kfree(opts) on error.  Also it's nicer to allocate opts
before taking the lock.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
index ed47e40..0a26a2e 100644
--- a/drivers/usb/gadget/function/f_tcm.c
+++ b/drivers/usb/gadget/function/f_tcm.c
@@ -2300,20 +2300,19 @@ static struct usb_function_instance *tcm_alloc_inst(void)
 	struct f_tcm_opts *opts;
 	int i;
 
-	mutex_lock(&tpg_instances_lock);
-	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
-	if (!opts) {
-		mutex_unlock(&tpg_instances_lock);
 
+	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+	if (!opts)
 		return ERR_PTR(-ENOMEM);
-	}
+
+	mutex_lock(&tpg_instances_lock);
 	for (i = 0; i < TPG_INSTANCES; ++i)
 		if (!tpg_instances[i].func_inst)
 			break;
 
 	if (i = TPG_INSTANCES) {
 		mutex_unlock(&tpg_instances_lock);
-
+		kfree(opts);
 		return ERR_PTR(-EBUSY);
 	}
 	tpg_instances[i].func_inst = &opts->func_inst;

             reply	other threads:[~2015-12-09 10:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-09 10:23 Dan Carpenter [this message]
2015-12-10 16:52 ` [patch] usb: gadget: f_tcm: memory leak on error in tcm_alloc_inst() Felipe Balbi

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=20151209102301.GC3173@mwanda \
    --to=dan.carpenter@oracle.com \
    --cc=kernel-janitors@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox