From patchwork Wed May 24 17:19:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Marinas X-Patchwork-Id: 13254351 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 3BAA5C7EE31 for ; Wed, 24 May 2023 17:20:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC79D90000F; Wed, 24 May 2023 13:20:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C77E2900002; Wed, 24 May 2023 13:20:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B400590000F; Wed, 24 May 2023 13:20:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A5A8E900002 for ; Wed, 24 May 2023 13:20:02 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7F4B6C08AA for ; Wed, 24 May 2023 17:20:02 +0000 (UTC) X-FDA: 80825811444.07.2C8E7FC Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf13.hostedemail.com (Postfix) with ESMTP id C4CBE20011 for ; Wed, 24 May 2023 17:20:00 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf13.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684948800; 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=trBjKA2P3t7Lv44FdRPNpP5ETnPfkIlOly8ehL5hPTw=; b=PhdSsGw6QXbW5hlc2SWmD3w/ZxaYsV4o/OXauaUD3bTd9QQtGhBT3PwnkkIP+PegZIeCOp URKtrg1cu82s85EQA9VDgKscJeGLy7bgfL+F7B77iha1WjYiedWaHYmY71fD2wr68MCTHv oMWRcAH9RFv4+xnHQiE9MRpMnOCehIM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf13.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684948800; a=rsa-sha256; cv=none; b=ni6uSCTyIFvAW0zrH4MEJuvOScuXAXpvieYAZrH6ezsi486efXnYhVIpKGAr04w0I4B5t6 aarHph0HCrRYZ6dxYQOhSq2d0A78vEUjBy52MxJX2lw0DaY6PY9ZT8r24WG4BpS9Vt/nl8 09lYkKnF++X3lMT1Rtswt6KATyJICqo= 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 D8365638D6; Wed, 24 May 2023 17:19:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9AC60C433D2; Wed, 24 May 2023 17:19:55 +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" , linux-mm@kvack.org, iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org Subject: [PATCH v5 12/15] dma-mapping: Force bouncing if the kmalloc() size is not cache-line-aligned Date: Wed, 24 May 2023 18:19:01 +0100 Message-Id: <20230524171904.3967031-13-catalin.marinas@arm.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230524171904.3967031-1-catalin.marinas@arm.com> References: <20230524171904.3967031-1-catalin.marinas@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: C4CBE20011 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 48imtgq3f3syg1myhqbxqdqxssbz4e6k X-HE-Tag: 1684948800-922560 X-HE-Meta: U2FsdGVkX18YaUs2/5q3zCNrTmAWy+r/GINU4UxIm4fB3sakxTVjtp30h2Av349WKaFhaT7PO46QXSZc5tG7AQOXbO/OrTvdYPw0kOh89yVNDZGlJGC75I6dTDVLzdoFdf3K261Yx4CMdd4D9YJVpgAMPq1rcTu36QXELQnggksA+sEGwzP3sc8ygF7EWe1o38UiGlLhnmtT1B+6EDBhMH9z5GFgxzvtQGackDvGVlnmmPzUxue1gxgqyVsqkSrQs/UlzPecFoGwgdObfaZPqcm/lASqyAa0O4HBnkl72L04u/XJU5dyJvvRG3ojOTKFb4Wr/Cm0GvXeuoVsmebqzSkD+hXSbfKA6aMQ8PxOzTwiZoDtim9i2OGacKkbGmey5eW8hWYM7UvqnY8SAu29zHs5lKr/9cr+sp+YUocm77O6DznKx3p185uxXLkNjxV2DbdCvyiH2L0EhPdLOK3ZXxIekgcFUad1mDAV48QrX0j+n46D+5HxfnZIGp5jAjZYpu3nQEZ8PheieuPqJsF5dx/l7vlydldbKnLt7z8ZcvMFpr25MRCQ8s8uy2OMnladY5P8V5btUWDeSAdL5dhwTbhSAWYpHJXzLWQfJZ55HHRbM5qayEaCbDq7ox2eof7r3eAP3f1XH/x1NL7wLqXdUPZKuFVe2zASREIYPGwIB6CT1aaEApxjl7x3qkU0g1LblP191OZyiJx0cwvShONmflwujML9Lo2m+S63AcDsrY2siNUswJWlUskCjb8mLeTTQeK6LEBhgfsz4lP4LCCxYr65wU+fxvLmHPL8paqAtMfEl8Dry78TM3gEWv2yTtMpd6jseqfnuk21DjPRGz6YjDgssb1aMl6OrUuae2Hzuxlrgm7ei8Iy4MGc4Qja+WIlCcLUkGJ2lgUB/+C1Egm8HwySYuWzBAfkKXcbPrK/K60SDRF6jW5+/tMs0UQMWNMKGevrnSRxMTwK7iYNv77 wICySYMA AaZ4VKgONkBkVoxcfZAR8WpoFfKN03aN2R1DOai6vp1MqxtVNkWUzBZmXJgUS5zd04/gtAEPQHg2g5LzFrX3TU+RjIF46QRHaZ6n6hIQWgUK0JT7Xp/1ghO3QrufPUFOBVdPPvRfj7mt5JdBhIFlZfg9oD9o1ZlJCF48+V8ROLtFsBs1wXd6k3SBbNwxi0apIAxPqkywoimRhuCmc8gl1XdCT4obw+UbvNipdmtRmJY2lRGSbBOtEDsSiFlGYDb91Ppk8HQuV+DajcnZoCVXBjySeLfenBoWbJP7TDoHPo8hKI20R68AAAzJ4BAEySa1KJNsIzsfu3+MkZG7LjuBpVggWYawh9ipHqfyxwDGrVXMn9trYo99vAf8sNbmEudsFuWlXtJxVYyfS2DltdkrOUCAhHg/1wvyuqekE11kdwzuRwGtc8UEzIjHt7YgKyFyAVPBDqmMBdhLeOqEaFGwPttfGrQ== 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: For direct DMA, if the size is small enough to have originated from a kmalloc() cache below ARCH_DMA_MINALIGN, check its alignment against dma_get_cache_alignment() and bounce if necessary. For larger sizes, it is the responsibility of the DMA API caller to ensure proper alignment. At this point, the kmalloc() caches are properly aligned but this will change in a subsequent patch. Architectures can opt in by selecting ARCH_WANT_KMALLOC_DMA_BOUNCE. Signed-off-by: Catalin Marinas Reviewed-by: Christoph Hellwig Cc: Robin Murphy Reviewed-by: Robin Murphy --- include/linux/dma-map-ops.h | 61 +++++++++++++++++++++++++++++++++++++ kernel/dma/Kconfig | 4 +++ kernel/dma/direct.h | 3 +- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 31f114f486c4..9bf19b5bf755 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -8,6 +8,7 @@ #include #include +#include struct cma; @@ -277,6 +278,66 @@ static inline bool dev_is_dma_coherent(struct device *dev) } #endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */ +/* + * Check whether potential kmalloc() buffers are safe for non-coherent DMA. + */ +static inline bool dma_kmalloc_safe(struct device *dev, + enum dma_data_direction dir) +{ + /* + * If DMA bouncing of kmalloc() buffers is disabled, the kmalloc() + * caches have already been aligned to a DMA-safe size. + */ + if (!IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC)) + return true; + + /* + * kmalloc() buffers are DMA-safe irrespective of size if the device + * is coherent or the direction is DMA_TO_DEVICE (non-desctructive + * cache maintenance and benign cache line evictions). + */ + if (dev_is_dma_coherent(dev) || dir == DMA_TO_DEVICE) + return true; + + return false; +} + +/* + * Check whether the given size, assuming it is for a kmalloc()'ed buffer, is + * sufficiently aligned for non-coherent DMA. + */ +static inline bool dma_kmalloc_size_aligned(size_t size) +{ + /* + * Larger kmalloc() sizes are guaranteed to be aligned to + * ARCH_DMA_MINALIGN. + */ + if (size >= 2 * ARCH_DMA_MINALIGN || + IS_ALIGNED(kmalloc_size_roundup(size), dma_get_cache_alignment())) + return true; + + return false; +} + +/* + * Check whether the given object size may have originated from a kmalloc() + * buffer with a slab alignment below the DMA-safe alignment and needs + * bouncing for non-coherent DMA. The pointer alignment is not considered and + * in-structure DMA-safe offsets are the responsibility of the caller. Such + * code should use the static ARCH_DMA_MINALIGN for compiler annotations. + * + * The heuristics can have false positives, bouncing unnecessarily, though the + * buffers would be small. False negatives are theoretically possible if, for + * example, multiple small kmalloc() buffers are coalesced into a larger + * buffer that passes the alignment check. There are no such known constructs + * in the kernel. + */ +static inline bool dma_kmalloc_needs_bounce(struct device *dev, size_t size, + enum dma_data_direction dir) +{ + return !dma_kmalloc_safe(dev, dir) && !dma_kmalloc_size_aligned(size); +} + 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, diff --git a/kernel/dma/Kconfig b/kernel/dma/Kconfig index acc6f231259c..abea1823fe21 100644 --- a/kernel/dma/Kconfig +++ b/kernel/dma/Kconfig @@ -90,6 +90,10 @@ config SWIOTLB bool select NEED_DMA_MAP_STATE +config DMA_BOUNCE_UNALIGNED_KMALLOC + bool + depends on SWIOTLB + config DMA_RESTRICTED_POOL bool "DMA Restricted Pool" depends on OF && OF_RESERVED_MEM && SWIOTLB diff --git a/kernel/dma/direct.h b/kernel/dma/direct.h index e38ffc5e6bdd..97ec892ea0b5 100644 --- a/kernel/dma/direct.h +++ b/kernel/dma/direct.h @@ -94,7 +94,8 @@ static inline dma_addr_t dma_direct_map_page(struct device *dev, return swiotlb_map(dev, phys, size, dir, attrs); } - if (unlikely(!dma_capable(dev, dma_addr, size, true))) { + if (unlikely(!dma_capable(dev, dma_addr, size, true)) || + dma_kmalloc_needs_bounce(dev, size, dir)) { if (is_pci_p2pdma_page(page)) return DMA_MAPPING_ERROR; if (is_swiotlb_active(dev))