Skip to contentSkip to conversation

power/intel_uncore: fix crash closing uninitialized driver

PATCH In review Bruce Richardson 3 participants 5 messages May 15, 2025 – Jun 10, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 power/intel_uncore: fix crash closing uninitialized driver

When the power_intel_uncore_autotest unit test is run as an unprivileged user which cannot init the power library, it crashes the unit test binary due to calling "rte_power_uncore_exit" after the first test case (initialization) fails. This crash is due to trying to write to NULL file handles.

Fix the crash by checking each file handle is non-null before writing to it and closing it.

Fixes 60b8a661a957 ("power: add Intel uncore frequency control")
---
 drivers/power/intel_uncore/intel_uncore.c | 35 ++++++++++++-----------
 1 file changed, 18 insertions(+), 17 deletions(-)
drivers/power/intel_uncore/intel_uncore.c +18 -17
index 804ad5d755..6759ea1445 100644
@@ -307,27 +307,28 @@ power_intel_uncore_exit(unsigned int pkg, unsigned int die)
 
 	ui = &uncore_info[pkg][die];
 
-	if (fprintf(ui->f_cur_min, "%u", ui->org_min_freq) < 0) {
-		POWER_LOG(ERR, "Fail to write original uncore frequency for "
-				"pkg %02u die %02u", ui->pkg, ui->die);
-		return -1;
+	if (ui->f_cur_min != NULL) {
+		if (fprintf(ui->f_cur_min, "%u", ui->org_min_freq) < 0) {
+			POWER_LOG(ERR, "Fail to write original uncore frequency for pkg %02u die %02u",
+					 ui->pkg, ui->die);
+			return -1;
+		}
+		fflush(ui->f_cur_min);
+		fclose(ui->f_cur_min);
+		ui->f_cur_min = NULL;
 	}
 
-	if (fprintf(ui->f_cur_max, "%u", ui->org_max_freq) < 0) {
-		POWER_LOG(ERR, "Fail to write original uncore frequency for "
-				"pkg %02u die %02u", ui->pkg, ui->die);
-		return -1;
+	if (ui->f_cur_max != NULL) {
+		if (fprintf(ui->f_cur_max, "%u", ui->org_max_freq) < 0) {
+			POWER_LOG(ERR, "Fail to write original uncore frequency for pkg %02u die %02u",
+					 ui->pkg, ui->die);
+			return -1;
+		}
+		fflush(ui->f_cur_max);
+		fclose(ui->f_cur_max);
+		ui->f_cur_max = NULL;
 	}
 
-	fflush(ui->f_cur_min);
-	fflush(ui->f_cur_max);
-
-	/* Close FD of setting freq */
-	fclose(ui->f_cur_min);
-	fclose(ui->f_cur_max);
-	ui->f_cur_min = NULL;
-	ui->f_cur_max = NULL;
-
 	return 0;
 }
 
Signature · 1 line
2.48.1
#1 lore raw

PATCH 2/2 test/power_intel_uncore: skip test if init fails

If the initialization of the uncore power management fails, mark the test as skipped rather than a failure. This prevents false failures in the case where we are running as a normal user, who doesn't have permissions to edit uncore power states.

---
 app/test/test_power_intel_uncore.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
app/test/test_power_intel_uncore.c +5 -2
index 049658627d..973dc7a945 100644
@@ -35,7 +35,7 @@ static int check_power_uncore_init(void)
 			"may occur if environment is not configured "
 			"correctly(APCI cpufreq) or operating in another valid "
 			"Power management environment\n", VALID_PKG, VALID_DIE);
-		return -1;
+		return TEST_SKIPPED;
 	}
 
 	/* Unsuccessful Test */
@@ -257,8 +257,11 @@ test_power_intel_uncore(void)
 	}
 
 	ret = check_power_uncore_init();
-	if (ret < 0)
+	if (ret != 0) {
+		if (ret == TEST_SKIPPED)
+			return TEST_SKIPPED;
 		goto fail_all;
+	}
 
 	ret = check_power_get_uncore_freq();
 	if (ret < 0)
Signature · 1 line
2.48.1
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] test/power_intel_uncore: skip test if init fails

Quoted 10 lines · Bruce Richardson

If the initialization of the uncore power management fails, mark the test as skipped rather than a failure. This prevents false failures in the case where we are running as a normal user, who doesn't have permissions to edit uncore power states.

---
  app/test/test_power_intel_uncore.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
Acked-by on v1 1/2
#3 lore raw
In reply to Bruce Richardson
Quoted 9 lines · Bruce Richardson

When the power_intel_uncore_autotest unit test is run as an unprivileged user which cannot init the power library, it crashes the unit test binary due to calling "rte_power_uncore_exit" after the first test case (initialization) fails. This crash is due to trying to write to NULL file handles.

Fix the crash by checking each file handle is non-null before writing to it and closing it.

Quoted 8 lines
Fixes 60b8a661a957 ("power: add Intel uncore frequency control")
---
  drivers/power/intel_uncore/intel_uncore.c | 35 ++++++++++++-----------
  1 file changed, 18 insertions(+), 17 deletions(-)
#4 lore raw

21/05/2025 15:06, Kevin Laatz:

Quoted 10 lines · Bruce Richardson
Quoted 9 lines

When the power_intel_uncore_autotest unit test is run as an unprivileged user which cannot init the power library, it crashes the unit test binary due to calling "rte_power_uncore_exit" after the first test case (initialization) fails. This crash is due to trying to write to NULL file handles.

Fix the crash by checking each file handle is non-null before writing to it and closing it.

Quoted 6 lines
Quoted 5 lines
Fixes 60b8a661a957 ("power: add Intel uncore frequency control")

Series applied, thanks.