Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1667260pxm; Fri, 4 Mar 2022 00:50:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJxiJ6DPi+qji+I7Fc5cHu8FmiFaRi3sRnQF+reNkFfxDyVYrodK6Aa5sC4Ow8K0Wtl/KA/1 X-Received: by 2002:a17:903:22c7:b0:151:a594:bb85 with SMTP id y7-20020a17090322c700b00151a594bb85mr8910415plg.107.1646383849485; Fri, 04 Mar 2022 00:50:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646383849; cv=none; d=google.com; s=arc-20160816; b=HBdM7u4PsymTzpuMUzAFC32/IUsSfNJamK/z3tMqnkz5d74xAJRt/quE4AOKa1r6AH hXdbqlNDkBBjsUTaI4A9AIkOfmKoHqObCB0MILoRtsP8thPamfpck+7tCI844UDEQ/Dv uuJamG0+H/+fymQECzpVjYBxcQ1eUOh2yarc4fPspyo4b96uqm5O3XPA8c/hout94XTg oPRa/DYa+9zNJJw4NoQyMs00eMVarlJlGyuSe4GS5VFaGFpkrVFZRprnSbVPdG3bV5vD ugeEZ8WOd54xiWlCn0aOWag8EBwOvWuZCvSod6TfOwBGyfK7aNe4sxmZRninxj6Y5FNS J2IA== 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:dkim-signature; bh=tZhufTpqT+5isQeXv5BFMiydi/kBXzIUJ82mZUo2gkg=; b=JmDkfvqh0oPD7kkIGPnr4P7Q5JdIb0RfUDwogHltV2759VcPXTzMg+bnrUHk4EKYE3 +kvAdMfmMJ+BWontKb4fzpU2wg0kITjCvX0+rNVTglbDbYhQwlyQotKatPwO1+dmWlH6 B/q04teiDw3edp+pkEdgS4yFxy03IyFXDqfTIKY/LmgGOjVofkazRVtCm+LbCnV8N8Tv QiluerlggHVunaPUnxcAoz14nDlmnCCOpH7XWGhNJdHZbkVNDrRW+12Xb8BZXuRIKyT4 t7GgvOWBDF+PgCG4Y0WgXN0UKWXpwyuwAchDQVrn8+GorGsJ7rHa+1D842lLPCBJvVl+ voTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=J0JKSjlK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t21-20020a63f355000000b00373e8f30b5bsi4383005pgj.453.2022.03.04.00.50.34; Fri, 04 Mar 2022 00:50:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=J0JKSjlK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231738AbiCDAFp (ORCPT + 99 others); Thu, 3 Mar 2022 19:05:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229838AbiCDAFo (ORCPT ); Thu, 3 Mar 2022 19:05:44 -0500 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3440FECB28 for ; Thu, 3 Mar 2022 16:04:57 -0800 (PST) Received: by mail-ed1-x52c.google.com with SMTP id y2so4037733edc.2 for ; Thu, 03 Mar 2022 16:04:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=tZhufTpqT+5isQeXv5BFMiydi/kBXzIUJ82mZUo2gkg=; b=J0JKSjlKnAiSdcvPuHcxdiz8X/ziS3bofSLzgvGXNSdxY6MSJSWzfCrJxHo8GE5yLO HC4J3t+Gn23Dm+Ih40L/lKMFXrQey9suI9noqRnYuDAdr8P+vZRX1dyxCYN782/lDx84 iFoOgTWvEYBK9wn4os/FSjWZgeu7+kiFEemlo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=tZhufTpqT+5isQeXv5BFMiydi/kBXzIUJ82mZUo2gkg=; b=grE2/7rG3peSqN/HW/ZNHYGjGqCQA0oGq6r6PUzAnBvKLBP2eEexsL/0UfsylRQY/r pIGYI8SI6O/DGuvGvrZ5kjx6OwTg9Y5ZRbc3NoNfZdt7emUOBQeDtPodL65zZmoCQc8a iQI/Afo7a7602qmcN5wKVMJ7I4OtF2SQ6kbcHB2nZhpvwJELl/9i7U4k48/JNjb0Vttq GWJZXC9Mt+3UtnhUNCkxWb7uhoeJ92e96V6vBvDn8wsXXPkRLgtfWbfc8HntUjBhkGyd E3QHc25gXXgrEey23tsw1+vxVeL0smfxd+uzV8UF4Q0BHhmrGreYMv+qF/USsITXwOGs IdHA== X-Gm-Message-State: AOAM533mcmxx0tnnQuonsSIpOP03Xzmq1zfbL9JTNTkaq1rn9k82qMAf 2YHY/Mrdw9wx0dHwmwl8SqMUot5EZp6pHA== X-Received: by 2002:a05:6402:1e91:b0:415:ecdb:bb42 with SMTP id f17-20020a0564021e9100b00415ecdbbb42mr3586208edf.367.1646352295332; Thu, 03 Mar 2022 16:04:55 -0800 (PST) Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com. [209.85.128.45]) by smtp.gmail.com with ESMTPSA id lb14-20020a170907784e00b006d5c0baa503sm1163636ejc.110.2022.03.03.16.04.54 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Mar 2022 16:04:54 -0800 (PST) Received: by mail-wm1-f45.google.com with SMTP id l1-20020a05600c4f0100b00389645443d2so1334403wmq.2 for ; Thu, 03 Mar 2022 16:04:54 -0800 (PST) X-Received: by 2002:a7b:c0c1:0:b0:385:be1b:e6a with SMTP id s1-20020a7bc0c1000000b00385be1b0e6amr5595603wmh.73.1646352294058; Thu, 03 Mar 2022 16:04:54 -0800 (PST) MIME-Version: 1.0 References: <20220223083441.1.I925ce9fa12992a58caed6b297e0171d214866fe7@changeid> In-Reply-To: From: Doug Anderson Date: Thu, 3 Mar 2022 16:04:41 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] PM: runtime: Have devm_pm_runtime_enable() handle pm_runtime_dont_use_autosuspend() To: Ulf Hansson Cc: Laurent Pinchart , "Rafael J . Wysocki" , Dmitry Baryshkov , Linus Walleij , Stephen Boyd , Greg Kroah-Hartman , Len Brown , Pavel Machek , LKML , Linux PM Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On Thu, Mar 3, 2022 at 3:11 PM Ulf Hansson wrote: > > Hi Laurent, Doug, > > On Tue, 1 Mar 2022 at 17:29, Laurent Pinchart > wrote: > > > > Hi Ulf, > > > > On Tue, Mar 01, 2022 at 12:18:02PM +0100, Ulf Hansson wrote: > > > On Tue, 1 Mar 2022 at 11:49, Laurent Pinchart wrote: > > > > On Tue, Mar 01, 2022 at 11:26:46AM +0100, Ulf Hansson wrote: > > > > > On Wed, 23 Feb 2022 at 17:35, Douglas Anderson wrote: > > > > > > > > > > > > The PM Runtime docs say: > > > > > > Drivers in ->remove() callback should undo the runtime PM changes done > > > > > > in ->probe(). Usually this means calling pm_runtime_disable(), > > > > > > pm_runtime_dont_use_autosuspend() etc. > > > > > > > > > > > > From grepping code, it's clear that many people aren't aware of the > > > > > > need to call pm_runtime_dont_use_autosuspend(). > > > > > > > > > > Well, I admit it's good practice that they should take care of this. > > > > > > > > > > However, it doesn't really matter to keep the autosuspend turned on > > > > > when runtime PM becomes disabled, I think. When the driver gets probed > > > > > again, it will most likely call pm_runtime_use_autosuspend() again, > > > > > which should work fine, right? > > > > > > > > For the probe path I agree, but are there valid use cases where, at > > > > runtime, a driver would disable runtime PM and re-enable it a bit later > > > > ? If so, we need to ensure this won't disable auto-suspend. > > > > > > I am not sure I fully understand whether there is a problem. > > > > > > Can you perhaps write the sequence of the runtime PM calls that may > > > cause an issue? > > > > Simply > > > > pm_runtime_disable(); > > /* Do something that requires runtime PM to be disabled */ > > pm_runtime_enable(); > > > > at runtime (not at probe or remove time). If probe() has enabled > > auto-suspend, we don't want the above sequence to disable it. What I'm > > not sure is if there are any valid use cases for the above sequence. > > The above sequence certainly exists already, for example during system > suspend/resume. > > So what happens is that the runtime PM auto-suspend feature gets > temporarily disabled between pm_runtime_disable() and > pm_runtime_enable(). That seems correct, right? > > > > > > > > > When brainstorming solutions, one idea that came up was to leverage > > > > > > the new-ish devm_pm_runtime_enable() function. The idea here is that: > > > > > > * When the devm action is called we know that the driver is being > > > > > > removed. It's the perfect time to undo the use_autosuspend. > > > > > > * The code of pm_runtime_dont_use_autosuspend() already handles the > > > > > > case of being called when autosuspend wasn't enabled. > > > > > > > > > > Hmm, I am hesitating to extend devm_pm_runtime_enable(), as it > > > > > currently makes it look too simple to turn off things at ->remove() > > > > > for runtime PM. While in fact it's more complicated. > > > > > > > > > > A bigger problem, for example, is that a driver calls > > > > > pm_runtime_put_sync() during ->remove(), relying on that it actually > > > > > ends up calling its ->runtime_suspend() callback to turn off various > > > > > specific resources for the device. And in fact there are no guarantees > > > > > that will happen - and when it doesn't, the next time the driver's > > > > > ->probe() runs, things are likely to be really screwed up. > > > > > > > > > > To cover this case, one could use the below code in the ->remove() callback: > > > > > > > > > > ... > > > > > pm_runtime_get_sync(); > > > > > > > > > > "turn off resources for the devices - like calling > > > > > clk_disable_unprepare(), for example" > > > > > > > > > > pm_runtime_disable(); > > > > > pm_runtime_put_noidle(); > > > > > ... > > > > > > > > > > In this example, it would be too late to call pm_runtime_disable() > > > > > through the pm_runtime_disable_action(). > > > > > > > > My experience with runtime PM is that it's hard to use, at least if you > > > > want to get it right :-) That's especially the case if a driver wants to > > > > support both CONFIG_PM and !CONFIG_PM. Here's an example at probe time: > > > > > > > > /* > > > > * We need the driver to work in the event that CONFIG_PM is disabled in > > > > * the kernel, so power up and verify the chip now. In the event that > > > > * CONFIG_PM is disabled this will leave the chip on, so that streaming > > > > * will work. > > > > */ > > > > ret = ov5693_sensor_powerup(ov5693); > > > > if (ret) > > > > goto err_media_entity_cleanup; > > > > > > > > ret = ov5693_detect(ov5693); > > > > if (ret) > > > > goto err_powerdown; > > > > > > > > pm_runtime_set_active(&client->dev); > > > > pm_runtime_get_noresume(&client->dev); > > > > pm_runtime_enable(&client->dev); > > > > > > > > ret = v4l2_async_register_subdev_sensor(&ov5693->sd); > > > > if (ret) { > > > > dev_err(&client->dev, "failed to register V4L2 subdev: %d", > > > > ret); > > > > goto err_pm_runtime; > > > > } > > > > > > > > pm_runtime_set_autosuspend_delay(&client->dev, 1000); > > > > pm_runtime_use_autosuspend(&client->dev); > > > > pm_runtime_put_autosuspend(&client->dev); > > > > > > > > And the corresponding code at remove time: > > > > > > > > /* > > > > * Disable runtime PM. In case CONFIG_PM is disabled in the kernel, > > > > * make sure to turn power off manually. > > > > */ > > > > pm_runtime_disable(&client->dev); > > > > if (!pm_runtime_status_suspended(&client->dev)) > > > > ov5693_sensor_powerdown(ov5693); > > > > pm_runtime_set_suspended(&client->dev); > > > > > > > > And of course there's no documentation that explains all this, so there > > > > are endless variations of patterns originating from cargo-cult > > > > programming. > > > > > > > > I don't know what the right solution is, but we need to move towards an > > > > easier to use API if we want drivers to get it right. Any step in that > > > > direction would be welcome. > > > > > > Yep, I fully agree with you, while it's not an easy task. At least the > > > example above looks fine to me. :-) > > > > It took me several days to figure out how to get it right. Most > > developers don't bother, so we end up with drivers broken in different > > ways :-S > > Yes, it's definitely non-trivial. > > Power management in general relies on cross-interaction of several > different frameworks, so one really needs a decent overview too, > before adding PM support in a driver. > > > > > > Recently I noticed that some drivers are calling > > > pm_runtime_force_suspend() at ->remove(). This works fine in quite > > > many cases, but it wouldn't solve the case when CONFIG_PM is unset. > > > > > > Perhaps we should explore adding a new API, along the lines of > > > pm_runtime_force_suspend(), but make it specific for the ->remove() > > > path, and in some way make it work for when CONFIG_PM is unset too. > > > > I'm all for an improved API for drivers that would make the above > > simpler. And documentation too, Documentation/power/runtime_pm.rst is > > more of a documentation of the runtime PM core than the driver API. > > There are some useful tips for drivers, but they're lost in a sea of > > difficult to understand and/or irrelevant information (and there's also > > a tiny bit of information in Documentation/driver-api/pm/devices.rst). > > We're missing a document targetted at driver authors. > > Yes, I agree - the docs can certainly be improved! I will add it to my > TODO list and try to put some time on it, not too far ahead I hope. I > was actually planning for a blog-post/LWN article, maybe I should > spend some time on this instead - or both. :-) > > When it comes to the improved API for the ->remove() case, we need to > explore this a bit more. I will think about it. > > About $subject patch, if you or Doug insist that you want to move > forward on it, I will not object - even if I think we need something > entirely different, in the long run. Hrm, I guess the question is how far away are we from the long run. ;-) If this doesn't impede the long term solution and the long term solution is still a ways out, it feels worth landing this or something similar to it just to help make the existing code a little more robust, even if it doesn't solve every problem. That being said, I don't have any code that depends on this patch and I myself will likely not mess this up again in code that I write. ;-) Just for context, the patch was originally suggested by Laurent [1] in response to me fixing some code that I wrote. [1] https://lore.kernel.org/r/YhZY+FLTlv7V5BIB@pendragon.ideasonboard.com -Doug