Skip to content

Commit

Permalink
powerpc/paravirt: Improve vcpu_is_preempted
Browse files Browse the repository at this point in the history
PowerVM Hypervisor dispatches on a whole core basis. In a shared LPAR, a
CPU from a core that is CEDED or preempted may have a larger latency. In
such a scenario, its preferable to choose a different CPU to run.

If one of the CPUs in the core is active, i.e neither CEDED nor
preempted, then consider this CPU as not preempted.

Also if any of the CPUs in the core has yielded but OS has not requested
CEDE or CONFER, then consider this CPU to be preempted.

Correct detection of preempted CPUs is important for detecting idle
CPUs/cores in task scheduler.

Tested-by: Aboorva Devarajan <[email protected]>
Reviewed-by: Shrikanth Hegde <[email protected]>
Signed-off-by: Srikar Dronamraju <[email protected]>
  • Loading branch information
srikard authored and intel-lab-lkp committed Oct 19, 2023
1 parent eddc90e commit b3303e0
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions arch/powerpc/include/asm/paravirt.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ static inline void yield_to_any(void)
{
plpar_hcall_norets_notrace(H_CONFER, -1, 0);
}

static inline bool is_vcpu_idle(int vcpu)
{
return lppaca_of(vcpu).idle;
}
#else
static inline bool is_shared_processor(void)
{
Expand Down Expand Up @@ -100,6 +105,10 @@ static inline void prod_cpu(int cpu)
___bad_prod_cpu(); /* This would be a bug */
}

static inline bool is_vcpu_idle(int vcpu)
{
return false;
}
#endif

#define vcpu_is_preempted vcpu_is_preempted
Expand All @@ -121,9 +130,23 @@ static inline bool vcpu_is_preempted(int cpu)
if (!is_shared_processor())
return false;

/*
* If the hypervisor has dispatched the target CPU on a physical
* processor, then the target CPU is definitely not preempted.
*/
if (!(yield_count_of(cpu) & 1))
return false;

/*
* If the target CPU has yielded to Hypervisor but OS has not
* requested idle then the target CPU is definitely preempted.
*/
if (!is_vcpu_idle(cpu))
return true;

#ifdef CONFIG_PPC_SPLPAR
if (!is_kvm_guest()) {
int first_cpu;
int first_cpu, i;

/*
* The result of vcpu_is_preempted() is used in a
Expand All @@ -149,11 +172,29 @@ static inline bool vcpu_is_preempted(int cpu)
*/
if (cpu_first_thread_sibling(cpu) == first_cpu)
return false;

/*
* If any of the threads of the target CPU's core are not
* preempted or ceded, then consider target CPU to be
* non-preempted.
*/
first_cpu = cpu_first_thread_sibling(cpu);
for (i = first_cpu; i < first_cpu + threads_per_core; i++) {
if (i == cpu)
continue;
if (!(yield_count_of(i) & 1))
return false;
if (!is_vcpu_idle(i))
return true;
}
}
#endif

if (yield_count_of(cpu) & 1)
return true;
/*
* None of the threads in target CPU's core are running but none of
* them were preempted too. Hence assume the target CPU to be
* non-preempted.
*/
return false;
}

Expand Down

0 comments on commit b3303e0

Please sign in to comment.