Received: by 2002:a05:6a10:c7d3:0:0:0:0 with SMTP id h19csp1392980pxy; Sun, 15 Aug 2021 20:00:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz1sGul2x7tJ/eNIBnuNJJvBF9PDwyGQdsNWLWDOP8GcRdP28bViTT8YkBcnr1K9Due3TPP X-Received: by 2002:a05:6e02:1288:: with SMTP id y8mr10152852ilq.206.1629082822406; Sun, 15 Aug 2021 20:00:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629082822; cv=none; d=google.com; s=arc-20160816; b=EPbraSRF0aT8uGXwkEE4ORM9hG4oTFqD2/LD7Bhvau8Eaie81LWRHYExq7db3+BqgU 0+L2XfGG4PmublpTClAEIYkANdWMam+gKFECM/2ZoFoh+QMbtu54T7CkfzssKGDFOOMF /ka7RxklxsNiTwgboQdBBdNqsDtotS/u2cT5BR/H7gW+lDxKfjqtMJvS2j2oibiM+5hB Z+WSSzbPkc0EpDe935s3+oC3pNlOo0h5p/WRIx8Jr5zIRRYWxqRLP8U+TyPCDhrn62kn lAynYSPht0imvxQ/Wp6ooLMZEFAL6GHzQNwZCaC6a4pUW2RgqzPqJSXsukfmysJ7Q/a0 J9OQ== 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=+F9dSAyZHVmy26HMTvHdg1DO02SGISMQ5dvVqt7bETc=; b=ESKOi08X+17b76O+NA5+v4pGaaMNPiq9dLr25W2ddH48tCK4Lu0uP4dC0P6Upz2a5O JBrkn+kyknGBNHfrLjQwp87onjeXjK3sjYy9QssNjtZhICWWBwl1h4tq/a2x6SDwM3c+ w8m9ZhOM2OhQsmd9S5ZZpIXchXnb+6bNvzPznTcjUELt+4KdeLi/ErTKzsVhJ+ULS/YW YZn5Nh8M5/PZtNARWXEDkLQlVOyo9lZXrOCDlO4pOt7qXkkNG8L+LaMNkw5V0xin8e5g Bgol4TcVUmnA1S4Oyk4CwcgnbuXiBAIfEqNK5IxQW7Ld8SO6DfKk181MH0QxHFwkm2Cm MOtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=dAyNh8tC; 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 b18si5620484ioz.23.2021.08.15.20.00.11; Sun, 15 Aug 2021 20:00: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=dAyNh8tC; 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 S229663AbhHPDAA (ORCPT + 99 others); Sun, 15 Aug 2021 23:00:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231789AbhHPC75 (ORCPT ); Sun, 15 Aug 2021 22:59:57 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DF13C061764 for ; Sun, 15 Aug 2021 19:59:26 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id m24-20020a17090a7f98b0290178b1a81700so25145713pjl.4 for ; Sun, 15 Aug 2021 19:59:26 -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=+F9dSAyZHVmy26HMTvHdg1DO02SGISMQ5dvVqt7bETc=; b=dAyNh8tCT3B59dwbEUH10ImbUwwDZ/P6LskYkeAjKi3ou5/5JQoqEozp7nU+xVJDTg 8MblRadhQGDEI+tDrJYvQm9lVZOtv3SWyTduMA8RsfzOxMecS7cai0W2CffquFDjIeqT u1VDuYDJafiQZMl6hYeZwsg8VbB7ThgMYnGEg= 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=+F9dSAyZHVmy26HMTvHdg1DO02SGISMQ5dvVqt7bETc=; b=QmpyVlg5EmKhBeX6HGMD142GfLzZK/RR6NTNUsY22w5BsHozlJdCxk+aEwHBjkg8yp he9FlNdyzS+eru/W35E0lkP2bBJk23jCm5U6Lu3836tYb6bz/ZyzWw3CrDqa0sJAEtNE f4vRvi3OH9ivjjbcoO/vJgmjZdEdeVae/KArCK0SznVm4FduzAhPQsmpxhjG34XZkRBV MzCT6AxLXjecr/QcANW/u/qaKP/4V+2qXvlXqUM/ynQMvVFyMW4nmmqTbiG1mmSsPxKd yyYrOq92kWVA80jBDsk2IB7o0ky2sbCxV3Vm4zjixrTZp9nnke5IawESLm8jNtZwOEMZ Rb+Q== X-Gm-Message-State: AOAM530z8HNB/Gxa4afF7ENbsqtFAX3NjvXS/pulFbaZ8ddBJLpQNTv0 L/kd/i1mzLzTfJ1l1DHDshpyWw== X-Received: by 2002:a63:211c:: with SMTP id h28mr13898097pgh.83.1629082765891; Sun, 15 Aug 2021 19:59:25 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:17b8:f07a:2a52:317a]) by smtp.gmail.com with UTF8SMTPSA id m4sm7886091pjl.6.2021.08.15.19.59.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Aug 2021 19:59:25 -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 , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, David Stevens Subject: [PATCH v5 1/7] dma-iommu: fix sync_sg with swiotlb Date: Mon, 16 Aug 2021 11:57:49 +0900 Message-Id: <20210816025755.2906695-2-stevensd@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog In-Reply-To: <20210816025755.2906695-1-stevensd@google.com> References: <20210816025755.2906695-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 98ba927aee1a..968e0150c95e 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -810,17 +810,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, @@ -830,17 +826,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.rc1.237.g0d66db33f3-goog