From: Boqun Feng <boqun.feng@gmail.com>
To: Andreas Hindborg <a.hindborg@kernel.org>
Cc: "Alice Ryhl" <aliceryhl@google.com>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Abdiel Janulgue" <abdiel.janulgue@gmail.com>,
dakr@kernel.org, robin.murphy@arm.com,
rust-for-linux@vger.kernel.org, "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Valentin Obst" <kernel@valentinobst.de>,
linux-kernel@vger.kernel.org, "Christoph Hellwig" <hch@lst.de>,
"Marek Szyprowski" <m.szyprowski@samsung.com>,
airlied@redhat.com, iommu@lists.linux.dev,
"Ralf Jung" <post@ralfj.de>, comex <comexk@gmail.com>,
lkmm@lists.linux.dev
Subject: Re: Allow data races on some read/write operations
Date: Mon, 3 Mar 2025 12:08:46 -0800 [thread overview]
Message-ID: <Z8YMTiKS4T9wC4t_@boqun-archlinux> (raw)
In-Reply-To: <87mse2hrd8.fsf@kernel.org>
On Mon, Mar 03, 2025 at 08:00:03PM +0100, Andreas Hindborg wrote:
>
> [New subject, was: Re: [PATCH v12 2/3] rust: add dma coherent allocator abstraction]
>
> "Alice Ryhl" <aliceryhl@google.com> writes:
>
> > On Mon, Mar 3, 2025 at 4:21 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
> >>
> >> "Alice Ryhl" <aliceryhl@google.com> writes:
> >>
> >> > On Mon, Mar 3, 2025 at 2:00 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
> >> >>
> >> >> "Daniel Almeida" <daniel.almeida@collabora.com> writes:
> >> >>
> >> >> > Hi Benno,
> >> >> >
> >> >>
> >> >> [...]
> >> >>
> >> >> >>> + /// Writes data to the region starting from `offset`. `offset` is in units of `T`, not the
> >> >> >>> + /// number of bytes.
> >> >> >>> + ///
> >> >> >>> + /// # Examples
> >> >> >>> + ///
> >> >> >>> + /// ```
> >> >> >>> + /// # fn test(alloc: &mut kernel::dma::CoherentAllocation<u8>) -> Result {
> >> >> >>> + /// let somedata: [u8; 4] = [0xf; 4];
> >> >> >>> + /// let buf: &[u8] = &somedata;
> >> >> >>> + /// alloc.write(buf, 0)?;
> >> >> >>> + /// # Ok::<(), Error>(()) }
> >> >> >>> + /// ```
> >> >> >>> + pub fn write(&self, src: &[T], offset: usize) -> Result {
> >> >> >>> + let end = offset.checked_add(src.len()).ok_or(EOVERFLOW)?;
> >> >> >>> + if end >= self.count {
> >> >> >>> + return Err(EINVAL);
> >> >> >>> + }
> >> >> >>> + // SAFETY:
> >> >> >>> + // - The pointer is valid due to type invariant on `CoherentAllocation`
> >> >> >>> + // and we've just checked that the range and index is within bounds.
> >> >> >>> + // - `offset` can't overflow since it is smaller than `selfcount` and we've checked
> >> >> >>> + // that `self.count` won't overflow early in the constructor.
> >> >> >>> + unsafe {
> >> >> >>> + core::ptr::copy_nonoverlapping(src.as_ptr(), self.cpu_addr.add(offset), src.len())
> >> >> >>
> >> >> >> Why are there no concurrent write or read operations on `cpu_addr`?
> >> >> >
> >> >> > Sorry, can you rephrase this question?
> >> >>
> >> >> This write is suffering the same complications as discussed here [1].
> >> >> There are multiple issues with this implementation.
> >> >>
> >> >> 1) `write` takes a shared reference and thus may be called concurrently.
> >> >> There is no synchronization, so `copy_nonoverlapping` could be called
> >> >> concurrently on the same address. The safety requirements for
> >> >> `copy_nonoverlapping` state that the destination must be valid for
> >> >> write. Alice claims in [1] that any memory area that experience data
> >> >> races are not valid for writes. So the safety requirement of
> >> >> `copy_nonoverlapping` is violated and this call is potential UB.
> >> >>
> >> >> 2) The destination of this write is DMA memory. It could be concurrently
> >> >> modified by hardware, leading to the same issues as 1). Thus the
> >> >> function cannot be safe if we cannot guarantee hardware will not write
> >> >> to the region while this function is executing.
> >> >>
> >> >> Now, I don't think that these _should_ be issues, but according to our
> >> >> Rust language experts they _are_.
> >> >>
> >> >> I really think that copying data through a raw pointer to or from a
> >> >> place that experiences data races, should _not_ be UB if the data is not
> >> >> interpreted in any way, other than moving it.
> >> >>
> >> >>
> >> >> Best regards,
> >> >> Andreas Hindborg
> >> >
> >> > We need to make progress on this series, and it's starting to get late
> >> > in the cycle. I suggest we:
> >>
> >> There is always another cycle.
> >>
> >> >
> >> > 1. Delete as_slice, as_slice_mut, write, and skip_drop.
> >> > 2. Change field_read/field_write to use a volatile read/write.
> >>
> >> Volatile reads/writes that race are OK?
> >
> > I will not give a blanket yes to that. If you read their docs, you
> > will find that they claim to not allow it. But they are the correct
> > choice for DMA memory, and there's no way in practice to get
> > miscompilations on memory locations that are only accessed with
> > volatile operations, and never have references to them created.
> >
> > In general, this will fall into the exception that we've been given
> > from the Rust people. In cases such as this where the Rust language
> > does not give us the operation we want, do it like you do in C. Since
> > Rust uses LLVM which does not miscompile the C part of the kernel, it
> > should not miscompile the Rust part either.
>
> This exception we got for `core::ptr::{read,write}_volatile`, did we
> document that somewhere?
>
[Cc Ralf, comex and LKMM list]
Some related discussions:
* https://github.com/rust-lang/unsafe-code-guidelines/issues/476
* https://github.com/rust-lang/unsafe-code-guidelines/issues/348#issuecomment-1221376388
particularly Ralf's comment on comex's message:
"""
@comex
> First, keep in mind that you could simply transliterate the C
> versions of READ_ONCE/WRITE_ONCE, barriers, etc. directly to Rust,
> using ptr::read_volatile/ptr::write_volatile in place of C volatile
> loads and stores, and asm! in place of C asm blocks. If you do,
> you'll end up with the same LLVM IR instructions (or GCC equivalent
> with rustc_codegen_gcc), which will get passed to the same
> optimizer, and which ultimately will work or not work to the same
> extent as the C versions.
Indeed I think that is probably the best approach.
"""
* A LONG thread of the discussion:
https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/UB.20caused.20by.20races.20on.20.60.7Bread.2Cwrite.7D_volatile.60/near/399343771
In general, the rationale is if Rust code could generate the same LLVM
IR as C code, then if it's not data race per LKMM, then it's not treated
as data race in Rust as well. But this is not a "get-out-of-UB" free
card IMO:
* If both sides of the racing are Rust code, we should avoid using
{read,write}_volatile(), and use proper synchronization.
* If atomicity is also required, we should use Atomic::from_ptr()
instead of {read,write}_volatile().
> I feel slightly lost when trying to figure out what fits under this
> exception and what is UB. I think that fist step to making this more
> straight forward is having clear documentation.
>
I agree, and I'm happy to help on this.
> For cases where we need to do the equivalent of `memmove`/`memcpy`, what
> are is our options?
>
Seems we need "volatile" memmove and memcpy in Rust?
> In case we have no options, do you know who would be the right people on
> the Rust Project side to contact about getting an exception for this
> case?
>
I will say it'll be t-opsem.
Regards,
Boqun
>
> Best regards,
> Andreas Hindborg
>
>
next prev parent reply other threads:[~2025-03-03 20:09 UTC|newest]
Thread overview: 70+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-24 11:49 [PATCH v12 0/3] rust: add dma coherent allocator abstraction Abdiel Janulgue
2025-02-24 11:49 ` [PATCH v12 1/3] rust: error: Add EOVERFLOW Abdiel Janulgue
2025-02-24 13:11 ` Andreas Hindborg
2025-02-24 11:49 ` [PATCH v12 2/3] rust: add dma coherent allocator abstraction Abdiel Janulgue
2025-02-24 13:21 ` Alice Ryhl
2025-02-24 16:27 ` Abdiel Janulgue
2025-02-24 13:30 ` QUENTIN BOYER
2025-02-24 16:30 ` Abdiel Janulgue
2025-02-24 14:40 ` Andreas Hindborg
2025-02-24 16:27 ` Abdiel Janulgue
2025-02-24 22:35 ` Daniel Almeida
2025-02-28 8:35 ` Alexandre Courbot
2025-02-28 10:01 ` Danilo Krummrich
2025-02-24 20:07 ` Benno Lossin
2025-02-24 21:40 ` Miguel Ojeda
2025-02-24 23:12 ` Daniel Almeida
2025-03-03 13:00 ` Andreas Hindborg
2025-03-03 13:13 ` Alice Ryhl
2025-03-03 15:21 ` Andreas Hindborg
2025-03-03 15:44 ` Alice Ryhl
2025-03-03 18:45 ` Andreas Hindborg
2025-03-03 19:00 ` Allow data races on some read/write operations Andreas Hindborg
2025-03-03 20:08 ` Boqun Feng [this message]
2025-03-04 19:03 ` Ralf Jung
2025-03-04 20:18 ` comex
2025-03-05 3:24 ` Boqun Feng
2025-03-05 13:10 ` Ralf Jung
2025-03-05 13:23 ` Alice Ryhl
2025-03-05 13:27 ` Ralf Jung
2025-03-05 14:40 ` Robin Murphy
2025-03-05 18:43 ` Andreas Hindborg
2025-03-05 19:30 ` Alan Stern
2025-03-05 19:42 ` Ralf Jung
2025-03-05 21:26 ` Andreas Hindborg
2025-03-05 21:53 ` Ralf Jung
2025-03-07 8:43 ` Andreas Hindborg
2025-03-18 14:44 ` Ralf Jung
2025-03-05 18:41 ` Andreas Hindborg
2025-03-05 14:25 ` Daniel Almeida
2025-03-05 18:38 ` Andreas Hindborg
2025-03-05 22:01 ` Ralf Jung
2025-03-04 8:28 ` [PATCH v12 2/3] rust: add dma coherent allocator abstraction Abdiel Janulgue
2025-02-25 8:15 ` Abdiel Janulgue
2025-02-25 9:09 ` Alice Ryhl
2025-02-24 22:05 ` Miguel Ojeda
2025-02-25 8:15 ` Abdiel Janulgue
2025-03-03 11:30 ` Andreas Hindborg
2025-03-04 8:58 ` Abdiel Janulgue
2025-03-03 13:08 ` Robin Murphy
2025-03-05 17:41 ` Jason Gunthorpe
2025-03-06 13:37 ` Danilo Krummrich
2025-03-06 15:21 ` Simona Vetter
2025-03-06 15:49 ` Danilo Krummrich
2025-03-06 15:54 ` Danilo Krummrich
2025-03-06 16:18 ` Jason Gunthorpe
2025-03-06 16:34 ` Danilo Krummrich
2025-03-07 10:20 ` Simona Vetter
2025-03-06 16:09 ` Jason Gunthorpe
2025-03-07 8:50 ` Danilo Krummrich
2025-03-07 10:18 ` Simona Vetter
2025-03-07 12:48 ` Jason Gunthorpe
2025-03-07 13:16 ` Simona Vetter
2025-03-07 14:38 ` Jason Gunthorpe
2025-03-07 17:30 ` Danilo Krummrich
2025-03-07 18:02 ` Greg Kroah-Hartman
2025-03-07 16:09 ` Danilo Krummrich
2025-03-07 16:57 ` Jason Gunthorpe
2025-03-07 19:03 ` Danilo Krummrich
2025-02-24 11:49 ` [PATCH v12 3/3] MAINTAINERS: add entry for Rust dma mapping helpers device driver API Abdiel Janulgue
2025-02-24 13:10 ` Andreas Hindborg
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=Z8YMTiKS4T9wC4t_@boqun-archlinux \
--to=boqun.feng@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=abdiel.janulgue@gmail.com \
--cc=airlied@redhat.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=comexk@gmail.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=hch@lst.de \
--cc=iommu@lists.linux.dev \
--cc=kernel@valentinobst.de \
--cc=linux-kernel@vger.kernel.org \
--cc=lkmm@lists.linux.dev \
--cc=m.szyprowski@samsung.com \
--cc=ojeda@kernel.org \
--cc=post@ralfj.de \
--cc=robin.murphy@arm.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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).