linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v6 1/2] cpufreq: Abort show/store for half initialized policy
@ 2022-05-16  3:02 Schspa Shi
  2022-05-16  3:02 ` [PATCH v6 2/2] cpufreq: make interface functions and lock holding state clear Schspa Shi
  2022-05-16 10:24 ` [PATCH v6 1/2] cpufreq: Abort show/store for half initialized policy Viresh Kumar
  0 siblings, 2 replies; 5+ messages in thread
From: Schspa Shi @ 2022-05-16  3:02 UTC (permalink / raw)
  To: rafael, viresh.kumar; +Cc: linux-pm, linux-kernel, schspa

If policy initialization fails after the sysfs files are created,
there is a possibility that we may end up running show()/store()
callbacks for half initialized policies, which may have unpredictable
outcomes.

Abort show/store in such a case by making sure the policy is active.
Also inactivate the policy on such failures.

Signed-off-by: Schspa Shi <schspa@gmail.com>

---

Changelog:
v1 -> v2:
        - Fix bad critical region enlarge which causes uninitialized
          unlock.
        - Move cpumask_clear(policy->cpus); before out_offline_policy
v2 -> v3:
        - Remove the missed down_write() before
          cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
v3 -> v4:
        - Seprate to two patchs.
        - Add policy_is_inactive check before sysfs access
v4 -> v5:
        - Change the commit message as Viresh advised.
        - Initialize ret to -EBUSY to get rid of the else part.
v5 -> v6:
        - Change up_write(&policy->rwsem); to after out_free_policy;
        - Fixes: https://lore.kernel.org/all/20220515095313.GE10578@xsang-OptiPlex-9020/

Signed-off-by: Schspa Shi <schspa@gmail.com>
---
 drivers/cpufreq/cpufreq.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 80f535cc8a75..ba73be6f0490 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -947,13 +947,14 @@ 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;
+	ssize_t ret = -EBUSY;
 
 	if (!fattr->show)
 		return -EIO;
 
 	down_read(&policy->rwsem);
-	ret = fattr->show(policy, buf);
+	if (likely(!policy_is_inactive(policy)))
+		ret = fattr->show(policy, buf);
 	up_read(&policy->rwsem);
 
 	return ret;
@@ -964,7 +965,7 @@ 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 = -EINVAL;
+	ssize_t ret = -EBUSY;
 
 	if (!fattr->store)
 		return -EIO;
@@ -978,7 +979,8 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
 
 	if (cpu_online(policy->cpu)) {
 		down_write(&policy->rwsem);
-		ret = fattr->store(policy, buf, count);
+		if (likely(!policy_is_inactive(policy)))
+			ret = fattr->store(policy, buf, count);
 		up_write(&policy->rwsem);
 	}
 
@@ -1533,6 +1535,7 @@ static int cpufreq_online(unsigned int cpu)
 	for_each_cpu(j, policy->real_cpus)
 		remove_cpu_dev_symlink(policy, get_cpu_device(j));
 
+	cpumask_clear(policy->cpus);
 	up_write(&policy->rwsem);
 
 out_offline_policy:
-- 
2.29.0


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

* [PATCH v6 2/2] cpufreq: make interface functions and lock holding state clear
  2022-05-16  3:02 [PATCH v6 1/2] cpufreq: Abort show/store for half initialized policy Schspa Shi
@ 2022-05-16  3:02 ` Schspa Shi
  2022-05-16 10:25   ` Viresh Kumar
  2022-05-16 10:24 ` [PATCH v6 1/2] cpufreq: Abort show/store for half initialized policy Viresh Kumar
  1 sibling, 1 reply; 5+ messages in thread
From: Schspa Shi @ 2022-05-16  3:02 UTC (permalink / raw)
  To: rafael, viresh.kumar; +Cc: linux-pm, linux-kernel, schspa

cpufreq_offline() calls offline() and exit() under the policy rwsem
But they are called outside the rwsem in cpufreq_online().

This patch move the offline(), exit(), online(), init() to be inside
of policy rwsem to achieve a clear lock relationship.

All the init() online() implement only initialize policy object without
holding this lock and won't call cpufreq APIs need to hold this lock.

Signed-off-by: Schspa Shi <schspa@gmail.com>
---
 drivers/cpufreq/cpufreq.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index ba73be6f0490..ccb774e02934 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1339,12 +1339,12 @@ static int cpufreq_online(unsigned int cpu)
 		down_write(&policy->rwsem);
 		policy->cpu = cpu;
 		policy->governor = NULL;
-		up_write(&policy->rwsem);
 	} else {
 		new_policy = true;
 		policy = cpufreq_policy_alloc(cpu);
 		if (!policy)
 			return -ENOMEM;
+		down_write(&policy->rwsem);
 	}
 
 	if (!new_policy && cpufreq_driver->online) {
@@ -1384,7 +1384,6 @@ static int cpufreq_online(unsigned int cpu)
 		cpumask_copy(policy->related_cpus, policy->cpus);
 	}
 
-	down_write(&policy->rwsem);
 	/*
 	 * affected cpus must always be the one, which are online. We aren't
 	 * managing offline cpus here.
@@ -1536,7 +1535,6 @@ static int cpufreq_online(unsigned int cpu)
 		remove_cpu_dev_symlink(policy, get_cpu_device(j));
 
 	cpumask_clear(policy->cpus);
-	up_write(&policy->rwsem);
 
 out_offline_policy:
 	if (cpufreq_driver->offline)
@@ -1547,6 +1545,8 @@ static int cpufreq_online(unsigned int cpu)
 		cpufreq_driver->exit(policy);
 
 out_free_policy:
+	up_write(&policy->rwsem);
+
 	cpufreq_policy_free(policy);
 	return ret;
 }
-- 
2.29.0


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

* Re: [PATCH v6 1/2] cpufreq: Abort show/store for half initialized policy
  2022-05-16  3:02 [PATCH v6 1/2] cpufreq: Abort show/store for half initialized policy Schspa Shi
  2022-05-16  3:02 ` [PATCH v6 2/2] cpufreq: make interface functions and lock holding state clear Schspa Shi
@ 2022-05-16 10:24 ` Viresh Kumar
  2022-05-17 19:40   ` Rafael J. Wysocki
  1 sibling, 1 reply; 5+ messages in thread
From: Viresh Kumar @ 2022-05-16 10:24 UTC (permalink / raw)
  To: Schspa Shi; +Cc: rafael, linux-pm, linux-kernel

On Mon, 16 May 2022 at 04:03, Schspa Shi <schspa@gmail.com> wrote:
>
> If policy initialization fails after the sysfs files are created,
> there is a possibility that we may end up running show()/store()
> callbacks for half initialized policies, which may have unpredictable
> outcomes.
>
> Abort show/store in such a case by making sure the policy is active.
> Also inactivate the policy on such failures.
>
> Signed-off-by: Schspa Shi <schspa@gmail.com>

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

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

* Re: [PATCH v6 2/2] cpufreq: make interface functions and lock holding state clear
  2022-05-16  3:02 ` [PATCH v6 2/2] cpufreq: make interface functions and lock holding state clear Schspa Shi
@ 2022-05-16 10:25   ` Viresh Kumar
  0 siblings, 0 replies; 5+ messages in thread
From: Viresh Kumar @ 2022-05-16 10:25 UTC (permalink / raw)
  To: Schspa Shi; +Cc: rafael, linux-pm, linux-kernel

On Mon, 16 May 2022 at 04:12, Schspa Shi <schspa@gmail.com> wrote:
>
> cpufreq_offline() calls offline() and exit() under the policy rwsem
> But they are called outside the rwsem in cpufreq_online().
>
> This patch move the offline(), exit(), online(), init() to be inside
> of policy rwsem to achieve a clear lock relationship.
>
> All the init() online() implement only initialize policy object without
> holding this lock and won't call cpufreq APIs need to hold this lock.
>
> Signed-off-by: Schspa Shi <schspa@gmail.com>
> ---
>  drivers/cpufreq/cpufreq.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)

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

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

* Re: [PATCH v6 1/2] cpufreq: Abort show/store for half initialized policy
  2022-05-16 10:24 ` [PATCH v6 1/2] cpufreq: Abort show/store for half initialized policy Viresh Kumar
@ 2022-05-17 19:40   ` Rafael J. Wysocki
  0 siblings, 0 replies; 5+ messages in thread
From: Rafael J. Wysocki @ 2022-05-17 19:40 UTC (permalink / raw)
  To: Viresh Kumar, Schspa Shi
  Cc: Rafael J. Wysocki, Linux PM, Linux Kernel Mailing List

On Mon, May 16, 2022 at 12:25 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> On Mon, 16 May 2022 at 04:03, Schspa Shi <schspa@gmail.com> wrote:
> >
> > If policy initialization fails after the sysfs files are created,
> > there is a possibility that we may end up running show()/store()
> > callbacks for half initialized policies, which may have unpredictable
> > outcomes.
> >
> > Abort show/store in such a case by making sure the policy is active.
> > Also inactivate the policy on such failures.
> >
> > Signed-off-by: Schspa Shi <schspa@gmail.com>
>
> Acked-by: Viresh Kumar <viresh.kumar@linaro.org>

Applied along with the [2/2], with some adjustments in the subject and
changelog, as 5.19 material.

Thanks!

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

end of thread, other threads:[~2022-05-17 19:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-16  3:02 [PATCH v6 1/2] cpufreq: Abort show/store for half initialized policy Schspa Shi
2022-05-16  3:02 ` [PATCH v6 2/2] cpufreq: make interface functions and lock holding state clear Schspa Shi
2022-05-16 10:25   ` Viresh Kumar
2022-05-16 10:24 ` [PATCH v6 1/2] cpufreq: Abort show/store for half initialized policy Viresh Kumar
2022-05-17 19:40   ` Rafael J. Wysocki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).