Skip to content

Commit

Permalink
page_pool: do not release pool until inflight == 0.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Acked-by: Jesper Dangaard Brouer <[email protected]>
Acked-by: Ilias Apalodimas <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
jlemon authored and davem330 committed Nov 16, 2019
1 parent 3af7ff9 commit c3f812c
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 183 deletions.
4 changes: 1 addition & 3 deletions drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1503,10 +1503,8 @@ static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
rx_q->dma_erx, rx_q->dma_rx_phy);

kfree(rx_q->buf_pool);
if (rx_q->page_pool) {
page_pool_request_shutdown(rx_q->page_pool);
if (rx_q->page_pool)
page_pool_destroy(rx_q->page_pool);
}
}
}

Expand Down
52 changes: 14 additions & 38 deletions include/net/page_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ struct page_pool_params {
struct page_pool {
struct page_pool_params p;

u32 pages_state_hold_cnt;
struct delayed_work release_dw;
void (*disconnect)(void *);
unsigned long defer_start;
unsigned long defer_warn;

u32 pages_state_hold_cnt;

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

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

void __page_pool_free(struct page_pool *pool);
static inline void page_pool_free(struct page_pool *pool)
{
/* When page_pool isn't compiled-in, net/core/xdp.c doesn't
* allow registering MEM_TYPE_PAGE_POOL, but shield linker.
*/
#ifdef CONFIG_PAGE_POOL
__page_pool_free(pool);
#endif
}

/* Drivers use this instead of page_pool_free */
void page_pool_destroy(struct page_pool *pool);
void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *));
#else
static inline void page_pool_destroy(struct page_pool *pool)
{
if (!pool)
return;
}

page_pool_free(pool);
static inline void page_pool_use_xdp_mem(struct page_pool *pool,
void (*disconnect)(void *))
{
}
#endif

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

/* API user MUST have disconnected alloc-side (not allowed to call
* page_pool_alloc_pages()) before calling this. The free-side can
* still run concurrently, to handle in-flight packet-pages.
*
* A request to shutdown can fail (with false) if there are still
* in-flight packet-pages.
*/
bool __page_pool_request_shutdown(struct page_pool *pool);
static inline bool page_pool_request_shutdown(struct page_pool *pool)
{
bool safe_to_remove = false;

#ifdef CONFIG_PAGE_POOL
safe_to_remove = __page_pool_request_shutdown(pool);
#endif
return safe_to_remove;
}

/* 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
Expand Down Expand Up @@ -216,11 +197,6 @@ static inline bool is_page_pool_compiled_in(void)
#endif
}

static inline void page_pool_get(struct page_pool *pool)
{
refcount_inc(&pool->user_cnt);
}

static inline bool page_pool_put(struct page_pool *pool)
{
return refcount_dec_and_test(&pool->user_cnt);
Expand Down
4 changes: 0 additions & 4 deletions include/net/xdp_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ struct xdp_mem_allocator {
struct page_pool *page_pool;
struct zero_copy_allocator *zc_alloc;
};
int disconnect_cnt;
unsigned long defer_start;
struct rhash_head node;
struct rcu_head rcu;
struct delayed_work defer_wq;
unsigned long defer_warn;
};

#endif /* __LINUX_NET_XDP_PRIV_H__ */
19 changes: 4 additions & 15 deletions include/trace/events/xdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,39 +317,28 @@ __MEM_TYPE_MAP(__MEM_TYPE_TP_FN)

TRACE_EVENT(mem_disconnect,

TP_PROTO(const struct xdp_mem_allocator *xa,
bool safe_to_remove, bool force),
TP_PROTO(const struct xdp_mem_allocator *xa),

TP_ARGS(xa, safe_to_remove, force),
TP_ARGS(xa),

TP_STRUCT__entry(
__field(const struct xdp_mem_allocator *, xa)
__field(u32, mem_id)
__field(u32, mem_type)
__field(const void *, allocator)
__field(bool, safe_to_remove)
__field(bool, force)
__field(int, disconnect_cnt)
),

TP_fast_assign(
__entry->xa = xa;
__entry->mem_id = xa->mem.id;
__entry->mem_type = xa->mem.type;
__entry->allocator = xa->allocator;
__entry->safe_to_remove = safe_to_remove;
__entry->force = force;
__entry->disconnect_cnt = xa->disconnect_cnt;
),

TP_printk("mem_id=%d mem_type=%s allocator=%p"
" safe_to_remove=%s force=%s disconnect_cnt=%d",
TP_printk("mem_id=%d mem_type=%s allocator=%p",
__entry->mem_id,
__print_symbolic(__entry->mem_type, __MEM_TYPE_SYM_TAB),
__entry->allocator,
__entry->safe_to_remove ? "true" : "false",
__entry->force ? "true" : "false",
__entry->disconnect_cnt
__entry->allocator
)
);

Expand Down
122 changes: 76 additions & 46 deletions net/core/page_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

#include <trace/events/page_pool.h>

#define DEFER_TIME (msecs_to_jiffies(1000))
#define DEFER_WARN_INTERVAL (60 * HZ)

static int page_pool_init(struct page_pool *pool,
const struct page_pool_params *params)
{
Expand Down Expand Up @@ -193,29 +196,22 @@ static s32 page_pool_inflight(struct page_pool *pool)
{
u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
s32 distance;

distance = _distance(hold_cnt, release_cnt);

trace_page_pool_inflight(pool, distance, hold_cnt, release_cnt);
return distance;
}
s32 inflight;

static bool __page_pool_safe_to_destroy(struct page_pool *pool)
{
s32 inflight = page_pool_inflight(pool);
inflight = _distance(hold_cnt, release_cnt);

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

return (inflight == 0);
return inflight;
}

/* Cleanup page_pool state from page */
static void __page_pool_clean_page(struct page_pool *pool,
struct page *page)
{
dma_addr_t dma;
int count;

if (!(pool->p.flags & PP_FLAG_DMA_MAP))
goto skip_dma_unmap;
Expand All @@ -227,9 +223,11 @@ static void __page_pool_clean_page(struct page_pool *pool,
DMA_ATTR_SKIP_CPU_SYNC);
page->dma_addr = 0;
skip_dma_unmap:
atomic_inc(&pool->pages_state_release_cnt);
trace_page_pool_state_release(pool, page,
atomic_read(&pool->pages_state_release_cnt));
/* This may be the last page returned, releasing the pool, so
* it is not safe to reference pool afterwards.
*/
count = atomic_inc_return(&pool->pages_state_release_cnt);
trace_page_pool_state_release(pool, page, count);
}

/* unmap the page and clean our state */
Expand Down Expand Up @@ -338,31 +336,10 @@ static void __page_pool_empty_ring(struct page_pool *pool)
}
}

static void __warn_in_flight(struct page_pool *pool)
static void page_pool_free(struct page_pool *pool)
{
u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
s32 distance;

distance = _distance(hold_cnt, release_cnt);

/* Drivers should fix this, but only problematic when DMA is used */
WARN(1, "Still in-flight pages:%d hold:%u released:%u",
distance, hold_cnt, release_cnt);
}

void __page_pool_free(struct page_pool *pool)
{
/* Only last user actually free/release resources */
if (!page_pool_put(pool))
return;

WARN(pool->alloc.count, "API usage violation");
WARN(!ptr_ring_empty(&pool->ring), "ptr_ring is not empty");

/* Can happen due to forced shutdown */
if (!__page_pool_safe_to_destroy(pool))
__warn_in_flight(pool);
if (pool->disconnect)
pool->disconnect(pool);

ptr_ring_cleanup(&pool->ring, NULL);

Expand All @@ -371,12 +348,8 @@ void __page_pool_free(struct page_pool *pool)

kfree(pool);
}
EXPORT_SYMBOL(__page_pool_free);

/* Request to shutdown: release pages cached by page_pool, and check
* for in-flight pages
*/
bool __page_pool_request_shutdown(struct page_pool *pool)
static void page_pool_scrub(struct page_pool *pool)
{
struct page *page;

Expand All @@ -393,7 +366,64 @@ bool __page_pool_request_shutdown(struct page_pool *pool)
* be in-flight.
*/
__page_pool_empty_ring(pool);
}

static int page_pool_release(struct page_pool *pool)
{
int inflight;

page_pool_scrub(pool);
inflight = page_pool_inflight(pool);
if (!inflight)
page_pool_free(pool);

return inflight;
}

static void page_pool_release_retry(struct work_struct *wq)
{
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);
}

void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *))
{
refcount_inc(&pool->user_cnt);
pool->disconnect = disconnect;
}

void page_pool_destroy(struct page_pool *pool)
{
if (!pool)
return;

if (!page_pool_put(pool))
return;

if (!page_pool_release(pool))
return;

pool->defer_start = jiffies;
pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;

return __page_pool_safe_to_destroy(pool);
INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry);
schedule_delayed_work(&pool->release_dw, DEFER_TIME);
}
EXPORT_SYMBOL(__page_pool_request_shutdown);
EXPORT_SYMBOL(page_pool_destroy);
Loading

0 comments on commit c3f812c

Please sign in to comment.