From: Daniel Almeida <daniel.almeida@collabora.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
"Fiona Behrens" <me@kloenk.dev>
Subject: Re: [PATCH v13 1/3] rust: io: add resource abstraction
Date: Wed, 16 Jul 2025 13:52:36 -0300 [thread overview]
Message-ID: <D9C5D4EC-FA24-47DB-BE89-609713F093FF@collabora.com> (raw)
In-Reply-To: <aHYLWc_KkMHj_jF-@google.com>
Hi Alice,
>
>> + let inner = self.0.get();
>> + // SAFETY: safe as per the invariants of `Resource`.
>> + unsafe { (*inner).start }
>> + }
>> +
>> + /// Returns the name of the resource.
>> + pub fn name(&self) -> &'static CStr {
>> + let inner = self.0.get();
>> + // SAFETY: safe as per the invariants of `Resource`
>> + unsafe { CStr::from_char_ptr((*inner).name) }
>
> This is 'static? I would like this safety comment to explicitly say that
> the string always lives forever no matter what resource you call this
> on.
>
> Alice
>
Actually, we have a bit of a problem here.
First, there appears to be no guarantee that a `Resource` has a valid name.
In fact:
#define DEFINE_RES_NAMED(_start, _size, _name, _flags) \
DEFINE_RES_NAMED_DESC(_start, _size, _name, _flags, IORES_DESC_NONE)
#define DEFINE_RES(_start, _size, _flags) \
DEFINE_RES_NAMED(_start, _size, NULL, _flags)
#define DEFINE_RES_IO_NAMED(_start, _size, _name) \
DEFINE_RES_NAMED((_start), (_size), (_name), IORESOURCE_IO)
#define DEFINE_RES_IO(_start, _size) \
DEFINE_RES_IO_NAMED((_start), (_size), NULL)
The non _NAMED version of these macros will assign a NULL pointer, so we can't
derive a CStr from that at all.
On top of that, although some call sites do use static names, i.e.:
struct resource ioport_resource = {
.name = "PCI IO",
.start = 0,
.end = IO_SPACE_LIMIT,
.flags = IORESOURCE_IO,
};
EXPORT_SYMBOL(ioport_resource);
struct resource iomem_resource = {
.name = "PCI mem",
.start = 0,
.end = -1,
.flags = IORESOURCE_MEM,
};
EXPORT_SYMBOL(iomem_resource);
static struct resource busn_resource = {
.name = "PCI busn",
.start = 0,
.end = 255,
.flags = IORESOURCE_BUS,
};
Some appear to use other, smaller lifetimes, like the one below:
struct pnp_resource *pnp_add_resource(struct pnp_dev *dev,
struct resource *res)
{
struct pnp_resource *pnp_res;
pnp_res = pnp_new_resource(dev);
if (!pnp_res) {
dev_err(&dev->dev, "can't add resource %pR\n", res);
return NULL;
}
pnp_res->res = *res;
pnp_res->res.name = dev->name;
I guess the easiest solution is to drop 'static in order to account for the
above, and change the signature to return Option<&CStr> instead.
We can also change Region to own the name, and pass name by value here:
pub fn request_region(
&self,
start: ResourceSize,
size: ResourceSize,
name: &'static CStr <------
Thoughts?
— Daniel
next prev parent reply other threads:[~2025-07-16 16:53 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-11 22:32 [PATCH v13 0/3] rust: platform: add Io support Daniel Almeida
2025-07-11 22:32 ` [PATCH v13 1/3] rust: io: add resource abstraction Daniel Almeida
2025-07-15 8:03 ` Alice Ryhl
2025-07-16 16:52 ` Daniel Almeida [this message]
2025-07-16 17:04 ` Alice Ryhl
2025-07-16 17:10 ` Danilo Krummrich
2025-07-11 22:32 ` [PATCH v13 2/3] rust: io: mem: add a generic iomem abstraction Daniel Almeida
2025-07-15 8:12 ` Alice Ryhl
2025-07-15 8:20 ` Danilo Krummrich
2025-07-11 22:32 ` [PATCH v13 3/3] rust: platform: add resource accessors Daniel Almeida
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=D9C5D4EC-FA24-47DB-BE89-609713F093FF@collabora.com \
--to=daniel.almeida@collabora.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=me@kloenk.dev \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®