From patchwork Mon Jun 12 15:31:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Marinas X-Patchwork-Id: 13276870 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 B4701C88CB6 for ; Mon, 12 Jun 2023 15:33:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E7A0940008; Mon, 12 Jun 2023 11:33:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 497A28E0005; Mon, 12 Jun 2023 11:33:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3604F940008; Mon, 12 Jun 2023 11:33:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2155D8E0005 for ; Mon, 12 Jun 2023 11:33:14 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id EDCF3140299 for ; Mon, 12 Jun 2023 15:33:13 +0000 (UTC) X-FDA: 80894489466.21.AF10567 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf01.hostedemail.com (Postfix) with ESMTP id 756304001A for ; Mon, 12 Jun 2023 15:33:11 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf01.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=1686583991; 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=UAWloagYHC8zwKSOoXFeJLmx1VhZfhH4Y6rt98gl2io=; b=38ul5Wg3p79C617e+/4CLzLcC/heZPEVJbpvTABCqnO9VCLTrPVZ9I4BEFGsXEHssY1J9u hfVLBB313Lp5Yz6ZDC0HZCTlqcbFP1EYfZ2aWGkGGVBkc2r8cv2mAKe/x0MWczhCfjXw8N DhCEe7oJYM6XqGIWfYk0/1OM9CWHroE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf01.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=1686583991; a=rsa-sha256; cv=none; b=FOWDhdVzRZGIjWyBhfU4fqqY2WsPh3bKMyRJMeUgjMFxOCsvJtC3OL1VnfEuU6P7/DEx9T TeqBavZYe2ykk3JxxqvI65/WLeSUgb6vQLzFb+7EYzox8+blAr9bIK4cy7W+bUQ5M4LN/e 7PV2EKvLdi7wrX8SOsIW2TZm6FAEbHs= 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 6DB2E617A4; Mon, 12 Jun 2023 15:33:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D1F8DC433D2; Mon, 12 Jun 2023 15:33:05 +0000 (UTC) From: Catalin Marinas To: Andrew Morton , Linus Torvalds Cc: Christoph Hellwig , Robin Murphy , Arnd Bergmann , Greg Kroah-Hartman , Will Deacon , Marc Zyngier , Herbert Xu , Ard Biesheuvel , Isaac Manjarres , Saravana Kannan , Alasdair Kergon , Daniel Vetter , Joerg Roedel , Mark Brown , Mike Snitzer , "Rafael J. Wysocki" , Jonathan Cameron , Vlastimil Babka , linux-mm@kvack.org, iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org Subject: [PATCH v7 14/17] dma-mapping: Force bouncing if the kmalloc() size is not cache-line-aligned Date: Mon, 12 Jun 2023 16:31:58 +0100 Message-Id: <20230612153201.554742-15-catalin.marinas@arm.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230612153201.554742-1-catalin.marinas@arm.com> References: <20230612153201.554742-1-catalin.marinas@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 756304001A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: n9pg4pwtiqx1fyecg1x1jrka361z6e75 X-HE-Tag: 1686583991-786500 X-HE-Meta: U2FsdGVkX1+8zZ12hTjavDUla91St9ypO4xXOaBLevBcq2dlTNaq6RaeS7h+MMsROEQqwTHTC32D6WlDuTSeDEndjV5Vx90DgUAaasdXBmNxsMntbmh6KRlJQTJoZz1sDbIYt1BKKszwi4N9TdM32lAdSPVKMoyTmjzhiP38Ns8eV0NLK8+rutwVA7SMrRccSlT9S9NfYN8H2ZlkSlzegKal1pEuwJfhVVWVlbSQt7WZwLOLL1rxZ56+9H06LRkoDgz04apuMQBWOBN0Nm+B4lgFpftOURNi0NIexDQ9pogTPgzKwKDBCEFN5N8mvLlP3KiZvh0vY2Uku0w6ZlgKiiMe6g3v5nFVaFS04+5XVcjxUfln18KruzOXVPOMW5N/ijysJzIRsNH6Anw+6qQd1Fib43oKy9A2148NRUXO0hIS13sTjVrRSeZIMufb1DbO+LIXKUPDKmyK7whv/8ICAhrXPmNOVCutv9lyYbtvRX/8eAP6fiOWhXVDWYS0+kDZiqYs+PhozsThzN0p+ZmshFvpyFt+n4tWWROipF+y6D22HEOuFhYqLX/gbwyqrZTq1sjFoNFQhpXTswflB51o4qp9aA9C2Xl/ai+OnKfcc7PoRtZqsBbdm+J5FiVlR3pyKwQOGpqhSfw7WcS8MiSNP36k2fb331uA6uQylNLJyW6Q1orOt2QIZ0IJWAL6ocM4cda/m18dZcC9ZitbQwNdXLelwvhks8qVhWM2mVoiUs/B6K7NDxCP+IHMNbF/kAw25n1zTqit/r8Kn+ShDfCQnmb43xfzlaIkcjrZKeJnyMdRjegrijSu274Y/jEhhZBmUecgULvo03Ug3p76Sevfzo+DlL+NyHcCKywpCjdqy1quV9h5oDfLLyQTU9mMBgus5lgRFeATE3BKo0+JhltMYNbKCVRlWGFLWSuFZghfhqgxZbaWJPpTEcla3PnWFv1RLtpoo5eb+LiP50oPiKw 5Zf/UkWP 9o+6mC2MtdRijAqimZomhMzQL6IxIwLYG0/9bK8sglzAw93M7MEnpKYj9RuoZObFUu2BY0utkKnSIiL79BdQcCZtLhBhEFnJwGV4+BG0fuoft7gB1hfsVce5H1fNRrYrRBxMFeHS3Y5TW6ZLqqJMVVzCwiWASMwTYnArP1y5or7cB6q2zkfg1J6fjENBSUAyUqHPikzHXuHchaE0cf7rwIrT+S20/cCQvNv+QLlHk8mmlrwbF4jAd0i0gBTiiEU5AeDuSMFjfT/qIuwuJoSYz9iFgQJm766oxs+Fdw4gaA1FFwZkNtxrkMrrLOaxqh3bT7ZG6nJ0JN845Tv4nPrM8GrZq2yo77wlyiSEouh27dKDEFRRJKAepPjF8kKXb2b3H0lN0noYs07gciikEV6tidz9E6XTBFm5p7K2XZ5ZkoUAbqowB69KFnkasUMRRWNp7yryu6v0wfp7PUzsFnwaPBn8KR2Q6mlxWMWtubx1AbF0kY3M= 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 DMA_BOUNCE_UNALIGNED_KMALLOC. Signed-off-by: Catalin Marinas Reviewed-by: Christoph Hellwig Reviewed-by: Robin Murphy Tested-by: Isaac J. Manjarres --- 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))