From: Peter Zijlstra > Sent: 08 May 2023 09:39 > > 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. It isn't as though READ_ONCE() is expensive. For kernel/device driver code, while CSE is useful, you pretty much always want the compiler to always do loads into local variables. It is rather a shame there isn't a compiler option that avoids these unusual any annoying operations. Since the current 'rules' seem to require READ_ONCE() and WRITE_ONCE() be used as pairs, why not make the data 'volatile'? That ought to be the same as using volatile casts on all accesses. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)