Skip to contentSkip to conversation

cpufreq: fix governor start/stop race condition

PATCH Superseded Xiaoguang Chen 4 participants 12 messages Jun 13, 2013 – Jun 20, 2013 CPU frequency scaling framework

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 cpufreq: fix governor start/stop race condition

cpufreq governor stop and start should be kept in sequence. If not, there will be unexpected behavior, for example:

we have 4 cpus and policy->cpu=cpu0, cpu1/2/3 are linked to cpu0. the normal sequence is as below:

1) Current governor is userspace, one application tries to set governor to ondemand. it will call __cpufreq_set_policy in which it will stop userspace governor and then start ondemand governor.

2) Current governor is userspace, now cpu0 hotplugs in cpu3, it will call cpufreq_add_policy_cpu. on which it first stops userspace governor, and then starts userspace governor.

Now if the sequence of above two cases interleaves, it becames below sequence:

1) application stops userspace governor
2) hotplug stops userspace governor
3) application starts ondemand governor
4) hotplug starts a governor

in step 4, hotplug is supposed to start userspace governor, but now the governor has been changed by application to ondemand, so hotplug starts ondemand governor again !!!!

The solution is: do not allow stop one policy's governor multi-times Governor stop should only do once for one policy, after it is stopped, no other governor stop should be executed. also add one mutext to protect __cpufreq_governor so governor operation can be kept in sequence.

---
 drivers/cpufreq/cpufreq.c | 24 ++++++++++++++++++++++++
 include/linux/cpufreq.h   |  1 +
 2 files changed, 25 insertions(+)
drivers/cpufreq/cpufreq.c +24 -0
index 2d53f47..b51473e 100644
@@ -46,6 +46,7 @@ static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
 #endif
 static DEFINE_RWLOCK(cpufreq_driver_lock);
+static DEFINE_MUTEX(cpufreq_governor_lock);
 
 /*
  * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
@@ -1562,6 +1563,21 @@ static int __cpufreq_governor(struct cpufreq_policy *policy,
 
 	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
 						policy->cpu, event);
+
+	mutex_lock(&cpufreq_governor_lock);
+	if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
+	    (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
+		mutex_unlock(&cpufreq_governor_lock);
+		return -EBUSY;
+	}
+
+	if (event == CPUFREQ_GOV_STOP)
+		policy->governor_enabled = 0;
+	else if (event == CPUFREQ_GOV_START)
+		policy->governor_enabled = 1;
+
+	mutex_unlock(&cpufreq_governor_lock);
+
 	ret = policy->governor->governor(policy, event);
 
 	if (!ret) {
@@ -1569,6 +1585,14 @@ static int __cpufreq_governor(struct cpufreq_policy *policy,
 			policy->governor->initialized++;
 		else if (event == CPUFREQ_GOV_POLICY_EXIT)
 			policy->governor->initialized--;
+	} else {
+		/* Restore original values */
+		mutex_lock(&cpufreq_governor_lock);
+		if (event == CPUFREQ_GOV_STOP)
+			policy->governor_enabled = 1;
+		else if (event == CPUFREQ_GOV_START)
+			policy->governor_enabled = 0;
+		mutex_unlock(&cpufreq_governor_lock);
 	}
 
 	/* we keep one module reference alive for
include/linux/cpufreq.h +1 -0
index 037d36a..c12db73 100644
@@ -107,6 +107,7 @@ struct cpufreq_policy {
 	unsigned int		policy; /* see above */
 	struct cpufreq_governor	*governor; /* see below */
 	void			*governor_data;
+	int			governor_enabled; /* governor start/stop flag */
 
 	struct work_struct	update; /* if update_policy() needs to be
 					 * called, but you're in IRQ context */
Signature · 1 line
1.8.0
maintainer Acked-by on v5 1/1
#1 lore raw
Quoted 31 lines · Xiaoguang Chen

cpufreq governor stop and start should be kept in sequence. If not, there will be unexpected behavior, for example:

we have 4 cpus and policy->cpu=cpu0, cpu1/2/3 are linked to cpu0. the normal sequence is as below:

1) Current governor is userspace, one application tries to set governor to ondemand. it will call __cpufreq_set_policy in which it will stop userspace governor and then start ondemand governor.

2) Current governor is userspace, now cpu0 hotplugs in cpu3, it will call cpufreq_add_policy_cpu. on which it first stops userspace governor, and then starts userspace governor.

Now if the sequence of above two cases interleaves, it becames below sequence:

1) application stops userspace governor
2) hotplug stops userspace governor
3) application starts ondemand governor
4) hotplug starts a governor

in step 4, hotplug is supposed to start userspace governor, but now the governor has been changed by application to ondemand, so hotplug starts ondemand governor again !!!!

The solution is: do not allow stop one policy's governor multi-times Governor stop should only do once for one policy, after it is stopped, no other governor stop should be executed. also add one mutext to protect __cpufreq_governor so governor operation can be kept in sequence.

Quoted 5 lines
---
 drivers/cpufreq/cpufreq.c | 24 ++++++++++++++++++++++++
 include/linux/cpufreq.h   |  1 +
 2 files changed, 25 insertions(+)

I forgot to tell you earlier but please update changelog everytime you send a new version.

#2 lore raw

2013/6/13 Viresh Kumar <[email protected]>:

Quoted 38 lines · Xiaoguang Chen
Quoted 31 lines

cpufreq governor stop and start should be kept in sequence. If not, there will be unexpected behavior, for example:

we have 4 cpus and policy->cpu=cpu0, cpu1/2/3 are linked to cpu0. the normal sequence is as below:

1) Current governor is userspace, one application tries to set governor to ondemand. it will call __cpufreq_set_policy in which it will stop userspace governor and then start ondemand governor.

2) Current governor is userspace, now cpu0 hotplugs in cpu3, it will call cpufreq_add_policy_cpu. on which it first stops userspace governor, and then starts userspace governor.

Now if the sequence of above two cases interleaves, it becames below sequence:

1) application stops userspace governor
2) hotplug stops userspace governor
3) application starts ondemand governor
4) hotplug starts a governor

in step 4, hotplug is supposed to start userspace governor, but now the governor has been changed by application to ondemand, so hotplug starts ondemand governor again !!!!

The solution is: do not allow stop one policy's governor multi-times Governor stop should only do once for one policy, after it is stopped, no other governor stop should be executed. also add one mutext to protect __cpufreq_governor so governor operation can be kept in sequence.

Quoted 5 lines
---
 drivers/cpufreq/cpufreq.c | 24 ++++++++++++++++++++++++
 include/linux/cpufreq.h   |  1 +
 2 files changed, 25 insertions(+)
Quoted 4 lines

I forgot to tell you earlier but please update changelog everytime you send a new version.

Thanks
I'll pay attention next time :)

Xiaoguang

#3 lore raw
In reply to Xiaoguang Chen
Quoted 4 lines · Xiaoguang Chen

cpufreq governor stop and start should be kept in sequence. If not, there will be unexpected behavior, for example:

we have 4 cpus and policy->cpu=cpu0, cpu1/2/3 are linked to cpu0.

Please spell cpus as "CPUs". And please start sequences from capitals.

[Yes, it *really* is a problem.]

Quoted 5 lines

the normal sequence is as below:

1) Current governor is userspace, one application tries to set governor to ondemand. it will call __cpufreq_set_policy in which it will stop userspace governor and then start ondemand governor.

Do I think correctly that this is for all CPUs?

Quoted 1 line

2) Current governor is userspace, now cpu0 hotplugs in cpu3, it will

Can you please tell me what the above is supposed to mean? Is it supposed to mean "the online of cpu3 is being run on cpu0" or something different? If something different, then what?

Quoted 8 lines

call cpufreq_add_policy_cpu. on which it first stops userspace governor, and then starts userspace governor.

Now if the sequence of above two cases interleaves, it becames below sequence:

1) application stops userspace governor
2) hotplug stops userspace governor

The problem is already here, right? The governor shouldn't be stopped twice?

Quoted 11 lines

3) application starts ondemand governor
4) hotplug starts a governor

in step 4, hotplug is supposed to start userspace governor, but now the governor has been changed by application to ondemand, so hotplug starts ondemand governor again !!!!

The solution is: do not allow stop one policy's governor multi-times Governor stop should only do once for one policy, after it is stopped, no other governor stop should be executed. also add one mutext to protect __cpufreq_governor so governor operation can be kept in sequence.

One more request. ->

Quoted 60 lines
---
 drivers/cpufreq/cpufreq.c | 24 ++++++++++++++++++++++++
 include/linux/cpufreq.h   |  1 +
 2 files changed, 25 insertions(+)
drivers/cpufreq/cpufreq.c +24 -0
index 2d53f47..b51473e 100644
@@ -46,6 +46,7 @@ static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
 #endif
 static DEFINE_RWLOCK(cpufreq_driver_lock);
+static DEFINE_MUTEX(cpufreq_governor_lock);
 
 /*
  * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
@@ -1562,6 +1563,21 @@ static int __cpufreq_governor(struct cpufreq_policy *policy,
 
 	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
 						policy->cpu, event);
+
+	mutex_lock(&cpufreq_governor_lock);
+	if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
+	    (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
+		mutex_unlock(&cpufreq_governor_lock);
+		return -EBUSY;
+	}
+
+	if (event == CPUFREQ_GOV_STOP)
+		policy->governor_enabled = 0;
+	else if (event == CPUFREQ_GOV_START)
+		policy->governor_enabled = 1;
+
+	mutex_unlock(&cpufreq_governor_lock);
+
 	ret = policy->governor->governor(policy, event);
 
 	if (!ret) {
@@ -1569,6 +1585,14 @@ static int __cpufreq_governor(struct cpufreq_policy *policy,
 			policy->governor->initialized++;
 		else if (event == CPUFREQ_GOV_POLICY_EXIT)
 			policy->governor->initialized--;
+	} else {
+		/* Restore original values */
+		mutex_lock(&cpufreq_governor_lock);
+		if (event == CPUFREQ_GOV_STOP)
+			policy->governor_enabled = 1;
+		else if (event == CPUFREQ_GOV_START)
+			policy->governor_enabled = 0;
+		mutex_unlock(&cpufreq_governor_lock);
 	}
 
 	/* we keep one module reference alive for
include/linux/cpufreq.h +0 -0
index 037d36a..c12db73 100644

include/linux/cpufreq.h struct cpufreq_policy { · line 110

Quoted 4 lines
include/linux/cpufreq.h +1 -0
@@ -107,6 +107,7 @@ struct cpufreq_policy {
 	unsigned int		policy; /* see above */
 	struct cpufreq_governor	*governor; /* see below */
 	void			*governor_data;
+	int			governor_enabled; /* governor start/stop flag */

-> Please use bool here and true/false instead of 1/0 above.

include/linux/cpufreq.h struct cpufreq_policy { · line 113

Quoted 3 lines
include/linux/cpufreq.h +0 -0
 	struct work_struct	update; /* if update_policy() needs to be
 					 * called, but you're in IRQ context */

Thanks,
Rafael

Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
#4 lore raw

2013/6/19 Rafael J. Wysocki <[email protected]>:

Quoted 7 lines · Xiaoguang Chen
Quoted 4 lines

cpufreq governor stop and start should be kept in sequence. If not, there will be unexpected behavior, for example:

we have 4 cpus and policy->cpu=cpu0, cpu1/2/3 are linked to cpu0.

Please spell cpus as "CPUs". And please start sequences from capitals.

Ok, thanks for the remind

Quoted 10 lines

[Yes, it *really* is a problem.]

Quoted 5 lines

the normal sequence is as below:

1) Current governor is userspace, one application tries to set governor to ondemand. it will call __cpufreq_set_policy in which it will stop userspace governor and then start ondemand governor.

Do I think correctly that this is for all CPUs?

From current code design, it is for all CPUs.

Quoted 10 lines
Quoted 2 lines

2) Current governor is userspace, now cpu0 hotplugs in cpu3, it will

Can you please tell me what the above is supposed to mean? Is it supposed to mean "the online of cpu3 is being run on cpu0" or something different? If something different, then what?

Quoted 3 lines

call cpufreq_add_policy_cpu. on which it first stops userspace governor, and then starts userspace governor.

Quoted 7 lines
Quoted 5 lines

Now if the sequence of above two cases interleaves, it becames below sequence:

1) application stops userspace governor
2) hotplug stops userspace governor

The problem is already here, right? The governor shouldn't be stopped twice?

Yes, we should make sure governor is started before it is stopped.

Quoted 69 lines
Quoted 4 lines

3) application starts ondemand governor
4) hotplug starts a governor

Quoted 8 lines

in step 4, hotplug is supposed to start userspace governor, but now the governor has been changed by application to ondemand, so hotplug starts ondemand governor again !!!!

The solution is: do not allow stop one policy's governor multi-times Governor stop should only do once for one policy, after it is stopped, no other governor stop should be executed. also add one mutext to protect __cpufreq_governor so governor operation can be kept in sequence.

One more request. ->

Quoted 54 lines
---
 drivers/cpufreq/cpufreq.c | 24 ++++++++++++++++++++++++
 include/linux/cpufreq.h   |  1 +
 2 files changed, 25 insertions(+)
drivers/cpufreq/cpufreq.c +24 -0
index 2d53f47..b51473e 100644
@@ -46,6 +46,7 @@ static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
 #endif
 static DEFINE_RWLOCK(cpufreq_driver_lock);
+static DEFINE_MUTEX(cpufreq_governor_lock);
 
 /*
  * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
@@ -1562,6 +1563,21 @@ static int __cpufreq_governor(struct cpufreq_policy *policy,
 
      pr_debug("__cpufreq_governor for CPU %u, event %u\n",
                                              policy->cpu, event);
+
+     mutex_lock(&cpufreq_governor_lock);
+     if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
+         (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
+             mutex_unlock(&cpufreq_governor_lock);
+             return -EBUSY;
+     }
+
+     if (event == CPUFREQ_GOV_STOP)
+             policy->governor_enabled = 0;
+     else if (event == CPUFREQ_GOV_START)
+             policy->governor_enabled = 1;
+
+     mutex_unlock(&cpufreq_governor_lock);
+
      ret = policy->governor->governor(policy, event);
 
      if (!ret) {
@@ -1569,6 +1585,14 @@ static int __cpufreq_governor(struct cpufreq_policy *policy,
                      policy->governor->initialized++;
              else if (event == CPUFREQ_GOV_POLICY_EXIT)
                      policy->governor->initialized--;
+     } else {
+             /* Restore original values */
+             mutex_lock(&cpufreq_governor_lock);
+             if (event == CPUFREQ_GOV_STOP)
+                     policy->governor_enabled = 1;
+             else if (event == CPUFREQ_GOV_START)
+                     policy->governor_enabled = 0;
+             mutex_unlock(&cpufreq_governor_lock);
      }
 
Quoted 13 lines

include/linux/cpufreq.h struct cpufreq_policy { · line 110

Quoted 10 lines
drivers/cpufreq/cpufreq.c +0 -0
      /* we keep one module reference alive for
include/linux/cpufreq.h +1 -0
index 037d36a..c12db73 100644
@@ -107,6 +107,7 @@ struct cpufreq_policy {
      unsigned int            policy; /* see above */
      struct cpufreq_governor *governor; /* see below */
      void                    *governor_data;
+     int                     governor_enabled; /* governor start/stop flag */

-> Please use bool here and true/false instead of 1/0 above.

Ok, I'll change it to bool.

Quoted 11 lines

include/linux/cpufreq.h struct cpufreq_policy { · line 113

Quoted 3 lines
include/linux/cpufreq.h +0 -0
      struct work_struct      update; /* if update_policy() needs to be
                                       * called, but you're in IRQ context */

Thanks,
Rafael

--
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.

maintainer
#5 lore raw
Quoted 13 lines · Xiaoguang Chen

2013/6/19 Rafael J. Wysocki <[email protected]>:

Quoted 7 lines · Xiaoguang Chen
Quoted 4 lines

cpufreq governor stop and start should be kept in sequence. If not, there will be unexpected behavior, for example:

we have 4 cpus and policy->cpu=cpu0, cpu1/2/3 are linked to cpu0.

Please spell cpus as "CPUs". And please start sequences from capitals.

Ok, thanks for the remind

Quoted 2 lines

[Yes, it *really* is a problem.]

Just wanted to know the reasoning behind it so that I can remind others about it and then argue :)

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

the normal sequence is as below:

1) Current governor is userspace, one application tries to set governor to ondemand. it will call __cpufreq_set_policy in which it will stop userspace governor and then start ondemand governor.

Do I think correctly that this is for all CPUs?

From current code design, it is for all CPUs.

Why? This can be for a single cpu (which would eventually force all others CPUs sharing policy with it).

maintainer
#8 lore raw
In reply to Xiaoguang Chen
Quoted 7 lines · Xiaoguang Chen

2013/6/19 Rafael J. Wysocki <[email protected]>:

Quoted 5 lines
Quoted 1 line

2) Current governor is userspace, now cpu0 hotplugs in cpu3, it will

Can you please tell me what the above is supposed to mean? Is it supposed to mean "the online of cpu3 is being run on cpu0" or something different? If something different, then what?

Please read all the questions carefully. You missed this one.

Actually you should write: Current governor is userspace, now cpu0 hot-unplugs cpu3, it will **

maintainer
#9 lore raw
Quoted 5 lines · Viresh Kumar
Quoted 4 lines · Xiaoguang Chen

2013/6/19 Rafael J. Wysocki <[email protected]>:

Quoted 2 lines
Quoted 1 line

2) Current governor is userspace, now cpu0 hotplugs in cpu3, it will

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

Can you please tell me what the above is supposed to mean? Is it supposed to mean "the online of cpu3 is being run on cpu0" or something different? If something different, then what?

Please read all the questions carefully. You missed this one.

Actually you should write: Current governor is userspace, now cpu0 hot-unplugs cpu3, it will **

Ahh I am mistaken, you are actually bringing cpu3 back to the system. Then you must have mentioned earlier that cpu3 wasn't online.

#10 lore raw

2013/6/19 Viresh Kumar <[email protected]>:

Quoted 9 lines · Viresh Kumar
Quoted 8 lines · Xiaoguang Chen
Quoted 7 lines

2013/6/19 Rafael J. Wysocki <[email protected]>:

Quoted 5 lines
Quoted 1 line

2) Current governor is userspace, now cpu0 hotplugs in cpu3, it will

Can you please tell me what the above is supposed to mean? Is it supposed to mean "the online of cpu3 is being run on cpu0" or something different? If something different, then what?

Sorry I missed this question, Let me explain it in detail Suppose we are in such condtition, current cpufreq goveror is userspace governor. cpu3 is offline.
and two things happen as above two cases, first thing is application tries to change current governor to ondemand governor, second thing is cpu0 tries to make cpu3 online which is off line before. both of these two cases will try to stop current governor and start a governor. if above two things interleave, unexpected behavior will happen.

Quoted 8 lines
Quoted 5 lines

Please read all the questions carefully. You missed this one.

Actually you should write: Current governor is userspace, now cpu0 hot-unplugs cpu3, it will **

Ahh I am mistaken, you are actually bringing cpu3 back to the system. Then you must have mentioned earlier that cpu3 wasn't online.

#6 lore raw
In reply to Viresh Kumar
Quoted 10 lines · Viresh Kumar
Quoted 9 lines · Xiaoguang Chen

2013/6/19 Rafael J. Wysocki <[email protected]>:

Quoted 7 lines · Xiaoguang Chen
Quoted 4 lines

cpufreq governor stop and start should be kept in sequence. If not, there will be unexpected behavior, for example:

we have 4 cpus and policy->cpu=cpu0, cpu1/2/3 are linked to cpu0.

Please spell cpus as "CPUs". And please start sequences from capitals.

Quoted 7 lines
Quoted 4 lines

Ok, thanks for the remind

Quoted 2 lines

[Yes, it *really* is a problem.]

Just wanted to know the reasoning behind it so that I can remind others about it and then argue :)

Well, sentences that don't start from upper-case letters don't look like separate sentences and when I see "cpus" written like this I tend to read it as "cups" with a typo.

Generally speaking, don't make changelogs harder to read than they have to be.

They usually are hard enough to read anyway.

Thanks,
Rafael

Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
#11 lore raw
In reply to Xiaoguang Chen
Quoted 11 lines · Xiaoguang Chen

2013/6/19 Viresh Kumar <[email protected]>:

Quoted 9 lines · Viresh Kumar
Quoted 8 lines · Xiaoguang Chen
Quoted 7 lines

2013/6/19 Rafael J. Wysocki <[email protected]>:

Quoted 5 lines
Quoted 1 line

2) Current governor is userspace, now cpu0 hotplugs in cpu3, it will

Can you please tell me what the above is supposed to mean? Is it supposed to mean "the online of cpu3 is being run on cpu0" or something different? If something different, then what?

Quoted 9 lines

Sorry I missed this question, Let me explain it in detail Suppose we are in such condtition, current cpufreq goveror is userspace governor. cpu3 is offline.
and two things happen as above two cases, first thing is application tries to change current governor to ondemand governor, second thing is cpu0 tries to make cpu3 online which is off line before. both of these two cases will try to stop current governor and start a governor. if above two things interleave, unexpected behavior will happen.

Now it's clear, thanks for the explanation. [Well, I'll still need to fix up the changelog. Sigh.]

Thanks,
Rafael

Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
maintainer
#7 lore raw
In reply to Rafael J. Wysocki
Quoted 2 lines · Rafael J. Wysocki

Well, sentences that don't start from upper-case letters don't look like separate sentences

s/sequences/sentences for your initial mail.

I misunderstood it as, you want to name cpu{0|1|2} as CPU{1|2|3}.. Obviously sentences must start with capitals.

Quoted 2 lines

and when I see "cpus" written like this I tend to read it as "cups" with a typo.

Hmm..