Skip to content

Commit 943d355

Browse files
Rohit Jainingomolnar
authored andcommitted
sched/core: Distinguish between idle_cpu() calls based on desired effect, introduce available_idle_cpu()
In the following commit: 247f2f6 ("sched/core: Don't schedule threads on pre-empted vCPUs") ... we distinguish between idle_cpu() when the vCPU is not running for scheduling threads. However, the idle_cpu() function is used in other places for actually checking whether the state of the CPU is idle or not. Hence split the use of that function based on the desired return value, by introducing the available_idle_cpu() function. This fixes a (slight) regression in that initial vCPU commit, because some code paths (like the load-balancer) don't care and shouldn't care if the vCPU is preempted or not, they just want to know if there's any tasks on the CPU. Signed-off-by: Rohit Jain <rohit.k.jain@oracle.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: dhaval.giani@oracle.com Cc: linux-kernel@vger.kernel.org Cc: matt@codeblueprint.co.uk Cc: steven.sistare@oracle.com Cc: subhra.mazumdar@oracle.com Link: http://lkml.kernel.org/r/1525883988-10356-1-git-send-email-rohit.k.jain@oracle.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent a59a68f commit 943d355

3 files changed

Lines changed: 25 additions & 10 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,7 @@ static inline int task_nice(const struct task_struct *p)
15041504
extern int can_nice(const struct task_struct *p, const int nice);
15051505
extern int task_curr(const struct task_struct *p);
15061506
extern int idle_cpu(int cpu);
1507+
extern int available_idle_cpu(int cpu);
15071508
extern int sched_setscheduler(struct task_struct *, int, const struct sched_param *);
15081509
extern int sched_setscheduler_nocheck(struct task_struct *, int, const struct sched_param *);
15091510
extern int sched_setattr(struct task_struct *, const struct sched_attr *);

‎kernel/sched/core.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4009,6 +4009,20 @@ int idle_cpu(int cpu)
40094009
return 0;
40104010
#endif
40114011

4012+
return 1;
4013+
}
4014+
4015+
/**
4016+
* available_idle_cpu - is a given CPU idle for enqueuing work.
4017+
* @cpu: the CPU in question.
4018+
*
4019+
* Return: 1 if the CPU is currently idle. 0 otherwise.
4020+
*/
4021+
int available_idle_cpu(int cpu)
4022+
{
4023+
if (!idle_cpu(cpu))
4024+
return 0;
4025+
40124026
if (vcpu_is_preempted(cpu))
40134027
return 0;
40144028

‎kernel/sched/fair.c‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5899,8 +5899,8 @@ wake_affine_idle(int this_cpu, int prev_cpu, int sync)
58995899
* a cpufreq perspective, it's better to have higher utilisation
59005900
* on one CPU.
59015901
*/
5902-
if (idle_cpu(this_cpu) && cpus_share_cache(this_cpu, prev_cpu))
5903-
return idle_cpu(prev_cpu) ? prev_cpu : this_cpu;
5902+
if (available_idle_cpu(this_cpu) && cpus_share_cache(this_cpu, prev_cpu))
5903+
return available_idle_cpu(prev_cpu) ? prev_cpu : this_cpu;
59045904

59055905
if (sync && cpu_rq(this_cpu)->nr_running == 1)
59065906
return this_cpu;
@@ -6143,7 +6143,7 @@ find_idlest_group_cpu(struct sched_group *group, struct task_struct *p, int this
61436143

61446144
/* Traverse only the allowed CPUs */
61456145
for_each_cpu_and(i, sched_group_span(group), &p->cpus_allowed) {
6146-
if (idle_cpu(i)) {
6146+
if (available_idle_cpu(i)) {
61476147
struct rq *rq = cpu_rq(i);
61486148
struct cpuidle_state *idle = idle_get_state(rq);
61496149
if (idle && idle->exit_latency < min_exit_latency) {
@@ -6272,7 +6272,7 @@ void __update_idle_core(struct rq *rq)
62726272
if (cpu == core)
62736273
continue;
62746274

6275-
if (!idle_cpu(cpu))
6275+
if (!available_idle_cpu(cpu))
62766276
goto unlock;
62776277
}
62786278

@@ -6304,7 +6304,7 @@ static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int
63046304

63056305
for_each_cpu(cpu, cpu_smt_mask(core)) {
63066306
cpumask_clear_cpu(cpu, cpus);
6307-
if (!idle_cpu(cpu))
6307+
if (!available_idle_cpu(cpu))
63086308
idle = false;
63096309
}
63106310

@@ -6333,7 +6333,7 @@ static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int t
63336333
for_each_cpu(cpu, cpu_smt_mask(target)) {
63346334
if (!cpumask_test_cpu(cpu, &p->cpus_allowed))
63356335
continue;
6336-
if (idle_cpu(cpu))
6336+
if (available_idle_cpu(cpu))
63376337
return cpu;
63386338
}
63396339

@@ -6396,7 +6396,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int t
63966396
return -1;
63976397
if (!cpumask_test_cpu(cpu, &p->cpus_allowed))
63986398
continue;
6399-
if (idle_cpu(cpu))
6399+
if (available_idle_cpu(cpu))
64006400
break;
64016401
}
64026402

@@ -6416,21 +6416,21 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
64166416
struct sched_domain *sd;
64176417
int i, recent_used_cpu;
64186418

6419-
if (idle_cpu(target))
6419+
if (available_idle_cpu(target))
64206420
return target;
64216421

64226422
/*
64236423
* If the previous CPU is cache affine and idle, don't be stupid:
64246424
*/
6425-
if (prev != target && cpus_share_cache(prev, target) && idle_cpu(prev))
6425+
if (prev != target && cpus_share_cache(prev, target) && available_idle_cpu(prev))
64266426
return prev;
64276427

64286428
/* Check a recently used CPU as a potential idle candidate: */
64296429
recent_used_cpu = p->recent_used_cpu;
64306430
if (recent_used_cpu != prev &&
64316431
recent_used_cpu != target &&
64326432
cpus_share_cache(recent_used_cpu, target) &&
6433-
idle_cpu(recent_used_cpu) &&
6433+
available_idle_cpu(recent_used_cpu) &&
64346434
cpumask_test_cpu(p->recent_used_cpu, &p->cpus_allowed)) {
64356435
/*
64366436
* Replace recent_used_cpu with prev as it is a potential

0 commit comments

Comments
 (0)