Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp4082247pxt; Tue, 10 Aug 2021 19:46:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzv+zE9MLE6f6LP0toFu6P7po8d2FvCn8JOdanfUy/OVDmiFGBMZFmRhGJqIh66nloZIAJv X-Received: by 2002:a05:6402:214a:: with SMTP id bq10mr8642968edb.296.1628649976517; Tue, 10 Aug 2021 19:46:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628649976; cv=none; d=google.com; s=arc-20160816; b=rnvFzPLBeuNDH6b+KZeQyfNVSmdxI4vlzuOPv3FVdwu9XG7SVFBdQ7ZwomXCmvuNgP xuLtEF9qnuscLW0oFHQIi9rLHd6kIHvy0owOaC3+4Z+Jro3uCE6C/sVOaATf5r23l294 wdSgvHwLVyXqW7nye48CK4guXMjUQ7myELksbQCosuYV9uCgcpp1EFbRViRgsJ5nAG/a smprdkBMgaN7eEqsJR500LeHUZapR2po0LYPKqpkBhCDBNBTRRLTQzJHzkOe29usZgAM c9DXzMF1uYitnM5yhkLY4+Obi8i1O3PTSPzX7brGlAtdMgadQwbivVLTS9Ug5qxNejXm 3iTg== 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=STvZImlSg453zxS/WPOwXGrHy946aGM7gL5fircHQaY=; b=Fqno9UCy/UL5E04CscBcDpAcVwyMDT4YOGZJrhWnbk4gzcd4XCrfyLndwSyeHZTV3l b5pvxhaIcLmC2hR3zNl7IHocORUjujZPuiufNLiYuqc3NBzbFeQIE+TJqaMf5IDFiYBs DsaMCL2C017K/SVCBw5s2hxNkOCXLiIZ7zhrSnmDpeQo59VmgH9oTwZLUpz0xK66tIg3 9WJFqn6BlwP6aafploZxJvEyipjNMCucjrjIHF5u0KlqMz/V3xWWfBGUBJBEbuGlJEkR Q1YboMw6h383T+lpxArl/houp95AXOwbCu1oBVvTJfiz0Ln3yCD0VGXeTmclPTbgLrFZ RuVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=g0dnUO8g; 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 d23si5720904eja.499.2021.08.10.19.45.49; Tue, 10 Aug 2021 19:46:16 -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=g0dnUO8g; 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 S232387AbhHKCof (ORCPT + 99 others); Tue, 10 Aug 2021 22:44:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232169AbhHKCod (ORCPT ); Tue, 10 Aug 2021 22:44:33 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 043BDC061765 for ; Tue, 10 Aug 2021 19:44:11 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id t3so720063plg.9 for ; Tue, 10 Aug 2021 19:44:10 -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=STvZImlSg453zxS/WPOwXGrHy946aGM7gL5fircHQaY=; b=g0dnUO8gTU16GFKNQ5dbkY/g0YXcD9c2FT5bQMhIz1w4vbHr7Y0Fvh/GVEzJBkarx6 mW68D65LMEPgcaq4NOh2lyk925jd+XdTARj08VOh/+BrdWwI1t1r8Q+XV/LCjqanXGny gLCYFbM4WEdFAkw2fhgKHA0dNM4rPz6OJEtc8= 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=STvZImlSg453zxS/WPOwXGrHy946aGM7gL5fircHQaY=; b=YNxmX9T04OHRE/vp7p/8uJo6oGGOC9Rs/08jipx0c9NScffbgehb05KFbBKGhmhhGZ RJkNYtqzxHEZ2G6pmHTuEdhwpEt8PTWXsHXqawimfsZE4n6hNwzqWnHNHx8kQXhclD83 KcsVs87ZOQlzrT4gj84ENZd8FH6aulJA/l/9sO0Up4d6PHZ7vLXemARsyX0Bu5nNcR6p L5kDFZVn4uyCEup4RuAeDVqZPokVst/hkBTjW3Y1V5vdfb+NIPz+GdNP6i5Ew7G2VnYF iHecojRu9y8yJfBBR7nfOdt8XVxLUvy0h2utImVFWt+UuosOAqksHt1GKj/rm5zSGUG9 +vMw== X-Gm-Message-State: AOAM532j6/I69MF36l+9A+I/xG9aGh9u83RICQCT50UhRJ3d3b4kux3W V//iAVQBqPfTmZHckBE4FAYe+g== X-Received: by 2002:a17:902:7d88:b029:12d:3f9a:d34 with SMTP id a8-20020a1709027d88b029012d3f9a0d34mr5384873plm.13.1628649850589; Tue, 10 Aug 2021 19:44:10 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:1e5c:70cb:3289:1b5b]) by smtp.gmail.com with UTF8SMTPSA id v1sm30068736pgj.40.2021.08.10.19.44.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 10 Aug 2021 19:44:10 -0700 (PDT) From: David Stevens X-Google-Original-From: David Stevens To: Robin Murphy , Will Deacon Cc: Joerg Roedel , Lu Baolu , Tom Murphy , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, David Stevens Subject: [PATCH v3 1/5] dma-iommu: fix sync_sg with swiotlb Date: Wed, 11 Aug 2021 11:42:43 +0900 Message-Id: <20210811024247.1144246-2-stevensd@google.com> X-Mailer: git-send-email 2.32.0.605.g8dce9f2422-goog In-Reply-To: <20210811024247.1144246-1-stevensd@google.com> References: <20210811024247.1144246-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 --- drivers/iommu/dma-iommu.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 98ba927aee1a..54e103b989d9 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -813,14 +813,13 @@ 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)) + 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) 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, @@ -833,14 +832,14 @@ static void iommu_dma_sync_sg_for_device(struct device *dev, 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 + 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.32.0.605.g8dce9f2422-goog