From: Jesper Dangaard Brouer <brouer@redhat.com>
To: Ilias Apalodimas <ilias.apalodimas@linaro.org>,
netdev@vger.kernel.org, Eric Dumazet <eric.dumazet@gmail.com>,
linux-mm@kvack.org, Mel Gorman <mgorman@techsingularity.net>
Cc: "Jesper Dangaard Brouer" <brouer@redhat.com>,
lorenzo@kernel.org, "Toke Høiland-Jørgensen" <toke@redhat.com>,
linyunsheng@huawei.com, bpf@vger.kernel.org,
"David S. Miller" <davem@davemloft.net>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Andrew Morton" <akpm@linux-foundation.org>,
willy@infradead.org
Subject: [PATCH RFC net-next/mm V1 1/3] page_pool: Remove workqueue in new shutdown scheme
Date: Tue, 25 Apr 2023 19:15:38 +0200 [thread overview]
Message-ID: <168244293875.1741095.10502498932946558516.stgit@firesoul> (raw)
In-Reply-To: <168244288038.1741095.1092368365531131826.stgit@firesoul>
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 <brouer@redhat.com>
---
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 <trace/events/page_pool.h>
-#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);
next prev parent reply other threads:[~2023-04-25 17:15 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-25 17:15 [PATCH RFC net-next/mm V1 0/3] page_pool: new approach for leak detection and shutdown phase Jesper Dangaard Brouer
2023-04-25 17:15 ` Jesper Dangaard Brouer [this message]
2023-04-27 0:57 ` [PATCH RFC net-next/mm V1 1/3] page_pool: Remove workqueue in new shutdown scheme Yunsheng Lin
2023-04-27 10:47 ` Jesper Dangaard Brouer
2023-04-27 18:29 ` Jesper Dangaard Brouer
2023-04-25 17:15 ` [PATCH RFC net-next/mm V1 2/3] page_pool: Use static_key for shutdown phase Jesper Dangaard Brouer
2023-04-25 23:30 ` Alexei Starovoitov
2023-04-26 15:15 ` Jesper Dangaard Brouer
2023-04-25 17:15 ` [PATCH RFC net-next/mm V1 3/3] mm/page_pool: catch page_pool memory leaks Jesper Dangaard Brouer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=168244293875.1741095.10502498932946558516.stgit@firesoul \
--to=brouer@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=bpf@vger.kernel.org \
--cc=davem@davemloft.net \
--cc=eric.dumazet@gmail.com \
--cc=ilias.apalodimas@linaro.org \
--cc=kuba@kernel.org \
--cc=linux-mm@kvack.org \
--cc=linyunsheng@huawei.com \
--cc=lorenzo@kernel.org \
--cc=mgorman@techsingularity.net \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=toke@redhat.com \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox