From patchwork Wed May 31 15:48:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Marinas X-Patchwork-Id: 13262478 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 5AFA0C77B7A for ; Wed, 31 May 2023 15:49:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2C6E8E0003; Wed, 31 May 2023 11:49:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DDCA1900003; Wed, 31 May 2023 11:49:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA4228E000E; Wed, 31 May 2023 11:49:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id BD6088E0003 for ; Wed, 31 May 2023 11:49:47 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 83F6B16028D for ; Wed, 31 May 2023 15:49:47 +0000 (UTC) X-FDA: 80850985614.21.895012E Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf27.hostedemail.com (Postfix) with ESMTP id 9BB494001B for ; Wed, 31 May 2023 15:49:45 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685548185; 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; bh=djcU7uQ6jO6NWmeykoF7MFcFDUy8GO+UVTvXmdJHI6s=; b=sFXyBs3Ti0rsbHlJyHqmDebbVqpvsNbJLP3aMn6Jg3mzzoVYl0HzOGHXTvGWCTvZpqI4+B UADS+3lchbNUvzsBTjJpqu6wTNT9c2wJ0VfwGQ6dq/VLzKJYUv5VflUYO4gXEb0+1kl6BQ IOmMgx5ciGI792w4zdatSmINp3WyV7U= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none) ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685548185; a=rsa-sha256; cv=none; b=dFuCFnIgrcJ+mWRhB2UYC+eHwd/heH9SkkH0/EGI2eIrllZi38mHcqD6lWvATY+Mn+rSsQ MLkd1q4yX63HJ9CP2MZSjiC1nVuBIpKpS5X4zafRLXRZ5CEmSdu+i+szByyJFb8Fi7Tp4o NdTIbudI0Iw3wJR9S2J50Pi/5/pVCiU= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 757AF62B59; Wed, 31 May 2023 15:49:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35ADEC433A1; Wed, 31 May 2023 15:49:40 +0000 (UTC) From: Catalin Marinas To: Linus Torvalds , Christoph Hellwig , Robin Murphy Cc: Arnd Bergmann , Greg Kroah-Hartman , Will Deacon , Marc Zyngier , Andrew Morton , Herbert Xu , Ard Biesheuvel , Isaac Manjarres , Saravana Kannan , Alasdair Kergon , Daniel Vetter , Joerg Roedel , Mark Brown , Mike Snitzer , "Rafael J. Wysocki" , Jonathan Cameron , linux-mm@kvack.org, iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org Subject: [PATCH v6 15/17] iommu/dma: Force bouncing if the size is not cacheline-aligned Date: Wed, 31 May 2023 16:48:34 +0100 Message-Id: <20230531154836.1366225-16-catalin.marinas@arm.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230531154836.1366225-1-catalin.marinas@arm.com> References: <20230531154836.1366225-1-catalin.marinas@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9BB494001B X-Rspam-User: X-Stat-Signature: xpng5uj4xiu8f6bgpytk474q6y444km7 X-Rspamd-Server: rspam01 X-HE-Tag: 1685548185-979301 X-HE-Meta: U2FsdGVkX1/qndESNkDdPu5V6YmpEpMU19nUs3TJTp11dQu+8hOKEcL27QFom7JW/6t/l25SqLb1WUy9W5sUuuohhXPs1wno9oBTBVpufUDe/DWEjYLNfsr+2VlEc8IwFZFdq9qx07C3T+dEO90rgVlKa2MqRhdnDimyj80+3OpGFtKepWpa4mMyyKdYaPIgl+0gMlNToFKdAEy1fmIGb403wUcwKN8kOQ4f5V1HRZ1ABq0Z86/tiaRRkeYWFSsx9gjr3kxyqRZUkQ5ZDev5tbWkJggwaJjuBVdanjiswo7xGxhJrVLt7FN/ZHfnEu0s2iP3g9LXtQLSQBiKl4emwtRM9wr05fwUzWdrdimI/MSwHneljQARZjOHz1++AVgau3wTkcUYP74wKzhH9MGoO0uLZkS3q39vB+i1BpGt9cvxw8MlOqQ8dwuXgNhw4gKpUSY+T8jEpuNFVA05tK+R/YsRVuX8o6fTgE619GUxvuj+Fsw+PzU9XPjVdPkQp8Pwk7pm+hlPwRs9PeFJ7LzogkgPBpkfFAANPtUz6VBGgZlt9sbH6Clhri/T7pJitr2ql58uDlBI9k8SP4YXKGQYazFabJzALVwBpweOKmUbx6p0rZuhst774ljVbxZNQA9aokyeTlVj+swv3e1gHsdY+fVMH0F3IFKhebgLpSdI4Lcg5lwirdlOQd3GpWuYt3yGwhb3kpNOOkPfN9w0/QE93yb8KRhmJlY8m0jO37BJ44+bOeaxt0i1UWkJM9Fof0zvcswHEeUwaK0v4YC4ZNk9WxehZMku9WhOUXvfLhhQvTqb7lF4LYYGwFNkH8KbFcuy1Cz6YebmeH+HMCehlPuaseAPH91ojVjjvE4HDl6cFJT1ue1vjT0kr0fsHAQSmIJ43Bn0LBByQSXR93vsbnTLZ0cvBm2irsMhfvYQXp38W6TRZcknyG+EOhJJ1Um2A46JCFURNv0l8f23hn20x9j gM2qp4Hy Sdy4xaNuuI3Xl/o9gIjdIodCzx/cah/9E8wfPl3OWW53MrIfIbhODq26tfSlbi7bsrtKUr7iuxk17U0j8jZpEPrTxOp1Zb92M6t4uqDcOI0x8cyqS3abVRuNKJftPVDrfktPvL00wVNk23eN3ovJiKAJOHt4gF2uaWQN6zV6ZFVRfD6b6dji9kZgxCb65dUu5gzJdVkxL1Wh4A/zk7/D2WRFGtpdxYwi44nue0apn0zM9kjpY9Lr88Lq44lOHhxb2HA7QcYu2wU95NVgJPgjBdIVsd6gg0ho9mZ3mky+QW+MOT9Xi8eVKLP2V8xE7nMz8hSCw8JnaGBb19B1oeApl0UqcqLCF084PlFvs0iyHyLA+nFd93WawHy8nDVEvHOeUHI6LeeLj8oTMsGlT6rz2a9YD7klt8iXaTQeepVk96UB/JH2fCXpuisnFcg8KbhPAcWYoREnF+FqBlH0boWLUR8Nw6r2JlfnreMnEW/1deYTl7RYQ0q1ZEKxFrGqZNe7Jf0ig0K/PV2nYBKt9+pEUmcBgcw== 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: Similarly to the direct DMA, bounce small allocations as they may have originated from a kmalloc() cache not safe for DMA. Unlike the direct DMA, iommu_dma_map_sg() cannot call iommu_dma_map_sg_swiotlb() for all non-coherent devices as this would break some cases where the iova is expected to be contiguous (dmabuf). Instead, scan the scatterlist for any small sizes and only go the swiotlb path if any element of the list needs bouncing (note that iommu_dma_map_page() would still only bounce those buffers which are not DMA-aligned). To avoid scanning the scatterlist on the 'sync' operations, introduce an SG_DMA_SWIOTLB flag set by iommu_dma_map_sg_swiotlb(). The dev_use_swiotlb() function together with the newly added dev_use_sg_swiotlb() now check for both untrusted devices and unaligned kmalloc() buffers (suggested by Robin Murphy). Signed-off-by: Catalin Marinas Reviewed-by: Robin Murphy Cc: Joerg Roedel Cc: Christoph Hellwig --- drivers/iommu/Kconfig | 1 + drivers/iommu/dma-iommu.c | 50 ++++++++++++++++++++++++++++++------- include/linux/scatterlist.h | 41 ++++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index db98c3f86e8c..670eff7a8e11 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -152,6 +152,7 @@ config IOMMU_DMA select IOMMU_IOVA select IRQ_MSI_IOMMU select NEED_SG_DMA_LENGTH + select NEED_SG_DMA_FLAGS if SWIOTLB # Shared Virtual Addressing config IOMMU_SVA diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index b8bba4aa196f..6eaac5123839 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -520,9 +520,38 @@ 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) +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); + 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) +{ + struct scatterlist *s; + int i; + + if (!IS_ENABLED(CONFIG_SWIOTLB)) + return false; + + if (dev_is_untrusted(dev)) + return true; + + /* + * If kmalloc() buffers are not DMA-safe for this device and + * direction, check the individual lengths in the sg list. If any + * element is deemed unsafe, use the swiotlb for bouncing. + */ + if (!dma_kmalloc_safe(dev, dir)) { + for_each_sg(sg, s, nents, i) + if (!dma_kmalloc_size_aligned(s->length)) + return true; + } + + return false; } /** @@ -922,7 +951,7 @@ static void iommu_dma_sync_single_for_cpu(struct device *dev, { phys_addr_t phys; - if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev)) + if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir)) return; phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle); @@ -938,7 +967,7 @@ static void iommu_dma_sync_single_for_device(struct device *dev, { phys_addr_t phys; - if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev)) + if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir)) return; phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle); @@ -956,7 +985,7 @@ static void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg; int i; - if (dev_use_swiotlb(dev)) + if (sg_dma_use_swiotlb(sgl)) for_each_sg(sgl, sg, nelems, i) iommu_dma_sync_single_for_cpu(dev, sg_dma_address(sg), sg->length, dir); @@ -972,7 +1001,7 @@ static void iommu_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg; int i; - if (dev_use_swiotlb(dev)) + if (sg_dma_use_swiotlb(sgl)) for_each_sg(sgl, sg, nelems, i) iommu_dma_sync_single_for_device(dev, sg_dma_address(sg), @@ -998,7 +1027,8 @@ static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, * If both the physical buffer start address and size are * page aligned, we don't need to use a bounce page. */ - if (dev_use_swiotlb(dev) && iova_offset(iovad, phys | size)) { + if (dev_use_swiotlb(dev, size, dir) && + iova_offset(iovad, phys | size)) { void *padding_start; size_t padding_size, aligned_size; @@ -1166,6 +1196,8 @@ static int iommu_dma_map_sg_swiotlb(struct device *dev, struct scatterlist *sg, struct scatterlist *s; int i; + sg_dma_mark_swiotlb(sg); + for_each_sg(sg, s, nents, i) { sg_dma_address(s) = iommu_dma_map_page(dev, sg_page(s), s->offset, s->length, dir, attrs); @@ -1210,7 +1242,7 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, goto out; } - if (dev_use_swiotlb(dev)) + if (dev_use_sg_swiotlb(dev, sg, nents, dir)) return iommu_dma_map_sg_swiotlb(dev, sg, nents, dir, attrs); if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC)) @@ -1315,7 +1347,7 @@ static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, struct scatterlist *tmp; int i; - if (dev_use_swiotlb(dev)) { + if (sg_dma_use_swiotlb(sg)) { iommu_dma_unmap_sg_swiotlb(dev, sg, nents, dir, attrs); return; } diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index 2f06178996ba..69d87e312263 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -251,11 +251,13 @@ static inline void sg_unmark_end(struct scatterlist *sg) /* * One 64-bit architectures there is a 4-byte padding in struct scatterlist * (assuming also CONFIG_NEED_SG_DMA_LENGTH is set). Use this padding for DMA - * flags bits to indicate when a specific dma address is a bus address. + * flags bits to indicate when a specific dma address is a bus address or the + * buffer may have been bounced via SWIOTLB. */ #ifdef CONFIG_NEED_SG_DMA_FLAGS -#define SG_DMA_BUS_ADDRESS (1 << 0) +#define SG_DMA_BUS_ADDRESS (1 << 0) +#define SG_DMA_SWIOTLB (1 << 1) /** * sg_dma_is_bus_address - Return whether a given segment was marked @@ -298,6 +300,34 @@ static inline void sg_dma_unmark_bus_address(struct scatterlist *sg) sg->dma_flags &= ~SG_DMA_BUS_ADDRESS; } +/** + * sg_dma_use_swiotlb - Return whether the scatterlist was marked for SWIOTLB + * bouncing + * @sg: SG entry + * + * Description: + * Returns true if the scatterlist was marked for SWIOTLB bouncing. Not all + * elements may have been bounced, so the caller would have to check + * individual SG entries with is_swiotlb_buffer(). + */ +static inline bool sg_dma_use_swiotlb(struct scatterlist *sg) +{ + return sg->dma_flags & SG_DMA_SWIOTLB; +} + +/** + * sg_dma_use_swiotlb - Mark the scatterlist for SWIOTLB bouncing + * @sg: SG entry + * + * Description: + * Marks a a scatterlist for SWIOTLB bounce. Not all SG entries may be + * bounced. + */ +static inline void sg_dma_mark_swiotlb(struct scatterlist *sg) +{ + sg->dma_flags |= SG_DMA_SWIOTLB; +} + #else static inline bool sg_dma_is_bus_address(struct scatterlist *sg) @@ -310,6 +340,13 @@ static inline void sg_dma_mark_bus_address(struct scatterlist *sg) static inline void sg_dma_unmark_bus_address(struct scatterlist *sg) { } +static inline bool sg_dma_use_swiotlb(struct scatterlist *sg) +{ + return false; +} +static inline void sg_dma_mark_swiotlb(struct scatterlist *sg) +{ +} #endif /* CONFIG_NEED_SG_DMA_FLAGS */