Skip to content

Commit b95303e

Browse files
Barry SongPeter Zijlstra
authored andcommitted
sched: Add cpus_share_resources API
Add cpus_share_resources() API. This is the preparation for the optimization of select_idle_cpu() on platforms with cluster scheduler level. On a machine with clusters cpus_share_resources() will test whether two cpus are within the same cluster. On a non-cluster machine it will behaves the same as cpus_share_cache(). So we use "resources" here for cache resources. Signed-off-by: Barry Song <[email protected]> Signed-off-by: Yicong Yang <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Gautham R. Shenoy <[email protected]> Reviewed-by: Tim Chen <[email protected]> Reviewed-by: Vincent Guittot <[email protected]> Tested-and-reviewed-by: Chen Yu <[email protected]> Tested-by: K Prateek Nayak <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 5ebde09 commit b95303e

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

include/linux/sched/sd_flags.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ SD_FLAG(SD_ASYM_CPUCAPACITY_FULL, SDF_SHARED_PARENT | SDF_NEEDS_GROUPS)
109109
*/
110110
SD_FLAG(SD_SHARE_CPUCAPACITY, SDF_SHARED_CHILD | SDF_NEEDS_GROUPS)
111111

112+
/*
113+
* Domain members share CPU cluster (LLC tags or L2 cache)
114+
*
115+
* NEEDS_GROUPS: Clusters are shared between groups.
116+
*/
117+
SD_FLAG(SD_CLUSTER, SDF_NEEDS_GROUPS)
118+
112119
/*
113120
* Domain members share CPU package resources (i.e. caches)
114121
*

include/linux/sched/topology.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static inline int cpu_smt_flags(void)
4545
#ifdef CONFIG_SCHED_CLUSTER
4646
static inline int cpu_cluster_flags(void)
4747
{
48-
return SD_SHARE_PKG_RESOURCES;
48+
return SD_CLUSTER | SD_SHARE_PKG_RESOURCES;
4949
}
5050
#endif
5151

@@ -179,6 +179,7 @@ cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
179179
void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
180180

181181
bool cpus_share_cache(int this_cpu, int that_cpu);
182+
bool cpus_share_resources(int this_cpu, int that_cpu);
182183

183184
typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
184185
typedef int (*sched_domain_flags_f)(void);
@@ -232,6 +233,11 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
232233
return true;
233234
}
234235

236+
static inline bool cpus_share_resources(int this_cpu, int that_cpu)
237+
{
238+
return true;
239+
}
240+
235241
#endif /* !CONFIG_SMP */
236242

237243
#if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)

kernel/sched/core.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3939,6 +3939,18 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
39393939
return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
39403940
}
39413941

3942+
/*
3943+
* Whether CPUs are share cache resources, which means LLC on non-cluster
3944+
* machines and LLC tag or L2 on machines with clusters.
3945+
*/
3946+
bool cpus_share_resources(int this_cpu, int that_cpu)
3947+
{
3948+
if (this_cpu == that_cpu)
3949+
return true;
3950+
3951+
return per_cpu(sd_share_id, this_cpu) == per_cpu(sd_share_id, that_cpu);
3952+
}
3953+
39423954
static inline bool ttwu_queue_cond(struct task_struct *p, int cpu)
39433955
{
39443956
/*

kernel/sched/sched.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,7 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
18531853
DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
18541854
DECLARE_PER_CPU(int, sd_llc_size);
18551855
DECLARE_PER_CPU(int, sd_llc_id);
1856+
DECLARE_PER_CPU(int, sd_share_id);
18561857
DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
18571858
DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
18581859
DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);

kernel/sched/topology.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ static void destroy_sched_domains(struct sched_domain *sd)
668668
DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
669669
DEFINE_PER_CPU(int, sd_llc_size);
670670
DEFINE_PER_CPU(int, sd_llc_id);
671+
DEFINE_PER_CPU(int, sd_share_id);
671672
DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
672673
DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
673674
DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
@@ -693,6 +694,17 @@ static void update_top_cache_domain(int cpu)
693694
per_cpu(sd_llc_id, cpu) = id;
694695
rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
695696

697+
sd = lowest_flag_domain(cpu, SD_CLUSTER);
698+
if (sd)
699+
id = cpumask_first(sched_domain_span(sd));
700+
701+
/*
702+
* This assignment should be placed after the sd_llc_id as
703+
* we want this id equals to cluster id on cluster machines
704+
* but equals to LLC id on non-Cluster machines.
705+
*/
706+
per_cpu(sd_share_id, cpu) = id;
707+
696708
sd = lowest_flag_domain(cpu, SD_NUMA);
697709
rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
698710

@@ -1550,6 +1562,7 @@ static struct cpumask ***sched_domains_numa_masks;
15501562
*/
15511563
#define TOPOLOGY_SD_FLAGS \
15521564
(SD_SHARE_CPUCAPACITY | \
1565+
SD_CLUSTER | \
15531566
SD_SHARE_PKG_RESOURCES | \
15541567
SD_NUMA | \
15551568
SD_ASYM_PACKING)

0 commit comments

Comments
 (0)