[PATCH v2 10/35] clk: mediatek: Fix memory leak in mtk_clk_register_gate()

From: Akari Tsuyukusa

Date: Tue Oct 13 2026 - 15:59:07 EST


When a gate uses the hardware voter ops but the hardware voter
regmap is missing (e.g. the "mediatek,hardware-voter" phandle is
absent from the device tree), mtk_clk_register_gate() returns an
error without freeing the mtk_clk_gate structure allocated earlier.

Free cg on this error path.

Fixes: e504d3bdb3d0 ("clk: mediatek: clk-gate: Add ops for gates with HW voter")
Assisted-by: LLM
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Akari Tsuyukusa <akkun11.open@xxxxxxxxx>
---
Newly introduced in v2.
---
drivers/clk/mediatek/clk-gate.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/mediatek/clk-gate.c b/drivers/clk/mediatek/clk-gate.c
index bd2fcd489a3f..0b1389993751 100644
--- a/drivers/clk/mediatek/clk-gate.c
+++ b/drivers/clk/mediatek/clk-gate.c
@@ -221,10 +221,12 @@ static struct clk_hw *mtk_clk_register_gate(struct device *dev,
init.parent_names = gate->parent_name ? &gate->parent_name : NULL;
init.num_parents = gate->parent_name ? 1 : 0;
init.ops = gate->ops;
- if (mtk_cg_uses_hwv(init.ops) && !regmap_hwv)
+ if (mtk_cg_uses_hwv(init.ops) && !regmap_hwv) {
+ kfree(cg);
return dev_err_ptr_probe(
dev, -ENXIO,
"regmap not found for hardware voter clocks\n");
+ }

cg->regmap = regmap;
cg->regmap_hwv = regmap_hwv;
--
2.56.0