Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 #ifndef _ASM_POWERPC_PARAVIRT_H
0003 #define _ASM_POWERPC_PARAVIRT_H
0004 
0005 #include <linux/jump_label.h>
0006 #include <asm/smp.h>
0007 #ifdef CONFIG_PPC64
0008 #include <asm/paca.h>
0009 #include <asm/hvcall.h>
0010 #endif
0011 
0012 #ifdef CONFIG_PPC_SPLPAR
0013 #include <linux/smp.h>
0014 #include <asm/kvm_guest.h>
0015 #include <asm/cputhreads.h>
0016 
0017 DECLARE_STATIC_KEY_FALSE(shared_processor);
0018 
0019 static inline bool is_shared_processor(void)
0020 {
0021     return static_branch_unlikely(&shared_processor);
0022 }
0023 
0024 /* If bit 0 is set, the cpu has been ceded, conferred, or preempted */
0025 static inline u32 yield_count_of(int cpu)
0026 {
0027     __be32 yield_count = READ_ONCE(lppaca_of(cpu).yield_count);
0028     return be32_to_cpu(yield_count);
0029 }
0030 
0031 /*
0032  * Spinlock code confers and prods, so don't trace the hcalls because the
0033  * tracing code takes spinlocks which can cause recursion deadlocks.
0034  *
0035  * These calls are made while the lock is not held: the lock slowpath yields if
0036  * it can not acquire the lock, and unlock slow path might prod if a waiter has
0037  * yielded). So this may not be a problem for simple spin locks because the
0038  * tracing does not technically recurse on the lock, but we avoid it anyway.
0039  *
0040  * However the queued spin lock contended path is more strictly ordered: the
0041  * H_CONFER hcall is made after the task has queued itself on the lock, so then
0042  * recursing on that lock will cause the task to then queue up again behind the
0043  * first instance (or worse: queued spinlocks use tricks that assume a context
0044  * never waits on more than one spinlock, so such recursion may cause random
0045  * corruption in the lock code).
0046  */
0047 static inline void yield_to_preempted(int cpu, u32 yield_count)
0048 {
0049     plpar_hcall_norets_notrace(H_CONFER, get_hard_smp_processor_id(cpu), yield_count);
0050 }
0051 
0052 static inline void prod_cpu(int cpu)
0053 {
0054     plpar_hcall_norets_notrace(H_PROD, get_hard_smp_processor_id(cpu));
0055 }
0056 
0057 static inline void yield_to_any(void)
0058 {
0059     plpar_hcall_norets_notrace(H_CONFER, -1, 0);
0060 }
0061 #else
0062 static inline bool is_shared_processor(void)
0063 {
0064     return false;
0065 }
0066 
0067 static inline u32 yield_count_of(int cpu)
0068 {
0069     return 0;
0070 }
0071 
0072 extern void ___bad_yield_to_preempted(void);
0073 static inline void yield_to_preempted(int cpu, u32 yield_count)
0074 {
0075     ___bad_yield_to_preempted(); /* This would be a bug */
0076 }
0077 
0078 extern void ___bad_yield_to_any(void);
0079 static inline void yield_to_any(void)
0080 {
0081     ___bad_yield_to_any(); /* This would be a bug */
0082 }
0083 
0084 extern void ___bad_prod_cpu(void);
0085 static inline void prod_cpu(int cpu)
0086 {
0087     ___bad_prod_cpu(); /* This would be a bug */
0088 }
0089 
0090 #endif
0091 
0092 #define vcpu_is_preempted vcpu_is_preempted
0093 static inline bool vcpu_is_preempted(int cpu)
0094 {
0095     /*
0096      * The dispatch/yield bit alone is an imperfect indicator of
0097      * whether the hypervisor has dispatched @cpu to run on a physical
0098      * processor. When it is clear, @cpu is definitely not preempted.
0099      * But when it is set, it means only that it *might* be, subject to
0100      * other conditions. So we check other properties of the VM and
0101      * @cpu first, resorting to the yield count last.
0102      */
0103 
0104     /*
0105      * Hypervisor preemption isn't possible in dedicated processor
0106      * mode by definition.
0107      */
0108     if (!is_shared_processor())
0109         return false;
0110 
0111 #ifdef CONFIG_PPC_SPLPAR
0112     if (!is_kvm_guest()) {
0113         int first_cpu;
0114 
0115         /*
0116          * The result of vcpu_is_preempted() is used in a
0117          * speculative way, and is always subject to invalidation
0118          * by events internal and external to Linux. While we can
0119          * be called in preemptable context (in the Linux sense),
0120          * we're not accessing per-cpu resources in a way that can
0121          * race destructively with Linux scheduler preemption and
0122          * migration, and callers can tolerate the potential for
0123          * error introduced by sampling the CPU index without
0124          * pinning the task to it. So it is permissible to use
0125          * raw_smp_processor_id() here to defeat the preempt debug
0126          * warnings that can arise from using smp_processor_id()
0127          * in arbitrary contexts.
0128          */
0129         first_cpu = cpu_first_thread_sibling(raw_smp_processor_id());
0130 
0131         /*
0132          * The PowerVM hypervisor dispatches VMs on a whole core
0133          * basis. So we know that a thread sibling of the local CPU
0134          * cannot have been preempted by the hypervisor, even if it
0135          * has called H_CONFER, which will set the yield bit.
0136          */
0137         if (cpu_first_thread_sibling(cpu) == first_cpu)
0138             return false;
0139     }
0140 #endif
0141 
0142     if (yield_count_of(cpu) & 1)
0143         return true;
0144     return false;
0145 }
0146 
0147 static inline bool pv_is_native_spin_unlock(void)
0148 {
0149     return !is_shared_processor();
0150 }
0151 
0152 #endif /* _ASM_POWERPC_PARAVIRT_H */