[2/2] virtio_ring: use helper function is_power_of_2()

Message ID 20221021062734.228881-3-shaoqin.huang@intel.com
State New
Headers
Series virtio: use helper function is_power_of_2() |

Commit Message

Huang, Shaoqin Oct. 21, 2022, 6:27 a.m. UTC
  From: Shaoqin Huang <shaoqin.huang@intel.com>

Use helper function is_power_of_2() to check if num is power of two.
Minor readability improvement.

Signed-off-by: Shaoqin Huang <shaoqin.huang@intel.com>
---
 drivers/virtio/virtio_ring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Philippe Mathieu-Daudé Oct. 21, 2022, 8:53 a.m. UTC | #1
On 21/10/22 08:27, shaoqin.huang@intel.com wrote:
> From: Shaoqin Huang <shaoqin.huang@intel.com>
> 
> Use helper function is_power_of_2() to check if num is power of two.
> Minor readability improvement.
> 
> Signed-off-by: Shaoqin Huang <shaoqin.huang@intel.com>
> ---
>   drivers/virtio/virtio_ring.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 2e7689bb933b..723c4e29e1d3 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -1052,7 +1052,7 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split,
>   	dma_addr_t dma_addr;
>   
>   	/* We assume num is a power of 2. */
> -	if (num & (num - 1)) {
> +	if (!is_power_of_2(num)) {
>   		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
>   		return -EINVAL;
>   	}

This makes the following code unreachable:

	if (!num)
		return -ENOMEM;

Do we want to move it earlier or remove it?

Regards,

Phil.
  
Huang, Shaoqin Oct. 22, 2022, 12:31 p.m. UTC | #2
On 10/21/2022 4:53 PM, Philippe Mathieu-Daudé wrote:
> On 21/10/22 08:27, shaoqin.huang@intel.com wrote:
>> From: Shaoqin Huang <shaoqin.huang@intel.com>
>>
>> Use helper function is_power_of_2() to check if num is power of two.
>> Minor readability improvement.
>>
>> Signed-off-by: Shaoqin Huang <shaoqin.huang@intel.com>
>> ---
>>   drivers/virtio/virtio_ring.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
>> index 2e7689bb933b..723c4e29e1d3 100644
>> --- a/drivers/virtio/virtio_ring.c
>> +++ b/drivers/virtio/virtio_ring.c
>> @@ -1052,7 +1052,7 @@ static int vring_alloc_queue_split(struct 
>> vring_virtqueue_split *vring_split,
>>       dma_addr_t dma_addr;
>>       /* We assume num is a power of 2. */
>> -    if (num & (num - 1)) {
>> +    if (!is_power_of_2(num)) {
>>           dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
>>           return -EINVAL;
>>       }
> 
> This makes the following code unreachable:
> 
>      if (!num)
>          return -ENOMEM;
> 
> Do we want to move it earlier or remove it?
> 

I think the following code can still be executed if num > 0, and if 
there if no enough memory, the allocation of the vring will failed and 
will decrease the num by divide 2, this will makes num equal to 0. And 
then trigger the check and return -ENOMEM.

> Regards,
> 
> Phil.
  
Xuan Zhuo Oct. 24, 2022, 11:33 a.m. UTC | #3
On Thu, 20 Oct 2022 23:27:34 -0700, shaoqin.huang@intel.com wrote:
> From: Shaoqin Huang <shaoqin.huang@intel.com>
>
> Use helper function is_power_of_2() to check if num is power of two.
> Minor readability improvement.
>
> Signed-off-by: Shaoqin Huang <shaoqin.huang@intel.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> ---
>  drivers/virtio/virtio_ring.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 2e7689bb933b..723c4e29e1d3 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -1052,7 +1052,7 @@ static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split,
>  	dma_addr_t dma_addr;
>
>  	/* We assume num is a power of 2. */
> -	if (num & (num - 1)) {
> +	if (!is_power_of_2(num)) {
>  		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
>  		return -EINVAL;
>  	}
> --
> 2.34.1
>
> _______________________________________________
> Virtualization mailing list
> Virtualization@lists.linux-foundation.org
> https://lists.linuxfoundation.org/mailman/listinfo/virtualization
  

Patch

diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 2e7689bb933b..723c4e29e1d3 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -1052,7 +1052,7 @@  static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split,
 	dma_addr_t dma_addr;
 
 	/* We assume num is a power of 2. */
-	if (num & (num - 1)) {
+	if (!is_power_of_2(num)) {
 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
 		return -EINVAL;
 	}