Skip to contentSkip to conversation

drm/amd/pm: return -EINVAL on invalid CCLK OD core index

PATCH In review Candice Li 2 participants 9 messages May 28

Subsystems (3)
lore
Share & tools
lore

Candice Li

author
#0 lore raw

PATCH 1/8 drm/amd/pm: return -EINVAL on invalid CCLK OD core index

Return -EINVAL after an out-of-range core index for PP_OD_EDIT_CCLK_VDDC_TABLE.

---
 drivers/gpu/drm/amd/pm/swsmu/smu11/vangogh_ppt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/pm/swsmu/smu11/vangogh_ppt.c +1 -0
index 0349c73eb00077..717ba46c893312 100644
@@ -2047,6 +2047,7 @@ static int vangogh_od_edit_dpm_table(struct smu_context *smu, enum PP_OD_DPM_TAB
 		if (input[0] >= smu->cpu_core_num) {
 			dev_err(smu->adev->dev, "core index is overflow, should be less than %d\n",
 				smu->cpu_core_num);
+			return -EINVAL;
 		}
 		smu->cpu_core_id_select = input[0];
 		if (input[1] == 0) {
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/8 drm/amdgpu/pm: fix SmartShift bias sysfs store PM refcount on parse error

Return the parse error before acquiring PM access.

---
 drivers/gpu/drm/amd/pm/amdgpu_pm.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/pm/amdgpu_pm.c +3 -5
index 0c58d23013f41f..8140bc77591c3e 100644
@@ -1862,12 +1862,12 @@ static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
 {
 	struct drm_device *ddev = dev_get_drvdata(dev);
 	struct amdgpu_device *adev = drm_to_adev(ddev);
-	int r = 0;
+	int r;
 	int bias = 0;
 
 	r = kstrtoint(buf, 10, &bias);
 	if (r)
-		goto out;
+		return r;
 
 	r = amdgpu_pm_get_access(adev);
 	if (r < 0)
@@ -1879,14 +1879,12 @@ static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
 		bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
 
 	amdgpu_smartshift_bias = bias;
-	r = count;
 
 	/* TODO: update bias level with SMU message */
 
-out:
 	amdgpu_pm_put_access(adev);
 
-	return r;
+	return count;
 }
 
 static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH 3/8 drm/amd/ras: validate RAS EEPROM tbl_size before record count

In reply to Candice Li

Corrupt EEPROM data can set tbl_size below the table header size. Guard the RAS_NUM_RECS macros against undersized tbl_size and reset the table during init when tbl_size is below the minimum for the table version instead of trusting the header.

---
 drivers/gpu/drm/amd/ras/rascore/ras_eeprom.c | 30 +++++++++++++++-----
 1 file changed, 23 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/ras/rascore/ras_eeprom.c +23 -7
index 34b798f45af210..9fecec22560995 100644
@@ -141,12 +141,15 @@
 #define RAS_RI_TO_AI(_C, _I) (((_I) + (_C)->ras_fri) % \
 			      (_C)->ras_max_record_count)
 
-#define RAS_NUM_RECS(_tbl_hdr)  (((_tbl_hdr)->tbl_size - \
-				  RAS_TABLE_HEADER_SIZE) / RAS_TABLE_RECORD_SIZE)
+#define RAS_NUM_RECS(_tbl_hdr)						\
+	(((_tbl_hdr)->tbl_size < RAS_TABLE_HEADER_SIZE) ? 0u :		\
+	 (((_tbl_hdr)->tbl_size - RAS_TABLE_HEADER_SIZE) / RAS_TABLE_RECORD_SIZE))
 
-#define RAS_NUM_RECS_V2_1(_tbl_hdr)  (((_tbl_hdr)->tbl_size - \
-				       RAS_TABLE_HEADER_SIZE - \
-				       RAS_TABLE_V2_1_INFO_SIZE) / RAS_TABLE_RECORD_SIZE)
+#define RAS_NUM_RECS_V2_1(_tbl_hdr)					\
+	(((_tbl_hdr)->tbl_size < RAS_TABLE_HEADER_SIZE +			\
+	  RAS_TABLE_V2_1_INFO_SIZE) ? 0u :				\
+	 (((_tbl_hdr)->tbl_size - RAS_TABLE_HEADER_SIZE -		\
+	   RAS_TABLE_V2_1_INFO_SIZE) / RAS_TABLE_RECORD_SIZE))
 
 #define to_ras_core_context(x) (container_of(x, struct ras_core_context, ras_eeprom))
 
@@ -1139,14 +1142,27 @@ static int __check_ras_table_status(struct ras_core_context *ras_core)
 	switch (hdr->version) {
 	case RAS_TABLE_VER_V2_1:
 	case RAS_TABLE_VER_V3:
-		control->ras_num_recs = RAS_NUM_RECS_V2_1(hdr);
+		if (hdr->tbl_size < RAS_TABLE_HEADER_SIZE + RAS_TABLE_V2_1_INFO_SIZE) {
+			RAS_DEV_ERR(ras_core->dev,
+				"RAS header invalid, tbl_size %u smaller than minimum %u, resetting table\n",
+				hdr->tbl_size,
+				RAS_TABLE_HEADER_SIZE + RAS_TABLE_V2_1_INFO_SIZE);
+			return ras_eeprom_reset_table(ras_core);
+		}
 		control->ras_record_offset = RAS_RECORD_START_V2_1;
 		control->ras_max_record_count = RAS_MAX_RECORD_COUNT_V2_1;
+		control->ras_num_recs = RAS_NUM_RECS_V2_1(hdr);
 		break;
 	case RAS_TABLE_VER_V1:
-		control->ras_num_recs = RAS_NUM_RECS(hdr);
+		if (hdr->tbl_size < RAS_TABLE_HEADER_SIZE) {
+			RAS_DEV_ERR(ras_core->dev,
+				"RAS header invalid, tbl_size %u smaller than minimum %u, resetting table\n",
+				hdr->tbl_size, RAS_TABLE_HEADER_SIZE);
+			return ras_eeprom_reset_table(ras_core);
+		}
 		control->ras_record_offset = RAS_RECORD_START;
 		control->ras_max_record_count = RAS_MAX_RECORD_COUNT;
+		control->ras_num_recs = RAS_NUM_RECS(hdr);
 		break;
 	default:
 		RAS_DEV_ERR(ras_core->dev,
Signature · 1 line
2.25.1
author
#3 lore raw

PATCH 4/8 drm/amdgpu: validate RAS EEPROM tbl_size before record count

In reply to Candice Li

Corrupt EEPROM data can set tbl_size below the table header size. Guard the RAS_NUM_RECS macros against undersized tbl_size and reset the table during init when tbl_size is below the minimum for the table version instead of trusting the header.

---
 .../gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c    | 30 ++++++++++++++-----
 1 file changed, 23 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c +23 -7
index c61389a079822e..159edaf2ba7576 100644
@@ -145,12 +145,15 @@
 #define RAS_RI_TO_AI(_C, _I) (((_I) + (_C)->ras_fri) % \
 			      (_C)->ras_max_record_count)
 
-#define RAS_NUM_RECS(_tbl_hdr)  (((_tbl_hdr)->tbl_size - \
-				  RAS_TABLE_HEADER_SIZE) / RAS_TABLE_RECORD_SIZE)
+#define RAS_NUM_RECS(_tbl_hdr)						\
+	(((_tbl_hdr)->tbl_size < RAS_TABLE_HEADER_SIZE) ? 0u :		\
+	 (((_tbl_hdr)->tbl_size - RAS_TABLE_HEADER_SIZE) / RAS_TABLE_RECORD_SIZE))
 
-#define RAS_NUM_RECS_V2_1(_tbl_hdr)  (((_tbl_hdr)->tbl_size - \
-				       RAS_TABLE_HEADER_SIZE - \
-				       RAS_TABLE_V2_1_INFO_SIZE) / RAS_TABLE_RECORD_SIZE)
+#define RAS_NUM_RECS_V2_1(_tbl_hdr)					\
+	(((_tbl_hdr)->tbl_size < RAS_TABLE_HEADER_SIZE +			\
+	  RAS_TABLE_V2_1_INFO_SIZE) ? 0u :				\
+	 (((_tbl_hdr)->tbl_size - RAS_TABLE_HEADER_SIZE -		\
+	   RAS_TABLE_V2_1_INFO_SIZE) / RAS_TABLE_RECORD_SIZE))
 
 #define to_amdgpu_device(x) ((container_of(x, struct amdgpu_ras, eeprom_control))->adev)
 
@@ -1610,14 +1613,27 @@ int amdgpu_ras_eeprom_init(struct amdgpu_ras_eeprom_control *control)
 	switch (hdr->version) {
 	case RAS_TABLE_VER_V2_1:
 	case RAS_TABLE_VER_V3:
-		control->ras_num_recs = RAS_NUM_RECS_V2_1(hdr);
+		if (hdr->tbl_size < RAS_TABLE_HEADER_SIZE + RAS_TABLE_V2_1_INFO_SIZE) {
+			dev_err(adev->dev,
+				"RAS header invalid, tbl_size %u smaller than minimum %u, resetting table\n",
+				hdr->tbl_size,
+				RAS_TABLE_HEADER_SIZE + RAS_TABLE_V2_1_INFO_SIZE);
+			return amdgpu_ras_eeprom_reset_table(control);
+		}
 		control->ras_record_offset = RAS_RECORD_START_V2_1;
 		control->ras_max_record_count = RAS_MAX_RECORD_COUNT_V2_1;
+		control->ras_num_recs = RAS_NUM_RECS_V2_1(hdr);
 		break;
 	case RAS_TABLE_VER_V1:
-		control->ras_num_recs = RAS_NUM_RECS(hdr);
+		if (hdr->tbl_size < RAS_TABLE_HEADER_SIZE) {
+			dev_err(adev->dev,
+				"RAS header invalid, tbl_size %u smaller than minimum %u, resetting table\n",
+				hdr->tbl_size, RAS_TABLE_HEADER_SIZE);
+			return amdgpu_ras_eeprom_reset_table(control);
+		}
 		control->ras_record_offset = RAS_RECORD_START;
 		control->ras_max_record_count = RAS_MAX_RECORD_COUNT;
+		control->ras_num_recs = RAS_NUM_RECS(hdr);
 		break;
 	default:
 		dev_err(adev->dev,
Signature · 1 line
2.25.1
author
#4 lore raw

PATCH 5/8 drm/amdgpu: NUL-terminate securedisplay debugfs input from userspace

In reply to Candice Li

Use strncpy_from_user() instead of copy_from_user() before sscanf() in the securedisplay_test debugfs write handler so a full-length write cannot leave the stack buffer without a terminator.

---
 .../gpu/drm/amd/amdgpu/amdgpu_securedisplay.c | 25 ++++++++++++-------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c +16 -9
index 3739be1b71e0d3..6c6d73804e34e2 100644
@@ -98,15 +98,17 @@ static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u
 	uint32_t phy_id;
 	uint32_t op;
 	char str[64];
-	int ret;
+	long len;
+	int ret, nargs;
 
-	if (*pos || size > sizeof(str) - 1)
+	if (*pos)
 		return -EINVAL;
 
-	memset(str,  0, sizeof(str));
-	ret = copy_from_user(str, buf, size);
-	if (ret)
+	len = strncpy_from_user(str, buf, sizeof(str));
+	if (len < 0)
 		return -EFAULT;
+	if (len == 0 || len >= sizeof(str))
+		return -EINVAL;
 
 	ret = pm_runtime_get_sync(dev->dev);
 	if (ret < 0) {
@@ -114,10 +116,14 @@ static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u
 		return ret;
 	}
 
-	if (size < 3)
-		sscanf(str, "%u ", &op);
+	if (len < 3)
+		nargs = sscanf(str, "%u", &op);
 	else
-		sscanf(str, "%u %u", &op, &phy_id);
+		nargs = sscanf(str, "%u %u", &op, &phy_id);
+	if (nargs < 1) {
+		pm_runtime_put_autosuspend(dev->dev);
+		return -EINVAL;
+	}
 
 	switch (op) {
 	case 1:
@@ -135,8 +141,9 @@ static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u
 		mutex_unlock(&psp->securedisplay_context.mutex);
 		break;
 	case 2:
-		if (size < 3 || phy_id >= TA_SECUREDISPLAY_MAX_PHY) {
+		if (len < 3 || nargs < 2 || phy_id >= TA_SECUREDISPLAY_MAX_PHY) {
 			dev_err(adev->dev, "Invalid input: %s\n", str);
+			pm_runtime_put_autosuspend(dev->dev);
 			return -EINVAL;
 		}
 		mutex_lock(&psp->securedisplay_context.mutex);
Signature · 1 line
2.25.1
author
#5 lore raw

PATCH 6/8 drm/amd/pm: use kcalloc in phm table copy helpers

In reply to Candice Li

Use kcalloc() so multiplication overflow is detected and allocation fails safely for phm table copy helpers.

---
 drivers/gpu/drm/amd/pm/powerplay/hwmgr/smu_helper.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/pm/powerplay/hwmgr/smu_helper.c +4 -6
index 5d454b18905ce8..3ee6f086d0c217 100644
@@ -49,11 +49,10 @@ int phm_copy_clock_limits_array(
 	const uint32_t *pptable_array,
 	uint32_t power_saving_clock_count)
 {
-	uint32_t array_size, i;
+	uint32_t i;
 	uint32_t *table;
 
-	array_size = sizeof(uint32_t) * power_saving_clock_count;
-	table = kzalloc(array_size, GFP_KERNEL);
+	table = kcalloc(power_saving_clock_count, sizeof(uint32_t), GFP_KERNEL);
 	if (NULL == table)
 		return -ENOMEM;
 
@@ -71,11 +70,10 @@ int phm_copy_overdrive_settings_limits_array(
 	const uint32_t *pptable_array,
 	uint32_t od_setting_count)
 {
-	uint32_t array_size, i;
+	uint32_t i;
 	uint32_t *table;
 
-	array_size = sizeof(uint32_t) * od_setting_count;
-	table = kzalloc(array_size, GFP_KERNEL);
+	table = kcalloc(od_setting_count, sizeof(uint32_t), GFP_KERNEL);
 	if (NULL == table)
 		return -ENOMEM;
 
Signature · 1 line
2.25.1
author
#6 lore raw

PATCH 7/8 drm/amd/pm: bound OD parameter parsing to stack array size

In reply to Candice Li

Reject inputs once parameter_size reaches the array limit, and pass ARRAY_SIZE(parameter) into parse_input_od_command_lines() for defense in depth.

---
 drivers/gpu/drm/amd/pm/amdgpu_pm.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/pm/amdgpu_pm.c +7 -0
index 8140bc77591c3e..60db9b66d08c20 100644
@@ -798,6 +798,8 @@ static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
 		if (strlen(sub_str) == 0)
 			continue;
+		if (parameter_size >= ARRAY_SIZE(parameter))
+			return -EINVAL;
 		ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
 		if (ret)
 			return -EINVAL;
@@ -3931,6 +3933,7 @@ static int parse_input_od_command_lines(const char *buf,
 					size_t count,
 					u32 *type,
 					long *params,
+					size_t params_max,
 					uint32_t *num_of_params)
 {
 	const char delimiter[3] = {' ', '\n', '\0'};
@@ -3966,6 +3969,9 @@ static int parse_input_od_command_lines(const char *buf,
 		if (strlen(sub_str) == 0)
 			continue;
 
+		if (parameter_size >= params_max)
+			return -EINVAL;
+
 		ret = kstrtol(sub_str, 0, &params[parameter_size]);
 		if (ret)
 			return -EINVAL;
@@ -3997,6 +4003,7 @@ amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
 					   count,
 					   &cmd_type,
 					   parameter,
+					   ARRAY_SIZE(parameter),
 					   &parameter_size);
 	if (ret)
 		return ret;
Signature · 1 line
2.25.1
author
#7 lore raw

PATCH 8/8 drm/amd/amdxcp: assert XCP platform device names

In reply to Candice Li

Replace the fixed stack buffer and static_assert with kasprintf() so platform device names are always fully formatted.

---
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c +7 -2
index 44009aa8216ed0..995cae6be144a0 100644
@@ -25,6 +25,7 @@
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/slab.h>
 
 #include <drm/drm_drv.h>
 
@@ -52,7 +53,7 @@ int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
 {
 	struct platform_device *pdev;
 	struct xcp_device *pxcp_dev;
-	char dev_name[20];
+	char *dev_name;
 	int ret, i;
 
 	guard(mutex)(&xcp_mutex);
@@ -68,8 +69,12 @@ int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
 	if (i >= MAX_XCP_PLATFORM_DEVICE)
 		return -ENODEV;
 
-	snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", i);
+	dev_name = kasprintf(GFP_KERNEL, "amdgpu_xcp_%d", i);
+	if (!dev_name)
+		return -ENOMEM;
+
 	pdev = platform_device_register_simple(dev_name, -1, NULL, 0);
+	kfree(dev_name);
 	if (IS_ERR(pdev))
 		return PTR_ERR(pdev);
 
Signature · 1 line
2.25.1
Reviewed-by on v1 8/8
#8 lore raw

RE: [PATCH 8/8] drm/amd/amdxcp: assert XCP platform device names

AMD General

Series is

Regards,
Hawking
-----Original Message-----
From: Li, Candice <[email protected]>
Sent: Thursday, May 28, 2026 12:34 PM
To: [email protected]
Cc: Zhang, Hawking <[email protected]>; Zhou1, Tao <[email protected]>; Yang, Stanley <[email protected]>; Chai, Thomas <[email protected]>; Li, Candice <[email protected]>
Subject: [PATCH 8/8] drm/amd/amdxcp: assert XCP platform device names

Replace the fixed stack buffer and static_assert with kasprintf() so platform device names are always fully formatted.

---
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c +7 -2
index 44009aa8216ed0..995cae6be144a0 100644
@@ -25,6 +25,7 @@
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/slab.h>
 
 #include <drm/drm_drv.h>
 
@@ -52,7 +53,7 @@ int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)  {
        struct platform_device *pdev;
        struct xcp_device *pxcp_dev;
-       char dev_name[20];
+       char *dev_name;
        int ret, i;
 
        guard(mutex)(&xcp_mutex);
@@ -68,8 +69,12 @@ int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
        if (i >= MAX_XCP_PLATFORM_DEVICE)
                return -ENODEV;
 
-       snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", i);
+       dev_name = kasprintf(GFP_KERNEL, "amdgpu_xcp_%d", i);
+       if (!dev_name)
+               return -ENOMEM;
+
        pdev = platform_device_register_simple(dev_name, -1, NULL, 0);
+       kfree(dev_name);
        if (IS_ERR(pdev))
                return PTR_ERR(pdev);
 

--
2.25.1