[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aHTvMNIenbaGtBBt@google.com>
Date: Mon, 14 Jul 2025 11:51:12 +0000
From: Alice Ryhl <aliceryhl@...gle.com>
To: Daniel Almeida <daniel.almeida@...labora.com>
Cc: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
"Björn Roy Baron" <bjorn3_gh@...tonmail.com>, Andreas Hindborg <a.hindborg@...nel.org>,
Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>, "Rafael J. Wysocki" <rafael@...nel.org>,
Andrew Morton <akpm@...ux-foundation.org>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
"Ilpo Järvinen" <ilpo.jarvinen@...ux.intel.com>, Bjorn Helgaas <bhelgaas@...gle.com>,
Mika Westerberg <mika.westerberg@...ux.intel.com>, Ying Huang <huang.ying.caritas@...il.com>,
Benno Lossin <lossin@...nel.org>, linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org
Subject: Re: [PATCH v12 2/3] rust: io: mem: add a generic iomem abstraction
On Thu, Jul 10, 2025 at 10:58:13AM -0300, Daniel Almeida wrote:
> Hi Alice,
>
> >> +impl<const SIZE: usize> IoMem<SIZE> {
> >> + fn ioremap(resource: &Resource) -> Result<Self> {
> >> + let size = resource.size();
> >> + if size == 0 {
> >> + return Err(EINVAL);
> >> + }
> >> +
> >> + let res_start = resource.start();
> >> +
> >> + let addr = if resource
> >> + .flags()
> >> + .contains(io::resource::flags::IORESOURCE_MEM_NONPOSTED)
> >> + {
> >> + // SAFETY:
> >> + // - `res_start` and `size` are read from a presumably valid `struct resource`.
> >> + // - `size` is known not to be zero at this point.
> >> + unsafe { bindings::ioremap_np(res_start, size as usize) }
> >
> > Here you cast from ResourceSize to usize. Are you sure that is correct?
> > I thought those types could be different.
>
> This seems to what C is doing as well, i.e.:
>
> static void __iomem *__devm_ioremap(struct device *dev, resource_size_t offset,
> resource_size_t size, <---------
> enum devm_ioremap_type type)
> {
>
> […]
>
> case DEVM_IOREMAP_NP:
> addr = ioremap_np(offset, size);
> break;
> }
>
>
> Where:
>
> `static inline void __iomem *ioremap_np(phys_addr_t offset, size_t size)`
>
> IOW: this stems from the mix and match of types used the C API itself.
>
> What do you suggest here? Maybe a try_into() then?
What a mess. It looks like there aren't any 32-bit architectures that
define ioremap_np. This means that sometimes this cast will be lossy,
but in those cases the function body just returns NULL and doesn't read
the size.
I would probably cast to an underscore instead of explicitly mentioning
the target type and make a comment about it.
Alice
Powered by blists - more mailing lists