Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1626249pxy; Mon, 2 Aug 2021 06:32:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxCvXZcK1UhD0g2ouXAkHR7vm+6ZPp+xzVb7kqi9c7gI38Cyb7eEeXe82d/kMxIKFAU4UD X-Received: by 2002:a5d:8b51:: with SMTP id c17mr2036030iot.119.1627911126541; Mon, 02 Aug 2021 06:32:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627911126; cv=none; d=google.com; s=arc-20160816; b=qzq1HUI5JQjPL6GKw00x54o0uKACjwWwbLu+3zGiZ/bw3FJE9tagAdEjAFIMLBMwAp tuJXjG0PKjmMAEx+eZwF8jGNFi+2eHHyTc4VKJ1gHiF/QSWjlDmtXFhpOlB7wd4+owTI OzvWzMr126n4ha6svcT4wtvhlfcZ7THyNbUHTy8eJTITRZ37927rf1wFPqrwpuoCKV63 yY0Qy0QLOm3gMFn855AGiAeGbcLKhywKUMiTxOQux7Qfc0EoGKkdPDgMqklMGou3bCYV ENyGcQcbmaT3fqb0fEskKiZGgcppBG2MgUFGrqs4UesSUtnMEdgnK8KFIS9eL/fEgRTW Xytw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=et0rX0KsvM5c7WKBWwl8DnxIZ+Jyc0w13Ya+5XhGGvs=; b=U/brNOoqvPjJIIyA31uR6yHPL+od3pRHnAIZ7pwZmJp/K4LNqDJING4aA68Jyt1Gt9 4F34tZlIiVjFXY4yefNF132QvVkprxP+rd6kaZlffYcvxlrjoxNXOt7HK+9Dgiv94HDI bBAI6nsc9DcZ7+e7cpjNi9DBBN093FLe6cmDpmeu4jycO8T0+fOQV0KUok3IsUrnDTyH sgYRxuxy1RM2xhSFoKptUilhz8so2FyVxQwQfUcY4TBd1zfff047ZNkwyDf/7zuo8vRM qxlmyQm8Wc7RgibMdSGqlLUDtwpXcEO24yv9XLwlrRnirCdsUUEL7/P6mLn3WK1/VO/B Zmww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nHwL3Xng; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w17si14068884jad.27.2021.08.02.06.31.54; Mon, 02 Aug 2021 06:32:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nHwL3Xng; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233960AbhHBNaw (ORCPT + 99 others); Mon, 2 Aug 2021 09:30:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:46312 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233867AbhHBNav (ORCPT ); Mon, 2 Aug 2021 09:30:51 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5381260F6D; Mon, 2 Aug 2021 13:30:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1627911042; bh=AopIBg3UQuNzGUn6zHPTl2RUpxi1CeIiSyyBhV3W0IU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=nHwL3XngEiLQt4Didk/zSnyxzEHhLvI/h87NYzxgJW8aV9nergHLrRivaMg8Z2AZO 8kuEw4+FZosbZe4uikBvFP1Sml89L/BSeuj5Ff25UAMW6Pcy7EYLRaZp/DGE9vk/Q3 xr2oL845xDL4K22oGmVfyU0UzhLgn1e1K8khfY9wEkWXOOG6qbBH+ZnjQ3/qqnk0GR WXPH12PIZS96YbDsPp9MnZp6cvrcg1r/6GlQq0HFHKMI0yyFCqvnI/99FWO93aoC5q +9Um+bb8RbypSIvULxhV0A5kejZrgqd8NNxdC4uS2WUXxGe8MAXKh0p/EIZPA13Hpc EN7cMXB+23UFQ== Date: Mon, 2 Aug 2021 14:30:38 +0100 From: Will Deacon To: David Stevens Cc: Robin Murphy , Joerg Roedel , Lu Baolu , Tom Murphy , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 1/4] dma-iommu: fix sync_sg with swiotlb Message-ID: <20210802133037.GB28547@willie-the-truck> References: <20210709033502.3545820-1-stevensd@google.com> <20210709033502.3545820-2-stevensd@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210709033502.3545820-2-stevensd@google.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 09, 2021 at 12:34:59PM +0900, David Stevens wrote: > From: David Stevens > > The is_swiotlb_buffer function takes the physical address of the swiotlb > buffer, not the physical address of the original buffer. The sglist > contains the physical addresses of the original buffer, so for the > sync_sg functions to work properly when a bounce buffer might have been > used, we need to use iommu_iova_to_phys to look up the physical address. > This is what sync_single does, so call that function on each sglist > segment. > > The previous code mostly worked because swiotlb does the transfer on map > and unmap. However, any callers which use DMA_ATTR_SKIP_CPU_SYNC with > sglists or which call sync_sg would not have had anything copied to the > bounce buffer. > > Fixes: 82612d66d51d ("iommu: Allow the dma-iommu api to use bounce buffers") > Signed-off-by: David Stevens > --- > drivers/iommu/dma-iommu.c | 26 +++++++++++++------------- > 1 file changed, 13 insertions(+), 13 deletions(-) > > diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c > index 7bcdd1205535..eac65302439e 100644 > --- a/drivers/iommu/dma-iommu.c > +++ b/drivers/iommu/dma-iommu.c > @@ -811,14 +811,14 @@ static void iommu_dma_sync_sg_for_cpu(struct device *dev, > if (dev_is_dma_coherent(dev) && !dev_is_untrusted(dev)) > return; > > - for_each_sg(sgl, sg, nelems, i) { > - if (!dev_is_dma_coherent(dev)) > - arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir); > - > - if (is_swiotlb_buffer(sg_phys(sg))) > + if (dev_is_untrusted(dev)) > + for_each_sg(sgl, sg, nelems, i) > + iommu_dma_sync_single_for_cpu(dev, sg_dma_address(sg), > + sg->length, dir); > + else > + for_each_sg(sgl, sg, nelems, i) > swiotlb_sync_single_for_cpu(dev, sg_phys(sg), > sg->length, dir); Doesn't this skip arch_sync_dma_for_cpu() for non-coherent trusted devices? Why not skip the extra dev_is_untrusted(dev) call here and just call iommu_dma_sync_single_for_cpu() for each entry regardless? Will