From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796078189; b=t+B+G4YOrgLFpXoDuf+UtXnXGEcOiRw20PUWCWJej+V+hYzHaxOtWT/GjVj7mDFw9+mi Jxo91E/CSgMI6F4kEHM01vs1BjiWgPe+Z7R/twOkQUawh18iGLmwzX5b8TMEgoaFBO+wi QA3u08RBRSDItMy3tB9vHO2p/aJ7FfOSCIuWTX1DXs0ZuVIpO2GytcmojdyxYWLk4etGC NuDGMEVocAxhoUKB3ocF2CRyEgp71i5JTl8koR6UsFL4v227WnrCKuBa3jv6j85Cf5Uk6 zEPc+LnvCaVTCuC4TTGCACEAQfyTsETGL7EqjYgoNqpChbMgpZf0MqzGQrbMyzs+MQg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796078189; 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:Content-Transfer-Encoding:In-Reply-To; bh=HGpELodfYIpuOlzwmHgblrVzfAT+seAXgX6/wCLVIFg=; b=HtpFRLA4UNykaZR5ClZQfooZDaBCvt8dmEzWN0MjeCaNjkM4IonU2GccEixuLt8R9bvK Osfn+qf+owYTtjZb9Kn1YgBeEDQ1srxHeRrPYodnpfKQ62lcNczN+rcPC6gG4p8Woic17 uHRj48pkReboIM5odjp6Jd8u/sko/Zrx0V5ZoHzqDuvbuYM9apxfv2R7iEu3fOI3V2fiP BpP5I5B7c2WTn5pyiJ3+ZViMCAIjlMBePQb2l2vnaKixAXK0TA4iGTAKt9H79FVMjCymQ mOS9zjSxwrXHk3Kg6GyDj7CAKA2kRwpXAK9cYLASZrt3h/PQDJhx0KT+9IoajZd1EZQ== 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=oKT4FI9W; arc=none smtp.remote-ip=74.125.82.171 Received: from mail-dy1-f171.google.com (mail-dy1-f171.google.com [74.125.82.171]) (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 88962606A329 for ; Mon, 30 Nov 2026 22:36:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 88962606A329 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, unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=oKT4FI9W Received: by mail-dy1-f171.google.com with SMTP id 5a478bee46e88-386ab9ab619so16537396eec.1 for ; Mon, 30 Nov 2026 14:36:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1796078189; x=1796682989; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding: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=HGpELodfYIpuOlzwmHgblrVzfAT+seAXgX6/wCLVIFg=; b=oKT4FI9WfFk2vbu2SiELcOHjhBCpXRJwbD6Lq2X9x6mHREdPykmB5qRjncftuoE0g5 BCDlXvccQpfXFmRNfMm9C23N2klA/17nv0NOPJ8oGcbhuTKAXPQgs/LN0Pn3hQKRM7uo 7rWBfCDlKhtkL6CNbCSmMTJmviPPtlF9MSKwA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796078189; x=1796682989; h=in-reply-to:content-transfer-encoding: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=HGpELodfYIpuOlzwmHgblrVzfAT+seAXgX6/wCLVIFg=; b=bZtz5LDrKXGXTJEsGVZye0t3TmeBrW9NVJAWgR9skrHWoSxhbP6bmyraNuWeRK/RMw s/R7ISXJuCqqWo1m5dxGNipPl92h11+65hoH/XAcAaVycNUl8x/2AvgUDppuarHBKo7b F8uNJB1zC0GF/HRzCd9tYf2mMcRRY9OKvp8FajaeiZN4v6VPLt7APuoTym8IQeP3W9TK th9P1UkXwJp0FGqqP/V6GB4LgqhJXvglc6/3X2MK0RCUT0201xK7TWtFzgAyhEEwD674 SG8JJpchPKopNvuDNoUo30ivTOHf21+Ndyf5F5cKbWPZqGUddtua5xi82WemyAUaQZjO BWvA== X-Gm-Message-State: APuU4Z+xKJ6otG31cOrjgtveYIHFS8RJCu5OWqe2OXZW0iQ71PijedWR ezCMeCk33uMPofDhvI2rSAlHkBX3r7yQ1yuZAnMvTMs/UD7Vn3htiRFdmwx+qFYVBQ== X-Gm-Gg: AZFdTZf0mL2uxt+qtWKWOFm2soccJNpZOu6CFOIsJz33qa/9SHBUQULkRq1xvzI+STX D9WxUXOTQ0YUY7UoO5HuNjq6udKR0+dagIV3OZvUWfAAiBnatjLv0ZxrLYnjcEZ9dj2AxitPLQC 04iMaJHsEC6oywXmSjqTyNxsQauv0VyK12j/8GO7CMfHC+xwwEtSyIdQod0Z1//0FsEJ8a85KOX Dlb6DFLJY6KAyTRKa8fx0Ji+CA+Msrl7Lr4o8r299GRG9ewsfzkm94UO6G+bKaLN2cv3Sztb1XO RtteawYVPFQ3gsqQZR35DaIjwE0ClP68SDg0ChkrpTkUhnZ6YtypFAOAZXDRgVs206Xn/S+4UIy oPtJ30EmlQ1Ulo18X8zXyGyAdhHo/U5Gi0Vlepvfb4ZiZTo0tls5Aq43qNxOg+QZCl4+GaA4EcV Oa9MEGpvfyZUNXC47PcTyFdJ9ToBvEC/q0NG0kDI6AVhewbLmaHsASj5QtktQ7a74To6cE69dXf Sl3cgk1J6N+jZUMOsT0MCmWJwlsC45nz0C4J1K+ayQpYk4CeG/pxFIOm4ACaLi88BTlQ/Y1j4P1 SXjha1opykmZ1InNjGl5bfmPD1lVH05fHQtQfUhrNEFe4J9Tezg0m2bIZPipvczGkaQ2xv1Vqau 7bDytYQORReG+QLU= X-Received: by 2002:a05:7301:4d11:b0:386:c666:ec45 with SMTP id 5a478bee46e88-386c666eeb2mr18069572eec.12.1796078188391; Mon, 30 Nov 2026 14:36:28 -0800 (PST) Received: from localhost ([2a00:79e0:2e7c:8:d64c:d468:46b8:64da]) by smtp.gmail.com with UTF8SMTPSA id 5a478bee46e88-386b9bdafb5sm69933773eec.9.2026.11.30.14.36.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Nov 2026 14:36:27 -0800 (PST) Date: Mon, 30 Nov 2026 14:36:26 -0800 From: Brian Norris To: "Rafael J. Wysocki (Intel)" Cc: Linux PM , Alan Stern , Sakari Ailus , Ulf Hansson , Linux Driver Core Development Subject: Re: [PATCH v2 1/4] PM: runtime: Add memory barrier before reading last_busy Message-ID: References: <4779892.LvFx2qVVIh@rafael.j.wysocki> <14142601.uLZWGnKmhe@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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Thu, Nov 26, 2026 at 03:04:04PM +0100, Rafael J. Wysocki (Intel) wrote: > On Thu, Nov 26, 2026 at 2:44 AM Brian Norris wrote: > > > > On Tue, Nov 24, 2026 at 04:31:51PM +0100, Rafael J. Wysocki wrote: > > > From: "Rafael J. Wysocki" > > > > > > The users of pm_runtime_put_sync_autosuspend() and > > > pm_runtime_put_autosuspend() can expect that the autosuspend delay of > > > the device will be respected after calling one of them because they > > > both invoke pm_runtime_mark_last_busy() upfront. However, that may not > > > happen due to a leftover autosuspend timer running when the last_busy > > > update takes place, in which case rpm_suspend() triggered by the timer > > > may run in parallel with the lockless last_busy update. > > > > > > Namely, the last_busy access in pm_runtime_autosuspend_expiration() > > > called by rpm_suspend() may be reordered before the initial read of the > > > device's runtime PM usage counter in rpm_suspend(), and if last_busy > > > gets updated between those two reads, the update will be missed and the > > > device may be suspended right away. > > > > > > To prevent that from occurring, add a read memory barrier before the > > > last_busy read in pm_runtime_autosuspend_expiration() along with a > > > comment explaining the reason for adding it. > > > > > > Fixes: b3db492e8335 ("PM: runtime: Mark last busy stamp in pm_runtime_put_autosuspend()") > > > Fixes: e24e0630b5ba ("PM: runtime: Mark last busy stamp in pm_runtime_put_sync_autosuspend()") > > > Signed-off-by: Rafael J. Wysocki > > > > If you're into this sort of thing: > > > > Reported-by: Brian Norris > > Closes: https://lore.kernel.org/all/awC0tcqgq2KvCyxO@google.com/ > > OK > > > and > > > > Reviewed-by: Brian Norris > > Thanks! > > > > --- > > > > > > New patch in v2, based on > > > > > > https://web.git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git/commit/?h=linux-next&id=26236a3e8803f7e1ace104020d7f006edb220116 > > > > > > --- > > > drivers/base/power/runtime.c | 7 +++++++ > > > 1 file changed, 7 insertions(+) > > > > > > diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c > > > index 0b15c718c1bb..c295acf6a718 100644 > > > --- a/drivers/base/power/runtime.c > > > +++ b/drivers/base/power/runtime.c > > > @@ -186,6 +186,13 @@ u64 pm_runtime_autosuspend_expiration(struct device *dev) > > > if (autosuspend_delay < 0) > > > 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(). > > > > super-nit / feel free to ignore: it's a bit odd to write this comment > > covering super-precise details of function calls, and then immediately > > rewrite it in patch 2. > > Well, say "stable" picks up this one without the next one (which is > entirely possible). > > And the time spent on writing it has already been sacrificed so to speak. Part of my thought (that I failed to mention) was that depending on what you really meant, the __pm_runtime_idle() part is a bit misleading, for exactly the "stable" case you mention. __pm_runtime_idle() doesn't actually update last_busy the way you mention until patch 2. If you meant literal code like the following, then sure, you have a point: pm_runtime_mark_last_busy(); pm_runtime_put(); // __pm_runtime_idle() is hidden under the hood here But I don't expect anybody should have been expecting such code to do sensible things, at least not until we recently decided to tweak autosuspend and rpm_idle() behaviors. In any case, I doubt anybody else is reading these code/comments that closely for advice. > > I think it might make for a better long-lasting comment if we just write > > in terms of "last_busy update" and "lockless usage-count decrement". > > Then we don't have to update it every time these things move around and > > get patched. > > > > Sample: > > > > /* > > * Ensure ordering between the initial usage counter check in > > * rpm_suspend() and this last_busy access, to avoid missing > > * any last_busy update performed before the last lockless usage > > * count decrement. > > */ > > > > > > But again, that's a bit nitpicky. > > > > I'll try to look at the rest next week. > > Thank you! > > I'll queue this series up in the meantime because it has been reviewed > by Ulf and Sashiko has no concerns, but if you find anything needing a > fix in it, please let me know. Tags would be appreciated too. ;-) Sounds good. Brian > > > + */ > > > + smp_rmb(); > > > expires = READ_ONCE(dev->power.last_busy); > > > expires += (u64)autosuspend_delay * NSEC_PER_MSEC; > > > if (expires > ktime_get_mono_fast_ns()) > > > --