* [PATCH v3] thermal: sysfs: Return ENODATA instead of EAGAIN for reads
@ 2025-06-20 10:41 Hsin-Te Yuan
2025-07-03 14:44 ` Rafael J. Wysocki
0 siblings, 1 reply; 2+ messages in thread
From: Hsin-Te Yuan @ 2025-06-20 10:41 UTC (permalink / raw)
To: Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba
Cc: linux-pm, linux-kernel, 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.
Signed-off-by: Hsin-Te Yuan <yuanhsinte@chromium.org>
---
Changes in v3:
- Refine the control flow and optimize the success case.
- Link to v2: https://lore.kernel.org/r/20250512-temp-v2-1-048be58eaaa5@chromium.org
Changes in v2:
- Modify commit message to make it clear
- Link to v1: https://lore.kernel.org/r/20250409-temp-v1-1-9a391d8c60fd@chromium.org
---
drivers/thermal/thermal_sysfs.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
index 24b9055a0b6c515b865e0d7e2db1d0de176ff767..d80612506a334ab739e7545cdfe984ab4dffab7c 100644
--- a/drivers/thermal/thermal_sysfs.c
+++ b/drivers/thermal/thermal_sysfs.c
@@ -40,10 +40,13 @@ temp_show(struct device *dev, struct device_attribute *attr, char *buf)
ret = thermal_zone_get_temp(tz, &temperature);
- if (ret)
- return ret;
+ if (!ret)
+ return sprintf(buf, "%d\n", temperature);
- return sprintf(buf, "%d\n", temperature);
+ if (ret == -EAGAIN)
+ return -ENODATA;
+
+ return ret;
}
static ssize_t
---
base-commit: 75f5f23f8787c5e184fcb2fbcd02d8e9317dc5e7
change-id: 20250409-temp-6ebd13ad0dbd
Best regards,
--
Hsin-Te Yuan <yuanhsinte@chromium.org>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v3] thermal: sysfs: Return ENODATA instead of EAGAIN for reads
2025-06-20 10:41 [PATCH v3] thermal: sysfs: Return ENODATA instead of EAGAIN for reads Hsin-Te Yuan
@ 2025-07-03 14:44 ` Rafael J. Wysocki
0 siblings, 0 replies; 2+ messages in thread
From: Rafael J. Wysocki @ 2025-07-03 14:44 UTC (permalink / raw)
To: Hsin-Te Yuan
Cc: Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
linux-pm, linux-kernel
On Fri, Jun 20, 2025 at 12:41 PM Hsin-Te Yuan <yuanhsinte@chromium.org> wrote:
>
> 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.
>
> Signed-off-by: Hsin-Te Yuan <yuanhsinte@chromium.org>
> ---
> Changes in v3:
> - Refine the control flow and optimize the success case.
> - Link to v2: https://lore.kernel.org/r/20250512-temp-v2-1-048be58eaaa5@chromium.org
>
> Changes in v2:
> - Modify commit message to make it clear
> - Link to v1: https://lore.kernel.org/r/20250409-temp-v1-1-9a391d8c60fd@chromium.org
> ---
> drivers/thermal/thermal_sysfs.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
> index 24b9055a0b6c515b865e0d7e2db1d0de176ff767..d80612506a334ab739e7545cdfe984ab4dffab7c 100644
> --- a/drivers/thermal/thermal_sysfs.c
> +++ b/drivers/thermal/thermal_sysfs.c
> @@ -40,10 +40,13 @@ temp_show(struct device *dev, struct device_attribute *attr, char *buf)
>
> ret = thermal_zone_get_temp(tz, &temperature);
>
> - if (ret)
> - return ret;
> + if (!ret)
> + return sprintf(buf, "%d\n", temperature);
>
> - return sprintf(buf, "%d\n", temperature);
> + if (ret == -EAGAIN)
> + return -ENODATA;
> +
> + return ret;
> }
>
> static ssize_t
>
> ---
Applied as 6.17 material, thanks!
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-07-03 14:45 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-20 10:41 [PATCH v3] thermal: sysfs: Return ENODATA instead of EAGAIN for reads Hsin-Te Yuan
2025-07-03 14:44 ` Rafael J. Wysocki
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).