From: Ankur Arora <ankur.a.arora@oracle.com>
To: Ankur Arora <ankur.a.arora@oracle.com>
Cc: linux-pm@vger.kernel.org, kvm@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org,
catalin.marinas@arm.com, will@kernel.org, x86@kernel.org,
pbonzini@redhat.com, vkuznets@redhat.com, rafael@kernel.org,
daniel.lezcano@linaro.org, peterz@infradead.org, arnd@arndb.de,
lenb@kernel.org, mark.rutland@arm.com, harisokn@amazon.com,
mtosatti@redhat.com, sudeep.holla@arm.com, cl@gentwo.org,
maz@kernel.org, misono.tomohiro@fujitsu.com, maobibo@loongson.cn,
zhenglifeng1@huawei.com, joao.m.martins@oracle.com,
boris.ostrovsky@oracle.com, konrad.wilk@oracle.com
Subject: Re: [PATCH v10 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed_timewait()
Date: Mon, 12 May 2025 22:29:28 -0700 [thread overview]
Message-ID: <87ikm5jcxz.fsf@oracle.com> (raw)
In-Reply-To: <20250218213337.377987-2-ankur.a.arora@oracle.com>
Ankur Arora <ankur.a.arora@oracle.com> writes:
> The inner loop in poll_idle() polls to see if the thread's
> TIF_NEED_RESCHED bit is set. The loop exits once the condition is met,
> or if the poll time limit has been exceeded.
>
> To minimize the number of instructions executed in each iteration, the
> time check is rate-limited. In addition, each loop iteration executes
> cpu_relax() which on certain platforms provides a hint to the pipeline
> that the loop is busy-waiting, which allows the processor to reduce
> power consumption.
>
> However, cpu_relax() is defined optimally only on x86. On arm64, for
> instance, it is implemented as a YIELD which only serves as a hint
> to the CPU that it prioritize a different hardware thread if one is
> available. arm64, does expose a more optimal polling mechanism via
> smp_cond_load_relaxed_timewait() which uses LDXR, WFE to wait until a
> store to a specified region, or until a timeout.
>
> These semantics are essentially identical to what we want
> from poll_idle(). So, restructure the loop to use
> smp_cond_load_relaxed_timewait() instead.
>
> The generated code remains close to the original version.
>
> Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
> Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
> ---
> drivers/cpuidle/poll_state.c | 27 ++++++++-------------------
> 1 file changed, 8 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/cpuidle/poll_state.c b/drivers/cpuidle/poll_state.c
> index 9b6d90a72601..5117d3d37036 100644
> --- a/drivers/cpuidle/poll_state.c
> +++ b/drivers/cpuidle/poll_state.c
> @@ -8,35 +8,24 @@
> #include <linux/sched/clock.h>
> #include <linux/sched/idle.h>
>
> -#define POLL_IDLE_RELAX_COUNT 200
> -
> static int __cpuidle poll_idle(struct cpuidle_device *dev,
> struct cpuidle_driver *drv, int index)
> {
> - u64 time_start;
> -
> - time_start = local_clock_noinstr();
>
> dev->poll_time_limit = false;
>
> raw_local_irq_enable();
> if (!current_set_polling_and_test()) {
> - unsigned int loop_count = 0;
> - u64 limit;
> + unsigned long flags;
> + u64 time_start = local_clock_noinstr();
> + u64 limit = cpuidle_poll_time(drv, dev);
>
> - limit = cpuidle_poll_time(drv, dev);
> + flags = smp_cond_load_relaxed_timewait(¤t_thread_info()->flags,
> + VAL & _TIF_NEED_RESCHED,
> + local_clock_noinstr(),
> + time_start + limit);
>
> - while (!need_resched()) {
> - cpu_relax();
> - if (loop_count++ < POLL_IDLE_RELAX_COUNT)
> - continue;
> -
> - loop_count = 0;
> - if (local_clock_noinstr() - time_start > limit) {
> - dev->poll_time_limit = true;
> - break;
> - }
> - }
> + dev->poll_time_limit = !(flags & _TIF_NEED_RESCHED);
> }
> raw_local_irq_disable();
The barrier-v2 [1] interface is slightly different from the one proposed
in v1 (which this series is based on.)
[1] https://lore.kernel.org/lkml/20250502085223.1316925-1-ankur.a.arora@oracle.com/
For testing please use the following patch. It adds a new parameter
(__smp_cond_timewait_coarse) explicitly specifying the waiting policy.
--
diff --git a/drivers/cpuidle/poll_state.c b/drivers/cpuidle/poll_state.c
index 9b6d90a72601..2970368663c7 100644
--- a/drivers/cpuidle/poll_state.c
+++ b/drivers/cpuidle/poll_state.c
@@ -8,35 +8,25 @@
#include <linux/sched/clock.h>
#include <linux/sched/idle.h>
-#define POLL_IDLE_RELAX_COUNT 200
-
static int __cpuidle poll_idle(struct cpuidle_device *dev,
struct cpuidle_driver *drv, int index)
{
- u64 time_start;
-
- time_start = local_clock_noinstr();
dev->poll_time_limit = false;
raw_local_irq_enable();
if (!current_set_polling_and_test()) {
- unsigned int loop_count = 0;
- u64 limit;
+ unsigned long flags;
+ u64 time_start = local_clock_noinstr();
+ u64 limit = cpuidle_poll_time(drv, dev);
- limit = cpuidle_poll_time(drv, dev);
+ flags = smp_cond_load_relaxed_timewait(¤t_thread_info()->flags,
+ VAL & _TIF_NEED_RESCHED,
+ __smp_cond_timewait_coarse,
+ local_clock_noinstr(),
+ time_start + limit);
- while (!need_resched()) {
- cpu_relax();
- if (loop_count++ < POLL_IDLE_RELAX_COUNT)
- continue;
-
- loop_count = 0;
- if (local_clock_noinstr() - time_start > limit) {
- dev->poll_time_limit = true;
- break;
- }
- }
+ dev->poll_time_limit = !(flags & _TIF_NEED_RESCHED);
}
raw_local_irq_disable();
--
ankur
next prev parent reply other threads:[~2025-05-13 5:32 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-18 21:33 [PATCH v10 00/11] arm64: support poll_idle() Ankur Arora
2025-02-18 21:33 ` [PATCH v10 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed_timewait() Ankur Arora
2025-05-13 5:29 ` Ankur Arora [this message]
2025-02-18 21:33 ` [PATCH v10 02/11] cpuidle: rename ARCH_HAS_CPU_RELAX to ARCH_HAS_OPTIMIZED_POLL Ankur Arora
2025-02-18 21:33 ` [PATCH v10 03/11] Kconfig: move ARCH_HAS_OPTIMIZED_POLL to arch/Kconfig Ankur Arora
2025-02-18 21:33 ` [PATCH v10 04/11] arm64: define TIF_POLLING_NRFLAG Ankur Arora
2025-02-18 21:33 ` [PATCH v10 05/11] arm64: add support for poll_idle() Ankur Arora
2025-02-18 21:33 ` [PATCH v10 06/11] ACPI: processor_idle: Support polling state for LPI Ankur Arora
2025-02-18 21:33 ` [PATCH v10 07/11] cpuidle-haltpoll: define arch_haltpoll_want() Ankur Arora
2025-02-18 21:33 ` [PATCH v10 08/11] governors/haltpoll: drop kvm_para_available() check Ankur Arora
2025-02-24 16:57 ` Christoph Lameter (Ampere)
2025-02-25 19:06 ` Ankur Arora
2025-02-18 21:33 ` [PATCH v10 09/11] cpuidle-haltpoll: condition on ARCH_CPUIDLE_HALTPOLL Ankur Arora
2025-02-18 21:33 ` [PATCH v10 10/11] arm64: idle: export arch_cpu_idle() Ankur Arora
2025-04-11 3:32 ` Shuai Xue
2025-04-11 17:42 ` Okanovic, Haris
2025-04-11 20:57 ` Ankur Arora
2025-04-14 2:01 ` Shuai Xue
2025-04-14 3:46 ` Ankur Arora
2025-04-14 7:43 ` Shuai Xue
2025-04-15 6:24 ` Ankur Arora
2025-02-18 21:33 ` [PATCH v10 11/11] arm64: support cpuidle-haltpoll Ankur Arora
2025-05-13 5:23 ` [PATCH v10 00/11] arm64: support poll_idle() Ankur Arora
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=87ikm5jcxz.fsf@oracle.com \
--to=ankur.a.arora@oracle.com \
--cc=arnd@arndb.de \
--cc=boris.ostrovsky@oracle.com \
--cc=catalin.marinas@arm.com \
--cc=cl@gentwo.org \
--cc=daniel.lezcano@linaro.org \
--cc=harisokn@amazon.com \
--cc=joao.m.martins@oracle.com \
--cc=konrad.wilk@oracle.com \
--cc=kvm@vger.kernel.org \
--cc=lenb@kernel.org \
--cc=linux-acpi@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=maobibo@loongson.cn \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=misono.tomohiro@fujitsu.com \
--cc=mtosatti@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=rafael@kernel.org \
--cc=sudeep.holla@arm.com \
--cc=vkuznets@redhat.com \
--cc=will@kernel.org \
--cc=x86@kernel.org \
--cc=zhenglifeng1@huawei.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;
as well as URLs for NNTP newsgroup(s).