Re: [RESEND PATCH v4] x86/hpet: Reduce HPET counter read contention

From: Dave Hansen
Date: Thu Aug 11 2016 - 20:31:59 EST


On 08/11/2016 04:22 PM, Waiman Long wrote:
> On 08/11/2016 03:32 PM, Dave Hansen wrote:
>> It's a real bummer that this all has to be open-coded. I have to wonder
>> if there were any alternatives that you tried that were simpler.
>
> What do you mean by "open-coded"? Do you mean the function can be inlined?

I just mean that it's implementing its own locking instead of being able
to use spinlocks or seqlocks, or some other existing primitive.

>> Is READ_ONCE()/smp_store_release() really strong enough here? It
>> guarantees ordering, but you need ordering *and* a guarantee that your
>> write is visible to the reader. Don't you need actual barriers for
>> that? Otherwise, you might be seeing a stale HPET value, and the spin
>> loop that you did waiting for it to be up-to-date was worthless. The
>> seqlock code, uses barriers, btw.
>
> The cmpxchg() and smp_store_release() act as the lock/unlock sequence
> with the proper barriers. Another important point is that the hpet value
> is visible to the other readers before the sequence number. This is
> what the smp_store_release() is providing. cmpxchg is an actual barrier,
> even though smp_store_release() is not. However, the x86 architecture
> will guarantee the writes are in order, I think.

The contended case (where HPET_SEQ_LOCKED(seq)) doesn't do the cmpxchg.
So it's entirely relying on the READ_ONCE() on the "reader" side and
the cmpxchg/smp_store_release() on the "writer". This probably works in
practice, but I'm not sure it's guaranteed behavior.