Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp439810pxb; Fri, 16 Apr 2021 09:12:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzM1O9sOKbkx9+DojP5SCh37+0NUy3SsExrEbk1ohMz5w786mAR3SVa5a49fANEiysdD+Kh X-Received: by 2002:a17:907:770d:: with SMTP id kw13mr8858956ejc.339.1618589541277; Fri, 16 Apr 2021 09:12:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618589541; cv=none; d=google.com; s=arc-20160816; b=mlwT+GHinGqBA95u/adq3RH7nGKCst/PcGdbFwFkSo7qhQDmcAdgAP4EidLLtu+9eM O0UYIoeoVaesUdfC6ugWwMfg7hu7QFr9lmfZoF9cLND4Tg9W9bLMdsCL9hgsYdkg5WEb HZ2W0vO1FO7vhpmUFtVH4T4TipkUXoc5N9QxeotgZ2Ilr1vOOf1Ly66pRyEi6e9Q2cgL lRWHMVytenGfpSsftBin6HuaJpfsQCm7YN+EuI3/sIWV0TUbUhG4/Y6c4cCRZLV8KP6I AkzQRciXo+lJgphwovNzM2wxFiyhQGJ3VNGME9YMZZwB6dRujbHBdSWln/rO4KbiNzPL 4lsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=BaMVtTgvjibUP1tlWd3mFNvy6j+xQdjDfnN/HyzU+FY=; b=h6KlgOrKVkxaFhqGyB/wPWKVaE8E/z1jP+Dueq/1ZzEB07eW0rpVphXARvaJhNx/pM cvTJA08oFaSENoaAjvI0TTlByOXtN4b2YhYMzkIOLPn7pm5jUj9waE3t5/Onw0sV4Wy2 IqeasbqtPHqIVfghTmBQn860G8yqnsSi2bDHTMfDX3HjtY1zgOBXyDxidedfa41UjsLi BFgQeBuB1v8DwvfK78SlemwzF+mRkHR1LtK0g113sgKheb4LcXfB4+njymVJdmmEdEAy Ic/2jSk1X7CQnNspDcDhDu2mccYKcKb0lSA4xHHdKMqw+kXgcEcbIISGe1R84wo2hLxj on2g== 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 nd7si3251068ejc.747.2021.04.16.09.11.57; Fri, 16 Apr 2021 09:12:21 -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 S245530AbhDPP2B convert rfc822-to-8bit (ORCPT + 99 others); Fri, 16 Apr 2021 11:28:01 -0400 Received: from netrider.rowland.org ([192.131.102.5]:48407 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S236062AbhDPP2A (ORCPT ); Fri, 16 Apr 2021 11:28:00 -0400 Received: (qmail 43914 invoked by uid 1000); 16 Apr 2021 11:27:34 -0400 Date: Fri, 16 Apr 2021 11:27:34 -0400 From: Alan Stern To: Anirudh Rayabharam Cc: Dmitry Vyukov , syzbot , Andrey Konovalov , Felipe Balbi , Dan Carpenter , Greg Kroah-Hartman , LKML , USB list , syzkaller-bugs Subject: Re: [syzbot] general protection fault in gadget_setup Message-ID: <20210416152734.GB42403@rowland.harvard.edu> References: <00000000000075c58405bfd6228c@google.com> <20210413161311.GC1454681@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: 8BIT In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 16, 2021 at 11:10:35AM +0530, Anirudh Rayabharam wrote: > On Tue, Apr 13, 2021 at 12:13:11PM -0400, Alan Stern wrote: > > Maybe we can test this reasoning by putting a delay just before the call > > to dum->driver->setup. That runs in the timer handler, so it's not a > > good place to delay, but it may be okay just for testing purposes. > > > > Hopefully this patch will make the race a lot more likely to occur. Is > > Hi Alan, > > Indeed, I was able to reproduce this bug easily on my machine with your > delay patch applied and using this syzkaller program: > > syz_usb_connect$cdc_ncm(0x1, 0x6e, &(0x7f0000000040)={{0x12, 0x1, 0x0, 0x2, 0x0, 0x0, 0x8, 0x525, 0xa4a1, 0x40, 0x1, 0x2, 0x3, 0x1, [{{0x9, 0x2, 0x5c, 0x2, 0x1, 0x0, 0x0, 0x0, {{0x9, 0x4, 0x0, 0x0, 0x1, 0x2, 0xd, 0x0, 0x0, {{0x5}, {0x5}, {0xd}, {0x6}}, {{0x9, 0x5, 0x81, 0x3, 0x200}}}}}}]}}, &(0x7f0000000480)={0x0, 0x0, 0x0, 0x0, 0x3, [{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}]}) > > I also tested doing the synchronize_irq emulation in dummy_pullup and it > fixed the issue. The patch is below. That's great! Thanks for testing. > Thanks! > > - Anirudh. > > diff --git a/drivers/usb/gadget/udc/dummy_hcd.c b/drivers/usb/gadget/udc/dummy_hcd.c > index ce24d4f28f2a..931d4612d859 100644 > --- a/drivers/usb/gadget/udc/dummy_hcd.c > +++ b/drivers/usb/gadget/udc/dummy_hcd.c > @@ -903,6 +903,12 @@ static int dummy_pullup(struct usb_gadget *_gadget, int value) > spin_lock_irqsave(&dum->lock, flags); > dum->pullup = (value != 0); > set_link_state(dum_hcd); > + /* emulate synchronize_irq(): wait for callbacks to finish */ > + while (dum->callback_usage > 0) { > + spin_unlock_irqrestore(&dum->lock, flags); > + usleep_range(1000, 2000); > + spin_lock_irqsave(&dum->lock, flags); > + } We should do this only if value == 0. No synchronization is needed when the pullup is turned on. Also, there should be a comment explaining that this is necessary because there's no other place to emulate the call made to synchronize_irq() in core.c:usb_gadget_remove_driver(). > spin_unlock_irqrestore(&dum->lock, flags); > > usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd)); > @@ -1005,13 +1011,6 @@ static int dummy_udc_stop(struct usb_gadget *g) > dum->ints_enabled = 0; > stop_activity(dum); > > - /* emulate synchronize_irq(): wait for callbacks to finish */ > - while (dum->callback_usage > 0) { > - spin_unlock_irq(&dum->lock); > - usleep_range(1000, 2000); > - spin_lock_irq(&dum->lock); > - } > - > dum->driver = NULL; > spin_unlock_irq(&dum->lock); Actually, I wanted to move this emulation code into a new subroutine and then call that subroutine from _both_ places. Would you like to write and submit a patch that does this? Alan Stern