[PATCH v2] drm/xe/hwmon: Update xe hwmon with couple of fixes

Lucas De Marchi lucas.demarchi at intel.com
Mon Apr 1 14:55:13 UTC 2024


On Sun, Mar 31, 2024 at 08:23:40PM +0530, Karthik Poosa wrote:
>Fix potential overflows with upcasting.
>Initialize variables which were being used uninitialized.
>
>v2:
> - Rebase.
> - In xe_hwmon_process_reg, set argument 'value' to 0 on failure.
>   With this change caller function need not initialize value to 0.
>
>Fixes: 4446fcf220ce ("drm/xe/hwmon: Expose power1_max_interval")
>Signed-off-by: Karthik Poosa <karthik.poosa at intel.com>
>Reviewed-by: Anshuman Gupta <anshuman.gupta at intel.com>
>---
> drivers/gpu/drm/xe/xe_hwmon.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
>diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
>index 7e8caac838e0..bc1bc73749c1 100644
>--- a/drivers/gpu/drm/xe/xe_hwmon.c
>+++ b/drivers/gpu/drm/xe/xe_hwmon.c
>@@ -129,8 +129,10 @@ static void xe_hwmon_process_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon
>
> 	reg.raw = xe_hwmon_get_reg(hwmon, hwmon_reg, channel);
>
>-	if (!reg.raw)
>+	if (!reg.raw) {
>+		*value = 0;
> 		return;
>+	}
>
> 	switch (operation) {
> 	case REG_READ32:
>@@ -143,6 +145,7 @@ static void xe_hwmon_process_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon
> 		*value = xe_mmio_read64_2x32(hwmon->gt, reg);
> 		break;
> 	default:
>+		*value = 0;
> 		drm_warn(&gt_to_xe(hwmon->gt)->drm, "Invalid xe hwmon reg operation: %d\n",
> 			 operation);
> 		break;
>@@ -298,7 +301,7 @@ xe_hwmon_power_max_interval_show(struct device *dev, struct device_attribute *at
> 	 * As y can be < 2, we compute tau4 = (4 | x) << y
> 	 * and then add 2 when doing the final right shift to account for units
> 	 */
>-	tau4 = ((1 << x_w) | x) << y;
>+	tau4 = (u64)((1 << x_w) | x) << y;
>
> 	/* val in hwmon interface units (millisec) */
> 	out = mul_u64_u32_shr(tau4, SF_TIME, hwmon->scl_shift_time + x_w);
>@@ -339,7 +342,7 @@ xe_hwmon_power_max_interval_store(struct device *dev, struct device_attribute *a
> 	r = FIELD_PREP(PKG_MAX_WIN, PKG_MAX_WIN_DEFAULT);
> 	x = REG_FIELD_GET(PKG_MAX_WIN_X, r);
> 	y = REG_FIELD_GET(PKG_MAX_WIN_Y, r);
>-	tau4 = ((1 << x_w) | x) << y;
>+	tau4 = (u64)((1 << x_w) | x) << y;

these last two hunks are justifiable as y max is 31 and x max is 3.
Casting one of them to u64 ensures the result doesn't overflow.

Lucas De Marchi

> 	max_win = mul_u64_u32_shr(tau4, SF_TIME, hwmon->scl_shift_time + x_w);
>
> 	if (val > max_win)
>-- 
>2.25.1
>


More information about the Intel-xe mailing list