From patchwork Fri Apr 28 16:16:24 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: 13226625 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 4A8B0C77B7E for ; Fri, 28 Apr 2023 16:16:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 96D396B0082; Fri, 28 Apr 2023 12:16:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F3C56B0083; Fri, 28 Apr 2023 12:16:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 76E676B0085; Fri, 28 Apr 2023 12:16:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 655B76B0082 for ; Fri, 28 Apr 2023 12:16:32 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2FC3C160171 for ; Fri, 28 Apr 2023 16:16:32 +0000 (UTC) X-FDA: 80731302624.20.B3CFA25 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 4730FA0044 for ; Fri, 28 Apr 2023 16:16:30 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=HvGu5DsP; spf=pass (imf25.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=1682698590; 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=z6y7Ndrhy/q8m1IONuvJy7n2xn0xA0Ad/HUeIzvxBjSzd3pHdpFfZSTA7+BZ+LvHwaR5rP fsoOfPsT9NXA9nr8YFN/cNWH6Tngjh6JmVrCCxfDrpLY+4o2HQzmuVb5LB2l6Rnl5GyLXA wIEJKtGBw8RzwlE214Gai5f8tXSAmUw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=HvGu5DsP; spf=pass (imf25.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=1682698590; a=rsa-sha256; cv=none; b=CSW/viErbFsvmW2pviSXn05QeC8FDY8v6lLmfaN7F2vpOqjv1ZX+RZC878SCPQozxvVVOZ 2wzEfK7jgOh3GSh6/YzQ+/EmZ11tfcMdwIljA92ZLo6GHRF/dlsKvxV3Ur5Cml8WyVwNSn d7DmRx30nV0OiaydRyHG4OR9mb9kNWI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682698589; 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=HvGu5DsPytF4C1TZWFpc7b0tGlx6L836lbXlm4nf5L3T57a/E+SHT5py65hsAqI4FbAhnP b+4+TInZIC+wdEwc/89H1s6+bWP9IMgQ9R2cFdxtqsotYXIYx+TVncR6bfPfo63R3yvwka kVP6HDXsvzNA8fDIfhAOZEUbOIo0GYc= 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-595-0Ujl1XxlO36hZnV-ImNXYA-1; Fri, 28 Apr 2023 12:16:26 -0400 X-MC-Unique: 0Ujl1XxlO36hZnV-ImNXYA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 858BF38237CE; Fri, 28 Apr 2023 16:16:25 +0000 (UTC) Received: from firesoul.localdomain (unknown [10.45.242.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 46F15492C13; Fri, 28 Apr 2023 16:16:25 +0000 (UTC) Received: from [10.1.1.1] (localhost [IPv6:::1]) by firesoul.localdomain (Postfix) with ESMTP id 68AC7307372E8; Fri, 28 Apr 2023 18:16:24 +0200 (CEST) Subject: [PATCH RFC net-next/mm V3 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: Fri, 28 Apr 2023 18:16:24 +0200 Message-ID: <168269858438.2191653.17490250680021679888.stgit@firesoul> In-Reply-To: <168269854650.2191653.8465259808498269815.stgit@firesoul> References: <168269854650.2191653.8465259808498269815.stgit@firesoul> User-Agent: StGit/1.4 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Stat-Signature: xzjjbma8czjnej3h5cnyn1t1nxfx4b3p X-Rspam-User: X-Rspamd-Queue-Id: 4730FA0044 X-Rspamd-Server: rspam06 X-HE-Tag: 1682698590-226541 X-HE-Meta: U2FsdGVkX1/t8iAzK3EVJJWt3SGICMGzOKHk2Px/HNsj67tdGYtv43dhy6fh3oMFxbGVhKwlqSPHR+k8G+Fwq+8qgpFgBjU1ObTdCPTU324QfMm8alFSMi5Hn3KulFQCgQzuUW1YR9N8XLOgnokD3DeJ0qlWT2PXMIngBLzXoMxqq4KmLY4DR0ZL83ziaXi1i/ai7XOrykERtkEBe4syp57U2PUIiHdDH/xYjsVZETsxCLtsoOzRWAB/J4GKyFcR33aCe7l3yin/BpRvVmQc9IXfwy1xtx12+2q+Z9WdGWiVCZdOd2g1eBbZELn43Not21pCCPVNKT3OnVedjdc7zM8HScPP4898VlElskZpQ0eEp5FecoEOSQWMHh3F6N6ZkdZdp2LOiiupoG7mMjtrFDq2mKS9Ej3IeNNAwIAKaOK0MSvxIn26oxfsFJfUfSlhI+dUQNMxSj4GPW5LLNS3i++tuHUtabae0+exD+lU/+ohbjvc6amIor679kFhVCL6bfBc4rkH/zspyeyLFuvbELgbBuAkRAEGsBSzSZVlY3zWdg8G2bdZYaqsRdT4RQ3v3bjETmbMCrXRp1ZpkBVhjjVV6jzKfJ/1tU+jTh4b5HJJvwhsF7pbF7xBxTOJLHN0S13NMdhTanni3jEoLNa3JYvS7wNnuv6RlhnWozfFNQkGjm5u9VD6Ze7jpAeBHnLO/l8ZlGz0zCxNR/yA4ivSpePEGBcYhqNPfy/evyI6/TKn3d5/Yyg301E4hqESfK4P/p4Ok4Mo35AwvVIBgzdq/yElbKy6lIoYgKM5XnYX4RJVaia7u/FaCAPdpeMScRu4F8952LlDVTCKCsyFODn0UEK/oEm/zE0p0/8GY6o/F+YIW8wS84AYo70Zi0hr2ms535hF8fegIISidwBlk9/OVVnwcZFJ7ZU/qR4A/nOT4lXvwJUAxdvFNSbFtesoxf+Z9v1MHE8ZSJzMZeRgzEk 7u2Qw+jr ZGsAOuaM2ueV4wBTRYqudUjV7SQ82ALXeyM89EufDKBX0UPI1LogPEvCXuD2mMHWM5h9ecqitBoXNf9ZJyhF5jFfbSfJlT9Wwu5pSKiHqbZMQpVfsudZ/HFr6/lZxVKNY6jqnfwDGKkFN+wCPzn5BDHe9hytbyKy5ESDyyJ5udvZrBG0qDWqlMJ+GUNMZCW5efIG8NO+f4xWtrltZJkG1ofFYEYlwnWsPBnwcTsimbIt0+6QXHDqCKm/BEjfjHdsIg0uYpLioPSpIFGwabw3RWWDBdnxXp0PbPoFFrezBndo43RcIwcsyqHIo6S5Saf+sm7Iw 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; }