The percpu_counter is used for scenarios where performance is more important than the accuracy. For percpu_counter users, who want more accurate information in their slowpath, percpu_counter_sum is provided which traverses all the online CPUs to accumulate the data. The reason it only needs to traverse online CPUs is because percpu_counter does implement CPU offline callback which syncs the local data of the offlined CPU. However there is a small race window between the online CPUs traversal of percpu_counter_sum and the CPU offline callback. The offline callback has to traverse all the percpu_counters on the system to flush the CPU local data which can be a lot. During that time, the CPU which is going offline has already been published as offline to all the readers. So, as the offline callback is running, percpu_counter_sum can be called for one counter which has some state on the CPU going offline. Since percpu_counter_sum only traverses online CPUs, it will skip that specific CPU and the offline callback might not have flushed the state for that specific percpu_counter on that offlined CPU. Normally this is not an issue because percpu_counter users can deal with some inaccuracy for small time window. However a new user i.e. mm_struct on the cleanup path wants to check the exact state of the percpu_counter through check_mm(). For such users, this patch introduces percpu_counter_sum_all() which traverses all possible CPUs and it is used in fork.c:check_mm() to avoid the potential race. This issue is exposed by the later patch "mm: convert mm's rss stats into percpu_counter".
Showing the changes to include (1 of 3 files). Show all
Unified · Split
| @@ -45,6 +45,7 @@ void percpu_counter_set(struct percpu_counter *fbc, s64 amount); · 44 unmodified lines | |||
| 45 | void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, | 45 | void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, |
| 46 | s32 batch); | 46 | s32 batch); |
| 47 | s64 __percpu_counter_sum(struct percpu_counter *fbc); | 47 | s64 __percpu_counter_sum(struct percpu_counter *fbc); |
| 48 | s64 percpu_counter_sum_all(struct percpu_counter *fbc); | ||
| 48 | int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch); | 49 | int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch); |
| 49 | void percpu_counter_sync(struct percpu_counter *fbc); | 50 | void percpu_counter_sync(struct percpu_counter *fbc); |
| 50 | 51 | ||
| @@ -193,6 +194,11 @@ static inline s64 percpu_counter_sum(struct percpu_counter *fbc) · 142 unmodified lines | |||
| 193 | return percpu_counter_read(fbc); | 194 | return percpu_counter_read(fbc); |
| 194 | } | 195 | } |
| 195 | 196 | ||
| 197 | static inline s64 percpu_counter_sum_all(struct percpu_counter *fbc) | ||
| 198 | { | ||
| 199 | return percpu_counter_read(fbc); | ||
| 200 | } | ||
| 201 | |||
| 196 | static inline bool percpu_counter_initialized(struct percpu_counter *fbc) | 202 | static inline bool percpu_counter_initialized(struct percpu_counter *fbc) |
| 197 | { | 203 | { |
| 198 | return true; | 204 | return true; |