summaryrefslogtreecommitdiffstats
path: root/Documentation/locking
diff options
context:
space:
mode:
authorAlex Shi <alex.shi@linaro.org>2017-07-30 21:50:53 -0400
committerJonathan Corbet <corbet@lwn.net>2017-08-24 15:37:55 -0400
commitf1824df12ecd495b25c8c116876e201ac764ecea (patch)
tree1a4e8c6d15ee606b23b6e7dfd9c241855ee19c76 /Documentation/locking
parent0e4c2b75897c6a621546b4f11a42c6072e833f81 (diff)
rtmutex: update rt-mutex-design
The rt-mutex-design documents didn't gotten meaningful update from its first version. Even after owner's pending bit was removed in commit 8161239a8bcc ("rtmutex: Simplify PI algorithm and make highest prio task get lock") and priority list 'plist' changed to rbtree. And Peter Zijlstra did some clean up and fix for deadline task changes on tip tree. So update it to latest code and make it meaningful. Steven Rostedt and Sebastian Siewior gave much of comments and input in this doc. Thanks! Signed-off-by: Alex Shi <alex.shi@linaro.org> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Sebastian Siewior <bigeasy@linutronix.de> Cc: Mathieu Poirier <mathieu.poirier@linaro.org> Cc: Juri Lelli <juri.lelli@arm.com> Cc: Thomas Gleixner <tglx@linutronix.de> To: linux-doc@vger.kernel.org To: linux-kernel@vger.kernel.org To: Jonathan Corbet <corbet@lwn.net> To: Ingo Molnar <mingo@redhat.com> To: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Diffstat (limited to 'Documentation/locking')
-rw-r--r--Documentation/locking/rt-mutex-design.txt432
1 files changed, 105 insertions, 327 deletions
diff --git a/Documentation/locking/rt-mutex-design.txt b/Documentation/locking/rt-mutex-design.txt
index 8666070d3189..6c6e8c2410de 100644
--- a/Documentation/locking/rt-mutex-design.txt
+++ b/Documentation/locking/rt-mutex-design.txt
@@ -97,9 +97,9 @@ waiter - A waiter is a struct that is stored on the stack of a blocked
97 a process being blocked on the mutex, it is fine to allocate 97 a process being blocked on the mutex, it is fine to allocate
98 the waiter on the process's stack (local variable). This 98 the waiter on the process's stack (local variable). This
99 structure holds a pointer to the task, as well as the mutex that 99 structure holds a pointer to the task, as well as the mutex that
100 the task is blocked on. It also has the plist node structures to 100 the task is blocked on. It also has rbtree node structures to
101 place the task in the waiter_list of a mutex as well as the 101 place the task in the waiters rbtree of a mutex as well as the
102 pi_list of a mutex owner task (described below). 102 pi_waiters rbtree of a mutex owner task (described below).
103 103
104 waiter is sometimes used in reference to the task that is waiting 104 waiter is sometimes used in reference to the task that is waiting
105 on a mutex. This is the same as waiter->task. 105 on a mutex. This is the same as waiter->task.
@@ -179,53 +179,34 @@ again.
179 | 179 |
180 F->L5-+ 180 F->L5-+
181 181
182If process G has the highest priority in the chain, then all the tasks up
183the chain (A and B in this example), must have their priorities increased
184to that of G.
182 185
183Plist 186Mutex Waiters Tree
184-----
185
186Before I go further and talk about how the PI chain is stored through lists
187on both mutexes and processes, I'll explain the plist. This is similar to
188the struct list_head functionality that is already in the kernel.
189The implementation of plist is out of scope for this document, but it is
190very important to understand what it does.
191
192There are a few differences between plist and list, the most important one
193being that plist is a priority sorted linked list. This means that the
194priorities of the plist are sorted, such that it takes O(1) to retrieve the
195highest priority item in the list. Obviously this is useful to store processes
196based on their priorities.
197
198Another difference, which is important for implementation, is that, unlike
199list, the head of the list is a different element than the nodes of a list.
200So the head of the list is declared as struct plist_head and nodes that will
201be added to the list are declared as struct plist_node.
202
203
204Mutex Waiter List
205----------------- 187-----------------
206 188
207Every mutex keeps track of all the waiters that are blocked on itself. The mutex 189Every mutex keeps track of all the waiters that are blocked on itself. The
208has a plist to store these waiters by priority. This list is protected by 190mutex has a rbtree to store these waiters by priority. This tree is protected
209a spin lock that is located in the struct of the mutex. This lock is called 191by a spin lock that is located in the struct of the mutex. This lock is called
210wait_lock. Since the modification of the waiter list is never done in 192wait_lock.
211interrupt context, the wait_lock can be taken without disabling interrupts.
212 193
213 194
214Task PI List 195Task PI Tree
215------------ 196------------
216 197
217To keep track of the PI chains, each process has its own PI list. This is 198To keep track of the PI chains, each process has its own PI rbtree. This is
218a list of all top waiters of the mutexes that are owned by the process. 199a tree of all top waiters of the mutexes that are owned by the process.
219Note that this list only holds the top waiters and not all waiters that are 200Note that this tree only holds the top waiters and not all waiters that are
220blocked on mutexes owned by the process. 201blocked on mutexes owned by the process.
221 202
222The top of the task's PI list is always the highest priority task that 203The top of the task's PI tree is always the highest priority task that
223is waiting on a mutex that is owned by the task. So if the task has 204is waiting on a mutex that is owned by the task. So if the task has
224inherited a priority, it will always be the priority of the task that is 205inherited a priority, it will always be the priority of the task that is
225at the top of this list. 206at the top of this tree.
226 207
227This list is stored in the task structure of a process as a plist called 208This tree is stored in the task structure of a process as a rbtree called
228pi_list. This list is protected by a spin lock also in the task structure, 209pi_waiters. It is protected by a spin lock also in the task structure,
229called pi_lock. This lock may also be taken in interrupt context, so when 210called pi_lock. This lock may also be taken in interrupt context, so when
230locking the pi_lock, interrupts must be disabled. 211locking the pi_lock, interrupts must be disabled.
231 212
@@ -312,15 +293,12 @@ Mutex owner and flags
312 293
313The mutex structure contains a pointer to the owner of the mutex. If the 294The mutex structure contains a pointer to the owner of the mutex. If the
314mutex is not owned, this owner is set to NULL. Since all architectures 295mutex is not owned, this owner is set to NULL. Since all architectures
315have the task structure on at least a four byte alignment (and if this is 296have the task structure on at least a two byte alignment (and if this is
316not true, the rtmutex.c code will be broken!), this allows for the two 297not true, the rtmutex.c code will be broken!), this allows for the least
317least significant bits to be used as flags. This part is also described 298significant bit to be used as a flag. Bit 0 is used as the "Has Waiters"
318in Documentation/rt-mutex.txt, but will also be briefly described here. 299flag. It's set whenever there are waiters on a mutex.
319
320Bit 0 is used as the "Pending Owner" flag. This is described later.
321Bit 1 is used as the "Has Waiters" flags. This is also described later
322 in more detail, but is set whenever there are waiters on a mutex.
323 300
301See Documentation/locking/rt-mutex.txt for further details.
324 302
325cmpxchg Tricks 303cmpxchg Tricks
326-------------- 304--------------
@@ -359,40 +337,31 @@ Priority adjustments
359-------------------- 337--------------------
360 338
361The implementation of the PI code in rtmutex.c has several places that a 339The implementation of the PI code in rtmutex.c has several places that a
362process must adjust its priority. With the help of the pi_list of a 340process must adjust its priority. With the help of the pi_waiters of a
363process this is rather easy to know what needs to be adjusted. 341process this is rather easy to know what needs to be adjusted.
364 342
365The functions implementing the task adjustments are rt_mutex_adjust_prio, 343The functions implementing the task adjustments are rt_mutex_adjust_prio
366__rt_mutex_adjust_prio (same as the former, but expects the task pi_lock 344and rt_mutex_setprio. rt_mutex_setprio is only used in rt_mutex_adjust_prio.
367to already be taken), rt_mutex_getprio, and rt_mutex_setprio.
368 345
369rt_mutex_getprio and rt_mutex_setprio are only used in __rt_mutex_adjust_prio. 346rt_mutex_adjust_prio examines the priority of the task, and the highest
347priority process that is waiting any of mutexes owned by the task. Since
348the pi_waiters of a task holds an order by priority of all the top waiters
349of all the mutexes that the task owns, we simply need to compare the top
350pi waiter to its own normal/deadline priority and take the higher one.
351Then rt_mutex_setprio is called to adjust the priority of the task to the
352new priority. Note that rt_mutex_setprio is defined in kernel/sched/core.c
353to implement the actual change in priority.
370 354
371rt_mutex_getprio returns the priority that the task should have. Either the 355(Note: For the "prio" field in task_struct, the lower the number, the
372task's own normal priority, or if a process of a higher priority is waiting on 356 higher the priority. A "prio" of 5 is of higher priority than a
373a mutex owned by the task, then that higher priority should be returned. 357 "prio" of 10.)
374Since the pi_list of a task holds an order by priority list of all the top
375waiters of all the mutexes that the task owns, rt_mutex_getprio simply needs
376to compare the top pi waiter to its own normal priority, and return the higher
377priority back.
378 358
379(Note: if looking at the code, you will notice that the lower number of 359It is interesting to note that rt_mutex_adjust_prio can either increase
380 prio is returned. This is because the prio field in the task structure
381 is an inverse order of the actual priority. So a "prio" of 5 is
382 of higher priority than a "prio" of 10.)
383
384__rt_mutex_adjust_prio examines the result of rt_mutex_getprio, and if the
385result does not equal the task's current priority, then rt_mutex_setprio
386is called to adjust the priority of the task to the new priority.
387Note that rt_mutex_setprio is defined in kernel/sched/core.c to implement the
388actual change in priority.
389
390It is interesting to note that __rt_mutex_adjust_prio can either increase
391or decrease the priority of the task. In the case that a higher priority 360or decrease the priority of the task. In the case that a higher priority
392process has just blocked on a mutex owned by the task, __rt_mutex_adjust_prio 361process has just blocked on a mutex owned by the task, rt_mutex_adjust_prio
393would increase/boost the task's priority. But if a higher priority task 362would increase/boost the task's priority. But if a higher priority task
394were for some reason to leave the mutex (timeout or signal), this same function 363were for some reason to leave the mutex (timeout or signal), this same function
395would decrease/unboost the priority of the task. That is because the pi_list 364would decrease/unboost the priority of the task. That is because the pi_waiters
396always contains the highest priority task that is waiting on a mutex owned 365always contains the highest priority task that is waiting on a mutex owned
397by the task, so we only need to compare the priority of that top pi waiter 366by the task, so we only need to compare the priority of that top pi waiter
398to the normal priority of the given task. 367to the normal priority of the given task.
@@ -412,9 +381,10 @@ priorities.
412 381
413rt_mutex_adjust_prio_chain is called with a task to be checked for PI 382rt_mutex_adjust_prio_chain is called with a task to be checked for PI
414(de)boosting (the owner of a mutex that a process is blocking on), a flag to 383(de)boosting (the owner of a mutex that a process is blocking on), a flag to
415check for deadlocking, the mutex that the task owns, and a pointer to a waiter 384check for deadlocking, the mutex that the task owns, a pointer to a waiter
416that is the process's waiter struct that is blocked on the mutex (although this 385that is the process's waiter struct that is blocked on the mutex (although this
417parameter may be NULL for deboosting). 386parameter may be NULL for deboosting), a pointer to the mutex on which the task
387is blocked, and a top_task as the top waiter of the mutex.
418 388
419For this explanation, I will not mention deadlock detection. This explanation 389For this explanation, I will not mention deadlock detection. This explanation
420will try to stay at a high level. 390will try to stay at a high level.
@@ -424,133 +394,14 @@ that the state of the owner and lock can change when entered into this function.
424 394
425Before this function is called, the task has already had rt_mutex_adjust_prio 395Before this function is called, the task has already had rt_mutex_adjust_prio
426performed on it. This means that the task is set to the priority that it 396performed on it. This means that the task is set to the priority that it
427should be at, but the plist nodes of the task's waiter have not been updated 397should be at, but the rbtree nodes of the task's waiter have not been updated
428with the new priorities, and that this task may not be in the proper locations 398with the new priorities, and this task may not be in the proper locations
429in the pi_lists and wait_lists that the task is blocked on. This function 399in the pi_waiters and waiters trees that the task is blocked on. This function
430solves all that. 400solves all that.
431 401
432A loop is entered, where task is the owner to be checked for PI changes that 402The main operation of this function is summarized by Thomas Gleixner in
433was passed by parameter (for the first iteration). The pi_lock of this task is 403rtmutex.c. See the 'Chain walk basics and protection scope' comment for further
434taken to prevent any more changes to the pi_list of the task. This also 404details.
435prevents new tasks from completing the blocking on a mutex that is owned by this
436task.
437
438If the task is not blocked on a mutex then the loop is exited. We are at
439the top of the PI chain.
440
441A check is now done to see if the original waiter (the process that is blocked
442on the current mutex) is the top pi waiter of the task. That is, is this
443waiter on the top of the task's pi_list. If it is not, it either means that
444there is another process higher in priority that is blocked on one of the
445mutexes that the task owns, or that the waiter has just woken up via a signal
446or timeout and has left the PI chain. In either case, the loop is exited, since
447we don't need to do any more changes to the priority of the current task, or any
448task that owns a mutex that this current task is waiting on. A priority chain
449walk is only needed when a new top pi waiter is made to a task.
450
451The next check sees if the task's waiter plist node has the priority equal to
452the priority the task is set at. If they are equal, then we are done with
453the loop. Remember that the function started with the priority of the
454task adjusted, but the plist nodes that hold the task in other processes
455pi_lists have not been adjusted.
456
457Next, we look at the mutex that the task is blocked on. The mutex's wait_lock
458is taken. This is done by a spin_trylock, because the locking order of the
459pi_lock and wait_lock goes in the opposite direction. If we fail to grab the
460lock, the pi_lock is released, and we restart the loop.
461
462Now that we have both the pi_lock of the task as well as the wait_lock of
463the mutex the task is blocked on, we update the task's waiter's plist node
464that is located on the mutex's wait_list.
465
466Now we release the pi_lock of the task.
467
468Next the owner of the mutex has its pi_lock taken, so we can update the
469task's entry in the owner's pi_list. If the task is the highest priority
470process on the mutex's wait_list, then we remove the previous top waiter
471from the owner's pi_list, and replace it with the task.
472
473Note: It is possible that the task was the current top waiter on the mutex,
474 in which case the task is not yet on the pi_list of the waiter. This
475 is OK, since plist_del does nothing if the plist node is not on any
476 list.
477
478If the task was not the top waiter of the mutex, but it was before we
479did the priority updates, that means we are deboosting/lowering the
480task. In this case, the task is removed from the pi_list of the owner,
481and the new top waiter is added.
482
483Lastly, we unlock both the pi_lock of the task, as well as the mutex's
484wait_lock, and continue the loop again. On the next iteration of the
485loop, the previous owner of the mutex will be the task that will be
486processed.
487
488Note: One might think that the owner of this mutex might have changed
489 since we just grab the mutex's wait_lock. And one could be right.
490 The important thing to remember is that the owner could not have
491 become the task that is being processed in the PI chain, since
492 we have taken that task's pi_lock at the beginning of the loop.
493 So as long as there is an owner of this mutex that is not the same
494 process as the tasked being worked on, we are OK.
495
496 Looking closely at the code, one might be confused. The check for the
497 end of the PI chain is when the task isn't blocked on anything or the
498 task's waiter structure "task" element is NULL. This check is
499 protected only by the task's pi_lock. But the code to unlock the mutex
500 sets the task's waiter structure "task" element to NULL with only
501 the protection of the mutex's wait_lock, which was not taken yet.
502 Isn't this a race condition if the task becomes the new owner?
503
504 The answer is No! The trick is the spin_trylock of the mutex's
505 wait_lock. If we fail that lock, we release the pi_lock of the
506 task and continue the loop, doing the end of PI chain check again.
507
508 In the code to release the lock, the wait_lock of the mutex is held
509 the entire time, and it is not let go when we grab the pi_lock of the
510 new owner of the mutex. So if the switch of a new owner were to happen
511 after the check for end of the PI chain and the grabbing of the
512 wait_lock, the unlocking code would spin on the new owner's pi_lock
513 but never give up the wait_lock. So the PI chain loop is guaranteed to
514 fail the spin_trylock on the wait_lock, release the pi_lock, and
515 try again.
516
517 If you don't quite understand the above, that's OK. You don't have to,
518 unless you really want to make a proof out of it ;)
519
520
521Pending Owners and Lock stealing
522--------------------------------
523
524One of the flags in the owner field of the mutex structure is "Pending Owner".
525What this means is that an owner was chosen by the process releasing the
526mutex, but that owner has yet to wake up and actually take the mutex.
527
528Why is this important? Why can't we just give the mutex to another process
529and be done with it?
530
531The PI code is to help with real-time processes, and to let the highest
532priority process run as long as possible with little latencies and delays.
533If a high priority process owns a mutex that a lower priority process is
534blocked on, when the mutex is released it would be given to the lower priority
535process. What if the higher priority process wants to take that mutex again.
536The high priority process would fail to take that mutex that it just gave up
537and it would need to boost the lower priority process to run with full
538latency of that critical section (since the low priority process just entered
539it).
540
541There's no reason a high priority process that gives up a mutex should be
542penalized if it tries to take that mutex again. If the new owner of the
543mutex has not woken up yet, there's no reason that the higher priority process
544could not take that mutex away.
545
546To solve this, we introduced Pending Ownership and Lock Stealing. When a
547new process is given a mutex that it was blocked on, it is only given
548pending ownership. This means that it's the new owner, unless a higher
549priority process comes in and tries to grab that mutex. If a higher priority
550process does come along and wants that mutex, we let the higher priority
551process "steal" the mutex from the pending owner (only if it is still pending)
552and continue with the mutex.
553
554 405
555Taking of a mutex (The walk through) 406Taking of a mutex (The walk through)
556------------------------------------ 407------------------------------------
@@ -563,14 +414,14 @@ done when we have CMPXCHG enabled (otherwise the fast taking automatically
563fails). Only when the owner field of the mutex is NULL can the lock be 414fails). Only when the owner field of the mutex is NULL can the lock be
564taken with the CMPXCHG and nothing else needs to be done. 415taken with the CMPXCHG and nothing else needs to be done.
565 416
566If there is contention on the lock, whether it is owned or pending owner 417If there is contention on the lock, we go about the slow path
567we go about the slow path (rt_mutex_slowlock). 418(rt_mutex_slowlock).
568 419
569The slow path function is where the task's waiter structure is created on 420The slow path function is where the task's waiter structure is created on
570the stack. This is because the waiter structure is only needed for the 421the stack. This is because the waiter structure is only needed for the
571scope of this function. The waiter structure holds the nodes to store 422scope of this function. The waiter structure holds the nodes to store
572the task on the wait_list of the mutex, and if need be, the pi_list of 423the task on the waiters tree of the mutex, and if need be, the pi_waiters
573the owner. 424tree of the owner.
574 425
575The wait_lock of the mutex is taken since the slow path of unlocking the 426The wait_lock of the mutex is taken since the slow path of unlocking the
576mutex also takes this lock. 427mutex also takes this lock.
@@ -581,102 +432,45 @@ contention).
581 432
582try_to_take_rt_mutex is used every time the task tries to grab a mutex in the 433try_to_take_rt_mutex is used every time the task tries to grab a mutex in the
583slow path. The first thing that is done here is an atomic setting of 434slow path. The first thing that is done here is an atomic setting of
584the "Has Waiters" flag of the mutex's owner field. Yes, this could really 435the "Has Waiters" flag of the mutex's owner field. By setting this flag
585be false, because if the mutex has no owner, there are no waiters and 436now, the current owner of the mutex being contended for can't release the mutex
586the current task also won't have any waiters. But we don't have the lock 437without going into the slow unlock path, and it would then need to grab the
587yet, so we assume we are going to be a waiter. The reason for this is to 438wait_lock, which this code currently holds. So setting the "Has Waiters" flag
588play nice for those architectures that do have CMPXCHG. By setting this flag 439forces the current owner to synchronize with this code.
589now, the owner of the mutex can't release the mutex without going into the 440
590slow unlock path, and it would then need to grab the wait_lock, which this 441The lock is taken if the following are true:
591code currently holds. So setting the "Has Waiters" flag forces the owner 442 1) The lock has no owner
592to synchronize with this code. 443 2) The current task is the highest priority against all other
593 444 waiters of the lock
594Now that we know that we can't have any races with the owner releasing the 445
595mutex, we check to see if we can take the ownership. This is done if the 446If the task succeeds to acquire the lock, then the task is set as the
596mutex doesn't have a owner, or if we can steal the mutex from a pending 447owner of the lock, and if the lock still has waiters, the top_waiter
597owner. Let's look at the situations we have here. 448(highest priority task waiting on the lock) is added to this task's
598 449pi_waiters tree.
599 1) Has owner that is pending 450
600 ---------------------------- 451If the lock is not taken by try_to_take_rt_mutex(), then the
601 452task_blocks_on_rt_mutex() function is called. This will add the task to
602 The mutex has a owner, but it hasn't woken up and the mutex flag 453the lock's waiter tree and propagate the pi chain of the lock as well
603 "Pending Owner" is set. The first check is to see if the owner isn't the 454as the lock's owner's pi_waiters tree. This is described in the next
604 current task. This is because this function is also used for the pending 455section.
605 owner to grab the mutex. When a pending owner wakes up, it checks to see
606 if it can take the mutex, and this is done if the owner is already set to
607 itself. If so, we succeed and leave the function, clearing the "Pending
608 Owner" bit.
609
610 If the pending owner is not current, we check to see if the current priority is
611 higher than the pending owner. If not, we fail the function and return.
612
613 There's also something special about a pending owner. That is a pending owner
614 is never blocked on a mutex. So there is no PI chain to worry about. It also
615 means that if the mutex doesn't have any waiters, there's no accounting needed
616 to update the pending owner's pi_list, since we only worry about processes
617 blocked on the current mutex.
618
619 If there are waiters on this mutex, and we just stole the ownership, we need
620 to take the top waiter, remove it from the pi_list of the pending owner, and
621 add it to the current pi_list. Note that at this moment, the pending owner
622 is no longer on the list of waiters. This is fine, since the pending owner
623 would add itself back when it realizes that it had the ownership stolen
624 from itself. When the pending owner tries to grab the mutex, it will fail
625 in try_to_take_rt_mutex if the owner field points to another process.
626
627 2) No owner
628 -----------
629
630 If there is no owner (or we successfully stole the lock), we set the owner
631 of the mutex to current, and set the flag of "Has Waiters" if the current
632 mutex actually has waiters, or we clear the flag if it doesn't. See, it was
633 OK that we set that flag early, since now it is cleared.
634
635 3) Failed to grab ownership
636 ---------------------------
637
638 The most interesting case is when we fail to take ownership. This means that
639 there exists an owner, or there's a pending owner with equal or higher
640 priority than the current task.
641
642We'll continue on the failed case.
643
644If the mutex has a timeout, we set up a timer to go off to break us out
645of this mutex if we failed to get it after a specified amount of time.
646
647Now we enter a loop that will continue to try to take ownership of the mutex, or
648fail from a timeout or signal.
649
650Once again we try to take the mutex. This will usually fail the first time
651in the loop, since it had just failed to get the mutex. But the second time
652in the loop, this would likely succeed, since the task would likely be
653the pending owner.
654
655If the mutex is TASK_INTERRUPTIBLE a check for signals and timeout is done
656here.
657
658The waiter structure has a "task" field that points to the task that is blocked
659on the mutex. This field can be NULL the first time it goes through the loop
660or if the task is a pending owner and had its mutex stolen. If the "task"
661field is NULL then we need to set up the accounting for it.
662 456
663Task blocks on mutex 457Task blocks on mutex
664-------------------- 458--------------------
665 459
666The accounting of a mutex and process is done with the waiter structure of 460The accounting of a mutex and process is done with the waiter structure of
667the process. The "task" field is set to the process, and the "lock" field 461the process. The "task" field is set to the process, and the "lock" field
668to the mutex. The plist nodes are initialized to the processes current 462to the mutex. The rbtree node of waiter are initialized to the processes
669priority. 463current priority.
670 464
671Since the wait_lock was taken at the entry of the slow lock, we can safely 465Since the wait_lock was taken at the entry of the slow lock, we can safely
672add the waiter to the wait_list. If the current process is the highest 466add the waiter to the task waiter tree. If the current process is the
673priority process currently waiting on this mutex, then we remove the 467highest priority process currently waiting on this mutex, then we remove the
674previous top waiter process (if it exists) from the pi_list of the owner, 468previous top waiter process (if it exists) from the pi_waiters of the owner,
675and add the current process to that list. Since the pi_list of the owner 469and add the current process to that tree. Since the pi_waiter of the owner
676has changed, we call rt_mutex_adjust_prio on the owner to see if the owner 470has changed, we call rt_mutex_adjust_prio on the owner to see if the owner
677should adjust its priority accordingly. 471should adjust its priority accordingly.
678 472
679If the owner is also blocked on a lock, and had its pi_list changed 473If the owner is also blocked on a lock, and had its pi_waiters changed
680(or deadlock checking is on), we unlock the wait_lock of the mutex and go ahead 474(or deadlock checking is on), we unlock the wait_lock of the mutex and go ahead
681and run rt_mutex_adjust_prio_chain on the owner, as described earlier. 475and run rt_mutex_adjust_prio_chain on the owner, as described earlier.
682 476
@@ -686,30 +480,23 @@ mutex (waiter "task" field is not NULL), then we go to sleep (call schedule).
686Waking up in the loop 480Waking up in the loop
687--------------------- 481---------------------
688 482
689The schedule can then wake up for a few reasons. 483The task can then wake up for a couple of reasons:
690 1) we were given pending ownership of the mutex. 484 1) The previous lock owner released the lock, and the task now is top_waiter
691 2) we received a signal and was TASK_INTERRUPTIBLE 485 2) we received a signal or timeout
692 3) we had a timeout and was TASK_INTERRUPTIBLE
693 486
694In any of these cases, we continue the loop and once again try to grab the 487In both cases, the task will try again to acquire the lock. If it
695ownership of the mutex. If we succeed, we exit the loop, otherwise we continue 488does, then it will take itself off the waiters tree and set itself back
696and on signal and timeout, will exit the loop, or if we had the mutex stolen 489to the TASK_RUNNING state.
697we just simply add ourselves back on the lists and go back to sleep.
698 490
699Note: For various reasons, because of timeout and signals, the steal mutex 491In first case, if the lock was acquired by another task before this task
700 algorithm needs to be careful. This is because the current process is 492could get the lock, then it will go back to sleep and wait to be woken again.
701 still on the wait_list. And because of dynamic changing of priorities,
702 especially on SCHED_OTHER tasks, the current process can be the
703 highest priority task on the wait_list.
704
705Failed to get mutex on Timeout or Signal
706----------------------------------------
707 493
708If a timeout or signal occurred, the waiter's "task" field would not be 494The second case is only applicable for tasks that are grabbing a mutex
709NULL and the task needs to be taken off the wait_list of the mutex and perhaps 495that can wake up before getting the lock, either due to a signal or
710pi_list of the owner. If this process was a high priority process, then 496a timeout (i.e. rt_mutex_timed_futex_lock()). When woken, it will try to
711the rt_mutex_adjust_prio_chain needs to be executed again on the owner, 497take the lock again, if it succeeds, then the task will return with the
712but this time it will be lowering the priorities. 498lock held, otherwise it will return with -EINTR if the task was woken
499by a signal, or -ETIMEDOUT if it timed out.
713 500
714 501
715Unlocking the Mutex 502Unlocking the Mutex
@@ -739,25 +526,12 @@ owner still needs to make this check. If there are no waiters then the mutex
739owner field is set to NULL, the wait_lock is released and nothing more is 526owner field is set to NULL, the wait_lock is released and nothing more is
740needed. 527needed.
741 528
742If there are waiters, then we need to wake one up and give that waiter 529If there are waiters, then we need to wake one up.
743pending ownership.
744 530
745On the wake up code, the pi_lock of the current owner is taken. The top 531On the wake up code, the pi_lock of the current owner is taken. The top
746waiter of the lock is found and removed from the wait_list of the mutex 532waiter of the lock is found and removed from the waiters tree of the mutex
747as well as the pi_list of the current owner. The task field of the new 533as well as the pi_waiters tree of the current owner. The "Has Waiters" bit is
748pending owner's waiter structure is set to NULL, and the owner field of the 534marked to prevent lower priority tasks from stealing the lock.
749mutex is set to the new owner with the "Pending Owner" bit set, as well
750as the "Has Waiters" bit if there still are other processes blocked on the
751mutex.
752
753The pi_lock of the previous owner is released, and the new pending owner's
754pi_lock is taken. Remember that this is the trick to prevent the race
755condition in rt_mutex_adjust_prio_chain from adding itself as a waiter
756on the mutex.
757
758We now clear the "pi_blocked_on" field of the new pending owner, and if
759the mutex still has waiters pending, we add the new top waiter to the pi_list
760of the pending owner.
761 535
762Finally we unlock the pi_lock of the pending owner and wake it up. 536Finally we unlock the pi_lock of the pending owner and wake it up.
763 537
@@ -772,10 +546,14 @@ Credits
772------- 546-------
773 547
774Author: Steven Rostedt <rostedt@goodmis.org> 548Author: Steven Rostedt <rostedt@goodmis.org>
549Updated: Alex Shi <alex.shi@linaro.org> - 7/6/2017
775 550
776Reviewers: Ingo Molnar, Thomas Gleixner, Thomas Duetsch, and Randy Dunlap 551Original Reviewers: Ingo Molnar, Thomas Gleixner, Thomas Duetsch, and
552 Randy Dunlap
553Update (7/6/2017) Reviewers: Steven Rostedt and Sebastian Siewior
777 554
778Updates 555Updates
779------- 556-------
780 557
781This document was originally written for 2.6.17-rc3-mm1 558This document was originally written for 2.6.17-rc3-mm1
559was updated on 4.12