Re: [PATCH 2/4] rust: dma: generalize `dma_{read,write}` macro

From: Benno Lossin

Date: Sat Feb 14 2026 - 09:53:36 EST


On Sat Feb 14, 2026 at 11:46 AM CET, Gary Guo wrote:
> 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)*] [.$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.

Ah right.

>>> @@ -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.

Maybe we should just have a function that returns a Result in this test
that's called from drop.

> I also dislike that the budget-try block that we have inside `dma_read!`
> currently hard-codes the error type.

Yeah me too.

Cheers,
Benno