On Mon, Feb 11, 2019 at 11:31 AM Waiman Long <longman@redhat.com> wrote:
>> Modify __down_read_trylock() to make it generate slightly better code> (smaller and maybe a tiny bit faster).
This looks good, but I would ask you to try one slightly different approach.
Instead of this:
> long tmp = atomic_long_read(&sem->count);>> while (tmp >= 0) {> if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,> tmp + RWSEM_ACTIVE_READ_BIAS)) {> return 1;> }> }
try doing this instead:
long tmp = 0;
do {
if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
tmp + RWSEM_ACTIVE_READ_BIAS)) {
return 1;
} while (tmp >= 0);
return 0;
because especially when it comes to locking, it's usually better to
just *guess* that the lock is unlocked, than it is to actually read
from the line to see what the state is.
Often - but certainly not always - the lock is the first access to the
target cacheline, and assuming the trylock is successful (which I
think is the case we want to optimize for), we're much better off
causing that first access to be a read-for-ownership, rather than a
read-for-sharing.
Because if you first read from the line, and then do a cmpxchg, and if
the line was not in the cache, your cache coherency protocol will
generally go through two states: first shared (for the initial read)
and then exclusive-dirty (for the cmpxchg).
Now, this is obviously very micro-architecture dependent, and in fact
the microarchitecture could even see the "predict fallthrough to a
cmpxchg with the same address" and turn the first read into a
read-for-ownership, but we've done this at some point before, and the
"guess unlocked" was actually the one that performed better.
Of course, the downside is that it might be worse when the guess is
incorrect - either because of a nested read lock or due to an actual
conflict with a write, but on the whole those *should* be the rare
cases, and not the cases where we necessarily optimize for latency of
the operation.
Hmm?
Linus

On 02/12/2019 02:58 PM, Linus Torvalds wrote:
> On Mon, Feb 11, 2019 at 11:31 AM Waiman Long <longman@redhat.com> wrote:>> Modify __down_read_trylock() to make it generate slightly better code>> (smaller and maybe a tiny bit faster).> This looks good, but I would ask you to try one slightly different approach.>> Instead of this:>>> long tmp = atomic_long_read(&sem->count);>>>> while (tmp >= 0) {>> if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,>> tmp + RWSEM_ACTIVE_READ_BIAS)) {>> return 1;>> }>> }> try doing this instead:>> long tmp = 0;>> do {> if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,> tmp + RWSEM_ACTIVE_READ_BIAS)) {> return 1;> } while (tmp >= 0);> return 0;>> because especially when it comes to locking, it's usually better to> just *guess* that the lock is unlocked, than it is to actually read> from the line to see what the state is.>> Often - but certainly not always - the lock is the first access to the> target cacheline, and assuming the trylock is successful (which I> think is the case we want to optimize for), we're much better off> causing that first access to be a read-for-ownership, rather than a> read-for-sharing.>> Because if you first read from the line, and then do a cmpxchg, and if> the line was not in the cache, your cache coherency protocol will> generally go through two states: first shared (for the initial read)> and then exclusive-dirty (for the cmpxchg).>> Now, this is obviously very micro-architecture dependent, and in fact> the microarchitecture could even see the "predict fallthrough to a> cmpxchg with the same address" and turn the first read into a> read-for-ownership, but we've done this at some point before, and the> "guess unlocked" was actually the one that performed better.>> Of course, the downside is that it might be worse when the guess is> incorrect - either because of a nested read lock or due to an actual> conflict with a write, but on the whole those *should* be the rare> cases, and not the cases where we necessarily optimize for latency of> the operation.>> Hmm?
I looked at the assembly code in arch/x86/include/asm/rwsem.h. For both
trylocks (read & write), the count is read first before attempting to
lock it. We did the same for all trylock functions in other locks.
Depending on how the trylock is used and how contended the lock is, it
may help or hurt performance. Changing down_read_trylock to do an
unconditional cmpxchg will change the performance profile of existing
code. So I would prefer keeping the current code.
I do notice now that the generic down_write_trylock() code is doing an
unconditional compxchg. So I wonder if we should change it to read the
lock first like other trylocks or just leave it as it is.
Cheers,
Longman

* Waiman Long <longman@redhat.com> wrote:
> I looked at the assembly code in arch/x86/include/asm/rwsem.h. For both> trylocks (read & write), the count is read first before attempting to> lock it. We did the same for all trylock functions in other locks.> Depending on how the trylock is used and how contended the lock is, it> may help or hurt performance. Changing down_read_trylock to do an> unconditional cmpxchg will change the performance profile of existing> code. So I would prefer keeping the current code.> > I do notice now that the generic down_write_trylock() code is doing an> unconditional compxchg. So I wonder if we should change it to read the> lock first like other trylocks or just leave it as it is.
No, I think we should instead move the other trylocks to the
try-for-ownership model as well, like Linus suggested.
That's the general assumption we make in locking primitives, that we
optimize for the common, expected case - which would be that the trylock
succeeds, and I don't see why trylock primitives should be different.
In fact I can see more ways for read-for-sharing to perform suboptimally
on larger systems.
Thanks,
Ingo

On 02/13/2019 02:45 AM, Ingo Molnar wrote:
> * Waiman Long <longman@redhat.com> wrote:>>> I looked at the assembly code in arch/x86/include/asm/rwsem.h. For both>> trylocks (read & write), the count is read first before attempting to>> lock it. We did the same for all trylock functions in other locks.>> Depending on how the trylock is used and how contended the lock is, it>> may help or hurt performance. Changing down_read_trylock to do an>> unconditional cmpxchg will change the performance profile of existing>> code. So I would prefer keeping the current code.>>>> I do notice now that the generic down_write_trylock() code is doing an>> unconditional compxchg. So I wonder if we should change it to read the>> lock first like other trylocks or just leave it as it is.> No, I think we should instead move the other trylocks to the > try-for-ownership model as well, like Linus suggested.>> That's the general assumption we make in locking primitives, that we > optimize for the common, expected case - which would be that the trylock > succeeds, and I don't see why trylock primitives should be different.>> In fact I can see more ways for read-for-sharing to perform suboptimally > on larger systems.
I don't mind changing to the try-for-ownership model for rwsem and
mutex. I do have some concern to do that for spinlock. Some of the lock
slowpath code do optimistic trylock. Making them unconditional cmpxchg
will impact lock contention performance.
I will update this rwsem patch to make the change while I am working on
it. For other locks, I will suggest we go slow and carefully evaluate
the performance implication before we make the changes.
Cheers,
Longman