Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4594290pxj; Wed, 12 May 2021 08:53:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVppiElKaNRvOShvMuniIheifSUxWitoh1L5vD3k8bZiEFsyhMl6H5129NzS3WGlUUDgrW X-Received: by 2002:aa7:c98b:: with SMTP id c11mr44393658edt.50.1620834836021; Wed, 12 May 2021 08:53:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620834836; cv=none; d=google.com; s=arc-20160816; b=WI6JFFyO6lnW3AaA6+FXCzAU2XKjnBVU1hoI19Pa88SP8uxXLDrcJDdcBASRO/qgsh pCsstZHrSU5oDLWURZPxKvzlYh2zFbcHm6SumXaPnyKVA9i9vUR3do9gzfM/rBm87oWM QosbTzM6lobpsqUvzpQpbeWtzTxpYVJnzKpiI4Hs4ou5fhce3QpQPlrdMl6GVL0Gxguc 64tzwD1TvEksNb2sOX6fTOPc4rhbY6sNEAISA5IEtsmPfPbFnXIn/TQ+3L3Yn50iSGwt yjMM1pbMPV1aNceFcTPoBIHvvsZA40G90mSP22PRgROvqBtXuBACKtfUG0cs/kmEaDny 9I6w== 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=/F0iyyq/dA1PIBxBJNKdVETFFBOqK2voJfRqoXC8K7s=; b=wCb/lMvvCj43piKzC9jdOk8i3xea7x+5w4vA3DXsJfsqzVOE4nhPEDSBhJYuo8DCw2 0WvYGoCmK4/hitOL1oEFPfv5PQiklngLNSseYYVkZUIWjp9Lo8tkQNAP6O6+o7qjNu7f uCgUPqZp1d/TKsvLUm+CIAP0MTSdIufvPFsN+jEEomkcM2mEGs/dBTvmiXeGoPA7/Rux KA65u0h1U9fKnBchfUu7XfGSJJEFO+PSOQPAZY8RF9kHgzD0qEtNGaV6rFKxmgaYJFEL rI1t3PTbt7jeYzVqcgXinWWpFfV+GhB0k344gAHnblDch5lWHQaeZaWh9sjJyDcYDNP0 u63A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KHhsAgGK; 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 y8si262926ejw.685.2021.05.12.08.53.32; Wed, 12 May 2021 08:53:56 -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=KHhsAgGK; 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 S237130AbhELPsX (ORCPT + 99 others); Wed, 12 May 2021 11:48:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:60802 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233639AbhELPWb (ORCPT ); Wed, 12 May 2021 11:22:31 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CEC11619B3; Wed, 12 May 2021 15:09:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620832168; bh=xQ7JJKljKye7E4VXcnFMRPqTY7ARUXpzKOFkrNL9TVo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KHhsAgGKow/bubLBvlH2ZNuYCsgQVwiAUtDlW5YDRaniQ15njSjcwSugBCkYkJasm dovH5fHFEEG8kyfbfWhtuA/dVN/KGXEvj5Mpo14yLowldNEWazDlz4MvOhtQk0QfHe 8ZcA6xNuL6qwGvS9ki38zgadoUVi6bXhYjmN3VL0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andy Shevchenko , Sasha Levin Subject: [PATCH 5.10 169/530] usb: gadget: pch_udc: Check if driver is present before calling ->setup() Date: Wed, 12 May 2021 16:44:39 +0200 Message-Id: <20210512144825.390778947@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144819.664462530@linuxfoundation.org> References: <20210512144819.664462530@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 f6f8965c4f8c..b46e9bdc720f 100644 --- a/drivers/usb/gadget/udc/pch_udc.c +++ b/drivers/usb/gadget/udc/pch_udc.c @@ -2307,6 +2307,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 @@ -2378,15 +2393,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 & @@ -2634,9 +2646,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); } /** @@ -2671,9 +2681,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