Skip to contentSkip to conversation

drm/amdgpu: guard zero divisors in soc_v1_0 partition code

PATCH In review Asad Kamal 2 participants 2 messages Jun 18

Subsystems (2)
lore
Share & tools
lore

Asad Kamal

author
#0 lore raw

PATCH v3 1/1 drm/amdgpu: guard zero divisors in soc_v1_0 partition code

Abort driver load when num_mem_partitions is zero since operation is unreliable without valid memory partition info. Skip absent resources in soc_v1_0_get_xcp_res_info() to avoid divide-by-zero on firmware- reported zero instance counts.

v2: Remove redundant checks (Lijo)
v3: Return error instead when num_mem_partitions is zero (Lijo)

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 9 +++++++--
 drivers/gpu/drm/amd/amdgpu/soc_v1_0.c   | 4 +++-
 2 files changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +7 -2
index 2b557c9096df..8bc347b75de0 100644
@@ -1762,10 +1762,15 @@ int amdgpu_gmc_init_mem_ranges(struct amdgpu_device *adev)
 		valid = true;
 	else
 		valid = amdgpu_gmc_validate_partition_info(adev);
-	if (!valid) {
-		/* TODO: handle invalid case */
+	if (!valid)
 		dev_warn(adev->dev,
 			 "Mem ranges not matching with hardware config\n");
+
+	if (!adev->gmc.num_mem_partitions) {
+		dev_err(adev->dev, "num_mem_partitions is zero\n");
+		kfree(adev->gmc.mem_partitions);
+		adev->gmc.mem_partitions = NULL;
+		return -EINVAL;
 	}
 
 	return 0;
drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +3 -1
index 5f05c8e68297..f3f3fac435d1 100644
@@ -600,8 +600,10 @@ static int soc_v1_0_get_xcp_res_info(struct amdgpu_xcp_mgr *xcp_mgr,
 	xcp_cfg->num_res = ARRAY_SIZE(max_res);
 
 	for (i = 0; i < xcp_cfg->num_res; i++) {
-		res_lt_xcp = max_res[i] < num_xcp;
 		xcp_cfg->xcp_res[i].id = i;
+		if (!max_res[i])
+			continue;
+		res_lt_xcp = max_res[i] < num_xcp;
 		xcp_cfg->xcp_res[i].num_inst =
 			res_lt_xcp ? 1 : max_res[i] / num_xcp;
 		xcp_cfg->xcp_res[i].num_inst =
Signature · 1 line
2.46.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 9 lines · Asad Kamal

Abort driver load when num_mem_partitions is zero since operation is unreliable without valid memory partition info. Skip absent resources in soc_v1_0_get_xcp_res_info() to avoid divide-by-zero on firmware- reported zero instance counts.

v2: Remove redundant checks (Lijo)
v3: Return error instead when num_mem_partitions is zero (Lijo)

Thanks,
Lijo

Quoted 43 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 9 +++++++--
  drivers/gpu/drm/amd/amdgpu/soc_v1_0.c   | 4 +++-
  2 files changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +7 -2
index 2b557c9096df..8bc347b75de0 100644
@@ -1762,10 +1762,15 @@ int amdgpu_gmc_init_mem_ranges(struct amdgpu_device *adev)
  		valid = true;
  	else
  		valid = amdgpu_gmc_validate_partition_info(adev);
-	if (!valid) {
-		/* TODO: handle invalid case */
+	if (!valid)
  		dev_warn(adev->dev,
  			 "Mem ranges not matching with hardware config\n");
+
+	if (!adev->gmc.num_mem_partitions) {
+		dev_err(adev->dev, "num_mem_partitions is zero\n");
+		kfree(adev->gmc.mem_partitions);
+		adev->gmc.mem_partitions = NULL;
+		return -EINVAL;
  	}
 
  	return 0;
drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +3 -1
index 5f05c8e68297..f3f3fac435d1 100644
@@ -600,8 +600,10 @@ static int soc_v1_0_get_xcp_res_info(struct amdgpu_xcp_mgr *xcp_mgr,
  	xcp_cfg->num_res = ARRAY_SIZE(max_res);
 
  	for (i = 0; i < xcp_cfg->num_res; i++) {
-		res_lt_xcp = max_res[i] < num_xcp;
  		xcp_cfg->xcp_res[i].id = i;
+		if (!max_res[i])
+			continue;
+		res_lt_xcp = max_res[i] < num_xcp;
  		xcp_cfg->xcp_res[i].num_inst =
  			res_lt_xcp ? 1 : max_res[i] / num_xcp;
  		xcp_cfg->xcp_res[i].num_inst =