Skip to contentSkip to conversation

clk: mediatek: fix memory leak on module removal

PATCH Superseded Akari Tsuyukusa 3 participants 4 messages Jun 29 – Aug 6

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk: mediatek: fix memory leak on module removal

Some MediaTek clock drivers do not call platform_set_drvdata() during probe(), but their remove() callback calls platform_get_drvdata(). This results in platform_get_drvdata() returning NULL, which leads to calling mtk_free_clk_data(NULL) -> kfree(NULL).
Therefore, the actual clk_data is never released, causing a memory leak.

Fix this by calling platform_set_drvdata() during probe.

Fixes 124294ff468f ("clk: mediatek: mt8192: Move apmixedsys clock driver to its own file")
Fixes 4c02c9af3cb9 ("clk: mediatek: mt8173: Break down clock drivers and allow module build")
Fixes 54b7026f011e ("clk: mediatek: mt8135-apmixedsys: Convert to platform_driver and module")
Fixes c50e2ea6507b ("clk: mediatek: mt7622-apmixedsys: Add .remove() callback for module build")
Fixes 0d363282bb0c ("clk: mediatek: Add MediaTek Helio X10 MT6795 clock drivers")
Fixes c6368ce86435 ("clk: mediatek: mt2712-apmixedsys: Add .remove() callback for module build")
Fixes 838b86331c5e ("clk: mediatek: mt7622: Move infracfg to clk-mt7622-infracfg.c")
---
 drivers/clk/mediatek/clk-mt2712-apmixedsys.c | 1 +
 drivers/clk/mediatek/clk-mt6795-apmixedsys.c | 1 +
 drivers/clk/mediatek/clk-mt6795-infracfg.c   | 1 +
 drivers/clk/mediatek/clk-mt6795-pericfg.c    | 1 +
 drivers/clk/mediatek/clk-mt7622-apmixedsys.c | 1 +
 drivers/clk/mediatek/clk-mt7622-infracfg.c   | 1 +
 drivers/clk/mediatek/clk-mt8135-apmixedsys.c | 1 +
 drivers/clk/mediatek/clk-mt8173-apmixedsys.c | 1 +
 drivers/clk/mediatek/clk-mt8173-infracfg.c   | 1 +
 drivers/clk/mediatek/clk-mt8192-apmixedsys.c | 1 +
 10 files changed, 10 insertions(+)
drivers/clk/mediatek/clk-mt2712-apmixedsys.c +1 -0
index 54b18e9f83f8..24522fc24019 100644
@@ -129,6 +129,7 @@ static int clk_mt2712_apmixed_probe(struct platform_device *pdev)
 		goto unregister_plls;
 	}
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_plls:
drivers/clk/mediatek/clk-mt6795-apmixedsys.c +1 -0
index 123d5d7fea85..b607592a7c37 100644
@@ -175,6 +175,7 @@ static int clk_mt6795_apmixed_probe(struct platform_device *pdev)
 	dev_dbg(dev, "Performing initial setup for MD1\n");
 	clk_mt6795_apmixed_setup_md1(base);
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_ref2usb:
drivers/clk/mediatek/clk-mt6795-infracfg.c +1 -0
index e4559569f5b0..12146bb3b726 100644
@@ -116,6 +116,7 @@ static int clk_mt6795_infracfg_probe(struct platform_device *pdev)
 	if (ret)
 		goto unregister_cpumuxes;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_cpumuxes:
drivers/clk/mediatek/clk-mt6795-pericfg.c +1 -0
index d48240eb2a67..28faeb2e657a 100644
@@ -125,6 +125,7 @@ static int clk_mt6795_pericfg_probe(struct platform_device *pdev)
 	if (ret)
 		goto unregister_composites;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_composites:
drivers/clk/mediatek/clk-mt7622-apmixedsys.c +1 -0
index 8a29eaab0cfc..a9fc2e5536b3 100644
@@ -109,6 +109,7 @@ static int clk_mt7622_apmixed_probe(struct platform_device *pdev)
 	if (ret)
 		goto unregister_gates;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_gates:
drivers/clk/mediatek/clk-mt7622-infracfg.c +1 -0
index cfdf3b07c3e0..b44baf521d2f 100644
@@ -90,6 +90,7 @@ static int clk_mt7622_infracfg_probe(struct platform_device *pdev)
 	if (ret)
 		goto unregister_cpumuxes;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_cpumuxes:
drivers/clk/mediatek/clk-mt8135-apmixedsys.c +1 -0
index 19e4ee489ec3..41e5cfbcbb76 100644
@@ -66,6 +66,7 @@ static int clk_mt8135_apmixed_probe(struct platform_device *pdev)
 	if (ret)
 		goto unregister_plls;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_plls:
drivers/clk/mediatek/clk-mt8173-apmixedsys.c +1 -0
index d7d416172ab3..fe36d2eac3da 100644
@@ -179,6 +179,7 @@ static int clk_mt8173_apmixed_probe(struct platform_device *pdev)
 	if (r)
 		goto unregister_ref2usb;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_ref2usb:
drivers/clk/mediatek/clk-mt8173-infracfg.c +1 -0
index fa2d1d557e04..b923b73c64f6 100644
@@ -128,6 +128,7 @@ static int clk_mt8173_infracfg_probe(struct platform_device *pdev)
 	if (r)
 		goto unregister_clk_hw;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_clk_hw:
drivers/clk/mediatek/clk-mt8192-apmixedsys.c +1 -0
index b0563a285bd6..446c55b77777 100644
@@ -176,6 +176,7 @@ static int clk_mt8192_apmixed_probe(struct platform_device *pdev)
 	if (r)
 		goto unregister_gates;
 
+	platform_set_drvdata(pdev, clk_data);
 	return r;
 
 unregister_gates:
Signature · 1 line
2.54.0
#1 lore raw

Hi Akari,

Quoted 34 lines · Akari Tsuyukusa

Some MediaTek clock drivers do not call platform_set_drvdata() during probe(), but their remove() callback calls platform_get_drvdata(). This results in platform_get_drvdata() returning NULL, which leads to calling mtk_free_clk_data(NULL) -> kfree(NULL).
Therefore, the actual clk_data is never released, causing a memory leak.

Fix this by calling platform_set_drvdata() during probe.

Fixes 124294ff468f ("clk: mediatek: mt8192: Move apmixedsys clock driver to its own file")
Fixes 4c02c9af3cb9 ("clk: mediatek: mt8173: Break down clock drivers and allow module build")
Fixes 54b7026f011e ("clk: mediatek: mt8135-apmixedsys: Convert to platform_driver and module")
Fixes c50e2ea6507b ("clk: mediatek: mt7622-apmixedsys: Add .remove() callback for module build")
Fixes 0d363282bb0c ("clk: mediatek: Add MediaTek Helio X10 MT6795 clock drivers")
Fixes c6368ce86435 ("clk: mediatek: mt2712-apmixedsys: Add .remove() callback for module build")
Fixes 838b86331c5e ("clk: mediatek: mt7622: Move infracfg to clk-mt7622-infracfg.c")
---
 drivers/clk/mediatek/clk-mt2712-apmixedsys.c | 1 +
 drivers/clk/mediatek/clk-mt6795-apmixedsys.c | 1 +
 drivers/clk/mediatek/clk-mt6795-infracfg.c   | 1 +
 drivers/clk/mediatek/clk-mt6795-pericfg.c    | 1 +
 drivers/clk/mediatek/clk-mt7622-apmixedsys.c | 1 +
 drivers/clk/mediatek/clk-mt7622-infracfg.c   | 1 +
 drivers/clk/mediatek/clk-mt8135-apmixedsys.c | 1 +
 drivers/clk/mediatek/clk-mt8173-apmixedsys.c | 1 +
 drivers/clk/mediatek/clk-mt8173-infracfg.c   | 1 +
 drivers/clk/mediatek/clk-mt8192-apmixedsys.c | 1 +
 10 files changed, 10 insertions(+)
drivers/clk/mediatek/clk-mt2712-apmixedsys.c +0 -0
index 54b18e9f83f8..24522fc24019 100644

drivers/clk/mediatek/clk-mt2712-apmixedsys.c static int clk_mt2712_apmixed_probe(struct platform_device *pdev) · line 133

Quoted 5 lines
drivers/clk/mediatek/clk-mt2712-apmixedsys.c +1 -0
@@ -129,6 +129,7 @@ static int clk_mt2712_apmixed_probe(struct platform_device *pdev)
 		goto unregister_plls;
 	}
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;

Please add a newline before the return.

Also, please split each fix out into their own patch to make backporting to downstream kernels easier.

Brian

Quoted 113 lines
drivers/clk/mediatek/clk-mt2712-apmixedsys.c +0 -0
 unregister_plls:
drivers/clk/mediatek/clk-mt6795-apmixedsys.c +1 -0
index 123d5d7fea85..b607592a7c37 100644
@@ -175,6 +175,7 @@ static int clk_mt6795_apmixed_probe(struct platform_device *pdev)
 	dev_dbg(dev, "Performing initial setup for MD1\n");
 	clk_mt6795_apmixed_setup_md1(base);
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_ref2usb:
drivers/clk/mediatek/clk-mt6795-infracfg.c +1 -0
index e4559569f5b0..12146bb3b726 100644
@@ -116,6 +116,7 @@ static int clk_mt6795_infracfg_probe(struct platform_device *pdev)
 	if (ret)
 		goto unregister_cpumuxes;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_cpumuxes:
drivers/clk/mediatek/clk-mt6795-pericfg.c +1 -0
index d48240eb2a67..28faeb2e657a 100644
@@ -125,6 +125,7 @@ static int clk_mt6795_pericfg_probe(struct platform_device *pdev)
 	if (ret)
 		goto unregister_composites;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_composites:
drivers/clk/mediatek/clk-mt7622-apmixedsys.c +1 -0
index 8a29eaab0cfc..a9fc2e5536b3 100644
@@ -109,6 +109,7 @@ static int clk_mt7622_apmixed_probe(struct platform_device *pdev)
 	if (ret)
 		goto unregister_gates;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_gates:
drivers/clk/mediatek/clk-mt7622-infracfg.c +1 -0
index cfdf3b07c3e0..b44baf521d2f 100644
@@ -90,6 +90,7 @@ static int clk_mt7622_infracfg_probe(struct platform_device *pdev)
 	if (ret)
 		goto unregister_cpumuxes;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_cpumuxes:
drivers/clk/mediatek/clk-mt8135-apmixedsys.c +1 -0
index 19e4ee489ec3..41e5cfbcbb76 100644
@@ -66,6 +66,7 @@ static int clk_mt8135_apmixed_probe(struct platform_device *pdev)
 	if (ret)
 		goto unregister_plls;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_plls:
drivers/clk/mediatek/clk-mt8173-apmixedsys.c +1 -0
index d7d416172ab3..fe36d2eac3da 100644
@@ -179,6 +179,7 @@ static int clk_mt8173_apmixed_probe(struct platform_device *pdev)
 	if (r)
 		goto unregister_ref2usb;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_ref2usb:
drivers/clk/mediatek/clk-mt8173-infracfg.c +1 -0
index fa2d1d557e04..b923b73c64f6 100644
@@ -128,6 +128,7 @@ static int clk_mt8173_infracfg_probe(struct platform_device *pdev)
 	if (r)
 		goto unregister_clk_hw;
 
+	platform_set_drvdata(pdev, clk_data);
 	return 0;
 
 unregister_clk_hw:
drivers/clk/mediatek/clk-mt8192-apmixedsys.c +1 -0
index b0563a285bd6..446c55b77777 100644
@@ -176,6 +176,7 @@ static int clk_mt8192_apmixed_probe(struct platform_device *pdev)
 	if (r)
 		goto unregister_gates;
 
+	platform_set_drvdata(pdev, clk_data);
 	return r;
 
 unregister_gates:
Signature · 1 line
2.54.0
#2 lore raw

Hi Brian,

Quoted 9 lines · Brian Masney
Quoted 8 lines · Akari Tsuyukusa
drivers/clk/mediatek/clk-mt2712-apmixedsys.c +0 -0
index 54b18e9f83f8..24522fc24019 100644
@@ -129,6 +129,7 @@ static int clk_mt2712_apmixed_probe(struct platform_device *pdev)
              goto unregister_plls;
      }
 
Quoted 7 lines

drivers/clk/mediatek/clk-mt2712-apmixedsys.c static int clk_mt2712_apmixed_probe(struct platform_device *pdev) · line 133

Quoted 2 lines
drivers/clk/mediatek/clk-mt2712-apmixedsys.c +1 -0
+     platform_set_drvdata(pdev, clk_data);
      return 0;

Please add a newline before the return.

Also, please split each fix out into their own patch to make backporting to downstream kernels easier.

Thank you for your review.
I fixed them in v2.
https://lore.kernel.org/linux-mediatek/[email protected]/T/#t

Best regards,
Akari

#3 lore raw
In reply to Akari Tsuyukusa
kernel test robot noticed the following build errors:

Hi Akari,

kernel test robot noticed the following build errors:

[auto build test ERROR on clk/clk-next]
[also build test ERROR on linus/master v7.2-rc6 next-20260805] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Akari-Tsuyukusa/clk-mediatek-fix-memory-leak-on-module-removal/20260805-032732
base:   https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next
patch link:    https://lore.kernel.org/r/20260629142348.273766-1-akkun11.open%40gmail.com
patch subject: [PATCH] clk: mediatek: fix memory leak on module removal
config: sparc-randconfig-001-20260806 (https://download.01.org/0day-ci/archive/20260806/[email protected]/config)
compiler: sparc-linux-gcc (GCC) 16.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260806/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/clk/mediatek/clk-mt8173-infracfg.c: In function 'clk_mt8173_infracfg_probe':

>> drivers/clk/mediatek/clk-mt8173-infracfg.c:131:36: error: 'clk_data' undeclared (first use in this function); did you mean 'clk_gate'?
     131 |         platform_set_drvdata(pdev, clk_data);
         |                                    ^~~~~~~~
         |                                    clk_gate

drivers/clk/mediatek/clk-mt8173-infracfg.c:131:36: note: each undeclared identifier is reported only once for each function it appears in

vim +131 drivers/clk/mediatek/clk-mt8173-infracfg.c

    78	
    79	static void clk_mt8173_infra_init_early(struct device_node *node)
    80	{
    81		int i;
    82	
    83		infra_clk_data = mtk_alloc_clk_data(CLK_INFRA_NR_CLK);
    84		if (!infra_clk_data)
    85			return;
    86	
    87		for (i = 0; i < CLK_INFRA_NR_CLK; i++)
    88			infra_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
    89	
    90		mtk_clk_register_factors(infra_early_divs,
    91					 ARRAY_SIZE(infra_early_divs), infra_clk_data);
    92	
    93		of_clk_add_hw_provider(node, of_clk_hw_onecell_get, infra_clk_data);
    94	}
    95	CLK_OF_DECLARE_DRIVER(mtk_infrasys, "mediatek,mt8173-infracfg",
    96			      clk_mt8173_infra_init_early);
    97	
    98	static int clk_mt8173_infracfg_probe(struct platform_device *pdev)
    99	{
   100		struct device_node *node = pdev->dev.of_node;
   101		int r, i;
   102	
   103		if (!infra_clk_data) {
   104			infra_clk_data = mtk_alloc_clk_data(CLK_INFRA_NR_CLK);
   105			if (!infra_clk_data)
   106				return -ENOMEM;
   107		} else {
   108			for (i = 0; i < CLK_INFRA_NR_CLK; i++)
   109				if (infra_clk_data->hws[i] == ERR_PTR(-EPROBE_DEFER))
   110					infra_clk_data->hws[i] = ERR_PTR(-ENOENT);
   111		}
   112	
   113		r = mtk_clk_register_gates(&pdev->dev, node, infra_gates,
   114					   ARRAY_SIZE(infra_gates), infra_clk_data);
   115		if (r)
   116			return r;
   117	
   118		r = mtk_clk_register_cpumuxes(&pdev->dev, node, cpu_muxes,
   119					      ARRAY_SIZE(cpu_muxes), infra_clk_data);
   120		if (r)
   121			goto unregister_gates;
   122	
   123		r = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, infra_clk_data);
   124		if (r)
   125			goto unregister_cpumuxes;
   126	
   127		r = mtk_register_reset_controller_with_dev(&pdev->dev, &clk_rst_desc);
   128		if (r)
   129			goto unregister_clk_hw;
   130	
 > 131		platform_set_drvdata(pdev, clk_data);

132 return 0;
133
134 unregister_clk_hw:
135 of_clk_del_provider(node);
136 unregister_cpumuxes:
137 mtk_clk_unregister_cpumuxes(cpu_muxes, ARRAY_SIZE(cpu_muxes), infra_clk_data);
138 unregister_gates:
139 mtk_clk_unregister_gates(infra_gates, ARRAY_SIZE(infra_gates), infra_clk_data);
140 return r;
141 }
142

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki