Skip to content

Commit ba17ca4

Browse files
XiaoguangChengregkh
authored andcommitted
cpufreq: Fix governor start/stop race condition
commit 95731eb upstream. Cpufreq governors' stop and start operations should be carried out in sequence. Otherwise, there will be unexpected behavior, like in the example below. Suppose there are 4 CPUs and policy->cpu=CPU0, CPU1/2/3 are linked to CPU0. The normal sequence is: 1) Current governor is userspace. An application tries to set the governor to ondemand. It will call __cpufreq_set_policy() in which it will stop the userspace governor and then start the ondemand governor. 2) Current governor is userspace. The online of CPU3 runs on CPU0. It will call cpufreq_add_policy_cpu() in which it will first stop the userspace governor, and then start it again. If the sequence of the above two cases interleaves, it becomes: 1) Application stops userspace governor 2) Hotplug stops userspace governor which is a problem, because the governor shouldn't be stopped twice in a row. What happens next is: 3) Application starts ondemand governor 4) Hotplug starts a governor In step 4, the hotplug is supposed to start the userspace governor, but now the governor has been changed by the application to ondemand, so the ondemand governor is started once again, which is incorrect. The solution is to prevent policy governors from being stopped multiple times in a row. A governor should only be stopped once for one policy. After it has been stopped, no more governor stop operations should be executed. Also add a mutex to serialize governor operations. [rjw: Changelog. And you owe me a beverage of my choice.] Signed-off-by: Xiaoguang Chen <chenxg@marvell.com> Acked-by: Viresh Kumar <viresh.kumar@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Cc: Krzysztof Kozlowski <k.kozlowski@samsung.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 87f93ce commit ba17ca4

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎drivers/cpufreq/cpufreq.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
4646
static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
4747
#endif
4848
static DEFINE_RWLOCK(cpufreq_driver_lock);
49+
static DEFINE_MUTEX(cpufreq_governor_lock);
4950

5051
/*
5152
* cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
@@ -1563,13 +1564,36 @@ static int __cpufreq_governor(struct cpufreq_policy *policy,
15631564

15641565
pr_debug("__cpufreq_governor for CPU %u, event %u\n",
15651566
policy->cpu, event);
1567+
1568+
mutex_lock(&cpufreq_governor_lock);
1569+
if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1570+
(policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1571+
mutex_unlock(&cpufreq_governor_lock);
1572+
return -EBUSY;
1573+
}
1574+
1575+
if (event == CPUFREQ_GOV_STOP)
1576+
policy->governor_enabled = false;
1577+
else if (event == CPUFREQ_GOV_START)
1578+
policy->governor_enabled = true;
1579+
1580+
mutex_unlock(&cpufreq_governor_lock);
1581+
15661582
ret = policy->governor->governor(policy, event);
15671583

15681584
if (!ret) {
15691585
if (event == CPUFREQ_GOV_POLICY_INIT)
15701586
policy->governor->initialized++;
15711587
else if (event == CPUFREQ_GOV_POLICY_EXIT)
15721588
policy->governor->initialized--;
1589+
} else {
1590+
/* Restore original values */
1591+
mutex_lock(&cpufreq_governor_lock);
1592+
if (event == CPUFREQ_GOV_STOP)
1593+
policy->governor_enabled = true;
1594+
else if (event == CPUFREQ_GOV_START)
1595+
policy->governor_enabled = false;
1596+
mutex_unlock(&cpufreq_governor_lock);
15731597
}
15741598

15751599
/* we keep one module reference alive for

‎include/linux/cpufreq.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ struct cpufreq_policy {
107107
unsigned int policy; /* see above */
108108
struct cpufreq_governor *governor; /* see below */
109109
void *governor_data;
110+
bool governor_enabled; /* governor start/stop flag */
110111

111112
struct work_struct update; /* if update_policy() needs to be
112113
* called, but you're in IRQ context */

0 commit comments

Comments
 (0)