Thank you for your contribution! Sashiko AI review found 1 potential issue(s) 
to consider:
- [High] Using `Region<SIZE>` for `IoMem` and `pci::Bar` breaks I/O mappings 
for valid resources with non-4-byte alignment or odd sizes.
--

commit ac5441d891df2073c7c58a81916e6a7310f48450
Author: Gary Guo <[email protected]>

rust: io: generalize `MmioRaw` to pointer to arbitrary type

This commit updates MmioRaw to be generic over the type it points to.
It introduces MmioRaw::new_region utilizing the Region type and updates
IoMem and pci::Bar to use this new region API instead of the raw address.

> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index 9f060dd291826..12be266d7ed7e 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs

[ ... ]

> +impl<const SIZE: usize> MmioRaw<Region<SIZE>> {
> +    /// Create a `MmioRaw` representing a I/O region with given size.
> +    ///
> +    /// The size is checked against the minimum size specified via const 
> generics.
> +    #[inline]
> +    pub fn new_region(addr: usize, size: usize) -> Result<Self> {
> +        Ok(Self {
> +            ptr: 
> Region::ptr_try_from_raw_parts_mut(core::ptr::without_provenance_mut(addr), 
> size)?,

[Severity: High]
Does this change prevent drivers from mapping valid hardware resources that
are not 4-byte aligned or have sizes that aren't multiples of 4?

Looking at Region::ptr_try_from_raw_parts_mut(), it enforces strict
alignment and size constraints:

    if size < SIZE || base.align_offset(4) != 0 || !size.is_multiple_of(4) {
        return Err(EINVAL);
    }

Since IoMem::ioremap() and pci::Bar::new() now both call MmioRaw::new_region()
instead of MmioRaw::new(), wouldn't this cause device probes to unconditionally
fail for drivers attempting to map legacy 8-bit or 16-bit peripherals where
the I/O resources are not strictly 4-byte aligned or sized?

> +        })
> +    }
> +}

[ ... ]

> diff --git a/rust/kernel/io/mem.rs b/rust/kernel/io/mem.rs
> index fc2a3e24f8d5d..9e15bc8fde784 100644
> --- a/rust/kernel/io/mem.rs
> +++ b/rust/kernel/io/mem.rs

[ ... ]

> @@ -264,8 +264,7 @@ fn ioremap(dev: &'a Device<Bound>, resource: &Resource) 
> -> Result<Self> {
>              return Err(ENOMEM);
>          }
>  
> -        let io = MmioRaw::new(addr as usize, size)?;
> -
> +        let io = MmioRaw::new_region(addr as usize, size)?;
>          Ok(IoMem { dev, io })
>      }
>

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=5

Reply via email to