-
Notifications
You must be signed in to change notification settings - Fork 3
/
0013-KVM-arm64-Support-the-vCPU-preemption-check.patch
228 lines (215 loc) · 6.53 KB
/
0013-KVM-arm64-Support-the-vCPU-preemption-check.patch
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
From 4d0364b5d38be17d2285cfb9fb8e076495cebd80 Mon Sep 17 00:00:00 2001
From: AltArch Kernel <[email protected]>
Date: Wed, 23 Sep 2020 16:47:47 +0800
Subject: [PATCH 13/25] KVM: arm64: Support the vCPU preemption check
euleros inclusion
category: feature
bugzilla: NA
DTS: #231
CVE: NA
--------------------------------
Support the vcpu_is_preempted() functionality under KVM/arm64. This will
enhance lock performance on overcommitted hosts (more runnable vCPUs
than physical CPUs in the system) as doing busy waits for preempted
vCPUs will hurt system performance far worse than early yielding.
unix benchmark result:
host: kernel 4.19.87, HiSilicon Kunpeng920, 8 CPUs
guest: kernel 4.19.87, 16 vCPUs
test-case | after-patch | before-patch
----------------------------------------+-------------------+------------------
Dhrystone 2 using register variables | 338955728.5 lps | 339266319.5 lps
Double-Precision Whetstone | 30634.9 MWIPS | 30884.4 MWIPS
Execl Throughput | 6753.2 lps | 3580.1 lps
File Copy 1024 bufsize 2000 maxblocks | 490048.0 KBps | 313282.3 KBps
File Copy 256 bufsize 500 maxblocks | 129662.5 KBps | 83550.7 KBps
File Copy 4096 bufsize 8000 maxblocks | 1552551.5 KBps | 814327.0 KBps
Pipe Throughput | 8976422.5 lps | 9048628.4 lps
Pipe-based Context Switching | 258641.7 lps | 252925.9 lps
Process Creation | 5312.2 lps | 4507.9 lps
Shell Scripts (1 concurrent) | 8704.2 lpm | 6720.9 lpm
Shell Scripts (8 concurrent) | 1708.8 lpm | 607.2 lpm
System Call Overhead | 3714444.7 lps | 3746386.8 lps
----------------------------------------+-------------------+------------------
System Benchmarks Index Score | 2270.6 | 1679.2
---
arch/arm64/include/asm/paravirt.h | 8 ++-
arch/arm64/kernel/paravirt.c | 112 ++++++++++++++++++++++++++++++++++++++
arch/arm64/kernel/setup.c | 2 +
include/linux/cpuhotplug.h | 1 +
4 files changed, 122 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/include/asm/paravirt.h b/arch/arm64/include/asm/paravirt.h
index ff266c6..62e9ba7 100644
--- a/arch/arm64/include/asm/paravirt.h
+++ b/arch/arm64/include/asm/paravirt.h
@@ -27,12 +27,18 @@ static inline u64 paravirt_steal_clock(int cpu)
return pv_ops.time.steal_clock(cpu);
}
+int __init pv_sched_init(void);
+
__visible bool __native_vcpu_is_preempted(int cpu);
static inline bool pv_vcpu_is_preempted(int cpu)
{
return pv_ops.sched.vcpu_is_preempted(cpu);
}
-#endif
+#else
+
+#define pv_sched_init() do {} while (0)
+
+#endif /* CONFIG_PARAVIRT */
#endif
diff --git a/arch/arm64/kernel/paravirt.c b/arch/arm64/kernel/paravirt.c
index 3a410db..2370908 100644
--- a/arch/arm64/kernel/paravirt.c
+++ b/arch/arm64/kernel/paravirt.c
@@ -13,10 +13,18 @@
* Author: Stefano Stabellini <[email protected]>
*/
+#define pr_fmt(fmt) "arm-pv: " fmt
+
+#include <linux/arm-smccc.h>
+#include <linux/cpuhotplug.h>
#include <linux/export.h>
+#include <linux/io.h>
#include <linux/jump_label.h>
+#include <linux/printk.h>
+#include <linux/psci.h>
#include <linux/types.h>
#include <asm/paravirt.h>
+#include <asm/pvsched-abi.h>
struct static_key paravirt_steal_enabled;
struct static_key paravirt_steal_rq_enabled;
@@ -25,3 +33,107 @@ struct paravirt_patch_template pv_ops = {
.sched.vcpu_is_preempted = __native_vcpu_is_preempted,
};
EXPORT_SYMBOL_GPL(pv_ops);
+
+DEFINE_PER_CPU(struct pvsched_vcpu_state, pvsched_vcpu_region) __aligned(64);
+EXPORT_PER_CPU_SYMBOL(pvsched_vcpu_region);
+
+static bool kvm_vcpu_is_preempted(int cpu)
+{
+ struct pvsched_vcpu_state *reg;
+ u32 preempted;
+
+ reg = &per_cpu(pvsched_vcpu_region, cpu);
+ if (!reg) {
+ pr_warn_once("PV sched enabled but not configured for cpu %d\n",
+ cpu);
+ return false;
+ }
+
+ preempted = le32_to_cpu(READ_ONCE(reg->preempted));
+
+ return !!preempted;
+}
+
+static int pvsched_vcpu_state_dying_cpu(unsigned int cpu)
+{
+ struct pvsched_vcpu_state *reg;
+ struct arm_smccc_res res;
+
+ reg = this_cpu_ptr(&pvsched_vcpu_region);
+ if (!reg)
+ return -EFAULT;
+
+ arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_SCHED_IPA_RELEASE, &res);
+ memset(reg, 0, sizeof(*reg));
+
+ return 0;
+}
+
+static int init_pvsched_vcpu_state(unsigned int cpu)
+{
+ struct pvsched_vcpu_state *reg;
+ struct arm_smccc_res res;
+
+ reg = this_cpu_ptr(&pvsched_vcpu_region);
+ if (!reg)
+ return -EFAULT;
+
+ /* Pass the memory address to host via hypercall */
+ arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_SCHED_IPA_INIT,
+ virt_to_phys(reg), &res);
+
+ return 0;
+}
+
+static int kvm_arm_init_pvsched(void)
+{
+ int ret;
+
+ ret = cpuhp_setup_state(CPUHP_AP_ARM_KVM_PVSCHED_STARTING,
+ "hypervisor/arm/pvsched:starting",
+ init_pvsched_vcpu_state,
+ pvsched_vcpu_state_dying_cpu);
+
+ if (ret < 0) {
+ pr_warn("PV sched init failed\n");
+ return ret;
+ }
+
+ return 0;
+}
+
+static bool has_kvm_pvsched(void)
+{
+ struct arm_smccc_res res;
+
+ /* To detect the presence of PV sched support we require SMCCC 1.1+ */
+ if (psci_ops.smccc_version < SMCCC_VERSION_1_1)
+ return false;
+
+ arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
+ ARM_SMCCC_HV_PV_SCHED_FEATURES, &res);
+
+ return (res.a0 == SMCCC_RET_SUCCESS);
+}
+
+int __init pv_sched_init(void)
+{
+ int ret;
+
+ if (is_hyp_mode_available())
+ return 0;
+
+ if (!has_kvm_pvsched()) {
+ pr_warn("PV sched is not available\n");
+ return 0;
+ }
+
+ ret = kvm_arm_init_pvsched();
+ if (ret)
+ return ret;
+
+ pv_ops.sched.vcpu_is_preempted = kvm_vcpu_is_preempted;
+ pr_info("using PV sched preempted\n");
+
+ return 0;
+}
diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c
index ad285f0..4d8ff2e 100644
--- a/arch/arm64/kernel/setup.c
+++ b/arch/arm64/kernel/setup.c
@@ -306,6 +306,8 @@ void __init setup_arch(char **cmdline_p)
smp_init_cpus();
smp_build_mpidr_hash();
+ pv_sched_init();
+
#ifdef CONFIG_ARM64_SW_TTBR0_PAN
/*
* Make sure init_thread_info.ttbr0 always generates translation
diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h
index dbb2b38..88b66e5 100644
--- a/include/linux/cpuhotplug.h
+++ b/include/linux/cpuhotplug.h
@@ -132,6 +132,7 @@ enum cpuhp_state {
/* Must be the last timer callback */
CPUHP_AP_DUMMY_TIMER_STARTING,
CPUHP_AP_ARM_XEN_STARTING,
+ CPUHP_AP_ARM_KVM_PVSCHED_STARTING,
CPUHP_AP_ARM_CORESIGHT_STARTING,
CPUHP_AP_ARM64_ISNDEP_STARTING,
CPUHP_AP_SMPCFD_DYING,
--
1.8.3.1