From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796326858; b=W/RJoxz2H9aUcMEWEyvxuHzY7sHmsdaiF8RRBP0lxK6JtqY6s5eUm4UwVhOci9hLlKMT FqMrqEmtpvng5n75Uhk7d3xTZ4EAo1ijoSy4TD4yi2uLJuGkakF0WznDJA7MFrTb4M7o5 A4Bv3/wWN8magQ3g6d3IzJRmwux1v3vlfk5mCEBYR8yLCeEGnsArjEkHOn0lpuk4bKnya VITOR5MOIifak2mCPsSfe/sj6dsvMZBz+U2K9GsC+KKJgtHajzk8tWkJBWeWBbs+VbD9x QTseYjW9c4gl31aQWfcHwkHbWMK81jcCygs0BOdE0NvoZk5WOrIBS62rDbBMisLMOaQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796326858; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=8GTkljcz5YWIteOdTKs5ORx24FV4LEEZQVy4utZJhVU=; b=e8Ct1UllRNkCS67Lp26qoArCUs3PqejLRAyWsR7Jh1I0+abm80tr9ft92EiqEOTfAhHc 0PyP7Aowq/tv5k2l3AIWUpDTmmd6g3GxOvnoMjGDAcWUCpJywBNx2Iba+7raJKbiXRVPz FVEPZk+6SQ0vllCKTVkn/2mn0bX5ruUVCu7RHRKHVNhVE6OmIzm2d7DKD6DTg3JFLvIJf bWWPFhofUU2tR3ILO5Vx8whmHMm754dI6aX5G0tnu6yNLXGj6ti/5aKEagWEfABaPVv7q n1PtS2DQ/2LYv9jOVVRWB/Ot+ZUC7obVM/u/P1SLlqTdwvFnJ8cnZ6xmdD0Mbz1zSqg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=X+f0Q4Sw; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF56460A3808 for ; Thu, 3 Dec 2026 19:40:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=X+f0Q4Sw Received: by smtp.kernel.org (Postfix) with ESMTPSA id 846061F0092B; Thu, 3 Dec 2026 19:40:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796326858; bh=8GTkljcz5YWIteOdTKs5ORx24FV4LEEZQVy4utZJhVU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=X+f0Q4SwfywhIbXwxXRt+FDA1VNJzaCuDwx12+MPqiP07NYzNF9zpg0tGLVKK7I06 l2C09vHVaS+X4kuQd+Rx9+dvgCu63zEX9ldAfdNPwIoOqC8AhAYZT3OChFQjyvXExj NN3BVZjT1wwFRlDIp6V8bZTx/CbMR4d3XLRTWPoILEjoOLKf/s235+Ap+VIo10YPW+ F6RTBP4vY71WfIzjErvnNP5NYZEEw9XZ3lmobcZ+PKP0DYzDT5/Jw+gM5zDmK2IohR boWwq+YhdmX1GuBjjXDT73LP2hFYb8U+KrsFvlV+ryehwGTkOCYFqdvQ8mrssaVFru 726E9F0+PJ40A== From: Sasha Levin To: stable@vger.kernel.org Cc: "Rafael J. Wysocki" , Viresh Kumar , Mario Limonciello , Sudeep Holla , Sasha Levin Subject: [PATCH 6.1.y 3/4] cpufreq: Add and use cpufreq policy locking guards Date: Thu, 3 Dec 2026 14:40:52 -0500 Message-ID: <20261203194041.666445-3-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261203194041.666445-1-sashal@kernel.org> References: <2026120248-concerned-system-eb91@gregkh> <20261203194041.666445-1-sashal@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Rafael J. Wysocki" [ Upstream commit 6fec833b9d70c54ceacbf7d07665215fbd0cddef ] Introduce "read" and "write" locking guards for cpufreq policies and use them where applicable in the cpufreq core. No intentional functional impact. Signed-off-by: Rafael J. Wysocki Acked-by: Viresh Kumar Reviewed-by: Mario Limonciello Acked-by: Sudeep Holla Tested-by: Sudeep Holla Link: https://patch.msgid.link/8518682.T7Z3S40VBb@rjwysocki.net Stable-dep-of: c5bdb8757372 ("cpufreq: Skip inactive policies in cpufreq_driver_target()") Signed-off-by: Sasha Levin --- drivers/cpufreq/cpufreq.c | 109 +++++++++++++++++--------------------- include/linux/cpufreq.h | 6 +++ 2 files changed, 54 insertions(+), 61 deletions(-) diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index b49d7bd3eeb83..8fda458d38bce 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -975,17 +975,16 @@ static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) { struct cpufreq_policy *policy = to_policy(kobj); struct freq_attr *fattr = to_attr(attr); - ssize_t ret = -EBUSY; if (!fattr->show) return -EIO; - down_read(&policy->rwsem); + guard(cpufreq_policy_read)(policy); + if (likely(!policy_is_inactive(policy))) - ret = fattr->show(policy, buf); - up_read(&policy->rwsem); + return fattr->show(policy, buf); - return ret; + return -EBUSY; } static ssize_t store(struct kobject *kobj, struct attribute *attr, @@ -993,17 +992,16 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr, { struct cpufreq_policy *policy = to_policy(kobj); struct freq_attr *fattr = to_attr(attr); - ssize_t ret = -EBUSY; if (!fattr->store) return -EIO; - down_write(&policy->rwsem); + guard(cpufreq_policy_write)(policy); + if (likely(!policy_is_inactive(policy))) - ret = fattr->store(policy, buf, count); - up_write(&policy->rwsem); + return fattr->store(policy, buf, count); - return ret; + return -EBUSY; } static void cpufreq_sysfs_release(struct kobject *kobj) @@ -1134,7 +1132,8 @@ static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cp if (cpumask_test_cpu(cpu, policy->cpus)) return 0; - down_write(&policy->rwsem); + guard(cpufreq_policy_write)(policy); + if (has_target()) cpufreq_stop_governor(policy); @@ -1145,7 +1144,7 @@ static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cp if (ret) pr_err("%s: Failed to start governor\n", __func__); } - up_write(&policy->rwsem); + return ret; } @@ -1165,9 +1164,10 @@ static void handle_update(struct work_struct *work) container_of(work, struct cpufreq_policy, update); pr_debug("handle_update for cpu %u called\n", policy->cpu); - down_write(&policy->rwsem); + + guard(cpufreq_policy_write)(policy); + refresh_frequency_limits(policy); - up_write(&policy->rwsem); } static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq, @@ -1193,11 +1193,11 @@ static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) struct kobject *kobj; struct completion *cmp; - down_write(&policy->rwsem); - cpufreq_stats_free_table(policy); - kobj = &policy->kobj; - cmp = &policy->kobj_unregister; - up_write(&policy->rwsem); + scoped_guard(cpufreq_policy_write, policy) { + cpufreq_stats_free_table(policy); + kobj = &policy->kobj; + cmp = &policy->kobj_unregister; + } kobject_put(kobj); /* @@ -1349,7 +1349,7 @@ static int cpufreq_policy_online(struct cpufreq_policy *policy, unsigned int j; int ret; - down_write(&policy->rwsem); + guard(cpufreq_policy_write)(policy); policy->cpu = cpu; policy->governor = NULL; @@ -1522,10 +1522,7 @@ static int cpufreq_policy_online(struct cpufreq_policy *policy, goto out_destroy_policy; } -out_unlock: - up_write(&policy->rwsem); - - return ret; + return 0; out_destroy_policy: for_each_cpu(j, policy->real_cpus) @@ -1542,7 +1539,7 @@ static int cpufreq_policy_online(struct cpufreq_policy *policy, out_clear_policy: cpumask_clear(policy->cpus); - goto out_unlock; + return ret; } static int cpufreq_online(unsigned int cpu) @@ -1677,11 +1674,10 @@ static int cpufreq_offline(unsigned int cpu) return 0; } - down_write(&policy->rwsem); + guard(cpufreq_policy_write)(policy); __cpufreq_offline(cpu, policy); - up_write(&policy->rwsem); return 0; } @@ -1698,33 +1694,29 @@ static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) if (!policy) return; - down_write(&policy->rwsem); + scoped_guard(cpufreq_policy_write, policy) { + if (cpu_online(cpu)) + __cpufreq_offline(cpu, policy); - if (cpu_online(cpu)) - __cpufreq_offline(cpu, policy); + remove_cpu_dev_symlink(policy, cpu, dev); - remove_cpu_dev_symlink(policy, cpu, dev); + if (!cpumask_empty(policy->real_cpus)) + return; - if (!cpumask_empty(policy->real_cpus)) { - up_write(&policy->rwsem); - return; - } + /* + * Unregister cpufreq cooling once all the CPUs of the policy + * are removed. + */ + if (cpufreq_thermal_control_enabled(cpufreq_driver)) { + cpufreq_cooling_unregister(policy->cdev); + policy->cdev = NULL; + } - /* - * Unregister cpufreq cooling once all the CPUs of the policy are - * removed. - */ - if (cpufreq_thermal_control_enabled(cpufreq_driver)) { - cpufreq_cooling_unregister(policy->cdev); - policy->cdev = NULL; + /* We did light-weight exit earlier, do full tear down now */ + if (cpufreq_driver->offline && cpufreq_driver->exit) + cpufreq_driver->exit(policy); } - /* We did light-weight exit earlier, do full tear down now */ - if (cpufreq_driver->offline && cpufreq_driver->exit) - cpufreq_driver->exit(policy); - - up_write(&policy->rwsem); - cpufreq_policy_free(policy); } @@ -1877,15 +1869,16 @@ unsigned int cpufreq_get(unsigned int cpu) struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); unsigned int ret_freq = 0; - if (policy) { - down_read(&policy->rwsem); + if (!policy) + return 0; + + scoped_guard(cpufreq_policy_read, policy) { if (cpufreq_driver->get) ret_freq = __cpufreq_get(policy); - up_read(&policy->rwsem); - - cpufreq_cpu_put(policy); } + cpufreq_cpu_put(policy); + return ret_freq; } EXPORT_SYMBOL(cpufreq_get); @@ -2400,15 +2393,9 @@ int cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation) { - int ret; + guard(cpufreq_policy_write)(policy); - down_write(&policy->rwsem); - - ret = __cpufreq_driver_target(policy, target_freq, relation); - - up_write(&policy->rwsem); - - return ret; + return __cpufreq_driver_target(policy, target_freq, relation); } EXPORT_SYMBOL_GPL(cpufreq_driver_target); diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index a6535f51f3681..7cf9b7db0abff 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -170,6 +170,12 @@ struct cpufreq_policy { struct notifier_block nb_max; }; +DEFINE_GUARD(cpufreq_policy_write, struct cpufreq_policy *, + down_write(&_T->rwsem), up_write(&_T->rwsem)) + +DEFINE_GUARD(cpufreq_policy_read, struct cpufreq_policy *, + down_read(&_T->rwsem), up_read(&_T->rwsem)) + /* * Used for passing new cpufreq policy data to the cpufreq driver's ->verify() * callback for sanitization. That callback is only expected to modify the min -- 2.53.0