From nobody Fri Dec 19 20:55:43 2025 Received: from cloudserver094114.home.pl (cloudserver094114.home.pl [79.96.170.134]) (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 C6AFD1F4289; Wed, 16 Apr 2025 14:12:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.96.170.134 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744812769; cv=none; b=N+S49iF2YpNGVuzs/qk5PlxxilWd7cfqL5uYFMGI6/YjNL97se8XvbOTkr43peyF0yMF/lK/m334Z8tt8EtANCDKdhuLt0yl9XkZONASNkyfoR5peAMkPB1lW95ECIR77k0jpUl5Dt+RGj90fbzE2Ap3EvSTftLiDbHg41zAqYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744812769; c=relaxed/simple; bh=KotPUopKxcGbf+KA04P6eWybcs6CE4c0CchTd06wOMM=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=kbtoAw2G3Vzmix/DCoR7RY8qiGvbKqHZTVJujehdEW4424ip2JPY/r2I4JTwUTxXkREdzTDTM3V0CkGD0OACbp1v8Z2qhJxKG+lYorYDesiMa731sm9FGUwN68vrTAhij6HcaNoqJOcG+fmLTVEOq+t3FOzEnXwIP6VqrESubHw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rjwysocki.net; spf=pass smtp.mailfrom=rjwysocki.net; dkim=pass (2048-bit key) header.d=rjwysocki.net header.i=@rjwysocki.net header.b=ah/bMtUE; arc=none smtp.client-ip=79.96.170.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rjwysocki.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rjwysocki.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rjwysocki.net header.i=@rjwysocki.net header.b="ah/bMtUE" Received: from kreacher.localnet (unknown [195.136.19.94]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by cloudserver094114.home.pl (Postfix) with ESMTPSA id C479A662710; Wed, 16 Apr 2025 16:12:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=rjwysocki.net; s=dkim; t=1744812758; bh=KotPUopKxcGbf+KA04P6eWybcs6CE4c0CchTd06wOMM=; h=From:Subject:Date; b=ah/bMtUE3VwO4HUM2oWQIjcHci2YGl5czsYNjRMbM8UZRR7RC6WYZJooCO493D1JN lYDQJHnM985tYy1+DCQJlV/hxpPi4eqYVlGpjIdCQRS9h5qEc9K8mMUtldyYCXpHg/ 0zcF5e6cysWrXAvOmeq9pUmyRhf/QOZHcybsAnRwAXsDXFk0+cqkqUpnTFsem2AjqI PW+dhgdNFoiys3Esiu6GUB3CtxAQuaUyg8SdB8qOCWpDfLp8qpBqxCNxmuuLrbjgPu kQdLTmeM1d2AEA9kwEUXQ/hxZHSgz/KZUm6qaw/Sl3ZNUAm5QJSquAJo0HEm2JVUqN fttvboINyatZw== From: "Rafael J. Wysocki" To: Linux PM , Christian Loehle Cc: LKML , Viresh Kumar , Srinivas Pandruvada , Mario Limonciello , Vincent Guittot , Sultan Alsawaf , Peter Zijlstra , Valentin Schneider , Ingo Molnar Subject: [PATCH v3] cpufreq: Avoid using inconsistent policy->min and policy->max Date: Wed, 16 Apr 2025 16:12:37 +0200 Message-ID: <5907080.DvuYhMxLoT@rjwysocki.net> 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 X-CLIENT-IP: 195.136.19.94 X-CLIENT-HOSTNAME: 195.136.19.94 X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvvdeiheekucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecujffqoffgrffnpdggtffipffknecuuegrihhlohhuthemucduhedtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufffkfgggfgtsehtufertddttdejnecuhfhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqnecuggftrfgrthhtvghrnhepgeffhfdujeelhfdtgeffkeetudfhtefhhfeiteethfekvefgvdfgfeeikeeigfehnecuffhomhgrihhnpehkvghrnhgvlhdrohhrghenucfkphepudelhedrudefiedrudelrdelgeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeduleehrddufeeirdduledrleegpdhhvghlohepkhhrvggrtghhvghrrdhlohgtrghlnhgvthdpmhgrihhlfhhrohhmpehrjhifsehrjhifhihsohgtkhhirdhnvghtpdhnsggprhgtphhtthhopeduuddprhgtphhtthhopehlihhnuhigqdhpmhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegthhhrihhsthhirghnrdhlohgvhhhlvgesrghrmhdrtghomhdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehvihhrvghshhdrkhhumhgrrheslhhinhgrrhhordhorhhgpdhrtghpthhtohepshh X-DCC--Metrics: v370.home.net.pl 1024; Body=11 Fuz1=11 Fuz2=11 Content-Type: text/plain; charset="utf-8" From: Rafael J. Wysocki Since cpufreq_driver_resolve_freq() can run in parallel with cpufreq_set_policy() and there is no synchronization between them, the former may access policy->min and policy->max while the latter is updating them and it may see intermediate values of them due to the way the update is carried out. Also the compiler is free to apply any optimizations it wants both to the stores in cpufreq_set_policy() and to the loads in cpufreq_driver_resolve_freq() which may result in additional inconsistencies. To address this, use WRITE_ONCE() when updating policy->min and policy->max in cpufreq_set_policy() and use READ_ONCE() for reading them in cpufreq_driver_resolve_freq(). Moreover, rearrange the update in cpufreq_set_policy() to avoid storing intermediate values in policy->min and policy->max with the help of the observation that their new values are expected to be properly ordered upfront. Also modify cpufreq_driver_resolve_freq() to take the possible reverse ordering of policy->min and policy->max, which may happen depending on the ordering of operations when this function and cpufreq_set_policy() run concurrently, into account by always honoring the max when it turns out to be less than the min (in case it comes from thermal throttling or similar). Fixes: 151717690694 ("cpufreq: Make policy min/max hard requirements") Cc: 5.16+ # 5.16+ Signed-off-by: Rafael J. Wysocki Acked-by: Viresh Kumar Reviewed-by: Christian Loehle --- This replaces the last 3 patches in https://lore.kernel.org/linux-pm/6171293.lOV4Wx5bFT@rjwysocki.net/ v2 -> v3: * Fold 3 patches into one. * Drop an unrelated white space fixup change. * Fix a typo in a comment (Christian). v1 -> v2: Cosmetic changes --- drivers/cpufreq/cpufreq.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -495,8 +495,6 @@ { unsigned int idx; =20 - target_freq =3D clamp_val(target_freq, policy->min, policy->max); - if (!policy->freq_table) return target_freq; =20 @@ -520,7 +518,22 @@ unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, unsigned int target_freq) { - return __resolve_freq(policy, target_freq, CPUFREQ_RELATION_LE); + unsigned int min =3D READ_ONCE(policy->min); + unsigned int max =3D READ_ONCE(policy->max); + + /* + * If this function runs in parallel with cpufreq_set_policy(), it may + * read policy->min before the update and policy->max after the update + * or the other way around, so there is no ordering guarantee. + * + * Resolve this by always honoring the max (in case it comes from + * thermal throttling or similar). + */ + if (unlikely(min > max)) + min =3D max; + + return __resolve_freq(policy, clamp_val(target_freq, min, max), + CPUFREQ_RELATION_LE); } EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); =20 @@ -2338,6 +2351,7 @@ if (cpufreq_disabled()) return -ENODEV; =20 + target_freq =3D clamp_val(target_freq, policy->min, policy->max); target_freq =3D __resolve_freq(policy, target_freq, relation); =20 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", @@ -2631,11 +2645,15 @@ * Resolve policy min/max to available frequencies. It ensures * no frequency resolution will neither overshoot the requested maximum * nor undershoot the requested minimum. + * + * Avoid storing intermediate values in policy->max or policy->min and + * compiler optimizations around them because they may be accessed + * concurrently by cpufreq_driver_resolve_freq() during the update. */ - policy->min =3D new_data.min; - policy->max =3D new_data.max; - policy->min =3D __resolve_freq(policy, policy->min, CPUFREQ_RELATION_L); - policy->max =3D __resolve_freq(policy, policy->max, CPUFREQ_RELATION_H); + WRITE_ONCE(policy->max, __resolve_freq(policy, new_data.max, CPUFREQ_RELA= TION_H)); + new_data.min =3D __resolve_freq(policy, new_data.min, CPUFREQ_RELATION_L); + WRITE_ONCE(policy->min, new_data.min > policy->max ? policy->max : new_da= ta.min); + trace_cpu_frequency_limits(policy); =20 cpufreq_update_pressure(policy);