From patchwork Tue Apr 25 17:15:38 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: 13223546 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 76BFAC77B73 for ; Tue, 25 Apr 2023 17:15:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 08F746B0092; Tue, 25 Apr 2023 13:15:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 03E7A6B0093; Tue, 25 Apr 2023 13:15:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E48C46B0095; Tue, 25 Apr 2023 13:15:47 -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 D77496B0092 for ; Tue, 25 Apr 2023 13:15:47 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 92AEA12015F for ; Tue, 25 Apr 2023 17:15:47 +0000 (UTC) X-FDA: 80720565534.27.D27AF10 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 9EA4220029 for ; Tue, 25 Apr 2023 17:15:45 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TouZh2XZ; spf=pass (imf13.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=1682442945; 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=670hg1AZvr7qtlDRF5iLq0fUp54srb+aqDOh/giGrsI=; b=aOikAqlXj0PHciqQntThLHK6jZt8PUVcsyHof/vseCJs/DLsWjhuHvX3cfoTlSqGUqU36d laewpuDfwjoPWI12sGciKVsTf8k0z424l26IkqYUgjBNwb8E5PQqtbU4v9fS0oAcyGXauh aX+fzmMB9fIOk281tlny/KX/HBz6bLc= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TouZh2XZ; spf=pass (imf13.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=1682442945; a=rsa-sha256; cv=none; b=tvH04R32Wi1XqLqzHdxaN0LxuHgSr1t6n0jBDpv7ZW/mz5g0MtRK5OIxIGy2m8PSBzTEqq PX9Bc4DeDSWNajkcFwntEs3dZk+x7rFOV2SgCRFwgovQBMjD+qExrRXPN2YmT2g9vT2t9I S1uLklZgClrCghdbLQFIHfg+GYoh3xk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682442944; 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=670hg1AZvr7qtlDRF5iLq0fUp54srb+aqDOh/giGrsI=; b=TouZh2XZcgVvIKRMveQki5mKayejxweLeitm53c2uI8TNe4xTMN6b3alXAo7N6YyS5uqhJ lvLYkVU6NIUDp5emYfGR3ICdFb/AViP/dI4A09cGF3U3HNy9zLQrFu2hkrZY973UqxcJMx SH5Z8xMqKE/u1u++k4w5ewdF+u/zolE= 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-628-A1AxtTPcPWO5xQ1mDE96BA-1; Tue, 25 Apr 2023 13:15:42 -0400 X-MC-Unique: A1AxtTPcPWO5xQ1mDE96BA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D24351C0515E; Tue, 25 Apr 2023 17:15:39 +0000 (UTC) Received: from firesoul.localdomain (unknown [10.45.242.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6E6312166B3A; Tue, 25 Apr 2023 17:15:39 +0000 (UTC) Received: from [10.1.1.1] (localhost [IPv6:::1]) by firesoul.localdomain (Postfix) with ESMTP id C9D96307372E8; Tue, 25 Apr 2023 19:15:38 +0200 (CEST) Subject: [PATCH RFC net-next/mm V1 1/3] 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, 25 Apr 2023 19:15:38 +0200 Message-ID: <168244293875.1741095.10502498932946558516.stgit@firesoul> In-Reply-To: <168244288038.1741095.1092368365531131826.stgit@firesoul> References: <168244288038.1741095.1092368365531131826.stgit@firesoul> User-Agent: StGit/1.4 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9EA4220029 X-Stat-Signature: 96j1ia9socpzq3jfpd57rz8kbx1z5ugz X-HE-Tag: 1682442945-785035 X-HE-Meta: U2FsdGVkX1+tEifYYHq2aIk7auc883x5Pmoqwif5xweAjB8CSAvyrXSiPmIRhQu6Tfnd/1b480V7wn28u6PU5lKUWgXo8EVYetOdRGwzaY2MHxzlT4Y2w4D9S6Ir3b0JBKW5E7Kic5bBHDBjGg/kuXhzIRFiSTKb6/ezbQkppimyyRf3LwlXxKgOs4uuqCqCDD5vr7yoMJgquLosR+ZNEwFwOk8w0Qfx+5shrE0ynnmzPZ2JYJTPc0MtwOJ3RHixg3tj0QTBRFEPgRObPNTX5WMjqITfiqtHu/GTphkTN054NPK1i8bIb6yxz7HO2w6U38UYeCs8FRcea+Phq/HVniLy5ePWcm1XykfPilLtO86Z22fK3PWL8xI8wtQ1k3lRvpeFZunYpxc6khthucadAK06bhA0I9XcJbAr9KMGLrElfey/fb49XZqYZEJvlCLmG69nrw7K249lfRv9uyA1opTsucK0obj86yrwxru1cJjcMyj9bQMdwiE+w8uCqiEX2kqq8dn+VAh+7V1GCbZ9OfXiQ3fRjCcCMKALso9mniW4xxh3zqTIg1G/xFHnDtVDHW46i6UnWS4TDg4athkh0mp5vUa6G/hDmGdqpUqMurjvd2i1pL8/unWDzG6P6NaTHZSyywF0edk2k1onjaWxyN75EeaTWT/hlUww0icdbz0/xtFEgnIb/drOoSrsX1/y9/jyjcUMtt7HvgG+zrligtUpH7OB312AJuD9IdrXi8dIosJQJgPYnvaurednCI8DHHANoR8LzjiGgjitz0NAAdndoe5j2FN4fzAwEEFv54qGNyiFxD86yjcxr6coM8180OXY2T0HgUtDgKa80nQKOLTP20FyEeMJk3lgsQKO549Lsjo9VPkHfG7Q3dRkOhBMHSJ1Or2fflFIJc4ofPOpmwvJkhF39U5Y6Isr3lM+5o1zDwJn6fcn28Vwao6GcXIZekW95pEXAE9QMSZ5fB3 Fa05d5AP F6r4jkLZm0O/0O4lXADUCx/7rtBhZBcOL0ZOweMfyI/rRB1CxfJdS9Q1EqRJVkBwkiONQUfpsnxWB5cv0a+LZ3PxDtJwLHaCmc0BpInStokwGAdpoGRrsRVSF9aAOhFXO/3BvM/gNbF7FdnLJAFEvU4+8ybg8iVHE2IkzH4QdIGDN7g9NeU0f13hq5N3Gq41UJEyXcqpZZN7pkBFlhXc8sgCqDsrgTbq3MqjOGY2QT4DAIuerNHEifWAG/PR/lZQ6l+hjN31CAXU3iVz+a/uU6gB/AutZe0MKgSa3QKBbSD2Rx6oBBPjt3BIEIyyG798rOH2e/bfdwNnFAPh/p/PAFZWJVWlSeR4RrDwk 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. The goal were that this code change should have zero impact on this fast-path. The slight code reorg of likely() are deliberate. Micro benchmarking done via kernel module[1] on x86_64, shows this code change only cost a single instruction extra (approx 0.3 nanosec on CPU E5-1650 @3.60GHz). It is possible to make this code zero impact via static_key, but that change is split out into the next patch, as we are unsure if it is worth the complexity. [1] https://github.com/netoptimizer/prototype-kernel/blob/master/kernel/lib/bench_page_pool_simple.c Signed-off-by: Jesper Dangaard Brouer Signed-off-by: Jesper Dangaard Brouer --- include/net/page_pool.h | 9 +++---- net/core/page_pool.c | 59 +++++++++++++++++++---------------------------- 2 files changed, 28 insertions(+), 40 deletions(-) diff --git a/include/net/page_pool.h b/include/net/page_pool.h index c8ec2f34722b..a71c0f2695b0 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; /* diff --git a/net/core/page_pool.c b/net/core/page_pool.c index e212e9d7edcb..ce7e8dda6403 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); @@ -489,10 +490,6 @@ void page_pool_release_page(struct page_pool *pool, struct page *page) page_pool_set_dma_addr(page, 0); 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); } @@ -535,7 +532,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 +543,8 @@ static bool page_pool_recycle_in_cache(struct page *page, return true; } +static void page_pool_shutdown_attempt(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 +571,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 +609,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 (pool->p.flags & PP_FLAG_SHUTDOWN) + page_pool_shutdown_attempt(pool); } EXPORT_SYMBOL(page_pool_put_defragged_page); @@ -648,13 +650,17 @@ void page_pool_put_page_bulk(struct page_pool *pool, void **data, /* Hopefully all pages was return into ptr_ring */ if (likely(i == bulk_len)) - return; + goto out; /* ptr_ring cache full, free remaining pages outside producer lock * since put_page() with refcnt == 1 can be an expensive operation */ for (; i < bulk_len; i++) page_pool_return_page(pool, data[i]); + +out: + if (pool->p.flags & PP_FLAG_SHUTDOWN) + page_pool_shutdown_attempt(pool); } EXPORT_SYMBOL(page_pool_put_page_bulk); @@ -808,27 +814,10 @@ static int page_pool_release(struct page_pool *pool) return inflight; } -static void page_pool_release_retry(struct work_struct *wq) +noinline +static void page_pool_shutdown_attempt(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); - 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; - } - - /* Still not ready to be disconnected, retry later */ - schedule_delayed_work(&pool->release_dw, DEFER_TIME); + page_pool_release(pool); } void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *), @@ -868,11 +857,11 @@ 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; - - INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry); - schedule_delayed_work(&pool->release_dw, DEFER_TIME); + /* PP have pages inflight, thus cannot immediately release memory. + * Enter into shutdown phase, and retry release to handle races. + */ + pool->p.flags |= PP_FLAG_SHUTDOWN; + page_pool_shutdown_attempt(pool); } EXPORT_SYMBOL(page_pool_destroy); From patchwork Tue Apr 25 17:15:43 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: 13223547 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 2236AC77B78 for ; Tue, 25 Apr 2023 17:15:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B15F36B0095; Tue, 25 Apr 2023 13:15:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC6236B0096; Tue, 25 Apr 2023 13:15:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98E576B0098; Tue, 25 Apr 2023 13:15:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 820636B0095 for ; Tue, 25 Apr 2023 13:15:50 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3B30B120115 for ; Tue, 25 Apr 2023 17:15:50 +0000 (UTC) X-FDA: 80720565660.18.2E05F6F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 228771C0020 for ; Tue, 25 Apr 2023 17:15:47 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VooI1ROS; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf21.hostedemail.com: domain of brouer@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=brouer@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682442948; 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=QIaVUQ2kkkd1foj1zcvHm82kEONSqPRsWu1f6N9g/Wg=; b=No1441SgHzmhPnvOZTlAPmYTEDIb35r+swzUbxRHJcJCQmuvKUp2Fr4Jps4kIGwmlDUN03 E2CPT/tuM4/hOhpFg90WoF9agFEAWgP85MzJ4VNP+S28yQ9ig/Lhw351N4pBCLir57MK3n 8ka/wSmOEGQpiEeGB+n/CMXXStUSEyY= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VooI1ROS; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf21.hostedemail.com: domain of brouer@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=brouer@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682442948; a=rsa-sha256; cv=none; b=EW3z7/E27fd4iSAiUX3Bw0Fytvsuq8t68ju5XgWMw2wspLQexp8AK+pZk0IsunQbXSsNP+ rTAn7IRS8/j+UdcBqYBb9X2FFgegdZ2G1U1964bcnaEI8KaAYUyrbw2hGsjlfIYR+i7gwi lKBj1C9L86PW0igH4BpmHyNJ+OWvI6I= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682442947; 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=QIaVUQ2kkkd1foj1zcvHm82kEONSqPRsWu1f6N9g/Wg=; b=VooI1ROSiahgdvZhS3qoF2+87X6EmQEflLHLG5gWPNlDsfB9UH8tGYvXoWsYNIO1XoVhr3 NhFW/U70Y1++sN/iQ5hZo5crthoUIXucMJM0x6rcvvzM0r1CO3r/uBPPJo30Scs5i+X8uH Z+TWW4DyEJMwdW/1GTqzzI1GMmayNNc= 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-637-XDdFthw9O1qxyONoG3QHmw-1; Tue, 25 Apr 2023 13:15:45 -0400 X-MC-Unique: XDdFthw9O1qxyONoG3QHmw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F09EB381D4C6; Tue, 25 Apr 2023 17:15:44 +0000 (UTC) Received: from firesoul.localdomain (unknown [10.45.242.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id B5CF840C2064; Tue, 25 Apr 2023 17:15:44 +0000 (UTC) Received: from [10.1.1.1] (localhost [IPv6:::1]) by firesoul.localdomain (Postfix) with ESMTP id D70F7307372E8; Tue, 25 Apr 2023 19:15:43 +0200 (CEST) Subject: [PATCH RFC net-next/mm V1 2/3] page_pool: Use static_key for shutdown phase 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, 25 Apr 2023 19:15:43 +0200 Message-ID: <168244294384.1741095.6037010854411310099.stgit@firesoul> In-Reply-To: <168244288038.1741095.1092368365531131826.stgit@firesoul> References: <168244288038.1741095.1092368365531131826.stgit@firesoul> User-Agent: StGit/1.4 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 228771C0020 X-Stat-Signature: x15fc1quutbiuuqafrybnzdew4138pdt X-HE-Tag: 1682442947-178748 X-HE-Meta: U2FsdGVkX19ukzERWazzYuyhFNS4O/oU5Q40RMUoI7Cd0XCvI8XdL8wgCYQf5WgnOyBn+Fgi+048DLdk6oiSrpe75v/zJmjT16yIGpwzF7WGFBSpejoOf3afSFnC3b2AgtfGEPRzMdGvRZQnTQiGUYs+mdYCE9NLUnK3gfV3vy9ivyIYIQjmBCjnDlxO/AjijcbFoIN+OsTp4foRX5ORTycRJHGzA7+vLACm0gwocMmCgkqO+rUensR5gk4aonQThJxil4Q8g1ZpW5/sWKr9SkEA95HHvTeDxUIvQ2DC6dNxJqGhE0uZcZiSUgjtQYG4CFhmRonOTKWQel/3hS9QvSuqsECZERnNnX+Gv0O0FCw3vp1GBaJxmO2OfQPrrvprbseC5m94LTGaz8GD79Vn/REBtHz5z0mlv6iiXOAAIaO+ZHwqPs7TbzK8pt99iSLZcTobjP4fdQbn/G09yTRoh8t8jvwBsXNpO4H8Te76Wzz9TVoQ9Yy3k0CC7fkXf1k9vaD+Z2Ualgp+TGbiVz6OsC3+/W+kpb7xSbvQUlYqb2g1XgcOMNkK2biRAADzI+d7vFUa0Flz/rf2aXipKBahHe0dCnaCh3CXbEMDutPxs6L5SGFiIvNh+EmNMXyW6HuxlZnGe+olDYShkCR7RSOaYwHbadoxgHGg8IByIvscpyt5V1m04qhoKsL+bIAE7sY2zXUJ5Gbs8IXXVV1TEd5D5Q7WsWGHmqMdisrhZnie6J1mn5b/ySLPPUQnr+2Yy5OhHZ9papvFR1OIvbdkYc4+QRQwKphdFFkoTPIVgGYIcdF/S+mmfO65lsxzRWhgghxqp3IeS2fFGsVgHsZKEIIPJhRUrkXQ0L294R6E14SFjdZqJGl0sOVb7mwQuvW94Afq2f0f3xEFNG4kAyvnS2rQOKNv/ivOBJxMHcczW8J4qljUpz1UM1Q//SHBlPQqs+qACcGZgLouKGbvhm7WTqH CG7x/fvU MJvF8YCykm0LljxQUiUIaITxQliZamEaj1ypNjyhQFBP8mf42IEYZpc5NAcSL/0r0KUUbH4sOjo51DaM17aS8j8A/7qk500sXy0nLebjRTSPhCNpbFkuVnAcAsq9NnaBK5WZcnKsLgpefat0ereHMo+oVpRcFXSwNyz13lLrclHKehKJDhxZcqau8KTmpsvf8TMKKDACJVkK5kzEaABMBPuip0G4ZCrNRsfCEaToFU3DjUNJBm27lSaAUrCUPOpizS+PjUvnSHYn8NG+cpW2HY+JEdDtyeoV/MLtookGWNfKTcOBVKzXZbI7VEFIyG+X8afCL 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: Performance is very important for page pool (PP). This add the use of static_key APIs for regaining a single instruction, which makes the new PP shutdown scheme zero impact. We are uncertain if this is 100% correct, because static_key APIs uses a mutex lock and it is uncertain if all contexts that can return pages can support this. We could spawn a workqueue (like we just removed) to workaround this issue. Seeking input if this is worth the complexity. Signed-off-by: Jesper Dangaard Brouer --- net/core/page_pool.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/net/core/page_pool.c b/net/core/page_pool.c index ce7e8dda6403..3821d8874b15 100644 --- a/net/core/page_pool.c +++ b/net/core/page_pool.c @@ -25,6 +25,8 @@ #define BIAS_MAX LONG_MAX +DEFINE_STATIC_KEY_FALSE(pp_shutdown_phase); + #ifdef CONFIG_PAGE_POOL_STATS /* alloc_stat_inc is intended to be used in softirq context */ #define alloc_stat_inc(pool, __stat) (pool->alloc_stats.__stat++) @@ -378,7 +380,8 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool, int i, nr_pages; /* API usage BUG: PP in shutdown phase, cannot alloc new pages */ - if (WARN_ON(pool->p.flags & PP_FLAG_SHUTDOWN)) + if (static_key_enabled(&pp_shutdown_phase) && + WARN_ON(pool->p.flags & PP_FLAG_SHUTDOWN)) return NULL; /* Don't support bulk alloc for high-order pages */ @@ -609,7 +612,7 @@ 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 (pool->p.flags & PP_FLAG_SHUTDOWN) + if (static_branch_unlikely(&pp_shutdown_phase)) page_pool_shutdown_attempt(pool); } EXPORT_SYMBOL(page_pool_put_defragged_page); @@ -659,7 +662,7 @@ void page_pool_put_page_bulk(struct page_pool *pool, void **data, page_pool_return_page(pool, data[i]); out: - if (pool->p.flags & PP_FLAG_SHUTDOWN) + if (static_branch_unlikely(&pp_shutdown_phase)) page_pool_shutdown_attempt(pool); } EXPORT_SYMBOL(page_pool_put_page_bulk); @@ -817,7 +820,15 @@ static int page_pool_release(struct page_pool *pool) noinline static void page_pool_shutdown_attempt(struct page_pool *pool) { - page_pool_release(pool); + int inflight; + + if (!(pool->p.flags & PP_FLAG_SHUTDOWN)) + return; + + inflight = page_pool_release(pool); + + if (static_key_enabled(&pp_shutdown_phase) && !inflight) + static_branch_dec(&pp_shutdown_phase); } void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *), @@ -861,6 +872,7 @@ void page_pool_destroy(struct page_pool *pool) * Enter into shutdown phase, and retry release to handle races. */ pool->p.flags |= PP_FLAG_SHUTDOWN; + static_branch_inc(&pp_shutdown_phase); page_pool_shutdown_attempt(pool); } EXPORT_SYMBOL(page_pool_destroy); From patchwork Tue Apr 25 17:15:48 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: 13223548 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 A00D3C77B61 for ; Tue, 25 Apr 2023 17:15:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4865B6B0096; Tue, 25 Apr 2023 13:15:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4368B6B0098; Tue, 25 Apr 2023 13:15:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FE9E6B0099; Tue, 25 Apr 2023 13:15:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 21D406B0096 for ; Tue, 25 Apr 2023 13:15:57 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B2D9F402EB for ; Tue, 25 Apr 2023 17:15:56 +0000 (UTC) X-FDA: 80720565912.16.FCAD641 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf15.hostedemail.com (Postfix) with ESMTP id BFE97A000F for ; Tue, 25 Apr 2023 17:15:54 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Ug72baL6; spf=pass (imf15.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=1682442954; 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=thudqcRM9rLv5msQO3C3MkgbjZRxYWWuxI1TB/VeEhqsDiRvA49OMZFj98uYnB+mInULy9 SB/EN+//wpcHBGYvNfdnhpTar7/RRNiQlrVt6AAMDnvCZMhjscQIuYjYAWpqDhxoUPmCDg 4cXxGVIEk+OWrWaXfa2anI+zk0bgs9s= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Ug72baL6; spf=pass (imf15.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=1682442954; a=rsa-sha256; cv=none; b=Jsn7p+wj217xlIxDpAv8d+P0MIKlDRyw0uVe5A9evw0avXCu/PKz4tMTbf/H+SUr/qip/C C9Y1RwvylhNf1u66NJbELSMoTU5d5IHpVDGjeBcoxdH2D705JQ5JJyrJjI8Z7t2A6/dgRp xywz8IhXL/EzOjR+dMxulYRclhfh7Ko= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682442954; 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=Ug72baL6x5sZpAdOz/W5wB74qLCEkrlJbAOTMlL1442qVwBdS4N2KBczy4mbnpvwN+2lrQ vxwU5AO4ciiyLzZxnti7EVEGCYJGX2Vc4AMKQKY5mGvaSPFWxUOrsW3sdfGj4oLRa/1uQ4 bVF8gA4FAPrrImj/f7yKe7HhSylqC74= 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-493-wrDmbxbYOgmM7gRaWquA5g-1; Tue, 25 Apr 2023 13:15:50 -0400 X-MC-Unique: wrDmbxbYOgmM7gRaWquA5g-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BE36E101A552; Tue, 25 Apr 2023 17:15:49 +0000 (UTC) Received: from firesoul.localdomain (unknown [10.45.242.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 806322166B3A; Tue, 25 Apr 2023 17:15:49 +0000 (UTC) Received: from [10.1.1.1] (localhost [IPv6:::1]) by firesoul.localdomain (Postfix) with ESMTP id E6032307372E8; Tue, 25 Apr 2023 19:15:48 +0200 (CEST) Subject: [PATCH RFC net-next/mm V1 3/3] 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, 25 Apr 2023 19:15:48 +0200 Message-ID: <168244294889.1741095.16231421250476473539.stgit@firesoul> In-Reply-To: <168244288038.1741095.1092368365531131826.stgit@firesoul> References: <168244288038.1741095.1092368365531131826.stgit@firesoul> User-Agent: StGit/1.4 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Stat-Signature: e47gdyjnrog6ybeatbmmskbcz1t899r5 X-Rspam-User: X-Rspamd-Queue-Id: BFE97A000F X-Rspamd-Server: rspam06 X-HE-Tag: 1682442954-284774 X-HE-Meta: U2FsdGVkX1+62TlKNuXN2tq5iZ3969PUwd9MuRVRac+iDTo6TfG1sArIz9t9E5nPFcuS+Vq4slurn6sgZ9bEuIFF5aXTAN9r6TrAjvd7gnv5O9nL6g4xh7iwku4A4I1LMfVS4Y4SBeNE7jbSDh5yr4TEQo3c6+pPkJ2b8HZBf8nptYZtNPK7xz4w4PRfZpEeBl1VKH1X1azFXTIWoyDBoHV0IRa2y7JtccHONwFGzTtK+IqPUCjtUPLNWDB5/d1Zfp3IDYc8so8heEZ3WGzQ8ea0kMbF0L3V3/zcQ+M296hjejt6dwoVCImHs7vIqdfAuLgxpKOttUqLZclIk9kIJiDyDNSqx0eqSVyf7wa9JJq9lIhhevHqqLkIPmbADFvMlgH6nIbzogz2NaC/UTJtd6QoMhpG6A3Juhza2mcYPWlrEYs8rEgo/mUntOKT+W3xmwmGbIjJJxf5ttvy4qcR6psP5EANF7JH00h7aexFkVne7cGt/A1vdQMmzpNj6RjTsili/CJqJn3UlqGATwdv0WLumxko/uzElBY3eswv8DVFyQWjA/e9Z5CPDEmJGekasP4nUBTuDFKteVMD2tHlDH4RjxIfZ+fIUXCHln+k0P6pEfQC2sAn0q+SQAtfkA0KBMjWu9IsG6aMWc/3pa4mMUFAXK7YtiiImNl8e0SSEjCYMHJLPRO3OpgT+M7X1xy/k8EpRsREJJA88kGYu4fGX+OFvv2VoDwFS4zPb70AgeOWrSme2AYi7DreLbOxF4rYMZ4mtPpEvK9ovS8ek8SZ3JcSu/gY+8dk0O30klwg9yxuqk5Ioo+LWbuBh8l0HGsQRe75IeBKWQUjHviYBXCJsES5pjgJkiV1TgvrQbyX2KO4ZgP4dBDf1tVuHQxfR4AMUn8wN40X6HiwQW44QWPKc0hDAvOzjG3EumLxvok58xwOVHzD9lzVk/zfsvHg8ctTnI+rIG0fb+oIg7mJ4oR aZPUwysY LY5lcXOYwkeFDGRTQHCLmqhzGvEVC5x9RFKissbo6L7EsagC1pYdRzJTm0cW/sDGgJ8iaHAvkMf06sAR9lTix8HzpMlylOEdqUaYUu3lT82hL9tNdAvbCGFqzt0uDdozv/NPPHQflZCQWfY5RSsKaSihU7KE3dcD7+vHescmDZA4rR6H++eLEcKtELkzRx+qSFh3OD1BVyGDt5qVoyNImX54kNmXkwiY0Igdft9HtBg8/L1ytsvE5VV++t/BqtKdRGYNVxmicoh43OSgOUk23dPt7m1TofBCr8I16MegMm8O6NMt8avVHB4BHSTnt0NcmUZDL 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; }