From patchwork Thu Apr 27 19:25:16 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: 13225709 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 46A81C77B61 for ; Thu, 27 Apr 2023 19:25:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D63756B0074; Thu, 27 Apr 2023 15:25:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D13D46B0075; Thu, 27 Apr 2023 15:25:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDBA16B0078; Thu, 27 Apr 2023 15:25:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B0CD56B0074 for ; Thu, 27 Apr 2023 15:25:27 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8607B14013F for ; Thu, 27 Apr 2023 19:25:27 +0000 (UTC) X-FDA: 80728149894.15.DF88CF6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 9B4F0100016 for ; Thu, 27 Apr 2023 19:25:25 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OQJbJFp1; spf=pass (imf14.hostedemail.com: domain of brouer@redhat.com designates 170.10.129.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=1682623525; 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=wynb7GdU79YYV9ENu+xz5worQ7/3FbOaM+jXoCkc6Vg=; b=MmYP0bV6BbdLB8ewod8L3XiSkz0vYBpBsEpwZEkzAiOlF0UjEf870jBlR0POJG9Lxsg5uc DRONtO75yknuyyLDu+WDI9HwxQ46n0t9ZMjRSXrA7hKcpmwyvA6m+52nL8IX2BvQhCdFpX MQtdf2aH8AVWufmvxAUnabZo8GZ08sI= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OQJbJFp1; spf=pass (imf14.hostedemail.com: domain of brouer@redhat.com designates 170.10.129.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=1682623525; a=rsa-sha256; cv=none; b=KpqN+DBaQbN4IV8UAP/a236vs4PANHOTQcIJf5yyB3pfnBJcWYDUynQW7uLNK0e7sxH9F6 RyWw/sWSZsnnKvAUdOJdzZiniJN6feyYDbQvrgFHaQFZedAMUywUDmeYP3MM4oFF1m7rUD IBzyKSspLcNsucM9mC7LOXsHhvUi7hA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682623525; 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=wynb7GdU79YYV9ENu+xz5worQ7/3FbOaM+jXoCkc6Vg=; b=OQJbJFp1Dw4SYvZSWhag03MXxedfwpKbmTDd6CBAQI/BYbtOoIujan8xThCZ+tKEqS7RWG UjdRooWAE4NztTy6D2ZdJVREdjHv9wDLpw1uedYdkTzyGZK4i323HukqtHkFdUnsD9vSXn G6/OwAh4Dz/AFsfCssuYhUYZEy6qy7U= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-571-kl80mYlSMgig3GF4mRmU3Q-1; Thu, 27 Apr 2023 15:25:18 -0400 X-MC-Unique: kl80mYlSMgig3GF4mRmU3Q-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7FD86281294A; Thu, 27 Apr 2023 19:25:17 +0000 (UTC) Received: from firesoul.localdomain (unknown [10.45.242.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 430DA1121314; Thu, 27 Apr 2023 19:25:17 +0000 (UTC) Received: from [10.1.1.1] (localhost [IPv6:::1]) by firesoul.localdomain (Postfix) with ESMTP id 65F7C307372E8; Thu, 27 Apr 2023 21:25:16 +0200 (CEST) Subject: [PATCH RFC net-next/mm V2 2/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: Thu, 27 Apr 2023 21:25:16 +0200 Message-ID: <168262351637.2036355.17064734185414935239.stgit@firesoul> In-Reply-To: <168262348084.2036355.16294550378793036683.stgit@firesoul> References: <168262348084.2036355.16294550378793036683.stgit@firesoul> User-Agent: StGit/1.4 MIME-Version: 1.0 X-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6X9fE3MZGyz29f/pwUugfrkJdoZyE4IOeCly510KaFCDRrH1/AqZD+eCQkzRd+dC+E73 VdLg9+Ul KU4AwVc/+I7STnl5K7fk7vZihVZdtqKBbEdVFnSqCE7CfVr0X0ujeoh35TGSq4YoLxVGLWZZsSBJ0wSjqACjC3GkhDvz95UZBtYgU07hsVnVnZERqCFjKevYTcWrhCDK1tl5TiZ43jol4Y53+1AdYkrvixf3syMIRiTo/oqdTeP4ATGyPeTqG/3P5g4Stnik3ITHonmkqAAwUeERxz3Xcq2x3XaL4XotuK+93z67k465nL4C1pgMZIgVa2an/gIvC2orz3t7oWBvpvqx7lxWdAIFaoV2zHjrdZPvGlwfKg/dK9dy6v+F8mU9scTEJS3dembxP 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 8e39705c7bdc..137b72f8ab8b 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1247,6 +1247,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; @@ -1273,6 +1276,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; }