cpufreq Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix memory leak in cpufreq_stats.c
@ 2011-04-26 21:39 Steven Finney (Palm GBU)
  2011-04-29 19:25 ` Dave Jones
  0 siblings, 1 reply; 2+ messages in thread
From: Steven Finney (Palm GBU) @ 2011-04-26 21:39 UTC (permalink / raw)
  To: cpufreq@vger.kernel.org; +Cc: Steven Finney (Palm GBU)

[-- Attachment #1: Type: text/plain, Size: 2948 bytes --]

My first post here; hopefully I've done everything right.

Running kmemleak on a hotplug SMP system with CONFIG_CPU_FREQ_STAT=y
shows a cpufreq_stats related leak everytime a cpu is taken down 
(which may occur frequently on some devices).  Here is a proposed 
patch which fixes the problem. Tested on ARM 2.6.35, applies cleanly to
Linus' tree.

Thanks,
Steve

--------------------------------------

When a CPU is taken offline in an SMP system, cpufreq_remove_dev()
nulls out the per-cpu policy before cpufreq_stats_free_table() can
make use of it.  cpufreq_stats_free_table() then skips the
call to sysfs_remove_group(), leaving about 100 bytes of sysfs-related
memory unclaimed each time a CPU-removal occurs. Break up
cpu_stats_free_table into sysfs and table portions, and
call the sysfs portion early.

Change-Id: I4d2eab13a68ead5943fe89dae6f71862b4fb3dd9

Signed-off-by: Steven Finney <steven.finney@palm.com>
---
 drivers/cpufreq/cpufreq_stats.c |   21 ++++++++++++++++++---
 1 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/drivers/cpufreq/cpufreq_stats.c b/drivers/cpufreq/cpufreq_stats.c
index 00d73fc..4f1b8de 100644
--- a/drivers/cpufreq/cpufreq_stats.c
+++ b/drivers/cpufreq/cpufreq_stats.c
@@ -165,17 +165,27 @@ static int freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq)
 	return -1;
 }
 
+/* should be called late in the CPU removal sequence so that the stats
+ * memory is still available in case someone tries to use it.
+ */
 static void cpufreq_stats_free_table(unsigned int cpu)
 {
 	struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, cpu);
-	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
-	if (policy && policy->cpu == cpu)
-		sysfs_remove_group(&policy->kobj, &stats_attr_group);
 	if (stat) {
 		kfree(stat->time_in_state);
 		kfree(stat);
 	}
 	per_cpu(cpufreq_stats_table, cpu) = NULL;
+}
+
+/* must be called early in the CPU removal sequence (before
+ * cpufreq_remove_dev) so that policy is still valid.
+ */
+static void cpufreq_stats_free_sysfs(unsigned int cpu)
+{
+	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
+	if (policy && policy->cpu == cpu)
+		sysfs_remove_group(&policy->kobj, &stats_attr_group);
 	if (policy)
 		cpufreq_cpu_put(policy);
 }
@@ -316,6 +326,9 @@ static int __cpuinit cpufreq_stat_cpu_callback(struct notifier_block *nfb,
 	case CPU_ONLINE_FROZEN:
 		cpufreq_update_policy(cpu);
 		break;
+	case CPU_DOWN_PREPARE:
+		cpufreq_stats_free_sysfs(cpu);
+		break;
 	case CPU_DEAD:
 	case CPU_DEAD_FROZEN:
 		cpufreq_stats_free_table(cpu);
@@ -324,9 +337,11 @@ static int __cpuinit cpufreq_stat_cpu_callback(struct notifier_block *nfb,
 	return NOTIFY_OK;
 }
 
+/* priority=1 so this will get called before cpufreq_remove_dev */
 static struct notifier_block cpufreq_stat_cpu_notifier __refdata =
 {
 	.notifier_call = cpufreq_stat_cpu_callback,
+	.priority = 1,
 };
 
 static struct notifier_block notifier_policy_block = {
-- 
1.7.0.4



[-- Attachment #2: winmail.dat --]
[-- Type: application/ms-tnef, Size: 4175 bytes --]

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

* Re: [PATCH] Fix memory leak in cpufreq_stats.c
  2011-04-26 21:39 [PATCH] Fix memory leak in cpufreq_stats.c Steven Finney (Palm GBU)
@ 2011-04-29 19:25 ` Dave Jones
  0 siblings, 0 replies; 2+ messages in thread
From: Dave Jones @ 2011-04-29 19:25 UTC (permalink / raw)
  To: Steven Finney (Palm GBU); +Cc: cpufreq@vger.kernel.org

On Tue, Apr 26, 2011 at 02:39:15PM -0700, Steven Finney (Palm GBU) wrote:
 > My first post here; hopefully I've done everything right.
 > 
 > Running kmemleak on a hotplug SMP system with CONFIG_CPU_FREQ_STAT=y
 > shows a cpufreq_stats related leak everytime a cpu is taken down 
 > (which may occur frequently on some devices).  Here is a proposed 
 > patch which fixes the problem. Tested on ARM 2.6.35, applies cleanly to
 > Linus' tree.

I can't apply this, because your mail client sent it in a form
that isn't plaintext. Git doesn't eat base64, or winmail.dat.

	Dave

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

end of thread, other threads:[~2011-04-29 19:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-26 21:39 [PATCH] Fix memory leak in cpufreq_stats.c Steven Finney (Palm GBU)
2011-04-29 19:25 ` Dave Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox