Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp813073pxj; Thu, 13 May 2021 18:23:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwaDGQqMmzR7Y0+P3VchXejhJtSu6iROTIPWDIz6141I4LDaJ0ExYM1Yk6e0EaOlLRKG1wa X-Received: by 2002:a05:6e02:1d18:: with SMTP id i24mr17946216ila.157.1620955391234; Thu, 13 May 2021 18:23:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620955391; cv=none; d=google.com; s=arc-20160816; b=CZQmZGgTnATfau/LRrPfaSWEoEgTiMZ0yKuKmJJ3cQvpwEPr1hVWdzZortfuILWEBo uQ0p0S5ZkZGxVxmlGHDojn20h/6SYX0UdOpdX/+Z5SoweFtID27a+LZExmI3C55QLlNl nfgSQC8X2NIbYzrRjROu6uX40boMOrbqH56hJNCpsj6Y7Yd4K12J4GrjM3FJHyzYNAc+ RaqG5mcEfsg0ohP4mcZiQ1kUejDJ2JhnQAH4vYtq5qqAzaQ+AkfI6NtKIF61o3XoTR0U irXsTXVSeXf5DWO2pyXT5HIxy2TkdFSGD0SdnGytboLJFTPw3EqrFwt1oL+oQbiIC6zT oARQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:cc:to:from:dkim-signature; bh=M9URDKLIjWjYTVDR2jo0iTH/7oGIDWR/L4jsn5dDFEQ=; b=BRTUWIxT/kDkBx9ylqYGnKKvJHWRMdPdah9EXPsFJVlLnV4qN4ImiGpMT97c87k8F6 +D1vw0w6B2Cl1ozS4VpfK0httUEpCvQYPbTgiLWWVPYDJrW0gRlRVQPlB+8Mxai918LA UlWhDAsC4vhQHNWnhTQ0BplxbcdKiCrT1mU2LaaLWj5ZN8GPnpNAI/2hEDBExJ+paWpl HVpxkJyf/HRKCWIRXokxbtAm/bpXXUxmpk5IqRClUgPtozJ7y84Rc4S5v7pVbi2RcKiY GopgFYKPBrHM3OzUmFDTZWk1MS0MKu/UYkQDnG8cjukV7NmMjZJVQrE0lET85rkY72+A iBOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@deltatee.com header.s=20200525 header.b=n061DgFC; 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 j23si5373279ioo.31.2021.05.13.18.22.57; Thu, 13 May 2021 18:23:11 -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=@deltatee.com header.s=20200525 header.b=n061DgFC; 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 S233704AbhEMWdh (ORCPT + 99 others); Thu, 13 May 2021 18:33:37 -0400 Received: from ale.deltatee.com ([204.191.154.188]:58988 "EHLO ale.deltatee.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233556AbhEMWd0 (ORCPT ); Thu, 13 May 2021 18:33:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=deltatee.com; s=20200525; h=Subject:MIME-Version:References:In-Reply-To: Message-Id:Date:Cc:To:From:content-disposition; bh=M9URDKLIjWjYTVDR2jo0iTH/7oGIDWR/L4jsn5dDFEQ=; b=n061DgFCU9NzN4lKQkcvE0Vgff KqhQDsQf0FiHKJh0lpMDW4cqOXRvLM/1rmcyvnVpn/5jOAJSiT/yAzjgyESNl4ZXz+x7ORPlwQVjh SSm0Re7Hp1abk1zwbYETKG6X5/K7JsNKOCnjUK3UmCvlBxfkfTH+qwQbextk6lh11y0h6q58XSIQw 6gOm0v5wVAy8/07ejEh5EqIUurPNcs1vtn25yGx05VbRETmh+h2ex+rOAzXUDibvFm2ryrZdrgWkq YeZI+qDF6xJDuqpnUx1/jIX8Tvr5C7DvFVIDvpZsoLK6/v3ulzBnPocYSJspfOD6XijvgJtl+IlbT GAESFp5w==; Received: from cgy1-donard.priv.deltatee.com ([172.16.1.31]) by ale.deltatee.com with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lhJsL-0000nF-QQ; Thu, 13 May 2021 16:32:15 -0600 Received: from gunthorp by cgy1-donard.priv.deltatee.com with local (Exim 4.92) (envelope-from ) id 1lhJsG-0001Se-6p; Thu, 13 May 2021 16:32:08 -0600 From: Logan Gunthorpe To: linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org, linux-block@vger.kernel.org, linux-pci@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org Cc: Stephen Bates , Christoph Hellwig , Dan Williams , Jason Gunthorpe , =?UTF-8?q?Christian=20K=C3=B6nig?= , John Hubbard , Don Dutile , Matthew Wilcox , Daniel Vetter , Jakowski Andrzej , Minturn Dave B , Jason Ekstrand , Dave Hansen , Xiong Jianxin , Bjorn Helgaas , Ira Weiny , Robin Murphy , Logan Gunthorpe Date: Thu, 13 May 2021 16:31:45 -0600 Message-Id: <20210513223203.5542-5-logang@deltatee.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210513223203.5542-1-logang@deltatee.com> References: <20210513223203.5542-1-logang@deltatee.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 172.16.1.31 X-SA-Exim-Rcpt-To: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-pci@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org, sbates@raithlin.com, hch@lst.de, jgg@ziepe.ca, christian.koenig@amd.com, jhubbard@nvidia.com, ddutile@redhat.com, willy@infradead.org, daniel.vetter@ffwll.ch, jason@jlekstrand.net, dave.hansen@linux.intel.com, helgaas@kernel.org, dan.j.williams@intel.com, andrzej.jakowski@intel.com, dave.b.minturn@intel.com, jianxin.xiong@intel.com, ira.weiny@intel.com, robin.murphy@arm.com, logang@deltatee.com X-SA-Exim-Mail-From: gunthorp@deltatee.com X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on ale.deltatee.com X-Spam-Level: X-Spam-Status: No, score=-6.5 required=5.0 tests=ALL_TRUSTED,BAYES_00, MYRULES_FREE,MYRULES_NO_TEXT autolearn=no autolearn_force=no version=3.4.2 Subject: [PATCH v2 04/22] PCI/P2PDMA: Avoid pci_get_slot() which sleeps X-SA-Exim-Version: 4.2.1 (built Wed, 08 May 2019 21:11:16 +0000) X-SA-Exim-Scanned: Yes (on ale.deltatee.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Logan Gunthorpe --- 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 09c864f193d2..175da3a9c8fb 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.20.1