Re: [PATCH 07/10] rust: io: add subregion method with compile-time check

"Alexandre Courbot" <[email protected]> Tue, 28 Jul 2026 16:21:14 +0900
Newsgroups dev.linux.lists.nova-gpu,dev.linux.lists.driver-core,org.freedesktop.lists.dri-devel,org.kernel.vger.linux-kernel,org.kernel.vger.linux-pci,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On Wed Jul 22, 2026 at 1:54 AM JST, Gary Guo wrote:
> Add a helper function that allows obtaining a subregion from region with
> compile-time check. This can be used by drivers to obtaining a subregion
> for relative register access.
>
> Signed-off-by: Gary Guo <[email protected]>
> ---
>  rust/kernel/io.rs | 24 ++++++++++++++++++++++++
>  1 file changed, 24 insertions(+)
>
> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index 0f8b166a6d3b..f31d37c8a3d5 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs
> @@ -80,6 +80,30 @@ pub fn ptr_try_from_raw_parts_mut(base: *mut u8, size:=
 usize) -> Result<*mut Sel
> =20
>          Ok(Self::ptr_from_raw_parts_mut(base, size))
>      }
> +
> +    /// Create a subregion with provided offset and size.
> +    #[inline]
> +    pub fn subregion<'a, const OFFSET: usize, const NEW_SIZE: usize, IO>=
(
> +        io: IO,
> +    ) -> <IO::Backend as IoBackend>::View<'a, Region<NEW_SIZE>>
> +    where
> +        IO: IoBase<'a, Target =3D Self>,
> +    {
> +        const_assert!(
> +            OFFSET + NEW_SIZE <=3D SIZE && OFFSET.is_multiple_of(4) && N=
EW_SIZE.is_multiple_of(4)

We are starting to have quite a bit of these magic `4` throughout the
file (they are also used in `ptr_try_from_raw_parts_mut`). It's a bit
heavier, but how about using the semantically more meaningful
`Self::MIN_ALIGN.as_usize()`?