linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] vfio: put off the allocation of "minor" in vfio_create_group
@ 2014-11-20 11:25 Zhen Lei
  2014-11-20 15:37 ` Alex Williamson
  2015-03-16 22:00 ` Alex Williamson
  0 siblings, 2 replies; 4+ messages in thread
From: Zhen Lei @ 2014-11-20 11:25 UTC (permalink / raw)
  To: Alex Williamson, KVM, linux-kernel
  Cc: Zefan Li, Xinwei Hu, Tianhong Ding, Kefeng Wang, Zhen Lei

The next code fragment "list_for_each_entry" is not depend on "minor". With this
patch, the free of "minor" in "list_for_each_entry" can be reduced, and there is
no functional change.

Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
---
 drivers/vfio/vfio.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index f018d8d..737eb468 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -225,22 +225,21 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)

 	mutex_lock(&vfio.group_lock);

-	minor = vfio_alloc_group_minor(group);
-	if (minor < 0) {
-		vfio_group_unlock_and_free(group);
-		return ERR_PTR(minor);
-	}
-
 	/* Did we race creating this group? */
 	list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
 		if (tmp->iommu_group == iommu_group) {
 			vfio_group_get(tmp);
-			vfio_free_group_minor(minor);
 			vfio_group_unlock_and_free(group);
 			return tmp;
 		}
 	}

+	minor = vfio_alloc_group_minor(group);
+	if (minor < 0) {
+		vfio_group_unlock_and_free(group);
+		return ERR_PTR(minor);
+	}
+
 	dev = device_create(vfio.class, NULL,
 			    MKDEV(MAJOR(vfio.group_devt), minor),
 			    group, "%d", iommu_group_id(iommu_group));
--
1.8.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] vfio: put off the allocation of "minor" in vfio_create_group
  2014-11-20 11:25 [PATCH 1/1] vfio: put off the allocation of "minor" in vfio_create_group Zhen Lei
@ 2014-11-20 15:37 ` Alex Williamson
  2014-11-21  1:25   ` leizhen
  2015-03-16 22:00 ` Alex Williamson
  1 sibling, 1 reply; 4+ messages in thread
From: Alex Williamson @ 2014-11-20 15:37 UTC (permalink / raw)
  To: Zhen Lei
  Cc: KVM, linux-kernel, Zefan Li, Xinwei Hu, Tianhong Ding, Kefeng Wang

On Thu, 2014-11-20 at 19:25 +0800, Zhen Lei wrote:
> The next code fragment "list_for_each_entry" is not depend on "minor". With this
> patch, the free of "minor" in "list_for_each_entry" can be reduced, and there is
> no functional change.

A reasonable micro-optimization, but I'm curious if you're actually
seeing some measurable overhead from this.  It seems like we'd need to
have multiple devices, all within the same IOMMU group, all probed by
vfio-pci at the same time to exercise the race condition.  Thanks,

Alex

> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> ---
>  drivers/vfio/vfio.c | 13 ++++++-------
>  1 file changed, 6 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index f018d8d..737eb468 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -225,22 +225,21 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
> 
>  	mutex_lock(&vfio.group_lock);
> 
> -	minor = vfio_alloc_group_minor(group);
> -	if (minor < 0) {
> -		vfio_group_unlock_and_free(group);
> -		return ERR_PTR(minor);
> -	}
> -
>  	/* Did we race creating this group? */
>  	list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
>  		if (tmp->iommu_group == iommu_group) {
>  			vfio_group_get(tmp);
> -			vfio_free_group_minor(minor);
>  			vfio_group_unlock_and_free(group);
>  			return tmp;
>  		}
>  	}
> 
> +	minor = vfio_alloc_group_minor(group);
> +	if (minor < 0) {
> +		vfio_group_unlock_and_free(group);
> +		return ERR_PTR(minor);
> +	}
> +
>  	dev = device_create(vfio.class, NULL,
>  			    MKDEV(MAJOR(vfio.group_devt), minor),
>  			    group, "%d", iommu_group_id(iommu_group));
> --
> 1.8.0
> 
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] vfio: put off the allocation of "minor" in vfio_create_group
  2014-11-20 15:37 ` Alex Williamson
@ 2014-11-21  1:25   ` leizhen
  0 siblings, 0 replies; 4+ messages in thread
From: leizhen @ 2014-11-21  1:25 UTC (permalink / raw)
  To: Alex Williamson
  Cc: KVM, linux-kernel, Zefan Li, Xinwei Hu, Tianhong Ding, Kefeng Wang

On 2014/11/20 23:37, Alex Williamson wrote:
> On Thu, 2014-11-20 at 19:25 +0800, Zhen Lei wrote:
>> The next code fragment "list_for_each_entry" is not depend on "minor". With this
>> patch, the free of "minor" in "list_for_each_entry" can be reduced, and there is
>> no functional change.
> 
> A reasonable micro-optimization, but I'm curious if you're actually
> seeing some measurable overhead from this.  It seems like we'd need to

Oh, I just found this by code review. It's good to put pure check first and
resource allocation later, if they are order independent. Isn't it?
I think it will not improve performance but only save a little code space
and looks feel better.

> have multiple devices, all within the same IOMMU group, all probed by
> vfio-pci at the same time to exercise the race condition.  Thanks,

I think vfio_create_group can not be invoked frequently. So, the performance of this function
is not a major consideration, we don't care slightly performance change.

> 
> Alex
> 
>> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
>> ---
>>  drivers/vfio/vfio.c | 13 ++++++-------
>>  1 file changed, 6 insertions(+), 7 deletions(-)
>>
>> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
>> index f018d8d..737eb468 100644
>> --- a/drivers/vfio/vfio.c
>> +++ b/drivers/vfio/vfio.c
>> @@ -225,22 +225,21 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
>>
>>  	mutex_lock(&vfio.group_lock);
>>
>> -	minor = vfio_alloc_group_minor(group);
>> -	if (minor < 0) {
>> -		vfio_group_unlock_and_free(group);
>> -		return ERR_PTR(minor);
>> -	}
>> -
>>  	/* Did we race creating this group? */
>>  	list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
>>  		if (tmp->iommu_group == iommu_group) {
>>  			vfio_group_get(tmp);
>> -			vfio_free_group_minor(minor);
>>  			vfio_group_unlock_and_free(group);
>>  			return tmp;
>>  		}
>>  	}
>>
>> +	minor = vfio_alloc_group_minor(group);
>> +	if (minor < 0) {
>> +		vfio_group_unlock_and_free(group);
>> +		return ERR_PTR(minor);
>> +	}
>> +
>>  	dev = device_create(vfio.class, NULL,
>>  			    MKDEV(MAJOR(vfio.group_devt), minor),
>>  			    group, "%d", iommu_group_id(iommu_group));
>> --
>> 1.8.0
>>
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>> Please read the FAQ at  http://www.tux.org/lkml/
> 
> 
> 
> 
> .
> 



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] vfio: put off the allocation of "minor" in vfio_create_group
  2014-11-20 11:25 [PATCH 1/1] vfio: put off the allocation of "minor" in vfio_create_group Zhen Lei
  2014-11-20 15:37 ` Alex Williamson
@ 2015-03-16 22:00 ` Alex Williamson
  1 sibling, 0 replies; 4+ messages in thread
From: Alex Williamson @ 2015-03-16 22:00 UTC (permalink / raw)
  To: Zhen Lei
  Cc: KVM, linux-kernel, Zefan Li, Xinwei Hu, Tianhong Ding, Kefeng Wang

On Thu, 2014-11-20 at 19:25 +0800, Zhen Lei wrote:
> The next code fragment "list_for_each_entry" is not depend on "minor". With this
> patch, the free of "minor" in "list_for_each_entry" can be reduced, and there is
> no functional change.
> 
> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> ---
>  drivers/vfio/vfio.c | 13 ++++++-------
>  1 file changed, 6 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index f018d8d..737eb468 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -225,22 +225,21 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
> 
>  	mutex_lock(&vfio.group_lock);
> 
> -	minor = vfio_alloc_group_minor(group);
> -	if (minor < 0) {
> -		vfio_group_unlock_and_free(group);
> -		return ERR_PTR(minor);
> -	}
> -
>  	/* Did we race creating this group? */
>  	list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
>  		if (tmp->iommu_group == iommu_group) {
>  			vfio_group_get(tmp);
> -			vfio_free_group_minor(minor);
>  			vfio_group_unlock_and_free(group);
>  			return tmp;
>  		}
>  	}
> 
> +	minor = vfio_alloc_group_minor(group);
> +	if (minor < 0) {
> +		vfio_group_unlock_and_free(group);
> +		return ERR_PTR(minor);
> +	}
> +
>  	dev = device_create(vfio.class, NULL,
>  			    MKDEV(MAJOR(vfio.group_devt), minor),
>  			    group, "%d", iommu_group_id(iommu_group));

Applied to next for v4.1.  Sorry I didn't put this in earlier, it got
lost in my inbox.  Thanks,

Alex


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-03-16 22:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-20 11:25 [PATCH 1/1] vfio: put off the allocation of "minor" in vfio_create_group Zhen Lei
2014-11-20 15:37 ` Alex Williamson
2014-11-21  1:25   ` leizhen
2015-03-16 22:00 ` Alex Williamson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).