Skip to content

Commit d51e106

Browse files
jiaweichang-mtkvireshk
authored andcommitted
cpufreq: mediatek: fix KP caused by handler usage after regulator_put/clk_put
Any kind of failure in mtk_cpu_dvfs_info_init() will lead to calling regulator_put() or clk_put() and the KP will occur since the regulator/clk handlers are used after released in mtk_cpu_dvfs_info_release(). To prevent the usage after regulator_put()/clk_put(), the regulator/clk handlers are addressed in a way of "Free the Last Thing Style". Signed-off-by: Jia-Wei Chang <jia-wei.chang@mediatek.com> Fixes: 4b9ceb7 ("cpufreq: mediatek: Enable clocks and regulators") Suggested-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> Suggested-by: Dan Carpenter <error27@gmail.com> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
1 parent d51c632 commit d51e106

1 file changed

Lines changed: 30 additions & 32 deletions

File tree

drivers/cpufreq/mediatek-cpufreq.c

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -420,36 +420,36 @@ static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info *info, int cpu)
420420
ret = PTR_ERR(info->inter_clk);
421421
dev_err_probe(cpu_dev, ret,
422422
"cpu%d: failed to get intermediate clk\n", cpu);
423-
goto out_free_resources;
423+
goto out_free_mux_clock;
424424
}
425425

426426
info->proc_reg = regulator_get_optional(cpu_dev, "proc");
427427
if (IS_ERR(info->proc_reg)) {
428428
ret = PTR_ERR(info->proc_reg);
429429
dev_err_probe(cpu_dev, ret,
430430
"cpu%d: failed to get proc regulator\n", cpu);
431-
goto out_free_resources;
431+
goto out_free_inter_clock;
432432
}
433433

434434
ret = regulator_enable(info->proc_reg);
435435
if (ret) {
436436
dev_warn(cpu_dev, "cpu%d: failed to enable vproc\n", cpu);
437-
goto out_free_resources;
437+
goto out_free_proc_reg;
438438
}
439439

440440
/* Both presence and absence of sram regulator are valid cases. */
441441
info->sram_reg = regulator_get_optional(cpu_dev, "sram");
442442
if (IS_ERR(info->sram_reg)) {
443443
ret = PTR_ERR(info->sram_reg);
444444
if (ret == -EPROBE_DEFER)
445-
goto out_free_resources;
445+
goto out_disable_proc_reg;
446446

447447
info->sram_reg = NULL;
448448
} else {
449449
ret = regulator_enable(info->sram_reg);
450450
if (ret) {
451451
dev_warn(cpu_dev, "cpu%d: failed to enable vsram\n", cpu);
452-
goto out_free_resources;
452+
goto out_free_sram_reg;
453453
}
454454
}
455455

@@ -458,13 +458,13 @@ static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info *info, int cpu)
458458
if (ret) {
459459
dev_err(cpu_dev,
460460
"cpu%d: failed to get OPP-sharing information\n", cpu);
461-
goto out_free_resources;
461+
goto out_disable_sram_reg;
462462
}
463463

464464
ret = dev_pm_opp_of_cpumask_add_table(&info->cpus);
465465
if (ret) {
466466
dev_warn(cpu_dev, "cpu%d: no OPP table\n", cpu);
467-
goto out_free_resources;
467+
goto out_disable_sram_reg;
468468
}
469469

470470
ret = clk_prepare_enable(info->cpu_clk);
@@ -533,43 +533,41 @@ static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info *info, int cpu)
533533
out_free_opp_table:
534534
dev_pm_opp_of_cpumask_remove_table(&info->cpus);
535535

536-
out_free_resources:
537-
if (regulator_is_enabled(info->proc_reg))
538-
regulator_disable(info->proc_reg);
539-
if (info->sram_reg && regulator_is_enabled(info->sram_reg))
536+
out_disable_sram_reg:
537+
if (info->sram_reg)
540538
regulator_disable(info->sram_reg);
541539

542-
if (!IS_ERR(info->proc_reg))
543-
regulator_put(info->proc_reg);
544-
if (!IS_ERR(info->sram_reg))
540+
out_free_sram_reg:
541+
if (info->sram_reg)
545542
regulator_put(info->sram_reg);
546-
if (!IS_ERR(info->cpu_clk))
547-
clk_put(info->cpu_clk);
548-
if (!IS_ERR(info->inter_clk))
549-
clk_put(info->inter_clk);
543+
544+
out_disable_proc_reg:
545+
regulator_disable(info->proc_reg);
546+
547+
out_free_proc_reg:
548+
regulator_put(info->proc_reg);
549+
550+
out_free_inter_clock:
551+
clk_put(info->inter_clk);
552+
553+
out_free_mux_clock:
554+
clk_put(info->cpu_clk);
550555

551556
return ret;
552557
}
553558

554559
static void mtk_cpu_dvfs_info_release(struct mtk_cpu_dvfs_info *info)
555560
{
556-
if (!IS_ERR(info->proc_reg)) {
557-
regulator_disable(info->proc_reg);
558-
regulator_put(info->proc_reg);
559-
}
560-
if (!IS_ERR(info->sram_reg)) {
561+
regulator_disable(info->proc_reg);
562+
regulator_put(info->proc_reg);
563+
if (info->sram_reg) {
561564
regulator_disable(info->sram_reg);
562565
regulator_put(info->sram_reg);
563566
}
564-
if (!IS_ERR(info->cpu_clk)) {
565-
clk_disable_unprepare(info->cpu_clk);
566-
clk_put(info->cpu_clk);
567-
}
568-
if (!IS_ERR(info->inter_clk)) {
569-
clk_disable_unprepare(info->inter_clk);
570-
clk_put(info->inter_clk);
571-
}
572-
567+
clk_disable_unprepare(info->cpu_clk);
568+
clk_put(info->cpu_clk);
569+
clk_disable_unprepare(info->inter_clk);
570+
clk_put(info->inter_clk);
573571
dev_pm_opp_of_cpumask_remove_table(&info->cpus);
574572
dev_pm_opp_unregister_notifier(info->cpu_dev, &info->opp_nb);
575573
}

0 commit comments

Comments
 (0)