Skip to contentSkip to conversation

vfio/type1: Fix vfio_find_dma_valid return

PATCH Superseded Anthony Yznaga 2 participants 4 messages Aug 19, 2021 – Aug 20, 2021

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio/type1: Fix vfio_find_dma_valid return

Fix vfio_find_dma_valid to return WAITED on success if it was necessary to wait which mean iommu lock was dropped and reacquired. This allows vfio_iommu_type1_pin_pages to recheck vaddr_invalid_count and possibly avoid the checking the validity of every vaddr in its list.

---
 drivers/vfio/vfio_iommu_type1.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/vfio/vfio_iommu_type1.c +3 -2
index a3e925a41b0d..7ca8c4e95da4 100644
@@ -612,6 +612,7 @@ static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start,
 			       size_t size, struct vfio_dma **dma_p)
 {
 	int ret;
+	int waited = 0;
 
 	do {
 		*dma_p = vfio_find_dma(iommu, start, size);
@@ -620,10 +621,10 @@ static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start,
 		else if (!(*dma_p)->vaddr_invalid)
 			ret = 0;
 		else
-			ret = vfio_wait(iommu);
+			ret = waited = vfio_wait(iommu);
 	} while (ret > 0);
 
-	return ret;
+	return ret ? ret : waited;
 }
 
 /*
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 25 lines · On Thu, 19 Aug 2021 16:53:57 -0700 Anthony Yznaga <[email protected]>…

Fix vfio_find_dma_valid to return WAITED on success if it was necessary to wait which mean iommu lock was dropped and reacquired. This allows vfio_iommu_type1_pin_pages to recheck vaddr_invalid_count and possibly avoid the checking the validity of every vaddr in its list.

---
 drivers/vfio/vfio_iommu_type1.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/vfio/vfio_iommu_type1.c +1 -0
index a3e925a41b0d..7ca8c4e95da4 100644
@@ -612,6 +612,7 @@ static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start,
 			       size_t size, struct vfio_dma **dma_p)
 {
 	int ret;
+	int waited = 0;
 
 	do {
 		*dma_p = vfio_find_dma(iommu, start, size);
@@ -620,10 +621,10 @@ static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start,
 		else if (!(*dma_p)->vaddr_invalid)
 			ret = 0;

drivers/vfio/vfio_iommu_type1.c static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start, · line 630

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +2 -2
 		else
-			ret = vfio_wait(iommu);
+			ret = waited = vfio_wait(iommu);
 	} while (ret > 0);
 
-	return ret;
+	return ret ? ret : waited;
 }
 
 /*

How about...

drivers/vfio/vfio_iommu_type1.c +4 -4
index 0b4f7c174c7a..0e9217687f5c 100644
@@ -612,17 +612,17 @@ static int vfio_wait(struct vfio_iommu *iommu)
 static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start,
 			       size_t size, struct vfio_dma **dma_p)
 {
-	int ret;
+	int ret = 0;
 
 	do {
 		*dma_p = vfio_find_dma(iommu, start, size);
 		if (!*dma_p)
-			ret = -EINVAL;
+			return -EINVAL;
 		else if (!(*dma_p)->vaddr_invalid)
-			ret = 0;
+			return ret;
 		else
 			ret = vfio_wait(iommu);
-	} while (ret > 0);
+	} while (ret == WAITED);
 
 	return ret;
 }
#2 lore raw
Quoted 57 lines · Alex Williamson
Quoted 25 lines · On Thu, 19 Aug 2021 16:53:57 -0700 Anthony Yznaga <[email protected]>…

Fix vfio_find_dma_valid to return WAITED on success if it was necessary to wait which mean iommu lock was dropped and reacquired. This allows vfio_iommu_type1_pin_pages to recheck vaddr_invalid_count and possibly avoid the checking the validity of every vaddr in its list.

---
 drivers/vfio/vfio_iommu_type1.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/vfio/vfio_iommu_type1.c +1 -0
index a3e925a41b0d..7ca8c4e95da4 100644
@@ -612,6 +612,7 @@ static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start,
 			       size_t size, struct vfio_dma **dma_p)
 {
 	int ret;
+	int waited = 0;
 
 	do {
 		*dma_p = vfio_find_dma(iommu, start, size);
@@ -620,10 +621,10 @@ static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start,
 		else if (!(*dma_p)->vaddr_invalid)
 			ret = 0;

drivers/vfio/vfio_iommu_type1.c static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start, · line 630

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +2 -2
 		else
-			ret = vfio_wait(iommu);
+			ret = waited = vfio_wait(iommu);
 	} while (ret > 0);
 
-	return ret;
+	return ret ? ret : waited;
 }
 
 /*

How about...

drivers/vfio/vfio_iommu_type1.c +2 -2
index 0b4f7c174c7a..0e9217687f5c 100644
@@ -612,17 +612,17 @@ static int vfio_wait(struct vfio_iommu *iommu)
 static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start,
 			       size_t size, struct vfio_dma **dma_p)
 {
-	int ret;
+	int ret = 0;
 
 	do {
 		*dma_p = vfio_find_dma(iommu, start, size);
 		if (!*dma_p)
-			ret = -EINVAL;
+			return -EINVAL;

drivers/vfio/vfio_iommu_type1.c static int vfio_wait(struct vfio_iommu *iommu) · line 628

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +2 -2
 		else if (!(*dma_p)->vaddr_invalid)
-			ret = 0;
+			return ret;
 		else
 			ret = vfio_wait(iommu);
-	} while (ret > 0);
+	} while (ret == WAITED);
 
 	return ret;
 }

Even better.  Should I send a new patch?

Thanks,
Anthony

#3 lore raw
Quoted 65 lines · On Fri, 20 Aug 2021 11:44:01 -0700 Anthony Yznaga <[email protected]>…
Quoted 64 lines · Alex Williamson
Quoted 25 lines · On Thu, 19 Aug 2021 16:53:57 -0700 Anthony Yznaga <[email protected]>…

Fix vfio_find_dma_valid to return WAITED on success if it was necessary to wait which mean iommu lock was dropped and reacquired. This allows vfio_iommu_type1_pin_pages to recheck vaddr_invalid_count and possibly avoid the checking the validity of every vaddr in its list.

---
 drivers/vfio/vfio_iommu_type1.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/vfio/vfio_iommu_type1.c +1 -0
index a3e925a41b0d..7ca8c4e95da4 100644
@@ -612,6 +612,7 @@ static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start,
 			       size_t size, struct vfio_dma **dma_p)
 {
 	int ret;
+	int waited = 0;
 
 	do {
 		*dma_p = vfio_find_dma(iommu, start, size);
@@ -620,10 +621,10 @@ static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start,
 		else if (!(*dma_p)->vaddr_invalid)
 			ret = 0;

drivers/vfio/vfio_iommu_type1.c static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start, · line 630

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +2 -2
 		else
-			ret = vfio_wait(iommu);
+			ret = waited = vfio_wait(iommu);
 	} while (ret > 0);
 
-	return ret;
+	return ret ? ret : waited;
 }
 
 /*  

How about...

drivers/vfio/vfio_iommu_type1.c +4 -4
index 0b4f7c174c7a..0e9217687f5c 100644
@@ -612,17 +612,17 @@ static int vfio_wait(struct vfio_iommu *iommu)
 static int vfio_find_dma_valid(struct vfio_iommu *iommu, dma_addr_t start,
 			       size_t size, struct vfio_dma **dma_p)
 {
-	int ret;
+	int ret = 0;
 
 	do {
 		*dma_p = vfio_find_dma(iommu, start, size);
 		if (!*dma_p)
-			ret = -EINVAL;
+			return -EINVAL;
 		else if (!(*dma_p)->vaddr_invalid)
-			ret = 0;
+			return ret;
 		else
 			ret = vfio_wait(iommu);
-	} while (ret > 0);
+	} while (ret == WAITED);
 
Quoted 5 lines

drivers/vfio/vfio_iommu_type1.c static int vfio_wait(struct vfio_iommu *iommu) · line 628

Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +0 -0
 	return ret;
 }

Even better.  Should I send a new patch?

That would be preferable, otherwise at least a sign-off or reviewed-by for the newer version. Thanks,

Alex