cli-sti-removal.txt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #### cli()/sti() removal guide, started by Ingo Molnar <mingo@redhat.com>
  2. as of 2.5.28, five popular macros have been removed on SMP, and
  3. are being phased out on UP:
  4. cli(), sti(), save_flags(flags), save_flags_cli(flags), restore_flags(flags)
  5. until now it was possible to protect driver code against interrupt
  6. handlers via a cli(), but from now on other, more lightweight methods
  7. have to be used for synchronization, such as spinlocks or semaphores.
  8. for example, driver code that used to do something like:
  9. struct driver_data;
  10. irq_handler (...)
  11. {
  12. ....
  13. driver_data.finish = 1;
  14. driver_data.new_work = 0;
  15. ....
  16. }
  17. ...
  18. ioctl_func (...)
  19. {
  20. ...
  21. cli();
  22. ...
  23. driver_data.finish = 0;
  24. driver_data.new_work = 2;
  25. ...
  26. sti();
  27. ...
  28. }
  29. was SMP-correct because the cli() function ensured that no
  30. interrupt handler (amongst them the above irq_handler()) function
  31. would execute while the cli()-ed section is executing.
  32. but from now on a more direct method of locking has to be used:
  33. spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
  34. struct driver_data;
  35. irq_handler (...)
  36. {
  37. unsigned long flags;
  38. ....
  39. spin_lock_irqsave(&driver_lock, flags);
  40. ....
  41. driver_data.finish = 1;
  42. driver_data.new_work = 0;
  43. ....
  44. spin_unlock_irqrestore(&driver_lock, flags);
  45. ....
  46. }
  47. ...
  48. ioctl_func (...)
  49. {
  50. ...
  51. spin_lock_irq(&driver_lock);
  52. ...
  53. driver_data.finish = 0;
  54. driver_data.new_work = 2;
  55. ...
  56. spin_unlock_irq(&driver_lock);
  57. ...
  58. }
  59. the above code has a number of advantages:
  60. - the locking relation is easier to understand - actual lock usage
  61. pinpoints the critical sections. cli() usage is too opaque.
  62. Easier to understand means it's easier to debug.
  63. - it's faster, because spinlocks are faster to acquire than the
  64. potentially heavily-used IRQ lock. Furthermore, your driver does
  65. not have to wait eg. for a big heavy SCSI interrupt to finish,
  66. because the driver_lock spinlock is only used by your driver.
  67. cli() on the other hand was used by many drivers, and extended
  68. the critical section to the whole IRQ handler function - creating
  69. serious lock contention.
  70. to make the transition easier, we've still kept the cli(), sti(),
  71. save_flags(), save_flags_cli() and restore_flags() macros defined
  72. on UP systems - but their usage will be phased out until 2.6 is
  73. released.
  74. drivers that want to disable local interrupts (interrupts on the
  75. current CPU), can use the following five macros:
  76. local_irq_disable(), local_irq_enable(), local_save_flags(flags),
  77. local_irq_save(flags), local_irq_restore(flags)
  78. but beware, their meaning and semantics are much simpler, far from
  79. that of the old cli(), sti(), save_flags(flags) and restore_flags(flags)
  80. SMP meaning:
  81. local_irq_disable() => turn local IRQs off
  82. local_irq_enable() => turn local IRQs on
  83. local_save_flags(flags) => save the current IRQ state into flags. The
  84. state can be on or off. (on some
  85. architectures there's even more bits in it.)
  86. local_irq_save(flags) => save the current IRQ state into flags and
  87. disable interrupts.
  88. local_irq_restore(flags) => restore the IRQ state from flags.
  89. (local_irq_save can save both irqs on and irqs off state, and
  90. local_irq_restore can restore into both irqs on and irqs off state.)
  91. another related change is that synchronize_irq() now takes a parameter:
  92. synchronize_irq(irq). This change too has the purpose of making SMP
  93. synchronization more lightweight - this way you can wait for your own
  94. interrupt handler to finish, no need to wait for other IRQ sources.
  95. why were these changes done? The main reason was the architectural burden
  96. of maintaining the cli()/sti() interface - it became a real problem. The
  97. new interrupt system is much more streamlined, easier to understand, debug,
  98. and it's also a bit faster - the same happened to it that will happen to
  99. cli()/sti() using drivers once they convert to spinlocks :-)