[PATCH] tools/power/x86: Fix write_sysfs() return type

From: amarjeet

Date: Tue Jul 28 2026 - 04:01:59 EST


From: Amarjeet <amarjeet@xxxxxxxxx>

write_sysfs() returns an unsigned int even though it returns -1 when
write() fails. This converts the error to UINT_MAX and can cause callers
that treat a nonzero return value as success to miss the failure.

Use ssize_t to match write(), update the receiving variable accordingly,
and explicitly require a positive result in sysfs_write_string().

Signed-off-by: Amarjeet <amarjeet@xxxxxxxxx>
---
.../x86/x86_energy_perf_policy/x86_energy_perf_policy.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
index 0dc959e30076..6db7620e7b00 100644
--- a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
+++ b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
@@ -867,7 +867,7 @@ static unsigned int read_sysfs(const char *path, char *buf, size_t buflen)
return (unsigned int)numread;
}

-static unsigned int write_sysfs(const char *path, char *buf, size_t buflen)
+static ssize_t write_sysfs(const char *path, char *buf, size_t buflen)
{
ssize_t numwritten;
int fd;
@@ -886,7 +886,7 @@ static unsigned int write_sysfs(const char *path, char *buf, size_t buflen)

close(fd);

- return (unsigned int)numwritten;
+ return numwritten;
}

static int sysfs_read_string(const char *path, char *buf, size_t buflen)
@@ -911,7 +911,7 @@ static int sysfs_write_string(const char *path, const char *buf)
len = snprintf(tmp, sizeof(tmp), "%s\n", buf);
if (len < 0 || len >= (int)sizeof(tmp))
return -1;
- return write_sysfs(path, tmp, (size_t)len + 1) ? 0 : -1;
+ return write_sysfs(path, tmp, (size_t)len + 1) > 0 ? 0 : -1;
}

void print_hwp_cap(int cpu, struct msr_hwp_cap *cap, char *str)
@@ -1016,7 +1016,7 @@ static int set_epb_sysfs(int cpu, int val)
char path[SYSFS_PATH_MAX];
char linebuf[3];
char *endp;
- int ret;
+ ssize_t ret;

if (!has_epb)
return -1;
--
2.34.1