Priority inheritance
How a mutex bounds priority inversion by lending the waiter's priority to the task that holds the lock.
Simulated kernel: FreeRTOS V11.3.3 and Zephyr v4.5.0. This scenario and every other one are in the scheduler playground. The simulator and its data files are not covered by the site's CC BY-SA license; see their license notices.
Priority inversion happens because the kernel does not know which task a blocked task is waiting for. A mutex records its owner, so the kernel does know, and it can do something about it: while a more urgent task waits for a mutex, the task that holds the mutex runs at the waiter’s priority. This is priority inheritance.
The simulator above runs the same three tasks as the priority inversion lesson. The only change is
that the lock M is a mutex.
What happens (FreeRTOS)
- Tick 0:
LowtakesMand does the first tick of its work. - Tick 1:
Medwakes and preemptsLow. - Tick 2:
Highwakes, preemptsMedand asks forM.Lowholds it. - Before
Highblocks, the kernel raisesLowfrom priority 1 to priority 3, the priority ofHigh. The task card shows1 → 3, and theLowchip moves to the ready list of priority 3. Highblocks. The most urgent ready task is nowLow, notMed.Lowruns ticks 2, 3 and 4 and givesMback at tick 5. Its priority returns to 1 at that moment.HightakesM, runs at tick 5 and ends.Medgets the CPU at tick 6.
High waited three ticks: exactly the time Low still needed inside its critical section. Med
no longer has any say in it. With the semaphore the wait was eight ticks and grew with the work of
Med.
What inheritance promises, and what it does not
The wait of the high-priority task is now bounded by the length of the critical sections of the tasks it shares mutexes with. It is not zero. The rule that follows for the application is: keep the time a mutex is held short, and never sleep or wait for something slow while holding one.
Inheritance also has a cost on the other side: Med was delayed by Low, a task less urgent than
itself. That is the intended effect, but it means the timing of every task between the holder and
the waiter depends on the critical section too.
The two kernels do it differently
Switch between FreeRTOS and Zephyr in the simulator and step through tick 2 (tick 3 on Zephyr, where every wake-up is a tick later).
- FreeRTOS stores the task’s own priority in
uxBasePriorityand the priority it runs at inuxPriority. It restores the base priority only when the task holds no mutex at all (uxMutexesHeldis 0). See the FreeRTOS walkthrough. - Zephyr keeps a list of the mutexes each thread holds. Every time one is unlocked it works the priority out again from the waiters of the mutexes that remain, and it raises a whole chain of owners if the holder is itself waiting for another mutex. See the Zephyr walkthrough.
- When the mutex is unlocked, Zephyr makes
Highits owner beforeHighhas run; FreeRTOS only makesHighready. Mutex handoff shows a program where that changes the outcome.
The difference in when the priority drops shows with nested mutexes. In the
nested mutexes lesson, Low holds two mutexes and
High waits for the inner one. On Zephyr, Low drops back as soon as it releases that one. On
FreeRTOS it keeps the raised priority until it has released both.
What to try
- Change M is a to binary semaphore to get the inversion back.
- Give
Medan urgency of 4, aboveHigh. Inheritance changes nothing now, and it should not:Medis entitled to run before both.