Skip to content

Commit

Permalink
hotplug_exit: remove parents recursively
Browse files Browse the repository at this point in the history
Remove all parents with a single child remaining in parent tree.
This ensures that no parents of the direct parent of the device being considered
are left in the list, when appearing before their child(ren) in the processing order
of the context device list cleaning.
  • Loading branch information
sonatique committed Feb 13, 2024
1 parent b452ca6 commit 4067d23
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions libusb/hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ void usbi_hotplug_init(struct libusb_context *ctx)
usbi_atomic_store(&ctx->hotplug_ready, 1);
}

static void usbi_recursively_remove_parents(struct libusb_device *dev, struct libusb_device *next_dev)
{
if (dev && dev->parent_dev) {
if (usbi_atomic_load(&dev->parent_dev->refcnt) == 1) {
/* the parent was processed before this device in the list
* and therefore has its ref count already decrement for its own ref.
* The only remaining counted ref come from its remaining single child.
* It will thus be released when its child will be released.
* We remove it from the list. This is safe as parent_dev can not be
* equal to next_dev given we know at this point that it was
* previously seen in the list. */
assert (dev->parent_dev != next_dev);
if(dev->parent_dev->list.next && dev->parent_dev->list.prev) {
list_del(&dev->parent_dev->list);
}
}

usbi_recursively_remove_parents(dev->parent_dev, next_dev);
}
}

void usbi_hotplug_exit(struct libusb_context *ctx)
{
struct usbi_hotplug_callback *hotplug_cb, *next_cb;
Expand Down Expand Up @@ -193,21 +214,17 @@ void usbi_hotplug_exit(struct libusb_context *ctx)
free(msg);
}

/* free all discovered devices. due to parent references loop until no devices are freed. */
/* free all discovered devices */
for_each_device_safe(ctx, dev, next_dev) {
/* remove the device from the usb_devs list only if there are no
* references held, otherwise leave it on the list so that a
* warning message will be shown */
if (usbi_atomic_load(&dev->refcnt) == 1) {
list_del(&dev->list);
}
if (dev->parent_dev && usbi_atomic_load(&dev->parent_dev->refcnt) == 1) {
/* the parent was before this device in the list and will be released.
remove it from the list. this is safe as parent_dev can not be
equal to next_dev. */
assert (dev->parent_dev != next_dev);
list_del(&dev->parent_dev->list);
}

usbi_recursively_remove_parents(dev, next_dev);

libusb_unref_device(dev);
}

Expand Down

0 comments on commit 4067d23

Please sign in to comment.