History

percpu_counter: add percpu_counter_sum_all interface

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".

Shakeel Butt committed 4 years ago

Commit f6890541 parent f1a79413 files · +34 −6Browse files

Unified · Split

3 files
  1. include/linux/percpu_counter.h +6 −0
  2. kernel/fork.c +5 −0
  3. lib/percpu_counter.c +23 −6
include/linux/percpu_counter.h+6−0
@@ -45,6 +45,7 @@ void percpu_counter_set(struct percpu_counter *fbc, s64 amount); · 44 unmodified lines
45void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount,45void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount,
46 s32 batch);46 s32 batch);
47s64 __percpu_counter_sum(struct percpu_counter *fbc);47s64 __percpu_counter_sum(struct percpu_counter *fbc);
48s64 percpu_counter_sum_all(struct percpu_counter *fbc);
48int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch);49int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch);
49void percpu_counter_sync(struct percpu_counter *fbc);50void 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
197static inline s64 percpu_counter_sum_all(struct percpu_counter *fbc)
198{
199 return percpu_counter_read(fbc);
200}
201
196static inline bool percpu_counter_initialized(struct percpu_counter *fbc)202static inline bool percpu_counter_initialized(struct percpu_counter *fbc)
197{203{
198 return true;204 return true;
kernel/fork.c+5−0
@@ -755,6 +755,11 @@ static void check_mm(struct mm_struct *mm) · 754 unmodified lines
755 for (i = 0; i < NR_MM_COUNTERS; i++) {755 for (i = 0; i < NR_MM_COUNTERS; i++) {
756 long x = percpu_counter_sum(&mm->rss_stat[i]);756 long x = percpu_counter_sum(&mm->rss_stat[i]);
757 757
758 if (likely(!x))
759 continue;
760
761 /* Making sure this is not due to race with CPU offlining. */
762 x = percpu_counter_sum_all(&mm->rss_stat[i]);
758 if (unlikely(x))763 if (unlikely(x))
759 pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld\n",764 pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld\n",
760 mm, resident_page_types[i], x);765 mm, resident_page_types[i], x);
lib/percpu_counter.c+23−6
@@ -117,11 +117,8 @@ void percpu_counter_sync(struct percpu_counter *fbc) · 116 unmodified lines
117}117}
118EXPORT_SYMBOL(percpu_counter_sync);118EXPORT_SYMBOL(percpu_counter_sync);
119 119
120/*120static s64 __percpu_counter_sum_mask(struct percpu_counter *fbc,
121 * Add up all the per-cpu counts, return the result. This is a more accurate121 const struct cpumask *cpu_mask)
122 * but much slower version of percpu_counter_read_positive()
123 */
124s64 __percpu_counter_sum(struct percpu_counter *fbc)
125{122{
126 s64 ret;123 s64 ret;
127 int cpu;124 int cpu;
@@ -129,15 +126,35 @@ s64 __percpu_counter_sum(struct percpu_counter *fbc) · 1 unmodified line
129 126
130 raw_spin_lock_irqsave(&fbc->lock, flags);127 raw_spin_lock_irqsave(&fbc->lock, flags);
131 ret = fbc->count;128 ret = fbc->count;
132 for_each_online_cpu(cpu) {129 for_each_cpu(cpu, cpu_mask) {
133 s32 *pcount = per_cpu_ptr(fbc->counters, cpu);130 s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
134 ret += *pcount;131 ret += *pcount;
135 }132 }
136 raw_spin_unlock_irqrestore(&fbc->lock, flags);133 raw_spin_unlock_irqrestore(&fbc->lock, flags);
137 return ret;134 return ret;
138}135}
136
137/*
138 * Add up all the per-cpu counts, return the result. This is a more accurate
139 * but much slower version of percpu_counter_read_positive()
140 */
141s64 __percpu_counter_sum(struct percpu_counter *fbc)
142{
143 return __percpu_counter_sum_mask(fbc, cpu_online_mask);
144}
139EXPORT_SYMBOL(__percpu_counter_sum);145EXPORT_SYMBOL(__percpu_counter_sum);
140 146
147/*
148 * This is slower version of percpu_counter_sum as it traverses all possible
149 * cpus. Use this only in the cases where accurate data is needed in the
150 * presense of CPUs getting offlined.
151 */
152s64 percpu_counter_sum_all(struct percpu_counter *fbc)
153{
154 return __percpu_counter_sum_mask(fbc, cpu_possible_mask);
155}
156EXPORT_SYMBOL(percpu_counter_sum_all);
157
141int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp,158int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp,
142 struct lock_class_key *key)159 struct lock_class_key *key)
143{160{