@@ -76,6 +76,17 @@ static inline bool is_vcpu_idle(int vcpu)
7676{
7777 return lppaca_of (vcpu ).idle ;
7878}
79+
80+ static inline bool vcpu_is_dispatched (int vcpu )
81+ {
82+ /*
83+ * This is the yield_count. An "odd" value (low bit on) means that
84+ * the processor is yielded (either because of an OS yield or a
85+ * hypervisor preempt). An even value implies that the processor is
86+ * currently executing.
87+ */
88+ return (!(yield_count_of (vcpu ) & 1 ));
89+ }
7990#else
8091static inline bool is_shared_processor (void )
8192{
@@ -109,6 +120,10 @@ static inline bool is_vcpu_idle(int vcpu)
109120{
110121 return false;
111122}
123+ static inline bool vcpu_is_dispatched (int vcpu )
124+ {
125+ return true;
126+ }
112127#endif
113128
114129#define vcpu_is_preempted vcpu_is_preempted
@@ -134,12 +149,12 @@ static inline bool vcpu_is_preempted(int cpu)
134149 * If the hypervisor has dispatched the target CPU on a physical
135150 * processor, then the target CPU is definitely not preempted.
136151 */
137- if (!( yield_count_of ( cpu ) & 1 ))
152+ if (vcpu_is_dispatched ( cpu ))
138153 return false;
139154
140155 /*
141- * If the target CPU has yielded to Hypervisor but OS has not
142- * requested idle then the target CPU is definitely preempted.
156+ * if the target CPU is not dispatched and the guest OS
157+ * has not marked the CPU idle, then it is hypervisor preempted.
143158 */
144159 if (!is_vcpu_idle (cpu ))
145160 return true;
@@ -166,23 +181,25 @@ static inline bool vcpu_is_preempted(int cpu)
166181
167182 /*
168183 * The PowerVM hypervisor dispatches VMs on a whole core
169- * basis. So we know that a thread sibling of the local CPU
184+ * basis. So we know that a thread sibling of the executing CPU
170185 * cannot have been preempted by the hypervisor, even if it
171186 * has called H_CONFER, which will set the yield bit.
172187 */
173188 if (cpu_first_thread_sibling (cpu ) == first_cpu )
174189 return false;
175190
176191 /*
177- * If any of the threads of the target CPU's core are not
178- * preempted or ceded, then consider target CPU to be
179- * non-preempted.
192+ * The specific target CPU was marked by guest OS as idle, but
193+ * then also check all other cpus in the core for PowerVM
194+ * because it does core scheduling and one of the vcpu
195+ * of the core getting preempted by hypervisor implies
196+ * other vcpus can also be considered preempted.
180197 */
181198 first_cpu = cpu_first_thread_sibling (cpu );
182199 for (i = first_cpu ; i < first_cpu + threads_per_core ; i ++ ) {
183200 if (i == cpu )
184201 continue ;
185- if (!( yield_count_of ( i ) & 1 ))
202+ if (vcpu_is_dispatched ( i ))
186203 return false;
187204 if (!is_vcpu_idle (i ))
188205 return true;
0 commit comments