Re: [PATCH 3/5] mm/vmstat: use cmpxchg loop in cpu_vm_stats_fold

Matthew Wilcox
2023-02-06T19:19:22Z

On Wed, Feb 01, 2023 at 04:50:16PM -0300, Marcelo Tosatti wrote:
> In preparation to switch vmstat shepherd to flush
> per-CPU counters remotely, use a cmpxchg loop
> instead of a pair of read/write instructions.

FYI, try_cmpxchg() is preferred to plain cmpxchg() these days.
Apparently it generates better code on x86.

> - v = pzstats->vm_stat_diff[i];
> - pzstats->vm_stat_diff[i] = 0;
> + do {
> + v = pzstats->vm_stat_diff[i];
> + } while (cmpxchg(&pzstats->vm_stat_diff[i], v, 0) != v);

I think this would be:

do {
v = pzstats->vm_stat_diff[i];
} while (!try_cmpxchg(&pzstats->vm_stat_diff[i], v, 0));