[1/2] power/intel_uncore: fix crash closing uninitialized driver

Message ID 20250515165854.1087247-1-bruce.richardson@intel.com (mailing list archive)
State Accepted
Delegated to: Thomas Monjalon
Headers
Series [1/2] power/intel_uncore: fix crash closing uninitialized driver |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Bruce Richardson May 15, 2025, 4:58 p.m. UTC
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")
Cc: stable@dpdk.org

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 drivers/power/intel_uncore/intel_uncore.c | 35 ++++++++++++-----------
 1 file changed, 18 insertions(+), 17 deletions(-)
  

Comments

Kevin Laatz May 21, 2025, 1:06 p.m. UTC | #1
On 15/05/2025 17:58, Bruce Richardson wrote:
> 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")
> Cc:stable@dpdk.org
>
> Signed-off-by: Bruce Richardson<bruce.richardson@intel.com>
> ---
>   drivers/power/intel_uncore/intel_uncore.c | 35 ++++++++++++-----------
>   1 file changed, 18 insertions(+), 17 deletions(-)
>
Acked-by: Kevin Laatz <kevin.laatz@intel.com>
  
Thomas Monjalon June 10, 2025, 7:07 a.m. UTC | #2
21/05/2025 15:06, Kevin Laatz:
> On 15/05/2025 17:58, Bruce Richardson wrote:
> > 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")
> > Cc:stable@dpdk.org
> >
> > Signed-off-by: Bruce Richardson<bruce.richardson@intel.com>
> >
> Acked-by: Kevin Laatz <kevin.laatz@intel.com>

Series applied, thanks.
  

Patch

diff --git a/drivers/power/intel_uncore/intel_uncore.c b/drivers/power/intel_uncore/intel_uncore.c
index 804ad5d755..6759ea1445 100644
--- a/drivers/power/intel_uncore/intel_uncore.c
+++ b/drivers/power/intel_uncore/intel_uncore.c
@@ -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;
 }