Re: [PATCH v12 2/3] rust: add dma coherent allocator abstraction.

From: Miguel Ojeda
Date: Mon Feb 24 2025 - 17:06:02 EST


Hi Abdiel,

Some quick doc-related nits -- please take them as a general guide for
potential improvements in newer versions etc., given there are still
other comments that could change the contents.

On Mon, Feb 24, 2025 at 12:50 PM Abdiel Janulgue
<abdiel.janulgue@xxxxxxxxx> wrote:
>
> +/// Inform the kernel about the device's DMA addressing capabilities. This will set the mask for
> +/// both streaming and coherent APIs together.

This comment differs from the C side one -- that is OK, but just
wondering if there was a strong reason for that.

> +pub fn dma_set_mask_and_coherent(dev: &Device, mask: u64) -> i32 {

This returns `i32` -- I have not read the users of this, but should we
take the chance to have a `Result` already here? Same below for the
other one.

> + // SAFETY: device pointer is guaranteed as valid by invariant on `Device`.

To keep things consistent, please start comments with uppercase, i.e.
"SAFETY: Device pointer ..."

It may also be clearer to say "by the type invariant on".

> +/// Possible attributes associated with a DMA mapping.
> +///
> +/// They can be combined with the operators `|`, `&`, and `!`.

Even if it may be trivial, a small example could be nice here (when I
see a sentence like "This can be used ...", I typically consider
whether it is a good place to show how).

> +/// DMA mapping attrributes.

Typo: attributes.

> + /// let c: CoherentAllocation<u64> = CoherentAllocation::alloc_attrs(dev.into(), 4, GFP_KERNEL,
> + /// DMA_ATTR_NO_WARN)?;

Please try to format the code as `rustfmt` would normally do it. I
know it is a pain to do it manually -- hopefully
`format_code_in_doc_comments` will eventually be stable.

> + // We ensure that we catch the failure on this function and throw an ENOMEM

Apart from what Benno said, please try to use Markdown in all comments.

> + /// Performs the same functionality as `alloc_attrs`, except the `dma_attrs` is 0 by default.

Intra-doc links (I will mark a few more that I think may work).

> + /// Create a duplicate of the `CoherentAllocation` object but prevent it from being dropped.

Intra-doc link.

> + /// r/w access or use-cases where the pointer to the live data is needed, `start_ptr()` or
> + /// `start_ptr_mut()` could be used instead.

Intra-doc links.

> + /// Performs the same functionality as `as_slice`, except that a mutable slice is returned.

Intra-doc link.

> + /// Reads the value of `field` and ensures that its type is `FromBytes`

Intra-doc link.

> + /// # Safety:

Typo: no colon. Also another one below.

> + /// This must be called from the `dma_read` macro which ensures that the `field` pointer is
> + /// validated beforehand.
> + ///
> + /// Public but hidden since it should only be used from `dma_read` macro.

Intra-doc links -- even if they are not rendered because it is hidden
(also even if it were a private item).

> + #[doc(hidden)]
> + pub unsafe fn field_read<F: FromBytes>(&self, field: *const F) -> F {
> + // SAFETY: By the safety requirements field is valid

Markdown; and please end the sentence with a period for consistency.

> + /// Writes a value to `field` and ensures that its type is `AsBytes`

Intra-doc link, and period at the end (same below too).

> +/// Reads a field of an item from an allocated region of structs.
> +/// # Examples

Newline between these two lines. Also for the write equivalent one below.

> +/// struct MyStruct { field: u32, }
> +/// // SAFETY: All bit patterns are acceptable values for MyStruct.

Newline between these two, also Markdown. Same below and in the write
equivalent.

I think it is fairly important to have clean examples, since people
will learn from and follow them!

Thanks!

Cheers,
Miguel