Skip to contentSkip to conversation

thermal: sysfs: Return ENODATA instead of EAGAIN for reads

PATCH Superseded Hsin-Te Yuan 2 participants 2 messages May 12, 2025 – May 20, 2025 Thermal

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 thermal: sysfs: Return ENODATA instead of EAGAIN for reads

According to POSIX spec, EAGAIN returned by read with O_NONBLOCK set means the read would block. Hence, the common implementation in nonblocking model will poll the file when the nonblocking read returns EAGAIN. However, when the target file is thermal zone, this mechanism will totally malfunction because thermal zone doesn't implement sysfs notification and thus the poll will never return.

For example, the read in Golang implemnts such method and sometimes hangs at reading some thermal zones via sysfs.

Change to throw ENODATA instead of EAGAIN to userspace.

---
Changes in v2:
- Modify commit message to make it clear
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 drivers/thermal/thermal_sysfs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +4 -1
index 24b9055a0b6c515b865e0d7e2db1d0de176ff767..3d1713e053dfb867933d95131f1f2491d2ecd07e 100644
@@ -40,8 +40,11 @@ temp_show(struct device *dev, struct device_attribute *attr, char *buf)
 
 	ret = thermal_zone_get_temp(tz, &temperature);
 
-	if (ret)
+	if (ret) {
+		if (ret == -EAGAIN)
+			return -ENODATA;
 		return ret;
+	}
 
 	return sprintf(buf, "%d\n", temperature);
 }

---
base-commit: 82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3
change-id: 20250409-temp-6ebd13ad0dbd

Best regards,

Signature · 1 line
Hsin-Te Yuan <[email protected]>
#1 lore raw
Quoted 27 lines · Hsin-Te Yuan

According to POSIX spec, EAGAIN returned by read with O_NONBLOCK set means the read would block. Hence, the common implementation in nonblocking model will poll the file when the nonblocking read returns EAGAIN. However, when the target file is thermal zone, this mechanism will totally malfunction because thermal zone doesn't implement sysfs notification and thus the poll will never return.

For example, the read in Golang implemnts such method and sometimes hangs at reading some thermal zones via sysfs.

Change to throw ENODATA instead of EAGAIN to userspace.

---
Changes in v2:
- Modify commit message to make it clear
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 drivers/thermal/thermal_sysfs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +0 -0
index 24b9055a0b6c515b865e0d7e2db1d0de176ff767..3d1713e053dfb867933d95131f1f2491d2ecd07e 100644

drivers/thermal/thermal_sysfs.c temp_show(struct device *dev, struct device_attribute *attr, char *buf) · line 47

Quoted 9 lines
drivers/thermal/thermal_sysfs.c +4 -1
@@ -40,8 +40,11 @@ temp_show(struct device *dev, struct device_attribute *attr, char *buf)
 
        ret = thermal_zone_get_temp(tz, &temperature);
 
-       if (ret)
+       if (ret) {
+               if (ret == -EAGAIN)
+                       return -ENODATA;
                return ret;
+       }

I would prefer to do

if (ret == -EAGAIN)

        return -ENODATA;

if (ret)

        return ret;

here or even

if (!ret)

        return sprintf(buf, "%d\n", temperature);

if (ret == -EAGAIN)

        return -ENODATA;

return ret;

if you want to optimize for the success case.

Quoted 5 lines
drivers/thermal/thermal_sysfs.c +0 -1
        return sprintf(buf, "%d\n", temperature);
 }
 
---