Back to home page

OSCL-LXR

 
 

    


0001 ======================
0002 Lightweight PI-futexes
0003 ======================
0004 
0005 We are calling them lightweight for 3 reasons:
0006 
0007  - in the user-space fastpath a PI-enabled futex involves no kernel work
0008    (or any other PI complexity) at all. No registration, no extra kernel
0009    calls - just pure fast atomic ops in userspace.
0010 
0011  - even in the slowpath, the system call and scheduling pattern is very
0012    similar to normal futexes.
0013 
0014  - the in-kernel PI implementation is streamlined around the mutex
0015    abstraction, with strict rules that keep the implementation
0016    relatively simple: only a single owner may own a lock (i.e. no
0017    read-write lock support), only the owner may unlock a lock, no
0018    recursive locking, etc.
0019 
0020 Priority Inheritance - why?
0021 ---------------------------
0022 
0023 The short reply: user-space PI helps achieving/improving determinism for
0024 user-space applications. In the best-case, it can help achieve
0025 determinism and well-bound latencies. Even in the worst-case, PI will
0026 improve the statistical distribution of locking related application
0027 delays.
0028 
0029 The longer reply
0030 ----------------
0031 
0032 Firstly, sharing locks between multiple tasks is a common programming
0033 technique that often cannot be replaced with lockless algorithms. As we
0034 can see it in the kernel [which is a quite complex program in itself],
0035 lockless structures are rather the exception than the norm - the current
0036 ratio of lockless vs. locky code for shared data structures is somewhere
0037 between 1:10 and 1:100. Lockless is hard, and the complexity of lockless
0038 algorithms often endangers to ability to do robust reviews of said code.
0039 I.e. critical RT apps often choose lock structures to protect critical
0040 data structures, instead of lockless algorithms. Furthermore, there are
0041 cases (like shared hardware, or other resource limits) where lockless
0042 access is mathematically impossible.
0043 
0044 Media players (such as Jack) are an example of reasonable application
0045 design with multiple tasks (with multiple priority levels) sharing
0046 short-held locks: for example, a highprio audio playback thread is
0047 combined with medium-prio construct-audio-data threads and low-prio
0048 display-colory-stuff threads. Add video and decoding to the mix and
0049 we've got even more priority levels.
0050 
0051 So once we accept that synchronization objects (locks) are an
0052 unavoidable fact of life, and once we accept that multi-task userspace
0053 apps have a very fair expectation of being able to use locks, we've got
0054 to think about how to offer the option of a deterministic locking
0055 implementation to user-space.
0056 
0057 Most of the technical counter-arguments against doing priority
0058 inheritance only apply to kernel-space locks. But user-space locks are
0059 different, there we cannot disable interrupts or make the task
0060 non-preemptible in a critical section, so the 'use spinlocks' argument
0061 does not apply (user-space spinlocks have the same priority inversion
0062 problems as other user-space locking constructs). Fact is, pretty much
0063 the only technique that currently enables good determinism for userspace
0064 locks (such as futex-based pthread mutexes) is priority inheritance:
0065 
0066 Currently (without PI), if a high-prio and a low-prio task shares a lock
0067 [this is a quite common scenario for most non-trivial RT applications],
0068 even if all critical sections are coded carefully to be deterministic
0069 (i.e. all critical sections are short in duration and only execute a
0070 limited number of instructions), the kernel cannot guarantee any
0071 deterministic execution of the high-prio task: any medium-priority task
0072 could preempt the low-prio task while it holds the shared lock and
0073 executes the critical section, and could delay it indefinitely.
0074 
0075 Implementation
0076 --------------
0077 
0078 As mentioned before, the userspace fastpath of PI-enabled pthread
0079 mutexes involves no kernel work at all - they behave quite similarly to
0080 normal futex-based locks: a 0 value means unlocked, and a value==TID
0081 means locked. (This is the same method as used by list-based robust
0082 futexes.) Userspace uses atomic ops to lock/unlock these mutexes without
0083 entering the kernel.
0084 
0085 To handle the slowpath, we have added two new futex ops:
0086 
0087   - FUTEX_LOCK_PI
0088   - FUTEX_UNLOCK_PI
0089 
0090 If the lock-acquire fastpath fails, [i.e. an atomic transition from 0 to
0091 TID fails], then FUTEX_LOCK_PI is called. The kernel does all the
0092 remaining work: if there is no futex-queue attached to the futex address
0093 yet then the code looks up the task that owns the futex [it has put its
0094 own TID into the futex value], and attaches a 'PI state' structure to
0095 the futex-queue. The pi_state includes an rt-mutex, which is a PI-aware,
0096 kernel-based synchronization object. The 'other' task is made the owner
0097 of the rt-mutex, and the FUTEX_WAITERS bit is atomically set in the
0098 futex value. Then this task tries to lock the rt-mutex, on which it
0099 blocks. Once it returns, it has the mutex acquired, and it sets the
0100 futex value to its own TID and returns. Userspace has no other work to
0101 perform - it now owns the lock, and futex value contains
0102 FUTEX_WAITERS|TID.
0103 
0104 If the unlock side fastpath succeeds, [i.e. userspace manages to do a
0105 TID -> 0 atomic transition of the futex value], then no kernel work is
0106 triggered.
0107 
0108 If the unlock fastpath fails (because the FUTEX_WAITERS bit is set),
0109 then FUTEX_UNLOCK_PI is called, and the kernel unlocks the futex on the
0110 behalf of userspace - and it also unlocks the attached
0111 pi_state->rt_mutex and thus wakes up any potential waiters.
0112 
0113 Note that under this approach, contrary to previous PI-futex approaches,
0114 there is no prior 'registration' of a PI-futex. [which is not quite
0115 possible anyway, due to existing ABI properties of pthread mutexes.]
0116 
0117 Also, under this scheme, 'robustness' and 'PI' are two orthogonal
0118 properties of futexes, and all four combinations are possible: futex,
0119 robust-futex, PI-futex, robust+PI-futex.
0120 
0121 More details about priority inheritance can be found in
0122 Documentation/locking/rt-mutex.rst.