Skip to contentSkip to conversation

net: ethernet: sunplus: check budget before calling napi_complete_done()

PATCH In review Janis Edvarts Lacis 4 participants 11 messages Nov 14 – Nov 18

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: ethernet: sunplus: check budget before calling napi_complete_done()

Sunplus drivers currently do napi_complete() after each rx/tx poll is done without checking whether budget was exhausted or not. Interrupts are also unmasked before the call to napi_complete().

Replace napi_complete() with napi_complete_done() and add a check for budget_left. Call napi_complete_done() only when the budget has been not exhausted fully, e.g. budget_left > 0 and unmask interrupts only when napi_complete_done() returns true.

I do not have the hardware necessary, this patch is not tested on real hardware.

Fixes fd3040b9394c ("net: ethernet: Add driver for Sunplus SP7021")
---
 drivers/net/ethernet/sunplus/spl2sw_int.c | 26 ++++++++++++-----------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/net/ethernet/sunplus/spl2sw_int.c +14 -12
index a37c9a4c281f..1aada11429d4 100644
@@ -125,13 +125,14 @@ int spl2sw_rx_poll(struct napi_struct *napi, int budget)
 			h_desc = comm->rx_desc[queue] + rx_pos;
 	}
 
-	spin_lock_irqsave(&comm->int_mask_lock, flags);
-	mask = readl(comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
-	mask &= ~MAC_INT_RX;
-	writel(mask, comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
-	spin_unlock_irqrestore(&comm->int_mask_lock, flags);
+	if (budget_left > 0 && napi_complete_done(napi, budget - budget_left)) {
+		spin_lock_irqsave(&comm->int_mask_lock, flags);
+		mask = readl(comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
+		mask &= ~MAC_INT_RX;
+		writel(mask, comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
+		spin_unlock_irqrestore(&comm->int_mask_lock, flags);
+	}
 
-	napi_complete(napi);
 	return budget - budget_left;
 }
 
@@ -198,13 +199,14 @@ int spl2sw_tx_poll(struct napi_struct *napi, int budget)
 
 	spin_unlock(&comm->tx_lock);
 
-	spin_lock_irqsave(&comm->int_mask_lock, flags);
-	mask = readl(comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
-	mask &= ~MAC_INT_TX;
-	writel(mask, comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
-	spin_unlock_irqrestore(&comm->int_mask_lock, flags);
+	if (budget_left > 0 && napi_complete_done(napi, budget - budget_left)) {
+		spin_lock_irqsave(&comm->int_mask_lock, flags);
+		mask = readl(comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
+		mask &= ~MAC_INT_TX;
+		writel(mask, comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
+		spin_unlock_irqrestore(&comm->int_mask_lock, flags);
+	}
 
-	napi_complete(napi);
 	return budget - budget_left;
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 9 lines · Janis Edvarts Lacis

Sunplus drivers currently do napi_complete() after each rx/tx poll is done without checking whether budget was exhausted or not. Interrupts are also unmasked before the call to napi_complete().

Replace napi_complete() with napi_complete_done() and add a check for budget_left. Call napi_complete_done() only when the budget has been not exhausted fully, e.g. budget_left > 0 and unmask interrupts only when napi_complete_done() returns true.

Quoted 5 lines

I do not have the hardware necessary, this patch is not tested on real hardware.

Fixes fd3040b9394c ("net: ethernet: Add driver for Sunplus SP7021")

Does this bother anybody? If not, net-next. No Fixes: tag.

Given that you have not tested this, how confident are you it is correct?

Andrew

#2 lore raw
Quoted 10 lines · Andrew Lunn
Quoted 9 lines · Janis Edvarts Lacis

Sunplus drivers currently do napi_complete() after each rx/tx poll is done without checking whether budget was exhausted or not. Interrupts are also unmasked before the call to napi_complete().

Replace napi_complete() with napi_complete_done() and add a check for budget_left. Call napi_complete_done() only when the budget has been not exhausted fully, e.g. budget_left > 0 and unmask interrupts only when napi_complete_done() returns true.

Quoted 7 lines
Quoted 5 lines

I do not have the hardware necessary, this patch is not tested on real hardware.

Fixes fd3040b9394c ("net: ethernet: Add driver for Sunplus SP7021")

Does this bother anybody? If not, net-next. No Fixes: tag.

Will do in the v2.

Quoted 3 lines

Given that you have not tested this, how confident are you it is correct?

I am fairly confident the change is correct, as the documentation
supports it. When netpoll calls the poll function with budget = 0, napi_complete() returns false, but the driver ignores the return value and unmasks interrupts anyway.

During normal NAPI polling, budget is set to default (64), but the driver has less descriptors and it looks at each one at most once. Therefore, on this path, the only benefit of this patch is that interrupts are unmasked after napi_complete_done() instead of before, as in the old version.

Of course, actual testing on a device would be great though.

Best regards,
Janis

#3 lore raw
Quoted 2 lines

I am fairly confident the change is correct, as the documentation supports it.

So the LLM review is wrong?

Andrew

#4 lore raw
Quoted 4 lines · Andrew Lunn
Quoted 2 lines

I am fairly confident the change is correct, as the documentation supports it.

So the LLM review is wrong?

No, the review is not wrong. It correctly points out that the budget cannot be exhausted during normal NAPI polling in this driver, which I had missed originally.

It correctly identifies that the old code unmasks interrupts even when napi_complete() returns false. My change calls napi_complete_done() before unmasking interrupts and only unmasks them if it returns true, indicating that NAPI polling has completed and interrupts can be re-enabled.

Finally, it brings up some pre existing issues that seem to be appropriate for separate patches.

If this patch still makes sense as a net-next change, I will update the commit message to explain the ordering and conditional interrupt masking.

Best regards,
Janis

#5 lore raw
Quoted 15 lines · Janis Edvarts Lacis
Quoted 4 lines · Andrew Lunn
Quoted 2 lines

I am fairly confident the change is correct, as the documentation supports it.

So the LLM review is wrong?

No, the review is not wrong. It correctly points out that the budget cannot be exhausted during normal NAPI polling in this driver, which I had missed originally.

It correctly identifies that the old code unmasks interrupts even when napi_complete() returns false. My change calls napi_complete_done() before unmasking interrupts and only unmasks them if it returns true, indicating that NAPI polling has completed and interrupts can be re-enabled.

Quoted 5 lines

Finally, it brings up some pre existing issues that seem to be appropriate for separate patches.

If this patch still makes sense as a net-next change, I will update the commit message to explain the ordering and conditional interrupt masking.

I don't think it makes sense since what it is trying to fix can never happen.

Andrew

---
pw-bot: cr

#6 lore raw
Quoted 16 lines · Andrew Lunn
Quoted 15 lines · Janis Edvarts Lacis
Quoted 4 lines · Andrew Lunn
Quoted 2 lines

I am fairly confident the change is correct, as the documentation supports it.

So the LLM review is wrong?

No, the review is not wrong. It correctly points out that the budget cannot be exhausted during normal NAPI polling in this driver, which I had missed originally.

It correctly identifies that the old code unmasks interrupts even when napi_complete() returns false. My change calls napi_complete_done() before unmasking interrupts and only unmasks them if it returns true, indicating that NAPI polling has completed and interrupts can be re-enabled.

Quoted 8 lines
Quoted 5 lines

Finally, it brings up some pre existing issues that seem to be appropriate for separate patches.

If this patch still makes sense as a net-next change, I will update the commit message to explain the ordering and conditional interrupt masking.

I don't think it makes sense since what it is trying to fix can never happen.

Understood, thanks for the quick review!

Best regards,
Janis

#7 lore raw
In reply to Janis Edvarts Lacis
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#8 lore raw
In reply to Janis Edvarts Lacis
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 (2) · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The patch uses budget_left > 0 in spl2sw_rx_poll() as the only sign that all work is done.
- [Low] The commit message says what changes but not what goes wrong today.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but spl2sw_remove() frees the descriptor rings and buffers before it…
- [High] This problem wasn't introduced by this patch, but spl2sw_rx_poll() leaves a broken ring slot behind when it fails to…

--

net: ethernet: sunplus: check budget before calling napi_complete_done()

This commit replaces napi_complete() with napi_complete_done() in the spl2sw RX and TX poll functions. NAPI is only completed when budget_left is greater than zero. The RX/TX interrupt is only unmasked when napi_complete_done() returns true.

Quoted 3 lines

Sunplus drivers currently do napi_complete() after each rx/tx poll is done without checking whether budget was exhausted or not. Interrupts are also unmasked before the call to napi_complete().

[Severity: Low]
Can the budget actually be exhausted here in normal operation?

spl2sw_probe() registers both NAPI instances with netif_napi_add(), so they use NAPI_POLL_WEIGHT (64). spl2sw_rx_poll() handles at most rx_desc_num[0] + rx_desc_num[1] = 32 descriptors per call, because the inner loop is bounded by num < rx_count. spl2sw_tx_poll() holds tx_lock and reclaims at most TX_DESC_NUM = 16 descriptors.

That means budget_left is always > 0 for polls from net_rx_action(). The new check only changes anything for budget 0 (netpoll's poll_one_napi()) or a small busy-poll budget.

In those cases the old napi_complete() already did nothing, because napi_complete_done() returns false right away when NAPIF_STATE_NPSVC or NAPIF_STATE_IN_BUSY_POLL is set. The only visible effect of the old code was that MAC_INT_RX/MAC_INT_TX got unmasked while netpoll or busy-poll owned the instance.

Could the commit message describe that actual impact, so the Fixes: tag and a stable backport can be judged?

Quoted 2 lines

I do not have the hardware necessary, this patch is not tested on real hardware.

This note is in the commit body rather than below the --- line, so it would end up in git history.

Quoted 5 lines
drivers/net/ethernet/sunplus/spl2sw_int.c +0 -0
index a37c9a4c281f0..1aada11429d4d 100644

drivers/net/ethernet/sunplus/spl2sw_int.c int spl2sw_rx_poll(struct napi_struct *napi, int budget) · line 128

Quoted 9 lines
drivers/net/ethernet/sunplus/spl2sw_int.c +1 -5
@@ -125,13 +125,14 @@ int spl2sw_rx_poll(struct napi_struct *napi, int budget)
 			h_desc = comm->rx_desc[queue] + rx_pos;
 	}
 
-	spin_lock_irqsave(&comm->int_mask_lock, flags);
-	mask = readl(comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
-	mask &= ~MAC_INT_RX;
-	writel(mask, comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
-	spin_unlock_irqrestore(&comm->int_mask_lock, flags);
+	if (budget_left > 0 && napi_complete_done(napi, budget - budget_left)) {

[Severity: Medium]
Is budget_left > 0 enough to show that all RX work is done? Normal polls never bring it to 0, but spl2sw_rx_poll() can still stop while completed descriptors are pending.

The first case is the high-priority early break in the low-priority loop:

			if (queue == 1 && !(h_desc->cmd1 & RXD_OWN))
				break;

queue 1 is the last queue (RX_DESC_QUEUE_NUM == 2), so this goes straight to the completion check while new high-priority packets are waiting.

The second case is a queue's inner loop stopping at the ring limit:

		for (num = 0; num < rx_count && budget_left; num++) {

Descriptors given back with RXD_OWN earlier in the loop may already have been refilled by the hardware.

In both cases the poll returns less than budget, calls napi_complete_done() and clears MAC_INT_RX in L2SW_SW_INT_MASK_0 while work is still pending. Documentation/networking/napi.rst says a poll with outstanding work should return exactly budget and not complete.

The cost depends on whether L2SW_SW_INT_STATUS_0 latches RX events while masked. If it does, this costs an extra interrupt round-trip. If it doesn't, the pending packets wait for the next RX event.

Would it work better to track these more-work conditions explicitly and return budget in those cases, instead of relying on budget_left alone?

[Severity: High]
This isn't a bug introduced by this patch, but spl2sw_rx_poll() leaves a broken ring slot behind when an RX buffer refill fails:

spl2sw_rx_poll() {
	...
			new_skb = netdev_alloc_skb(NULL, comm->rx_desc_buff_size);
			if (unlikely(!new_skb)) {
				desc->cmd2 = (rx_pos == comm->rx_desc_num[queue] - 1) ?
					     RXD_EOR : 0;
				sinfo->skb = NULL;
				sinfo->mapping = 0;
				desc->addr1 = 0;
				goto spl2sw_rx_poll_alloc_err;
			}
	...
}

The dma_mapping_error() branch does the same. Both jumps skip desc->cmd1 = RXD_OWN, so cmd1 keeps the stale completed status the hardware wrote, and nothing ever refills the slot.

After 15 more packets on that queue, rx_pos wraps back to this slot. The RXD_OWN check doesn't stop the loop, and the stale status passes the port, length and error checks:

			dma_unmap_single(&comm->pdev->dev, sinfo->mapping,
					 comm->rx_desc_buff_size, DMA_FROM_DEVICE);

			skb = sinfo->skb;
			skb_put(skb, pkg_len - 4); /* Minus FCS */

Can this call dma_unmap_single() on mapping 0 and then skb_put() on a NULL skb in the NET_RX softirq?

There is also a second path. On a TX timeout, spl2sw_mac_soft_reset() calls spl2sw_rx_descs_flush(), which gives the slot back to the hardware with RXD_OWN set:

			rx_desc[j].addr1 = rx_skbinfo[j].mapping;

Could the hardware then DMA a received frame to bus address 0?

One fix would be to keep the old buffer in the ring and drop the packet when the refill fails. Another would be to retry the refill on later polls and never treat a slot without an skb as completed.

[ ... ]

[Severity: High]
This isn't a bug introduced by this patch, but spl2sw_remove() frees the rings that both poll functions use before it disables NAPI:

drivers/net/ethernet/sunplus/spl2sw_driver.c:spl2sw_remove() {
	...
	comm->enable = 0;
	spl2sw_mac_hw_stop(comm);
	spl2sw_descs_free(comm);

	/* Disable and delete napi. */
	napi_disable(&comm->rx_napi);
	netif_napi_del(&comm->rx_napi);
	napi_disable(&comm->tx_napi);
	netif_napi_del(&comm->tx_napi);
	...
}

spl2sw_descs_free() frees the RX skbs and rx_skb_info arrays, frees the TX skbs in spl2sw_tx_descs_clean() without holding tx_lock, sets comm->tx_desc and comm->rx_desc[i] to NULL and frees desc_base with dma_free_coherent().

unregister_netdev() and spl2sw_mac_hw_stop() don't stop a NAPI instance that is already scheduled. Only napi_disable() cancels the NAPI hrtimer.

With napi_defer_hard_irqs and gro_flush_timeout set, napi_complete_done() keeps returning false and re-arms the timer. napi_watchdog() then schedules the poll again with no interrupt needed. Without those settings there is still a window if NAPI was scheduled just before ndo_stop masked the interrupts.

If remove runs in that window, spl2sw_rx_poll() would do:

			desc = comm->rx_desc[queue] + rx_pos;
			cmd = desc->cmd1;

with a NULL comm->rx_desc[queue], and spl2sw_tx_poll() would read comm->tx_desc[tx_done_pos].cmd1 the same way. A TX poll running at the same time as spl2sw_tx_descs_clean() could also call dev_kfree_skb_irq() on an skb that dev_kfree_skb_any() is already freeing.

This patch doesn't widen the window, since normal polls still complete after one pass.

Should napi_disable() for both instances move before spl2sw_descs_free()?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261114222744.535158-1-janislacis06%40gmail.com
#9 lore raw
Quoted 48 lines · On 16 Nov 02:22, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 (2) · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The patch uses budget_left > 0 in spl2sw_rx_poll() as the only sign that all work is done.
- [Low] The commit message says what changes but not what goes wrong today.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but spl2sw_remove() frees the descriptor rings and buffers before it…
- [High] This problem wasn't introduced by this patch, but spl2sw_rx_poll() leaves a broken ring slot behind when it fails to…

--

net: ethernet: sunplus: check budget before calling napi_complete_done()

This commit replaces napi_complete() with napi_complete_done() in the spl2sw RX and TX poll functions. NAPI is only completed when budget_left is greater than zero. The RX/TX interrupt is only unmasked when napi_complete_done() returns true.

Quoted 3 lines

Sunplus drivers currently do napi_complete() after each rx/tx poll is done without checking whether budget was exhausted or not. Interrupts are also unmasked before the call to napi_complete().

[Severity: Low]
Can the budget actually be exhausted here in normal operation?

spl2sw_probe() registers both NAPI instances with netif_napi_add(), so they use NAPI_POLL_WEIGHT (64). spl2sw_rx_poll() handles at most rx_desc_num[0] + rx_desc_num[1] = 32 descriptors per call, because the inner loop is bounded by num < rx_count. spl2sw_tx_poll() holds tx_lock and reclaims at most TX_DESC_NUM = 16 descriptors.

That means budget_left is always > 0 for polls from net_rx_action(). The new check only changes anything for budget 0 (netpoll's poll_one_napi()) or a small busy-poll budget.

Quoted 5 lines

In those cases the old napi_complete() already did nothing, because napi_complete_done() returns false right away when NAPIF_STATE_NPSVC or NAPIF_STATE_IN_BUSY_POLL is set. The only visible effect of the old code was that MAC_INT_RX/MAC_INT_TX got unmasked while netpoll or busy-poll owned the instance.

Netpoll can invoke napi->poll() with budget = 0. In that case, napi_complete() already returns false because NAPI_STATE_NPSVC is set. However, the old code ignores its return value and unconditionally unmasks interrupts.

This patch avoids calling completion with a zero budget and moves interrupt unmasking after napi_complete_done(), making it conditional on successful completion, as documented.

Quoted 9 lines

Could the commit message describe that actual impact, so the Fixes: tag and a stable backport can be judged?

Quoted 2 lines

I do not have the hardware necessary, this patch is not tested on real hardware.

This note is in the commit body rather than below the --- line, so it would end up in git history.

Shouldn't it? Documentation/process/maintainer-netdev.rst, under “Bug fixes”, requires describing how the fix was tested. I included the lack of hardware testing in the commit message to make it explicit that it has not been
tested on hardware.

Quoted 41 lines
Quoted 6 lines
drivers/net/ethernet/sunplus/spl2sw_int.c +0 -0
index a37c9a4c281f0..1aada11429d4d 100644

drivers/net/ethernet/sunplus/spl2sw_int.c int spl2sw_rx_poll(struct napi_struct *napi, int budget) · line 128

Quoted 9 lines
drivers/net/ethernet/sunplus/spl2sw_int.c +1 -5
@@ -125,13 +125,14 @@ int spl2sw_rx_poll(struct napi_struct *napi, int budget)
 			h_desc = comm->rx_desc[queue] + rx_pos;
 	}
 
-	spin_lock_irqsave(&comm->int_mask_lock, flags);
-	mask = readl(comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
-	mask &= ~MAC_INT_RX;
-	writel(mask, comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
-	spin_unlock_irqrestore(&comm->int_mask_lock, flags);
+	if (budget_left > 0 && napi_complete_done(napi, budget - budget_left)) {

[Severity: Medium]
Is budget_left > 0 enough to show that all RX work is done? Normal polls never bring it to 0, but spl2sw_rx_poll() can still stop while completed descriptors are pending.

The first case is the high-priority early break in the low-priority loop:

			if (queue == 1 && !(h_desc->cmd1 & RXD_OWN))
				break;

queue 1 is the last queue (RX_DESC_QUEUE_NUM == 2), so this goes straight to the completion check while new high-priority packets are waiting.

The second case is a queue's inner loop stopping at the ring limit:

		for (num = 0; num < rx_count && budget_left; num++) {

Descriptors given back with RXD_OWN earlier in the loop may already have been refilled by the hardware.

In both cases the poll returns less than budget, calls napi_complete_done() and clears MAC_INT_RX in L2SW_SW_INT_MASK_0 while work is still pending. Documentation/networking/napi.rst says a poll with outstanding work should return exactly budget and not complete.

Quoted 6 lines

The cost depends on whether L2SW_SW_INT_STATUS_0 latches RX events while masked. If it does, this costs an extra interrupt round-trip. If it doesn't, the pending packets wait for the next RX event.

Would it work better to track these more-work conditions explicitly and return budget in those cases, instead of relying on budget_left alone?

Agreed. This seems to be a separate issue, the old version also did call the napi_complete(), even if the work was not 'done'. To fix this, the solution could be to iterate while budget_left > 0 and one of the queues has packets to process, but this does seem like a big change that should not be done by me as I can't test it properly.

Quoted 1 line

Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261114222744.535158-1-janislacis06%40gmail.com

#10 lore raw
Quoted 8 lines
Quoted 2 lines

This note is in the commit body rather than below the --- line, so it would end up in git history.

Shouldn't it? Documentation/process/maintainer-netdev.rst, under “Bug fixes”, requires describing how the fix was tested. I included the lack of hardware
testing in the commit message to make it explicit that it has not been tested on hardware.

It is unclear if this is a bug fix.

It is unlikely to reach the criteria for stable:

https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html

So i don't think it is a bug fix as such.

Since these emails are kept forever, anybody who wants to know will be able to find these emails and know it is untested. So i don't think it needs to be in the actual commit message.

Andrew