From patchwork Fri Nov 17 00:13:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 10062199 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 5D1C860352 for ; Fri, 17 Nov 2017 00:14:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F57A2A965 for ; Fri, 17 Nov 2017 00:14:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 441742A96D; Fri, 17 Nov 2017 00:14:18 +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 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 CAA582A965 for ; Fri, 17 Nov 2017 00:14:17 +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:References:In-Reply-To: Message-Id:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=mPL/pNcVJk5RcWqFHn4rKYSQYBZ5FjKfEuA0jxnrXg8=; b=lXHrmrSlblQMoQ oKCyXfcnO3vXIbfpwRY/RXoUHG6RQJEkZfTxKuLjVg70HcfbnfVIYZfcqVBp5qT8zvmaShSo+nGTV ntDRZzrH9B/gAc4LDX0KKRk5wqHPYrGfyg65wK8khxNQBrFM9Z4q/pk63F1iFCvFBMaakGhkiMcwv MfRcDD020nDMl2SrjXqj9i1kmDWDrKG+Y2AOUcmZp9oFZ14SnMPSZpIXjvgpXFl1UPfwtL9w9d65+ rJRHKzTfoizNU9GuHKH1jxD76oQFaJ42c+ecepojPmvsjqBThSMq0NqGb+k+0aTEqL6VQIaomMuwg tzfRIdF8tMvO0OHK56CA==; 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 1eFUIR-0003Wb-G9; Fri, 17 Nov 2017 00:14:15 +0000 Received: from mail.linuxfoundation.org ([140.211.169.12]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eFUIP-0003KD-7I for linux-mediatek@lists.infradead.org; Fri, 17 Nov 2017 00:14:14 +0000 Received: from akpm3.svl.corp.google.com (unknown [104.133.9.92]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 13767A67; Fri, 17 Nov 2017 00:13:51 +0000 (UTC) Date: Thu, 16 Nov 2017 16:13:50 -0800 From: Andrew Morton To: Subject: Re: [PATCH v4] dma-debug: fix incorrect pfn calculation Message-Id: <20171116161350.3b8bd1fbcaae8e032441d3e7@linux-foundation.org> In-Reply-To: <1510872972-23919-1-git-send-email-miles.chen@mediatek.com> References: <1510872972-23919-1-git-send-email-miles.chen@mediatek.com> X-Mailer: Sylpheed 3.4.1 (GTK+ 2.24.23; x86_64-pc-linux-gnu) Mime-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171116_161413_331705_4DC973B3 X-CRM114-Status: GOOD ( 11.18 ) 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: wsd_upstream@mediatek.com, linux-kernel@vger.kernel.org, Christoph Hellwig , linux-mm@kvack.org, iommu@lists.linux-foundation.org, linux-mediatek@lists.infradead.org, Robin Murphy , Christoph Hellwig Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+patchwork-linux-mediatek=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP On Fri, 17 Nov 2017 06:56:12 +0800 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(). > > Fix this by calculating pfn for linear and non-linear addresses. > It's a shame you didn't Cc Christoph, who was the sole reviewer of the earlier version. And it's a shame you didn't capture the result of that review discussion in the v3 changelog. And it's a shame that you didn't describe how this patch differs from earlier versions. Oh well, here's the incremental patch: --- a/lib/dma-debug.c~dma-debug-fix-incorrect-pfn-calculation-v4 +++ a/lib/dma-debug.c @@ -1495,15 +1495,22 @@ void debug_dma_alloc_coherent(struct dev 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 = 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; 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); @@ -1514,14 +1521,21 @@ void debug_dma_free_coherent(struct devi struct dma_debug_entry ref = { .type = dma_debug_coherent, .dev = dev, - .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, .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;