From patchwork Tue May 23 14:52:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesper Dangaard Brouer X-Patchwork-Id: 13252483 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91C69C77B75 for ; Tue, 23 May 2023 14:53:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D94B26B0075; Tue, 23 May 2023 10:53:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D45E96B0078; Tue, 23 May 2023 10:53:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C0DB26B007B; Tue, 23 May 2023 10:53:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B12DA6B0075 for ; Tue, 23 May 2023 10:53:03 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7FA8CC0507 for ; Tue, 23 May 2023 14:53:03 +0000 (UTC) X-FDA: 80821812246.20.15930FC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf09.hostedemail.com (Postfix) with ESMTP id F3F9214000F for ; Tue, 23 May 2023 14:52:59 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dCAHuEkE; spf=pass (imf09.hostedemail.com: domain of brouer@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=brouer@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684853580; a=rsa-sha256; cv=none; b=8MXl+/Dl2yyZSy6q429vD9dIVysGyVvszaKZVrr1aPvLfGA1z+VhMdIRWXqnOU2OiWTjoN uElPoJD4W0Z8/JINJR5IZubhFCJZAhPNqYzafCJ0dSX38SH3L+H35fi6QaPCaz3dsCVQW4 +TqUonSxv0EnT2dkr0QSI3LDzjt2m9c= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dCAHuEkE; spf=pass (imf09.hostedemail.com: domain of brouer@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=brouer@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684853580; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0VBtvZeRIDwJBsf1UEDSI5VydF2ePAZSTRJieedCE2w=; b=SOIjlZb4CUOX9AVkyFDLEQzzOXvfe5kN0oUUvfmkNo5R5K+ss6DkfPlUUjEG8aXHFzKEac kJjPY26EB1QJ+FBQp0uPdHl6XaoWen0xFk54D2sAv+B/gGmO9gJYXEAc6ctqHMxg79o+KQ /iBu28GvATENPHcgRk+llei9jH6A/rA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684853579; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0VBtvZeRIDwJBsf1UEDSI5VydF2ePAZSTRJieedCE2w=; b=dCAHuEkEUh+G+UYGf1TvWJLzWXUvsvxstvBvX2Ykpit4XtjkP0Yik1ulJ1m42+dXGso3VJ /AHINpy8uw6UTkIWtTVkKXxhDQSFE704R7GSPxHHR2+giCun1+Unxs2Nx8hWWyDMVT4tdG sND4jzzYMVo4js/CIrjx1rZBrXekzcc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-630-F7ED3jOaNYOIEIWeDgZB8w-1; Tue, 23 May 2023 10:52:56 -0400 X-MC-Unique: F7ED3jOaNYOIEIWeDgZB8w-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8BAEC8039BE; Tue, 23 May 2023 14:52:54 +0000 (UTC) Received: from firesoul.localdomain (unknown [10.45.242.23]) by smtp.corp.redhat.com (Postfix) with ESMTP id 43415400F49; Tue, 23 May 2023 14:52:54 +0000 (UTC) Received: from [10.1.1.1] (localhost [IPv6:::1]) by firesoul.localdomain (Postfix) with ESMTP id 503A1307372E8; Tue, 23 May 2023 16:52:53 +0200 (CEST) Subject: [PATCH RFC net-next/mm V4 1/2] mm/page_pool: catch page_pool memory leaks From: Jesper Dangaard Brouer To: Ilias Apalodimas , netdev@vger.kernel.org, Eric Dumazet , linux-mm@kvack.org, Mel Gorman Cc: Jesper Dangaard Brouer , lorenzo@kernel.org, =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , linyunsheng@huawei.com, bpf@vger.kernel.org, "David S. Miller" , Jakub Kicinski , Paolo Abeni , Andrew Morton , willy@infradead.org Date: Tue, 23 May 2023 16:52:53 +0200 Message-ID: <168485357325.2849279.485978688281828101.stgit@firesoul> In-Reply-To: <168485351546.2849279.13771638045665633339.stgit@firesoul> References: <168485351546.2849279.13771638045665633339.stgit@firesoul> User-Agent: StGit/1.4 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: F3F9214000F X-Stat-Signature: ktqe7cy1nwkazwkyazhcm6ti73pn43uy X-Rspam-User: X-HE-Tag: 1684853579-679397 X-HE-Meta: U2FsdGVkX1+vJ2LL7QzXtglw0N00NaaBNlLSb2xFSstHgqFtJvZFLfb4lB7jAx5uem2zJ/S6x6qwrB3yBXGy1rAJPiLHY1L90RyEnlGaWygzLpdxL6DzVfWgiNMed1QSx7MXUic6Z+Vmtywsa6clxXAaDmEyf15+ISWkMJCG6g4JMJ7qugVSNa4inPDAglYrIscF/3zNGvWjssPIZpgQbGOxrjPLpM5nq1hPsa/hUV5TLnSt1n7dVQ5ANeymgzJtK2X6W2ih3p4iDbu7ZceYDkDna48gdQDu21aBqANQb4g6dTocaPa2G9ylenVd8ru8/SkkA2bZpGI2KbREVsuOE/WDdDBj+gmUtCPkyx4SksAM8FCqHNGtml0cwaHmWS9jD8yY9+Urt7Suu79kLugwK+DW1fXxunme0UoBUUN+pJUyhY/XBStT+amul05mEkA/xBHOunOO8HZ/1BEz/5cOhlazgRjj/EBUz0pDESIhsC0vyx8XWb5wJWHOabeIPcu20kdxgZeRoDvYKuVouEE/oCWhhQkBa/1vYyqAqjxdtL89/u3yqDrPPmzR5MtVuqEk/kLohzKfy4wbnyGFlTQnYmkKENftvPADXRB1iQDpEYeUCu40i1U4QGftufinLN/js3m7SfQpDAKK8OmQvZQOu4pf2HQ3gO7VIzKDy2M1YKBVSePqAg79N9MaQ0SUhVkb0yigp+FRmPniNVuVExY3K9A06rm+0Ufpk7ESTh3GKl2DPVZgDz+SiHDWl9wxxxVGwrC3d/jmXTeahoyIAkasmMr0/a3j+oVn+53XQXNkFb1b2J4adJjJnbw+AapFasLuKviy6cd7TJElQot/g0KRigZFBaXC4e/gCr9jCf4osetLzLK/Na1m0dqHYiEA8xe6nTN0HgcdXi7F0Z8syiRsDHEnqZqSCTYTTghJH43fTuhKAOsMYthfflFZwnW2aG1m+mtjrtGzSPemlsvz6u+ uXv6Lthu /+qwc1iGa7a236iiTuqsfcXQhl4ar4Y+ITTp4ksKMxOJu0opPb1Z6yQ1PZcLks/ojY6HhFFJ5jaPcN847OX4s1FH4nCkulTygxZYsl3kBvQIyWTP9GXkGEG5gG1as0kfq0vNRfzdo4G60v3GmgL2iwEMbdJn8K3V5C6ss0rSQ9qmF8FRfSYeAycHXmr0jA5QkU9JdOcpt7uX+VYkPveUNFrKmrLVTmmcI9YPJiVN/+yhAektxylrU9MQXF+bgijTxH4tm/T3gHErlF5j1z4XjJ0QBHqYJqo/uJWQWVsGv8PStIJV/ToIZQlxyDXiJ8v2c8BJu X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Pages belonging to a page_pool (PP) instance must be freed through the PP APIs in-order to correctly release any DMA mappings and release refcnt on the DMA device when freeing PP instance. When PP release a page (page_pool_release_page) the page->pp_magic value is cleared. This patch detect a leaked PP page in free_page_is_bad() via unexpected state of page->pp_magic value being PP_SIGNATURE. We choose to report and treat it as a bad page. It would be possible to release the page via returning it to the PP instance as the page->pp pointer is likely still valid. Notice this code is only activated when either compiled with CONFIG_DEBUG_VM or boot cmdline debug_pagealloc=on, and CONFIG_PAGE_POOL. Reduced example output of leak with PP_SIGNATURE = dead000000000040: BUG: Bad page state in process swapper/0 pfn:110bbf page:000000005bc8cfb8 refcount:0 mapcount:0 mapping:0000000000000000 index:0x110bbf000 pfn:0x110bbf flags: 0x2fffff80000000(node=0|zone=2|lastcpupid=0x1fffff) raw: 002fffff80000000 dead000000000040 ffff888117255000 0000000000000000 raw: 0000000110bbf000 000000000000003e 00000000ffffffff 0000000000000000 page dumped because: page_pool leak [...] Signed-off-by: Jesper Dangaard Brouer --- mm/page_alloc.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 47421bedc12b..e6b996da39d4 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1085,6 +1085,9 @@ static inline bool page_expected_state(struct page *page, page_ref_count(page) | #ifdef CONFIG_MEMCG page->memcg_data | +#endif +#ifdef CONFIG_PAGE_POOL + ((page->pp_magic & ~0x3UL) == PP_SIGNATURE) | #endif (page->flags & check_flags))) return false; @@ -1111,6 +1114,10 @@ static const char *page_bad_reason(struct page *page, unsigned long flags) #ifdef CONFIG_MEMCG if (unlikely(page->memcg_data)) bad_reason = "page still charged to cgroup"; +#endif +#ifdef CONFIG_PAGE_POOL + if (unlikely((page->pp_magic & ~0x3UL) == PP_SIGNATURE)) + bad_reason = "page_pool leak"; #endif return bad_reason; }