All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Simon Song" <xinmengs@nvidia.com>,
	dakr@kernel.org, acourbot@nvidia.com, kvm@vger.kernel.org,
	nova-gpu@lists.linux.dev, linux-pci@vger.kernel.org
Cc: "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, 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 v5 2/4] rust: vfio: add common VFIO device handling
Date: Sat, 05 Dec 2026 04:19:05 +0000	[thread overview]
Message-ID: <sashiko-outbox-251064@kernel.org> (raw)
In-Reply-To: <20261204041727.4021092-3-xinmengs@nvidia.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Low] rust: vfio: missing #[inline] on small DeviceBase helpers
- [Low] rust: vfio: DeviceBase::callback_data() bypasses
  ForeignOwnable::borrow()
- [Low] rust: vfio: AlwaysRefCounted reaches into private
  vfio_device::device field
--

commit 00c5e0b75ff0082149fe02d0ba3c927a81f1b2c9
Author: Zhi Wang <zhiw@nvidia.com>

rust: vfio: add common VFIO device handling

This commit adds the core Rust VFIO device abstraction (DeviceBase and
Device), along with callback context types, registration and open data
storage, reference counting, and InfoCap.

> diff --git a/rust/kernel/vfio.rs b/rust/kernel/vfio.rs
> new file mode 100644
> index 0000000000000..30a593634a72b
> --- /dev/null
> +++ b/rust/kernel/vfio.rs
> @@ -0,0 +1,357 @@

[ ... ]

> +    unsafe fn init(this: *mut Self) {
> +        // SAFETY: The caller provides exclusive access to a suitable allocation.
> +        // INVARIANT: No data is published, and no open data exists.
> +        unsafe {
> +            (&raw mut (*this).data).write(CallbackData {
> +                reg_data: UnsafeCell::new(None),
> +                open_data: UnsafeCell::new(None),
> +            })
> +        };
> +        // SAFETY: By the same reasoning above, `_context` can be written.
> +        unsafe { (&raw mut (*this)._context).write(PhantomData) };
> +    }

[Severity: Low]
Should DeviceBase::init(), DeviceBase::publish_registration_data(), and
DeviceBase::callback_data() be marked #[inline]?  Kernel crate convention is
to mark small abstraction helpers #[inline], matching the other small
helpers in this module.

[ ... ]

> +        let open_data = unsafe { *self.data.open_data.get() }
> +            .expect("VFIO OpenData is expected to be populated.");
> +        // SAFETY: By the same reasoning above, the registration data is valid for this callback.
> +        let reg_data = unsafe { reg_data.as_ref() };
> +        // SAFETY: By the same reasoning above, the open data is valid for this callback.
> +        let open_data = unsafe { open_data.as_ref() };
> +        // SAFETY: By the same reasoning above, the open data is pinned until the last close.
> +        (reg_data, unsafe { Pin::new_unchecked(open_data) })

[Severity: Low]
Should callback_data() borrow open_data via
Pin::<KBox<T::OpenData<'static, 'static>>>::borrow() instead of
dereferencing the raw pointer directly?

DeviceBase::open() produces this pointer with
ForeignOwnable::into_foreign(), which documents that using the pointer
outside from_foreign(), try_from_foreign(), borrow(), or borrow_mut() can
cause undefined behavior.

Calling borrow(open_data.as_ptr().cast()) matches close()'s use of
from_foreign() and avoids relying on Pin<KBox<T>>'s internal pointer
representation.

[ ... ]

> +// SAFETY: VFIO uses the embedded class device's refcount to own the full allocation.
> +unsafe impl<Raw, T: Operations> AlwaysRefCounted for DeviceBase<Raw, T> {
> +    #[inline]
> +    fn inc_ref(&self) {
> +        // SAFETY: A shared reference keeps the initialized VFIO device alive.
> +        unsafe { bindings::get_device(&raw mut (*self.as_raw_vfio()).device) };
> +    }
> +
> +    #[inline]
> +    unsafe fn dec_ref(obj: NonNull<Self>) {
> +        // By the type invariants, `Raw` begins with its `vfio_device`, and `raw` is the first
> +        // field of `Self`.
> +        let vdev = obj.as_ptr().cast::<bindings::vfio_device>();
> +        // SAFETY: The caller owns the VFIO reference being released.
> +        unsafe { bindings::put_device(&raw mut (*vdev).device) };
> +    }
> +}

[Severity: Low]
Should AlwaysRefCounted avoid reaching into struct vfio_device::device
directly?

In include/linux/vfio.h, vfio_device::device is marked private and not for
driver use, and vfio_put_device() is already the public release helper.
Bypassing it risks silently diverging if VFIO changes its internal
refcounting or adds logic to vfio_put_device().

Adding a paired vfio_get_device() helper in include/linux/vfio.h and
wrapping both via rust/helpers/vfio.c would keep this within the public VFIO
API.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204041727.4021092-1-xinmengs@nvidia.com?part=2

  reply	other threads:[~2026-12-05  4:19 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  4:17 [PATCH v5 0/4] Rust VFIO PCI support Simon Song
2026-12-04  4:17 ` [PATCH v5 1/4] rust: uaccess: support bounded forward buffer traversal Simon Song
2026-12-04  4:17 ` [PATCH v5 2/4] rust: vfio: add common VFIO device handling Simon Song
2026-12-05  4:19   ` sashiko-bot [this message]
2026-12-04  4:17 ` [PATCH v5 3/4] rust: mm: add VmaRef::size() and VmaRef::pgoff() Simon Song
2026-12-04  4:17 ` [PATCH v5 4/4] rust: vfio: add PCI variant driver abstractions Simon Song
2026-12-05  4:19   ` sashiko-bot
2026-12-05  4:19 ` [PATCH v5 0/4] Rust VFIO PCI support sashiko-bot

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=sashiko-outbox-251064@kernel.org \
    --to=sashiko-bot@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=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=liam@infradead.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=sashiko-reviews@lists.linux.dev \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.