Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp620962pxj; Thu, 20 May 2021 18:08:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzIAPXJpM61L4wWb0aQVj8M6n+6xgNUMDphy4vHGxomVoAuM58XOOgxuX0f1NG4AOdMgFsT X-Received: by 2002:a17:907:768c:: with SMTP id jv12mr7121340ejc.215.1621559337532; Thu, 20 May 2021 18:08:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621559337; cv=none; d=google.com; s=arc-20160816; b=FDatMaLuj0uuryhcCQwjq0HNJuW47FwtSO5StAPw06zM4Oa96ATOaET/KeL3voAxlP xhGIb4qTkxRfd9mkEIyAcC6mcfmNTEx75qlkzsdDiusGGQVd8kXLaiF/woCySq1IG9K4 aGpW4LiMFMjJNcccS7bIVyUwB9iR+Zob7MF7/TL5iHqpvO018e5jXKWDDgpKuDSFxfQ2 lAGe3YQXsD/IHdZOtSBCqGEyTTJ0UiS5FdTurRNrqXVkp2L/w5fgdVz+qWXmHCx3VSbT bhzccMQ9xgbF6JevCMN3xDHSJ0BKqwjiJVnF9WjgaIczaU5aHC/TFpOa5wbTuSSQStjM X28g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LqffvIygJezt2bqUO50tPBD4rfdurh2nQzv4VIDhSG8=; b=YHSvAEUc2mZVGyYfqfOLsa2nBruDTq1NM46/X3UwuEkDdjRUrquhDpCvkr1XePyJMb gq0fGN26rUWZzUlcGyhDGtHpoO2YWTJG+S1sRGXTIXRrKZ7Frs7cmpLbHJ/V4SLYMIyH 3XCyP+Etvr62YuaYvbTgdKPnJ2iyuOndrealOMJRtVO0W8mVKkzgkep4C9oTHnK6V2o9 bEwVlOcL4VipIGluR/WFmn92BXgN/7VOY+TTTHW78vOcCVPTEDQx+ppSeWYH4OpNTEUh sDv/lfHPAJOO3/BSWF/JSCv5H3YsFwo8xZSQdk02XDFFUnNuw36pieWzn5Gl641BrK+a YkKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=x5SQPhmT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a20si3745701ejt.620.2021.05.20.18.08.33; Thu, 20 May 2021 18:08:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=x5SQPhmT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242498AbhETLjq (ORCPT + 99 others); Thu, 20 May 2021 07:39:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:38798 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240240AbhETLRy (ORCPT ); Thu, 20 May 2021 07:17:54 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B1A5D61959; Thu, 20 May 2021 10:09:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621505393; bh=S4Iivk6AzkpjutMY7Iuibz0oTZnCHlM6cOxr0pmGD4c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=x5SQPhmT0VlxnlV8aiWiALh780XOMZ+iQkG5IpDOaOcBuVO7e9jz7RD3dBiu0HPdr rB8qGhzJOANetS7V/9MX3vVphDs2fkmG7Bzsc8lWunVE5eRGvUJUGNn2FR9TaVYlwH o0xNb7KppvbgmXu4eDFkj/SeUrctiGv5SVLYboVM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andy Shevchenko , Sasha Levin Subject: [PATCH 4.4 080/190] usb: gadget: pch_udc: Check if driver is present before calling ->setup() Date: Thu, 20 May 2021 11:22:24 +0200 Message-Id: <20210520092104.846231521@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092102.149300807@linuxfoundation.org> References: <20210520092102.149300807@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andy Shevchenko [ Upstream commit fbdbbe6d3ee502b3bdeb4f255196bb45003614be ] Since we have a separate routine for VBUS sense, the interrupt may occur before gadget driver is present. Hence, ->setup() call may oops the kernel: [ 55.245843] BUG: kernel NULL pointer dereference, address: 00000010 ... [ 55.245843] EIP: pch_udc_isr.cold+0x162/0x33f ... [ 55.245843] [ 55.245843] ? pch_udc_svc_data_out+0x160/0x160 Check if driver is present before calling ->setup(). Fixes: f646cf94520e ("USB device driver of Topcliff PCH") Signed-off-by: Andy Shevchenko Link: https://lore.kernel.org/r/20210323153626.54908-2-andriy.shevchenko@linux.intel.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/gadget/udc/pch_udc.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/drivers/usb/gadget/udc/pch_udc.c b/drivers/usb/gadget/udc/pch_udc.c index 6e84b27c14dd..152c2ee0ca50 100644 --- a/drivers/usb/gadget/udc/pch_udc.c +++ b/drivers/usb/gadget/udc/pch_udc.c @@ -2341,6 +2341,21 @@ static void pch_udc_svc_data_out(struct pch_udc_dev *dev, int ep_num) pch_udc_set_dma(dev, DMA_DIR_RX); } +static int pch_udc_gadget_setup(struct pch_udc_dev *dev) + __must_hold(&dev->lock) +{ + int rc; + + /* In some cases we can get an interrupt before driver gets setup */ + if (!dev->driver) + return -ESHUTDOWN; + + spin_unlock(&dev->lock); + rc = dev->driver->setup(&dev->gadget, &dev->setup_data); + spin_lock(&dev->lock); + return rc; +} + /** * pch_udc_svc_control_in() - Handle Control IN endpoint interrupts * @dev: Reference to the device structure @@ -2412,15 +2427,12 @@ static void pch_udc_svc_control_out(struct pch_udc_dev *dev) dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IDX].ep; else /* OUT */ dev->gadget.ep0 = &ep->ep; - spin_lock(&dev->lock); /* If Mass storage Reset */ if ((dev->setup_data.bRequestType == 0x21) && (dev->setup_data.bRequest == 0xFF)) dev->prot_stall = 0; /* call gadget with setup data received */ - setup_supported = dev->driver->setup(&dev->gadget, - &dev->setup_data); - spin_unlock(&dev->lock); + setup_supported = pch_udc_gadget_setup(dev); if (dev->setup_data.bRequestType & USB_DIR_IN) { ep->td_data->status = (ep->td_data->status & @@ -2673,9 +2685,7 @@ static void pch_udc_svc_intf_interrupt(struct pch_udc_dev *dev) dev->ep[i].halted = 0; } dev->stall = 0; - spin_unlock(&dev->lock); - dev->driver->setup(&dev->gadget, &dev->setup_data); - spin_lock(&dev->lock); + pch_udc_gadget_setup(dev); } /** @@ -2710,9 +2720,7 @@ static void pch_udc_svc_cfg_interrupt(struct pch_udc_dev *dev) dev->stall = 0; /* call gadget zero with setup data received */ - spin_unlock(&dev->lock); - dev->driver->setup(&dev->gadget, &dev->setup_data); - spin_lock(&dev->lock); + pch_udc_gadget_setup(dev); } /** -- 2.30.2