UP.txt 5.0 KB

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