[Devel] [PATCH hci-8.1 v3 1/1] vfio: take the kvm reference only for drivers that read device->kvm

Konstantin Khorenko khorenko at virtuozzo.com
Thu Nov 5 20:51:18 MSK 2026


Applying with these fixes on my side:
- commit message: "all in open_device(), the first and the last at
  close too" becomes "vfio_ap reads it only in open_device(), zdev
  also at close, KVMGT also at close and while the guest runs";
- struct vfio_device comment: "reads device->kvm in open_device()"
  becomes "uses device->kvm while the device is open";
- the comments before the kvm get in vfio_df_group_open() and
  vfio_df_ioctl_bind_iommufd(): "Save the pointer in the device for
  use by drivers." is followed by "Both only if the driver set
  needs_kvm_ref.", since the flag gates the reference too.

On Thu, 5 Nov 2026 16:17:59 +0200, Andrey Drobyshev wrote:
> Only three drivers read device->kvm: KVMGT, vfio_ap and vfio-pci's
> zdev support, all in open_device(), the first and the last at close
> too. Let them say so with needs_kvm_ref. Without it device->kvm stays
> NULL and no reference is taken, so the device can't keep a VM alive.

KVMGT write-protects guest pages through device->kvm while the guest
runs, in intel_gvt_page_track_add() and intel_gvt_page_track_remove().
The list of readers is right, only "in open_device()" is too narrow.
That came from item 1 of my v2 reply, which was wrong for KVMGT.

> +	 * Set before registration by a driver that reads device->kvm in
> +	 * open_device(): the core then keeps the KVM the device was opened
> +	 * with referenced until close_device(). Without it device->kvm
> +	 * stays NULL.

The same for the comment: a future driver that reads the pointer only
from an ioctl would follow "in open_device()", leave the flag unset and
get NULL.

The two open paths still say the pointer is always saved for drivers,
which is no longer true without the flag.

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team



More information about the Devel mailing list