[PATCH net-next v3] mctp i2c: add sysfs control for mux child netdevs

From: Kenny Hsieh

Date: Wed Nov 18 2026 - 05:03:01 EST


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,
so a probe walk or an add notifier that reaches an adapter a second
time no longer fails on the duplicate mctpi2c name.

The netdev is created in the writer's network namespace, and a write
requires CAP_NET_ADMIN there, as for a netdev created over rtnetlink.

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.

Signed-off-by: Kenny Hsieh <kenny_hsieh@xxxxxxxxx>
---
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, as
with rtnetlink, so there's no same-netns check anymore.
- Dropped the midev = NULL change, the double free is fixed separately:
https://lore.kernel.org/netdev/20261118090515.3390034-1-kenny_hsieh@xxxxxxxxx/
- Rewrote the ABI doc, rebased on current net-next.

v2: https://lore.kernel.org/netdev/20261015081341.1262695-1-kenny_hsieh@xxxxxxxxx/
v1: https://lore.kernel.org/netdev/20261002073156.759005-1-kenny_hsieh@xxxxxxxxx/

.../ABI/testing/sysfs-bus-i2c-devices-mctp | 24 ++++
MAINTAINERS | 1 +
drivers/net/mctp/mctp-i2c.c | 110 ++++++++++++++++--
3 files changed, 124 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..4ee96254f57a4
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp
@@ -0,0 +1,24 @@
+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@xxxxxxxxx>
+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.
+
+ 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@xxxxxxxxxxxxxxxxxxxx>
M: Matt Johnston <matt@xxxxxxxxxxxxxxxxxxxx>
L: netdev@xxxxxxxxxxxxxxx
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..61dd4a9e72545 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 (!ns_capable(current->nsproxy->net_ns->user_ns, 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