mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net-next v4] mctp i2c: add sysfs control for mux child netdevs
@ 2026-11-25  2:54 Kenny Hsieh
  2026-11-29  3:14 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Kenny Hsieh @ 2026-11-25  2:54 UTC (permalink / raw)
  To: netdev, Jeremy Kerr, Matt Johnston, Andi Shyti, Peter Rosin
  Cc: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, linux-i2c, linux-kernel

An MCTP netdev is created for an I2C bus, root or mux channel, that
carries the "mctp-controller" devicetree property. A mux created at
runtime through the I2C sysfs interface has no devicetree node, and
neither do its channels, so they can never get a netdev.

This matters when the mux topology is only known at runtime. A BMC
that ships one firmware image across several chassis cannot describe
every chassis's mux tree in its devicetree, so userspace creates the
muxes it finds over I2C sysfs instead.

Add two write-only attributes to the mctp-i2c-controller device:

  echo i2c-30 > /sys/bus/i2c/devices/9-1010/add_mctp_controller
  echo i2c-30 > /sys/bus/i2c/devices/9-1010/remove_mctp_controller

Writing the name of a bus in the controller's mux tree creates or
removes the netdev for that bus.

The attributes belong to the driver, through dev_groups, so they exist
only while the controller is bound. Netdevs added through them are
removed on unbind and are not restored on rebind. The devicetree
property keeps working, and both can be used on the same mux tree.

mctp_i2c_add_netdev() now skips an adapter that already has a netdev.
An adapter can be reached twice: by the probe walk and then by its own
add notifier, or by a sysfs write that lands before that notifier.
The notifier also handles BUS_NOTIFY_REMOVED_DEVICE, since a sysfs
write can add a netdev after BUS_NOTIFY_DEL_DEVICE.

A write requires CAP_NET_ADMIN in the initial user namespace, as for
mctp-serial, since the netdev is for a physical bus.

Tested on an AST2700 BMC with a two-level mux tree: add and remove on
a channel without the devicetree property, unbind and bind of the
controller, and a loop of both while MCTP traffic runs on the channel.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Kenny Hsieh <kenny_hsieh@jabil.com>
---
v4:
  - Check CAP_NET_ADMIN with capable() instead of in the writer's own
    netns, as Sashiko pointed out. Any task can get CAP_NET_ADMIN in a
    netns it creates, which made the old check no gate at all.
  - Fixed the commit message on where a duplicate add comes from, and
    explained BUS_NOTIFY_REMOVED_DEVICE in it.
  - Listed EPERM, EINVAL and the second ENODEV case in the ABI doc.
  - The double free on a failed register is fixed separately in net,
    now at v5, and the two patches apply in either order:
    https://lore.kernel.org/netdev/20261124101237.3884438-1-kenny_hsieh@jabil.com/

v3:
  - Moved the attributes to the mctp-i2c-controller device, as Matt
    suggested. The driver core handles their lifetime now, so the
    tracking list and the races Sashiko found with it are gone.
  - Repeated add returns -EEXIST, remove with no netdev returns -ENODEV,
    per Matt.
  - Also remove on BUS_NOTIFY_REMOVED_DEVICE, so a write racing an
    adapter delete can't leave a netdev behind.
  - Dropped the stored netns, since get_net() kept it alive while the
    controller was bound. The netdev goes in the writer's netns now.
  - Dropped the midev = NULL change, the double free is fixed separately:
    https://lore.kernel.org/netdev/20261118090515.3390034-1-kenny_hsieh@jabil.com/
  - Rewrote the ABI doc, rebased on current net-next.

v3: https://lore.kernel.org/netdev/20261118100231.3827609-1-kenny_hsieh@jabil.com/
v2: https://lore.kernel.org/netdev/20261015081341.1262695-1-kenny_hsieh@jabil.com/
v1: https://lore.kernel.org/netdev/20261002073156.759005-1-kenny_hsieh@jabil.com/

 .../ABI/testing/sysfs-bus-i2c-devices-mctp    |  27 +++++
 MAINTAINERS                                   |   1 +
 drivers/net/mctp/mctp-i2c.c                   | 110 ++++++++++++++++--
 3 files changed, 127 insertions(+), 11 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp

diff --git a/Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp b/Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp
new file mode 100644
index 0000000000000..4efbc888c7bb8
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp
@@ -0,0 +1,27 @@
+What:		/sys/bus/i2c/devices/<bus>-<addr>/add_mctp_controller
+What:		/sys/bus/i2c/devices/<bus>-<addr>/remove_mctp_controller
+Date:		December 2026
+KernelVersion:	7.5
+Contact:	Kenny Hsieh <kenny_hsieh@jabil.com>
+Description:
+		Write-only, on each bound mctp-i2c-controller device.
+
+		Writing an I2C bus name such as "i2c-30" to add_mctp_controller
+		creates an MCTP network device for that bus, and writing it to
+		remove_mctp_controller removes it. The bus must be the
+		controller's root bus or a mux child below it. Adding a bus
+		that already has a network device fails with EEXIST, and
+		removing one that has none fails with ENODEV.
+		Writes need CAP_NET_ADMIN, else EPERM. A name that matches no
+		I2C device fails with ENODEV, and one that is not a bus in this
+		controller's mux tree with EINVAL.
+
+		This covers busses instantiated at runtime, which have no
+		devicetree node for the "mctp-controller" property. Network
+		devices added here are removed on unbind and not restored on
+		rebind.
+
+		Example::
+
+		  # echo i2c-30 > /sys/bus/i2c/devices/9-1010/add_mctp_controller
+		  # echo i2c-30 > /sys/bus/i2c/devices/9-1010/remove_mctp_controller
diff --git a/MAINTAINERS b/MAINTAINERS
index 7d8b59ae6ea6c..ca07689318285 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -16093,6 +16093,7 @@ M:	Jeremy Kerr <jk@codeconstruct.com.au>
 M:	Matt Johnston <matt@codeconstruct.com.au>
 L:	netdev@vger.kernel.org
 S:	Maintained
+F:	Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp
 F:	Documentation/networking/mctp.rst
 F:	drivers/net/mctp/
 F:	include/linux/usb/mctp-usb.h
diff --git a/drivers/net/mctp/mctp-i2c.c b/drivers/net/mctp/mctp-i2c.c
index b02476d338bbf..ecc3036d54a35 100644
--- a/drivers/net/mctp/mctp-i2c.c
+++ b/drivers/net/mctp/mctp-i2c.c
@@ -857,6 +857,24 @@ static int mctp_i2c_ndo_open(struct net_device *dev)
 	return 0;
 }
 
+static struct mctp_i2c_dev *mctp_i2c_find_dev(struct mctp_i2c_client *mcli,
+					      struct i2c_adapter *adap)
+{
+	struct mctp_i2c_dev *midev = NULL, *m = NULL;
+	unsigned long flags;
+
+	spin_lock_irqsave(&mcli->sel_lock, flags);
+	/* List size is limited by number of MCTP netdevs on a single hardware bus */
+	list_for_each_entry(m, &mcli->devs, list)
+		if (m->adapter == adap) {
+			midev = m;
+			break;
+		}
+	spin_unlock_irqrestore(&mcli->sel_lock, flags);
+
+	return midev;
+}
+
 static int mctp_i2c_add_netdev(struct mctp_i2c_client *mcli,
 			       struct i2c_adapter *adap)
 {
@@ -876,6 +894,9 @@ static int mctp_i2c_add_netdev(struct mctp_i2c_client *mcli,
 	}
 
 	WARN_ON(!mutex_is_locked(&driver_clients_lock));
+	if (mctp_i2c_find_dev(mcli, adap))
+		return 0;
+
 	snprintf(namebuf, sizeof(namebuf), "mctpi2c%d", adap->nr);
 	ndev = alloc_netdev(sizeof(*midev), namebuf, NET_NAME_ENUM, mctp_i2c_net_setup);
 	if (!ndev) {
@@ -920,19 +941,10 @@ static int mctp_i2c_add_netdev(struct mctp_i2c_client *mcli,
 static void mctp_i2c_remove_netdev(struct mctp_i2c_client *mcli,
 				   struct i2c_adapter *adap)
 {
-	struct mctp_i2c_dev *midev = NULL, *m = NULL;
-	unsigned long flags;
+	struct mctp_i2c_dev *midev;
 
 	WARN_ON(!mutex_is_locked(&driver_clients_lock));
-	spin_lock_irqsave(&mcli->sel_lock, flags);
-	/* List size is limited by number of MCTP netdevs on a single hardware bus */
-	list_for_each_entry(m, &mcli->devs, list)
-		if (m->adapter == adap) {
-			midev = m;
-			break;
-		}
-	spin_unlock_irqrestore(&mcli->sel_lock, flags);
-
+	midev = mctp_i2c_find_dev(mcli, adap);
 	if (midev)
 		mctp_i2c_unregister(midev);
 }
@@ -1077,6 +1089,79 @@ static void mctp_i2c_remove(struct i2c_client *client)
 	mutex_unlock(&driver_clients_lock);
 }
 
+/* Returns the named adapter in mcli's mux tree, with a reference held */
+static struct i2c_adapter *mctp_i2c_find_named(struct mctp_i2c_client *mcli,
+					       const char *name)
+{
+	struct i2c_adapter *adap = NULL, *root = NULL;
+	struct device *dev;
+
+	dev = bus_find_device_by_name(&i2c_bus_type, NULL, name);
+	if (!dev)
+		return ERR_PTR(-ENODEV);
+
+	adap = mctp_i2c_get_adapter(dev, &root);
+	if (!adap || root != mcli->client->adapter) {
+		put_device(dev);
+		return ERR_PTR(-EINVAL);
+	}
+	return adap;
+}
+
+static ssize_t mctp_i2c_controller_write(struct device *dev, const char *buf,
+					 size_t count, bool add)
+{
+	struct mctp_i2c_client *mcli = i2c_get_clientdata(to_i2c_client(dev));
+	struct i2c_adapter *adap;
+	int rc = 0;
+
+	if (!capable(CAP_NET_ADMIN))
+		return -EPERM;
+
+	mutex_lock(&driver_clients_lock);
+	adap = mctp_i2c_find_named(mcli, buf);
+	if (IS_ERR(adap)) {
+		rc = PTR_ERR(adap);
+		goto out;
+	}
+
+	if (add && mctp_i2c_find_dev(mcli, adap))
+		rc = -EEXIST;
+	else if (add)
+		rc = mctp_i2c_add_netdev(mcli, adap);
+	else if (!mctp_i2c_find_dev(mcli, adap))
+		rc = -ENODEV;
+	else
+		mctp_i2c_remove_netdev(mcli, adap);
+	put_device(&adap->dev);
+out:
+	mutex_unlock(&driver_clients_lock);
+	return rc < 0 ? rc : count;
+}
+
+static ssize_t add_mctp_controller_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf, size_t count)
+{
+	return mctp_i2c_controller_write(dev, buf, count, true);
+}
+static DEVICE_ATTR_WO(add_mctp_controller);
+
+static ssize_t remove_mctp_controller_store(struct device *dev,
+					    struct device_attribute *attr,
+					    const char *buf, size_t count)
+{
+	return mctp_i2c_controller_write(dev, buf, count, false);
+}
+static DEVICE_ATTR_WO(remove_mctp_controller);
+
+static struct attribute *mctp_i2c_attrs[] = {
+	&dev_attr_add_mctp_controller.attr,
+	&dev_attr_remove_mctp_controller.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(mctp_i2c);
+
 /* We look for a 'mctp-controller' property on I2C busses as they are
  * added/deleted, creating/removing netdevs as required.
  */
@@ -1090,6 +1175,8 @@ static int mctp_i2c_notifier_call(struct notifier_block *nb,
 		mctp_i2c_notify_add(dev);
 		break;
 	case BUS_NOTIFY_DEL_DEVICE:
+	/* A sysfs write can add a netdev after DEL_DEVICE */
+	case BUS_NOTIFY_REMOVED_DEVICE:
 		mctp_i2c_notify_del(dev);
 		break;
 	}
@@ -1116,6 +1203,7 @@ static struct i2c_driver mctp_i2c_driver = {
 	.driver = {
 		.name = "mctp-i2c-interface",
 		.of_match_table = mctp_i2c_of_match,
+		.dev_groups = mctp_i2c_groups,
 	},
 	.probe = mctp_i2c_probe,
 	.remove = mctp_i2c_remove,
-- 
2.50.1


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

* Re: [PATCH net-next v4] mctp i2c: add sysfs control for mux child netdevs
  2026-11-25  2:54 [PATCH net-next v4] mctp i2c: add sysfs control for mux child netdevs Kenny Hsieh
@ 2026-11-29  3:14 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-29  3:14 UTC (permalink / raw)
  To: kenny_hsieh
  Cc: netdev, jk, matt, andi.shyti, peda, andrew+netdev, davem,
	edumazet, kuba, pabeni, linux-i2c, linux-kernel

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

Critical: 0 · High: 0 (1) · Medium: 2 · 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] This net-next feature patch also fixes two races in existing
  code, but it has no Fixes: tag and is not sent to net.
- [Medium] A sysfs add can attach a netdev to an I2C adapter whose
  device_add() later fails.
- [Low] The new sysfs ABI does not document which network namespace the
  netdev lands in.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the new sysfs
  path makes it easier to hit.

--

mctp i2c: add sysfs control for mux child netdevs

This patch adds two write-only attributes, add_mctp_controller and
remove_mctp_controller, to the mctp-i2c controller device. They let
userspace create or remove MCTP netdevs for busses in the controller's mux
tree that have no devicetree node. It also makes mctp_i2c_add_netdev() skip
adapters that already have a netdev, and handles BUS_NOTIFY_REMOVED_DEVICE
in the bus notifier.

> diff --git a/Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp b/Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp
> new file mode 100644
> index 0000000000000..4efbc888c7bb8
> --- /dev/null
> +++ b/Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp
> @@ -0,0 +1,27 @@

[ ... ]

> +Description:
> +		Write-only, on each bound mctp-i2c-controller device.
> +
> +		Writing an I2C bus name such as "i2c-30" to add_mctp_controller
> +		creates an MCTP network device for that bus, and writing it to
> +		remove_mctp_controller removes it. The bus must be the
> +		controller's root bus or a mux child below it. Adding a bus
> +		that already has a network device fails with EEXIST, and
> +		removing one that has none fails with ENODEV.

[Severity: Low]
Which network namespace does the new netdev end up in? The add path runs
mctp_i2c_controller_write()->mctp_i2c_add_netdev(), which does:

	dev_net_set(ndev, current->nsproxy->net_ns);

So the netdev for the physical bus is created in the writer's netns. That
happens, for example, under ip netns exec or inside a privileged container.

The capable(CAP_NET_ADMIN) check only tests the initial user namespace.
remove_mctp_controller looks the netdev up in mcli->devs and never compares
namespaces. A writer in any netns can therefore remove a netdev that lives
in another one, including devicetree-created netdevs in init_net.

Could the ABI description and the commit message say this? Right now only
the v3 changelog below the --- line says "The netdev goes in the writer's
netns now", and that text won't reach git history.

[ ... ]

> diff --git a/drivers/net/mctp/mctp-i2c.c b/drivers/net/mctp/mctp-i2c.c
> index b02476d338bbf..ecc3036d54a35 100644
> --- a/drivers/net/mctp/mctp-i2c.c
> +++ b/drivers/net/mctp/mctp-i2c.c

[ ... ]

> @@ -876,6 +894,9 @@ static int mctp_i2c_add_netdev(struct mctp_i2c_client *mcli,
>  	}
>  
>  	WARN_ON(!mutex_is_locked(&driver_clients_lock));
> +	if (mctp_i2c_find_dev(mcli, adap))
> +		return 0;
> +

[Severity: Medium]
Does this early return also fix an existing race that has nothing to do
with the new sysfs attributes?

mctp_i2c_probe() walks the bus under driver_clients_lock:

	i2c_for_each_dev(mcli, mctp_i2c_client_try_attach);

device_add() puts the adapter on the bus klist in bus_add_device() before
it sends BUS_NOTIFY_ADD_DEVICE. So the probe walk can add a netdev first,
and then mctp_i2c_notify_add() calls mctp_i2c_add_netdev() again for the
same adapter.

Before this patch, the second netdev failed registration with -EEXIST and
the driver logged errors. If the probe and the adapter registration ran in
different network namespaces, a second netdev was registered for the same
adapter.

The new BUS_NOTIFY_REMOVED_DEVICE case in mctp_i2c_notifier_call() looks
similar:

	case BUS_NOTIFY_DEL_DEVICE:
	/* A sysfs write can add a netdev after DEL_DEVICE */
	case BUS_NOTIFY_REMOVED_DEVICE:

device_del() sends BUS_NOTIFY_DEL_DEVICE but leaves the adapter on the bus
klist until bus_remove_device(). i2c_del_adapter() also doesn't hold
core_lock across device_unregister().

A controller probe walk in that window can reach
mctp_i2c_client_try_attach()->mctp_i2c_add_netdev() for the adapter being
deleted, and takes a reference on adap->dev. Before this patch nothing
removed that netdev, and i2c_del_adapter() then waited on adap->dev_released
forever.

The code comment and the commit message credit only the sysfs write for the
REMOVED_DEVICE case. The probe walk duplicate is described as part of the
feature rather than as a fix.

Would it make sense to split these two fixes out and send them to net with
Fixes: tags, so that stable kernels get them as well?

[ ... ]

> @@ -1077,6 +1089,79 @@ static void mctp_i2c_remove(struct i2c_client *client)
>  	mutex_unlock(&driver_clients_lock);
>  }
>  
> +/* Returns the named adapter in mcli's mux tree, with a reference held */
> +static struct i2c_adapter *mctp_i2c_find_named(struct mctp_i2c_client *mcli,
> +					       const char *name)
> +{
> +	struct i2c_adapter *adap = NULL, *root = NULL;
> +	struct device *dev;
> +
> +	dev = bus_find_device_by_name(&i2c_bus_type, NULL, name);

[Severity: Medium]
Can this find an adapter whose device_add() hasn't finished yet and later
fails?

i2c_register_adapter()->device_add() puts the adapter on the bus klist in
bus_add_device() and only then calls dpm_sysfs_add(). A write to
add_mctp_controller can land in that window and find the adapter here.

mctp_i2c_get_adapter() only checks dev->type, so mctp_i2c_add_netdev() goes
on to register a netdev. The midev holds a reference on adap->dev from
mctp_i2c_midev_init(), and the netdev has adap->dev as its parent through
SET_NETDEV_DEV().

If dpm_sysfs_add() then fails, device_add() unwinds without sending any bus
notification:

drivers/base/core.c:device_add() {
    ...
 DPMError:
	device_set_driver(dev, NULL);
	bus_remove_device(dev);
    ...
}

BUS_NOTIFY_REMOVED_DEVICE is only sent from device_del(), so
mctp_i2c_notifier_call() never calls mctp_i2c_notify_del() for this
adapter. i2c_register_adapter() then blocks in its error path, because the
netdev still holds its references:

drivers/i2c/i2c-core-base.c:i2c_register_adapter() {
    ...
	init_completion(&adap->dev_released);
	put_device(&adap->dev);
	wait_for_completion(&adap->dev_released);
    ...
}

A write to remove_mctp_controller can't recover from this. The adapter is
no longer on the klist, so mctp_i2c_find_named() returns -ENODEV. Only
unbinding the controller frees the netdev.

The existing probe walk has the same window, but this sysfs path adds a
trigger that userspace controls.

[ ... ]

> +static ssize_t mctp_i2c_controller_write(struct device *dev, const char *buf,
> +					 size_t count, bool add)
> +{
> +	struct mctp_i2c_client *mcli = i2c_get_clientdata(to_i2c_client(dev));
> +	struct i2c_adapter *adap;
> +	int rc = 0;
> +
> +	if (!capable(CAP_NET_ADMIN))
> +		return -EPERM;
> +
> +	mutex_lock(&driver_clients_lock);
> +	adap = mctp_i2c_find_named(mcli, buf);
> +	if (IS_ERR(adap)) {
> +		rc = PTR_ERR(adap);
> +		goto out;
> +	}
> +
> +	if (add && mctp_i2c_find_dev(mcli, adap))
> +		rc = -EEXIST;
> +	else if (add)
> +		rc = mctp_i2c_add_netdev(mcli, adap);

[Severity: High]
This isn't a bug introduced by this patch, but this path lets userspace
call mctp_i2c_add_netdev() as often as it likes. That makes an existing
double free in its error path easier to hit.

Suppose mctp_register_netdev() fails after register_netdevice() has passed
the name check. register_netdevice() then calls ndo_uninit, which for this
driver is mctp_i2c_ndo_uninit()->mctp_i2c_midev_free(midev).

That covers failures in netdev_check_ops, netdev_do_alloc_pcpu_stats,
dev_index_reserve, NETDEV_POST_INIT and netdev_register_kobject. It also
covers a NETDEV_REGISTER notifier veto, such as
mctp_dev_notify()->mctp_register() returning -ENOMEM, which goes through
unregister_netdevice_queue(). A name collision doesn't trigger it, since
dev_get_valid_name() fails before ndo_init.

mctp_i2c_add_netdev() then frees midev a second time:

drivers/net/mctp/mctp-i2c.c:mctp_i2c_add_netdev() {
    ...
err:
	if (midev)
		mctp_i2c_midev_free(midev);
	if (ndev)
		free_netdev(ndev);
	return rc;
}

The second mctp_i2c_midev_free() calls list_del() on an entry that is
already poisoned. It also calls put_device() on midev->adapter->dev and
mcli->client->dev again, which can underflow those refcounts and free the
devices early.

The changelog says the double free is fixed separately in net (now at v5),
but that fix isn't in this tree. If this patch lands first, can the bug be
reached through add_mctp_controller?

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125025411.986998-1-kenny_hsieh%40jabil.com

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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25  2:54 [PATCH net-next v4] mctp i2c: add sysfs control for mux child netdevs Kenny Hsieh
2026-11-29  3:14 ` 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®