Skip to content

Commit b407460

Browse files
committed
iopoll: Call cpu_relax() in busy loops
It is considered good practice to call cpu_relax() in busy loops, see Documentation/process/volatile-considered-harmful.rst. This can not only lower CPU power consumption or yield to a hyperthreaded twin processor, but also allows an architecture to mitigate hardware issues (e.g. ARM Erratum 754327 for Cortex-A9 prior to r2p0) in the architecture-specific cpu_relax() implementation. In addition, cpu_relax() is also a compiler barrier. It is not immediately obvious that the @op argument "function" will result in an actual function call (e.g. in case of inlining). Where a function call is a C sequence point, this is lost on inlining. Therefore, with agressive enough optimization it might be possible for the compiler to hoist the: (val) = op(args); "load" out of the loop because it doesn't see the value changing. The addition of cpu_relax() would inhibit this. As the iopoll helpers lack calls to cpu_relax(), people are sometimes reluctant to use them, and may fall back to open-coded polling loops (including cpu_relax() calls) instead. Fix this by adding calls to cpu_relax() to the iopoll helpers: - For the non-atomic case, it is sufficient to call cpu_relax() in case of a zero sleep-between-reads value, as a call to usleep_range() is a safe barrier otherwise. However, it doesn't hurt to add the call regardless, for simplicity, and for similarity with the atomic case below. - For the atomic case, cpu_relax() must be called regardless of the sleep-between-reads value, as there is no guarantee all architecture-specific implementations of udelay() handle this. Signed-off-by: Geert Uytterhoeven <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: Arnd Bergmann <[email protected]> Reviewed-by: Tony Lindgren <[email protected]> Reviewed-by: Ulf Hansson <[email protected]> Link: https://lore.kernel.org/r/45c87bec3397fdd704376807f0eec5cc71be440f.1685692810.git.geert+renesas@glider.be
1 parent ac9a786 commit b407460

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

include/linux/iopoll.h

Lines changed: 2 additions & 0 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
})
@@ -95,6 +96,7 @@
9596
} \
9697
if (__delay_us) \
9798
udelay(__delay_us); \
99+
cpu_relax(); \
98100
} \
99101
(cond) ? 0 : -ETIMEDOUT; \
100102
})

0 commit comments

Comments
 (0)