From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796078307; b=g9+Y+yu4+3wl1nU/oV8lsxIPMZzi9V1uviRL36Rht0IemvaDDeW1rL0veMDhryN4qjkh /ExGhwJO7UFqMxJEXsElMvU9Rs3xtn6ltq7WClNd8/Ww0/HV7Jntrk9EnIMOZybMe7RxN slyiqTwJ0s859/rFn1n9UrN/jftSlp6bb7wgtoNJALkp2I4o/axrdqDFapKs/1VCIqYgw u3tYPeAzWfd+tm5yUZVp7s4tzXlvveGi1cazUNcGRSm/MMha5YTs1wYjl8k5wvTCZ4HFx KsWupx2u0hD22LpzNvV3hxaZQysB6wr5aXcg/jYdKp9FJwRIxXeIKLVEZtIDZMXf4Xw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796078307; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: 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=jKEtGFlElgeNRE7dX01hhx3i2W0UBc7o+EpEnnBszF+V4I1wFSqP40PaQP0Vqp0Ltst6 t1FvHcba6abOIpnwOa4E3CKA6rgRolVQrLBd0b33V0mKZxAQiuAzKk2j4tR8PG6XTwCLx mpr1HFG0ONm78+QHOZSidXsM7uGm0GtuJkFbauZcpotWOR7xeFzWeaBMHNViswNrTvFtU 4DTaGO5ob8BQe7oGPcMUZb9a2QsJDf4WMe0DES+EvXxgQL9hojaCqzqQHbkB5uMlAmpTm nJJUiyOEZ4/9HR2R1Gp2cB/i6jc7WUW9BqQzzK/H1VldN/P8gpX2/Jp4HZDOfwecLtw== 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=SJ7VXc9d; arc=none smtp.remote-ip=74.125.82.179 Received: from mail-dy1-f179.google.com (mail-dy1-f179.google.com [74.125.82.179]) (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 DAFD3606A325 for ; Mon, 30 Nov 2026 22:38:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DAFD3606A325 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=SJ7VXc9d Received: by mail-dy1-f179.google.com with SMTP id 5a478bee46e88-386ab9ab619so16539918eec.1 for ; Mon, 30 Nov 2026 14:38:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1796078305; x=1796683105; darn=vger.kernel.org; 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=SJ7VXc9dbQL63sDOneVO+5gbaS28yNNpZRT1L+5f6AmazTny/rwUG+mzd4iKpc0NC+ p+B1R5qVC+bQVCl7MNXJwAM2kYcQB93ZvddZ4lJvQoXWGdU4E4RZABDz+fQODGZ5j+wn qqDDFUKEVTFyNLSRAlRDB3v77EUGC1sLLWFi4= 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=HsmuimiezBB5j5IFvtXzV2eu0WBkgaNPJHeWikmRfM3IHe5Xii1Jt+a5je1fHofXwv 3Ts4CZ22/dHKfP4SuwYwKGiINiuSLYkIJZrd88UFEvQ76gSS5dC9B6ROb6NZRsCx43Aw Isv1eQipSk93Ui2kubvTc8NN8f4hHdPx7YqfHckGrhxuz5uNte+qlNItcNhvyLm7pEfm 4aeQzGI+F7PFBppfZKxMcnSsu9hPkyEy43x6xofvS7ZAs4mS7odnVi018+FRqQEUac/E A97Tq1y3DuRtS3ubCrNgzJxu3A4SQwA4d09b4uJ/yWIylAz5jamxfJ1WzD28IWCC1qJ2 R6hw== X-Gm-Message-State: APuU4Z/+bAqanroZKxCcwOk9dVaA3YNJzO9H3Mh76C0wGD3aBsxy7LL2 d6xPcAUDjrXynM0dP0OJnCUjd+NUtiIsYBEZyI/4A2vmQ857E4Fit0W5BgRzZxoucQ== X-Gm-Gg: AZFdTZf6g6qsWco87qBlcENN2ZrBxaulSMKyMrE6lVkgxlhe9cwlrJviKoD5gr/ZvcW hm617WHaBaYysI+ILaAKhdeF9Rsao0ve+r71A0JBrC0+xM8d0Vm1vaDKs4FtKAnG8KByM/C5sO1 bbXqUicF35I7K+JEwo5xVzeNckGesOn4dkLB7WsoGfVZUIJfSLQvrZWimMPxIl0RBdmmAgBJxFW ANBhRzwyUqncLbDUzVr1giffFRQaM97mdVS16S7/4M5y+ft2FPHNxVCHbZNYsIyxrcKX1QxhUp4 4w44/rSEIS8Vjnxn/sz92eUgouz1S60BLBuuAk4NFGAm9r8/NzevPKszA5Ec8j6sqAySG1L83OV 2fbDTxu9+zadDOP5Bs4tRVRB90rw5YaSfeAoIKuUNqMRba2yefZjIkFjTalIlxlGBwZ2SE5gW2o u7QjSunWf6Z1G+1JJ5ovxdSuW/jvctm4yXX8Dk2fENCjNDRcn9t8nQANBQNpWkx2wzh+xY0+n7C E1mLnmGu2pTnCY7mtuYqCLSTMgroko1B62XEwrH9SfkP10jIrCNZJA5Dw+zMgXaXDY1w13dBDd2 Ln9yCUVATiG8DDJeO4c5MR6BSBsWNpmFQeqv56qFvXhodlUMI8JIFvR1a854Pv0hur0v 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: linux-pm@vger.kernel.org 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 > > > >