Skip to content

Commit

Permalink
sched/fair: select idle cpu from idle cpumask for task wakeup
Browse files Browse the repository at this point in the history
Add idle cpumask to track idle cpus in sched domain. Every time
a CPU enters idle, the CPU is set in idle cpumask to be a wakeup
target. And if the CPU is not in idle, the CPU is cleared in idle
cpumask during scheduler tick to ratelimit idle cpumask update.

When a task wakes up to select an idle cpu, scanning idle cpumask
has lower cost than scanning all the cpus in last level cache domain,
especially when the system is heavily loaded.

v8->v9:
- rebase on top of tip/sched/core, no functionality change

v7->v8:
- refine update_idle_cpumask, no functionality change
- fix a suspicious RCU usage warning with CONFIG_PROVE_RCU=y

v6->v7:
- place the whole idle cpumask mechanism under CONFIG_SMP

v5->v6:
- decouple idle cpumask update from stop_tick signal, set idle CPU
  in idle cpumask every time the CPU enters idle

v4->v5:
- add update_idle_cpumask for s2idle case
- keep the same ordering of tick_nohz_idle_stop_tick() and update_
  idle_cpumask() everywhere

v3->v4:
- change setting idle cpumask from every idle entry to tickless idle
  if cpu driver is available
- move clearing idle cpumask to scheduler_tick to decouple nohz mode

v2->v3:
- change setting idle cpumask to every idle entry, otherwise schbench
  has a regression of 99th percentile latency
- change clearing idle cpumask to nohz_balancer_kick(), so updating
  idle cpumask is ratelimited in the idle exiting path
- set SCHED_IDLE cpu in idle cpumask to allow it as a wakeup target

v1->v2:
- idle cpumask is updated in the nohz routines, by initializing idle
  cpumask with sched_domain_span(sd), nohz=off case remains the original
  behavior

Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Vincent Guittot <vincent.guittot@linaro.org>
Cc: Qais Yousef <qais.yousef@arm.com>
Cc: Valentin Schneider <valentin.schneider@arm.com>
Cc: Jiang Biao <benbjiang@gmail.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Signed-off-by: Aubrey Li <aubrey.li@linux.intel.com>
  • Loading branch information
Aubrey Li authored and intel-lab-lkp committed Mar 10, 2021
1 parent 4117ceb commit ca52510
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
13 changes: 13 additions & 0 deletions include/linux/sched/topology.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,21 @@ struct sched_domain_shared {
atomic_t ref;
atomic_t nr_busy_cpus;
int has_idle_cores;
/*
* Span of all idle CPUs in this domain.
*
* NOTE: this field is variable length. (Allocated dynamically
* by attaching extra space to the end of the structure,
* depending on how many CPUs the kernel has booted up with)
*/
unsigned long idle_cpus_span[];
};

static inline struct cpumask *sds_idle_cpus(struct sched_domain_shared *sds)
{
return to_cpumask(sds->idle_cpus_span);
}

struct sched_domain {
/* These fields must be setup */
struct sched_domain __rcu *parent; /* top domain must be null terminated */
Expand Down
2 changes: 2 additions & 0 deletions kernel/sched/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -4558,6 +4558,7 @@ void scheduler_tick(void)

#ifdef CONFIG_SMP
rq->idle_balance = idle_cpu(cpu);
update_idle_cpumask(cpu, rq->idle_balance);
trigger_load_balance(rq);
#endif
}
Expand Down Expand Up @@ -8215,6 +8216,7 @@ void __init sched_init(void)
rq->idle_stamp = 0;
rq->avg_idle = 2*sysctl_sched_migration_cost;
rq->max_idle_balance_cost = sysctl_sched_migration_cost;
rq->last_idle_state = 1;

INIT_LIST_HEAD(&rq->cfs_tasks);

Expand Down
45 changes: 44 additions & 1 deletion kernel/sched/fair.c
Original file line number Diff line number Diff line change
Expand Up @@ -6143,7 +6143,12 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int t
if (!this_sd)
return -1;

cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);
/*
* sched_domain_shared is set only at shared cache level,
* this works only because select_idle_cpu is called with
* sd_llc.
*/
cpumask_and(cpus, sds_idle_cpus(sd->shared), p->cpus_ptr);

if (sched_feat(SIS_PROP) && !smt) {
u64 avg_cost, avg_idle, span_avg;
Expand Down Expand Up @@ -6863,6 +6868,44 @@ balance_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)

return newidle_balance(rq, rf) != 0;
}

/*
* Update cpu idle state and record this information
* in sd_llc_shared->idle_cpus_span.
*
* This function is called with interrupts disabled.
*/
void update_idle_cpumask(int cpu, bool idle)
{
struct sched_domain *sd;
struct rq *rq = cpu_rq(cpu);
int idle_state;

/*
* Also set SCHED_IDLE cpu in idle cpumask to
* allow SCHED_IDLE cpu as a wakeup target.
*/
idle_state = idle || sched_idle_cpu(cpu);
/*
* No need to update idle cpumask if the state
* does not change.
*/
if (rq->last_idle_state == idle_state)
return;
/*
* Called with irq disabled, rcu protection is not needed.
*/
sd = per_cpu(sd_llc, cpu);
if (unlikely(!sd))
return;

if (idle_state)
cpumask_set_cpu(cpu, sds_idle_cpus(sd->shared));
else
cpumask_clear_cpu(cpu, sds_idle_cpus(sd->shared));

rq->last_idle_state = idle_state;
}
#endif /* CONFIG_SMP */

static unsigned long wakeup_gran(struct sched_entity *se)
Expand Down
5 changes: 5 additions & 0 deletions kernel/sched/idle.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ static void do_idle(void)
cpuhp_report_idle_dead();
arch_cpu_idle_dead();
}
/*
* The CPU is about to go idle, set it in idle cpumask
* to be a wake up target.
*/
update_idle_cpumask(cpu, true);

arch_cpu_idle_enter();
rcu_nocb_flush_deferred_wakeup();
Expand Down
4 changes: 4 additions & 0 deletions kernel/sched/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ struct rq {

unsigned char nohz_idle_balance;
unsigned char idle_balance;
unsigned char last_idle_state;

unsigned long misfit_task_load;

Expand Down Expand Up @@ -1552,6 +1553,8 @@ static inline unsigned int group_first_cpu(struct sched_group *group)

extern int group_balance_cpu(struct sched_group *sg);

void update_idle_cpumask(int cpu, bool idle);

#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
void register_sched_domain_sysctl(void);
void dirty_sched_domain_sysctl(int cpu);
Expand All @@ -1572,6 +1575,7 @@ extern void flush_smp_call_function_from_idle(void);

#else /* !CONFIG_SMP: */
static inline void flush_smp_call_function_from_idle(void) { }
static inline void update_idle_cpumask(int cpu, bool idle) { }
#endif

#include "stats.h"
Expand Down
3 changes: 2 additions & 1 deletion kernel/sched/topology.c
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,7 @@ sd_init(struct sched_domain_topology_level *tl,
sd->shared = *per_cpu_ptr(sdd->sds, sd_id);
atomic_inc(&sd->shared->ref);
atomic_set(&sd->shared->nr_busy_cpus, sd_weight);
cpumask_copy(sds_idle_cpus(sd->shared), sched_domain_span(sd));
}

sd->private = sdd;
Expand Down Expand Up @@ -1856,7 +1857,7 @@ static int __sdt_alloc(const struct cpumask *cpu_map)

*per_cpu_ptr(sdd->sd, j) = sd;

sds = kzalloc_node(sizeof(struct sched_domain_shared),
sds = kzalloc_node(sizeof(struct sched_domain_shared) + cpumask_size(),
GFP_KERNEL, cpu_to_node(j));
if (!sds)
return -ENOMEM;
Expand Down

0 comments on commit ca52510

Please sign in to comment.