xref: /linux/Documentation/locking/pi-futex.rst (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1======================
2Lightweight PI-futexes
3======================
4
5We are calling them lightweight for 3 reasons:
6
7 - in the user-space fastpath a PI-enabled futex involves no kernel work
8   (or any other PI complexity) at all. No registration, no extra kernel
9   calls - just pure fast atomic ops in userspace.
10
11 - even in the slowpath, the system call and scheduling pattern is very
12   similar to normal futexes.
13
14 - the in-kernel PI implementation is streamlined around the mutex
15   abstraction, with strict rules that keep the implementation
16   relatively simple: only a single owner may own a lock (i.e. no
17   read-write lock support), only the owner may unlock a lock, no
18   recursive locking, etc.
19
20Priority Inheritance - why?
21---------------------------
22
23The short reply: user-space PI helps achieving/improving determinism for
24user-space applications. In the best-case, it can help achieve
25determinism and well-bound latencies. Even in the worst-case, PI will
26improve the statistical distribution of locking related application
27delays.
28
29The longer reply
30----------------
31
32Firstly, sharing locks between multiple tasks is a common programming
33technique that often cannot be replaced with lockless algorithms. As we
34can see it in the kernel [which is a quite complex program in itself],
35lockless structures are rather the exception than the norm - the current
36ratio of lockless vs. locky code for shared data structures is somewhere
37between 1:10 and 1:100. Lockless is hard, and the complexity of lockless
38algorithms often endangers to ability to do robust reviews of said code.
39I.e. critical RT apps often choose lock structures to protect critical
40data structures, instead of lockless algorithms. Furthermore, there are
41cases (like shared hardware, or other resource limits) where lockless
42access is mathematically impossible.
43
44Media players (such as Jack) are an example of reasonable application
45design with multiple tasks (with multiple priority levels) sharing
46short-held locks: for example, a highprio audio playback thread is
47combined with medium-prio construct-audio-data threads and low-prio
48display-colory-stuff threads. Add video and decoding to the mix and
49we've got even more priority levels.
50
51So once we accept that synchronization objects (locks) are an
52unavoidable fact of life, and once we accept that multi-task userspace
53apps have a very fair expectation of being able to use locks, we've got
54to think about how to offer the option of a deterministic locking
55implementation to user-space.
56
57Most of the technical counter-arguments against doing priority
58inheritance only apply to kernel-space locks. But user-space locks are
59different, there we cannot disable interrupts or make the task
60non-preemptible in a critical section, so the 'use spinlocks' argument
61does not apply (user-space spinlocks have the same priority inversion
62problems as other user-space locking constructs). Fact is, pretty much
63the only technique that currently enables good determinism for userspace
64locks (such as futex-based pthread mutexes) is priority inheritance:
65
66Currently (without PI), if a high-prio and a low-prio task shares a lock
67[this is a quite common scenario for most non-trivial RT applications],
68even if all critical sections are coded carefully to be deterministic
69(i.e. all critical sections are short in duration and only execute a
70limited number of instructions), the kernel cannot guarantee any
71deterministic execution of the high-prio task: any medium-priority task
72could preempt the low-prio task while it holds the shared lock and
73executes the critical section, and could delay it indefinitely.
74
75Implementation
76--------------
77
78As mentioned before, the userspace fastpath of PI-enabled pthread
79mutexes involves no kernel work at all - they behave quite similarly to
80normal futex-based locks: a 0 value means unlocked, and a value==TID
81means locked. (This is the same method as used by list-based robust
82futexes.) Userspace uses atomic ops to lock/unlock these mutexes without
83entering the kernel.
84
85To handle the slowpath, we have added two new futex ops:
86
87  - FUTEX_LOCK_PI
88  - FUTEX_UNLOCK_PI
89
90If the lock-acquire fastpath fails, [i.e. an atomic transition from 0 to
91TID fails], then FUTEX_LOCK_PI is called. The kernel does all the
92remaining work: if there is no futex-queue attached to the futex address
93yet then the code looks up the task that owns the futex [it has put its
94own TID into the futex value], and attaches a 'PI state' structure to
95the futex-queue. The pi_state includes an rt-mutex, which is a PI-aware,
96kernel-based synchronization object. The 'other' task is made the owner
97of the rt-mutex, and the FUTEX_WAITERS bit is atomically set in the
98futex value. Then this task tries to lock the rt-mutex, on which it
99blocks. Once it returns, it has the mutex acquired, and it sets the
100futex value to its own TID and returns. Userspace has no other work to
101perform - it now owns the lock, and futex value contains
102FUTEX_WAITERS|TID.
103
104If the unlock side fastpath succeeds, [i.e. userspace manages to do a
105TID -> 0 atomic transition of the futex value], then no kernel work is
106triggered.
107
108If the unlock fastpath fails (because the FUTEX_WAITERS bit is set),
109then FUTEX_UNLOCK_PI is called, and the kernel unlocks the futex on the
110behalf of userspace - and it also unlocks the attached
111pi_state->rt_mutex and thus wakes up any potential waiters.
112
113Note that under this approach, contrary to previous PI-futex approaches,
114there is no prior 'registration' of a PI-futex. [which is not quite
115possible anyway, due to existing ABI properties of pthread mutexes.]
116
117Also, under this scheme, 'robustness' and 'PI' are two orthogonal
118properties of futexes, and all four combinations are possible: futex,
119robust-futex, PI-futex, robust+PI-futex.
120
121More details about priority inheritance can be found in
122Documentation/locking/rt-mutex.rst.
123