Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp3320024pxb; Sun, 29 Aug 2021 22:02:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxam7dUkBfnoryIhK2pN9GXjuw09L4Md8DsB8CsZqO50j3DwsjiFEOOgJzFdVsrIo+pan6+ X-Received: by 2002:aa7:c882:: with SMTP id p2mr13042621eds.46.1630299735502; Sun, 29 Aug 2021 22:02:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630299735; cv=none; d=google.com; s=arc-20160816; b=JqhPuKqVQVGtfl+SdV/Vq6JvVxSAautWibpql6EjbZhhbB2rJx+y9Dm3oZyApZFd5Y dtXivBLaC1nR5mY1qodMFztYVmmW0vqv3cVW46v9FkJIUZ/k7JzicD1N+axkiC/8NeuL LzGmBiSYcUTUpbaNFQNDKvASX3Xm9aOlCX6G9lqH6IvFrY3lvXOo9Bopda3sLddEH/1M a21YfEYKYvbC+5Qvx9yLXemFMVCD1PngIhkO1slT4t6f0ltcapbTpcMQfTl3o3pwJyOq GYOoNSXFcXACZLKCwYd4Z6U8qd0iPBqs4xNhqCO1PCcZAxqZvEv588+FNkBL08TTrogF zqzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ODCLmgJroklYClFaSFOowOn0wl0I5e9vA7yPkFVbffA=; b=A+9WoIrQ54YaTqYaHLQvKppwnfeCZQVkjd7QVqCNiaNBmmiXnKCtXphFt+AcMgT5mW XKceuELb1XvaWE6T47YBtj+2gX22M6eIYZF+Do579rnbzVCXnhhfX9MMb1ZXqcXwwpMm 0XM9sNTTu/9fu+D0PppvPhSbjKChaO1MfGG5JD4XaxryWuCPe5+Q5s6Eir+FiNOLvoLp ZXkN3EB5LjtVHIuGvc5CBph/+bsrU5GPlWbhI1rhVltQ1AL5Wl8WPCqlJ2+2xUhpflWr l+o2hbXdRBAcdOR7hWoWvXd4LQWYNTFDThygaN3ofArNRdmheeGESeTDRksYFok1hhQb 2iwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=fodwtW8S; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k3si12952500edi.285.2021.08.29.22.01.51; Sun, 29 Aug 2021 22:02:15 -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=@chromium.org header.s=google header.b=fodwtW8S; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231176AbhH3FBJ (ORCPT + 99 others); Mon, 30 Aug 2021 01:01:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231135AbhH3FBI (ORCPT ); Mon, 30 Aug 2021 01:01:08 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D992C06175F for ; Sun, 29 Aug 2021 22:00:15 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id t42so11199236pfg.12 for ; Sun, 29 Aug 2021 22:00:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ODCLmgJroklYClFaSFOowOn0wl0I5e9vA7yPkFVbffA=; b=fodwtW8SR+rX87U8RheOBcjS6kk7hqtO9du4VG2u0/cFxjS1wGatTh2nTkqLAmvLId +ZrhsgHDU6HUPtRjE6Li6gvThSIz3wtFgk7MMMmIxl6sywg9wPC0qRBvjEZ+XFfQDtc7 1RTJEi/JtOkFUpyov7DDvn1OPtTD3RxIRU10w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ODCLmgJroklYClFaSFOowOn0wl0I5e9vA7yPkFVbffA=; b=PZ2z/O0nRZRsDmx2ugI8rblzsuJCbeGOJJc1Pua8ewmBvSDpN3WlCBz9cqddjD/ZLB XbYN+zLhP7tLEhGLIoHp5nA3+mum6lMFWRRhlREXnY9+I6v407zaMeURQg0u4coCwXg+ +mqMNyXUHVY2PdVp5G2OCsXOfhIO4Dr1ZmQ3Y5nfl3Gz2kH+2xI+IQej+UzSUMOueDJ1 7nRNfa3swiBVEbj0mHnXwHp6i8fxCUzgfbh+HIJ7IQMbf5LyE8ictCWU/KxjnJ2ZxQJm qEvApfj9dloQs9YkFXeZreBe6bIEmOJTzKTVW1QjaG1J714eOoQvRmP8F7C22fmE/xbn iGRg== X-Gm-Message-State: AOAM531r/316Rk8+udBc/OGpJ5r9tm9Tx4r3WtgQ7gyPcX+YCk4/TXJe LeYfdpovztNU0Mk7W0KGp88zXQ== X-Received: by 2002:a62:1c4e:0:b0:3ee:7c8e:ce6 with SMTP id c75-20020a621c4e000000b003ee7c8e0ce6mr21281935pfc.60.1630299615053; Sun, 29 Aug 2021 22:00:15 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:28a5:1b9f:af1d:5542]) by smtp.gmail.com with UTF8SMTPSA id c124sm13579665pfc.216.2021.08.29.22.00.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 29 Aug 2021 22:00:14 -0700 (PDT) From: David Stevens X-Google-Original-From: David Stevens To: Robin Murphy , Christoph Hellwig Cc: Joerg Roedel , Will Deacon , Lu Baolu , Tom Murphy , Rajat Jain , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, David Stevens Subject: [PATCH v7 1/7] dma-iommu: fix sync_sg with swiotlb Date: Mon, 30 Aug 2021 13:59:19 +0900 Message-Id: <20210830045925.4163412-2-stevensd@google.com> X-Mailer: git-send-email 2.33.0.259.gc128427fd7-goog In-Reply-To: <20210830045925.4163412-1-stevensd@google.com> References: <20210830045925.4163412-1-stevensd@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Reviewed-by: Robin Murphy Reviewed-by: Christoph Hellwig --- drivers/iommu/dma-iommu.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index bac7370ead3e..d6ae87212768 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -827,17 +827,13 @@ static void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg; int i; - if (dev_is_dma_coherent(dev) && !dev_is_untrusted(dev)) - return; - - for_each_sg(sgl, sg, nelems, i) { - if (!dev_is_dma_coherent(dev)) + 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 if (!dev_is_dma_coherent(dev)) + for_each_sg(sgl, sg, nelems, i) arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir); - - if (is_swiotlb_buffer(sg_phys(sg))) - swiotlb_sync_single_for_cpu(dev, sg_phys(sg), - sg->length, dir); - } } static void iommu_dma_sync_sg_for_device(struct device *dev, @@ -847,17 +843,14 @@ static void iommu_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg; int i; - if (dev_is_dma_coherent(dev) && !dev_is_untrusted(dev)) - return; - - for_each_sg(sgl, sg, nelems, i) { - if (is_swiotlb_buffer(sg_phys(sg))) - swiotlb_sync_single_for_device(dev, sg_phys(sg), - sg->length, dir); - - if (!dev_is_dma_coherent(dev)) + if (dev_is_untrusted(dev)) + for_each_sg(sgl, sg, nelems, i) + iommu_dma_sync_single_for_device(dev, + sg_dma_address(sg), + sg->length, dir); + else if (!dev_is_dma_coherent(dev)) + for_each_sg(sgl, sg, nelems, i) arch_sync_dma_for_device(sg_phys(sg), sg->length, dir); - } } static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, -- 2.33.0.259.gc128427fd7-goog