From: Simon Song <xinmengs@nvidia.com>
To: Danilo Krummrich <dakr@kernel.org>
Cc: 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, gary@garyguo.net, 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
Subject: Re: [PATCH v3 6/9] rust: vfio: add common VFIO device handling
Date: Thu, 26 Nov 2026 10:45:13 -0500 [thread overview]
Message-ID: <227509f2-834f-44fa-926f-110c735b30bd@nvidia.com> (raw)
In-Reply-To: <DN4HIQ5VPWEI.FFVR53SNX54Y@kernel.org>
On 11/25/2026 9:57 AM, Danilo Krummrich wrote:
> On Thu Nov 19, 2026 at 10:50 PM CET, Simon Song wrote:
>> +/// Maps a C device structure to its embedded VFIO device.
>> +///
>> +/// # Safety
>> +///
>> +/// Implementations must return the address of the `vfio_device` at the beginning of `Self`,
>
> I think "at the beginning of `Self`" is not a safety requirement for this trait,
> it's an implementation detail of the current implementation on the C side, that
> could be a type invariant of DeviceBase.
>
> Besides that, I think the abstraction we currently need with DeviceBase and
> RawDevice (which I proposed to deal with the current implementation) could be
> simplified if the C code would be more flexible regarding the layout of a struct
> subclassing a struct vfio_device.
>
> However, this could be a subsequent consideration and nothing that needs to be
> tackled now; it is mostly an implementation detail of this abstraction.
Agreed, we will leave the C side change as is for this series.
>> +#[expect(dead_code)]
>> +impl<Raw: RawDevice, R, O, T, Ctx: DeviceContext> DeviceBase<Raw, R, O, T, Ctx> {
>> + /// Initializes the Rust fields of a newly allocated device.
>> + ///
>> + /// # Safety
>> + ///
>> + /// `this` must point to an allocation with the size and alignment of `Self`, whose `raw`
>> + /// has been initialized. Nothing else may access the allocation during this call.
>> + unsafe fn init(this: *mut Self) {
>> + // SAFETY: The caller provides exclusive access to a suitable allocation.
>> + unsafe {
>> + (&raw mut (*this).data).write(CallbackData {
>> + reg_data: UnsafeCell::new(None),
>> + open_data: UnsafeCell::new(core::ptr::null_mut()),
>> + });
>> + (&raw mut (*this)._context).write(PhantomData);
>> + }
>> + }
>> +
>> + /// Recovers `&Self` from the `vfio_device` VFIO passes to a callback.
>> + ///
>> + /// # Safety
>> + ///
>> + /// `vdev` must point to the `vfio_device` at the beginning of a `Self` that remains alive
>> + /// for `'a`. The caller must provide the guarantees of `Ctx` throughout the borrow.
>> + unsafe fn from_raw_vfio<'a>(vdev: *mut bindings::vfio_device) -> &'a Self {
>> + // SAFETY: `Raw` begins with its `vfio_device` and is the first field of `Self`. The
>> + // caller guarantees validity and the context.
>> + unsafe { &*vdev.cast() }
>
> If we rely on this layout guarantee here, there's no point to have the RawDevice
> trait dealing with the layout.
>
> Either we add from_raw_vfio() to RawDevice, or we just remove RawDevice and make
> the layout guarantee a type invariant.
>
> Either is fine with me, but maybe the RawDevice trait is a bit overengineered
> and a type invaraint is sufficient.
I will remove RawDevice, then DeviceBase gets the Raw which is the C struct that always begins
with its struct vfio_device.
Then as_raw_vfio() function can use the same .cast() like from_raw_vfio()
next prev parent reply other threads:[~2026-11-26 15:45 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 21:50 [PATCH v3 0/9] Rust VFIO PCI support and NVIDIA vGPU driver Simon Song
2026-11-19 21:50 ` [PATCH v3 1/9] rust: pci: add VFIO override device IDs Simon Song
2026-11-19 21:50 ` [PATCH v3 2/9] rust: pci: expose the VF index within its PF Simon Song
2026-11-19 21:50 ` [PATCH v3 3/9] rust: pci: allow drivers to manage DMA ownership Simon Song
2026-11-19 21:50 ` [PATCH v3 4/9] rust: pci: add reset callbacks Simon Song
2026-11-19 21:50 ` [PATCH v3 5/9] rust: pci: add an error_detected callback Simon Song
2026-11-19 21:50 ` [PATCH v3 6/9] rust: vfio: add common VFIO device handling Simon Song
2026-11-25 17:57 ` Danilo Krummrich
2026-11-26 15:45 ` Simon Song [this message]
2026-11-19 21:50 ` [PATCH v3 7/9] rust: vfio: add PCI variant driver abstractions Simon Song
2026-11-25 23:51 ` Danilo Krummrich
2026-11-26 15:01 ` Simon Song
2026-11-26 15:15 ` Alice Ryhl
2026-11-19 21:50 ` [PATCH v3 8/9] gpu: nova-core: publish typed SR-IOV PF APIs Simon Song
2026-11-19 21:50 ` [PATCH v3 9/9] vfio/nova-vgpu: add the Rust VFIO variant driver Simon Song
2026-11-25 3:19 ` Alex Williamson
2026-11-26 2:45 ` Simon Song
2026-11-26 6:08 ` Alex Williamson
2026-11-24 22:21 ` (subset) [PATCH v3 0/9] Rust VFIO PCI support and NVIDIA vGPU driver 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=227509f2-834f-44fa-926f-110c735b30bd@nvidia.com \
--to=xinmengs@nvidia.com \
--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=dakr@kernel.org \
--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=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.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=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
all inboxes | Powered by JetHome®