[PATCH v3 1/2] platform/mellanox: mlxbf-pmc: Remove newline char from event name input

Shravan Kumar Ramani posted 2 patches 3 months, 1 week ago
There is a newer version of this series
[PATCH v3 1/2] platform/mellanox: mlxbf-pmc: Remove newline char from event name input
Posted by Shravan Kumar Ramani 3 months, 1 week ago
Since the input string passed via the command line appends a newline char,
it needs to be removed before comparison with the event_list.

Fixes: 1a218d312e65 ("platform/mellanox: mlxbf-pmc: Add Mellanox BlueField PMC driver")
Signed-off-by: Shravan Kumar Ramani <shravankr@nvidia.com>
Reviewed-by: David Thompson <davthomspson@nvidia.com>
---
 drivers/platform/mellanox/mlxbf-pmc.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/platform/mellanox/mlxbf-pmc.c b/drivers/platform/mellanox/mlxbf-pmc.c
index 900069eb186e..16cc909aecab 100644
--- a/drivers/platform/mellanox/mlxbf-pmc.c
+++ b/drivers/platform/mellanox/mlxbf-pmc.c
@@ -1204,9 +1204,10 @@ static bool mlxbf_pmc_event_supported(const char *blk)
 }
 
 /* Get the event number given the name */
-static int mlxbf_pmc_get_event_num(const char *blk, const char *evt)
+static int mlxbf_pmc_get_event_num(const char *blk, char *evt)
 {
 	const struct mlxbf_pmc_events *events;
+	int len = strlen(evt);
 	unsigned int i;
 	size_t size;
 
@@ -1214,6 +1215,10 @@ static int mlxbf_pmc_get_event_num(const char *blk, const char *evt)
 	if (!events)
 		return -EINVAL;
 
+	/* Remove the trailing newline character if present */
+	if (evt[len - 1] == '\n')
+		evt[len - 1] = '\0';
+
 	for (i = 0; i < size; ++i) {
 		if (!strcmp(evt, events[i].evt_name))
 			return events[i].evt_num;
@@ -1681,7 +1686,7 @@ static ssize_t mlxbf_pmc_counter_show(struct device *dev,
 			return -EINVAL;
 	} else if (pmc->block[blk_num].type == MLXBF_PMC_TYPE_REGISTER) {
 		offset = mlxbf_pmc_get_event_num(pmc->block_name[blk_num],
-						 attr->attr.name);
+						 (char *)attr->attr.name);
 		if (offset < 0)
 			return -EINVAL;
 		if (mlxbf_pmc_read_reg(blk_num, offset, &value))
@@ -1730,7 +1735,7 @@ static ssize_t mlxbf_pmc_counter_store(struct device *dev,
 			return err;
 	} else if (pmc->block[blk_num].type == MLXBF_PMC_TYPE_REGISTER) {
 		offset = mlxbf_pmc_get_event_num(pmc->block_name[blk_num],
-						 attr->attr.name);
+						 (char *)attr->attr.name);
 		if (offset < 0)
 			return -EINVAL;
 		err = mlxbf_pmc_write_reg(blk_num, offset, data);
@@ -1792,7 +1797,7 @@ static ssize_t mlxbf_pmc_event_store(struct device *dev,
 
 	if (isalpha(buf[0])) {
 		evt_num = mlxbf_pmc_get_event_num(pmc->block_name[blk_num],
-						  buf);
+						  (char *)buf);
 		if (evt_num < 0)
 			return -EINVAL;
 	} else {
-- 
2.43.2
Re: [PATCH v3 1/2] platform/mellanox: mlxbf-pmc: Remove newline char from event name input
Posted by Ilpo Järvinen 3 months, 1 week ago
On Mon, 30 Jun 2025, Shravan Kumar Ramani wrote:

> Since the input string passed via the command line appends a newline char,
> it needs to be removed before comparison with the event_list.
> 
> Fixes: 1a218d312e65 ("platform/mellanox: mlxbf-pmc: Add Mellanox BlueField PMC driver")
> Signed-off-by: Shravan Kumar Ramani <shravankr@nvidia.com>
> Reviewed-by: David Thompson <davthomspson@nvidia.com>
> ---
>  drivers/platform/mellanox/mlxbf-pmc.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/platform/mellanox/mlxbf-pmc.c b/drivers/platform/mellanox/mlxbf-pmc.c
> index 900069eb186e..16cc909aecab 100644
> --- a/drivers/platform/mellanox/mlxbf-pmc.c
> +++ b/drivers/platform/mellanox/mlxbf-pmc.c
> @@ -1204,9 +1204,10 @@ static bool mlxbf_pmc_event_supported(const char *blk)
>  }
>  
>  /* Get the event number given the name */
> -static int mlxbf_pmc_get_event_num(const char *blk, const char *evt)
> +static int mlxbf_pmc_get_event_num(const char *blk, char *evt)
>  {
>  	const struct mlxbf_pmc_events *events;
> +	int len = strlen(evt);
>  	unsigned int i;
>  	size_t size;
>  
> @@ -1214,6 +1215,10 @@ static int mlxbf_pmc_get_event_num(const char *blk, const char *evt)
>  	if (!events)
>  		return -EINVAL;
>  
> +	/* Remove the trailing newline character if present */
> +	if (evt[len - 1] == '\n')
> +		evt[len - 1] = '\0';
> +
>  	for (i = 0; i < size; ++i) {
>  		if (!strcmp(evt, events[i].evt_name))
>  			return events[i].evt_num;
> @@ -1681,7 +1686,7 @@ static ssize_t mlxbf_pmc_counter_show(struct device *dev,
>  			return -EINVAL;
>  	} else if (pmc->block[blk_num].type == MLXBF_PMC_TYPE_REGISTER) {
>  		offset = mlxbf_pmc_get_event_num(pmc->block_name[blk_num],
> -						 attr->attr.name);
> +						 (char *)attr->attr.name);
>  		if (offset < 0)
>  			return -EINVAL;
>  		if (mlxbf_pmc_read_reg(blk_num, offset, &value))
> @@ -1730,7 +1735,7 @@ static ssize_t mlxbf_pmc_counter_store(struct device *dev,
>  			return err;
>  	} else if (pmc->block[blk_num].type == MLXBF_PMC_TYPE_REGISTER) {
>  		offset = mlxbf_pmc_get_event_num(pmc->block_name[blk_num],
> -						 attr->attr.name);
> +						 (char *)attr->attr.name);

These shouldn't need any newline removal, right?

>  		if (offset < 0)
>  			return -EINVAL;
>  		err = mlxbf_pmc_write_reg(blk_num, offset, data);
> @@ -1792,7 +1797,7 @@ static ssize_t mlxbf_pmc_event_store(struct device *dev,
>  
>  	if (isalpha(buf[0])) {
>  		evt_num = mlxbf_pmc_get_event_num(pmc->block_name[blk_num],
> -						  buf);
> +						  (char *)buf);

You should cleanup the newline here, not inside mlxbf_pmc_get_event_num() 
so that you can keep the argument type const.

As the input parameter is const char *, I suggest using 
kstrdup_and_replace() so you can modify it.

In general, casting constness away is bad practice.

>  		if (evt_num < 0)
>  			return -EINVAL;
>  	} else {
> 

-- 
 i.