Skip to content

Commit 7886244

Browse files
LorenzoBianconiborkmann
authored andcommitted
net: page_pool: Add bulk support for ptr_ring
Introduce the capability to batch page_pool ptr_ring refill since it is usually run inside the driver NAPI tx completion loop. Suggested-by: Jesper Dangaard Brouer <[email protected]> Co-developed-by: Jesper Dangaard Brouer <[email protected]> Signed-off-by: Jesper Dangaard Brouer <[email protected]> Signed-off-by: Lorenzo Bianconi <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Ilias Apalodimas <[email protected]> Link: https://lore.kernel.org/bpf/08dd249c9522c001313f520796faa777c4089e1c.1605267335.git.lorenzo@kernel.org
1 parent 8965398 commit 7886244

File tree

3 files changed

+88
-17
lines changed

3 files changed

+88
-17
lines changed

include/net/page_pool.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ struct page_pool *page_pool_create(const struct page_pool_params *params);
152152
void page_pool_destroy(struct page_pool *pool);
153153
void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *));
154154
void page_pool_release_page(struct page_pool *pool, struct page *page);
155+
void page_pool_put_page_bulk(struct page_pool *pool, void **data,
156+
int count);
155157
#else
156158
static inline void page_pool_destroy(struct page_pool *pool)
157159
{
@@ -165,6 +167,11 @@ static inline void page_pool_release_page(struct page_pool *pool,
165167
struct page *page)
166168
{
167169
}
170+
171+
static inline void page_pool_put_page_bulk(struct page_pool *pool, void **data,
172+
int count)
173+
{
174+
}
168175
#endif
169176

170177
void page_pool_put_page(struct page_pool *pool, struct page *page,
@@ -215,4 +222,23 @@ static inline void page_pool_nid_changed(struct page_pool *pool, int new_nid)
215222
if (unlikely(pool->p.nid != new_nid))
216223
page_pool_update_nid(pool, new_nid);
217224
}
225+
226+
static inline void page_pool_ring_lock(struct page_pool *pool)
227+
__acquires(&pool->ring.producer_lock)
228+
{
229+
if (in_serving_softirq())
230+
spin_lock(&pool->ring.producer_lock);
231+
else
232+
spin_lock_bh(&pool->ring.producer_lock);
233+
}
234+
235+
static inline void page_pool_ring_unlock(struct page_pool *pool)
236+
__releases(&pool->ring.producer_lock)
237+
{
238+
if (in_serving_softirq())
239+
spin_unlock(&pool->ring.producer_lock);
240+
else
241+
spin_unlock_bh(&pool->ring.producer_lock);
242+
}
243+
218244
#endif /* _NET_PAGE_POOL_H */

net/core/page_pool.c

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <linux/device.h>
1212

1313
#include <net/page_pool.h>
14+
#include <net/xdp.h>
15+
1416
#include <linux/dma-direction.h>
1517
#include <linux/dma-mapping.h>
1618
#include <linux/page-flags.h>
@@ -362,8 +364,9 @@ static bool pool_page_reusable(struct page_pool *pool, struct page *page)
362364
* If the page refcnt != 1, then the page will be returned to memory
363365
* subsystem.
364366
*/
365-
void page_pool_put_page(struct page_pool *pool, struct page *page,
366-
unsigned int dma_sync_size, bool allow_direct)
367+
static __always_inline struct page *
368+
__page_pool_put_page(struct page_pool *pool, struct page *page,
369+
unsigned int dma_sync_size, bool allow_direct)
367370
{
368371
/* This allocator is optimized for the XDP mode that uses
369372
* one-frame-per-page, but have fallbacks that act like the
@@ -379,15 +382,12 @@ void page_pool_put_page(struct page_pool *pool, struct page *page,
379382
page_pool_dma_sync_for_device(pool, page,
380383
dma_sync_size);
381384

382-
if (allow_direct && in_serving_softirq())
383-
if (page_pool_recycle_in_cache(page, pool))
384-
return;
385+
if (allow_direct && in_serving_softirq() &&
386+
page_pool_recycle_in_cache(page, pool))
387+
return NULL;
385388

386-
if (!page_pool_recycle_in_ring(pool, page)) {
387-
/* Cache full, fallback to free pages */
388-
page_pool_return_page(pool, page);
389-
}
390-
return;
389+
/* Page found as candidate for recycling */
390+
return page;
391391
}
392392
/* Fallback/non-XDP mode: API user have elevated refcnt.
393393
*
@@ -405,9 +405,59 @@ void page_pool_put_page(struct page_pool *pool, struct page *page,
405405
/* Do not replace this with page_pool_return_page() */
406406
page_pool_release_page(pool, page);
407407
put_page(page);
408+
409+
return NULL;
410+
}
411+
412+
void page_pool_put_page(struct page_pool *pool, struct page *page,
413+
unsigned int dma_sync_size, bool allow_direct)
414+
{
415+
page = __page_pool_put_page(pool, page, dma_sync_size, allow_direct);
416+
if (page && !page_pool_recycle_in_ring(pool, page)) {
417+
/* Cache full, fallback to free pages */
418+
page_pool_return_page(pool, page);
419+
}
408420
}
409421
EXPORT_SYMBOL(page_pool_put_page);
410422

423+
/* Caller must not use data area after call, as this function overwrites it */
424+
void page_pool_put_page_bulk(struct page_pool *pool, void **data,
425+
int count)
426+
{
427+
int i, bulk_len = 0;
428+
429+
for (i = 0; i < count; i++) {
430+
struct page *page = virt_to_head_page(data[i]);
431+
432+
page = __page_pool_put_page(pool, page, -1, false);
433+
/* Approved for bulk recycling in ptr_ring cache */
434+
if (page)
435+
data[bulk_len++] = page;
436+
}
437+
438+
if (unlikely(!bulk_len))
439+
return;
440+
441+
/* Bulk producer into ptr_ring page_pool cache */
442+
page_pool_ring_lock(pool);
443+
for (i = 0; i < bulk_len; i++) {
444+
if (__ptr_ring_produce(&pool->ring, data[i]))
445+
break; /* ring full */
446+
}
447+
page_pool_ring_unlock(pool);
448+
449+
/* Hopefully all pages was return into ptr_ring */
450+
if (likely(i == bulk_len))
451+
return;
452+
453+
/* ptr_ring cache full, free remaining pages outside producer lock
454+
* since put_page() with refcnt == 1 can be an expensive operation
455+
*/
456+
for (; i < bulk_len; i++)
457+
page_pool_return_page(pool, data[i]);
458+
}
459+
EXPORT_SYMBOL(page_pool_put_page_bulk);
460+
411461
static void page_pool_empty_ring(struct page_pool *pool)
412462
{
413463
struct page *page;

net/core/xdp.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -393,16 +393,11 @@ EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);
393393
void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq)
394394
{
395395
struct xdp_mem_allocator *xa = bq->xa;
396-
int i;
397396

398-
if (unlikely(!xa))
397+
if (unlikely(!xa || !bq->count))
399398
return;
400399

401-
for (i = 0; i < bq->count; i++) {
402-
struct page *page = virt_to_head_page(bq->q[i]);
403-
404-
page_pool_put_full_page(xa->page_pool, page, false);
405-
}
400+
page_pool_put_page_bulk(xa->page_pool, bq->q, bq->count);
406401
/* bq->xa is not cleared to save lookup, if mem.id same in next bulk */
407402
bq->count = 0;
408403
}

0 commit comments

Comments
 (0)