Skip to contentSkip to conversation

sched/fair: Check idle_cpu in select_idle_core/cpu()

PATCH In review Tao Zhou 3 participants 6 messages Oct 9, 2021 – Oct 10, 2021 Scheduler

lore
Share & tools
lore

Tao Zhou

author
#0 lore raw

PATCH 1/1 sched/fair: Check idle_cpu in select_idle_core/cpu()

In select_idle_core(), the idle core returned may have no cpu allowed. I think the idle core returned for the task is the one that can be allowed to run. I insist on this semantics.

In select_idle_cpu(), if select_idle_core() can not find the idle core, one reason is that the core is not allowed for the task, but the core itself is idle from the point of sds->has_idle_cores. I insist on this semantics.

No others, just two additional check.

---
 kernel/sched/fair.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/fair.c +2 -2
index f6a05d9b5443..a44aca5095d3 100644
@@ -6213,7 +6213,7 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
 			*idle_cpu = cpu;
 	}
 
-	if (idle)
+	if (idle && *idle_cpu != -1)
 		return core;
 
 	cpumask_andnot(cpus, cpus, cpu_smt_mask(core));
@@ -6324,7 +6324,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
 		}
 	}
 
-	if (has_idle_core)
+	if (has_idle_core && *idle_cpu != -1)
 		set_idle_cores(target, false);
 
 	if (sched_feat(SIS_PROP) && !has_idle_core) {
Signature · 1 line
2.32.0
maintainer
#1 lore raw
Quoted 19 lines · Tao Zhou

In select_idle_core(), the idle core returned may have no cpu allowed. I think the idle core returned for the task is the one that can be allowed to run. I insist on this semantics.

In select_idle_cpu(), if select_idle_core() can not find the idle core, one reason is that the core is not allowed for the task, but the core itself is idle from the point of sds->has_idle_cores. I insist on this semantics.

No others, just two additional check.

---
 kernel/sched/fair.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/fair.c +0 -0
index f6a05d9b5443..a44aca5095d3 100644

kernel/sched/fair.c static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu · line 6217

Quoted 6 lines
kernel/sched/fair.c +1 -1
@@ -6213,7 +6213,7 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
 			*idle_cpu = cpu;
 	}
 
-	if (idle)
+	if (idle && *idle_cpu != -1)
 		return core;

In that case, core would be nr_cpu_ids (==nr_cpumask_bits), and then the caller checks:

(unsigned)i < nr_cpumask_bits

kernel/sched/fair.c static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool · line 6328

Quoted 8 lines
kernel/sched/fair.c +1 -1
 	cpumask_andnot(cpus, cpus, cpu_smt_mask(core));
@@ -6324,7 +6324,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
 		}
 	}
 
-	if (has_idle_core)
+	if (has_idle_core && *idle_cpu != -1)
 		set_idle_cores(target, false);

And this one I'm completely failing, why shouldn't we mark the core as non-idle when there is a single idle CPU found? That's just worng.

author
#2 lore raw

Hi Peter,

Quoted 23 lines · Peter Zijlstra
Quoted 22 lines · Tao Zhou

In select_idle_core(), the idle core returned may have no cpu allowed. I think the idle core returned for the task is the one that can be allowed to run. I insist on this semantics.

In select_idle_cpu(), if select_idle_core() can not find the idle core, one reason is that the core is not allowed for the task, but the core itself is idle from the point of sds->has_idle_cores. I insist on this semantics.

No others, just two additional check.

---
 kernel/sched/fair.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/fair.c +0 -0
index f6a05d9b5443..a44aca5095d3 100644
@@ -6213,7 +6213,7 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
 			*idle_cpu = cpu;
 	}
 
Quoted 7 lines

kernel/sched/fair.c static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu · line 6217

Quoted 3 lines
kernel/sched/fair.c +1 -1
-	if (idle)
+	if (idle && *idle_cpu != -1)
 		return core;

In that case, core would be nr_cpu_ids (==nr_cpumask_bits), and then the caller checks:

(unsigned)i < nr_cpumask_bits

Thank you for reply.

If (1)there is no idle core or (2)the idle core has no allowed cpu, we return -1. Originally, just (1) has happened, we return -1. The (2) is what I want to add.

If we find idle core and has allowed cpu in the core, is it better to return @*idle_cpu.

    if (idle && *idle_cpu != -1)

return *idle_cpu;

This @*idle_cpu is the allowed cpu in the idle core. We do not promise anything about the @core(target) is the allowed cpu until we hit in select_task_rq() --> select_fallback_rq(). And the select_fallback_rq() will return a different cpu than the @core or @*idle_cpu.

Quoted 11 lines

kernel/sched/fair.c static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool · line 6328

Quoted 8 lines
kernel/sched/fair.c +1 -1
 	cpumask_andnot(cpus, cpus, cpu_smt_mask(core));
@@ -6324,7 +6324,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
 		}
 	}
 
-	if (has_idle_core)
+	if (has_idle_core && *idle_cpu != -1)
 		set_idle_cores(target, false);

And this one I'm completely failing, why shouldn't we mark the core as non-idle when there is a single idle CPU found? That's just worng.

When @has_idle_core is true, it implies for all cpu in the core the case
(1) or case (2) has happened. The (1) can be mark as non-idle. I conclude to contradiction myself last time. The (2) is also seemed to be non-idle.

But, I think I am totally wrong because the sds->has_idle_cores is related to the cpu not task. So, the affinity should not affect the decision of sds->has_idle_cores.

Thanks,
Tao

#3 lore raw
Quoted 33 lines · Tao Zhou

Hi Peter,

Quoted 29 lines · Peter Zijlstra
Quoted 19 lines · Tao Zhou

In select_idle_core(), the idle core returned may have no cpu allowed. I think the idle core returned for the task is the one that can be allowed to run. I insist on this semantics.

In select_idle_cpu(), if select_idle_core() can not find the idle core, one reason is that the core is not allowed for the task, but the core itself is idle from the point of sds->has_idle_cores. I insist on this semantics.

No others, just two additional check.

---
 kernel/sched/fair.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/fair.c +0 -0
index f6a05d9b5443..a44aca5095d3 100644

kernel/sched/fair.c static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu · line 6217

Quoted 6 lines
kernel/sched/fair.c +1 -1
@@ -6213,7 +6213,7 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
                    *idle_cpu = cpu;
    }
 
-   if (idle)
+   if (idle && *idle_cpu != -1)
            return core;

In that case, core would be nr_cpu_ids (==nr_cpumask_bits), and then the caller checks:

Quoted 7 lines
Quoted 1 line

(unsigned)i < nr_cpumask_bits

Thank you for reply.

If (1)there is no idle core or (2)the idle core has no allowed cpu, we return -1. Originally, just (1) has happened, we return -1. The (2) is what I want to add.

I don't understand (2). before doing

        for_each_cpu_wrap(cpu, cpus, target + 1) {
                if (has_idle_core) {
                        i = select_idle_core(p, cpu, cpus, &idle_cpu);
                        if ((unsigned int)i < nr_cpumask_bits)
                                return i;

                } else {
                        if (!--nr)
                                return -1;
                        idle_cpu = __select_idle_cpu(cpu, p);
                        if ((unsigned int)idle_cpu < nr_cpumask_bits)
                                break;
                }
        }

to select idle core, we have already done:

    cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);

so we are only scanning allowed cpus.

Quoted 29 lines

If we find idle core and has allowed cpu in the core, is it better to return @*idle_cpu.

    if (idle && *idle_cpu != -1)
            return *idle_cpu;

This @*idle_cpu is the allowed cpu in the idle core. We do not promise anything about the @core(target) is the allowed cpu until we hit in select_task_rq() --> select_fallback_rq(). And the select_fallback_rq() will return a different cpu than the @core or @*idle_cpu.

Quoted 11 lines

kernel/sched/fair.c static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool · line 6328

Quoted 8 lines
kernel/sched/fair.c +1 -1
    cpumask_andnot(cpus, cpus, cpu_smt_mask(core));
@@ -6324,7 +6324,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
            }
    }
 
-   if (has_idle_core)
+   if (has_idle_core && *idle_cpu != -1)
            set_idle_cores(target, false);

And this one I'm completely failing, why shouldn't we mark the core as non-idle when there is a single idle CPU found? That's just worng.

When @has_idle_core is true, it implies for all cpu in the core the case
(1) or case (2) has happened. The (1) can be mark as non-idle. I conclude to contradiction myself last time. The (2) is also seemed to be non-idle.

Quoted 8 lines

But, I think I am totally wrong because the sds->has_idle_cores is related to the cpu not task. So, the affinity should not affect the decision of sds->has_idle_cores.

Thanks,
Tao

Thanks
barry

author
#4 lore raw

Hi Barry,

Quoted 58 lines · Barry Song
Quoted 33 lines · Tao Zhou

Hi Peter,

Quoted 29 lines · Peter Zijlstra
Quoted 19 lines · Tao Zhou

In select_idle_core(), the idle core returned may have no cpu allowed. I think the idle core returned for the task is the one that can be allowed to run. I insist on this semantics.

In select_idle_cpu(), if select_idle_core() can not find the idle core, one reason is that the core is not allowed for the task, but the core itself is idle from the point of sds->has_idle_cores. I insist on this semantics.

No others, just two additional check.

---
 kernel/sched/fair.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/fair.c +0 -0
index f6a05d9b5443..a44aca5095d3 100644

kernel/sched/fair.c static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu · line 6217

Quoted 6 lines
kernel/sched/fair.c +1 -1
@@ -6213,7 +6213,7 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
                    *idle_cpu = cpu;
    }
 
-   if (idle)
+   if (idle && *idle_cpu != -1)
            return core;

In that case, core would be nr_cpu_ids (==nr_cpumask_bits), and then the caller checks:

Quoted 7 lines
Quoted 1 line

(unsigned)i < nr_cpumask_bits

Thank you for reply.

If (1)there is no idle core or (2)the idle core has no allowed cpu, we return -1. Originally, just (1) has happened, we return -1. The (2) is what I want to add.

I don't understand (2). before doing

        for_each_cpu_wrap(cpu, cpus, target + 1) {
                if (has_idle_core) {
                        i = select_idle_core(p, cpu, cpus, &idle_cpu);
                        if ((unsigned int)i < nr_cpumask_bits)
                                return i;

                } else {
                        if (!--nr)
                                return -1;
                        idle_cpu = __select_idle_cpu(cpu, p);
                        if ((unsigned int)idle_cpu < nr_cpumask_bits)
                                break;
                }
        }
Quoted 4 lines

to select idle core, we have already done:

    cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);

so we are only scanning allowed cpus.

Um.. You read top down.. and you are right.
The function itself semantics is important to me.

After a secondary recall and not thorough now, I realize that cpus_ptr may be changed.

See code of this:

static void migrate_disable_switch(struct rq *rq, struct task_struct *p)

{
	if (likely(!p->migration_disabled))
		return;

	if (p->cpus_ptr != &p->cpus_mask)
		return;

	/*
	 * Violates locking rules! see comment in __do_set_cpus_allowed().
	 */
	__do_set_cpus_allowed(p, cpumask_of(rq->cpu), SCA_MIGRATE_DISABLE);
}

This change is under the light of ->pi_lock.
That thing is quick to forget to me..
Not sure I am right. Thank you for remind.

If the cpu_ptr can be changed, you can not depend on the first AND operation there.

Quoted 35 lines
Quoted 35 lines

If we find idle core and has allowed cpu in the core, is it better to return @*idle_cpu.

    if (idle && *idle_cpu != -1)
            return *idle_cpu;

This @*idle_cpu is the allowed cpu in the idle core. We do not promise anything about the @core(target) is the allowed cpu until we hit in select_task_rq() --> select_fallback_rq(). And the select_fallback_rq() will return a different cpu than the @core or @*idle_cpu.

Quoted 11 lines

kernel/sched/fair.c static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool · line 6328

Quoted 8 lines
kernel/sched/fair.c +1 -1
    cpumask_andnot(cpus, cpus, cpu_smt_mask(core));
@@ -6324,7 +6324,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
            }
    }
 
-   if (has_idle_core)
+   if (has_idle_core && *idle_cpu != -1)
            set_idle_cores(target, false);

And this one I'm completely failing, why shouldn't we mark the core as non-idle when there is a single idle CPU found? That's just worng.

When @has_idle_core is true, it implies for all cpu in the core the case
(1) or case (2) has happened. The (1) can be mark as non-idle. I conclude to contradiction myself last time. The (2) is also seemed to be non-idle.

But, I think I am totally wrong because the sds->has_idle_cores is related to the cpu not task. So, the affinity should not affect the decision of sds->has_idle_cores.

Quoted 5 lines
Quoted 2 lines

Thanks,
Tao

Thanks
barry

Thanks,
Tao

#5 lore raw
Quoted 91 lines · Tao Zhou

Hi Barry,

Quoted 58 lines · Barry Song
Quoted 33 lines · Tao Zhou

Hi Peter,

Quoted 29 lines · Peter Zijlstra
Quoted 19 lines · Tao Zhou

In select_idle_core(), the idle core returned may have no cpu allowed. I think the idle core returned for the task is the one that can be allowed to run. I insist on this semantics.

In select_idle_cpu(), if select_idle_core() can not find the idle core, one reason is that the core is not allowed for the task, but the core itself is idle from the point of sds->has_idle_cores. I insist on this semantics.

No others, just two additional check.

---
 kernel/sched/fair.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/fair.c +0 -0
index f6a05d9b5443..a44aca5095d3 100644

kernel/sched/fair.c static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu · line 6217

Quoted 6 lines
kernel/sched/fair.c +1 -1
@@ -6213,7 +6213,7 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
                    *idle_cpu = cpu;
    }
 
-   if (idle)
+   if (idle && *idle_cpu != -1)
            return core;

In that case, core would be nr_cpu_ids (==nr_cpumask_bits), and then the caller checks:

Quoted 7 lines
Quoted 1 line

(unsigned)i < nr_cpumask_bits

Thank you for reply.

If (1)there is no idle core or (2)the idle core has no allowed cpu, we return -1. Originally, just (1) has happened, we return -1. The (2) is what I want to add.

I don't understand (2). before doing

        for_each_cpu_wrap(cpu, cpus, target + 1) {
                if (has_idle_core) {
                        i = select_idle_core(p, cpu, cpus, &idle_cpu);
                        if ((unsigned int)i < nr_cpumask_bits)
                                return i;

                } else {
                        if (!--nr)
                                return -1;
                        idle_cpu = __select_idle_cpu(cpu, p);
                        if ((unsigned int)idle_cpu < nr_cpumask_bits)
                                break;
                }
        }
Quoted 4 lines

to select idle core, we have already done:

    cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);

so we are only scanning allowed cpus.

Um.. You read top down.. and you are right.
The function itself semantics is important to me.

After a secondary recall and not thorough now, I realize that cpus_ptr may be changed.

See code of this:

static void migrate_disable_switch(struct rq *rq, struct task_struct *p)

{
        if (likely(!p->migration_disabled))
                return;

        if (p->cpus_ptr != &p->cpus_mask)
                return;

        /*
         * Violates locking rules! see comment in __do_set_cpus_allowed().
         */
        __do_set_cpus_allowed(p, cpumask_of(rq->cpu), SCA_MIGRATE_DISABLE);
}
Quoted 6 lines

This change is under the light of ->pi_lock.
That thing is quick to forget to me..
Not sure I am right. Thank you for remind.

If the cpu_ptr can be changed, you can not depend on the first AND operation there.

The explanation doesn't make any sense to me. We are scanning based on the first AND operation. select_idle_core() is returning *idle_cpu based on the cpumask after AND operation.
Even though cpumask can change after select_idle_core() is done or before select_idle_core() is called, the return value is not wrong.

Quoted 46 lines
Quoted 36 lines
Quoted 36 lines

If we find idle core and has allowed cpu in the core, is it better to return @*idle_cpu.

    if (idle && *idle_cpu != -1)
            return *idle_cpu;

This @*idle_cpu is the allowed cpu in the idle core. We do not promise anything about the @core(target) is the allowed cpu until we hit in select_task_rq() --> select_fallback_rq(). And the select_fallback_rq() will return a different cpu than the @core or @*idle_cpu.

Quoted 11 lines

kernel/sched/fair.c static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool · line 6328

Quoted 8 lines
kernel/sched/fair.c +1 -1
    cpumask_andnot(cpus, cpus, cpu_smt_mask(core));
@@ -6324,7 +6324,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
            }
    }
 
-   if (has_idle_core)
+   if (has_idle_core && *idle_cpu != -1)
            set_idle_cores(target, false);

And this one I'm completely failing, why shouldn't we mark the core as non-idle when there is a single idle CPU found? That's just worng.

When @has_idle_core is true, it implies for all cpu in the core the case
(1) or case (2) has happened. The (1) can be mark as non-idle. I conclude to contradiction myself last time. The (2) is also seemed to be non-idle.

But, I think I am totally wrong because the sds->has_idle_cores is related to the cpu not task. So, the affinity should not affect the decision of sds->has_idle_cores.

Quoted 5 lines
Quoted 2 lines

Thanks,
Tao

Thanks
barry

Thanks,
Tao