All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "FUJITA Tomonori" <tomo@flapping.org>, <gary@garyguo.net>
Cc: <anna-maria@linutronix.de>, <frederic@kernel.org>,
	<tglx@kernel.org>, <a.hindborg@kernel.org>, <ojeda@kernel.org>,
	<acourbot@nvidia.com>, <aliceryhl@google.com>,
	<bjorn3_gh@protonmail.com>, <boqun@kernel.org>, <dakr@kernel.org>,
	<daniel.almeida@collabora.com>, <jstultz@google.com>,
	<lossin@kernel.org>, <lyude@redhat.com>, <sboyd@kernel.org>,
	<tamird@kernel.org>, <tmgross@umich.edu>, <work@onurozkan.dev>,
	<rust-for-linux@vger.kernel.org>, <fujita.tomonori@gmail.com>
Subject: Re: [PATCH 0/4] Fix forward()/expires() racing with concurrent arming
Date: Fri, 14 Aug 2026 15:24:10 +0100	[thread overview]
Message-ID: <DKOQH5AD00IZ.2UHAO78NDFZZ6@garyguo.net> (raw)
In-Reply-To: <20260814.224838.67768463960169120.tomo@flapping.org>

On Fri Aug 14, 2026 at 2:48 PM BST, FUJITA Tomonori wrote:
> On Fri, 14 Aug 2026 01:54:25 +0100
> "Gary Guo" <gary@garyguo.net> wrote:
>> Let's ignore the implementation detail of all various Rust pointers. It is
>> something that I plan to overhaul and doesn't matter to the core issue here.
>> 
>> The change you're making is to remove the ability to concurrently start a timer
>> in Rust. So if you have a timer might be running, you'd need to first cancel it
>> before you can arm it again.
>> 
>> I do think it is conceptually cleaner -- however given this is explicitly added
>> in
>> https://lore.kernel.org/all/tip-5de2755c8c8b3a6b8414870e2c284914a2b42e4d@git.kernel.org/
>> and the pattern is what perf core uses; so I wouldn't just dismiss the existence
>> of this pattern. Perhaps cancelling before restarting is considered too
>> expensive and has to be avoided?
>
> The pattern perf core uses is "no arming while armed".
>
> While perf_mux_hrtimer_handler() returns HRTIMER_RESTART -- while the
> timer is active -- perf_mux_hrtimer_restart() does nothing. Only once
> the handler has cleared cpc->hrtimer_active and returned
> HRTIMER_NORESTART does perf_mux_hrtimer_restart() arm it again.
>
> That flag was added precisely to implement "no arming while armed", in
> 4cfafd3082af ("sched,perf: Fix periodic timers"):
>
>     We do not want to race such that the handler has already decided
>     to stop, but the (external) restart sees the timer still active and we
>     end up with a 'lost' timer.
>
>     The problem with the current code is that the re-start can come before
>     the callback does the forward, at which point the forward from the
>     callback will WARN about forwarding an enqueued timer.
>
>
> With cpc->hrtimer_active in place, neither of the two conditions that
> 5de2755c8c8b touches is reachable in perf's usage.

So are we okay saying that concurrent restart is problematic because apparently
it cannot be used correctly (because you don't have a way to synchronize it)?
Perhaps we should just revert 5de2755c8c8b or at least do

    if (restart != HRTIMER_NORESTART) {
        WARN_ON(timer->state != HRTIMER_STATE_CALLBACK);
        if (!(timer->state & HRTIMER_STATE_ENQUEUED))
            enqueue_hrtimer(timer, base);
    }

?

That said, the perf core's pattern is still different from the API that you're
designing. When perf_mux_hrtimer_handler unlocks cpc->hrtimer_lock at that point
perf_mux_hrtimer_restart can already kick in and restart the timer. From hrtimer
core's perspective, it is starting a timer that is still have running callback
-- but that callback shall only return NORESTART.

Best,
Gary

>
> v1 is missing the ability to restart a stopped timer: once the
> callback has returned NoRestart, the handle owns the right to arm and
> never gives it back. I'll add it in v2, including a non-blocking
> variant built on hrtimer_try_to_cancel(), so that a caller which
> cannot sleep can re-arm the way perf does. That makes perf's model
> expressible in the Rust abstraction.



  reply	other threads:[~2026-08-14 14:24 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-13 13:48 [PATCH 0/4] Fix forward()/expires() racing with concurrent arming FUJITA Tomonori
2026-08-13 13:48 ` [PATCH v1 1/4] rust: hrtimer: Introduce HrTimerArc to make arming exclusive FUJITA Tomonori
2026-08-13 13:48 ` [PATCH v1 2/4] rust: hrtimer: Introduce HrTimerPin " FUJITA Tomonori
2026-08-13 13:48 ` [PATCH v1 3/4] rust: hrtimer: Restrict expires() to safe contexts FUJITA Tomonori
2026-08-13 13:48 ` [PATCH v1 4/4] rust: hrtimer: Make HrTimer repr(transparent) FUJITA Tomonori
2026-08-13 14:16 ` [PATCH 0/4] Fix forward()/expires() racing with concurrent arming Gary Guo
2026-08-13 23:47   ` FUJITA Tomonori
2026-08-14  0:54     ` Gary Guo
2026-08-14 13:48       ` FUJITA Tomonori
2026-08-14 14:24         ` Gary Guo [this message]
2026-08-18  0:56           ` FUJITA Tomonori
2026-08-17 15:40         ` Gary Guo
2026-08-18  1:35           ` FUJITA Tomonori
2026-08-17 15:26   ` Andreas Hindborg
2026-08-18  2:26     ` FUJITA Tomonori
2026-08-18  9:02       ` Andreas Hindborg
2026-08-18 11:21         ` FUJITA Tomonori
2026-08-18 11:59           ` Andreas Hindborg
2026-08-19 13:01             ` FUJITA Tomonori
2026-08-20  9:00               ` Andreas Hindborg
2026-08-20  9:21                 ` FUJITA Tomonori
2026-08-20 12:34                   ` Andreas Hindborg
2026-08-20 12:53                     ` FUJITA Tomonori
2026-08-21  7:13                       ` Andreas Hindborg
2026-08-21  9:53                         ` FUJITA Tomonori
2026-08-24 10:44                           ` Andreas Hindborg
2026-08-24 10:59                             ` Miguel Ojeda
2026-08-24 11:07                             ` FUJITA Tomonori

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=DKOQH5AD00IZ.2UHAO78NDFZZ6@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=anna-maria@linutronix.de \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=frederic@kernel.org \
    --cc=fujita.tomonori@gmail.com \
    --cc=jstultz@google.com \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tamird@kernel.org \
    --cc=tglx@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=tomo@flapping.org \
    --cc=work@onurozkan.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.