Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1614953ybv; Fri, 21 Feb 2020 00:19:35 -0800 (PST) X-Google-Smtp-Source: APXvYqzpSmXVQbBJFvHtFgm52sKmuETyW9oovt1eEXt5N/YxE8mKcAS4lSbw/mZabXJtLoTxSzGj X-Received: by 2002:a05:6830:1198:: with SMTP id u24mr26318566otq.215.1582273175219; Fri, 21 Feb 2020 00:19:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582273175; cv=none; d=google.com; s=arc-20160816; b=vEO0yvUOHEadRSmqJzdCHKZWtKBeG/9U5Bf4pBri7ssvZzu8/Yy89OfxA/t3PMjt02 SxTbl5wpi5gCAn8GyzSh/aDXrww1UQVkjSmFMRFiGyLymfihRLu2EjfwEiCFXwgUG7VO Lo8eZSMSdqWaNFB8N8LMdWn5HaPQz0O95t1UXqS57IJi1dzX9f1xFK7Z6f2K6pvkVGg4 YCYjXvz8k4GYHzbBvfaq8Hszbop1WilXDJKSiYe5H/HIH1GD7Ko1uwc34Wz0HhCgOCia Yezhi9yBDhK9aXfDOOagOjqXKzO10k1RuTF67wLQww/2n/9lCHuvGJ6eSC0KBP+g/MB2 NeiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=SfvUrtmpP1leYBTY7yoqiTeoD+vIdB7keuv+x3OpnoU=; b=s8qZJeLg73xDtcQJKXqySj8h4W+8GRi9y+uTjI9hLrJ2JZZyuHEYU4UdeWfGZVq8rc c5ngRynVz7j+GDsjuw4vG6BOVm9sJSiOsbP2sIeYMBwxt5R5IXXCyCsVYZUJaTVmERKn 2U+sxHLLPlc+SMphhy1TbkmVaXW8OAGQQukyDPA5p2SOoIGwVT6GE9lnqcl6VKafjkDs sgW6bPYSdWhjkHO9zS/rAA0jS3yVn0pd6hEo5Q5gXa/KM8wqyysrHO1j8z5FjFpQ6TzS //Y/c+/F0tJtOLC9FgiRJQhxXWPimq9rORcRJ94EvcsqpLqgpCpUqC8RBswhc6Eke4kb lDsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2nc16583; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k10si1139929otn.323.2020.02.21.00.19.23; Fri, 21 Feb 2020 00:19:35 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2nc16583; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387464AbgBUIRp (ORCPT + 99 others); Fri, 21 Feb 2020 03:17:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:55254 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733125AbgBUIRl (ORCPT ); Fri, 21 Feb 2020 03:17:41 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6CD592468A; Fri, 21 Feb 2020 08:17:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582273060; bh=2k5dLpzOTaQZE2SXUGDau5ksnSG+ZmybjEBTX6qlPmc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2nc16583SmNKzsC2JTQpkVhpBULHq6ZV8zuBCuCTqnCY2Lqj+XVK0UevQH/exyOAc hMipvtsw+O9R+sIP0X6Moxn2ZYBCFxbfnn262ljAfHCX/5BgUrvuGQni7zqL32SvJK cR85gn1lgYOVRkjyMzFbCOW8os9oz8xWOVn+aO4U= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jia-Ju Bai , Felipe Balbi , Sasha Levin Subject: [PATCH 4.19 032/191] usb: gadget: udc: fix possible sleep-in-atomic-context bugs in gr_probe() Date: Fri, 21 Feb 2020 08:40:05 +0100 Message-Id: <20200221072255.095987384@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200221072250.732482588@linuxfoundation.org> References: <20200221072250.732482588@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jia-Ju Bai [ Upstream commit 9c1ed62ae0690dfe5d5e31d8f70e70a95cb48e52 ] The driver may sleep while holding a spinlock. The function call path (from bottom to top) in Linux 4.19 is: drivers/usb/gadget/udc/core.c, 1175: kzalloc(GFP_KERNEL) in usb_add_gadget_udc_release drivers/usb/gadget/udc/core.c, 1272: usb_add_gadget_udc_release in usb_add_gadget_udc drivers/usb/gadget/udc/gr_udc.c, 2186: usb_add_gadget_udc in gr_probe drivers/usb/gadget/udc/gr_udc.c, 2183: spin_lock in gr_probe drivers/usb/gadget/udc/core.c, 1195: mutex_lock in usb_add_gadget_udc_release drivers/usb/gadget/udc/core.c, 1272: usb_add_gadget_udc_release in usb_add_gadget_udc drivers/usb/gadget/udc/gr_udc.c, 2186: usb_add_gadget_udc in gr_probe drivers/usb/gadget/udc/gr_udc.c, 2183: spin_lock in gr_probe drivers/usb/gadget/udc/gr_udc.c, 212: debugfs_create_file in gr_probe drivers/usb/gadget/udc/gr_udc.c, 2197: gr_dfs_create in gr_probe drivers/usb/gadget/udc/gr_udc.c, 2183: spin_lock in gr_probe drivers/usb/gadget/udc/gr_udc.c, 2114: devm_request_threaded_irq in gr_request_irq drivers/usb/gadget/udc/gr_udc.c, 2202: gr_request_irq in gr_probe drivers/usb/gadget/udc/gr_udc.c, 2183: spin_lock in gr_probe kzalloc(GFP_KERNEL), mutex_lock(), debugfs_create_file() and devm_request_threaded_irq() can sleep at runtime. To fix these possible bugs, usb_add_gadget_udc(), gr_dfs_create() and gr_request_irq() are called without handling the spinlock. These bugs are found by a static analysis tool STCheck written by myself. Signed-off-by: Jia-Ju Bai Signed-off-by: Felipe Balbi Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/gadget/udc/gr_udc.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/usb/gadget/udc/gr_udc.c b/drivers/usb/gadget/udc/gr_udc.c index 729e60e495641..e50108f9a374e 100644 --- a/drivers/usb/gadget/udc/gr_udc.c +++ b/drivers/usb/gadget/udc/gr_udc.c @@ -2180,8 +2180,6 @@ static int gr_probe(struct platform_device *pdev) return -ENOMEM; } - spin_lock(&dev->lock); - /* Inside lock so that no gadget can use this udc until probe is done */ retval = usb_add_gadget_udc(dev->dev, &dev->gadget); if (retval) { @@ -2190,15 +2188,21 @@ static int gr_probe(struct platform_device *pdev) } dev->added = 1; + spin_lock(&dev->lock); + retval = gr_udc_init(dev); - if (retval) + if (retval) { + spin_unlock(&dev->lock); goto out; - - gr_dfs_create(dev); + } /* Clear all interrupt enables that might be left on since last boot */ gr_disable_interrupts_and_pullup(dev); + spin_unlock(&dev->lock); + + gr_dfs_create(dev); + retval = gr_request_irq(dev, dev->irq); if (retval) { dev_err(dev->dev, "Failed to request irq %d\n", dev->irq); @@ -2227,8 +2231,6 @@ static int gr_probe(struct platform_device *pdev) dev_info(dev->dev, "regs: %p, irq %d\n", dev->regs, dev->irq); out: - spin_unlock(&dev->lock); - if (retval) gr_remove(pdev); -- 2.20.1