Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp685323iog; Thu, 30 Jun 2022 08:18:22 -0700 (PDT) X-Google-Smtp-Source: AGRyM1taRzXtNr7dyXBoa3baHQZ094NsCFA79k8YZLHssjscrTxNYmX+PijNC/gzRU2TW1zGkYpL X-Received: by 2002:a17:902:aa8a:b0:16a:1ea5:d417 with SMTP id d10-20020a170902aa8a00b0016a1ea5d417mr16649085plr.4.1656602302157; Thu, 30 Jun 2022 08:18:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656602302; cv=none; d=google.com; s=arc-20160816; b=K2IykK69bVfjvvnsd7qvMOB6HH+PniCSpDG7SjNjjrOenIrZzHubo+tYO7B/l6o8yI k4EBewhkuODbFb6rqvuaDZE+/aV0H/yL7QSpSYNdZQuDmfTR/qsQJ5v7UatdWCMcvq6N xgZ+qF/3a1ef0X0usXi2bSC1bm4f+DszdiuGQm16Dvt0WoVFvtwKi4ELXvtKbjqsMDur zA55G3x08D9EufsD5Ii61JC3rVYwZ9U6bjWihdnW58tgY2UnRnvTO/r/MOPfSe/PJMzI 8ueCT0I0OK75gjFJhdyNt0gtdgi3CfIuAOHz+UQknhLAZ13Fi1k9yjgDyTKj0wfA02yX zi1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=QrP2DFq9XwKtGF+jJR9LeaqFNJpEALv6wE7oUwj8U4E=; b=Y6d/xHMKaEe743ja5i5r+yLyGpHM90Hynojv64v0FC8rpkVSKSisxzhwA2+LXhJGbP zMjMtD7LZ/+qXVSkvy7iB7KUiqPMhU9NTV62lB8cn259Hw8pDrcjKlNPRdmrbT8SexKe sH0pW5Gpes6WOfbuYgPISt7mAwCse8sFI9ZRAHZAWtGowxFVh4IljhiyoXoEW86T/+tX XfQ4r7A+6+UN7uxBpCVUdAbH2hlKOURy9IhG4zk2jDm6aFUeSF2g5753bxbn8mccHB6H 7X9D8w0jpmkT73GE0tKSqITKONugS12QU4oSorOqn5zFoiuTfzWThYVxWrbv3RxhGed4 aMVg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ha12-20020a17090af3cc00b001ecbb77d9f2si7002690pjb.135.2022.06.30.08.18.10; Thu, 30 Jun 2022 08:18:22 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235578AbiF3O5I (ORCPT + 99 others); Thu, 30 Jun 2022 10:57:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235407AbiF3O5H (ORCPT ); Thu, 30 Jun 2022 10:57:07 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2EB831DA7E; Thu, 30 Jun 2022 07:57:06 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E7FC61063; Thu, 30 Jun 2022 07:57:05 -0700 (PDT) Received: from [10.57.85.25] (unknown [10.57.85.25]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C8C5A3F66F; Thu, 30 Jun 2022 07:57:01 -0700 (PDT) Message-ID: Date: Thu, 30 Jun 2022 15:56:56 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH v7 08/21] iommu/dma: support PCI P2PDMA pages in dma-iommu map_sg Content-Language: en-GB To: 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: Minturn Dave B , Martin Oliveira , Ralph Campbell , Jason Gunthorpe , John Hubbard , Dave Hansen , Matthew Wilcox , =?UTF-8?Q?Christian_K=c3=b6nig?= , Jason Gunthorpe , Chaitanya Kulkarni , Jason Ekstrand , Daniel Vetter , Bjorn Helgaas , Dan Williams , Stephen Bates , Ira Weiny , Christoph Hellwig , Xiong Jianxin References: <20220615161233.17527-1-logang@deltatee.com> <20220615161233.17527-9-logang@deltatee.com> <7f0673e1-433b-65fb-1d2b-c3e4adeebf87@arm.com> <626de61d-e85e-bc9f-9e3d-836a408c859f@deltatee.com> From: Robin Murphy In-Reply-To: <626de61d-e85e-bc9f-9e3d-836a408c859f@deltatee.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022-06-29 23:41, Logan Gunthorpe wrote: > > > On 2022-06-29 13:15, Robin Murphy wrote: >> On 2022-06-29 16:57, Logan Gunthorpe wrote: >>> >>> >>> >>> On 2022-06-29 06:07, Robin Murphy wrote: >>>> On 2022-06-15 17:12, Logan Gunthorpe wrote: >>>>> When a PCI P2PDMA page is seen, set the IOVA length of the segment >>>>> to zero so that it is not mapped into the IOVA. Then, in finalise_sg(), >>>>> apply the appropriate bus address to the segment. The IOVA is not >>>>> created if the scatterlist only consists of P2PDMA 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 with an IOMMU IOVA. >>>>>     3) It is not possible for the two devices to communicate and thus >>>>>        the mapping operation should fail (and it will return >>>>> -EREMOTEIO). >>>>> >>>>> Similar to dma-direct, the sg_dma_mark_pci_p2pdma() flag is used to >>>>> indicate bus address segments. On unmap, P2PDMA segments are skipped >>>>> over when determining the start and end IOVA addresses. >>>>> >>>>> With this change, the flags variable in the dma_map_ops is set to >>>>> DMA_F_PCI_P2PDMA_SUPPORTED to indicate support for P2PDMA pages. >>>>> >>>>> Signed-off-by: Logan Gunthorpe >>>>> Reviewed-by: Jason Gunthorpe >>>>> --- >>>>>    drivers/iommu/dma-iommu.c | 68 >>>>> +++++++++++++++++++++++++++++++++++---- >>>>>    1 file changed, 61 insertions(+), 7 deletions(-) >>>>> >>>>> diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c >>>>> index f90251572a5d..b01ca0c6a7ab 100644 >>>>> --- a/drivers/iommu/dma-iommu.c >>>>> +++ b/drivers/iommu/dma-iommu.c >>>>> @@ -21,6 +21,7 @@ >>>>>    #include >>>>>    #include >>>>>    #include >>>>> +#include >>>>>    #include >>>>>    #include >>>>>    #include >>>>> @@ -1062,6 +1063,16 @@ static int __finalise_sg(struct device *dev, >>>>> struct scatterlist *sg, int nents, >>>>>            sg_dma_address(s) = DMA_MAPPING_ERROR; >>>>>            sg_dma_len(s) = 0; >>>>>    +        if (is_pci_p2pdma_page(sg_page(s)) && !s_iova_len) { >>>> >>>> Logically, should we not be able to use sg_is_dma_bus_address() here? I >>>> think it should be feasible, and simpler, to prepare the p2p segments >>>> up-front, such that at this point all we need to do is restore the >>>> original length (if even that, see below). >>> >>> Per my previous email, no, because sg_is_dma_bus_address() is not set >>> yet and not meant to tell you something about the page. That flag will >>> be set below by pci_p2pdma_map_bus_segment() and then checkd in >>> iommu_dma_unmap_sg() to determine if the dma_address in the segment >>> needs to be unmapped. >> >> I know it's not set yet as-is; I'm suggesting things should be >> restructured so that it *would be*. In the logical design of this code, >> the DMA addresses are effectively determined in iommu_dma_map_sg(), and >> __finalise_sg() merely converts them from a relative to an absolute form >> (along with undoing the other trickery). Thus the call to >> pci_p2pdma_map_bus_segment() absolutely belongs in the main >> iommu_map_sg() loop. > > I don't see how that can work: __finalise_sg() does more than convert > them from relative to absolute, it also figures out which SG entry will > contain which dma_address segment. Which segment a P2PDMA address needs > to be programmed into depends on the how 'cur' is calculated which in > turn depends on things like seg_mask and max_len. This calculation is > not done in iommu_dma_map_sg() so I don't see how there's any hope of > assigning the bus address for the P2P segments in that function. > > If there's a way to restructure things so that's possible that I'm not > seeing, I'm open to it but it's certainly not immediately obvious. Huh? It's still virtually the same thing; iommu_dma_map_sg() calls pci_p2pdma_map_bus_segment(s) and sets s->length to 0 if PCI_P2PDMA_MAP_BUS_ADDR, then __finalise_sg() can use sg_is_dma_bus_address(s) in place of is_pci_p2pdma_page(sg_page(s)), and just propagate the DMA address and original length from s to cur. Here you've written a patch which looks to correctly interrupt any ongoing concatenation state and convey some data from the given input segment to the appropriate output segment, so I'm baffled by why you'd think you couldn't do what you've already done. Thanks, Robin.