Received: by 10.223.164.202 with SMTP id h10csp277562wrb; Fri, 17 Nov 2017 00:01:16 -0800 (PST) X-Google-Smtp-Source: AGs4zMaVYhfN3HiJrE2BHdKxJPHZcUcNfcC6zJD8rQEuP2kRYcjdW6JMApW2JGldkhJMQ1akFdha X-Received: by 10.84.157.74 with SMTP id u10mr4566489plu.414.1510905676102; Fri, 17 Nov 2017 00:01:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510905676; cv=none; d=google.com; s=arc-20160816; b=LOeHkvTIYM4x8qTp6H2kmI5GyiPXAhDUH1AWjtOJ0eQbdIcobSoNQAfYmM9mhT6iei FdUigLSAGWa9wr8UGWcEUCgmEEAunFb3rWxNKLST12y0OLAIXloeL+wizu+WQXn1txvb uolJlcdQX/IND00k7TXRwRcU8uA/cZWrIZZj82J4OIcpAcaYRNO6tLM1xQ68rFTAcx/J vyMHwIn3ea/RUHY6JiC9kghHQKuB1+e8nzxKd5p//w8giAa5CnHwNcNaPRvuM/KZoMxK Li5fBIyomyIySHJEeuNtTGRdoZMrELt6p96hQMpI2NaCDLL7N/0JspxF/TH+DAh1LFjD vlEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from:arc-authentication-results; bh=0Lk8KGWCt/nU3iCkZTtjcPPPfjONS3jtcLFUZ5acURI=; b=E+Bu593AN/goe3EOyTab0FRivViFg5U0aXUO9/KT3Kpx5N4Rqe/VU4KO1Gxep7NR2G kpu7bAhUsJ5W0U0k0xehlOsXBVAuvbPEi54FNixj9oMMqcGYvtS2TAN+9LgV1nF0ZQPI N9w+648cRKXHpQtP47iHtktAqehETHnPWaDZMEO4h4M12nzeAblEjfD3R6Z+xeCqisXc kZZ97vIjXBJ82Hk5sGUtec5sAuY3vYmms0y8SJuSURdZnJ8nql2so+flc/NZAmQPZf9z urnxK1uTWHC3NCN3iFjiqnIsa4ZtBmmHPEYn5T5GhARZaIxtpPviRkYXslJwR16dih0v hl6g== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j1si2318202pld.136.2017.11.17.00.01.03; Fri, 17 Nov 2017 00:01:16 -0800 (PST) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965920AbdKPW4Y (ORCPT + 92 others); Thu, 16 Nov 2017 17:56:24 -0500 Received: from mailgw01.mediatek.com ([210.61.82.183]:24673 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S965500AbdKPW4T (ORCPT ); Thu, 16 Nov 2017 17:56:19 -0500 X-UUID: 226678a9aa9b42fc80cf30fd672b7a21-20171117 Received: from mtkexhb02.mediatek.inc [(172.21.101.103)] by mailgw01.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 697916090; Fri, 17 Nov 2017 06:56:15 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs08n2.mediatek.inc (172.21.101.56) with Microsoft SMTP Server (TLS) id 15.0.1210.3; Fri, 17 Nov 2017 06:56:13 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1210.3 via Frontend Transport; Fri, 17 Nov 2017 06:56:13 +0800 From: To: Christoph Hellwig , Robin Murphy , Andrew Morton CC: , , , , , Miles Chen Subject: [PATCH v4] dma-debug: fix incorrect pfn calculation Date: Fri, 17 Nov 2017 06:56:12 +0800 Message-ID: <1510872972-23919-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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- lib/dma-debug.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/dma-debug.c b/lib/dma-debug.c index ea4cc3d..1b34d21 100644 --- a/lib/dma-debug.c +++ b/lib/dma-debug.c @@ -1495,14 +1495,22 @@ void debug_dma_alloc_coherent(struct device *dev, size_t size, if (!entry) return; + /* handle vmalloc and linear addresses */ + if (!is_vmalloc_addr(virt) && !virt_to_page(virt)) + return; + entry->type = dma_debug_coherent; entry->dev = dev; - entry->pfn = page_to_pfn(virt_to_page(virt)); entry->offset = offset_in_page(virt); entry->size = size; entry->dev_addr = dma_addr; entry->direction = DMA_BIDIRECTIONAL; + if (is_vmalloc_addr(virt)) + entry->pfn = vmalloc_to_pfn(virt); + else + entry->pfn = page_to_pfn(virt_to_page(virt)); + add_dma_entry(entry); } EXPORT_SYMBOL(debug_dma_alloc_coherent); @@ -1513,13 +1521,21 @@ 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)), .offset = offset_in_page(virt), .dev_addr = addr, .size = size, .direction = DMA_BIDIRECTIONAL, }; + /* handle vmalloc and linear addresses */ + if (!is_vmalloc_addr(virt) && !virt_to_page(virt)) + return; + + if (is_vmalloc_addr(virt)) + ref.pfn = vmalloc_to_pfn(virt); + else + ref.pfn = page_to_pfn(virt_to_page(virt)); + if (unlikely(dma_debug_disabled())) return; -- 1.9.1 From 1585389951167421718@xxx Wed Nov 29 08:54:37 +0000 2017 X-GM-THRID: 1585213420152329215 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread