[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DBBRM08HB0UO.3HTRN04ZSHVQ8@kernel.org>
Date: Mon, 14 Jul 2025 14:09:06 +0200
From: "Danilo Krummrich" <dakr@...nel.org>
To: "Alice Ryhl" <aliceryhl@...gle.com>
Cc: "Daniel Almeida" <daniel.almeida@...labora.com>, "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>,
"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 Mon Jul 14, 2025 at 1:51 PM CEST, Alice Ryhl wrote:
> 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.
Yeah, it mixes up types describing CPU word width and bus address width. :(
> 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.
I think fixing up the C side would be even nicer, but for the scope of this
series that's fine. The comment should mention that, ultimately, we want to fix
up the C side type wise.
Powered by blists - more mailing lists