~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/locking/pi-futex.rst

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /Documentation/locking/pi-futex.rst (Version linux-6.12-rc7) and /Documentation/locking/pi-futex.rst (Version linux-5.9.16)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php