Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3889094pxv; Mon, 19 Jul 2021 11:11:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwi76E+ur4sZonwwyagPgbUzkm8bIMIUEeO5i9qsVy5ra5RdtRW09og2iYYr9SBA17QpYPh X-Received: by 2002:a17:906:81da:: with SMTP id e26mr29334687ejx.352.1626718305896; Mon, 19 Jul 2021 11:11:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626718305; cv=none; d=google.com; s=arc-20160816; b=WYua7nPMAkDUh+2cVEiFmbVSchUER5667PqvjLAFSfyp1InTVubNbm/XoK/ef2vEHr FxsyNYcPvx3RO5x2YB9A0eh8eJNRSBjBQFmmpwkm+XIgtwDtxbfHQxdwexvDkpBQeN5w DCxCiAXIR/7yHcWGCf0mSvHigHDZ6xkdP9cA+rC8dCxivwwt4AAOBrxerwdc7x5wmarS H/ikPC5DUSqHPno6sCa+hZxgT3PdGbaPCE427466ENmGjiWt84UNsRqfVeppBVif3OH7 jpZMKwflAZN39I2g4Px6GJP/yzVQz+DrOKEwkA9lfBa2UoH1dybVbfPkfyNPH6JeV5+j W2NQ== 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=kshlHC1M3oMkIQuNtzv66J/u4M9Zv2ls1DgcwPI0RSs=; b=s5y0++KbP6KDsGQ19Yqedeg1Ho50+FfI4ANpLTybcw0/hO32LqBI+/m0rFaK67ngCJ UqNCWZsqcByFvy5hVkVWErnVoTrwMgCUeTY2VFi/VFj+tkdllXRh7SC06UCwMXi4BFjm mIzN158bEbmZdVdCpYi/ZFj8LqRZY4V+dbnuYSrC83ptJ34hCH7uR5/9kZCZGiAWR+7O DkRB3bK0D9sIRpJeJKrQvU9rKVk8w0RKeF7Djt/FQzadyJi2mAtvPewkBplVd9sYhO7H 4AZ64WKv/3XlmgwANS31k7W83zfL2iDcXvBP8P2oxjfeGMQ1wDSlgLaLtR0zJM0xEATr c1zQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="oKv/bH1K"; 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 g20si26124587ejm.455.2021.07.19.11.11.22; Mon, 19 Jul 2021 11:11:45 -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="oKv/bH1K"; 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 S1355403AbhGSRXg (ORCPT + 99 others); Mon, 19 Jul 2021 13:23:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:45328 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348946AbhGSPoj (ORCPT ); Mon, 19 Jul 2021 11:44:39 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D227E6157F; Mon, 19 Jul 2021 16:23:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626711812; bh=yZ19D2EA1OUhyCLOW/hS71/J5T/SbBrNsg3xivWuV4A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oKv/bH1KGvbn4Ok4loYvIsq0vBEzOlA1rq5qd7AG1h4fnq+foEeTFeuaLEnBjG3hd K+WaqVOrAN1fDMt5ntPON95rGik4hJVHN4VcyhUN+dNhznC2OebJzzP8A0B0xqmFH5 pKhAFaaDx5G8Jc6tx9YurmKLaeQsowPxqhyC9yQg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Logan Gunthorpe , Bjorn Helgaas , Sasha Levin Subject: [PATCH 5.12 141/292] PCI/P2PDMA: Avoid pci_get_slot(), which may sleep Date: Mon, 19 Jul 2021 16:53:23 +0200 Message-Id: <20210719144947.119718289@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210719144942.514164272@linuxfoundation.org> References: <20210719144942.514164272@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: Logan Gunthorpe [ Upstream commit 3ec0c3ec2d92c09465534a1ff9c6f9d9506ffef6 ] In order to use upstream_bridge_distance_warn() from a dma_map function, it must not sleep. However, pci_get_slot() takes the pci_bus_sem so it might sleep. In order to avoid this, try to get the host bridge's device from the first element in the device list. It should be impossible for the host bridge's device to go away while references are held on child devices, so the first element should not be able to change and, thus, this should be safe. Introduce a static function called pci_host_bridge_dev() to obtain the host bridge's root device. Link: https://lore.kernel.org/r/20210610160609.28447-7-logang@deltatee.com Signed-off-by: Logan Gunthorpe Signed-off-by: Bjorn Helgaas Signed-off-by: Sasha Levin --- drivers/pci/p2pdma.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c index 196382630363..c49c13a5fedc 100644 --- a/drivers/pci/p2pdma.c +++ b/drivers/pci/p2pdma.c @@ -308,10 +308,41 @@ static const struct pci_p2pdma_whitelist_entry { {} }; +/* + * This lookup function tries to find the PCI device corresponding to a given + * host bridge. + * + * It assumes the host bridge device is the first PCI device in the + * bus->devices list and that the devfn is 00.0. These assumptions should hold + * for all the devices in the whitelist above. + * + * This function is equivalent to pci_get_slot(host->bus, 0), however it does + * not take the pci_bus_sem lock seeing __host_bridge_whitelist() must not + * sleep. + * + * For this to be safe, the caller should hold a reference to a device on the + * bridge, which should ensure the host_bridge device will not be freed + * or removed from the head of the devices list. + */ +static struct pci_dev *pci_host_bridge_dev(struct pci_host_bridge *host) +{ + struct pci_dev *root; + + root = list_first_entry_or_null(&host->bus->devices, + struct pci_dev, bus_list); + + if (!root) + return NULL; + if (root->devfn != PCI_DEVFN(0, 0)) + return NULL; + + return root; +} + static bool __host_bridge_whitelist(struct pci_host_bridge *host, bool same_host_bridge) { - struct pci_dev *root = pci_get_slot(host->bus, PCI_DEVFN(0, 0)); + struct pci_dev *root = pci_host_bridge_dev(host); const struct pci_p2pdma_whitelist_entry *entry; unsigned short vendor, device; @@ -320,7 +351,6 @@ static bool __host_bridge_whitelist(struct pci_host_bridge *host, vendor = root->vendor; device = root->device; - pci_dev_put(root); for (entry = pci_p2pdma_whitelist; entry->vendor; entry++) { if (vendor != entry->vendor || device != entry->device) -- 2.30.2