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 <bro...@redhat.com>
Signed-off-by: Lorenzo Bianconi <lore...@kernel.org>
---
 include/net/page_pool.h | 21 +++++++++++++++++++++
 net/core/page_pool.c    | 37 +++++++++++++++++++++++++++++++++++++
 net/core/xdp.c          | 13 ++++++-------
 3 files changed, 64 insertions(+), 7 deletions(-)

diff --git a/include/net/page_pool.h b/include/net/page_pool.h
index 81d7773f96cd..1330419efec7 100644
--- a/include/net/page_pool.h
+++ b/include/net/page_pool.h
@@ -169,6 +169,8 @@ static inline void page_pool_release_page(struct page_pool 
*pool,
 
 void page_pool_put_page(struct page_pool *pool, struct page *page,
                        unsigned int dma_sync_size, bool allow_direct);
+void page_pool_put_page_bulk(struct page_pool *pool, void **data, int count,
+                            bool allow_direct);
 
 /* Same as above but will try to sync the entire area pool->max_len */
 static inline void page_pool_put_full_page(struct page_pool *pool,
@@ -215,4 +217,23 @@ static inline void page_pool_nid_changed(struct page_pool 
*pool, int new_nid)
        if (unlikely(pool->p.nid != new_nid))
                page_pool_update_nid(pool, new_nid);
 }
+
+static inline void page_pool_ring_lock(struct page_pool *pool)
+       __acquires(&pool->ring.producer_lock)
+{
+       if (in_serving_softirq())
+               spin_lock(&pool->ring.producer_lock);
+       else
+               spin_lock_bh(&pool->ring.producer_lock);
+}
+
+static inline void page_pool_ring_unlock(struct page_pool *pool)
+       __releases(&pool->ring.producer_lock)
+{
+       if (in_serving_softirq())
+               spin_unlock(&pool->ring.producer_lock);
+       else
+               spin_unlock_bh(&pool->ring.producer_lock);
+}
+
 #endif /* _NET_PAGE_POOL_H */
diff --git a/net/core/page_pool.c b/net/core/page_pool.c
index ef98372facf6..03c3a92c9179 100644
--- a/net/core/page_pool.c
+++ b/net/core/page_pool.c
@@ -11,6 +11,8 @@
 #include <linux/device.h>
 
 #include <net/page_pool.h>
+#include <net/xdp.h>
+
 #include <linux/dma-direction.h>
 #include <linux/dma-mapping.h>
 #include <linux/page-flags.h>
@@ -408,6 +410,41 @@ void page_pool_put_page(struct page_pool *pool, struct 
page *page,
 }
 EXPORT_SYMBOL(page_pool_put_page);
 
+void page_pool_put_page_bulk(struct page_pool *pool, void **data, int count,
+                            bool allow_direct)
+{
+       struct page *page_ring[XDP_BULK_QUEUE_SIZE];
+       int i, len = 0;
+
+       for (i = 0; i < count; i++) {
+               struct page *page = virt_to_head_page(data[i]);
+
+               if (unlikely(page_ref_count(page) != 1 ||
+                            !pool_page_reusable(pool, page))) {
+                       page_pool_release_page(pool, page);
+                       put_page(page);
+                       continue;
+               }
+
+               if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
+                       page_pool_dma_sync_for_device(pool, page, -1);
+
+               if (allow_direct && in_serving_softirq() &&
+                   page_pool_recycle_in_cache(page, pool))
+                       continue;
+
+               page_ring[len++] = page;
+       }
+
+       page_pool_ring_lock(pool);
+       for (i = 0; i < len; i++) {
+               if (__ptr_ring_produce(&pool->ring, page_ring[i]))
+                       page_pool_return_page(pool, page_ring[i]);
+       }
+       page_pool_ring_unlock(pool);
+}
+EXPORT_SYMBOL(page_pool_put_page_bulk);
+
 static void page_pool_empty_ring(struct page_pool *pool)
 {
        struct page *page;
diff --git a/net/core/xdp.c b/net/core/xdp.c
index b05467a916b4..7ebe159e3835 100644
--- a/net/core/xdp.c
+++ b/net/core/xdp.c
@@ -384,14 +384,13 @@ void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq,
                          bool napi_direct)
 {
        struct xdp_mem_allocator *xa = bq->xa;
-       int i;
 
-       for (i = 0; i < bq->count; i++) {
-               napi_direct &= !xdp_return_frame_no_direct();
-               page_pool_put_full_page(xa->page_pool,
-                                       virt_to_head_page(bq->q[i]),
-                                       napi_direct);
-       }
+       if (unlikely(!bq->count))
+               return;
+
+       napi_direct &= !xdp_return_frame_no_direct();
+       page_pool_put_page_bulk(xa->page_pool, bq->q, bq->count,
+                               napi_direct);
        bq->count = 0;
 }
 EXPORT_SYMBOL_GPL(xdp_flush_frame_bulk);
-- 
2.26.2

Reply via email to