Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp740890rdh; Wed, 14 Feb 2024 09:55:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXhTm3HSVJPL6S352QASSrCGVY8pqMgYAqjv5pBgq5NAnGrxmkXJRA1HdFcMuSY+6ZhYi4ZInxiZ8Q9WfDRe1x5n3YuU597mEfn61+cpg== X-Google-Smtp-Source: AGHT+IGhC+avgI/OJkoA5UdPF32Ly0O96Q1lRwmSQPRphqsZQNazeh0kSTn5R2Bb9PO8o/dYY5Eu X-Received: by 2002:a17:906:4a10:b0:a3d:1fad:e390 with SMTP id w16-20020a1709064a1000b00a3d1fade390mr2435196eju.59.1707933345454; Wed, 14 Feb 2024 09:55:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707933345; cv=pass; d=google.com; s=arc-20160816; b=dnApswtC8frE92T9reKWEqi+VsJ1P6uRbQozNTeAwScrT0RPauRszsxZ/MYU3lMEHx n7npsO3hYY3HtafdXoW89jKQEVtKqIqK5fiBBNo5TDEfFt40f7hhW4DEha+QoE3GZ3BJ x5/Cvyn7rZd07UwyphvvdveM7rtAvkKiSkcysrU1NPvfY3mdljPC4UTudN+qh+euNUsh vOuPi1sTc1zRWCBwXJUXLGoNEu03rmOrxk7jytjqj9/ZJk4x0/Ssa/y3qsn56HJyggpq FNyaLFHfEHk7QLrk09FqYCD4ex3cztXtdLMAMV8Tb3tjQe1g5sfEBWRZUHaH19akReWZ HYKQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=UwQb0pM0LFzf3PwI4z8XBIPAw4TBnsS97suW0uSIiS4=; fh=BrOJr4v/blkuIxnQZ41I9rT7E3s5QkAxCVmf82zb1Sk=; b=dizeXHs4Rk3aFsQL5EfdqpZrlrfgjxRK/GfFc1ktS7Kxlv02tYQG7l0Kd3CrpeoYOJ dZ1xSIoNsJJhDWckVy0X4sjrK2PaCksW9UslrGeg4IF6z6UJeeQwae9fm1Q4M8HF5872 dlr5QuWIw6gfO+QcjVwwFImr6s9UELVhGznu/NPrfp0kQvUlN4PPAAEt97gzbgja8ALv jtT6UdpxK4rAyXzbD4yvodkS3cu09pkkk6A+JPkCRJ6Cq28oaTWteyHIemigMl6gAzir Pdw8Q0awt+sKsUMQP6XN0006GW54Z9pUVxh1rrlmMaekLvR1HVcToVouN9Zv0XrCN4rF Wu9w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-65728-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65728-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com X-Forwarded-Encrypted: i=2; AJvYcCWkM1lDOiChE3zDbwPJsqcYQG4MCqhJa6+2uErKf+r70OpXvz7Q0xsNmH7wyGHgXG2rQwhGV2fCmOfYPTaQEga3y35enjAx5Q9bWd3yWQ== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id h25-20020a170906111900b00a3d24262df9si1281826eja.283.2024.02.14.09.55.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:55:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65728-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-65728-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65728-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 0A65B1F22B20 for ; Wed, 14 Feb 2024 17:55:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 83AE4127B7D; Wed, 14 Feb 2024 17:55:33 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4B9141170F; Wed, 14 Feb 2024 17:55:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707933332; cv=none; b=GJaqlaOjJ/R2j6IXv2WF7oC8s5oMKVIBljqooQIqxtko/ZQ+ikHOscBX2t4DT9gOaFyOpTIDgTdCndNGedbq8iGVPYBVIP5FJpIVOeCEUK+zuO7e781QuV0MbTjl23Ygn8HCG7wxki/3WnbRREXLllAEbukSyq6eJU4p3ZRoeXo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707933332; c=relaxed/simple; bh=Q+zwuCQCedgZ3MWerytsD0hvfHCUgL8H8cPvcaHYg0w=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=eOU/LRC8YqzaPEch0rPwdPv2A17hUgPK/tWQvL2S+UGy4mNq8f+NT60CZfUrP6V2PigbnKbZ57bd9LOJtx2gROvJl0SCNfrkLIfoVAGCaYZDAfAEWQrZHjRslhdqV+rKRKsxr6Kws2cWZUs9DZnr9CfNS5qPuuY1zSGTcJou2iI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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 5FC911FB; Wed, 14 Feb 2024 09:56:09 -0800 (PST) Received: from [10.57.47.86] (unknown [10.57.47.86]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1C2E73F766; Wed, 14 Feb 2024 09:55:24 -0800 (PST) Message-ID: <3a9dd580-1977-418f-a3f3-73003dd37710@arm.com> Date: Wed, 14 Feb 2024 17:55:23 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH net-next v3 2/7] dma: avoid redundant calls for sync operations Content-Language: en-GB To: Alexander Lobakin , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Christoph Hellwig , Marek Szyprowski , 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 References: <20240214162201.4168778-1-aleksander.lobakin@intel.com> <20240214162201.4168778-3-aleksander.lobakin@intel.com> From: Robin Murphy In-Reply-To: <20240214162201.4168778-3-aleksander.lobakin@intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2024-02-14 4:21 pm, Alexander Lobakin wrote: > Quite often, devices do not need dma_sync operations on x86_64 at least. > Indeed, when dev_is_dma_coherent(dev) is true and > dev_use_swiotlb(dev) is false, iommu_dma_sync_single_for_cpu() > and friends do nothing. > > However, indirectly calling them when CONFIG_RETPOLINE=y consumes about > 10% of cycles on a cpu receiving packets from softirq at ~100Gbit rate. > Even if/when CONFIG_RETPOLINE is not set, there is a cost of about 3%. > > Add dev->skip_dma_sync boolean which is set during the device > initialization depending on the setup: dev_is_dma_coherent() for the > direct DMA, !(sync_single_for_device || sync_single_for_cpu) or the new > dma_map_ops flag, %DMA_F_CAN_SKIP_SYNC, advertised for non-NULL DMA ops. > Then later, if/when swiotlb is used for the first time, the flag > is turned off, from swiotlb_tbl_map_single(). > > On iavf, the UDP trafficgen with XDP_DROP in skb mode test shows > +3-5% increase for direct DMA. > > Suggested-by: Christoph Hellwig # direct DMA shortcut > Co-developed-by: Eric Dumazet > Signed-off-by: Eric Dumazet > Signed-off-by: Alexander Lobakin > --- > include/linux/device.h | 5 +++++ > include/linux/dma-map-ops.h | 21 ++++++++++++++++++++ > include/linux/dma-mapping.h | 6 +++++- > drivers/base/dd.c | 2 ++ > kernel/dma/mapping.c | 39 ++++++++++++++++++++++++++++++++++++- > kernel/dma/swiotlb.c | 8 ++++++++ > 6 files changed, 79 insertions(+), 2 deletions(-) > > diff --git a/include/linux/device.h b/include/linux/device.h > index 97c4b046c09d..f23e6a32bea0 100644 > --- a/include/linux/device.h > +++ b/include/linux/device.h > @@ -686,6 +686,8 @@ struct device_physical_location { > * other devices probe successfully. > * @dma_coherent: this particular device is dma coherent, even if the > * architecture supports non-coherent devices. > + * @dma_skip_sync: DMA sync operations can be skipped for coherent non-SWIOTLB > + * buffers. > * @dma_ops_bypass: If set to %true then the dma_ops are bypassed for the > * streaming DMA operations (->map_* / ->unmap_* / ->sync_*), > * and optionall (if the coherent mask is large enough) also > @@ -800,6 +802,9 @@ struct device { > defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) > bool dma_coherent:1; > #endif > +#ifdef CONFIG_DMA_NEED_SYNC > + bool dma_skip_sync:1; > +#endif > #ifdef CONFIG_DMA_OPS_BYPASS > bool dma_ops_bypass : 1; > #endif > diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h > index 4abc60f04209..327b73f653ad 100644 > --- a/include/linux/dma-map-ops.h > +++ b/include/linux/dma-map-ops.h > @@ -18,8 +18,11 @@ struct iommu_ops; > * > * DMA_F_PCI_P2PDMA_SUPPORTED: Indicates the dma_map_ops implementation can > * handle PCI P2PDMA pages in the map_sg/unmap_sg operation. > + * DMA_F_CAN_SKIP_SYNC: DMA sync operations can be skipped if the device is > + * coherent and it's not an SWIOTLB buffer. > */ > #define DMA_F_PCI_P2PDMA_SUPPORTED (1 << 0) > +#define DMA_F_CAN_SKIP_SYNC BIT(1) Yuck, please be consistent - either match the style of the existing code, or change that to BIT(0) as well. > struct dma_map_ops { > unsigned int flags; > @@ -111,6 +114,24 @@ static inline void set_dma_ops(struct device *dev, > } > #endif /* CONFIG_DMA_OPS */ > > +#ifdef CONFIG_DMA_NEED_SYNC > +void dma_setup_skip_sync(struct device *dev); > + > +static inline void dma_clear_skip_sync(struct device *dev) > +{ > + /* Clear it only once so that the function can be called on hotpath */ > + if (unlikely(dev->dma_skip_sync)) > + dev->dma_skip_sync = false; > +} > +#else /* !CONFIG_DMA_NEED_SYNC */ > +static inline void dma_setup_skip_sync(struct device *dev) > +{ > +} > +static inline void dma_clear_skip_sync(struct device *dev) > +{ > +} > +#endif /* !CONFIG_DMA_NEED_SYNC */ > + > #ifdef CONFIG_DMA_CMA > extern struct cma *dma_contiguous_default_area; > > diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h > index 6c7640441214..d85ae541c267 100644 > --- a/include/linux/dma-mapping.h > +++ b/include/linux/dma-mapping.h > @@ -364,7 +364,11 @@ static inline void __dma_sync_single_range_for_device(struct device *dev, > > static inline bool dma_skip_sync(const struct device *dev) > { > - return !IS_ENABLED(CONFIG_DMA_NEED_SYNC); > +#ifdef CONFIG_DMA_NEED_SYNC > + return dev->dma_skip_sync; > +#else > + return true; > +#endif > } > > static inline bool dma_need_sync(struct device *dev, dma_addr_t dma_addr) > diff --git a/drivers/base/dd.c b/drivers/base/dd.c > index 85152537dbf1..67ad3e1d51f6 100644 > --- a/drivers/base/dd.c > +++ b/drivers/base/dd.c > @@ -642,6 +642,8 @@ static int really_probe(struct device *dev, struct device_driver *drv) > goto pinctrl_bind_failed; > } > > + dma_setup_skip_sync(dev); > + > ret = driver_sysfs_add(dev); > if (ret) { > pr_err("%s: driver_sysfs_add(%s) failed\n", > diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c > index 85feaa0e008c..5f588e31ea89 100644 > --- a/kernel/dma/mapping.c > +++ b/kernel/dma/mapping.c > @@ -846,8 +846,14 @@ bool __dma_need_sync(struct device *dev, dma_addr_t dma_addr) > const struct dma_map_ops *ops = get_dma_ops(dev); > > if (dma_map_direct(dev, ops)) > + /* > + * dma_skip_sync could've been set to false on first SWIOTLB > + * buffer mapping, but @dma_addr is not necessary an SWIOTLB > + * buffer. In this case, fall back to more granular check. > + */ > return dma_direct_need_sync(dev, dma_addr); > - return ops->sync_single_for_cpu || ops->sync_single_for_device; > + > + return true; > } > EXPORT_SYMBOL_GPL(__dma_need_sync); > > @@ -861,3 +867,34 @@ unsigned long dma_get_merge_boundary(struct device *dev) > return ops->get_merge_boundary(dev); > } > EXPORT_SYMBOL_GPL(dma_get_merge_boundary); > + > +#ifdef CONFIG_DMA_NEED_SYNC > +void dma_setup_skip_sync(struct device *dev) > +{ > + const struct dma_map_ops *ops = get_dma_ops(dev); > + > + if (dma_map_direct(dev, ops)) For DMA_OPS_BYPASS this will be making the decision based on the default dma_mask, but a driver could subsequently set a smaller mask for which the bypass condition will no longer be true. Maybe instead of driver probe this setup should actually be tied in to dma_set_mask() anyway? > + /* > + * dma_skip_sync will be set to false on first SWIOTLB buffer > + * mapping, if any. During the device initialization, it's > + * enough to check only for DMA coherence. > + */ > + dev->dma_skip_sync = dev_is_dma_coherent(dev); > + else if (!ops->sync_single_for_device && !ops->sync_single_for_cpu) I guess this was the existing condition from dma_need_sync(), but now it's on a one-off slow path it might be nice to check the sync_sg_* ops as well for completeness, or at least comment that nobody should be implementing those without also implementing the sync_single_* ops. > + /* > + * Synchronization is not possible when none of DMA sync ops > + * is set. This check precedes the below one as it disables > + * the synchronization unconditionally. > + */ > + dev->dma_skip_sync = true; > + else if (ops->flags & DMA_F_CAN_SKIP_SYNC) Personally I'd combine this into the dma-direct condition. > + /* > + * Assume that when ``DMA_F_CAN_SKIP_SYNC`` is advertised, > + * the conditions for synchronizing are the same as with > + * the direct DMA. > + */ > + dev->dma_skip_sync = dev_is_dma_coherent(dev); > + else > + dev->dma_skip_sync = false; > +} > +#endif /* CONFIG_DMA_NEED_SYNC */ > diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c > index b079a9a8e087..0b737eab4d48 100644 > --- a/kernel/dma/swiotlb.c > +++ b/kernel/dma/swiotlb.c > @@ -1323,6 +1323,12 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, > return (phys_addr_t)DMA_MAPPING_ERROR; > } > > + /* > + * If dma_skip_sync was set, reset it to false on first SWIOTLB buffer > + * mapping to always sync SWIOTLB buffers. > + */ > + dma_clear_skip_sync(dev); > + > /* > * Save away the mapping from the original address to the DMA address. > * This is needed when we sync the memory. Then we sync the buffer if > @@ -1640,6 +1646,8 @@ struct page *swiotlb_alloc(struct device *dev, size_t size) > if (index == -1) > return NULL; > > + dma_clear_skip_sync(dev); We don't need this here, since this isn't a streaming API path. Thanks, Robin. > + > tlb_addr = slot_addr(pool->start, index); > > return pfn_to_page(PFN_DOWN(tlb_addr));