volatile-considered-harmful.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. .. _volatile_considered_harmful:
  2. Why the "volatile" type class should not be used
  3. ------------------------------------------------
  4. C programmers have often taken volatile to mean that the variable could be
  5. changed outside of the current thread of execution; as a result, they are
  6. sometimes tempted to use it in kernel code when shared data structures are
  7. being used. In other words, they have been known to treat volatile types
  8. as a sort of easy atomic variable, which they are not. The use of volatile in
  9. kernel code is almost never correct; this document describes why.
  10. The key point to understand with regard to volatile is that its purpose is
  11. to suppress optimization, which is almost never what one really wants to
  12. do. In the kernel, one must protect shared data structures against
  13. unwanted concurrent access, which is very much a different task. The
  14. process of protecting against unwanted concurrency will also avoid almost
  15. all optimization-related problems in a more efficient way.
  16. Like volatile, the kernel primitives which make concurrent access to data
  17. safe (spinlocks, mutexes, memory barriers, etc.) are designed to prevent
  18. unwanted optimization. If they are being used properly, there will be no
  19. need to use volatile as well. If volatile is still necessary, there is
  20. almost certainly a bug in the code somewhere. In properly-written kernel
  21. code, volatile can only serve to slow things down.
  22. Consider a typical block of kernel code::
  23. spin_lock(&the_lock);
  24. do_something_on(&shared_data);
  25. do_something_else_with(&shared_data);
  26. spin_unlock(&the_lock);
  27. If all the code follows the locking rules, the value of shared_data cannot
  28. change unexpectedly while the_lock is held. Any other code which might
  29. want to play with that data will be waiting on the lock. The spinlock
  30. primitives act as memory barriers - they are explicitly written to do so -
  31. meaning that data accesses will not be optimized across them. So the
  32. compiler might think it knows what will be in shared_data, but the
  33. spin_lock() call, since it acts as a memory barrier, will force it to
  34. forget anything it knows. There will be no optimization problems with
  35. accesses to that data.
  36. If shared_data were declared volatile, the locking would still be
  37. necessary. But the compiler would also be prevented from optimizing access
  38. to shared_data _within_ the critical section, when we know that nobody else
  39. can be working with it. While the lock is held, shared_data is not
  40. volatile. When dealing with shared data, proper locking makes volatile
  41. unnecessary - and potentially harmful.
  42. The volatile storage class was originally meant for memory-mapped I/O
  43. registers. Within the kernel, register accesses, too, should be protected
  44. by locks, but one also does not want the compiler "optimizing" register
  45. accesses within a critical section. But, within the kernel, I/O memory
  46. accesses are always done through accessor functions; accessing I/O memory
  47. directly through pointers is frowned upon and does not work on all
  48. architectures. Those accessors are written to prevent unwanted
  49. optimization, so, once again, volatile is unnecessary.
  50. Another situation where one might be tempted to use volatile is
  51. when the processor is busy-waiting on the value of a variable. The right
  52. way to perform a busy wait is::
  53. while (my_variable != what_i_want)
  54. cpu_relax();
  55. The cpu_relax() call can lower CPU power consumption or yield to a
  56. hyperthreaded twin processor; it also happens to serve as a compiler
  57. barrier, so, once again, volatile is unnecessary. Of course, busy-
  58. waiting is generally an anti-social act to begin with.
  59. There are still a few rare situations where volatile makes sense in the
  60. kernel:
  61. - The above-mentioned accessor functions might use volatile on
  62. architectures where direct I/O memory access does work. Essentially,
  63. each accessor call becomes a little critical section on its own and
  64. ensures that the access happens as expected by the programmer.
  65. - Inline assembly code which changes memory, but which has no other
  66. visible side effects, risks being deleted by GCC. Adding the volatile
  67. keyword to asm statements will prevent this removal.
  68. - The jiffies variable is special in that it can have a different value
  69. every time it is referenced, but it can be read without any special
  70. locking. So jiffies can be volatile, but the addition of other
  71. variables of this type is strongly frowned upon. Jiffies is considered
  72. to be a "stupid legacy" issue (Linus's words) in this regard; fixing it
  73. would be more trouble than it is worth.
  74. - Pointers to data structures in coherent memory which might be modified
  75. by I/O devices can, sometimes, legitimately be volatile. A ring buffer
  76. used by a network adapter, where that adapter changes pointers to
  77. indicate which descriptors have been processed, is an example of this
  78. type of situation.
  79. For most code, none of the above justifications for volatile apply. As a
  80. result, the use of volatile is likely to be seen as a bug and will bring
  81. additional scrutiny to the code. Developers who are tempted to use
  82. volatile should take a step back and think about what they are truly trying
  83. to accomplish.
  84. Patches to remove volatile variables are generally welcome - as long as
  85. they come with a justification which shows that the concurrency issues have
  86. been properly thought through.
  87. References
  88. ==========
  89. [1] https://lwn.net/Articles/233481/
  90. [2] https://lwn.net/Articles/233482/
  91. Credits
  92. =======
  93. Original impetus and research by Randy Dunlap
  94. Written by Jonathan Corbet
  95. Improvements via comments from Satyam Sharma, Johannes Stezenbach, Jesper
  96. Juhl, Heikki Orsila, H. Peter Anvin, Philipp Hahn, and Stefan
  97. Richter.