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; } From patchwork Tue May 23 14:52:58 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: 13252484 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 233D1C7EE23 for ; Tue, 23 May 2023 14:53:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B250B6B0078; Tue, 23 May 2023 10:53:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AD5D06B007B; Tue, 23 May 2023 10:53:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 927E56B007D; Tue, 23 May 2023 10:53:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7E8146B0078 for ; Tue, 23 May 2023 10:53:05 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 479351203D2 for ; Tue, 23 May 2023 14:53:05 +0000 (UTC) X-FDA: 80821812330.05.E570527 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 4FFBC160018 for ; Tue, 23 May 2023 14:53:02 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YZ2dwKaS; dmarc=pass (policy=none) header.from=redhat.com; spf=temperror (imf08.hostedemail.com: error in processing during lookup of brouer@redhat.com: DNS error) smtp.mailfrom=brouer@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684853583; a=rsa-sha256; cv=none; b=EUiHFDw3BEej7yoKRe9nk6nsbT9re7ICo2tjzFYCSvnstViOEqG26PLgvJ8VfFoSv2Oy/O lJJA1i9gmdBce8qlRbta4UoPI0npw7P4EvBDoTgPcqPMr4M1zylJMtKlPnrRazIXmCbreX 0NufZiNOuvj00qjV227QUnpGO7GNxOI= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YZ2dwKaS; dmarc=pass (policy=none) header.from=redhat.com; spf=temperror (imf08.hostedemail.com: error in processing during lookup of brouer@redhat.com: DNS error) smtp.mailfrom=brouer@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684853583; 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=gENW+j3SfpOMmo8IUY3lvawdOv8AtpaP6smSmxRWsyg=; b=jRZDssoG/mP9znIQyI0yxrI6q/htj3s0+2tgRDaLOUPam2VTlvZW0RbqvlmK3g8KvHr/O9 ZIl4d9aPIBjdHEIXrynSZim2hes/bBIxAuuz79JXMUut3sxgcmrBZIY+WlBONRR+3LoruU IOCHdGesVvmwXmSe7zTyXDOmtPz4yPE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684853582; 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=gENW+j3SfpOMmo8IUY3lvawdOv8AtpaP6smSmxRWsyg=; b=YZ2dwKaS3Hd9oXkol9AMh1Xe0S6uV+9eA6bYtzh9EmDhq2nCm1eha1gH/T+VCGJQAilyRi RyfQQOvoHc6tUTrqXogb8Bo4CW583+fCnpHz0Ocz2jKehv3Yl3QhnOGTqBxopGPuvkwG4I /spu+D2zpgZEqBVl51dM0CXdX1fCOjo= 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-388-Ity1FgVMOoCVJ3LCzI8CUg-1; Tue, 23 May 2023 10:53:00 -0400 X-MC-Unique: Ity1FgVMOoCVJ3LCzI8CUg-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 C0EDA1C0A58B; Tue, 23 May 2023 14:52:59 +0000 (UTC) Received: from firesoul.localdomain (unknown [10.45.242.23]) by smtp.corp.redhat.com (Postfix) with ESMTP id 511F5492B0A; Tue, 23 May 2023 14:52:59 +0000 (UTC) Received: from [10.1.1.1] (localhost [IPv6:::1]) by firesoul.localdomain (Postfix) with ESMTP id 5DC4D307372E8; Tue, 23 May 2023 16:52:58 +0200 (CEST) Subject: [PATCH RFC net-next/mm V4 2/2] page_pool: Remove workqueue in new shutdown scheme 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:58 +0200 Message-ID: <168485357834.2849279.8073426325295894331.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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 4FFBC160018 X-Stat-Signature: 3bgjofrpi1y1q4bdgmk8zgmtuxboj7ia X-HE-Tag: 1684853582-152698 X-HE-Meta: U2FsdGVkX1/6qfA1VMdduUgKPH5NhJTFxzyab0jqokmnH5qCyH5hWEq6OlRPkRMle4wuQ2SfEL8XLc9neRHnIWreIISKZOQdOFweZaNKskJOjyJX0b6gllsAHf2c48sQ7FAw5MwDxkNoRNKbTYwlXUCtFOlekpiLkpjtUzDzsn1+GcAdIikVPZYBt6LtV57DtyHMq/u1tzwTYCBB1+gIuM1Jpz1gYD4qwaIwK/6IwiZHlVr9cxAvdRyMVu0rRupPWXt7uqwzj24vn5dRgHb2qnbHLraGYasw2iAXyBmroI7Xxj0n6DeYj8tuPrca0ybpv6aY1W+/ecBesay1Ls4ld2bDLd+zwNcpM8rvtnLVJ9anrD2miIM2cd7Wqqr8IKlR6is815sCZ1PLPKED+3MqzFwFPJD8k2tXEeVqAH983UoWwJ8eqENDClvXE4zno278aOkrzDmfFYKD40pTbLKRklzFi8J/H0hePNpOfeeMbb/8Ge7x+y3rRsB3RhrVqsjeptqFvi+IwpqJHNrB0T/i66WzbiClBavlus04jkbH+0XVJhKdozxItE3uDejLFEkQjMs0k7oq4RwjkPdAecufB+ugV26+4cewxZ5hJdkrbKJhMqqJKyOqAN7QQvDRdTG3COW/TeAhdYtwpoRVIpQjD/Ml8j/7OJ9OugiphA/zudJv+aE8zf1tYTst9KpR3GLtHkf/NFqdXrT8QBy/LLwi90r8BHe9Uh8JxUgTmiDIf83JmMhdlvaRZdAJrN5/PVZLAh5IIuHQQ5aJW5j/7iWEfbFE50aGajv1D1MDKgx8oswN20oNrEFObxGP7LPemwJueba4BQX08BT+LltWTnfkjqMFd4gXnteyJ2TeQwU2nHsuQPn/8Tz+WvlI3L52vFp6agAJ1J7MXgVzwOR6tNK7+AuCTG7Klc8RN0Pu2siGoaZY2VayIEoQKq4AjASUIXRDW7euh+Z0vBBU+XFvooe CoE+ycYh WTi16tgbGevJ+h4j20t/Z0T14YDI35ejQwUeswjk1ZrCfsl9WGKKfoNUTSLcWijnUpbWlwQJX5nfhc/rVavqTuobJ7LqDSAXiF2Ij8ZnEY8tMOIWMagZO0QjvdDEiUzkW29xzRIMwYUwrCFBHdW2uQDViYQ3jTSZhl2pXqB6qaykVHE38hIgXNESHNUV+ZLWA/n567DeNQc/Pbev0XseMAT4PVHnfbxUrRulKS+gxvdaSG7YgSLUvTITlk7jF0pVNOk70wcnvEV0Th77PZPDEPXfR2oRUCGfmd8FbQG+Loy0rxzj7RjwTpHjlwDagFOGEJDezfYpdjESjmAC/+J6RCS2as8Ofnlq3SvXN 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: This removes the workqueue scheme that periodically tests when inflight reach zero such that page_pool memory can be freed. This change adds code to fast-path free checking for a shutdown flags bit after returning PP pages. Performance is very important for PP, as the fast path is used for XDP_DROP use-cases where NIC drivers recycle PP pages directly into PP alloc cache. This patch (since V3) shows zero impact on this fast path. Micro benchmarked with [1] on Intel CPU E5-1650 @3.60GHz. The slight code reorg of likely() are deliberate. [1] https://github.com/netoptimizer/prototype-kernel/blob/master/kernel/lib/bench_page_pool_simple.c Signed-off-by: Jesper Dangaard Brouer --- include/net/page_pool.h | 10 ++- net/core/page_pool.c | 138 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 104 insertions(+), 44 deletions(-) diff --git a/include/net/page_pool.h b/include/net/page_pool.h index c8ec2f34722b..19396e05f12d 100644 --- a/include/net/page_pool.h +++ b/include/net/page_pool.h @@ -50,6 +50,9 @@ PP_FLAG_DMA_SYNC_DEV |\ PP_FLAG_PAGE_FRAG) +/* Internal flag: PP in shutdown phase, waiting for inflight pages */ +#define PP_FLAG_SHUTDOWN BIT(8) + /* * Fast allocation side cache array/stack * @@ -151,11 +154,6 @@ static inline u64 *page_pool_ethtool_stats_get(u64 *data, void *stats) struct page_pool { struct page_pool_params p; - struct delayed_work release_dw; - void (*disconnect)(void *); - unsigned long defer_start; - unsigned long defer_warn; - u32 pages_state_hold_cnt; unsigned int frag_offset; struct page *frag_page; @@ -165,6 +163,7 @@ struct page_pool { /* these stats are incremented while in softirq context */ struct page_pool_alloc_stats alloc_stats; #endif + void (*disconnect)(void *); u32 xdp_mem_id; /* @@ -208,6 +207,7 @@ struct page_pool { refcount_t user_cnt; u64 destroy_cnt; + struct rcu_head rcu; }; struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp); diff --git a/net/core/page_pool.c b/net/core/page_pool.c index e212e9d7edcb..213349148a48 100644 --- a/net/core/page_pool.c +++ b/net/core/page_pool.c @@ -23,9 +23,6 @@ #include -#define DEFER_TIME (msecs_to_jiffies(1000)) -#define DEFER_WARN_INTERVAL (60 * HZ) - #define BIAS_MAX LONG_MAX #ifdef CONFIG_PAGE_POOL_STATS @@ -380,6 +377,10 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool, struct page *page; int i, nr_pages; + /* API usage BUG: PP in shutdown phase, cannot alloc new pages */ + if (WARN_ON(pool->p.flags & PP_FLAG_SHUTDOWN)) + return NULL; + /* Don't support bulk alloc for high-order pages */ if (unlikely(pp_order)) return __page_pool_alloc_page_order(pool, gfp); @@ -450,9 +451,8 @@ EXPORT_SYMBOL(page_pool_alloc_pages); */ #define _distance(a, b) (s32)((a) - (b)) -static s32 page_pool_inflight(struct page_pool *pool) +static s32 __page_pool_inflight(struct page_pool *pool, u32 release_cnt) { - u32 release_cnt = atomic_read(&pool->pages_state_release_cnt); u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt); s32 inflight; @@ -464,6 +464,14 @@ static s32 page_pool_inflight(struct page_pool *pool) return inflight; } +static s32 page_pool_inflight(struct page_pool *pool) +{ + u32 release_cnt = atomic_read(&pool->pages_state_release_cnt); + return __page_pool_inflight(pool, release_cnt); +} + +static int page_pool_free_attempt(struct page_pool *pool, u32 release_cnt); + /* Disconnects a page (from a page_pool). API users can have a need * to disconnect a page (from a page_pool), to allow it to be used as * a regular page (that will eventually be returned to the normal @@ -472,7 +480,7 @@ static s32 page_pool_inflight(struct page_pool *pool) void page_pool_release_page(struct page_pool *pool, struct page *page) { dma_addr_t dma; - int count; + u32 release_cnt; if (!(pool->p.flags & PP_FLAG_DMA_MAP)) /* Always account for inflight pages, even if we didn't @@ -490,11 +498,12 @@ void page_pool_release_page(struct page_pool *pool, struct page *page) skip_dma_unmap: page_pool_clear_pp_info(page); - /* This may be the last page returned, releasing the pool, so - * it is not safe to reference pool afterwards. - */ - count = atomic_inc_return_relaxed(&pool->pages_state_release_cnt); - trace_page_pool_state_release(pool, page, count); + release_cnt = atomic_inc_return(&pool->pages_state_release_cnt); + trace_page_pool_state_release(pool, page, release_cnt); + + /* In shutdown phase, last page will free pool instance */ + if (READ_ONCE(pool->p.flags) & PP_FLAG_SHUTDOWN) + page_pool_free_attempt(pool, release_cnt); } EXPORT_SYMBOL(page_pool_release_page); @@ -535,7 +544,7 @@ static bool page_pool_recycle_in_ring(struct page_pool *pool, struct page *page) static bool page_pool_recycle_in_cache(struct page *page, struct page_pool *pool) { - if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE)) { + if (pool->alloc.count == PP_ALLOC_CACHE_SIZE) { recycle_stat_inc(pool, cache_full); return false; } @@ -546,6 +555,8 @@ static bool page_pool_recycle_in_cache(struct page *page, return true; } +static void page_pool_empty_ring(struct page_pool *pool); + /* If the page refcnt == 1, this will try to recycle the page. * if PP_FLAG_DMA_SYNC_DEV is set, we'll try to sync the DMA area for * the configured size min(dma_sync_size, pool->max_len). @@ -572,7 +583,8 @@ __page_pool_put_page(struct page_pool *pool, struct page *page, page_pool_dma_sync_for_device(pool, page, dma_sync_size); - if (allow_direct && in_softirq() && + /* During PP shutdown, no direct recycle must occur */ + if (likely(allow_direct && in_softirq()) && page_pool_recycle_in_cache(page, pool)) return NULL; @@ -609,6 +621,8 @@ void page_pool_put_defragged_page(struct page_pool *pool, struct page *page, recycle_stat_inc(pool, ring_full); page_pool_return_page(pool, page); } + if (page && pool->p.flags & PP_FLAG_SHUTDOWN) + page_pool_empty_ring(pool); } EXPORT_SYMBOL(page_pool_put_defragged_page); @@ -646,6 +660,9 @@ void page_pool_put_page_bulk(struct page_pool *pool, void **data, recycle_stat_add(pool, ring, i); page_pool_ring_unlock(pool); + if (pool->p.flags & PP_FLAG_SHUTDOWN) + page_pool_empty_ring(pool); + /* Hopefully all pages was return into ptr_ring */ if (likely(i == bulk_len)) return; @@ -737,12 +754,18 @@ struct page *page_pool_alloc_frag(struct page_pool *pool, } EXPORT_SYMBOL(page_pool_alloc_frag); +noinline static void page_pool_empty_ring(struct page_pool *pool) { - struct page *page; + struct page *page, *next; + + next = ptr_ring_consume_bh(&pool->ring); /* Empty recycle ring */ - while ((page = ptr_ring_consume_bh(&pool->ring))) { + while (next) { + page = next; + next = ptr_ring_consume_bh(&pool->ring); + /* Verify the refcnt invariant of cached pages */ if (!(page_ref_count(page) == 1)) pr_crit("%s() page_pool refcnt %d violation\n", @@ -768,6 +791,14 @@ static void page_pool_free(struct page_pool *pool) kfree(pool); } +static void page_pool_free_rcu(struct rcu_head *rcu) +{ + struct page_pool *pool; + + pool = container_of(rcu, struct page_pool, rcu); + page_pool_free(pool); +} + static void page_pool_empty_alloc_cache_once(struct page_pool *pool) { struct page *page; @@ -796,39 +827,30 @@ static void page_pool_scrub(struct page_pool *pool) page_pool_empty_ring(pool); } -static int page_pool_release(struct page_pool *pool) +noinline +static int page_pool_free_attempt(struct page_pool *pool, u32 release_cnt) { int inflight; - page_pool_scrub(pool); - inflight = page_pool_inflight(pool); + rcu_read_lock(); + inflight = __page_pool_inflight(pool, release_cnt); + rcu_read_unlock(); if (!inflight) - page_pool_free(pool); + call_rcu(&pool->rcu, page_pool_free_rcu); return inflight; } -static void page_pool_release_retry(struct work_struct *wq) +static int page_pool_release(struct page_pool *pool) { - struct delayed_work *dwq = to_delayed_work(wq); - struct page_pool *pool = container_of(dwq, typeof(*pool), release_dw); int inflight; - inflight = page_pool_release(pool); + page_pool_scrub(pool); + inflight = page_pool_inflight(pool); if (!inflight) - return; - - /* Periodic warning */ - if (time_after_eq(jiffies, pool->defer_warn)) { - int sec = (s32)((u32)jiffies - (u32)pool->defer_start) / HZ; - - pr_warn("%s() stalled pool shutdown %d inflight %d sec\n", - __func__, inflight, sec); - pool->defer_warn = jiffies + DEFER_WARN_INTERVAL; - } + page_pool_free(pool); - /* Still not ready to be disconnected, retry later */ - schedule_delayed_work(&pool->release_dw, DEFER_TIME); + return inflight; } void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *), @@ -856,6 +878,10 @@ EXPORT_SYMBOL(page_pool_unlink_napi); void page_pool_destroy(struct page_pool *pool) { + unsigned int flags; + u32 release_cnt; + u32 hold_cnt; + if (!pool) return; @@ -868,11 +894,45 @@ void page_pool_destroy(struct page_pool *pool) if (!page_pool_release(pool)) return; - pool->defer_start = jiffies; - pool->defer_warn = jiffies + DEFER_WARN_INTERVAL; + /* PP have pages inflight, thus cannot immediately release memory. + * Enter into shutdown phase, depending on remaining in-flight PP + * pages to trigger shutdown process (on concurrent CPUs) and last + * page will free pool instance. + * + * There exist two race conditions here, we need to take into + * account in the following code. + * + * 1. Before setting PP_FLAG_SHUTDOWN another CPU released the last + * pages into the ptr_ring. Thus, it missed triggering shutdown + * process, which can then be stalled forever. + * + * 2. After setting PP_FLAG_SHUTDOWN another CPU released the last + * page, which triggered shutdown process and freed pool + * instance. Thus, its not safe to dereference *pool afterwards. + * + * Handling races by holding a fake in-flight count, via artificially + * bumping pages_state_hold_cnt, which assures pool isn't freed under + * us. Use RCU Grace-Periods to guarantee concurrent CPUs will + * transition safely into the shutdown phase. + * + * After safely transition into this state the races are resolved. For + * race(1) its safe to recheck and empty ptr_ring (it will not free + * pool). Race(2) cannot happen, and we can release fake in-flight count + * as last step. + */ + hold_cnt = READ_ONCE(pool->pages_state_hold_cnt) + 1; + WRITE_ONCE(pool->pages_state_hold_cnt, hold_cnt); + synchronize_rcu(); + + flags = READ_ONCE(pool->p.flags) | PP_FLAG_SHUTDOWN; + WRITE_ONCE(pool->p.flags, flags); + synchronize_rcu(); + + /* Concurrent CPUs could have returned last pages into ptr_ring */ + page_pool_empty_ring(pool); - INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry); - schedule_delayed_work(&pool->release_dw, DEFER_TIME); + release_cnt = atomic_inc_return(&pool->pages_state_release_cnt); + page_pool_free_attempt(pool, release_cnt); } EXPORT_SYMBOL(page_pool_destroy);