blob: 3b5097a380e6bd90e69ce3999a15683159789076 [file] [log] [blame]
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -03001==================================
Steven Rostedta6537be2006-06-27 02:54:54 -07002RT-mutex subsystem with PI support
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -03003==================================
Steven Rostedta6537be2006-06-27 02:54:54 -07004
5RT-mutexes with priority inheritance are used to support PI-futexes,
6which enable pthread_mutex_t priority inheritance attributes
Mauro Carvalho Chehab95ca6d72020-05-01 17:37:54 +02007(PTHREAD_PRIO_INHERIT). [See Documentation/locking/pi-futex.rst for more details
Steven Rostedta6537be2006-06-27 02:54:54 -07008about PI-futexes.]
9
10This technology was developed in the -rt tree and streamlined for
11pthread_mutex support.
12
13Basic principles:
14-----------------
15
16RT-mutexes extend the semantics of simple mutexes by the priority
17inheritance protocol.
18
19A low priority owner of a rt-mutex inherits the priority of a higher
20priority waiter until the rt-mutex is released. If the temporarily
21boosted owner blocks on a rt-mutex itself it propagates the priority
22boosting to the owner of the other rt_mutex it gets blocked on. The
23priority boosting is immediately removed once the rt_mutex has been
24unlocked.
25
26This approach allows us to shorten the block of high-prio tasks on
27mutexes which protect shared resources. Priority inheritance is not a
28magic bullet for poorly designed applications, but it allows
29well-designed applications to use userspace locks in critical parts of
30an high priority thread, without losing determinism.
31
Alex Shi68a1e342017-07-31 09:50:54 +080032The enqueueing of the waiters into the rtmutex waiter tree is done in
Steven Rostedta6537be2006-06-27 02:54:54 -070033priority order. For same priorities FIFO order is chosen. For each
34rtmutex, only the top priority waiter is enqueued into the owner's
Alex Shi68a1e342017-07-31 09:50:54 +080035priority waiters tree. This tree too queues in priority order. Whenever
Steven Rostedta6537be2006-06-27 02:54:54 -070036the top priority waiter of a task changes (for example it timed out or
Alex Shi68a1e342017-07-31 09:50:54 +080037got a signal), the priority of the owner task is readjusted. The
38priority enqueueing is handled by "pi_waiters".
Steven Rostedta6537be2006-06-27 02:54:54 -070039
40RT-mutexes are optimized for fastpath operations and have no internal
41locking overhead when locking an uncontended mutex or unlocking a mutex
42without waiters. The optimized fastpath operations require cmpxchg
43support. [If that is not available then the rt-mutex internal spinlock
44is used]
45
46The state of the rt-mutex is tracked via the owner field of the rt-mutex
47structure:
48
Alex Shi68a1e342017-07-31 09:50:54 +080049lock->owner holds the task_struct pointer of the owner. Bit 0 is used to
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030050keep track of the "lock has waiters" state:
Steven Rostedta6537be2006-06-27 02:54:54 -070051
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030052 ============ ======= ================================================
53 owner bit0 Notes
54 ============ ======= ================================================
Alex Shi68a1e342017-07-31 09:50:54 +080055 NULL 0 lock is free (fast acquire possible)
56 NULL 1 lock is free and has waiters and the top waiter
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030057 is going to take the lock [1]_
Alex Shi68a1e342017-07-31 09:50:54 +080058 taskpointer 0 lock is held (fast release possible)
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030059 taskpointer 1 lock is held and has waiters [2]_
60 ============ ======= ================================================
Steven Rostedta6537be2006-06-27 02:54:54 -070061
Alex Shi68a1e342017-07-31 09:50:54 +080062The fast atomic compare exchange based acquire and release is only
63possible when bit 0 of lock->owner is 0.
Steven Rostedta6537be2006-06-27 02:54:54 -070064
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030065.. [1] It also can be a transitional state when grabbing the lock
66 with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
67 we need to set the bit0 before looking at the lock, and the owner may
68 be NULL in this small time, hence this can be a transitional state.
Steven Rostedta6537be2006-06-27 02:54:54 -070069
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030070.. [2] There is a small time when bit 0 is set but there are no
71 waiters. This can happen when grabbing the lock in the slow path.
72 To prevent a cmpxchg of the owner releasing the lock, we need to
73 set this bit before looking at the lock.
Alex Shi68a1e342017-07-31 09:50:54 +080074
75BTW, there is still technically a "Pending Owner", it's just not called
76that anymore. The pending owner happens to be the top_waiter of a lock
77that has no owner and has been woken up to grab the lock.