Skip to content

Commit bf22306

Browse files
jlemongregkh
authored andcommitted
page_pool: do not release pool until inflight == 0.
[ Upstream commit c3f812c ] The page pool keeps track of the number of pages in flight, and it isn't safe to remove the pool until all pages are returned. Disallow removing the pool until all pages are back, so the pool is always available for page producers. Make the page pool responsible for its own delayed destruction instead of relying on XDP, so the page pool can be used without the xdp memory model. When all pages are returned, free the pool and notify xdp if the pool is registered with the xdp memory system. Have the callback perform a table walk since some drivers (cpsw) may share the pool among multiple xdp_rxq_info. Note that the increment of pages_state_release_cnt may result in inflight == 0, resulting in the pool being released. Fixes: d956a04 ("xdp: force mem allocator removal and periodic warning") Signed-off-by: Jonathan Lemon <jonathan.lemon@gmail.com> Acked-by: Jesper Dangaard Brouer <brouer@redhat.com> Acked-by: Ilias Apalodimas <ilias.apalodimas@linaro.org> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 573da3c commit bf22306

6 files changed

Lines changed: 139 additions & 183 deletions

File tree

‎drivers/net/ethernet/stmicro/stmmac/stmmac_main.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,10 +1477,8 @@ static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
14771477
rx_q->dma_erx, rx_q->dma_rx_phy);
14781478

14791479
kfree(rx_q->buf_pool);
1480-
if (rx_q->page_pool) {
1481-
page_pool_request_shutdown(rx_q->page_pool);
1480+
if (rx_q->page_pool)
14821481
page_pool_destroy(rx_q->page_pool);
1483-
}
14841482
}
14851483
}
14861484

‎include/net/page_pool.h‎

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ struct page_pool_params {
7070
struct page_pool {
7171
struct page_pool_params p;
7272

73-
u32 pages_state_hold_cnt;
73+
struct delayed_work release_dw;
74+
void (*disconnect)(void *);
75+
unsigned long defer_start;
76+
unsigned long defer_warn;
77+
78+
u32 pages_state_hold_cnt;
7479

7580
/*
7681
* Data structure for allocation side
@@ -129,25 +134,19 @@ inline enum dma_data_direction page_pool_get_dma_dir(struct page_pool *pool)
129134

130135
struct page_pool *page_pool_create(const struct page_pool_params *params);
131136

132-
void __page_pool_free(struct page_pool *pool);
133-
static inline void page_pool_free(struct page_pool *pool)
134-
{
135-
/* When page_pool isn't compiled-in, net/core/xdp.c doesn't
136-
* allow registering MEM_TYPE_PAGE_POOL, but shield linker.
137-
*/
138137
#ifdef CONFIG_PAGE_POOL
139-
__page_pool_free(pool);
140-
#endif
141-
}
142-
143-
/* Drivers use this instead of page_pool_free */
138+
void page_pool_destroy(struct page_pool *pool);
139+
void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *));
140+
#else
144141
static inline void page_pool_destroy(struct page_pool *pool)
145142
{
146-
if (!pool)
147-
return;
143+
}
148144

149-
page_pool_free(pool);
145+
static inline void page_pool_use_xdp_mem(struct page_pool *pool,
146+
void (*disconnect)(void *))
147+
{
150148
}
149+
#endif
151150

152151
/* Never call this directly, use helpers below */
153152
void __page_pool_put_page(struct page_pool *pool,
@@ -170,24 +169,6 @@ static inline void page_pool_recycle_direct(struct page_pool *pool,
170169
__page_pool_put_page(pool, page, true);
171170
}
172171

173-
/* API user MUST have disconnected alloc-side (not allowed to call
174-
* page_pool_alloc_pages()) before calling this. The free-side can
175-
* still run concurrently, to handle in-flight packet-pages.
176-
*
177-
* A request to shutdown can fail (with false) if there are still
178-
* in-flight packet-pages.
179-
*/
180-
bool __page_pool_request_shutdown(struct page_pool *pool);
181-
static inline bool page_pool_request_shutdown(struct page_pool *pool)
182-
{
183-
bool safe_to_remove = false;
184-
185-
#ifdef CONFIG_PAGE_POOL
186-
safe_to_remove = __page_pool_request_shutdown(pool);
187-
#endif
188-
return safe_to_remove;
189-
}
190-
191172
/* Disconnects a page (from a page_pool). API users can have a need
192173
* to disconnect a page (from a page_pool), to allow it to be used as
193174
* a regular page (that will eventually be returned to the normal
@@ -216,11 +197,6 @@ static inline bool is_page_pool_compiled_in(void)
216197
#endif
217198
}
218199

219-
static inline void page_pool_get(struct page_pool *pool)
220-
{
221-
refcount_inc(&pool->user_cnt);
222-
}
223-
224200
static inline bool page_pool_put(struct page_pool *pool)
225201
{
226202
return refcount_dec_and_test(&pool->user_cnt);

‎include/net/xdp_priv.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ struct xdp_mem_allocator {
1212
struct page_pool *page_pool;
1313
struct zero_copy_allocator *zc_alloc;
1414
};
15-
int disconnect_cnt;
16-
unsigned long defer_start;
1715
struct rhash_head node;
1816
struct rcu_head rcu;
19-
struct delayed_work defer_wq;
20-
unsigned long defer_warn;
2117
};
2218

2319
#endif /* __LINUX_NET_XDP_PRIV_H__ */

‎include/trace/events/xdp.h‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -316,39 +316,28 @@ __MEM_TYPE_MAP(__MEM_TYPE_TP_FN)
316316

317317
TRACE_EVENT(mem_disconnect,
318318

319-
TP_PROTO(const struct xdp_mem_allocator *xa,
320-
bool safe_to_remove, bool force),
319+
TP_PROTO(const struct xdp_mem_allocator *xa),
321320

322-
TP_ARGS(xa, safe_to_remove, force),
321+
TP_ARGS(xa),
323322

324323
TP_STRUCT__entry(
325324
__field(const struct xdp_mem_allocator *, xa)
326325
__field(u32, mem_id)
327326
__field(u32, mem_type)
328327
__field(const void *, allocator)
329-
__field(bool, safe_to_remove)
330-
__field(bool, force)
331-
__field(int, disconnect_cnt)
332328
),
333329

334330
TP_fast_assign(
335331
__entry->xa = xa;
336332
__entry->mem_id = xa->mem.id;
337333
__entry->mem_type = xa->mem.type;
338334
__entry->allocator = xa->allocator;
339-
__entry->safe_to_remove = safe_to_remove;
340-
__entry->force = force;
341-
__entry->disconnect_cnt = xa->disconnect_cnt;
342335
),
343336

344-
TP_printk("mem_id=%d mem_type=%s allocator=%p"
345-
" safe_to_remove=%s force=%s disconnect_cnt=%d",
337+
TP_printk("mem_id=%d mem_type=%s allocator=%p",
346338
__entry->mem_id,
347339
__print_symbolic(__entry->mem_type, __MEM_TYPE_SYM_TAB),
348-
__entry->allocator,
349-
__entry->safe_to_remove ? "true" : "false",
350-
__entry->force ? "true" : "false",
351-
__entry->disconnect_cnt
340+
__entry->allocator
352341
)
353342
);
354343

‎net/core/page_pool.c‎

Lines changed: 76 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
#include <trace/events/page_pool.h>
2020

21+
#define DEFER_TIME (msecs_to_jiffies(1000))
22+
#define DEFER_WARN_INTERVAL (60 * HZ)
23+
2124
static int page_pool_init(struct page_pool *pool,
2225
const struct page_pool_params *params)
2326
{
@@ -200,29 +203,22 @@ static s32 page_pool_inflight(struct page_pool *pool)
200203
{
201204
u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
202205
u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
203-
s32 distance;
204-
205-
distance = _distance(hold_cnt, release_cnt);
206-
207-
trace_page_pool_inflight(pool, distance, hold_cnt, release_cnt);
208-
return distance;
209-
}
206+
s32 inflight;
210207

211-
static bool __page_pool_safe_to_destroy(struct page_pool *pool)
212-
{
213-
s32 inflight = page_pool_inflight(pool);
208+
inflight = _distance(hold_cnt, release_cnt);
214209

215-
/* The distance should not be able to become negative */
210+
trace_page_pool_inflight(pool, inflight, hold_cnt, release_cnt);
216211
WARN(inflight < 0, "Negative(%d) inflight packet-pages", inflight);
217212

218-
return (inflight == 0);
213+
return inflight;
219214
}
220215

221216
/* Cleanup page_pool state from page */
222217
static void __page_pool_clean_page(struct page_pool *pool,
223218
struct page *page)
224219
{
225220
dma_addr_t dma;
221+
int count;
226222

227223
if (!(pool->p.flags & PP_FLAG_DMA_MAP))
228224
goto skip_dma_unmap;
@@ -234,9 +230,11 @@ static void __page_pool_clean_page(struct page_pool *pool,
234230
DMA_ATTR_SKIP_CPU_SYNC);
235231
page->dma_addr = 0;
236232
skip_dma_unmap:
237-
atomic_inc(&pool->pages_state_release_cnt);
238-
trace_page_pool_state_release(pool, page,
239-
atomic_read(&pool->pages_state_release_cnt));
233+
/* This may be the last page returned, releasing the pool, so
234+
* it is not safe to reference pool afterwards.
235+
*/
236+
count = atomic_inc_return(&pool->pages_state_release_cnt);
237+
trace_page_pool_state_release(pool, page, count);
240238
}
241239

242240
/* unmap the page and clean our state */
@@ -345,31 +343,10 @@ static void __page_pool_empty_ring(struct page_pool *pool)
345343
}
346344
}
347345

348-
static void __warn_in_flight(struct page_pool *pool)
346+
static void page_pool_free(struct page_pool *pool)
349347
{
350-
u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
351-
u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
352-
s32 distance;
353-
354-
distance = _distance(hold_cnt, release_cnt);
355-
356-
/* Drivers should fix this, but only problematic when DMA is used */
357-
WARN(1, "Still in-flight pages:%d hold:%u released:%u",
358-
distance, hold_cnt, release_cnt);
359-
}
360-
361-
void __page_pool_free(struct page_pool *pool)
362-
{
363-
/* Only last user actually free/release resources */
364-
if (!page_pool_put(pool))
365-
return;
366-
367-
WARN(pool->alloc.count, "API usage violation");
368-
WARN(!ptr_ring_empty(&pool->ring), "ptr_ring is not empty");
369-
370-
/* Can happen due to forced shutdown */
371-
if (!__page_pool_safe_to_destroy(pool))
372-
__warn_in_flight(pool);
348+
if (pool->disconnect)
349+
pool->disconnect(pool);
373350

374351
ptr_ring_cleanup(&pool->ring, NULL);
375352

@@ -378,12 +355,8 @@ void __page_pool_free(struct page_pool *pool)
378355

379356
kfree(pool);
380357
}
381-
EXPORT_SYMBOL(__page_pool_free);
382358

383-
/* Request to shutdown: release pages cached by page_pool, and check
384-
* for in-flight pages
385-
*/
386-
bool __page_pool_request_shutdown(struct page_pool *pool)
359+
static void page_pool_scrub(struct page_pool *pool)
387360
{
388361
struct page *page;
389362

@@ -400,7 +373,64 @@ bool __page_pool_request_shutdown(struct page_pool *pool)
400373
* be in-flight.
401374
*/
402375
__page_pool_empty_ring(pool);
376+
}
377+
378+
static int page_pool_release(struct page_pool *pool)
379+
{
380+
int inflight;
381+
382+
page_pool_scrub(pool);
383+
inflight = page_pool_inflight(pool);
384+
if (!inflight)
385+
page_pool_free(pool);
386+
387+
return inflight;
388+
}
389+
390+
static void page_pool_release_retry(struct work_struct *wq)
391+
{
392+
struct delayed_work *dwq = to_delayed_work(wq);
393+
struct page_pool *pool = container_of(dwq, typeof(*pool), release_dw);
394+
int inflight;
395+
396+
inflight = page_pool_release(pool);
397+
if (!inflight)
398+
return;
399+
400+
/* Periodic warning */
401+
if (time_after_eq(jiffies, pool->defer_warn)) {
402+
int sec = (s32)((u32)jiffies - (u32)pool->defer_start) / HZ;
403+
404+
pr_warn("%s() stalled pool shutdown %d inflight %d sec\n",
405+
__func__, inflight, sec);
406+
pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
407+
}
408+
409+
/* Still not ready to be disconnected, retry later */
410+
schedule_delayed_work(&pool->release_dw, DEFER_TIME);
411+
}
412+
413+
void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *))
414+
{
415+
refcount_inc(&pool->user_cnt);
416+
pool->disconnect = disconnect;
417+
}
418+
419+
void page_pool_destroy(struct page_pool *pool)
420+
{
421+
if (!pool)
422+
return;
423+
424+
if (!page_pool_put(pool))
425+
return;
426+
427+
if (!page_pool_release(pool))
428+
return;
429+
430+
pool->defer_start = jiffies;
431+
pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
403432

404-
return __page_pool_safe_to_destroy(pool);
433+
INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry);
434+
schedule_delayed_work(&pool->release_dw, DEFER_TIME);
405435
}
406-
EXPORT_SYMBOL(__page_pool_request_shutdown);
436+
EXPORT_SYMBOL(page_pool_destroy);

0 commit comments

Comments
 (0)