1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _ASM_LOONGARCH_QSPINLOCK_H 3 #define _ASM_LOONGARCH_QSPINLOCK_H 4 5 #include <asm/kvm_para.h> 6 #include <asm/paravirt.h> 7 8 #ifdef CONFIG_PARAVIRT 9 10 #define virt_spin_lock virt_spin_lock 11 12 static inline bool virt_spin_lock(struct qspinlock *lock) 13 { 14 int val; 15 16 if (!static_branch_unlikely(&virt_spin_lock_key)) 17 return false; 18 19 /* 20 * On hypervisors without PARAVIRT_SPINLOCKS support we fall 21 * back to a Test-and-Set spinlock, because fair locks have 22 * horrible lock 'holder' preemption issues. 23 */ 24 25 __retry: 26 val = atomic_read(&lock->val); 27 28 if (val || !atomic_try_cmpxchg(&lock->val, &val, _Q_LOCKED_VAL)) { 29 cpu_relax(); 30 goto __retry; 31 } 32 33 return true; 34 } 35 36 /* 37 * Macro is better than inline function here 38 * With macro, parameter cpu is parsed only when it is used. 39 * With inline function, parameter cpu is parsed even though it is not used. 40 * This may cause cache line thrashing across NUMA nodes. 41 */ 42 #define vcpu_is_preempted(cpu) \ 43 ({ \ 44 bool __val; \ 45 \ 46 if (!static_branch_unlikely(&virt_preempt_key)) \ 47 __val = false; \ 48 else { \ 49 struct kvm_steal_time *src; \ 50 src = &per_cpu(steal_time, cpu); \ 51 __val = !!(READ_ONCE(src->preempted) & KVM_VCPU_PREEMPTED); \ 52 } \ 53 __val; \ 54 }) 55 56 #endif /* CONFIG_PARAVIRT */ 57 58 #include <asm-generic/qspinlock.h> 59 60 #endif // _ASM_LOONGARCH_QSPINLOCK_H 61