On Wed, Dec 16, 2020 at 01:27:43AM +0100, Thomas Gleixner wrote: > On Tue, Dec 08 2020 at 07:03, Paul E. McKenney wrote: > > > On Tue, Dec 08, 2020 at 09:11:29AM +0100, Peter Zijlstra wrote: > >> On Mon, Dec 07, 2020 at 11:44:06AM -0800, Paul E. McKenney wrote: > >> > >> > Also, in this particular case, why data_race() rather than READ_ONCE()? > >> > Do we really expect the compiler to be able to optimize this case > >> > significantly without READ_ONCE()? > > There is probably not much optimization potential for the compiler if > data_race() is used vs. READ/WRITE_ONCE() in this code.
OK, got it. > >> It's about intent and how the code reads. READ_ONCE() is something > >> completely different from data_race(). data_race() is correct here. > > > > Why? > > Lemme answer that to the extent why _I_ chose data_race() - aside of my > likely confusion over our IRC conversation. > > The code does not really care about the compiler trying to be clever or > not as it is designed to be tolerant of all sorts of concurrency > including competing writes. It does not care about multiple reloads > either. It neither cares about invented stores as long as these > invented stores are not storing phantasy values. > > The only thing it cares about is store/load tearing, but there is no > 'clever' way to use that because of the only valid transitions of > 'cpunr' which comes from smp_processor_id() to TICK_DO_TIMER_NONE which > is the only constant involved or the other way round (which is > intentionally subject to competing stores). > > If the compiler is free to store the 32bit value as 4 seperate bytes or > does invented stores with phantasy values, then there is surely a reason > to switch to READ/WRITE_ONCE(), but that'd be a really daft reason. > > So my intent was to document that this code does not care about anything > else than what I'd consider to be plain compiler bugs. > > My conclusion might be wrong as usual :) Given that there is no optimization potential, then the main reason to use data_race() instead of *_ONCE() is to prevent KCSAN from considering the accesses when looking for data races. But that is mostly for debugging accesses, in cases when these accesses are not really part of the concurrent algorithm. So if I understand the situation correctly, I would be using *ONCE(). Thanx, Paul