From patchwork Tue Jul 2 09:09:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13719167 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 B2D6BC41513 for ; Tue, 2 Jul 2024 09:10:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 414766B007B; Tue, 2 Jul 2024 05:10:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 375346B009A; Tue, 2 Jul 2024 05:10:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 23EC26B009B; Tue, 2 Jul 2024 05:10:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 058126B007B for ; Tue, 2 Jul 2024 05:10:14 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6F534141AC8 for ; Tue, 2 Jul 2024 09:10:14 +0000 (UTC) X-FDA: 82294241148.27.F1D6701 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf11.hostedemail.com (Postfix) with ESMTP id 252394001D for ; Tue, 2 Jul 2024 09:10:11 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=PqzjMLOj; spf=pass (imf11.hostedemail.com: domain of leon@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719911391; 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=g9SErVqLGwmT74XKztdMcaMW7Z/xOj3VfCtyRrAV9cY=; b=u1dh3ZsupD5IgoJAh3eDGQuWAJu5UiKH3zHVd+HvtqGnMbmEafPXf8XR5mREi1NyzZ1yNf yOuB+uzTgGfaFLRi4Ka+97uJEgj3Uq7q+BdrUwcZurOo6YTrgUXh/6nGOnwFS8mythVMFa bTESKutDBglCYRYYEedsz5yBFWenBR4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719911391; a=rsa-sha256; cv=none; b=pgxhAb9VI3E166AnVx4ywxKxVA+YwAsjwThFSFnm+8rpLII+F8A0MhJ+Y1wPwKq6rYwgSX aNaUTcDFvOZ3lCwbzAsWgFuVT3xREiYITlmmjzxKpt6dDBi7hCtyX02a1YJS+Emg4jBuSL 7w1Hw5XIODevA1awwcW35lWna+xd50Q= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=PqzjMLOj; spf=pass (imf11.hostedemail.com: domain of leon@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id C12ACCE1CFA; Tue, 2 Jul 2024 09:10:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9DCD7C4AF0C; Tue, 2 Jul 2024 09:10:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719911408; bh=I7mD3dENF4HZBNBumoLyPjANfOtz0e/DAq7U9sRqLk0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PqzjMLOjXeye9zq/mHzTj6Cl4RxSudqszFKsPx54AmE/tJ+OHgWu5ivCnN1LbBRUN 1Yv5vxJ4cXclv2ETPV3ZWWpK913fTcXr7yVC/nJo7AtJ6rnu3SJBTnF9+JY89SOEvd TYkrX534Vg7HQIcEHujxARKgvk/nUOly8dQEFeqHVLZriAax5EaH09H4nUGZ/y8Loi vDIjzYbeVRyYivHIMQkPD/nstZnUBJhmQWQx2JPLSSZymUewhzPZooZ7yJC7ufM6es ONXggsrnv88nbqTGWDYThjoAZxFxhMpavbb+IIZpeIVu2VNBfqxKFRLDEd3u7X+sYd zohrHgWstJ+9w== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Keith Busch , Christoph Hellwig , "Zeng, Oak" , Chaitanya Kulkarni Cc: Leon Romanvosky , 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 PATCH v1 03/18] dma-mapping: check if IOVA can be used Date: Tue, 2 Jul 2024 12:09:33 +0300 Message-ID: <4c479ac482c3bd123a5f999fdff46454a7faa905.1719909395.git.leon@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 252394001D X-Stat-Signature: 8it5o7xcuco93ci8pneu18jbgc7u4g7w X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1719911411-963374 X-HE-Meta: U2FsdGVkX1/LAvc1gulmwHuJx92z1d+iIoZZ3i+LXy+TW20zPy20Tyvc1lpI1Rm9kWjCGZj9mXP4PR/8QjorsrIJ3ZjfWxWVFkIrF6Ld4hzn5S9BtAUOr875pGs/qubL/9KEEX80P5R8bj6WAiEfzUp0T9032pshwpJzBR/UBY7pQUPWPh1o5Pv72bEW9mn069R0UvMz0d3fZSp3HS7saJxIHILBjwhmV1HMHhVLTrHh8KYa0v0SkUBwI21GaMyTv8UMHP2Q/C1wxE2sLuDjHWIylloisFjzZVTr3nPq7561xT3iN7MdtXjybhxwotrv54G/K7gzcDsXGMvhkeGSFj6zKtLvD7k6i2DF9VYQVZty8LzG1+OM6mvLa4976bCnVOHovYuMoyqyh38PvytE1+Vnvgxm5zsZZfKWx+1ht4DmtL8I5RmKzyCfGijPxxFZtJqrCfPj7lierpsvWkj7mlboFSiiqYDfdyB0uu2ZT5ttTEIbJoNNhVVKsg0FqiOQJgRBSclorNGX0AHSp8PT6uzFvyniedcDowDcTsTc+qpJO/vNgAwr5M542IzJN0/cIczCbg5wZa22p2Au0j/JiS1Zxyu7HZiczJ1rWy81id6/h1eMkR0zsJrm2iOjGHjT3THqJ6VwJijaeXK4sSihaKCcZlu7ELaTjKmihJ8i8rRKO2dPemS5tqgePHStLxXGSVfoi2h7gSxnPmCTiSLHvpbAyQh794tO9fBtFtLslAtGcq1XBxQr5/C4aCWe7df7CLNF1F48bp+sO+YhwQTvGspvyXuAs+xSNwqN9HPzcucQYtfbb0TXe6ILizWQwig7xH9LI3Swth7OykwM5Xn1JRY8aWHIu/15uOp9H1pNlmcq+Q5VjO8WwJkgVqthq1jo0iQjZE10B3v2eA/mQrePgwwT2rHekA/gsLCxCumlArymP95jmK0sXhKSs05w4hLbqyyR13hp6i9M9LdJPc6 brk/flko ipwNSAqMeUdVofPbchj+I7czu7BbsOJEskUR10PxYCZzePrtixKp5vXtnB6QS5lpJTfzx3KfcDwvz77g3d61KqnqaM8bv/UWVcWKwhGbDteAG5uRkxLURyk4MOuqqoQAIk8684XdCWhBviJ8+01Z4FLwOaTLyUG+tcz+rR587v/kGzBPQXHlhypDtm5D3aMqY474ltklqh5tc+Do= 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 Romanvosky Provide a way to the callers to see if IOVA can be used for specific DMA memory type. Signed-off-by: Leon Romanovsky --- drivers/iommu/dma-iommu.c | 13 ------------- drivers/pci/p2pdma.c | 4 ++-- include/linux/dma-map-ops.h | 21 +++++++++++++++++++++ include/linux/dma-mapping.h | 10 ++++++++++ kernel/dma/mapping.c | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 15 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 43520e7275cc..89e34503e0bb 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -597,19 +597,6 @@ static int iova_reserve_iommu_regions(struct device *dev, return ret; } -static bool dev_is_untrusted(struct device *dev) -{ - return dev_is_pci(dev) && to_pci_dev(dev)->untrusted; -} - -static bool dev_use_swiotlb(struct device *dev, size_t size, - enum dma_data_direction dir) -{ - return IS_ENABLED(CONFIG_SWIOTLB) && - (dev_is_untrusted(dev) || - dma_kmalloc_needs_bounce(dev, size, dir)); -} - static bool dev_use_sg_swiotlb(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir) { 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 23e5e2f63a1c..b52e9c8db241 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -9,6 +9,7 @@ #include #include #include +#include struct cma; struct iommu_ops; @@ -348,6 +349,19 @@ static inline bool dma_kmalloc_needs_bounce(struct device *dev, size_t size, return !dma_kmalloc_safe(dev, dir) && !dma_kmalloc_size_aligned(size); } +static inline bool dev_is_untrusted(struct device *dev) +{ + return dev_is_pci(dev) && to_pci_dev(dev)->untrusted; +} + +static inline bool dev_use_swiotlb(struct device *dev, size_t size, + enum dma_data_direction dir) +{ + return IS_ENABLED(CONFIG_SWIOTLB) && + (dev_is_untrusted(dev) || + dma_kmalloc_needs_bounce(dev, size, dir)); +} + void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs); void arch_dma_free(struct device *dev, size_t size, void *cpu_addr, @@ -514,6 +528,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, @@ -521,6 +537,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/dma-mapping.h b/include/linux/dma-mapping.h index 673ddcf140ff..9d1e020869a6 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -100,6 +100,11 @@ struct dma_iova_attrs { unsigned long attrs; }; +struct dma_iova_state { + struct dma_iova_attrs *iova; + struct dma_memory_type *type; +}; + #ifdef CONFIG_DMA_API_DEBUG void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr); void debug_dma_map_single(struct device *dev, const void *addr, @@ -178,6 +183,7 @@ int dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma, size_t size, struct sg_table *sgt); void dma_get_memory_type(struct page *page, struct dma_memory_type *type); +bool dma_can_use_iova(struct dma_iova_state *state, size_t size); #else /* CONFIG_HAS_DMA */ static inline int dma_alloc_iova(struct dma_iova_attrs *iova) { @@ -319,6 +325,10 @@ static inline void dma_get_memory_type(struct page *page, struct dma_memory_type *type) { } +static inline bool dma_can_use_iova(struct dma_iova_state *state, size_t size) +{ + return false; +} #endif /* CONFIG_HAS_DMA */ #if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC) diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 0c8f51010d08..9044ee525fdb 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -968,3 +968,35 @@ void dma_free_iova(struct dma_iova_attrs *iova) ops->free_iova(dev, iova->addr, iova->size); } EXPORT_SYMBOL_GPL(dma_free_iova); + +/** + * dma_can_use_iova - check if the device type is valid + * and won't take SWIOTLB path + * @state: IOVA state + * @size: size of the buffer + * + * Return %true if the device should use swiotlb for the given buffer, else + * %false. + */ +bool dma_can_use_iova(struct dma_iova_state *state, size_t size) +{ + struct device *dev = state->iova->dev; + const struct dma_map_ops *ops = get_dma_ops(dev); + struct dma_memory_type *type = state->type; + enum pci_p2pdma_map_type map; + + if (is_swiotlb_force_bounce(dev) || + dev_use_swiotlb(dev, size, state->iova->dir)) + return false; + + if (dma_map_direct(dev, ops) || !ops->alloc_iova) + return false; + + if (type->type == DMA_MEMORY_TYPE_P2P) { + map = pci_p2pdma_map_type(type->p2p_pgmap, dev); + return map == PCI_P2PDMA_MAP_THRU_HOST_BRIDGE; + } + + return type->type == DMA_MEMORY_TYPE_NORMAL; +} +EXPORT_SYMBOL_GPL(dma_can_use_iova);