Search code examples
linuxlinux-kernelmutexsemanticssmp

mutex owner in SMP linux


In the kernel implementation of mutex, thread owner field exists only if it is a SMP build. I can understand that under good and clean code a thread will call release only if acquire was successful so we can save some cache and cpu cycles by not keeping track of the thread. But then why does SMP require it ? Is it because in SMP the mutex is hybrid and spins while the thread owner is on another cpu and requires this field for the reference or are there more reasons behind it ?


Solution

  • Honestly, I don't remember linux kernel mutex implementation now, but I see at least three reasons to store mutex owner directly in the data structure:

    1. For debugging. It's easy to detect an owner from the kernel crash dump
    2. For runtime assertions. It becomes dead easy to detect an attempt to acquire the mutex you've already acquired.
    3. For implementation of priority inheritance protocol or any other mechanism that resolves priority inversion problem: http://en.wikipedia.org/wiki/Priority_inversion