Section 9.16

Priority inversion

Definition

Priority inversion is a situation in a priority-based scheduler, one that always runs the most urgent ready task, in which a high-priority task waits for a lower-priority task, usually because the lower-priority task holds a resource the high-priority task needs, such as a mutex §9.5 (a lock one task at a time may hold). The inversion is bounded when the wait lasts only until the holder finishes using the resource. It is unbounded when medium-priority tasks, which do not need the resource, take the processor from the holder, prolonging the wait.

The idea

A fixed-priority scheduler promises that the most urgent ready task runs. A mutex promises that only one task at a time uses a shared resource. Each promise is sound on its own; together they leave a gap. When the high-priority task H needs a mutex that the low-priority task L holds, H must wait for L, so the effective rule changes from H runs to H waits for L. Yet L still runs at its own low priority, so any task M with a priority between those of L and H can preempt L, that is, take the processor away from L. M has nothing to do with the mutex, and H now waits for M as well.

This happened to a spacecraft. In 1997 the computer of the Mars Pathfinder lander kept resetting itself after landing. A high-priority task that managed the lander’s information bus waited for a mutex held by a low-priority task that gathered weather data, while tasks of medium priority ran instead of the weather task. A timing check in the flight software saw that the bus task had not finished in time and reset the computer. Engineers fixed it by enabling priority inheritance §9.17 on that mutex through a software update sent from Earth.

How it works

Follow the events in order.

  1. L, the low-priority task, locks the mutex and enters its critical section §9.4, the code that runs while it holds the mutex.

  2. H, the high-priority task, becomes ready and preempts L, because the scheduler always runs the highest-priority ready task.

  3. H tries to lock the mutex, finds it held and blocks. A blocked task cannot run, so its priority no longer counts.

  4. The scheduler picks the highest-priority ready task. If only L is ready, L continues. This delay of H is bounded inversion: it lasts at most the rest of L’s critical section.

  5. If M, a task of medium priority, becomes ready, it outranks L and runs. L is ready but does not run, so the mutex stays locked and H keeps waiting. Each further medium-priority task extends the wait. This is unbounded inversion: only the amount of medium-priority work limits the delay of H.

  6. When L finally runs and unlocks the mutex, H takes it and continues.

Bounded inversion cannot be removed while H and L share the resource. The remedies stop the unbounded part:

Worked example

Use a scheduler in which a larger number means a higher priority: L has priority 1, M has 2 and H has 3. Measure time in milliseconds from the moment H requests the mutex, and ignore the time the scheduler itself takes. At that moment L holds the mutex and needs 2 ms more to finish its critical section and unlock. M has just become ready and needs 6 ms. H needs 1 ms of work once it holds the mutex.

Without a remedy: H blocks at 0 ms. M (priority 2) outranks L (priority 1), so M runs from 0 to 6 ms. L runs from 6 to 8 ms and unlocks. H runs from 8 to 9 ms. H’s response time §9.32, from request to completion, is 6+2+1=96 + 2 + 1 = 9 ms. Only 1 ms is H’s own work and 2 ms is the bounded inversion; the other 6 ms come from M, a task with lower priority than H.

With priority inheritance: L runs at priority 3 while H waits, so M cannot preempt it. L runs from 0 to 2 ms and unlocks, H runs from 2 to 3 ms, and M runs from 3 to 9 ms. H’s response time is 2+1=32 + 1 = 3 ms. M now finishes at 9 ms instead of 6 ms. M is 3 ms later: the 2 ms that L needed to release the mutex and the 1 ms of H’s own work. That is correct, because H outranks M.

If 10 medium-priority tasks of 6 ms each were ready instead of one, H’s response time without a remedy would be 10×6+2+1=6310 \times 6 + 2 + 1 = 63 ms, while with inheritance it would stay 3 ms. The unbounded part grows with the medium-priority work; the bounded part does not.

Stated precisely

Response-time analysis for fixed-priority scheduling on one processor computes the worst-case response time RiR_i of task ii, the longest time from its release to its completion, as the smallest positive solution of Ri=Ci+Bi+∑j∈hp(i)⌈RiTj⌉Cj.R_i = C_i + B_i + \sum_{j \in \mathrm{hp}(i)} \left\lceil \frac{R_i}{T_j} \right\rceil C_j. Here CiC_i is the worst-case execution time §9.33 of task ii, the longest processor time one release of the task needs; hp(i)\mathrm{hp}(i) is the set of tasks with higher priority than task ii; TjT_j is the shortest time between two releases of task jj; ⌈x⌉\lceil x \rceil is xx rounded up to a whole number; and BiB_i, the blocking time, is the longest time task ii can wait for lower-priority tasks. Priority inversion is exactly what BiB_i measures. The sum covers only higher-priority tasks, so medium-priority tasks that delay task ii through a held resource can enter only through BiB_i. Without a protocol, BiB_i has no bound and the analysis gives no answer.

Sha, Rajkumar and Lehoczky (1990) proved two bounds on BiB_i. Under basic priority inheritance, task ii can be blocked for at most the total length of min(n,m)\min(n, m) critical sections, where nn is the number of lower-priority tasks and mm the number of mutexes that can block task ii. Under the priority ceiling protocol, it can be blocked for at most the length of one such critical section. Both bounds assume one processor, fixed priorities, critical sections of known maximum length that are properly nested, and no task that suspends itself, for example to wait for a device, while it holds a mutex.

Limits and common mistakes

Priority inversion is not a fault of the scheduler, which does exactly what the priorities say. The flaw is that priorities do not travel along the dependency from H to L unless a protocol carries them.

Bounded inversion remains under every protocol: once L holds the mutex, H waits for the rest of L’s critical section. Keep critical sections short, and never hold a mutex across a slow operation such as a flash erase or a long cryptographic computation.

The remedies need a resource with a known holder. A counting semaphore §9.6, an event flag or a message queue has no owner whose priority the kernel could raise, so a task waiting on one of them is not protected.

Inversion also arises without locks: when H waits for a reply from a lower-priority server task, for a job queued behind lower-priority jobs, or for a device that L started. There is no mutex to inherit through, so the design must bound those waits separately.

Raising the priority of a task that waits for hardware does not make the hardware faster.

Interrupt handlers §6.12 run above all tasks. Their execution delays H regardless of task priorities and enters the analysis as interference from higher-priority work, not as inversion.

Questions answered

How can a low-priority update worker delay a high-priority callback §14.22?

A low-priority update worker delays a high-priority callback whenever the callback must wait for something the worker holds or must finish first. In the usual case, the worker locks a mutex that protects shared state, and the callback then runs and needs the same mutex, so the callback blocks until the worker unlocks. That wait has two parts. The first is the rest of the worker’s critical section, the code it runs while holding the mutex. The second, which without a protocol has no limit, is all the work of medium-priority tasks that preempt the worker, that is, take the processor from it, while the callback waits: the worker is ready but does not run, so the mutex stays locked. The same shape arises without a mutex when the callback waits for a device the worker started, such as a flash erase, or for a job the worker queued ahead of it on a shared worker thread.

Three measures bound the delay. A mutex with priority inheritance or a priority ceiling stops medium-priority tasks from preempting the worker while the callback waits. A short critical section keeps the worker from holding the mutex across slow work such as a flash erase or a long cryptographic computation: the worker prepares the new data outside the mutex and holds the mutex only to swap it in. An analysis then adds the longest remaining critical section to the callback’s worst-case response time.

Related entries: Priority inheritance (§9.17), Response time versus execution time (§9.32), Schedulability (§9.37), Mutex (§9.5), Critical section (§9.4). Further reading: [298].