From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.1 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 387A6C43462 for ; Tue, 11 May 2021 16:05:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B4DB461921 for ; Tue, 11 May 2021 16:05:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B4DB461921 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 47FBB8D0002; Tue, 11 May 2021 12:05:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4564D8E0002; Tue, 11 May 2021 12:05:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D1448D0003; Tue, 11 May 2021 12:05:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0084.hostedemail.com [216.40.44.84]) by kanga.kvack.org (Postfix) with ESMTP id 0EF7A8D0002 for ; Tue, 11 May 2021 12:05:51 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id BBB6D181AF5D7 for ; Tue, 11 May 2021 16:05:50 +0000 (UTC) X-FDA: 78129426060.03.F39DF1C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf12.hostedemail.com (Postfix) with ESMTP id 382333EF for ; Tue, 11 May 2021 16:05:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620749149; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hrrdEBLqCOmDZJ+zOkXaPF7O/7nbLIRMtWhGOzsY1Zc=; b=VWa2s+HwWpIWWJZ/Mxn6LNjjw07GiLIJOYVupnNLLEZQ3UmkJvF6f8N3ZyqQib/44gGkUG 59OEIonBN6Ch9KGHK0T9kDhtEri7CuFUY4xxZqWiLQ1g8cJsPXiIgje77i273RiR4hQc4M IJn9MECMDwER/zy6vbOwi5mbJMjYmFI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-411-IY58VNbdOuCPlC-_betBlg-1; Tue, 11 May 2021 12:05:47 -0400 X-MC-Unique: IY58VNbdOuCPlC-_betBlg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4CE848186E1; Tue, 11 May 2021 16:05:44 +0000 (UTC) Received: from [10.3.115.19] (ovpn-115-19.phx2.redhat.com [10.3.115.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0AD745D9F2; Tue, 11 May 2021 16:05:40 +0000 (UTC) Subject: Re: [PATCH 02/16] PCI/P2PDMA: Avoid pci_get_slot() which sleeps To: John Hubbard , Logan Gunthorpe , 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_K=c3=b6nig?= , Matthew Wilcox , Daniel Vetter , Jakowski Andrzej , Minturn Dave B , Jason Ekstrand , Dave Hansen , Xiong Jianxin , Bjorn Helgaas , Ira Weiny , Robin Murphy References: <20210408170123.8788-1-logang@deltatee.com> <20210408170123.8788-3-logang@deltatee.com> From: Don Dutile Message-ID: Date: Tue, 11 May 2021 12:05:40 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VWa2s+Hw; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf12.hostedemail.com: domain of ddutile@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=ddutile@redhat.com X-Stat-Signature: i4gj14pe9m1zo5tcsgwfsmf8ukbaqkso X-Rspamd-Queue-Id: 382333EF X-Rspamd-Server: rspam02 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf12; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620749134-676456 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 5/2/21 1:35 AM, John Hubbard wrote: > On 4/8/21 10:01 AM, Logan Gunthorpe wrote: >> In order to use upstream_bridge_distance_warn() from a dma_map functio= n, >> 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 >> bus->self, and if that is not set, just get 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 eleme= nt >> should not be able to change and, thus, this should be safe. >> >> Signed-off-by: Logan Gunthorpe >> --- >> =C2=A0 drivers/pci/p2pdma.c | 14 ++++++++++++-- >> =C2=A0 1 file changed, 12 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c >> index bd89437faf06..473a08940fbc 100644 >> --- a/drivers/pci/p2pdma.c >> +++ b/drivers/pci/p2pdma.c >> @@ -311,16 +311,26 @@ static const struct pci_p2pdma_whitelist_entry { >> =C2=A0 static bool __host_bridge_whitelist(struct pci_host_bridge *hos= t, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 bool same_host_bri= dge) >> =C2=A0 { >> -=C2=A0=C2=A0=C2=A0 struct pci_dev *root =3D pci_get_slot(host->bus, P= CI_DEVFN(0, 0)); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 const struct pci_p2pdma_whitelist_entry= *entry; >> +=C2=A0=C2=A0=C2=A0 struct pci_dev *root =3D host->bus->self; >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 unsigned short vendor, device; >> =C2=A0 +=C2=A0=C2=A0=C2=A0 /* >> +=C2=A0=C2=A0=C2=A0=C2=A0 * This makes the assumption that the first d= evice on the bus is the >> +=C2=A0=C2=A0=C2=A0=C2=A0 * bridge itself and it has the devfn of 00.0= . This assumption should >> +=C2=A0=C2=A0=C2=A0=C2=A0 * hold for the devices in the white list abo= ve, and if there are cases >> +=C2=A0=C2=A0=C2=A0=C2=A0 * where this isn't true they will have to be= dealt with when such a >> +=C2=A0=C2=A0=C2=A0=C2=A0 * case is added to the whitelist. > > Actually, it makes the assumption that the first device *in the list* > (the host->bus-devices list) is 00.0.=C2=A0 The previous code made the > assumption that you wrote. > > By the way, pre-existing code comment: pci_p2pdma_whitelist[] seems > really short. From a naive point of view, I'd expect that there must be > a lot more CPUs/chipsets that can do pci p2p, what do you think? I > wonder if we have to be so super strict, anyway. It just seems extremel= y > limited, and I suspect there will be some additions to the list as soon > as we start to use this. > > >> +=C2=A0=C2=A0=C2=A0=C2=A0 */ >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (!root) >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 root =3D list_first_entry_= or_null(&host->bus->devices, >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 str= uct pci_dev, bus_list); > > OK, yes this avoids taking the pci_bus_sem, but it's kind of cheating. > Why is it OK to avoid taking any locks in order to retrieve the > first entry from the list, but in order to retrieve any other entry, yo= u > have to aquire the pci_bus_sem, and get a reference as well? Something > is inconsistent there. > > The new version here also no longer takes a reference on the device, > which is also cheating. But I'm guessing that the unstated assumption > here is that there is always at least one entry in the list. But if > that's true, then it's better to show clearly that assumption, instead > of hiding it in an implicit call that skips both locking and reference > counting. > > You could add a new function, which is a cut-down version of pci_get_sl= ot(), > like this, and call this from __host_bridge_whitelist(): > > /* > =C2=A0* A special purpose variant of pci_get_slot() that doesn't take t= he pci_bus_sem > =C2=A0* lock, and only looks for the 00.0 bus-device-function. Once the= PCI bus is > =C2=A0* up, it is safe to call this, because there will always be a top= -level PCI > =C2=A0* root device. > =C2=A0* > =C2=A0* Other assumptions: the root device is the first device in the l= ist, and the > =C2=A0* root device is numbered 00.0. > =C2=A0*/ > struct pci_dev *pci_get_root_slot(struct pci_bus *bus) > { > =C2=A0=C2=A0=C2=A0=C2=A0struct pci_dev *root; > =C2=A0=C2=A0=C2=A0=C2=A0unsigned devfn =3D PCI_DEVFN(0, 0); > > =C2=A0=C2=A0=C2=A0=C2=A0root =3D list_first_entry_or_null(&bus->devices= , struct pci_dev, > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 bus_list); > =C2=A0=C2=A0=C2=A0=C2=A0if (root->devfn =3D=3D devfn) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 goto out; > ... add a flag (set for p2pdma use)=C2=A0 to the function to print out wh= at the root->devfn is, and what the device is so the needed quirk &/or modification can added to handle w= hen this assumption fails; or make it a prdebug that can be flipped on for this failing situation, a= gain, to add needed change to accomodate. > =C2=A0=C2=A0=C2=A0=C2=A0root =3D NULL; > =C2=A0out: > =C2=A0=C2=A0=C2=A0=C2=A0pci_dev_get(root); > =C2=A0=C2=A0=C2=A0=C2=A0return root; > } > EXPORT_SYMBOL(pci_get_root_slot); > > ...I think that's a lot clearer to the reader, about what's going on he= re. > > Note that I'm not really sure if it *is* safe, I would need to ask othe= r > PCIe subsystem developers with more experience. But I don't think anyon= e > is trying to make p2pdma calls so early that PCIe buses are uninitializ= ed. > > >> + >> +=C2=A0=C2=A0=C2=A0 if (!root || root->devfn) >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return false; >> =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 vendor =3D root->vendor; >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 device =3D root->device; >> -=C2=A0=C2=A0=C2=A0 pci_dev_put(root); and the reason to remove the dev_put is b/c it can sleep as well? is that ok, given the dev_get that John put into the new pci_get_root_slo= t()? ... seems like a locking version with no get/put's is needed, or, fix the= host-bridge setups so no !NULL self pointers. >> =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 for (entry =3D pci_p2pdma_whitel= ist; entry->vendor; entry++) { >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (vendor !=3D= entry->vendor || device !=3D entry->device) >> > > thanks,