Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752601AbdI0KX5 (ORCPT ); Wed, 27 Sep 2017 06:23:57 -0400 Received: from foss.arm.com ([217.140.101.70]:43728 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751800AbdI0KX4 (ORCPT ); Wed, 27 Sep 2017 06:23:56 -0400 Subject: Re: [PATCH v3] dma-debug: fix incorrect pfn calculation To: miles.chen@mediatek.com, Christoph Hellwig , Marek Szyprowski Cc: Andrew Morton , wsd_upstream@mediatek.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org, linux-mediatek@lists.infradead.org References: <1506484087-1177-1-git-send-email-miles.chen@mediatek.com> From: Robin Murphy Message-ID: <273077fd-c5ad-82c8-60aa-cde89355e5e8@arm.com> Date: Wed, 27 Sep 2017 11:23:52 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: <1506484087-1177-1-git-send-email-miles.chen@mediatek.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2787 Lines: 69 [+DMA maintainers] On 27/09/17 04:48, miles.chen@mediatek.com wrote: > From: Miles Chen > > dma-debug reports 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() > assume that dma_alloc_coherent() always returns a linear address. > However it's possible that dma_alloc_coherent() returns a non-linear > address. 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(). Yeah, we definitely want that explanation recorded in the commit, since the warning is pretty non-obvious otherwise. Reviewed-by: Robin Murphy > Fix this by calculating pfn for linear and non-linear addresses. > > Signed-off-by: Miles Chen > --- > lib/dma-debug.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/lib/dma-debug.c b/lib/dma-debug.c > index ea4cc3d..e5b4237 100644 > --- a/lib/dma-debug.c > +++ b/lib/dma-debug.c > @@ -1497,7 +1497,8 @@ void debug_dma_alloc_coherent(struct device *dev, size_t size, > > entry->type = dma_debug_coherent; > entry->dev = dev; > - entry->pfn = page_to_pfn(virt_to_page(virt)); > + entry->pfn = is_vmalloc_addr(virt) ? vmalloc_to_pfn(virt) : > + page_to_pfn(virt_to_page(virt)); > entry->offset = offset_in_page(virt); > entry->size = size; > entry->dev_addr = dma_addr; > @@ -1513,7 +1514,8 @@ void debug_dma_free_coherent(struct device *dev, size_t size, > struct dma_debug_entry ref = { > .type = dma_debug_coherent, > .dev = dev, > - .pfn = page_to_pfn(virt_to_page(virt)), > + .pfn = is_vmalloc_addr(virt) ? vmalloc_to_pfn(virt) : > + page_to_pfn(virt_to_page(virt)), > .offset = offset_in_page(virt), > .dev_addr = addr, > .size = size, >