Skip to contentSkip to conversation

thermal: create softlink by name for thermal_zone and cooling_device

PATCH In review Wei Wang 3 participants 17 messages Oct 15, 2019 – Dec 5, 2019 Thermal

lore
Share & tools
lore

Wei Wang

author
#0 lore raw

PATCH 1/1 thermal: create softlink by name for thermal_zone and cooling_device

The paths thermal_zone%d and cooling_device%d are not intuitive and the numbers are subject to change due to device tree change. This usually leads to tree traversal in userspace code.
The patch creates tz-by-name' and cdev-by-name' for thermal zone and cooling_device respectively.

---
 drivers/thermal/thermal_core.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +21 -2
index d4481cc8958f..0ff8fb1d7b0a 100644
@@ -22,6 +22,7 @@
 #include <net/netlink.h>
 #include <net/genetlink.h>
 #include <linux/suspend.h>
+#include <linux/kobject.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/thermal.h>
@@ -46,6 +47,8 @@ static DEFINE_MUTEX(poweroff_lock);
 
 static atomic_t in_suspend;
 static bool power_off_triggered;
+static struct kobject *cdev_link_kobj;
+static struct kobject *tz_link_kobj;
 
 static struct thermal_governor *def_governor;
 
@@ -954,7 +957,7 @@ __thermal_cooling_device_register(struct device_node *np,
 	struct thermal_zone_device *pos = NULL;
 	int result;
 
-	if (type && strlen(type) >= THERMAL_NAME_LENGTH)
+	if (!type || !type[0] || strlen(type) >= THERMAL_NAME_LENGTH)
 		return ERR_PTR(-EINVAL);
 
 	if (!ops || !ops->get_max_state || !ops->get_cur_state ||
@@ -989,9 +992,15 @@ __thermal_cooling_device_register(struct device_node *np,
 		return ERR_PTR(result);
 	}
 
-	/* Add 'this' new cdev to the global cdev list */
+	/* Add 'this' new cdev to the global cdev list and create link*/
 	mutex_lock(&thermal_list_lock);
 	list_add(&cdev->node, &thermal_cdev_list);
+	if (!cdev_link_kobj)
+		cdev_link_kobj = kobject_create_and_add("cdev-by-name",
+						cdev->device.kobj.parent);
+	if (!cdev_link_kobj || sysfs_create_link(cdev_link_kobj,
+						&cdev->device.kobj, cdev->type))
+		dev_err(&cdev->device, "Failed to create cdev-by-name link\n");
 	mutex_unlock(&thermal_list_lock);
 
 	/* Update binding information for 'this' new cdev */
@@ -1157,6 +1166,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
 			}
 		}
 	}
+	if (cdev_link_kobj)
+		sysfs_remove_link(cdev_link_kobj, cdev->type);
 
 	mutex_unlock(&thermal_list_lock);
 
@@ -1340,6 +1351,12 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 
 	mutex_lock(&thermal_list_lock);
 	list_add_tail(&tz->node, &thermal_tz_list);
+	if (!tz_link_kobj)
+		tz_link_kobj = kobject_create_and_add("tz-by-name",
+						tz->device.kobj.parent);
+	if (!tz_link_kobj || sysfs_create_link(tz_link_kobj,
+						&tz->device.kobj, tz->type))
+		dev_err(&tz->device, "Failed to create tz-by-name link\n");
 	mutex_unlock(&thermal_list_lock);
 
 	/* Bind cooling devices for this zone */
@@ -1411,6 +1428,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
 			}
 		}
 	}
+	if (tz_link_kobj)
+		sysfs_remove_link(tz_link_kobj, tz->type);
 
 	mutex_unlock(&thermal_list_lock);
 
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#1 lore raw
Quoted 6 lines · Wei Wang

The paths thermal_zone%d and cooling_device%d are not intuitive and the numbers are subject to change due to device tree change. This usually leads to tree traversal in userspace code.
The patch creates tz-by-name' and cdev-by-name' for thermal zone and cooling_device respectively.

I like this.

Quoted 27 lines
---
 drivers/thermal/thermal_core.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +3 -0
index d4481cc8958f..0ff8fb1d7b0a 100644
@@ -22,6 +22,7 @@
 #include <net/netlink.h>
 #include <net/genetlink.h>
 #include <linux/suspend.h>
+#include <linux/kobject.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/thermal.h>
@@ -46,6 +47,8 @@ static DEFINE_MUTEX(poweroff_lock);
 
 static atomic_t in_suspend;
 static bool power_off_triggered;
+static struct kobject *cdev_link_kobj;
+static struct kobject *tz_link_kobj;
 
 static struct thermal_governor *def_governor;
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 961

Quoted 6 lines
drivers/thermal/thermal_core.c +1 -1
@@ -954,7 +957,7 @@ __thermal_cooling_device_register(struct device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
+       if (!type || !type[0] || strlen(type) >= THERMAL_NAME_LENGTH)
                return ERR_PTR(-EINVAL);

This should be a separate fix, if needed.

Quoted 5 lines
drivers/thermal/thermal_core.c +0 -0
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
@@ -989,9 +992,15 @@ __thermal_cooling_device_register(struct device_node *np,
                return ERR_PTR(result);
        }
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 1003

Quoted 10 lines
drivers/thermal/thermal_core.c +7 -1
-       /* Add 'this' new cdev to the global cdev list */
+       /* Add 'this' new cdev to the global cdev list and create link*/
        mutex_lock(&thermal_list_lock);
        list_add(&cdev->node, &thermal_cdev_list);
+       if (!cdev_link_kobj)
+               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
+                                               cdev->device.kobj.parent);
+       if (!cdev_link_kobj || sysfs_create_link(cdev_link_kobj,
+                                               &cdev->device.kobj, cdev->type))
+               dev_err(&cdev->device, "Failed to create cdev-by-name link\n");

Any reason not to use the following form instead? It seems easier to read.

if (!cdev_link_kobj) {
               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
                                               cdev->device.kobj.parent);
              ret = sysfs_create_link(cdev_link_kobj,
                                              &cdev->device.kobj, cdev->type))
              if (ret)
                       dev_err(&cdev->device, "Failed to create

cdev-by-name link\n");
}

Quoted 13 lines
drivers/thermal/thermal_core.c +2 -0
        mutex_unlock(&thermal_list_lock);
 
        /* Update binding information for 'this' new cdev */
@@ -1157,6 +1166,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
                        }
                }
        }
+       if (cdev_link_kobj)
+               sysfs_remove_link(cdev_link_kobj, cdev->type);
 
        mutex_unlock(&thermal_list_lock);
 

drivers/thermal/thermal_core.c thermal_zone_device_register(const char *type, int trips, int mask, · line 1359

Quoted 9 lines
drivers/thermal/thermal_core.c +6 -0
@@ -1340,6 +1351,12 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 
        mutex_lock(&thermal_list_lock);
        list_add_tail(&tz->node, &thermal_tz_list);
+       if (!tz_link_kobj)
+               tz_link_kobj = kobject_create_and_add("tz-by-name",
+                                               tz->device.kobj.parent);
+       if (!tz_link_kobj || sysfs_create_link(tz_link_kobj,
+                                               &tz->device.kobj, tz->type))
+               dev_err(&tz->device, "Failed to create tz-by-name link\n");

Same as above.

Quoted 15 lines
drivers/thermal/thermal_core.c +2 -1
        mutex_unlock(&thermal_list_lock);
 
        /* Bind cooling devices for this zone */
@@ -1411,6 +1428,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
                        }
                }
        }
+       if (tz_link_kobj)
+               sysfs_remove_link(tz_link_kobj, tz->type);
 
        mutex_unlock(&thermal_list_lock);
 
--

2.23.0.700.g56cf767bdb-goog

#2 lore raw
Quoted 72 lines · Amit Kucheria
Quoted 6 lines · Wei Wang

The paths thermal_zone%d and cooling_device%d are not intuitive and the numbers are subject to change due to device tree change. This usually leads to tree traversal in userspace code.
The patch creates tz-by-name' and cdev-by-name' for thermal zone and cooling_device respectively.

I like this.

Quoted 27 lines
---
 drivers/thermal/thermal_core.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +3 -0
index d4481cc8958f..0ff8fb1d7b0a 100644
@@ -22,6 +22,7 @@
 #include <net/netlink.h>
 #include <net/genetlink.h>
 #include <linux/suspend.h>
+#include <linux/kobject.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/thermal.h>
@@ -46,6 +47,8 @@ static DEFINE_MUTEX(poweroff_lock);
 
 static atomic_t in_suspend;
 static bool power_off_triggered;
+static struct kobject *cdev_link_kobj;
+static struct kobject *tz_link_kobj;
 
 static struct thermal_governor *def_governor;
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 961

Quoted 6 lines
drivers/thermal/thermal_core.c +1 -1
@@ -954,7 +957,7 @@ __thermal_cooling_device_register(struct device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
+       if (!type || !type[0] || strlen(type) >= THERMAL_NAME_LENGTH)
                return ERR_PTR(-EINVAL);

This should be a separate fix, if needed.

Quoted 5 lines
drivers/thermal/thermal_core.c +0 -0
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
@@ -989,9 +992,15 @@ __thermal_cooling_device_register(struct device_node *np,
                return ERR_PTR(result);
        }
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 1003

Quoted 10 lines
drivers/thermal/thermal_core.c +7 -1
-       /* Add 'this' new cdev to the global cdev list */
+       /* Add 'this' new cdev to the global cdev list and create link*/
        mutex_lock(&thermal_list_lock);
        list_add(&cdev->node, &thermal_cdev_list);
+       if (!cdev_link_kobj)
+               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
+                                               cdev->device.kobj.parent);
+       if (!cdev_link_kobj || sysfs_create_link(cdev_link_kobj,
+                                               &cdev->device.kobj, cdev->type))
+               dev_err(&cdev->device, "Failed to create cdev-by-name link\n");

Any reason not to use the following form instead? It seems easier to read.

if (!cdev_link_kobj) {
               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
                                               cdev->device.kobj.parent);
              ret = sysfs_create_link(cdev_link_kobj,
                                              &cdev->device.kobj, cdev->type))
              if (ret)
                       dev_err(&cdev->device, "Failed to create
Quoted 2 lines

cdev-by-name link\n");
}

I can now see why you had to do that - none of the other links would get created after the first one.

Perhaps create the directories in the __init functions and only create the links here?

Quoted 40 lines
Quoted 13 lines
drivers/thermal/thermal_core.c +2 -0
        mutex_unlock(&thermal_list_lock);
 
        /* Update binding information for 'this' new cdev */
@@ -1157,6 +1166,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
                        }
                }
        }
+       if (cdev_link_kobj)
+               sysfs_remove_link(cdev_link_kobj, cdev->type);
 
        mutex_unlock(&thermal_list_lock);
 

drivers/thermal/thermal_core.c thermal_zone_device_register(const char *type, int trips, int mask, · line 1359

Quoted 9 lines
drivers/thermal/thermal_core.c +6 -0
@@ -1340,6 +1351,12 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 
        mutex_lock(&thermal_list_lock);
        list_add_tail(&tz->node, &thermal_tz_list);
+       if (!tz_link_kobj)
+               tz_link_kobj = kobject_create_and_add("tz-by-name",
+                                               tz->device.kobj.parent);
+       if (!tz_link_kobj || sysfs_create_link(tz_link_kobj,
+                                               &tz->device.kobj, tz->type))
+               dev_err(&tz->device, "Failed to create tz-by-name link\n");

Same as above.

Quoted 15 lines
drivers/thermal/thermal_core.c +2 -1
        mutex_unlock(&thermal_list_lock);
 
        /* Bind cooling devices for this zone */
@@ -1411,6 +1428,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
                        }
                }
        }
+       if (tz_link_kobj)
+               sysfs_remove_link(tz_link_kobj, tz->type);
 
        mutex_unlock(&thermal_list_lock);
 
--

2.23.0.700.g56cf767bdb-goog

author
#3 lore raw
Quoted 44 lines · Amit Kucheria
Quoted 41 lines · Amit Kucheria
Quoted 6 lines · Wei Wang

The paths thermal_zone%d and cooling_device%d are not intuitive and the numbers are subject to change due to device tree change. This usually leads to tree traversal in userspace code.
The patch creates tz-by-name' and cdev-by-name' for thermal zone and cooling_device respectively.

I like this.

Quoted 30 lines
---
 drivers/thermal/thermal_core.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +3 -0
index d4481cc8958f..0ff8fb1d7b0a 100644
@@ -22,6 +22,7 @@
 #include <net/netlink.h>
 #include <net/genetlink.h>
 #include <linux/suspend.h>
+#include <linux/kobject.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/thermal.h>
@@ -46,6 +47,8 @@ static DEFINE_MUTEX(poweroff_lock);
 
 static atomic_t in_suspend;
 static bool power_off_triggered;
+static struct kobject *cdev_link_kobj;
+static struct kobject *tz_link_kobj;
 
 static struct thermal_governor *def_governor;
 
@@ -954,7 +957,7 @@ __thermal_cooling_device_register(struct device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 
Quoted 5 lines
Quoted 5 lines

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 961

Quoted 3 lines
drivers/thermal/thermal_core.c +1 -1
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
+       if (!type || !type[0] || strlen(type) >= THERMAL_NAME_LENGTH)
                return ERR_PTR(-EINVAL);

This should be a separate fix, if needed.

Agree, but the link now requires that "" as invalid _type_.

Quoted 29 lines
Quoted 26 lines
Quoted 6 lines
drivers/thermal/thermal_core.c +0 -0
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
@@ -989,9 +992,15 @@ __thermal_cooling_device_register(struct device_node *np,
                return ERR_PTR(result);
        }
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 1003

Quoted 10 lines
drivers/thermal/thermal_core.c +7 -1
-       /* Add 'this' new cdev to the global cdev list */
+       /* Add 'this' new cdev to the global cdev list and create link*/
        mutex_lock(&thermal_list_lock);
        list_add(&cdev->node, &thermal_cdev_list);
+       if (!cdev_link_kobj)
+               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
+                                               cdev->device.kobj.parent);
+       if (!cdev_link_kobj || sysfs_create_link(cdev_link_kobj,
+                                               &cdev->device.kobj, cdev->type))
+               dev_err(&cdev->device, "Failed to create cdev-by-name link\n");

Any reason not to use the following form instead? It seems easier to read.

if (!cdev_link_kobj) {
               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
                                               cdev->device.kobj.parent);
              ret = sysfs_create_link(cdev_link_kobj,
                                              &cdev->device.kobj, cdev->type))
              if (ret)
                       dev_err(&cdev->device, "Failed to create
Quoted 2 lines

cdev-by-name link\n");
}

Quoted 6 lines

I can now see why you had to do that - none of the other links would get created after the first one.

Perhaps create the directories in the __init functions and only create the links here?

AFAICT, this is no such API except the private get_device_parent() under driver/base/. Also the lazy initialization makes sense in such case when there is no thermal device attached. Looks like the class dir is also lazy-initialized when first device registered
https://elixir.bootlin.com/linux/v5.3.5/source/drivers/base/core.c#L1790.

Quoted 41 lines
Quoted 41 lines
Quoted 14 lines
drivers/thermal/thermal_core.c +2 -0
        mutex_unlock(&thermal_list_lock);
 
        /* Update binding information for 'this' new cdev */
@@ -1157,6 +1166,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
                        }
                }
        }
+       if (cdev_link_kobj)
+               sysfs_remove_link(cdev_link_kobj, cdev->type);
 
        mutex_unlock(&thermal_list_lock);
 

drivers/thermal/thermal_core.c thermal_zone_device_register(const char *type, int trips, int mask, · line 1359

Quoted 9 lines
drivers/thermal/thermal_core.c +6 -0
@@ -1340,6 +1351,12 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 
        mutex_lock(&thermal_list_lock);
        list_add_tail(&tz->node, &thermal_tz_list);
+       if (!tz_link_kobj)
+               tz_link_kobj = kobject_create_and_add("tz-by-name",
+                                               tz->device.kobj.parent);
+       if (!tz_link_kobj || sysfs_create_link(tz_link_kobj,
+                                               &tz->device.kobj, tz->type))
+               dev_err(&tz->device, "Failed to create tz-by-name link\n");

Same as above.

Quoted 15 lines
drivers/thermal/thermal_core.c +2 -1
        mutex_unlock(&thermal_list_lock);
 
        /* Bind cooling devices for this zone */
@@ -1411,6 +1428,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
                        }
                }
        }
+       if (tz_link_kobj)
+               sysfs_remove_link(tz_link_kobj, tz->type);
 
        mutex_unlock(&thermal_list_lock);
 
--

2.23.0.700.g56cf767bdb-goog

#4 lore raw
Quoted 47 lines · Wei Wang
Quoted 44 lines · Amit Kucheria
Quoted 41 lines · Amit Kucheria
Quoted 6 lines · Wei Wang

The paths thermal_zone%d and cooling_device%d are not intuitive and the numbers are subject to change due to device tree change. This usually leads to tree traversal in userspace code.
The patch creates tz-by-name' and cdev-by-name' for thermal zone and cooling_device respectively.

I like this.

Quoted 30 lines
---
 drivers/thermal/thermal_core.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +3 -0
index d4481cc8958f..0ff8fb1d7b0a 100644
@@ -22,6 +22,7 @@
 #include <net/netlink.h>
 #include <net/genetlink.h>
 #include <linux/suspend.h>
+#include <linux/kobject.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/thermal.h>
@@ -46,6 +47,8 @@ static DEFINE_MUTEX(poweroff_lock);
 
 static atomic_t in_suspend;
 static bool power_off_triggered;
+static struct kobject *cdev_link_kobj;
+static struct kobject *tz_link_kobj;
 
 static struct thermal_governor *def_governor;
 
@@ -954,7 +957,7 @@ __thermal_cooling_device_register(struct device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 961

Quoted 3 lines
drivers/thermal/thermal_core.c +1 -1
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
+       if (!type || !type[0] || strlen(type) >= THERMAL_NAME_LENGTH)
                return ERR_PTR(-EINVAL);

This should be a separate fix, if needed.

Agree, but the link now requires that "" as invalid _type_.

I'm not sure I understand. What does this change have to do with adding symlinks below?

Quoted 32 lines
Quoted 32 lines
Quoted 26 lines
Quoted 6 lines
drivers/thermal/thermal_core.c +0 -0
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
@@ -989,9 +992,15 @@ __thermal_cooling_device_register(struct device_node *np,
                return ERR_PTR(result);
        }
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 1003

Quoted 10 lines
drivers/thermal/thermal_core.c +7 -1
-       /* Add 'this' new cdev to the global cdev list */
+       /* Add 'this' new cdev to the global cdev list and create link*/
        mutex_lock(&thermal_list_lock);
        list_add(&cdev->node, &thermal_cdev_list);
+       if (!cdev_link_kobj)
+               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
+                                               cdev->device.kobj.parent);
+       if (!cdev_link_kobj || sysfs_create_link(cdev_link_kobj,
+                                               &cdev->device.kobj, cdev->type))
+               dev_err(&cdev->device, "Failed to create cdev-by-name link\n");

Any reason not to use the following form instead? It seems easier to read.

if (!cdev_link_kobj) {
               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
                                               cdev->device.kobj.parent);
              ret = sysfs_create_link(cdev_link_kobj,
                                              &cdev->device.kobj, cdev->type))
              if (ret)
                       dev_err(&cdev->device, "Failed to create
Quoted 2 lines

cdev-by-name link\n");
}

I can now see why you had to do that - none of the other links would get created after the first one.

Quoted 8 lines
Quoted 3 lines

Perhaps create the directories in the __init functions and only create the links here?

AFAICT, this is no such API except the private get_device_parent() under driver/base/. Also the lazy initialization makes sense in such case when there is no thermal device attached. Looks like the class dir is also lazy-initialized when first device registered
https://elixir.bootlin.com/linux/v5.3.5/source/drivers/base/core.c#L1790.

OK.

Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 14 lines
drivers/thermal/thermal_core.c +2 -0
        mutex_unlock(&thermal_list_lock);
 
        /* Update binding information for 'this' new cdev */
@@ -1157,6 +1166,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
                        }
                }
        }
+       if (cdev_link_kobj)
+               sysfs_remove_link(cdev_link_kobj, cdev->type);
 
        mutex_unlock(&thermal_list_lock);
 

drivers/thermal/thermal_core.c thermal_zone_device_register(const char *type, int trips, int mask, · line 1359

Quoted 9 lines
drivers/thermal/thermal_core.c +6 -0
@@ -1340,6 +1351,12 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 
        mutex_lock(&thermal_list_lock);
        list_add_tail(&tz->node, &thermal_tz_list);
+       if (!tz_link_kobj)
+               tz_link_kobj = kobject_create_and_add("tz-by-name",
+                                               tz->device.kobj.parent);
+       if (!tz_link_kobj || sysfs_create_link(tz_link_kobj,
+                                               &tz->device.kobj, tz->type))
+               dev_err(&tz->device, "Failed to create tz-by-name link\n");

Same as above.

Quoted 15 lines
drivers/thermal/thermal_core.c +2 -1
        mutex_unlock(&thermal_list_lock);
 
        /* Bind cooling devices for this zone */
@@ -1411,6 +1428,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
                        }
                }
        }
+       if (tz_link_kobj)
+               sysfs_remove_link(tz_link_kobj, tz->type);
 
        mutex_unlock(&thermal_list_lock);
 
--

2.23.0.700.g56cf767bdb-goog

author
#5 lore raw
Quoted 54 lines · Amit Kucheria
Quoted 52 lines · Wei Wang
Quoted 49 lines · Amit Kucheria
Quoted 46 lines · Amit Kucheria
Quoted 6 lines · Wei Wang

The paths thermal_zone%d and cooling_device%d are not intuitive and the numbers are subject to change due to device tree change. This usually leads to tree traversal in userspace code.
The patch creates tz-by-name' and cdev-by-name' for thermal zone and cooling_device respectively.

I like this.

Quoted 27 lines
---
 drivers/thermal/thermal_core.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +3 -0
index d4481cc8958f..0ff8fb1d7b0a 100644
@@ -22,6 +22,7 @@
 #include <net/netlink.h>
 #include <net/genetlink.h>
 #include <linux/suspend.h>
+#include <linux/kobject.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/thermal.h>
@@ -46,6 +47,8 @@ static DEFINE_MUTEX(poweroff_lock);
 
 static atomic_t in_suspend;
 static bool power_off_triggered;
+static struct kobject *cdev_link_kobj;
+static struct kobject *tz_link_kobj;
 
 static struct thermal_governor *def_governor;
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 961

Quoted 6 lines
drivers/thermal/thermal_core.c +1 -1
@@ -954,7 +957,7 @@ __thermal_cooling_device_register(struct device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
+       if (!type || !type[0] || strlen(type) >= THERMAL_NAME_LENGTH)
                return ERR_PTR(-EINVAL);

This should be a separate fix, if needed.

Quoted 5 lines
Quoted 1 line

Agree, but the link now requires that "" as invalid _type_.

I'm not sure I understand. What does this change have to do with adding symlinks below?

cdev->type will be used later in sysfs_create_link and adding "" as symlink is kind of bad practise AIUI.

thanks

Quoted 84 lines
Quoted 32 lines
Quoted 32 lines
Quoted 26 lines
Quoted 6 lines
drivers/thermal/thermal_core.c +0 -0
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
@@ -989,9 +992,15 @@ __thermal_cooling_device_register(struct device_node *np,
                return ERR_PTR(result);
        }
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 1003

Quoted 10 lines
drivers/thermal/thermal_core.c +7 -1
-       /* Add 'this' new cdev to the global cdev list */
+       /* Add 'this' new cdev to the global cdev list and create link*/
        mutex_lock(&thermal_list_lock);
        list_add(&cdev->node, &thermal_cdev_list);
+       if (!cdev_link_kobj)
+               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
+                                               cdev->device.kobj.parent);
+       if (!cdev_link_kobj || sysfs_create_link(cdev_link_kobj,
+                                               &cdev->device.kobj, cdev->type))
+               dev_err(&cdev->device, "Failed to create cdev-by-name link\n");

Any reason not to use the following form instead? It seems easier to read.

if (!cdev_link_kobj) {
               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
                                               cdev->device.kobj.parent);
              ret = sysfs_create_link(cdev_link_kobj,
                                              &cdev->device.kobj, cdev->type))
              if (ret)
                       dev_err(&cdev->device, "Failed to create
Quoted 2 lines

cdev-by-name link\n");
}

I can now see why you had to do that - none of the other links would get created after the first one.

Quoted 8 lines
Quoted 3 lines

Perhaps create the directories in the __init functions and only create the links here?

AFAICT, this is no such API except the private get_device_parent() under driver/base/. Also the lazy initialization makes sense in such case when there is no thermal device attached. Looks like the class dir is also lazy-initialized when first device registered
https://elixir.bootlin.com/linux/v5.3.5/source/drivers/base/core.c#L1790.

OK.

Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 14 lines
drivers/thermal/thermal_core.c +2 -0
        mutex_unlock(&thermal_list_lock);
 
        /* Update binding information for 'this' new cdev */
@@ -1157,6 +1166,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
                        }
                }
        }
+       if (cdev_link_kobj)
+               sysfs_remove_link(cdev_link_kobj, cdev->type);
 
        mutex_unlock(&thermal_list_lock);
 

drivers/thermal/thermal_core.c thermal_zone_device_register(const char *type, int trips, int mask, · line 1359

Quoted 9 lines
drivers/thermal/thermal_core.c +6 -0
@@ -1340,6 +1351,12 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 
        mutex_lock(&thermal_list_lock);
        list_add_tail(&tz->node, &thermal_tz_list);
+       if (!tz_link_kobj)
+               tz_link_kobj = kobject_create_and_add("tz-by-name",
+                                               tz->device.kobj.parent);
+       if (!tz_link_kobj || sysfs_create_link(tz_link_kobj,
+                                               &tz->device.kobj, tz->type))
+               dev_err(&tz->device, "Failed to create tz-by-name link\n");

Same as above.

Quoted 15 lines
drivers/thermal/thermal_core.c +2 -1
        mutex_unlock(&thermal_list_lock);
 
        /* Bind cooling devices for this zone */
@@ -1411,6 +1428,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
                        }
                }
        }
+       if (tz_link_kobj)
+               sysfs_remove_link(tz_link_kobj, tz->type);
 
        mutex_unlock(&thermal_list_lock);
 
--

2.23.0.700.g56cf767bdb-goog

#6 lore raw
Quoted 57 lines · Wei Wang
Quoted 54 lines · Amit Kucheria
Quoted 52 lines · Wei Wang
Quoted 49 lines · Amit Kucheria
Quoted 46 lines · Amit Kucheria
Quoted 6 lines · Wei Wang

The paths thermal_zone%d and cooling_device%d are not intuitive and the numbers are subject to change due to device tree change. This usually leads to tree traversal in userspace code.
The patch creates tz-by-name' and cdev-by-name' for thermal zone and cooling_device respectively.

I like this.

Quoted 27 lines
---
 drivers/thermal/thermal_core.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +3 -0
index d4481cc8958f..0ff8fb1d7b0a 100644
@@ -22,6 +22,7 @@
 #include <net/netlink.h>
 #include <net/genetlink.h>
 #include <linux/suspend.h>
+#include <linux/kobject.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/thermal.h>
@@ -46,6 +47,8 @@ static DEFINE_MUTEX(poweroff_lock);
 
 static atomic_t in_suspend;
 static bool power_off_triggered;
+static struct kobject *cdev_link_kobj;
+static struct kobject *tz_link_kobj;
 
 static struct thermal_governor *def_governor;
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 961

Quoted 6 lines
drivers/thermal/thermal_core.c +1 -1
@@ -954,7 +957,7 @@ __thermal_cooling_device_register(struct device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
+       if (!type || !type[0] || strlen(type) >= THERMAL_NAME_LENGTH)
                return ERR_PTR(-EINVAL);

This should be a separate fix, if needed.

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

Agree, but the link now requires that "" as invalid _type_.

I'm not sure I understand. What does this change have to do with adding symlinks below?

cdev->type will be used later in sysfs_create_link and adding "" as symlink is kind of bad practise AIUI.

Perhaps I'm being dense here, in which case my apologies.

But what stops us from splitting the patch into two?
1. Make type == "" as invalid by using your change above and fixing up the strlcpy(cdev->type....) further down.
2. Create the actual symlinks in a separate patch.

Regards,
Amit

Quoted 84 lines
Quoted 84 lines
Quoted 32 lines
Quoted 32 lines
Quoted 26 lines
Quoted 6 lines
drivers/thermal/thermal_core.c +0 -0
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
@@ -989,9 +992,15 @@ __thermal_cooling_device_register(struct device_node *np,
                return ERR_PTR(result);
        }
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 1003

Quoted 10 lines
drivers/thermal/thermal_core.c +7 -1
-       /* Add 'this' new cdev to the global cdev list */
+       /* Add 'this' new cdev to the global cdev list and create link*/
        mutex_lock(&thermal_list_lock);
        list_add(&cdev->node, &thermal_cdev_list);
+       if (!cdev_link_kobj)
+               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
+                                               cdev->device.kobj.parent);
+       if (!cdev_link_kobj || sysfs_create_link(cdev_link_kobj,
+                                               &cdev->device.kobj, cdev->type))
+               dev_err(&cdev->device, "Failed to create cdev-by-name link\n");

Any reason not to use the following form instead? It seems easier to read.

if (!cdev_link_kobj) {
               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
                                               cdev->device.kobj.parent);
              ret = sysfs_create_link(cdev_link_kobj,
                                              &cdev->device.kobj, cdev->type))
              if (ret)
                       dev_err(&cdev->device, "Failed to create
Quoted 2 lines

cdev-by-name link\n");
}

I can now see why you had to do that - none of the other links would get created after the first one.

Quoted 8 lines
Quoted 3 lines

Perhaps create the directories in the __init functions and only create the links here?

AFAICT, this is no such API except the private get_device_parent() under driver/base/. Also the lazy initialization makes sense in such case when there is no thermal device attached. Looks like the class dir is also lazy-initialized when first device registered
https://elixir.bootlin.com/linux/v5.3.5/source/drivers/base/core.c#L1790.

OK.

Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 14 lines
drivers/thermal/thermal_core.c +2 -0
        mutex_unlock(&thermal_list_lock);
 
        /* Update binding information for 'this' new cdev */
@@ -1157,6 +1166,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
                        }
                }
        }
+       if (cdev_link_kobj)
+               sysfs_remove_link(cdev_link_kobj, cdev->type);
 
        mutex_unlock(&thermal_list_lock);
 

drivers/thermal/thermal_core.c thermal_zone_device_register(const char *type, int trips, int mask, · line 1359

Quoted 9 lines
drivers/thermal/thermal_core.c +6 -0
@@ -1340,6 +1351,12 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 
        mutex_lock(&thermal_list_lock);
        list_add_tail(&tz->node, &thermal_tz_list);
+       if (!tz_link_kobj)
+               tz_link_kobj = kobject_create_and_add("tz-by-name",
+                                               tz->device.kobj.parent);
+       if (!tz_link_kobj || sysfs_create_link(tz_link_kobj,
+                                               &tz->device.kobj, tz->type))
+               dev_err(&tz->device, "Failed to create tz-by-name link\n");

Same as above.

Quoted 15 lines
drivers/thermal/thermal_core.c +2 -1
        mutex_unlock(&thermal_list_lock);
 
        /* Bind cooling devices for this zone */
@@ -1411,6 +1428,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
                        }
                }
        }
+       if (tz_link_kobj)
+               sysfs_remove_link(tz_link_kobj, tz->type);
 
        mutex_unlock(&thermal_list_lock);
 
--

2.23.0.700.g56cf767bdb-goog

author
#7 lore raw

v2 cover thermal: introduce by-name softlink

The paths thermal_zone%d and cooling_device%d are not intuitive and the numbers are subject to change due to device tree change. This usually leads to tree traversal in userspace code.
The patch creates tz-by-name' and cdev-by-name' for thermal zone and cooling_device respectively.

Changes since v1 [1]:
* Split cooling device registration into a seperate patch

[1]: v1: https://lore.kernel.org/patchwork/patch/1139450/

Wei Wang (2):
thermal: fix and clean up tz and cdev registration
thermal: create softlink by name for thermal_zone and cooling_device

 drivers/thermal/thermal_core.c | 37 +++++++++++++++++++++++++++++-----
 1 file changed, 32 insertions(+), 5 deletions(-)
Signature · 1 line
2.24.0.393.g34dc348eaf-goog
author
#8 lore raw

PATCH v2 1/2 thermal: fix and clean up tz and cdev registration

Make cooling device registration behavior consistent with thermal zone. This patch also cleans up a unnecessary nullptr check.

---
 drivers/thermal/thermal_core.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/thermal/thermal_core.c +12 -4
index d4481cc8958f..64fbb59c2f44 100644
@@ -954,8 +954,16 @@ __thermal_cooling_device_register(struct device_node *np,
 	struct thermal_zone_device *pos = NULL;
 	int result;
 
-	if (type && strlen(type) >= THERMAL_NAME_LENGTH)
-		return ERR_PTR(-EINVAL);
+	if (!type || !type[0]) {
+	    pr_err("Error: No cooling device type defined\n");
+	    return ERR_PTR(-EINVAL);
+	}
+
+	if (strlen(type) >= THERMAL_NAME_LENGTH) {
+	    pr_err("Error: Cooling device name (%s) too long, "
+		   "should be under %d chars\n", type, THERMAL_NAME_LENGTH);
+	    return ERR_PTR(-EINVAL);
+	}
 
 	if (!ops || !ops->get_max_state || !ops->get_cur_state ||
 	    !ops->set_cur_state)
@@ -972,7 +980,7 @@ __thermal_cooling_device_register(struct device_node *np,
 	}
 
 	cdev->id = result;
-	strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
+	strlcpy(cdev->type, type, sizeof(cdev->type));
 	mutex_init(&cdev->lock);
 	INIT_LIST_HEAD(&cdev->thermal_instances);
 	cdev->np = np;
@@ -1250,7 +1258,7 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 		return ERR_PTR(-EINVAL);
 	}
 
-	if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
+	if (strlen(type) >= THERMAL_NAME_LENGTH) {
 		pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
 		       type, THERMAL_NAME_LENGTH);
 		return ERR_PTR(-EINVAL);
Signature · 1 line
2.24.0.393.g34dc348eaf-goog
#9 lore raw

Re: [PATCH v2 1/2] thermal: fix and clean up tz and cdev registration

Hi Wei,

Quoted 2 lines · Wei Wang

Make cooling device registration behavior consistent with

Consistent how? Please add details.

Quoted 16 lines

thermal zone. This patch also cleans up a unnecessary nullptr check.

---
 drivers/thermal/thermal_core.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/thermal/thermal_core.c +0 -0
index d4481cc8958f..64fbb59c2f44 100644
@@ -954,8 +954,16 @@ __thermal_cooling_device_register(struct device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 964

Quoted 10 lines
drivers/thermal/thermal_core.c +8 -2
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
-               return ERR_PTR(-EINVAL);
+       if (!type || !type[0]) {
+           pr_err("Error: No cooling device type defined\n");
+           return ERR_PTR(-EINVAL);
+       }
+
+       if (strlen(type) >= THERMAL_NAME_LENGTH) {
+           pr_err("Error: Cooling device name (%s) too long, "
+                  "should be under %d chars\n", type, THERMAL_NAME_LENGTH);

Consider fitting into a single greppable string as "Error: Cooling device name over %d chars: %s\n"

Quoted 26 lines
drivers/thermal/thermal_core.c +4 -3
+           return ERR_PTR(-EINVAL);
+       }
 
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
            !ops->set_cur_state)
@@ -972,7 +980,7 @@ __thermal_cooling_device_register(struct device_node *np,
        }
 
        cdev->id = result;
-       strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
+       strlcpy(cdev->type, type, sizeof(cdev->type));
        mutex_init(&cdev->lock);
        INIT_LIST_HEAD(&cdev->thermal_instances);
        cdev->np = np;
@@ -1250,7 +1258,7 @@ thermal_zone_device_register(const char *type, int trips, int mask,
                return ERR_PTR(-EINVAL);
        }
 
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
+       if (strlen(type) >= THERMAL_NAME_LENGTH) {
                pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
                       type, THERMAL_NAME_LENGTH);
                return ERR_PTR(-EINVAL);
--

2.24.0.393.g34dc348eaf-goog

author
#10 lore raw

Re: [PATCH v2 1/2] thermal: fix and clean up tz and cdev registration

Quoted 9 lines · Amit Kucheria

Hi Wei,

Quoted 2 lines · Wei Wang

Make cooling device registration behavior consistent with

Consistent how? Please add details.

Consistent with
https://lore.kernel.org/linux-pm/[email protected]/

will include aboce in next version.

Quoted 16 lines
Quoted 16 lines

thermal zone. This patch also cleans up a unnecessary nullptr check.

---
 drivers/thermal/thermal_core.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/thermal/thermal_core.c +0 -0
index d4481cc8958f..64fbb59c2f44 100644
@@ -954,8 +954,16 @@ __thermal_cooling_device_register(struct device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 
Quoted 14 lines

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 964

Quoted 10 lines
drivers/thermal/thermal_core.c +8 -2
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
-               return ERR_PTR(-EINVAL);
+       if (!type || !type[0]) {
+           pr_err("Error: No cooling device type defined\n");
+           return ERR_PTR(-EINVAL);
+       }
+
+       if (strlen(type) >= THERMAL_NAME_LENGTH) {
+           pr_err("Error: Cooling device name (%s) too long, "
+                  "should be under %d chars\n", type, THERMAL_NAME_LENGTH);

Consider fitting into a single greppable string as "Error: Cooling device name over %d chars: %s\n"

Was intentionally keep it the same as this
https://lore.kernel.org/linux-pm/31a29628894a14e716fff113fd9ce945fe649c05.1562876950.git.amit.kucheria@linaro.org/
Will make it shorter in both places next verion

Quoted 26 lines
Quoted 26 lines
drivers/thermal/thermal_core.c +4 -3
+           return ERR_PTR(-EINVAL);
+       }
 
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
            !ops->set_cur_state)
@@ -972,7 +980,7 @@ __thermal_cooling_device_register(struct device_node *np,
        }
 
        cdev->id = result;
-       strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
+       strlcpy(cdev->type, type, sizeof(cdev->type));
        mutex_init(&cdev->lock);
        INIT_LIST_HEAD(&cdev->thermal_instances);
        cdev->np = np;
@@ -1250,7 +1258,7 @@ thermal_zone_device_register(const char *type, int trips, int mask,
                return ERR_PTR(-EINVAL);
        }
 
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
+       if (strlen(type) >= THERMAL_NAME_LENGTH) {
                pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
                       type, THERMAL_NAME_LENGTH);
                return ERR_PTR(-EINVAL);
--

2.24.0.393.g34dc348eaf-goog

#11 lore raw

Re: [PATCH v2 1/2] thermal: fix and clean up tz and cdev registration

Quoted 8 lines · Wei Wang
Quoted 5 lines · Amit Kucheria

Hi Wei,

Quoted 1 line · Wei Wang
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Make cooling device registration behavior consistent with

Consistent how? Please add details.

Consistent with
https://lore.kernel.org/linux-pm/[email protected]/

will include aboce in next version.

Thanks.

Quoted 31 lines
Quoted 17 lines
Quoted 17 lines

thermal zone. This patch also cleans up a unnecessary nullptr check.

---
 drivers/thermal/thermal_core.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/thermal/thermal_core.c +0 -0
index d4481cc8958f..64fbb59c2f44 100644
@@ -954,8 +954,16 @@ __thermal_cooling_device_register(struct device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 
Quoted 14 lines

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 964

Quoted 10 lines
drivers/thermal/thermal_core.c +8 -2
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
-               return ERR_PTR(-EINVAL);
+       if (!type || !type[0]) {
+           pr_err("Error: No cooling device type defined\n");
+           return ERR_PTR(-EINVAL);
+       }
+
+       if (strlen(type) >= THERMAL_NAME_LENGTH) {
+           pr_err("Error: Cooling device name (%s) too long, "
+                  "should be under %d chars\n", type, THERMAL_NAME_LENGTH);

Consider fitting into a single greppable string as "Error: Cooling device name over %d chars: %s\n"

Quoted 3 lines

Was intentionally keep it the same as this
https://lore.kernel.org/linux-pm/31a29628894a14e716fff113fd9ce945fe649c05.1562876950.git.amit.kucheria@linaro.org/
Will make it shorter in both places next verion

Yes please, make it a separate patch. We didn't catch it during review.

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
drivers/thermal/thermal_core.c +4 -3
+           return ERR_PTR(-EINVAL);
+       }
 
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
            !ops->set_cur_state)
@@ -972,7 +980,7 @@ __thermal_cooling_device_register(struct device_node *np,
        }
 
        cdev->id = result;
-       strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
+       strlcpy(cdev->type, type, sizeof(cdev->type));
        mutex_init(&cdev->lock);
        INIT_LIST_HEAD(&cdev->thermal_instances);
        cdev->np = np;
@@ -1250,7 +1258,7 @@ thermal_zone_device_register(const char *type, int trips, int mask,
                return ERR_PTR(-EINVAL);
        }
 
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
+       if (strlen(type) >= THERMAL_NAME_LENGTH) {
                pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
                       type, THERMAL_NAME_LENGTH);
                return ERR_PTR(-EINVAL);
--

2.24.0.393.g34dc348eaf-goog

#12 lore raw

Re: [PATCH v2 1/2] thermal: fix and clean up tz and cdev registration

Quoted 8 lines · Amit Kucheria
Quoted 6 lines · Wei Wang
Quoted 3 lines · Amit Kucheria

Hi Wei,

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines · Wei Wang
Quoted 2 lines

Make cooling device registration behavior consistent with

Consistent how? Please add details.

Consistent with
https://lore.kernel.org/linux-pm/[email protected]/

Studying this a bit more, git blame pointed to this SHA[1] that fixed it so that NULL value for 'type' is allowed, we just check for it. However, none of the users of thermal_cooling_device_register() seem to pass NULL.

Rui, any insight into the history of why we would NOT want to create a sysfs attribute by passing NULL? Do we still need to allow for NULL values or should we cleanup the API to prevent NULL values?

[1] 204dd1d39c32f39a95

Quoted 69 lines
Quoted 2 lines

will include aboce in next version.

Thanks.

Quoted 31 lines
Quoted 17 lines
Quoted 17 lines

thermal zone. This patch also cleans up a unnecessary nullptr check.

---
 drivers/thermal/thermal_core.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/thermal/thermal_core.c +0 -0
index d4481cc8958f..64fbb59c2f44 100644
@@ -954,8 +954,16 @@ __thermal_cooling_device_register(struct device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 
Quoted 14 lines

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 964

Quoted 10 lines
drivers/thermal/thermal_core.c +8 -2
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
-               return ERR_PTR(-EINVAL);
+       if (!type || !type[0]) {
+           pr_err("Error: No cooling device type defined\n");
+           return ERR_PTR(-EINVAL);
+       }
+
+       if (strlen(type) >= THERMAL_NAME_LENGTH) {
+           pr_err("Error: Cooling device name (%s) too long, "
+                  "should be under %d chars\n", type, THERMAL_NAME_LENGTH);

Consider fitting into a single greppable string as "Error: Cooling device name over %d chars: %s\n"

Quoted 3 lines

Was intentionally keep it the same as this
https://lore.kernel.org/linux-pm/31a29628894a14e716fff113fd9ce945fe649c05.1562876950.git.amit.kucheria@linaro.org/
Will make it shorter in both places next verion

Yes please, make it a separate patch. We didn't catch it during review.

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
drivers/thermal/thermal_core.c +4 -3
+           return ERR_PTR(-EINVAL);
+       }
 
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
            !ops->set_cur_state)
@@ -972,7 +980,7 @@ __thermal_cooling_device_register(struct device_node *np,
        }
 
        cdev->id = result;
-       strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
+       strlcpy(cdev->type, type, sizeof(cdev->type));
        mutex_init(&cdev->lock);
        INIT_LIST_HEAD(&cdev->thermal_instances);
        cdev->np = np;
@@ -1250,7 +1258,7 @@ thermal_zone_device_register(const char *type, int trips, int mask,
                return ERR_PTR(-EINVAL);
        }
 
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
+       if (strlen(type) >= THERMAL_NAME_LENGTH) {
                pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
                       type, THERMAL_NAME_LENGTH);
                return ERR_PTR(-EINVAL);
--

2.24.0.393.g34dc348eaf-goog

reviewer
#13 lore raw

Re: [PATCH v2 1/2] thermal: fix and clean up tz and cdev registration

Quoted 12 lines · Amit Kucheria
Quoted 10 lines · Amit Kucheria
Quoted 8 lines · Wei Wang
Quoted 5 lines · Amit Kucheria

Hi Wei,

Quoted 1 line · Wei Wang
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Make cooling device registration behavior consistent with

Consistent how? Please add details.

Consistent with

https://lore.kernel.org/linux-pm/[email protected]/

Quoted 9 lines

Studying this a bit more, git blame pointed to this SHA[1] that fixed it so that NULL value for 'type' is allowed, we just check for it. However, none of the users of thermal_cooling_device_register() seem to pass NULL.

Rui, any insight into the history of why we would NOT want to create a
sysfs attribute by passing NULL?

Actually, I don't recall there is any requirement that wants to register a cooling_device without "type".

Quoted 3 lines

Do we still need to allow for NULL
values or should we cleanup the API to prevent NULL values?

well, my suggestion is to make this (do NULL check) a separate patch and see if we have any complains, if yes, we can revert it easily.

thanks,
rui

Quoted 48 lines

[1] 204dd1d39c32f39a95

Quoted 45 lines
Quoted 2 lines

will include aboce in next version.

Thanks.

Quoted 40 lines
Quoted 28 lines
Quoted 28 lines

thermal zone. This patch also cleans up a unnecessary nullptr check.

---
 drivers/thermal/thermal_core.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/thermal/thermal_core.c +6 -2
index d4481cc8958f..64fbb59c2f44 100644
@@ -954,8 +954,16 @@ __thermal_cooling_device_register(struct
 device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
-               return ERR_PTR(-EINVAL);
+       if (!type || !type[0]) {
+           pr_err("Error: No cooling device type
 defined\n");
+           return ERR_PTR(-EINVAL);
+       }
+
+       if (strlen(type) >= THERMAL_NAME_LENGTH) {
Quoted 9 lines
Quoted 4 lines
drivers/thermal/thermal_core.c +1 -0
+           pr_err("Error: Cooling device name (%s) too long,

"
+ "should be under %d chars\n", type,
THERMAL_NAME_LENGTH);

Consider fitting into a single greppable string as "Error:
Cooling
device name over %d chars: %s\n"

Was intentionally keep it the same as this

https://lore.kernel.org/linux-pm/31a29628894a14e716fff113fd9ce945fe649c05.1562876950.git.amit.kucheria@linaro.org/

Quoted 36 lines
Quoted 36 lines
Quoted 1 line

Will make it shorter in both places next verion

Yes please, make it a separate patch. We didn't catch it during review.

Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
drivers/thermal/thermal_core.c +2 -0
+           return ERR_PTR(-EINVAL);
+       }
 
        if (!ops || !ops->get_max_state || !ops-
Quoted 1 line

get_cur_state ||

            !ops->set_cur_state)
drivers/thermal/thermal_core.c +1 -1
@@ -972,7 +980,7 @@ __thermal_cooling_device_register(struct
 device_node *np,
        }
 
        cdev->id = result;
-       strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
+       strlcpy(cdev->type, type, sizeof(cdev->type));
        mutex_init(&cdev->lock);
        INIT_LIST_HEAD(&cdev->thermal_instances);
        cdev->np = np;
drivers/thermal/thermal_core.c +1 -1
@@ -1250,7 +1258,7 @@ thermal_zone_device_register(const char
 *type, int trips, int mask,
                return ERR_PTR(-EINVAL);
        }
 
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
+       if (strlen(type) >= THERMAL_NAME_LENGTH) {
                pr_err("Error: Thermal zone name (%s) too
 long, should be under %d chars\n",
                       type, THERMAL_NAME_LENGTH);
                return ERR_PTR(-EINVAL);

--
2.24.0.393.g34dc348eaf-goog

author
#14 lore raw

PATCH v2 2/2 thermal: create softlink by name for thermal_zone and cooling_device

In reply to Wei Wang

The paths thermal_zone%d and cooling_device%d are not intuitive and the numbers are subject to change due to device tree change. This usually leads to tree traversal in userspace code.
The patch creates tz-by-name' and cdev-by-name' for thermal zone and cooling_device respectively.

---
 drivers/thermal/thermal_core.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
drivers/thermal/thermal_core.c +20 -1
index 64fbb59c2f44..4f55e3f16265 100644
@@ -22,6 +22,7 @@
 #include <net/netlink.h>
 #include <net/genetlink.h>
 #include <linux/suspend.h>
+#include <linux/kobject.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/thermal.h>
@@ -46,6 +47,8 @@ static DEFINE_MUTEX(poweroff_lock);
 
 static atomic_t in_suspend;
 static bool power_off_triggered;
+static struct kobject *cdev_link_kobj;
+static struct kobject *tz_link_kobj;
 
 static struct thermal_governor *def_governor;
 
@@ -997,9 +1000,15 @@ __thermal_cooling_device_register(struct device_node *np,
 		return ERR_PTR(result);
 	}
 
-	/* Add 'this' new cdev to the global cdev list */
+	/* Add 'this' new cdev to the global cdev list and create link*/
 	mutex_lock(&thermal_list_lock);
 	list_add(&cdev->node, &thermal_cdev_list);
+	if (!cdev_link_kobj)
+		cdev_link_kobj = kobject_create_and_add("cdev-by-name",
+						cdev->device.kobj.parent);
+	if (!cdev_link_kobj || sysfs_create_link(cdev_link_kobj,
+						&cdev->device.kobj, cdev->type))
+		dev_err(&cdev->device, "Failed to create cdev-by-name link\n");
 	mutex_unlock(&thermal_list_lock);
 
 	/* Update binding information for 'this' new cdev */
@@ -1165,6 +1174,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
 			}
 		}
 	}
+	if (cdev_link_kobj)
+		sysfs_remove_link(cdev_link_kobj, cdev->type);
 
 	mutex_unlock(&thermal_list_lock);
 
@@ -1348,6 +1359,12 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 
 	mutex_lock(&thermal_list_lock);
 	list_add_tail(&tz->node, &thermal_tz_list);
+	if (!tz_link_kobj)
+		tz_link_kobj = kobject_create_and_add("tz-by-name",
+						tz->device.kobj.parent);
+	if (!tz_link_kobj || sysfs_create_link(tz_link_kobj,
+						&tz->device.kobj, tz->type))
+		dev_err(&tz->device, "Failed to create tz-by-name link\n");
 	mutex_unlock(&thermal_list_lock);
 
 	/* Bind cooling devices for this zone */
@@ -1419,6 +1436,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
 			}
 		}
 	}
+	if (tz_link_kobj)
+		sysfs_remove_link(tz_link_kobj, tz->type);
 
 	mutex_unlock(&thermal_list_lock);
 
Signature · 1 line
2.24.0.393.g34dc348eaf-goog
Reviewed-by on v2 2/2
#15 lore raw
Quoted 8 lines · Wei Wang

The paths thermal_zone%d and cooling_device%d are not intuitive and the numbers are subject to change due to device tree change. This usually leads to tree traversal in userspace code.
The patch creates tz-by-name' and cdev-by-name' for thermal zone and cooling_device respectively.

Quoted 76 lines
---
 drivers/thermal/thermal_core.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
drivers/thermal/thermal_core.c +20 -2
index 64fbb59c2f44..4f55e3f16265 100644
@@ -22,6 +22,7 @@
 #include <net/netlink.h>
 #include <net/genetlink.h>
 #include <linux/suspend.h>
+#include <linux/kobject.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/thermal.h>
@@ -46,6 +47,8 @@ static DEFINE_MUTEX(poweroff_lock);
 
 static atomic_t in_suspend;
 static bool power_off_triggered;
+static struct kobject *cdev_link_kobj;
+static struct kobject *tz_link_kobj;
 
 static struct thermal_governor *def_governor;
 
@@ -997,9 +1000,15 @@ __thermal_cooling_device_register(struct device_node *np,
                return ERR_PTR(result);
        }
 
-       /* Add 'this' new cdev to the global cdev list */
+       /* Add 'this' new cdev to the global cdev list and create link*/
        mutex_lock(&thermal_list_lock);
        list_add(&cdev->node, &thermal_cdev_list);
+       if (!cdev_link_kobj)
+               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
+                                               cdev->device.kobj.parent);
+       if (!cdev_link_kobj || sysfs_create_link(cdev_link_kobj,
+                                               &cdev->device.kobj, cdev->type))
+               dev_err(&cdev->device, "Failed to create cdev-by-name link\n");
        mutex_unlock(&thermal_list_lock);
 
        /* Update binding information for 'this' new cdev */
@@ -1165,6 +1174,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
                        }
                }
        }
+       if (cdev_link_kobj)
+               sysfs_remove_link(cdev_link_kobj, cdev->type);
 
        mutex_unlock(&thermal_list_lock);
 
@@ -1348,6 +1359,12 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 
        mutex_lock(&thermal_list_lock);
        list_add_tail(&tz->node, &thermal_tz_list);
+       if (!tz_link_kobj)
+               tz_link_kobj = kobject_create_and_add("tz-by-name",
+                                               tz->device.kobj.parent);
+       if (!tz_link_kobj || sysfs_create_link(tz_link_kobj,
+                                               &tz->device.kobj, tz->type))
+               dev_err(&tz->device, "Failed to create tz-by-name link\n");
        mutex_unlock(&thermal_list_lock);
 
        /* Bind cooling devices for this zone */
@@ -1419,6 +1436,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
                        }
                }
        }
+       if (tz_link_kobj)
+               sysfs_remove_link(tz_link_kobj, tz->type);
 
        mutex_unlock(&thermal_list_lock);
 
--

2.24.0.393.g34dc348eaf-goog

author
#16 lore raw
In reply to Amit Kucheria
Quoted 71 lines · Amit Kucheria
Quoted 57 lines · Wei Wang
Quoted 54 lines · Amit Kucheria
Quoted 52 lines · Wei Wang
Quoted 49 lines · Amit Kucheria
Quoted 46 lines · Amit Kucheria
Quoted 6 lines · Wei Wang

The paths thermal_zone%d and cooling_device%d are not intuitive and the numbers are subject to change due to device tree change. This usually leads to tree traversal in userspace code.
The patch creates tz-by-name' and cdev-by-name' for thermal zone and cooling_device respectively.

I like this.

Quoted 27 lines
---
 drivers/thermal/thermal_core.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +3 -0
index d4481cc8958f..0ff8fb1d7b0a 100644
@@ -22,6 +22,7 @@
 #include <net/netlink.h>
 #include <net/genetlink.h>
 #include <linux/suspend.h>
+#include <linux/kobject.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/thermal.h>
@@ -46,6 +47,8 @@ static DEFINE_MUTEX(poweroff_lock);
 
 static atomic_t in_suspend;
 static bool power_off_triggered;
+static struct kobject *cdev_link_kobj;
+static struct kobject *tz_link_kobj;
 
 static struct thermal_governor *def_governor;
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 961

Quoted 6 lines
drivers/thermal/thermal_core.c +1 -1
@@ -954,7 +957,7 @@ __thermal_cooling_device_register(struct device_node *np,
        struct thermal_zone_device *pos = NULL;
        int result;
 
-       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
+       if (!type || !type[0] || strlen(type) >= THERMAL_NAME_LENGTH)
                return ERR_PTR(-EINVAL);

This should be a separate fix, if needed.

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

Agree, but the link now requires that "" as invalid _type_.

I'm not sure I understand. What does this change have to do with adding symlinks below?

cdev->type will be used later in sysfs_create_link and adding "" as symlink is kind of bad practise AIUI.

Perhaps I'm being dense here, in which case my apologies.

Quoted 8 lines

But what stops us from splitting the patch into two?
1. Make type == "" as invalid by using your change above and fixing up the strlcpy(cdev->type....) further down.
2. Create the actual symlinks in a separate patch.

Regards,
Amit

Updated with v2, thanks.

Quoted 85 lines
Quoted 85 lines
Quoted 85 lines
Quoted 33 lines
Quoted 33 lines
Quoted 27 lines
Quoted 7 lines
drivers/thermal/thermal_core.c +0 -0
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
@@ -989,9 +992,15 @@ __thermal_cooling_device_register(struct device_node *np,
                return ERR_PTR(result);
        }
 

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 1003

Quoted 10 lines
drivers/thermal/thermal_core.c +7 -1
-       /* Add 'this' new cdev to the global cdev list */
+       /* Add 'this' new cdev to the global cdev list and create link*/
        mutex_lock(&thermal_list_lock);
        list_add(&cdev->node, &thermal_cdev_list);
+       if (!cdev_link_kobj)
+               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
+                                               cdev->device.kobj.parent);
+       if (!cdev_link_kobj || sysfs_create_link(cdev_link_kobj,
+                                               &cdev->device.kobj, cdev->type))
+               dev_err(&cdev->device, "Failed to create cdev-by-name link\n");

Any reason not to use the following form instead? It seems easier to read.

if (!cdev_link_kobj) {
               cdev_link_kobj = kobject_create_and_add("cdev-by-name",
                                               cdev->device.kobj.parent);
              ret = sysfs_create_link(cdev_link_kobj,
                                              &cdev->device.kobj, cdev->type))
              if (ret)
                       dev_err(&cdev->device, "Failed to create
Quoted 2 lines

cdev-by-name link\n");
}

I can now see why you had to do that - none of the other links would get created after the first one.

Quoted 8 lines
Quoted 3 lines

Perhaps create the directories in the __init functions and only create the links here?

AFAICT, this is no such API except the private get_device_parent() under driver/base/. Also the lazy initialization makes sense in such case when there is no thermal device attached. Looks like the class dir is also lazy-initialized when first device registered
https://elixir.bootlin.com/linux/v5.3.5/source/drivers/base/core.c#L1790.

OK.

Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 14 lines
drivers/thermal/thermal_core.c +2 -0
        mutex_unlock(&thermal_list_lock);
 
        /* Update binding information for 'this' new cdev */
@@ -1157,6 +1166,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
                        }
                }
        }
+       if (cdev_link_kobj)
+               sysfs_remove_link(cdev_link_kobj, cdev->type);
 
        mutex_unlock(&thermal_list_lock);
 

drivers/thermal/thermal_core.c thermal_zone_device_register(const char *type, int trips, int mask, · line 1359

Quoted 9 lines
drivers/thermal/thermal_core.c +6 -0
@@ -1340,6 +1351,12 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 
        mutex_lock(&thermal_list_lock);
        list_add_tail(&tz->node, &thermal_tz_list);
+       if (!tz_link_kobj)
+               tz_link_kobj = kobject_create_and_add("tz-by-name",
+                                               tz->device.kobj.parent);
+       if (!tz_link_kobj || sysfs_create_link(tz_link_kobj,
+                                               &tz->device.kobj, tz->type))
+               dev_err(&tz->device, "Failed to create tz-by-name link\n");

Same as above.

Quoted 15 lines
drivers/thermal/thermal_core.c +2 -1
        mutex_unlock(&thermal_list_lock);
 
        /* Bind cooling devices for this zone */
@@ -1411,6 +1428,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
                        }
                }
        }
+       if (tz_link_kobj)
+               sysfs_remove_link(tz_link_kobj, tz->type);
 
        mutex_unlock(&thermal_list_lock);
 
--

2.23.0.700.g56cf767bdb-goog