Skip to content

Commit bc9ffef

Browse files
author
Peter Zijlstra
committed
sched/core: Simplify core-wide task selection
Tao suggested a two-pass task selection to avoid the retry loop. Not only does it avoid the retry loop, it results in *much* simpler code. This also fixes an issue spotted by Josh Don where, for SMT3+, we can forget to update max on the first pass and get to do an extra round. Suggested-by: Tao Zhou <tao.zhou@linux.dev> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Josh Don <joshdon@google.com> Reviewed-by: Vineeth Pillai (Microsoft) <vineethrp@gmail.com> Link: https://lkml.kernel.org/r/YSS9+k1teA9oPEKl@hirez.programming.kicks-ass.net
1 parent c33627e commit bc9ffef

1 file changed

Lines changed: 45 additions & 111 deletions

File tree

‎kernel/sched/core.c‎

Lines changed: 45 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5580,8 +5580,7 @@ __pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
55805580
return p;
55815581
}
55825582

5583-
/* The idle class should always have a runnable task: */
5584-
BUG();
5583+
BUG(); /* The idle class should always have a runnable task. */
55855584
}
55865585

55875586
#ifdef CONFIG_SCHED_CORE
@@ -5603,66 +5602,31 @@ static inline bool cookie_match(struct task_struct *a, struct task_struct *b)
56035602
return a->core_cookie == b->core_cookie;
56045603
}
56055604

5606-
// XXX fairness/fwd progress conditions
5607-
/*
5608-
* Returns
5609-
* - NULL if there is no runnable task for this class.
5610-
* - the highest priority task for this runqueue if it matches
5611-
* rq->core->core_cookie or its priority is greater than max.
5612-
* - Else returns idle_task.
5613-
*/
5614-
static struct task_struct *
5615-
pick_task(struct rq *rq, const struct sched_class *class, struct task_struct *max, bool in_fi)
5605+
static inline struct task_struct *pick_task(struct rq *rq)
56165606
{
5617-
struct task_struct *class_pick, *cookie_pick;
5618-
unsigned long cookie = rq->core->core_cookie;
5619-
5620-
class_pick = class->pick_task(rq);
5621-
if (!class_pick)
5622-
return NULL;
5623-
5624-
if (!cookie) {
5625-
/*
5626-
* If class_pick is tagged, return it only if it has
5627-
* higher priority than max.
5628-
*/
5629-
if (max && class_pick->core_cookie &&
5630-
prio_less(class_pick, max, in_fi))
5631-
return idle_sched_class.pick_task(rq);
5607+
const struct sched_class *class;
5608+
struct task_struct *p;
56325609

5633-
return class_pick;
5610+
for_each_class(class) {
5611+
p = class->pick_task(rq);
5612+
if (p)
5613+
return p;
56345614
}
56355615

5636-
/*
5637-
* If class_pick is idle or matches cookie, return early.
5638-
*/
5639-
if (cookie_equals(class_pick, cookie))
5640-
return class_pick;
5641-
5642-
cookie_pick = sched_core_find(rq, cookie);
5643-
5644-
/*
5645-
* If class > max && class > cookie, it is the highest priority task on
5646-
* the core (so far) and it must be selected, otherwise we must go with
5647-
* the cookie pick in order to satisfy the constraint.
5648-
*/
5649-
if (prio_less(cookie_pick, class_pick, in_fi) &&
5650-
(!max || prio_less(max, class_pick, in_fi)))
5651-
return class_pick;
5652-
5653-
return cookie_pick;
5616+
BUG(); /* The idle class should always have a runnable task. */
56545617
}
56555618

56565619
extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi);
56575620

56585621
static struct task_struct *
56595622
pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
56605623
{
5661-
struct task_struct *next, *max = NULL;
5662-
const struct sched_class *class;
5624+
struct task_struct *next, *p, *max = NULL;
56635625
const struct cpumask *smt_mask;
56645626
bool fi_before = false;
5665-
int i, j, cpu, occ = 0;
5627+
unsigned long cookie;
5628+
int i, cpu, occ = 0;
5629+
struct rq *rq_i;
56665630
bool need_sync;
56675631

56685632
if (!sched_core_enabled(rq))
@@ -5735,12 +5699,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
57355699
* and there are no cookied tasks running on siblings.
57365700
*/
57375701
if (!need_sync) {
5738-
for_each_class(class) {
5739-
next = class->pick_task(rq);
5740-
if (next)
5741-
break;
5742-
}
5743-
5702+
next = pick_task(rq);
57445703
if (!next->core_cookie) {
57455704
rq->core_pick = NULL;
57465705
/*
@@ -5753,76 +5712,51 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
57535712
}
57545713
}
57555714

5756-
for_each_cpu(i, smt_mask) {
5757-
struct rq *rq_i = cpu_rq(i);
5758-
5759-
rq_i->core_pick = NULL;
5715+
/*
5716+
* For each thread: do the regular task pick and find the max prio task
5717+
* amongst them.
5718+
*
5719+
* Tie-break prio towards the current CPU
5720+
*/
5721+
for_each_cpu_wrap(i, smt_mask, cpu) {
5722+
rq_i = cpu_rq(i);
57605723

57615724
if (i != cpu)
57625725
update_rq_clock(rq_i);
5726+
5727+
p = rq_i->core_pick = pick_task(rq_i);
5728+
if (!max || prio_less(max, p, fi_before))
5729+
max = p;
57635730
}
57645731

5732+
cookie = rq->core->core_cookie = max->core_cookie;
5733+
57655734
/*
5766-
* Try and select tasks for each sibling in descending sched_class
5767-
* order.
5735+
* For each thread: try and find a runnable task that matches @max or
5736+
* force idle.
57685737
*/
5769-
for_each_class(class) {
5770-
again:
5771-
for_each_cpu_wrap(i, smt_mask, cpu) {
5772-
struct rq *rq_i = cpu_rq(i);
5773-
struct task_struct *p;
5774-
5775-
if (rq_i->core_pick)
5776-
continue;
5738+
for_each_cpu(i, smt_mask) {
5739+
rq_i = cpu_rq(i);
5740+
p = rq_i->core_pick;
57775741

5778-
/*
5779-
* If this sibling doesn't yet have a suitable task to
5780-
* run; ask for the most eligible task, given the
5781-
* highest priority task already selected for this
5782-
* core.
5783-
*/
5784-
p = pick_task(rq_i, class, max, fi_before);
5742+
if (!cookie_equals(p, cookie)) {
5743+
p = NULL;
5744+
if (cookie)
5745+
p = sched_core_find(rq_i, cookie);
57855746
if (!p)
5786-
continue;
5747+
p = idle_sched_class.pick_task(rq_i);
5748+
}
57875749

5788-
if (!is_task_rq_idle(p))
5789-
occ++;
5750+
rq_i->core_pick = p;
57905751

5791-
rq_i->core_pick = p;
5792-
if (rq_i->idle == p && rq_i->nr_running) {
5752+
if (p == rq_i->idle) {
5753+
if (rq_i->nr_running) {
57935754
rq->core->core_forceidle = true;
57945755
if (!fi_before)
57955756
rq->core->core_forceidle_seq++;
57965757
}
5797-
5798-
/*
5799-
* If this new candidate is of higher priority than the
5800-
* previous; and they're incompatible; we need to wipe
5801-
* the slate and start over. pick_task makes sure that
5802-
* p's priority is more than max if it doesn't match
5803-
* max's cookie.
5804-
*
5805-
* NOTE: this is a linear max-filter and is thus bounded
5806-
* in execution time.
5807-
*/
5808-
if (!max || !cookie_match(max, p)) {
5809-
struct task_struct *old_max = max;
5810-
5811-
rq->core->core_cookie = p->core_cookie;
5812-
max = p;
5813-
5814-
if (old_max) {
5815-
rq->core->core_forceidle = false;
5816-
for_each_cpu(j, smt_mask) {
5817-
if (j == i)
5818-
continue;
5819-
5820-
cpu_rq(j)->core_pick = NULL;
5821-
}
5822-
occ = 1;
5823-
goto again;
5824-
}
5825-
}
5758+
} else {
5759+
occ++;
58265760
}
58275761
}
58285762

@@ -5842,7 +5776,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
58425776
* non-matching user state.
58435777
*/
58445778
for_each_cpu(i, smt_mask) {
5845-
struct rq *rq_i = cpu_rq(i);
5779+
rq_i = cpu_rq(i);
58465780

58475781
/*
58485782
* An online sibling might have gone offline before a task

0 commit comments

Comments
 (0)