Re: [PATCH] block: Use try_cmpxchg some more

From: Uros Bizjak
Date: Tue Jul 12 2022 - 03:05:09 EST

On Tue, Jul 12, 2022 at 7:13 AM Christoph Hellwig <hch@xxxxxxxxxxxxx> wrote:
> On Mon, Jul 11, 2022 at 05:33:01PM +0200, Uros Bizjak wrote:
> > Use try_cmpxchg family of functions instead of cmpxchg (*ptr, old, new) == old.
> > 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, enabling further code simplifications.
> >
> > No functional change intended.
> You might want to split this into a patch per caller as it might
> attact different reviewers.

No problem for me. returned Jens as the sole
maintainer for all these parts, so I put everything together in order
to ease the maintainer's job.

> > + do {
> > + } while (old && !atomic_try_cmpxchg(&blkg->use_delay, &old, old - 1));
> It might just be me, but for loops with an empty body this do { } while
> construct looks odd. Why not:
> while (old && !atomic_try_cmpxchg(&blkg->use_delay, &old, old - 1))
> ;

The form was taken from e6790e4b5d5e97dc287f3496dd2cf2dbabdfdb35 [1].
Using try_cmpxhchg, almost every use fits in

do {
// the body of the loop
} while (try_cmpxchg ...)

and when the body of the loop is empty, it is clear that this was
indeed intended. Using

while (try_cmpxchg ...);

looks to me like a semicolon was left there in error, like "if (...);".

> The the use of the atomic on ->use_delay looks really whacky to start
> with. To me it sounds like it really wants to use a proper lock
> instead of all this magic.

I took a lot of care not to change the functionality of the
surrounding code, and any functional change should be outside of the
scope of the patch.