aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/cpufreq
diff options
context:
space:
mode:
authorSrivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com>2014-03-24 04:05:44 -0400
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>2014-03-26 11:41:40 -0400
commit12478cf0c55e5969f740bb38a24b1a0104ae18d8 (patch)
tree6bae48ba5b54525c3df629bd44757574ae11c92c /drivers/cpufreq
parentc2294a2f7853e6450361d078b65407bdaa6d1d11 (diff)
cpufreq: Make sure frequency transitions are serialized
Whenever we change the frequency of a CPU, we call the PRECHANGE and POSTCHANGE notifiers. They must be serialized, i.e. PRECHANGE and POSTCHANGE notifiers should strictly alternate, thereby preventing two different sets of PRECHANGE or POSTCHANGE notifiers from interleaving arbitrarily. The following examples illustrate why this is important: Scenario 1: ----------- A thread reading the value of cpuinfo_cur_freq, will call __cpufreq_cpu_get()->cpufreq_out_of_sync()->cpufreq_notify_transition() The ondemand governor can decide to change the frequency of the CPU at the same time and hence it can end up sending the notifications via ->target(). If the notifiers are not serialized, the following sequence can occur: - PRECHANGE Notification for freq A (from cpuinfo_cur_freq) - PRECHANGE Notification for freq B (from target()) - Freq changed by target() to B - POSTCHANGE Notification for freq B - POSTCHANGE Notification for freq A We can see from the above that the last POSTCHANGE Notification happens for freq A but the hardware is set to run at freq B. Where would we break then?: adjust_jiffies() in cpufreq.c & cpufreq_callback() in arch/arm/kernel/smp.c (which also adjusts the jiffies). All the loops_per_jiffy calculations will get messed up. Scenario 2: ----------- The governor calls __cpufreq_driver_target() to change the frequency. At the same time, if we change scaling_{min|max}_freq from sysfs, it will end up calling the governor's CPUFREQ_GOV_LIMITS notification, which will also call __cpufreq_driver_target(). And hence we end up issuing concurrent calls to ->target(). Typically, platforms have the following logic in their ->target() routines: (Eg: cpufreq-cpu0, omap, exynos, etc) A. If new freq is more than old: Increase voltage B. Change freq C. If new freq is less than old: decrease voltage Now, if the two concurrent calls to ->target() are X and Y, where X is trying to increase the freq and Y is trying to decrease it, we get the following race condition: X.A: voltage gets increased for larger freq Y.A: nothing happens Y.B: freq gets decreased Y.C: voltage gets decreased X.B: freq gets increased X.C: nothing happens Thus we can end up setting a freq which is not supported by the voltage we have set. That will probably make the clock to the CPU unstable and the system might not work properly anymore. This patch introduces a set of synchronization primitives to serialize frequency transitions, which are to be used as shown below: cpufreq_freq_transition_begin(); //Perform the frequency change cpufreq_freq_transition_end(); The _begin() call sends the PRECHANGE notification whereas the _end() call sends the POSTCHANGE notification. Also, all the necessary synchronization is handled within these calls. In particular, even drivers which set the ASYNC_NOTIFICATION flag can also use these APIs for performing frequency transitions (ie., you can call _begin() from one task, and call the corresponding _end() from a different task). The actual synchronization underneath is not that complicated: The key challenge is to allow drivers to begin the transition from one thread and end it in a completely different thread (this is to enable drivers that do asynchronous POSTCHANGE notification from bottom-halves, to also use the same interface). To achieve this, a 'transition_ongoing' flag, a 'transition_lock' spinlock and a wait-queue are added per-policy. The flag and the wait-queue are used in conjunction to create an "uninterrupted flow" from _begin() to _end(). The spinlock is used to ensure that only one such "flow" is in flight at any given time. Put together, this provides us all the necessary synchronization. Signed-off-by: Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Diffstat (limited to 'drivers/cpufreq')
-rw-r--r--drivers/cpufreq/cpufreq.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index d8d6bc9d1815..d57806a85def 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -353,6 +353,41 @@ void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
353} 353}
354EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition); 354EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition);
355 355
356void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
357 struct cpufreq_freqs *freqs)
358{
359wait:
360 wait_event(policy->transition_wait, !policy->transition_ongoing);
361
362 spin_lock(&policy->transition_lock);
363
364 if (unlikely(policy->transition_ongoing)) {
365 spin_unlock(&policy->transition_lock);
366 goto wait;
367 }
368
369 policy->transition_ongoing = true;
370
371 spin_unlock(&policy->transition_lock);
372
373 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
374}
375EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
376
377void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
378 struct cpufreq_freqs *freqs, int transition_failed)
379{
380 if (unlikely(WARN_ON(!policy->transition_ongoing)))
381 return;
382
383 cpufreq_notify_post_transition(policy, freqs, transition_failed);
384
385 policy->transition_ongoing = false;
386
387 wake_up(&policy->transition_wait);
388}
389EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
390
356 391
357/********************************************************************* 392/*********************************************************************
358 * SYSFS INTERFACE * 393 * SYSFS INTERFACE *
@@ -985,6 +1020,8 @@ static struct cpufreq_policy *cpufreq_policy_alloc(void)
985 1020
986 INIT_LIST_HEAD(&policy->policy_list); 1021 INIT_LIST_HEAD(&policy->policy_list);
987 init_rwsem(&policy->rwsem); 1022 init_rwsem(&policy->rwsem);
1023 spin_lock_init(&policy->transition_lock);
1024 init_waitqueue_head(&policy->transition_wait);
988 1025
989 return policy; 1026 return policy;
990 1027