Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp281875pxb; Tue, 12 Apr 2022 01:42:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyOF51tZkYQN+7TeloqOOrzQLjxmArfNWjsTASaO/VUsLWR2s5wf6cjD6GQYEx/f5EthrUF X-Received: by 2002:a17:903:41c5:b0:158:6bde:1d8b with SMTP id u5-20020a17090341c500b001586bde1d8bmr7847386ple.74.1649752928333; Tue, 12 Apr 2022 01:42:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649752928; cv=none; d=google.com; s=arc-20160816; b=djtG1jqZCwPQTqAkW4ZG19h5T38mIBUO/3GUQWmu9lLhILyRp8KdbZZOSQeqSk3BSv wD1PtnPLGA5w6Af1HFzczF2kISZr8gSuMDbC/+TTBQl/IIP0s4nn9l/P9RJlV44t20sH Vos+GSNz0oH0H1IiOHSmbQ/4T1gGiCfcGdMQB7c14Gs+cA4gQ/bDNPlMFW+e+1baPHf3 1YQyu/njYYzZX3oAG2CkhNy0uOuZHDE1n4N9hW14LXdJGBHdL59IRSfopaqH00CCR/Aj OhRy4celthk0DPdR+SyC/8drL2M/joRzLRpHZr2ZeFK98U7d1DRo1td8bOxRlOqoQ7D7 vjDA== 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=lWbA8BxSlOYVP3LVwiIDtHxC35MchtX1bQhiwUUgIBM=; b=Yh1uEy3yTmcvYM7Fp23zW7y/Qc6u0XO8WWBNeSJM7pNqFRSRaEt57zvkjGBTpivGF1 igJrsD3UIpd+zBRGtYpSPM1TI8Duh7Q/uw+chgYj9YCLC1kzq0uCXiiTAgVy5ULvSyIp wGn2DJBiQRQ76hgPDH0RbpQMzQxS4Q6gG/vxpuDQ/zyFKb3fPOWRBAjRTkjmgB6mLu/D 8dwB1Cei8tSWp0sAa6ocqt4tWd686VQN1FHEqkTo/BiUMPkudPjETc4yinZNXxEDL+mT cbfgdhGkgZZrHBZw/R72sFkchvL/wjvijjNJcTYXpPKlWyLjR+30lJaTtrpf3yG7p2IZ w5dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wQZ5G4wb; 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=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lb18-20020a17090b4a5200b001c650f7fa7esi16294634pjb.48.2022.04.12.01.41.55; Tue, 12 Apr 2022 01:42:08 -0700 (PDT) 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=@linaro.org header.s=google header.b=wQZ5G4wb; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345629AbiDKKif (ORCPT + 99 others); Mon, 11 Apr 2022 06:38:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345625AbiDKKi3 (ORCPT ); Mon, 11 Apr 2022 06:38:29 -0400 Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [IPv6:2a00:1450:4864:20::233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CA6A43EC3 for ; Mon, 11 Apr 2022 03:36:14 -0700 (PDT) Received: by mail-lj1-x233.google.com with SMTP id u19so9918063ljd.11 for ; Mon, 11 Apr 2022 03:36:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=lWbA8BxSlOYVP3LVwiIDtHxC35MchtX1bQhiwUUgIBM=; b=wQZ5G4wbP6asggk3xfZw8FnN5ffLC093ppWdL3I2U0Ii5eh+cP2bkUc5jPU/z/n8VP 6fTLvwZu/vYB7UOXSZJipSL1SLu/3fP5wrA5YJQLwpIRaCL0ritC/xdeoKq6Jt8F1dix 6t/H2rlxbAH0hAj+8GioML/2lI/jlw94KbL4csTtPG59Whl4ZFO0GnGjWDGBxtiMv2/N vpWW4Qy01T02tBh4jWiNanQKhMgnZ+IPhEKXUfwOvXQxF4m/I6yr0/rnV/w7n8szSz4u 2PJnK185zzerZKxrK6on34Ww6HIEwIIMrdL9e2sMg10IbXiJlhLWVVM1qwtSH1HX5D2K q8jQ== 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=lWbA8BxSlOYVP3LVwiIDtHxC35MchtX1bQhiwUUgIBM=; b=Evhpj5tjBN5gRrIko3QixWq0E/3pa7QlKK3ZjGK7bQWrXtwUUQfCFxrH9fJX5Zv+aW /DWe4Ci3UqiotMoK5OvfwsQlCoDVRAz7b4efx1NV0/PV64wDDDfQLLVbDp2xBHojSXPW m5xAvUfDPyaZlB12+diT2leXyLaHv71C0vefj/1JtfraupQq32XHZO2Yos4cH66cOyBD nddRj7R933vwuwhwUMrmKDK6aHIl4c80nd+GlLcBemefg9R1NwtgcDV6+yl48MOqCNoA SlSFzWFS+W+3F4pXB8qz83vW0gCSmfLmJJTAbEPQVZHIWd9tVN91FOWLwik2TbE4AIL3 cyqg== X-Gm-Message-State: AOAM533VHgvcG5t/8YXd/ky3bL/KJpcoHk8RuTSGiceJMM1zbmq+NlNf RGiYdjBc7TQ+DPGfuwBTzhdnEwxJktQrord0dF3PBPJimEvXxg== X-Received: by 2002:a2e:9048:0:b0:249:78bb:375e with SMTP id n8-20020a2e9048000000b0024978bb375emr20336046ljg.229.1649673372244; Mon, 11 Apr 2022 03:36:12 -0700 (PDT) MIME-Version: 1.0 References: <1836398.tdWV9SEqCh@kreacher> In-Reply-To: From: Ulf Hansson Date: Mon, 11 Apr 2022 12:35:35 +0200 Message-ID: Subject: Re: [PATCH v1] PM: runtime: Avoid device usage count underflows To: "Rafael J. Wysocki" Cc: "Rafael J. Wysocki" , Linux PM , LKML , Alan Stern Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,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=unavailable 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 On Fri, 8 Apr 2022 at 19:05, Rafael J. Wysocki wrote: > > On Fri, Apr 8, 2022 at 4:05 PM Ulf Hansson wrote: > > > > On Wed, 6 Apr 2022 at 21:03, Rafael J. Wysocki wrote: > > > > > > From: Rafael J. Wysocki > > > > > > A PM-runtime device usage count underflow is potentially critical, > > > because it may cause a device to be suspended when it is expected to > > > be operational. > > > > I get the point. Although, perhaps we should also state that it's a > > programming problem that we would like to catch and warn about? > > OK, I can add that to the changelog. > > > > > > > For this reason, (1) make rpm_check_suspend_allowed() return an error > > > when the device usage count is negative to prevent devices from being > > > suspended in that case, (2) introduce rpm_drop_usage_count() that will > > > detect device usage count underflows, warn about them and fix them up, > > > and (3) use it to drop the usage count in a few places instead of > > > atomic_dec_and_test(). > > > > > > Signed-off-by: Rafael J. Wysocki > > > --- > > > drivers/base/power/runtime.c | 44 ++++++++++++++++++++++++++++++++++++------- > > > 1 file changed, 37 insertions(+), 7 deletions(-) > > > > > > Index: linux-pm/drivers/base/power/runtime.c > > > =================================================================== > > > --- linux-pm.orig/drivers/base/power/runtime.c > > > +++ linux-pm/drivers/base/power/runtime.c > > > @@ -263,7 +263,7 @@ static int rpm_check_suspend_allowed(str > > > retval = -EINVAL; > > > else if (dev->power.disable_depth > 0) > > > retval = -EACCES; > > > - else if (atomic_read(&dev->power.usage_count) > 0) > > > + else if (atomic_read(&dev->power.usage_count)) > > > retval = -EAGAIN; > > > else if (!dev->power.ignore_children && > > > atomic_read(&dev->power.child_count)) > > > @@ -1039,13 +1039,33 @@ int pm_schedule_suspend(struct device *d > > > } > > > EXPORT_SYMBOL_GPL(pm_schedule_suspend); > > > > > > +static int rpm_drop_usage_count(struct device *dev) > > > +{ > > > + int ret; > > > + > > > + ret = atomic_sub_return(1, &dev->power.usage_count); > > > + if (ret >= 0) > > > + return ret; > > > + > > > + /* > > > + * Because rpm_resume() does not check the usage counter, it will resume > > > + * the device even if the usage counter is 0 or negative, so it is > > > + * sufficient to increment the usage counter here to reverse the change > > > + * made above. > > > + */ > > > + atomic_inc(&dev->power.usage_count); > > > > Rather than this two-step process, couldn't we just do an > > "atomic_add_unless(&dev->power.usage_count, -1, 0)" - and check the > > return value? > > No, we couldn't, because atomic_add_unless() returns a bool and we > need to know the new counter value (and in particular whether or not > it is 0). atomic_add_unless(&dev->power.usage_count, -1, 0) would return true as long as the counter value is greater than 0. If the counter has become 0, atomic_add_unless() would return false and not continue to decrease the value below zero. Isn't this exactly what we want? > > I thought that it would be better to do the extra access in the > failing case only. > > > > + dev_warn(dev, "Runtime PM usage count underflow!\n"); > > > + return -EINVAL; > > > +} > > > + > > > > [...] Kind regards Uffe