From: Jani Nikula <jani.nikula@intel.com>
To: Ville Syrjala <ville.syrjala@linux.intel.com>,
linux-kernel@vger.kernel.org
Cc: "Lucas De Marchi" <lucas.demarchi@intel.com>,
"Dibin Moolakadan Subrahmanian"
<dibin.moolakadan.subrahmanian@intel.com>,
"Imre Deak" <imre.deak@intel.com>,
"David Laight" <david.laight.linux@gmail.com>,
"Geert Uytterhoeven" <geert+renesas@glider.be>,
"Matt Wagantall" <mattw@codeaurora.org>,
"Dejin Zheng" <zhengdejin5@gmail.com>,
intel-gfx@lists.freedesktop.org, intel-xe@lists.freedesktop.org,
"Ville Syrjälä" <ville.syrjala@linux.intel.com>
Subject: Re: [PATCH 2/4] iopoll: Avoid evaluating 'cond' twice in poll_timeout_us()
Date: Thu, 03 Jul 2025 14:55:24 +0300 [thread overview]
Message-ID: <514069318ce4ae0746af507aa24321ddfe4dc399@intel.com> (raw)
In-Reply-To: <20250702223439.19752-2-ville.syrjala@linux.intel.com>
On Thu, 03 Jul 2025, Ville Syrjala <ville.syrjala@linux.intel.com> wrote:
> From: Ville Syrjälä <ville.syrjala@linux.intel.com>
>
> 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: Jani Nikula <jani.nikula@intel.com>
> 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
> Signed-off-by: Ville Syrjälä <ville.syrjala@linux.intel.com>
Reviewed-by: Jani Nikula <jani.nikula@intel.com>
> ---
> include/linux/iopoll.h | 22 ++++++++++++++++++----
> 1 file changed, 18 insertions(+), 4 deletions(-)
>
> diff --git a/include/linux/iopoll.h b/include/linux/iopoll.h
> index 0d8186d3df03..69296e6adbf3 100644
> --- a/include/linux/iopoll.h
> +++ b/include/linux/iopoll.h
> @@ -36,23 +36,30 @@
> 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) \
> @@ -90,10 +98,16 @@
> } \
> 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) { \
> @@ -105,7 +119,7 @@
> if (__timeout_us) \
> __left_ns--; \
> } \
> - (cond) ? 0 : -ETIMEDOUT; \
> + ___ret; \
> })
>
> /**
--
Jani Nikula, Intel
next prev parent reply other threads:[~2025-07-03 11:55 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-02 22:34 [PATCH 1/4] iopoll: Generalize read_poll_timeout() into poll_timeout_us() Ville Syrjala
2025-07-02 22:34 ` [PATCH 2/4] iopoll: Avoid evaluating 'cond' twice in poll_timeout_us() Ville Syrjala
2025-07-03 11:55 ` Jani Nikula [this message]
2025-07-02 22:34 ` [PATCH 3/4] iopoll: Reorder the timeout handling " Ville Syrjala
2025-07-03 12:00 ` Jani Nikula
2025-07-02 22:34 ` [PATCH 4/4] DO-NOT-MERGE: drm/i915: Use poll_timeout_us() Ville Syrjala
2025-07-03 12:12 ` Jani Nikula
2025-07-03 12:50 ` Ville Syrjälä
2025-07-03 11:51 ` [PATCH 1/4] iopoll: Generalize read_poll_timeout() into poll_timeout_us() Jani Nikula
2025-07-03 14:28 ` Lucas De Marchi
2025-07-04 8:40 ` Jani Nikula
2025-07-08 13:16 ` [PATCH v2 " Ville Syrjala
2025-07-15 18:20 ` Ville Syrjälä
2025-07-31 8:51 ` Jani Nikula
2025-08-26 10:56 ` Jani Nikula
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=514069318ce4ae0746af507aa24321ddfe4dc399@intel.com \
--to=jani.nikula@intel.com \
--cc=david.laight.linux@gmail.com \
--cc=dibin.moolakadan.subrahmanian@intel.com \
--cc=geert+renesas@glider.be \
--cc=imre.deak@intel.com \
--cc=intel-gfx@lists.freedesktop.org \
--cc=intel-xe@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lucas.demarchi@intel.com \
--cc=mattw@codeaurora.org \
--cc=ville.syrjala@linux.intel.com \
--cc=zhengdejin5@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®