octeontx2-pf: Use the napi_alloc_frag() to alloc the pool buffers
PATCH 1/1 octeontx2-pf: Use the napi_alloc_frag() to alloc the pool buffers
In the current codes, the octeontx2 uses its own method to allocate the pool buffers, but there are some issues in this implementation.
1. We have to run the otx2_get_page() for each allocation cycle and this is pretty error prone. As I can see there is no invocation of the otx2_get_page() in otx2_pool_refill_task(), this will leave the allocated pages have the wrong refcount and may be freed wrongly.
2. It wastes memory. For example, if we only receive one packet in a NAPI RX cycle, and then allocate a 2K buffer with otx2_alloc_rbuf() to refill the pool buffers and leave the remain area of the allocated page wasted. On a kernel with 64K page, 62K area is wasted.
IMHO it is really unnecessary to implement our own method for the buffers allocate, we can reuse the napi_alloc_frag() to simplify our code.
--- .../marvell/octeontx2/nic/otx2_common.c | 51 ++++++++----------- .../marvell/octeontx2/nic/otx2_common.h | 15 +----- .../marvell/octeontx2/nic/otx2_txrx.c | 3 +- .../marvell/octeontx2/nic/otx2_txrx.h | 4 -- 4 files changed, 22 insertions(+), 51 deletions(-)
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +20 -31
@@ -379,40 +379,32 @@ void otx2_config_irq_coalescing(struct otx2_nic *pfvf, int qidx) (pfvf->hw.cq_ecount_wait - 1)); } -dma_addr_t otx2_alloc_rbuf (struct otx2_nic *pfvf,struct otx2_pool *pool, -gfp_t gfp) +dma_addr_t _otx2_alloc_rbuf (struct otx2_nic *pfvf,struct otx2_pool *pool) {dma_addr_t iova; +u8 *buf; -/* Check if request can be accommodated in previous allocated page */ -if (pool->page && ((pool->page_offset + pool->rbsize) <= - (PAGE_SIZE << pool->rbpage_order))) { - pool->pageref++; - goto ret; - } - -otx2_get_page (pool); - -/* Allocate a new page */ - pool->page =alloc_pages (gfp | __GFP_COMP | __GFP_NOWARN, - pool->rbpage_order); -if (unlikely (!pool->page)) + buf =napi_alloc_frag (pool->rbsize); +if (unlikely (!buf))return -ENOMEM ; - pool->page_offset =0 ; -ret: - iova = (u64 )otx2_dma_map_page (pfvf, pool->page, pool->page_offset, - pool->rbsize,DMA_FROM_DEVICE ); -if (!iova) { -if (!pool->page_offset) -__free_pages (pool->page, pool->rbpage_order); - pool->page =NULL ; + iova =dma_map_single (pfvf->dev, buf, pool->rbsize,DMA_FROM_DEVICE ); +if (unlikely (dma_mapping_error (pfvf->dev, iova)))return -ENOMEM ; - } - pool->page_offset += pool->rbsize; +return iova; } +static dma_addr_t otx2_alloc_rbuf (struct otx2_nic *pfvf,struct otx2_pool *pool) +{ +dma_addr_t addr; + +local_bh_disable (); + addr =_otx2_alloc_rbuf (pfvf, pool); +local_bh_enable (); +return addr; +} +void otx2_tx_timeout (struct net_device *netdev,unsigned int txq) {struct otx2_nic *pfvf =netdev_priv (netdev);
@@ -805,7 +797,7 @@ static void otx2_pool_refill_task(struct work_struct *work) free_ptrs = cq->pool_ptrs;while (cq->pool_ptrs) { - bufptr =otx2_alloc_rbuf (pfvf, rbpool,GFP_KERNEL ); + bufptr =otx2_alloc_rbuf (pfvf, rbpool);if (bufptr <=0 ) {/* Schedule a WQ if we fails to free atleast half of the * pointers else enable napi for this RQ.
@@ -1064,7 +1056,6 @@ static int otx2_pool_init(struct otx2_nic *pfvf, u16 pool_id,return err; pool->rbsize = buf_size; - pool->rbpage_order =get_order (buf_size);/* Initialize this pool's context via AF */ aq =otx2_mbox_alloc_msg_npa_aq_enq (&pfvf->mbox);
@@ -1152,13 +1143,12 @@ int otx2_sq_aura_pool_init(struct otx2_nic *pfvf)return -ENOMEM ;for (ptr =0 ; ptr < num_sqbs; ptr++) { - bufptr =otx2_alloc_rbuf (pfvf, pool,GFP_KERNEL ); + bufptr =otx2_alloc_rbuf (pfvf, pool);if (bufptr <=0 )return bufptr;otx2_aura_freeptr (pfvf, pool_id, bufptr); sq->sqb_ptrs[sq->sqb_count++] = (u64 )bufptr; } -otx2_get_page (pool); }return 0 ;
@@ -1204,13 +1194,12 @@ int otx2_rq_aura_pool_init(struct otx2_nic *pfvf)for (pool_id =0 ; pool_id < hw->rqpool_cnt; pool_id++) { pool = &pfvf->qset.pool[pool_id];for (ptr =0 ; ptr < num_ptrs; ptr++) { - bufptr =otx2_alloc_rbuf (pfvf, pool,GFP_KERNEL ); + bufptr =otx2_alloc_rbuf (pfvf, pool);if (bufptr <=0 )return bufptr;otx2_aura_freeptr (pfvf, pool_id, bufptr +OTX2_HEAD_ROOM ); } -otx2_get_page (pool); }return 0 ;
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.h +1 -14
@@ -434,18 +434,6 @@ static inline void otx2_aura_freeptr(struct otx2_nic *pfvf, otx2_get_regaddr(pfvf, NPA_LF_AURA_OP_FREE0)); } -/* Update page ref count */ -static inline void otx2_get_page (struct otx2_pool *pool) -{ -if (!pool->page) -return ; - -if (pool->pageref) -page_ref_add (pool->page, pool->pageref); - pool->pageref =0 ; - pool->page =NULL ; -} -static inline int otx2_get_pool_idx (struct otx2_nic *pfvf,int type,int idx) {if (type ==AURA_NIX_SQ )
@@ -589,8 +577,7 @@ int otx2_txschq_config(struct otx2_nic *pfvf, int lvl);int otx2_txsch_alloc (struct otx2_nic *pfvf);int otx2_txschq_stop (struct otx2_nic *pfvf);void otx2_sqb_flush (struct otx2_nic *pfvf); -dma_addr_t otx2_alloc_rbuf (struct otx2_nic *pfvf,struct otx2_pool *pool, -gfp_t gfp); +dma_addr_t _otx2_alloc_rbuf (struct otx2_nic *pfvf,struct otx2_pool *pool);int otx2_rxtx_enable (struct otx2_nic *pfvf,bool enable);void otx2_ctx_disable (struct mbox *mbox,int type,bool npa);int otx2_nix_config_bp (struct otx2_nic *pfvf,bool enable);
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +1 -2
@@ -286,7 +286,7 @@ static int otx2_rx_napi_handler(struct otx2_nic *pfvf,/* Refill pool with new buffers */ while (cq->pool_ptrs) { - bufptr =otx2_alloc_rbuf (pfvf, cq->rbpool,GFP_ATOMIC ); + bufptr =_otx2_alloc_rbuf (pfvf, cq->rbpool);if (unlikely (bufptr <=0 )) {struct refill_work *work;struct delayed_work *dwork;
@@ -304,7 +304,6 @@ static int otx2_rx_napi_handler(struct otx2_nic *pfvf,otx2_aura_freeptr (pfvf, cq->cq_idx, bufptr +OTX2_HEAD_ROOM ); cq->pool_ptrs--; } -otx2_get_page (cq->rbpool);return processed_cqe; }
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.h +0 -4
@@ -113,11 +113,7 @@ struct otx2_cq_poll {struct otx2_pool {struct qmem *stack;struct qmem *fc_addr; -u8 rbpage_order;u16 rbsize; -u32 page_offset; -u16 pageref; -struct page *page; };struct otx2_cq_queue {
Signature · 1 line
2.26.0
Quoted 7 lines · Kevin Hao
In the current codes, the octeontx2 uses its own method to allocate the pool buffers, but there are some issues in this implementation.
1. We have to run theotx2_get_page()for each allocation cycle and this is pretty error prone. As I can see there is no invocation of theotx2_get_page()inotx2_pool_refill_task(), this will leave the allocated pages have the wrong refcount and may be freed wrongly.
Thanks for pointing, will fix.
Quoted 10 lines
2. It wastes memory. For example, if we only receive one packet in a NAPI RX cycle, and then allocate a 2K buffer with
otx2_alloc_rbuf()to refill the pool buffers and leave the remain area of the allocated page wasted. On a kernel with 64K page, 62K area is wasted.IMHO it is really unnecessary to implement our own method for the buffers allocate, we can reuse the
napi_alloc_frag()to simplify our code.Signed-off-by Kevin Hao <[email protected]>
Have you measured performance with and without your patch ?
I didn't use napi_alloc_frag() as it's too costly, if in one NAPI instance driver
receives 32 pkts, then 32 calls to napi_alloc_frag() and updates to page ref count per fragment etc are costly. When traffic rate is less then it may not matter
much.
Thanks,
Sunil.
Quoted 16 lines · Sunil Kovvuri
Quoted 7 lines · Kevin Hao
In the current codes, the octeontx2 uses its own method to allocate the pool buffers, but there are some issues in this implementation.
1. We have to run theotx2_get_page()for each allocation cycle and this is pretty error prone. As I can see there is no invocation of theotx2_get_page()inotx2_pool_refill_task(), this will leave the allocated pages have the wrong refcount and may be freed wrongly.Thanks for pointing, will fix.
Quoted 5 lines
2. It wastes memory. For example, if we only receive one packet in a NAPI RX cycle, and then allocate a 2K buffer with
otx2_alloc_rbuf()to refill the pool buffers and leave the remain area of the allocated page wasted. On a kernel with 64K page, 62K area is wasted.
Quoted 7 lines
Quoted 5 lines
IMHO it is really unnecessary to implement our own method for the buffers allocate, we can reuse the
napi_alloc_frag()to simplify our code.Signed-off-by Kevin Hao <[email protected]>Have you measured performance with and without your patch ?
I will do performance compare later. But I don't think there will be measurable difference.
Quoted 4 lines
I didn't use
napi_alloc_frag()as it's too costly, if in one NAPI instance driver
receives 32 pkts, then 32 calls tonapi_alloc_frag()and updates to page ref count per fragment etc are costly.
No, the page ref only be updated at the page allocation and all the space are used. In general, the invocation of napi_alloc_frag() will not cause the update of the page ref. So in theory, the count of updating page ref should be reduced by using of napi_alloc_frag() compare to the current otx2 implementation.
Thanks,
Kevin
Quoted 6 lines
When traffic rate is less then it
may not matter
much.Thanks,
Sunil.
Sunil Kovvuri
Tested-by Sunil GouthamQuoted 34 lines · Kevin Hao
Quoted 16 lines · Sunil Kovvuri
Quoted 7 lines · Kevin Hao
In the current codes, the octeontx2 uses its own method to allocate the pool buffers, but there are some issues in this implementation.
1. We have to run theotx2_get_page()for each allocation cycle and this is pretty error prone. As I can see there is no invocation of theotx2_get_page()inotx2_pool_refill_task(), this will leave the allocated pages have the wrong refcount and may be freed wrongly.Thanks for pointing, will fix.
Quoted 5 lines
2. It wastes memory. For example, if we only receive one packet in a NAPI RX cycle, and then allocate a 2K buffer with
otx2_alloc_rbuf()to refill the pool buffers and leave the remain area of the allocated page wasted. On a kernel with 64K page, 62K area is wasted.Quoted 7 lines
Quoted 5 lines
IMHO it is really unnecessary to implement our own method for the buffers allocate, we can reuse the
napi_alloc_frag()to simplify our code.Signed-off-by Kevin Hao <[email protected]>Have you measured performance with and without your patch ?
I will do performance compare later. But I don't think there will be measurable difference.
Quoted 4 lines
I didn't use
napi_alloc_frag()as it's too costly, if in one NAPI instance driver
receives 32 pkts, then 32 calls tonapi_alloc_frag()and updates to page ref count per fragment etc are costly.
Quoted 5 lines
No, the page ref only be updated at the page allocation and all the space are used. In general, the invocation of
napi_alloc_frag()will not cause the update of the page ref. So in theory, the count of updating page ref should be reduced by using ofnapi_alloc_frag()compare to the current otx2 implementation.
Okay, it seems i misunderstood it. Have tested this patch.
Quoted 57 lines · Kevin Hao
In the current codes, the octeontx2 uses its own method to allocate the pool buffers, but there are some issues in this implementation.
1. We have to run theotx2_get_page()for each allocation cycle and this is pretty error prone. As I can see there is no invocation of theotx2_get_page()inotx2_pool_refill_task(), this will leave the allocated pages have the wrong refcount and may be freed wrongly.
2. It wastes memory. For example, if we only receive one packet in a NAPI RX cycle, and then allocate a 2K buffer withotx2_alloc_rbuf()to refill the pool buffers and leave the remain area of the allocated page wasted. On a kernel with 64K page, 62K area is wasted.IMHO it is really unnecessary to implement our own method for the buffers allocate, we can reuse the
napi_alloc_frag()to simplify our code.Signed-off-by Kevin Hao <[email protected]>--- .../marvell/octeontx2/nic/otx2_common.c | 51 ++++++++----------- .../marvell/octeontx2/nic/otx2_common.h | 15 +----- .../marvell/octeontx2/nic/otx2_txrx.c | 3 +- .../marvell/octeontx2/nic/otx2_txrx.h | 4 -- 4 files changed, 22 insertions(+), 51 deletions(-)drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +4
-16@@ -379,40 +379,32 @@ void otx2_config_irq_coalescing(struct otx2_nic *pfvf, int qidx) (pfvf->hw.cq_ecount_wait - 1)); } -dma_addr_t otx2_alloc_rbuf (struct otx2_nic *pfvf,struct otx2_pool *pool, -gfp_t gfp) +dma_addr_t _otx2_alloc_rbuf (struct otx2_nic *pfvf,struct otx2_pool *pool) {dma_addr_t iova; +u8 *buf; -/* Check if request can be accommodated in previous allocated page */ -if (pool->page && ((pool->page_offset + pool->rbsize) <= - (PAGE_SIZE << pool->rbpage_order))) { - pool->pageref++; - goto ret; - } - -otx2_get_page (pool); - -/* Allocate a new page */ - pool->page =alloc_pages (gfp | __GFP_COMP | __GFP_NOWARN, - pool->rbpage_order); -if (unlikely (!pool->page)) + buf =napi_alloc_frag (pool->rbsize); +if (unlikely (!buf))return -ENOMEM ; - pool->page_offset =0 ;
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c void otx2_config_irq_coalescing(struct otx2_nic *pfvf, int qidx) · line 393
Quoted 10 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +2
-7-ret: - iova = (u64 )otx2_dma_map_page (pfvf, pool->page, pool->page_offset, - pool->rbsize,DMA_FROM_DEVICE ); -if (!iova) { -if (!pool->page_offset) -__free_pages (pool->page, pool->rbpage_order); - pool->page =NULL ; + iova =dma_map_single (pfvf->dev, buf, pool->rbsize,DMA_FROM_DEVICE ); +if (unlikely (dma_mapping_error (pfvf->dev, iova)))return -ENOMEM ;
Use DMA_ATTR_SKIP_CPU_SYNC while mapping the buffer.
Thanks,
Sunil.
Quoted 57 lines · Sunil Kovvuri
Quoted 56 lines · Kevin Hao
In the current codes, the octeontx2 uses its own method to allocate the pool buffers, but there are some issues in this implementation.
1. We have to run theotx2_get_page()for each allocation cycle and this is pretty error prone. As I can see there is no invocation of theotx2_get_page()inotx2_pool_refill_task(), this will leave the allocated pages have the wrong refcount and may be freed wrongly.
2. It wastes memory. For example, if we only receive one packet in a NAPI RX cycle, and then allocate a 2K buffer withotx2_alloc_rbuf()to refill the pool buffers and leave the remain area of the allocated page wasted. On a kernel with 64K page, 62K area is wasted.IMHO it is really unnecessary to implement our own method for the buffers allocate, we can reuse the
napi_alloc_frag()to simplify our code.Signed-off-by Kevin Hao <[email protected]>--- .../marvell/octeontx2/nic/otx2_common.c | 51 ++++++++----------- .../marvell/octeontx2/nic/otx2_common.h | 15 +----- .../marvell/octeontx2/nic/otx2_txrx.c | 3 +- .../marvell/octeontx2/nic/otx2_txrx.h | 4 -- 4 files changed, 22 insertions(+), 51 deletions(-)drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +4
-15@@ -379,40 +379,32 @@ void otx2_config_irq_coalescing(struct otx2_nic *pfvf, int qidx) (pfvf->hw.cq_ecount_wait - 1)); } -dma_addr_t otx2_alloc_rbuf (struct otx2_nic *pfvf,struct otx2_pool *pool, -gfp_t gfp) +dma_addr_t _otx2_alloc_rbuf (struct otx2_nic *pfvf,struct otx2_pool *pool) {dma_addr_t iova; +u8 *buf; -/* Check if request can be accommodated in previous allocated page */ -if (pool->page && ((pool->page_offset + pool->rbsize) <= - (PAGE_SIZE << pool->rbpage_order))) { - pool->pageref++; - goto ret; - } - -otx2_get_page (pool); - -/* Allocate a new page */ - pool->page =alloc_pages (gfp | __GFP_COMP | __GFP_NOWARN, - pool->rbpage_order); -if (unlikely (!pool->page)) + buf =napi_alloc_frag (pool->rbsize); +if (unlikely (!buf))return -ENOMEM ;
Quoted 13 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c void otx2_config_irq_coalescing(struct otx2_nic *pfvf, int qidx) · line 393
Quoted 11 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +2
-8- pool->page_offset =0 ; -ret: - iova = (u64 )otx2_dma_map_page (pfvf, pool->page, pool->page_offset, - pool->rbsize,DMA_FROM_DEVICE ); -if (!iova) { -if (!pool->page_offset) -__free_pages (pool->page, pool->rbpage_order); - pool->page =NULL ; + iova =dma_map_single (pfvf->dev, buf, pool->rbsize,DMA_FROM_DEVICE ); +if (unlikely (dma_mapping_error (pfvf->dev, iova)))return -ENOMEM ;Use
DMA_ATTR_SKIP_CPU_SYNCwhile mapping the buffer.
Sure. V2 is coming.
Thanks,
Kevin
Quoted 3 lines
Thanks,
Sunil.
Quoted 35 lines · Sunil Kovvuri
Quoted 34 lines · Kevin Hao
Quoted 16 lines · Sunil Kovvuri
Quoted 7 lines · Kevin Hao
In the current codes, the octeontx2 uses its own method to allocate the pool buffers, but there are some issues in this implementation.
1. We have to run theotx2_get_page()for each allocation cycle and this is pretty error prone. As I can see there is no invocation of theotx2_get_page()inotx2_pool_refill_task(), this will leave the allocated pages have the wrong refcount and may be freed wrongly.Thanks for pointing, will fix.
Quoted 5 lines
2. It wastes memory. For example, if we only receive one packet in a NAPI RX cycle, and then allocate a 2K buffer with
otx2_alloc_rbuf()to refill the pool buffers and leave the remain area of the allocated page wasted. On a kernel with 64K page, 62K area is wasted.Quoted 7 lines
Quoted 5 lines
IMHO it is really unnecessary to implement our own method for the buffers allocate, we can reuse the
napi_alloc_frag()to simplify our code.Signed-off-by Kevin Hao <[email protected]>Have you measured performance with and without your patch ?
I will do performance compare later. But I don't think there will be measurable difference.
Quoted 4 lines
I didn't use
napi_alloc_frag()as it's too costly, if in one NAPI instance driver
receives 32 pkts, then 32 calls tonapi_alloc_frag()and updates to page ref count per fragment etc are costly.
Quoted 7 lines
Quoted 5 lines
No, the page ref only be updated at the page allocation and all the space are used. In general, the invocation of
napi_alloc_frag()will not cause the update of the page ref. So in theory, the count of updating page ref should be reduced by using ofnapi_alloc_frag()compare to the current otx2 implementation.Okay, it seems i misunderstood it.
Hi Sunil
In general, you should not work around issues in the core, you should improve the core. If your implementation really was more efficient than the core code, it would of been better if you proposed fixes to the core, not hide away better code in your own driver.
Andrew
Quoted 42 lines · Andrew Lunn
Quoted 41 lines · Sunil Kovvuri
Quoted 39 lines · Kevin Hao
Quoted 16 lines · Sunil Kovvuri
Quoted 7 lines · Kevin Hao
In the current codes, the octeontx2 uses its own method to allocate the pool buffers, but there are some issues in this implementation.
1. We have to run theotx2_get_page()for each allocation cycle and this is pretty error prone. As I can see there is no invocation of theotx2_get_page()inotx2_pool_refill_task(), this will leave the allocated pages have the wrong refcount and may be freed wrongly.Thanks for pointing, will fix.
Quoted 5 lines
2. It wastes memory. For example, if we only receive one packet in a NAPI RX cycle, and then allocate a 2K buffer with
otx2_alloc_rbuf()to refill the pool buffers and leave the remain area of the allocated page wasted. On a kernel with 64K page, 62K area is wasted.Quoted 7 lines
Quoted 5 lines
IMHO it is really unnecessary to implement our own method for the buffers allocate, we can reuse the
napi_alloc_frag()to simplify our code.Signed-off-by Kevin Hao <[email protected]>Have you measured performance with and without your patch ?
I will do performance compare later. But I don't think there will be measurable difference.
Quoted 4 lines
I didn't use
napi_alloc_frag()as it's too costly, if in one NAPI instance driver
receives 32 pkts, then 32 calls tonapi_alloc_frag()and updates to page ref count per fragment etc are costly.No, the page ref only be updated at the page allocation and all the space are used. In general, the invocation of
napi_alloc_frag()will not cause the update of the page ref. So in theory, the count of updating page ref should be reduced by using ofnapi_alloc_frag()compare to the current otx2 implementation.
Quoted 8 lines
Quoted 1 line
Okay, it seems i misunderstood it.
Hi Sunil
In general, you should not work around issues in the core, you should improve the core. If your implementation really was more efficient than the core code, it would of been better if you proposed fixes to the core, not hide away better code in your own driver.
Hi, Andrew
When looking the napi_alloc_frag() api, the mapping/unmapping is done by caller, if the mapping/unmapping is managed in the core, then the mapping/unmapping can be avoided when the page is reused, because the mapping/unmapping operation is costly when IOMMU is on, do you think it makes sense to do the mapping/ummapping in the page_frag_*()?
Quoted 4 lines
Andrew
.