* [PATCH net v3] mctp i2c: fix double free of midev on netdev register failure
@ 2026-11-18 9:05 Kenny Hsieh
2026-11-18 9:08 ` netdev-bot+sinfo
0 siblings, 1 reply; 2+ messages in thread
From: Kenny Hsieh @ 2026-11-18 9:05 UTC (permalink / raw)
To: netdev, Jeremy Kerr, Matt Johnston
Cc: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, stable, linux-kernel
mctp_i2c_add_netdev() frees midev if mctp_register_netdev() fails, but
register_netdevice() may already have freed it through ndo_uninit,
mctp_i2c_ndo_uninit(). midev is then freed twice: list_del() runs on a
poisoned entry, and the adapter and client lose a second reference.
Free midev only where it was set up: in the add_netdev() error path,
and in mctp_i2c_unregister() before free_netdev().
ndo_uninit now only drops mcli->sel's reference and purges tx_queue.
Both have to happen where the core calls it: after dev_close() and
before it waits for the last netdev reference, also when a
NETDEV_REGISTER notifier fails. sel's reference would otherwise block
that wait, and a queued skb can hold a flow key that pins the netdev.
Both steps are safe to repeat.
Freeing after unregister exposes two more things the old order hid.
Flows released while the netdev unregisters queue the unlock marker,
which is embedded in midev and must not reach kfree_skb() through the
purge. And the bus lock, an rt_mutex taken by the tx thread, was
released by whoever called kthread_stop(); the tx thread now drops it
itself before exiting.
The double free was pointed out by the Sashiko AI review of a pending
mctp-i2c change, as a problem that predates it.
Suggested-by: Matt Johnston <matt@codeconstruct.com.au>
Fixes: f5b8abf9fc3d ("mctp i2c: MCTP I2C binding driver")
Closes: https://lore.kernel.org/netdev/179239912623.3135.11352652156626817288@kernel.org/
Cc: stable@vger.kernel.org
Signed-off-by: Kenny Hsieh <kenny_hsieh@jabil.com>
---
v3:
- Kept ndo_uninit, but it now only drops sel and purges tx_queue.
Removing it in v2 made register hang if a NETDEV_REGISTER notifier
fails, and moved the purge after the refcount wait, where a queued
skb can pin the netdev. Both found by Sashiko on v2. midev is still
freed only once, in mctp_i2c_midev_free().
- Rebased on current net.
v2: https://lore.kernel.org/netdev/20261027053715.1623087-1-kenny_hsieh@jabil.com/
v1: https://lore.kernel.org/netdev/20261022031704.2295541-1-kenny_hsieh@jabil.com/
drivers/net/mctp/mctp-i2c.c | 86 ++++++++++++++++++++++++++-----------
1 file changed, 62 insertions(+), 24 deletions(-)
diff --git a/drivers/net/mctp/mctp-i2c.c b/drivers/net/mctp/mctp-i2c.c
index b02476d338bb..a3d0cae41e13 100644
--- a/drivers/net/mctp/mctp-i2c.c
+++ b/drivers/net/mctp/mctp-i2c.c
@@ -628,8 +628,11 @@ static int mctp_i2c_tx_thread(void *data)
unsigned long flags;
for (;;) {
- if (kthread_should_stop())
+ if (kthread_should_stop()) {
+ /* Only the rt_mutex owner may release the bus lock */
+ mctp_i2c_unlock_reset(midev);
break;
+ }
spin_lock_irqsave(&midev->tx_queue.lock, flags);
skb = __skb_dequeue(&midev->tx_queue);
@@ -782,11 +785,60 @@ static struct mctp_i2c_dev *mctp_i2c_midev_init(struct net_device *dev,
return midev;
}
+/* Removes midev from the parent i2c client, moving sel off midev.
+ * ndo_uninit and mctp_i2c_unregister() need this before the refcount wait,
+ * as sel's netdev reference would block it. A second call on the same
+ * midev does nothing.
+ */
+static void mctp_i2c_midev_detach(struct mctp_i2c_dev *midev)
+{
+ struct mctp_i2c_client *mcli = midev->client;
+ unsigned long flags;
+
+ spin_lock_irqsave(&mcli->sel_lock, flags);
+ if (list_empty(&midev->list)) {
+ spin_unlock_irqrestore(&mcli->sel_lock, flags);
+ return;
+ }
+ list_del_init(&midev->list);
+ if (mcli->sel == midev) {
+ struct mctp_i2c_dev *first;
+
+ first = list_first_entry_or_null(&mcli->devs, struct mctp_i2c_dev, list);
+ __mctp_i2c_device_select(mcli, first);
+ }
+ spin_unlock_irqrestore(&mcli->sel_lock, flags);
+}
+
+/* Drops queued skbs, keeping the embedded unlock marker from kfree_skb() */
+static void mctp_i2c_tx_purge(struct mctp_i2c_dev *midev)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&midev->tx_queue.lock, flags);
+ if (midev->unlock_marker.next)
+ __skb_unlink(&midev->unlock_marker, &midev->tx_queue);
+ spin_unlock_irqrestore(&midev->tx_queue.lock, flags);
+
+ skb_queue_purge(&midev->tx_queue);
+}
+
+/* Runs inside unregister, after dev_close() and before the refcount wait,
+ * including when register_netdevice() fails at NETDEV_REGISTER. Both steps
+ * can repeat, so mctp_i2c_midev_free() still frees midev exactly once.
+ */
+static void mctp_i2c_ndo_uninit(struct net_device *dev)
+{
+ struct mctp_i2c_dev *midev = netdev_priv(dev);
+
+ mctp_i2c_midev_detach(midev);
+ mctp_i2c_tx_purge(midev);
+}
+
/* Counterpart of mctp_i2c_midev_init */
static void mctp_i2c_midev_free(struct mctp_i2c_dev *midev)
{
struct mctp_i2c_client *mcli = midev->client;
- unsigned long flags;
if (midev->tx_thread) {
kthread_stop(midev->tx_thread);
@@ -797,17 +849,9 @@ static void mctp_i2c_midev_free(struct mctp_i2c_dev *midev)
mctp_i2c_unlock_reset(midev);
/* Remove the netdev from the parent i2c client. */
- spin_lock_irqsave(&mcli->sel_lock, flags);
- list_del(&midev->list);
- if (mcli->sel == midev) {
- struct mctp_i2c_dev *first;
+ mctp_i2c_midev_detach(midev);
- first = list_first_entry_or_null(&mcli->devs, struct mctp_i2c_dev, list);
- __mctp_i2c_device_select(mcli, first);
- }
- spin_unlock_irqrestore(&mcli->sel_lock, flags);
-
- skb_queue_purge(&midev->tx_queue);
+ mctp_i2c_tx_purge(midev);
put_device(&midev->adapter->dev);
put_device(&mcli->client->dev);
}
@@ -827,23 +871,17 @@ static void mctp_i2c_unregister(struct mctp_i2c_dev *midev)
spin_unlock_irqrestore(&midev->lock, flags);
wait_for_completion(&midev->rx_done);
+ /* Release sel's reference on the netdev, otherwise
+ * mctp_unregister_netdev() blocks waiting for it.
+ */
+ mctp_i2c_midev_detach(midev);
+
mctp_unregister_netdev(midev->ndev);
- /* midev has been freed now by mctp_i2c_ndo_uninit callback */
+ mctp_i2c_midev_free(midev);
free_netdev(midev->ndev);
}
-static void mctp_i2c_ndo_uninit(struct net_device *dev)
-{
- struct mctp_i2c_dev *midev = netdev_priv(dev);
-
- /* Perform cleanup here to ensure that mcli->sel isn't holding
- * a reference that would prevent unregister_netdevice()
- * from completing.
- */
- mctp_i2c_midev_free(midev);
-}
-
static int mctp_i2c_ndo_open(struct net_device *dev)
{
struct mctp_i2c_dev *midev = netdev_priv(dev);
--
2.50.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH net v3] mctp i2c: fix double free of midev on netdev register failure
2026-11-18 9:05 [PATCH net v3] mctp i2c: fix double free of midev on netdev register failure Kenny Hsieh
@ 2026-11-18 9:08 ` netdev-bot+sinfo
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sinfo @ 2026-11-18 9:08 UTC (permalink / raw)
To: Kenny Hsieh
Cc: netdev, Jeremy Kerr, Matt Johnston, Andrew Lunn,
David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
stable, linux-kernel
Hi!
This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical
(e.g. found by LLM and not proven at runtime). If it was triggered
please include the symptoms, like the stack trace or kernel messages.
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] 2+ messages in thread
end of thread, other threads:[~2026-11-18 9:08 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 9:05 [PATCH net v3] mctp i2c: fix double free of midev on netdev register failure Kenny Hsieh
2026-11-18 9:08 ` netdev-bot+sinfo
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®