Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3086458pxb; Tue, 19 Jan 2021 13:26:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJygR2hkuUFN5/kNlogu9S+vbbVsF1q/XtF251lXau1Dp2fVXi+QkoBWhtMUyaRGV7c1rJQo X-Received: by 2002:aa7:c6cc:: with SMTP id b12mr4891129eds.67.1611091593725; Tue, 19 Jan 2021 13:26:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611091593; cv=none; d=google.com; s=arc-20160816; b=xe1VM3iD6W9IO8wOhyLqk8Ac2jlPULl6PMEEOlTdeN9LEkOqjA88CwXvsh0pYCSo2U rSMpRanoWNWRaq/e2dmwic/Jp/P+qDqI4KkkE0g65dKIljHsOmzSXH6P82dvCpuJk6om WAa+rO/a5wQ3leBdYQPAwT+R05k7xPK+tTwJasNl/ojmGFIZuGi1efTo07vqPmsEfaDt 80NNlO50onZkUJB7WYtoioASjKHOexzZ1k3ftQ1KAq0Fr0Nq/o4lzS+B5e/rT55AElx/ iWCDtfUmko3Y78MDLBarlrbsBegHyctazwLv8CKQiLS5y8/3n6rmtP3KBjVvEk/hJQik h0wA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:message-id:in-reply-to :subject:cc:to:from:date:dkim-signature; bh=QoR15X76Wa8utojDFzghU0XUanvjdtOD0JtOJepg2zQ=; b=duCcT8LnLa3Bx+i4528eY48JPpY6MJvp5mzvxqLH9HJ0PTViG1Lfy/jTc/v2v5XbKe 7XbIZv02QfHrqKO1CcUYI6NdCVybBTA9Hr2lsSd0pEA1FsIooZElLijX+kHdejIHwpLR Gj9p3WRFdJh4iHgBKsEfRpeYBO1DO63kvPkrJWYREfMl5svozZgSc3/5PfCQgtGzyNt1 WyZ2RKMEACoXYQW3CN96eTNGs3Meq3TRaqOnQyoqWQXiRJGf2URIrBvq0lsoAjuTYiNC v4JL6M+jk6selNosE679nB1o05ppw5/ToX18B90wLgr+8XY/hd7D0BvR6bx0mXzLFBuD ZRoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=iPJY1moe; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dd18si47487edb.195.2021.01.19.13.26.04; Tue, 19 Jan 2021 13:26:33 -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; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=iPJY1moe; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729613AbhASVXw (ORCPT + 99 others); Tue, 19 Jan 2021 16:23:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729436AbhASVXa (ORCPT ); Tue, 19 Jan 2021 16:23:30 -0500 Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1375FC061757 for ; Tue, 19 Jan 2021 13:22:48 -0800 (PST) Received: by mail-qv1-xf32.google.com with SMTP id j18so9907399qvu.3 for ; Tue, 19 Jan 2021 13:22:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:in-reply-to:message-id:references :mime-version; bh=QoR15X76Wa8utojDFzghU0XUanvjdtOD0JtOJepg2zQ=; b=iPJY1moesz74BGlcRCZu6JplYli8oRPYxZQBKc+pWMMbglhVWCtRcav6p7QbWlmfum y9W2MoQitiRxq4vuw0hSckO056h6CHdVtyShAQ+wz+tlHDIjKqUAffiYs3CsV0+376MY i8hyuUkqVPwmKiEluATsxtURE48Sqj8WPxQSqwJZhULp4yCG1KlkbEwseTV8eWdQSal0 yPhyyGwa1jxr7yCWBYKuryrDiBQ3BMIyUfykY6LKVg2m6kQdX/mCUhgPfYAb/XNq/IH6 Xy529AmwgQW8E7Mrhw1tcFJgcBLgkfVWq+KicOk4/kvAkAggZFcFNAGg7mehzP4iYq/5 z5yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=QoR15X76Wa8utojDFzghU0XUanvjdtOD0JtOJepg2zQ=; b=MIKbhG4XmVEAPtCy2rr9n5WWcxwgYflt78uE1e7wClCFGL9o9I3o9f4zkmiDTtMQRw cXbySlGbO4Y0oxFD937oBBzbaULW1bSmban7isnN8k9AiARpHlLJx8qgMTqnhSd+8yha fZO5J8T2l0G5+Ys6K+FCt44YtZmxhh/y4JE38aye6ihkxSCj7o1EvTlkk+a3tCcHEOD7 /4hC7PhtQ2GZoF/vKG8ON804oR7gRXlTXLRG4+rik8qwfYjdc5zsdCdZHR6xhFFmbKbB YBCz1hz+xq3+iBVy5J9xPgxa0cpN0+rAcaa5D7F9OrKksFWMO+5uqVGrphKwn1XQDs5y R+3A== X-Gm-Message-State: AOAM533/SwdXhA4JN7u761WnP7q5qyo8iHmvqUg0yV4RB+fUkfU+oPiH GtBCgwX7fdTPojfve+3zgSJgTA== X-Received: by 2002:ad4:4431:: with SMTP id e17mr6465871qvt.21.1611091367227; Tue, 19 Jan 2021 13:22:47 -0800 (PST) Received: from xanadu.home (modemcable076.50-203-24.mc.videotron.ca. [24.203.50.76]) by smtp.gmail.com with ESMTPSA id l38sm3459069qte.88.2021.01.19.13.22.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Jan 2021 13:22:46 -0800 (PST) Date: Tue, 19 Jan 2021 16:22:45 -0500 (EST) From: Nicolas Pitre To: Geert Uytterhoeven cc: Kevin Hilman , "Rafael J. Wysocki" , Greg Kroah-Hartman , Michael Turquette , Stephen Boyd , Russell King , Linux PM list , linux-clk , Linux Kernel Mailing List Subject: Re: [PATCH] PM / clk: make PM clock layer compatible with clocks that must sleep In-Reply-To: Message-ID: References: <17nqrn25-rp5s-4652-o5o1-72p2oprqpq90@onlyvoer.pbz> <7him7sydd6.fsf@baylibre.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 19 Jan 2021, Geert Uytterhoeven wrote: > Hi Kevin, Nicolas, > > On Tue, Jan 19, 2021 at 7:45 PM Kevin Hilman wrote: > > [ + Geert.. renesas SoCs are the primary user of PM clk ] > > Thanks! > > > Nicolas Pitre writes: > > > The clock API splits its interface into sleepable ant atomic contexts: > > > > > > - clk_prepare/clk_unprepare for stuff that might sleep > > > > > > - clk_enable_clk_disable for anything that may be done in atomic context > > > > > > The code handling runtime PM for clocks only calls clk_disable() on > > > suspend requests, and clk_enable on resume requests. This means that > > > runtime PM with clock providers that only have the prepare/unprepare > > > methods implemented is basically useless. > > > > > > Many clock implementations can't accommodate atomic contexts. This is > > > often the case when communication with the clock happens through another > > > subsystem like I2C or SCMI. > > > > > > Let's make the clock PM code useful with such clocks by safely invoking > > > clk_prepare/clk_unprepare upon resume/suspend requests. Of course, when > > > such clocks are registered with the PM layer then pm_runtime_irq_safe() > > > can't be used, and neither pm_runtime_suspend() nor pm_runtime_resume() > > > may be invoked in atomic context. > > > > > > For clocks that do implement the enable and disable methods then > > > everything just works as before. > > > > > > Signed-off-by: Nicolas Pitre > > Thanks for your patch! > > > > --- a/drivers/base/power/clock_ops.c > > > +++ b/drivers/base/power/clock_ops.c > > > > +/** > > > + * pm_clk_op_lock - ensure exclusive access for performing clock operations. > > > + * @psd: pm_subsys_data instance corresponding to the PM clock entry list > > > + * and clk_op_might_sleep count being used. > > > + * @flags: stored irq flags. > > > + * @fn: string for the caller function's name. > > > + * > > > + * This is used by pm_clk_suspend() and pm_clk_resume() to guard > > > + * against concurrent modifications to the clock entry list and the > > > + * clock_op_might_sleep count. If clock_op_might_sleep is != 0 then > > > + * only the mutex can be locked and those functions can only be used in > > > + * non atomic context. If clock_op_might_sleep == 0 then these functions > > > + * may be used in any context and only the spinlock can be locked. > > > + * Returns -EINVAL if called in atomic context when clock ops might sleep. > > > + */ > > > +static int pm_clk_op_lock(struct pm_subsys_data *psd, unsigned long *flags, > > > + const char *fn) > > > +{ > > > + bool atomic_context = in_atomic() || irqs_disabled(); > > Is this safe? Cfr. > https://lore.kernel.org/dri-devel/20200914204209.256266093@linutronix.de/ I noticed this topic is a mess. This is why I'm not relying on in_atomic() alone as it turned out not to be sufficient in all cases during testing. What's there now is safe at least in the context from which it is called i.e. the runtime pm core code. If not then hopefully the might_sleep() that follows will catch misuses. It should be noted that we assume an atomic context by default. However, if you rely on clocks that must sleep then you must not invoke runtime pm facilities in atomic context from your driver in the first place. The atomic_context variable above is there only used further down as a validation check to catch programming mistakes and not an operational parameter. Nicolas