Skip to contentSkip to conversation

drm/amd/pm: Use strscpy in profile mode parsing

PATCH In review Lijo Lazar 3 participants 4 messages May 29

Subsystems (3)
lore
Share & tools
lore

Lijo Lazar

author
#0 lore raw

PATCH 1/2 drm/amd/pm: Use strscpy in profile mode parsing

Use strscpy to copy the buffer which makes it explicit that a valid NULL terminated string gets copied. Also, make it explicit that the source buffer can be copied safely to the temporary buffer by checking against its size.

---
 drivers/gpu/drm/amd/pm/amdgpu_pm.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/pm/amdgpu_pm.c +5 -9
index 450ecb188aed..ebe0b320ca4f 100644
@@ -1375,26 +1375,22 @@ static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
 	long parameter[64];
 	char *sub_str, buf_cpy[128];
 	char *tmp_str;
-	uint32_t i = 0;
 	char tmp[2];
 	long int profile_mode = 0;
 	const char delimiter[3] = {' ', '\n', '\0'};
-	size_t len;
 
-	tmp[0] = *(buf);
+	tmp[0] = *(buf++);
 	tmp[1] = '\0';
 	ret = kstrtol(tmp, 0, &profile_mode);
 	if (ret)
 		return -EINVAL;
 
 	if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
-		if (count < 2 || count > 127)
+		if (count < 2 || count > sizeof(buf_cpy))
 			return -EINVAL;
-		while (isspace(*++buf))
-			i++;
-		len = count - 1 - i;
-		memcpy(buf_cpy, buf, len);
-		buf_cpy[len] = '\0';
+		while (isspace(*buf))
+			buf++;
+		strscpy(buf_cpy, buf, sizeof(buf_cpy));
 		tmp_str = buf_cpy;
 		while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
 			if (strlen(sub_str) == 0)
Signature · 1 line
2.49.0
author
#1 lore raw

PATCH 2/2 drm/amd/pm: Add helper for parameter parsing

Add a helper function to extract long values passed in a string. The string may have values of multiple parameters separated by space char.

---
 drivers/gpu/drm/amd/pm/amdgpu_pm.c | 116 ++++++++++++-----------------
 1 file changed, 48 insertions(+), 68 deletions(-)
drivers/gpu/drm/amd/pm/amdgpu_pm.c +48 -68
index ebe0b320ca4f..040839dc6368 100644
@@ -98,6 +98,37 @@ const char * const amdgpu_pp_profile_name[] = {
 	"UNCAPPED",
 };
 
+static int amdgpu_pm_parse_long_params(char *str, long *params,
+				       uint32_t max_params,
+				       uint32_t *num_params)
+{
+	const char delimiter[] = { ' ', '\n', '\0' };
+	uint32_t count = 0;
+	char *sub_str;
+	int ret;
+
+	if (!params || !num_params)
+		return -EINVAL;
+
+	while ((sub_str = strsep(&str, delimiter)) != NULL) {
+		if (strlen(sub_str) == 0)
+			continue;
+		if (count >= max_params)
+			return -EINVAL;
+		ret = kstrtol(sub_str, 0, &params[count]);
+		if (ret)
+			return -EINVAL;
+		count++;
+		if (!str)
+			break;
+		while (isspace(*str))
+			str++;
+	}
+	*num_params = count;
+
+	return 0;
+}
+
 /**
  * amdgpu_pm_dev_state_check - Check if device can be accessed.
  * @adev: Target device.
@@ -759,8 +790,6 @@ static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
 	long parameter[64];
 	char buf_cpy[128];
 	char *tmp_str;
-	char *sub_str;
-	const char delimiter[3] = {' ', '\n', '\0'};
 	uint32_t type;
 
 	if (count > 127 || count == 0)
@@ -795,22 +824,10 @@ static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
 		tmp_str++;
 	while (isspace(*++tmp_str));
 
-	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;
-		parameter_size++;
-
-		if (!tmp_str)
-			break;
-
-		while (isspace(*tmp_str))
-			tmp_str++;
-	}
+	ret = amdgpu_pm_parse_long_params(
+		tmp_str, parameter, ARRAY_SIZE(parameter), &parameter_size);
+	if (ret)
+		return ret;
 
 	ret = amdgpu_pm_get_access(adev);
 	if (ret < 0)
@@ -1373,11 +1390,9 @@ static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 	uint32_t parameter_size = 0;
 	long parameter[64];
-	char *sub_str, buf_cpy[128];
-	char *tmp_str;
+	char buf_cpy[128];
 	char tmp[2];
 	long int profile_mode = 0;
-	const char delimiter[3] = {' ', '\n', '\0'};
 
 	tmp[0] = *(buf++);
 	tmp[1] = '\0';
@@ -1391,19 +1406,11 @@ static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
 		while (isspace(*buf))
 			buf++;
 		strscpy(buf_cpy, buf, sizeof(buf_cpy));
-		tmp_str = buf_cpy;
-		while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
-			if (strlen(sub_str) == 0)
-				continue;
-			ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
-			if (ret)
-				return -EINVAL;
-			parameter_size++;
-			if (!tmp_str)
-				break;
-			while (isspace(*tmp_str))
-				tmp_str++;
-		}
+		ret = amdgpu_pm_parse_long_params(buf_cpy, parameter,
+						  ARRAY_SIZE(parameter) - 1,
+						  &parameter_size);
+		if (ret)
+			return ret;
 	}
 	parameter[parameter_size] = profile_mode;
 
@@ -3928,18 +3935,14 @@ static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
 	return size;
 }
 
-static int parse_input_od_command_lines(const char *buf,
-					size_t count,
-					u32 *type,
-					long *params,
-					size_t params_max,
+static int parse_input_od_command_lines(const char *buf, size_t count,
+					u32 *type, long *params,
+					uint32_t max_params,
 					uint32_t *num_of_params)
 {
-	const char delimiter[3] = {' ', '\n', '\0'};
 	uint32_t parameter_size = 0;
 	char buf_cpy[128] = {0};
-	char *tmp_str, *sub_str;
-	int ret;
+	char *tmp_str;
 
 	if (count > sizeof(buf_cpy) - 1)
 		return -EINVAL;
@@ -3964,28 +3967,8 @@ static int parse_input_od_command_lines(const char *buf,
 		break;
 	}
 
-	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
-		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;
-		parameter_size++;
-
-		if (!tmp_str)
-			break;
-
-		while (isspace(*tmp_str))
-			tmp_str++;
-	}
-
-	*num_of_params = parameter_size;
-
-	return 0;
+	return amdgpu_pm_parse_long_params(tmp_str, params, max_params,
+					   num_of_params);
 }
 
 static int
@@ -3998,10 +3981,7 @@ amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
 	long parameter[64];
 	int ret;
 
-	ret = parse_input_od_command_lines(in_buf,
-					   count,
-					   &cmd_type,
-					   parameter,
+	ret = parse_input_od_command_lines(in_buf, count, &cmd_type, parameter,
 					   ARRAY_SIZE(parameter),
 					   &parameter_size);
 	if (ret)
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH 2/2] drm/amd/pm: Add helper for parameter parsing

Quoted 2 lines · Lijo Lazar

Add a helper function to extract long values passed in a string. The string may have values of multiple parameters separated by space char.

Hah! :) Long time ago, in a galaxy not so far away:

https://patchwork.freedesktop.org/series/154657/

Regards,

Tvrtko

Quoted 187 lines
---
  drivers/gpu/drm/amd/pm/amdgpu_pm.c | 116 ++++++++++++-----------------
  1 file changed, 48 insertions(+), 68 deletions(-)
drivers/gpu/drm/amd/pm/amdgpu_pm.c +48 -68
index ebe0b320ca4f..040839dc6368 100644
@@ -98,6 +98,37 @@ const char * const amdgpu_pp_profile_name[] = {
  	"UNCAPPED",
  };
 
+static int amdgpu_pm_parse_long_params(char *str, long *params,
+				       uint32_t max_params,
+				       uint32_t *num_params)
+{
+	const char delimiter[] = { ' ', '\n', '\0' };
+	uint32_t count = 0;
+	char *sub_str;
+	int ret;
+
+	if (!params || !num_params)
+		return -EINVAL;
+
+	while ((sub_str = strsep(&str, delimiter)) != NULL) {
+		if (strlen(sub_str) == 0)
+			continue;
+		if (count >= max_params)
+			return -EINVAL;
+		ret = kstrtol(sub_str, 0, &params[count]);
+		if (ret)
+			return -EINVAL;
+		count++;
+		if (!str)
+			break;
+		while (isspace(*str))
+			str++;
+	}
+	*num_params = count;
+
+	return 0;
+}
+
  /**
   * amdgpu_pm_dev_state_check - Check if device can be accessed.
   * @adev: Target device.
@@ -759,8 +790,6 @@ static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
  	long parameter[64];
  	char buf_cpy[128];
  	char *tmp_str;
-	char *sub_str;
-	const char delimiter[3] = {' ', '\n', '\0'};
  	uint32_t type;
 
  	if (count > 127 || count == 0)
@@ -795,22 +824,10 @@ static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
  		tmp_str++;
  	while (isspace(*++tmp_str));
 
-	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;
-		parameter_size++;
-
-		if (!tmp_str)
-			break;
-
-		while (isspace(*tmp_str))
-			tmp_str++;
-	}
+	ret = amdgpu_pm_parse_long_params(
+		tmp_str, parameter, ARRAY_SIZE(parameter), &parameter_size);
+	if (ret)
+		return ret;
 
  	ret = amdgpu_pm_get_access(adev);
  	if (ret < 0)
@@ -1373,11 +1390,9 @@ static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
  	struct amdgpu_device *adev = drm_to_adev(ddev);
  	uint32_t parameter_size = 0;
  	long parameter[64];
-	char *sub_str, buf_cpy[128];
-	char *tmp_str;
+	char buf_cpy[128];
  	char tmp[2];
  	long int profile_mode = 0;
-	const char delimiter[3] = {' ', '\n', '\0'};
 
  	tmp[0] = *(buf++);
  	tmp[1] = '\0';
@@ -1391,19 +1406,11 @@ static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
  		while (isspace(*buf))
  			buf++;
  		strscpy(buf_cpy, buf, sizeof(buf_cpy));
-		tmp_str = buf_cpy;
-		while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
-			if (strlen(sub_str) == 0)
-				continue;
-			ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
-			if (ret)
-				return -EINVAL;
-			parameter_size++;
-			if (!tmp_str)
-				break;
-			while (isspace(*tmp_str))
-				tmp_str++;
-		}
+		ret = amdgpu_pm_parse_long_params(buf_cpy, parameter,
+						  ARRAY_SIZE(parameter) - 1,
+						  &parameter_size);
+		if (ret)
+			return ret;
  	}
  	parameter[parameter_size] = profile_mode;
 
@@ -3928,18 +3935,14 @@ static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
  	return size;
  }
 
-static int parse_input_od_command_lines(const char *buf,
-					size_t count,
-					u32 *type,
-					long *params,
-					size_t params_max,
+static int parse_input_od_command_lines(const char *buf, size_t count,
+					u32 *type, long *params,
+					uint32_t max_params,
  					uint32_t *num_of_params)
  {
-	const char delimiter[3] = {' ', '\n', '\0'};
  	uint32_t parameter_size = 0;
  	char buf_cpy[128] = {0};
-	char *tmp_str, *sub_str;
-	int ret;
+	char *tmp_str;
 
  	if (count > sizeof(buf_cpy) - 1)
  		return -EINVAL;
@@ -3964,28 +3967,8 @@ static int parse_input_od_command_lines(const char *buf,
  		break;
  	}
 
-	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
-		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;
-		parameter_size++;
-
-		if (!tmp_str)
-			break;
-
-		while (isspace(*tmp_str))
-			tmp_str++;
-	}
-
-	*num_of_params = parameter_size;
-
-	return 0;
+	return amdgpu_pm_parse_long_params(tmp_str, params, max_params,
+					   num_of_params);
  }
 
  static int
@@ -3998,10 +3981,7 @@ amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
  	long parameter[64];
  	int ret;
 
-	ret = parse_input_od_command_lines(in_buf,
-					   count,
-					   &cmd_type,
-					   parameter,
+	ret = parse_input_od_command_lines(in_buf, count, &cmd_type, parameter,
  					   ARRAY_SIZE(parameter),
  					   &parameter_size);
  	if (ret)
Reviewed-by on v1 1/2
#3 lore raw
In reply to Lijo Lazar

AMD General

Series is

Regards,
Hawking
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Friday, May 29, 2026 5:38 PM
To: [email protected]
Cc: Zhang, Hawking <[email protected]>; Deucher, Alexander <[email protected]>; Kamal, Asad <[email protected]>; Wang, Yang(Kevin) <[email protected]>
Subject: [PATCH 1/2] drm/amd/pm: Use strscpy in profile mode parsing

Use strscpy to copy the buffer which makes it explicit that a valid NULL terminated string gets copied. Also, make it explicit that the source buffer can be copied safely to the temporary buffer by checking against its size.

---
 drivers/gpu/drm/amd/pm/amdgpu_pm.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/pm/amdgpu_pm.c +5 -9
index 450ecb188aed..ebe0b320ca4f 100644
@@ -1375,26 +1375,22 @@ static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
        long parameter[64];
        char *sub_str, buf_cpy[128];
        char *tmp_str;
-       uint32_t i = 0;
        char tmp[2];
        long int profile_mode = 0;
        const char delimiter[3] = {' ', '\n', '\0'};
-       size_t len;
 
-       tmp[0] = *(buf);
+       tmp[0] = *(buf++);
        tmp[1] = '\0';
        ret = kstrtol(tmp, 0, &profile_mode);
        if (ret)
                return -EINVAL;
 
        if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
-               if (count < 2 || count > 127)
+               if (count < 2 || count > sizeof(buf_cpy))
                        return -EINVAL;
-               while (isspace(*++buf))
-                       i++;
-               len = count - 1 - i;
-               memcpy(buf_cpy, buf, len);
-               buf_cpy[len] = '\0';
+               while (isspace(*buf))
+                       buf++;
+               strscpy(buf_cpy, buf, sizeof(buf_cpy));
                tmp_str = buf_cpy;
                while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
                        if (strlen(sub_str) == 0)

--
2.49.0