From: Gary Guo <gary@garyguo.net>
To: Benno Lossin <lossin@kernel.org>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Abdiel Janulgue" <abdiel.janulgue@gmail.com>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Robin Murphy" <robin.murphy@arm.com>,
"Alexandre Courbot" <acourbot@nvidia.com>,
rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/4] rust: dma: generalize `dma_{read,write}` macro
Date: Sat, 14 Feb 2026 10:46:34 +0000 [thread overview]
Message-ID: <84ef050ac2c2cf858ec766f23d1fb90e@garyguo.net> (raw)
In-Reply-To: <DGELM1R9FJNE.30QDHV2IXV0BI@kernel.org>
On 2026-02-14 10:04, Benno Lossin wrote:
> On Sat Feb 14, 2026 at 6:33 AM CET, Gary Guo wrote:
>> + (@parse [$dma:expr] [$($proj:tt)*] [, $val:expr]) => {
>> + let ptr = $crate::project_pointer!(
>> + mut $crate::dma::CoherentAllocation::as_mut_ptr(&$dma), $($proj)*
>> + );
>> + let val = $val;
>> + // SAFETY: pointer created by projection is within DMA region.
>> + unsafe { $crate::dma::CoherentAllocation::field_write(&$dma, ptr, val) }
>
> This evaluates `$dma` for a second time (and also places it inside an
> `unsafe` block).
Ah good point. The macro that we have today put `$val` inside unsafe and I've spotted and
lifted it out, but I didn't spot the `$dma` part.
>
>> + };
>
> Missing surrounding `{}` to allow this in expression position?
Yeah, I also spotted this myself after sending the series out.
>
>> + (@parse [$dma:expr] [$($proj:tt)*] [.$field:tt $($rest:tt)*]) => {
>> + $crate::dma_write!(@parse [$dma] [$($proj)* .$field] [$($rest)*])
>> + };
>> + (@parse [$dma:expr] [$($proj:tt)*] [[$index:expr]? $($rest:tt)*]) => {
>> + $crate::dma_write!(@parse [$dma] [$($proj)* [$index]?] [$($rest)*])
>> + };
>> + (@parse [$dma:expr] [$($proj:tt)*] [[$index:expr] $($rest:tt)*]) => {
>> + $crate::dma_write!(@parse [$dma] [$($proj)* [$index]] [$($rest)*])
>> + };
>> + ($dma:expr, $($rest:tt)*) => {
>> + $crate::dma_write!(@parse [$dma] [] [$($rest)*])
>> };
>
> I'm wondering if this also works:
>
> ($dma:expr, $($(.$field:ident)? $([$index:expr])?)*, $val:expr) => {{
> let dma = &$dma;
> let ptr = $crate::project_pointer!(
> mut $crate::dma::CoherentAllocation::as_mut_ptr(dma),
> $($(.$field)? $([$index])?)*,
> );
> let val = $val;
> // SAFETY: pointer created by projection is within DMA region.
> unsafe { $crate::dma::CoherentAllocation::field_write(dma, ptr, val) }
> }}
Rust would complain that the outer repetition can match empty token tree.
>
>> }
>> diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs
>> index 9c45851c876e..b772ada2c65c 100644
>> --- a/samples/rust/rust_dma.rs
>> +++ b/samples/rust/rust_dma.rs
>> @@ -68,7 +68,7 @@ fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, E
>> CoherentAllocation::alloc_coherent(pdev.as_ref(), TEST_VALUES.len(), GFP_KERNEL)?;
>>
>> for (i, value) in TEST_VALUES.into_iter().enumerate() {
>> - kernel::dma_write!(ca[i] = MyStruct::new(value.0, value.1))?;
>> + kernel::dma_write!(ca, [i]?, MyStruct::new(value.0, value.1));
>> }
>>
>> let size = 4 * page::PAGE_SIZE;
>> @@ -91,17 +91,17 @@ fn drop(self: Pin<&mut Self>) {
>> dev_info!(self.pdev, "Unload DMA test driver.\n");
>>
>> for (i, value) in TEST_VALUES.into_iter().enumerate() {
>> - let val0 = kernel::dma_read!(self.ca[i].h);
>> - let val1 = kernel::dma_read!(self.ca[i].b);
>> - assert!(val0.is_ok());
>> - assert!(val1.is_ok());
>> + let result = (|| -> Result<_> {
>> + let val0 = kernel::dma_read!(self.ca, [i]?.h);
>> + let val1 = kernel::dma_read!(self.ca, [i]?.b);
>>
>> - if let Ok(val0) = val0 {
>> assert_eq!(val0, value.0);
>> - }
>> - if let Ok(val1) = val1 {
>> assert_eq!(val1, value.1);
>> - }
>> +
>> + Ok(())
>> + })();
>
> I dislike that we have to reintroduce the budget-try block here. Ideally
> we could add something like `try` at the beginning of the macro and then
> automatically add the try block. Feel free to make that a future series.
I don't think this is an issue. It's visible inside the samples because
we are testing the values, but in practice most users would propagate the
errors out.
I also dislike that the budget-try block that we have inside `dma_read!`
currently hard-codes the error type.
Best,
Gary
>
> Cheers,
> Benno
>
>> +
>> + assert!(result.is_ok());
>> }
>>
>> for (i, entry) in self.sgt.iter().enumerate() {
next prev parent reply other threads:[~2026-02-14 10:46 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-14 5:33 [PATCH 0/4] rust: add pointer projection infrastructure and convert DMA Gary Guo
2026-02-14 5:33 ` [PATCH 1/4] rust: add projection infrastructure Gary Guo
2026-02-14 9:53 ` Benno Lossin
2026-02-14 10:36 ` Gary Guo
2026-02-14 14:48 ` Benno Lossin
2026-02-14 10:27 ` Danilo Krummrich
2026-02-22 0:57 ` Benno Lossin
2026-02-22 10:52 ` Gary Guo
2026-02-14 5:33 ` [PATCH 2/4] rust: dma: generalize `dma_{read,write}` macro Gary Guo
2026-02-14 10:04 ` Benno Lossin
2026-02-14 10:46 ` Gary Guo [this message]
2026-02-14 14:53 ` Benno Lossin
2026-02-14 5:33 ` [PATCH 3/4] gpu: nova-core: convert to use new `dma_write!` syntax Gary Guo
2026-02-14 10:06 ` Benno Lossin
2026-02-14 5:33 ` [PATCH 4/4] rust: dma: remove old dma_{read,write} macro compatibility syntax Gary Guo
2026-02-14 10:05 ` Benno Lossin
2026-02-14 10:33 ` [PATCH 0/4] rust: add pointer projection infrastructure and convert DMA Danilo Krummrich
2026-02-14 10:50 ` Gary Guo
2026-02-14 11:00 ` Danilo Krummrich
2026-02-15 0:47 ` Miguel Ojeda
2026-02-15 11:06 ` Danilo Krummrich
2026-02-15 12:06 ` Miguel Ojeda
2026-02-15 12:56 ` Danilo Krummrich
2026-02-15 15:16 ` Miguel Ojeda
2026-02-15 17:02 ` Danilo Krummrich
2026-02-18 10:49 ` Miguel Ojeda
2026-02-18 18:25 ` Danilo Krummrich
2026-02-15 14:39 ` Benno Lossin
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=84ef050ac2c2cf858ec766f23d1fb90e@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=abdiel.janulgue@gmail.com \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=driver-core@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--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