Message ID | 20221109012011.881058-1-shakeelb@google.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | [v2] percpu_counter: add percpu_counter_sum_all interface | expand |
On Wed, 9 Nov 2022 01:20:11 +0000 Shakeel Butt <shakeelb@google.com> wrote: > 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. LGTM. What are the user-visible effects of this bug? Should we cc:stable? If so, can you identify a suitable Fixes:? Thanks.
On Tue, Nov 8, 2022 at 5:26 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > On Wed, 9 Nov 2022 01:20:11 +0000 Shakeel Butt <shakeelb@google.com> wrote: > > > 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. > > LGTM. > > What are the user-visible effects of this bug? Should we cc:stable? If > so, can you identify a suitable Fixes:? > The patch which exposed this is still in mm-unstable (converting rss stats in mm_struct into percpu_counter) and other percpu_counter users don't really need this new interface. So, there is no need for a cc:stable. thanks, Shakeel
diff --git a/include/linux/percpu_counter.h b/include/linux/percpu_counter.h index bde6c4c1f405..a3aae8d57a42 100644 --- a/include/linux/percpu_counter.h +++ b/include/linux/percpu_counter.h @@ -45,6 +45,7 @@ void percpu_counter_set(struct percpu_counter *fbc, s64 amount); void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch); s64 __percpu_counter_sum(struct percpu_counter *fbc); +s64 percpu_counter_sum_all(struct percpu_counter *fbc); int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch); void percpu_counter_sync(struct percpu_counter *fbc); @@ -193,6 +194,11 @@ static inline s64 percpu_counter_sum(struct percpu_counter *fbc) return percpu_counter_read(fbc); } +static inline s64 percpu_counter_sum_all(struct percpu_counter *fbc) +{ + return percpu_counter_read(fbc); +} + static inline bool percpu_counter_initialized(struct percpu_counter *fbc) { return true; diff --git a/kernel/fork.c b/kernel/fork.c index 9c32f593ef11..9f7fe3541897 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -758,6 +758,11 @@ static void check_mm(struct mm_struct *mm) for (i = 0; i < NR_MM_COUNTERS; i++) { long x = percpu_counter_sum(&mm->rss_stat[i]); + if (likely(!x)) + continue; + + /* Making sure this is not due to race with CPU offlining. */ + x = percpu_counter_sum_all(&mm->rss_stat[i]); if (unlikely(x)) pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld\n", mm, resident_page_types[i], x); diff --git a/lib/percpu_counter.c b/lib/percpu_counter.c index ed610b75dc32..42f729c8e56c 100644 --- a/lib/percpu_counter.c +++ b/lib/percpu_counter.c @@ -117,11 +117,8 @@ void percpu_counter_sync(struct percpu_counter *fbc) } EXPORT_SYMBOL(percpu_counter_sync); -/* - * Add up all the per-cpu counts, return the result. This is a more accurate - * but much slower version of percpu_counter_read_positive() - */ -s64 __percpu_counter_sum(struct percpu_counter *fbc) +static s64 __percpu_counter_sum_mask(struct percpu_counter *fbc, + const struct cpumask *cpu_mask) { s64 ret; int cpu; @@ -129,15 +126,35 @@ s64 __percpu_counter_sum(struct percpu_counter *fbc) raw_spin_lock_irqsave(&fbc->lock, flags); ret = fbc->count; - for_each_online_cpu(cpu) { + for_each_cpu(cpu, cpu_mask) { s32 *pcount = per_cpu_ptr(fbc->counters, cpu); ret += *pcount; } raw_spin_unlock_irqrestore(&fbc->lock, flags); return ret; } + +/* + * Add up all the per-cpu counts, return the result. This is a more accurate + * but much slower version of percpu_counter_read_positive() + */ +s64 __percpu_counter_sum(struct percpu_counter *fbc) +{ + return __percpu_counter_sum_mask(fbc, cpu_online_mask); +} EXPORT_SYMBOL(__percpu_counter_sum); +/* + * This is slower version of percpu_counter_sum as it traverses all possible + * cpus. Use this only in the cases where accurate data is needed in the + * presense of CPUs getting offlined. + */ +s64 percpu_counter_sum_all(struct percpu_counter *fbc) +{ + return __percpu_counter_sum_mask(fbc, cpu_possible_mask); +} +EXPORT_SYMBOL(percpu_counter_sum_all); + int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp, struct lock_class_key *key) {