Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1931152pxb; Wed, 2 Feb 2022 16:16:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJy5rBhhpcepp+m96hS/reD35pjB2pxtA61X9pf8b6+27lOK/MKBC5B2tscjd6GqjKpO8PXX X-Received: by 2002:a63:9346:: with SMTP id w6mr26632064pgm.65.1643847403364; Wed, 02 Feb 2022 16:16:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643847403; cv=none; d=google.com; s=arc-20160816; b=aoF+JGS6WzWgMo5wYbKwt0CYpO4f4SJjcrkjOcXexnmQ9PMJhPlXJzLcAwiqrR8OwQ 1sNY6So+dcRoobdJkMl8+DYiMxFI/clPbhIjx8VBT837P27gCtwGoXNVXoCnvGpJ7W8Z mZmcIqUketKPY00j3+NsCeippU69oZ8GytqEpg34sPqj00RsKTSm9P0Hs9CywzrqpA1p f8VnXBV+7tj3m9lP+d5c42snh5zAc+FuiKTighhDjV2X4vI4SHM9Sg7hRgEGiZVH+6/d Su1rULR0NT1GjDVoqoyipSRh2wB3o6f4h6BWd8zKf7SDu03uam1tNBSij558lFsNcxWB XydQ== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:dkim-signature; bh=cJSKxOJIzFWJNB/4kEdPj9SAYSFCsRpghMAEka+ai04=; b=M8R/8LjUnhRfbGX2aSyCGf2mmT5yBj6GQzE5G3Nu8yx5uctB6Qy3jE4CJa/37QxfJd 1MnhSQ08ETCZFLsgubedYDNkkFIbMQIu+UHuHPEVlhQxIm01wGklNmyyQO4FwbSqF3tA 4rki1IZBRgP2C+1X5H7knCGTnH3AF7OCijBOJg7mN8L2bMr1XMl42rfro1t4q5gr+x/w uEer2xKMRlJwDktcZ6IJjQ5o2MfmCrUkmxjTWquuKrK/yR9cu5xdO86lnUqkCy/cSX2a USGGM756yod1lk0I6GgBw7u3MRXTvzsRmbOg0GC5lDDs81GwmVQB47gU5SXfthpRYgcu 2ppw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@deltatee.com header.s=20200525 header.b=inRrrLF0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=deltatee.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d131si20254387pgc.595.2022.02.02.16.16.31; Wed, 02 Feb 2022 16:16:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@deltatee.com header.s=20200525 header.b=inRrrLF0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=deltatee.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242650AbiBAU5k (ORCPT + 99 others); Tue, 1 Feb 2022 15:57:40 -0500 Received: from ale.deltatee.com ([204.191.154.188]:57830 "EHLO ale.deltatee.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230415AbiBAU5i (ORCPT ); Tue, 1 Feb 2022 15:57:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=deltatee.com; s=20200525; h=Subject:In-Reply-To:MIME-Version:Date: Message-ID:From:References:Cc:To:content-disposition; bh=cJSKxOJIzFWJNB/4kEdPj9SAYSFCsRpghMAEka+ai04=; b=inRrrLF05DauLsozmFhswOyT+U WPSDQkzNTcNkRZ8czrnUNGLopNg88HLX9pdCK4UlXF1bzYf1BKgtsKpvGypPta7CHlcUOku9K9vge +NAO35wcF3CTICM/mBTLmVIVzczzqTvX+VVkIJTevRDS2LfYWgy1a6+2EUp3AAiQR5ChqflV0MWso oxckJk6HwJ98oVn/PvRLw3x09cOhCMFDyStF8NnTcjdXZ77YjfG+lzhwmhLo1H71ALY2CJFwigpj+ UdeGbDqFGDIIDyWwb0VsLflcq8qGQM/ErCZ1YgjT6Xi6D86QbJbPGca8MIAn8BpFKQVINH97Ks3w6 NHJute4A==; Received: from guinness.priv.deltatee.com ([172.16.1.162]) by ale.deltatee.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1nF0D7-009dre-2I; Tue, 01 Feb 2022 13:57:10 -0700 To: Jonathan Derrick , 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?= , 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 , Martin Oliveira , Chaitanya Kulkarni , Ralph Campbell References: <20220128002614.6136-1-logang@deltatee.com> <20220128002614.6136-9-logang@deltatee.com> <6c939012-8d68-fbb5-50c6-3fe757a31b48@linux.dev> From: Logan Gunthorpe Message-ID: Date: Tue, 1 Feb 2022 13:57:01 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: <6c939012-8d68-fbb5-50c6-3fe757a31b48@linux.dev> Content-Type: text/plain; charset=utf-8 Content-Language: en-CA Content-Transfer-Encoding: 7bit X-SA-Exim-Connect-IP: 172.16.1.162 X-SA-Exim-Rcpt-To: rcampbell@nvidia.com, ckulkarnilinux@gmail.com, martin.oliveira@eideticom.com, robin.murphy@arm.com, ira.weiny@intel.com, helgaas@kernel.org, jianxin.xiong@intel.com, dave.hansen@linux.intel.com, jason@jlekstrand.net, dave.b.minturn@intel.com, andrzej.jakowski@intel.com, daniel.vetter@ffwll.ch, willy@infradead.org, ddutile@redhat.com, jhubbard@nvidia.com, christian.koenig@amd.com, jgg@ziepe.ca, dan.j.williams@intel.com, hch@lst.de, sbates@raithlin.com, iommu@lists.linux-foundation.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, linux-block@vger.kernel.org, linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, jonathan.derrick@linux.dev X-SA-Exim-Mail-From: logang@deltatee.com X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ale.deltatee.com X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=ALL_TRUSTED,BAYES_00, NICE_REPLY_A,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Subject: Re: [PATCH v5 08/24] dma-direct: support PCI P2PDMA pages in dma-direct map_sg X-SA-Exim-Version: 4.2.1 (built Sat, 13 Feb 2021 17:57:42 +0000) X-SA-Exim-Scanned: Yes (on ale.deltatee.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022-02-01 1:53 p.m., Jonathan Derrick wrote: > > > On 1/27/2022 5:25 PM, Logan Gunthorpe wrote: >> Add PCI P2PDMA support for dma_direct_map_sg() so that it can map >> PCI P2PDMA pages directly without a hack in the callers. This allows >> for heterogeneous SGLs that contain both P2PDMA and regular pages. >> >> A P2PDMA page may have three possible outcomes when being mapped: >> 1) If the data path between the two devices doesn't go through the >> root port, then it should be mapped with a PCI bus address >> 2) If the data path goes through the host bridge, it should be mapped >> normally, as though it were a CPU physical address >> 3) It is not possible for the two devices to communicate and thus >> the mapping operation should fail (and it will return -EREMOTEIO). >> >> SGL segments that contain PCI bus addresses are marked with >> sg_dma_mark_pci_p2pdma() and are ignored when unmapped. >> >> P2PDMA mappings are also failed if swiotlb needs to be used on the >> mapping. >> >> Signed-off-by: Logan Gunthorpe >> --- >> kernel/dma/direct.c | 43 +++++++++++++++++++++++++++++++++++++------ >> kernel/dma/direct.h | 7 ++++++- >> 2 files changed, 43 insertions(+), 7 deletions(-) >> >> diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c >> index 50f48e9e4598..975df5f3aaf9 100644 >> --- a/kernel/dma/direct.c >> +++ b/kernel/dma/direct.c >> @@ -461,29 +461,60 @@ void dma_direct_sync_sg_for_cpu(struct device *dev, >> arch_sync_dma_for_cpu_all(); >> } >> >> +/* >> + * Unmaps segments, except for ones marked as pci_p2pdma which do not >> + * require any further action as they contain a bus address. >> + */ >> void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl, >> int nents, enum dma_data_direction dir, unsigned long attrs) >> { >> struct scatterlist *sg; >> int i; >> >> - for_each_sg(sgl, sg, nents, i) >> - dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir, >> - attrs); >> + for_each_sg(sgl, sg, nents, i) { >> + if (sg_is_dma_bus_address(sg)) >> + sg_dma_unmark_bus_address(sg); >> + else >> + dma_direct_unmap_page(dev, sg->dma_address, >> + sg_dma_len(sg), dir, attrs); >> + } >> } >> #endif >> >> int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, >> enum dma_data_direction dir, unsigned long attrs) >> { >> - int i; >> + struct pci_p2pdma_map_state p2pdma_state = {}; >> + enum pci_p2pdma_map_type map; >> struct scatterlist *sg; >> + int i, ret; >> >> for_each_sg(sgl, sg, nents, i) { >> + if (is_pci_p2pdma_page(sg_page(sg))) { >> + map = pci_p2pdma_map_segment(&p2pdma_state, dev, sg); >> + switch (map) { >> + case PCI_P2PDMA_MAP_BUS_ADDR: >> + continue; >> + case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE: >> + /* >> + * Any P2P mapping that traverses the PCI >> + * host bridge must be mapped with CPU physical >> + * address and not PCI bus addresses. This is >> + * done with dma_direct_map_page() below. >> + */ >> + break; >> + default: >> + ret = -EREMOTEIO; >> + goto out_unmap; >> + } >> + } > I'm a little confused about this code. Would there be a case where the mapping needs > to be checked for each sg in the list? And if some sg in the sgl can be mapped > differently, would we want to continue checking the rest of the sg in the sgl instead > of breaking out of the loop completely? Yes, the code supports heterogeneous SGLs with P2PDMA and regular memory; it's also theoretically possible to mix P2PDMA memory for different devices. So yes, the mapping must be checked for every SG in the list. It can't just see one SG that points to P2PDMA memory and assume the rest are all good. Logan