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.
Unified · Split
| @@ -31,20 +31,20 @@ static void cpufreq_stats_update(struct cpufreq_stats *stats) · 30 unmodified lines | ||
| 31 | 31 | { |
| 32 | 32 | unsigned long long cur_time = get_jiffies_64(); |
| 33 | 33 | |
| 34 | − spin_lock(&cpufreq_stats_lock); | |
| 35 | 34 | stats->time_in_state[stats->last_index] += cur_time - stats->last_time; |
| 36 | 35 | stats->last_time = cur_time; |
| 37 | − spin_unlock(&cpufreq_stats_lock); | |
| 38 | 36 | } |
| 39 | 37 | |
| 40 | 38 | static void cpufreq_stats_clear_table(struct cpufreq_stats *stats) |
| 41 | 39 | { |
| 42 | 40 | unsigned int count = stats->max_state; |
| 43 | 41 | |
| 42 | + spin_lock(&cpufreq_stats_lock); | |
| 44 | 43 | memset(stats->time_in_state, 0, count * sizeof(u64)); |
| 45 | 44 | memset(stats->trans_table, 0, count * count * sizeof(int)); |
| 46 | 45 | stats->last_time = get_jiffies_64(); |
| 47 | 46 | stats->total_trans = 0; |
| 47 | + spin_unlock(&cpufreq_stats_lock); | |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | 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 | ||
| 62 | 62 | if (policy->fast_switch_enabled) |
| 63 | 63 | return 0; |
| 64 | 64 | |
| 65 | + spin_lock(&cpufreq_stats_lock); | |
| 65 | 66 | cpufreq_stats_update(stats); |
| 67 | + spin_unlock(&cpufreq_stats_lock); | |
| 68 | + | |
| 66 | 69 | for (i = 0; i < stats->state_num; i++) { |
| 67 | 70 | len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i], |
| 68 | 71 | (unsigned long long) |
| @@ -239,9 +242,11 @@ void cpufreq_stats_record_transition(struct cpufreq_policy *policy, · 170 unmodified lines | ||
| 239 | 242 | if (old_index == -1 || new_index == -1 || old_index == new_index) |
| 240 | 243 | return; |
| 241 | 244 | |
| 245 | + spin_lock(&cpufreq_stats_lock); | |
| 242 | 246 | cpufreq_stats_update(stats); |
| 243 | 247 | |
| 244 | 248 | stats->last_index = new_index; |
| 245 | 249 | stats->trans_table[old_index * stats->max_state + new_index]++; |
| 246 | 250 | stats->total_trans++; |
| 251 | + spin_unlock(&cpufreq_stats_lock); | |
| 247 | 252 | } |