From mboxrd@z Thu Jan 1 00:00:00 1970 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 C8FB7C77B7A for ; Thu, 25 May 2023 15:54:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B3BD900002; Thu, 25 May 2023 11:54:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43B4E6B0075; Thu, 25 May 2023 11:54:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2DB5B900002; Thu, 25 May 2023 11:54:08 -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 198DF6B0074 for ; Thu, 25 May 2023 11:54:08 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C9798A093D for ; Thu, 25 May 2023 15:54:07 +0000 (UTC) X-FDA: 80829223734.21.3440666 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf16.hostedemail.com (Postfix) with ESMTP id 7B9A9180014 for ; Thu, 25 May 2023 15:54:05 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf16.hostedemail.com: domain of robin.murphy@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=robin.murphy@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685030046; a=rsa-sha256; cv=none; b=iAAkD7gX7OjQ+2sfFmXTvwB0yQ49cScBIZrhl7uO0Jvlmc5Tt4tqJP4h5xovLQWtQgn6Mv MccBZVVGTu3X2HrGWFOWQpQsP0kHwcyLeQ2ZENoc4h7n4t0Bz3wE1M2EbkAv2k8cbgc6uT oFbJIFTwJrEe13VwLtjECXUYDv0HusI= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf16.hostedemail.com: domain of robin.murphy@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=robin.murphy@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685030046; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nnpffnfYXB1CIGA6L1nh5KCKS4QHB3lP3vm0MBlkLHU=; b=jljKaEfMhQYHWtEhKeIUbh2Ol0ax6lBryo2RqvfWkuoYGiG19YGL54QtyJcUM6Eq2n2xjE cdPr8/A3tloQIHvcy7S1s84pTKfaBdjake7U8J1M6rzd2EBh52Y4Tb2fdbz1+1NQHMeOvq u/R1lB9Tf1c/LjK73N32bACVOuWB1rw= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3E9CDD75; Thu, 25 May 2023 08:54:49 -0700 (PDT) Received: from [10.1.196.40] (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9EDEE3F762; Thu, 25 May 2023 08:54:01 -0700 (PDT) Message-ID: Date: Thu, 25 May 2023 16:53:56 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux aarch64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v5 12/15] dma-mapping: Force bouncing if the kmalloc() size is not cache-line-aligned Content-Language: en-GB To: Catalin Marinas , Linus Torvalds , Christoph Hellwig 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 References: <20230524171904.3967031-1-catalin.marinas@arm.com> <20230524171904.3967031-13-catalin.marinas@arm.com> From: Robin Murphy In-Reply-To: <20230524171904.3967031-13-catalin.marinas@arm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 7B9A9180014 X-Stat-Signature: hb63uy4pqjda4x3a81zeqsqk4wr4uymd X-Rspam-User: X-HE-Tag: 1685030045-114134 X-HE-Meta: U2FsdGVkX18qbS7MB3ZAxyzUQ4ANIrneq47/7tiRlYRLac1yWpl40ylZsdCvCzUxKXarsd3WwF+T4ply9OIZS9D0FVEqxfZCJUkGVeMj9XJY8gogzXntHnTHO17sGw6KPJydvRDah/iuo/OqSim88EwJTRJVWTB6HxTCCJ+0FCYBTp96RjDR8zsovbPLnIukk/cQ4osgNjs947Y7rjLvCoUEKoDnwSFala61rKgaf6sUfzAtIe3fe5vxfuWBaYHG4eAGxy+aveD/MTaSxb/V5icDMC41qPwqN/OvnH1gzHa5SFuKDNpTwTE1kRMEmpjJliJ+zuT8S3bxP3BNcbJzPLBd8hCIbYNiVgvpQ5prED4R0fBzKuKQ+0QE6QbmpLymYFWFanJ1Yx1iJnAykL3zyVKg8Yw2mt88cwyD0JzrN/VY7gAt+qi3tSeHVdkvt8eVbig9qIAAKxeXG6EaJY86BXAT25GvcH9zKDMwr5UNoRjJM56qu0+vDrm7+TSR2jHz97yi808iEFE7M8jzv6SlyqaNvyjBJ/4YHqt/on/q5ut9RlqyttjpPvh5QB9DmFLuvi+vJta1tXOjRXOI3RYxnfcP5BrVCQ1FvPfY/snzgIAtyeOG47wXCI7JcLRGV9zXF3H+akiaX1U3igehHx67EhQ3GDGR2cTUPZobM7GzjcdQ2/eS4SWdrsOiQh4pAXDMIdU78RCYIfk+Dvfu3a5BLNSSxMLL0rBQxv4MRC5RScQdoSfvDlcMYwv8CmK58GIyC+kTDugFv0vifDoVDW8mEWEUsNKdp5Hb7SZMbnszslw5ra/BhNizWOc62c/7Rcxlqit4U0x6vh/PAXAEaGL2LnBesUs9nhN/8AjNmzZ4FvIigwY8gfXujllo0aAvnGJ9yXHF4i5/4WOvY/VYiGkpNFLNwtdF98JjuHzpo1HYHZPE2eEyXVkzy5Ek5A8zq2WKHdPje/hnUxrAIpbMAWE 3PZOanEJ EClHk95UpxkR/t+8r/vGUux30a/X6WFOFy3o9HueKviq60Ezjbm2qVJKe1jaJ35Dxmv3tTAyGPGFMU4m4JeENyb72CbAgDV28+XpR/tgBAmxrBTuFsNSs0Wh2UIuoF7cV58raityinh5bXiM4wshUEtbjouohXUzKpFmHhUQa4i7uqFA= 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: On 24/05/2023 6:19 pm, Catalin Marinas wrote: > 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. Thanks for the additional comment, that's a great summary for future reference. Reviewed-by: Robin Murphy > Signed-off-by: Catalin Marinas > Reviewed-by: Christoph Hellwig > Cc: 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))