[PATCH RFC v3 55/88] vfio: Track device file access mode with an enum

From: Ackerley Tng via B4 Relay

Date: Fri Nov 06 2026 - 19:51:03 EST


From: Ackerley Tng <ackerleytng@xxxxxxxxxx>

Device file operations currently track whether access has been granted
using a boolean flag, assuming that binding a device always unlocks both
control-plane operations and direct region access simultaneously.

Upcoming support for control-only device bindings requires distinguishing
unbound descriptors from descriptors bound with restricted vs. full
access, without introducing multiple flags that could get out of sync or
represent invalid combinations under lockless release-acquire ordering.

Replace the boolean flag with an enum representing the access mode so
that the entire access state continues to be published and read as a
single release-acquire value.

No functional change intended.

Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
---
drivers/vfio/device_cdev.c | 8 ++++----
drivers/vfio/group.c | 2 +-
drivers/vfio/vfio.h | 7 ++++++-
drivers/vfio/vfio_main.c | 8 ++++----
4 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c
index c1c26d7a9aec9..19be3947fc74a 100644
--- a/drivers/vfio/device_cdev.c
+++ b/drivers/vfio/device_cdev.c
@@ -125,7 +125,7 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,

mutex_lock(&device->dev_set->lock);
/* one device cannot be bound twice */
- if (df->access_granted) {
+ if (df->access_mode != VFIO_DF_ACCESS_NONE) {
ret = -EINVAL;
goto out_unlock;
}
@@ -163,7 +163,7 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
* Paired with smp_load_acquire() in vfio_device_fops::ioctl/
* read/write/mmap
*/
- smp_store_release(&df->access_granted, true);
+ smp_store_release(&df->access_mode, VFIO_DF_ACCESS_FULL);
mutex_unlock(&device->dev_set->lock);
return 0;

@@ -185,10 +185,10 @@ void vfio_df_unbind_iommufd(struct vfio_device_file *df)

/*
* In the time of close, there is no contention with another one
- * changing this flag. So read df->access_granted without lock
+ * changing this mode. So read df->access_mode without lock
* and no smp_load_acquire() is ok.
*/
- if (!df->access_granted)
+ if (df->access_mode == VFIO_DF_ACCESS_NONE)
return;

mutex_lock(&device->dev_set->lock);
diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c
index 185f359f75d38..0a33e0e025bd8 100644
--- a/drivers/vfio/group.c
+++ b/drivers/vfio/group.c
@@ -217,7 +217,7 @@ static int vfio_df_group_open(struct vfio_device_file *df)
* Paired with smp_load_acquire() in vfio_device_fops::ioctl/
* read/write/mmap and vfio_file_has_device_access()
*/
- smp_store_release(&df->access_granted, true);
+ smp_store_release(&df->access_mode, VFIO_DF_ACCESS_FULL);

mutex_unlock(&device->dev_set->lock);
mutex_unlock(&device->group->group_lock);
diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h
index 2eeb526a8da58..af2b8158b40c0 100644
--- a/drivers/vfio/vfio.h
+++ b/drivers/vfio/vfio.h
@@ -16,11 +16,16 @@ struct iommufd_ctx;
struct iommu_group;
struct vfio_container;

+enum vfio_df_access_mode {
+ VFIO_DF_ACCESS_NONE = 0,
+ VFIO_DF_ACCESS_FULL,
+};
+
struct vfio_device_file {
struct vfio_device *device;
struct vfio_group *group;

- u8 access_granted;
+ enum vfio_df_access_mode access_mode;
u32 devid; /* only valid when iommufd is valid */
spinlock_t kvm_ref_lock; /* protect kvm field */
struct file *kvm;
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index ea8e6cc13c457..fe126e137d5ca 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -1349,7 +1349,7 @@ static long vfio_device_fops_unl_ioctl(struct file *filep,
return vfio_df_ioctl_bind_iommufd(df, uptr);

/* Paired with smp_store_release() following vfio_df_open() */
- if (!smp_load_acquire(&df->access_granted))
+ if (smp_load_acquire(&df->access_mode) == VFIO_DF_ACCESS_NONE)
return -EINVAL;

ret = vfio_device_pm_runtime_get(device);
@@ -1397,7 +1397,7 @@ static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
struct vfio_device *device = df->device;

/* Paired with smp_store_release() following vfio_df_open() */
- if (!smp_load_acquire(&df->access_granted))
+ if (smp_load_acquire(&df->access_mode) == VFIO_DF_ACCESS_NONE)
return -EINVAL;

if (unlikely(!device->ops->read))
@@ -1414,7 +1414,7 @@ static ssize_t vfio_device_fops_write(struct file *filep,
struct vfio_device *device = df->device;

/* Paired with smp_store_release() following vfio_df_open() */
- if (!smp_load_acquire(&df->access_granted))
+ if (smp_load_acquire(&df->access_mode) == VFIO_DF_ACCESS_NONE)
return -EINVAL;

if (unlikely(!device->ops->write))
@@ -1429,7 +1429,7 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
struct vfio_device *device = df->device;

/* Paired with smp_store_release() following vfio_df_open() */
- if (!smp_load_acquire(&df->access_granted))
+ if (smp_load_acquire(&df->access_mode) == VFIO_DF_ACCESS_NONE)
return -EINVAL;

if (unlikely(!device->ops->mmap))

--
2.56.0.455.g6c5a503b38-goog