mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] net: ethernet: mtk_eth_soc: Fix potential PPE hash table leak
@ 2026-11-12 13:59 Guangshuo Li
  2026-11-12 14:02 ` netdev-bot+sinfo
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Guangshuo Li @ 2026-11-12 13:59 UTC (permalink / raw)
  To: Felix Fietkau, Lorenzo Bianconi, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Matthias Brugger,
	AngeloGioacchino Del Regno
  Cc: netdev, linux-kernel, linux-arm-kernel, linux-mediatek

The remove path does not destroy the PPE L2 flow hash tables. Their
bucket tables are not device-managed and remain allocated when the PPE
objects are released.

Call mtk_ppe_deinit() after unregistering the netdevs and cancelling the
driver work, as the probe error path already does.

This issue was found by our static analysis tool under development.

Fixes: 33fc42de3327 ("net: ethernet: mtk_eth_soc: support creating mac address based offload entries")
Signed-off-by: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>
---
 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/net/ethernet/mediatek/mtk_eth_soc.c b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
index ad0fdc4133fb..08973b6f0711 100644
--- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
+++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
@@ -5426,6 +5426,7 @@ static void mtk_remove(struct platform_device *pdev)
 	netif_napi_del(&eth->rx_napi);
 	mtk_cleanup(eth);
 	free_netdev(eth->dummy_dev);
+	mtk_ppe_deinit(eth);
 	mtk_mdio_cleanup(eth);
 }
 

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.43.0


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

* Re: [PATCH net] net: ethernet: mtk_eth_soc: Fix potential PPE hash table leak
  2026-11-12 13:59 [PATCH net] net: ethernet: mtk_eth_soc: Fix potential PPE hash table leak Guangshuo Li
@ 2026-11-12 14:02 ` netdev-bot+sinfo
  2026-11-12 14:55 ` Lorenzo Bianconi
  2026-11-16 14:01 ` netdev-bot+sashiko
  2 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sinfo @ 2026-11-12 14:02 UTC (permalink / raw)
  To: Guangshuo Li
  Cc: Felix Fietkau, Lorenzo Bianconi, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Matthias Brugger,
	AngeloGioacchino Del Regno, netdev, linux-kernel,
	linux-arm-kernel, linux-mediatek

Hi!

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

 - What hardware the change was tested on. For driver fixes please
   mention the device (and if relevant firmware version) used for
   testing, or say that the change was not tested on real hardware.

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. If the series needs another revision for
other reasons, please include the information in the commit messages
then.

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: mtk_eth_soc: Fix potential PPE hash table leak
  2026-11-12 13:59 [PATCH net] net: ethernet: mtk_eth_soc: Fix potential PPE hash table leak Guangshuo Li
  2026-11-12 14:02 ` netdev-bot+sinfo
@ 2026-11-12 14:55 ` Lorenzo Bianconi
  2026-11-16 14:01 ` netdev-bot+sashiko
  2 siblings, 0 replies; 4+ messages in thread
From: Lorenzo Bianconi @ 2026-11-12 14:55 UTC (permalink / raw)
  To: Guangshuo Li
  Cc: Felix Fietkau, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Matthias Brugger,
	AngeloGioacchino Del Regno, netdev, linux-kernel,
	linux-arm-kernel, linux-mediatek

[-- Attachment #1: Type: text/plain, Size: 1279 bytes --]

> The remove path does not destroy the PPE L2 flow hash tables. Their
> bucket tables are not device-managed and remain allocated when the PPE
> objects are released.
> 
> Call mtk_ppe_deinit() after unregistering the netdevs and cancelling the
> driver work, as the probe error path already does.
> 
> This issue was found by our static analysis tool under development.
> 
> Fixes: 33fc42de3327 ("net: ethernet: mtk_eth_soc: support creating mac address based offload entries")
> Signed-off-by: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>

Reviewed-by: Lorenzo Bianconi <lorenzo@kernel.org>

> ---
>  drivers/net/ethernet/mediatek/mtk_eth_soc.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/net/ethernet/mediatek/mtk_eth_soc.c b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
> index ad0fdc4133fb..08973b6f0711 100644
> --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
> +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
> @@ -5426,6 +5426,7 @@ static void mtk_remove(struct platform_device *pdev)
>  	netif_napi_del(&eth->rx_napi);
>  	mtk_cleanup(eth);
>  	free_netdev(eth->dummy_dev);
> +	mtk_ppe_deinit(eth);
>  	mtk_mdio_cleanup(eth);
>  }
>  
> 
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> -- 
> 2.43.0
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: [PATCH net] net: ethernet: mtk_eth_soc: Fix potential PPE hash table leak
  2026-11-12 13:59 [PATCH net] net: ethernet: mtk_eth_soc: Fix potential PPE hash table leak Guangshuo Li
  2026-11-12 14:02 ` netdev-bot+sinfo
  2026-11-12 14:55 ` Lorenzo Bianconi
@ 2026-11-16 14:01 ` netdev-bot+sashiko
  2 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 14:01 UTC (permalink / raw)
  To: liguangshuo
  Cc: nbd, lorenzo, andrew+netdev, davem, edumazet, kuba, pabeni,
	matthias.bgg, angelogioacchino.delregno, netdev, linux-kernel,
	linux-arm-kernel, linux-mediatek

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

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

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

- [High] The fix is incomplete.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is a high-
  severity use-after-free in the same PPE teardown.
- [High] This problem wasn't introduced by this patch, but it is a high-
  severity work-cancellation ordering race in the remove path the patch…

--

net: ethernet: mtk_eth_soc: Fix potential PPE hash table leak

This patch adds a call to mtk_ppe_deinit() in mtk_remove(). The goal is
to destroy the PPE L2 flow rhashtables, whose bucket tables are not
device-managed. This matches what the probe error path already does.

> diff --git a/drivers/net/ethernet/mediatek/mtk_eth_soc.c b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
> index ad0fdc4133fb6..08973b6f0711d 100644
> --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
> +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
> @@ -5426,6 +5426,7 @@ static void mtk_remove(struct platform_device *pdev)
>  	netif_napi_del(&eth->rx_napi);
>  	mtk_cleanup(eth);

[Severity: High]
This is a pre-existing issue, but the commit message says
mtk_ppe_deinit() runs "after unregistering the netdevs and cancelling
the driver work". Is the driver work actually guaranteed to be stopped
at this point?

mtk_cleanup() frees the netdevs before it cancels either work item. It
also cancels pending_work before monitor_work:

drivers/net/ethernet/mediatek/mtk_eth_soc.c:mtk_cleanup() {
	mtk_sgmii_destroy(eth);
	mtk_unreg_dev(eth);
	mtk_free_dev(eth);
	cancel_work_sync(&eth->pending_work);
	cancel_delayed_work_sync(&eth->reset.monitor_work);
	...
}

monitor_work stays armed during all of mtk_remove(), and it can queue
pending_work again:

mtk_hw_reset_monitor_work() {
	...
	if (mtk_hw_check_dma_hang(eth))
		schedule_work(&eth->pending_work);
out:
	schedule_delayed_work(&eth->reset.monitor_work,
			      MTK_DMA_MONITOR_TIMEOUT);
}

Say a monitor_work instance is running when
cancel_work_sync(&eth->pending_work) returns. It can queue pending_work
again. cancel_delayed_work_sync() waits for monitor_work but does not
cancel that newly queued pending_work.

mtk_pending_work() would then run after mtk_remove() returns:

	if (!eth->netdev[i] || !netif_running(eth->netdev[i]))
		continue;
	mtk_stop(eth->netdev[i]);

Would that dereference the netdevs freed by mtk_free_dev()? The
eth->netdev[] pointers are never cleared, and eth itself is
devm-allocated, so it may already be freed too.

pending_work queued during remove can hit the same problem. It waits
for rtnl while unregister_netdev() holds it, and runs once rtnl is
released.

Would it be safer to disable monitor_work first and then pending_work,
for example with disable_delayed_work_sync() and disable_work_sync(),
before the netdevs are unregistered and freed?

>  	free_netdev(eth->dummy_dev);
> +	mtk_ppe_deinit(eth);

[Severity: High]
Does this cover all of the PPE offload hash tables? mtk_ppe_deinit()
only destroys ppe->l2_flows. A second rhashtable that is not
device-managed, eth->flow_table, is never destroyed anywhere in the
driver:

drivers/net/ethernet/mediatek/mtk_ppe_offload.c:mtk_eth_offload_init() {
	if (!eth->ppe[id] || !eth->ppe[id]->foe_table)
		return 0;
	return rhashtable_init(&eth->flow_table, &mtk_flow_ht_params);
}

mtk_probe() calls this inside the per-PPE loop:

	for (i = 0; i < ppe_num; i++) {
		...
		eth->ppe[i] = mtk_ppe_init(eth, eth->base + ppe_addr, i);
		...
		err = mtk_eth_offload_init(eth, i);

Some SoCs set .ppe_num to 2 (MT7981/MT7986) or 3 (MT7988). On those,
does each loop iteration re-initialize the shared eth->flow_table and
lose the bucket table allocated in the previous iteration? That would
also repeat on every EPROBE_DEFER retry.

The bucket table that remains also stays allocated after mtk_remove(),
even with this patch. The same is true for the err_deinit_ppe unwind
in mtk_probe().

There is also the deferred shrink work to consider. mtk_flow_ht_params
sets .automatic_shrinking = true. That means rhashtable_remove_fast()
called from mtk_flow_offload_destroy() can queue ht->run_irq_work, and
then ht->run_work, when the table is lightly loaded.

Without a rhashtable_destroy(&eth->flow_table), nothing syncs or
cancels that work. Can rht_deferred_worker() then run on eth after
devres has freed it?

Would it make sense to initialize eth->flow_table only once, and pair
it with rhashtable_destroy() in both mtk_remove() and the probe unwind?

[Severity: High]
This isn't a bug introduced by this patch, but mtk_ppe_deinit() also
leaves the per-PPE debugfs entries in place. mtk_ppe_init() calls
mtk_ppe_debugfs_init(), which does this:

drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c:mtk_ppe_debugfs_init() {
	...
	root = debugfs_create_dir(ppe->dirname, NULL);
	debugfs_create_file("entries", S_IRUGO, root, ppe, &mtk_ppe_debugfs_foe_all_fops);
	debugfs_create_file("bind", S_IRUGO, root, ppe, &mtk_ppe_debugfs_foe_bind_fops);
	...
}

The root dentry is thrown away, and debugfs_remove() is never called.
That is true for mtk_remove(), mtk_ppe_deinit() and the probe error
path.

Unbinding, or a probe failure after mtk_ppe_init(), releases ppe,
ppe->foe_table, ppe->acct_table and the MMIO mapping through devres.
But /sys/kernel/debug/ppeN/entries and /sys/kernel/debug/ppeN/bind
stay in place.

Can reading those files after unbind use freed memory?
mtk_ppe_debugfs_foe_show() dereferences the freed ppe and foe_table.
It then calls mtk_foe_entry_get_mib(), which reads registers through
the stale ppe->base and writes into the freed acct_table:

	acct->bytes += bytes;
	acct->packets += packets;

On re-probe, debugfs_create_dir() fails because the directory already
exists. The stale files would then keep pointing at the old ppe.

Could the dentry be stored in struct mtk_ppe and removed in
mtk_ppe_deinit() and the probe error path?

>  	mtk_mdio_cleanup(eth);
>  }

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112-88a3a7ff32-net-mtk%40mail.sdu.edu.cn

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

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

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-12 13:59 [PATCH net] net: ethernet: mtk_eth_soc: Fix potential PPE hash table leak Guangshuo Li
2026-11-12 14:02 ` netdev-bot+sinfo
2026-11-12 14:55 ` Lorenzo Bianconi
2026-11-16 14:01 ` 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

all inboxes | Powered by JetHome®