From patchwork Thu Jan 14 18:41:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rafael J. Wysocki" X-Patchwork-Id: 363228 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97BABC433E0 for ; Thu, 14 Jan 2021 18:42:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 49ABE23B3E for ; Thu, 14 Jan 2021 18:42:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726960AbhANSmg (ORCPT ); Thu, 14 Jan 2021 13:42:36 -0500 Received: from cloudserver094114.home.pl ([79.96.170.134]:53368 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726482AbhANSmg (ORCPT ); Thu, 14 Jan 2021 13:42:36 -0500 Received: from 89-64-81-33.dynamic.chello.pl (89.64.81.33) (HELO kreacher.localnet) by serwer1319399.home.pl (79.96.170.134) with SMTP (IdeaSmtpServer 0.83.537) id 53ba55cc67cb7881; Thu, 14 Jan 2021 19:41:54 +0100 From: "Rafael J. Wysocki" To: Greg Kroah-Hartman Cc: LKML , Linux PM , Stephan Gerhold , Saravana Kannan Subject: [PATCH] driver core: Extend device_is_dependent() Date: Thu, 14 Jan 2021 19:41:53 +0100 Message-ID: <2073294.4OfjquceTg@kreacher> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Rafael J. Wysocki When adding a new device link, device_is_dependent() is used to check whether or not the prospective supplier device does not depend on the prospective consumer one to avoid adding loops to the graph of device dependencies. However, device_is_dependent() does not take the ancestors of the target device into account, so it may not detect an existing reverse dependency if, for example, the parent of the target device depends on the device passed as its first argument. For this reason, extend device_is_dependent() to also check if the device passed as its first argument is an ancestor of the target one and return 1 if that is the case. Signed-off-by: Rafael J. Wysocki Reported-by: Stephan Gerhold Tested-by: Stephan Gerhold --- drivers/base/core.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) Index: linux-pm/drivers/base/core.c =================================================================== --- linux-pm.orig/drivers/base/core.c +++ linux-pm/drivers/base/core.c @@ -208,6 +208,16 @@ int device_links_read_lock_held(void) #endif #endif /* !CONFIG_SRCU */ +static bool device_is_ancestor(struct device *dev, struct device *target) +{ + while (target->parent) { + target = target->parent; + if (dev == target) + return true; + } + return false; +} + /** * device_is_dependent - Check if one device depends on another one * @dev: Device to check dependencies for. @@ -221,7 +231,7 @@ int device_is_dependent(struct device *d struct device_link *link; int ret; - if (dev == target) + if (dev == target || device_is_ancestor(dev, target)) return 1; ret = device_for_each_child(dev, target, device_is_dependent);