Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp101175pxa; Fri, 31 Jul 2020 07:25:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwMY06P6Kl79mbds6PXukaPZZYydl02vjMYppCzLuNhgRVpCX/uXfS+ckxf4WkOJLMMZ58p X-Received: by 2002:aa7:d516:: with SMTP id y22mr4093253edq.221.1596205559502; Fri, 31 Jul 2020 07:25:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596205559; cv=none; d=google.com; s=arc-20160816; b=g+dRM7yP3OpOzFgX2fZj4okbJr37U8gAhKWtffmFcc4e7LYiuiEsIkngFjj63XIKET zBE9FkWN/YS+ApAwGquSjI1BVzIVIYuaF4hfGUEUs3F5x/I/bYLt4Hfx3Lt0k1oqFXj2 HnS22QLQmTZIqLHj52wkHnRG4TCOflzvJnz3B39DJBReHMXPQJKIAFCE5uRduT/TFqJr +2+xltBJyTSuo+jSVnW709AFzRyf0s60hRFAlebZ/Sn9FZ1mxBUhhgFB95/yCvx/aJ4l o5Q4Z3HKj+BzKN6hOCucTt15KYm6UmSEjwwHRLmLhiL/zVDeO+Rwmkr250uURrzTUm10 m4sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=BkKxVD4BOOKVwdnFseZS6/1qW+buVkVWjYLmiKN0YSM=; b=mhE1IosUcgSWfT+OVsVHh9yJrZTxeMXFPUATPlLbNH53cwdGT83KmSiy4jtbOTzhnU +gnTs3H8s0g4Z5i4oLTusx4exjYWdDfdrWmovLhB6xf/Ep7j3Ri8B8XK7e3Geyzyqi8l 8Cqu3DuRZaBa4R1qQo9oSters5ga5k4qHor2hnXpT5Z6Fpu/k0a9vJP4bi02gRnqQA0E IfYVHTjeznz+Nt/ekr7dNkqtbhn22hVTQWIIKSbL75n3YxsB3b5MvMHfrih478X55qvJ Vg0ywcuHHT4t9214fB8r2sbekPiMbtk+iZoGPU2hzKc6asI8s9LQsPhM78YCXYKPpdik oxGg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j11si5118276edq.584.2020.07.31.07.25.35; Fri, 31 Jul 2020 07:25:59 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729258AbgGaOWN (ORCPT + 99 others); Fri, 31 Jul 2020 10:22:13 -0400 Received: from netrider.rowland.org ([192.131.102.5]:56441 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1729195AbgGaOWN (ORCPT ); Fri, 31 Jul 2020 10:22:13 -0400 Received: (qmail 37750 invoked by uid 1000); 31 Jul 2020 10:22:12 -0400 Date: Fri, 31 Jul 2020 10:22:12 -0400 From: Alan Stern To: Macpaul Lin Cc: Chunfeng Yun , Eddie Hung , Greg Kroah-Hartman , Matthias Brugger , linux-usb@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, Mediatek WSD Upstream , Macpaul Lin , stable@vger.kernel.org Subject: Re: [PATCH v2] usb: mtu3: fix panic in mtu3_gadget_disconnect() Message-ID: <20200731142212.GE36650@rowland.harvard.edu> References: <1596177366-12029-1-git-send-email-macpaul.lin@mediatek.com> <1596185878-24360-1-git-send-email-macpaul.lin@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1596185878-24360-1-git-send-email-macpaul.lin@mediatek.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 31, 2020 at 04:57:58PM +0800, Macpaul Lin wrote: > This patch fixes a possible issue when mtu3_gadget_stop() > already assigned NULL to mtu->gadget_driver during mtu_gadget_disconnect(). > > Signed-off-by: Macpaul Lin > Cc: stable@vger.kernel.org > --- > Changes for v2: > - Check mtu_gadget_driver out of spin_lock might still not work. > We use a temporary pointer to keep the callback function. > > drivers/usb/mtu3/mtu3_gadget.c | 9 ++++++++- > 1 file changed, 8 insertions(+), 1 deletion(-) > > diff --git a/drivers/usb/mtu3/mtu3_gadget.c b/drivers/usb/mtu3/mtu3_gadget.c > index 68ea4395f871..40cb6626f496 100644 > --- a/drivers/usb/mtu3/mtu3_gadget.c > +++ b/drivers/usb/mtu3/mtu3_gadget.c > @@ -840,10 +840,17 @@ void mtu3_gadget_suspend(struct mtu3 *mtu) > /* called when VBUS drops below session threshold, and in other cases */ > void mtu3_gadget_disconnect(struct mtu3 *mtu) > { > + struct usb_gadget_driver *driver; > + > dev_dbg(mtu->dev, "gadget DISCONNECT\n"); > if (mtu->gadget_driver && mtu->gadget_driver->disconnect) { > + driver = mtu->gadget_driver; > spin_unlock(&mtu->lock); > - mtu->gadget_driver->disconnect(&mtu->g); > + /* > + * avoid kernel panic because mtu3_gadget_stop() assigned NULL > + * to mtu->gadget_driver. > + */ > + driver->disconnect(&mtu->g); > spin_lock(&mtu->lock); > } This is not the right approach; it might race with the gadget driver unregistering itself. Instead, mtu3_gadget_stop() should call synchronize_irq() after releasing the IRQ line. When synchronize_irq() returns, you'll know any IRQ handlers have finished running, so you won't receive any more disconnect notifications. Then it will be safe to acquire the spinlock and set mtu->gadget_driver to NULL. Alan Stern