[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <DFTGLWA024Q8.2J7FPJPVL6RA5@garyguo.net>
Date: Tue, 20 Jan 2026 13:43:38 +0000
From: "Gary Guo" <gary@...yguo.net>
To: "Andreas Hindborg" <a.hindborg@...nel.org>, "Gary Guo"
<gary@...yguo.net>, "FUJITA Tomonori" <fujita.tomonori@...il.com>,
<ojeda@...nel.org>
Cc: <aliceryhl@...gle.com>, <anna-maria@...utronix.de>,
<bjorn3_gh@...tonmail.com>, <boqun.feng@...il.com>, <dakr@...nel.org>,
<frederic@...nel.org>, <jstultz@...gle.com>, <lossin@...nel.org>,
<lyude@...hat.com>, <sboyd@...nel.org>, <tglx@...utronix.de>,
<tmgross@...ch.edu>, <linux-kernel@...r.kernel.org>,
<rust-for-linux@...r.kernel.org>
Subject: Re: [PATCH v1] rust: hrtimer: Restrict expires() to safe contexts
On Tue Jan 20, 2026 at 8:23 AM GMT, Andreas Hindborg wrote:
> "Gary Guo" <gary@...yguo.net> writes:
>>>>>
>>>>> Also, we got some new docs for `read_volatile` that allow us to read
>>>>> memory outside Rust of any allocation that are not "valid for read" [1],
>>>>> meaning racy reads are OK as far as I understand. So the original
>>>>> implementation might actually be OK, although the number might not be
>>>>> correct always.
>>>>
>>>> The wording is for MMIO and should not be relied on if the accessed memory is
>>>> C memory. Also, `HrTimer` is going to be a Rust allocation.
>>>
>>> Why do you think the wording is only valid for MMIO accesses?
>>
>> Because it is intent for this paragraph, and is mentioned in the text. This
>> specific text is introduced to allow hardware MMIO access and the need to be
>> able to access otherwise always-invalid pointers, such as pointers to 0 and end
>> of address space.
>
> The text does not say "only valid for MMIO locations", it says "intended
> for". It also would make no sense if it did, because MMIO locations are
> not special in that sense.
>
> <...>
>
> As long as we are not ever making a reference pointing within the
> allocation or doing non-volatile operations on it, it we should be able
> to consider the region as a non-rust allocation, right? If not, why not?
The important thing is that it is something that is never handled at all by
non-volatile means throughout the lifetime of the object. MMIO is speical in
this regard.
So for example, if you put it on the stack, then it is already within the
purview of the AM. You might be able to argue the otherwise if the thing is
created using pin_init and then you never initialize the memory in the first
place (which is not the case if you just use `Opauqe::uninit` by the way,
because there's a move, you'd need to use `ffi_init` and then do nothing in the
callback).
So yes, you can probably get what you want if this is behind a `Opaque` and you
never creates reference to the internal value or touch the memory in other
means. But this is all very sketchy, and I would just say "don't rely on this
behaviour unless you're doing MMIO". Use LKMM for anything else.
>
> I am aware that this does not apply for the expires field, I'm just
> asking for the sake of clarification.
>
>>
>>>
>>>>
>>>> Even if we don't treat it Rust allocation, it's also only "fine" in a sense that
>>>> you don't get UB for doing it. But the value you read can still be completely
>>>> meaningless if the updater is not atomic (it would be valid compiler
>>>> implementation to, say, turn a non-atomic write into a write of a garbage value
>>>> and then an overwrite of the actual data).
>>>
>>> This is exactly the behavior I expect. In case of concurrent writes, the
>>> value might be garbage, but in the absence of concurrent writes, the
>>> value will be correct.
>>>
>>>> I think the usage you quoted is just wrong, as on 32-bit platforms this could
>>>> well read a teared value.
>>>
>>> Referring to the C code above, it seems like what people in C land will
>>> do is read the value until it seems to be stable yolo ahead with that
>>> value.
>>
>> It is non-obvious to me on why this code is correct. The updater seems to do a
>> hrtimer_forward_now and then call into drivers to update the vblank count. So
>> in this sequence:
>>
>> CPU1 CPU2
>> first drm_crtc_vblank_count_and_time
>> hrtimer triggered
>> hrtimer_forward_now READ_ONCE(expires) <- racy read
>> second drm_crtc_vblank_count_and_time
>> store_vblank
>>
>> you will get a teared read of garbage value while the vblank count is stable.
>
> Right, that is interesting. And it will not help them to flip the
> operations on `expires` and `count`. I guess they need to update `count`
> twice to make it sound, effectively implementing a seqlock.
Alternatively just update `expires` field to be `atomic64_t` and use 64-bit
atomic ops always. But it then it means locks on 32-bit systems without 64-bit
atomics.
But anyway I think this explains why I would encourage to use LKMM rather than
just do a `read_volatile` (or READ_ONCE), because it is very unclear what
semantics you actually want.
Best,
Gary
Powered by blists - more mailing lists