History

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.

Viresh Kumar committed 7 years ago

Commit 97956071 parent 10b81821 file · +7 −2Browse files

Unified · Split

drivers/cpufreq/cpufreq_stats.c+7−2
@@ -31,20 +31,20 @@ static void cpufreq_stats_update(struct cpufreq_stats *stats) · 30 unmodified lines
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) · 11 unmodified lines
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, · 170 unmodified lines
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 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t