Skip to content

Commit 8f111bc

Browse files
Peter Zijlstraingomolnar
authored andcommitted
cpufreq/schedutil: Rewrite CPUFREQ_RT support
Instead of trying to duplicate scheduler state to track if an RT task is running, directly use the scheduler runqueue state for it. This vastly simplifies things and fixes a number of bugs related to sugov and the scheduler getting out of sync wrt this state. As a consequence we not also update the remove cfs/dl state when iterating the shared mask. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Juri Lelli <juri.lelli@arm.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Viresh Kumar <viresh.kumar@linaro.org> Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 4042d00 commit 8f111bc

3 files changed

Lines changed: 41 additions & 45 deletions

File tree

‎include/linux/sched/cpufreq.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
* Interface between cpufreq drivers and the scheduler:
99
*/
1010

11-
#define SCHED_CPUFREQ_RT (1U << 0)
12-
#define SCHED_CPUFREQ_IOWAIT (1U << 1)
11+
#define SCHED_CPUFREQ_IOWAIT (1U << 0)
1312

1413
#ifdef CONFIG_CPU_FREQ
1514
struct update_util_data {

‎kernel/sched/cpufreq_schedutil.c‎

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ struct sugov_cpu {
5757
unsigned long util_cfs;
5858
unsigned long util_dl;
5959
unsigned long max;
60-
unsigned int flags;
6160

6261
/* The field below is for single-CPU policies only: */
6362
#ifdef CONFIG_NO_HZ_COMMON
@@ -183,17 +182,28 @@ static void sugov_get_util(struct sugov_cpu *sg_cpu)
183182

184183
static unsigned long sugov_aggregate_util(struct sugov_cpu *sg_cpu)
185184
{
185+
struct rq *rq = cpu_rq(sg_cpu->cpu);
186+
unsigned long util;
187+
188+
if (rq->rt.rt_nr_running) {
189+
util = sg_cpu->max;
190+
} else {
191+
util = sg_cpu->util_dl;
192+
if (rq->cfs.h_nr_running)
193+
util += sg_cpu->util_cfs;
194+
}
195+
186196
/*
187197
* Ideally we would like to set util_dl as min/guaranteed freq and
188198
* util_cfs + util_dl as requested freq. However, cpufreq is not yet
189199
* ready for such an interface. So, we only do the latter for now.
190200
*/
191-
return min(sg_cpu->util_cfs + sg_cpu->util_dl, sg_cpu->max);
201+
return min(util, sg_cpu->max);
192202
}
193203

194-
static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time)
204+
static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time, unsigned int flags)
195205
{
196-
if (sg_cpu->flags & SCHED_CPUFREQ_IOWAIT) {
206+
if (flags & SCHED_CPUFREQ_IOWAIT) {
197207
if (sg_cpu->iowait_boost_pending)
198208
return;
199209

@@ -262,38 +272,34 @@ static void sugov_update_single(struct update_util_data *hook, u64 time,
262272
{
263273
struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
264274
struct sugov_policy *sg_policy = sg_cpu->sg_policy;
265-
struct cpufreq_policy *policy = sg_policy->policy;
266275
unsigned long util, max;
267276
unsigned int next_f;
268277
bool busy;
269278

270-
sugov_set_iowait_boost(sg_cpu, time);
279+
sugov_set_iowait_boost(sg_cpu, time, flags);
271280
sg_cpu->last_update = time;
272281

273282
if (!sugov_should_update_freq(sg_policy, time))
274283
return;
275284

276285
busy = sugov_cpu_is_busy(sg_cpu);
277286

278-
if (flags & SCHED_CPUFREQ_RT) {
279-
next_f = policy->cpuinfo.max_freq;
280-
} else {
281-
sugov_get_util(sg_cpu);
282-
max = sg_cpu->max;
283-
util = sugov_aggregate_util(sg_cpu);
284-
sugov_iowait_boost(sg_cpu, &util, &max);
285-
next_f = get_next_freq(sg_policy, util, max);
286-
/*
287-
* Do not reduce the frequency if the CPU has not been idle
288-
* recently, as the reduction is likely to be premature then.
289-
*/
290-
if (busy && next_f < sg_policy->next_freq) {
291-
next_f = sg_policy->next_freq;
287+
sugov_get_util(sg_cpu);
288+
max = sg_cpu->max;
289+
util = sugov_aggregate_util(sg_cpu);
290+
sugov_iowait_boost(sg_cpu, &util, &max);
291+
next_f = get_next_freq(sg_policy, util, max);
292+
/*
293+
* Do not reduce the frequency if the CPU has not been idle
294+
* recently, as the reduction is likely to be premature then.
295+
*/
296+
if (busy && next_f < sg_policy->next_freq) {
297+
next_f = sg_policy->next_freq;
292298

293-
/* Reset cached freq as next_freq has changed */
294-
sg_policy->cached_raw_freq = 0;
295-
}
299+
/* Reset cached freq as next_freq has changed */
300+
sg_policy->cached_raw_freq = 0;
296301
}
302+
297303
sugov_update_commit(sg_policy, time, next_f);
298304
}
299305

@@ -309,6 +315,8 @@ static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
309315
unsigned long j_util, j_max;
310316
s64 delta_ns;
311317

318+
sugov_get_util(j_sg_cpu);
319+
312320
/*
313321
* If the CFS CPU utilization was last updated before the
314322
* previous frequency update and the time elapsed between the
@@ -322,21 +330,15 @@ static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
322330
if (delta_ns > TICK_NSEC) {
323331
j_sg_cpu->iowait_boost = 0;
324332
j_sg_cpu->iowait_boost_pending = false;
325-
j_sg_cpu->util_cfs = 0;
326-
if (j_sg_cpu->util_dl == 0)
327-
continue;
328333
}
329-
if (j_sg_cpu->flags & SCHED_CPUFREQ_RT)
330-
return policy->cpuinfo.max_freq;
331334

332335
j_max = j_sg_cpu->max;
333336
j_util = sugov_aggregate_util(j_sg_cpu);
337+
sugov_iowait_boost(j_sg_cpu, &j_util, &j_max);
334338
if (j_util * max > j_max * util) {
335339
util = j_util;
336340
max = j_max;
337341
}
338-
339-
sugov_iowait_boost(j_sg_cpu, &util, &max);
340342
}
341343

342344
return get_next_freq(sg_policy, util, max);
@@ -351,18 +353,11 @@ sugov_update_shared(struct update_util_data *hook, u64 time, unsigned int flags)
351353

352354
raw_spin_lock(&sg_policy->update_lock);
353355

354-
sugov_get_util(sg_cpu);
355-
sg_cpu->flags = flags;
356-
357-
sugov_set_iowait_boost(sg_cpu, time);
356+
sugov_set_iowait_boost(sg_cpu, time, flags);
358357
sg_cpu->last_update = time;
359358

360359
if (sugov_should_update_freq(sg_policy, time)) {
361-
if (flags & SCHED_CPUFREQ_RT)
362-
next_f = sg_policy->policy->cpuinfo.max_freq;
363-
else
364-
next_f = sugov_next_freq_shared(sg_cpu, time);
365-
360+
next_f = sugov_next_freq_shared(sg_cpu, time);
366361
sugov_update_commit(sg_policy, time, next_f);
367362
}
368363

@@ -673,7 +668,6 @@ static int sugov_start(struct cpufreq_policy *policy)
673668
memset(sg_cpu, 0, sizeof(*sg_cpu));
674669
sg_cpu->cpu = cpu;
675670
sg_cpu->sg_policy = sg_policy;
676-
sg_cpu->flags = 0;
677671
sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
678672
}
679673

‎kernel/sched/rt.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,9 +957,6 @@ static void update_curr_rt(struct rq *rq)
957957
if (unlikely((s64)delta_exec <= 0))
958958
return;
959959

960-
/* Kick cpufreq (see the comment in kernel/sched/sched.h). */
961-
cpufreq_update_util(rq, SCHED_CPUFREQ_RT);
962-
963960
schedstat_set(curr->se.statistics.exec_max,
964961
max(curr->se.statistics.exec_max, delta_exec));
965962

@@ -1001,6 +998,9 @@ dequeue_top_rt_rq(struct rt_rq *rt_rq)
1001998

1002999
sub_nr_running(rq, rt_rq->rt_nr_running);
10031000
rt_rq->rt_queued = 0;
1001+
1002+
/* Kick cpufreq (see the comment in kernel/sched/sched.h). */
1003+
cpufreq_update_util(rq, 0);
10041004
}
10051005

10061006
static void
@@ -1017,6 +1017,9 @@ enqueue_top_rt_rq(struct rt_rq *rt_rq)
10171017

10181018
add_nr_running(rq, rt_rq->rt_nr_running);
10191019
rt_rq->rt_queued = 1;
1020+
1021+
/* Kick cpufreq (see the comment in kernel/sched/sched.h). */
1022+
cpufreq_update_util(rq, 0);
10201023
}
10211024

10221025
#if defined CONFIG_SMP

0 commit comments

Comments
 (0)