delay_sleep_functions.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. .. SPDX-License-Identifier: GPL-2.0
  2. Delay and sleep mechanisms
  3. ==========================
  4. This document seeks to answer the common question: "What is the
  5. RightWay (TM) to insert a delay?"
  6. This question is most often faced by driver writers who have to
  7. deal with hardware delays and who may not be the most intimately
  8. familiar with the inner workings of the Linux Kernel.
  9. The following table gives a rough overview about the existing function
  10. 'families' and their limitations. This overview table does not replace the
  11. reading of the function description before usage!
  12. .. list-table::
  13. :widths: 20 20 20 20 20
  14. :header-rows: 2
  15. * -
  16. - `*delay()`
  17. - `usleep_range*()`
  18. - `*sleep()`
  19. - `fsleep()`
  20. * -
  21. - busy-wait loop
  22. - hrtimers based
  23. - timer list timers based
  24. - combines the others
  25. * - Usage in atomic Context
  26. - yes
  27. - no
  28. - no
  29. - no
  30. * - precise on "short intervals"
  31. - yes
  32. - yes
  33. - depends
  34. - yes
  35. * - precise on "long intervals"
  36. - Do not use!
  37. - yes
  38. - max 12.5% slack
  39. - yes
  40. * - interruptible variant
  41. - no
  42. - yes
  43. - yes
  44. - no
  45. A generic advice for non atomic contexts could be:
  46. #. Use `fsleep()` whenever unsure (as it combines all the advantages of the
  47. others)
  48. #. Use `*sleep()` whenever possible
  49. #. Use `usleep_range*()` whenever accuracy of `*sleep()` is not sufficient
  50. #. Use `*delay()` for very, very short delays
  51. Find some more detailed information about the function 'families' in the next
  52. sections.
  53. `*delay()` family of functions
  54. ------------------------------
  55. These functions use the jiffy estimation of clock speed and will busy wait for
  56. enough loop cycles to achieve the desired delay. udelay() is the basic
  57. implementation and ndelay() as well as mdelay() are variants.
  58. These functions are mainly used to add a delay in atomic context. Please make
  59. sure to ask yourself before adding a delay in atomic context: Is this really
  60. required?
  61. .. kernel-doc:: include/asm-generic/delay.h
  62. :identifiers: udelay ndelay
  63. .. kernel-doc:: include/linux/delay.h
  64. :identifiers: mdelay
  65. `usleep_range*()` and `*sleep()` family of functions
  66. ----------------------------------------------------
  67. These functions use hrtimers or timer list timers to provide the requested
  68. sleeping duration. In order to decide which function is the right one to use,
  69. take some basic information into account:
  70. #. hrtimers are more expensive as they are using an rb-tree (instead of hashing)
  71. #. hrtimers are more expensive when the requested sleeping duration is the first
  72. timer which means real hardware has to be programmed
  73. #. timer list timers always provide some sort of slack as they are jiffy based
  74. The generic advice is repeated here:
  75. #. Use `fsleep()` whenever unsure (as it combines all the advantages of the
  76. others)
  77. #. Use `*sleep()` whenever possible
  78. #. Use `usleep_range*()` whenever accuracy of `*sleep()` is not sufficient
  79. First check fsleep() function description and to learn more about accuracy,
  80. please check msleep() function description.
  81. `usleep_range*()`
  82. ~~~~~~~~~~~~~~~~~
  83. .. kernel-doc:: include/linux/delay.h
  84. :identifiers: usleep_range usleep_range_idle
  85. .. kernel-doc:: kernel/time/sleep_timeout.c
  86. :identifiers: usleep_range_state
  87. `*sleep()`
  88. ~~~~~~~~~~
  89. .. kernel-doc:: kernel/time/sleep_timeout.c
  90. :identifiers: msleep msleep_interruptible
  91. .. kernel-doc:: include/linux/delay.h
  92. :identifiers: ssleep fsleep