Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796078306; b=CusR5dQqsVUmWXcPtapSSMuKIiNrtd6lQ0wqLj88BUrL3AO1DT9a2fVfLbX4SlCSMtE2 DbvXJ5GdYPfpTg2CZ180tu217zGVV1ku0ksdE6ZdKQS7a60f6u5vUyo/nyXlP6rDeh1W9 3aVeGGTgIDP5nN0ALySQ+pK2nw/QaB4WT4UQ0O6NSCoKGJZcQ2xMf8xCLxYODwWVuuNOS rTsIBv0/n6RKOvVkciKCiIuMzXHSsPL97ddsQZmWUNPX6/eaf/x5Lf9Q9iecKS34AYegb xXO6domZtxsvQDNUpXUW/L3z0Db9vFGI637zR1zTHdoQyPZWuvP0aHlHyyPipZu3w6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796078306; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; bh=nZZQCFWlJK17PaljkO3Bj66ZKN4+SrK3FNLc2VcrsNM=; b=pkcXhwRlyzEuTulbD7FUqrHXaAVgjjQHlEPCDQLG7mxjCLR8fgvSd2A55Wa1zNoqUT/y rVFJxc+mmhatl2QIH8x+T1nj7Hnn4Kuh4d2ysMup/VVzxnDdGtF2+hiYvokmGhDuhAhe2 KTZs16rCLbFUqHJiqQqEcbDiHrFpeOhi3ykCa8RT6VS/j03VcTuFVbdgwlVkL3pBcLLMp sCQyIgIJYsmkCXMX6jzL9bhQHLYruHMbJVkRkSTHm0NBdYtwWkl8kqHNoOlb7BdovhEG3 hYp/QJgtWLLSyJ13rwfVcOzOJ6/JW/wQH5f0OlHmBJ7oVviBUnDLvPKr0yvD5xHCBNg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=KqHXEnQ0; arc=none smtp.remote-ip=74.125.82.182 Received: from mail-dy1-f182.google.com (mail-dy1-f182.google.com [74.125.82.182]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 112F3606A32C for ; Mon, 30 Nov 2026 22:38:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 112F3606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=KqHXEnQ0 Received: by mail-dy1-f182.google.com with SMTP id 5a478bee46e88-3866a784546so15591953eec.0 for ; Mon, 30 Nov 2026 14:38:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1796078305; x=1796683105; darn=lists.linux.dev; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=nZZQCFWlJK17PaljkO3Bj66ZKN4+SrK3FNLc2VcrsNM=; b=KqHXEnQ0WRMLBbbAFdetqy9CnzvhTmQwK8000P4cSMvK+M/7za/2XrSphDRNqh6vwi L5++yFZVDoGpfKtC2TXc1w2w74f+nqElYVKdlgS4DiTj6vrEvz2Z7RloJPcGbu45HbT4 3y+65Im8pL+bAJhCwe2USPzQC4IjGmgjvZROA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796078305; x=1796683105; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=nZZQCFWlJK17PaljkO3Bj66ZKN4+SrK3FNLc2VcrsNM=; b=wUkKEAI2hxiCTfgEJTHrPCtbjzHiv3bt4wAL4U/9HCi9ipWiiOfJlJeIxfJNFT0aGU fhUzpoIyegY7I3mNm/drXeHPtfSB8vs/1C0eUHCgEkT1E2l724PjdkHfELlJJR2vp+p7 xirdxXp5gWTgibcvJPz3SIHmps9Bu4FUBoeuVYsgGAdYSH85z0PNsfj0ovF4nA0/K3yD THYowjzbqWj0MfZiQLj39iVEO3K/KivUMDd5ObQpr91X2e1C+4i8ZVvH0lEc4+ef74b5 SyPi2LDGgOQgP52g9J7z/uj2svEyrgM+/9qHc+mYwqZLjFwv/MR3PPjaCgtZfuGw2GWl L2ag== X-Forwarded-Encrypted: i=1; AL+hErGTYDv/sCblKbc/V8AKeookzekytECQHKPBzaMXOLR/EQjk6vnqA2inRBoD1k5mykoFc0cr842+bQs6uA==@lists.linux.dev X-Gm-Message-State: APuU4Z8u/buawdqT9qk9T2o4rnxAbEeRCf284XttmgM180sUC9/YcppX ny36HZuoMNTQl+gnn9aY0lElGYhmY1oXxvVtO1DPSzKF5WEEI8gdQNV4NIwM2hQ7Rg== X-Gm-Gg: AZFdTZcv3ARGkT9jW1OAsCMpBxRqCyTK6JgCOMoWa1K8UxyncD2Ra9O4UEIQ5qivIiw GQWmq1eotxH9E/ujqQOdww2qaghc77sjQ/G/0Yp/7OIA42tNQKPdklVOSX5utugSQCcj+6BTD4N wgiI1nYFUi9Jmuzgx0J71++L99ijw5W9QKbqlFuKFhkqbD2PbxN+LKc3iC/lWD6R/xF+FDDnQzO FxlsgIjsgB3Ke9ZFN1sP3TRqhidFR25tHtzvB63XyBcQH9pMq3jKuf4QsJdV8B2ktmmVXA4Thg/ oTphIARc3UDTbQg8H74Xt/9v1v9+91iy1LOLPCwhKH4UEEF50cos1ad6C4IPqla28cIRrULT3fC Gdft4XsIgQBPJCaAL4ZhQvJ4772iWDUE10AoHnykVofIEphO3kK4BpUrqu6enZ0mZZkRWVwPGyW v0tZl6ANtWDSGW1mDFNE68qzKY2hPCGOX/zY7Rkf5JHyBsV8XknqJEVIxAB9zgzKxRaXspuKRXN BKuNpssDfkpRgR1o4LLex5uViVEdc9ymFDHjAeiyWoKI1fSv7uerSm2QMQ3pMYzWmwjVKnx2NCS XslzYq/CbFsgF70Kip11a6DI5G+ymvWsoOQnxdYtLtnJZcXyUg/01QfK+H8zlYOwjsX3 X-Received: by 2002:a05:7300:1486:b0:383:6eb6:5730 with SMTP id 5a478bee46e88-38570e24c0fmr41524105eec.32.1796078304948; Mon, 30 Nov 2026 14:38:24 -0800 (PST) Received: from localhost ([2a00:79e0:2e7c:8:d64c:d468:46b8:64da]) by smtp.gmail.com with UTF8SMTPSA id 5a478bee46e88-38566cd46c5sm129010155eec.11.2026.11.30.14.38.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Nov 2026 14:38:24 -0800 (PST) Date: Mon, 30 Nov 2026 14:38:23 -0800 From: Brian Norris To: "Rafael J. Wysocki" Cc: Linux PM , Alan Stern , Sakari Ailus , Ulf Hansson , Linux Driver Core Development Subject: Re: [PATCH v2 2/4] PM: runtime: Avoid suspends without respecting autosuspend delay Message-ID: References: <4779892.LvFx2qVVIh@rafael.j.wysocki> <3450859.44csPzL39Z@rafael.j.wysocki> X-Mailing-List: driver-core@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3450859.44csPzL39Z@rafael.j.wysocki> On Tue, Nov 24, 2026 at 04:35:08PM +0100, Rafael J. Wysocki wrote: > From: "Rafael J. Wysocki" > > Commit 26236a3e8803 ("PM: runtime: Update last_busy before calling > rpm_suspend() in rpm_idle()") overlooked some cases in which the given > device may be suspended without respecting its autosuspend delay right > after the last runtime PM reference to it has been dropped. They are all > scenarios in which a leftover autosuspend timer is running when the last > runtime PM reference to the given device is dropped, and dropping that > reference allows the timer to suspend the device right away. > > For example, pm_runtime_put() may be called while an autosuspend timer > is running. Then, dropping the device's runtime PM usage counter to 0 > in __pm_runtime_idle() allows rpm_suspend() triggered by the timer to > suspend the device right away before __pm_runtime_idle() is able to > grab its PM spinlock. > > Analogous situations may occur when child_count of a device's parent > is dropped in rpm_suspend() and __pm_runtime_set_status(), and in the > callers of pm_runtime_release_supplier(). > > Prevent them all from occurring by adding last_busy updates in a few > places where rpm_idle() calls follow dropping runtime PM references to > devices. > > It is not necessary to add memory barriers to enforce the requisite > ordering of updates which is naturally enforced by the existing code in > all of those cases. However, add comments explaining the rationale for > updating last_busy and the pairing of memory barriers, and accordingly > update the memory barrier comment in pm_runtime_autosuspend_expiration(). > > Fixes: 26236a3e8803 ("PM: runtime: Update last_busy before calling rpm_suspend() in rpm_idle()") > Signed-off-by: Rafael J. Wysocki > --- > > v1 -> v2: > * New patch addressing one of the cases addressed by the v1 and a few > more analogous cases (Brian) > > --- > drivers/base/power/runtime.c | 53 ++++++++++++++++++++++++++++++++---- > 1 file changed, 47 insertions(+), 6 deletions(-) > > diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c > index c295acf6a718..a71afaea7707 100644 > --- a/drivers/base/power/runtime.c > +++ b/drivers/base/power/runtime.c > @@ -187,10 +187,13 @@ u64 pm_runtime_autosuspend_expiration(struct device *dev) > return 0; > > /* > - * Prevent last_busy updates occurring before the rpm_drop_usage_count() > - * invocations in __pm_runtime_idle() and __pm_runtime_suspend() from > - * being missed if the last_busy access below gets reordered before the > - * initial usage counter check in rpm_suspend(). > + * If the last_busy access below gets reordered before the initial usage > + * counter check in rpm_suspend(), last_busy updates made before any of > + * the following may be missed: > + * - rpm_drop_usage_count() in __pm_runtime_idle() and > + * __pm_runtime_suspend(), > + * - the usage count update in pm_runtime_release_supplier(), > + * - the child_count update in rpm_release_parent(). > */ > smp_rmb(); > expires = READ_ONCE(dev->power.last_busy); > @@ -333,6 +336,16 @@ void pm_runtime_release_supplier(struct device_link *link) > { > struct device *supplier = link->supplier; > > + /* > + * Since rpm_suspend() may run as a whole right after the loop below, > + * prevent it from suspending the supplier without respecting its > + * autosuspend delay. The last_busy update cannot be reordered with > + * the usage counter update in pm_runtime_put_noidle(), which is fully > + * ordered and pairs with the read memory barrier in > + * pm_runtime_autosuspend_expiration(). > + */ > + pm_runtime_mark_last_busy(supplier); pm_runtime_release_supplier() is already a bit of an odd function, because it is designed to be called even in cases where the link in question does *not* hold an active refcount on the supplier. (i.e., there are cases where refcount_dec_not_one(&link->rpm_active) sees 1 (no refcount) and quits without doing anything.) This means you are potentially adding spurious last_busy updates to cases like: * pm_runtime_set_suspended(dev), when dev is already suspended. This will still update last_busy for its suppliers * some other odds and ends, like pm_runtime_drop_link() * device_link_release_fn() -- even for non-DL_FLAG_PM_RUNTIME links. (Kind of unimportant.) Perhaps this this last_busy update should go inside the 'if'? Or otherwise refactor this function to be less subtle. The other changes in this patch look good, so maybe this still qualifies as: Reviewed-by: Brian Norris > + > /* > * The additional power.usage_count check is a safety net in case > * the rpm_active refcount becomes saturated, in which case > @@ -574,6 +587,25 @@ static int rpm_idle(struct device *dev, int rpmflags) > return rpm_suspend(dev, rpmflags | RPM_AUTO); > } > > +static void rpm_release_parent(struct device *parent) > +{ > + /* > + * Since rpm_suspend() may run as a whole for parent right after its > + * child_count update below, prevent it from suspending parent without > + * respecting its autosuspend delay. > + */ > + if (!parent->power.ignore_children) > + pm_runtime_mark_last_busy(parent); > + > + /* > + * A successful atomic_add_unless() call is a memory barrier which is > + * sufficient to enforce the requisite ordering with respect to the > + * last_busy update above and it pairs with the read memory barrier in > + * pm_runtime_autosuspend_expiration(). > + */ > + atomic_add_unless(&parent->power.child_count, -1, 0); > +} > + > /** > * rpm_suspend - Carry out runtime suspend of given device. > * @dev: Device to suspend. > @@ -719,7 +751,7 @@ static int rpm_suspend(struct device *dev, int rpmflags) > > if (dev->parent) { > parent = dev->parent; > - atomic_add_unless(&parent->power.child_count, -1, 0); > + rpm_release_parent(parent); > } > wake_up_all(&dev->power.wait_queue); > > @@ -1139,6 +1171,15 @@ int __pm_runtime_idle(struct device *dev, int rpmflags) > int retval; > > if (rpmflags & RPM_GET_PUT) { > + /* > + * Since rpm_suspend() may run as a whole right after the usage > + * counter decrementation below, prevent it from suspending the > + * device without respecting its autosuspend delay. The > + * requisite ordering is enforced by rpm_drop_usage_count(), > + * which is an implicit memory barrier and pairs with the > + * explicit one in pm_runtime_autosuspend_expiration(). > + */ It feels like a bad sign that we have to scatter these comments everywhere. It's not a criticism of the comment -- just the design^W evolution of the code we have. It feels like we'd be in a better place when these comments only go in functions like rpm_drop_usage_count(), rpm_release_parent(), pm_runtime_release_supplier(), as central places to manage refcounts. And we are *almost* to the point that rpm_drop_usage_count() should always call pm_runtime_mark_last_busy(). Anyway, I'm just musing. I think we're getting to a better place here. Regards, Brian > + pm_runtime_mark_last_busy(dev); > retval = rpm_drop_usage_count(dev); > if (retval < 0) { > return retval; > @@ -1383,7 +1424,7 @@ int __pm_runtime_set_status(struct device *dev, unsigned int status) > goto out_set; > > if (status == RPM_SUSPENDED) { > - atomic_add_unless(&parent->power.child_count, -1, 0); > + rpm_release_parent(parent); > notify_parent = !parent->power.ignore_children; > } else { > spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING); > -- > 2.51.0 > > > >