From owner-svn-src-all@freebsd.org Sun Nov 24 13:10:18 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 8B1CD1AEF8E; Sun, 24 Nov 2019 13:10:18 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 47LVrp2Hd4z41X6; Sun, 24 Nov 2019 13:10:17 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id xAODAAPa034648 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Sun, 24 Nov 2019 15:10:13 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua xAODAAPa034648 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id xAODAAwm034647; Sun, 24 Nov 2019 15:10:10 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Sun, 24 Nov 2019 15:10:10 +0200 From: Konstantin Belousov To: Warner Losh Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: Re: svn commit: r355037 - head/sys/dev/pci Message-ID: <20191124131010.GB2707@kib.kiev.ua> References: <201911232343.xANNhqkQ097797@repo.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <201911232343.xANNhqkQ097797@repo.freebsd.org> User-Agent: Mutt/1.12.2 (2019-09-21) X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on tom.home X-Rspamd-Queue-Id: 47LVrp2Hd4z41X6 X-Spamd-Bar: ----- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-5.99 / 15.00]; NEURAL_HAM_MEDIUM(-0.99)[-0.991,0]; REPLY(-4.00)[]; NEURAL_HAM_LONG(-0.99)[-0.994,0] X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 24 Nov 2019 13:10:18 -0000 On Sat, Nov 23, 2019 at 11:43:52PM +0000, Warner Losh wrote: > Author: imp > Date: Sat Nov 23 23:43:52 2019 > New Revision: 355037 > URL: https://svnweb.freebsd.org/changeset/base/355037 > > Log: > Push Giant down one layer > > The /dev/pci device doesn't need GIANT, per se. However, one routine > that it calls, pci_find_dbsf implicitly does. It walks a list that can > change when PCI scans a new bus. With hotplug, this means we could > have a race with that scanning. To prevent that, take out Giant around > scanning the list. > > However, given that we have places in the tree that drop giant, if > held when we call into them, the whole use of Giant to protect newbus > may be less effective that we desire, so add a comment about why we're > talking it out, and we'll address the issue when we lock newbus with > something other than Giant. > > Modified: > head/sys/dev/pci/pci.c > head/sys/dev/pci/pci_user.c > > Modified: head/sys/dev/pci/pci.c > ============================================================================== > --- head/sys/dev/pci/pci.c Sat Nov 23 23:41:21 2019 (r355036) > +++ head/sys/dev/pci/pci.c Sat Nov 23 23:43:52 2019 (r355037) > @@ -445,18 +445,21 @@ pci_find_bsf(uint8_t bus, uint8_t slot, uint8_t func) > device_t > pci_find_dbsf(uint32_t domain, uint8_t bus, uint8_t slot, uint8_t func) > { > - struct pci_devinfo *dinfo; > + struct pci_devinfo *dinfo = NULL; > > + /* Giant because newbus is Giant locked revisit with newbus locking */ > + mtx_lock(&Giant); > STAILQ_FOREACH(dinfo, &pci_devq, pci_links) { > if ((dinfo->cfg.domain == domain) && > (dinfo->cfg.bus == bus) && > (dinfo->cfg.slot == slot) && > (dinfo->cfg.func == func)) { > - return (dinfo->cfg.dev); > + break; > } > } > + mtx_unlock(&Giant); > > - return (NULL); > + return (dinfo != NULL ? dinfo->cfg.dev : NULL); I do not think this change is correct. If the parallel hotplug, or rather, hot-unplug event occurs, then dinfo potentially becomes invalid right after the Giant unlock, which makes both this function and its callers to access freed memory. Having caller to lock a newbus lock around both the call and consumption of the returned data is required. > } > > /* Find a device_t by vendor/device ID */ > > Modified: head/sys/dev/pci/pci_user.c > ============================================================================== > --- head/sys/dev/pci/pci_user.c Sat Nov 23 23:41:21 2019 (r355036) > +++ head/sys/dev/pci/pci_user.c Sat Nov 23 23:43:52 2019 (r355037) > @@ -119,7 +119,7 @@ static d_ioctl_t pci_ioctl; > > struct cdevsw pcicdev = { > .d_version = D_VERSION, > - .d_flags = D_NEEDGIANT, > + .d_flags = 0, > .d_open = pci_open, > .d_close = pci_close, > .d_ioctl = pci_ioctl,