Skip to content

Commit e8f9071

Browse files
nicolincawilliam
authored andcommitted
vfio: Make vfio_unpin_pages() return void
There's only one caller that checks its return value with a WARN_ON_ONCE, while all other callers don't check the return value at all. Above that, an undo function should not fail. So, simplify the API to return void by embedding similar WARN_ONs. Also for users to pinpoint which condition fails, separate WARN_ON lines, yet remove the "driver->ops->unpin_pages" check, since it's unreasonable for callers to unpin on something totally random that wasn't even pinned. And remove NULL pointer checks for they would trigger oops vs. warnings. Note that npage is already validated in the vfio core, thus drop the same check in the type1 code. Suggested-by: Christoph Hellwig <hch@infradead.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Kirti Wankhede <kwankhede@nvidia.com> Tested-by: Terrence Xu <terrence.xu@intel.com> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Link: https://lore.kernel.org/r/20220723020256.30081-2-nicolinc@nvidia.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent 9cb633a commit e8f9071

File tree

6 files changed

+18
-31
lines changed

6 files changed

+18
-31
lines changed

Documentation/driver-api/vfio-mediated-device.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ driver::
265265
int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
266266
int npage, int prot, unsigned long *phys_pfn);
267267

268-
int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
268+
void vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
269269
int npage);
270270

271271
These functions call back into the back-end IOMMU module by using the pin_pages

drivers/gpu/drm/i915/gvt/kvmgt.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,15 @@ static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt)
231231
static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
232232
unsigned long size)
233233
{
234-
struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
235234
int total_pages;
236235
int npage;
237-
int ret;
238236

239237
total_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
240238

241239
for (npage = 0; npage < total_pages; npage++) {
242240
unsigned long cur_gfn = gfn + npage;
243241

244-
ret = vfio_unpin_pages(&vgpu->vfio_device, &cur_gfn, 1);
245-
drm_WARN_ON(&i915->drm, ret != 1);
242+
vfio_unpin_pages(&vgpu->vfio_device, &cur_gfn, 1);
246243
}
247244
}
248245

drivers/vfio/vfio.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,31 +1983,24 @@ EXPORT_SYMBOL(vfio_pin_pages);
19831983
* PFNs should not be greater than VFIO_PIN_PAGES_MAX_ENTRIES.
19841984
* @npage [in] : count of elements in user_pfn array. This count should not
19851985
* be greater than VFIO_PIN_PAGES_MAX_ENTRIES.
1986-
* Return error or number of pages unpinned.
19871986
*/
1988-
int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
1989-
int npage)
1987+
void vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
1988+
int npage)
19901989
{
19911990
struct vfio_container *container;
19921991
struct vfio_iommu_driver *driver;
1993-
int ret;
19941992

1995-
if (!user_pfn || !npage || !vfio_assert_device_open(device))
1996-
return -EINVAL;
1993+
if (WARN_ON(npage <= 0 || npage > VFIO_PIN_PAGES_MAX_ENTRIES))
1994+
return;
19971995

1998-
if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
1999-
return -E2BIG;
1996+
if (WARN_ON(!vfio_assert_device_open(device)))
1997+
return;
20001998

20011999
/* group->container cannot change while a vfio device is open */
20022000
container = device->group->container;
20032001
driver = container->iommu_driver;
2004-
if (likely(driver && driver->ops->unpin_pages))
2005-
ret = driver->ops->unpin_pages(container->iommu_data, user_pfn,
2006-
npage);
2007-
else
2008-
ret = -ENOTTY;
20092002

2010-
return ret;
2003+
driver->ops->unpin_pages(container->iommu_data, user_pfn, npage);
20112004
}
20122005
EXPORT_SYMBOL(vfio_unpin_pages);
20132006

drivers/vfio/vfio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct vfio_iommu_driver_ops {
5353
unsigned long *user_pfn,
5454
int npage, int prot,
5555
unsigned long *phys_pfn);
56-
int (*unpin_pages)(void *iommu_data,
56+
void (*unpin_pages)(void *iommu_data,
5757
unsigned long *user_pfn, int npage);
5858
void (*register_device)(void *iommu_data,
5959
struct vfio_device *vdev);

drivers/vfio/vfio_iommu_type1.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -949,20 +949,16 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
949949
return ret;
950950
}
951951

952-
static int vfio_iommu_type1_unpin_pages(void *iommu_data,
953-
unsigned long *user_pfn,
954-
int npage)
952+
static void vfio_iommu_type1_unpin_pages(void *iommu_data,
953+
unsigned long *user_pfn, int npage)
955954
{
956955
struct vfio_iommu *iommu = iommu_data;
957956
bool do_accounting;
958957
int i;
959958

960-
if (!iommu || !user_pfn || npage <= 0)
961-
return -EINVAL;
962-
963959
/* Supported for v2 version only */
964-
if (!iommu->v2)
965-
return -EACCES;
960+
if (WARN_ON(!iommu->v2))
961+
return;
966962

967963
mutex_lock(&iommu->lock);
968964

@@ -980,7 +976,8 @@ static int vfio_iommu_type1_unpin_pages(void *iommu_data,
980976
}
981977

982978
mutex_unlock(&iommu->lock);
983-
return i > 0 ? i : -EINVAL;
979+
980+
WARN_ON(i != npage);
984981
}
985982

986983
static long vfio_sync_unpin(struct vfio_dma *dma, struct vfio_domain *domain,

include/linux/vfio.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
163163

164164
int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
165165
int npage, int prot, unsigned long *phys_pfn);
166-
int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
167-
int npage);
166+
void vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
167+
int npage);
168168
int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova,
169169
void *data, size_t len, bool write);
170170

0 commit comments

Comments
 (0)