From patchwork Mon Apr 16 10:16:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Grigor Tovmasyan X-Patchwork-Id: 10342553 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AAEA760365 for ; Mon, 16 Apr 2018 10:16:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9679020121 for ; Mon, 16 Apr 2018 10:16:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8ACAD20246; Mon, 16 Apr 2018 10:16:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DAE432018E for ; Mon, 16 Apr 2018 10:16:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754391AbeDPKQU (ORCPT ); Mon, 16 Apr 2018 06:16:20 -0400 Received: from us01smtprelay-2.synopsys.com ([198.182.60.111]:54673 "EHLO smtprelay.synopsys.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754372AbeDPKQT (ORCPT ); Mon, 16 Apr 2018 06:16:19 -0400 Received: from mailhost.synopsys.com (mailhost2.synopsys.com [10.13.184.66]) by smtprelay.synopsys.com (Postfix) with ESMTP id C22EB10C118B; Mon, 16 Apr 2018 03:16:18 -0700 (PDT) Received: from US01WXQAHTC1.internal.synopsys.com (us01wxqahtc1.internal.synopsys.com [10.12.238.230]) by mailhost.synopsys.com (Postfix) with ESMTP id 95369373A; Mon, 16 Apr 2018 03:16:18 -0700 (PDT) Received: from US01WEHTC1.internal.synopsys.com (10.12.239.236) by US01WXQAHTC1.internal.synopsys.com (10.12.238.230) with Microsoft SMTP Server (TLS) id 14.3.361.1; Mon, 16 Apr 2018 03:16:18 -0700 Received: from tovmasya (10.13.184.20) by us01wehtc1.internal.synopsys.com (10.12.239.236) with Microsoft SMTP Server (TLS) id 14.3.361.1; Mon, 16 Apr 2018 03:16:16 -0700 Received: by tovmasya (sSMTP sendmail emulation); Mon, 16 Apr 2018 14:16:03 +0400 Date: Mon, 16 Apr 2018 14:16:03 +0400 Message-ID: From: Grigor Tovmasyan Subject: [PATCH] usb: dwc2: gadget: Fix memory leak in dwc2_gadget_init() To: Felipe Balbi , Greg Kroah-Hartman , Minas Harutyunyan , CC: John Youn , Grigor Tovmasyan , Stefan Wahren MIME-Version: 1.0 X-Originating-IP: [10.13.184.20] Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In dwc2_gadget_init() we allocate EP0 request via dwc2_hsotg_ep_alloc_request(). After that there are usb_add_gadget_udc() call and if it failed, then ctrl_req will not be freed and will cause memory leak. Reordered function calls in gadget_init: moved up usb_add_gadget_udc() before dwc2_hsotg_ep_alloc_request(). Tested using kmemleak. Cc: Stefan Wahren Signed-off-by: Grigor Tovmasyan Acked-by: Minas Harutyunyan --- drivers/usb/dwc2/gadget.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c index 6c32bf26e48e..24000bda5c20 100644 --- a/drivers/usb/dwc2/gadget.c +++ b/drivers/usb/dwc2/gadget.c @@ -4679,6 +4679,10 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg) INIT_LIST_HEAD(&hsotg->gadget.ep_list); hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep; + ret = usb_add_gadget_udc(dev, &hsotg->gadget); + if (ret) + return ret; + /* allocate EP0 request */ hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep, @@ -4698,10 +4702,6 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg) epnum, 0); } - ret = usb_add_gadget_udc(dev, &hsotg->gadget); - if (ret) - return ret; - dwc2_hsotg_dump(hsotg); return 0;