All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cpufreq: imx6q: fix devres accumulation across driver rebind
@ 2026-08-06  5:09 Karl Mehltretter
  2026-08-06  5:17 ` sashiko-bot
  2026-08-06  6:12 ` Viresh Kumar
  0 siblings, 2 replies; 3+ messages in thread
From: Karl Mehltretter @ 2026-08-06  5:09 UTC (permalink / raw)
  To: Rafael J. Wysocki, Viresh Kumar
  Cc: Karl Mehltretter, Frank Li, Sascha Hauer, Shawn Guo,
	Fabio Estevam, Anson Huang, linux-pm, imx, linux-arm-kernel,
	linux-kernel

imx6_soc_volt is allocated with devm_kcalloc(cpu_dev, ...), where cpu_dev
is the CPU device from get_cpu_device(0). That device is never unbound, so
its devres list is never released, and imx6q_cpufreq_remove() does not free
the array either. Every probe therefore adds an allocation that stays for
the lifetime of the system.

Allocate against the platform device instead. Its devres is released when
the driver is unbound, which is exactly the lifetime the array wants:
imx6q_set_target() reads it, and nothing may reach that after
cpufreq_unregister_driver().

That makes the array actually go away on unbind, so also clear the
file-scope pointer in remove and on the failed-probe path, rather than
leave it pointing at memory devres is about to release.

Tested by rebinding the driver on qemu's mcimx6ul-evk.

Fixes: b4573d1d657a ("cpufreq: imx6q: correct VDDSOC/PU voltage scaling when cpufreq is changed")
Assisted-by: Claude:claude-opus-5
Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
---
 drivers/cpufreq/imx6q-cpufreq.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/cpufreq/imx6q-cpufreq.c b/drivers/cpufreq/imx6q-cpufreq.c
index e93697d3edfd9..8110c95059e0e 100644
--- a/drivers/cpufreq/imx6q-cpufreq.c
+++ b/drivers/cpufreq/imx6q-cpufreq.c
@@ -400,7 +400,7 @@ static int imx6q_cpufreq_probe(struct platform_device *pdev)
 	}
 
 	/* Make imx6_soc_volt array's size same as arm opp number */
-	imx6_soc_volt = devm_kcalloc(cpu_dev, num, sizeof(*imx6_soc_volt),
+	imx6_soc_volt = devm_kcalloc(&pdev->dev, num, sizeof(*imx6_soc_volt),
 				     GFP_KERNEL);
 	if (imx6_soc_volt == NULL) {
 		ret = -ENOMEM;
@@ -485,6 +485,7 @@ static int imx6q_cpufreq_probe(struct platform_device *pdev)
 	return 0;
 
 free_freq_table:
+	imx6_soc_volt = NULL;
 	dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
 out_free_opp:
 	dev_pm_opp_of_remove_table(cpu_dev);
@@ -506,6 +507,7 @@ static int imx6q_cpufreq_probe(struct platform_device *pdev)
 static void imx6q_cpufreq_remove(struct platform_device *pdev)
 {
 	cpufreq_unregister_driver(&imx6q_cpufreq_driver);
+	imx6_soc_volt = NULL;
 	dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
 	dev_pm_opp_of_remove_table(cpu_dev);
 	regulator_put(arm_reg);
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] cpufreq: imx6q: fix devres accumulation across driver rebind
  2026-08-06  5:09 [PATCH] cpufreq: imx6q: fix devres accumulation across driver rebind Karl Mehltretter
@ 2026-08-06  5:17 ` sashiko-bot
  2026-08-06  6:12 ` Viresh Kumar
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-08-06  5:17 UTC (permalink / raw)
  To: Karl Mehltretter; +Cc: imx, Frank.Li

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The file-scope static variable `soc_opp_count` is never reset to 0 across driver unbind/rebind or deferred probe cycles, leading to a severe out-of-bounds write array overflow.
--

--- Patch [1]: [PATCH] cpufreq: imx6q: fix devres accumulation across driver rebind ---
commit d8bcb5a2ed1157b0b23fd4c657a9fd392c54309b
Author: Karl Mehltretter <kmehltretter@gmail.com>

cpufreq: imx6q: fix devres accumulation across driver rebind

This commit fixes a memory leak caused by devres accumulation during driver
unbind and rebind cycles. It changes the memory allocation of imx6_soc_volt
to tie its lifetime to the platform device, and clears the file-scope pointer
in the remove and probe-failure paths.

> diff --git a/drivers/cpufreq/imx6q-cpufreq.c b/drivers/cpufreq/imx6q-cpufreq.c
> index e93697d3edfd9..8110c95059e0e 100644
> --- a/drivers/cpufreq/imx6q-cpufreq.c
> +++ b/drivers/cpufreq/imx6q-cpufreq.c

[ ... ]

> @@ -506,6 +507,7 @@ static int imx6q_cpufreq_probe(struct platform_device *pdev)
>  static void imx6q_cpufreq_remove(struct platform_device *pdev)
>  {
>  	cpufreq_unregister_driver(&imx6q_cpufreq_driver);
> +	imx6_soc_volt = NULL;

[Severity: High]
This is a pre-existing issue, but since this patch explicitly tests and 
fixes rebind behavior, should soc_opp_count also be reset to 0 here?

The file-scope static variable soc_opp_count is never reset to 0 across 
driver unbind/rebind or deferred probe cycles. 

In imx6q_cpufreq_probe(), the loop increments soc_opp_count:

	for (j = 0; j < num; j++) {
		val = prop->value;
		for (i = 0; i < nr / 2; i++) {
			unsigned long freq = be32_to_cpup(val++);
			unsigned long volt = be32_to_cpup(val++);
			if (freq_table[j].frequency == freq) {
				imx6_soc_volt[soc_opp_count++] = volt;
				break;
			}
		}
	}

On subsequent probes (e.g. after the unbind/rebind cycle you tested), it 
resumes counting from its previous value instead of 0. Since imx6_soc_volt 
is newly allocated with size 'num', this will write beyond the bounds of 
the array, causing memory corruption.

Does the driver need to reset soc_opp_count to 0 during removal and on the 
error paths in probe?

>  	dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
>  	dev_pm_opp_of_remove_table(cpu_dev);
>  	regulator_put(arm_reg);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260806050902.90550-1-kmehltretter@gmail.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] cpufreq: imx6q: fix devres accumulation across driver rebind
  2026-08-06  5:09 [PATCH] cpufreq: imx6q: fix devres accumulation across driver rebind Karl Mehltretter
  2026-08-06  5:17 ` sashiko-bot
@ 2026-08-06  6:12 ` Viresh Kumar
  1 sibling, 0 replies; 3+ messages in thread
From: Viresh Kumar @ 2026-08-06  6:12 UTC (permalink / raw)
  To: Karl Mehltretter
  Cc: Rafael J. Wysocki, Frank Li, Sascha Hauer, Shawn Guo,
	Fabio Estevam, Anson Huang, linux-pm, imx, linux-arm-kernel,
	linux-kernel

On 06-08-26, 07:09, Karl Mehltretter wrote:
> imx6_soc_volt is allocated with devm_kcalloc(cpu_dev, ...), where cpu_dev
> is the CPU device from get_cpu_device(0). That device is never unbound, so
> its devres list is never released, and imx6q_cpufreq_remove() does not free
> the array either. Every probe therefore adds an allocation that stays for
> the lifetime of the system.
> 
> Allocate against the platform device instead. Its devres is released when
> the driver is unbound, which is exactly the lifetime the array wants:
> imx6q_set_target() reads it, and nothing may reach that after
> cpufreq_unregister_driver().
> 
> That makes the array actually go away on unbind, so also clear the
> file-scope pointer in remove and on the failed-probe path, rather than
> leave it pointing at memory devres is about to release.
> 
> Tested by rebinding the driver on qemu's mcimx6ul-evk.
> 
> Fixes: b4573d1d657a ("cpufreq: imx6q: correct VDDSOC/PU voltage scaling when cpufreq is changed")
> Assisted-by: Claude:claude-opus-5
> Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
> ---
>  drivers/cpufreq/imx6q-cpufreq.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)

Applied. Thanks.

-- 
viresh

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-08-06  6:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-06  5:09 [PATCH] cpufreq: imx6q: fix devres accumulation across driver rebind Karl Mehltretter
2026-08-06  5:17 ` sashiko-bot
2026-08-06  6:12 ` Viresh Kumar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.