staging: vme_user: simplify bus number allocation using ffz()
PATCH v3 1/1 staging: vme_user: simplify bus number allocation using ffz()
David Laight pointed out that the loop in vme_register_bridge() is effectively searching for the first zero bit in vme_bus_numbers. Add a check for vme_bus_numbers == ~0UL before calling ffz().
Replace the manual loop with ffz() and use BIT(n) for consistency.
--- drivers/staging/vme_user/vme.c | 15 +++++++++++++---------- 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/staging/vme_user/vme.c +15 -10
@@ -1765,16 +1765,19 @@ int vme_register_bridge(struct vme_bridge *bridge)int ret =-1 ;mutex_lock (&vme_buses_lock); -for (i =0 ; i <sizeof (vme_bus_numbers) *8 ; i++){ -if ((vme_bus_numbers & (1 << i)) ==0 ) { - vme_bus_numbers |= (1 << i); - bridge->num = i; -INIT_LIST_HEAD (&bridge->devices); -list_add_tail (&bridge->bus_list, &vme_bus_list); - ret =0 ; -break ; - } +/* Ensure we have a free slot before using ffz() */ +if (vme_bus_numbers == ~0UL ) + goto out;/* No free bus number */ + +/* Find the first clear bit */ + i =ffz (vme_bus_numbers); + + vme_bus_numbers |=BIT (i); + bridge->num = i; +INIT_LIST_HEAD (&bridge->devices); +list_add_tail (&bridge->bus_list, &vme_bus_list); + ret =0 ; + +out:mutex_unlock (&vme_buses_lock);return ret;
@@ -1787,7 +1790,7 @@ void vme_unregister_bridge(struct vme_bridge *bridge)struct vme_dev *tmp;mutex_lock (&vme_buses_lock); - vme_bus_numbers &= ~(1 << bridge->num); + vme_bus_numbers &= ~BIT (bridge->num);list_for_each_entry_safe (vdev, tmp, &bridge->devices, bridge_list) {list_del (&vdev->drv_list);list_del (&vdev->bridge_list);
Signature · 1 line
2.43.0
Quoted 9 lines · Dharanitharan R
David Laight pointed out that the loop in
vme_register_bridge()is effectively searching for the first zero bit invme_bus_numbers. Add a check forvme_bus_numbers== ~0UL before callingffz().Replace the manual loop with
ffz()and use BIT(n) for consistency.Suggested-by David Laight <[email protected]>Signed-off-by Dharanitharan R <[email protected]>---
You need to write a change log here after the --- cut off line.
https://staticthinking.wordpress.com/2022/07/27/how-to-send-a-v2-patch/
This still doesn't apply.
regards,
dan carpenter