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