[SRU][J:linux-bluefield][PATCH v1 2/4] platform/mellanox: mlxbf-pmc: Remove newline char from event name input
Chris Babroski
cbabroski at nvidia.com
Wed Sep 10 19:49:12 UTC 2025
From: Shravan Kumar Ramani <shravankr at nvidia.com>
BugLink: https://bugs.launchpad.net/bugs/2121550
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 at nvidia.com>
Reviewed-by: David Thompson <davthompson at nvidia.com>
Link: https://lore.kernel.org/r/4978c18e33313b48fa2ae7f3aa6dbcfce40877e4.1751380187.git.shravankr@nvidia.com
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen at linux.intel.com>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen at linux.intel.com>
(backported from commit 44e6ca8faeeed12206f3e7189c5ac618b810bb9c)
[cbabroski: implement and use custom version of kstrdup_and_replace
because that is only available in newer kernels]
Signed-off-by: Chris Babroski <cbabroski at nvidia.com>
---
drivers/platform/mellanox/mlxbf-pmc.c | 31 ++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)
diff --git a/drivers/platform/mellanox/mlxbf-pmc.c b/drivers/platform/mellanox/mlxbf-pmc.c
index a1c529f1ff1a..7f96443e7d71 100644
--- a/drivers/platform/mellanox/mlxbf-pmc.c
+++ b/drivers/platform/mellanox/mlxbf-pmc.c
@@ -15,6 +15,7 @@
#include <linux/hwmon.h>
#include <linux/platform_device.h>
#include <linux/string.h>
+#include <linux/string_helpers.h>
#include <uapi/linux/psci.h>
#define MLXBF_PMC_WRITE_REG_32 0x82000009
@@ -1775,6 +1776,27 @@ static ssize_t mlxbf_pmc_event_show(struct device *dev,
return sysfs_emit(buf, "0x%llx: %s\n", evt_num, evt_name);
}
+/*
+ * Returns duplicate string in which the @old characters are replaced by @new.
+ * Based on kstrdup_and_replace added in newer kernels.
+ */
+static char *mlxbf_pmc_dup_and_replace(const char *src, size_t str_len,
+ char old, char new, gfp_t gfp)
+{
+ char *dst;
+
+ /* str_len does not include NUL terminator */
+ dst = kmalloc(str_len + 1, gfp);
+ if (!dst)
+ return NULL;
+
+ memcpy(dst, src, str_len);
+ dst[str_len] = '\0';
+
+ strreplace(dst, old, new);
+ return dst;
+}
+
/* Store function for "event" sysfs files */
static ssize_t mlxbf_pmc_event_store(struct device *dev,
struct device_attribute *attr,
@@ -1784,6 +1806,7 @@ static ssize_t mlxbf_pmc_event_store(struct device *dev,
attr, struct mlxbf_pmc_attribute, dev_attr);
unsigned int blk_num, cnt_num;
bool is_l3 = false;
+ char *evt_name;
int evt_num;
int err;
@@ -1791,8 +1814,14 @@ static ssize_t mlxbf_pmc_event_store(struct device *dev,
cnt_num = attr_event->index;
if (isalpha(buf[0])) {
+ /* Remove the trailing newline character if present */
+ evt_name = mlxbf_pmc_dup_and_replace(buf, count, '\n', '\0', GFP_KERNEL);
+ if (!evt_name)
+ return -ENOMEM;
+
evt_num = mlxbf_pmc_get_event_num(pmc->block_name[blk_num],
- buf);
+ evt_name);
+ kfree(evt_name);
if (evt_num < 0)
return -EINVAL;
} else {
--
2.34.1
More information about the kernel-team
mailing list