@@ -52,9 +52,11 @@ struct sugov_policy {
5252struct sugov_cpu {
5353 struct update_util_data update_util ;
5454 struct sugov_policy * sg_policy ;
55+ unsigned int cpu ;
5556
56- unsigned long iowait_boost ;
57- unsigned long iowait_boost_max ;
57+ bool iowait_boost_pending ;
58+ unsigned int iowait_boost ;
59+ unsigned int iowait_boost_max ;
5860 u64 last_update ;
5961
6062 /* The fields below are only needed when sharing a policy. */
@@ -76,6 +78,26 @@ static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
7678{
7779 s64 delta_ns ;
7880
81+ /*
82+ * Since cpufreq_update_util() is called with rq->lock held for
83+ * the @target_cpu, our per-cpu data is fully serialized.
84+ *
85+ * However, drivers cannot in general deal with cross-cpu
86+ * requests, so while get_next_freq() will work, our
87+ * sugov_update_commit() call may not for the fast switching platforms.
88+ *
89+ * Hence stop here for remote requests if they aren't supported
90+ * by the hardware, as calculating the frequency is pointless if
91+ * we cannot in fact act on it.
92+ *
93+ * For the slow switching platforms, the kthread is always scheduled on
94+ * the right set of CPUs and any CPU can find the next frequency and
95+ * schedule the kthread.
96+ */
97+ if (sg_policy -> policy -> fast_switch_enabled &&
98+ !cpufreq_can_do_remote_dvfs (sg_policy -> policy ))
99+ return false;
100+
79101 if (sg_policy -> work_in_progress )
80102 return false;
81103
@@ -106,7 +128,7 @@ static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
106128
107129 if (policy -> fast_switch_enabled ) {
108130 next_freq = cpufreq_driver_fast_switch (policy , next_freq );
109- if (next_freq == CPUFREQ_ENTRY_INVALID )
131+ if (! next_freq )
110132 return ;
111133
112134 policy -> cur = next_freq ;
@@ -154,12 +176,12 @@ static unsigned int get_next_freq(struct sugov_policy *sg_policy,
154176 return cpufreq_driver_resolve_freq (policy , freq );
155177}
156178
157- static void sugov_get_util (unsigned long * util , unsigned long * max )
179+ static void sugov_get_util (unsigned long * util , unsigned long * max , int cpu )
158180{
159- struct rq * rq = this_rq ( );
181+ struct rq * rq = cpu_rq ( cpu );
160182 unsigned long cfs_max ;
161183
162- cfs_max = arch_scale_cpu_capacity (NULL , smp_processor_id () );
184+ cfs_max = arch_scale_cpu_capacity (NULL , cpu );
163185
164186 * util = min (rq -> cfs .avg .util_avg , cfs_max );
165187 * max = cfs_max ;
@@ -169,30 +191,54 @@ static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
169191 unsigned int flags )
170192{
171193 if (flags & SCHED_CPUFREQ_IOWAIT ) {
172- sg_cpu -> iowait_boost = sg_cpu -> iowait_boost_max ;
194+ if (sg_cpu -> iowait_boost_pending )
195+ return ;
196+
197+ sg_cpu -> iowait_boost_pending = true;
198+
199+ if (sg_cpu -> iowait_boost ) {
200+ sg_cpu -> iowait_boost <<= 1 ;
201+ if (sg_cpu -> iowait_boost > sg_cpu -> iowait_boost_max )
202+ sg_cpu -> iowait_boost = sg_cpu -> iowait_boost_max ;
203+ } else {
204+ sg_cpu -> iowait_boost = sg_cpu -> sg_policy -> policy -> min ;
205+ }
173206 } else if (sg_cpu -> iowait_boost ) {
174207 s64 delta_ns = time - sg_cpu -> last_update ;
175208
176209 /* Clear iowait_boost if the CPU apprears to have been idle. */
177- if (delta_ns > TICK_NSEC )
210+ if (delta_ns > TICK_NSEC ) {
178211 sg_cpu -> iowait_boost = 0 ;
212+ sg_cpu -> iowait_boost_pending = false;
213+ }
179214 }
180215}
181216
182217static void sugov_iowait_boost (struct sugov_cpu * sg_cpu , unsigned long * util ,
183218 unsigned long * max )
184219{
185- unsigned long boost_util = sg_cpu -> iowait_boost ;
186- unsigned long boost_max = sg_cpu -> iowait_boost_max ;
220+ unsigned int boost_util , boost_max ;
187221
188- if (!boost_util )
222+ if (!sg_cpu -> iowait_boost )
189223 return ;
190224
225+ if (sg_cpu -> iowait_boost_pending ) {
226+ sg_cpu -> iowait_boost_pending = false;
227+ } else {
228+ sg_cpu -> iowait_boost >>= 1 ;
229+ if (sg_cpu -> iowait_boost < sg_cpu -> sg_policy -> policy -> min ) {
230+ sg_cpu -> iowait_boost = 0 ;
231+ return ;
232+ }
233+ }
234+
235+ boost_util = sg_cpu -> iowait_boost ;
236+ boost_max = sg_cpu -> iowait_boost_max ;
237+
191238 if (* util * boost_max < * max * boost_util ) {
192239 * util = boost_util ;
193240 * max = boost_max ;
194241 }
195- sg_cpu -> iowait_boost >>= 1 ;
196242}
197243
198244#ifdef CONFIG_NO_HZ_COMMON
@@ -229,7 +275,7 @@ static void sugov_update_single(struct update_util_data *hook, u64 time,
229275 if (flags & SCHED_CPUFREQ_RT_DL ) {
230276 next_f = policy -> cpuinfo .max_freq ;
231277 } else {
232- sugov_get_util (& util , & max );
278+ sugov_get_util (& util , & max , sg_cpu -> cpu );
233279 sugov_iowait_boost (sg_cpu , & util , & max );
234280 next_f = get_next_freq (sg_policy , util , max );
235281 /*
@@ -264,6 +310,7 @@ static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
264310 delta_ns = time - j_sg_cpu -> last_update ;
265311 if (delta_ns > TICK_NSEC ) {
266312 j_sg_cpu -> iowait_boost = 0 ;
313+ j_sg_cpu -> iowait_boost_pending = false;
267314 continue ;
268315 }
269316 if (j_sg_cpu -> flags & SCHED_CPUFREQ_RT_DL )
@@ -290,7 +337,7 @@ static void sugov_update_shared(struct update_util_data *hook, u64 time,
290337 unsigned long util , max ;
291338 unsigned int next_f ;
292339
293- sugov_get_util (& util , & max );
340+ sugov_get_util (& util , & max , sg_cpu -> cpu );
294341
295342 raw_spin_lock (& sg_policy -> update_lock );
296343
@@ -445,7 +492,11 @@ static int sugov_kthread_create(struct sugov_policy *sg_policy)
445492 }
446493
447494 sg_policy -> thread = thread ;
448- kthread_bind_mask (thread , policy -> related_cpus );
495+
496+ /* Kthread is bound to all CPUs by default */
497+ if (!policy -> dvfs_possible_from_any_cpu )
498+ kthread_bind_mask (thread , policy -> related_cpus );
499+
449500 init_irq_work (& sg_policy -> irq_work , sugov_irq_work );
450501 mutex_init (& sg_policy -> work_lock );
451502
@@ -663,6 +714,11 @@ struct cpufreq_governor *cpufreq_default_governor(void)
663714
664715static int __init sugov_register (void )
665716{
717+ int cpu ;
718+
719+ for_each_possible_cpu (cpu )
720+ per_cpu (sugov_cpu , cpu ).cpu = cpu ;
721+
666722 return cpufreq_register_governor (& schedutil_gov );
667723}
668724fs_initcall (sugov_register );
0 commit comments