linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephen Boyd <sboyd@kernel.org>
To: Danilo Krummrich <dakr@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Len Brown <len.brown@intel.com>,
	Michael Turquette <mturquette@baylibre.com>,
	Miquel Raynal <miquel.raynal@bootlin.com>,
	Pavel Machek <pavel@ucw.cz>,
	Rafael J. Wysocki <rafael@kernel.org>
Cc: "Thomas Petazzoni" <thomas.petazzoni@bootlin.com>,
	linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-clk@vger.kernel.org, "Chen-Yu Tsai" <wenst@chromium.org>,
	"Lucas Stach" <l.stach@pengutronix.de>,
	"Laurent Pinchart" <laurent.pinchart@ideasonboard.com>,
	"Marek Vasut" <marex@denx.de>,
	"Ulf Hansson" <ulf.hansson@linaro.org>,
	"Kevin Hilman" <khilman@kernel.org>,
	"Fabio Estevam" <festevam@denx.de>,
	"Jacky Bai" <ping.bai@nxp.com>, "Peng Fan" <peng.fan@nxp.com>,
	"Shawn Guo" <shawnguo@kernel.org>,
	"Shengjiu Wang" <shengjiu.wang@nxp.com>,
	linux-imx@nxp.com, "Ian Ray" <ian.ray@gehealthcare.com>,
	"Hervé Codina" <herve.codina@bootlin.com>,
	"Luca Ceresoli" <luca.ceresoli@bootlin.com>,
	"Saravana Kannan" <saravanak@google.com>,
	"Miquel Raynal" <miquel.raynal@bootlin.com>
Subject: Re: [PATCH RFC 00/10] Fix the ABBA locking situation between clk and runtime PM
Date: Mon, 14 Apr 2025 18:00:15 -0700	[thread overview]
Message-ID: <8dfe4bfff1256c1ceffeab81cd587d0d@kernel.org> (raw)
In-Reply-To: <20250326-cross-lock-dep-v1-0-3199e49e8652@bootlin.com>

Quoting Miquel Raynal (2025-03-26 11:26:15)
> As explained in the following thread, there is a known ABBA locking
> dependency between clk and runtime PM.
> Link: https://lore.kernel.org/linux-clk/20240527181928.4fc6b5f0@xps-13/
> 
> The problem is that the clk subsystem uses a mutex to protect concurrent
> accesses to its tree structure, and so do other subsystems such as
> generic power domains. While it holds its own mutex, the clk subsystem
> performs runtime PM calls which end up executing callbacks from other
> subsystems (again, gen PD is in the loop). But typically power domains
> may also need to perform clock related operations, and thus the
> following two situations may happen:
> 
> mutex_lock(clk);
> mutex_lock(genpd);
> 
> or
> 
> mutex_lock(genpd);
> mutex_lock(clk);
> 
> As of today I know that at least NXP i.MX8MP and MediaTek MT8183 SoCs
> are complex enough to face this kind of issues.
> 
> There's been a first workaround to "silence" lockdep with the most
> obvious case triggering the warning: making sure all clocks are RPM
> enabled before running the clk_disable_unused() work, but this is just
> addressing one situation among many other potentially problematic
> situations. In the past, both Laurent Pinchart and Marek Vasut have
> experienced these issues when enabling HDMI and audio support,
> respectively.
> 
> Following a discussion we had at last Plumbers with Steven, I am
> proposing to decouple both locks by changing a bit the clk approach:
> let's always runtime resume all clocks that we *might* need before
> taking the clock lock. But how do we know the list? Well, depending on
> the situation we may either need to wake up:
> - the upper part of the tree during prepare/unprepare operations.
> - the lower part of the tree during (read) rate operations.
> - the upper part and the lower part of the tree otherwise (especially
>   during rate changes which may involve reparenting).

Thanks for taking on this work. This problem is coming up more and more
often.

> 
> Luckily, we do not need to do that by hand, are more importantly we do
> not need to use the clock tree for that because thanks to the work from
> Saravana, we already have device links describing exhaustively the
> consumer/supplier relationships. The clock topology (from a runtime PM
> perspective) is reflected in these links. In practice, we do not care
> about all consumers, but the few clock operations that will actually
> trigger runtime PM operations are probably not impacting enough to
> justify something more complex.

This won't always work, for a couple reasons. First because clk drivers
aren't required to describe their parent clks that are outside the clk
controller by using DT with a 'clocks' property in the clk controller
node. Second because there can be a many to one relationship between a
struct device and struct device_node. We're trying to push drivers to be
written in a way that the binding has the 'clocks' property, but that
isn't always the case, so we still need a solution that works in all
cases so as to not regress old (legacy?) implementations or ones that
divide a platform device into some number of auxiliary devices and
drivers.

One idea to do that would be to implement the device links between clk
controller devices based on all possible parents of the clk. We support
lazily registering clks though, meaning a parent could be registered at
any time, so we would have to explore the clk tree each time a clk is
registered to see if any new clks need to be found and device links made
between devices. The general algorithm is probably something like:

  clk_register()
   make_links_for_node()
    if device node missing 'clocks' property
     for each parent string
      pclk = find parent clk
      pdev = pclk->dev
      link pdev to dev node
    else
     for each clk in clocks property
      pclk = find parent clk
      pdev = pclk->dev
      link pdev to dev node

We have to get the parent clk in all cases because we don't know which
device it may be registered for (the platform device or auxiliary
device). If we optimize here, I'd prefer we optimize for the case where
the 'clocks' property is present to encourage migration. Maybe what we
can do is make some structure for a clk controller and have a linked
list of those that we look up when a new clk is registered. We actually
have that already with 'struct clock_provider' so maybe we need to
extend that.

Stash the device pointer in there and some variable sized array of the
clk_core pointers to the external clks. In the 'clocks' DT property
case, we can size this immediately and map the array index to the
property but in the non-property case we'll have to grow this array each
time a new clk is found to be a parent of the device. Maybe for that we
should just have some other sort of linked list of clk_core pointers
that we continue to stack clks onto.

  struct clock_provider {
    void (*clk_init_cb)(struct device_node *);
    struct device *dev;
    struct device_node *np;
    struct list_head node;
    struct clk_core *legacy_clks; // Or struct list_head legacy?
    size_t len_clocks;
    struct clk_core clocks_property[];
 };

  parent reply	other threads:[~2025-04-15  1:00 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-26 18:26 [PATCH RFC 00/10] Fix the ABBA locking situation between clk and runtime PM Miquel Raynal
2025-03-26 18:26 ` [PATCH RFC 01/10] PM: runtime: Add helpers to resume consumers Miquel Raynal
2025-03-26 19:18   ` Rafael J. Wysocki
2025-03-28  9:59     ` Miquel Raynal
2025-04-09 17:55       ` Rafael J. Wysocki
2025-04-10  7:54         ` Miquel Raynal
2025-04-10  9:55           ` Rafael J. Wysocki
2025-03-26 18:26 ` [PATCH RFC 02/10] clk: Improve comments with usual punctuation Miquel Raynal
2025-03-26 18:26 ` [PATCH RFC 03/10] clk: Avoid non needed runtime PM calls Miquel Raynal
2025-03-26 18:26 ` [PATCH RFC 04/10] clk: Avoid open coded-logic when a there is a helper available Miquel Raynal
2025-03-26 18:26 ` [PATCH RFC 05/10] clk: Move runtime PM calls out of the prepare_lock in clk_init() Miquel Raynal
2025-03-26 18:26 ` [PATCH RFC 06/10] clk: Move runtime PM calls out of the prepare_lock in clk_prepare() Miquel Raynal
2025-03-26 18:26 ` [PATCH RFC 07/10] clk: Ensure all RPM enabled clocks are enabled before reparenting orphans Miquel Raynal
2025-03-26 18:26 ` [PATCH RFC 08/10] clk: Move runtime PM calls out of the prepare_lock in clk_unregister() Miquel Raynal
2025-03-26 18:26 ` [PATCH RFC 09/10] clk: Make sure clock parents and children are resumed when necessary Miquel Raynal
2025-04-09 18:01   ` Rafael J. Wysocki
2025-03-26 18:26 ` [PATCH RFC 10/10] clk: Fix the ABBA locking issue with runtime PM subcalls Miquel Raynal
2025-04-15  1:00 ` Stephen Boyd [this message]
2025-10-03 16:24   ` [PATCH RFC 00/10] Fix the ABBA locking situation between clk and runtime PM Luca Ceresoli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8dfe4bfff1256c1ceffeab81cd587d0d@kernel.org \
    --to=sboyd@kernel.org \
    --cc=dakr@kernel.org \
    --cc=festevam@denx.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=herve.codina@bootlin.com \
    --cc=ian.ray@gehealthcare.com \
    --cc=khilman@kernel.org \
    --cc=l.stach@pengutronix.de \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=len.brown@intel.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-imx@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=luca.ceresoli@bootlin.com \
    --cc=marex@denx.de \
    --cc=miquel.raynal@bootlin.com \
    --cc=mturquette@baylibre.com \
    --cc=pavel@ucw.cz \
    --cc=peng.fan@nxp.com \
    --cc=ping.bai@nxp.com \
    --cc=rafael@kernel.org \
    --cc=saravanak@google.com \
    --cc=shawnguo@kernel.org \
    --cc=shengjiu.wang@nxp.com \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=ulf.hansson@linaro.org \
    --cc=wenst@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).