Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3397702ybi; Fri, 19 Jul 2019 02:37:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqw4Ljolw8OSSHkkXWb/SzOMCBfroDpg4OB52+vZR0DqqguMv0vb5Xd4+QD7KlZ1XLk/vEFF X-Received: by 2002:a65:51c1:: with SMTP id i1mr31627443pgq.417.1563529027900; Fri, 19 Jul 2019 02:37:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563529027; cv=none; d=google.com; s=arc-20160816; b=OMuMEFz+VzxGuYgbt8DjOCotbkHRFHvvfekZFX7K0BvpwqWh0wogWHrqI7Q4vzUyqX efXHOrIHaIr3kWhZv4DmfKuWEo9ZiiRfv/uipuJrK5vwe6mwBN/s9Qmw8uJCxY/baLoz xwP/h4r/6HWpKD16nrSW95vWwv1OLJIYAQzgTkF9SW5OdMlUTRZU8JLqHn4fZrEOUDua 5r9dYFWkZDPZ+rY146Ed1Ao7WJBEFdG3uy8dqihuK5ZO2hsxxo7+UV3OP2gS4cnI0gTh vnkygOmTtgRs/gEqRGVXQDcmI4wgUr/vyB+OqAB2BFzWfkHeexPLMSwgL+0DH2C6j1jG n5NQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=dm2EtJW29usBAq/x/i9g5D6j4uhkfCRTGhcrOTsY2Hk=; b=q+QfAl9COAN1zJM6JX0yOUnEJaM3LGzwNL0bAMMbC8P6fnDgQr0L4+UAcqWul/pZga e1LLjxUZq+pM4GMX9hLPIbcGEQHcq6eVGHcnB9RsdgwoipR9IcbCh5PHRYKW+odFA8dQ eo+zO+hDVUEYT2PqMbcvJmWkEOmv+DTigp7PKIgnUxOcHgBpozIo3p4r8FhpycES0Ho1 L5eHmy8jNqQ0dhIqvtWxGEs5OYLAWH48u1Bvm8lXx0xaEggPeiN//dD1dD6SfDzw4k15 +RDHVVm9LXWf7mH2Gb4WjvoQ5rgTV/c9BtWbdwf6QHPtzUBYqlOUIsQHHaJ2LufZOj+b 8B7w== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r200si1869334pgr.518.2019.07.19.02.36.51; Fri, 19 Jul 2019 02:37:07 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726243AbfGSJgL (ORCPT + 99 others); Fri, 19 Jul 2019 05:36:11 -0400 Received: from inva021.nxp.com ([92.121.34.21]:42360 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725798AbfGSJgK (ORCPT ); Fri, 19 Jul 2019 05:36:10 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id CCB58200158; Fri, 19 Jul 2019 11:36:08 +0200 (CEST) Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 71BEB200153; Fri, 19 Jul 2019 11:36:04 +0200 (CEST) Received: from titan.ap.freescale.net (TITAN.ap.freescale.net [10.192.208.233]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id C92B4402B5; Fri, 19 Jul 2019 17:35:58 +0800 (SGT) From: fugang.duan@nxp.com To: hch@lst.de, m.szyprowski@samsung.com Cc: festevam@gmail.com, robin.murphy@arm.com, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, aisheng.dong@nxp.com, Fugang Duan Subject: [PATCH dma 1/1] dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device Date: Fri, 19 Jul 2019 17:26:48 +0800 Message-Id: <20190719092648.11085-1-fugang.duan@nxp.com> X-Mailer: git-send-email 2.9.5 X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Fugang Duan dma_map_sg() may use swiotlb buffer when kernel parameter include "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 similar issue, correct it. Fixes: 55897af63091("dma-direct: merge swiotlb_dma_ops into the dma_direct code") Signed-off-by: Fugang Duan --- 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 b90e1ae..0e87f86 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -242,12 +242,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); } } @@ -279,11 +281,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.7.4