On Tue, Nov 24, 2015 at 12:28 PM, Tejun Heo <t...@kernel.org> wrote:
>>
>> In general, it's very dangerous to try to cook up your own locking
>> rules. People *always* get it wrong.
>
> It's either trylock on timer side or timer active spinning trick on
> canceling side, so this seems the lesser of the two evils.

I'm not saying the approach is wrong.

I'm saying that people need to realize that locking is harder than
they think, and not cook up their own lock primitives using things
like trylock without really thinking about it a *lot*.

Basically, "trylock()" on its own should never be used in a loop. The
main use for trylock should be one of:

 - thing that you can just not do at all if you can't get the lock

 - avoiding ABBA deadlocks: if you have a A->B locking order, but you
already hold B, instead of "drop B, then take A and B in the right
order", you may decide to first "trylock(A)" - and if that fails you
then fall back on the "drop and relock in the right order".

but if what you want to create is a "get lock using trylock", you need
to be very aware of the cache coherency traffic issue at least.

It is possible that we should think about trying to introduce a new
primitive for that "loop_try_lock()" thing. But it's probably not
common enough to be worth it - we've had this issue before, but I
think it's a "once every couple of years" kind of thing rather than
anything that we need to worry about.

The "locking is hard" issue is very real, though. We've traditionally
had a *lot* of code that tried to do its own locking, and not getting
the memory ordering right etc. Things that happen to work on x86 but
don't on other architectures etc.

                       Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-api" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to