Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp231237lqg; Fri, 1 Mar 2024 03:42:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW647Lt6uviMpft1z7JOMpPYwkovgKxbaqIFslXZX+bPF6zw/JyBoyEvvnMs0zLU24mejo4EauEH5HVkEaj9IjZSzKYyexuPcnYhIrnlA== X-Google-Smtp-Source: AGHT+IElzbA3IBNJQgbnjqGkqcHsNLB7QZawvZ1lUyE48N/dVM7SPDtbMTqP8068BDDZTazZnoWl X-Received: by 2002:a05:6870:c0d:b0:21f:d201:6d6b with SMTP id le13-20020a0568700c0d00b0021fd2016d6bmr1394331oab.20.1709293347018; Fri, 01 Mar 2024 03:42:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709293346; cv=pass; d=google.com; s=arc-20160816; b=LkS0Qf0qcEGACrefw3zyR1Zgdw3sufcJKPkPr3sDJM2K8DtSC0OEboiZsDCaiMJAgF 0SkyV4iDcAiKMFuwcRLATIzuhQciOyB4Xa99AEaE8XBThOFFUAo8/OB7ghSnVMCWhEmc ILXRB+rlnr5i2QccIJundjQOtQEJOtnilctl1gHEFheXTvy54Qaah+hi+ppuTwaWcwWR 1ciQZE4hqXdSHZFi0gxTu2+9X6OQdMD6KsAkVCjOFUcFhQ7UtX0tymrG4ULxMkEJiMB/ ailVa2wAZKJqqEBmmnxbx6y+jaL8PTBEbWilOfQ7xYV+UEF5lR0KenxHh0D/bPTOD6MF i2FA== 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:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=cI1cCBRKUJxIcdsWEySGTeo5Auol3zCg7kf3wejhCGg=; fh=NLWOzyeiHvMVXkCZ56Ck5MGJlazP9cG84+dVA20SPJU=; b=dBGftiTmTEP1M55G4B3iwbjGxRnpuedk4PBWU8nVhNLYqpAS0Uk3H+nQ6Wtgt53kHP 2yzNVM1lvzAkTH//1+XvfOHyiHi79bhNqgKtodMbeTduG3rLuUCQ+1ySfA73SOM8qOFF zpU/rK1B0Voy3JO8sDv5MwbkQzQTIxedljZqIIoJeGzfV9tqfPmO4eE6WECu5WTuGFkM MhIYChchf00vt8wtmEkEwCcRzlEH5a2osftmaL6DQMaChJPX0zNAW870CUhjjtPSaX/c WqM67H2YwEyKj9hfzZr+J854qcmIliyZ2XuEos86FkBbVemvi0zFatjnp/C5YrgmfCNm 1uZA==; 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-88369-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88369-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id f11-20020a631f0b000000b005dc956c2c09si3425723pgf.147.2024.03.01.03.42.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 03:42:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88369-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; 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-88369-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88369-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 28D09B24BE0 for ; Fri, 1 Mar 2024 11:38:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 048676CDC3; Fri, 1 Mar 2024 11:38:45 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CCEA66A8AD for ; Fri, 1 Mar 2024 11:38:41 +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=1709293124; cv=none; b=a0yfrXwryrPn6sjSACOTo9mjuQ47ikgpSEHaSskO0yZvLAdo9jUm1nzQEyHCJtJWOuezVOf3O0kGUahNuUStQHT3JprYAfs9c6jeeb+nNAkssQZl1vjtMM3yLS8js0gnyIgFrDO90eGVi5g0zMpB+muId+eK4xaLvUxvPy7rruM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709293124; c=relaxed/simple; bh=Bho0V3L6EalNqI/VaFRZI239nh8uqJQHHjMpb9j/96I=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=N1mSJHmZx0oGOyzdEdUpjPc46LTx2C2qk4yFFDO4TsmnMS1fBiWLZVZJrEE2kknm/t+R/GuNcAt7E6dRHcjwpmoq9FUcF9ga/ES82B0CvtQ2DWdeR4zuh/L5SvA8q1pvC7FI+eZJMojwqZnkuwTwYnODicS7oOa1xjHvyKFFLbk= 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 714EB1FB; Fri, 1 Mar 2024 03:39:19 -0800 (PST) Received: from [10.57.67.78] (unknown [10.57.67.78]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B9F1E3F6C4; Fri, 1 Mar 2024 03:38:39 -0800 (PST) Message-ID: <64be2e23-c526-45d3-bb7b-29e31241bbef@arm.com> Date: Fri, 1 Mar 2024 11:38:25 +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: [RFC] dma-mapping: introduce dma_can_skip_unmap() To: Xuan Zhuo , linux-kernel@vger.kernel.org Cc: Joerg Roedel , Will Deacon , Christoph Hellwig , Marek Szyprowski , iommu@lists.linux.dev, "Michael S. Tsirkin" , Zelin Deng References: <20240301071918.64631-1-xuanzhuo@linux.alibaba.com> Content-Language: en-GB From: Robin Murphy In-Reply-To: <20240301071918.64631-1-xuanzhuo@linux.alibaba.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2024-03-01 7:19 am, Xuan Zhuo wrote: > In a typical workflow, we first perform a dma map on an address to > obtain a dma address, followed by a dma unmap on that address. Generally, > this process works without issues. However, under certain circumstances, > we require additional resources to manage these dma addresses. For > instance, in layered architectures, we pass the dma address to another > module, but retrieving it back from that module can present some > challenges. In such cases, we must allocate extra resources to manage > these dma addresses. > > However, considering that many times the dma unmap operation is actually > a no-op, if we know in advance that unmap is not necessary, we can save > on these extra management overheads. Moreover, we can directly skip the > dma unmap operation. This would be advantageous. > > This tries to resolve the problem of patchset: > > http://lore.kernel.org/all/20240225032330-mutt-send-email-mst@kernel.org > > For a single packet, virtio-net may submit 1-19 dma addresses to virtio > core. If the virtio-net maintains the dma addresses will waste too much > memory when the unmap is not necessary. If the virtio-net retrieves the > dma addresses of the packet from the virtio core, we need to hold the 19 > dma addresses by one call. And the net drivers maintain the dma is the > future. So we hope to check the unmap is necessary or not. > > Co-developed-by: Zelin Deng > Signed-off-by: Zelin Deng > Signed-off-by: Xuan Zhuo > --- > drivers/iommu/dma-iommu.c | 11 +++++++++++ > include/linux/dma-map-ops.h | 1 + > include/linux/dma-mapping.h | 5 +++++ > kernel/dma/mapping.c | 23 +++++++++++++++++++++++ > 4 files changed, 40 insertions(+) > > diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c > index 50ccc4f1ef81..8c661a0e1111 100644 > --- a/drivers/iommu/dma-iommu.c > +++ b/drivers/iommu/dma-iommu.c > @@ -1706,6 +1706,16 @@ static size_t iommu_dma_opt_mapping_size(void) > return iova_rcache_range(); > } > > +static bool iommu_dma_opt_can_skip_unmap(struct device *dev) > +{ > + struct iommu_domain *domain = iommu_get_dma_domain(dev); > + > + if (domain->type == IOMMU_DOMAIN_IDENTITY) This is nonsense; iommu-dma does not operate on identity domains in the first place. > + return true; > + else > + return false; > +} > + > static const struct dma_map_ops iommu_dma_ops = { > .flags = DMA_F_PCI_P2PDMA_SUPPORTED, > .alloc = iommu_dma_alloc, > @@ -1728,6 +1738,7 @@ static const struct dma_map_ops iommu_dma_ops = { > .unmap_resource = iommu_dma_unmap_resource, > .get_merge_boundary = iommu_dma_get_merge_boundary, > .opt_mapping_size = iommu_dma_opt_mapping_size, > + .dma_can_skip_unmap = iommu_dma_opt_can_skip_unmap, > }; > > /* > diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h > index 4abc60f04209..d508fa90bc06 100644 > --- a/include/linux/dma-map-ops.h > +++ b/include/linux/dma-map-ops.h > @@ -83,6 +83,7 @@ struct dma_map_ops { > size_t (*max_mapping_size)(struct device *dev); > size_t (*opt_mapping_size)(void); > unsigned long (*get_merge_boundary)(struct device *dev); > + bool (*dma_can_skip_unmap)(struct device *dev); > }; > > #ifdef CONFIG_DMA_OPS > diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h > index 4a658de44ee9..af5d9275f8cc 100644 > --- a/include/linux/dma-mapping.h > +++ b/include/linux/dma-mapping.h > @@ -140,6 +140,7 @@ int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, > void *cpu_addr, dma_addr_t dma_addr, size_t size, > unsigned long attrs); > bool dma_can_mmap(struct device *dev); > +bool dma_can_skip_unmap(struct device *dev); > bool dma_pci_p2pdma_supported(struct device *dev); > int dma_set_mask(struct device *dev, u64 mask); > int dma_set_coherent_mask(struct device *dev, u64 mask); > @@ -249,6 +250,10 @@ static inline bool dma_can_mmap(struct device *dev) > { > return false; > } > +static inline bool dma_can_skip_unmap(struct device *dev) > +{ > + return false; > +} > static inline bool dma_pci_p2pdma_supported(struct device *dev) > { > return false; > diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c > index 58db8fd70471..99a81932820b 100644 > --- a/kernel/dma/mapping.c > +++ b/kernel/dma/mapping.c > @@ -445,6 +445,29 @@ bool dma_can_mmap(struct device *dev) > } > EXPORT_SYMBOL_GPL(dma_can_mmap); > > +/** > + * dma_can_skip_unmap - check if unmap can be skipped > + * @dev: device to check > + * > + * Returns %true if @dev supports direct map or dma_can_skip_unmap() return true. > + */ > +bool dma_can_skip_unmap(struct device *dev) > +{ > + const struct dma_map_ops *ops = get_dma_ops(dev); > + > + if (is_swiotlb_force_bounce(dev)) > + return false; > + > + if (dma_map_direct(dev, ops)) > + return true; And this is also broken and nonsensical. What about non-coherent cache maintenance, or regular non-forced SWIOTLB bouncing due to per-mapping address limitations or buffer alignment, or dma-debug? Not only is this idea not viable, the entire premise seems flawed - the reasons for virtio needing to use the DMA API at all are highly likely to be the same reasons for it needing to use the DMA API *properly* anyway. Thanks, Robin. > + > + if (ops->dma_can_skip_unmap) > + return ops->dma_can_skip_unmap(dev); > + > + return false; > +} > +EXPORT_SYMBOL_GPL(dma_can_skip_unmap); > + > /** > * dma_mmap_attrs - map a coherent DMA allocation into user space > * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices