public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Théo Lebrun" <theo.lebrun@bootlin.com>
To: "Jakub Kicinski" <kuba@kernel.org>
Cc: <nicolas.ferre@microchip.com>, <claudiu.beznea@tuxon.dev>,
	<andrew+netdev@lunn.ch>, <davem@davemloft.net>,
	<edumazet@google.com>, <pabeni@redhat.com>,
	<richardcochran@gmail.com>, <linux@armlinux.org.uk>,
	<pvalerio@redhat.com>, <conor@kernel.org>, <nb@tipi-net.de>,
	<vladimir.kondratiev@mobileye.com>, <gregory.clement@bootlin.com>,
	<benoit.monin@bootlin.com>, <tawfik.bayouk@mobileye.com>,
	<thomas.petazzoni@bootlin.com>, <maxime.chevallier@bootlin.com>,
	<netdev@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH net-next v2 14/14] net: macb: use context swapping in .ndo_change_mtu()
Date: Wed, 22 Apr 2026 16:35:01 +0200	[thread overview]
Message-ID: <DHZRBCN3OFXQ.18Y21LL2HDX02@bootlin.com> (raw)
In-Reply-To: <20260414005657.3767049-1-kuba@kernel.org>

Hello Jakub,

On Tue Apr 14, 2026 at 2:56 AM CEST, Jakub Kicinski wrote:
> This is an AI-generated review of your patch. The human sending this
> email has considered the AI review valid, or at least plausible.
> ---
> net: macb: use context swapping in .ndo_change_mtu()
>
> This commit introduces a mechanism to perform context swapping when changing
> the MTU while the interface is running. It allocates a new context,
> reconfigures the hardware via a start and end swap process, and then frees
> the old context to avoid PHY reinitialization and gracefully handle memory
> pressure.
>
>> diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
>> index d1536cb1e40bb..79371718f8392 100644
>> --- a/drivers/net/ethernet/cadence/macb_main.c
>> +++ b/drivers/net/ethernet/cadence/macb_main.c
>> @@ -3430,11 +3430,25 @@ static int macb_close(struct net_device *netdev)
>>  
>>  static int macb_change_mtu(struct net_device *netdev, int new_mtu)
>>  {
>> -	if (netif_running(netdev))
>> -		return -EBUSY;
>> +	struct macb *bp = netdev_priv(netdev);
>> +	bool running = netif_running(netdev);
>> +	struct macb_context *new_ctx;
>> +
>> +	if (running) {
>> +		new_ctx = macb_context_alloc(bp, new_mtu,
>> +					     bp->configured_rx_ring_size,
>> +					     bp->configured_tx_ring_size);
>> +		if (IS_ERR(new_ctx))
>> +			return PTR_ERR(new_ctx);
>> +
>> +		macb_context_swap_start(bp);
>
> Does macb_context_swap_start() gracefully halt the transmitter before the
> context is freed?
>
> It appears to attempt disabling the hardware transmitter by clearing the
> TE and RE bits in the NCR register, but does not poll the hardware to ensure
> it has reached an idle state.
>
> Immediately after, macb_context_swap_end() frees the descriptor rings and data
> buffers via macb_free_consistent().
>
> If the MAC DMA engine is still mid-transfer, could it read or write to kernel
> memory that has already been freed or reallocated?

Yes and we already have the solution in macb_halt_tx(), used by
macb_tx_error_task(). Close and swap should also use it. Will fix.

Though from the manual's description of TE it sounds super unlikely.

Rx isn't an issue, modifying RE in NCR is the way to go.

> Can this code cause an AB-BA deadlock between napi_disable() and
> cancel_work_sync()?
>
> In macb_context_swap_start(), the code calls napi_disable() for all queues,
> and then immediately calls cancel_work_sync() on the error tasks.
>
> Both of these work items internally invoke napi_disable().
>
> If either error task runs concurrently, it will block forever inside its
> napi_disable() waiting for the NAPI state bits (which were already set by the
> first call in macb_context_swap_start()) to clear.
>
> Consequently, could cancel_work_sync() hang indefinitely waiting for the
> blocked work task to finish?

Wrong because napi_disable() grabs the netdev->lock mutex.

>>  	}
>>  
>>  	WRITE_ONCE(netdev->mtu, new_mtu);
>>  
>> +	if (running)
>> +		macb_context_swap_end(bp, new_ctx);
>
> Does macb_context_swap_end() leak pending TX buffers and their DMA mappings?

> When changing the MTU while the interface is running, macb_context_swap_end()
> calls macb_free_consistent() to free the old DMA context. While RX buffers are
> properly freed, for the TX queues it appears to only free the tracking array
> pointer without iterating through the ring to unmap the DMA mappings and free
> the actual buffers.
>
> Any packets queued in the hardware ring at the moment of the MTU swap might
> be permanently leaked, which could lead to memory and IOMMU space exhaustion
> under active traffic.

Yes, I have a pending patch for this. Will send soon. It is a bugfix
fixing the close codepath and we'll reuse the code in the swap
operation.

> Will MTU changes on EMAC devices permanently disable hardware interrupts?
>
> macb_context_swap_start() disables all interrupts unconditionally by writing
> -1 to the IDR register.
>
> However, macb_context_swap_end() restores interrupts via the IER register
> inside an explicit check:
>
>     if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
>         ...
>     }
>
> Since there is no else branch to re-enable interrupts for EMAC devices,
> could changing the MTU on these devices leave interrupts permanently masked
> and cause the network interface to silently stall?

Ignoring because we will refuse swap for EMAC devices. Their state is
too different; they have different ndo callbacks for everything and
don't use NAPI.

> Is there a missing spinlock when modifying the IER and NCR registers in
> macb_context_swap_end()?
>
> Modifications to the NCR and interrupt registers in macb_context_swap_start()
> are protected by the spinlock. However, macb_context_swap_end() performs
> lockless read-modify-write operations on both IER and NCR:
>
>     ctrl = macb_readl(bp, NCR);
>     macb_writel(bp, NCR, ctrl | MACB_BIT(RE) | MACB_BIT(TE));
>
> If concurrent hardware events or background tasks take the lock and modify
> NCR during this window, could their updates be silently overwritten?

Yes swap_end needs to wrap its access in the bp->lock spinlock to have
the read-modify-write operation be atomic.

Thanks,

--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com


  reply	other threads:[~2026-04-22 14:35 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-10 19:51 [PATCH net-next v2 00/14] net: macb: implement context swapping Théo Lebrun
2026-04-10 19:51 ` [PATCH net-next v2 01/14] net: macb: unify device pointer naming convention Théo Lebrun
2026-04-10 19:51 ` [PATCH net-next v2 02/14] net: macb: unify `struct macb *` " Théo Lebrun
2026-04-10 19:51 ` [PATCH net-next v2 03/14] net: macb: unify queue index variable naming convention and types Théo Lebrun
2026-04-10 19:51 ` [PATCH net-next v2 04/14] net: macb: enforce reverse christmas tree (RCT) convention Théo Lebrun
2026-04-10 19:51 ` [PATCH net-next v2 05/14] net: macb: allocate tieoff descriptor once across device lifetime Théo Lebrun
2026-04-10 19:51 ` [PATCH net-next v2 06/14] net: macb: introduce macb_context struct for buffer management Théo Lebrun
2026-04-10 19:51 ` [PATCH net-next v2 07/14] net: macb: avoid macb_init_rx_buffer_size() modifying state Théo Lebrun
2026-04-10 19:51 ` [PATCH net-next v2 08/14] net: macb: make `struct macb` subset reachable from macb_context struct Théo Lebrun
2026-04-10 19:51 ` [PATCH net-next v2 09/14] net: macb: change caps helpers signatures Théo Lebrun
2026-04-14  0:47   ` Jakub Kicinski
2026-04-15 13:58     ` Théo Lebrun
2026-04-10 19:51 ` [PATCH net-next v2 10/14] net: macb: change function signatures to take contexts Théo Lebrun
2026-04-10 19:51 ` [PATCH net-next v2 11/14] net: macb: introduce macb_context_alloc() helper Théo Lebrun
2026-04-10 19:52 ` [PATCH net-next v2 12/14] net: macb: re-read ISR inside IRQ handler locked section Théo Lebrun
2026-04-14  0:52   ` Jakub Kicinski
2026-04-15 14:07     ` Théo Lebrun
2026-04-10 19:52 ` [PATCH net-next v2 13/14] net: macb: use context swapping in .set_ringparam() Théo Lebrun
2026-04-14  0:50   ` Jakub Kicinski
2026-04-16  8:54     ` Théo Lebrun
2026-04-14  0:56   ` Jakub Kicinski
2026-04-22 14:00     ` Théo Lebrun
2026-04-10 19:52 ` [PATCH net-next v2 14/14] net: macb: use context swapping in .ndo_change_mtu() Théo Lebrun
2026-04-14  0:56   ` Jakub Kicinski
2026-04-22 14:35     ` Théo Lebrun [this message]
2026-04-10 19:58 ` [PATCH net-next v2 00/14] net: macb: implement context swapping Théo Lebrun

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=DHZRBCN3OFXQ.18Y21LL2HDX02@bootlin.com \
    --to=theo.lebrun@bootlin.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=benoit.monin@bootlin.com \
    --cc=claudiu.beznea@tuxon.dev \
    --cc=conor@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=gregory.clement@bootlin.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=maxime.chevallier@bootlin.com \
    --cc=nb@tipi-net.de \
    --cc=netdev@vger.kernel.org \
    --cc=nicolas.ferre@microchip.com \
    --cc=pabeni@redhat.com \
    --cc=pvalerio@redhat.com \
    --cc=richardcochran@gmail.com \
    --cc=tawfik.bayouk@mobileye.com \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=vladimir.kondratiev@mobileye.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