From patchwork Thu Sep 12 11:15:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13801930 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19817EEB593 for ; Thu, 12 Sep 2024 11:16:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9E4E46B009A; Thu, 12 Sep 2024 07:16:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 994EC6B009B; Thu, 12 Sep 2024 07:16:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8358B6B009C; Thu, 12 Sep 2024 07:16:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 6678E6B009A for ; Thu, 12 Sep 2024 07:16:36 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 25492121EFA for ; Thu, 12 Sep 2024 11:16:36 +0000 (UTC) X-FDA: 82555833192.12.0A45681 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf26.hostedemail.com (Postfix) with ESMTP id 8AC82140005 for ; Thu, 12 Sep 2024 11:16:34 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JaEIJPQl; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726139717; a=rsa-sha256; cv=none; b=ga5o6NhQu1i5bNrZLGpwWAsDEZQqX1gkOt0kX57yBJ1GrcoMeDFm1kw8BPAwExdqN/UCq3 CZv7o06p703vP2gt2Fv+Mmg3dTHmOT4D9O6iC7PF93uPI6CswBWyyPDrtrH6GcbK5yxnmq 5PREHM8uREVva+EBi8LsUYYrPvaM6+k= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JaEIJPQl; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726139717; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=a8A9ch32OnCK6zlVv+ChhoMntSAAJjv1Y1wVJxAAmWs=; b=5BKrToZ8tnt3KP3VisfEWS63R+lXwzJzYc1Z3CA/nEH1RxM2kLN0hktQRCNM4ejGNK7094 ZgswjkVRigauUZ2/ECD4yb+5E6nn33BtYdhGcu0IA+d+f+IiAcahYs5IqEa6EXRmh/Fm8C g//T2zeP3bYyyZ3EmyvkAHZdq/tGv6I= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 33B8AA45274; Thu, 12 Sep 2024 11:16:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8814DC4CECC; Thu, 12 Sep 2024 11:16:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726139793; bh=UdWHtN/ksXEH4oEZ6oLMvfaCKk3WWuTK2xFlFpaVROQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JaEIJPQlXW2dDMP6p7hqTalr5/nZfzPDNjt31Ao+a/Z+6Qd6aR11344w2a+DiulS6 iMiK8gTB/L8nSXItJGUYe9cwRmL2GgnozYUV8QmwIPwxlHyW9aemlOw4kUFJ274pAA PN0d31Risc4/iSKKWebUhSD66kfvH9LIyEwYMS4hOAKbYQxJRBmddM3wevSD+t9Lab 3P9ksYF1WRRS9QQhDgWjrPfCaOyxagFMoWzGFxcyzomruL7xcQnhG0yOGfbzjF3fR/ bCmDiQ+O2/NGR32Y9Ac9Uqzw207ZTfmgI/uaIsUSGZlxOlUgCp8lIc5BJSywScgOIC JJAYki7oZADlw== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Keith Busch , Christoph Hellwig , "Zeng, Oak" , Chaitanya Kulkarni Cc: Leon Romanovsky , Sagi Grimberg , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Subject: [RFC v2 03/21] iommu/dma: Add check if IOVA can be used Date: Thu, 12 Sep 2024 14:15:38 +0300 Message-ID: X-Mailer: git-send-email 2.46.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 8AC82140005 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 4owrn9ga6oe3ashwp4a9i3n94hk9aft9 X-HE-Tag: 1726139794-316695 X-HE-Meta: U2FsdGVkX1+3IzWK8xEpBQPaluN++TZgeVPpiu3IodTPSq79ofr42BJAgCxSg725GFBpLXdlNgVFhe5wtzt8FXXbjVqeG9+26N6+pvFskACHiB5BPPTpUb/0uy//JCNd/3db17/S5vI3S3DPWbo93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g4RXs= 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: List-Subscribe: List-Unsubscribe: From: Leon Romanovsky This patch adds a check if IOVA can be used for the page and size. Signed-off-by: Leon Romanovsky --- drivers/iommu/dma-iommu.c | 21 +++++++++++++++++++++ drivers/pci/p2pdma.c | 4 ++-- include/linux/dma-map-ops.h | 7 +++++++ include/linux/iommu-dma.h | 7 +++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 72763f76b712..3e2e382bb502 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -1829,6 +1830,26 @@ void iommu_dma_unlink_range(struct device *dev, dma_addr_t start, size_t size, iommu_iotlb_sync(domain, &iotlb_gather); } +bool iommu_can_use_iova(struct device *dev, struct page *page, size_t size, + enum dma_data_direction dir) +{ + enum pci_p2pdma_map_type map; + + if (is_swiotlb_force_bounce(dev) || dev_use_swiotlb(dev, size, dir)) + return false; + + /* TODO: Rewrite this check to rely on specific struct page flags */ + if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) + return false; + + if (page && is_pci_p2pdma_page(page)) { + map = pci_p2pdma_map_type(page->pgmap, dev); + return map == PCI_P2PDMA_MAP_THRU_HOST_BRIDGE; + } + + return true; +} + void iommu_setup_dma_ops(struct device *dev) { struct iommu_domain *domain = iommu_get_domain_for_dev(dev); diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c index 4f47a13cb500..6ceea32bb041 100644 --- a/drivers/pci/p2pdma.c +++ b/drivers/pci/p2pdma.c @@ -964,8 +964,8 @@ void pci_p2pmem_publish(struct pci_dev *pdev, bool publish) } EXPORT_SYMBOL_GPL(pci_p2pmem_publish); -static enum pci_p2pdma_map_type pci_p2pdma_map_type(struct dev_pagemap *pgmap, - struct device *dev) +enum pci_p2pdma_map_type pci_p2pdma_map_type(struct dev_pagemap *pgmap, + struct device *dev) { enum pci_p2pdma_map_type type = PCI_P2PDMA_MAP_NOT_SUPPORTED; struct pci_dev *provider = to_p2p_pgmap(pgmap)->provider; diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 103d9c66c445..936e822e9f40 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -516,6 +516,8 @@ struct pci_p2pdma_map_state { enum pci_p2pdma_map_type pci_p2pdma_map_segment(struct pci_p2pdma_map_state *state, struct device *dev, struct scatterlist *sg); +enum pci_p2pdma_map_type pci_p2pdma_map_type(struct dev_pagemap *pgmap, + struct device *dev); #else /* CONFIG_PCI_P2PDMA */ static inline enum pci_p2pdma_map_type pci_p2pdma_map_segment(struct pci_p2pdma_map_state *state, struct device *dev, @@ -523,6 +525,11 @@ pci_p2pdma_map_segment(struct pci_p2pdma_map_state *state, struct device *dev, { return PCI_P2PDMA_MAP_NOT_SUPPORTED; } +static inline enum pci_p2pdma_map_type +pci_p2pdma_map_type(struct dev_pagemap *pgmap, struct device *dev) +{ + return PCI_P2PDMA_MAP_NOT_SUPPORTED; +} #endif /* CONFIG_PCI_P2PDMA */ #endif /* _LINUX_DMA_MAP_OPS_H */ diff --git a/include/linux/iommu-dma.h b/include/linux/iommu-dma.h index 21b0341f52b8..561d81b12d9c 100644 --- a/include/linux/iommu-dma.h +++ b/include/linux/iommu-dma.h @@ -66,6 +66,8 @@ dma_addr_t iommu_dma_link_range(struct dma_iova_state *state, phys_addr_t phys, size_t size, unsigned long attrs); void iommu_dma_unlink_range(struct device *dev, dma_addr_t start, size_t size, enum dma_data_direction dir, unsigned long attrs); +bool iommu_can_use_iova(struct device *dev, struct page *page, size_t size, + enum dma_data_direction dir); #else static inline bool use_dma_iommu(struct device *dev) { @@ -209,5 +211,10 @@ static inline void iommu_dma_unlink_range(struct device *dev, dma_addr_t start, unsigned long attrs) { } +static inline bool iommu_can_use_iova(struct device *dev, struct page *page, + size_t size, enum dma_data_direction dir) +{ + return false; +} #endif /* CONFIG_IOMMU_DMA */ #endif /* _LINUX_IOMMU_DMA_H */