Skip to contentSkip to conversation

drm/amd/pm: Handle truncation in common clk/pcie printers

PATCH In review Asad Kamal 2 participants 7 messages May 29

Subsystems (3)
lore
Share & tools
lore

Asad Kamal

author
#0 lore raw

PATCH 1/4 drm/amd/pm: Handle truncation in common clk/pcie printers

In smu_cmn_print_dpm_clk_levels() and smu_cmn_print_pcie_levels(), use the sysfs_emit_at() return value, break out of per-level loops when n == 0, and use a shared out label before updating offset.

---
 drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c | 77 +++++++++++++++-----------
 1 file changed, 44 insertions(+), 33 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +44 -33
index 546e64e3ba9c..0a745afa8552 100644
@@ -1384,7 +1384,7 @@ int smu_cmn_print_dpm_clk_levels(struct smu_context *smu,
 {
 	uint32_t min_clk, max_clk, level_index, count;
 	uint32_t freq_values[3];
-	int size, lvl, i;
+	int size, lvl, i, n;
 	bool is_fine_grained;
 	bool is_deep_sleep;
 	bool freq_match;
@@ -1402,7 +1402,10 @@ int smu_cmn_print_dpm_clk_levels(struct smu_context *smu,
 	/* Deep sleep - current clock < min_clock/2, TBD: cur_clk = 0 as GFXOFF */
 	is_deep_sleep = cur_clk < min_clk / 2;
 	if (is_deep_sleep) {
-		size += sysfs_emit_at(buf, size, "S: %uMhz *\n", cur_clk);
+		n = sysfs_emit_at(buf, size, "S: %uMhz *\n", cur_clk);
+		if (!n)
+			goto out;
+		size += n;
 		level_index = 1;
 	}
 
@@ -1412,10 +1415,13 @@ int smu_cmn_print_dpm_clk_levels(struct smu_context *smu,
 				     smu_cmn_freqs_match(
 					     cur_clk,
 					     dpm_table->dpm_levels[i].value);
-			size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
-					      level_index + i,
-					      dpm_table->dpm_levels[i].value,
-					      freq_match ? "*" : "");
+			n = sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
+					  level_index + i,
+					  dpm_table->dpm_levels[i].value,
+					  freq_match ? "*" : "");
+			if (!n)
+				break;
+			size += n;
 		}
 	} else {
 		count = 2;
@@ -1437,13 +1443,16 @@ int smu_cmn_print_dpm_clk_levels(struct smu_context *smu,
 		}
 
 		for (i = 0; i < count; i++) {
-			size += sysfs_emit_at(
-				buf, size, "%d: %uMhz %s\n", level_index + i,
-				freq_values[i],
-				(!is_deep_sleep && i == lvl) ? "*" : "");
+			n = sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
+					  level_index + i, freq_values[i],
+					  (!is_deep_sleep && i == lvl) ? "*" : "");
+			if (!n)
+				break;
+			size += n;
 		}
 	}
 
+out:
 	*offset = size;
 
 	return 0;
@@ -1454,7 +1463,7 @@ int smu_cmn_print_pcie_levels(struct smu_context *smu,
 			      uint32_t cur_gen, uint32_t cur_lane, char *buf,
 			      int *offset)
 {
-	int size, i;
+	int size, i, n;
 
 	if (!pcie_table || !buf)
 		return -EINVAL;
@@ -1462,28 +1471,30 @@ int smu_cmn_print_pcie_levels(struct smu_context *smu,
 	size = *offset;
 
 	for (i = 0; i < pcie_table->lclk_levels; i++) {
-		size += sysfs_emit_at(
-			buf, size, "%d: %s %s %dMhz %s\n", i,
-			(pcie_table->pcie_gen[i] == 0) ? "2.5GT/s," :
-			(pcie_table->pcie_gen[i] == 1) ? "5.0GT/s," :
-			(pcie_table->pcie_gen[i] == 2) ? "8.0GT/s," :
-			(pcie_table->pcie_gen[i] == 3) ? "16.0GT/s," :
-			(pcie_table->pcie_gen[i] == 4) ? "32.0GT/s," :
-			(pcie_table->pcie_gen[i] == 5) ? "64.0GT/s," :
-							 "",
-			(pcie_table->pcie_lane[i] == 1) ? "x1" :
-			(pcie_table->pcie_lane[i] == 2) ? "x2" :
-			(pcie_table->pcie_lane[i] == 3) ? "x4" :
-			(pcie_table->pcie_lane[i] == 4) ? "x8" :
-			(pcie_table->pcie_lane[i] == 5) ? "x12" :
-			(pcie_table->pcie_lane[i] == 6) ? "x16" :
-			(pcie_table->pcie_lane[i] == 7) ? "x32" :
-							  "",
-			pcie_table->lclk_freq[i],
-			(cur_gen == pcie_table->pcie_gen[i]) &&
-					(cur_lane == pcie_table->pcie_lane[i]) ?
-				"*" :
-				"");
+		n = sysfs_emit_at(buf, size, "%d: %s %s %dMhz %s\n", i,
+				  (pcie_table->pcie_gen[i] == 0) ? "2.5GT/s," :
+				  (pcie_table->pcie_gen[i] == 1) ? "5.0GT/s," :
+				  (pcie_table->pcie_gen[i] == 2) ? "8.0GT/s," :
+				  (pcie_table->pcie_gen[i] == 3) ? "16.0GT/s," :
+				  (pcie_table->pcie_gen[i] == 4) ? "32.0GT/s," :
+				  (pcie_table->pcie_gen[i] == 5) ? "64.0GT/s," :
+				  "",
+				  (pcie_table->pcie_lane[i] == 1) ? "x1" :
+				  (pcie_table->pcie_lane[i] == 2) ? "x2" :
+				  (pcie_table->pcie_lane[i] == 3) ? "x4" :
+				  (pcie_table->pcie_lane[i] == 4) ? "x8" :
+				  (pcie_table->pcie_lane[i] == 5) ? "x12" :
+				  (pcie_table->pcie_lane[i] == 6) ? "x16" :
+				  (pcie_table->pcie_lane[i] == 7) ? "x32" :
+				  "",
+				  pcie_table->lclk_freq[i],
+				  (cur_gen == pcie_table->pcie_gen[i]) &&
+				  (cur_lane == pcie_table->pcie_lane[i]) ?
+				  "*" :
+				  "");
+		if (!n)
+			break;
+		size += n;
 	}
 
 	*offset = size;
Signature · 1 line
2.46.0
author
#1 lore raw

PATCH 2/4 drm/amd/pm: Handle sysfs_emit truncation for smu15

In smu_v15_0_0_emit_clk_levels() and smu_v15_0_8_emit_clk_levels(), check each sysfs_emit_at() return value and stop further output when it returns zero (page full).

---
 .../drm/amd/pm/swsmu/smu15/smu_v15_0_0_ppt.c  | 71 +++++++++++++------
 .../drm/amd/pm/swsmu/smu15/smu_v15_0_8_ppt.c  | 31 +++++---
 2 files changed, 72 insertions(+), 30 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu15/smu_v15_0_0_ppt.c +50 -21
index 4e7d6a602c6c..977ed70b8689 100644
@@ -995,23 +995,40 @@ static int smu_v15_0_0_emit_clk_levels(struct smu_context *smu,
 				       enum smu_clk_type clk_type, char *buf,
 				       int *offset)
 {
-	int i, idx, ret = 0, size = *offset;
+	int i, idx, ret = 0, size = *offset, n;
 	uint32_t cur_value = 0, value = 0, count = 0;
 	uint32_t min, max;
 
 	switch (clk_type) {
 	case SMU_OD_SCLK:
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_SCLK");
-		size += sysfs_emit_at(buf, size, "0: %10uMhz\n",
-		(smu->gfx_actual_hard_min_freq > 0) ? smu->gfx_actual_hard_min_freq : smu->gfx_default_hard_min_freq);
-		size += sysfs_emit_at(buf, size, "1: %10uMhz\n",
-		(smu->gfx_actual_soft_max_freq > 0) ? smu->gfx_actual_soft_max_freq : smu->gfx_default_soft_max_freq);
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_SCLK");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "0: %10uMhz\n",
+				  (smu->gfx_actual_hard_min_freq > 0) ?
+				  smu->gfx_actual_hard_min_freq : smu->gfx_default_hard_min_freq);
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "1: %10uMhz\n",
+				  (smu->gfx_actual_soft_max_freq > 0) ?
+				  smu->gfx_actual_soft_max_freq : smu->gfx_default_soft_max_freq);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 	case SMU_OD_RANGE:
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
-		size += sysfs_emit_at(buf, size, "SCLK: %7uMhz %10uMhz\n",
-				      smu->gfx_default_hard_min_freq,
-				      smu->gfx_default_soft_max_freq);
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "SCLK: %7uMhz %10uMhz\n",
+				  smu->gfx_default_hard_min_freq,
+				  smu->gfx_default_soft_max_freq);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 	case SMU_SOCCLK:
 	case SMU_VCLK:
@@ -1034,9 +1051,12 @@ static int smu_v15_0_0_emit_clk_levels(struct smu_context *smu,
 			if (ret)
 				return ret;
 
-			size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
-					      value,
-					      cur_value == value ? "*" : "");
+			n = sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
+					  value,
+					  cur_value == value ? "*" : "");
+			if (!n)
+				goto out;
+			size += n;
 		}
 		break;
 	case SMU_GFXCLK:
@@ -1052,14 +1072,22 @@ static int smu_v15_0_0_emit_clk_levels(struct smu_context *smu,
 			i = 0;
 		else
 			i = 1;
-		size += sysfs_emit_at(buf, size, "0: %uMhz %s\n", min,
-				      i == 0 ? "*" : "");
-		size += sysfs_emit_at(
-			buf, size, "1: %uMhz %s\n",
-			i == 1 ? cur_value : 1100, /* UMD PSTATE GFXCLK 1100 */
-			i == 1 ? "*" : "");
-		size += sysfs_emit_at(buf, size, "2: %uMhz %s\n", max,
-				      i == 2 ? "*" : "");
+		n = sysfs_emit_at(buf, size, "0: %uMhz %s\n", min,
+				  i == 0 ? "*" : "");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "1: %uMhz %s\n",
+				  i == 1 ? cur_value : 1100, /* UMD PSTATE GFXCLK 1100 */
+				  i == 1 ? "*" : "");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "2: %uMhz %s\n", max,
+				  i == 2 ? "*" : "");
+		if (!n)
+			goto out;
+		size += n;
 		break;
 	default:
 		break;
@@ -1067,6 +1095,7 @@ static int smu_v15_0_0_emit_clk_levels(struct smu_context *smu,
 
 	*offset = size;
 
+out:
 	return 0;
 }
 
drivers/gpu/drm/amd/pm/swsmu/smu15/smu_v15_0_8_ppt.c +22 -9
index 2105a1d7bb34..e076ad68b66b 100644
@@ -692,7 +692,7 @@ static int smu_v15_0_8_emit_clk_levels(struct smu_context *smu,
 	struct smu_15_0_dpm_context *dpm_context;
 	struct smu_dpm_table *single_dpm_table = NULL;
 	struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
-	int ret, now, size = *offset;
+	int ret, now, size = *offset, n;
 
 	if (amdgpu_ras_intr_triggered()) {
 		sysfs_emit_at(buf, size, "unavailable\n");
@@ -703,16 +703,28 @@ static int smu_v15_0_8_emit_clk_levels(struct smu_context *smu,
 
 	switch (type) {
 	case SMU_OD_SCLK:
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_SCLK");
-		size += sysfs_emit_at(buf, size, "0: %uMhz\n1: %uMhz\n",
-				      pstate_table->gfxclk_pstate.curr.min,
-				      pstate_table->gfxclk_pstate.curr.max);
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_SCLK");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "0: %uMhz\n1: %uMhz\n",
+				  pstate_table->gfxclk_pstate.curr.min,
+				  pstate_table->gfxclk_pstate.curr.max);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 	case SMU_OD_MCLK:
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_MCLK");
-		size += sysfs_emit_at(buf, size, "0: %uMhz\n1: %uMhz\n",
-				      pstate_table->uclk_pstate.curr.min,
-				      pstate_table->uclk_pstate.curr.max);
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_MCLK");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "0: %uMhz\n1: %uMhz\n",
+				  pstate_table->uclk_pstate.curr.min,
+				  pstate_table->uclk_pstate.curr.max);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 	case SMU_SCLK:
 	case SMU_GFXCLK:
@@ -738,6 +750,7 @@ static int smu_v15_0_8_emit_clk_levels(struct smu_context *smu,
 		break;
 	}
 
+out:
 	if (single_dpm_table) {
 		ret = smu_v15_0_8_get_current_clk_freq_by_table(smu, type, &now);
 		if (ret) {
Signature · 1 line
2.46.0
author
#2 lore raw

PATCH 3/4 drm/amd/pm: Handle sysfs_emit truncation for smu13

In reply to Asad Kamal

Check sysfs_emit_at() in emit_clk_levels() for smu_v13_0_4, smu_v13_0_5, yellow_carp, aldebaran, and smu_v13_0_6 (OD sysfs and OD_FCLK on v13_0_6) and stop emitting when the sysfs page is full

---
 .../drm/amd/pm/swsmu/smu13/aldebaran_ppt.c    | 36 +++++++---
 .../drm/amd/pm/swsmu/smu13/smu_v13_0_4_ppt.c  | 68 +++++++++++++------
 .../drm/amd/pm/swsmu/smu13/smu_v13_0_5_ppt.c  | 66 +++++++++++++-----
 .../drm/amd/pm/swsmu/smu13/smu_v13_0_6_ppt.c  | 45 ++++++++----
 .../drm/amd/pm/swsmu/smu13/yellow_carp_ppt.c  | 66 +++++++++++++-----
 5 files changed, 203 insertions(+), 78 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c +26 -10
index 3a19f0ffcf3c..2d4c557c04e9 100644
@@ -791,7 +791,7 @@ static int aldebaran_get_current_clk_freq_by_table(struct smu_context *smu,
 static int aldebaran_emit_clk_levels(struct smu_context *smu,
 				     enum smu_clk_type type, char *buf, int *offset)
 {
-	int ret = 0;
+	int ret = 0, n;
 	struct smu_umd_pstate_table *pstate_table = &smu->pstate_table;
 	struct smu_dpm_table *single_dpm_table = NULL;
 	struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
@@ -800,7 +800,10 @@ static int aldebaran_emit_clk_levels(struct smu_context *smu,
 	static const char attempt_string[] = "Attempt to get current";
 
 	if (amdgpu_ras_intr_triggered()) {
-		*offset += sysfs_emit_at(buf, *offset, "unavailable\n");
+		int unr = sysfs_emit_at(buf, *offset, "unavailable\n");
+
+		if (unr)
+			*offset += unr;
 		return -EBUSY;
 	}
 
@@ -808,16 +811,28 @@ static int aldebaran_emit_clk_levels(struct smu_context *smu,
 
 	switch (type) {
 	case SMU_OD_SCLK:
-		*offset += sysfs_emit_at(buf, *offset, "%s:\n", "OD_SCLK");
-		*offset += sysfs_emit_at(buf, *offset, "0: %uMhz\n1: %uMhz\n",
-					 pstate_table->gfxclk_pstate.curr.min,
-					 pstate_table->gfxclk_pstate.curr.max);
+		n = sysfs_emit_at(buf, *offset, "%s:\n", "OD_SCLK");
+		if (!n)
+			goto out;
+		*offset += n;
+		n = sysfs_emit_at(buf, *offset, "0: %uMhz\n1: %uMhz\n",
+				  pstate_table->gfxclk_pstate.curr.min,
+				  pstate_table->gfxclk_pstate.curr.max);
+		if (!n)
+			goto out;
+		*offset += n;
 		return 0;
 	case SMU_OD_MCLK:
-		*offset += sysfs_emit_at(buf, *offset, "%s:\n", "OD_MCLK");
-		*offset += sysfs_emit_at(buf, *offset, "0: %uMhz\n1: %uMhz\n",
-					 pstate_table->uclk_pstate.curr.min,
-					 pstate_table->uclk_pstate.curr.max);
+		n = sysfs_emit_at(buf, *offset, "%s:\n", "OD_MCLK");
+		if (!n)
+			goto out;
+		*offset += n;
+		n = sysfs_emit_at(buf, *offset, "0: %uMhz\n1: %uMhz\n",
+				  pstate_table->uclk_pstate.curr.min,
+				  pstate_table->uclk_pstate.curr.max);
+		if (!n)
+			goto out;
+		*offset += n;
 		return 0;
 
 	case SMU_SCLK:
@@ -842,6 +857,7 @@ static int aldebaran_emit_clk_levels(struct smu_context *smu,
 		return -EINVAL;
 	}
 
+out:
 	if (single_dpm_table) {
 		ret = aldebaran_get_current_clk_freq_by_table(smu, type,
 							      &cur_value);
drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_4_ppt.c +49 -19

98 lines not on this page · raw message

drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_5_ppt.c +48 -18

96 lines not on this page · raw message

drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_6_ppt.c +32 -13

73 lines not on this page · raw message

drivers/gpu/drm/amd/pm/swsmu/smu13/yellow_carp_ppt.c +48 -18

97 lines not on this page · raw message

Signature · 1 line
2.46.0
author
#3 lore raw

PATCH 4/4 drm/amd/pm: Handle sysfs_emit truncation for smu13 OD

In reply to Asad Kamal

Check sysfs_emit_at() in smu_v13_0_0_emit_clk_levels() and smu_v13_0_7_emit_clk_levels() and stop emitting when the sysfs page is full

---
 .../drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c  | 263 +++++++++++++-----
 .../drm/amd/pm/swsmu/smu13/smu_v13_0_7_ppt.c  | 251 ++++++++++++-----
 2 files changed, 376 insertions(+), 138 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c +191 -72
index 30d9cfac0d89..fa91837f7024 100644
@@ -1206,7 +1206,7 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 	struct smu_13_0_dpm_context *dpm_context = smu_dpm->dpm_context;
 	OverDriveTableExternal_t *od_table =
 		(OverDriveTableExternal_t *)smu->smu_table.overdrive_table;
-	int i, curr_freq, size = *offset, start_offset = *offset;
+	int i, curr_freq, size = *offset, start_offset = *offset, n;
 	struct smu_dpm_table *single_dpm_table = NULL;
 	struct smu_pcie_table *pcie_table;
 	uint32_t gen_speed, lane_width;
@@ -1265,10 +1265,16 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							 PP_OD_FEATURE_GFXCLK_BIT))
 			break;
 
-		size += sysfs_emit_at(buf, size, "OD_SCLK:\n");
-		size += sysfs_emit_at(buf, size, "0: %uMhz\n1: %uMhz\n",
-					od_table->OverDriveTable.GfxclkFmin,
-					od_table->OverDriveTable.GfxclkFmax);
+		n = sysfs_emit_at(buf, size, "OD_SCLK:\n");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "0: %uMhz\n1: %uMhz\n",
+				  od_table->OverDriveTable.GfxclkFmin,
+				  od_table->OverDriveTable.GfxclkFmax);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 
 	case SMU_OD_MCLK:
@@ -1276,10 +1282,16 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							 PP_OD_FEATURE_UCLK_BIT))
 			break;
 
-		size += sysfs_emit_at(buf, size, "OD_MCLK:\n");
-		size += sysfs_emit_at(buf, size, "0: %uMhz\n1: %uMHz\n",
-					od_table->OverDriveTable.UclkFmin,
-					od_table->OverDriveTable.UclkFmax);
+		n = sysfs_emit_at(buf, size, "OD_MCLK:\n");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "0: %uMhz\n1: %uMHz\n",
+				  od_table->OverDriveTable.UclkFmin,
+				  od_table->OverDriveTable.UclkFmax);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 
 	case SMU_OD_VDDGFX_OFFSET:
@@ -1287,9 +1299,15 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							 PP_OD_FEATURE_GFX_VF_CURVE_BIT))
 			break;
 
-		size += sysfs_emit_at(buf, size, "OD_VDDGFX_OFFSET:\n");
-		size += sysfs_emit_at(buf, size, "%dmV\n",
-				      od_table->OverDriveTable.VoltageOffsetPerZoneBoundary[0]);
+		n = sysfs_emit_at(buf, size, "OD_VDDGFX_OFFSET:\n");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "%dmV\n",
+				  od_table->OverDriveTable.VoltageOffsetPerZoneBoundary[0]);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 
 	case SMU_OD_FAN_CURVE:
@@ -1297,27 +1315,43 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							 PP_OD_FEATURE_FAN_CURVE_BIT))
 			break;
 
-		size += sysfs_emit_at(buf, size, "OD_FAN_CURVE:\n");
-		for (i = 0; i < NUM_OD_FAN_MAX_POINTS - 1; i++)
-			size += sysfs_emit_at(buf, size, "%d: %dC %d%%\n",
-						i,
-						(int)od_table->OverDriveTable.FanLinearTempPoints[i],
-						(int)od_table->OverDriveTable.FanLinearPwmPoints[i]);
+		n = sysfs_emit_at(buf, size, "OD_FAN_CURVE:\n");
+		if (!n)
+			goto out;
+		size += n;
+		for (i = 0; i < NUM_OD_FAN_MAX_POINTS - 1; i++) {
+			n = sysfs_emit_at(buf, size, "%d: %dC %d%%\n",
+					  i,
+					  (int)od_table->OverDriveTable.FanLinearTempPoints[i],
+					  (int)od_table->OverDriveTable.FanLinearPwmPoints[i]);
+			if (!n)
+				goto out;
+			size += n;
+		}
 
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		if (!n)
+			goto out;
+		size += n;
 		smu_v13_0_0_get_od_setting_limits(smu,
 						  PP_OD_FEATURE_FAN_CURVE_TEMP,
 						  &min_value,
 						  &max_value);
-		size += sysfs_emit_at(buf, size, "FAN_CURVE(hotspot temp): %uC %uC\n",
-				      min_value, max_value);
+		n = sysfs_emit_at(buf, size, "FAN_CURVE(hotspot temp): %uC %uC\n",
+				  min_value, max_value);
+		if (!n)
+			goto out;
+		size += n;
 
 		smu_v13_0_0_get_od_setting_limits(smu,
 						  PP_OD_FEATURE_FAN_CURVE_PWM,
 						  &min_value,
 						  &max_value);
-		size += sysfs_emit_at(buf, size, "FAN_CURVE(fan speed): %u%% %u%%\n",
-				      min_value, max_value);
+		n = sysfs_emit_at(buf, size, "FAN_CURVE(fan speed): %u%% %u%%\n",
+				  min_value, max_value);
+		if (!n)
+			goto out;
+		size += n;
 
 		break;
 
@@ -1326,17 +1360,29 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							 PP_OD_FEATURE_FAN_CURVE_BIT))
 			break;
 
-		size += sysfs_emit_at(buf, size, "OD_ACOUSTIC_LIMIT:\n");
-		size += sysfs_emit_at(buf, size, "%d\n",
-					(int)od_table->OverDriveTable.AcousticLimitRpmThreshold);
-
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		n = sysfs_emit_at(buf, size, "OD_ACOUSTIC_LIMIT:\n");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "%d\n",
+				  (int)od_table->OverDriveTable.AcousticLimitRpmThreshold);
+		if (!n)
+			goto out;
+		size += n;
+
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		if (!n)
+			goto out;
+		size += n;
 		smu_v13_0_0_get_od_setting_limits(smu,
 						  PP_OD_FEATURE_FAN_ACOUSTIC_LIMIT,
 						  &min_value,
 						  &max_value);
-		size += sysfs_emit_at(buf, size, "ACOUSTIC_LIMIT: %u %u\n",
-				      min_value, max_value);
+		n = sysfs_emit_at(buf, size, "ACOUSTIC_LIMIT: %u %u\n",
+				  min_value, max_value);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 
 	case SMU_OD_ACOUSTIC_TARGET:
@@ -1344,17 +1390,29 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							 PP_OD_FEATURE_FAN_CURVE_BIT))
 			break;
 
-		size += sysfs_emit_at(buf, size, "OD_ACOUSTIC_TARGET:\n");
-		size += sysfs_emit_at(buf, size, "%d\n",
-					(int)od_table->OverDriveTable.AcousticTargetRpmThreshold);
-
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		n = sysfs_emit_at(buf, size, "OD_ACOUSTIC_TARGET:\n");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "%d\n",
+				  (int)od_table->OverDriveTable.AcousticTargetRpmThreshold);
+		if (!n)
+			goto out;
+		size += n;
+
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		if (!n)
+			goto out;
+		size += n;
 		smu_v13_0_0_get_od_setting_limits(smu,
 						  PP_OD_FEATURE_FAN_ACOUSTIC_TARGET,
 						  &min_value,
 						  &max_value);
-		size += sysfs_emit_at(buf, size, "ACOUSTIC_TARGET: %u %u\n",
-				      min_value, max_value);
+		n = sysfs_emit_at(buf, size, "ACOUSTIC_TARGET: %u %u\n",
+				  min_value, max_value);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 
 	case SMU_OD_FAN_TARGET_TEMPERATURE:
@@ -1362,17 +1420,29 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							 PP_OD_FEATURE_FAN_CURVE_BIT))
 			break;
 
-		size += sysfs_emit_at(buf, size, "FAN_TARGET_TEMPERATURE:\n");
-		size += sysfs_emit_at(buf, size, "%d\n",
-					(int)od_table->OverDriveTable.FanTargetTemperature);
-
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		n = sysfs_emit_at(buf, size, "FAN_TARGET_TEMPERATURE:\n");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "%d\n",
+				  (int)od_table->OverDriveTable.FanTargetTemperature);
+		if (!n)
+			goto out;
+		size += n;
+
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		if (!n)
+			goto out;
+		size += n;
 		smu_v13_0_0_get_od_setting_limits(smu,
 						  PP_OD_FEATURE_FAN_TARGET_TEMPERATURE,
 						  &min_value,
 						  &max_value);
-		size += sysfs_emit_at(buf, size, "TARGET_TEMPERATURE: %u %u\n",
-				      min_value, max_value);
+		n = sysfs_emit_at(buf, size, "TARGET_TEMPERATURE: %u %u\n",
+				  min_value, max_value);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 
 	case SMU_OD_FAN_MINIMUM_PWM:
@@ -1380,17 +1450,29 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							 PP_OD_FEATURE_FAN_CURVE_BIT))
 			break;
 
-		size += sysfs_emit_at(buf, size, "FAN_MINIMUM_PWM:\n");
-		size += sysfs_emit_at(buf, size, "%d\n",
-					(int)od_table->OverDriveTable.FanMinimumPwm);
-
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		n = sysfs_emit_at(buf, size, "FAN_MINIMUM_PWM:\n");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "%d\n",
+				  (int)od_table->OverDriveTable.FanMinimumPwm);
+		if (!n)
+			goto out;
+		size += n;
+
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		if (!n)
+			goto out;
+		size += n;
 		smu_v13_0_0_get_od_setting_limits(smu,
 						  PP_OD_FEATURE_FAN_MINIMUM_PWM,
 						  &min_value,
 						  &max_value);
-		size += sysfs_emit_at(buf, size, "MINIMUM_PWM: %u %u\n",
-				      min_value, max_value);
+		n = sysfs_emit_at(buf, size, "MINIMUM_PWM: %u %u\n",
+				  min_value, max_value);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 
 	case SMU_OD_FAN_ZERO_RPM_ENABLE:
@@ -1398,17 +1480,29 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							 PP_OD_FEATURE_ZERO_FAN_BIT))
 			break;
 
-		size += sysfs_emit_at(buf, size, "FAN_ZERO_RPM_ENABLE:\n");
-		size += sysfs_emit_at(buf, size, "%d\n",
-					(int)od_table->OverDriveTable.FanZeroRpmEnable);
-
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		n = sysfs_emit_at(buf, size, "FAN_ZERO_RPM_ENABLE:\n");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "%d\n",
+				  (int)od_table->OverDriveTable.FanZeroRpmEnable);
+		if (!n)
+			goto out;
+		size += n;
+
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		if (!n)
+			goto out;
+		size += n;
 		smu_v13_0_0_get_od_setting_limits(smu,
 						  PP_OD_FEATURE_FAN_ZERO_RPM_ENABLE,
 						  &min_value,
 						  &max_value);
-		size += sysfs_emit_at(buf, size, "ZERO_RPM_ENABLE: %u %u\n",
-				      min_value, max_value);
+		n = sysfs_emit_at(buf, size, "ZERO_RPM_ENABLE: %u %u\n",
+				  min_value, max_value);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 
 	case SMU_OD_FAN_ZERO_RPM_STOP_TEMP:
@@ -1416,17 +1510,29 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							 PP_OD_FEATURE_ZERO_FAN_BIT))
 			break;
 
-		size += sysfs_emit_at(buf, size, "FAN_ZERO_RPM_STOP_TEMPERATURE:\n");
-		size += sysfs_emit_at(buf, size, "%d\n",
-					(int)od_table->OverDriveTable.FanZeroRpmStopTemp);
-
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		n = sysfs_emit_at(buf, size, "FAN_ZERO_RPM_STOP_TEMPERATURE:\n");
+		if (!n)
+			goto out;
+		size += n;
+		n = sysfs_emit_at(buf, size, "%d\n",
+				  (int)od_table->OverDriveTable.FanZeroRpmStopTemp);
+		if (!n)
+			goto out;
+		size += n;
+
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		if (!n)
+			goto out;
+		size += n;
 		smu_v13_0_0_get_od_setting_limits(smu,
 						  PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP,
 						  &min_value,
 						  &max_value);
-		size += sysfs_emit_at(buf, size, "ZERO_RPM_STOP_TEMPERATURE: %u %u\n",
-				      min_value, max_value);
+		n = sysfs_emit_at(buf, size, "ZERO_RPM_STOP_TEMPERATURE: %u %u\n",
+				  min_value, max_value);
+		if (!n)
+			goto out;
+		size += n;
 		break;
 
 	case SMU_OD_RANGE:
@@ -1435,7 +1541,10 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 		    !smu_v13_0_0_is_od_feature_supported(smu, PP_OD_FEATURE_GFX_VF_CURVE_BIT))
 			break;
 
-		size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		n = sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
+		if (!n)
+			goto out;
+		size += n;
 
 		if (smu_v13_0_0_is_od_feature_supported(smu, PP_OD_FEATURE_GFXCLK_BIT)) {
 			smu_v13_0_0_get_od_setting_limits(smu,
@@ -1446,8 +1555,11 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							  PP_OD_FEATURE_GFXCLK_FMAX,
 							  NULL,
 							  &max_value);
-			size += sysfs_emit_at(buf, size, "SCLK: %7uMhz %10uMhz\n",
-					      min_value, max_value);
+			n = sysfs_emit_at(buf, size, "SCLK: %7uMhz %10uMhz\n",
+					  min_value, max_value);
+			if (!n)
+				goto out;
+			size += n;
 		}
 
 		if (smu_v13_0_0_is_od_feature_supported(smu, PP_OD_FEATURE_UCLK_BIT)) {
@@ -1459,8 +1571,11 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							  PP_OD_FEATURE_UCLK_FMAX,
 							  NULL,
 							  &max_value);
-			size += sysfs_emit_at(buf, size, "MCLK: %7uMhz %10uMhz\n",
-					      min_value, max_value);
+			n = sysfs_emit_at(buf, size, "MCLK: %7uMhz %10uMhz\n",
+					  min_value, max_value);
+			if (!n)
+				goto out;
+			size += n;
 		}
 
 		if (smu_v13_0_0_is_od_feature_supported(smu, PP_OD_FEATURE_GFX_VF_CURVE_BIT)) {
@@ -1468,8 +1583,11 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 							  PP_OD_FEATURE_GFX_VF_CURVE,
 							  &min_value,
 							  &max_value);
-			size += sysfs_emit_at(buf, size, "VDDGFX_OFFSET: %7dmv %10dmv\n",
-					      min_value, max_value);
+			n = sysfs_emit_at(buf, size, "VDDGFX_OFFSET: %7dmv %10dmv\n",
+					  min_value, max_value);
+			if (!n)
+				goto out;
+			size += n;
 		}
 		break;
 
@@ -1489,6 +1607,7 @@ static int smu_v13_0_0_emit_clk_levels(struct smu_context *smu,
 						    curr_freq, buf, offset);
 	}
 
+out:
 	*offset += size - start_offset;
 
 	return 0;
drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_7_ppt.c +185 -66

387 lines not on this page · raw message

Signature · 1 line
2.46.0
#4 lore raw
In reply to Asad Kamal

AMD General

The "len += sysfs_emit_at(...)" is a common practice in the Linux kernel.
Its return value has no adverse impact on subsequent calls, so repeatedly checking the return value serves no practical purpose. You may refer to other usages of sysfs_emit_at() in the kernel source; none of the existing examples check its return value. (this may be a widely adopted convention).

Cc @Koenig, Christian

Best Regards,
Kevin

Quoted 153 lines

-----Original Message-----
From: Kamal, Asad <[email protected]>
Sent: Friday, May 29, 2026 14:19
To: [email protected]
Cc: Lazar, Lijo <[email protected]>; Zhang, Hawking <[email protected]>; Ma, Le <[email protected]>; Zhang, Morris <[email protected]>; Deucher, Alexander <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Kamal, Asad <[email protected]>
Subject: [PATCH 1/4] drm/amd/pm: Handle truncation in common clk/pcie printers

In smu_cmn_print_dpm_clk_levels() and smu_cmn_print_pcie_levels(), use the sysfs_emit_at() return value, break out of per-level loops when n == 0, and use a shared out label before updating offset.

---
 drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c | 77 +++++++++++++++-----------
 1 file changed, 44 insertions(+), 33 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +12 -6
index 546e64e3ba9c..0a745afa8552 100644
@@ -1384,7 +1384,7 @@ int smu_cmn_print_dpm_clk_levels(struct smu_context
 *smu,  {
      uint32_t min_clk, max_clk, level_index, count;
      uint32_t freq_values[3];
-     int size, lvl, i;
+     int size, lvl, i, n;
      bool is_fine_grained;
      bool is_deep_sleep;
      bool freq_match;
@@ -1402,7 +1402,10 @@ int smu_cmn_print_dpm_clk_levels(struct smu_context
 *smu,
      /* Deep sleep - current clock < min_clock/2, TBD: cur_clk = 0 as GFXOFF */
      is_deep_sleep = cur_clk < min_clk / 2;
      if (is_deep_sleep) {
-             size += sysfs_emit_at(buf, size, "S: %uMhz *\n", cur_clk);
+             n = sysfs_emit_at(buf, size, "S: %uMhz *\n", cur_clk);
+             if (!n)
+                     goto out;
+             size += n;
              level_index = 1;
      }
@@ -1412,10 +1415,13 @@ int smu_cmn_print_dpm_clk_levels(struct
 smu_context *smu,
                                   smu_cmn_freqs_match(
                                           cur_clk,
                                           dpm_table->dpm_levels[i].value);
-                     size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
-                                           level_index + i,
-                                           dpm_table->dpm_levels[i].value,
-                                           freq_match ? "*" : "");
+                     n = sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
+                                       level_index + i,
+                                       dpm_table->dpm_levels[i].value,
+                                       freq_match ? "*" : "");
+                     if (!n)
+                             break;
+                     size += n;
              }
      } else {
              count = 2;
drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +7 -4
@@ -1437,13 +1443,16 @@ int smu_cmn_print_dpm_clk_levels(struct
 smu_context *smu,
              }
 
              for (i = 0; i < count; i++) {
-                     size += sysfs_emit_at(
-                             buf, size, "%d: %uMhz %s\n", level_index + i,
-                             freq_values[i],
-                             (!is_deep_sleep && i == lvl) ? "*" : "");
+                     n = sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
+                                       level_index + i, freq_values[i],
+                                       (!is_deep_sleep && i == lvl) ? "*" : "");
+                     if (!n)
+                             break;
+                     size += n;
              }
      }
 
+out:
      *offset = size;
 
      return 0;
drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +25 -23
@@ -1454,7 +1463,7 @@ int smu_cmn_print_pcie_levels(struct smu_context *smu,
                            uint32_t cur_gen, uint32_t cur_lane, char *buf,
                            int *offset)
 {
-     int size, i;
+     int size, i, n;
 
      if (!pcie_table || !buf)
              return -EINVAL;
@@ -1462,28 +1471,30 @@ int smu_cmn_print_pcie_levels(struct smu_context
 *smu,
      size = *offset;
 
      for (i = 0; i < pcie_table->lclk_levels; i++) {
-             size += sysfs_emit_at(
-                     buf, size, "%d: %s %s %dMhz %s\n", i,
-                     (pcie_table->pcie_gen[i] == 0) ? "2.5GT/s," :
-                     (pcie_table->pcie_gen[i] == 1) ? "5.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 2) ? "8.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 3) ? "16.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 4) ? "32.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 5) ? "64.0GT/s," :
-                                                      "",
-                     (pcie_table->pcie_lane[i] == 1) ? "x1" :
-                     (pcie_table->pcie_lane[i] == 2) ? "x2" :
-                     (pcie_table->pcie_lane[i] == 3) ? "x4" :
-                     (pcie_table->pcie_lane[i] == 4) ? "x8" :
-                     (pcie_table->pcie_lane[i] == 5) ? "x12" :
-                     (pcie_table->pcie_lane[i] == 6) ? "x16" :
-                     (pcie_table->pcie_lane[i] == 7) ? "x32" :
-                                                       "",
-                     pcie_table->lclk_freq[i],
-                     (cur_gen == pcie_table->pcie_gen[i]) &&
-                                     (cur_lane == pcie_table->pcie_lane[i]) ?
-                             "*" :
-                             "");
+             n = sysfs_emit_at(buf, size, "%d: %s %s %dMhz %s\n", i,
+                               (pcie_table->pcie_gen[i] == 0) ? "2.5GT/s," :
+                               (pcie_table->pcie_gen[i] == 1) ? "5.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 2) ? "8.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 3) ? "16.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 4) ? "32.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 5) ? "64.0GT/s," :
+                               "",
+                               (pcie_table->pcie_lane[i] == 1) ? "x1" :
+                               (pcie_table->pcie_lane[i] == 2) ? "x2" :
+                               (pcie_table->pcie_lane[i] == 3) ? "x4" :
+                               (pcie_table->pcie_lane[i] == 4) ? "x8" :
+                               (pcie_table->pcie_lane[i] == 5) ? "x12" :
+                               (pcie_table->pcie_lane[i] == 6) ? "x16" :
+                               (pcie_table->pcie_lane[i] == 7) ? "x32" :
+                               "",
+                               pcie_table->lclk_freq[i],
+                               (cur_gen == pcie_table->pcie_gen[i]) &&
+                               (cur_lane == pcie_table->pcie_lane[i]) ?
+                               "*" :
+                               "");
+             if (!n)
+                     break;
+             size += n;
      }
 
      *offset = size;

--
2.46.0

#5 lore raw

AMD General

Thanks for the review.

I agree that size += sysfs_emit_at() is the common pattern for simple sysfs show handlers, and for many SMU paths the output is small enough that PAGE_SIZE is never exhausted.

The checks were added specifically for helpers that append many sections into one PAGE_SIZE buffer, in particular pp_od_clk_voltage, which calls amdgpu_dpm_emit_clock_levels() repeatedly in a single show(). On SMU13 OD paths the combined OD/fan output can approach the sysfs page limit. Once the buffer is full, further sysfs_emit_at(buf, size, ...) calls hit the at >= PAGE_SIZE WARN path in sysfs_emit_at() and return 0, continuing the loop only generates warnings and does not improve userspace output.

I'm happy to narrow the series, keep the return check only in multi-emit/loop paths (e.g. smu_cmn_print_dpm_clk_levels, SMU13 OD emit_clk_levels), and revert it for small single-shot emits such as smu_v13_0_6 where truncation is not realistic. Alternatively, we could add a single stop condition in amdgpu_get_pp_od_clk_voltage() instead of per-emit checks.

Let me know your thoughts.

Regards
Asad

-----Original Message-----
From: Wang, Yang(Kevin) <[email protected]>
Sent: Friday, May 29, 2026 12:46 PM
To: Kamal, Asad <[email protected]>; [email protected]; Koenig, Christian <[email protected]>
Cc: Lazar, Lijo <[email protected]>; Zhang, Hawking <[email protected]>; Ma, Le <[email protected]>; Zhang, Morris <[email protected]>; Deucher, Alexander <[email protected]>
Subject: RE: [PATCH 1/4] drm/amd/pm: Handle truncation in common clk/pcie printers

AMD General

The "len += sysfs_emit_at(...)" is a common practice in the Linux kernel.
Its return value has no adverse impact on subsequent calls, so repeatedly checking the return value serves no practical purpose. You may refer to other usages of sysfs_emit_at() in the kernel source; none of the existing examples check its return value. (this may be a widely adopted convention).

Cc @Koenig, Christian

Best Regards,
Kevin

Quoted 155 lines

-----Original Message-----
From: Kamal, Asad <[email protected]>
Sent: Friday, May 29, 2026 14:19
To: [email protected]
Cc: Lazar, Lijo <[email protected]>; Zhang, Hawking <[email protected]>; Ma, Le <[email protected]>; Zhang, Morris <[email protected]>; Deucher, Alexander <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Kamal, Asad <[email protected]>
Subject: [PATCH 1/4] drm/amd/pm: Handle truncation in common clk/pcie printers

In smu_cmn_print_dpm_clk_levels() and smu_cmn_print_pcie_levels(), use the
sysfs_emit_at() return value, break out of per-level loops when n == 0, and use a shared out label before updating offset.

---
 drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c | 77
+++++++++++++++-----------
 1 file changed, 44 insertions(+), 33 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +12 -6
index 546e64e3ba9c..0a745afa8552 100644
@@ -1384,7 +1384,7 @@ int smu_cmn_print_dpm_clk_levels(struct
 smu_context *smu,  {
      uint32_t min_clk, max_clk, level_index, count;
      uint32_t freq_values[3];
-     int size, lvl, i;
+     int size, lvl, i, n;
      bool is_fine_grained;
      bool is_deep_sleep;
      bool freq_match;
@@ -1402,7 +1402,10 @@ int smu_cmn_print_dpm_clk_levels(struct
 smu_context *smu,
      /* Deep sleep - current clock < min_clock/2, TBD: cur_clk = 0 as GFXOFF */
      is_deep_sleep = cur_clk < min_clk / 2;
      if (is_deep_sleep) {
-             size += sysfs_emit_at(buf, size, "S: %uMhz *\n", cur_clk);
+             n = sysfs_emit_at(buf, size, "S: %uMhz *\n", cur_clk);
+             if (!n)
+                     goto out;
+             size += n;
              level_index = 1;
      }
@@ -1412,10 +1415,13 @@ int smu_cmn_print_dpm_clk_levels(struct
 smu_context *smu,
                                   smu_cmn_freqs_match(
                                           cur_clk,
                                           dpm_table->dpm_levels[i].value);
-                     size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
-                                           level_index + i,
-                                           dpm_table->dpm_levels[i].value,
-                                           freq_match ? "*" : "");
+                     n = sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
+                                       level_index + i,
+                                       dpm_table->dpm_levels[i].value,
+                                       freq_match ? "*" : "");
+                     if (!n)
+                             break;
+                     size += n;
              }
      } else {
              count = 2;
drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +7 -4
@@ -1437,13 +1443,16 @@ int smu_cmn_print_dpm_clk_levels(struct
 smu_context *smu,
              }
 
              for (i = 0; i < count; i++) {
-                     size += sysfs_emit_at(
-                             buf, size, "%d: %uMhz %s\n", level_index + i,
-                             freq_values[i],
-                             (!is_deep_sleep && i == lvl) ? "*" : "");
+                     n = sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
+                                       level_index + i, freq_values[i],
+                                       (!is_deep_sleep && i == lvl) ? "*" : "");
+                     if (!n)
+                             break;
+                     size += n;
              }
      }
 
+out:
      *offset = size;
 
      return 0;
drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +25 -23
@@ -1454,7 +1463,7 @@ int smu_cmn_print_pcie_levels(struct smu_context *smu,
                            uint32_t cur_gen, uint32_t cur_lane, char *buf,
                            int *offset)  {
-     int size, i;
+     int size, i, n;
 
      if (!pcie_table || !buf)
              return -EINVAL;
@@ -1462,28 +1471,30 @@ int smu_cmn_print_pcie_levels(struct smu_context
 *smu,
      size = *offset;
 
      for (i = 0; i < pcie_table->lclk_levels; i++) {
-             size += sysfs_emit_at(
-                     buf, size, "%d: %s %s %dMhz %s\n", i,
-                     (pcie_table->pcie_gen[i] == 0) ? "2.5GT/s," :
-                     (pcie_table->pcie_gen[i] == 1) ? "5.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 2) ? "8.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 3) ? "16.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 4) ? "32.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 5) ? "64.0GT/s," :
-                                                      "",
-                     (pcie_table->pcie_lane[i] == 1) ? "x1" :
-                     (pcie_table->pcie_lane[i] == 2) ? "x2" :
-                     (pcie_table->pcie_lane[i] == 3) ? "x4" :
-                     (pcie_table->pcie_lane[i] == 4) ? "x8" :
-                     (pcie_table->pcie_lane[i] == 5) ? "x12" :
-                     (pcie_table->pcie_lane[i] == 6) ? "x16" :
-                     (pcie_table->pcie_lane[i] == 7) ? "x32" :
-                                                       "",
-                     pcie_table->lclk_freq[i],
-                     (cur_gen == pcie_table->pcie_gen[i]) &&
-                                     (cur_lane == pcie_table->pcie_lane[i]) ?
-                             "*" :
-                             "");
+             n = sysfs_emit_at(buf, size, "%d: %s %s %dMhz %s\n", i,
+                               (pcie_table->pcie_gen[i] == 0) ? "2.5GT/s," :
+                               (pcie_table->pcie_gen[i] == 1) ? "5.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 2) ? "8.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 3) ? "16.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 4) ? "32.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 5) ? "64.0GT/s," :
+                               "",
+                               (pcie_table->pcie_lane[i] == 1) ? "x1" :
+                               (pcie_table->pcie_lane[i] == 2) ? "x2" :
+                               (pcie_table->pcie_lane[i] == 3) ? "x4" :
+                               (pcie_table->pcie_lane[i] == 4) ? "x8" :
+                               (pcie_table->pcie_lane[i] == 5) ? "x12" :
+                               (pcie_table->pcie_lane[i] == 6) ? "x16" :
+                               (pcie_table->pcie_lane[i] == 7) ? "x32" :
+                               "",
+                               pcie_table->lclk_freq[i],
+                               (cur_gen == pcie_table->pcie_gen[i]) &&
+                               (cur_lane == pcie_table->pcie_lane[i]) ?
+                               "*" :
+                               "");
+             if (!n)
+                     break;
+             size += n;
      }
 
      *offset = size;

--
2.46.0

#6 lore raw

AMD General

Quoted 1 line
Quoted 1 line

Alternatively, we could add a single stop condition in amdgpu_get_pp_od_clk_voltage() instead of per-emit checks.

I prefer this way. Thanks.

Best Regards,
Kevin

Quoted 221 lines

-----Original Message-----
From: Kamal, Asad <[email protected]>
Sent: Friday, May 29, 2026 16:00
To: Wang, Yang(Kevin) <[email protected]>; amd- [email protected]; Koenig, Christian <[email protected]>
Cc: Lazar, Lijo <[email protected]>; Zhang, Hawking <[email protected]>; Ma, Le <[email protected]>; Zhang, Morris <[email protected]>; Deucher, Alexander <[email protected]>
Subject: RE: [PATCH 1/4] drm/amd/pm: Handle truncation in common clk/pcie printers

AMD General

Thanks for the review.

I agree that size += sysfs_emit_at() is the common pattern for simple sysfs show handlers, and for many SMU paths the output is small enough that PAGE_SIZE is never exhausted.

The checks were added specifically for helpers that append many sections into one PAGE_SIZE buffer, in particular pp_od_clk_voltage, which calls amdgpu_dpm_emit_clock_levels() repeatedly in a single show(). On SMU13 OD paths the combined OD/fan output can approach the sysfs page limit. Once the buffer is full, further sysfs_emit_at(buf, size, ...) calls hit the at >= PAGE_SIZE WARN path in sysfs_emit_at() and return 0, continuing the loop only generates warnings and does not improve userspace output.

I'm happy to narrow the series, keep the return check only in multi-emit/loop paths (e.g. smu_cmn_print_dpm_clk_levels, SMU13 OD emit_clk_levels), and revert it for small single-shot emits such as smu_v13_0_6 where truncation is not realistic. Alternatively, we could add a single stop condition in amdgpu_get_pp_od_clk_voltage() instead of per-emit checks.

Let me know your thoughts.

Regards
Asad

-----Original Message-----
From: Wang, Yang(Kevin) <[email protected]>
Sent: Friday, May 29, 2026 12:46 PM
To: Kamal, Asad <[email protected]>; [email protected]; Koenig, Christian <[email protected]>
Cc: Lazar, Lijo <[email protected]>; Zhang, Hawking <[email protected]>; Ma, Le <[email protected]>; Zhang, Morris <[email protected]>; Deucher, Alexander <[email protected]>
Subject: RE: [PATCH 1/4] drm/amd/pm: Handle truncation in common clk/pcie printers

AMD General

The "len += sysfs_emit_at(...)" is a common practice in the Linux kernel. Its return value has no adverse impact on subsequent calls, so repeatedly checking the return value serves no practical purpose.
You may refer to other usages of sysfs_emit_at() in the kernel source; none of the existing examples check its return value.
(this may be a widely adopted convention).

Cc @Koenig, Christian

Best Regards,
Kevin

Quoted 7 lines

-----Original Message-----
From: Kamal, Asad <[email protected]>
Sent: Friday, May 29, 2026 14:19
To: [email protected]
Cc: Lazar, Lijo <[email protected]>; Zhang, Hawking <[email protected]>; Ma, Le <[email protected]>; Zhang, Morris <[email protected]>; Deucher, Alexander

<[email protected]>;

drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c int smu_cmn_print_pcie_levels(struct smu_context

Quoted 85 lines

Wang, Yang(Kevin) <[email protected]>; Kamal, Asad <[email protected]>
Subject: [PATCH 1/4] drm/amd/pm: Handle truncation in common clk/pcie printers

In smu_cmn_print_dpm_clk_levels() and smu_cmn_print_pcie_levels(), use the
sysfs_emit_at() return value, break out of per-level loops when n == 0, and use a shared out label before updating offset.

---
 drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c | 77
+++++++++++++++-----------
 1 file changed, 44 insertions(+), 33 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +12 -6
index 546e64e3ba9c..0a745afa8552 100644
@@ -1384,7 +1384,7 @@ int smu_cmn_print_dpm_clk_levels(struct
 smu_context *smu,  {
      uint32_t min_clk, max_clk, level_index, count;
      uint32_t freq_values[3];
-     int size, lvl, i;
+     int size, lvl, i, n;
      bool is_fine_grained;
      bool is_deep_sleep;
      bool freq_match;
@@ -1402,7 +1402,10 @@ int smu_cmn_print_dpm_clk_levels(struct
 smu_context *smu,
      /* Deep sleep - current clock < min_clock/2, TBD: cur_clk = 0 as GFXOFF */
      is_deep_sleep = cur_clk < min_clk / 2;
      if (is_deep_sleep) {
-             size += sysfs_emit_at(buf, size, "S: %uMhz *\n", cur_clk);
+             n = sysfs_emit_at(buf, size, "S: %uMhz *\n", cur_clk);
+             if (!n)
+                     goto out;
+             size += n;
              level_index = 1;
      }
@@ -1412,10 +1415,13 @@ int smu_cmn_print_dpm_clk_levels(struct
 smu_context *smu,
                                   smu_cmn_freqs_match(
                                           cur_clk,
                                           dpm_table->dpm_levels[i].value);
-                     size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
-                                           level_index + i,
-                                           dpm_table->dpm_levels[i].value,
-                                           freq_match ? "*" : "");
+                     n = sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
+                                       level_index + i,
+                                       dpm_table->dpm_levels[i].value,
+                                       freq_match ? "*" : "");
+                     if (!n)
+                             break;
+                     size += n;
              }
      } else {
              count = 2;
drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +7 -4
@@ -1437,13 +1443,16 @@ int smu_cmn_print_dpm_clk_levels(struct
 smu_context *smu,
              }
 
              for (i = 0; i < count; i++) {
-                     size += sysfs_emit_at(
-                             buf, size, "%d: %uMhz %s\n", level_index + i,
-                             freq_values[i],
-                             (!is_deep_sleep && i == lvl) ? "*" : "");
+                     n = sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
+                                       level_index + i, freq_values[i],
+                                       (!is_deep_sleep && i == lvl) ? "*" : "");
+                     if (!n)
+                             break;
+                     size += n;
              }
      }
 
+out:
      *offset = size;
 
      return 0;
drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +0 -0
@@ -1454,7 +1463,7 @@ int smu_cmn_print_pcie_levels(struct smu_context

*smu,

Quoted 63 lines
drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +25 -23
                            uint32_t cur_gen, uint32_t cur_lane, char *buf,
                            int *offset)  {
-     int size, i;
+     int size, i, n;
 
      if (!pcie_table || !buf)
              return -EINVAL;
@@ -1462,28 +1471,30 @@ int smu_cmn_print_pcie_levels(struct
 smu_context *smu,
      size = *offset;
 
      for (i = 0; i < pcie_table->lclk_levels; i++) {
-             size += sysfs_emit_at(
-                     buf, size, "%d: %s %s %dMhz %s\n", i,
-                     (pcie_table->pcie_gen[i] == 0) ? "2.5GT/s," :
-                     (pcie_table->pcie_gen[i] == 1) ? "5.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 2) ? "8.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 3) ? "16.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 4) ? "32.0GT/s," :
-                     (pcie_table->pcie_gen[i] == 5) ? "64.0GT/s," :
-                                                      "",
-                     (pcie_table->pcie_lane[i] == 1) ? "x1" :
-                     (pcie_table->pcie_lane[i] == 2) ? "x2" :
-                     (pcie_table->pcie_lane[i] == 3) ? "x4" :
-                     (pcie_table->pcie_lane[i] == 4) ? "x8" :
-                     (pcie_table->pcie_lane[i] == 5) ? "x12" :
-                     (pcie_table->pcie_lane[i] == 6) ? "x16" :
-                     (pcie_table->pcie_lane[i] == 7) ? "x32" :
-                                                       "",
-                     pcie_table->lclk_freq[i],
-                     (cur_gen == pcie_table->pcie_gen[i]) &&
-                                     (cur_lane == pcie_table->pcie_lane[i]) ?
-                             "*" :
-                             "");
+             n = sysfs_emit_at(buf, size, "%d: %s %s %dMhz %s\n", i,
+                               (pcie_table->pcie_gen[i] == 0) ? "2.5GT/s," :
+                               (pcie_table->pcie_gen[i] == 1) ? "5.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 2) ? "8.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 3) ? "16.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 4) ? "32.0GT/s," :
+                               (pcie_table->pcie_gen[i] == 5) ? "64.0GT/s," :
+                               "",
+                               (pcie_table->pcie_lane[i] == 1) ? "x1" :
+                               (pcie_table->pcie_lane[i] == 2) ? "x2" :
+                               (pcie_table->pcie_lane[i] == 3) ? "x4" :
+                               (pcie_table->pcie_lane[i] == 4) ? "x8" :
+                               (pcie_table->pcie_lane[i] == 5) ? "x12" :
+                               (pcie_table->pcie_lane[i] == 6) ? "x16" :
+                               (pcie_table->pcie_lane[i] == 7) ? "x32" :
+                               "",
+                               pcie_table->lclk_freq[i],
+                               (cur_gen == pcie_table->pcie_gen[i]) &&
+                               (cur_lane == pcie_table->pcie_lane[i]) ?
+                               "*" :
+                               "");
+             if (!n)
+                     break;
+             size += n;
      }
 
      *offset = size;

--
2.46.0