Skip to content

Commit 5a4fe7c

Browse files
efarmanawilliam
authored andcommitted
vfio/ccw: Add length to DMA_UNMAP checks
As pointed out with the simplification of the VFIO_IOMMU_NOTIFY_DMA_UNMAP notifier [1], the length parameter was never used to check against the pinned pages. Let's correct that, and see if a page is within the affected range instead of simply the first page of the range. [1] https://lore.kernel.org/kvm/20220720170457.39cda0d0.alex.williamson@redhat.com/ Signed-off-by: Eric Farman <farman@linux.ibm.com> Reviewed-by: Matthew Rosato <mjrosato@linux.ibm.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/20220728204914.2420989-2-farman@linux.ibm.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent 34a255e commit 5a4fe7c

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

drivers/s390/cio/vfio_ccw_cp.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,18 @@ static void page_array_unpin_free(struct page_array *pa, struct vfio_device *vde
170170
kfree(pa->pa_iova);
171171
}
172172

173-
static bool page_array_iova_pinned(struct page_array *pa, unsigned long iova)
173+
static bool page_array_iova_pinned(struct page_array *pa, u64 iova, u64 length)
174174
{
175+
u64 iova_pfn_start = iova >> PAGE_SHIFT;
176+
u64 iova_pfn_end = (iova + length - 1) >> PAGE_SHIFT;
177+
u64 pfn;
175178
int i;
176179

177-
for (i = 0; i < pa->pa_nr; i++)
178-
if (pa->pa_iova[i] == iova)
180+
for (i = 0; i < pa->pa_nr; i++) {
181+
pfn = pa->pa_iova[i] >> PAGE_SHIFT;
182+
if (pfn >= iova_pfn_start && pfn <= iova_pfn_end)
179183
return true;
184+
}
180185

181186
return false;
182187
}
@@ -899,11 +904,12 @@ void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
899904
* cp_iova_pinned() - check if an iova is pinned for a ccw chain.
900905
* @cp: channel_program on which to perform the operation
901906
* @iova: the iova to check
907+
* @length: the length to check from @iova
902908
*
903909
* If the @iova is currently pinned for the ccw chain, return true;
904910
* else return false.
905911
*/
906-
bool cp_iova_pinned(struct channel_program *cp, u64 iova)
912+
bool cp_iova_pinned(struct channel_program *cp, u64 iova, u64 length)
907913
{
908914
struct ccwchain *chain;
909915
int i;
@@ -913,7 +919,7 @@ bool cp_iova_pinned(struct channel_program *cp, u64 iova)
913919

914920
list_for_each_entry(chain, &cp->ccwchain_list, next) {
915921
for (i = 0; i < chain->ch_len; i++)
916-
if (page_array_iova_pinned(chain->ch_pa + i, iova))
922+
if (page_array_iova_pinned(chain->ch_pa + i, iova, length))
917923
return true;
918924
}
919925

drivers/s390/cio/vfio_ccw_cp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ void cp_free(struct channel_program *cp);
4646
int cp_prefetch(struct channel_program *cp);
4747
union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm);
4848
void cp_update_scsw(struct channel_program *cp, union scsw *scsw);
49-
bool cp_iova_pinned(struct channel_program *cp, u64 iova);
49+
bool cp_iova_pinned(struct channel_program *cp, u64 iova, u64 length);
5050

5151
#endif

drivers/s390/cio/vfio_ccw_ops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void vfio_ccw_dma_unmap(struct vfio_device *vdev, u64 iova, u64 length)
3939
container_of(vdev, struct vfio_ccw_private, vdev);
4040

4141
/* Drivers MUST unpin pages in response to an invalidation. */
42-
if (!cp_iova_pinned(&private->cp, iova))
42+
if (!cp_iova_pinned(&private->cp, iova, length))
4343
return;
4444

4545
vfio_ccw_mdev_reset(private);

0 commit comments

Comments
 (0)