octeontx2-pf: Fix page pool cache index corruption.
Ratheesh Kannoth
maintainerPATCH v2 1/1 octeontx2-pf: Fix page pool cache index corruption.
The access to page pool cache' array and the count' variable is not locked. Page pool cache access is fine as long as there is only one consumer per pool.
octeontx2 driver fills in rx buffers from page pool in NAPI context. If system is stressed and could not allocate buffers, refiiling work will be delegated to a delayed workqueue. This means that there are two cosumers to the page pool cache.
Either workqueue or IRQ/NAPI can be run on other CPU. This will lead to lock less access, hence corruption of cache pool indexes.
To fix this issue, NAPI is rescheduled from workqueue context to refill rx buffers.
---
ChangeLogs
v1 -> v2: Added local_bh_disable() to be precise on napi scheduling.
v0 -> v1: udelay will waste CPU cycles in UP. So call napi_schedule from
delayed work queue context.
---
--- .../ethernet/marvell/octeontx2/nic/cn10k.c | 4 +- .../ethernet/marvell/octeontx2/nic/cn10k.h | 2 +- .../marvell/octeontx2/nic/otx2_common.c | 43 +++---------------- .../marvell/octeontx2/nic/otx2_common.h | 3 +- .../ethernet/marvell/octeontx2/nic/otx2_pf.c | 7 +-- .../marvell/octeontx2/nic/otx2_txrx.c | 29 ++++++++++--- .../marvell/octeontx2/nic/otx2_txrx.h | 4 +- 7 files changed, 42 insertions(+), 50 deletions(-)
drivers/net/ethernet/marvell/octeontx2/nic/cn10k.c +3 -1
@@ -107,12 +107,13 @@ int cn10k_sq_aq_init(void *dev, u16 qidx, u16 sqb_aura) }#define NPA_MAX_BURST 16 -void cn10k_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq) +int cn10k_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq) {struct otx2_nic *pfvf = dev;u64 ptrs[NPA_MAX_BURST ];int num_ptrs =1 ;dma_addr_t bufptr; +int cnt = cq->pool_ptrs;/* Refill pool with new buffers */ while (cq->pool_ptrs) {
@@ -131,6 +132,7 @@ void cn10k_refill_pool_ptrs(void *dev, struct otx2_cq_queue *cq) num_ptrs =1 ; } } +return cnt - cq->pool_ptrs; }void cn10k_sqe_flush (void *dev,struct otx2_snd_queue *sq,int size,int qidx)
drivers/net/ethernet/marvell/octeontx2/nic/cn10k.h +1 -1
@@ -24,7 +24,7 @@ static inline int mtu_to_dwrr_weight(struct otx2_nic *pfvf, int mtu)return weight; } -void cn10k_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq); +int cn10k_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq);void cn10k_sqe_flush (void *dev,struct otx2_snd_queue *sq,int size,int qidx);int cn10k_sq_aq_init (void *dev,u16 qidx,u16 sqb_aura);int cn10k_lmtst_init (struct otx2_nic *pfvf);
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +6 -37
65 lines not on this page · raw message
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.h +2 -1
@@ -302,6 +302,7 @@ struct flr_work {struct refill_work {struct delayed_work pool_refill_work;struct otx2_nic *pf; +struct napi_struct *napi; };/* PTPv2 originTimestamp structure */
@@ -370,7 +371,7 @@ struct dev_hw_ops {int (*sq_aq_init)(void *dev,u16 qidx,u16 sqb_aura);void (*sqe_flush)(void *dev,struct otx2_snd_queue *sq,int size,int qidx); -void (*refill_pool_ptrs)(void *dev,struct otx2_cq_queue *cq); +int (*refill_pool_ptrs)(void *dev,struct otx2_cq_queue *cq);void (*aura_freeptr)(void *dev,int aura,u64 buf); };
drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c +4 -3
@@ -1943,6 +1943,10 @@ int otx2_stop(struct net_device *netdev)netif_tx_disable (netdev); +for (wrk =0 ; wrk < pf->qset.cq_cnt; wrk++) +cancel_delayed_work_sync (&pf->refill_wrk[wrk].pool_refill_work); +devm_kfree (pf->dev, pf->refill_wrk); +otx2_free_hw_resources (pf);otx2_free_cints (pf, pf->hw.cint_cnt);otx2_disable_napi (pf);
@@ -1950,9 +1954,6 @@ int otx2_stop(struct net_device *netdev)for (qidx =0 ; qidx < netdev->num_tx_queues; qidx++)netdev_tx_reset_queue (netdev_get_tx_queue (netdev, qidx)); -for (wrk =0 ; wrk < pf->qset.cq_cnt; wrk++) -cancel_delayed_work_sync (&pf->refill_wrk[wrk].pool_refill_work); -devm_kfree (pf->dev, pf->refill_wrk);kfree (qset->sq);kfree (qset->cq);
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +24 -5
@@ -424,9 +424,10 @@ static int otx2_rx_napi_handler(struct otx2_nic *pfvf,return processed_cqe; } -void otx2_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq) +int otx2_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq) {struct otx2_nic *pfvf = dev; +int cnt = cq->pool_ptrs;dma_addr_t bufptr;while (cq->pool_ptrs) {
@@ -435,6 +436,8 @@ void otx2_refill_pool_ptrs(void *dev, struct otx2_cq_queue *cq)otx2_aura_freeptr (pfvf, cq->cq_idx, bufptr +OTX2_HEAD_ROOM ); cq->pool_ptrs--; } + +return cnt - cq->pool_ptrs; } static int otx2_tx_napi_handler(struct otx2_nic *pfvf,
@@ -521,6 +524,7 @@ int otx2_napi_handler(struct napi_struct *napi, int budget)struct otx2_cq_queue *cq;struct otx2_qset *qset;struct otx2_nic *pfvf; +int filled_cnt =-1 ; cq_poll = container_of(napi, struct otx2_cq_poll, napi); pfvf = (struct otx2_nic *)cq_poll->dev;
@@ -541,7 +545,7 @@ int otx2_napi_handler(struct napi_struct *napi, int budget) }if (rx_cq && rx_cq->pool_ptrs) - pfvf->hw_ops->refill_pool_ptrs (pfvf, rx_cq); + filled_cnt = pfvf->hw_ops->refill_pool_ptrs (pfvf, rx_cq);/* Clear the IRQ */ otx2_write64 (pfvf,NIX_LF_CINTX_INT (cq_poll->cint_idx),BIT_ULL (0 ));
@@ -561,9 +565,24 @@ int otx2_napi_handler(struct napi_struct *napi, int budget)otx2_config_irq_coalescing (pfvf, i); } -/* Re-enable interrupts */ -otx2_write64 (pfvf,NIX_LF_CINTX_ENA_W1S (cq_poll->cint_idx), -BIT_ULL (0 )); +if (unlikely (!filled_cnt)) { +struct refill_work *work; +struct delayed_work *dwork; + + work = &pfvf->refill_wrk[cq->cq_idx]; + dwork = &work->pool_refill_work; +/* Schedule a task if no other task is running */ +if (!cq->refill_task_sched) { + work->napi = napi; + cq->refill_task_sched = true; +schedule_delayed_work (dwork, +msecs_to_jiffies (100 )); + } + }else { +/* Re-enable interrupts */ +otx2_write64 (pfvf,NIX_LF_CINTX_ENA_W1S (cq_poll->cint_idx), +BIT_ULL (0 )); + } }return workdone; }
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.h +2 -2
@@ -170,6 +170,6 @@ void cn10k_sqe_flush(void *dev, struct otx2_snd_queue *sq, int size, int qidx);void otx2_sqe_flush (void *dev,struct otx2_snd_queue *sq,int size,int qidx); -void otx2_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq); -void cn10k_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq); +int otx2_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq); +int cn10k_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq);#endif /* OTX2_TXRX_H */
Signature · 1 line
2.25.1
Simon Horman
reviewerQuoted 42 lines · Ratheesh Kannoth
The access to page pool
cache' array and thecount' variable is not locked. Page pool cache access is fine as long as there is only one consumer per pool.octeontx2 driver fills in rx buffers from page pool in NAPI context. If system is stressed and could not allocate buffers, refiiling work will be delegated to a delayed workqueue. This means that there are two cosumers to the page pool cache.
Either workqueue or IRQ/NAPI can be run on other CPU. This will lead to lock less access, hence corruption of cache pool indexes.
To fix this issue, NAPI is rescheduled from workqueue context to refill rx buffers.
Fixes b2e3406a38f0 ("octeontx2-pf: Add support for page pool")Signed-off-by Ratheesh Kannoth <[email protected]>---
ChangeLogs
v1 -> v2: Addedlocal_bh_disable()to be precise on napi scheduling.v0 -> v1: udelay will waste CPU cycles in UP. So call
napi_schedulefrom
delayed work queue context.---
--- .../ethernet/marvell/octeontx2/nic/cn10k.c | 4 +- .../ethernet/marvell/octeontx2/nic/cn10k.h | 2 +- .../marvell/octeontx2/nic/otx2_common.c | 43 +++---------------- .../marvell/octeontx2/nic/otx2_common.h | 3 +- .../ethernet/marvell/octeontx2/nic/otx2_pf.c | 7 +-- .../marvell/octeontx2/nic/otx2_txrx.c | 29 ++++++++++--- .../marvell/octeontx2/nic/otx2_txrx.h | 4 +- 7 files changed, 42 insertions(+), 50 deletions(-)drivers/net/ethernet/marvell/octeontx2/nic/cn10k.c +0
-0@@ -107,12 +107,13 @@ int cn10k_sq_aq_init(void *dev, u16 qidx, u16 sqb_aura) }
drivers/net/ethernet/marvell/octeontx2/nic/cn10k.c int cn10k_sq_aq_init(void *dev, u16 qidx, u16 sqb_aura) · line 116
Quoted 10 lines
drivers/net/ethernet/marvell/octeontx2/nic/cn10k.c +2
-1#define NPA_MAX_BURST 16 -void cn10k_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq) +int cn10k_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq) {struct otx2_nic *pfvf = dev;u64 ptrs[NPA_MAX_BURST ];int num_ptrs =1 ;dma_addr_t bufptr; +int cnt = cq->pool_ptrs;
nit: please arrange local variables in new Networking code in reverse xmas
tree order - longest line to shortest.
Quoted 3 lines
drivers/net/ethernet/marvell/octeontx2/nic/cn10k.c +0
-0/* Refill pool with new buffers */ while (cq->pool_ptrs) {
...
Ratheesh Kannoth
maintainerQuoted 4 lines
From: Simon Horman <[email protected]>
Subject: [EXT] Re: [PATCH net v2] octeontx2-pf: Fix page pool cache index corruption.
-voidcn10k_refill_pool_ptrs(void *dev, struct otx2_cq_queue *cq)
Quoted 10 lines
drivers/net/ethernet/marvell/octeontx2/nic/cn10k.c int cn10k_sq_aq_init(void *dev, u16 qidx, u16 sqb_aura) · line 116
Quoted 7 lines
drivers/net/ethernet/marvell/octeontx2/nic/cn10k.c +2
-0+int cn10k_refill_pool_ptrs (void *dev,struct otx2_cq_queue *cq) {struct otx2_nic *pfvf = dev;u64 ptrs[NPA_MAX_BURST ];int num_ptrs =1 ;dma_addr_t bufptr; +int cnt = cq->pool_ptrs;nit: please arrange local variables in new Networking code in reverse xmas
tree order - longest line to shortest.
ACK
-Ratheesh
Quoted 9 lines · Ratheesh Kannoth
The access to page pool
cache' array and thecount' variable is not locked. Page pool cache access is fine as long as there is only one consumer per pool.octeontx2 driver fills in rx buffers from page pool in NAPI context. If system is stressed and could not allocate buffers, refiiling work will be delegated to a delayed workqueue. This means that there are two cosumers to the page pool cache.
Quoted 8 lines
Either workqueue or IRQ/NAPI can be run on other CPU. This will lead to lock less access, hence corruption of cache pool indexes.
To fix this issue, NAPI is rescheduled from workqueue context to refill rx buffers.
Fixes b2e3406a38f0 ("octeontx2-pf: Add support for page pool")Signed-off-by Ratheesh Kannoth <[email protected]>
Quoted 18 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +1
-3static void otx2_pool_refill_task (struct work_struct *work) {struct otx2_cq_queue *cq; -struct otx2_pool *rbpool;struct refill_work *wrk; -int qidx, free_ptrs =0 ;struct otx2_nic *pfvf; -dma_addr_t bufptr; +int qidx; wrk = container_of(work, struct refill_work, pool_refill_work.work); pfvf = wrk->pf; qidx = wrk - pfvf->refill_wrk; cq = &pfvf->qset.cq[qidx];
…
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c static int otx2_cq_init(struct otx2_nic *pfvf, u16 qidx) · line 1086
Quoted 5 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +4
-0cq->refill_task_sched = false; + +local_bh_disable (); +napi_schedule (wrk->napi); +local_bh_enable ();
This is a nitpick since I haven't look how it works exactly: Is it possible that the wrk->napi pointer gets overwritten by otx2_napi_handler() since you cleared cq->refill_task_sched() earlier?
Quoted 25 lines
}
int
otx2_config_nix_queues(struct otx2_nic *pfvf)drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +11
-3@@ -561,9 +565,24 @@ int otx2_napi_handler(struct napi_struct *napi, int budget)otx2_config_irq_coalescing (pfvf, i); } -/* Re-enable interrupts */ -otx2_write64 (pfvf,NIX_LF_CINTX_ENA_W1S (cq_poll->cint_idx), -BIT_ULL (0 )); +if (unlikely (!filled_cnt)) { +struct refill_work *work; +struct delayed_work *dwork; + + work = &pfvf->refill_wrk[cq->cq_idx]; + dwork = &work->pool_refill_work; +/* Schedule a task if no other task is running */ +if (!cq->refill_task_sched) { + work->napi = napi; + cq->refill_task_sched = true; + schedule_delayed_work(dwork,
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c int otx2_napi_handler(struct napi_struct *napi, int budget) · line 588
Quoted 10 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +7
-0+ msecs_to_jiffies(100)); + } + }else { +/* Re-enable interrupts */ +otx2_write64 (pfvf,NIX_LF_CINTX_ENA_W1S (cq_poll->cint_idx), +BIT_ULL (0 )); + } }return workdone; }
Sebastian
Ratheesh Kannoth
maintainerQuoted 12 lines
From: Sebastian Andrzej Siewior <[email protected]>
Subject: [EXT] Re: [PATCH net v2] octeontx2-pf: Fix page pool cache index corruption.drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c static int otx2_cq_init(struct otx2_nic *pfvf, u16 qidx) · line 1086
Quoted 5 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +4
-0cq->refill_task_sched = false; + +local_bh_disable (); +napi_schedule (wrk->napi); +local_bh_enable ();This is a nitpick since I haven't look how it works exactly: Is it possible that the
wrk->napipointer gets overwritten byotx2_napi_handler()since you clearedcq->refill_task_sched()earlier?
I don’t see any issue here. As NAPI and workqueue execution is serialized (as interrupt is disabled when Workqueue is scheduled). I don’t think we can move "refill_task_sched = false" after local_bh_enable(). But we can move refill_task_sched = false as below . but I don’t see a need.
+
+ local_bh_disable();
+ napi_schedule(wrk->napi);
+ cq->refill_task_sched = false;
+ local_bh_enable();
Quoted 12 lines · Ratheesh Kannoth
Quoted 12 lines
From: Sebastian Andrzej Siewior <[email protected]>
Subject: [EXT] Re: [PATCH net v2] octeontx2-pf: Fix page pool cache index corruption.drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c static int otx2_cq_init(struct otx2_nic *pfvf, u16 qidx) · line 1086
Quoted 5 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +4
-0cq->refill_task_sched = false; + +local_bh_disable (); +napi_schedule (wrk->napi); +local_bh_enable ();This is a nitpick since I haven't look how it works exactly: Is it possible that the
wrk->napipointer gets overwritten byotx2_napi_handler()since you clearedcq->refill_task_sched()earlier?
Quoted 3 lines
I don’t see any issue here. As NAPI and workqueue execution is serialized (as interrupt is disabled when Workqueue is scheduled). I don’t think we can move "
refill_task_sched= false" afterlocal_bh_enable(). But we can moverefill_task_sched= false as below . but I don’t see a need.
Right, there might be no issue I was just asking. I don't know how the cq <-> NAPI mapping is working. If you say that there is no race/issue, okay ;)
Quoted 5 lines
+
+local_bh_disable();
+napi_schedule(wrk->napi);
+cq->refill_task_sched= false;
+local_bh_enable();
Sebastian