[PATCH v2 2/3] power: check for errors when writing frequency
Stephen Hemminger
stephen at networkplumber.org
Tue Sep 8 18:52:33 CEST 2026
The sysfs file is buffered, so fprintf() returns success even when the
kernel rejects the value. The error only shows up when the buffer is
flushed, and the fflush() return was never checked. A failed frequency
change was therefore reported as success.
Use write_core_sysfs_s() which already does the fseek, write and a
checked fflush.
Fixes: 445c6528b55f ("power: common interface for guest and host")
Fixes: ef1cc88f1837 ("power: support cppc_cpufreq driver")
Fixes: 1ed04d33cf19 ("power: support amd-pstate cpufreq driver")
Cc: stable at dpdk.org
Signed-off-by: Stephen Hemminger <stephen at networkplumber.org>
---
drivers/power/acpi/acpi_cpufreq.c | 12 +++++-------
drivers/power/amd_pstate/amd_pstate_cpufreq.c | 16 +++++++---------
drivers/power/cppc/cppc_cpufreq.c | 12 +++++-------
3 files changed, 17 insertions(+), 23 deletions(-)
diff --git a/drivers/power/acpi/acpi_cpufreq.c b/drivers/power/acpi/acpi_cpufreq.c
index af85a8cdec..4ee3d5bd26 100644
--- a/drivers/power/acpi/acpi_cpufreq.c
+++ b/drivers/power/acpi/acpi_cpufreq.c
@@ -54,6 +54,8 @@ static struct acpi_power_info lcore_power_info[RTE_MAX_LCORE];
static int
set_freq_internal(struct acpi_power_info *pi, uint32_t idx)
{
+ char buf[16];
+
if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
POWER_LOG(ERR, "Invalid frequency index %u, which "
"should be less than %u", idx, pi->nb_freqs);
@@ -66,17 +68,13 @@ set_freq_internal(struct acpi_power_info *pi, uint32_t idx)
POWER_DEBUG_LOG("Frequency[%u] %u to be set for lcore %u",
idx, pi->freqs[idx], pi->lcore_id);
- if (fseek(pi->f, 0, SEEK_SET) < 0) {
- POWER_LOG(ERR, "Fail to set file position indicator to 0 "
- "for setting frequency for lcore %u", pi->lcore_id);
- return -1;
- }
- if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
+
+ snprintf(buf, sizeof(buf), "%u", pi->freqs[idx]);
+ if (write_core_sysfs_s(pi->f, buf) != 0) {
POWER_LOG(ERR, "Fail to write new frequency for "
"lcore %u", pi->lcore_id);
return -1;
}
- fflush(pi->f);
pi->curr_idx = idx;
return 1;
diff --git a/drivers/power/amd_pstate/amd_pstate_cpufreq.c b/drivers/power/amd_pstate/amd_pstate_cpufreq.c
index 13ffcefc84..f7e32fe0ac 100644
--- a/drivers/power/amd_pstate/amd_pstate_cpufreq.c
+++ b/drivers/power/amd_pstate/amd_pstate_cpufreq.c
@@ -57,17 +57,15 @@ static struct amd_pstate_power_info lcore_power_info[RTE_MAX_LCORE];
static int
write_freq(struct amd_pstate_power_info *pi, uint32_t idx)
{
- if (fseek(pi->f, 0, SEEK_SET) < 0) {
- POWER_LOG(ERR, "Fail to set file position indicator to 0 "
- "for setting frequency for lcore %u", pi->lcore_id);
- return -1;
- }
- if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
- POWER_LOG(ERR, "Fail to write new frequency for "
- "lcore %u", pi->lcore_id);
+ char buf[16];
+
+ snprintf(buf, sizeof(buf), "%u", pi->freqs[idx]);
+
+ if (write_core_sysfs_s(pi->f, buf) != 0) {
+ POWER_LOG(ERR, "Fail to write new frequency for lcore %u",
+ pi->lcore_id);
return -1;
}
- fflush(pi->f);
pi->curr_idx = idx;
return 1;
diff --git a/drivers/power/cppc/cppc_cpufreq.c b/drivers/power/cppc/cppc_cpufreq.c
index aed44c1212..3bcd4dec41 100644
--- a/drivers/power/cppc/cppc_cpufreq.c
+++ b/drivers/power/cppc/cppc_cpufreq.c
@@ -63,6 +63,8 @@ static struct cppc_power_info lcore_power_info[RTE_MAX_LCORE];
static int
set_freq_internal(struct cppc_power_info *pi, uint32_t idx)
{
+ char buf[16];
+
if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
POWER_LOG(ERR, "Invalid frequency index %u, which "
"should be less than %u", idx, pi->nb_freqs);
@@ -75,17 +77,13 @@ set_freq_internal(struct cppc_power_info *pi, uint32_t idx)
POWER_DEBUG_LOG("Frequency[%u] %u to be set for lcore %u",
idx, pi->freqs[idx], pi->lcore_id);
- if (fseek(pi->f, 0, SEEK_SET) < 0) {
- POWER_LOG(ERR, "Fail to set file position indicator to 0 "
- "for setting frequency for lcore %u", pi->lcore_id);
- return -1;
- }
- if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
+
+ snprintf(buf, sizeof(buf), "%u", pi->freqs[idx]);
+ if (write_core_sysfs_s(pi->f, buf) != 0) {
POWER_LOG(ERR, "Fail to write new frequency for "
"lcore %u", pi->lcore_id);
return -1;
}
- fflush(pi->f);
pi->curr_idx = idx;
return 1;
--
2.53.0
More information about the stable
mailing list