Re: [PATCH] rust: types: add FOREIGN_ALIGN to ForeignOwnable

From: Alice Ryhl
Date: Wed Jun 11 2025 - 07:13:07 EST


On Wed, Jun 11, 2025 at 12:46 PM Andreas Hindborg <a.hindborg@xxxxxxxxxx> wrote:
>
> "Alice Ryhl" <aliceryhl@xxxxxxxxxx> writes:
>
> > On Thu, Jun 5, 2025 at 10:00 PM Andreas Hindborg <a.hindborg@xxxxxxxxxx> wrote:
> >>
> >> The current implementation of `ForeignOwnable` is leaking the type of the
> >> opaque pointer to consumers of the API. This allows consumers of the opaque
> >> pointer to rely on the information that can be extracted from the pointer
> >> type.
> >>
> >> To prevent this, change the API to the version suggested by Maira
> >> Canal (link below): Remove `ForeignOwnable::PointedTo` in favor of a
> >> constant, which specifies the alignment of the pointers returned by
> >> `into_foreign`.
> >>
> >> Suggested-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> >> Suggested-by: Maíra Canal <mcanal@xxxxxxxxxx>
> >> Link: https://lore.kernel.org/r/20240309235927.168915-3-mcanal@xxxxxxxxxx
> >> Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
> >
> > One nit below. With that and things other folks mentioned fixed, you may add:
> >
> > Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> >
> >> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> >> index 22985b6f6982..025c619a2195 100644
> >> --- a/rust/kernel/types.rs
> >> +++ b/rust/kernel/types.rs
> >> @@ -21,15 +21,11 @@
> >> ///
> >> /// # Safety
> >> ///
> >> -/// Implementers must ensure that [`into_foreign`] returns a pointer which meets the alignment
> >> -/// requirements of [`PointedTo`].
> >> -///
> >> -/// [`into_foreign`]: Self::into_foreign
> >> -/// [`PointedTo`]: Self::PointedTo
> >> +/// Implementers must ensure that [`Self::into_foreign`] return pointers with alignment that is an
> >> +/// integer multiple of [`Self::FOREIGN_ALIGN`].
> >
> > We should require non-null:
>
> What is your rationale for this?

The rationale is that the implementation of XArray assumes that the
pointers are non-null. If we allow null pointers, we will need to fix
the XArray.

Alice