devm_kasprintf() returns a pointer to dynamically allocated memory
which can be NULL upon failure.
devm_hwmon_device_register_with_groups return a error pointer upon failure.
Compile-tested only.
Fixes: 1a218d312e65 ("platform/mellanox: mlxbf-pmc: Add Mellanox BlueField PMC driver")
Suggested-by: Ilpo Järvinen <[email protected]>
Suggested-by: Vadim Pasternak <[email protected]>
Signed-off-by: Kunwu Chan <[email protected]>
---
drivers/platform/mellanox/mlxbf-pmc.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/drivers/platform/mellanox/mlxbf-pmc.c b/drivers/platform/mellanox/mlxbf-pmc.c
index 0b427fc24a96..1dd84c7a79de 100644
--- a/drivers/platform/mellanox/mlxbf-pmc.c
+++ b/drivers/platform/mellanox/mlxbf-pmc.c
@@ -1771,6 +1771,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
attr->dev_attr.show = mlxbf_pmc_event_list_show;
attr->nr = blk_num;
attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL, "event_list");
+ if (!attr->dev_attr.attr.name)
+ return -ENOMEM;
pmc->block[blk_num].block_attr[i] = &attr->dev_attr.attr;
attr = NULL;
@@ -1784,6 +1786,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
attr->nr = blk_num;
attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
"enable");
+ if (!attr->dev_attr.attr.name)
+ return -ENOMEM;
pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
attr = NULL;
}
@@ -1810,6 +1814,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
attr->nr = blk_num;
attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
"counter%d", j);
+ if (!attr->dev_attr.attr.name)
+ return -ENOMEM;
pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
attr = NULL;
@@ -1821,6 +1827,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
attr->nr = blk_num;
attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
"event%d", j);
+ if (!attr->dev_attr.attr.name)
+ return -ENOMEM;
pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
attr = NULL;
}
@@ -1853,6 +1861,8 @@ static int mlxbf_pmc_init_perftype_reg(struct device *dev, int blk_num)
attr->nr = blk_num;
attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
events[j].evt_name);
+ if (!attr->dev_attr.attr.name)
+ return -ENOMEM;
pmc->block[blk_num].block_attr[i] = &attr->dev_attr.attr;
attr = NULL;
i++;
@@ -1882,6 +1892,8 @@ static int mlxbf_pmc_create_groups(struct device *dev, int blk_num)
pmc->block[blk_num].block_attr_grp.attrs = pmc->block[blk_num].block_attr;
pmc->block[blk_num].block_attr_grp.name = devm_kasprintf(
dev, GFP_KERNEL, pmc->block_name[blk_num]);
+ if (!pmc->block[blk_num].block_attr_grp.name)
+ return -ENOMEM;
pmc->groups[pmc->group_num] = &pmc->block[blk_num].block_attr_grp;
pmc->group_num++;
@@ -2063,6 +2075,8 @@ static int mlxbf_pmc_probe(struct platform_device *pdev)
pmc->hwmon_dev = devm_hwmon_device_register_with_groups(
dev, "bfperf", pmc, pmc->groups);
+ if (IS_ERR(pmc->hwmon_dev))
+ return PTR_ERR(pmc->hwmon_dev);
platform_set_drvdata(pdev, pmc);
return 0;
--
2.34.1
> -----Original Message-----
> From: Kunwu Chan <[email protected]>
> Sent: Friday, 1 December 2023 7:55
> To: [email protected]; [email protected]; Vadim Pasternak
> <[email protected]>; [email protected]; [email protected]; Shravan
> Ramani <[email protected]>; [email protected]
> Cc: [email protected]; [email protected]; linux-
> [email protected]; [email protected]; Kunwu Chan
> <[email protected]>
> Subject: [PATCH v2] platform/mellanox: Add some null/error pointer checks to
> mlxbf-pmc.c
>
> devm_kasprintf() returns a pointer to dynamically allocated memory which
> can be NULL upon failure.
> devm_hwmon_device_register_with_groups return a error pointer upon
> failure.
>
> Compile-tested only.
>
> Fixes: 1a218d312e65 ("platform/mellanox: mlxbf-pmc: Add Mellanox
> BlueField PMC driver")
> Suggested-by: Ilpo Järvinen <[email protected]>
> Suggested-by: Vadim Pasternak <[email protected]>
> Signed-off-by: Kunwu Chan <[email protected]>
Reviewed-by: Vadim Pasternak <[email protected]>
> ---
> drivers/platform/mellanox/mlxbf-pmc.c | 14 ++++++++++++++
> 1 file changed, 14 insertions(+)
>
> diff --git a/drivers/platform/mellanox/mlxbf-pmc.c
> b/drivers/platform/mellanox/mlxbf-pmc.c
> index 0b427fc24a96..1dd84c7a79de 100644
> --- a/drivers/platform/mellanox/mlxbf-pmc.c
> +++ b/drivers/platform/mellanox/mlxbf-pmc.c
> @@ -1771,6 +1771,8 @@ static int mlxbf_pmc_init_perftype_counter(struct
> device *dev, int blk_num)
> attr->dev_attr.show = mlxbf_pmc_event_list_show;
> attr->nr = blk_num;
> attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
> "event_list");
> + if (!attr->dev_attr.attr.name)
> + return -ENOMEM;
> pmc->block[blk_num].block_attr[i] = &attr->dev_attr.attr;
> attr = NULL;
>
> @@ -1784,6 +1786,8 @@ static int mlxbf_pmc_init_perftype_counter(struct
> device *dev, int blk_num)
> attr->nr = blk_num;
> attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
> "enable");
> + if (!attr->dev_attr.attr.name)
> + return -ENOMEM;
> pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
> attr = NULL;
> }
> @@ -1810,6 +1814,8 @@ static int mlxbf_pmc_init_perftype_counter(struct
> device *dev, int blk_num)
> attr->nr = blk_num;
> attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
> "counter%d", j);
> + if (!attr->dev_attr.attr.name)
> + return -ENOMEM;
> pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
> attr = NULL;
>
> @@ -1821,6 +1827,8 @@ static int mlxbf_pmc_init_perftype_counter(struct
> device *dev, int blk_num)
> attr->nr = blk_num;
> attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
> "event%d", j);
> + if (!attr->dev_attr.attr.name)
> + return -ENOMEM;
> pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
> attr = NULL;
> }
> @@ -1853,6 +1861,8 @@ static int mlxbf_pmc_init_perftype_reg(struct
> device *dev, int blk_num)
> attr->nr = blk_num;
> attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
> events[j].evt_name);
> + if (!attr->dev_attr.attr.name)
> + return -ENOMEM;
> pmc->block[blk_num].block_attr[i] = &attr->dev_attr.attr;
> attr = NULL;
> i++;
> @@ -1882,6 +1892,8 @@ static int mlxbf_pmc_create_groups(struct device
> *dev, int blk_num)
> pmc->block[blk_num].block_attr_grp.attrs = pmc-
> >block[blk_num].block_attr;
> pmc->block[blk_num].block_attr_grp.name = devm_kasprintf(
> dev, GFP_KERNEL, pmc->block_name[blk_num]);
> + if (!pmc->block[blk_num].block_attr_grp.name)
> + return -ENOMEM;
> pmc->groups[pmc->group_num] = &pmc-
> >block[blk_num].block_attr_grp;
> pmc->group_num++;
>
> @@ -2063,6 +2075,8 @@ static int mlxbf_pmc_probe(struct platform_device
> *pdev)
>
> pmc->hwmon_dev = devm_hwmon_device_register_with_groups(
> dev, "bfperf", pmc, pmc->groups);
> + if (IS_ERR(pmc->hwmon_dev))
> + return PTR_ERR(pmc->hwmon_dev);
> platform_set_drvdata(pdev, pmc);
>
> return 0;
> --
> 2.34.1
> > Subject: [PATCH v2] platform/mellanox: Add some null/error pointer checks to
> > mlxbf-pmc.c
> >
> > devm_kasprintf() returns a pointer to dynamically allocated memory which
> > can be NULL upon failure.
> > devm_hwmon_device_register_with_groups return a error pointer upon
> > failure.
> >
> > Compile-tested only.
> >
> > Fixes: 1a218d312e65 ("platform/mellanox: mlxbf-pmc: Add Mellanox
> > BlueField PMC driver")
> > Suggested-by: Ilpo Järvinen <[email protected]>
> > Suggested-by: Vadim Pasternak <[email protected]>
> > Signed-off-by: Kunwu Chan <[email protected]>
>
> Reviewed-by: Vadim Pasternak <[email protected]>
> > attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
> > "enable");
> > + if (!attr->dev_attr.attr.name)
> > + return -ENOMEM;
> > pmc->hwmon_dev = devm_hwmon_device_register_with_groups(
> > dev, "bfperf", pmc, pmc->groups);
> > + if (IS_ERR(pmc->hwmon_dev))
> > + return PTR_ERR(pmc->hwmon_dev);
Thank you both, applied to review-ilpo and will go into fixes branch once
LKP has had its chance to build this.
In the end, I decided to split the devm_kasprintf() and
devm_hwmon_device_register_with_groups() changes into separate commits.
--
i.