Netdev List
 help / color / mirror / Atom feed
* [PATCH net] net: ethernet: sunplus: check budget before calling napi_complete_done()
@ 2026-11-14 22:27 Janis Edvarts Lacis
  2026-11-15 15:50 ` Andrew Lunn
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Janis Edvarts Lacis @ 2026-11-14 22:27 UTC (permalink / raw)
  To: netdev
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, wellslutw,
	linux-kernel, 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.

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

Fixes: fd3040b9394c ("net: ethernet: Add driver for Sunplus SP7021")
Signed-off-by: Janis Edvarts Lacis <janislacis06@gmail.com>
---
 drivers/net/ethernet/sunplus/spl2sw_int.c | 26 ++++++++++++-----------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/drivers/net/ethernet/sunplus/spl2sw_int.c b/drivers/net/ethernet/sunplus/spl2sw_int.c
index a37c9a4c281f..1aada11429d4 100644
--- a/drivers/net/ethernet/sunplus/spl2sw_int.c
+++ b/drivers/net/ethernet/sunplus/spl2sw_int.c
@@ -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;
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net] net: ethernet: sunplus: check budget before calling napi_complete_done()
  2026-11-14 22:27 [PATCH net] net: ethernet: sunplus: check budget before calling napi_complete_done() Janis Edvarts Lacis
@ 2026-11-15 15:50 ` Andrew Lunn
  2026-11-15 20:06 ` netdev-bot+sinfo
  2026-11-16  2:22 ` netdev-bot+sashiko
  2 siblings, 0 replies; 4+ messages in thread
From: Andrew Lunn @ 2026-11-15 15:50 UTC (permalink / raw)
  To: Janis Edvarts Lacis
  Cc: netdev, andrew+netdev, davem, edumazet, kuba, pabeni, wellslutw,
	linux-kernel

On Sun, Nov 15, 2026 at 12:27:44AM +0200, Janis Edvarts Lacis wrote:
> 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")
> Signed-off-by: Janis Edvarts Lacis <janislacis06@gmail.com>

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net] net: ethernet: sunplus: check budget before calling napi_complete_done()
  2026-11-14 22:27 [PATCH net] net: ethernet: sunplus: check budget before calling napi_complete_done() Janis Edvarts Lacis
  2026-11-15 15:50 ` Andrew Lunn
@ 2026-11-15 20:06 ` netdev-bot+sinfo
  2026-11-16  2:22 ` netdev-bot+sashiko
  2 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sinfo @ 2026-11-15 20:06 UTC (permalink / raw)
  To: Janis Edvarts Lacis
  Cc: netdev, andrew+netdev, davem, edumazet, kuba, pabeni, wellslutw,
	linux-kernel

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.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net] net: ethernet: sunplus: check budget before calling napi_complete_done()
  2026-11-14 22:27 [PATCH net] net: ethernet: sunplus: check budget before calling napi_complete_done() Janis Edvarts Lacis
  2026-11-15 15:50 ` Andrew Lunn
  2026-11-15 20:06 ` netdev-bot+sinfo
@ 2026-11-16  2:22 ` netdev-bot+sashiko
  2 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16  2:22 UTC (permalink / raw)
  To: janislacis06
  Cc: netdev, andrew+netdev, davem, edumazet, kuba, pabeni, wellslutw,
	linux-kernel

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.

> 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?

> 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.

> diff --git a/drivers/net/ethernet/sunplus/spl2sw_int.c b/drivers/net/ethernet/sunplus/spl2sw_int.c
> index a37c9a4c281f0..1aada11429d4d 100644
> --- a/drivers/net/ethernet/sunplus/spl2sw_int.c
> +++ b/drivers/net/ethernet/sunplus/spl2sw_int.c
> @@ -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()?

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-16  2:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-14 22:27 [PATCH net] net: ethernet: sunplus: check budget before calling napi_complete_done() Janis Edvarts Lacis
2026-11-15 15:50 ` Andrew Lunn
2026-11-15 20:06 ` netdev-bot+sinfo
2026-11-16  2:22 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox