Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: cbabroski@nvidia.com
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, davthompson@nvidia.com
Subject: Re: [PATCH net-next v1 1/2] mlxbf_gige: protect against MAC disable during TX
Date: Tue, 24 Nov 2026 20:44:20 +0000	[thread overview]
Message-ID: <179555306048.3135.18299984453276873378@kernel.org> (raw)
In-Reply-To: <7742d597c5321d5cb587180c1d29eccffa9d51bc.1795206150.git.cbabroski@nvidia.com>

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

Critical: 0 · High: 0 · Medium: 3 · Low: 2

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 resyncs only the consumer side (`priv->prev_tx_ci =
  tx_ci`, mlxbf_gige_tx.c:174) and leaves `priv->tx_pi` unchanged.
- [Medium] When the HW consumer index goes backwards (e.g.
  prev_tx_ci=500, tx_pi=510, CI reads 0), `(u16)(tx_ci - prev_tx_ci)`…
- [Medium] In the desync case the new loop bound lets the NAPI completion
  walk all `tx_q_entries` slots starting at prev_tx_ci.
- [Low] This commit fixes a NULL pointer dereference (oops in
  NAPI/softirq context) in mlxbf_gige_handle_tx_complete() that a…
- [Low] The comment above the TX completion loop in
  mlxbf_gige_handle_tx_complete() (lines 140-146) still says the logic…

--

mlxbf_gige: protect against MAC disable during TX

This patch limits the TX completion loop in mlxbf_gige_handle_tx_complete()
to tx_q_entries iterations and skips NULL tx_skb[] slots. After the loop it
sets prev_tx_ci to the HW consumer index. The goal is to fix a NULL pointer
dereference seen when firmware disables and resets the OOB MAC while TX is
in flight.

> Previously, mlxbf_gige_handle_tx_complete() would continue to loop
> around the TX queue, counting packets and freeing their memory, until
> the two indices were equal. If entries were already freed the first time
> through the queue, then a second attempt could cause a NULL pointer
> dereference.

[Severity: Low]
This fixes a NULL pointer dereference in NAPI context, so should it carry
a Fixes: tag? The unbounded loop and the unconditional
priv->tx_skb[tx_wqe_index]->len dereference both come from:

Fixes: f92e1869d74e ("Add Mellanox BlueField Gigabit Ethernet driver")

> diff --git a/drivers/net/ethernet/mellanox/mlxbf_gige/mlxbf_gige_tx.c b/drivers/net/ethernet/mellanox/mlxbf_gige/mlxbf_gige_tx.c
> index 04982e888c635..73c3033591d59 100644
> --- a/drivers/net/ethernet/mellanox/mlxbf_gige/mlxbf_gige_tx.c
> +++ b/drivers/net/ethernet/mellanox/mlxbf_gige/mlxbf_gige_tx.c

[ ... ]

> @@ -143,7 +144,8 @@ bool mlxbf_gige_handle_tx_complete(struct mlxbf_gige *priv)
>  	 * to be supported, that is TX consumer index wrapped from 0xFFFF
>  	 * to 0 while TX completion index is still < 0xFFFF.
>  	 */
> -	for (; priv->prev_tx_ci != tx_ci; priv->prev_tx_ci++) {
> +	for (entry = 0; priv->prev_tx_ci != tx_ci && entry < priv->tx_q_entries;
> +	     priv->prev_tx_ci++, entry++) {

[Severity: Medium]
When the indices are out of sync, can this loop run past tx_pi into slots
that mlxbf_gige_start_xmit() owns?

mlxbf_gige_handle_tx_complete() runs from mlxbf_gige_poll(), which
mlxbf_gige_rx_intr() can schedule. It holds neither priv->lock nor the
netdev TX lock.

Take prev_tx_ci = 1000, tx_pi = 1005 and a CI reset to 0. The loop visits
ring positions 1000 through 1127, and positions 1005 onward are free slots
that another CPU may be filling at the same time:

CPU1 mlxbf_gige_start_xmit()
    *tx_wqe_addr = tx_buf_dma;    /* outside priv->lock */
    ...
    spin_lock_irqsave(&priv->lock, flags);
    priv->tx_skb[priv->tx_pi % priv->tx_q_entries] = tx_skb;
    priv->tx_pi++;

CPU2 mlxbf_gige_handle_tx_complete()
    if (priv->tx_skb[tx_wqe_index]) {
        ...
        dma_unmap_single(priv->dev, *tx_wqe_addr, ...);
        dev_consume_skb_any(priv->tx_skb[tx_wqe_index]);

Could CPU2 free an skb that CPU1 has just queued?

The plain NULL check also doesn't order the *tx_wqe_addr load after the
tx_skb[] load. On the producer side, nothing orders the WQE store before
the tx_skb[] store. On arm64, could CPU2 see the new skb together with the
old, already unmapped DMA address? That would unmap a stale address a
second time and leak the new mapping.

Would bounding the walk by tx_pi - prev_tx_ci, sampled under priv->lock,
keep it out of producer-owned slots?

[Severity: Low]
This isn't a bug, but the comment above this loop still says the
completion logic "needs to loop until the completion index (in SW) equals
TX consumer index (from HW)". With the new tx_q_entries bound, the loop can
exit with prev_tx_ci != tx_ci. Should the comment be updated?

>  		tx_wqe_index = priv->prev_tx_ci % priv->tx_q_entries;
>  		/* Each TX WQE is 16 bytes. The 8 MSB store the 2KB TX
>  		 * buffer address and the 8 LSB contain information
> @@ -152,18 +154,25 @@ bool mlxbf_gige_handle_tx_complete(struct mlxbf_gige *priv)
>  		tx_wqe_addr = priv->tx_wqe_base +
>  			       (tx_wqe_index * MLXBF_GIGE_TX_WQE_SZ_QWORDS);
>  
> -		stats->tx_packets++;
> -		stats->tx_bytes += MLXBF_GIGE_TX_WQE_PKT_LEN(tx_wqe_addr);
> -
> -		dma_unmap_single(priv->dev, *tx_wqe_addr,
> -				 priv->tx_skb[tx_wqe_index]->len, DMA_TO_DEVICE);
> -		dev_consume_skb_any(priv->tx_skb[tx_wqe_index]);
> -		priv->tx_skb[tx_wqe_index] = NULL;
> +		if (priv->tx_skb[tx_wqe_index]) {
> +			stats->tx_packets++;
> +			stats->tx_bytes += MLXBF_GIGE_TX_WQE_PKT_LEN(tx_wqe_addr);
> +			dma_unmap_single(priv->dev, *tx_wqe_addr,
> +					 priv->tx_skb[tx_wqe_index]->len, DMA_TO_DEVICE);
> +			dev_consume_skb_any(priv->tx_skb[tx_wqe_index]);

[Severity: Medium]
Suppose the HW consumer index goes backwards, for example prev_tx_ci = 500,
tx_pi = 510 and CI reads 0. Then (u16)(tx_ci - prev_tx_ci) looks like
almost a full wrap, and this loop runs all tx_q_entries iterations.

Every non-NULL slot is unmapped, freed and counted in tx_packets/tx_bytes.
That includes the in-flight descriptors between prev_tx_ci and tx_pi,
which the hardware never completed.

The coherent WQE ring still holds the bus addresses of those freed
buffers. mlxbf_gige_start_xmit() also keeps writing a tx_pi that is far
ahead of the HW CI:

	writeq(priv->tx_pi, priv->base + MLXBF_GIGE_TX_PRODUCER_INDEX);

The only thing stopping the device from DMAing out of these buffers is the
commit message's statement that firmware has disabled the MAC. Nothing here
checks MAC or DMA enable state.

If TX DMA is still active when CI is seen going backwards, or if firmware
re-enables the MAC later, can the device read freed and possibly
reallocated pages?

Before this patch the kernel oopsed in this path, so those pages were not
reused while the system kept running.

> +			priv->tx_skb[tx_wqe_index] = NULL;
>  
> -		/* Ensure completion of updates across all cores */
> -		mb();
> +			/* Ensure completion of updates across all cores */
> +			mb();
> +		}
>  	}
>  
> +	/* At this point the completion index should be equal to the
> +	 * consumer index. If they aren't then the driver is out of sync
> +	 * from the HW's state and needs to be adjusted.
> +	 */
> +	priv->prev_tx_ci = tx_ci;

[Severity: Medium]
Only the consumer side is resynced here, and priv->tx_pi stays where it
was. mlxbf_gige_tx_buffs_avail() assumes prev_tx_ci trails tx_pi by less
than tx_q_entries. Does this break that assumption?

mlxbf_gige_tx_buffs_avail() computes:

	avail = ((priv->tx_q_entries + priv->prev_tx_ci - priv->tx_pi)
		  % priv->tx_q_entries) - 1;

The u16 operands are promoted to int. With tx_q_entries = 128, tx_pi = 1000
and the HW CI reset to 0, the numerator is negative. avail comes out as
-105 and is returned as the u16 value 65431. Once tx_pi - prev_tx_ci is
larger than tx_q_entries, avail can never be 0.

If the queue was stopped because the ring was full, the code right after
this resync wakes it:

	if (netif_queue_stopped(priv->netdev) &&
	    mlxbf_gige_tx_buffs_avail(priv))
		netif_wake_queue(priv->netdev);

After that, this check in mlxbf_gige_start_xmit() never fires again:

	if (!mlxbf_gige_tx_buffs_avail(priv)) {
		/* TX ring is full, inform stack */
		netif_stop_queue(netdev);

With the MAC disabled nothing completes, because every poll sees
prev_tx_ci == tx_ci. After tx_q_entries more packets,
mlxbf_gige_start_xmit() starts overwriting slots that still hold
DMA-mapped skbs:

	priv->tx_skb[priv->tx_pi % priv->tx_q_entries] = tx_skb;

Does each overwrite leak the old skb, its DMA mapping and its socket wmem
charge? In the example above that continues until tx_pi wraps the 16 bits,
roughly 64K packets. The driver has no ndo_tx_timeout and doesn't log the
desync, so nothing else stops it.

A related case: the walk is bounded by tx_q_entries, not by the posted
window tx_pi - prev_tx_ci. A CI reset that lands within tx_q_entries of
prev_tx_ci, for example prev_tx_ci = 65500, tx_pi = 65505 and CI = 0, is
handled as a normal completion and ends up in the same state.

Packets the disabled MAC never sent are also counted in tx_packets/tx_bytes
and released with dev_consume_skb_any(), rather than counted as drops.

The commit message says:

    TX is not recovered after a sudden MAC disable.

Should it also say that the driver keeps accepting packets and leaking them
in this state? Alternatively, should the queue be stopped once the desync
is detected?

>  	/* Since the TX ring was likely just drained, check if TX queue
>  	 * had previously been stopped and now that there are TX buffers
>  	 * available the TX queue can be awakened.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795206150.git.cbabroski%40nvidia.com

  reply	other threads:[~2026-11-24 20:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 20:24 [PATCH net-next v1 0/2] mlxbf_gige: disable MAC after panic Chris Babroski
2026-11-20 20:24 ` [PATCH net-next v1 1/2] mlxbf_gige: protect against MAC disable during TX Chris Babroski
2026-11-24 20:44   ` netdev-bot+sashiko [this message]
2026-11-20 20:24 ` [PATCH net-next v1 2/2] mlxbf_gige: disable MAC after kernel panic Chris Babroski
2026-11-24 20:44   ` netdev-bot+sashiko
2026-11-20 20:27 ` [PATCH net-next v1 0/2] mlxbf_gige: disable MAC after panic netdev-bot+sinfo
2026-11-20 20:43   ` Chris Babroski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179555306048.3135.18299984453276873378@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=cbabroski@nvidia.com \
    --cc=davem@davemloft.net \
    --cc=davthompson@nvidia.com \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox