Skip to contentSkip to conversation

auxiliary: Automatically generate id

PATCH In review Sean Anderson 3 participants 9 messages Jul 28, 2025 – Jul 29, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 auxiliary: Automatically generate id

As it turns out, ids are not allowed to have semantic meaning. Their only purpose is to prevent sysfs collisions. To simplify things, just generate a unique id for each auxiliary device. Remove all references to filling in the id member of the device.

---

 drivers/base/auxiliary.c      | 32 +++++++++++++++++++++++---------
 include/linux/auxiliary_bus.h | 26 ++++++++------------------
 2 files changed, 31 insertions(+), 27 deletions(-)
drivers/base/auxiliary.c +23 -9
index dba7c8e13a53..f66067df03ad 100644
@@ -264,6 +264,8 @@ static const struct bus_type auxiliary_bus_type = {
 	.pm = &auxiliary_dev_pm_ops,
 };
 
+static DEFINE_IDA(auxiliary_id);
+
 /**
  * auxiliary_device_init - check auxiliary_device and initialize
  * @auxdev: auxiliary device struct
@@ -331,20 +333,37 @@ int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
 		return -EINVAL;
 	}
 
+	ret = ida_alloc(&auxiliary_id, GFP_KERNEL);
+	if (ret < 0) {
+		dev_err(dev, "auxiliary device id_alloc fauiled: %d\n", ret);
+		return ret;
+	}
+	auxdev->id = ret;
+
 	ret = dev_set_name(dev, "%s.%s.%d", modname, auxdev->name, auxdev->id);
 	if (ret) {
 		dev_err(dev, "auxiliary device dev_set_name failed: %d\n", ret);
+		ida_free(&auxiliary_id, auxdev->id);
 		return ret;
 	}
 
 	ret = device_add(dev);
-	if (ret)
+	if (ret) {
 		dev_err(dev, "adding auxiliary device failed!: %d\n", ret);
+		ida_free(&auxiliary_id, auxdev->id);
+	}
 
 	return ret;
 }
 EXPORT_SYMBOL_GPL(__auxiliary_device_add);
 
+void auxiliary_device_delete(struct auxiliary_device *auxdev)
+{
+	ida_free(&auxiliary_id, auxdev->id);
+	device_del(&auxdev->dev);
+}
+EXPORT_SYMBOL_GPL(auxiliary_device_delete);
+
 /**
  * __auxiliary_driver_register - register a driver for auxiliary bus devices
  * @auxdrv: auxiliary_driver structure
@@ -408,7 +427,6 @@ static void auxiliary_device_release(struct device *dev)
  * @modname: module name used to create the auxiliary driver name.
  * @devname: auxiliary bus device name
  * @platform_data: auxiliary bus device platform data
- * @id: auxiliary bus device id
  *
  * Helper to create an auxiliary bus device.
  * The device created matches driver 'modname.devname' on the auxiliary bus.
@@ -416,8 +434,7 @@ static void auxiliary_device_release(struct device *dev)
 struct auxiliary_device *auxiliary_device_create(struct device *dev,
 						 const char *modname,
 						 const char *devname,
-						 void *platform_data,
-						 int id)
+						 void *platform_data)
 {
 	struct auxiliary_device *auxdev;
 	int ret;
@@ -426,7 +443,6 @@ struct auxiliary_device *auxiliary_device_create(struct device *dev,
 	if (!auxdev)
 		return NULL;
 
-	auxdev->id = id;
 	auxdev->name = devname;
 	auxdev->dev.parent = dev;
 	auxdev->dev.platform_data = platform_data;
@@ -476,7 +492,6 @@ EXPORT_SYMBOL_GPL(auxiliary_device_destroy);
  * @modname: module name used to create the auxiliary driver name.
  * @devname: auxiliary bus device name
  * @platform_data: auxiliary bus device platform data
- * @id: auxiliary bus device id
  *
  * Device managed helper to create an auxiliary bus device.
  * The device created matches driver 'modname.devname' on the auxiliary bus.
@@ -484,13 +499,12 @@ EXPORT_SYMBOL_GPL(auxiliary_device_destroy);
 struct auxiliary_device *__devm_auxiliary_device_create(struct device *dev,
 							const char *modname,
 							const char *devname,
-							void *platform_data,
-							int id)
+							void *platform_data)
 {
 	struct auxiliary_device *auxdev;
 	int ret;
 
-	auxdev = auxiliary_device_create(dev, modname, devname, platform_data, id);
+	auxdev = auxiliary_device_create(dev, modname, devname, platform_data);
 	if (!auxdev)
 		return NULL;
 
include/linux/auxiliary_bus.h +8 -18
index 4086afd0cc6b..c972b5a3c2c4 100644
@@ -57,7 +57,7 @@
  *       The release and parent fields of the device structure must be filled
  *       in
  * @name: Match name found by the auxiliary device driver,
- * @id: unique identitier if multiple devices of the same name are exported,
+ * @id: automatically-generated unique identitier,
  * @sysfs: embedded struct which hold all sysfs related fields,
  * @sysfs.irqs: irqs xarray contains irq indices which are used by the device,
  * @sysfs.lock: Synchronize irq sysfs creation,
@@ -74,15 +74,11 @@
  * Registering an auxiliary_device is a three-step process.
  *
  * First, a 'struct auxiliary_device' needs to be defined or allocated for each
- * sub-device desired.  The name, id, dev.release, and dev.parent fields of
- * this structure must be filled in as follows.
+ * sub-device desired.  The name, dev.release, and dev.parent fields of this
+ * structure must be filled in as follows.
  *
  * The 'name' field is to be given a name that is recognized by the auxiliary
- * driver.  If two auxiliary_devices with the same match_name, eg
- * "foo_mod.foo_dev", are registered onto the bus, they must have unique id
- * values (e.g. "x" and "y") so that the registered devices names are
- * "foo_mod.foo_dev.x" and "foo_mod.foo_dev.y".  If match_name + id are not
- * unique, then the device_add fails and generates an error message.
+ * driver.
  *
  * The auxiliary_device.dev.type.release or auxiliary_device.dev.release must
  * be populated with a non-NULL pointer to successfully register the
@@ -113,7 +109,6 @@
  *
  *	// Step 1:
  *	my_aux_dev->name = MY_DEVICE_NAME;
- *	my_aux_dev->id = my_unique_id_alloc(xxx);
  *	my_aux_dev->dev.release = my_aux_dev_release;
  *	my_aux_dev->dev.parent = my_dev;
  *
@@ -242,10 +237,7 @@ static inline void auxiliary_device_uninit(struct auxiliary_device *auxdev)
 	put_device(&auxdev->dev);
 }
 
-static inline void auxiliary_device_delete(struct auxiliary_device *auxdev)
-{
-	device_del(&auxdev->dev);
-}
+void auxiliary_device_delete(struct auxiliary_device *auxdev);
 
 int __auxiliary_driver_register(struct auxiliary_driver *auxdrv, struct module *owner,
 				const char *modname);
@@ -257,19 +249,17 @@ void auxiliary_driver_unregister(struct auxiliary_driver *auxdrv);
 struct auxiliary_device *auxiliary_device_create(struct device *dev,
 						 const char *modname,
 						 const char *devname,
-						 void *platform_data,
-						 int id);
+						 void *platform_data);
 void auxiliary_device_destroy(void *auxdev);
 
 struct auxiliary_device *__devm_auxiliary_device_create(struct device *dev,
 							const char *modname,
 							const char *devname,
-							void *platform_data,
-							int id);
+							void *platform_data);
 
 #define devm_auxiliary_device_create(dev, devname, platform_data)     \
 	__devm_auxiliary_device_create(dev, KBUILD_MODNAME, devname,  \
-				       platform_data, 0)
+				       platform_data)
 
 /**
  * module_auxiliary_driver() - Helper macro for registering an auxiliary driver
Signature · 1 line
2.35.1.1320.gc452695387.dirty
maintainer
#1 lore raw
Quoted 17 lines · Sean Anderson

As it turns out, ids are not allowed to have semantic meaning. Their only purpose is to prevent sysfs collisions. To simplify things, just generate a unique id for each auxiliary device. Remove all references to filling in the id member of the device.

---

 drivers/base/auxiliary.c      | 32 +++++++++++++++++++++++---------
 include/linux/auxiliary_bus.h | 26 ++++++++------------------
 2 files changed, 31 insertions(+), 27 deletions(-)
drivers/base/auxiliary.c +0 -0
index dba7c8e13a53..f66067df03ad 100644

drivers/base/auxiliary.c static const struct bus_type auxiliary_bus_type = { · line 267

Quoted 4 lines
drivers/base/auxiliary.c +1 -0
@@ -264,6 +264,8 @@ static const struct bus_type auxiliary_bus_type = {
 	.pm = &auxiliary_dev_pm_ops,
 };
 
+static DEFINE_IDA(auxiliary_id);

I think this is the correct thing to do, even though the per device IDA drivers typically went for so far produces IDs that are easier to handle when debugging things.

Quoted 5 lines
drivers/base/auxiliary.c +1 -0
+
 /**
  * auxiliary_device_init - check auxiliary_device and initialize
  * @auxdev: auxiliary device struct

drivers/base/auxiliary.c int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) · line 341

Quoted 9 lines
drivers/base/auxiliary.c +6 -0
@@ -331,20 +333,37 @@ int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
 		return -EINVAL;
 	}
 
+	ret = ida_alloc(&auxiliary_id, GFP_KERNEL);
+	if (ret < 0) {
+		dev_err(dev, "auxiliary device id_alloc fauiled: %d\n", ret);
+		return ret;
+	}
+	auxdev->id = ret;

This overwrites the ID number set by various drivers that (still) use the auxiliary_device_init() and auxiliary_device_add() pair.

While I agree with the general intent, I think it's a very bad idea to just perform this change silently leaving drivers with their IDA instances not knowing that the set ID numbers do not have an effect anymore.

I think this should be multiple steps:

(1) Remove the id parameter and force an internal ID only for auxiliary_device_create().

(2) Convert applicable drivers (and the Rust abstraction) to use auxiliary_device_create() rather than auxiliary_device_init() and auxiliary_device_add().

(3) Treewide change to force an internal ID for all auxiliary devices considering this change in all affected drivers.

Quoted 12 lines
drivers/base/auxiliary.c +3 -1
+
 	ret = dev_set_name(dev, "%s.%s.%d", modname, auxdev->name, auxdev->id);
 	if (ret) {
 		dev_err(dev, "auxiliary device dev_set_name failed: %d\n", ret);
+		ida_free(&auxiliary_id, auxdev->id);
 		return ret;
 	}
 
 	ret = device_add(dev);
-	if (ret)
+	if (ret) {
 		dev_err(dev, "adding auxiliary device failed!: %d\n", ret);

drivers/base/auxiliary.c int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) · line 362

Quoted 10 lines
drivers/base/auxiliary.c +5 -0
+		ida_free(&auxiliary_id, auxdev->id);
+	}
 
 	return ret;
 }
 EXPORT_SYMBOL_GPL(__auxiliary_device_add);
 
+void auxiliary_device_delete(struct auxiliary_device *auxdev)
+{
+	ida_free(&auxiliary_id, auxdev->id);

Hm...I wonder if we should call this from the device's release callback instead.

The above ensures that we never have duplicate IDs in sysfs, but we can still end up with two auxiliary devices that have the same ID.

If we want the ID to be *always* unique, we need to introduce our own auxiliary device release callback, such that we have a hook for the auxiliary_device_init() and auxiliary_device_add() pair. Since so far drivers are controlling the underlying device's release callback.

Quoted 3 lines
drivers/base/auxiliary.c +3 -0
+	device_del(&auxdev->dev);
+}
+EXPORT_SYMBOL_GPL(auxiliary_device_delete);
#2 lore raw
Quoted 54 lines · Danilo Krummrich
Quoted 17 lines · Sean Anderson

As it turns out, ids are not allowed to have semantic meaning. Their only purpose is to prevent sysfs collisions. To simplify things, just generate a unique id for each auxiliary device. Remove all references to filling in the id member of the device.

---

 drivers/base/auxiliary.c      | 32 +++++++++++++++++++++++---------
 include/linux/auxiliary_bus.h | 26 ++++++++------------------
 2 files changed, 31 insertions(+), 27 deletions(-)
drivers/base/auxiliary.c +0 -0
index dba7c8e13a53..f66067df03ad 100644

drivers/base/auxiliary.c static const struct bus_type auxiliary_bus_type = { · line 267

Quoted 4 lines
drivers/base/auxiliary.c +1 -0
@@ -264,6 +264,8 @@ static const struct bus_type auxiliary_bus_type = {
 	.pm = &auxiliary_dev_pm_ops,
 };
 
+static DEFINE_IDA(auxiliary_id);

I think this is the correct thing to do, even though the per device IDA drivers typically went for so far produces IDs that are easier to handle when debugging things.

Quoted 5 lines
drivers/base/auxiliary.c +1 -0
+
 /**
  * auxiliary_device_init - check auxiliary_device and initialize
  * @auxdev: auxiliary device struct

drivers/base/auxiliary.c int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) · line 341

Quoted 9 lines
drivers/base/auxiliary.c +6 -0
@@ -331,20 +333,37 @@ int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
 		return -EINVAL;
 	}
 
+	ret = ida_alloc(&auxiliary_id, GFP_KERNEL);
+	if (ret < 0) {
+		dev_err(dev, "auxiliary device id_alloc fauiled: %d\n", ret);
+		return ret;
+	}
+	auxdev->id = ret;

This overwrites the ID number set by various drivers that (still) use the auxiliary_device_init() and auxiliary_device_add() pair.

While I agree with the general intent, I think it's a very bad idea to just perform this change silently leaving drivers with their IDA instances not knowing that the set ID numbers do not have an effect anymore.

I think this should be multiple steps:

(1) Remove the id parameter and force an internal ID only for auxiliary_device_create().

Quoted 6 lines

(2) Convert applicable drivers (and the Rust abstraction) to use auxiliary_device_create() rather than auxiliary_device_init() and auxiliary_device_add().

(3) Treewide change to force an internal ID for all auxiliary devices considering this change in all affected drivers.

I would suggest easier approach.
1. Add to the proposal patch, the sed generated line which removes auxdev->id assignment in the drivers.
Something like this from mlx5:
- sf_dev->adev.id = id;

2. Add standalone patches to remove not used ida_alloc/ida_free calls from the drivers.

Quoted 20 lines
Quoted 20 lines
drivers/base/auxiliary.c +5 -1
+
 	ret = dev_set_name(dev, "%s.%s.%d", modname, auxdev->name, auxdev->id);
 	if (ret) {
 		dev_err(dev, "auxiliary device dev_set_name failed: %d\n", ret);
+		ida_free(&auxiliary_id, auxdev->id);
 		return ret;
 	}
 
 	ret = device_add(dev);
-	if (ret)
+	if (ret) {
 		dev_err(dev, "adding auxiliary device failed!: %d\n", ret);
+		ida_free(&auxiliary_id, auxdev->id);
+	}
 
 	return ret;
 }
 EXPORT_SYMBOL_GPL(__auxiliary_device_add);
 
Quoted 5 lines

drivers/base/auxiliary.c int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) · line 362

Quoted 3 lines
drivers/base/auxiliary.c +3 -0
+void auxiliary_device_delete(struct auxiliary_device *auxdev)
+{
+	ida_free(&auxiliary_id, auxdev->id);

Hm...I wonder if we should call this from the device's release callback instead.

Yes, you are right.

Thanks

maintainer
#3 lore raw
Quoted 62 lines · Leon Romanovsky
Quoted 54 lines · Danilo Krummrich
Quoted 17 lines · Sean Anderson

As it turns out, ids are not allowed to have semantic meaning. Their only purpose is to prevent sysfs collisions. To simplify things, just generate a unique id for each auxiliary device. Remove all references to filling in the id member of the device.

---

 drivers/base/auxiliary.c      | 32 +++++++++++++++++++++++---------
 include/linux/auxiliary_bus.h | 26 ++++++++------------------
 2 files changed, 31 insertions(+), 27 deletions(-)
drivers/base/auxiliary.c +0 -0
index dba7c8e13a53..f66067df03ad 100644

drivers/base/auxiliary.c static const struct bus_type auxiliary_bus_type = { · line 267

Quoted 4 lines
drivers/base/auxiliary.c +1 -0
@@ -264,6 +264,8 @@ static const struct bus_type auxiliary_bus_type = {
 	.pm = &auxiliary_dev_pm_ops,
 };
 
+static DEFINE_IDA(auxiliary_id);

I think this is the correct thing to do, even though the per device IDA drivers typically went for so far produces IDs that are easier to handle when debugging things.

Quoted 5 lines
drivers/base/auxiliary.c +1 -0
+
 /**
  * auxiliary_device_init - check auxiliary_device and initialize
  * @auxdev: auxiliary device struct

drivers/base/auxiliary.c int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) · line 341

Quoted 9 lines
drivers/base/auxiliary.c +6 -0
@@ -331,20 +333,37 @@ int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
 		return -EINVAL;
 	}
 
+	ret = ida_alloc(&auxiliary_id, GFP_KERNEL);
+	if (ret < 0) {
+		dev_err(dev, "auxiliary device id_alloc fauiled: %d\n", ret);
+		return ret;
+	}
+	auxdev->id = ret;

This overwrites the ID number set by various drivers that (still) use the auxiliary_device_init() and auxiliary_device_add() pair.

While I agree with the general intent, I think it's a very bad idea to just perform this change silently leaving drivers with their IDA instances not knowing that the set ID numbers do not have an effect anymore.

I think this should be multiple steps:

(1) Remove the id parameter and force an internal ID only for auxiliary_device_create().

Quoted 6 lines

(2) Convert applicable drivers (and the Rust abstraction) to use auxiliary_device_create() rather than auxiliary_device_init() and auxiliary_device_add().

(3) Treewide change to force an internal ID for all auxiliary devices considering this change in all affected drivers.

Quoted 8 lines

I would suggest easier approach.
1. Add to the proposal patch, the sed generated line which removes auxdev->id assignment in the drivers.
Something like this from mlx5:
- sf_dev->adev.id = id;

2. Add standalone patches to remove not used ida_alloc/ida_free calls from the drivers.

I assume you suggest this as an alternative to (3) above? If so, that's what I meant in (3), I should have written "treewide series" instead of "treewide change".

Technically (2) is orthogonal, yet I think it's a bit better to do the desired change right away. Otherwise we end up converting all applicable drivers to implement the auxiliary device release callback (which we need for a common ida_free()) first, just to remove it later on when we convert to auxiliary_device_create().

#4 lore raw
Quoted 69 lines · Danilo Krummrich
Quoted 68 lines · Leon Romanovsky
Quoted 54 lines · Danilo Krummrich
Quoted 17 lines · Sean Anderson

As it turns out, ids are not allowed to have semantic meaning. Their only purpose is to prevent sysfs collisions. To simplify things, just generate a unique id for each auxiliary device. Remove all references to filling in the id member of the device.

---

 drivers/base/auxiliary.c      | 32 +++++++++++++++++++++++---------
 include/linux/auxiliary_bus.h | 26 ++++++++------------------
 2 files changed, 31 insertions(+), 27 deletions(-)
drivers/base/auxiliary.c +0 -0
index dba7c8e13a53..f66067df03ad 100644

drivers/base/auxiliary.c static const struct bus_type auxiliary_bus_type = { · line 267

Quoted 4 lines
drivers/base/auxiliary.c +1 -0
@@ -264,6 +264,8 @@ static const struct bus_type auxiliary_bus_type = {
 	.pm = &auxiliary_dev_pm_ops,
 };
 
+static DEFINE_IDA(auxiliary_id);

I think this is the correct thing to do, even though the per device IDA drivers typically went for so far produces IDs that are easier to handle when debugging things.

Quoted 5 lines
drivers/base/auxiliary.c +1 -0
+
 /**
  * auxiliary_device_init - check auxiliary_device and initialize
  * @auxdev: auxiliary device struct

drivers/base/auxiliary.c int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) · line 341

Quoted 9 lines
drivers/base/auxiliary.c +6 -0
@@ -331,20 +333,37 @@ int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
 		return -EINVAL;
 	}
 
+	ret = ida_alloc(&auxiliary_id, GFP_KERNEL);
+	if (ret < 0) {
+		dev_err(dev, "auxiliary device id_alloc fauiled: %d\n", ret);
+		return ret;
+	}
+	auxdev->id = ret;

This overwrites the ID number set by various drivers that (still) use the auxiliary_device_init() and auxiliary_device_add() pair.

While I agree with the general intent, I think it's a very bad idea to just perform this change silently leaving drivers with their IDA instances not knowing that the set ID numbers do not have an effect anymore.

I think this should be multiple steps:

(1) Remove the id parameter and force an internal ID only for auxiliary_device_create().

Quoted 6 lines

(2) Convert applicable drivers (and the Rust abstraction) to use auxiliary_device_create() rather than auxiliary_device_init() and auxiliary_device_add().

(3) Treewide change to force an internal ID for all auxiliary devices considering this change in all affected drivers.

I would suggest easier approach.
1. Add to the proposal patch, the sed generated line which removes auxdev->id assignment in the drivers.
Something like this from mlx5:
- sf_dev->adev.id = id;

Quoted 6 lines
Quoted 2 lines

2. Add standalone patches to remove not used ida_alloc/ida_free calls from the drivers.

I assume you suggest this as an alternative to (3) above? If so, that's what I meant in (3), I should have written "treewide series" instead of "treewide change".

I would say for all steps. Very important reason to use auxiliary_device_init() and not auxiliary_device_create() is to bind custom release callback, which is needed to release private data.

In addition, complex devices embed struct auxiliary_device in their internal struct to rely on container_of to access the data.
See mlx5_sf_dev_add() in drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c as an example.

Quoted 6 lines

Technically (2) is orthogonal, yet I think it's a bit better to do the desired change right away. Otherwise we end up converting all applicable drivers to implement the auxiliary device release callback (which we need for a common ida_free()) first, just to remove it later on when we convert to auxiliary_device_create().

My expectation is to see extension of driver/base/core.c. Something like that:

drivers/base/core.c +3 -1
index cbc0099d8ef24..63847c84dbdc0 100644
@@ -2560,8 +2560,10 @@ static void device_release(struct kobject *kobj)
 
        kfree(dev->dma_range_map);
 
-       if (dev->release)
+       if (dev->release) {
+               dev->bus_specific_cleanup(dev);
                dev->release(dev);
+       }
        else if (dev->type && dev->type->release)
                dev->type->release(dev);
        else if (dev->class && dev->class->dev_release)

(END)

maintainer
#5 lore raw
Quoted 77 lines · Leon Romanovsky
Quoted 69 lines · Danilo Krummrich
Quoted 68 lines · Leon Romanovsky
Quoted 54 lines · Danilo Krummrich
Quoted 17 lines · Sean Anderson

As it turns out, ids are not allowed to have semantic meaning. Their only purpose is to prevent sysfs collisions. To simplify things, just generate a unique id for each auxiliary device. Remove all references to filling in the id member of the device.

---

 drivers/base/auxiliary.c      | 32 +++++++++++++++++++++++---------
 include/linux/auxiliary_bus.h | 26 ++++++++------------------
 2 files changed, 31 insertions(+), 27 deletions(-)
drivers/base/auxiliary.c +0 -0
index dba7c8e13a53..f66067df03ad 100644

drivers/base/auxiliary.c static const struct bus_type auxiliary_bus_type = { · line 267

Quoted 4 lines
drivers/base/auxiliary.c +1 -0
@@ -264,6 +264,8 @@ static const struct bus_type auxiliary_bus_type = {
 	.pm = &auxiliary_dev_pm_ops,
 };
 
+static DEFINE_IDA(auxiliary_id);

I think this is the correct thing to do, even though the per device IDA drivers typically went for so far produces IDs that are easier to handle when debugging things.

Quoted 5 lines
drivers/base/auxiliary.c +1 -0
+
 /**
  * auxiliary_device_init - check auxiliary_device and initialize
  * @auxdev: auxiliary device struct

drivers/base/auxiliary.c int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) · line 341

Quoted 9 lines
drivers/base/auxiliary.c +6 -0
@@ -331,20 +333,37 @@ int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
 		return -EINVAL;
 	}
 
+	ret = ida_alloc(&auxiliary_id, GFP_KERNEL);
+	if (ret < 0) {
+		dev_err(dev, "auxiliary device id_alloc fauiled: %d\n", ret);
+		return ret;
+	}
+	auxdev->id = ret;

This overwrites the ID number set by various drivers that (still) use the auxiliary_device_init() and auxiliary_device_add() pair.

While I agree with the general intent, I think it's a very bad idea to just perform this change silently leaving drivers with their IDA instances not knowing that the set ID numbers do not have an effect anymore.

I think this should be multiple steps:

(1) Remove the id parameter and force an internal ID only for auxiliary_device_create().

Quoted 6 lines

(2) Convert applicable drivers (and the Rust abstraction) to use auxiliary_device_create() rather than auxiliary_device_init() and auxiliary_device_add().

(3) Treewide change to force an internal ID for all auxiliary devices considering this change in all affected drivers.

I would suggest easier approach.
1. Add to the proposal patch, the sed generated line which removes auxdev->id assignment in the drivers.
Something like this from mlx5:
- sf_dev->adev.id = id;

Quoted 6 lines
Quoted 2 lines

2. Add standalone patches to remove not used ida_alloc/ida_free calls from the drivers.

I assume you suggest this as an alternative to (3) above? If so, that's what I meant in (3), I should have written "treewide series" instead of "treewide change".

Quoted 8 lines

I would say for all steps. Very important reason to use auxiliary_device_init() and not auxiliary_device_create() is to bind custom release callback, which is needed to release private data.

In addition, complex devices embed struct auxiliary_device in their internal struct to rely on container_of to access the data.
See mlx5_sf_dev_add() in drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c as an example.

That's why I said "*applicable* drivers" everywhere. :)

The examples you mention don't fall under this category, but in general drivers that *can* use auxiliary_device_create() should do it.

Quoted 14 lines
Quoted 5 lines

Technically (2) is orthogonal, yet I think it's a bit better to do the desired change right away. Otherwise we end up converting all applicable drivers to implement the auxiliary device release callback (which we need for a common ida_free()) first, just to remove it later on when we convert to auxiliary_device_create().

My expectation is to see extension of driver/base/core.c. Something like that:

drivers/base/core.c +0 -0
index cbc0099d8ef24..63847c84dbdc0 100644
@@ -2560,8 +2560,10 @@ static void device_release(struct kobject *kobj)
 

drivers/base/core.c static void device_release(struct kobject *kobj) · line 2569

Quoted 10 lines
drivers/base/core.c +3 -1
        kfree(dev->dma_range_map);
 
-       if (dev->release)
+       if (dev->release) {
+               dev->bus_specific_cleanup(dev);
                dev->release(dev);
+       }
        else if (dev->type && dev->type->release)
                dev->type->release(dev);
        else if (dev->class && dev->class->dev_release)

The common pattern is to have custom release callbacks for class or bus specific device types.

In this case drivers would set struct auxiliary_device::release. And the auxiliary bus would implement the underlying struct device::release to call the driver provided struct auxiliary_device::release plus the additional cleanup.

What you propose works as well, but it moves bus or class device specifics into the generic struct device, where the normal inheritance pattern already solves this.

#6 lore raw
Quoted 87 lines · Danilo Krummrich
Quoted 77 lines · Leon Romanovsky
Quoted 69 lines · Danilo Krummrich
Quoted 68 lines · Leon Romanovsky
Quoted 54 lines · Danilo Krummrich
Quoted 17 lines · Sean Anderson

As it turns out, ids are not allowed to have semantic meaning. Their only purpose is to prevent sysfs collisions. To simplify things, just generate a unique id for each auxiliary device. Remove all references to filling in the id member of the device.

---

 drivers/base/auxiliary.c      | 32 +++++++++++++++++++++++---------
 include/linux/auxiliary_bus.h | 26 ++++++++------------------
 2 files changed, 31 insertions(+), 27 deletions(-)
drivers/base/auxiliary.c +0 -0
index dba7c8e13a53..f66067df03ad 100644

drivers/base/auxiliary.c static const struct bus_type auxiliary_bus_type = { · line 267

Quoted 4 lines
drivers/base/auxiliary.c +1 -0
@@ -264,6 +264,8 @@ static const struct bus_type auxiliary_bus_type = {
 	.pm = &auxiliary_dev_pm_ops,
 };
 
+static DEFINE_IDA(auxiliary_id);

I think this is the correct thing to do, even though the per device IDA drivers typically went for so far produces IDs that are easier to handle when debugging things.

Quoted 5 lines
drivers/base/auxiliary.c +1 -0
+
 /**
  * auxiliary_device_init - check auxiliary_device and initialize
  * @auxdev: auxiliary device struct

drivers/base/auxiliary.c int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) · line 341

Quoted 9 lines
drivers/base/auxiliary.c +6 -0
@@ -331,20 +333,37 @@ int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
 		return -EINVAL;
 	}
 
+	ret = ida_alloc(&auxiliary_id, GFP_KERNEL);
+	if (ret < 0) {
+		dev_err(dev, "auxiliary device id_alloc fauiled: %d\n", ret);
+		return ret;
+	}
+	auxdev->id = ret;

This overwrites the ID number set by various drivers that (still) use the auxiliary_device_init() and auxiliary_device_add() pair.

While I agree with the general intent, I think it's a very bad idea to just perform this change silently leaving drivers with their IDA instances not knowing that the set ID numbers do not have an effect anymore.

I think this should be multiple steps:

(1) Remove the id parameter and force an internal ID only for auxiliary_device_create().

Quoted 6 lines

(2) Convert applicable drivers (and the Rust abstraction) to use auxiliary_device_create() rather than auxiliary_device_init() and auxiliary_device_add().

(3) Treewide change to force an internal ID for all auxiliary devices considering this change in all affected drivers.

I would suggest easier approach.
1. Add to the proposal patch, the sed generated line which removes auxdev->id assignment in the drivers.
Something like this from mlx5:
- sf_dev->adev.id = id;

Quoted 6 lines
Quoted 2 lines

2. Add standalone patches to remove not used ida_alloc/ida_free calls from the drivers.

I assume you suggest this as an alternative to (3) above? If so, that's what I meant in (3), I should have written "treewide series" instead of "treewide change".

Quoted 8 lines

I would say for all steps. Very important reason to use auxiliary_device_init() and not auxiliary_device_create() is to bind custom release callback, which is needed to release private data.

In addition, complex devices embed struct auxiliary_device in their internal struct to rely on container_of to access the data.
See mlx5_sf_dev_add() in drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c as an example.

Quoted 4 lines

That's why I said "*applicable* drivers" everywhere. :)

The examples you mention don't fall under this category, but in general drivers that *can* use auxiliary_device_create() should do it.

Of course, the thing is that even drivers with auxiliary_device_init() shouldn't set "id" and because they need to be updated.

The auxiliary_device_create() relies on auxiliary_device_init() under the hood, so most likely the change should be there.

Quoted 29 lines
Quoted 15 lines
Quoted 6 lines

Technically (2) is orthogonal, yet I think it's a bit better to do the desired change right away. Otherwise we end up converting all applicable drivers to implement the auxiliary device release callback (which we need for a common ida_free()) first, just to remove it later on when we convert to auxiliary_device_create().

My expectation is to see extension of driver/base/core.c. Something like that:

drivers/base/core.c +0 -0
index cbc0099d8ef24..63847c84dbdc0 100644
@@ -2560,8 +2560,10 @@ static void device_release(struct kobject *kobj)
 

drivers/base/core.c static void device_release(struct kobject *kobj) · line 2569

Quoted 10 lines
drivers/base/core.c +3 -1
        kfree(dev->dma_range_map);
 
-       if (dev->release)
+       if (dev->release) {
+               dev->bus_specific_cleanup(dev);
                dev->release(dev);
+       }
        else if (dev->type && dev->type->release)
                dev->type->release(dev);
        else if (dev->class && dev->class->dev_release)

The common pattern is to have custom release callbacks for class or bus specific device types.

Quoted 7 lines

In this case drivers would set struct auxiliary_device::release. And the auxiliary bus would implement the underlying struct device::release to call the driver provided struct auxiliary_device::release plus the additional cleanup.

What you propose works as well, but it moves bus or class device specifics into the generic struct device, where the normal inheritance pattern already solves this.

It was just a sketch, everything that allows to set custom release callback is fine by me.

Thanks

maintainer
#7 lore raw
Quoted 88 lines · Leon Romanovsky
Quoted 87 lines · Danilo Krummrich
Quoted 85 lines · Leon Romanovsky
Quoted 69 lines · Danilo Krummrich
Quoted 68 lines · Leon Romanovsky
Quoted 54 lines · Danilo Krummrich
Quoted 17 lines · Sean Anderson

As it turns out, ids are not allowed to have semantic meaning. Their only purpose is to prevent sysfs collisions. To simplify things, just generate a unique id for each auxiliary device. Remove all references to filling in the id member of the device.

---

 drivers/base/auxiliary.c      | 32 +++++++++++++++++++++++---------
 include/linux/auxiliary_bus.h | 26 ++++++++------------------
 2 files changed, 31 insertions(+), 27 deletions(-)
drivers/base/auxiliary.c +0 -0
index dba7c8e13a53..f66067df03ad 100644

drivers/base/auxiliary.c static const struct bus_type auxiliary_bus_type = { · line 267

Quoted 4 lines
drivers/base/auxiliary.c +1 -0
@@ -264,6 +264,8 @@ static const struct bus_type auxiliary_bus_type = {
 	.pm = &auxiliary_dev_pm_ops,
 };
 
+static DEFINE_IDA(auxiliary_id);

I think this is the correct thing to do, even though the per device IDA drivers typically went for so far produces IDs that are easier to handle when debugging things.

Quoted 5 lines
drivers/base/auxiliary.c +1 -0
+
 /**
  * auxiliary_device_init - check auxiliary_device and initialize
  * @auxdev: auxiliary device struct

drivers/base/auxiliary.c int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) · line 341

Quoted 9 lines
drivers/base/auxiliary.c +6 -0
@@ -331,20 +333,37 @@ int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
 		return -EINVAL;
 	}
 
+	ret = ida_alloc(&auxiliary_id, GFP_KERNEL);
+	if (ret < 0) {
+		dev_err(dev, "auxiliary device id_alloc fauiled: %d\n", ret);
+		return ret;
+	}
+	auxdev->id = ret;

This overwrites the ID number set by various drivers that (still) use the auxiliary_device_init() and auxiliary_device_add() pair.

While I agree with the general intent, I think it's a very bad idea to just perform this change silently leaving drivers with their IDA instances not knowing that the set ID numbers do not have an effect anymore.

I think this should be multiple steps:

(1) Remove the id parameter and force an internal ID only for auxiliary_device_create().

Quoted 6 lines

(2) Convert applicable drivers (and the Rust abstraction) to use auxiliary_device_create() rather than auxiliary_device_init() and auxiliary_device_add().

(3) Treewide change to force an internal ID for all auxiliary devices considering this change in all affected drivers.

I would suggest easier approach.
1. Add to the proposal patch, the sed generated line which removes auxdev->id assignment in the drivers.
Something like this from mlx5:
- sf_dev->adev.id = id;

Quoted 6 lines
Quoted 2 lines

2. Add standalone patches to remove not used ida_alloc/ida_free calls from the drivers.

I assume you suggest this as an alternative to (3) above? If so, that's what I meant in (3), I should have written "treewide series" instead of "treewide change".

I would say for all steps. Very important reason to use auxiliary_device_init() and not auxiliary_device_create() is to bind custom release callback, which is needed to release private data.

In addition, complex devices embed struct auxiliary_device in their internal struct to rely on container_of to access the data.
See mlx5_sf_dev_add() in drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c as an example.

Quoted 7 lines
Quoted 4 lines

That's why I said "*applicable* drivers" everywhere. :)

The examples you mention don't fall under this category, but in general drivers that *can* use auxiliary_device_create() should do it.

Of course, the thing is that even drivers with auxiliary_device_init() shouldn't set "id" and because they need to be updated.

I think we agree here. :) It's only about the ordering between "convert to auxiliary_device_create()" and "use internal IDA".

I think it'd be a good synergy to convert applicable drivers to auxiliary_device_create() first, but I'm not insisting on it.

Quoted 36 lines

The auxiliary_device_create() relies on auxiliary_device_init() under the hood, so most likely the change should be there.

Quoted 33 lines
Quoted 15 lines
Quoted 6 lines

Technically (2) is orthogonal, yet I think it's a bit better to do the desired change right away. Otherwise we end up converting all applicable drivers to implement the auxiliary device release callback (which we need for a common ida_free()) first, just to remove it later on when we convert to auxiliary_device_create().

My expectation is to see extension of driver/base/core.c. Something like that:

drivers/base/core.c +0 -0
index cbc0099d8ef24..63847c84dbdc0 100644
@@ -2560,8 +2560,10 @@ static void device_release(struct kobject *kobj)
 

drivers/base/core.c static void device_release(struct kobject *kobj) · line 2569

Quoted 10 lines
drivers/base/core.c +3 -1
        kfree(dev->dma_range_map);
 
-       if (dev->release)
+       if (dev->release) {
+               dev->bus_specific_cleanup(dev);
                dev->release(dev);
+       }
        else if (dev->type && dev->type->release)
                dev->type->release(dev);
        else if (dev->class && dev->class->dev_release)

The common pattern is to have custom release callbacks for class or bus specific device types.

In this case drivers would set struct auxiliary_device::release. And the auxiliary bus would implement the underlying struct device::release to call the driver provided struct auxiliary_device::release plus the additional cleanup.

Quoted 6 lines
Quoted 3 lines

What you propose works as well, but it moves bus or class device specifics into the generic struct device, where the normal inheritance pattern already solves this.

It was just a sketch, everything that allows to set custom release callback is fine by me.

Yeah, what I meant is that we shouldn't add an additional release callback to struct device for such things in general.

device::release() is for the user of the struct device, in this case this is struct auxiliary_device.

auxiliary_device::release() will be for the user of struct auxiliary_device, which could be some generic driver specific device structure, let's say struct foo_device.

Now, struct foo_device may have another release callback for its specific cleanup.

So, the callchain would look like this:

	device::release {
		auxiliary_device::release {
			foo_device::release {
				// clean up struct foo_device
			}

			// clean up struct auxiliary_device
		}

		// clean up struct device
	}

Having additional release callbacks on struct device does not scale.

#8 lore raw
Quoted 173 lines · Danilo Krummrich
Quoted 88 lines · Leon Romanovsky
Quoted 87 lines · Danilo Krummrich
Quoted 85 lines · Leon Romanovsky
Quoted 69 lines · Danilo Krummrich
Quoted 68 lines · Leon Romanovsky
Quoted 54 lines · Danilo Krummrich
Quoted 17 lines · Sean Anderson

As it turns out, ids are not allowed to have semantic meaning. Their only purpose is to prevent sysfs collisions. To simplify things, just generate a unique id for each auxiliary device. Remove all references to filling in the id member of the device.

---

 drivers/base/auxiliary.c      | 32 +++++++++++++++++++++++---------
 include/linux/auxiliary_bus.h | 26 ++++++++------------------
 2 files changed, 31 insertions(+), 27 deletions(-)
drivers/base/auxiliary.c +0 -0
index dba7c8e13a53..f66067df03ad 100644

drivers/base/auxiliary.c static const struct bus_type auxiliary_bus_type = { · line 267

Quoted 4 lines
drivers/base/auxiliary.c +1 -0
@@ -264,6 +264,8 @@ static const struct bus_type auxiliary_bus_type = {
 	.pm = &auxiliary_dev_pm_ops,
 };
 
+static DEFINE_IDA(auxiliary_id);

I think this is the correct thing to do, even though the per device IDA drivers typically went for so far produces IDs that are easier to handle when debugging things.

Quoted 5 lines
drivers/base/auxiliary.c +1 -0
+
 /**
  * auxiliary_device_init - check auxiliary_device and initialize
  * @auxdev: auxiliary device struct

drivers/base/auxiliary.c int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) · line 341

Quoted 9 lines
drivers/base/auxiliary.c +6 -0
@@ -331,20 +333,37 @@ int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
 		return -EINVAL;
 	}
 
+	ret = ida_alloc(&auxiliary_id, GFP_KERNEL);
+	if (ret < 0) {
+		dev_err(dev, "auxiliary device id_alloc fauiled: %d\n", ret);
+		return ret;
+	}
+	auxdev->id = ret;

This overwrites the ID number set by various drivers that (still) use the auxiliary_device_init() and auxiliary_device_add() pair.

While I agree with the general intent, I think it's a very bad idea to just perform this change silently leaving drivers with their IDA instances not knowing that the set ID numbers do not have an effect anymore.

I think this should be multiple steps:

(1) Remove the id parameter and force an internal ID only for auxiliary_device_create().

Quoted 6 lines

(2) Convert applicable drivers (and the Rust abstraction) to use auxiliary_device_create() rather than auxiliary_device_init() and auxiliary_device_add().

(3) Treewide change to force an internal ID for all auxiliary devices considering this change in all affected drivers.

I would suggest easier approach.
1. Add to the proposal patch, the sed generated line which removes auxdev->id assignment in the drivers.
Something like this from mlx5:
- sf_dev->adev.id = id;

Quoted 6 lines
Quoted 2 lines

2. Add standalone patches to remove not used ida_alloc/ida_free calls from the drivers.

I assume you suggest this as an alternative to (3) above? If so, that's what I meant in (3), I should have written "treewide series" instead of "treewide change".

I would say for all steps. Very important reason to use auxiliary_device_init() and not auxiliary_device_create() is to bind custom release callback, which is needed to release private data.

In addition, complex devices embed struct auxiliary_device in their internal struct to rely on container_of to access the data.
See mlx5_sf_dev_add() in drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c as an example.

Quoted 7 lines
Quoted 4 lines

That's why I said "*applicable* drivers" everywhere. :)

The examples you mention don't fall under this category, but in general drivers that *can* use auxiliary_device_create() should do it.

Of course, the thing is that even drivers with auxiliary_device_init() shouldn't set "id" and because they need to be updated.

I think we agree here. :) It's only about the ordering between "convert to auxiliary_device_create()" and "use internal IDA".

I think it'd be a good synergy to convert applicable drivers to auxiliary_device_create() first, but I'm not insisting on it.

Quoted 36 lines

The auxiliary_device_create() relies on auxiliary_device_init() under the hood, so most likely the change should be there.

Quoted 33 lines
Quoted 15 lines
Quoted 6 lines

Technically (2) is orthogonal, yet I think it's a bit better to do the desired change right away. Otherwise we end up converting all applicable drivers to implement the auxiliary device release callback (which we need for a common ida_free()) first, just to remove it later on when we convert to auxiliary_device_create().

My expectation is to see extension of driver/base/core.c. Something like that:

drivers/base/core.c +0 -0
index cbc0099d8ef24..63847c84dbdc0 100644
@@ -2560,8 +2560,10 @@ static void device_release(struct kobject *kobj)
 

drivers/base/core.c static void device_release(struct kobject *kobj) · line 2569

Quoted 10 lines
drivers/base/core.c +3 -1
        kfree(dev->dma_range_map);
 
-       if (dev->release)
+       if (dev->release) {
+               dev->bus_specific_cleanup(dev);
                dev->release(dev);
+       }
        else if (dev->type && dev->type->release)
                dev->type->release(dev);
        else if (dev->class && dev->class->dev_release)

The common pattern is to have custom release callbacks for class or bus specific device types.

In this case drivers would set struct auxiliary_device::release. And the auxiliary bus would implement the underlying struct device::release to call the driver provided struct auxiliary_device::release plus the additional cleanup.

Quoted 6 lines
Quoted 3 lines

What you propose works as well, but it moves bus or class device specifics into the generic struct device, where the normal inheritance pattern already solves this.

It was just a sketch, everything that allows to set custom release callback is fine by me.

Yeah, what I meant is that we shouldn't add an additional release callback to struct device for such things in general.

device::release() is for the user of the struct device, in this case this is struct auxiliary_device.

auxiliary_device::release() will be for the user of struct auxiliary_device, which could be some generic driver specific device structure, let's say struct foo_device.

Now, struct foo_device may have another release callback for its specific cleanup.

So, the callchain would look like this:

	device::release {
		auxiliary_device::release {
			foo_device::release {
				// clean up struct foo_device
			}

			// clean up struct auxiliary_device
		}

		// clean up struct device
	}
Quoted 1 line

Having additional release callbacks on struct device does not scale.

Yes, we both agree that more work on this "auxiliary: Automatically generate id" patch is needed. :)

Thanks