Skip to content

Commit f267be7

Browse files
committed
Merge tag 'iopoll-busy-loop-timeout-tag' into renesas-clk-for-v6.5
iopoll: Busy loop and timeout improvements
2 parents d1c2088 + 7349a69 commit f267be7

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

include/linux/iopoll.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
} \
5454
if (__sleep_us) \
5555
usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
56+
cpu_relax(); \
5657
} \
5758
(cond) ? 0 : -ETIMEDOUT; \
5859
})
@@ -73,28 +74,41 @@
7374
* Returns 0 on success and -ETIMEDOUT upon a timeout. In either
7475
* case, the last read value at @args is stored in @val.
7576
*
77+
* This macro does not rely on timekeeping. Hence it is safe to call even when
78+
* timekeeping is suspended, at the expense of an underestimation of wall clock
79+
* time, which is rather minimal with a non-zero delay_us.
80+
*
7681
* When available, you'll probably want to use one of the specialized
7782
* macros defined below rather than this macro directly.
7883
*/
7984
#define read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, \
8085
delay_before_read, args...) \
8186
({ \
8287
u64 __timeout_us = (timeout_us); \
88+
s64 __left_ns = __timeout_us * NSEC_PER_USEC; \
8389
unsigned long __delay_us = (delay_us); \
84-
ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
85-
if (delay_before_read && __delay_us) \
90+
u64 __delay_ns = __delay_us * NSEC_PER_USEC; \
91+
if (delay_before_read && __delay_us) { \
8692
udelay(__delay_us); \
93+
if (__timeout_us) \
94+
__left_ns -= __delay_ns; \
95+
} \
8796
for (;;) { \
8897
(val) = op(args); \
8998
if (cond) \
9099
break; \
91-
if (__timeout_us && \
92-
ktime_compare(ktime_get(), __timeout) > 0) { \
100+
if (__timeout_us && __left_ns < 0) { \
93101
(val) = op(args); \
94102
break; \
95103
} \
96-
if (__delay_us) \
104+
if (__delay_us) { \
97105
udelay(__delay_us); \
106+
if (__timeout_us) \
107+
__left_ns -= __delay_ns; \
108+
} \
109+
cpu_relax(); \
110+
if (__timeout_us) \
111+
__left_ns--; \
98112
} \
99113
(cond) ? 0 : -ETIMEDOUT; \
100114
})

0 commit comments

Comments
 (0)