Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp303910lqh; Sat, 4 May 2024 01:53:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWWdeUvJ49EsCsJnvhe6yZ+++3T895uRKQWy+a0HygsOusxqjN1CNtpf65iYF+kkhy3Wp0Gdb3nT5ou1jMaxRKY0pBO6gFCvkFKnFsafg== X-Google-Smtp-Source: AGHT+IEc3TCSIqNUtCDdn5doGVm0FiUnHGMFW+5/M10U7fjihhXCgGMSX6QS94A7OeXDxRSeCqPn X-Received: by 2002:a17:903:230b:b0:1ea:6fb2:5a88 with SMTP id d11-20020a170903230b00b001ea6fb25a88mr6406580plh.68.1714812818386; Sat, 04 May 2024 01:53:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714812818; cv=pass; d=google.com; s=arc-20160816; b=BeDl1Q7sk9CbR2HwlQ53/TLiUMbp4xFSRwh19+Q4MIwdGS3vHIwyBIjwRffzgem40C kk/0WF2moFr3nH1eQyOX86HBZJcTUB0iZY3Pr6jsRLat/wDHN6+EIZfb094tWmcbLsCw 1d1sYbGAG5x1P0zxo55x+t0eyLAnzxS1lYQoAEY+YMfHYLZEkAPZszopArERAUxm0nwq /KCIL0nlsI5TPucAurvLom1RjHpTbtZI0kTBV2SLv9z6mC8Sx4mZWzbkYl7x6QOKDJyZ c4ys+bZrXTwKdI1xXn8+TnqbaYknAUTAZM8eR3cui4aT2McMxzKcUBslga6t3v52ZB1X 6AOQ== 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 :subject:cc:to:from:date:dkim-signature; bh=r8mY2kiUlttRaIUpK7q7YlOxdcrNiVnu7avba+IRVxI=; fh=vvxSh181BdXCYQGDV3JJxjLgOjfXThBiNFyXxY9Pt7Q=; b=q7j9gqegN36SIig1UjKN6SUrQt67rg3qnQMs8friKOVFsk+dKIH9iox0RnJeo7caFX kmSSQLbmB/Zk+409M3+/++vmxE7e6S7nxJLGHZ8wShTTSnrlTP7zF2mvbupSx3eV8r/C OxRWaLProywX+iNM7IY7Nj04T5Z1MRiAC9Y3VVo70f6r5pruYLcTLZbfgWf5/Xh1J4iQ VxfYcKoNr2C7/3GEqJbEiUsqbeunV0fC8E9aSRAfWYTB6KuWSkQ11fAndH9kLxwIG6RE 3GMvmNt/1yP7gGjvaKmKsNQy4EodVSpx68NAHY6I0kgzt++kLAJPcHKkS58WDMwvN8Cv zRIQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=degN7I7+; arc=pass (i=1 spf=pass spfdomain=tesarici.cz dkim=pass dkdomain=tesarici.cz dmarc=pass fromdomain=tesarici.cz); spf=pass (google.com: domain of linux-kernel+bounces-168560-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168560-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=tesarici.cz Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d3-20020a170903230300b001e29ac24838si4705788plh.177.2024.05.04.01.53.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 May 2024 01:53:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-168560-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=degN7I7+; arc=pass (i=1 spf=pass spfdomain=tesarici.cz dkim=pass dkdomain=tesarici.cz dmarc=pass fromdomain=tesarici.cz); spf=pass (google.com: domain of linux-kernel+bounces-168560-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168560-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=tesarici.cz 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 61F76B21BAA for ; Sat, 4 May 2024 08:53:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 01CC012E55; Sat, 4 May 2024 08:53:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tesarici.cz header.i=@tesarici.cz header.b="degN7I7+" Received: from bee.tesarici.cz (bee.tesarici.cz [37.205.15.56]) (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 0EE9B4C8D for ; Sat, 4 May 2024 08:53:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.205.15.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714812804; cv=none; b=k5EmlCvPlxjSi0R7BoH+FxBJCPMF+Jx8efk5tWvLFEonpMCSeZ4UXX2/0aJQtJ2TNqN3fERwVEYFdT2hImpwgY80axxzkyxf3nuqzTO6rl3xCQ59TFvchVTmF8/GGUnllF8erBca6dZ8y4RLIbdaqXgfAsStiw/lVEpTqyqqxaY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714812804; c=relaxed/simple; bh=F9WZ+7ipilzLdMlYfhOYLZqygvVOA423pQDFbzl/QuU=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AbV3vqOJ6BO5T322FVL8G+WUSpGexXQ6FPrv0GtNER+1bLFeDR9LlWK+ArOUe2WXja3TylEAOmKSbroDV9Yr5q17BbaDm2D5EGAoZdJ06IJOmvtKCOr3dj3it0kG1QSfMdWjviVKzjOA4hgNK1AoXKVBMHY/LCPj6dGTD9l3NxY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tesarici.cz; spf=pass smtp.mailfrom=tesarici.cz; dkim=pass (2048-bit key) header.d=tesarici.cz header.i=@tesarici.cz header.b=degN7I7+; arc=none smtp.client-ip=37.205.15.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tesarici.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tesarici.cz Received: from meshulam.tesarici.cz (dynamic-2a00-1028-83b8-1e7a-4427-cc85-6706-c595.ipv6.o2.cz [IPv6:2a00:1028:83b8:1e7a:4427:cc85:6706:c595]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bee.tesarici.cz (Postfix) with ESMTPSA id 31E2C1BA6F4; Sat, 4 May 2024 10:53:20 +0200 (CEST) Authentication-Results: mail.tesarici.cz; dmarc=fail (p=quarantine dis=none) header.from=tesarici.cz DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tesarici.cz; s=mail; t=1714812800; bh=r8mY2kiUlttRaIUpK7q7YlOxdcrNiVnu7avba+IRVxI=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=degN7I7+pspwb3mLYxb0ogmlx8ejRLy7D2Ev0bUul/M+eylJC6YK2M56xK6FjEBU2 wW6sEyvDaglmZHeggAAVR52WAMwIgekdTDY/SO0Diz/Q2oOk4FLWbY9Uhhr+OlziSe 04W0X3GVEM/bu/Ys/86JeMrIulIQP9QR8MWQ++foPGmb2iG7+vokqGGmNzdpr0GfVN UJP1tWZTr9ILXWzkO+b4dE+ZWr6liB8m81ZyY05k/nZ8Z2ABwanwNvosupQ4+Bx+Lc cDJuPLXvBVnVwvpp1nRoY5oSdMwr1IjMWBSHk64PfSDn/DT7tRqSXeyoKfNb2EuOmZ uieRPXRj+i8UQ== Date: Sat, 4 May 2024 10:53:19 +0200 From: Petr =?UTF-8?B?VGVzYcWZw61r?= To: "T.J. Mercier" Cc: Christoph Hellwig , Marek Szyprowski , Robin Murphy , isaacmanjarres@google.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH] dma-direct: Set SG_DMA_SWIOTLB flag for dma-direct Message-ID: <20240504105319.75eec54a@meshulam.tesarici.cz> In-Reply-To: <20240503183713.1557480-1-tjmercier@google.com> References: <20240503183713.1557480-1-tjmercier@google.com> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.41; x86_64-suse-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 3 May 2024 18:37:12 +0000 "T.J. Mercier" wrote: > As of commit 861370f49ce4 ("iommu/dma: force bouncing if the size is > not cacheline-aligned") sg_dma_mark_swiotlb is called when > dma_map_sgtable takes the IOMMU path and uses SWIOTLB for some portion > of a scatterlist. It is never set for the direct path, so drivers > cannot always rely on sg_dma_is_swiotlb to return correctly after > calling dma_map_sgtable. Fix this by calling sg_dma_mark_swiotlb in the > direct path like it is in the IOMMU path. > > Signed-off-by: T.J. Mercier > --- > kernel/dma/direct.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c > index 4d543b1e9d57..52f0dcb25ca2 100644 > --- a/kernel/dma/direct.c > +++ b/kernel/dma/direct.c > @@ -12,7 +12,7 @@ > #include > #include > #include > -#include > +#include > #include "direct.h" > > /* > @@ -497,6 +497,8 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, > goto out_unmap; > } > sg_dma_len(sg) = sg->length; > + if (is_swiotlb_buffer(dev, dma_to_phys(dev, sg->dma_address))) > + sg_dma_mark_swiotlb(sg); > } > > return nents; I'm not sure this does the right thing. IIUC when the scatterlist flags include SG_DMA_SWIOTLB, iommu_dma_sync_sg_for_*() will call iommu_dma_sync_single_for_*(), which in turn translates the DMA address to a physical address using iommu_iova_to_phys(). It seems to me that this function may not work correctly if there is no IOMMU, but it also seems to me that the scatterlist may contain such non-IOMMU addresses. I'm no expert, so correct DMA-to-physical translation might in fact be somehow implicitly guaranteed. If that's the case, could you explain it in the commit message, please? Petr T