Section 9.17

Priority inheritance

Definition

Priority inheritance is a rule for mutexes §9.5, locks that one task at a time may hold, in a priority-scheduled system: while a higher-priority task waits for a mutex, the task holding the mutex runs at the waiter’s priority instead of its own lower one. When the holder releases the mutex, its priority drops back to the highest level it is still entitled to. The rule bounds priority inversion §9.16, the delay of a high-priority task by lower-priority ones. Sha, Rajkumar and Lehoczky analyzed it in 1990 [298].

The idea

Priority inversion happens because a blocked task’s priority stops counting. The scheduler runs the most urgent ready task, and a task waiting for a mutex is not ready. Yet the waiting task’s urgency is real, and the kernel, the core of the operating system that schedules tasks and manages mutexes, knows exactly which task stands in its way: the holder of the mutex. Priority inheritance passes the urgency along that known dependency. While H waits for L’s mutex, L is scheduled as if it were H, because finishing L’s critical section §9.4, the code L runs while holding the mutex, is at that moment H’s work.

Inheritance changes only who gets the processor. It does not shorten L’s critical section, and it does not help when L is waiting for something other than the processor. H still waits for the rest of L’s critical section; what disappears is the extra wait for unrelated tasks of medium priority.

How it works

  1. Every task has a base priority, the one its designer assigned, and an effective priority, the one the scheduler uses. Normally the two are equal.

  2. When task H blocks on a mutex held by task L, and H’s effective priority is higher than L’s, the kernel raises L’s effective priority to H’s.

  3. If L is itself blocked on a second mutex held by task Q, raising L alone achieves nothing, because L cannot run. The kernel therefore follows the chain of holders and raises Q as well, and so on along the chain. This is transitive inheritance.

  4. When several tasks wait on mutexes that L holds, L’s effective priority is the highest among its base priority and the effective priorities of all those waiters.

  5. When L unlocks a mutex, the mutex passes to its highest-priority waiter, and the kernel recalculates L’s effective priority from the waiters on the mutexes L still holds. Restoring the base priority at once is wrong whenever L still holds another mutex with a high-priority waiter.

  6. If a waiter stops waiting before it gets the mutex, for example because its wait has a timeout §9.34, the kernel recalculates the holder’s priority in the same way.

POSIX (the Portable Operating System Interface, a standard for Unix-like operating systems) offers inheritance as the mutex protocol attribute PTHREAD_PRIO_INHERIT, and the immediate priority ceiling protocol as PTHREAD_PRIO_PROTECT. The Linux kernel implements transitive inheritance in its real-time mutexes, which back priority-inheritance mutexes in user programs. In the Zephyr real-time operating system §6.33, every k_mutex uses priority inheritance. Zephyr’s documentation describes steps 3 to 6: the kernel recalculates the holder’s priority from all the mutexes the holder still holds, and it propagates a boost along a chain of holders up to an implementation-defined number of hops per lock attempt. Its configuration option CONFIG_PRIORITY_CEILING caps how high inheritance may raise a priority; despite the name, it is not the priority ceiling protocol. Behavior can differ between kernel versions, so read the documentation of the version in use.

Worked example

Use a scheduler in which a larger number means more urgent. Task L has base priority 1 and holds two mutexes, X and Y. Task A, of priority 3, waits for X; task B, of priority 5, waits for Y. L’s effective priority is max(1,3,5)=5\max(1, 3, 5) = 5.

  1. L unlocks Y. B takes Y, becomes ready and, at priority 5, runs first. L still holds X, with A waiting, so L’s effective priority becomes max(1,3)=3\max(1, 3) = 3, not 1. When B finishes, L continues at priority 3.

  2. L unlocks X. A takes X. L now holds no mutex with a waiter, so its effective priority becomes its base priority, 1.

A kernel that restored L to 1 at step 1 would let any task of priority 2 preempt L, that is, take the processor from L, while A waits for X: the inversion that inheritance was meant to stop would return.

A chain: H, of priority 5, waits for mutex X, held by L, of priority 1. L waits for mutex Y, held by Q, of priority 2. Task D, of priority 4, is ready. If the kernel raises only L to 5, nothing changes, because L is blocked: Q runs at priority 2, D outranks Q, and H waits for D. Walking the chain raises Q to 5 as well. Q runs ahead of D and unlocks Y, and its priority falls back to 2. L takes Y and runs at 5, because H still waits for X. L unlocks X, and H continues. D runs only after H has finished, or after H blocks for another reason.

Stated precisely

Let pbase(T)p_{\mathrm{base}}(T) be the base priority of task TT, and let W(T)W(T) be the set of tasks blocked on mutexes that TT holds. With larger numbers meaning more urgent, the effective priority is peff(T)=max(pbase(T),maxU∈W(T)peff(U)),p_{\mathrm{eff}}(T) = \max\left(p_{\mathrm{base}}(T),\ \max_{U \in W(T)} p_{\mathrm{eff}}(U)\right), where the inner maximum is left out when W(T)W(T) is empty. A waiter’s effective priority already includes what that waiter inherits, so the rule is transitive. The definition is well founded as long as the waits form no cycle; a cycle is a deadlock §9.14, which inheritance cannot resolve. The kernel must evaluate the rule again whenever a task starts or stops waiting and whenever a mutex changes holder. In a kernel where smaller numbers mean more urgent, as in Zephyr, replace maximum by minimum: the rule always takes the most urgent priority, not the largest number.

Sha, Rajkumar and Lehoczky (1990) proved that under basic priority inheritance a task is blocked by lower-priority tasks 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 it. The bound assumes one processor, fixed base priorities, critical sections of known maximum length that are properly nested, and no task that suspends itself while it holds a mutex, for example to wait for a device.

Limits and common mistakes

Inheritance does not prevent deadlock. If L holds X and waits for Y while H holds Y and waits for X, both tasks are blocked, and raising either priority lets neither run.

Blocking can be chained: a high-priority task can be blocked once by each of several lower-priority holders in turn, up to min(n,m)\min(n, m) critical sections. The priority ceiling protocols, described in the priority inversion entry, reduce this to one critical section and also prevent deadlock among the mutexes they govern, at the cost of knowing in advance which tasks use which mutex.

Only a resource with an owner can pass on priority. A semaphore §9.6, an event flag, a message queue or a condition has no single holder, so the kernel has no task to raise. In POSIX and in Zephyr, only mutexes inherit; a binary semaphore used as a lock does not.

Inheritance speeds up only the holder’s share of the processor. If the holder waits for a device, such as a flash erase, raising its priority changes nothing.

The bound needs bounded critical sections. Inheritance limits how many critical sections can delay a task, not how long each one lasts.

Inheritance also delays other tasks: while L runs at a raised priority, tasks with priorities between L’s base and the inherited priority wait. The schedulability §9.37 analysis, the check that every task meets its deadlines, must count that delay for them.

A kernel may cap the raised priority or the number of chain links it follows, as Zephyr does. A timing argument must use the kernel’s actual rule, not the ideal one.

Mutexes are for tasks, not for interrupt handlers §6.12, which cannot wait; Zephyr’s documentation states that its mutexes are not designed for use by interrupt service routines.

Questions answered

Does inheritance handle every nested-lock or deadlock case?

No, priority inheritance does not handle every nested-lock or deadlock case. It never resolves a deadlock: every task in a deadlock is blocked, and raising the priority of a blocked task does not let it run. Deadlock needs a prevention rule such as lock ordering §9.15.

For nested locks, meaning a task that holds one mutex while waiting for another, inheritance bounds the delay only if the kernel follows the whole chain of holders and recalculates priorities correctly when a task releases one of several mutexes or a waiter gives up. A kernel may limit the chain walk; Zephyr, for example, follows an implementation-defined number of links per lock attempt. Even with complete chain handling, a high-priority task can be blocked once by each of several lower-priority holders in turn. The priority ceiling protocols, for example the immediate form in which a task that locks a mutex runs at once at the priority of the most urgent task that ever uses it, limit the blocking to one critical section and also prevent deadlock among the mutexes they cover.

Related entries: Priority inversion (§9.16), Deadlock (§9.14), Mutex (§9.5), Semaphore (§9.6), Lock ordering (§9.15). Further reading: [83, 298].