[PATCH linux-next 02/33] clk: mediatek: Fix NULL pointer dereference in pdev remove

From: Akari Tsuyukusa

Date: Sat Nov 28 2026 - 20:47:49 EST


__mtk_clk_simple_remove() only calls device_get_match_data() and does
not fall back to platform_get_device_id() to retrieve mcd,
so when removing clock drivers which are not registered from devicetree
such as mmsys, mcd becomes NULL and should cause a kernel panic due to
a NULL pointer dereference.

Extract the descriptor lookup logic into mtk_clk_get_desc() and use it
in both __mtk_clk_simple_probe() and __mtk_clk_simple_remove().

Fixes: 252091242404 ("clk: mediatek: clk-mtk: Introduce clk_mtk_pdev_{probe,remove}()")
Signed-off-by: Akari Tsuyukusa <akkun11.open@xxxxxxxxx>
---
drivers/clk/mediatek/clk-mtk.c | 33 +++++++++++++++++++++------------
1 file changed, 21 insertions(+), 12 deletions(-)

diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index f5938672ba20..5d8bcd09c9b5 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -466,25 +466,34 @@ void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
}
EXPORT_SYMBOL_GPL(mtk_clk_unregister_dividers);

+static const struct mtk_clk_desc *mtk_clk_get_desc(struct platform_device *pdev)
+{
+ const struct platform_device_id *id;
+ const struct mtk_clk_desc *mcd;
+
+ mcd = device_get_match_data(&pdev->dev);
+ if (mcd)
+ return mcd;
+
+ /* Clock driver wasn't registered from devicetree */
+ id = platform_get_device_id(pdev);
+ if (id)
+ return (const struct mtk_clk_desc *)id->driver_data;
+
+ return NULL;
+}
+
static int __mtk_clk_simple_probe(struct platform_device *pdev,
struct device_node *node)
{
- const struct platform_device_id *id;
const struct mtk_clk_desc *mcd;
struct clk_hw_onecell_data *clk_data;
void __iomem *base = NULL;
int num_clks, r;

- mcd = device_get_match_data(&pdev->dev);
- if (!mcd) {
- /* Clock driver wasn't registered from devicetree */
- id = platform_get_device_id(pdev);
- if (id)
- mcd = (const struct mtk_clk_desc *)id->driver_data;
-
- if (!mcd)
- return -EINVAL;
- }
+ mcd = mtk_clk_get_desc(pdev);
+ if (!mcd)
+ return -EINVAL;

/* Composite and divider clocks needs us to pass iomem pointer */
if (mcd->composite_clks || mcd->divider_clks) {
@@ -668,7 +677,7 @@ static void __mtk_clk_simple_remove(struct platform_device *pdev,
struct device_node *node)
{
struct clk_hw_onecell_data *clk_data = platform_get_drvdata(pdev);
- const struct mtk_clk_desc *mcd = device_get_match_data(&pdev->dev);
+ const struct mtk_clk_desc *mcd = mtk_clk_get_desc(pdev);

if (mcd->populate_children)
of_platform_depopulate(&pdev->dev);
--
2.56.0