From: "Danilo Krummrich" <dakr@kernel.org>
To: "Gary Guo" <gary@garyguo.net>
Cc: "Simon Song" <xinmengs@nvidia.com>, <acourbot@nvidia.com>,
<kvm@vger.kernel.org>, <rust-for-linux@vger.kernel.org>,
<nova-gpu@lists.linux.dev>, <linux-pci@vger.kernel.org>,
"Zhi Wang" <zhiw@nvidia.com>, <alex@shazbot.org>,
<jgg@nvidia.com>, <yishaih@nvidia.com>, <skolothumtho@nvidia.com>,
<kevin.tian@intel.com>, <airlied@gmail.com>, <simona@ffwll.ch>,
<ojeda@kernel.org>, <alex.gaynor@gmail.com>,
<boqun.feng@gmail.com>, <bjorn3_gh@protonmail.com>,
<lossin@kernel.org>, <a.hindborg@kernel.org>,
<aliceryhl@google.com>, <tmgross@umich.edu>,
<jhubbard@nvidia.com>, <ecourtney@nvidia.com>, <cjia@nvidia.com>,
<smitra@nvidia.com>, <kjaju@nvidia.com>, <alkumar@nvidia.com>,
<ankita@nvidia.com>, <aniketa@nvidia.com>, <kwankhede@nvidia.com>,
<targupta@nvidia.com>, <linux-kernel@vger.kernel.org>,
<zhiwang@kernel.org>, <boqun@kernel.org>,
<daniel.almeida@collabora.com>, <tamird@kernel.org>,
<work@onurozkan.dev>, <jgg@ziepe.ca>, <bhelgaas@google.com>,
<kwilczynski@kernel.org>, <dri-devel@lists.freedesktop.org>,
<peter@colberg.org>, "Liam R. Howlett" <liam@infradead.org>,
<linux-mm@kvack.org>, "Lorenzo Stoakes" <ljs@kernel.org>
Subject: Re: [PATCH v4 1/5] rust: vfio: add common VFIO device handling
Date: Wed, 02 Dec 2026 18:55:01 +0100 [thread overview]
Message-ID: <DNAFUIDQJEFT.36BDC1KS4OQZ4@kernel.org> (raw)
In-Reply-To: <DNADDE14I0HB.3TQR1MAEOW81T@garyguo.net>
On Wed Dec 2, 2026 at 4:58 PM CET, Gary Guo wrote:
> On Fri Nov 27, 2026 at 10:49 PM GMT, Simon Song wrote:
>> rust/bindings/bindings_helper.h | 2 +
>> rust/kernel/lib.rs | 2 +
>> rust/kernel/vfio.rs | 454 ++++++++++++++++++++++++++++++++
>
> I'd prefer this to be placed in vfio/mod.rs so it's in the same directory as vfio/pci.rs.
This is independent of this series, but I think we should settle on a convention
eventually. Originally the convention that is also used here was the predominant
one (and it probably still is, I didn't check). We got more of the mod.rs ones
recently though.
Personally, I prefer the former, but I'd even more prefer consistency.
>> +/// The data the driver of a [`Device`] shares with its callbacks.
>> +///
>> +/// Bus-specific operations traits extend this trait.
>> +pub trait Operations: Sized + Send + Sync + 'static {
>> + /// Data owned by the registration and passed to VFIO callbacks.
>> + ///
>> + /// It can borrow resources of the driver's binding for `'reg`. Per-device state lives here.
>> + type RegistrationData<'reg>: Send + Sync + 'reg;
>> +
>> + /// Data shared by callbacks from the first successful open until the last close.
>> + ///
>> + /// It can borrow the registration data, of lifetime `'reg`, for `'open`.
>> + type OpenData<'open, 'reg: 'open>: Send + Sync + 'open;
>
> The lifetime here is confusing. `'open` is actually the lifetime borrowed from
> `'reg`, and the `'reg` is lifetime borrowed (usually) from the device.
I think it's reasonable if this is read as 'reg is the lifetime RegistrationData
is constrained by and 'open is the lifetime OpenData is constrained by; since
OpenData may borrow RegistrationData, we require 'reg: 'open.
With that reading, I think the above shouldn't say "the registration data, of
lifetime `'reg`" though, as this is misleading.
That said, I can see why you say it is confusing if you read it as "borrows".
In this case we'd probably want something like
type RegistrationData<'a>: Send + Sync + 'a;
type OpenData<'reg, 'a: 'reg>: Send + Sync + 'reg;
'a is usually the lifetime of borrowed device resources, so it could also be
called 'res.
I'm fine with both; but in case of the latter I'd probably prefer 'res over 'a.
>> +#[expect(dead_code)]
>> +impl<Raw, T: Operations> DeviceBase<Raw, T> {
>> + /// Publishes the registration data passed to callbacks.
>> + ///
>> + /// Returns [`EBUSY`] if registration data has been published before, so that a device can
>> + /// be registered at most once.
>> + ///
>> + /// # Safety
>> + ///
>> + /// The caller must serialize calls for this device. `data` must point to pinned data that
>> + /// remains valid until the device has been unregistered or, if registration fails, is not
>> + /// accessed through this device.
>> + unsafe fn publish_registration_data<'reg>(
>> + &self,
>> + data: NonNull<T::RegistrationData<'reg>>,
>> + ) -> Result {
>> + // SAFETY: The caller serializes publication, and callbacks only read the slot.
>> + if unsafe { (*self.data.reg_data.get()).is_some() } {
>> + return Err(EBUSY);
>> + }
>
> This looks quite strange.
>
> I believe this is only here so you can never call `Registration::new()` twice.
> But this raises the question on why you'd want the user to create the device
> first before giving it to the registration.
>
> I think this should be one of the either approach:
>
> - Never allow user to create the device directly. Create it inside registration
> and expose it to the user via registration only.
>
> - (drm approach) If there're some configuration needed on the device before
> registering, create a `UnregisteredDevice` (or `UnpublishedRegistration`)
> type. It is then converted to a `Registration`.
I think this is fair and in my PoC I did it that way to make it a bit more
accessible reading it coming from C; I think it should be the former for now.
>> +/// Opaque wrapper around a `char __user *` buffer from a VFIO read or write callback.
>> +///
>> +/// Provides bounds-checked copying of kernel data into the user-space buffer.
>> +///
>> +/// This type cannot be constructed by driver code — it is only created by the
>> +/// callback trampoline.
>> +pub struct UserBuf {
>> + ptr: UserPtr,
>> + count: usize,
>> +}
>
> No ad-hoc infrastructure for what's supposed to be in uaccess please. This looks
> like like it should just be `User<[u8]>` (the generic user pointer type that
> integrates with I/O projection); I have that in my plan and I can prioritize it.
IIRC, I did bring this up previously too. I don't think adding the User type
should be a blocker for this series though.
We should be able to pass a UserSlice in the callbacks and take it from there
with UserSliceReader and UserSliceWriter, e.g. with core_read() taking a
UserSliceWriter as argument.
The only change we need in uaccess is something like this:
diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
index f1aecda7a90c..5e95d017b52e 100644
--- a/rust/kernel/uaccess.rs
+++ b/rust/kernel/uaccess.rs
@@ -15,7 +15,13 @@
ptr::KnownSize,
transmute::{AsBytes, FromBytes},
};
-use core::mem::{size_of, MaybeUninit};
+use core::{
+ cmp,
+ mem::{
+ size_of,
+ MaybeUninit, //
+ }, //
+};
/// A pointer into userspace.
///
@@ -217,6 +223,16 @@ pub struct UserSliceReader {
}
impl UserSliceReader {
+ /// Returns the current userspace pointer.
+ pub(crate) fn as_ptr(&self) -> UserPtr {
+ self.ptr
+ }
+
+ /// Limits the remaining buffer to at most `length` bytes, keeping its current address.
+ pub fn truncate(&mut self, length: usize) {
+ self.length = cmp::min(self.length, length);
+ }
+
/// Skip the provided number of bytes.
///
/// Returns an error if skipping more than the length of the buffer.
@@ -449,6 +465,25 @@ pub struct UserSliceWriter {
}
impl UserSliceWriter {
+ /// Returns the current userspace pointer.
+ pub(crate) fn as_ptr(&self) -> UserPtr {
+ self.ptr
+ }
+
+ /// Limits the remaining buffer to at most `length` bytes, keeping its current address.
+ pub fn truncate(&mut self, length: usize) {
+ self.length = cmp::min(self.length, length);
+ }
+
+ /// Skips the provided number of bytes without writing to them.
+ ///
+ /// Returns an error if skipping more than the length of the buffer.
+ pub fn skip(&mut self, num_skip: usize) -> Result {
+ self.length = self.length.checked_sub(num_skip).ok_or(EFAULT)?;
+ self.ptr = self.as_ptr().wrapping_byte_add(num_skip);
+ Ok(())
+ }
+
/// Returns the amount of space remaining in this buffer.
///
/// Note that even writing less than this number of bytes may fail.
next prev parent reply other threads:[~2026-12-02 17:55 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 22:49 [PATCH v4 0/5] Rust VFIO PCI support and NVIDIA vGPU driver Simon Song
2026-11-27 22:49 ` [PATCH v4 1/5] rust: vfio: add common VFIO device handling Simon Song
2026-12-02 15:58 ` Gary Guo
2026-12-02 17:55 ` Danilo Krummrich [this message]
2026-12-02 18:43 ` Gary Guo
2026-12-02 19:20 ` Danilo Krummrich
2026-11-27 22:49 ` [PATCH v4 2/5] rust: mm: add VmaRef::size() and VmaRef::pgoff() Simon Song
2026-11-27 22:49 ` [PATCH v4 3/5] rust: vfio: add PCI variant driver abstractions Simon Song
2026-12-02 16:19 ` Gary Guo
2026-11-27 22:49 ` [PATCH v4 4/5] gpu: nova-core: publish typed SR-IOV PF APIs Simon Song
2026-11-27 22:49 ` [PATCH v4 5/5] vfio/nova-vgpu: add the Rust VFIO variant driver Simon Song
2026-12-02 14:01 ` [PATCH v4 0/5] Rust VFIO PCI support and NVIDIA vGPU driver Gary Guo
2026-12-02 14:14 ` Gary Guo
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=DNAFUIDQJEFT.36BDC1KS4OQZ4@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=alex@shazbot.org \
--cc=aliceryhl@google.com \
--cc=alkumar@nvidia.com \
--cc=aniketa@nvidia.com \
--cc=ankita@nvidia.com \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=boqun@kernel.org \
--cc=cjia@nvidia.com \
--cc=daniel.almeida@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=ecourtney@nvidia.com \
--cc=gary@garyguo.net \
--cc=jgg@nvidia.com \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=kevin.tian@intel.com \
--cc=kjaju@nvidia.com \
--cc=kvm@vger.kernel.org \
--cc=kwankhede@nvidia.com \
--cc=kwilczynski@kernel.org \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-pci@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=lossin@kernel.org \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=peter@colberg.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=skolothumtho@nvidia.com \
--cc=smitra@nvidia.com \
--cc=tamird@kernel.org \
--cc=targupta@nvidia.com \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
--cc=xinmengs@nvidia.com \
--cc=yishaih@nvidia.com \
--cc=zhiw@nvidia.com \
--cc=zhiwang@kernel.org \
/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