Re: [PATCH] lib/genalloc: use try_cmpxchg in {set,clear}_bits_ll

From: Andrew Morton
Date: Wed Jan 18 2023 - 16:18:36 EST


On Wed, 18 Jan 2023 16:07:03 +0100 Uros Bizjak <ubizjak@xxxxxxxxx> wrote:

> Use try_cmpxchg instead of cmpxchg (*ptr, old, new) == old in
> {set,clear}_bits_ll. x86 CMPXCHG instruction returns success in ZF
> flag, so this change saves a compare after cmpxchg (and related move
> instruction in front of cmpxchg).
>
> Also, try_cmpxchg implicitly assigns old *ptr value to "old"
> when cmpxchg fails.
>
> Note that the value from *ptr should be read using READ_ONCE to prevent
> the compiler from merging, refetching or reordering the read.
>
> The patch also declares these two functions inline, to ensure inlining.

But why is that better? This adds a few hundred bytes more text, which
has a cost.