public inbox for linux-pci@vger.kernel.org
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Danilo Krummrich" <dakr@kernel.org>
Cc: "Gary Guo" <gary@garyguo.net>,
	lyude@redhat.com, "Alice Ryhl" <aliceryhl@google.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
	driver-core@lists.linux.dev, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org,
	"Zhi Wang" <zhiw@nvidia.com>,
	"Eliot Courtney" <ecourtney@nvidia.com>
Subject: Re: [PATCH v2 1/6] rust: io: turn IoCapable into a functional trait
Date: Tue, 17 Feb 2026 10:36:56 +0900	[thread overview]
Message-ID: <DGGUOQT5BJW2.14AK9GFZKC854@nvidia.com> (raw)
In-Reply-To: <DGGJSHVZB6U5.2V8OMF5N3KZNZ@kernel.org>

On Tue Feb 17, 2026 at 2:04 AM JST, Danilo Krummrich wrote:
> On Mon Feb 16, 2026 at 2:27 PM CET, Alexandre Courbot wrote:
>> It doesn't - here is the implementation of Io for Mmio:
>>
>>     impl<const SIZE: usize> Io for Mmio<SIZE> {
>>         /// Returns the base address of this mapping.
>>         #[inline]
>>         fn addr(&self) -> usize {
>>             self.0.addr()
>>         }
>>
>>         /// Returns the maximum size of this mapping.
>>         #[inline]
>>         fn maxsize(&self) -> usize {
>>             self.0.maxsize()
>>         }
>>     }
>>
>> Now what prevents me from doing this:
>>
>>     impl<const SIZE: usize> Io for YoloMmio<SIZE> {
>>         fn addr(&self) -> usize {
>>             self.0.addr()
>>         }
>>
>>         fn maxsize(&self) -> usize {
>>             self.0.maxsize() + 0x10000
>>         }
>>     }
>>
>> With that, I have allowed callers to invoke the unsafe methods of
>> `IoCapable` on an extra 0x10000 bytes of I/O I don't own, without any
>> unsafe code.
>
> I don't think you did, as you only present half of your counter example; you
> left out the IoCapable part.
>
> I.e. with what you have above cannot uphold the safety justification in the
> corresponding IoCapable implementation:
>
> This is the invariant on struct Mmio:
>
> 	/// # Invariant
> 	///
> 	/// `addr` is the start and `maxsize` the length of valid I/O mapped memory region of size
> 	/// `maxsize`.
>
> And in impl_mmio_io_capable!() you refer to this invariant:
>
> 	macro_rules! impl_mmio_io_capable {
> 	    ($mmio:ident, $(#[$attr:meta])* $ty:ty, $read_fn:ident, $write_fn:ident) => {
> 	        $(#[$attr])*
> 	        impl<const SIZE: usize> IoCapable<$ty> for $mmio<SIZE> {
> 	            unsafe fn io_read(&self, address: usize) -> $ty {
> 	                // SAFETY: By the trait invariant `address` is a valid address for MMIO operations.
> 	                unsafe { bindings::$read_fn(address as *const c_void) }
> 	            }
> 	
> 	            unsafe fn io_write(&self, value: $ty, address: usize) {
> 	                // SAFETY: By the trait invariant `address` is a valid address for MMIO operations.
> 	                unsafe { bindings::$write_fn(value, address as *mut c_void) }
> 	            }
> 	        }
> 	    };
> 	}
>
> But your YoloMmio implementation doesn't provide this invariant (because it
> can't).
>
> So, how do you justify the unsafe call to bindings::$write_fn and
> bindings::$read_fn now that you have to call impl_mmio_io_capable!() for
> YoloMmio?
>
> Again, you can't justify it, which proves that it doesn't matter what YoloMmio
> returns, it matters how you can justify it in io_read() and io_write().

Ah, I think I finally get it now. `Io` and `IoKnownSize` can piggyback
on the `IoCapable` safety requirement, because all the unsafe code
requires an `IoCapable` implementation anyway. I guess that's sound
indeed.

I don't think the same can apply to `io_read` and `io_write` though -
`IoCapable` is public, and so anyone could call them with an arbitrary
argument, e.g.

   let mmio: &Mmio<0x100>;
   ...
   IoCapable::<u8>::io_read(mmio, usize::MAX);

Here we have no way of making the callers enforce the internal safety
requirement of these methods, thus we need to keep them unsafe.

  reply	other threads:[~2026-02-17  1:37 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-06  6:00 [PATCH v2 0/6] rust: io: turn IoCapable into a functional trait Alexandre Courbot
2026-02-06  6:00 ` [PATCH v2 1/6] " Alexandre Courbot
2026-02-06 20:29   ` lyude
2026-02-12 12:04     ` Alexandre Courbot
2026-02-12 12:23       ` Danilo Krummrich
2026-02-12 14:11       ` Gary Guo
2026-02-12 14:52         ` Danilo Krummrich
2026-02-16 11:51           ` Alexandre Courbot
2026-02-16 12:08             ` Danilo Krummrich
2026-02-16 13:27               ` Alexandre Courbot
2026-02-16 17:04                 ` Danilo Krummrich
2026-02-17  1:36                   ` Alexandre Courbot [this message]
2026-02-17 11:17                     ` Danilo Krummrich
2026-02-06  6:00 ` [PATCH v2 2/6] rust: io: mem: use non-relaxed I/O ops in examples Alexandre Courbot
2026-02-06  6:00 ` [PATCH v2 3/6] rust: io: provide Mmio relaxed ops through a wrapper type Alexandre Courbot
2026-02-06 16:09   ` Daniel Almeida
2026-02-06  6:00 ` [PATCH v2 4/6] rust: io: remove legacy relaxed accessors of Mmio Alexandre Courbot
2026-02-06  6:00 ` [PATCH v2 5/6] rust: pci: io: remove overloaded Io methods of ConfigSpace Alexandre Courbot
2026-02-06  6:00 ` [PATCH v2 6/6] rust: io: remove overloaded Io methods of Mmio Alexandre Courbot
2026-02-06 15:02 ` [PATCH v2 0/6] rust: io: turn IoCapable into a functional trait Gary Guo
2026-02-06 19:12 ` Danilo Krummrich
2026-02-06 19:48 ` lyude
2026-02-12 12:28   ` Alexandre Courbot
2026-02-12 14:40     ` Daniel Almeida
2026-03-15  0:56 ` Danilo Krummrich

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=DGGUOQT5BJW2.14AK9GFZKC854@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bhelgaas@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=driver-core@lists.linux.dev \
    --cc=ecourtney@nvidia.com \
    --cc=gary@garyguo.net \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=zhiw@nvidia.com \
    /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