Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

iopoll: Reorder the timeout handling in poll_timeout_us()

Currently poll_timeout_us() evaluates 'op' and 'cond' twice
within the loop, once at the start, and a second time after
the timeout check. While it's probably not a big deal to do
it twice almost back to back, it does make the macro a bit messy.

Simplify the implementation to evaluate the timeout at the
very start, then follow up with 'op'/'cond', and finally
check if the timeout did in fact happen or not.

For good measure throw in a compiler barrier between the timeout
and 'op'/'cond' evaluations to make sure the compiler can't reoder
the operations (which could cause false positive timeouts).
The similar i915 __wait_for() macro already has the barrier, though
there it is between the 'op' and 'cond' evaluations, which seems
like it could still allow 'op' and the timeout evaluations to get
reordered incorrectly. I suppose the ktime_get() might itself act
as a sufficient barrier here, but better safe than sorry I guess.

Cc: Lucas De Marchi <lucas.demarchi@intel.com>
Cc: Dibin Moolakadan Subrahmanian <dibin.moolakadan.subrahmanian@intel.com>
Cc: Imre Deak <imre.deak@intel.com>
Cc: David Laight <david.laight.linux@gmail.com>
Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Matt Wagantall <mattw@codeaurora.org>
Cc: Dejin Zheng <zhengdejin5@gmail.com>
Cc: intel-gfx@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: linux-kernel@vger.kernel.org
Reviewed-by: Jani Nikula <jani.nikula@intel.com>
Acked-by: Simona Vetter <simona.vetter@ffwll.ch>
Signed-off-by: Ville Syrjälä <ville.syrjala@linux.intel.com>
Link: https://lore.kernel.org/r/20250826121859.15497-3-ville.syrjala@linux.intel.com
Signed-off-by: Jani Nikula <jani.nikula@intel.com>

authored by

Ville Syrjälä and committed by
Jani Nikula
3b6f62b6 563e5eca

+11 -13
+11 -13
include/linux/iopoll.h
··· 41 41 if ((sleep_before_op) && __sleep_us) \ 42 42 usleep_range((__sleep_us >> 2) + 1, __sleep_us); \ 43 43 for (;;) { \ 44 + bool __expired = __timeout_us && \ 45 + ktime_compare(ktime_get(), __timeout) > 0; \ 46 + /* guarantee 'op' and 'cond' are evaluated after timeout expired */ \ 47 + barrier(); \ 44 48 op; \ 45 49 if (cond) { \ 46 50 ___ret = 0; \ 47 51 break; \ 48 52 } \ 49 - if (__timeout_us && \ 50 - ktime_compare(ktime_get(), __timeout) > 0) { \ 51 - op; \ 52 - if (cond) \ 53 - ___ret = 0; \ 54 - else \ 55 - ___ret = -ETIMEDOUT; \ 53 + if (__expired) { \ 54 + ___ret = -ETIMEDOUT; \ 56 55 break; \ 57 56 } \ 58 57 if (__sleep_us) \ ··· 96 97 __left_ns -= __delay_ns; \ 97 98 } \ 98 99 for (;;) { \ 100 + bool __expired = __timeout_us && __left_ns < 0; \ 101 + /* guarantee 'op' and 'cond' are evaluated after timeout expired */ \ 102 + barrier(); \ 99 103 op; \ 100 104 if (cond) { \ 101 105 ___ret = 0; \ 102 106 break; \ 103 107 } \ 104 - if (__timeout_us && __left_ns < 0) { \ 105 - op; \ 106 - if (cond) \ 107 - ___ret = 0; \ 108 - else \ 109 - ___ret = -ETIMEDOUT; \ 108 + if (__expired) { \ 109 + ___ret = -ETIMEDOUT; \ 110 110 break; \ 111 111 } \ 112 112 if (__delay_us) { \