From: Kuen-Han Tsai <khtsai@google.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Thinh Nguyen <Thinh.Nguyen@synopsys.com>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
Kuen-Han Tsai <khtsai@google.com>
Subject: [PATCH 2/2] usb: gadget: f_loopback: Use auto-cleanup for usb_request
Date: Thu, 30 Oct 2025 23:14:20 +0800 [thread overview]
Message-ID: <20251030-auto-cleanup-v1-2-db30584fadfd@google.com> (raw)
In-Reply-To: <20251030-auto-cleanup-v1-0-db30584fadfd@google.com>
Refactor f_loopback.c to use auto-cleanup mechanism for usb_request
allocations in alloc_requests().
The shared buffer between in_req and out_req is handled by nullifying
in_req->buf before returning on usb_ep_queue failure, ensuring the
buffer is only freed once by the out_req cleanup.
Signed-off-by: Kuen-Han Tsai <khtsai@google.com>
---
drivers/usb/gadget/function/f_loopback.c | 30 +++++++++++++-----------------
1 file changed, 13 insertions(+), 17 deletions(-)
diff --git a/drivers/usb/gadget/function/f_loopback.c b/drivers/usb/gadget/function/f_loopback.c
index 49b009a7d5d79285c2397c7aebb8c8fcd3b7dafb..cdf42d8b3d774e5324e83dc308aa5caa265eac3b 100644
--- a/drivers/usb/gadget/function/f_loopback.c
+++ b/drivers/usb/gadget/function/f_loopback.c
@@ -8,6 +8,7 @@
/* #define VERBOSE_DEBUG */
+#include <linux/cleanup.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/device.h>
@@ -308,9 +309,8 @@ static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
static int alloc_requests(struct usb_composite_dev *cdev,
struct f_loopback *loop)
{
- struct usb_request *in_req, *out_req;
int i;
- int result = 0;
+ int result;
/*
* allocate a bunch of read buffers and queue them all at once.
@@ -318,16 +318,16 @@ static int alloc_requests(struct usb_composite_dev *cdev,
* for out transfer and reuse them in IN transfers to implement
* our loopback functionality
*/
- for (i = 0; i < loop->qlen && result == 0; i++) {
- result = -ENOMEM;
-
- in_req = usb_ep_alloc_request(loop->in_ep, GFP_ATOMIC);
+ for (i = 0; i < loop->qlen; i++) {
+ struct usb_request *in_req __free(free_usb_request) =
+ usb_ep_alloc_request(loop->in_ep, GFP_ATOMIC);
if (!in_req)
- goto fail;
+ return -ENOMEM;
- out_req = lb_alloc_ep_req(loop->out_ep, loop->buflen);
+ struct usb_request *out_req __free(free_usb_request) =
+ lb_alloc_ep_req(loop->out_ep, loop->buflen);
if (!out_req)
- goto fail_in;
+ return -ENOMEM;
in_req->complete = loopback_complete;
out_req->complete = loopback_complete;
@@ -339,20 +339,16 @@ static int alloc_requests(struct usb_composite_dev *cdev,
result = usb_ep_queue(loop->out_ep, out_req, GFP_ATOMIC);
if (result) {
+ in_req->buf = NULL;
ERROR(cdev, "%s queue req --> %d\n",
loop->out_ep->name, result);
- goto fail_out;
+ return result;
}
+ retain_and_null_ptr(in_req);
+ retain_and_null_ptr(out_req);
}
return 0;
-
-fail_out:
- free_ep_req(loop->out_ep, out_req);
-fail_in:
- usb_ep_free_request(loop->in_ep, in_req);
-fail:
- return result;
}
static int enable_endpoint(struct usb_composite_dev *cdev,
--
2.51.1.851.g4ebd6896fd-goog
next prev parent reply other threads:[~2025-10-30 15:14 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-30 15:14 [PATCH 0/2] usb: gadget: Auto-cleanup usb_request in f_loopback, f_tcm Kuen-Han Tsai
2025-10-30 15:14 ` [PATCH 1/2] usb: gadget: f_tcm: Use auto-cleanup for usb_request Kuen-Han Tsai
2025-10-30 15:33 ` Greg Kroah-Hartman
2025-11-03 9:54 ` Kuen-Han Tsai
2025-10-30 15:14 ` Kuen-Han Tsai [this message]
2025-10-30 15:34 ` [PATCH 2/2] usb: gadget: f_loopback: " Greg Kroah-Hartman
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=20251030-auto-cleanup-v1-2-db30584fadfd@google.com \
--to=khtsai@google.com \
--cc=Thinh.Nguyen@synopsys.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@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