From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 307B2C433FE for ; Wed, 29 Sep 2021 02:33:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0D675613E8 for ; Wed, 29 Sep 2021 02:33:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243812AbhI2Ce4 (ORCPT ); Tue, 28 Sep 2021 22:34:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243805AbhI2Cey (ORCPT ); Tue, 28 Sep 2021 22:34:54 -0400 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33E14C06161C for ; Tue, 28 Sep 2021 19:33:14 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id s16so715292pfk.0 for ; Tue, 28 Sep 2021 19:33:14 -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=oYJssbbh9oIZ4Q55TlprZpqnC9hYx+tSJnJso1kwE6c=; b=jc5yGWifw/bEG+KKwpjh+98D+XZf4CQpFi/ndAoa0m+9mpM3bMSvM092U1WnkdxhYs Pwnx8A0YtdoQgJX4/lMaEsv3izuloLOCvgwaEaePkNQ8kECUQgseIaSGsRNAHnAn+dPV ijc+dBe6a4oSsE8FalM2ML3RMOXI1LJphMpOg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oYJssbbh9oIZ4Q55TlprZpqnC9hYx+tSJnJso1kwE6c=; b=5rquQjhed81GHiYbTIUewp3ontMbhVdaQSpuoyOAYR4SQ3e2VmhBFgqcciRbbh/EVg 45mLkrqKjoaQbu/ppJAHO7HzORA1P7Gm3tbQI64dU/HbwxQDCefZv1ln/Mq0xPji5nu2 57VJm8J6NUShDHfXlAbHqWP+dRWKtR6lGddFwxfFSpUaUuO/EnH9F+KTgCA+mZDNjulH DILizGB8/sFD4o5j+9adyFfPDYN14CbEMhgJnQW8zfvVWfoaCKtlzFSwEdY8LfaPIbD6 f1VZ/EKEmJit0bbgLk4zjdT7z19AtFoY2OcJjQDeNwnTntK8ToC29o1+FzORu5SKZk0O r07w== X-Gm-Message-State: AOAM533WCj6STVJdoKHstp5F8ml1Ab5FZQ8GO2cFjAhAyVE5nEVWUlPg PoZKqYqw38mMPsy5V/qML58deZEw0iFhLw== X-Google-Smtp-Source: ABdhPJyMhhFrkKfbXUP+pi6UVNkCFEtOQRl5tTHRjOTFQlC0f190j31/W1gfbwks8ebulmfFejc4eg== X-Received: by 2002:a63:e551:: with SMTP id z17mr2559565pgj.203.1632882793801; Tue, 28 Sep 2021 19:33:13 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:f818:368:93ef:fa36]) by smtp.gmail.com with UTF8SMTPSA id y13sm491945pgc.46.2021.09.28.19.33.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 28 Sep 2021 19:33:13 -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 v8 1/7] dma-iommu: fix sync_sg with swiotlb Date: Wed, 29 Sep 2021 11:32:54 +0900 Message-Id: <20210929023300.335969-2-stevensd@google.com> X-Mailer: git-send-email 2.33.0.685.g46640cef36-goog In-Reply-To: <20210929023300.335969-1-stevensd@google.com> References: <20210929023300.335969-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 896bea04c347..c4d205b63c58 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -828,17 +828,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(dev, 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, @@ -848,17 +844,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(dev, 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.685.g46640cef36-goog