From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756981AbcINBPW (ORCPT ); Tue, 13 Sep 2016 21:15:22 -0400 Received: from cloudserver094114.home.net.pl ([79.96.170.134]:61513 "HELO cloudserver094114.home.net.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1751420AbcINBPU (ORCPT ); Tue, 13 Sep 2016 21:15:20 -0400 From: "Rafael J. Wysocki" To: Lukas Wunner Cc: Linux PM list , Greg Kroah-Hartman , Alan Stern , Linux Kernel Mailing List , Tomeu Vizoso , Mark Brown , Marek Szyprowski , Kevin Hilman , Ulf Hansson , "Luis R. Rodriguez" Subject: Re: [RFC/RFT][PATCH v2 2/7] driver core: Functional dependencies tracking support Date: Wed, 14 Sep 2016 03:21:27 +0200 Message-ID: <2250917.Aes4ApWfhc@vostro.rjw.lan> User-Agent: KMail/4.11.5 (Linux/4.8.0-rc2+; KDE/4.11.5; x86_64; ; ) In-Reply-To: <20160911204336.GA371@wunner.de> References: <27296716.H9VWo8ShOm@vostro.rjw.lan> <20160911134058.GA189@wunner.de> <20160911204336.GA371@wunner.de> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="utf-8" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sunday, September 11, 2016 10:43:36 PM Lukas Wunner wrote: > On Sun, Sep 11, 2016 at 03:40:58PM +0200, Lukas Wunner wrote: > > On Thu, Sep 08, 2016 at 11:27:45PM +0200, Rafael J. Wysocki wrote: > > > +/** > > > + * device_is_dependent - Check if one device depends on another one > > > + * @dev: Device to check dependencies for. > > > + * @target: Device to check against. > > > + * > > > + * Check if @dev or any device dependent on it (its child or its consumer etc) > > > + * depends on @target. Return 1 if that is the case or 0 otherwise. > > > + */ > > > +static int device_is_dependent(struct device *dev, void *target) > > > +{ > > > + struct device_link *link; > > > + int ret; > > > + > > > + ret = device_for_each_child(dev, target, device_is_dependent); > > > + list_for_each_entry(link, &dev->links_to_consumers, s_node) { > > > + if (WARN_ON(link->consumer == target)) > > > + return 1; > > > + > > > + ret = ret || device_is_dependent(link->consumer, target); > > > + } > > > + return ret; > > > +} > > > > What happens if someone tries to add a device link from a parent > > (as the consumer) to a child (as a supplier)? You're only checking > > if target is a consumer of dev, for full correctness you'd also have > > to check if target is a parent of dev. (Or grandparent, or great- > > grandparent, ... you need to walk the tree up to the root.) > > > > > > The function can be sped up by returning immediately if a match > > is found instead of continuing searching and accumulating the > > result in ret, i.e.: > > > > if (device_for_each_child(dev, target, device_is_dependent)) > > return 1; > > > > and in the list_for_each_entry block: > > > > if (device_is_dependent(link->consumer, target)) > > return 1; > > > > Then at the end of the function "return 0". > > > > > > I'd move the WARN_ON() to the single invocation of this function in > > device_link_add(), that way it's possible to use the function as a > > helper elsewhere should the need arise. > > Oh I'm grasping only now, you want to emit a WARN for *every* > infringing child/consumer. That could lead to a WARN flood if > a developer accidentally does something really dumb, like linking > the PCI root to some PCI endpoint device, but fair enough. > > The point about linking a parent to a child still stands however. > I think a simple way to check this is to just add > > if (WARN_ON(dev == target)) > return 1; > > at the top of the function, because when someone tries to link > a parent to a child, when recursing from the parent downward > one will eventually hit that child. This will also prevent > someone from linking a device to itself. I actually would prefer to make it impossible to link a parent to a child at all. Thanks, Rafael