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 V2 13/14] cpufreq: stats: Fix locking
Date: Fri,  2 Jan 2015 11:16:50 +0530	[thread overview]
Message-ID: <0447fedc2d07cd35eed66d7de97ad4e0940b6110.1420177186.git.viresh.kumar@linaro.org> (raw)
In-Reply-To: <cover.1420177186.git.viresh.kumar@linaro.org>
In-Reply-To: <cover.1420177186.git.viresh.kumar@linaro.org>

Locking wasn't handled properly at places and this patch is an attempt to fix
it. Specially while creating/removing stat tables.

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

diff --git a/drivers/cpufreq/cpufreq_stats.c b/drivers/cpufreq/cpufreq_stats.c
index de55ca86b6f1..95867985ea02 100644
--- a/drivers/cpufreq/cpufreq_stats.c
+++ b/drivers/cpufreq/cpufreq_stats.c
@@ -150,10 +150,12 @@ static void __cpufreq_stats_free_table(struct cpufreq_policy *policy)
 
 	pr_debug("%s: Free stat table\n", __func__);
 
+	mutex_lock(&cpufreq_stats_lock);
 	sysfs_remove_group(&policy->kobj, &stats_attr_group);
 	kfree(stat->time_in_state);
 	kfree(stat);
 	policy->stats_data = NULL;
+	mutex_unlock(&cpufreq_stats_lock);
 }
 
 static void cpufreq_stats_free_table(unsigned int cpu)
@@ -182,13 +184,17 @@ static int __cpufreq_stats_create_table(struct cpufreq_policy *policy)
 	if (unlikely(!table))
 		return 0;
 
+	mutex_lock(&cpufreq_stats_lock);
+
 	/* stats already initialized */
-	if (policy->stats_data)
-		return -EEXIST;
+	if (policy->stats_data) {
+		ret = -EEXIST;
+		goto unlock;
+	}
 
 	stat = kzalloc(sizeof(*stat), GFP_KERNEL);
 	if (!stat)
-		return -ENOMEM;
+		goto unlock;
 
 	/* Find total allocation size */
 	cpufreq_for_each_valid_entry(pos, table)
@@ -219,21 +225,21 @@ static int __cpufreq_stats_create_table(struct cpufreq_policy *policy)
 			stat->freq_table[i++] = pos->frequency;
 	stat->state_num = i;
 
-	mutex_lock(&cpufreq_stats_lock);
 	stat->last_time = get_jiffies_64();
 	stat->last_index = freq_table_get_index(stat, policy->cur);
-	mutex_unlock(&cpufreq_stats_lock);
 
 	policy->stats_data = stat;
 	ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
 	if (!ret)
-		return 0;
+		goto unlock;
 
 	/* We failed, release resources */
 	policy->stats_data = NULL;
 	kfree(stat->time_in_state);
 free_stat:
 	kfree(stat);
+unlock:
+	mutex_unlock(&cpufreq_stats_lock);
 
 	return ret;
 }
-- 
2.2.0


  parent reply	other threads:[~2015-01-02  5:47 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-02  5:46 [PATCH V2 00/14] cpufreq: stats: cleanups Viresh Kumar
2015-01-02  5:46 ` [PATCH V2 01/14] cpufreq: stats: don't break strings into multiple lines Viresh Kumar
2015-01-03 21:57   ` Rafael J. Wysocki
2015-01-04  3:15     ` Viresh Kumar
2015-01-02  5:46 ` [PATCH V2 02/14] cpufreq: stats: return -EEXIST when stats are already allocated Viresh Kumar
2015-01-03 21:58   ` Rafael J. Wysocki
2015-01-04  3:18     ` Viresh Kumar
2015-01-05 23:52       ` Rafael J. Wysocki
2015-01-06  3:42         ` Viresh Kumar
2015-01-02  5:46 ` [PATCH V2 03/14] cpufreq: stats: don't check for freq table while freeing stats Viresh Kumar
2015-01-02  5:46 ` [PATCH V2 04/14] cpufreq: stats: pass 'stat' to cpufreq_stats_update() Viresh Kumar
2015-01-03 22:04   ` Rafael J. Wysocki
2015-01-05  4:04     ` Viresh Kumar
     [not found]       ` <OF5E9CEA82.619493CC-ONC1257DC4.0027BC08-C1257DC4.0027BC14@lonatigroup.com>
2015-01-05  7:25         ` [PATCH V2 04/14] cpufreq: stats: pass 'stat' to cpufreq_stats_update() * Viresh Kumar
2015-01-02  5:46 ` [PATCH V2 05/14] cpufreq: stats: get rid of per-cpu cpufreq_stats_table Viresh Kumar
2015-01-03 22:20   ` Rafael J. Wysocki
2015-01-05  4:23     ` Viresh Kumar
2015-01-05 23:38       ` Rafael J. Wysocki
2015-01-02  5:46 ` [PATCH V2 06/14] cpufreq: Remove (now) unused 'last_cpu' from struct cpufreq_policy Viresh Kumar
2015-01-02  5:46 ` [PATCH V2 07/14] cpufreq: stats: remove cpufreq_stats_update_policy_cpu() Viresh Kumar
2015-01-03 22:25   ` Rafael J. Wysocki
2015-01-05  4:26     ` Viresh Kumar
2015-01-02  5:46 ` [PATCH V2 08/14] cpufreq: remove CPUFREQ_UPDATE_POLICY_CPU notifications Viresh Kumar
2015-01-03 22:28   ` Rafael J. Wysocki
2015-01-05  4:27     ` Viresh Kumar
2015-01-02  5:46 ` [PATCH V2 09/14] cpufreq: stats: create sysfs group once we are ready Viresh Kumar
2015-01-02  5:46 ` [PATCH V2 10/14] cpufreq: stats: don't update stats from show_trans_table() Viresh Kumar
2015-01-02  5:46 ` [PATCH V2 11/14] cpufreq: stats: don't update stats on false notifiers Viresh Kumar
2015-01-03 22:36   ` Rafael J. Wysocki
2015-01-02  5:46 ` [PATCH V2 12/14] cpufreq: stats: replace spinlock with mutex Viresh Kumar
2015-01-03 22:38   ` Rafael J. Wysocki
2015-01-02  5:46 ` Viresh Kumar [this message]
2015-01-03 22:43   ` [PATCH V2 13/14] cpufreq: stats: Fix locking Rafael J. Wysocki
2015-01-05  8:06     ` Viresh Kumar
2015-01-05 23:35       ` Rafael J. Wysocki
2015-01-06  3:34         ` Viresh Kumar
2015-01-02  5:46 ` [PATCH V2 14/14] cpufreq: stats: call cpufreq_stats_update() with locks held Viresh Kumar
2015-01-03 22:48   ` Rafael J. Wysocki
2015-01-02 13:08 ` [PATCH V2 00/14] cpufreq: stats: cleanups Prarit Bhargava

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=0447fedc2d07cd35eed66d7de97ad4e0940b6110.1420177186.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).