From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796234112; b=aR6/OAu9VkqCaiRvy1lPXjQSLXta816I5pr7tL3KQr4CYeAqfi63ibLtclBz5/joqlqG SpzPVZxuKHWZ0ewRRsxvlQgC5h88jO5BTtCeBAOTyff/No6k4s9X6ZbVhY1AcfpQCYnHx STMRlIHw77nruRiLG8GU39axU76D9hAs0hV0omfac/z1tI9GlGPJJfP19y3qgkOMu1geY ZYUYTP5iMHKPEZj/wYIDKhdeEcr/qAPvqE7Hlo819kzTdiOJM5QeE6wTGwnUM3Z3bGOpA oswC44D44jaBOGoKJTmP7bXCYPRRc20pGEwPkGON0G/1DK+ghWPWHBxhgfIfh+4o1AA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796234112; h=Received:DKIM-Signature:Mime-Version:Content-Transfer-Encoding: Content-Type:Date:Message-Id:To:From:Subject:Cc:References: In-Reply-To; bh=4GcqlQ1xm6/uoJcUH9kVMVi2YK0Bt1yqxHLsYnD+mzk=; b=5ngeIuPtrKyTUrrE0LMUbd6sLYnzrT4RIvvNti3xq8+blwSdvOZZ7yX9Tbxe6PBaTvb6 /teku2BUhWh7MT9nZEnCDA4dKe+0eUX5Luy97PxZGtvEjUPoF1u8wzIiHUjLKVCc1lMWu hjdhqsavONain3JroMSp9eDjECD4prvNPRHfMeTTzsemvSbHsvn+A7wuU5bHBHEpCZHu3 YjMCeBU7/c3aGuU7q6fFvo+nF7egf4DWEbz9qNF1ZvhpMAEnp5PdF8xX1dLjgbble3FVv iCvHI/iHIo5rSiz2f/+2LsNquej4HCRTgAl8EMjDC8USFSSvHdT9j6grwxBZqtfDasA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Vb4llxWk; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B166606A32C; Wed, 2 Dec 2026 17:55:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Vb4llxWk Received: by smtp.kernel.org (Postfix) with ESMTPSA id EDFAE1F00893; Wed, 2 Dec 2026 17:55:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796234111; bh=4GcqlQ1xm6/uoJcUH9kVMVi2YK0Bt1yqxHLsYnD+mzk=; h=Date:To:From:Subject:Cc:References:In-Reply-To; b=Vb4llxWksLxYDT890AIeohfgsiWKX5erZnYchNfEKvZW6md2QipLumc5HZf6kvoiY d5D2lBXJwjafDEGBTEnW5CX0Lip7RqWI5UnAYEm/RMhZoQM5hheqytIU624lE6PK9D NkUuFr4dVWv+nnWecCnTBup4r/3/A37mHDO82eoOyS0F5PDxrA3OyPRTlifALyda3f K2kjMhCI4K6ML9Bq1Gykv17YkMYTlGi64sOtrUMw0jcFU47KFEo1JhVl1AqOFm0rDA r05oXvRD7Qwr5/nnUVB95vhVsXD8lGmyxCiV/2GBxMPihfmQMQgIIShdun74kBqplN 2mRWYswBV4xWg== X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Wed, 02 Dec 2026 18:55:01 +0100 Message-Id: To: "Gary Guo" From: "Danilo Krummrich" Subject: Re: [PATCH v4 1/5] rust: vfio: add common VFIO device handling Cc: "Simon Song" , , , , , , "Zhi Wang" , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , "Liam R. Howlett" , , "Lorenzo Stoakes" References: <20261127224928.1511382-1-xinmengs@nvidia.com> <20261127224928.1511382-2-xinmengs@nvidia.com> In-Reply-To: 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 conve= ntion eventually. Originally the convention that is also used here was the predom= inant one (and it probably still is, I didn't check). We got more of the mod.rs o= nes 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 t= he 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 Registratio= nData is constrained by and 'open is the lifetime OpenData is constrained by; sin= ce 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 b= e called 'res. I'm fine with both; but in case of the latter I'd probably prefer 'res over= 'a. >> +#[expect(dead_code)] >> +impl DeviceBase { >> + /// Publishes the registration data passed to callbacks. >> + /// >> + /// Returns [`EBUSY`] if registration data has been published befor= e, so that a device can >> + /// be registered at most once. >> + /// >> + /// # Safety >> + /// >> + /// The caller must serialize calls for this device. `data` must po= int to pinned data that >> + /// remains valid until the device has been unregistered or, if reg= istration fails, is not >> + /// accessed through this device. >> + unsafe fn publish_registration_data<'reg>( >> + &self, >> + data: NonNull>, >> + ) -> Result { >> + // SAFETY: The caller serializes publication, and callbacks onl= y 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()` t= wice. > But this raises the question on why you'd want the user to create the dev= ice > 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 regist= ration > and expose it to the user via registration only. > > - (drm approach) If there're some configuration needed on the device befo= re > 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 no= w. >> +/// 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 =E2=80=94 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. Thi= s looks > like like it should just be `User<[u8]>` (the generic user pointer type t= hat > integrates with I/O projection); I have that in my plan and I can priorit= ize it. IIRC, I did bring this up previously too. I don't think adding the User typ= e should be a blocker for this series though. We should be able to pass a UserSlice in the callbacks and take it from the= re 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, // + }, // +}; =20 /// A pointer into userspace. /// @@ -217,6 +223,16 @@ pub struct UserSliceReader { } =20 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 =3D 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 { } =20 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 =3D 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 =3D self.length.checked_sub(num_skip).ok_or(EFAULT)?; + self.ptr =3D 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.