From nobody Sun Feb 8 00:03:36 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 544362222B2 for ; Thu, 30 Oct 2025 15:14:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837283; cv=none; b=oNwD9vtK3A9CzgIDMA/JzR+a88m8nu0kjprn2flz7IwDDTAePGbC6As2upKLvoNBC8HIfWGpgS6FaIcgsem7ovzzyw6v0pQkjb6cZW6kPcWP8m7wQAXIBUJnFe0Hkq7ITh7C9aP98F8R29C3w07iEJV7tQQgN+dLY53+qc2itoU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837283; c=relaxed/simple; bh=q59XP1HDZ7mD5Vmiv8zo+s7Q4fPF5v6QJu61zUHa8Jg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=niEMIpAWh7jsNzwtFFUVUsSHaFuUh+Hqg1I1EPFVYaX3HelMkqPCEAo2wnKaq11BiYVj3FBQ43R3MW5E0R5QQjWY6LeRbTe99zheRfXFiFVLDdL1ck/ZuA+OCxjEvXGp5pou7t88GcwK604BsDMhDY92gcU6Um71+fXRTkK3bmk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=AW2PYHRm; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AW2PYHRm" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-290b13c5877so23356215ad.0 for ; Thu, 30 Oct 2025 08:14:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1761837281; x=1762442081; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tOKsrqWz9v3pH3CqifzxDRCIt1c5IhuvPUyONKMbswY=; b=AW2PYHRmHIQRn9gsiphQ1uyQb/TI/1QHZf7cbS3KH8NQlZtGrXMNtz/gwEmL0fM82Q vBRWUiJa6WBlBB/mBnB32tGxjm2sAVbr/Usw9w5zrG7kzZlNoWUrFNbEurGYCHuxcfPf czqH6OjmjUormKUYeJuPxYzLwNsi8SJYmbIdT8sCHL8UiZ9/s918qzggMI35Mjow9LH6 NuVnssQU0TpIIiNHXpVvETTUIcSDDD4LEo1092OXmynOrXyo6LqWvpfXDqdhrmlI3vzO N+9SPdRQB88TNLZHGsNuske8yGskY+e0xU0O/+mNmI0LE+TG4Hl2B7+8WJilbkK4l1xn bQuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761837281; x=1762442081; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tOKsrqWz9v3pH3CqifzxDRCIt1c5IhuvPUyONKMbswY=; b=tkVbKwUhM+Nn0rn3lTqrdI3ND7+N1C1+Pw3qM0ywRd41H9lKkZRP9k54zLW5B9mWQl li0eKe0ELI+VowPqoaf4zI6WdKU56jd3QIRfgf0Wiy8L1ovx9fxZQFpYVjTHsg7hV2Aq 3+rF9dPfW3Z3d/oKYl2JQ6ObHRTKsKqqcRleSKcF4HnQ28B2Dz7BPaT2nVtwJEGFOq03 1s4vtHI2CMhYdS01GEE2B8ZdveErazzpYPk1u77V3Cho60Vbv1cEJiHRVM2w2ulf3v7Y fqu2+CymHfmvW5SxWBE4AS7U/t8idV2hu/bI7d5yKqJIE1OOJzwSK+KBGO2d+Fg4ungZ wK6Q== X-Forwarded-Encrypted: i=1; AJvYcCVYqf4Ss8/iJBB/pvKU1AhnClIMK2quIngiWFAdJf8rJDHqM/0SSQ+lpacJdbgPnhTtqAbj8KAacoNzpjU=@vger.kernel.org X-Gm-Message-State: AOJu0YwR6bYHGW9mITjasVn/81ZzHze2Jfqb+HIq8oZLUVAm2K6kBQTq Q7AsZfJd1QQfqeHyQMNT2d2HbN+sV2qtztRls3lZ2V2sCaGtG6YBm+nYiu5DEprUjxNtKV/+9kM tGhmQGg== X-Google-Smtp-Source: AGHT+IEKkb/E2RKrkxusDM6cv5FWmALGXDAB7THKuaYzKiPsWfnnW5P2usl6VZaAYaqa1LCy691i9SOKuec= X-Received: from plbbf4.prod.google.com ([2002:a17:902:b904:b0:269:740f:8ae8]) (user=khtsai job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1666:b0:269:9a7a:9a43 with SMTP id d9443c01a7336-2951a38dde4mr888805ad.10.1761837281314; Thu, 30 Oct 2025 08:14:41 -0700 (PDT) Date: Thu, 30 Oct 2025 23:14:19 +0800 In-Reply-To: <20251030-auto-cleanup-v1-0-db30584fadfd@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251030-auto-cleanup-v1-0-db30584fadfd@google.com> X-Developer-Key: i=khtsai@google.com; a=ed25519; pk=abA4Pw6dY2ZufSbSXW9mtp7xiv1AVPtgRhCFWJSEqLE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1761837277; l=6655; i=khtsai@google.com; s=20250916; h=from:subject:message-id; bh=q59XP1HDZ7mD5Vmiv8zo+s7Q4fPF5v6QJu61zUHa8Jg=; b=Njk3fnvT+Wbu4er1kE+A2IO10zYzzLahZCQzzaEH4owrPu0dfd2NBILAdjCmPYrbAYGiArzoE trNJqOzMBcbBkQAUVdW294ZmAboq+ecfqU56iOR8NkdY5Oa4rXJpHQt X-Mailer: b4 0.14.2 Message-ID: <20251030-auto-cleanup-v1-1-db30584fadfd@google.com> Subject: [PATCH 1/2] usb: gadget: f_tcm: Use auto-cleanup for usb_request From: Kuen-Han Tsai To: Greg Kroah-Hartman , Thinh Nguyen Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Kuen-Han Tsai Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Refactor f_tcm.c to use auto-cleanup mechanism for usb_request allocations in bot_prepare_reqs(), uasp_alloc_stream_res(), and uasp_alloc_cmd(). The explicit nullification of fu->..._req and stream->..._req pointers on error is no longer needed. This is safe because these pointers are only updated after all allocations within the function have succeeded. If an error occurs, the fu structure members retain their previous value, and the existing cleanup functions like bot_cleanup_old_alt() and uasp_cleanup_old_alt() already handle stale pointers in the fu structure. Signed-off-by: Kuen-Han Tsai --- drivers/usb/gadget/function/f_tcm.c | 141 +++++++++++++++++---------------= ---- 1 file changed, 67 insertions(+), 74 deletions(-) diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/funct= ion/f_tcm.c index 6e8804f04baa774f5e6bed548b64769e93f6eb1c..782995040af3acdb42d380b4dbb= 012941592da06 100644 --- a/drivers/usb/gadget/function/f_tcm.c +++ b/drivers/usb/gadget/function/f_tcm.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -309,57 +310,54 @@ static int bot_prepare_reqs(struct f_uas *fu) { int ret; =20 - fu->bot_req_in =3D usb_ep_alloc_request(fu->ep_in, GFP_KERNEL); - if (!fu->bot_req_in) - goto err; - - fu->bot_req_out =3D usb_ep_alloc_request(fu->ep_out, GFP_KERNEL); - if (!fu->bot_req_out) - goto err_out; + struct usb_request *bot_req_in __free(free_usb_request) =3D + usb_ep_alloc_request(fu->ep_in, GFP_KERNEL); + if (!bot_req_in) + return -ENOMEM; =20 - fu->cmd[0].req =3D usb_ep_alloc_request(fu->ep_out, GFP_KERNEL); - if (!fu->cmd[0].req) - goto err_cmd; + struct usb_request *bot_req_out __free(free_usb_request) =3D + usb_ep_alloc_request(fu->ep_out, GFP_KERNEL); + if (!bot_req_out) + return -ENOMEM; =20 - fu->bot_status.req =3D usb_ep_alloc_request(fu->ep_in, GFP_KERNEL); - if (!fu->bot_status.req) - goto err_sts; + struct usb_request *cmd_req __free(free_usb_request) =3D + usb_ep_alloc_request(fu->ep_out, GFP_KERNEL); + if (!cmd_req) + return -ENOMEM; =20 - fu->bot_status.req->buf =3D &fu->bot_status.csw; - fu->bot_status.req->length =3D US_BULK_CS_WRAP_LEN; - fu->bot_status.req->complete =3D bot_status_complete; - fu->bot_status.csw.Signature =3D cpu_to_le32(US_BULK_CS_SIGN); + struct usb_request *status_req __free(free_usb_request) =3D + usb_ep_alloc_request(fu->ep_in, GFP_KERNEL); + if (!status_req) + return -ENOMEM; =20 - fu->cmd[0].buf =3D kmalloc(fu->ep_out->maxpacket, GFP_KERNEL); - if (!fu->cmd[0].buf) - goto err_buf; + cmd_req->buf =3D kmalloc(fu->ep_out->maxpacket, GFP_KERNEL); + if (!cmd_req->buf) + return -ENOMEM; =20 - fu->cmd[0].req->complete =3D bot_cmd_complete; - fu->cmd[0].req->buf =3D fu->cmd[0].buf; - fu->cmd[0].req->length =3D fu->ep_out->maxpacket; - fu->cmd[0].req->context =3D fu; + cmd_req->complete =3D bot_cmd_complete; + cmd_req->length =3D fu->ep_out->maxpacket; + cmd_req->context =3D fu; =20 ret =3D bot_enqueue_cmd_cbw(fu); if (ret) - goto err_queue; + return ret; + + fu->bot_req_in =3D no_free_ptr(bot_req_in); + fu->bot_req_out =3D no_free_ptr(bot_req_out); + + /* This line is placed here because free_usb_request also frees its + * buffer, which in this case points to the static fu->bot_status.csw. + */ + status_req->buf =3D &fu->bot_status.csw; + status_req->length =3D US_BULK_CS_WRAP_LEN; + status_req->complete =3D bot_status_complete; + fu->bot_status.csw.Signature =3D cpu_to_le32(US_BULK_CS_SIGN); + fu->bot_status.req =3D no_free_ptr(status_req); + + fu->cmd[0].buf =3D cmd_req->buf; + fu->cmd[0].req =3D no_free_ptr(cmd_req); + return 0; -err_queue: - kfree(fu->cmd[0].buf); - fu->cmd[0].buf =3D NULL; -err_buf: - usb_ep_free_request(fu->ep_in, fu->bot_status.req); -err_sts: - usb_ep_free_request(fu->ep_out, fu->cmd[0].req); - fu->cmd[0].req =3D NULL; -err_cmd: - usb_ep_free_request(fu->ep_out, fu->bot_req_out); - fu->bot_req_out =3D NULL; -err_out: - usb_ep_free_request(fu->ep_in, fu->bot_req_in); - fu->bot_req_in =3D NULL; -err: - pr_err("BOT: endpoint setup failed\n"); - return -ENOMEM; } =20 static void bot_cleanup_old_alt(struct f_uas *fu) @@ -878,50 +876,45 @@ static int uasp_alloc_stream_res(struct f_uas *fu, st= ruct uas_stream *stream) { init_completion(&stream->cmd_completion); =20 - stream->req_in =3D usb_ep_alloc_request(fu->ep_in, GFP_KERNEL); - if (!stream->req_in) - goto out; + struct usb_request *req_in __free(free_usb_request) =3D + usb_ep_alloc_request(fu->ep_in, GFP_KERNEL); + if (!req_in) + return -ENOMEM; =20 - stream->req_out =3D usb_ep_alloc_request(fu->ep_out, GFP_KERNEL); - if (!stream->req_out) - goto err_out; + struct usb_request *req_out __free(free_usb_request) =3D + usb_ep_alloc_request(fu->ep_out, GFP_KERNEL); + if (!req_out) + return -ENOMEM; =20 - stream->req_status =3D usb_ep_alloc_request(fu->ep_status, GFP_KERNEL); + struct usb_request *req_status __free(free_usb_request) =3D + usb_ep_alloc_request(fu->ep_status, GFP_KERNEL); if (!stream->req_status) - goto err_sts; + return -ENOMEM; =20 - return 0; + stream->req_in =3D no_free_ptr(req_in); + stream->req_out =3D no_free_ptr(req_out); + stream->req_status =3D no_free_ptr(req_status); =20 -err_sts: - usb_ep_free_request(fu->ep_out, stream->req_out); - stream->req_out =3D NULL; -err_out: - usb_ep_free_request(fu->ep_in, stream->req_in); - stream->req_in =3D NULL; -out: - return -ENOMEM; + return 0; } =20 static int uasp_alloc_cmd(struct f_uas *fu, int i) { - fu->cmd[i].req =3D usb_ep_alloc_request(fu->ep_cmd, GFP_KERNEL); - if (!fu->cmd[i].req) - goto err; + struct usb_request *req __free(free_usb_request) =3D + usb_ep_alloc_request(fu->ep_cmd, GFP_KERNEL); + if (!req) + return -ENOMEM; =20 - fu->cmd[i].buf =3D kmalloc(fu->ep_cmd->maxpacket, GFP_KERNEL); - if (!fu->cmd[i].buf) - goto err_buf; + req->buf =3D kmalloc(fu->ep_cmd->maxpacket, GFP_KERNEL); + if (!req->buf) + return -ENOMEM; =20 - fu->cmd[i].req->complete =3D uasp_cmd_complete; - fu->cmd[i].req->buf =3D fu->cmd[i].buf; - fu->cmd[i].req->length =3D fu->ep_cmd->maxpacket; - fu->cmd[i].req->context =3D fu; + req->complete =3D uasp_cmd_complete; + req->length =3D fu->ep_cmd->maxpacket; + req->context =3D fu; + fu->cmd[i].buf =3D req->buf; + fu->cmd[i].req =3D no_free_ptr(req); return 0; - -err_buf: - usb_ep_free_request(fu->ep_cmd, fu->cmd[i].req); -err: - return -ENOMEM; } =20 static int uasp_prepare_reqs(struct f_uas *fu) --=20 2.51.1.851.g4ebd6896fd-goog From nobody Sun Feb 8 00:03:36 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52A0F32E122 for ; Thu, 30 Oct 2025 15:14:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837285; cv=none; b=n7gg6wn8V0qrNDi5OC76idlMir4A4KtcmbkRMU38YNxw+/8QFsUToq7J0U+CsfDIXHVGoTRxneOzsJmtvO3mKdh7Aors1zrgkGCxlRHgopQC9e6U/x7zpCfw/qSbV8cxdZdSJF9hlb4mwQzdiXRvDZYXXvCn3nNqoGNhIpwnkgg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837285; c=relaxed/simple; bh=WueqPrt9PcU5UOs3/n/eywvcdgK3zO0SwyWhjHm1VB8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SohOStKqGi/C59UDUG9Fw99FkDJyH8x4HVMgqFrr2k2pb2lFW0UWt8zMm8XTm3hIRTsDxO/0UxjxiR8gZa/S3pLKhkxBiBDHFnmIZK4/m4n5/oJR4oahq+SL3EipIKWMdGtSiXKIL1cIdrUiXR8Oa8yKq3yMG1MMu2emDR8MZA0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tWhBEaKS; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tWhBEaKS" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b57cf8dba28so899181a12.1 for ; Thu, 30 Oct 2025 08:14:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1761837283; x=1762442083; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lRXCLymhr/IzWTqsnVoHqbEbb803ekNgoGS0KjH34wQ=; b=tWhBEaKS73ObveaESw+PdHDH6y4fPqDXrb2+YBN1VG+yKtE4LtprxObnwLL0C55JT+ dSgBT5pVEbR8zF475n8zZHQvitrgO7Cu9/WhZ/pZDOfsXo5U7JmSdzdZJ49AYhi3fZ1O VsFWqVTnFo+Ivi1iN9uu/LPbCq6LHg0ZlDdBuFzQ2SskyfgAjDfeC6MB2zFxmVlZRgI9 VaqoapxkJgee5DR96PC5P5Y2q1vXtu0hpFQfeTWXSnOOo/U/pTAXRm4yfRNqD/B1BmQ6 7fPtbPZ0LKy5mJ+9JVIv0K8YnnxeEsaZ81hBqVPnf1s3ZouSluzGGmSg8nzCOQrwyYFG bu2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761837283; x=1762442083; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lRXCLymhr/IzWTqsnVoHqbEbb803ekNgoGS0KjH34wQ=; b=VNixfTJwAsBbvvUf8T+G5pLD3X4r9SjbuqXsnFTmZX1hVJ9QCZ7sqRdFYEthUNXnmz 7x7ND821RXrvCa2y5vVJ+H27K/Ou+1Il3QwrYrWuVvU9RTGDz0i2zwW/l+SrCWIUQSpu RcLjBRVHh23jkM5r1tEbVSCd3Q2upZtsFll+SWX2R2M8Lr/ihA94NC3CGZD6wtV8tEi/ PN5YHGb6Sk6rFd+50e1kYKuPFoKl+PPoMJhO8vM0TZWK/TnatWQaRVWiA0QFWJQY6BI/ vrAyRuEwRyBY9j6kZWxQ0gF4oP5H1UT0KF7pjf93VCuwUTMKTJlM2DQVNAsCTWV8Osj5 J6pQ== X-Forwarded-Encrypted: i=1; AJvYcCVQcnPhpGcluAyYsttXfXtZW5vAGnwFbbfVkYLqqbkx3ssI2mu9eru/ebAaJrY04X4QDJeRgdDNg3zmzVc=@vger.kernel.org X-Gm-Message-State: AOJu0Ywm5JVrp7sgAc8Ys14wiZlgydoZEV6t5MIFjBzVKE+AbbV41qO8 NbYUDBgEXkQrTjwepxU/5m6JMVS51Kua975+Q5M7FnPBlFCPTFpXUWvCWWZo9N/Q7hJYzwhoeAs LURT+CQ== X-Google-Smtp-Source: AGHT+IEBtKmRUWaO2GNwcap42mmQ1ywV3rSvOmFQAJJPExluSwBKsC0Atoqd3JmNEkv44E2DXEhyBeksJfQ= X-Received: from plkg14.prod.google.com ([2002:a17:903:19ce:b0:290:d4c5:90ad]) (user=khtsai job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1103:b0:290:dd1f:3d60 with SMTP id d9443c01a7336-2951a49de0amr928475ad.51.1761837283473; Thu, 30 Oct 2025 08:14:43 -0700 (PDT) Date: Thu, 30 Oct 2025 23:14:20 +0800 In-Reply-To: <20251030-auto-cleanup-v1-0-db30584fadfd@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251030-auto-cleanup-v1-0-db30584fadfd@google.com> X-Developer-Key: i=khtsai@google.com; a=ed25519; pk=abA4Pw6dY2ZufSbSXW9mtp7xiv1AVPtgRhCFWJSEqLE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1761837277; l=2782; i=khtsai@google.com; s=20250916; h=from:subject:message-id; bh=WueqPrt9PcU5UOs3/n/eywvcdgK3zO0SwyWhjHm1VB8=; b=6ueaX8sq1lNgoMah3k+GxkT1TUasXnrHj6yuhEYnrosVPWFY9rHWt2WEtsp7xWojuHqzjsk+x cyKY86DIf2gA6j0Brz1NNbRKN1eTGaIaENcnoDgBq2Z8wvxGb8/I15C X-Mailer: b4 0.14.2 Message-ID: <20251030-auto-cleanup-v1-2-db30584fadfd@google.com> Subject: [PATCH 2/2] usb: gadget: f_loopback: Use auto-cleanup for usb_request From: Kuen-Han Tsai To: Greg Kroah-Hartman , Thinh Nguyen Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Kuen-Han Tsai Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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 --- 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..cdf42d8b3d774e5324e83dc308a= a5caa265eac3b 100644 --- a/drivers/usb/gadget/function/f_loopback.c +++ b/drivers/usb/gadget/function/f_loopback.c @@ -8,6 +8,7 @@ =20 /* #define VERBOSE_DEBUG */ =20 +#include #include #include #include @@ -308,9 +309,8 @@ static inline struct usb_request *lb_alloc_ep_req(struc= t 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 =3D 0; + int result; =20 /* * allocate a bunch of read buffers and queue them all at once. @@ -318,16 +318,16 @@ static int alloc_requests(struct usb_composite_dev *c= dev, * for out transfer and reuse them in IN transfers to implement * our loopback functionality */ - for (i =3D 0; i < loop->qlen && result =3D=3D 0; i++) { - result =3D -ENOMEM; - - in_req =3D usb_ep_alloc_request(loop->in_ep, GFP_ATOMIC); + for (i =3D 0; i < loop->qlen; i++) { + struct usb_request *in_req __free(free_usb_request) =3D + usb_ep_alloc_request(loop->in_ep, GFP_ATOMIC); if (!in_req) - goto fail; + return -ENOMEM; =20 - out_req =3D lb_alloc_ep_req(loop->out_ep, loop->buflen); + struct usb_request *out_req __free(free_usb_request) =3D + lb_alloc_ep_req(loop->out_ep, loop->buflen); if (!out_req) - goto fail_in; + return -ENOMEM; =20 in_req->complete =3D loopback_complete; out_req->complete =3D loopback_complete; @@ -339,20 +339,16 @@ static int alloc_requests(struct usb_composite_dev *c= dev, =20 result =3D usb_ep_queue(loop->out_ep, out_req, GFP_ATOMIC); if (result) { + in_req->buf =3D 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); } =20 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; } =20 static int enable_endpoint(struct usb_composite_dev *cdev, --=20 2.51.1.851.g4ebd6896fd-goog