mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: John Keeping <john@metanate.com>
To: linux-usb@vger.kernel.org
Cc: Fabien Chouteau <fabien.chouteau@barco.com>,
	Peter Korsgaard <peter.korsgaard@barco.com>,
	Felipe Balbi <balbi@ti.com>,
	Andrzej Pietrasiewicz <andrzej.p@samsung.com>,
	linux-kernel@vger.kernel.org,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	John Keeping <john@metanate.com>, Lee Jones <lee@kernel.org>,
	Alan Stern <stern@rowland.harvard.edu>
Subject: [PATCH 3/3] usb: gadget: f_hid: tidy error handling in hidg_alloc
Date: Tue, 22 Nov 2022 12:35:23 +0000	[thread overview]
Message-ID: <20221122123523.3068034-4-john@metanate.com> (raw)
In-Reply-To: <20221122123523.3068034-1-john@metanate.com>

Unify error handling at the end of the function, reducing the risk of
missing something on one of the error paths.

Moving the increment of opts->refcnt later means there is no need to
decrement it on the error path and is safe as this is guarded by
opts->lock which is held for this entire section.

Signed-off-by: John Keeping <john@metanate.com>
---
 drivers/usb/gadget/function/f_hid.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c
index 6be6009f911e..a8da3b4a2855 100644
--- a/drivers/usb/gadget/function/f_hid.c
+++ b/drivers/usb/gadget/function/f_hid.c
@@ -1269,18 +1269,14 @@ static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
 	opts = container_of(fi, struct f_hid_opts, func_inst);
 
 	mutex_lock(&opts->lock);
-	++opts->refcnt;
 
 	device_initialize(&hidg->dev);
 	hidg->dev.release = hidg_release;
 	hidg->dev.class = hidg_class;
 	hidg->dev.devt = MKDEV(major, opts->minor);
 	ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor);
-	if (ret) {
-		--opts->refcnt;
-		mutex_unlock(&opts->lock);
-		return ERR_PTR(ret);
-	}
+	if (ret)
+		goto err_unlock;
 
 	hidg->bInterfaceSubClass = opts->subclass;
 	hidg->bInterfaceProtocol = opts->protocol;
@@ -1291,14 +1287,13 @@ static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
 						 opts->report_desc_length,
 						 GFP_KERNEL);
 		if (!hidg->report_desc) {
-			put_device(&hidg->dev);
-			--opts->refcnt;
-			mutex_unlock(&opts->lock);
-			return ERR_PTR(-ENOMEM);
+			ret = -ENOMEM;
+			goto err_put_device;
 		}
 	}
 	hidg->use_out_ep = !opts->no_out_endpoint;
 
+	++opts->refcnt;
 	mutex_unlock(&opts->lock);
 
 	hidg->func.name    = "hid";
@@ -1313,6 +1308,12 @@ static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
 	hidg->qlen	   = 4;
 
 	return &hidg->func;
+
+err_put_device:
+	put_device(&hidg->dev);
+err_unlock:
+	mutex_unlock(&opts->lock);
+	return ERR_PTR(ret);
 }
 
 DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
-- 
2.38.1


  parent reply	other threads:[~2022-11-22 12:35 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-22 12:35 [PATCH 0/3] usb: gadget: f_hid: fix f_hidg lifetime vs cdev John Keeping
2022-11-22 12:35 ` [PATCH 1/3] " John Keeping
2022-11-23 11:52   ` Andrzej Pietrasiewicz
2022-11-23 12:03     ` John Keeping
2022-11-23 12:20       ` Andrzej Pietrasiewicz
2022-11-22 12:35 ` [PATCH 2/3] usb: gadget: f_hid: fix refcount leak on error path John Keeping
2022-11-23 11:55   ` Andrzej Pietrasiewicz
2022-11-23 12:04     ` John Keeping
2022-11-22 12:35 ` John Keeping [this message]
2022-11-23 12:19   ` [PATCH 3/3] usb: gadget: f_hid: tidy error handling in hidg_alloc Andrzej Pietrasiewicz
2022-11-22 18:18 ` [PATCH 0/3] usb: gadget: f_hid: fix f_hidg lifetime vs cdev Lee Jones
2022-11-28 14:04   ` Lee Jones
2022-11-28 17:47     ` Greg Kroah-Hartman
2022-11-29  8:59       ` Lee Jones

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=20221122123523.3068034-4-john@metanate.com \
    --to=john@metanate.com \
    --cc=andrzej.p@samsung.com \
    --cc=balbi@ti.com \
    --cc=fabien.chouteau@barco.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=peter.korsgaard@barco.com \
    --cc=stern@rowland.harvard.edu \
    /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

all inboxes | Powered by JetHome®