From patchwork Sun Nov 6 22:01:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Marinas X-Patchwork-Id: 13033622 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 6BA10C4332F for ; Sun, 6 Nov 2022 22:02:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D72E48E0005; Sun, 6 Nov 2022 17:02:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D23288E0001; Sun, 6 Nov 2022 17:02:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C11CF8E0005; Sun, 6 Nov 2022 17:02:01 -0500 (EST) 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 B50F58E0001 for ; Sun, 6 Nov 2022 17:02:01 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 744C71208E5 for ; Sun, 6 Nov 2022 22:02:01 +0000 (UTC) X-FDA: 80104390842.10.FADABFE Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf22.hostedemail.com (Postfix) with ESMTP id D9ADEC000A for ; Sun, 6 Nov 2022 22:02:00 +0000 (UTC) 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 ams.source.kernel.org (Postfix) with ESMTPS id 585E2B80D38; Sun, 6 Nov 2022 22:01:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 37993C433B5; Sun, 6 Nov 2022 22:01:54 +0000 (UTC) From: Catalin Marinas To: Linus Torvalds , Arnd Bergmann , Christoph Hellwig , Greg Kroah-Hartman Cc: 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" , Robin Murphy , linux-mm@kvack.org, iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org Subject: [PATCH v3 02/13] dma-mapping: Force bouncing if the kmalloc() size is not cacheline-aligned Date: Sun, 6 Nov 2022 22:01:32 +0000 Message-Id: <20221106220143.2129263-3-catalin.marinas@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221106220143.2129263-1-catalin.marinas@arm.com> References: <20221106220143.2129263-1-catalin.marinas@arm.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667772121; 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=PvipVnjKjzWqPzfgQSoQHSuW6iWoAw45fZnbTXFvWx4=; b=UJ+cHo7Mr5NmUBPVUpxaTD/1RHAFPfmmUfwirXjSRwKGNywb9aNsedGezhacZijE1h5oTj k9+3u001a8Rebu/YVCJdbKvKE6ga0NanO9/ge/ndBB+YO9+fjpg7gvkQJj4XGnOUKIcM95 bhCMQUAv6KaD14HuRO8Y7E3TlOcnRno= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; spf=pass (imf22.hostedemail.com: domain of cmarinas@kernel.org designates 145.40.68.75 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=1667772121; a=rsa-sha256; cv=none; b=IotFKiXMU5bVJPS6V1pTzDCrSHYnPn8S6H8Sdj98bmKbH+a+NQ3Jb65a/IoreUJvcmYfs3 9hkZ1JmnQrcNQTZ1Dbrmpx59MYj6FYvArkzk4Iq+UDkG+QwCXz3d+uRgG38m74M0cT5LeK cdL0W5qYKKum21vdYXpKkcPy2O+/kLs= Authentication-Results: imf22.hostedemail.com; dkim=none; spf=pass (imf22.hostedemail.com: domain of cmarinas@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=cmarinas@kernel.org; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none) X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: D9ADEC000A X-Stat-Signature: 9oodq7r9e41zsnab87jnjpka4tzwop8n X-HE-Tag: 1667772120-740431 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 cache_line_size() and bounce if necessary. For larger sizes, it is the responsibility of the DMA API caller to ensure proper alignment. Signed-off-by: Catalin Marinas Cc: Christoph Hellwig Cc: Robin Murphy --- include/linux/dma-map-ops.h | 27 +++++++++++++++++++++++++++ kernel/dma/direct.h | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index d678afeb8a13..785f7aa90f57 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -8,6 +8,7 @@ #include #include +#include struct cma; @@ -275,6 +276,32 @@ static inline bool dev_is_dma_coherent(struct device *dev) } #endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */ +/* + * Check whether the given size, assuming it is for a kmalloc()'ed object, is + * safe for non-coherent DMA or needs bouncing. + */ +static inline bool dma_kmalloc_needs_bounce(struct device *dev, size_t size, + enum dma_data_direction dir) +{ + /* + * No need for bouncing if coherent DMA or the direction is + * DMA_TO_DEVICE. + */ + if (!IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) || + dir == DMA_TO_DEVICE || dev_is_dma_coherent(dev)) + return false; + + /* + * 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 false; + + return true; +} + 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/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))