Skip to content

Commit 9795607

Browse files
vireshkrafaeljw
authored andcommitted
cpufreq: stats: Fix concurrency issues while resetting stats
It is possible for cpufreq_stats_clear_table() and cpufreq_stats_record_transition() to get called concurrently and they will try to update same variables simultaneously and may lead to corruption of data. Prevent that with the help of existing spinlock. Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 10b8182 commit 9795607

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

drivers/cpufreq/cpufreq_stats.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ static void cpufreq_stats_update(struct cpufreq_stats *stats)
3131
{
3232
unsigned long long cur_time = get_jiffies_64();
3333

34-
spin_lock(&cpufreq_stats_lock);
3534
stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
3635
stats->last_time = cur_time;
37-
spin_unlock(&cpufreq_stats_lock);
3836
}
3937

4038
static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
4139
{
4240
unsigned int count = stats->max_state;
4341

42+
spin_lock(&cpufreq_stats_lock);
4443
memset(stats->time_in_state, 0, count * sizeof(u64));
4544
memset(stats->trans_table, 0, count * count * sizeof(int));
4645
stats->last_time = get_jiffies_64();
4746
stats->total_trans = 0;
47+
spin_unlock(&cpufreq_stats_lock);
4848
}
4949

5050
static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
@@ -62,7 +62,10 @@ static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
6262
if (policy->fast_switch_enabled)
6363
return 0;
6464

65+
spin_lock(&cpufreq_stats_lock);
6566
cpufreq_stats_update(stats);
67+
spin_unlock(&cpufreq_stats_lock);
68+
6669
for (i = 0; i < stats->state_num; i++) {
6770
len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
6871
(unsigned long long)
@@ -239,9 +242,11 @@ void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
239242
if (old_index == -1 || new_index == -1 || old_index == new_index)
240243
return;
241244

245+
spin_lock(&cpufreq_stats_lock);
242246
cpufreq_stats_update(stats);
243247

244248
stats->last_index = new_index;
245249
stats->trans_table[old_index * stats->max_state + new_index]++;
246250
stats->total_trans++;
251+
spin_unlock(&cpufreq_stats_lock);
247252
}

0 commit comments

Comments
 (0)