UP.txt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. RCU on Uniprocessor Systems
  2. A common misconception is that, on UP systems, the call_rcu() primitive
  3. may immediately invoke its function. The basis of this misconception
  4. is that since there is only one CPU, it should not be necessary to
  5. wait for anything else to get done, since there are no other CPUs for
  6. anything else to be happening on. Although this approach will -sort- -of-
  7. work a surprising amount of the time, it is a very bad idea in general.
  8. This document presents three examples that demonstrate exactly how bad
  9. an idea this is.
  10. Example 1: softirq Suicide
  11. Suppose that an RCU-based algorithm scans a linked list containing
  12. elements A, B, and C in process context, and can delete elements from
  13. this same list in softirq context. Suppose that the process-context scan
  14. is referencing element B when it is interrupted by softirq processing,
  15. which deletes element B, and then invokes call_rcu() to free element B
  16. after a grace period.
  17. Now, if call_rcu() were to directly invoke its arguments, then upon return
  18. from softirq, the list scan would find itself referencing a newly freed
  19. element B. This situation can greatly decrease the life expectancy of
  20. your kernel.
  21. This same problem can occur if call_rcu() is invoked from a hardware
  22. interrupt handler.
  23. Example 2: Function-Call Fatality
  24. Of course, one could avert the suicide described in the preceding example
  25. by having call_rcu() directly invoke its arguments only if it was called
  26. from process context. However, this can fail in a similar manner.
  27. Suppose that an RCU-based algorithm again scans a linked list containing
  28. elements A, B, and C in process contexts, but that it invokes a function
  29. on each element as it is scanned. Suppose further that this function
  30. deletes element B from the list, then passes it to call_rcu() for deferred
  31. freeing. This may be a bit unconventional, but it is perfectly legal
  32. RCU usage, since call_rcu() must wait for a grace period to elapse.
  33. Therefore, in this case, allowing call_rcu() to immediately invoke
  34. its arguments would cause it to fail to make the fundamental guarantee
  35. underlying RCU, namely that call_rcu() defers invoking its arguments until
  36. all RCU read-side critical sections currently executing have completed.
  37. Quick Quiz #1: why is it -not- legal to invoke synchronize_rcu() in
  38. this case?
  39. Example 3: Death by Deadlock
  40. Suppose that call_rcu() is invoked while holding a lock, and that the
  41. callback function must acquire this same lock. In this case, if
  42. call_rcu() were to directly invoke the callback, the result would
  43. be self-deadlock.
  44. In some cases, it would possible to restructure to code so that
  45. the call_rcu() is delayed until after the lock is released. However,
  46. there are cases where this can be quite ugly:
  47. 1. If a number of items need to be passed to call_rcu() within
  48. the same critical section, then the code would need to create
  49. a list of them, then traverse the list once the lock was
  50. released.
  51. 2. In some cases, the lock will be held across some kernel API,
  52. so that delaying the call_rcu() until the lock is released
  53. requires that the data item be passed up via a common API.
  54. It is far better to guarantee that callbacks are invoked
  55. with no locks held than to have to modify such APIs to allow
  56. arbitrary data items to be passed back up through them.
  57. If call_rcu() directly invokes the callback, painful locking restrictions
  58. or API changes would be required.
  59. Quick Quiz #2: What locking restriction must RCU callbacks respect?
  60. Summary
  61. Permitting call_rcu() to immediately invoke its arguments breaks RCU,
  62. even on a UP system. So do not do it! Even on a UP system, the RCU
  63. infrastructure -must- respect grace periods, and -must- invoke callbacks
  64. from a known environment in which no locks are held.
  65. It -is- safe for synchronize_sched() and synchronize_rcu_bh() to return
  66. immediately on an UP system. It is also safe for synchronize_rcu()
  67. to return immediately on UP systems, except when running preemptable
  68. RCU.
  69. Quick Quiz #3: Why can't synchronize_rcu() return immediately on
  70. UP systems running preemptable RCU?
  71. Answer to Quick Quiz #1:
  72. Why is it -not- legal to invoke synchronize_rcu() in this case?
  73. Because the calling function is scanning an RCU-protected linked
  74. list, and is therefore within an RCU read-side critical section.
  75. Therefore, the called function has been invoked within an RCU
  76. read-side critical section, and is not permitted to block.
  77. Answer to Quick Quiz #2:
  78. What locking restriction must RCU callbacks respect?
  79. Any lock that is acquired within an RCU callback must be
  80. acquired elsewhere using an _irq variant of the spinlock
  81. primitive. For example, if "mylock" is acquired by an
  82. RCU callback, then a process-context acquisition of this
  83. lock must use something like spin_lock_irqsave() to
  84. acquire the lock.
  85. If the process-context code were to simply use spin_lock(),
  86. then, since RCU callbacks can be invoked from softirq context,
  87. the callback might be called from a softirq that interrupted
  88. the process-context critical section. This would result in
  89. self-deadlock.
  90. This restriction might seem gratuitous, since very few RCU
  91. callbacks acquire locks directly. However, a great many RCU
  92. callbacks do acquire locks -indirectly-, for example, via
  93. the kfree() primitive.
  94. Answer to Quick Quiz #3:
  95. Why can't synchronize_rcu() return immediately on UP systems
  96. running preemptable RCU?
  97. Because some other task might have been preempted in the middle
  98. of an RCU read-side critical section. If synchronize_rcu()
  99. simply immediately returned, it would prematurely signal the
  100. end of the grace period, which would come as a nasty shock to
  101. that other thread when it started running again.