driver core: Extend device_is_dependent()

If the device passed as the target (second argument) to
device_is_dependent() is not completely registered (that is, it has
been initialized, but not added yet), but the parent pointer of it
is set, it may be missing from the list of the parent's children
and device_for_each_child() called by device_is_dependent() cannot
be relied on to catch that dependency.

For this reason, modify device_is_dependent() to check the ancestors
of the target device by following its parent pointer in addition to
the device_for_each_child() walk.

Fixes: 9ed9895370ae ("driver core: Functional dependencies tracking support")
Reported-by: Stephan Gerhold <stephan@gerhold.net>
Tested-by: Stephan Gerhold <stephan@gerhold.net>
Reviewed-by: Saravana Kannan <saravanak@google.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Link: https://lore.kernel.org/r/17705994.d592GUb2YH@kreacher
Cc: stable <stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by Rafael J. Wysocki and committed by Greg Kroah-Hartman 3d1cf435 f2d6c270

+16 -1
+16 -1
drivers/base/core.c
··· 208 #endif 209 #endif /* !CONFIG_SRCU */ 210 211 /** 212 * device_is_dependent - Check if one device depends on another one 213 * @dev: Device to check dependencies for. ··· 231 struct device_link *link; 232 int ret; 233 234 - if (dev == target) 235 return 1; 236 237 ret = device_for_each_child(dev, target, device_is_dependent);
··· 208 #endif 209 #endif /* !CONFIG_SRCU */ 210 211 + static bool device_is_ancestor(struct device *dev, struct device *target) 212 + { 213 + while (target->parent) { 214 + target = target->parent; 215 + if (dev == target) 216 + return true; 217 + } 218 + return false; 219 + } 220 + 221 /** 222 * device_is_dependent - Check if one device depends on another one 223 * @dev: Device to check dependencies for. ··· 221 struct device_link *link; 222 int ret; 223 224 + /* 225 + * The "ancestors" check is needed to catch the case when the target 226 + * device has not been completely initialized yet and it is still 227 + * missing from the list of children of its parent device. 228 + */ 229 + if (dev == target || device_is_ancestor(dev, target)) 230 return 1; 231 232 ret = device_for_each_child(dev, target, device_is_dependent);