Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp209231pxb; Tue, 28 Sep 2021 19:42:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxwOKUhFSCWZSti4/x8v+RAMXWY2+5As9a/HS32qALd5UBDv39BUSMeki7RGgd44XIQ2vhG X-Received: by 2002:a17:902:e806:b0:13e:45b4:2a4 with SMTP id u6-20020a170902e80600b0013e45b402a4mr770261plg.23.1632883342671; Tue, 28 Sep 2021 19:42:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632883342; cv=none; d=google.com; s=arc-20160816; b=00d2FoOmtdIU91sPR35Oav0jC7nDUCFIW4Z7Fj2g6xRvpHFr2Hd5+htgJZQi266I+e V0ofh4dj9LMj14y2o2X/DYteJ7Du94KMV2kcgzJcehY9C6CXEMsWI1Omylmpl7BU9F/R 1/rfmhXft4Ac7Odfpn7/m4uaCyN/Pc0lzi3vGe7+V1vE7sgEo1tUkYUDbw525b2D500K /1/QUosIi7Oq4nM45xof4kBveaEmccYZBbRvtflf6a80HRXVhaW7UKaToRWpxhF7vPas r3GZo0vZMS0gFjFLsya0QG+DZu9EmTT+Bphfrnr7gru6iS4n8xyJ+POd3CWItLF419su Dw6Q== 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=oYJssbbh9oIZ4Q55TlprZpqnC9hYx+tSJnJso1kwE6c=; b=nN4oEPCgZBFW6DnAM3V4PiIJEvnTV39KyQqdjJgiPRHKarDg7POHRAT/r/n7HSRaM8 bfEovUAZtxhhjWWf+HhuhlpHJE7i0XqwFEnUlZQQChxDkweQ/Sv043WK8VukHm+87ZV/ sm9iaYaWysz4wGQzlyxSXRJxZ948S0YqOo3Ydmi4F0rrQEtWUbxNNcC3Dq4PHFU6hJpW W6ZL44Lph6qeRwAfapINRLLKo+GOjCYm41u5AqwJS8BS9/AjXLRqVs/Oz5V6uhYVaEew 5WlLpUwtv/FpaHeQRCr3/ASZ5sfAE9XsfrcNxCyVgQN5HyKRIBsLAszPxQEQwILxKgD1 IE4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jc5yGWif; 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 u78si1232914pfc.79.2021.09.28.19.42.09; Tue, 28 Sep 2021 19:42:22 -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=jc5yGWif; 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 S243809AbhI2Cez (ORCPT + 99 others); Tue, 28 Sep 2021 22:34:55 -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-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