[PATCH] drivers/perf: Add NULL check in thunderx2_pmu

From: Charles Han
Date: Thu Apr 10 2025 - 07:20:50 EST


devm_kasprintf() can return a NULL pointer on failure,but this
returned value in tx2_uncore_pmu_register() and
tx2_uncore_pmu_init_dev() is not checked.
Add NULL check in tx2_uncore_pmu_register() and
tx2_uncore_pmu_init_dev(), to handle kernel NULL
pointer dereference error.

Fixes: 69c32972d593 ("drivers/perf: Add Cavium ThunderX2 SoC UNCORE PMU driver")
Fixes: 5e2c27e833bb ("drivers/perf: Add CCPI2 PMU support in ThunderX2 UNCORE driver.")
Signed-off-by: Charles Han <hanchunchao@xxxxxxxxxx>
---
drivers/perf/thunderx2_pmu.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)

diff --git a/drivers/perf/thunderx2_pmu.c b/drivers/perf/thunderx2_pmu.c
index 6ed4707bd6bb..03dd297ff326 100644
--- a/drivers/perf/thunderx2_pmu.c
+++ b/drivers/perf/thunderx2_pmu.c
@@ -738,6 +738,8 @@ static int tx2_uncore_pmu_register(

tx2_pmu->pmu.name = devm_kasprintf(dev, GFP_KERNEL,
"%s", name);
+ if (!tx2_pmu->pmu.name)
+ return -ENOMEM;

return perf_pmu_register(&tx2_pmu->pmu, tx2_pmu->pmu.name, -1);
}
@@ -837,6 +839,11 @@ static struct tx2_uncore_pmu *tx2_uncore_pmu_init_dev(struct device *dev,
tx2_pmu->attr_groups = l3c_pmu_attr_groups;
tx2_pmu->name = devm_kasprintf(dev, GFP_KERNEL,
"uncore_l3c_%d", tx2_pmu->node);
+ if (!tx2_pmu->name) {
+ devm_kfree(dev, tx2_pmu);
+ return ERR_PTR(-ENOMEM);
+ }
+
tx2_pmu->init_cntr_base = init_cntr_base_l3c;
tx2_pmu->start_event = uncore_start_event_l3c;
tx2_pmu->stop_event = uncore_stop_event_l3c;
@@ -852,6 +859,11 @@ static struct tx2_uncore_pmu *tx2_uncore_pmu_init_dev(struct device *dev,
tx2_pmu->attr_groups = dmc_pmu_attr_groups;
tx2_pmu->name = devm_kasprintf(dev, GFP_KERNEL,
"uncore_dmc_%d", tx2_pmu->node);
+ if (!tx2_pmu->name) {
+ devm_kfree(dev, tx2_pmu);
+ return ERR_PTR(-ENOMEM);
+ }
+
tx2_pmu->init_cntr_base = init_cntr_base_dmc;
tx2_pmu->start_event = uncore_start_event_dmc;
tx2_pmu->stop_event = uncore_stop_event_dmc;
@@ -866,6 +878,11 @@ static struct tx2_uncore_pmu *tx2_uncore_pmu_init_dev(struct device *dev,
tx2_pmu->attr_groups = ccpi2_pmu_attr_groups;
tx2_pmu->name = devm_kasprintf(dev, GFP_KERNEL,
"uncore_ccpi2_%d", tx2_pmu->node);
+ if (!tx2_pmu->name) {
+ devm_kfree(dev, tx2_pmu);
+ return ERR_PTR(-ENOMEM);
+ }
+
tx2_pmu->init_cntr_base = init_cntr_base_ccpi2;
tx2_pmu->start_event = uncore_start_event_ccpi2;
tx2_pmu->stop_event = uncore_stop_event_ccpi2;
--
2.43.0