From 714dd2252dacfaabf0a72b31b9b7e22a8d816460 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Thu, 5 Mar 2026 19:36:10 -0700 Subject: dm: Move flags_remove() check before child removal Move the flags_remove() call before device_chld_remove() and save the result in a separate variable. This is just a refactoring with no behaviour change, preparing for the next commit which needs to know whether the parent will be removed before deciding how to remove its children. Signed-off-by: Simon Glass --- drivers/core/device-remove.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'drivers') diff --git a/drivers/core/device-remove.c b/drivers/core/device-remove.c index 437080ed778..03ef49c4db0 100644 --- a/drivers/core/device-remove.c +++ b/drivers/core/device-remove.c @@ -198,7 +198,7 @@ static int flags_remove(uint flags, uint drv_flags) int device_remove(struct udevice *dev, uint flags) { const struct driver *drv; - int ret; + int ret, cret; if (!dev) return -EINVAL; @@ -210,6 +210,14 @@ int device_remove(struct udevice *dev, uint flags) if (ret) return ret; + /* + * Remove the device if called with the "normal" remove flag set, + * or if the remove flag matches any of the drivers remove flags + */ + drv = dev->driver; + assert(drv); + cret = flags_remove(flags, drv->flags); + /* * If the child returns EKEYREJECTED, continue. It just means that it * didn't match the flags. @@ -218,17 +226,10 @@ int device_remove(struct udevice *dev, uint flags) if (ret && ret != -EKEYREJECTED) return ret; - /* - * Remove the device if called with the "normal" remove flag set, - * or if the remove flag matches any of the drivers remove flags - */ - drv = dev->driver; - assert(drv); - ret = flags_remove(flags, drv->flags); - if (ret) { + if (cret) { log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n", - dev->name, flags, drv->flags, ret); - return ret; + dev->name, flags, drv->flags, cret); + return cret; } ret = uclass_pre_remove_device(dev); -- cgit v1.3.1 From 3632b5d63c8099c59c936c3653ece5bd04d8f6c7 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Thu, 5 Mar 2026 19:36:11 -0700 Subject: dm: Remove children when parent is removed by flags When dm_remove_devices_active() removes devices using specialised flags like DM_REMOVE_ACTIVE_ALL, a parent device may match (e.g. MMC has DM_FLAG_OS_PREPARE) while its children do not. This deactivates the parent but leaves children activated, an inconsistent state. Later, when uclass_destroy() calls device_remove() with DM_REMOVE_NORMAL on the already-deactivated parent, it returns early without touching the children. The subsequent device_unbind() then fails because the children are still activated. Fix this by dropping only the DM_REMOVE_ACTIVE_ALL requirement for child removal when the parent is being removed. This ensures children are removed along with their parent, while still preserving other flags like DM_REMOVE_NON_VITAL so that vital devices remain protected. Signed-off-by: Simon Glass --- drivers/core/device-remove.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/core/device-remove.c b/drivers/core/device-remove.c index 03ef49c4db0..557afb8d817 100644 --- a/drivers/core/device-remove.c +++ b/drivers/core/device-remove.c @@ -219,10 +219,19 @@ int device_remove(struct udevice *dev, uint flags) cret = flags_remove(flags, drv->flags); /* + * Remove all children. If this device is being removed due to + * active-DMA or OS-prepare flags, drop the active-flag requirement + * for children so they are removed even without matching active + * flags, since a deactivated device must not have activated + * children. Preserve other flags (e.g. DM_REMOVE_NON_VITAL) so + * that vital children are still protected. + * * If the child returns EKEYREJECTED, continue. It just means that it * didn't match the flags. */ - ret = device_chld_remove(dev, NULL, flags); + ret = device_chld_remove(dev, NULL, + cret ? flags : + (flags & ~DM_REMOVE_ACTIVE_ALL)); if (ret && ret != -EKEYREJECTED) return ret; -- cgit v1.3.1