Commit 34aeeecd authored by Yi Liu's avatar Yi Liu Committed by Alex Williamson

vfio: Accept vfio device file in the KVM facing kAPI

This makes the vfio file kAPIs to accept vfio device files, also a
preparation for vfio device cdev support.

For the kvm set with vfio device file, kvm pointer is stored in struct
vfio_device_file, and use kvm_ref_lock to protect kvm set and kvm
pointer usage within VFIO. This kvm pointer will be set to vfio_device
after device file is bound to iommufd in the cdev path.
Reviewed-by: default avatarKevin Tian <kevin.tian@intel.com>
Reviewed-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Tested-by: default avatarTerrence Xu <terrence.xu@intel.com>
Tested-by: default avatarNicolin Chen <nicolinc@nvidia.com>
Tested-by: default avatarMatthew Rosato <mjrosato@linux.ibm.com>
Tested-by: default avatarYanting Jiang <yanting.jiang@intel.com>
Tested-by: default avatarShameer Kolothum <shameerali.kolothum.thodi@huawei.com>
Tested-by: default avatarZhenzhong Duan <zhenzhong.duan@intel.com>
Signed-off-by: default avatarYi Liu <yi.l.liu@intel.com>
Link: https://lore.kernel.org/r/20230718135551.6592-4-yi.l.liu@intel.comSigned-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
parent b1a59be8
...@@ -18,6 +18,9 @@ struct vfio_container; ...@@ -18,6 +18,9 @@ struct vfio_container;
struct vfio_device_file { struct vfio_device_file {
struct vfio_device *device; struct vfio_device *device;
spinlock_t kvm_ref_lock; /* protect kvm field */
struct kvm *kvm;
}; };
void vfio_device_put_registration(struct vfio_device *device); void vfio_device_put_registration(struct vfio_device *device);
......
...@@ -429,6 +429,7 @@ vfio_allocate_device_file(struct vfio_device *device) ...@@ -429,6 +429,7 @@ vfio_allocate_device_file(struct vfio_device *device)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
df->device = device; df->device = device;
spin_lock_init(&df->kvm_ref_lock);
return df; return df;
} }
...@@ -1190,13 +1191,23 @@ const struct file_operations vfio_device_fops = { ...@@ -1190,13 +1191,23 @@ const struct file_operations vfio_device_fops = {
.mmap = vfio_device_fops_mmap, .mmap = vfio_device_fops_mmap,
}; };
static struct vfio_device *vfio_device_from_file(struct file *file)
{
struct vfio_device_file *df = file->private_data;
if (file->f_op != &vfio_device_fops)
return NULL;
return df->device;
}
/** /**
* vfio_file_is_valid - True if the file is valid vfio file * vfio_file_is_valid - True if the file is valid vfio file
* @file: VFIO group file or VFIO device file * @file: VFIO group file or VFIO device file
*/ */
bool vfio_file_is_valid(struct file *file) bool vfio_file_is_valid(struct file *file)
{ {
return vfio_group_from_file(file); return vfio_group_from_file(file) ||
vfio_device_from_file(file);
} }
EXPORT_SYMBOL_GPL(vfio_file_is_valid); EXPORT_SYMBOL_GPL(vfio_file_is_valid);
...@@ -1211,16 +1222,36 @@ EXPORT_SYMBOL_GPL(vfio_file_is_valid); ...@@ -1211,16 +1222,36 @@ EXPORT_SYMBOL_GPL(vfio_file_is_valid);
*/ */
bool vfio_file_enforced_coherent(struct file *file) bool vfio_file_enforced_coherent(struct file *file)
{ {
struct vfio_device *device;
struct vfio_group *group; struct vfio_group *group;
group = vfio_group_from_file(file); group = vfio_group_from_file(file);
if (group) if (group)
return vfio_group_enforced_coherent(group); return vfio_group_enforced_coherent(group);
device = vfio_device_from_file(file);
if (device)
return device_iommu_capable(device->dev,
IOMMU_CAP_ENFORCE_CACHE_COHERENCY);
return true; return true;
} }
EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent); EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)
{
struct vfio_device_file *df = file->private_data;
/*
* The kvm is first recorded in the vfio_device_file, and will
* be propagated to vfio_device::kvm when the file is bound to
* iommufd successfully in the vfio device cdev path.
*/
spin_lock(&df->kvm_ref_lock);
df->kvm = kvm;
spin_unlock(&df->kvm_ref_lock);
}
/** /**
* vfio_file_set_kvm - Link a kvm with VFIO drivers * vfio_file_set_kvm - Link a kvm with VFIO drivers
* @file: VFIO group file or VFIO device file * @file: VFIO group file or VFIO device file
...@@ -1236,6 +1267,9 @@ void vfio_file_set_kvm(struct file *file, struct kvm *kvm) ...@@ -1236,6 +1267,9 @@ void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
group = vfio_group_from_file(file); group = vfio_group_from_file(file);
if (group) if (group)
vfio_group_set_kvm(group, kvm); vfio_group_set_kvm(group, kvm);
if (vfio_device_from_file(file))
vfio_device_file_set_kvm(file, kvm);
} }
EXPORT_SYMBOL_GPL(vfio_file_set_kvm); EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment