[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <72809F34-033A-4A72-B56D-519D1D089299@collabora.com>
Date: Mon, 14 Jul 2025 20:39:15 -0300
From: Daniel Almeida <daniel.almeida@...labora.com>
To: Alice Ryhl <aliceryhl@...gle.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
Alice,
> On 14 Jul 2025, at 08:51, Alice Ryhl <aliceryhl@...gle.com> 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. 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
You replied here but v13 was already out [0]. Can we shift the dicussion
there? I ended up using try_into(), but feel free to suggest the cast to _
+ the TODO if you still feel like this is the right approach.
-- Daniel
[0] https://lore.kernel.org/rust-for-linux/20250711-topics-tyr-platform_iomem-v13-0-06328b514db3@collabora.com/
Powered by blists - more mailing lists