1 ============================================== 2 delays - Information on the various kernel del 3 ============================================== 4 5 This document seeks to answer the common quest 6 RightWay (TM) to insert a delay?" 7 8 This question is most often faced by driver wr 9 deal with hardware delays and who may not be t 10 familiar with the inner workings of the Linux 11 12 13 Inserting Delays 14 ---------------- 15 16 The first, and most important, question you ne 17 code in an atomic context?" This should be fo 18 it really need to delay in atomic context?" If 19 20 ATOMIC CONTEXT: 21 You must use the `*delay` family of fu 22 functions use the jiffy estimation of 23 and will busy wait for enough loop cyc 24 the desired delay: 25 26 ndelay(unsigned long nsecs) 27 udelay(unsigned long usecs) 28 mdelay(unsigned long msecs) 29 30 udelay is the generally preferred API; 31 precision may not actually exist on ma 32 33 mdelay is macro wrapper around udelay, 34 possible overflow when passing large a 35 In general, use of mdelay is discourag 36 be refactored to allow for the use of 37 38 NON-ATOMIC CONTEXT: 39 You should use the `*sleep[_range]` fa 40 There are a few more options here, whi 41 work correctly, using the "right" slee 42 help the scheduler, power management, 43 driver better :) 44 45 -- Backed by busy-wait loop: 46 47 udelay(unsigned long usecs) 48 49 -- Backed by hrtimers: 50 51 usleep_range(unsigned long min 52 53 -- Backed by jiffies / legacy_timers 54 55 msleep(unsigned long msecs) 56 msleep_interruptible(unsigned 57 58 Unlike the `*delay` family, the underl 59 driving each of these calls varies, th 60 quirks you should be aware of. 61 62 63 SLEEPING FOR "A FEW" USECS ( < ~10us? 64 * Use udelay 65 66 - Why not usleep? 67 On slower systems, (em 68 stepped PC!) the overh 69 for usleep *may* not b 70 will obviously depend 71 it is something to be 72 73 SLEEPING FOR ~USECS OR SMALL MSECS ( 1 74 * Use usleep_range 75 76 - Why not msleep for (1ms - 20 77 Explained originally h 78 https://lore.k 79 80 msleep(1~20) may not d 81 will often sleep longe 82 value given in the 1~2 83 is not the desired beh 84 85 - Why is there no "usleep" / W 86 Since usleep_range is 87 wakeup will be very pr 88 usleep function would 89 of undesired interrupt 90 91 With the introduction 92 free to coalesce your 93 that may have happened 94 worst case, fire an in 95 96 The larger a range you 97 that you will not trig 98 be balanced with what 99 delay / performance fo 100 tolerances here are ve 101 is left to the caller 102 103 SLEEPING FOR LARGER MSECS ( 10ms+ ) 104 * Use msleep or possibly mslee 105 106 - What's the difference? 107 msleep sets the curren 108 whereas msleep_interru 109 TASK_INTERRUPTIBLE bef 110 short, the difference 111 early by a signal. In 112 you know you have a ne 113 114 FLEXIBLE SLEEPING (any delay, uninterr 115 * Use fsleep
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.