linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Viresh Kumar <viresh.kumar@linaro.org>
To: Rafael Wysocki <rjw@rjwysocki.net>
Cc: linaro-kernel@lists.linaro.org, linux-pm@vger.kernel.org,
	prarit@redhat.com, skannan@codeaurora.org,
	Viresh Kumar <viresh.kumar@linaro.org>
Subject: [PATCH V3 12/16] cpufreq: stats: create sysfs group once we are ready
Date: Tue,  6 Jan 2015 21:09:11 +0530	[thread overview]
Message-ID: <ba8516f9ffacf0d2c00cbdfbca5b7b363404534d.1420558386.git.viresh.kumar@linaro.org> (raw)
In-Reply-To: <cover.1420558386.git.viresh.kumar@linaro.org>
In-Reply-To: <cover.1420558386.git.viresh.kumar@linaro.org>

Userspace is free to read value of any file from cpufreq/stats directory once
they are created. __cpufreq_stats_create_table() is creating the sysfs files
first and then allocating resources for them. Though it would be quite difficult
to trigger the racy situation here, but for the sake of keeping sensible code
lets create sysfs entries only after we are ready to go.

This also does some makeup to the routine to make it look better.

Reviewed-by: Prarit Bhargava <prarit@redhat.com>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 drivers/cpufreq/cpufreq_stats.c | 44 +++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/drivers/cpufreq/cpufreq_stats.c b/drivers/cpufreq/cpufreq_stats.c
index 1ed703a3e21e..17df8c507594 100644
--- a/drivers/cpufreq/cpufreq_stats.c
+++ b/drivers/cpufreq/cpufreq_stats.c
@@ -164,12 +164,13 @@ static void cpufreq_stats_free_table(unsigned int cpu)
 
 static int __cpufreq_stats_create_table(struct cpufreq_policy *policy)
 {
-	unsigned int i, count = 0, ret = 0;
+	unsigned int i = 0, count = 0, ret = -ENOMEM;
 	struct cpufreq_stats *stats;
 	unsigned int alloc_size;
 	unsigned int cpu = policy->cpu;
 	struct cpufreq_frequency_table *pos, *table;
 
+	/* We need cpufreq table for creating stats table */
 	table = cpufreq_frequency_get_table(cpu);
 	if (unlikely(!table))
 		return 0;
@@ -179,15 +180,10 @@ static int __cpufreq_stats_create_table(struct cpufreq_policy *policy)
 		return -EEXIST;
 
 	stats = kzalloc(sizeof(*stats), GFP_KERNEL);
-	if ((stats) == NULL)
+	if (!stats)
 		return -ENOMEM;
 
-	ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
-	if (ret)
-		goto error_out;
-
-	policy->stats = stats;
-
+	/* Find total allocation size */
 	cpufreq_for_each_valid_entry(pos, table)
 		count++;
 
@@ -196,32 +192,42 @@ static int __cpufreq_stats_create_table(struct cpufreq_policy *policy)
 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
 	alloc_size += count * count * sizeof(int);
 #endif
-	stats->max_state = count;
+
+	/* Allocate memory for time_in_state/freq_table/trans_table in one go */
 	stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
-	if (!stats->time_in_state) {
-		ret = -ENOMEM;
-		goto error_alloc;
-	}
+	if (!stats->time_in_state)
+		goto free_stat;
+
 	stats->freq_table = (unsigned int *)(stats->time_in_state + count);
 
 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
 	stats->trans_table = stats->freq_table + count;
 #endif
-	i = 0;
+
+	stats->max_state = count;
+
+	/* Find valid-unique entries */
 	cpufreq_for_each_valid_entry(pos, table)
 		if (freq_table_get_index(stats, pos->frequency) == -1)
 			stats->freq_table[i++] = pos->frequency;
 	stats->state_num = i;
+
 	spin_lock(&cpufreq_stats_lock);
 	stats->last_time = get_jiffies_64();
 	stats->last_index = freq_table_get_index(stats, policy->cur);
 	spin_unlock(&cpufreq_stats_lock);
-	return 0;
-error_alloc:
-	sysfs_remove_group(&policy->kobj, &stats_attr_group);
-error_out:
-	kfree(stats);
+
+	policy->stats = stats;
+	ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
+	if (!ret)
+		return 0;
+
+	/* We failed, release resources */
 	policy->stats = NULL;
+	kfree(stats->time_in_state);
+free_stat:
+	kfree(stats);
+
 	return ret;
 }
 
-- 
2.2.0


  parent reply	other threads:[~2015-01-06 15:40 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-06 15:38 [PATCH V3 00/16] cpufreq: stats: cleanups Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 01/16] cpufreq: stats: Improve module description string Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 02/16] cpufreq: stats: return -EEXIST when stats are already allocated Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 03/16] cpufreq: stats: remove unused cpufreq_stats_attribute Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 04/16] cpufreq: stats: initialize 'cur_time' on its definition Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 05/16] cpufreq: stats: don't check for freq table while freeing stats Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 06/16] cpufreq: stats: pass 'stat' to cpufreq_stats_update() Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 07/16] cpufreq: stats: get rid of per-cpu cpufreq_stats_table Viresh Kumar
2015-01-13  6:04   ` [PATCH V3 Resend " Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 08/16] cpufreq: stats: rename 'struct cpufreq_stats' objects as 'stats' Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 09/16] cpufreq: Remove (now) unused 'last_cpu' from struct cpufreq_policy Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 10/16] cpufreq: stats: drop 'cpu' field of struct cpufreq_stats Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 11/16] cpufreq: remove CPUFREQ_UPDATE_POLICY_CPU notifications Viresh Kumar
2015-01-06 15:39 ` Viresh Kumar [this message]
2015-01-06 15:39 ` [PATCH V3 13/16] cpufreq: stats: time_in_state can't be NULL in cpufreq_stats_update() Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 14/16] cpufreq: stats: don't update stats from show_trans_table() Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 15/16] cpufreq: stats: don't update stats on false notifiers Viresh Kumar
2015-01-06 15:39 ` [PATCH V3 16/16] cpufreq: stats: drop unnecessary locking Viresh Kumar
2015-01-12  6:12 ` [PATCH V3 00/16] cpufreq: stats: cleanups Viresh Kumar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ba8516f9ffacf0d2c00cbdfbca5b7b363404534d.1420558386.git.viresh.kumar@linaro.org \
    --to=viresh.kumar@linaro.org \
    --cc=linaro-kernel@lists.linaro.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=prarit@redhat.com \
    --cc=rjw@rjwysocki.net \
    --cc=skannan@codeaurora.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).