Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp2763014ybh; Mon, 5 Aug 2019 06:28:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqxFi5w7zz4i1NKCa7B7RyMfJLsefOIF8B4qUdE9olqZlc8INRhB6GBDFtS0Esfksqu2gBIw X-Received: by 2002:a63:e09:: with SMTP id d9mr19842102pgl.442.1565011686910; Mon, 05 Aug 2019 06:28:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565011686; cv=none; d=google.com; s=arc-20160816; b=ozLn5zl7Ci2aMMjdmcv03opG6bxkj52q7qLk4MHt3kVQWr+6aPWf7Rk/mJnn8Y0RDA lDYl+kkfOrsTpl+6seDmegbnp7e2nL4iX76GNtcSD56NlpKW1s/8baPlCPFyK/qmlNN8 QhhYu9SM3aKhpJePF9hvR0jwgcDtnyi+/yqagYJFziSVY96t4fu/KWShFK20nNV0c4KE F3n5KAtON7XDyuUNTW0i75Id3h7j88zsvv5sKCthDr5/sryaWx3iZKiFtNCJGoNRojLI gv0tYxTfOdo18x4NS8H8bB/S5Ag9j/8pRj9WXqtOIE3r89+sFqJHCvdk6yk3wXxpi410 +FcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=eArjSDPei0imLZwedXzKlszDY9WxGLCzMIe1rmiMAP4=; b=QdPQbu2EgNpucSfZ6eE9u7bg+ZqGMYU1TydovGNPQXz+6iq61gBksAGnPCRGrpXtFn Twy5oZZQ7bMlbE1YpMF0q0EK7ROON6xqgoUa8Fha5hY88NhbG14esS51S+5I8l3o3bxa Nun6weECCkLtrF2hykak1Kvu1OJEYr3x3PxuUnErDdC8SxC+JGUu3VcQv/ntzk8DslSg uzwdtrG/Wrp1L8WT9EHF4/K6VRm4UG2zx0F2xgjPHJM3JF2hKshTEIIlc/EnOY58LXqL 20j8+vu7zJ5CpM9TOr9AKwtOtPw6fda3PfPJJSJlIBps5LUy75i/OAQ5cAlFw9W80mZq TPzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1RexkAZT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k22si13983554pfi.289.2019.08.05.06.27.51; Mon, 05 Aug 2019 06:28:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1RexkAZT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731127AbfHENZB (ORCPT + 99 others); Mon, 5 Aug 2019 09:25:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:33462 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730312AbfHENY6 (ORCPT ); Mon, 5 Aug 2019 09:24:58 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B24DF20644; Mon, 5 Aug 2019 13:24:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1565011497; bh=Cu99jApK5uKYYlPO+FvuDKIlH7hKNvpn6uCuVC1DBrY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1RexkAZTHUYqMxi0fHzomFh52yLiXfa2le/S4hQX90YcLiep7TyVXGNepricdw1CM JGwcHCQ6bOG/YSWZMRd+WN9UnFIk+ifFxVi6k9HzO/vd05desxMEm1TuUICLIpw1xX e3Ak67b7Rni+qgtDSx/0qZm4ovy5r2mUJmU8S18c= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Fugang Duan , Robin Murphy , Christoph Hellwig , Sasha Levin Subject: [PATCH 5.2 073/131] dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device Date: Mon, 5 Aug 2019 15:02:40 +0200 Message-Id: <20190805124956.543654128@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190805124951.453337465@linuxfoundation.org> References: <20190805124951.453337465@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 449fa54d6815be8c2c1f68fa9dbbae9384a7c03e ] dma_map_sg() may use swiotlb buffer when the kernel command line includes "swiotlb=force" or the dma_addr is out of dev->dma_mask range. After DMA complete the memory moving from device to memory, then user call dma_sync_sg_for_cpu() to sync with DMA buffer, and copy the original virtual buffer to other space. So dma_direct_sync_sg_for_cpu() should use swiotlb physical addr, not the original physical addr from sg_phys(sg). dma_direct_sync_sg_for_device() also has the same issue, correct it as well. Fixes: 55897af63091("dma-direct: merge swiotlb_dma_ops into the dma_direct code") Signed-off-by: Fugang Duan Reviewed-by: Robin Murphy Signed-off-by: Christoph Hellwig Signed-off-by: Sasha Levin --- kernel/dma/direct.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 2c2772e9702ab..2c6d51a62251d 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -231,12 +231,14 @@ void dma_direct_sync_sg_for_device(struct device *dev, int i; for_each_sg(sgl, sg, nents, i) { - if (unlikely(is_swiotlb_buffer(sg_phys(sg)))) - swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length, + phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg)); + + if (unlikely(is_swiotlb_buffer(paddr))) + swiotlb_tbl_sync_single(dev, paddr, sg->length, dir, SYNC_FOR_DEVICE); if (!dev_is_dma_coherent(dev)) - arch_sync_dma_for_device(dev, sg_phys(sg), sg->length, + arch_sync_dma_for_device(dev, paddr, sg->length, dir); } } @@ -268,11 +270,13 @@ void dma_direct_sync_sg_for_cpu(struct device *dev, int i; for_each_sg(sgl, sg, nents, i) { + phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg)); + if (!dev_is_dma_coherent(dev)) - arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir); - - if (unlikely(is_swiotlb_buffer(sg_phys(sg)))) - swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length, dir, + arch_sync_dma_for_cpu(dev, paddr, sg->length, dir); + + if (unlikely(is_swiotlb_buffer(paddr))) + swiotlb_tbl_sync_single(dev, paddr, sg->length, dir, SYNC_FOR_CPU); } -- 2.20.1