mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ankur Arora <ankur.a.arora@oracle.com>
To: linux-pm@vger.kernel.org, kvm@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org
Cc: catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de,
	mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com,
	x86@kernel.org, hpa@zytor.com, 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: [PATCH v9 01/15] asm-generic: add barrier smp_cond_load_relaxed_timeout()
Date: Thu,  7 Nov 2024 11:08:04 -0800	[thread overview]
Message-ID: <20241107190818.522639-2-ankur.a.arora@oracle.com> (raw)
In-Reply-To: <20241107190818.522639-1-ankur.a.arora@oracle.com>

Add a timed variant of smp_cond_load_relaxed().

This is useful because arm64 supports polling on a conditional variable
by directly waiting on the cacheline instead of spin waiting for the
condition to change.

However, an implementation such as this has a problem that it can block
forever -- unless there's an explicit timeout or another out-of-band
mechanism which allows it to come out of the wait state periodically.

smp_cond_load_relaxed_timeout() supports these semantics by specifying
a time-check expression and an associated time-limit.

However, note that for the generic spin-wait implementation we want to
minimize the numbers of instructions executed in each iteration. So,
limit how often we evaluate the time-check expression by doing it once
every smp_cond_time_check_count.

The inner loop in poll_idle() has a substantially similar structure
and constraints as smp_cond_load_relaxed_timeout(), so define
smp_cond_time_check_count to the same value used in poll_idle().

Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
---
 include/asm-generic/barrier.h | 42 +++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/include/asm-generic/barrier.h b/include/asm-generic/barrier.h
index d4f581c1e21d..77726ef807e4 100644
--- a/include/asm-generic/barrier.h
+++ b/include/asm-generic/barrier.h
@@ -273,6 +273,48 @@ do {									\
 })
 #endif
 
+#ifndef smp_cond_time_check_count
+/*
+ * Limit how often smp_cond_load_relaxed_timeout() evaluates time_expr_ns.
+ * This helps reduce the number of instructions executed while spin-waiting.
+ */
+#define smp_cond_time_check_count	200
+#endif
+
+/**
+ * smp_cond_load_relaxed_timeout() - (Spin) wait for cond with no ordering
+ * guarantees until a timeout expires.
+ * @ptr: pointer to the variable to wait on
+ * @cond: boolean expression to wait for
+ * @time_expr_ns: evaluates to the current time
+ * @time_limit_ns: compared against time_expr_ns
+ *
+ * Equivalent to using READ_ONCE() on the condition variable.
+ *
+ * Due to C lacking lambda expressions we load the value of *ptr into a
+ * pre-named variable @VAL to be used in @cond.
+ */
+#ifndef smp_cond_load_relaxed_timeout
+#define smp_cond_load_relaxed_timeout(ptr, cond_expr, time_expr_ns,	\
+				      time_limit_ns) ({			\
+	typeof(ptr) __PTR = (ptr);					\
+	__unqual_scalar_typeof(*ptr) VAL;				\
+	unsigned int __count = 0;					\
+	for (;;) {							\
+		VAL = READ_ONCE(*__PTR);				\
+		if (cond_expr)						\
+			break;						\
+		cpu_relax();						\
+		if (__count++ < smp_cond_time_check_count)		\
+			continue;					\
+		if ((time_expr_ns) >= time_limit_ns)			\
+			break;						\
+		__count = 0;						\
+	}								\
+	(typeof(*ptr))VAL;						\
+})
+#endif
+
 /*
  * pmem_wmb() ensures that all stores for which the modification
  * are written to persistent storage by preceding instructions have
-- 
2.43.5


  reply	other threads:[~2024-11-07 19:09 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-07 19:08 [PATCH v9 00/15] arm64: support poll_idle() Ankur Arora
2024-11-07 19:08 ` Ankur Arora [this message]
2024-11-08  2:33   ` [PATCH v9 01/15] asm-generic: add barrier smp_cond_load_relaxed_timeout() Christoph Lameter (Ampere)
2024-11-08  7:53     ` Ankur Arora
2024-11-08 19:41       ` Christoph Lameter (Ampere)
2024-11-08 22:15         ` Ankur Arora
2024-11-12 16:50           ` Christoph Lameter (Ampere)
2024-11-14 17:22         ` Catalin Marinas
2024-11-15  0:28           ` Ankur Arora
2024-11-26  5:01   ` Ankur Arora
2024-11-26 10:36     ` Catalin Marinas
2024-11-07 19:08 ` [PATCH v9 02/15] cpuidle/poll_state: poll via smp_cond_load_relaxed_timeout() Ankur Arora
2024-11-07 19:08 ` [PATCH v9 03/15] cpuidle: rename ARCH_HAS_CPU_RELAX to ARCH_HAS_OPTIMIZED_POLL Ankur Arora
2024-11-07 19:08 ` [PATCH v9 04/15] Kconfig: move ARCH_HAS_OPTIMIZED_POLL to arch/Kconfig Ankur Arora
2024-11-07 19:08 ` [PATCH v9 05/15] arm64: barrier: add support for smp_cond_relaxed_timeout() Ankur Arora
2024-12-10 13:50   ` Will Deacon
2024-12-10 20:14     ` Ankur Arora
2024-11-07 19:08 ` [PATCH v9 06/15] arm64: define TIF_POLLING_NRFLAG Ankur Arora
2024-11-07 19:08 ` [PATCH v9 07/15] arm64: add support for polling in idle Ankur Arora
2024-11-07 19:08 ` [PATCH v9 08/15] ACPI: processor_idle: Support polling state for LPI Ankur Arora
2024-11-07 19:08 ` [PATCH v9 09/15] cpuidle-haltpoll: define arch_haltpoll_want() Ankur Arora
2024-11-07 19:08 ` [PATCH v9 10/15] governors/haltpoll: drop kvm_para_available() check Ankur Arora
2024-11-07 19:08 ` [PATCH v9 11/15] cpuidle-haltpoll: condition on ARCH_CPUIDLE_HALTPOLL Ankur Arora
2024-11-07 19:08 ` [PATCH v9 12/15] arm64: idle: export arch_cpu_idle Ankur Arora
2024-11-07 19:08 ` [PATCH v9 13/15] arm64: support cpuidle-haltpoll Ankur Arora
2024-11-07 19:08 ` [RFC PATCH v9 14/15] arm64/delay: move some constants out to a separate header Ankur Arora
2024-11-08  2:25   ` Christoph Lameter (Ampere)
2024-11-08  7:49     ` Ankur Arora
2024-11-07 19:08 ` [RFC PATCH v9 15/15] arm64: support WFET in smp_cond_relaxed_timeout() Ankur Arora
2025-01-07  5:23 ` [PATCH v9 00/15] arm64: support poll_idle() Ankur Arora
2025-01-20 21:13 ` Ankur Arora
2025-01-21  9:55   ` Will Deacon

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=20241107190818.522639-2-ankur.a.arora@oracle.com \
    --to=ankur.a.arora@oracle.com \
    --cc=arnd@arndb.de \
    --cc=boris.ostrovsky@oracle.com \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=cl@gentwo.org \
    --cc=daniel.lezcano@linaro.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=harisokn@amazon.com \
    --cc=hpa@zytor.com \
    --cc=joao.m.martins@oracle.com \
    --cc=konrad.wilk@oracle.com \
    --cc=kvm@vger.kernel.org \
    --cc=lenb@kernel.org \
    --cc=linux-arch@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=mingo@redhat.com \
    --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=tglx@linutronix.de \
    --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

Powered by JetHome