Skip to contentSkip to conversation

vfio iommu: Use shift operation for 64-bit integer division

PATCH In review Kirti Wankhede 2 participants 3 messages Jun 2, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 vfio iommu: Use shift operation for 64-bit integer division

Fixes compilation error with ARCH=i386.

Error fixed by this commit:
ld: drivers/vfio/vfio_iommu_type1.o: in function `vfio_dma_populate_bitmap':

Quoted 1 line
Quoted 1 line

vfio_iommu_type1.c:(.text+0x666): undefined reference to `__udivdi3'

Fixes d6a4c185660c (vfio iommu: Implementation of ioctl for dirty pages tracking)
---
 drivers/vfio/vfio_iommu_type1.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/vfio/vfio_iommu_type1.c +2 -1
index 97a29bc04d5d..9d9c8709a24c 100644
@@ -227,11 +227,12 @@ static void vfio_dma_bitmap_free(struct vfio_dma *dma)
 static void vfio_dma_populate_bitmap(struct vfio_dma *dma, size_t pgsize)
 {
 	struct rb_node *p;
+	unsigned long pgshift = __ffs(pgsize);
 
 	for (p = rb_first(&dma->pfn_list); p; p = rb_next(p)) {
 		struct vfio_pfn *vpfn = rb_entry(p, struct vfio_pfn, node);
 
-		bitmap_set(dma->bitmap, (vpfn->iova - dma->iova) / pgsize, 1);
+		bitmap_set(dma->bitmap, (vpfn->iova - dma->iova) >> pgshift, 1);
 	}
 }
 
Signature · 1 line
2.7.0
#1 lore raw

PATCH 2/2 vfio iommu: typecast corrections

Fixes sparse warnings by adding '__user' in typecast for copy_[from,to]_user()

Fixes d6a4c185660c (vfio iommu: Implementation of ioctl for dirty pages tracking)
---
 drivers/vfio/vfio_iommu_type1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/vfio/vfio_iommu_type1.c +2 -2
index 9d9c8709a24c..5e556ac9102a 100644
@@ -998,14 +998,14 @@ static int update_user_bitmap(u64 __user *bitmap, struct vfio_iommu *iommu,
 				  nbits + shift);
 
 		if (copy_from_user(&leftover,
-				   (const void *)(bitmap + copy_offset),
+				   (void __user *)(bitmap + copy_offset),
 				   sizeof(leftover)))
 			return -EFAULT;
 
 		bitmap_or(dma->bitmap, dma->bitmap, &leftover, shift);
 	}
 
-	if (copy_to_user((void *)(bitmap + copy_offset), dma->bitmap,
+	if (copy_to_user((void __user *)(bitmap + copy_offset), dma->bitmap,
 			 DIRTY_BITMAP_BYTES(nbits + shift)))
 		return -EFAULT;
 
Signature · 1 line
2.7.0
#2 lore raw
In reply to Kirti Wankhede
Quoted 22 lines · On Wed, 3 Jun 2020 00:12:36 +0530 Kirti Wankhede <[email protected]> wrote:

Fixes compilation error with ARCH=i386.

Error fixed by this commit:
ld: drivers/vfio/vfio_iommu_type1.o: in function `vfio_dma_populate_bitmap':

Quoted 1 line
Quoted 1 line

vfio_iommu_type1.c:(.text+0x666): undefined reference to `__udivdi3'

Fixes d6a4c185660c (vfio iommu: Implementation of ioctl for dirty pages tracking)
---
 drivers/vfio/vfio_iommu_type1.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/vfio/vfio_iommu_type1.c +0 -0
index 97a29bc04d5d..9d9c8709a24c 100644
@@ -227,11 +227,12 @@ static void vfio_dma_bitmap_free(struct vfio_dma *dma)
 static void vfio_dma_populate_bitmap(struct vfio_dma *dma, size_t pgsize)
 {
 	struct rb_node *p;

drivers/vfio/vfio_iommu_type1.c static void vfio_dma_bitmap_free(struct vfio_dma *dma) · line 238

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +2 -1
+	unsigned long pgshift = __ffs(pgsize);
 
 	for (p = rb_first(&dma->pfn_list); p; p = rb_next(p)) {
 		struct vfio_pfn *vpfn = rb_entry(p, struct vfio_pfn, node);
 
-		bitmap_set(dma->bitmap, (vpfn->iova - dma->iova) / pgsize, 1);
+		bitmap_set(dma->bitmap, (vpfn->iova - dma->iova) >> pgshift, 1);
 	}
 }
 

Applied and pushed both to the vfio next branch. Thanks!

Alex