Re: [GIT PULL] pipe: nonblocking rw for io_uring

From: Peter Zijlstra
Date: Mon May 08 2023 - 04:40:07 EST


On Sun, May 07, 2023 at 04:04:23PM +0200, Jonas Oberhauser wrote:
>
> Am 4/25/2023 um 9:58 PM schrieb Linus Torvalds:
> > Yes, I think Mark is right. It's not that 'old' might be wrong - that
> > doesn't matter because cmpxchg will work it out - it's just that 'new'
> > might not be consistent with the old value we then use.
>
> In the general pattern, besides the potential issue raised by Mark, tearing
> may also be an issue (longer example inspired by a case we met at the end of
> the mail) where 'old' being wrong matters.

There is yet another pattern where it actually matters:

old = READ_ONCE(*ptr);
do {
if (cond(old))
return false;

new = func(old);
} while (!try_cmpxchg(ptr, &old, new));

return true;

In this case we rely on old being 'coherent'. The more obvious case is
where it returns old (also not uncommon), but even if it just checks a
(multi-bit) condition on old you don't want tearing.