Linux PCI subsystem development
 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: 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,
	zhiw@nvidia.com, 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/2] vfio/nova-vgpu: add the Rust VFIO variant driver
Date: Sat, 05 Dec 2026 14:50:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-251658@kernel.org> (raw)
In-Reply-To: <20261204144900.286240-3-xinmengs@nvidia.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] vfio/nova-vgpu: drop() tears down vGPU before DMA-BUF
  revocation and disable
- [Medium] vfio/nova-vgpu: zero-sized BAR1 retains non-zero PCI and
  region flags
- [Medium] vfio/nova-vgpu: write() updates fb_bar_reg before
  core_write() succeeds
- [Medium] vfio/nova-vgpu: race on reset_error in NovaVgpuOps::ioctl()
--

commit 21acf5012ea5af1ae03f8ee86a48cf46eb6adc1e
Author: Danilo Krummrich <dakr@kernel.org>

vfio/nova-vgpu: add the Rust VFIO variant driver

This commit adds a Rust VFIO PCI variant driver for NVIDIA vGPU VFs that
manages vGPU instances through nova-core, enforces the assigned BAR1
aperture, and resets the active vGPU instance on PCI reset.

> diff --git a/drivers/vfio/pci/nova-vgpu/nova_vgpu.rs b/drivers/vfio/pci/nova-vgpu/nova_vgpu.rs
> new file mode 100644
> index 0000000000000..21d3ad70c84ef
> --- /dev/null
> +++ b/drivers/vfio/pci/nova-vgpu/nova_vgpu.rs
> @@ -0,0 +1,522 @@

[ ... ]

> +impl Drop for NovaVgpuOpenData<'_, '_> {
> +    fn drop(&mut self) {
> +        let mut state = self.registration.lock_instance();
> +        state.active = false;
> +        drop(self.instance.take());
> +    }
> +}

[Severity: High]
Does dropping self.instance here tear down the vGPU and free its VRAM slot
while exported BAR1 DMA-BUFs and VF bus mastering are still active?

In rust/kernel/vfio/pci.rs:close_device_cb(), OpenData is dropped before
vfio_pci_core_close_device() runs:

rust/kernel/vfio/pci.rs:close_device_cb() {
    let dev = unsafe { Device::<T>::from_raw_vfio(vdev) };
    unsafe { dev.close() };
    unsafe { bindings::vfio_pci_core_close_device(vdev) };
}

Because dmabuf_len() enables VFIO_DEVICE_FEATURE_DMA_BUF and exported
DMA-BUFs only pin the device registration, userspace can close the last VFIO
fd while a peer P2P importer still has BAR1 mapped.

InstanceRegistry::destroy_instance() then scrubs guest VRAM and returns
VgpuVramSlot to the shared PF pool before vfio_pci_dma_buf_cleanup() revokes
peer mappings or vfio_pci_core_disable() clears bus mastering, risking
cross-tenant VRAM corruption or disclosure if another VF allocates the slot.

[ ... ]

> +    fn ioctl<'res>(
> +        dev: &vfio::pci::Device<Self, Ioctl>,
> +        rd: &NovaVgpuRegData<'res>,
> +        open_data: Pin<&Self::OpenData<'_, 'res>>,
> +        cmd: u32,
> +        arg: usize,
> +    ) -> Result<isize> {
> +        // The core checks ioeventfd bounds against the physical BAR and writes
> +        // without calling write(), bypassing the vGPU's bar1_limit() check.
> +        // Checking a copy here could race with the core's later copy from userspace.
> +        // On rejection, QEMU falls back to its userspace eventfd handler, whose
> +        // writes pass through write() and bar1_limit().
> +        if cmd == vfio::DEVICE_IOEVENTFD && open_data.instance.is_some() {
> +            return Err(ENOTTY);
> +        }
> +        let result = dev.core_ioctl(cmd, arg)?;
> +        if cmd == vfio::DEVICE_RESET {
> +            if let Some(error) = rd.state.lock().reset_error {
> +                return Err(error);
> +            }
> +        }
> +        Ok(result)
> +    }

[Severity: Medium]
Can reading rd.state.lock().reset_error after dev.core_ioctl() returns race
with a concurrent PCI reset and misattribute or clobber the reset error?

vfio_pci_ioctl_reset() releases pci_dev_lock() and vdev->memory_lock before
returning to ioctl().  In that gap, another VFIO_DEVICE_RESET, a
config-space FLR write, or a host sysfs reset can enter reset_prepare() and
overwrite the single shared state.reset_error field:

CPU0                              CPU1
NovaVgpuOps::ioctl(DEVICE_RESET)
  dev.core_ioctl()
    vfio_pci_ioctl_reset()
      reset_prepare()
        reset_error = ETIMEDOUT
      pci_dev_unlock()
      up_write(memory_lock)
                                  pci_reset_function()
                                    reset_prepare()
                                      vgpu.api.reset() -> EBUSY
                                      reset_error = EBUSY
  rd.state.lock().reset_error <-- returns EBUSY, losing ETIMEDOUT

Because InstanceRegistry::reset_instance() latches the first failure and
returns EBUSY on subsequent calls, a racing reset overwrites the real
firmware error with EBUSY.  Similarly, if CPU0's reset succeeded and CPU1's
reset fails in the gap, CPU0 can return CPU1's failure for its own reset.

[ ... ]

> +                    if let (Some(size), Some(fb_bar)) = (open_data.bar1_size()?, rd.fb_bar) {
> +                        let reg = rd.state.lock().fb_bar_reg & !size.wrapping_sub(1);
> +                        // Byte 0 holds only the flag bits, which the core has filled in.
> +                        overrides.apply(
> +                            u64::from(bindings::PCI_BASE_ADDRESS_0) + 4 * u64::from(fb_bar) + 1,
> +                            &reg.to_le_bytes()[1..],
> +                        )?;
> +                    }

[Severity: Medium]
When bar1_size() returns Some(0) for a vGPU with bar1_length == 0 on a VF
with a non-zero physical BAR1, does skipping byte 0 leave non-zero PCI BAR
attribute flags in config space?

dev.core_read() runs vfio_bar_fixup(), which populates byte 0 with the
physical BAR's type and prefetchable flags.  Applying the override starting
at byte 1 leaves those flags set alongside an all-zero size mask instead of
0x0 for an unimplemented BAR, breaking standard PCI BAR sizing.

NovaVgpuOps::get_region_info() has the same mismatch when size == 0: it sets
info.size to 0 without clearing the READ/WRITE/MMAP flags populated by
vfio_pci_ioctl_get_region_info(), violating the convention that zero-sized
BAR regions report flags == 0.

[ ... ]

> +    fn write<'res>(
> +        dev: &vfio::pci::Device<Self, Write>,
> +        rd: &NovaVgpuRegData<'res>,
> +        open_data: Pin<&Self::OpenData<'_, 'res>>,
> +        buf: UserSlice,
> +        position: &mut Position<'_>,
> +    ) -> Result<isize> {
> +        let mut reader = buf.reader();
> +        if let Some(fb_bar) = rd.fb_bar {
> +            if position.region_index() == vfio::pci::CONFIG_REGION_INDEX {
> +                let mut written = [0; 8];
> +                let range = peek_write(
> +                    &reader,
> +                    position.region_offset(),
> +                    u64::from(bindings::PCI_BASE_ADDRESS_0) + 4 * u64::from(fb_bar),
> +                    &mut written,
> +                )?;
> +                if !range.is_empty() {
> +                    let mut state = rd.state.lock();
> +                    let mut reg = state.fb_bar_reg.to_le_bytes();
> +                    reg[range.clone()].copy_from_slice(&written[range]);
> +                    state.fb_bar_reg = u64::from_le_bytes(reg);
> +                }
> +            }
> +        }
> +        if let Some(limit) = open_data.bar1_limit(reader.len(), position)? {
> +            reader.truncate(limit);
> +        }
> +        dev.core_write(&reader, position)
> +    }

[Severity: Medium]
Can a failed config-space write corrupt state.fb_bar_reg because it is
updated before dev.core_write() runs?

If a write overlapping BAR1 extends past pdev->cfg_size,
vfio_pci_config_rw_single() rejects it with -EFAULT without modifying
vconfig, but state.fb_bar_reg has already been overwritten and subsequent
BAR1 reads will return values from the failed write.

In addition, if position.region_offset() + reader.len() overflows u64,
peek_write() returns EOVERFLOW instead of letting core_write() return
EFAULT.  Deferring the state.fb_bar_reg update until after dev.core_write()
succeeds (clamped to the returned byte count) would avoid both problems.

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

      parent reply	other threads:[~2026-12-05 14:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 14:48 [PATCH v5 0/2] vfio/nova-vgpu: add the NVIDIA VFIO variant driver Simon Song
2026-12-04 14:48 ` [PATCH v5 1/2] gpu: nova-core: publish typed SR-IOV PF APIs Simon Song
2026-12-05 14:50   ` sashiko-bot
2026-12-04 14:48 ` [PATCH v5 2/2] vfio/nova-vgpu: add the Rust VFIO variant driver Simon Song
2026-12-04 23:12   ` Alex Williamson
2026-12-04 23:28     ` Danilo Krummrich
2026-12-04 23:37       ` Alex Williamson
2026-12-05  0:09         ` Danilo Krummrich
2026-12-05 14:50   ` sashiko-bot [this message]

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-251658@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox