Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp342051pxb; Tue, 12 Apr 2022 03:19:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfjq6CM5wC3cT1UsVweHhqw4omxopryKhZeMqOLpy7sRf6n8GxkAT6399Zy95tMshyA37Z X-Received: by 2002:a17:906:1c0d:b0:6e8:94a5:c5d6 with SMTP id k13-20020a1709061c0d00b006e894a5c5d6mr7524610ejg.134.1649758771355; Tue, 12 Apr 2022 03:19:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649758771; cv=none; d=google.com; s=arc-20160816; b=udtNeyFBbpoqloxQQgulccz4+zipl1sUSuc+byjDefO0dcFNjJZp2FZS4hQTP1z7Nx vRBnGlG1jAZ7XhRzlFf8JJiJkiu+Ta9yf5yaOe4CoOAkF7Rthezb8nI8Sd8J6v2/g54X 2r4qRAm7LuzR8fI5vPa9mu5e6dP1JGXhoun8rgn37/YT5ft+5U8hwSxaThc6V+tax02v VmgtKUcYHXKJC9Ew5P/IKWDHkS2wGWl7ahAXL35OcTkazTPLFKjzrtmCmK5jp3GuzNrq volgY5OErx8dJHk3yWN9SrDTwrwteGqt1Yqp5vk6ruJ1IY154JoGywgkzgFKlkMyE/aI fheQ== 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; bh=wPfh2b3MSo8EtzyqRm45I0kme5Bj24uZnA2dVJHDBqY=; b=ggbJBWP2gnr4AOmcVUzm+WSnDU7B9UwzduqYWEEG48tgR08UzhkRaBt0ndndLyZlo5 8ebIx57kGw4AYCpvK/91JUgPuCLgHip5+X1KMsGnRIfEOcEORNRP97ktP8/ZbCQ+Ikta TuSDargtEcs3MIQat+//WvECV54WIH2KRqM2nr8UYNQ3tztA/D78Ef+IwcbBXAnxkRcS vyccM6pVtyInxEVjua4xIq/h3rFPfrqPckkXwqhoP35HVbdEty0dq5gtQkI/9GlKArWR rKSON8Ol9RiohEqyWojwTfyhe0DLMoYRtg0Yl2lbwKJhqbmWFCRGCmjucNzvpHRp95mg qDDg== ARC-Authentication-Results: i=1; mx.google.com; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i17-20020a170906445100b006df76385ec8si8577214ejp.872.2022.04.12.03.19.04; Tue, 12 Apr 2022 03:19:31 -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; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245201AbiDKLcG (ORCPT + 99 others); Mon, 11 Apr 2022 07:32:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234457AbiDKLcE (ORCPT ); Mon, 11 Apr 2022 07:32:04 -0400 Received: from mail-yb1-f175.google.com (mail-yb1-f175.google.com [209.85.219.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 264483EF3D; Mon, 11 Apr 2022 04:29:47 -0700 (PDT) Received: by mail-yb1-f175.google.com with SMTP id t12so6379000ybt.10; Mon, 11 Apr 2022 04:29:47 -0700 (PDT) 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=wPfh2b3MSo8EtzyqRm45I0kme5Bj24uZnA2dVJHDBqY=; b=59SHDFMDbTdV2wsGl76YrcYoyEkpTKHoFxlBJB2s1BlcxO2Ps3btFCYQvTOa2yN5lL W9Eke702cr6dWvFj7fV9zHvh2PPWuNetMf7MP2oR3axis0gPbSFWOayJ1dES8JoJeSqt rgEtrPimoVY4xK4OjDZShACGrRvb7vArlBDn15Wdl2DD38Iyi4hw8i/vV1hLc0OarJ5E O3CSC23M6B6AQ8GiwP9hHA+8sc1+xmMxOPjyQiPwB/uZVyC0wBj+8ZRd6+mUAZnnIJ+/ 5l5SAFVlNLtYeJGJAQILdb9wj7muudGN0ztc55whpfdJ7pYIGcbdOCKnorMWlPBC3TOr 5XpA== X-Gm-Message-State: AOAM530n0M7g6FW071Y3Kk86vvRNt0KioKD8TOC8daDFBiP/AUcEamap e0zmjzpnwErcjXS8YOueRMdNDCphEOWjGsDOqYE= X-Received: by 2002:a25:230d:0:b0:641:375c:b5ad with SMTP id j13-20020a25230d000000b00641375cb5admr5185825ybj.137.1649676586309; Mon, 11 Apr 2022 04:29:46 -0700 (PDT) MIME-Version: 1.0 References: <1836398.tdWV9SEqCh@kreacher> In-Reply-To: From: "Rafael J. Wysocki" Date: Mon, 11 Apr 2022 13:29:31 +0200 Message-ID: Subject: Re: [PATCH v1] PM: runtime: Avoid device usage count underflows To: Ulf Hansson Cc: "Rafael J. Wysocki" , "Rafael J. Wysocki" , Linux PM , LKML , Alan Stern Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no 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 Mon, Apr 11, 2022 at 12:36 PM Ulf Hansson wrote: > > 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. Yes, and it in particular, when the current value of the counter is 1 before the operation IIUC. So after the operation it is 0 and true will be returned, won't it? But that's exactly the case we want to catch. > 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? Not really. We want to detect transitions from 0 to 1 which is the case when the device can be suspended.