From patchwork Tue Sep 26 13:24:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miles Chen X-Patchwork-Id: 9972023 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 3598C60365 for ; Tue, 26 Sep 2017 13:25:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3440E212DB for ; Tue, 26 Sep 2017 13:25:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 28FD12868C; Tue, 26 Sep 2017 13:25:28 +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 BBB80212DB for ; Tue, 26 Sep 2017 13:25:27 +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=3IwTmJuU7j7KsRCzXC47dRZ4yp+h3yz5gWIYzvGejt8=; b=VN8Lkrh/UwgT7b AO+MwaudJmxoQRYUGg3wA790qVZD2/NthVyiWyYxs96BmuO30kT58EjaBBsYggGBcj2MKuDpEiz2+ DTAri8K7oWC1eMxmfiloHmhzO3TFIL6AzigOBzE3uPzjM6kH/rLdb+8SIGjUksDQ4kvL6AnCYbef0 AyaIkLPYhmdzbHXXu8vsSlcjZ/rrwbf2IMNMfHtjcXTjOwrQyIEIgz/iP+wd6CT0WAnA2dANvkwJi a6ZReWZWk4wOd/76RxSseMJki88DmVE3uVEKC4n5rotzbZeFKVf/ooHrVBeR4w/l6iAQUVoiWggxC hA7Jg9u4sbvSfFAZENSg==; 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 1dwprZ-000783-KE; Tue, 26 Sep 2017 13:25:25 +0000 Received: from [210.61.82.183] (helo=mailgw01.mediatek.com) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dwprW-0005dE-9i for linux-mediatek@lists.infradead.org; Tue, 26 Sep 2017 13:25:24 +0000 X-UUID: 82e1aa8128384f82ab51a8967e33ef1d-20170926 Received: from mtkcas08.mediatek.inc [(172.21.101.126)] by mailgw01.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 1569983717; Tue, 26 Sep 2017 21:24:52 +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; Tue, 26 Sep 2017 21:25:07 +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; Tue, 26 Sep 2017 21:24:28 +0800 From: To: Robin Murphy , Andrew Morton 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 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170926_062522_642212_11C1292F X-CRM114-Status: UNSURE ( 9.98 ) 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 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,