Priority inversion
How a medium-priority task can keep a high-priority task waiting for a lock it has nothing to do with.
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 is the situation where a high-priority task waits while a lower-priority task runs. A short inversion is unavoidable whenever tasks share a lock. The dangerous kind is the one shown above, where the wait has no upper limit.
The three tasks
Lowstarts first, takes the binary semaphoreSas a lock, and needs four ticks of work before it givesSback.Medwakes at tick 1 and has six ticks of work. It never touchesS.Highwakes at tick 2, needsSfor one tick of work, and is the most urgent task in the system.
What happens (FreeRTOS)
- Tick 0:
LowtakesSand does the first tick of its work. - Tick 1:
Medwakes and preemptsLow. That is correct:Medis more urgent. - Tick 2:
Highwakes and preemptsMed. It asks forS, whichLowholds, so it blocks. - The scheduler now runs the most urgent ready task. That is
Med, notLow. Medruns until it is finished, at tick 7. All that timeLowcannot run, so it cannot releaseS, soHighcannot run.Lowfinally runs ticks 7 to 9 and givesSback at tick 10.Highruns at tick 10.
High waited eight ticks. Three of them were for Low to finish with the lock, which is the price
of sharing. The other five were for Med, a task of lower priority that has nothing to do with the
lock. If Med had sixty ticks of work, High would have waited sixty-two. Nothing in the
program limits it.
On Zephyr every wake-up is one tick later (see the scheduling lesson),
but the result is the same: High gets the semaphore at tick 10.
Why the kernel cannot help here
A binary semaphore has no owner. Any task may give it, not only the one that took it; that is what
makes semaphores useful for signalling from one task to another. So when High blocks on S, the
kernel has no idea that Low is the task everyone is waiting for. In the simulator’s kernel
structures panel, the semaphore shows only a count and a list of waiters.
The best-known case
In 1997 the Mars Pathfinder lander kept resetting itself on the surface of Mars. A high-priority task that managed its data bus shared a mutex with a low-priority task that collected weather data. Medium-priority tasks sometimes ran while the low one held the mutex. The bus task waited too long, a watchdog noticed, and the watchdog reset the computer. The engineers reproduced it on a copy of the lander on Earth and fixed it by remote update, by turning on an option of the mutex: priority inheritance.
The fix
In the simulator, change S is a from binary semaphore to mutex and run it again. High now
gets the lock at tick 5. A mutex has an owner, and the kernel uses that knowledge:
Priority inheritance explains how.
What to try
- Give
Medan urgency of 1, the same asLow. On FreeRTOS the two now take turns, soLowreleases the lock sooner (at tick 7), butHighstill waits for work that is none of its business. On Zephyr,Med’s start delay only begins when it first gets a time slice, at tick 2, soLowfinishes undisturbed and releases the lock at tick 4. - Move the start of
Medto tick 6, afterLowhas released the lock. The inversion needsMedto arrive whileLowholds the lock, which is why such bugs show up rarely and are hard to reproduce.