Commit 8e432bb0 authored by Jason Gunthorpe's avatar Jason Gunthorpe Committed by Alex Williamson

vfio/mdev: Pass in a struct vfio_device * to vfio_pin/unpin_pages()

Every caller has a readily available vfio_device pointer, use that instead
of passing in a generic struct device. The struct vfio_device already
contains the group we need so this avoids complexity, extra refcountings,
and a confusing lifecycle model.
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Acked-by: default avatarEric Farman <farman@linux.ibm.com>
Reviewed-by: default avatarJason J. Herne <jjherne@linux.ibm.com>
Reviewed-by: default avatarTony Krowiak <akrowiak@linux.ibm.com>
Reviewed-by: default avatarKevin Tian <kevin.tian@intel.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/3-v4-8045e76bf00b+13d-vfio_mdev_no_group_jgg@nvidia.comSigned-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
parent 0a587956
...@@ -262,10 +262,10 @@ Translation APIs for Mediated Devices ...@@ -262,10 +262,10 @@ Translation APIs for Mediated Devices
The following APIs are provided for translating user pfn to host pfn in a VFIO The following APIs are provided for translating user pfn to host pfn in a VFIO
driver:: driver::
extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
int npage, int prot, unsigned long *phys_pfn); int npage, int prot, unsigned long *phys_pfn);
extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
int npage); int npage);
These functions call back into the back-end IOMMU module by using the pin_pages These functions call back into the back-end IOMMU module by using the pin_pages
......
...@@ -103,13 +103,13 @@ static int pfn_array_pin(struct pfn_array *pa, struct vfio_device *vdev) ...@@ -103,13 +103,13 @@ static int pfn_array_pin(struct pfn_array *pa, struct vfio_device *vdev)
{ {
int ret = 0; int ret = 0;
ret = vfio_pin_pages(vdev->dev, pa->pa_iova_pfn, pa->pa_nr, ret = vfio_pin_pages(vdev, pa->pa_iova_pfn, pa->pa_nr,
IOMMU_READ | IOMMU_WRITE, pa->pa_pfn); IOMMU_READ | IOMMU_WRITE, pa->pa_pfn);
if (ret < 0) { if (ret < 0) {
goto err_out; goto err_out;
} else if (ret > 0 && ret != pa->pa_nr) { } else if (ret > 0 && ret != pa->pa_nr) {
vfio_unpin_pages(vdev->dev, pa->pa_iova_pfn, ret); vfio_unpin_pages(vdev, pa->pa_iova_pfn, ret);
ret = -EINVAL; ret = -EINVAL;
goto err_out; goto err_out;
} }
...@@ -127,7 +127,7 @@ static void pfn_array_unpin_free(struct pfn_array *pa, struct vfio_device *vdev) ...@@ -127,7 +127,7 @@ static void pfn_array_unpin_free(struct pfn_array *pa, struct vfio_device *vdev)
{ {
/* Only unpin if any pages were pinned to begin with */ /* Only unpin if any pages were pinned to begin with */
if (pa->pa_nr) if (pa->pa_nr)
vfio_unpin_pages(vdev->dev, pa->pa_iova_pfn, pa->pa_nr); vfio_unpin_pages(vdev, pa->pa_iova_pfn, pa->pa_nr);
pa->pa_nr = 0; pa->pa_nr = 0;
kfree(pa->pa_iova_pfn); kfree(pa->pa_iova_pfn);
} }
......
...@@ -124,8 +124,7 @@ static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q) ...@@ -124,8 +124,7 @@ static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
q->saved_isc = VFIO_AP_ISC_INVALID; q->saved_isc = VFIO_AP_ISC_INVALID;
} }
if (q->saved_pfn && !WARN_ON(!q->matrix_mdev)) { if (q->saved_pfn && !WARN_ON(!q->matrix_mdev)) {
vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev), vfio_unpin_pages(&q->matrix_mdev->vdev, &q->saved_pfn, 1);
&q->saved_pfn, 1);
q->saved_pfn = 0; q->saved_pfn = 0;
} }
} }
...@@ -258,7 +257,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, ...@@ -258,7 +257,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
return status; return status;
} }
ret = vfio_pin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1, ret = vfio_pin_pages(&q->matrix_mdev->vdev, &g_pfn, 1,
IOMMU_READ | IOMMU_WRITE, &h_pfn); IOMMU_READ | IOMMU_WRITE, &h_pfn);
switch (ret) { switch (ret) {
case 1: case 1:
...@@ -301,7 +300,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, ...@@ -301,7 +300,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
break; break;
case AP_RESPONSE_OTHERWISE_CHANGED: case AP_RESPONSE_OTHERWISE_CHANGED:
/* We could not modify IRQ setings: clear new configuration */ /* We could not modify IRQ setings: clear new configuration */
vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1); vfio_unpin_pages(&q->matrix_mdev->vdev, &g_pfn, 1);
kvm_s390_gisc_unregister(kvm, isc); kvm_s390_gisc_unregister(kvm, isc);
break; break;
default: default:
...@@ -1250,7 +1249,7 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, ...@@ -1250,7 +1249,7 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
struct vfio_iommu_type1_dma_unmap *unmap = data; struct vfio_iommu_type1_dma_unmap *unmap = data;
unsigned long g_pfn = unmap->iova >> PAGE_SHIFT; unsigned long g_pfn = unmap->iova >> PAGE_SHIFT;
vfio_unpin_pages(mdev_dev(matrix_mdev->mdev), &g_pfn, 1); vfio_unpin_pages(&matrix_mdev->vdev, &g_pfn, 1);
return NOTIFY_OK; return NOTIFY_OK;
} }
......
...@@ -2134,7 +2134,7 @@ EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare); ...@@ -2134,7 +2134,7 @@ EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
/* /*
* Pin a set of guest PFNs and return their associated host PFNs for local * Pin a set of guest PFNs and return their associated host PFNs for local
* domain only. * domain only.
* @dev [in] : device * @device [in] : device
* @user_pfn [in]: array of user/guest PFNs to be pinned. * @user_pfn [in]: array of user/guest PFNs to be pinned.
* @npage [in] : count of elements in user_pfn array. This count should not * @npage [in] : count of elements in user_pfn array. This count should not
* be greater VFIO_PIN_PAGES_MAX_ENTRIES. * be greater VFIO_PIN_PAGES_MAX_ENTRIES.
...@@ -2142,32 +2142,26 @@ EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare); ...@@ -2142,32 +2142,26 @@ EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
* @phys_pfn[out]: array of host PFNs * @phys_pfn[out]: array of host PFNs
* Return error or number of pages pinned. * Return error or number of pages pinned.
*/ */
int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, int npage, int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
int prot, unsigned long *phys_pfn) int npage, int prot, unsigned long *phys_pfn)
{ {
struct vfio_container *container; struct vfio_container *container;
struct vfio_group *group; struct vfio_group *group = device->group;
struct vfio_iommu_driver *driver; struct vfio_iommu_driver *driver;
int ret; int ret;
if (!dev || !user_pfn || !phys_pfn || !npage) if (!user_pfn || !phys_pfn || !npage)
return -EINVAL; return -EINVAL;
if (npage > VFIO_PIN_PAGES_MAX_ENTRIES) if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
return -E2BIG; return -E2BIG;
group = vfio_group_get_from_dev(dev); if (group->dev_counter > 1)
if (!group) return -EINVAL;
return -ENODEV;
if (group->dev_counter > 1) {
ret = -EINVAL;
goto err_pin_pages;
}
ret = vfio_group_add_container_user(group); ret = vfio_group_add_container_user(group);
if (ret) if (ret)
goto err_pin_pages; return ret;
container = group->container; container = group->container;
driver = container->iommu_driver; driver = container->iommu_driver;
...@@ -2180,43 +2174,37 @@ int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, int npage, ...@@ -2180,43 +2174,37 @@ int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, int npage,
vfio_group_try_dissolve_container(group); vfio_group_try_dissolve_container(group);
err_pin_pages:
vfio_group_put(group);
return ret; return ret;
} }
EXPORT_SYMBOL(vfio_pin_pages); EXPORT_SYMBOL(vfio_pin_pages);
/* /*
* Unpin set of host PFNs for local domain only. * Unpin set of host PFNs for local domain only.
* @dev [in] : device * @device [in] : device
* @user_pfn [in]: array of user/guest PFNs to be unpinned. Number of user/guest * @user_pfn [in]: array of user/guest PFNs to be unpinned. Number of user/guest
* PFNs should not be greater than VFIO_PIN_PAGES_MAX_ENTRIES. * PFNs should not be greater than VFIO_PIN_PAGES_MAX_ENTRIES.
* @npage [in] : count of elements in user_pfn array. This count should not * @npage [in] : count of elements in user_pfn array. This count should not
* be greater than VFIO_PIN_PAGES_MAX_ENTRIES. * be greater than VFIO_PIN_PAGES_MAX_ENTRIES.
* Return error or number of pages unpinned. * Return error or number of pages unpinned.
*/ */
int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, int npage) int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
int npage)
{ {
struct vfio_container *container; struct vfio_container *container;
struct vfio_group *group;
struct vfio_iommu_driver *driver; struct vfio_iommu_driver *driver;
int ret; int ret;
if (!dev || !user_pfn || !npage) if (!user_pfn || !npage)
return -EINVAL; return -EINVAL;
if (npage > VFIO_PIN_PAGES_MAX_ENTRIES) if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
return -E2BIG; return -E2BIG;
group = vfio_group_get_from_dev(dev); ret = vfio_group_add_container_user(device->group);
if (!group)
return -ENODEV;
ret = vfio_group_add_container_user(group);
if (ret) if (ret)
goto err_unpin_pages; return ret;
container = group->container; container = device->group->container;
driver = container->iommu_driver; driver = container->iommu_driver;
if (likely(driver && driver->ops->unpin_pages)) if (likely(driver && driver->ops->unpin_pages))
ret = driver->ops->unpin_pages(container->iommu_data, user_pfn, ret = driver->ops->unpin_pages(container->iommu_data, user_pfn,
...@@ -2224,10 +2212,8 @@ int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, int npage) ...@@ -2224,10 +2212,8 @@ int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, int npage)
else else
ret = -ENOTTY; ret = -ENOTTY;
vfio_group_try_dissolve_container(group); vfio_group_try_dissolve_container(device->group);
err_unpin_pages:
vfio_group_put(group);
return ret; return ret;
} }
EXPORT_SYMBOL(vfio_unpin_pages); EXPORT_SYMBOL(vfio_unpin_pages);
......
...@@ -150,9 +150,9 @@ extern long vfio_external_check_extension(struct vfio_group *group, ...@@ -150,9 +150,9 @@ extern long vfio_external_check_extension(struct vfio_group *group,
#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, extern int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
int npage, int prot, unsigned long *phys_pfn); int npage, int prot, unsigned long *phys_pfn);
extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, extern int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
int npage); int npage);
extern int vfio_group_pin_pages(struct vfio_group *group, extern int vfio_group_pin_pages(struct vfio_group *group,
......
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