From patchwork Fri Jan 26 13:54:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 192598 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:e09d:b0:103:945f:af90 with SMTP id gm29csp671915dyb; Fri, 26 Jan 2024 05:56:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IHM+hKkzu0iRkhJhMYPToLRTJVZnjTDvkjDWJP577ZHM3Xj2xtnG8VXta123mRIUx6Q9Q9Q X-Received: by 2002:ac8:5a92:0:b0:42a:524d:fd0f with SMTP id c18-20020ac85a92000000b0042a524dfd0fmr1518398qtc.92.1706277399215; Fri, 26 Jan 2024 05:56:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706277399; cv=pass; d=google.com; s=arc-20160816; b=iFj5lENw0qN8a3lYjq+UQrYBmJT7G0qIg1s0JC0YGYgimFRvAJ5z5hL5xGecPRxYXz aFOe0ll4AVsF3Q/e5gwScMTO3qrYoq1bSNj/77RizlWjjx1SoD+r8r9ZrWAvkhIAr+0b r64ekGqxMKzbBpg+/4ElzjNPL2OnI3unLYZB6PicswBLMkucMkdtimqFafHSJpc5tIcB 2P9UabrRVH4twl2Cl6cWvXEc00fbpHaWyBuMShtgp7mUUCX299jpuqbCEyCLtwE4TOXS un1LSdu7Xvm6R7rz/4n3phS6e0qZjiJlGTR5MV4pIL4R1B7sZUjfq0cVneMwxeGgA46r 1aug== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=lHfmJRCwpYw8H23afBLhLnSr3onazkJPUqwODuZvV/A=; fh=eTd7umz5bDwrA2O8RT65r37r0t9PpqaUSn0Me2ng9gY=; b=HRmXgzZRZxM+vf5u51B6Iv5KGTaBEWfOEprVP7MCqcj5qOwZ3xVM1bDouL1ivhBAcl PY7Ziud8NZ5L3OQd4Qpwc4W3Bo1akQbQXcGPXt8Cp0LfiWJRk1VDxUer2mkgvZItSRrd UnsR+KsToYtuJoyqHaxumlf4F34HKFf3u7g9mYdpm0oks2p1YpXeKV/SRBpuDYjsnEZu pDzumTXuHcFDHK2V5glMx9ojB7pTm3vJGi87hcuw5u+8K6tWB9KmoMqQY79pnbYXCBjH 9NcuWYlqAycTbhUDdOhnYUVN0k6hEznv1wAcEjXdQzMp/8G5GM3U+5wHt7Ut8qkcfcOe cHwg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=c4CaJMz0; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-40150-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40150-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id h21-20020ac87d55000000b0042a6340c662si1364551qtb.96.2024.01.26.05.56.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 05:56:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-40150-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=c4CaJMz0; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-40150-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40150-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id EE6E01C231B2 for ; Fri, 26 Jan 2024 13:56:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AEB741BF55; Fri, 26 Jan 2024 13:56:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="c4CaJMz0" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADA531BDD7; Fri, 26 Jan 2024 13:55:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706277360; cv=none; b=HNA6NyqHqSn26Zn1W7O/9fXHaVjv+lsQCM2TJIlIbPENHrh2n/4kEdmLa+v0EMNW1ZWqGjxyxzQ3O18M3LLq3uMOYNHI4vduM+Tf7YbPEfNqxxsui3RASheDg58UegVHysKId+r1cOl1myWaPi4VxYWRAttbSeW86iVxJTrSqAI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706277360; c=relaxed/simple; bh=0Fwr/dcYEqlykVNBuyY0nc0tFWCDTHupBFPwfuK4dXU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MpS5uJg4F5M8oMdrwYiZEjuE5xJr7Ui5ydxucBIGhsUXI09xDCdkTgOcah8vQD+kCzRpD6gxlF88fOFZNVLhOUE4KY0o8R3nSeEt4/niJkuK+1uxJNraHD1GOUbxEF3ZD8xjHmAkZbpA1ZLoHc5AGqMdgqluizAxvlojhVimHZk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=c4CaJMz0; arc=none smtp.client-ip=192.198.163.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706277358; x=1737813358; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0Fwr/dcYEqlykVNBuyY0nc0tFWCDTHupBFPwfuK4dXU=; b=c4CaJMz0m7b/wgi08Z5caxLDwhHVwBmZXxGW51SyAqUOHTiZp1bQa2X6 6RMiizBjOI2c+RDFUSrYN/hJpKD7fY37Pbxmptc0/FQ5ihhFdP2dofCMC +9dnQW79fNSk82B1j/lfG5xIR6ohkV7fqrDEsfnN1cLsxV9PUxOyYyznQ ohFdnoQ2PV58rs9Y1FSAAhrzTY/nXGKpkpJ93kQVIJzgq9ckrxCOwK2wu d3U+GXpH5e0zVAxWUBVH/q+GjusGVH0ONYp/zPjjrK8f/RH2LdblVQIWZ joQFEjs5CazGREXHIfjinN7WWSv7Kat1TBazmEvjOHtme+G9Oev7fjkAl Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="15998414" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="15998414" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jan 2024 05:55:57 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="821142842" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="821142842" Received: from newjersey.igk.intel.com ([10.102.20.203]) by orsmga001.jf.intel.com with ESMTP; 26 Jan 2024 05:55:52 -0800 From: Alexander Lobakin To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Alexander Lobakin , Christoph Hellwig , Marek Szyprowski , Robin Murphy , Joerg Roedel , Will Deacon , Greg Kroah-Hartman , "Rafael J. Wysocki" , Magnus Karlsson , Maciej Fijalkowski , Alexander Duyck , bpf@vger.kernel.org, netdev@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH net-next 1/7] dma: compile-out DMA sync op calls when not used Date: Fri, 26 Jan 2024 14:54:50 +0100 Message-ID: <20240126135456.704351-2-aleksander.lobakin@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240126135456.704351-1-aleksander.lobakin@intel.com> References: <20240126135456.704351-1-aleksander.lobakin@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789161530302479730 X-GMAIL-MSGID: 1789161530302479730 Some platforms do have DMA, but DMA there is always direct and coherent. Currently, even on such platforms DMA sync operations are compiled and called. Add a new hidden Kconfig symbol, DMA_NEED_SYNC, and set it only when either sync operations are needed or there is DMA ops or swiotlb enabled. Set dma_need_sync() and dma_skip_sync() (stub for now) depending on this symbol state and don't call sync ops when dma_skip_sync() is true. The change allows for future optimizations of DMA sync calls depending on compile-time or runtime conditions. Signed-off-by: Alexander Lobakin --- kernel/dma/Kconfig | 4 ++ include/linux/dma-mapping.h | 92 +++++++++++++++++++++++++------------ kernel/dma/mapping.c | 26 ++++++----- 3 files changed, 81 insertions(+), 41 deletions(-) diff --git a/kernel/dma/Kconfig b/kernel/dma/Kconfig index d62f5957f36b..1c9ff05b1ecb 100644 --- a/kernel/dma/Kconfig +++ b/kernel/dma/Kconfig @@ -107,6 +107,10 @@ config DMA_BOUNCE_UNALIGNED_KMALLOC bool depends on SWIOTLB +config DMA_NEED_SYNC + def_bool ARCH_HAS_SYNC_DMA_FOR_DEVICE || ARCH_HAS_SYNC_DMA_FOR_CPU || \ + ARCH_HAS_SYNC_DMA_FOR_CPU_ALL || DMA_OPS || SWIOTLB + config DMA_RESTRICTED_POOL bool "DMA Restricted Pool" depends on OF && OF_RESERVED_MEM && SWIOTLB diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 4a658de44ee9..9dd7e1578bf6 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -117,14 +117,14 @@ dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr, size_t size, enum dma_data_direction dir, unsigned long attrs); void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size, enum dma_data_direction dir, unsigned long attrs); -void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size, - enum dma_data_direction dir); -void dma_sync_single_for_device(struct device *dev, dma_addr_t addr, - size_t size, enum dma_data_direction dir); -void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, - int nelems, enum dma_data_direction dir); -void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, - int nelems, enum dma_data_direction dir); +void __dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, + size_t size, enum dma_data_direction dir); +void __dma_sync_single_for_device(struct device *dev, dma_addr_t addr, + size_t size, enum dma_data_direction dir); +void __dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, + int nelems, enum dma_data_direction dir); +void __dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, + int nelems, enum dma_data_direction dir); void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flag, unsigned long attrs); void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr, @@ -147,7 +147,6 @@ u64 dma_get_required_mask(struct device *dev); bool dma_addressing_limited(struct device *dev); size_t dma_max_mapping_size(struct device *dev); size_t dma_opt_mapping_size(struct device *dev); -bool dma_need_sync(struct device *dev, dma_addr_t dma_addr); unsigned long dma_get_merge_boundary(struct device *dev); struct sg_table *dma_alloc_noncontiguous(struct device *dev, size_t size, enum dma_data_direction dir, gfp_t gfp, unsigned long attrs); @@ -195,20 +194,24 @@ static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size, enum dma_data_direction dir, unsigned long attrs) { } -static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, - size_t size, enum dma_data_direction dir) +static inline void __dma_sync_single_for_cpu(struct device *dev, + dma_addr_t addr, size_t size, + enum dma_data_direction dir) { } -static inline void dma_sync_single_for_device(struct device *dev, - dma_addr_t addr, size_t size, enum dma_data_direction dir) +static inline void __dma_sync_single_for_device(struct device *dev, + dma_addr_t addr, size_t size, + enum dma_data_direction dir) { } -static inline void dma_sync_sg_for_cpu(struct device *dev, - struct scatterlist *sg, int nelems, enum dma_data_direction dir) +static inline void __dma_sync_sg_for_cpu(struct device *dev, + struct scatterlist *sg, int nelems, + enum dma_data_direction dir) { } -static inline void dma_sync_sg_for_device(struct device *dev, - struct scatterlist *sg, int nelems, enum dma_data_direction dir) +static inline void __dma_sync_sg_for_device(struct device *dev, + struct scatterlist *sg, int nelems, + enum dma_data_direction dir) { } static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr) @@ -277,10 +280,6 @@ static inline size_t dma_opt_mapping_size(struct device *dev) { return 0; } -static inline bool dma_need_sync(struct device *dev, dma_addr_t dma_addr) -{ - return false; -} static inline unsigned long dma_get_merge_boundary(struct device *dev) { return 0; @@ -348,20 +347,55 @@ static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr, return dma_unmap_page_attrs(dev, addr, size, dir, attrs); } -static inline void dma_sync_single_range_for_cpu(struct device *dev, - dma_addr_t addr, unsigned long offset, size_t size, - enum dma_data_direction dir) +static inline void +__dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t addr, + unsigned long offset, size_t size, + enum dma_data_direction dir) { - return dma_sync_single_for_cpu(dev, addr + offset, size, dir); + __dma_sync_single_for_cpu(dev, addr + offset, size, dir); } -static inline void dma_sync_single_range_for_device(struct device *dev, - dma_addr_t addr, unsigned long offset, size_t size, - enum dma_data_direction dir) +static inline void +__dma_sync_single_range_for_device(struct device *dev, dma_addr_t addr, + unsigned long offset, size_t size, + enum dma_data_direction dir) { - return dma_sync_single_for_device(dev, addr + offset, size, dir); + __dma_sync_single_for_device(dev, addr + offset, size, dir); } +#ifdef CONFIG_DMA_NEED_SYNC + +#define dma_skip_sync(dev) false + +bool dma_need_sync(struct device *dev, dma_addr_t dma_addr); + +#else /* !CONFIG_DMA_NEED_SYNC */ + +#define dma_skip_sync(dev) true +#define dma_need_sync(dev, dma_addr) false + +#endif /* !CONFIG_DMA_NEED_SYNC */ + +#define dma_check_sync(op, dev, ...) \ + do { \ + if (!dma_skip_sync(dev)) \ + op(dev, __VA_ARGS__); \ + } while (0) + +#define dma_sync_single_for_cpu(d, a, s, r) \ + dma_check_sync(__dma_sync_single_for_cpu, d, a, s, r) +#define dma_sync_single_for_device(d, a, s, r) \ + dma_check_sync(__dma_sync_single_for_device, d, a, s, r) +#define dma_sync_sg_for_cpu(d, s, n, r) \ + dma_check_sync(__dma_sync_sg_for_cpu, d, s, n, r) +#define dma_sync_sg_for_device(d, s, n, r) \ + dma_check_sync(__dma_sync_sg_for_device, d, s, n, r) + +#define dma_sync_single_range_for_cpu(d, a, o, s, r) \ + dma_check_sync(__dma_sync_single_range_for_cpu, d, a, o, s, r) +#define dma_sync_single_range_for_device(d, a, o, s, r) \ + dma_check_sync(__dma_sync_single_range_for_device, d, a, o, s, r) + /** * dma_unmap_sgtable - Unmap the given buffer for DMA * @dev: The device for which to perform the DMA operation diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 58db8fd70471..a30f37f9d4db 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -329,8 +329,8 @@ void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size, } EXPORT_SYMBOL(dma_unmap_resource); -void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size, - enum dma_data_direction dir) +void __dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, + size_t size, enum dma_data_direction dir) { const struct dma_map_ops *ops = get_dma_ops(dev); @@ -341,10 +341,10 @@ void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size, ops->sync_single_for_cpu(dev, addr, size, dir); debug_dma_sync_single_for_cpu(dev, addr, size, dir); } -EXPORT_SYMBOL(dma_sync_single_for_cpu); +EXPORT_SYMBOL(__dma_sync_single_for_cpu); -void dma_sync_single_for_device(struct device *dev, dma_addr_t addr, - size_t size, enum dma_data_direction dir) +void __dma_sync_single_for_device(struct device *dev, dma_addr_t addr, + size_t size, enum dma_data_direction dir) { const struct dma_map_ops *ops = get_dma_ops(dev); @@ -355,10 +355,10 @@ void dma_sync_single_for_device(struct device *dev, dma_addr_t addr, ops->sync_single_for_device(dev, addr, size, dir); debug_dma_sync_single_for_device(dev, addr, size, dir); } -EXPORT_SYMBOL(dma_sync_single_for_device); +EXPORT_SYMBOL(__dma_sync_single_for_device); -void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, - int nelems, enum dma_data_direction dir) +void __dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, + int nelems, enum dma_data_direction dir) { const struct dma_map_ops *ops = get_dma_ops(dev); @@ -369,10 +369,10 @@ void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, ops->sync_sg_for_cpu(dev, sg, nelems, dir); debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir); } -EXPORT_SYMBOL(dma_sync_sg_for_cpu); +EXPORT_SYMBOL(__dma_sync_sg_for_cpu); -void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, - int nelems, enum dma_data_direction dir) +void __dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, + int nelems, enum dma_data_direction dir) { const struct dma_map_ops *ops = get_dma_ops(dev); @@ -383,7 +383,7 @@ void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, ops->sync_sg_for_device(dev, sg, nelems, dir); debug_dma_sync_sg_for_device(dev, sg, nelems, dir); } -EXPORT_SYMBOL(dma_sync_sg_for_device); +EXPORT_SYMBOL(__dma_sync_sg_for_device); /* * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems @@ -841,6 +841,7 @@ size_t dma_opt_mapping_size(struct device *dev) } EXPORT_SYMBOL_GPL(dma_opt_mapping_size); +#ifdef CONFIG_DMA_NEED_SYNC bool dma_need_sync(struct device *dev, dma_addr_t dma_addr) { const struct dma_map_ops *ops = get_dma_ops(dev); @@ -850,6 +851,7 @@ bool dma_need_sync(struct device *dev, dma_addr_t dma_addr) return ops->sync_single_for_cpu || ops->sync_single_for_device; } EXPORT_SYMBOL_GPL(dma_need_sync); +#endif /* CONFIG_DMA_NEED_SYNC */ unsigned long dma_get_merge_boundary(struct device *dev) {