Skip to contentSkip to conversation

hwmon: Remove checks for validity of dev

PATCH In review Muhammad Usama Anjum 2 participants 3 messages Jan 28, 2022

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

PATCH 1/1 hwmon: Remove checks for validity of dev

dev is being dereferenced in device_property_present() which means that it is valid. Don't check its validity again and simplify the code.

---
 drivers/hwmon/hwmon.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/hwmon.c +2 -2
index e36ea82da1474..aec32abd0a89f 100644
@@ -822,7 +822,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
 	hwdev->name = name;
 	hdev->class = &hwmon_class;
 	hdev->parent = dev;
-	hdev->of_node = dev ? dev->of_node : NULL;
+	hdev->of_node = dev->of_node;
 	hwdev->chip = chip;
 	dev_set_drvdata(hdev, drvdata);
 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
@@ -834,7 +834,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
 
 	INIT_LIST_HEAD(&hwdev->tzdata);
 
-	if (dev && dev->of_node && chip && chip->ops->read &&
+	if (dev->of_node && chip && chip->ops->read &&
 	    chip->info[0]->type == hwmon_chip &&
 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
 		err = hwmon_thermal_register_sensors(hdev);
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 22 lines · Muhammad Usama Anjum

dev is being dereferenced in device_property_present() which means that it is valid. Don't check its validity again and simplify the code.

---
  drivers/hwmon/hwmon.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/hwmon.c +1 -1
index e36ea82da1474..aec32abd0a89f 100644
@@ -822,7 +822,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
  	hwdev->name = name;
  	hdev->class = &hwmon_class;
  	hdev->parent = dev;
-	hdev->of_node = dev ? dev->of_node : NULL;
+	hdev->of_node = dev->of_node;
  	hwdev->chip = chip;
  	dev_set_drvdata(hdev, drvdata);
  	dev_set_name(hdev, HWMON_ID_FORMAT, id);

drivers/hwmon/hwmon.c __hwmon_device_register(struct device *dev, const char *name, void *drvdata, · line 840

Quoted 8 lines
drivers/hwmon/hwmon.c +1 -1
@@ -834,7 +834,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
 
  	INIT_LIST_HEAD(&hwdev->tzdata);
 
-	if (dev && dev->of_node && chip && chip->ops->read &&
+	if (dev->of_node && chip && chip->ops->read &&
  	    chip->info[0]->type == hwmon_chip &&
  	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
  		err = hwmon_thermal_register_sensors(hdev);

Wrong fix, sorry. While I would love to make dev mandatory, the function is called with dev == NULL from at least one place, and the check is (still) needed. Even if/when it is removed we would have to add an early check and return -EINVAL if it is NULL.

Guenter

#2 lore raw
Quoted 29 lines · Guenter Roeck
Quoted 28 lines · Muhammad Usama Anjum

dev is being dereferenced in device_property_present() which means that it is valid. Don't check its validity again and simplify the code.

---
  drivers/hwmon/hwmon.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/hwmon.c +1 -1
index e36ea82da1474..aec32abd0a89f 100644
@@ -822,7 +822,7 @@ __hwmon_device_register(struct device *dev, const
 char *name, void *drvdata,
       hwdev->name = name;
       hdev->class = &hwmon_class;
       hdev->parent = dev;
-    hdev->of_node = dev ? dev->of_node : NULL;
+    hdev->of_node = dev->of_node;
       hwdev->chip = chip;
       dev_set_drvdata(hdev, drvdata);

      dev_set_name(hdev, HWMON_ID_FORMAT, id);

drivers/hwmon/hwmon.c +0 -0
@@ -834,7 +834,7 @@ __hwmon_device_register(struct device *dev, const
 char *name, void *drvdata,
         INIT_LIST_HEAD(&hwdev->tzdata);

  -    if (dev && dev->of_node && chip && chip->ops->read && +    if (dev->of_node && chip && chip->ops->read && chip->info[0]->type == hwmon_chip &&

Quoted 9 lines
Quoted 2 lines

          (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { err = hwmon_thermal_register_sensors(hdev);

Wrong fix, sorry. While I would love to make dev mandatory, the function is called with dev == NULL from at least one place, and the check is (still)
needed. Even if/when it is removed we would have to add an early check and return -EINVAL if it is NULL.

Thank you for the reply. I've looked at the code again. You are correct.

Quoted 1 line

Guenter