summaryrefslogtreecommitdiff
path: root/arch/loongarch/include/asm/qspinlock.h
blob: fbfc6be82f26b78ade2739eefe61e3f4508f760a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _ASM_LOONGARCH_QSPINLOCK_H
#define _ASM_LOONGARCH_QSPINLOCK_H

#include <asm/kvm_para.h>
#include <asm/paravirt.h>

#ifdef CONFIG_PARAVIRT

#define virt_spin_lock virt_spin_lock

static inline bool virt_spin_lock(struct qspinlock *lock)
{
	int val;

	if (!static_branch_unlikely(&virt_spin_lock_key))
		return false;

	/*
	 * On hypervisors without PARAVIRT_SPINLOCKS support we fall
	 * back to a Test-and-Set spinlock, because fair locks have
	 * horrible lock 'holder' preemption issues.
	 */

__retry:
	val = atomic_read(&lock->val);

	if (val || !atomic_try_cmpxchg(&lock->val, &val, _Q_LOCKED_VAL)) {
		cpu_relax();
		goto __retry;
	}

	return true;
}

/*
 * Macro is better than inline function here
 * With macro, parameter cpu is parsed only when it is used.
 * With inline function, parameter cpu is parsed even though it is not used.
 * This may cause cache line thrashing across NUMA nodes.
 */
#define vcpu_is_preempted(cpu)							\
({										\
	bool __val;								\
										\
	if (!static_branch_unlikely(&virt_preempt_key))				\
		__val = false;							\
	else {									\
		struct kvm_steal_time *src;					\
		src = &per_cpu(steal_time, cpu);				\
		__val = !!(READ_ONCE(src->preempted) & KVM_VCPU_PREEMPTED);	\
	}									\
	__val;									\
})

#endif /* CONFIG_PARAVIRT */

#include <asm-generic/qspinlock.h>

#endif // _ASM_LOONGARCH_QSPINLOCK_H