From: Ankur Arora <ankur.a.arora@oracle.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Ankur Arora <ankur.a.arora@oracle.com>,
LKML <linux-kernel@vger.kernel.org>,
linux-arch <linux-arch@vger.kernel.org>,
linux-arm-kernel <linux-arm-kernel@lists.infradead.org>,
Linux Power Management <linux-pm@vger.kernel.org>,
bpf <bpf@vger.kernel.org>, Arnd Bergmann <arnd@arndb.de>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Andrew Morton <akpm@linux-foundation.org>,
Mark Rutland <mark.rutland@arm.com>,
harisokn@amazon.com, Christoph Lameter <cl@gentwo.org>,
Alexei Starovoitov <ast@kernel.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Daniel Lezcano <daniel.lezcano@linaro.org>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
zhenglifeng1@huawei.com, xueshuai@linux.alibaba.com,
joao.m.martins@oracle.com,
Boris Ostrovsky <boris.ostrovsky@oracle.com>,
konrad.wilk@oracle.com
Subject: Re: [PATCH v8 10/12] bpf/rqspinlock: Use smp_cond_load_acquire_timeout()
Date: Mon, 15 Dec 2025 23:34:07 -0800 [thread overview]
Message-ID: <874ipqc234.fsf@oracle.com> (raw)
In-Reply-To: <CAADnVQKYoE85HFAOE5OBFpKbXej=h12m4DVvHuPViJSjAncK4A@mail.gmail.com>
Alexei Starovoitov <alexei.starovoitov@gmail.com> writes:
> On Sun, Dec 14, 2025 at 8:51 PM Ankur Arora <ankur.a.arora@oracle.com> wrote:
>>
>> /**
>> * resilient_queued_spin_lock_slowpath - acquire the queued spinlock
>> * @lock: Pointer to queued spinlock structure
>> @@ -415,7 +415,9 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
>> */
>> if (val & _Q_LOCKED_MASK) {
>> RES_RESET_TIMEOUT(ts, RES_DEF_TIMEOUT);
>> - res_smp_cond_load_acquire(&lock->locked, !VAL || RES_CHECK_TIMEOUT(ts, timeout_err, _Q_LOCKED_MASK) < 0);
>> + smp_cond_load_acquire_timeout(&lock->locked, !VAL,
>> + (timeout_err = clock_deadlock(lock, _Q_LOCKED_MASK, &ts)),
>> + ts.duration);
>
> I'm pretty sure we already discussed this and pointed out that
> this approach is not acceptable.
Where? I don't see any mail on this at all.
In any case your technical point below is quite reasonable. It's better
to lead with that instead of peremptorily declaring what you find
acceptable and what not.
> We cannot call ktime_get_mono_fast_ns() first.
> That's why RES_CHECK_TIMEOUT() exists and it does
> if (!(ts).spin++)
> before doing the first check_timeout() that will do ktime_get_mono_fast_ns().
> Above is a performance critical lock acquisition path where
> pending is spinning on the lock word waiting for the owner to
> release the lock.
> Adding unconditional ktime_get_mono_fast_ns() will destroy
> performance for quick critical section.
Yes that makes sense.
This is not ideal, but how about something like this:
#define smp_cond_load_relaxed_timeout(ptr, cond_expr, \
time_expr_ns, timeout_ns) \
({ \
typeof(ptr) __PTR = (ptr); \
__unqual_scalar_typeof(*ptr) VAL; \
u32 __n = 0, __spin = SMP_TIMEOUT_POLL_COUNT; \
s64 __timeout = (s64)timeout_ns; \
s64 __time_now, __time_end = 0; \
\
for (;;) { \
VAL = READ_ONCE(*__PTR); \
if (cond_expr) \
break; \
cpu_poll_relax(__PTR, VAL, __timeout); \
if (++__n < __spin) \
continue; \
__time_now = (s64)(time_expr_ns); \
if (unlikely(__time_end == 0)) \
__time_end = __time_now + __timeout; \
__timeout = __time_end - __time_now; \
if (__time_now <= 0 || __timeout <= 0) { \
VAL = READ_ONCE(*__PTR); \
break; \
} \
__n = 0; \
} \
\
(typeof(*ptr))VAL; \
})
The problem with this version is that there's no way to know how much
time has passed in the first cpu_poll_relax()). So for the waiting case
this has a builtin overshoot of up to __timeout for the WFET case.
And ~100us for WFE and ~2us for x86 cpu_relax.
Of course we could specify a small __timeout value for the first iteration
which would help WFET.
Anyway let me take another look at this tomorrow.
But, that is more complexity.
Opinions?
--
ankur
next prev parent reply other threads:[~2025-12-16 7:34 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-15 4:49 [PATCH v8 00/12] barrier: Add smp_cond_load_{relaxed,acquire}_timeout() Ankur Arora
2025-12-15 4:49 ` [PATCH v8 01/12] asm-generic: barrier: Add smp_cond_load_relaxed_timeout() Ankur Arora
2025-12-22 7:47 ` Ankur Arora
2025-12-15 4:49 ` [PATCH v8 02/12] arm64: barrier: Support smp_cond_load_relaxed_timeout() Ankur Arora
2025-12-15 4:49 ` [PATCH v8 03/12] arm64/delay: move some constants out to a separate header Ankur Arora
2025-12-15 17:59 ` kernel test robot
2025-12-19 4:52 ` kernel test robot
2025-12-15 4:49 ` [PATCH v8 04/12] arm64: support WFET in smp_cond_relaxed_timeout() Ankur Arora
2025-12-15 4:49 ` [PATCH v8 05/12] arm64: rqspinlock: Remove private copy of smp_cond_load_acquire_timewait() Ankur Arora
2025-12-15 5:14 ` bot+bpf-ci
2025-12-15 4:49 ` [PATCH v8 06/12] asm-generic: barrier: Add smp_cond_load_acquire_timeout() Ankur Arora
2025-12-15 4:49 ` [PATCH v8 07/12] atomic: Add atomic_cond_read_*_timeout() Ankur Arora
2025-12-15 4:49 ` [PATCH v8 08/12] locking/atomic: scripts: build atomic_long_cond_read_*_timeout() Ankur Arora
2025-12-15 4:49 ` [PATCH v8 09/12] bpf/rqspinlock: switch check_timeout() to a clock interface Ankur Arora
2025-12-15 4:49 ` [PATCH v8 10/12] bpf/rqspinlock: Use smp_cond_load_acquire_timeout() Ankur Arora
2025-12-15 21:40 ` Alexei Starovoitov
2025-12-16 7:34 ` Ankur Arora [this message]
2025-12-15 4:49 ` [PATCH v8 11/12] sched: add need-resched timed wait interface Ankur Arora
2025-12-15 4:49 ` [PATCH v8 12/12] cpuidle/poll_state: Wait for need-resched via tif_need_resched_relaxed_wait() Ankur Arora
2025-12-15 11:11 ` Rafael J. Wysocki
2025-12-16 6:55 ` Ankur Arora
2025-12-15 21:23 ` kernel test robot
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=874ipqc234.fsf@oracle.com \
--to=ankur.a.arora@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=alexei.starovoitov@gmail.com \
--cc=arnd@arndb.de \
--cc=ast@kernel.org \
--cc=boris.ostrovsky@oracle.com \
--cc=bpf@vger.kernel.org \
--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=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=mark.rutland@arm.com \
--cc=memxor@gmail.com \
--cc=peterz@infradead.org \
--cc=rafael@kernel.org \
--cc=will@kernel.org \
--cc=xueshuai@linux.alibaba.com \
--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).