diff mbox series

[V3,5/9] vfio/type1: massage unmap iteration

Message ID 1611939252-7240-6-git-send-email-steven.sistare@oracle.com (mailing list archive)
State New, archived
Headers show
Series vfio virtual address update | expand

Commit Message

Steven Sistare Jan. 29, 2021, 4:54 p.m. UTC
Modify the iteration in vfio_dma_do_unmap so it does not depend on deletion
of each dma entry.  Add a variant of vfio_find_dma that returns the entry
with the lowest iova in the search range to initialize the iteration.  No
externally visible change, but this behavior is needed in the subsequent
update-vaddr patch.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
 drivers/vfio/vfio_iommu_type1.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

Comments

Alex Williamson Jan. 29, 2021, 9:56 p.m. UTC | #1
On Fri, 29 Jan 2021 08:54:08 -0800
Steve Sistare <steven.sistare@oracle.com> wrote:

> Modify the iteration in vfio_dma_do_unmap so it does not depend on deletion
> of each dma entry.  Add a variant of vfio_find_dma that returns the entry
> with the lowest iova in the search range to initialize the iteration.  No
> externally visible change, but this behavior is needed in the subsequent
> update-vaddr patch.
> 
> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
> ---
>  drivers/vfio/vfio_iommu_type1.c | 35 ++++++++++++++++++++++++++++++++++-
>  1 file changed, 34 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
> index 407f0f7..5823607 100644
> --- a/drivers/vfio/vfio_iommu_type1.c
> +++ b/drivers/vfio/vfio_iommu_type1.c
> @@ -173,6 +173,31 @@ static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu,
>  	return NULL;
>  }
>  
> +static struct rb_node *vfio_find_dma_first(struct vfio_iommu *iommu,
> +					    dma_addr_t start, size_t size)

Nit, we return an rb_node rather than a vfio_dma now, but the naming is
still pretty similar to vfio_find_dma().  Can I change it to
vfio_find_dma_first_node() (yes, getting wordy)?  Thanks,

Alex

> +{
> +	struct rb_node *res = NULL;
> +	struct rb_node *node = iommu->dma_list.rb_node;
> +	struct vfio_dma *dma_res = NULL;
> +
> +	while (node) {
> +		struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node);
> +
> +		if (start < dma->iova + dma->size) {
> +			res = node;
> +			dma_res = dma;
> +			if (start >= dma->iova)
> +				break;
> +			node = node->rb_left;
> +		} else {
> +			node = node->rb_right;
> +		}
> +	}
> +	if (res && size && dma_res->iova >= start + size)
> +		res = NULL;
> +	return res;
> +}
> +
>  static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new)
>  {
>  	struct rb_node **link = &iommu->dma_list.rb_node, *parent = NULL;
> @@ -1078,6 +1103,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
>  	dma_addr_t iova = unmap->iova;
>  	unsigned long size = unmap->size;
>  	bool unmap_all = !!(unmap->flags & VFIO_DMA_UNMAP_FLAG_ALL);
> +	struct rb_node *n;
>  
>  	mutex_lock(&iommu->lock);
>  
> @@ -1148,7 +1174,13 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
>  	}
>  
>  	ret = 0;
> -	while ((dma = vfio_find_dma(iommu, iova, size))) {
> +	n = vfio_find_dma_first(iommu, iova, size);
> +
> +	while (n) {
> +		dma = rb_entry(n, struct vfio_dma, node);
> +		if (dma->iova >= iova + size)
> +			break;
> +
>  		if (!iommu->v2 && iova > dma->iova)
>  			break;
>  		/*
> @@ -1193,6 +1225,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
>  		}
>  
>  		unmapped += dma->size;
> +		n = rb_next(n);
>  		vfio_remove_dma(iommu, dma);
>  	}
>
Steven Sistare Jan. 30, 2021, 4:51 p.m. UTC | #2
On 1/29/2021 4:56 PM, Alex Williamson wrote:
> On Fri, 29 Jan 2021 08:54:08 -0800
> Steve Sistare <steven.sistare@oracle.com> wrote:
> 
>> Modify the iteration in vfio_dma_do_unmap so it does not depend on deletion
>> of each dma entry.  Add a variant of vfio_find_dma that returns the entry
>> with the lowest iova in the search range to initialize the iteration.  No
>> externally visible change, but this behavior is needed in the subsequent
>> update-vaddr patch.
>>
>> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
>> ---
>>  drivers/vfio/vfio_iommu_type1.c | 35 ++++++++++++++++++++++++++++++++++-
>>  1 file changed, 34 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
>> index 407f0f7..5823607 100644
>> --- a/drivers/vfio/vfio_iommu_type1.c
>> +++ b/drivers/vfio/vfio_iommu_type1.c
>> @@ -173,6 +173,31 @@ static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu,
>>  	return NULL;
>>  }
>>  
>> +static struct rb_node *vfio_find_dma_first(struct vfio_iommu *iommu,
>> +					    dma_addr_t start, size_t size)
> 
> Nit, we return an rb_node rather than a vfio_dma now, but the naming is
> still pretty similar to vfio_find_dma().  Can I change it to
> vfio_find_dma_first_node() (yes, getting wordy)?  Thanks,

Sure - steve

>> +{
>> +	struct rb_node *res = NULL;
>> +	struct rb_node *node = iommu->dma_list.rb_node;
>> +	struct vfio_dma *dma_res = NULL;
>> +
>> +	while (node) {
>> +		struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node);
>> +
>> +		if (start < dma->iova + dma->size) {
>> +			res = node;
>> +			dma_res = dma;
>> +			if (start >= dma->iova)
>> +				break;
>> +			node = node->rb_left;
>> +		} else {
>> +			node = node->rb_right;
>> +		}
>> +	}
>> +	if (res && size && dma_res->iova >= start + size)
>> +		res = NULL;
>> +	return res;
>> +}
>> +
>>  static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new)
>>  {
>>  	struct rb_node **link = &iommu->dma_list.rb_node, *parent = NULL;
>> @@ -1078,6 +1103,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
>>  	dma_addr_t iova = unmap->iova;
>>  	unsigned long size = unmap->size;
>>  	bool unmap_all = !!(unmap->flags & VFIO_DMA_UNMAP_FLAG_ALL);
>> +	struct rb_node *n;
>>  
>>  	mutex_lock(&iommu->lock);
>>  
>> @@ -1148,7 +1174,13 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
>>  	}
>>  
>>  	ret = 0;
>> -	while ((dma = vfio_find_dma(iommu, iova, size))) {
>> +	n = vfio_find_dma_first(iommu, iova, size);
>> +
>> +	while (n) {
>> +		dma = rb_entry(n, struct vfio_dma, node);
>> +		if (dma->iova >= iova + size)
>> +			break;
>> +
>>  		if (!iommu->v2 && iova > dma->iova)
>>  			break;
>>  		/*
>> @@ -1193,6 +1225,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
>>  		}
>>  
>>  		unmapped += dma->size;
>> +		n = rb_next(n);
>>  		vfio_remove_dma(iommu, dma);
>>  	}
>>  
>
Cornelia Huck Feb. 1, 2021, 12:11 p.m. UTC | #3
On Fri, 29 Jan 2021 08:54:08 -0800
Steve Sistare <steven.sistare@oracle.com> wrote:

> Modify the iteration in vfio_dma_do_unmap so it does not depend on deletion
> of each dma entry.  Add a variant of vfio_find_dma that returns the entry
> with the lowest iova in the search range to initialize the iteration.  No
> externally visible change, but this behavior is needed in the subsequent
> update-vaddr patch.
> 
> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
> ---
>  drivers/vfio/vfio_iommu_type1.c | 35 ++++++++++++++++++++++++++++++++++-
>  1 file changed, 34 insertions(+), 1 deletion(-)

With the function name tweak:

Reviewed-by: Cornelia Huck <cohuck@redhat.com>
diff mbox series

Patch

diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index 407f0f7..5823607 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -173,6 +173,31 @@  static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu,
 	return NULL;
 }
 
+static struct rb_node *vfio_find_dma_first(struct vfio_iommu *iommu,
+					    dma_addr_t start, size_t size)
+{
+	struct rb_node *res = NULL;
+	struct rb_node *node = iommu->dma_list.rb_node;
+	struct vfio_dma *dma_res = NULL;
+
+	while (node) {
+		struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node);
+
+		if (start < dma->iova + dma->size) {
+			res = node;
+			dma_res = dma;
+			if (start >= dma->iova)
+				break;
+			node = node->rb_left;
+		} else {
+			node = node->rb_right;
+		}
+	}
+	if (res && size && dma_res->iova >= start + size)
+		res = NULL;
+	return res;
+}
+
 static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new)
 {
 	struct rb_node **link = &iommu->dma_list.rb_node, *parent = NULL;
@@ -1078,6 +1103,7 @@  static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
 	dma_addr_t iova = unmap->iova;
 	unsigned long size = unmap->size;
 	bool unmap_all = !!(unmap->flags & VFIO_DMA_UNMAP_FLAG_ALL);
+	struct rb_node *n;
 
 	mutex_lock(&iommu->lock);
 
@@ -1148,7 +1174,13 @@  static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
 	}
 
 	ret = 0;
-	while ((dma = vfio_find_dma(iommu, iova, size))) {
+	n = vfio_find_dma_first(iommu, iova, size);
+
+	while (n) {
+		dma = rb_entry(n, struct vfio_dma, node);
+		if (dma->iova >= iova + size)
+			break;
+
 		if (!iommu->v2 && iova > dma->iova)
 			break;
 		/*
@@ -1193,6 +1225,7 @@  static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
 		}
 
 		unmapped += dma->size;
+		n = rb_next(n);
 		vfio_remove_dma(iommu, dma);
 	}