diff mbox series

[v2,27/35] media: em28xx: cx231xx: fix opencoded find_and_set_bit()

Message ID 20231203193307.542794-26-yury.norov@gmail.com
State Superseded
Headers show
Series bitops: add atomic find_bit() operations | expand

Commit Message

Yury Norov Dec. 3, 2023, 7:32 p.m. UTC
Functions in the media/usb drivers opencode find_and_set_bit() by
polling on a found bit in a while-loop.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 drivers/media/usb/cx231xx/cx231xx-cards.c | 16 ++++------
 drivers/media/usb/em28xx/em28xx-cards.c   | 37 +++++++++--------------
 2 files changed, 21 insertions(+), 32 deletions(-)

Comments

Hans Verkuil Dec. 4, 2023, 8:39 a.m. UTC | #1
On 03/12/2023 20:32, Yury Norov wrote:
> Functions in the media/usb drivers opencode find_and_set_bit() by
> polling on a found bit in a while-loop.
> 
> Signed-off-by: Yury Norov <yury.norov@gmail.com>

Acked-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>

Feel free to take this yourself through the bitmap tree.

It's a nice improvement.

Regards,

	Hans

> ---
>  drivers/media/usb/cx231xx/cx231xx-cards.c | 16 ++++------
>  drivers/media/usb/em28xx/em28xx-cards.c   | 37 +++++++++--------------
>  2 files changed, 21 insertions(+), 32 deletions(-)
> 
> diff --git a/drivers/media/usb/cx231xx/cx231xx-cards.c b/drivers/media/usb/cx231xx/cx231xx-cards.c
> index 92efe6c1f47b..b314603932d7 100644
> --- a/drivers/media/usb/cx231xx/cx231xx-cards.c
> +++ b/drivers/media/usb/cx231xx/cx231xx-cards.c
> @@ -1708,16 +1708,12 @@ static int cx231xx_usb_probe(struct usb_interface *interface,
>  		return -ENODEV;
>  
>  	/* Check to see next free device and mark as used */
> -	do {
> -		nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
> -		if (nr >= CX231XX_MAXBOARDS) {
> -			/* No free device slots */
> -			dev_err(d,
> -				"Supports only %i devices.\n",
> -				CX231XX_MAXBOARDS);
> -			return -ENOMEM;
> -		}
> -	} while (test_and_set_bit(nr, &cx231xx_devused));
> +	nr = find_and_set_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
> +	if (nr >= CX231XX_MAXBOARDS) {
> +		/* No free device slots */
> +		dev_err(d, "Supports only %i devices.\n", CX231XX_MAXBOARDS);
> +		return -ENOMEM;
> +	}
>  
>  	udev = usb_get_dev(interface_to_usbdev(interface));
>  
> diff --git a/drivers/media/usb/em28xx/em28xx-cards.c b/drivers/media/usb/em28xx/em28xx-cards.c
> index 4d037c92af7c..af4809fe74a8 100644
> --- a/drivers/media/usb/em28xx/em28xx-cards.c
> +++ b/drivers/media/usb/em28xx/em28xx-cards.c
> @@ -3684,17 +3684,14 @@ static int em28xx_duplicate_dev(struct em28xx *dev)
>  		return -ENOMEM;
>  	}
>  	/* Check to see next free device and mark as used */
> -	do {
> -		nr = find_first_zero_bit(em28xx_devused, EM28XX_MAXBOARDS);
> -		if (nr >= EM28XX_MAXBOARDS) {
> -			/* No free device slots */
> -			dev_warn(&dev->intf->dev, ": Supports only %i em28xx boards.\n",
> -				 EM28XX_MAXBOARDS);
> -			kfree(sec_dev);
> -			dev->dev_next = NULL;
> -			return -ENOMEM;
> -		}
> -	} while (test_and_set_bit(nr, em28xx_devused));
> +	nr = find_and_set_bit(em28xx_devused, EM28XX_MAXBOARDS);
> +	if (nr >= EM28XX_MAXBOARDS) {
> +		/* No free device slots */
> +		dev_warn(&dev->intf->dev, ": Supports only %i em28xx boards.\n", EM28XX_MAXBOARDS);
> +		kfree(sec_dev);
> +		dev->dev_next = NULL;
> +		return -ENOMEM;
> +	}
>  	sec_dev->devno = nr;
>  	snprintf(sec_dev->name, 28, "em28xx #%d", nr);
>  	sec_dev->dev_next = NULL;
> @@ -3827,17 +3824,13 @@ static int em28xx_usb_probe(struct usb_interface *intf,
>  	udev = usb_get_dev(interface_to_usbdev(intf));
>  
>  	/* Check to see next free device and mark as used */
> -	do {
> -		nr = find_first_zero_bit(em28xx_devused, EM28XX_MAXBOARDS);
> -		if (nr >= EM28XX_MAXBOARDS) {
> -			/* No free device slots */
> -			dev_err(&intf->dev,
> -				"Driver supports up to %i em28xx boards.\n",
> -			       EM28XX_MAXBOARDS);
> -			retval = -ENOMEM;
> -			goto err_no_slot;
> -		}
> -	} while (test_and_set_bit(nr, em28xx_devused));
> +	nr = find_and_set_bit(em28xx_devused, EM28XX_MAXBOARDS);
> +	if (nr >= EM28XX_MAXBOARDS) {
> +		/* No free device slots */
> +		dev_err(&intf->dev, "Driver supports up to %i em28xx boards.\n", EM28XX_MAXBOARDS);
> +		retval = -ENOMEM;
> +		goto err_no_slot;
> +	}
>  
>  	/* Don't register audio interfaces */
>  	if (intf->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Andy Shevchenko Dec. 4, 2023, 1:05 p.m. UTC | #2
On Mon, Dec 04, 2023 at 09:39:59AM +0100, Hans Verkuil wrote:
> On 03/12/2023 20:32, Yury Norov wrote:
> > Functions in the media/usb drivers opencode find_and_set_bit() by
> > polling on a found bit in a while-loop.

...

> It's a nice improvement.

Wouldn't it be even nicer to utilise IDA framework?
Hans Verkuil Dec. 4, 2023, 1:09 p.m. UTC | #3
On 04/12/2023 14:05, Andy Shevchenko wrote:
> On Mon, Dec 04, 2023 at 09:39:59AM +0100, Hans Verkuil wrote:
>> On 03/12/2023 20:32, Yury Norov wrote:
>>> Functions in the media/usb drivers opencode find_and_set_bit() by
>>> polling on a found bit in a while-loop.
> 
> ...
> 
>> It's a nice improvement.
> 
> Wouldn't it be even nicer to utilise IDA framework?
> 

Not worth the effort IMHO.

Regards,

	Hans
diff mbox series

Patch

diff --git a/drivers/media/usb/cx231xx/cx231xx-cards.c b/drivers/media/usb/cx231xx/cx231xx-cards.c
index 92efe6c1f47b..b314603932d7 100644
--- a/drivers/media/usb/cx231xx/cx231xx-cards.c
+++ b/drivers/media/usb/cx231xx/cx231xx-cards.c
@@ -1708,16 +1708,12 @@  static int cx231xx_usb_probe(struct usb_interface *interface,
 		return -ENODEV;
 
 	/* Check to see next free device and mark as used */
-	do {
-		nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
-		if (nr >= CX231XX_MAXBOARDS) {
-			/* No free device slots */
-			dev_err(d,
-				"Supports only %i devices.\n",
-				CX231XX_MAXBOARDS);
-			return -ENOMEM;
-		}
-	} while (test_and_set_bit(nr, &cx231xx_devused));
+	nr = find_and_set_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
+	if (nr >= CX231XX_MAXBOARDS) {
+		/* No free device slots */
+		dev_err(d, "Supports only %i devices.\n", CX231XX_MAXBOARDS);
+		return -ENOMEM;
+	}
 
 	udev = usb_get_dev(interface_to_usbdev(interface));
 
diff --git a/drivers/media/usb/em28xx/em28xx-cards.c b/drivers/media/usb/em28xx/em28xx-cards.c
index 4d037c92af7c..af4809fe74a8 100644
--- a/drivers/media/usb/em28xx/em28xx-cards.c
+++ b/drivers/media/usb/em28xx/em28xx-cards.c
@@ -3684,17 +3684,14 @@  static int em28xx_duplicate_dev(struct em28xx *dev)
 		return -ENOMEM;
 	}
 	/* Check to see next free device and mark as used */
-	do {
-		nr = find_first_zero_bit(em28xx_devused, EM28XX_MAXBOARDS);
-		if (nr >= EM28XX_MAXBOARDS) {
-			/* No free device slots */
-			dev_warn(&dev->intf->dev, ": Supports only %i em28xx boards.\n",
-				 EM28XX_MAXBOARDS);
-			kfree(sec_dev);
-			dev->dev_next = NULL;
-			return -ENOMEM;
-		}
-	} while (test_and_set_bit(nr, em28xx_devused));
+	nr = find_and_set_bit(em28xx_devused, EM28XX_MAXBOARDS);
+	if (nr >= EM28XX_MAXBOARDS) {
+		/* No free device slots */
+		dev_warn(&dev->intf->dev, ": Supports only %i em28xx boards.\n", EM28XX_MAXBOARDS);
+		kfree(sec_dev);
+		dev->dev_next = NULL;
+		return -ENOMEM;
+	}
 	sec_dev->devno = nr;
 	snprintf(sec_dev->name, 28, "em28xx #%d", nr);
 	sec_dev->dev_next = NULL;
@@ -3827,17 +3824,13 @@  static int em28xx_usb_probe(struct usb_interface *intf,
 	udev = usb_get_dev(interface_to_usbdev(intf));
 
 	/* Check to see next free device and mark as used */
-	do {
-		nr = find_first_zero_bit(em28xx_devused, EM28XX_MAXBOARDS);
-		if (nr >= EM28XX_MAXBOARDS) {
-			/* No free device slots */
-			dev_err(&intf->dev,
-				"Driver supports up to %i em28xx boards.\n",
-			       EM28XX_MAXBOARDS);
-			retval = -ENOMEM;
-			goto err_no_slot;
-		}
-	} while (test_and_set_bit(nr, em28xx_devused));
+	nr = find_and_set_bit(em28xx_devused, EM28XX_MAXBOARDS);
+	if (nr >= EM28XX_MAXBOARDS) {
+		/* No free device slots */
+		dev_err(&intf->dev, "Driver supports up to %i em28xx boards.\n", EM28XX_MAXBOARDS);
+		retval = -ENOMEM;
+		goto err_no_slot;
+	}
 
 	/* Don't register audio interfaces */
 	if (intf->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {