Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp528677pxb; Thu, 14 Jan 2021 11:41:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJx9WQicxvwxY9eZXVvWx2YMaXERoE3E7GZPr4a6oG6NWIjKRJhPzTTmWubVCxHAtdCNj+Ae X-Received: by 2002:a17:906:af89:: with SMTP id mj9mr5545282ejb.528.1610653318838; Thu, 14 Jan 2021 11:41:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610653318; cv=none; d=google.com; s=arc-20160816; b=TU1wZjVq1liVRi79iONN4jUluTBkXtt/a5y2n4MWyfIjTxxKnKulUTJToXLMmCayh1 VC1om3ZHnsv6AXSrM7mrisrMjgmsw+W9wI00qVO7rI92bkv8lXkf0C+ZRFfIQjDsoDG+ 359G9I4lUBPCMNTSgyvPIypWJaGcQoz/TH2Lnpw9VO9Rfe4PTsCiNVyDzBDkwAFWHZzP 0gUFA8UHgq4fexYKa6O9jqXoGvnm3qDWFnvEbhuVQyKF8TBD6AyglFwnQFBKrtiSMT3k 9jN/CKrhol29a8hjdWltrGZyhPx1hRx46ba6nGTxE7OOYb10izfI4nVwb25pC5wLWO42 SKvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=yjAqxhBQ9KVJPQ7Xln3UosRFIz/jPg+p3aP0w5NuCFo=; b=DA2syOnGremez+CCwdTIHzBBrjWvMZ3b3GWxNF3w0OmaEoMejj8PjAd9EAyDzLFoQh 26jhuEM1nccbMyj3G1eaBhe707/ioVLEsrdFpvq0lH0jfHjt8KAKnFonwOLVFfFGcOby UZxWL2RtaIwJ1GibN6vB5WWkLzdyguK6+ZPIUx+ePxoXgRtbAloQS+O2V3X6BsZWIc+r 8XYwaB417zAj9zaoJtnVWbuw6ACeOno4wJfANzZp+M+s24jj2kglm3tRGSR5n3EDQZMZ Z923ShqflTtt5j5SXr/04Puc1Vj+BXPNaq8MAEC/TYy8p0LqRNg+gDcRb/H+ASg+D2YX Rqfg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j24si2831745ejs.60.2021.01.14.11.41.35; Thu, 14 Jan 2021 11:41:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730232AbhANTj3 (ORCPT + 99 others); Thu, 14 Jan 2021 14:39:29 -0500 Received: from mail-ot1-f52.google.com ([209.85.210.52]:45048 "EHLO mail-ot1-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728380AbhANTj3 (ORCPT ); Thu, 14 Jan 2021 14:39:29 -0500 Received: by mail-ot1-f52.google.com with SMTP id r9so6266742otk.11; Thu, 14 Jan 2021 11:39:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=yjAqxhBQ9KVJPQ7Xln3UosRFIz/jPg+p3aP0w5NuCFo=; b=QESmLSD4Bl8l4488Xq7QtSflwNp3n6+vpoe1MaxdGJJg7iJbHXhNXDWtXqrp1juOeO kB6jrCihdOAI5lkVCt4tEiElsvjTnX2PR5Ns93Vg2uN7FpIas5Hz/KXnLiLajcoxuGEF 2lDn5OJJIm18Z5sT837BB8kzN3z07/bBviiqL3t3y0mPQ+t1M9fxYSmmBMXZ0uqD3Fh7 EjxGbjnyuK2QoRHCetatatTFkbV6Gizq+JjuKHhRbfBaMBcnzDl/njo07s8YMSDhHdHq iimuOEnKY9pLIBQxSkMeiZWskGgv0ZdPRtSBSeJOmvwxWmFZuMHJK5Y/QW2ZSI7r8K8P DoGQ== X-Gm-Message-State: AOAM532HQy8x4+2TSd33YPebptc+KcFVgWkBlppOM2Uz7YlGw7HGeByO iTt3iXuU1rasR0zN9lUB0TarM2ukUmmZkwswApSMzAay X-Received: by 2002:a9d:745a:: with SMTP id p26mr5817920otk.206.1610653128501; Thu, 14 Jan 2021 11:38:48 -0800 (PST) MIME-Version: 1.0 References: <2073294.4OfjquceTg@kreacher> In-Reply-To: From: "Rafael J. Wysocki" Date: Thu, 14 Jan 2021 20:38:37 +0100 Message-ID: Subject: Re: [PATCH] driver core: Extend device_is_dependent() To: Saravana Kannan Cc: "Rafael J. Wysocki" , Greg Kroah-Hartman , LKML , Linux PM , Stephan Gerhold Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jan 14, 2021 at 8:32 PM Saravana Kannan wrote: > > On Thu, Jan 14, 2021 at 10:41 AM Rafael J. Wysocki wrote: > > > > 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 > > --- > > 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); > > > > The code works, but it's not at all obvious what it's doing. Because, > at first glance, it's easy to mistakenly think that it's trying to > catch this case: > dev <- child1 <- child2 <- target > > Maybe it's clearer if we do this check inside the loop? Which of the loops do you mean? There are two of them and both need to do the check in each step AFAICS. > Something like: > > if (link->consumer == target || > device_is_ancestor(link->consumer, target)) > return 1; So would this be sufficient?