]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
iopoll: Avoid evaluating 'cond' twice in poll_timeout_us()
authorVille Syrjälä <ville.syrjala@linux.intel.com>
Tue, 26 Aug 2025 12:18:58 +0000 (15:18 +0300)
committerJani Nikula <jani.nikula@intel.com>
Thu, 28 Aug 2025 09:17:32 +0000 (12:17 +0300)
Currently poll_timeout_us() evaluates 'cond' twice at the end
of the success case. This not desirable in case 'cond' itself
is expensive.

Avoid the double evaluation by tracking the return value in
a variable. Need to use a triple undescore '___ret' name to
avoid a conflict with an existing double undescore '__ret'
variable in the regmap code.

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-2-ville.syrjala@linux.intel.com
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
include/linux/iopoll.h

index 440aca5b4b59f4405c0bcdeffda15a828c7559e5..d8c801ad68fa6bbbd0ffe8f02b71c5ff0c3f46d2 100644 (file)
        u64 __timeout_us = (timeout_us); \
        unsigned long __sleep_us = (sleep_us); \
        ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
+       int ___ret; \
        might_sleep_if((__sleep_us) != 0); \
        if ((sleep_before_op) && __sleep_us) \
                usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
        for (;;) { \
                op; \
-               if (cond) \
+               if (cond) { \
+                       ___ret = 0; \
                        break; \
+               } \
                if (__timeout_us && \
                    ktime_compare(ktime_get(), __timeout) > 0) { \
                        op; \
+                       if (cond) \
+                               ___ret = 0; \
+                       else \
+                               ___ret = -ETIMEDOUT; \
                        break; \
                } \
                if (__sleep_us) \
                        usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
                cpu_relax(); \
        } \
-       (cond) ? 0 : -ETIMEDOUT; \
+       ___ret; \
 })
 
 /**
@@ -83,6 +90,7 @@
        s64 __left_ns = __timeout_us * NSEC_PER_USEC; \
        unsigned long __delay_us = (delay_us); \
        u64 __delay_ns = __delay_us * NSEC_PER_USEC; \
+       int ___ret; \
        if ((delay_before_op) && __delay_us) { \
                udelay(__delay_us); \
                if (__timeout_us) \
        } \
        for (;;) { \
                op; \
-               if (cond) \
+               if (cond) { \
+                       ___ret = 0; \
                        break; \
+               } \
                if (__timeout_us && __left_ns < 0) { \
                        op; \
+                       if (cond) \
+                               ___ret = 0; \
+                       else \
+                               ___ret = -ETIMEDOUT; \
                        break; \
                } \
                if (__delay_us) { \
                if (__timeout_us) \
                        __left_ns--; \
        } \
-       (cond) ? 0 : -ETIMEDOUT; \
+       ___ret; \
 })
 
 /**