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.