mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] virtio: use helper function is_power_of_2()
@ 2022-10-21  6:27 shaoqin.huang
  2022-10-21  6:27 ` [PATCH 1/2] virtio_pci: " shaoqin.huang
  2022-10-21  6:27 ` [PATCH 2/2] virtio_ring: " shaoqin.huang
  0 siblings, 2 replies; 8+ messages in thread
From: shaoqin.huang @ 2022-10-21  6:27 UTC (permalink / raw)
  To: mst, jasowang; +Cc: Shaoqin Huang, linux-kernel, virtualization

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.

Shaoqin Huang (2):
  virtio_pci: use helper function is_power_of_2()
  virtio_ring: use helper function is_power_of_2()

 drivers/virtio/virtio_pci_modern.c | 2 +-
 drivers/virtio/virtio_ring.c       | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

-- 
2.34.1


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

* [PATCH 1/2] virtio_pci: use helper function is_power_of_2()
  2022-10-21  6:27 [PATCH 0/2] virtio: use helper function is_power_of_2() shaoqin.huang
@ 2022-10-21  6:27 ` shaoqin.huang
  2022-10-21  8:47   ` Philippe Mathieu-Daudé
  2022-10-24 11:33   ` Xuan Zhuo
  2022-10-21  6:27 ` [PATCH 2/2] virtio_ring: " shaoqin.huang
  1 sibling, 2 replies; 8+ messages in thread
From: shaoqin.huang @ 2022-10-21  6:27 UTC (permalink / raw)
  To: mst, jasowang; +Cc: Shaoqin Huang, virtualization, linux-kernel

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_pci_modern.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c
index c3b9f2761849..207294bd7b9d 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -310,7 +310,7 @@ static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
 	if (!num || vp_modern_get_queue_enable(mdev, index))
 		return ERR_PTR(-ENOENT);
 
-	if (num & (num - 1)) {
+	if (!is_power_of_2(num)) {
 		dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
 		return ERR_PTR(-EINVAL);
 	}
-- 
2.34.1


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

* [PATCH 2/2] virtio_ring: use helper function is_power_of_2()
  2022-10-21  6:27 [PATCH 0/2] virtio: use helper function is_power_of_2() shaoqin.huang
  2022-10-21  6:27 ` [PATCH 1/2] virtio_pci: " shaoqin.huang
@ 2022-10-21  6:27 ` shaoqin.huang
  2022-10-21  8:53   ` Philippe Mathieu-Daudé
  2022-10-24 11:33   ` Xuan Zhuo
  1 sibling, 2 replies; 8+ messages in thread
From: shaoqin.huang @ 2022-10-21  6:27 UTC (permalink / raw)
  To: mst, jasowang; +Cc: Shaoqin Huang, virtualization, linux-kernel

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;
 	}
-- 
2.34.1


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

* Re: [PATCH 1/2] virtio_pci: use helper function is_power_of_2()
  2022-10-21  6:27 ` [PATCH 1/2] virtio_pci: " shaoqin.huang
@ 2022-10-21  8:47   ` Philippe Mathieu-Daudé
  2022-10-24 11:33   ` Xuan Zhuo
  1 sibling, 0 replies; 8+ messages in thread
From: Philippe Mathieu-Daudé @ 2022-10-21  8:47 UTC (permalink / raw)
  To: shaoqin.huang, mst, jasowang; +Cc: linux-kernel, virtualization

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_pci_modern.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>


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

* Re: [PATCH 2/2] virtio_ring: use helper function is_power_of_2()
  2022-10-21  6:27 ` [PATCH 2/2] virtio_ring: " shaoqin.huang
@ 2022-10-21  8:53   ` Philippe Mathieu-Daudé
  2022-10-22 12:31     ` Huang, Shaoqin
  2022-10-24 11:33   ` Xuan Zhuo
  1 sibling, 1 reply; 8+ messages in thread
From: Philippe Mathieu-Daudé @ 2022-10-21  8:53 UTC (permalink / raw)
  To: shaoqin.huang, mst, jasowang; +Cc: linux-kernel, virtualization

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.

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

* Re: [PATCH 2/2] virtio_ring: use helper function is_power_of_2()
  2022-10-21  8:53   ` Philippe Mathieu-Daudé
@ 2022-10-22 12:31     ` Huang, Shaoqin
  0 siblings, 0 replies; 8+ messages in thread
From: Huang, Shaoqin @ 2022-10-22 12:31 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, mst, jasowang; +Cc: linux-kernel, virtualization



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.

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

* Re: [PATCH 1/2] virtio_pci: use helper function is_power_of_2()
  2022-10-21  6:27 ` [PATCH 1/2] virtio_pci: " shaoqin.huang
  2022-10-21  8:47   ` Philippe Mathieu-Daudé
@ 2022-10-24 11:33   ` Xuan Zhuo
  1 sibling, 0 replies; 8+ messages in thread
From: Xuan Zhuo @ 2022-10-24 11:33 UTC (permalink / raw)
  To: shaoqin.huang; +Cc: linux-kernel, virtualization, mst, jasowang

On Thu, 20 Oct 2022 23:27:33 -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_pci_modern.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c
> index c3b9f2761849..207294bd7b9d 100644
> --- a/drivers/virtio/virtio_pci_modern.c
> +++ b/drivers/virtio/virtio_pci_modern.c
> @@ -310,7 +310,7 @@ static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
>  	if (!num || vp_modern_get_queue_enable(mdev, index))
>  		return ERR_PTR(-ENOENT);
>
> -	if (num & (num - 1)) {
> +	if (!is_power_of_2(num)) {
>  		dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
>  		return ERR_PTR(-EINVAL);
>  	}
> --
> 2.34.1
>
> _______________________________________________
> Virtualization mailing list
> Virtualization@lists.linux-foundation.org
> https://lists.linuxfoundation.org/mailman/listinfo/virtualization

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

* Re: [PATCH 2/2] virtio_ring: use helper function is_power_of_2()
  2022-10-21  6:27 ` [PATCH 2/2] virtio_ring: " shaoqin.huang
  2022-10-21  8:53   ` Philippe Mathieu-Daudé
@ 2022-10-24 11:33   ` Xuan Zhuo
  1 sibling, 0 replies; 8+ messages in thread
From: Xuan Zhuo @ 2022-10-24 11:33 UTC (permalink / raw)
  To: shaoqin.huang; +Cc: linux-kernel, virtualization, mst, jasowang

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

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

end of thread, other threads:[~2022-10-24 11:35 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-21  6:27 [PATCH 0/2] virtio: use helper function is_power_of_2() shaoqin.huang
2022-10-21  6:27 ` [PATCH 1/2] virtio_pci: " shaoqin.huang
2022-10-21  8:47   ` Philippe Mathieu-Daudé
2022-10-24 11:33   ` Xuan Zhuo
2022-10-21  6:27 ` [PATCH 2/2] virtio_ring: " shaoqin.huang
2022-10-21  8:53   ` Philippe Mathieu-Daudé
2022-10-22 12:31     ` Huang, Shaoqin
2022-10-24 11:33   ` Xuan Zhuo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®