Skip to content

Commit e3589f6

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
sched: Allow for overlapping sched_domain spans
Allow for sched_domain spans that overlap by giving such domains their own sched_group list instead of sharing the sched_groups amongst each-other. This is needed for machines with more than 16 nodes, because sched_domain_node_span() will generate a node mask from the 16 nearest nodes without regard if these masks have any overlap. Currently sched_domains have a sched_group that maps to their child sched_domain span, and since there is no overlap we share the sched_group between the sched_domains of the various CPUs. If however there is overlap, we would need to link the sched_group list in different ways for each cpu, and hence sharing isn't possible. In order to solve this, allocate private sched_groups for each CPU's sched_domain but have the sched_groups share a sched_group_power structure such that we can uniquely track the power. Reported-and-tested-by: Anton Blanchard <anton@samba.org> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Andrew Morton <akpm@linux-foundation.org> Link: http://lkml.kernel.org/n/tip-08bxqw9wis3qti9u5inifh3y@git.kernel.org Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 9c3f75c commit e3589f6

3 files changed

Lines changed: 132 additions & 29 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ enum cpu_idle_type {
844844
#define SD_SERIALIZE 0x0400 /* Only a single load balancing instance */
845845
#define SD_ASYM_PACKING 0x0800 /* Place busy groups earlier in the domain */
846846
#define SD_PREFER_SIBLING 0x1000 /* Prefer to place tasks in a sibling domain */
847+
#define SD_OVERLAP 0x2000 /* sched_domains of this level overlap */
847848

848849
enum powersavings_balance_level {
849850
POWERSAVINGS_BALANCE_NONE = 0, /* No power saving load balance */
@@ -894,6 +895,7 @@ static inline int sd_power_saving_flags(void)
894895
}
895896

896897
struct sched_group_power {
898+
atomic_t ref;
897899
/*
898900
* CPU power of this group, SCHED_LOAD_SCALE being max power for a
899901
* single CPU.

‎kernel/sched.c‎

Lines changed: 128 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6774,10 +6774,36 @@ static struct root_domain *alloc_rootdomain(void)
67746774
return rd;
67756775
}
67766776

6777+
static void free_sched_groups(struct sched_group *sg, int free_sgp)
6778+
{
6779+
struct sched_group *tmp, *first;
6780+
6781+
if (!sg)
6782+
return;
6783+
6784+
first = sg;
6785+
do {
6786+
tmp = sg->next;
6787+
6788+
if (free_sgp && atomic_dec_and_test(&sg->sgp->ref))
6789+
kfree(sg->sgp);
6790+
6791+
kfree(sg);
6792+
sg = tmp;
6793+
} while (sg != first);
6794+
}
6795+
67776796
static void free_sched_domain(struct rcu_head *rcu)
67786797
{
67796798
struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
6780-
if (atomic_dec_and_test(&sd->groups->ref)) {
6799+
6800+
/*
6801+
* If its an overlapping domain it has private groups, iterate and
6802+
* nuke them all.
6803+
*/
6804+
if (sd->flags & SD_OVERLAP) {
6805+
free_sched_groups(sd->groups, 1);
6806+
} else if (atomic_dec_and_test(&sd->groups->ref)) {
67816807
kfree(sd->groups->sgp);
67826808
kfree(sd->groups);
67836809
}
@@ -6967,15 +6993,73 @@ struct sched_domain_topology_level;
69676993
typedef struct sched_domain *(*sched_domain_init_f)(struct sched_domain_topology_level *tl, int cpu);
69686994
typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
69696995

6996+
#define SDTL_OVERLAP 0x01
6997+
69706998
struct sched_domain_topology_level {
69716999
sched_domain_init_f init;
69727000
sched_domain_mask_f mask;
7001+
int flags;
69737002
struct sd_data data;
69747003
};
69757004

6976-
/*
6977-
* Assumes the sched_domain tree is fully constructed
6978-
*/
7005+
static int
7006+
build_overlap_sched_groups(struct sched_domain *sd, int cpu)
7007+
{
7008+
struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
7009+
const struct cpumask *span = sched_domain_span(sd);
7010+
struct cpumask *covered = sched_domains_tmpmask;
7011+
struct sd_data *sdd = sd->private;
7012+
struct sched_domain *child;
7013+
int i;
7014+
7015+
cpumask_clear(covered);
7016+
7017+
for_each_cpu(i, span) {
7018+
struct cpumask *sg_span;
7019+
7020+
if (cpumask_test_cpu(i, covered))
7021+
continue;
7022+
7023+
sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
7024+
GFP_KERNEL, cpu_to_node(i));
7025+
7026+
if (!sg)
7027+
goto fail;
7028+
7029+
sg_span = sched_group_cpus(sg);
7030+
7031+
child = *per_cpu_ptr(sdd->sd, i);
7032+
if (child->child) {
7033+
child = child->child;
7034+
cpumask_copy(sg_span, sched_domain_span(child));
7035+
} else
7036+
cpumask_set_cpu(i, sg_span);
7037+
7038+
cpumask_or(covered, covered, sg_span);
7039+
7040+
sg->sgp = *per_cpu_ptr(sdd->sgp, cpumask_first(sg_span));
7041+
atomic_inc(&sg->sgp->ref);
7042+
7043+
if (cpumask_test_cpu(cpu, sg_span))
7044+
groups = sg;
7045+
7046+
if (!first)
7047+
first = sg;
7048+
if (last)
7049+
last->next = sg;
7050+
last = sg;
7051+
last->next = first;
7052+
}
7053+
sd->groups = groups;
7054+
7055+
return 0;
7056+
7057+
fail:
7058+
free_sched_groups(first, 0);
7059+
7060+
return -ENOMEM;
7061+
}
7062+
69797063
static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
69807064
{
69817065
struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
@@ -6987,30 +7071,34 @@ static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
69877071
if (sg) {
69887072
*sg = *per_cpu_ptr(sdd->sg, cpu);
69897073
(*sg)->sgp = *per_cpu_ptr(sdd->sgp, cpu);
7074+
atomic_set(&(*sg)->sgp->ref, 1); /* for claim_allocations */
69907075
}
69917076

69927077
return cpu;
69937078
}
69947079

69957080
/*
6996-
* build_sched_groups takes the cpumask we wish to span, and a pointer
6997-
* to a function which identifies what group(along with sched group) a CPU
6998-
* belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
6999-
* (due to the fact that we keep track of groups covered with a struct cpumask).
7000-
*
70017081
* build_sched_groups will build a circular linked list of the groups
70027082
* covered by the given span, and will set each group's ->cpumask correctly,
70037083
* and ->cpu_power to 0.
7084+
*
7085+
* Assumes the sched_domain tree is fully constructed
70047086
*/
7005-
static void
7006-
build_sched_groups(struct sched_domain *sd)
7087+
static int
7088+
build_sched_groups(struct sched_domain *sd, int cpu)
70077089
{
70087090
struct sched_group *first = NULL, *last = NULL;
70097091
struct sd_data *sdd = sd->private;
70107092
const struct cpumask *span = sched_domain_span(sd);
70117093
struct cpumask *covered;
70127094
int i;
70137095

7096+
get_group(cpu, sdd, &sd->groups);
7097+
atomic_inc(&sd->groups->ref);
7098+
7099+
if (cpu != cpumask_first(sched_domain_span(sd)))
7100+
return 0;
7101+
70147102
lockdep_assert_held(&sched_domains_mutex);
70157103
covered = sched_domains_tmpmask;
70167104

@@ -7042,6 +7130,8 @@ build_sched_groups(struct sched_domain *sd)
70427130
last = sg;
70437131
}
70447132
last->next = first;
7133+
7134+
return 0;
70457135
}
70467136

70477137
/*
@@ -7056,12 +7146,17 @@ build_sched_groups(struct sched_domain *sd)
70567146
*/
70577147
static void init_sched_groups_power(int cpu, struct sched_domain *sd)
70587148
{
7059-
WARN_ON(!sd || !sd->groups);
7149+
struct sched_group *sg = sd->groups;
70607150

7061-
if (cpu != group_first_cpu(sd->groups))
7062-
return;
7151+
WARN_ON(!sd || !sg);
7152+
7153+
do {
7154+
sg->group_weight = cpumask_weight(sched_group_cpus(sg));
7155+
sg = sg->next;
7156+
} while (sg != sd->groups);
70637157

7064-
sd->groups->group_weight = cpumask_weight(sched_group_cpus(sd->groups));
7158+
if (cpu != group_first_cpu(sg))
7159+
return;
70657160

70667161
update_group_power(sd, cpu);
70677162
}
@@ -7182,16 +7277,15 @@ static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
71827277
static void claim_allocations(int cpu, struct sched_domain *sd)
71837278
{
71847279
struct sd_data *sdd = sd->private;
7185-
struct sched_group *sg = sd->groups;
71867280

71877281
WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
71887282
*per_cpu_ptr(sdd->sd, cpu) = NULL;
71897283

7190-
if (cpu == cpumask_first(sched_group_cpus(sg))) {
7191-
WARN_ON_ONCE(*per_cpu_ptr(sdd->sg, cpu) != sg);
7284+
if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
71927285
*per_cpu_ptr(sdd->sg, cpu) = NULL;
7286+
7287+
if (atomic_read(&(*per_cpu_ptr(sdd->sgp, cpu))->ref))
71937288
*per_cpu_ptr(sdd->sgp, cpu) = NULL;
7194-
}
71957289
}
71967290

71977291
#ifdef CONFIG_SCHED_SMT
@@ -7216,7 +7310,7 @@ static struct sched_domain_topology_level default_topology[] = {
72167310
#endif
72177311
{ sd_init_CPU, cpu_cpu_mask, },
72187312
#ifdef CONFIG_NUMA
7219-
{ sd_init_NODE, cpu_node_mask, },
7313+
{ sd_init_NODE, cpu_node_mask, SDTL_OVERLAP, },
72207314
{ sd_init_ALLNODES, cpu_allnodes_mask, },
72217315
#endif
72227316
{ NULL, },
@@ -7284,7 +7378,9 @@ static void __sdt_free(const struct cpumask *cpu_map)
72847378
struct sd_data *sdd = &tl->data;
72857379

72867380
for_each_cpu(j, cpu_map) {
7287-
kfree(*per_cpu_ptr(sdd->sd, j));
7381+
struct sched_domain *sd = *per_cpu_ptr(sdd->sd, j);
7382+
if (sd && (sd->flags & SD_OVERLAP))
7383+
free_sched_groups(sd->groups, 0);
72887384
kfree(*per_cpu_ptr(sdd->sg, j));
72897385
kfree(*per_cpu_ptr(sdd->sgp, j));
72907386
}
@@ -7336,8 +7432,11 @@ static int build_sched_domains(const struct cpumask *cpu_map,
73367432
struct sched_domain_topology_level *tl;
73377433

73387434
sd = NULL;
7339-
for (tl = sched_domain_topology; tl->init; tl++)
7435+
for (tl = sched_domain_topology; tl->init; tl++) {
73407436
sd = build_sched_domain(tl, &d, cpu_map, attr, sd, i);
7437+
if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
7438+
sd->flags |= SD_OVERLAP;
7439+
}
73417440

73427441
while (sd->child)
73437442
sd = sd->child;
@@ -7349,13 +7448,13 @@ static int build_sched_domains(const struct cpumask *cpu_map,
73497448
for_each_cpu(i, cpu_map) {
73507449
for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
73517450
sd->span_weight = cpumask_weight(sched_domain_span(sd));
7352-
get_group(i, sd->private, &sd->groups);
7353-
atomic_inc(&sd->groups->ref);
7354-
7355-
if (i != cpumask_first(sched_domain_span(sd)))
7356-
continue;
7357-
7358-
build_sched_groups(sd);
7451+
if (sd->flags & SD_OVERLAP) {
7452+
if (build_overlap_sched_groups(sd, i))
7453+
goto error;
7454+
} else {
7455+
if (build_sched_groups(sd, i))
7456+
goto error;
7457+
}
73597458
}
73607459
}
73617460

‎kernel/sched_features.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,5 @@ SCHED_FEAT(NONIRQ_POWER, 1)
7070
* using the scheduler IPI. Reduces rq->lock contention/bounces.
7171
*/
7272
SCHED_FEAT(TTWU_QUEUE, 1)
73+
74+
SCHED_FEAT(FORCE_SD_OVERLAP, 0)

0 commit comments

Comments
 (0)