From patchwork Wed Sep 27 03:48:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miles Chen X-Patchwork-Id: 9973017 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1535F60365 for ; Wed, 27 Sep 2017 03:49:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 01AAF29068 for ; Wed, 27 Sep 2017 03:49:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EAAFB2906D; Wed, 27 Sep 2017 03:49:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 79A4129068 for ; Wed, 27 Sep 2017 03:49:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:To :From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=HY/gVnKsz6pOIwR+qKKOq5RQvCL5BJF1x7zcJc63ryY=; b=FsQQgIE25CF7Jq apQU1m2/jiZJwEod5w6iPHR++69YG164iQquYsOTTt+GjVgfmcKdhXMGnfklHCVHplzl8bz7REqlP 4JynueebdB0rSqsg+nX+rXNlTEQMr+vACcaZL/MEvEci/a/N2uk3ffV8W4xdihcbZz37BLceu+8U8 kNy5CsOdOsft1ocC0Bh3bE4/W24yq8gha9dWGRK6kdVZaYTXbki3z3Mld+Etx+zd9/TF2fxxbfPs/ ANWJt3JZq3DH23tKncLf2DfNPqtWeUU3nqkKqj/T53/P1/WL142S59XDt+2FejAObMqRJIGjInpSQ 1JkT7liyYPhaU/JBDHpQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dx3LT-0002Ty-HQ; Wed, 27 Sep 2017 03:49:11 +0000 Received: from [210.61.82.184] (helo=mailgw02.mediatek.com) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dx3LO-0002DZ-Rx for linux-mediatek@lists.infradead.org; Wed, 27 Sep 2017 03:49:09 +0000 X-UUID: 361035fadd6f46dea57fed292c95f438-20170927 Received: from mtkcas08.mediatek.inc [(172.21.101.126)] by mailgw02.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 546805018; Wed, 27 Sep 2017 11:48:31 +0800 Received: from MTKCAS06.mediatek.inc (172.21.101.30) by mtkmbs08n1.mediatek.inc (172.21.101.55) with Microsoft SMTP Server (TLS) id 15.0.1210.3; Wed, 27 Sep 2017 11:48:27 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by MTKCAS06.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1210.3 via Frontend Transport; Wed, 27 Sep 2017 11:47:47 +0800 From: To: Robin Murphy , Andrew Morton Subject: [PATCH v3] dma-debug: fix incorrect pfn calculation Date: Wed, 27 Sep 2017 11:48:07 +0800 Message-ID: <1506484087-1177-1-git-send-email-miles.chen@mediatek.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170926_204907_170113_A8050064 X-CRM114-Status: UNSURE ( 9.89 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Miles Chen , wsd_upstream@mediatek.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org, linux-mediatek@lists.infradead.org Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+patchwork-linux-mediatek=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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(). Fix this by calculating pfn for linear and non-linear addresses. Signed-off-by: Miles Chen Reviewed-by: Robin Murphy --- 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,