Skip to content

Commit c14d61f

Browse files
committed
Merge branch 'xdp-redirect-bulk'
Lorenzo Bianconi says: ==================== XDP bulk APIs introduce a defer/flush mechanism to return pages belonging to the same xdp_mem_allocator object (identified via the mem.id field) in bulk to optimize I-cache and D-cache since xdp_return_frame is usually run inside the driver NAPI tx completion loop. Convert mvneta, mvpp2 and mlx5 drivers to xdp_return_frame_bulk APIs. More details on benchmarks run on mlx5 can be found here: https://github.com/xdp-project/xdp-project/blob/master/areas/mem/xdp_bulk_return01.org Changes since v5: - do not keep looping over ptr_ring if the cache is full but release leftover pages running page_pool_return_page Changes since v4: - fix comments - introduce xdp_frame_bulk_init utility routine - compiler annotations for I-cache code layout - move rcu_read_lock outside fast-path - mlx5 xdp bulking code optimization Changes since v3: - align DEV_MAP_BULK_SIZE to XDP_BULK_QUEUE_SIZE - refactor page_pool_put_page_bulk to avoid code duplication Changes since v2: - move mvneta changes in a dedicated patch Changes since v1: - improve comments - rework xdp_return_frame_bulk routine logic - move count and xa fields at the beginning of xdp_frame_bulk struct - invert logic in page_pool_put_page_bulk for loop ==================== Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Jesper Dangaard Brouer <[email protected]>
2 parents 6f10064 + b87c57a commit c14d61f

File tree

7 files changed

+192
-17
lines changed

7 files changed

+192
-17
lines changed

drivers/net/ethernet/marvell/mvneta.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1834,8 +1834,13 @@ static void mvneta_txq_bufs_free(struct mvneta_port *pp,
18341834
struct netdev_queue *nq, bool napi)
18351835
{
18361836
unsigned int bytes_compl = 0, pkts_compl = 0;
1837+
struct xdp_frame_bulk bq;
18371838
int i;
18381839

1840+
xdp_frame_bulk_init(&bq);
1841+
1842+
rcu_read_lock(); /* need for xdp_return_frame_bulk */
1843+
18391844
for (i = 0; i < num; i++) {
18401845
struct mvneta_tx_buf *buf = &txq->buf[txq->txq_get_index];
18411846
struct mvneta_tx_desc *tx_desc = txq->descs +
@@ -1857,9 +1862,12 @@ static void mvneta_txq_bufs_free(struct mvneta_port *pp,
18571862
if (napi && buf->type == MVNETA_TYPE_XDP_TX)
18581863
xdp_return_frame_rx_napi(buf->xdpf);
18591864
else
1860-
xdp_return_frame(buf->xdpf);
1865+
xdp_return_frame_bulk(buf->xdpf, &bq);
18611866
}
18621867
}
1868+
xdp_flush_frame_bulk(&bq);
1869+
1870+
rcu_read_unlock();
18631871

18641872
netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
18651873
}

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2440,8 +2440,13 @@ static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
24402440
struct mvpp2_tx_queue *txq,
24412441
struct mvpp2_txq_pcpu *txq_pcpu, int num)
24422442
{
2443+
struct xdp_frame_bulk bq;
24432444
int i;
24442445

2446+
xdp_frame_bulk_init(&bq);
2447+
2448+
rcu_read_lock(); /* need for xdp_return_frame_bulk */
2449+
24452450
for (i = 0; i < num; i++) {
24462451
struct mvpp2_txq_pcpu_buf *tx_buf =
24472452
txq_pcpu->buffs + txq_pcpu->txq_get_index;
@@ -2454,10 +2459,13 @@ static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
24542459
dev_kfree_skb_any(tx_buf->skb);
24552460
else if (tx_buf->type == MVPP2_TYPE_XDP_TX ||
24562461
tx_buf->type == MVPP2_TYPE_XDP_NDO)
2457-
xdp_return_frame(tx_buf->xdpf);
2462+
xdp_return_frame_bulk(tx_buf->xdpf, &bq);
24582463

24592464
mvpp2_txq_inc_get(txq_pcpu);
24602465
}
2466+
xdp_flush_frame_bulk(&bq);
2467+
2468+
rcu_read_unlock();
24612469
}
24622470

24632471
static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,

drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ mlx5e_xmit_xdp_frame(struct mlx5e_xdpsq *sq, struct mlx5e_xmit_data *xdptxd,
366366
static void mlx5e_free_xdpsq_desc(struct mlx5e_xdpsq *sq,
367367
struct mlx5e_xdp_wqe_info *wi,
368368
u32 *xsk_frames,
369-
bool recycle)
369+
bool recycle,
370+
struct xdp_frame_bulk *bq)
370371
{
371372
struct mlx5e_xdp_info_fifo *xdpi_fifo = &sq->db.xdpi_fifo;
372373
u16 i;
@@ -379,7 +380,7 @@ static void mlx5e_free_xdpsq_desc(struct mlx5e_xdpsq *sq,
379380
/* XDP_TX from the XSK RQ and XDP_REDIRECT */
380381
dma_unmap_single(sq->pdev, xdpi.frame.dma_addr,
381382
xdpi.frame.xdpf->len, DMA_TO_DEVICE);
382-
xdp_return_frame(xdpi.frame.xdpf);
383+
xdp_return_frame_bulk(xdpi.frame.xdpf, bq);
383384
break;
384385
case MLX5E_XDP_XMIT_MODE_PAGE:
385386
/* XDP_TX from the regular RQ */
@@ -397,12 +398,15 @@ static void mlx5e_free_xdpsq_desc(struct mlx5e_xdpsq *sq,
397398

398399
bool mlx5e_poll_xdpsq_cq(struct mlx5e_cq *cq)
399400
{
401+
struct xdp_frame_bulk bq;
400402
struct mlx5e_xdpsq *sq;
401403
struct mlx5_cqe64 *cqe;
402404
u32 xsk_frames = 0;
403405
u16 sqcc;
404406
int i;
405407

408+
xdp_frame_bulk_init(&bq);
409+
406410
sq = container_of(cq, struct mlx5e_xdpsq, cq);
407411

408412
if (unlikely(!test_bit(MLX5E_SQ_STATE_ENABLED, &sq->state)))
@@ -434,7 +438,7 @@ bool mlx5e_poll_xdpsq_cq(struct mlx5e_cq *cq)
434438

435439
sqcc += wi->num_wqebbs;
436440

437-
mlx5e_free_xdpsq_desc(sq, wi, &xsk_frames, true);
441+
mlx5e_free_xdpsq_desc(sq, wi, &xsk_frames, true, &bq);
438442
} while (!last_wqe);
439443

440444
if (unlikely(get_cqe_opcode(cqe) != MLX5_CQE_REQ)) {
@@ -447,6 +451,8 @@ bool mlx5e_poll_xdpsq_cq(struct mlx5e_cq *cq)
447451
}
448452
} while ((++i < MLX5E_TX_CQ_POLL_BUDGET) && (cqe = mlx5_cqwq_get_cqe(&cq->wq)));
449453

454+
xdp_flush_frame_bulk(&bq);
455+
450456
if (xsk_frames)
451457
xsk_tx_completed(sq->xsk_pool, xsk_frames);
452458

@@ -463,8 +469,13 @@ bool mlx5e_poll_xdpsq_cq(struct mlx5e_cq *cq)
463469

464470
void mlx5e_free_xdpsq_descs(struct mlx5e_xdpsq *sq)
465471
{
472+
struct xdp_frame_bulk bq;
466473
u32 xsk_frames = 0;
467474

475+
xdp_frame_bulk_init(&bq);
476+
477+
rcu_read_lock(); /* need for xdp_return_frame_bulk */
478+
468479
while (sq->cc != sq->pc) {
469480
struct mlx5e_xdp_wqe_info *wi;
470481
u16 ci;
@@ -474,9 +485,12 @@ void mlx5e_free_xdpsq_descs(struct mlx5e_xdpsq *sq)
474485

475486
sq->cc += wi->num_wqebbs;
476487

477-
mlx5e_free_xdpsq_desc(sq, wi, &xsk_frames, false);
488+
mlx5e_free_xdpsq_desc(sq, wi, &xsk_frames, false, &bq);
478489
}
479490

491+
xdp_flush_frame_bulk(&bq);
492+
rcu_read_unlock();
493+
480494
if (xsk_frames)
481495
xsk_tx_completed(sq->xsk_pool, xsk_frames);
482496
}

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 */

include/net/xdp.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ struct xdp_frame {
104104
struct net_device *dev_rx; /* used by cpumap */
105105
};
106106

107+
#define XDP_BULK_QUEUE_SIZE 16
108+
struct xdp_frame_bulk {
109+
int count;
110+
void *xa;
111+
void *q[XDP_BULK_QUEUE_SIZE];
112+
};
113+
114+
static __always_inline void xdp_frame_bulk_init(struct xdp_frame_bulk *bq)
115+
{
116+
/* bq->count will be zero'ed when bq->xa gets updated */
117+
bq->xa = NULL;
118+
}
107119

108120
static inline struct skb_shared_info *
109121
xdp_get_shared_info_from_frame(struct xdp_frame *frame)
@@ -194,6 +206,9 @@ struct xdp_frame *xdp_convert_buff_to_frame(struct xdp_buff *xdp)
194206
void xdp_return_frame(struct xdp_frame *xdpf);
195207
void xdp_return_frame_rx_napi(struct xdp_frame *xdpf);
196208
void xdp_return_buff(struct xdp_buff *xdp);
209+
void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq);
210+
void xdp_return_frame_bulk(struct xdp_frame *xdpf,
211+
struct xdp_frame_bulk *bq);
197212

198213
/* When sending xdp_frame into the network stack, then there is no
199214
* return point callback, which is needed to release e.g. DMA-mapping
@@ -245,6 +260,6 @@ bool xdp_attachment_flags_ok(struct xdp_attachment_info *info,
245260
void xdp_attachment_setup(struct xdp_attachment_info *info,
246261
struct netdev_bpf *bpf);
247262

248-
#define DEV_MAP_BULK_SIZE 16
263+
#define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
249264

250265
#endif /* __LINUX_NET_XDP_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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,60 @@ void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)
380380
}
381381
EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);
382382

383+
/* XDP bulk APIs introduce a defer/flush mechanism to return
384+
* pages belonging to the same xdp_mem_allocator object
385+
* (identified via the mem.id field) in bulk to optimize
386+
* I-cache and D-cache.
387+
* The bulk queue size is set to 16 to be aligned to how
388+
* XDP_REDIRECT bulking works. The bulk is flushed when
389+
* it is full or when mem.id changes.
390+
* xdp_frame_bulk is usually stored/allocated on the function
391+
* call-stack to avoid locking penalties.
392+
*/
393+
void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq)
394+
{
395+
struct xdp_mem_allocator *xa = bq->xa;
396+
397+
if (unlikely(!xa || !bq->count))
398+
return;
399+
400+
page_pool_put_page_bulk(xa->page_pool, bq->q, bq->count);
401+
/* bq->xa is not cleared to save lookup, if mem.id same in next bulk */
402+
bq->count = 0;
403+
}
404+
EXPORT_SYMBOL_GPL(xdp_flush_frame_bulk);
405+
406+
/* Must be called with rcu_read_lock held */
407+
void xdp_return_frame_bulk(struct xdp_frame *xdpf,
408+
struct xdp_frame_bulk *bq)
409+
{
410+
struct xdp_mem_info *mem = &xdpf->mem;
411+
struct xdp_mem_allocator *xa;
412+
413+
if (mem->type != MEM_TYPE_PAGE_POOL) {
414+
__xdp_return(xdpf->data, &xdpf->mem, false);
415+
return;
416+
}
417+
418+
xa = bq->xa;
419+
if (unlikely(!xa)) {
420+
xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
421+
bq->count = 0;
422+
bq->xa = xa;
423+
}
424+
425+
if (bq->count == XDP_BULK_QUEUE_SIZE)
426+
xdp_flush_frame_bulk(bq);
427+
428+
if (unlikely(mem->id != xa->mem.id)) {
429+
xdp_flush_frame_bulk(bq);
430+
bq->xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
431+
}
432+
433+
bq->q[bq->count++] = xdpf->data;
434+
}
435+
EXPORT_SYMBOL_GPL(xdp_return_frame_bulk);
436+
383437
void xdp_return_buff(struct xdp_buff *xdp)
384438
{
385439
__xdp_return(xdp->data, &xdp->rxq->mem, true);

0 commit comments

Comments
 (0)