From nobody Wed Oct 1 22:33:21 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BC81283CB5; Fri, 26 Sep 2025 16:27:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758904069; cv=none; b=VZP2zD++mfcTqHbj7ESELMebRizRddiyomN/cDokHKUPO0JnsZFDaygn9qUX8xoktC/CopMklAHAm4aNNcjQ5JBuvRAYxs2LL1ZXaJBXnis2JgY67LMVyJB22SlTTFrL5TsohxoxowQy24fCFMSYepX1c9QtJJ3KC+JDxnI8rKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758904069; c=relaxed/simple; bh=7S79uZdrDyC8TorrZT9eUpU4HISQIufbMjy+cWyAHDo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LMesIHx2ftktU5eAC6PdxZfKTc8iAs/ghzRj/7KmlyQt0+22mBGNeypwRyEsQolMNoncYL+wTBF40AcwiCaLNfQyw6NGdonmAmrzzq49VLFWlwIlp3JJT13uX9LIcYN+RrylsQiZZFZfjZp9nYQzgaHNKnmhE4p9fCfQ8f34WQE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Emf+L8kU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Emf+L8kU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 134D9C113CF; Fri, 26 Sep 2025 16:27:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758904069; bh=7S79uZdrDyC8TorrZT9eUpU4HISQIufbMjy+cWyAHDo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Emf+L8kUMl1EdmvzikUoXBjkSR3BP2Lv/77S7wBnDuyiuADCC35rWDW0KCEJo4fVZ caBX13UvsviDoXC8gUtey8E8kecGHi5Rk11IXS51dfjQWY1/3A7EBBrXEu2mLjsffx EHtNamrne0lNOZy1AiB6iMytC9g+GtpW7iQiURJvjEb5fmkB6x4x8IURSxnxsX1jwy AgR8X6ioabLYucKLd5lWoJlVhyW67WUBgW3zGirGNg6fs9EJPqUAzEeaDHW60jjZYs TbPVdatLhJkvCi1NVjjR6eoInqOEd/rr8MaaFTG7nNczmykS37otIv4DNqGM6lyfda ZJ2M8JupRN+3w== From: "Rafael J. Wysocki" To: Linux PM , Jonathan Cameron , Bjorn Helgaas Cc: Takashi Iwai , LKML , Linux PCI , Alex Williamson , Zhang Qilong , Ulf Hansson , Frank Li , Dhruva Gole Subject: [PATCH v4 2/3] PCI/sysfs: Use runtime PM guard macro for auto-cleanup Date: Fri, 26 Sep 2025 18:24:05 +0200 Message-ID: <2323750.iZASKD2KPV@rafael.j.wysocki> Organization: Linux Kernel Development In-Reply-To: <6196611.lOV4Wx5bFT@rafael.j.wysocki> References: <6196611.lOV4Wx5bFT@rafael.j.wysocki> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Rafael J. Wysocki Use the newly introduced pm_runtime_active_try guard to simplify the code and add the proper error handling for PM runtime resume errors. Based on an earlier patch from Takashi Iwai [1]. Link: https://patch.msgid.link/20250919163147.4743-3-tiwai@suse.de [1] Acked-by: Bjorn Helgaas Signed-off-by: Rafael J. Wysocki Reviewed-by: Jonathan Cameron Reviewed-by: Takashi Iwai --- v3 -> v4: * Use ACQUIRE()/ACQUIRE_ERR() (Jonathan) * Adjust subject and changelog * Take patch ownership (it's all different now) * Pick up Bjorn's ACK from v3 (Bjorn, please let me know if that's not O= K) v2 -> v3: No changes v1 -> v2: * Adjust the name of the class to handle the disabled runtime PM case transparently (like the original code). --- drivers/pci/pci-sysfs.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1475,8 +1475,9 @@ static ssize_t reset_method_store(struct return count; } =20 - pm_runtime_get_sync(dev); - struct device *pmdev __free(pm_runtime_put) =3D dev; + ACQUIRE(pm_runtime_active_try, pm)(dev); + if (ACQUIRE_ERR(pm_runtime_active_try, &pm)) + return -ENXIO; =20 if (sysfs_streq(buf, "default")) { pci_init_reset_methods(pdev); From nobody Wed Oct 1 22:33:21 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A1B125FA13; Fri, 26 Sep 2025 16:27:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758904064; cv=none; b=OilACY4bcfV3ul5Orq3JK2QSkxN1u9yJr8qp8Lg3G/z3sBLtSn155PFiBOeodmj1J+xf/EPMlO1neMPukvrbGDhJTibP5rOtpw4q7N4thLpCSupnQYz5JM7ir3fPbGuTF3+dPp3oUAxQ21fPB37MmFiv6M5koiKDQSIpTEbCt18= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758904064; c=relaxed/simple; bh=q4hs9AuGgKoM1p8EhtnQRn0bIzEyhLE3EGxPyvfGB+Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Fpo/8h58Xalmjng9xOOxUdwBrZUdOjEohJKcjoyI2/f2f5NilDfrbYpDOzdiMBy3+YAatOgxhebSmv6LntIPeKVOQvAx5REUrnC2X4vfiQzy5tBEL37C5khvVgSbzfWxebn0jxA4AThdxp3skGO35/P2DFo/nFlXUArgTY0/9e0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZSenCnDy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZSenCnDy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 092C1C116D0; Fri, 26 Sep 2025 16:27:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758904063; bh=q4hs9AuGgKoM1p8EhtnQRn0bIzEyhLE3EGxPyvfGB+Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZSenCnDy7YJ0X2yIjCf4yy0nDY+42PJ8US/tkoY6QRpsb+EWdpkvNRYlKMFBRcgG9 AW5Hn7esd0gUuro1qsqfeqy29S/eYG1DT5rSUvQ0o4pRBLPqte12N9ggbTSg3gOHda I+ZXhppqDm14Qq1pFk/D38TOALPex1kkEvtt6liMJDrnmPOotRMtSoU7xNunXxuOBF Ax7XUSM8P3ze9oDILfUVIhA240nf8ymreAsB7f+PFbj0kE8RbdXSHCX70gli+FohKK 7FBMVpRUTjdE3L+XJQMVqX/vBaYHejWfh+NYls/K8COYj7o9+9ZjLf84rzI5eHcLpE DbsgeuvT+gUtg== From: "Rafael J. Wysocki" To: Linux PM , Jonathan Cameron Cc: Takashi Iwai , LKML , Linux PCI , Alex Williamson , Bjorn Helgaas , Zhang Qilong , Ulf Hansson , Frank Li , Dhruva Gole Subject: [PATCH v4 3/3] PM: runtime: Drop DEFINE_FREE() for pm_runtime_put() Date: Fri, 26 Sep 2025 18:26:40 +0200 Message-ID: <7855547.EvYhyI6sBW@rafael.j.wysocki> Organization: Linux Kernel Development In-Reply-To: <6196611.lOV4Wx5bFT@rafael.j.wysocki> References: <6196611.lOV4Wx5bFT@rafael.j.wysocki> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Rafael J. Wysocki The DEFINE_FREE() for pm_runtime_put has been superseded by recently introduced runtime PM auto-cleanup macros and its only user has been converted to using one of the new macros, so drop it. Signed-off-by: Rafael J. Wysocki Reviewed-by: Dhruva Gole Reviewed-by: Jonathan Cameron Reviewed-by: Takashi Iwai --- v3 -> v4: Pick up R-by from Dhruva v1 -> v3: No changes --- include/linux/pm_runtime.h | 2 -- rust/kernel/cpufreq.rs | 3 ++- 2 files changed, 2 insertions(+), 3 deletions(-) --- a/include/linux/pm_runtime.h +++ b/include/linux/pm_runtime.h @@ -561,8 +561,6 @@ static inline int pm_runtime_put(struct return __pm_runtime_idle(dev, RPM_GET_PUT | RPM_ASYNC); } =20 -DEFINE_FREE(pm_runtime_put, struct device *, if (_T) pm_runtime_put(_T)) - /** * __pm_runtime_put_autosuspend - Drop device usage counter and queue auto= suspend if 0. * @dev: Target device. --- a/rust/kernel/cpufreq.rs +++ b/rust/kernel/cpufreq.rs @@ -39,7 +39,8 @@ use macros::vtable; const CPUFREQ_NAME_LEN: usize =3D bindings::CPUFREQ_NAME_LEN as usize; =20 /// Default transition latency value in nanoseconds. -pub const ETERNAL_LATENCY_NS: u32 =3D bindings::CPUFREQ_ETERNAL as u32; +pub const DEFAULT_TRANSITION_LATENCY_NS: u32 =3D + bindings::CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS as u32; =20 /// CPU frequency driver flags. pub mod flags { From nobody Wed Oct 1 22:33:21 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3D36261B7F; Fri, 26 Sep 2025 16:27:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758904075; cv=none; b=EMBfP55rRU4UM4U7P3oQhHcPJ5/yDv7hcb06Ze0ORMUBy6QpeqzTEIduCGd32EvoE0M7tWj13KE/k5cI/k4/7pKZuBQyWbXe3QIjfLXLEzTxK71q2KIGCEEex2wTJFyBJ80FtQx3y1OsxH14pYAhkPEwGLvkSkYZ5r5WZJxG5l8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758904075; c=relaxed/simple; bh=teMC0tMk1mLM4Of0idpMUE3tdsOJWZSq9BD6q7gznoA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GU3mm6hnmG6z0p/8bkJPzT10qawM6cO1xRNQVmsFNId0gh7MzPkGzD014/HEXChhgcAC/KNtvBGr80qq0Sr2NoZXeeenkXr79BkOQv7Tk9gHYSikhgLIY5XOapuaJikq3+eWRLiS+Ig2GWYSv+Rns4fKkjzXw+dUTC6eDvmBFdo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=W41rJvup; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="W41rJvup" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 36D07C4CEF4; Fri, 26 Sep 2025 16:27:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758904075; bh=teMC0tMk1mLM4Of0idpMUE3tdsOJWZSq9BD6q7gznoA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W41rJvupOXbqPvxGyRp7j5AeAfrhejdE0HwU9ZmzdSh5mEVveCxWRFFFbR0lr2gSJ Y1tkKKU27yX4JyhxZIkzyRwwm2OQfVAkMDcMUodgr46zeaPfy7sKsErwtLmMcO79tk JT8edlZZKWEFHp9lzBRqrj0WpLr6UIf6m9jWmgmIZVx0s48nEGRhvPT+jQUEayPVpW wfwDoqESldskL+bf5KPBTxmr1XE1OV40kmu02x7TyDr1qddgSIaR7eRJjos6ZE4oti wIohqrHZDRdpjedNgZbiOeaGSiDqIs0/Krkd0R2ufn8evPhSoeIXGAZn4ZIadAS2uS xXllXCIsMJ/lA== From: "Rafael J. Wysocki" To: Linux PM , Jonathan Cameron Cc: Takashi Iwai , LKML , Linux PCI , Alex Williamson , Bjorn Helgaas , Zhang Qilong , Ulf Hansson , Frank Li , Dhruva Gole Subject: [PATCH v4 1/3] PM: runtime: Add auto-cleanup macros for "resume and get" operations Date: Fri, 26 Sep 2025 17:47:14 +0200 Message-ID: <2238241.irdbgypaU6@rafael.j.wysocki> Organization: Linux Kernel Development In-Reply-To: <6196611.lOV4Wx5bFT@rafael.j.wysocki> References: <6196611.lOV4Wx5bFT@rafael.j.wysocki> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Rafael J. Wysocki It is generally useful to be able to automatically drop a device's runtime PM usage counter incremented by runtime PM operations that resume a device and bump up its usage counter [1]. To that end, add guard definition macros allowing pm_runtime_put() and pm_runtime_put_autosuspend() to be used for the auto-cleanup in those cases. Simply put, a piece of code like below: pm_runtime_get_sync(dev); ..... pm_runtime_put(dev); return 0; can be transformed with guard() like: guard(pm_runtime_active)(dev); ..... return 0; (see the pm_runtime_put() call is gone). However, it is better to do proper error handling in the majority of cases, so doing something like this instead of the above is recommended: ACQUIRE(pm_runtime_active_try, pm)(dev); if (ACQUIRE_ERR(pm_runtime_active_try, &pm)) return -ENXIO; ..... return 0; In all of the cases in which runtime PM is known to be enabled for the given device or the device can be regarded as operational (and so it can be accessed) with runtime PM disabled, a piece of code like: ret =3D pm_runtime_resume_and_get(dev); if (ret < 0) return ret; ..... pm_runtime_put(dev); return 0; can be changed as follows: ACQUIRE(pm_runtime_active_try, pm)(dev); ret =3D ACQUIRE_ERR(pm_runtime_active_try, &pm); if (ret < 0) return ret; ..... return 0; (again, see the pm_runtime_put() call is gone). Still, if the device cannot be accessed unless runtime PM has been enabled for it, the CLASS(pm_runtime_get_active_enabled) variant needs to be used, that is (in the context of the example above): ACQUIRE(pm_runtime_active_try_enabled, pm)(dev); ret =3D ACQUIRE_ERR(pm_runtime_active_try_enabled, &pm); if (ret < 0) return ret; ..... return 0; When the original code calls pm_runtime_put_autosuspend(), use one of the "auto" guard variants, pm_runtime_active_auto/_try/_enabled, so for example, a piece of code like: ret =3D pm_runtime_resume_and_get(dev); if (ret < 0) return ret; ..... pm_runtime_put_autosuspend(dev); return 0; will become: ACQUIRE(pm_runtime_active_auto_try_enabled, pm)(dev); ret =3D ACQUIRE_ERR(pm_runtime_active_auto_try_enabled, &pm); if (ret < 0) return ret; ..... return 0; Note that the cases in which the return value of pm_runtime_get_sync() is checked can also be handled with the help of the new class macros. For example, a piece of code like: ret =3D pm_runtime_get_sync(dev); if (ret < 0) { pm_runtime_put(dev); return ret; } ..... pm_runtime_put(dev); return 0; can be rewritten as: ACQUIRE(pm_runtime_active_auto_try_enabled, pm)(dev); ret =3D ACQUIRE_ERR(pm_runtime_active_auto_try_enabled, &pm); if (ret < 0) return ret; ..... return 0; or pm_runtime_get_active_try can be used if transparent handling of disabled runtime PM is desirable. Link: https://lore.kernel.org/linux-pm/878qimv24u.wl-tiwai@suse.de/ [1] Link: https://lore.kernel.org/linux-pm/20250926150613.000073a4@huawei.com/ Signed-off-by: Rafael J. Wysocki Acked-by: Dan Williams Reviewed-by: Dhruva Gole Reviewed-by: Jonathan Cameron Reviewed-by: Takashi Iwai --- v3 -> v4: * Use guard definition macros instead of raw DEFINE_CLASS() (Jonathan) * Change pm_runtime_get_active() helper definition to return an int inst= ead of a pointer * Update changelog to match the new code v2 -> v3: * Two more class definitions for the case in which resume errors can be neglected. * Update of new code comments (for more clarity). * Changelog update. v1 -> v2: * Rename the new classes and the new static inline helper. * Add two classes for handling disabled runtime PM. * Expand the changelog. * Adjust the subject. --- drivers/base/power/runtime.c | 2 + include/linux/pm_runtime.h | 44 ++++++++++++++++++++++++++++++++++----= ----- 2 files changed, 37 insertions(+), 9 deletions(-) --- a/drivers/base/power/runtime.c +++ b/drivers/base/power/runtime.c @@ -796,6 +796,8 @@ static int rpm_resume(struct device *dev if (dev->power.runtime_status =3D=3D RPM_ACTIVE && dev->power.last_status =3D=3D RPM_ACTIVE) retval =3D 1; + else if (rpmflags & RPM_TRANSPARENT) + goto out; else retval =3D -EACCES; } --- a/include/linux/pm_runtime.h +++ b/include/linux/pm_runtime.h @@ -21,6 +21,7 @@ #define RPM_GET_PUT 0x04 /* Increment/decrement the usage_count */ #define RPM_AUTO 0x08 /* Use autosuspend_delay */ +#define RPM_TRANSPARENT 0x10 /* Succeed if runtime PM is disabled */ =20 /* * Use this for defining a set of PM operations to be used in all situatio= ns @@ -511,6 +512,19 @@ static inline int pm_runtime_get_sync(st return __pm_runtime_resume(dev, RPM_GET_PUT); } =20 +static inline int pm_runtime_get_active(struct device *dev, int rpmflags) +{ + int ret; + + ret =3D __pm_runtime_resume(dev, RPM_GET_PUT | rpmflags); + if (ret < 0) { + pm_runtime_put_noidle(dev); + return ret; + } + + return 0; +} + /** * pm_runtime_resume_and_get - Bump up usage counter of a device and resum= e it. * @dev: Target device. @@ -521,15 +535,7 @@ static inline int pm_runtime_get_sync(st */ static inline int pm_runtime_resume_and_get(struct device *dev) { - int ret; - - ret =3D __pm_runtime_resume(dev, RPM_GET_PUT); - if (ret < 0) { - pm_runtime_put_noidle(dev); - return ret; - } - - return 0; + return pm_runtime_get_active(dev, 0); } =20 /** @@ -606,6 +612,26 @@ static inline int pm_runtime_put_autosus return __pm_runtime_put_autosuspend(dev); } =20 +DEFINE_GUARD(pm_runtime_active, struct device *, + pm_runtime_get_sync(_T), pm_runtime_put(_T)); +DEFINE_GUARD(pm_runtime_active_auto, struct device *, + pm_runtime_get_sync(_T), pm_runtime_put_autosuspend(_T)); +/* + * Use the following guards with ACQUIRE()/ACQUIRE_ERR(). + * + * The difference between the "_try" and "_try_enabled" variants is that t= he + * former do not produce an error when runtime PM is disabled for the given + * device. + */ +DEFINE_GUARD_COND(pm_runtime_active, _try, + pm_runtime_get_active(_T, RPM_TRANSPARENT)) +DEFINE_GUARD_COND(pm_runtime_active, _try_enabled, + pm_runtime_resume_and_get(_T)) +DEFINE_GUARD_COND(pm_runtime_active_auto, _try, + pm_runtime_get_active(_T, RPM_TRANSPARENT)) +DEFINE_GUARD_COND(pm_runtime_active_auto, _try_enabled, + pm_runtime_resume_and_get(_T)) + /** * pm_runtime_put_sync - Drop device usage counter and run "idle check" if= 0. * @dev: Target device.