Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH] cpufreq: initialize policy rwsem before sysfs publication
@ 2026-08-30 15:53 Runyu Xiao
  2026-08-31  5:29 ` Viresh Kumar
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Runyu Xiao @ 2026-08-30 15:53 UTC (permalink / raw)
  To: rafael; +Cc: viresh.kumar, linux-pm, linux-kernel, runyu.xiao, jianhao.xu,
	stable

cpufreq_policy_alloc() initializes policy->rwsem after
kobject_init_and_add() has created the policy sysfs directory and its
default attributes. A sysfs access can therefore reach a policy callback
before the semaphore has been initialized.

Initialize policy->rwsem before publishing the policy kobject so sysfs
callbacks always see an initialized semaphore.

Fixes: ad7722dab729 ("cpufreq: create per policy rwsem instead of per CPU cpu_policy_rwsem")
Cc: stable@vger.kernel.org
Assisted-by: Codex:GPT-5
Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
---
 drivers/cpufreq/cpufreq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 0d0df986f..9efbf5b17 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1258,6 +1258,8 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
 	if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
 		goto err_free_rcpumask;
 
+	init_rwsem(&policy->rwsem);
+
 	init_completion(&policy->kobj_unregister);
 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
 				   cpufreq_global_kobject, "policy%u", cpu);
@@ -1272,8 +1274,6 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
 		goto err_free_real_cpus;
 	}
 
-	init_rwsem(&policy->rwsem);
-
 	freq_constraints_init(&policy->constraints);
 
 	policy->nb_min.notifier_call = cpufreq_notifier_min;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] cpufreq: initialize policy rwsem before sysfs publication
  2026-08-30 15:53 [PATCH] cpufreq: initialize policy rwsem before sysfs publication Runyu Xiao
@ 2026-08-31  5:29 ` Viresh Kumar
  2026-08-31 12:13 ` Zhongqiu Han
  2026-09-01 12:19 ` [PATCH v2] " Runyu Xiao
  2 siblings, 0 replies; 9+ messages in thread
From: Viresh Kumar @ 2026-08-31  5:29 UTC (permalink / raw)
  To: Runyu Xiao; +Cc: rafael, linux-pm, linux-kernel, jianhao.xu, stable

On 30-08-26, 23:53, Runyu Xiao wrote:
> cpufreq_policy_alloc() initializes policy->rwsem after
> kobject_init_and_add() has created the policy sysfs directory and its
> default attributes. A sysfs access can therefore reach a policy callback
> before the semaphore has been initialized.
> 
> Initialize policy->rwsem before publishing the policy kobject so sysfs
> callbacks always see an initialized semaphore.
> 
> Fixes: ad7722dab729 ("cpufreq: create per policy rwsem instead of per CPU cpu_policy_rwsem")
> Cc: stable@vger.kernel.org
> Assisted-by: Codex:GPT-5
> Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
> ---
>  drivers/cpufreq/cpufreq.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>

-- 
viresh

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] cpufreq: initialize policy rwsem before sysfs publication
  2026-08-30 15:53 [PATCH] cpufreq: initialize policy rwsem before sysfs publication Runyu Xiao
  2026-08-31  5:29 ` Viresh Kumar
@ 2026-08-31 12:13 ` Zhongqiu Han
  2026-09-01  4:50   ` Viresh Kumar
  2026-09-02  4:19   ` [PATCH v3] " Runyu Xiao
  2026-09-01 12:19 ` [PATCH v2] " Runyu Xiao
  2 siblings, 2 replies; 9+ messages in thread
From: Zhongqiu Han @ 2026-08-31 12:13 UTC (permalink / raw)
  To: Runyu Xiao, rafael
  Cc: viresh.kumar, linux-pm, linux-kernel, jianhao.xu, stable,
	zhongqiu.han

On 8/30/2026 11:53 PM, Runyu Xiao wrote:
> cpufreq_policy_alloc() initializes policy->rwsem after
> kobject_init_and_add() has created the policy sysfs directory and its
> default attributes. A sysfs access can therefore reach a policy callback
> before the semaphore has been initialized.
> 
> Initialize policy->rwsem before publishing the policy kobject so sysfs
> callbacks always see an initialized semaphore.
> 
> Fixes: ad7722dab729 ("cpufreq: create per policy rwsem instead of per CPU cpu_policy_rwsem")

The tag should be 2fc3384dc75b ("cpufreq: Initialize policy->kobj while
allocating policy").

> Cc: stable@vger.kernel.org
> Assisted-by: Codex:GPT-5
> Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
> ---
>   drivers/cpufreq/cpufreq.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index 0d0df986f..9efbf5b17 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -1258,6 +1258,8 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
>   	if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
>   		goto err_free_rcpumask;
>   
> +	init_rwsem(&policy->rwsem);
> +
>   	init_completion(&policy->kobj_unregister);
>   	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
>   				   cpufreq_global_kobject, "policy%u", cpu);
> @@ -1272,8 +1274,6 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
>   		goto err_free_real_cpus;
>   	}
>   
> -	init_rwsem(&policy->rwsem);
> -
>   	freq_constraints_init(&policy->constraints);
>   
>   	policy->nb_min.notifier_call = cpufreq_notifier_min;


A separate issue: policy->cpus is allocated with alloc_cpumask_var() (no
__GFP_ZERO), so once the kobject is published policy_is_inactive() can
read a garbage mask and let show()/store() reach the attribute callbacks
on a half-initialized policy. I'll send a separate fix for this.


-- 
Thx and BRs,
Zhongqiu Han

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] cpufreq: initialize policy rwsem before sysfs publication
  2026-08-31 12:13 ` Zhongqiu Han
@ 2026-09-01  4:50   ` Viresh Kumar
  2026-09-02  4:19   ` [PATCH v3] " Runyu Xiao
  1 sibling, 0 replies; 9+ messages in thread
From: Viresh Kumar @ 2026-09-01  4:50 UTC (permalink / raw)
  To: Zhongqiu Han
  Cc: Runyu Xiao, rafael, linux-pm, linux-kernel, jianhao.xu, stable

On 31-08-26, 20:13, Zhongqiu Han wrote:
> On 8/30/2026 11:53 PM, Runyu Xiao wrote:
> > cpufreq_policy_alloc() initializes policy->rwsem after
> > kobject_init_and_add() has created the policy sysfs directory and its
> > default attributes. A sysfs access can therefore reach a policy callback
> > before the semaphore has been initialized.
> > 
> > Initialize policy->rwsem before publishing the policy kobject so sysfs
> > callbacks always see an initialized semaphore.
> > 
> > Fixes: ad7722dab729 ("cpufreq: create per policy rwsem instead of per CPU cpu_policy_rwsem")
> 
> The tag should be 2fc3384dc75b ("cpufreq: Initialize policy->kobj while
> allocating policy").

Right.

-- 
viresh

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2] cpufreq: initialize policy rwsem before sysfs publication
  2026-08-30 15:53 [PATCH] cpufreq: initialize policy rwsem before sysfs publication Runyu Xiao
  2026-08-31  5:29 ` Viresh Kumar
  2026-08-31 12:13 ` Zhongqiu Han
@ 2026-09-01 12:19 ` Runyu Xiao
  2026-09-01 13:07   ` Zhongqiu Han
  2 siblings, 1 reply; 9+ messages in thread
From: Runyu Xiao @ 2026-09-01 12:19 UTC (permalink / raw)
  To: Rafael J . Wysocki
  Cc: Viresh Kumar, Zhongqiu Han, linux-pm, linux-kernel, stable,
	Runyu Xiao, Jianhao Xu

cpufreq_policy_alloc() initializes policy->rwsem after
kobject_init_and_add() has created the policy sysfs directory and its
default attributes. A sysfs access can therefore reach a policy callback
before the semaphore has been initialized.

Initialize policy->rwsem before publishing the policy kobject so sysfs
callbacks always see an initialized semaphore.

Fixes: 2fc3384dc75b ("cpufreq: Initialize policy->kobj while allocating policy")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/all/20260830155301.2713780-1-runyu.xiao@seu.edu.cn/
Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
Assisted-by: Codex:GPT-5

---
v2:
- Correct the Fixes tag to the commit that introduced policy kobject publication.
---
 drivers/cpufreq/cpufreq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 0d0df986f..9efbf5b17 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1258,6 +1258,8 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
 	if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
 		goto err_free_rcpumask;
 
+	init_rwsem(&policy->rwsem);
+
 	init_completion(&policy->kobj_unregister);
 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
 				   cpufreq_global_kobject, "policy%u", cpu);
@@ -1272,8 +1274,6 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
 		goto err_free_real_cpus;
 	}
 
-	init_rwsem(&policy->rwsem);
-
 	freq_constraints_init(&policy->constraints);
 
 	policy->nb_min.notifier_call = cpufreq_notifier_min;
-- 
2.34.1

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] cpufreq: initialize policy rwsem before sysfs publication
  2026-09-01 12:19 ` [PATCH v2] " Runyu Xiao
@ 2026-09-01 13:07   ` Zhongqiu Han
  0 siblings, 0 replies; 9+ messages in thread
From: Zhongqiu Han @ 2026-09-01 13:07 UTC (permalink / raw)
  To: Runyu Xiao, Rafael J . Wysocki
  Cc: Viresh Kumar, linux-pm, linux-kernel, stable, Jianhao Xu,
	zhongqiu.han

On 9/1/2026 8:19 PM, Runyu Xiao wrote:
> cpufreq_policy_alloc() initializes policy->rwsem after
> kobject_init_and_add() has created the policy sysfs directory and its
> default attributes. A sysfs access can therefore reach a policy callback
> before the semaphore has been initialized.
> 
> Initialize policy->rwsem before publishing the policy kobject so sysfs
> callbacks always see an initialized semaphore.
> 
> Fixes: 2fc3384dc75b ("cpufreq: Initialize policy->kobj while allocating policy")
> Cc: stable@vger.kernel.org
> Link: https://lore.kernel.org/all/20260830155301.2713780-1-runyu.xiao@seu.edu.cn/

Nit: It points to V1 Link, but should be okay according by https://
docs.kernel.org/process/submitting-patches.html#describe-your-changes

> Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
> Assisted-by: Codex:GPT-5

Nit: Sob tag should always be the last tag.


Reviewed-by: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>


> 
> ---
> v2:
> - Correct the Fixes tag to the commit that introduced policy kobject publication.
> ---
>   drivers/cpufreq/cpufreq.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index 0d0df986f..9efbf5b17 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -1258,6 +1258,8 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
>   	if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
>   		goto err_free_rcpumask;
>   
> +	init_rwsem(&policy->rwsem);
> +
>   	init_completion(&policy->kobj_unregister);
>   	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
>   				   cpufreq_global_kobject, "policy%u", cpu);
> @@ -1272,8 +1274,6 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
>   		goto err_free_real_cpus;
>   	}
>   
> -	init_rwsem(&policy->rwsem);
> -
>   	freq_constraints_init(&policy->constraints);
>   
>   	policy->nb_min.notifier_call = cpufreq_notifier_min;


-- 
Thx and BRs,
Zhongqiu Han

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v3] cpufreq: initialize policy rwsem before sysfs publication
  2026-08-31 12:13 ` Zhongqiu Han
  2026-09-01  4:50   ` Viresh Kumar
@ 2026-09-02  4:19   ` Runyu Xiao
  2026-09-02  4:27     ` Viresh Kumar
  1 sibling, 1 reply; 9+ messages in thread
From: Runyu Xiao @ 2026-09-02  4:19 UTC (permalink / raw)
  To: Rafael J . Wysocki
  Cc: Viresh Kumar, Zhongqiu Han, linux-pm, linux-kernel, stable,
	Runyu Xiao, Jianhao Xu

cpufreq_policy_alloc() initializes policy->rwsem after
kobject_init_and_add() has created the policy sysfs directory and its
default attributes. A sysfs access can therefore reach a policy callback
before the semaphore has been initialized.

Initialize policy->rwsem before publishing the policy kobject so sysfs
callbacks always see an initialized semaphore.

Fixes: 2fc3384dc75b ("cpufreq: Initialize policy->kobj while allocating policy")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/all/20260830155301.2713780-1-runyu.xiao@seu.edu.cn/
Assisted-by: Codex:GPT-5
Reviewed-by: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>

---
v3:
- Move Signed-off-by to the end of the trailer block and add Zhongqiu Han's
  Reviewed-by tag.

v2:
- Correct the Fixes tag to the commit that introduced policy kobject publication.
---
 drivers/cpufreq/cpufreq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 0d0df986f..9efbf5b17 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1258,6 +1258,8 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
 	if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
 		goto err_free_rcpumask;
 
+	init_rwsem(&policy->rwsem);
+
 	init_completion(&policy->kobj_unregister);
 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
 				   cpufreq_global_kobject, "policy%u", cpu);
@@ -1272,8 +1274,6 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
 		goto err_free_real_cpus;
 	}
 
-	init_rwsem(&policy->rwsem);
-
 	freq_constraints_init(&policy->constraints);
 
 	policy->nb_min.notifier_call = cpufreq_notifier_min;
-- 
2.34.1

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v3] cpufreq: initialize policy rwsem before sysfs publication
  2026-09-02  4:19   ` [PATCH v3] " Runyu Xiao
@ 2026-09-02  4:27     ` Viresh Kumar
  2026-09-04 15:06       ` Rafael J. Wysocki (Intel)
  0 siblings, 1 reply; 9+ messages in thread
From: Viresh Kumar @ 2026-09-02  4:27 UTC (permalink / raw)
  To: Runyu Xiao
  Cc: Rafael J . Wysocki, Zhongqiu Han, linux-pm, linux-kernel, stable,
	Jianhao Xu

On 02-09-26, 12:19, Runyu Xiao wrote:
> cpufreq_policy_alloc() initializes policy->rwsem after
> kobject_init_and_add() has created the policy sysfs directory and its
> default attributes. A sysfs access can therefore reach a policy callback
> before the semaphore has been initialized.
> 
> Initialize policy->rwsem before publishing the policy kobject so sysfs
> callbacks always see an initialized semaphore.
> 
> Fixes: 2fc3384dc75b ("cpufreq: Initialize policy->kobj while allocating policy")
> Cc: stable@vger.kernel.org
> Link: https://lore.kernel.org/all/20260830155301.2713780-1-runyu.xiao@seu.edu.cn/
> Assisted-by: Codex:GPT-5
> Reviewed-by: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
> Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>

-- 
viresh

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3] cpufreq: initialize policy rwsem before sysfs publication
  2026-09-02  4:27     ` Viresh Kumar
@ 2026-09-04 15:06       ` Rafael J. Wysocki (Intel)
  0 siblings, 0 replies; 9+ messages in thread
From: Rafael J. Wysocki (Intel) @ 2026-09-04 15:06 UTC (permalink / raw)
  To: Viresh Kumar, Runyu Xiao; +Cc: Zhongqiu Han, linux-pm, linux-kernel, Jianhao Xu

On Wed, Sep 2, 2026 at 6:27 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> On 02-09-26, 12:19, Runyu Xiao wrote:
> > cpufreq_policy_alloc() initializes policy->rwsem after
> > kobject_init_and_add() has created the policy sysfs directory and its
> > default attributes. A sysfs access can therefore reach a policy callback
> > before the semaphore has been initialized.
> >
> > Initialize policy->rwsem before publishing the policy kobject so sysfs
> > callbacks always see an initialized semaphore.
> >
> > Fixes: 2fc3384dc75b ("cpufreq: Initialize policy->kobj while allocating policy")
> > Cc: stable@vger.kernel.org
> > Link: https://lore.kernel.org/all/20260830155301.2713780-1-runyu.xiao@seu.edu.cn/
> > Assisted-by: Codex:GPT-5
> > Reviewed-by: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
> > Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
>
> Acked-by: Viresh Kumar <viresh.kumar@linaro.org>

Applied as 7.3-rc material, thanks!

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-09-04 15:07 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-30 15:53 [PATCH] cpufreq: initialize policy rwsem before sysfs publication Runyu Xiao
2026-08-31  5:29 ` Viresh Kumar
2026-08-31 12:13 ` Zhongqiu Han
2026-09-01  4:50   ` Viresh Kumar
2026-09-02  4:19   ` [PATCH v3] " Runyu Xiao
2026-09-02  4:27     ` Viresh Kumar
2026-09-04 15:06       ` Rafael J. Wysocki (Intel)
2026-09-01 12:19 ` [PATCH v2] " Runyu Xiao
2026-09-01 13:07   ` Zhongqiu Han

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox