Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S969183AbdIZNZA (ORCPT ); Tue, 26 Sep 2017 09:25:00 -0400 Received: from mailgw01.mediatek.com ([210.61.82.183]:52355 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S934627AbdIZNY6 (ORCPT ); Tue, 26 Sep 2017 09:24:58 -0400 X-UUID: 82e1aa8128384f82ab51a8967e33ef1d-20170926 From: To: Robin Murphy , Andrew Morton CC: , , , , , Miles Chen Subject: [PATCH] dma-debug: fix incorrect pfn calculation Date: Tue, 26 Sep 2017 21:24:47 +0800 Message-ID: <1506432287-7214-1-git-send-email-miles.chen@mediatek.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3773 Lines: 105 From: Miles Chen dma-debug report the following warning: [name:panic&]WARNING: CPU: 3 PID: 298 at kernel-4.4/lib/dma-debug.c:604 debug _dma_assert_idle+0x1a8/0x230() DMA-API: cpu touching an active dma mapped cacheline [cln=0x00000882300] CPU: 3 PID: 298 Comm: vold Tainted: G W O 4.4.22+ #1 Hardware name: MT6739 (DT) Call trace: [] dump_backtrace+0x0/0x1d4 [] show_stack+0x14/0x1c [] dump_stack+0xa8/0xe0 [] warn_slowpath_common+0xf4/0x11c [] warn_slowpath_fmt+0x60/0x80 [] debug_dma_assert_idle+0x1a8/0x230 [] wp_page_copy.isra.96+0x118/0x520 [] do_wp_page+0x4fc/0x534 [] handle_mm_fault+0xd4c/0x1310 [] do_page_fault+0x1c8/0x394 [] do_mem_abort+0x50/0xec I found that debug_dma_alloc_coherent() and debug_dma_free_coherent() always use type "dma_debug_coherent" and assume that dma_alloc_coherent() always returns a linear address. However if a device returns false on is_device_dma_coherent(), dma_alloc_coherent() will create another non-cacheable mapping (also non linear). In this case, page_to_pfn(virt_to_page(virt)) will return an incorrect pfn. If the pfn is valid and mapped as a COW page, we will hit the warning when doing wp_page_copy(). Fix this by calculating correct pfn if is_device_dma_coherent() returns false. Signed-off-by: Miles Chen --- lib/dma-debug.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/dma-debug.c b/lib/dma-debug.c index ea4cc3d..b17e56e 100644 --- a/lib/dma-debug.c +++ b/lib/dma-debug.c @@ -47,6 +47,8 @@ enum { dma_debug_sg, dma_debug_coherent, dma_debug_resource, + dma_debug_noncoherent, + nr_dma_debug_types, }; enum map_err_types { @@ -154,9 +156,9 @@ static inline bool dma_debug_disabled(void) [MAP_ERR_CHECKED] = "dma map error checked", }; -static const char *type2name[5] = { "single", "page", +static const char *type2name[nr_dma_debug_types] = { "single", "page", "scather-gather", "coherent", - "resource" }; + "resource", "noncoherent" }; static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE", "DMA_FROM_DEVICE", "DMA_NONE" }; @@ -1484,6 +1486,7 @@ void debug_dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t dma_addr, void *virt) { struct dma_debug_entry *entry; + bool coherent = is_device_dma_coherent(dev); if (unlikely(dma_debug_disabled())) return; @@ -1495,9 +1498,11 @@ void debug_dma_alloc_coherent(struct device *dev, size_t size, if (!entry) return; - entry->type = dma_debug_coherent; + entry->type = coherent ? dma_debug_coherent : + dma_debug_noncoherent; entry->dev = dev; - entry->pfn = page_to_pfn(virt_to_page(virt)); + entry->pfn = coherent ? page_to_pfn(virt_to_page(virt)) : + dma_addr >> PAGE_SHIFT; entry->offset = offset_in_page(virt); entry->size = size; entry->dev_addr = dma_addr; @@ -1510,10 +1515,13 @@ void debug_dma_alloc_coherent(struct device *dev, size_t size, void debug_dma_free_coherent(struct device *dev, size_t size, void *virt, dma_addr_t addr) { + bool coherent = is_device_dma_coherent(dev); struct dma_debug_entry ref = { - .type = dma_debug_coherent, + .type = coherent ? dma_debug_coherent : + dma_debug_noncoherent, .dev = dev, - .pfn = page_to_pfn(virt_to_page(virt)), + .pfn = coherent ? page_to_pfn(virt_to_page(virt)) : + addr >> PAGE_SHIFT, .offset = offset_in_page(virt), .dev_addr = addr, .size = size, -- 1.9.1