[PATCH v2 1/3] perf/cxlpmu: Fix devm_kcalloc() argument order in cxl_pmu_probe()

Alok Tiwari posted 3 patches 3 months, 2 weeks ago
[PATCH v2 1/3] perf/cxlpmu: Fix devm_kcalloc() argument order in cxl_pmu_probe()
Posted by Alok Tiwari 3 months, 2 weeks ago
The previous code mistakenly swapped the count and size parameters.
This fix corrects the argument order in devm_kcalloc() to follow the
conventional count, size form, avoiding potential confusion or bugs.

Previous usage:
  devm_kcalloc(dev, sizeof(*info->hw_events), info->num_counters,
   GFP_KERNEL);

New usage:
  devm_kcalloc(dev, info->num_counters, sizeof(*info->hw_events),
   GFP_KERNEL);

Previous incorrect order could lead to unexpected memory allocation
behavior. This fix ensures correct allocation of hw_event structure.

Signed-off-by: Alok Tiwari <alok.a.tiwari@oracle.com>
---
 drivers/perf/cxl_pmu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/perf/cxl_pmu.c b/drivers/perf/cxl_pmu.c
index d6693519eaee2..8998c0a2f3a2d 100644
--- a/drivers/perf/cxl_pmu.c
+++ b/drivers/perf/cxl_pmu.c
@@ -834,8 +834,8 @@ static int cxl_pmu_probe(struct device *dev)
 	if (rc)
 		return rc;
 
-	info->hw_events = devm_kcalloc(dev, sizeof(*info->hw_events),
-				       info->num_counters, GFP_KERNEL);
+	info->hw_events = devm_kcalloc(dev, info->num_counters,
+				       sizeof(*info->hw_events), GFP_KERNEL);
 	if (!info->hw_events)
 		return -ENOMEM;
 
-- 
2.46.0
Re: [PATCH v2 1/3] perf/cxlpmu: Fix devm_kcalloc() argument order in cxl_pmu_probe()
Posted by Jonathan Cameron 3 months, 2 weeks ago
On Tue, 24 Jun 2025 12:43:38 -0700
Alok Tiwari <alok.a.tiwari@oracle.com> wrote:

> The previous code mistakenly swapped the count and size parameters.
> This fix corrects the argument order in devm_kcalloc() to follow the
> conventional count, size form, avoiding potential confusion or bugs.
> 
> Previous usage:
>   devm_kcalloc(dev, sizeof(*info->hw_events), info->num_counters,
>    GFP_KERNEL);
> 
> New usage:
>   devm_kcalloc(dev, info->num_counters, sizeof(*info->hw_events),
>    GFP_KERNEL);

Too much detail! The sentence above would have been enough given
we have the code change to see how it applied.
> 
> Previous incorrect order could lead to unexpected memory allocation
> behavior. This fix ensures correct allocation of hw_event structure.

It doesn't actually make any real difference.  Look at the implementation
of devm_kmalloc_array()  What it does is make it harder to reason about the
code and for that it is worth fixing up.

Anyhow none of that really matters.

Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
> 
> Signed-off-by: Alok Tiwari <alok.a.tiwari@oracle.com>
> ---
>  drivers/perf/cxl_pmu.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/perf/cxl_pmu.c b/drivers/perf/cxl_pmu.c
> index d6693519eaee2..8998c0a2f3a2d 100644
> --- a/drivers/perf/cxl_pmu.c
> +++ b/drivers/perf/cxl_pmu.c
> @@ -834,8 +834,8 @@ static int cxl_pmu_probe(struct device *dev)
>  	if (rc)
>  		return rc;
>  
> -	info->hw_events = devm_kcalloc(dev, sizeof(*info->hw_events),
> -				       info->num_counters, GFP_KERNEL);
> +	info->hw_events = devm_kcalloc(dev, info->num_counters,
> +				       sizeof(*info->hw_events), GFP_KERNEL);
>  	if (!info->hw_events)
>  		return -ENOMEM;
>