Skip to contentSkip to conversation

hwmon: (core) Use device name as a fallback in devm_hwmon_device_register_with_info

PATCH Superseded Heiner Kallweit 2 participants 4 messages Jan 9, 2025 Hardware monitoring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: (core) Use device name as a fallback in devm_hwmon_device_register_with_info

A number of network PHY drivers use the following code:

name = devm_hwmon_sanitize_name(dev, dev_name(dev)); if (IS_ERR(name))

	return PTR_ERR(name);

devm_hwmon_device_register_with_info(dev, name, ..);

Make this a generic fallback option and use the device name if no name is provided to devm_hwmon_device_register_with_info(). This would allow to simplify the affected drivers.

---
 drivers/hwmon/hwmon.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/hwmon/hwmon.c +5 -0
index 685d4ce8d..1fd3d94e1 100644
@@ -1170,6 +1170,11 @@ devm_hwmon_device_register_with_info(struct device *dev, const char *name,
 	if (!dev)
 		return ERR_PTR(-EINVAL);
 
+	if (!name)
+		name = devm_hwmon_sanitize_name(dev, dev_name(dev));
+	if (IS_ERR(name))
+		return ERR_CAST(name);
+
 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
 	if (!ptr)
 		return ERR_PTR(-ENOMEM);
Signature · 1 line
2.47.1
maintainer
#1 lore raw
Quoted 21 lines · Heiner Kallweit

A number of network PHY drivers use the following code:

name = devm_hwmon_sanitize_name(dev, dev_name(dev)); if (IS_ERR(name))

	return PTR_ERR(name);

devm_hwmon_device_register_with_info(dev, name, ..);

Make this a generic fallback option and use the device name if no name is provided to devm_hwmon_device_register_with_info(). This would allow to simplify the affected drivers.

---
 drivers/hwmon/hwmon.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/hwmon/hwmon.c +0 -0
index 685d4ce8d..1fd3d94e1 100644

drivers/hwmon/hwmon.c devm_hwmon_device_register_with_info(struct device *dev, const char *name, · line 1177

Quoted 8 lines
drivers/hwmon/hwmon.c +5 -0
@@ -1170,6 +1170,11 @@ devm_hwmon_device_register_with_info(struct device *dev, const char *name,
 	if (!dev)
 		return ERR_PTR(-EINVAL);
 
+	if (!name)
+		name = devm_hwmon_sanitize_name(dev, dev_name(dev));
+	if (IS_ERR(name))
+		return ERR_CAST(name);
+

That introduces an undiscussed change: It handles an ERR_PTR() passed as name parameter, not just NULL. Why would that be warranted ? It may not look like much, but it introduces an inconsistency: Other pointers (dev, chip) are not checked against ERR_PTR(). It is also not immediately obvious to me why the driver would want to check if the passed name is an ER_PTR().

Guenter

Quoted 6 lines
drivers/hwmon/hwmon.c +0 -0
 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
 	if (!ptr)
 		return ERR_PTR(-ENOMEM);
Signature · 1 line
2.47.1
#2 lore raw
Quoted 25 lines · Guenter Roeck
Quoted 24 lines · Heiner Kallweit

A number of network PHY drivers use the following code:

name = devm_hwmon_sanitize_name(dev, dev_name(dev)); if (IS_ERR(name))

	return PTR_ERR(name);

devm_hwmon_device_register_with_info(dev, name, ..);

Make this a generic fallback option and use the device name if no name is provided to devm_hwmon_device_register_with_info(). This would allow to simplify the affected drivers.

---
 drivers/hwmon/hwmon.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/hwmon/hwmon.c +0 -0
index 685d4ce8d..1fd3d94e1 100644
@@ -1170,6 +1170,11 @@ devm_hwmon_device_register_with_info(struct device *dev, const char *name,
 	if (!dev)
 		return ERR_PTR(-EINVAL);
 
Quoted 13 lines

drivers/hwmon/hwmon.c devm_hwmon_device_register_with_info(struct device *dev, const char *name, · line 1177

Quoted 5 lines
drivers/hwmon/hwmon.c +5 -0
+	if (!name)
+		name = devm_hwmon_sanitize_name(dev, dev_name(dev));
+	if (IS_ERR(name))
+		return ERR_CAST(name);
+

That introduces an undiscussed change: It handles an ERR_PTR() passed as name parameter, not just NULL. Why would that be warranted ? It may not look like much, but it introduces an inconsistency: Other pointers (dev, chip) are not checked against ERR_PTR(). It is also not immediately obvious to me why the driver would want to check if the passed name is an ER_PTR().

If a caller would pass an ERRPTR as name, we'd be in trouble in __hwmon_device_register(), when the following code is executed:
if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))

By doing the IS_ERR() check outside the if(!name) clause we get a little bit more of argument checking for free, at least with no code overhead.

If that's not wanted I can also move the check into the if(!name) clause.

Quoted 2 lines

Guenter

Heiner

maintainer
#3 lore raw
Quoted 39 lines · Heiner Kallweit
Quoted 25 lines · Guenter Roeck
Quoted 24 lines · Heiner Kallweit

A number of network PHY drivers use the following code:

name = devm_hwmon_sanitize_name(dev, dev_name(dev)); if (IS_ERR(name))

	return PTR_ERR(name);

devm_hwmon_device_register_with_info(dev, name, ..);

Make this a generic fallback option and use the device name if no name is provided to devm_hwmon_device_register_with_info(). This would allow to simplify the affected drivers.

---
 drivers/hwmon/hwmon.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/hwmon/hwmon.c +0 -0
index 685d4ce8d..1fd3d94e1 100644
@@ -1170,6 +1170,11 @@ devm_hwmon_device_register_with_info(struct device *dev, const char *name,
 	if (!dev)
 		return ERR_PTR(-EINVAL);
 
Quoted 13 lines

drivers/hwmon/hwmon.c devm_hwmon_device_register_with_info(struct device *dev, const char *name, · line 1177

Quoted 5 lines
drivers/hwmon/hwmon.c +5 -0
+	if (!name)
+		name = devm_hwmon_sanitize_name(dev, dev_name(dev));
+	if (IS_ERR(name))
+		return ERR_CAST(name);
+

That introduces an undiscussed change: It handles an ERR_PTR() passed as name parameter, not just NULL. Why would that be warranted ? It may not look like much, but it introduces an inconsistency: Other pointers (dev, chip) are not checked against ERR_PTR(). It is also not immediately obvious to me why the driver would want to check if the passed name is an ER_PTR().

Quoted 4 lines

If a caller would pass an ERRPTR as name, we'd be in trouble in __hwmon_device_register(), when the following code is executed:
if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))

Just like we are in trouble if the caller passes an ERR_PTR() as any of the other pointer arguments. I see that as a bug in the calling code.

Quoted 6 lines

By doing the IS_ERR() check outside the if(!name) clause we get a little bit more of argument checking for free, at least with no code overhead.

If that's not wanted I can also move the check into the if(!name) clause.

Please do.

Guenter