[SRU][J/N:linux-bluefield][PATCH v1 2/2] UBUNTU: SAUCE: mlxbf-pmc: Validate user input for event/enable
Shravan Kumar Ramani
shravankr at nvidia.com
Wed Jul 9 09:19:49 UTC 2025
BugLink: https://bugs.launchpad.net/bugs/2113758
When taking the event number or name as input from the user, check if the event
actually exists in the event_list. For event_name input, the trailing '\n' char
needs to be removed before comparing strings. Also fix a typo in the comment
preveding mlxbf_pmc_get_event_name().
When programming the enable field, accept only 0 or 1 as valid values. This is
checked currently only for a specific scenario. Make this check common for all
scenarios of mlxbf_pmc_enable_store().
Signed-off-by: Shravan Kumar Ramani <shravankr at nvidia.com>
Reviewed-by: David Thompson <davthompson at nvidia.com>
---
drivers/platform/mellanox/mlxbf-pmc.c | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/drivers/platform/mellanox/mlxbf-pmc.c b/drivers/platform/mellanox/mlxbf-pmc.c
index a1c529f1ff1a..78929f8bc23f 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;
+ /* skip the newline 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;
@@ -1222,7 +1227,7 @@ static int mlxbf_pmc_get_event_num(const char *blk, const char *evt)
return -ENODEV;
}
-/* Get the event number given the name */
+/* Get the event name given the number */
static char *mlxbf_pmc_get_event_name(const char *blk, u32 evt)
{
const struct mlxbf_pmc_events *events;
@@ -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,13 +1797,16 @@ 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 {
err = kstrtouint(buf, 0, &evt_num);
if (err < 0)
return err;
+
+ if (!mlxbf_pmc_get_event_name(pmc->block_name[blk_num], evt_num))
+ return -EINVAL;
}
if (strstr(pmc->block_name[blk_num], "l3cache"))
@@ -1889,6 +1897,9 @@ static ssize_t mlxbf_pmc_enable_store(struct device *dev,
if (err < 0)
return err;
+ if (en != 0 && en != 1)
+ return -EINVAL;
+
if (pmc->block[blk_num].type == MLXBF_PMC_TYPE_CRSPACE) {
err = mlxbf_pmc_readl(pmc->block[blk_num].mmio_base +
MLXBF_PMC_CRSPACE_PERFMON_CTL(pmc->block[blk_num].counters),
@@ -1905,9 +1916,6 @@ static ssize_t mlxbf_pmc_enable_store(struct device *dev,
MLXBF_PMC_CRSPACE_PERFMON_CTL(pmc->block[blk_num].counters),
MLXBF_PMC_WRITE_REG_32, word);
} else {
- if (en && en != 1)
- return -EINVAL;
-
err = mlxbf_pmc_config_l3_counters(blk_num, false, !!en);
if (err)
return err;
--
2.43.2
More information about the kernel-team
mailing list