Gentoo Archives: gentoo-commits

From: Mike Pagano <mpagano@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/linux-patches:4.14 commit in: /
Date: Fri, 14 Feb 2020 23:46:40
Message-Id: 1581723981.cffcf9c9e8041b8bea7d321a15bc16d18583e312.mpagano@gentoo
1 commit: cffcf9c9e8041b8bea7d321a15bc16d18583e312
2 Author: Mike Pagano <mpagano <AT> gentoo <DOT> org>
3 AuthorDate: Fri Feb 14 23:46:21 2020 +0000
4 Commit: Mike Pagano <mpagano <AT> gentoo <DOT> org>
5 CommitDate: Fri Feb 14 23:46:21 2020 +0000
6 URL: https://gitweb.gentoo.org/proj/linux-patches.git/commit/?id=cffcf9c9
7
8 Linux patch 4.14.171
9
10 Signed-off-by: Mike Pagano <mpagano <AT> gentoo.org>
11
12 0000_README | 4 +
13 1170_linux-4.14.171.patch | 14184 ++++++++++++++++++++++++++++++++++++++++++++
14 2 files changed, 14188 insertions(+)
15
16 diff --git a/0000_README b/0000_README
17 index d6b7cdc..9b3f4c4 100644
18 --- a/0000_README
19 +++ b/0000_README
20 @@ -723,6 +723,10 @@ Patch: 1169_linux-4.14.170.patch
21 From: https://www.kernel.org
22 Desc: Linux 4.14.170
23
24 +Patch: 1170_linux-4.14.171.patch
25 +From: https://www.kernel.org
26 +Desc: Linux 4.14.171
27 +
28 Patch: 1500_XATTR_USER_PREFIX.patch
29 From: https://bugs.gentoo.org/show_bug.cgi?id=470644
30 Desc: Support for namespace user.pax.* on tmpfs.
31
32 diff --git a/1170_linux-4.14.171.patch b/1170_linux-4.14.171.patch
33 new file mode 100644
34 index 0000000..ffeec63
35 --- /dev/null
36 +++ b/1170_linux-4.14.171.patch
37 @@ -0,0 +1,14184 @@
38 +diff --git a/Makefile b/Makefile
39 +index b614291199f8..f2657f4838db 100644
40 +--- a/Makefile
41 ++++ b/Makefile
42 +@@ -1,7 +1,7 @@
43 + # SPDX-License-Identifier: GPL-2.0
44 + VERSION = 4
45 + PATCHLEVEL = 14
46 +-SUBLEVEL = 170
47 ++SUBLEVEL = 171
48 + EXTRAVERSION =
49 + NAME = Petit Gorille
50 +
51 +diff --git a/arch/arc/boot/dts/axs10x_mb.dtsi b/arch/arc/boot/dts/axs10x_mb.dtsi
52 +index e114000a84f5..d825b9dbae5d 100644
53 +--- a/arch/arc/boot/dts/axs10x_mb.dtsi
54 ++++ b/arch/arc/boot/dts/axs10x_mb.dtsi
55 +@@ -70,6 +70,7 @@
56 + interrupt-names = "macirq";
57 + phy-mode = "rgmii";
58 + snps,pbl = < 32 >;
59 ++ snps,multicast-filter-bins = <256>;
60 + clocks = <&apbclk>;
61 + clock-names = "stmmaceth";
62 + max-speed = <100>;
63 +diff --git a/arch/arm/boot/dts/sama5d3.dtsi b/arch/arm/boot/dts/sama5d3.dtsi
64 +index 554d0bdedc7a..f96b41ed5b96 100644
65 +--- a/arch/arm/boot/dts/sama5d3.dtsi
66 ++++ b/arch/arm/boot/dts/sama5d3.dtsi
67 +@@ -1185,49 +1185,49 @@
68 + usart0_clk: usart0_clk {
69 + #clock-cells = <0>;
70 + reg = <12>;
71 +- atmel,clk-output-range = <0 66000000>;
72 ++ atmel,clk-output-range = <0 83000000>;
73 + };
74 +
75 + usart1_clk: usart1_clk {
76 + #clock-cells = <0>;
77 + reg = <13>;
78 +- atmel,clk-output-range = <0 66000000>;
79 ++ atmel,clk-output-range = <0 83000000>;
80 + };
81 +
82 + usart2_clk: usart2_clk {
83 + #clock-cells = <0>;
84 + reg = <14>;
85 +- atmel,clk-output-range = <0 66000000>;
86 ++ atmel,clk-output-range = <0 83000000>;
87 + };
88 +
89 + usart3_clk: usart3_clk {
90 + #clock-cells = <0>;
91 + reg = <15>;
92 +- atmel,clk-output-range = <0 66000000>;
93 ++ atmel,clk-output-range = <0 83000000>;
94 + };
95 +
96 + uart0_clk: uart0_clk {
97 + #clock-cells = <0>;
98 + reg = <16>;
99 +- atmel,clk-output-range = <0 66000000>;
100 ++ atmel,clk-output-range = <0 83000000>;
101 + };
102 +
103 + twi0_clk: twi0_clk {
104 + reg = <18>;
105 + #clock-cells = <0>;
106 +- atmel,clk-output-range = <0 16625000>;
107 ++ atmel,clk-output-range = <0 41500000>;
108 + };
109 +
110 + twi1_clk: twi1_clk {
111 + #clock-cells = <0>;
112 + reg = <19>;
113 +- atmel,clk-output-range = <0 16625000>;
114 ++ atmel,clk-output-range = <0 41500000>;
115 + };
116 +
117 + twi2_clk: twi2_clk {
118 + #clock-cells = <0>;
119 + reg = <20>;
120 +- atmel,clk-output-range = <0 16625000>;
121 ++ atmel,clk-output-range = <0 41500000>;
122 + };
123 +
124 + mci0_clk: mci0_clk {
125 +@@ -1243,19 +1243,19 @@
126 + spi0_clk: spi0_clk {
127 + #clock-cells = <0>;
128 + reg = <24>;
129 +- atmel,clk-output-range = <0 133000000>;
130 ++ atmel,clk-output-range = <0 166000000>;
131 + };
132 +
133 + spi1_clk: spi1_clk {
134 + #clock-cells = <0>;
135 + reg = <25>;
136 +- atmel,clk-output-range = <0 133000000>;
137 ++ atmel,clk-output-range = <0 166000000>;
138 + };
139 +
140 + tcb0_clk: tcb0_clk {
141 + #clock-cells = <0>;
142 + reg = <26>;
143 +- atmel,clk-output-range = <0 133000000>;
144 ++ atmel,clk-output-range = <0 166000000>;
145 + };
146 +
147 + pwm_clk: pwm_clk {
148 +@@ -1266,7 +1266,7 @@
149 + adc_clk: adc_clk {
150 + #clock-cells = <0>;
151 + reg = <29>;
152 +- atmel,clk-output-range = <0 66000000>;
153 ++ atmel,clk-output-range = <0 83000000>;
154 + };
155 +
156 + dma0_clk: dma0_clk {
157 +@@ -1297,13 +1297,13 @@
158 + ssc0_clk: ssc0_clk {
159 + #clock-cells = <0>;
160 + reg = <38>;
161 +- atmel,clk-output-range = <0 66000000>;
162 ++ atmel,clk-output-range = <0 83000000>;
163 + };
164 +
165 + ssc1_clk: ssc1_clk {
166 + #clock-cells = <0>;
167 + reg = <39>;
168 +- atmel,clk-output-range = <0 66000000>;
169 ++ atmel,clk-output-range = <0 83000000>;
170 + };
171 +
172 + sha_clk: sha_clk {
173 +diff --git a/arch/arm/boot/dts/sama5d3_can.dtsi b/arch/arm/boot/dts/sama5d3_can.dtsi
174 +index c5a3772741bf..0fac79f75c06 100644
175 +--- a/arch/arm/boot/dts/sama5d3_can.dtsi
176 ++++ b/arch/arm/boot/dts/sama5d3_can.dtsi
177 +@@ -37,13 +37,13 @@
178 + can0_clk: can0_clk {
179 + #clock-cells = <0>;
180 + reg = <40>;
181 +- atmel,clk-output-range = <0 66000000>;
182 ++ atmel,clk-output-range = <0 83000000>;
183 + };
184 +
185 + can1_clk: can1_clk {
186 + #clock-cells = <0>;
187 + reg = <41>;
188 +- atmel,clk-output-range = <0 66000000>;
189 ++ atmel,clk-output-range = <0 83000000>;
190 + };
191 + };
192 + };
193 +diff --git a/arch/arm/boot/dts/sama5d3_tcb1.dtsi b/arch/arm/boot/dts/sama5d3_tcb1.dtsi
194 +index 801f9745e82f..b80dbc45a3c2 100644
195 +--- a/arch/arm/boot/dts/sama5d3_tcb1.dtsi
196 ++++ b/arch/arm/boot/dts/sama5d3_tcb1.dtsi
197 +@@ -23,6 +23,7 @@
198 + tcb1_clk: tcb1_clk {
199 + #clock-cells = <0>;
200 + reg = <27>;
201 ++ atmel,clk-output-range = <0 166000000>;
202 + };
203 + };
204 + };
205 +diff --git a/arch/arm/boot/dts/sama5d3_uart.dtsi b/arch/arm/boot/dts/sama5d3_uart.dtsi
206 +index 186377d41c91..48e23d18e5e3 100644
207 +--- a/arch/arm/boot/dts/sama5d3_uart.dtsi
208 ++++ b/arch/arm/boot/dts/sama5d3_uart.dtsi
209 +@@ -42,13 +42,13 @@
210 + uart0_clk: uart0_clk {
211 + #clock-cells = <0>;
212 + reg = <16>;
213 +- atmel,clk-output-range = <0 66000000>;
214 ++ atmel,clk-output-range = <0 83000000>;
215 + };
216 +
217 + uart1_clk: uart1_clk {
218 + #clock-cells = <0>;
219 + reg = <17>;
220 +- atmel,clk-output-range = <0 66000000>;
221 ++ atmel,clk-output-range = <0 83000000>;
222 + };
223 + };
224 + };
225 +diff --git a/arch/arm/include/asm/kvm_emulate.h b/arch/arm/include/asm/kvm_emulate.h
226 +index 98089ffd91bb..078dbd25cca4 100644
227 +--- a/arch/arm/include/asm/kvm_emulate.h
228 ++++ b/arch/arm/include/asm/kvm_emulate.h
229 +@@ -144,6 +144,11 @@ static inline bool kvm_vcpu_dabt_issext(struct kvm_vcpu *vcpu)
230 + return kvm_vcpu_get_hsr(vcpu) & HSR_SSE;
231 + }
232 +
233 ++static inline bool kvm_vcpu_dabt_issf(const struct kvm_vcpu *vcpu)
234 ++{
235 ++ return false;
236 ++}
237 ++
238 + static inline int kvm_vcpu_dabt_get_rd(struct kvm_vcpu *vcpu)
239 + {
240 + return (kvm_vcpu_get_hsr(vcpu) & HSR_SRT_MASK) >> HSR_SRT_SHIFT;
241 +diff --git a/arch/arm/include/asm/kvm_mmio.h b/arch/arm/include/asm/kvm_mmio.h
242 +index f3a7de71f515..848339d76f9a 100644
243 +--- a/arch/arm/include/asm/kvm_mmio.h
244 ++++ b/arch/arm/include/asm/kvm_mmio.h
245 +@@ -26,6 +26,8 @@
246 + struct kvm_decode {
247 + unsigned long rt;
248 + bool sign_extend;
249 ++ /* Not used on 32-bit arm */
250 ++ bool sixty_four;
251 + };
252 +
253 + void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data);
254 +diff --git a/arch/arm/mach-tegra/sleep-tegra30.S b/arch/arm/mach-tegra/sleep-tegra30.S
255 +index dd4a67dabd91..b7cd41461e7d 100644
256 +--- a/arch/arm/mach-tegra/sleep-tegra30.S
257 ++++ b/arch/arm/mach-tegra/sleep-tegra30.S
258 +@@ -382,6 +382,14 @@ _pll_m_c_x_done:
259 + pll_locked r1, r0, CLK_RESET_PLLC_BASE
260 + pll_locked r1, r0, CLK_RESET_PLLX_BASE
261 +
262 ++ tegra_get_soc_id TEGRA_APB_MISC_BASE, r1
263 ++ cmp r1, #TEGRA30
264 ++ beq 1f
265 ++ ldr r1, [r0, #CLK_RESET_PLLP_BASE]
266 ++ bic r1, r1, #(1<<31) @ disable PllP bypass
267 ++ str r1, [r0, #CLK_RESET_PLLP_BASE]
268 ++1:
269 ++
270 + mov32 r7, TEGRA_TMRUS_BASE
271 + ldr r1, [r7]
272 + add r1, r1, #LOCK_DELAY
273 +@@ -641,7 +649,10 @@ tegra30_switch_cpu_to_clk32k:
274 + str r0, [r4, #PMC_PLLP_WB0_OVERRIDE]
275 +
276 + /* disable PLLP, PLLA, PLLC and PLLX */
277 ++ tegra_get_soc_id TEGRA_APB_MISC_BASE, r1
278 ++ cmp r1, #TEGRA30
279 + ldr r0, [r5, #CLK_RESET_PLLP_BASE]
280 ++ orrne r0, r0, #(1 << 31) @ enable PllP bypass on fast cluster
281 + bic r0, r0, #(1 << 30)
282 + str r0, [r5, #CLK_RESET_PLLP_BASE]
283 + ldr r0, [r5, #CLK_RESET_PLLA_BASE]
284 +diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c
285 +index 27a40101dd3a..fd26b5c92b44 100644
286 +--- a/arch/arm/mm/init.c
287 ++++ b/arch/arm/mm/init.c
288 +@@ -356,7 +356,7 @@ static inline void poison_init_mem(void *s, size_t count)
289 + *p++ = 0xe7fddef0;
290 + }
291 +
292 +-static inline void
293 ++static inline void __init
294 + free_memmap(unsigned long start_pfn, unsigned long end_pfn)
295 + {
296 + struct page *start_pg, *end_pg;
297 +diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h
298 +index 2b55aee7c051..92f70a34c5e6 100644
299 +--- a/arch/arm64/include/asm/kvm_emulate.h
300 ++++ b/arch/arm64/include/asm/kvm_emulate.h
301 +@@ -188,6 +188,11 @@ static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu)
302 + return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SSE);
303 + }
304 +
305 ++static inline bool kvm_vcpu_dabt_issf(const struct kvm_vcpu *vcpu)
306 ++{
307 ++ return !!(kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SF);
308 ++}
309 ++
310 + static inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu)
311 + {
312 + return (kvm_vcpu_get_hsr(vcpu) & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT;
313 +diff --git a/arch/arm64/include/asm/kvm_mmio.h b/arch/arm64/include/asm/kvm_mmio.h
314 +index 75ea42079757..0240290cf764 100644
315 +--- a/arch/arm64/include/asm/kvm_mmio.h
316 ++++ b/arch/arm64/include/asm/kvm_mmio.h
317 +@@ -21,13 +21,11 @@
318 + #include <linux/kvm_host.h>
319 + #include <asm/kvm_arm.h>
320 +
321 +-/*
322 +- * This is annoying. The mmio code requires this, even if we don't
323 +- * need any decoding. To be fixed.
324 +- */
325 + struct kvm_decode {
326 + unsigned long rt;
327 + bool sign_extend;
328 ++ /* Witdth of the register accessed by the faulting instruction is 64-bits */
329 ++ bool sixty_four;
330 + };
331 +
332 + void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data);
333 +diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
334 +index 09c6499bc500..c477fd34a912 100644
335 +--- a/arch/arm64/kernel/cpufeature.c
336 ++++ b/arch/arm64/kernel/cpufeature.c
337 +@@ -1103,7 +1103,7 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
338 + {
339 + /* FP/SIMD is not implemented */
340 + .capability = ARM64_HAS_NO_FPSIMD,
341 +- .type = ARM64_CPUCAP_SYSTEM_FEATURE,
342 ++ .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
343 + .min_field_value = 0,
344 + .matches = has_no_fpsimd,
345 + },
346 +diff --git a/arch/mips/Makefile.postlink b/arch/mips/Makefile.postlink
347 +index 4eea4188cb20..13e0beb9eee3 100644
348 +--- a/arch/mips/Makefile.postlink
349 ++++ b/arch/mips/Makefile.postlink
350 +@@ -12,7 +12,7 @@ __archpost:
351 + include scripts/Kbuild.include
352 +
353 + CMD_RELOCS = arch/mips/boot/tools/relocs
354 +-quiet_cmd_relocs = RELOCS $@
355 ++quiet_cmd_relocs = RELOCS $@
356 + cmd_relocs = $(CMD_RELOCS) $@
357 +
358 + # `@true` prevents complaint when there is nothing to be done
359 +diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
360 +index de3b07c7be30..277e4ffb928b 100644
361 +--- a/arch/powerpc/Kconfig
362 ++++ b/arch/powerpc/Kconfig
363 +@@ -225,6 +225,7 @@ config PPC
364 + select MODULES_USE_ELF_RELA
365 + select NO_BOOTMEM
366 + select OF
367 ++ select OF_DMA_DEFAULT_COHERENT if !NOT_COHERENT_CACHE
368 + select OF_EARLY_FLATTREE
369 + select OF_RESERVED_MEM
370 + select OLD_SIGACTION if PPC32
371 +diff --git a/arch/powerpc/boot/4xx.c b/arch/powerpc/boot/4xx.c
372 +index f7da65169124..3c8774163c7e 100644
373 +--- a/arch/powerpc/boot/4xx.c
374 ++++ b/arch/powerpc/boot/4xx.c
375 +@@ -232,7 +232,7 @@ void ibm4xx_denali_fixup_memsize(void)
376 + dpath = 8; /* 64 bits */
377 +
378 + /* get address pins (rows) */
379 +- val = SDRAM0_READ(DDR0_42);
380 ++ val = SDRAM0_READ(DDR0_42);
381 +
382 + row = DDR_GET_VAL(val, DDR_APIN, DDR_APIN_SHIFT);
383 + if (row > max_row)
384 +diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
385 +index 7de26809340a..e4f81f014206 100644
386 +--- a/arch/powerpc/kvm/book3s_hv.c
387 ++++ b/arch/powerpc/kvm/book3s_hv.c
388 +@@ -1997,7 +1997,7 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
389 + mutex_unlock(&kvm->lock);
390 +
391 + if (!vcore)
392 +- goto free_vcpu;
393 ++ goto uninit_vcpu;
394 +
395 + spin_lock(&vcore->lock);
396 + ++vcore->num_threads;
397 +@@ -2014,6 +2014,8 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
398 +
399 + return vcpu;
400 +
401 ++uninit_vcpu:
402 ++ kvm_vcpu_uninit(vcpu);
403 + free_vcpu:
404 + kmem_cache_free(kvm_vcpu_cache, vcpu);
405 + out:
406 +diff --git a/arch/powerpc/kvm/book3s_pr.c b/arch/powerpc/kvm/book3s_pr.c
407 +index e2ef16198456..f5bbb188f18d 100644
408 +--- a/arch/powerpc/kvm/book3s_pr.c
409 ++++ b/arch/powerpc/kvm/book3s_pr.c
410 +@@ -1482,10 +1482,12 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_pr(struct kvm *kvm,
411 +
412 + err = kvmppc_mmu_init(vcpu);
413 + if (err < 0)
414 +- goto uninit_vcpu;
415 ++ goto free_shared_page;
416 +
417 + return vcpu;
418 +
419 ++free_shared_page:
420 ++ free_page((unsigned long)vcpu->arch.shared);
421 + uninit_vcpu:
422 + kvm_vcpu_uninit(vcpu);
423 + free_shadow_vcpu:
424 +diff --git a/arch/powerpc/platforms/pseries/hotplug-memory.c b/arch/powerpc/platforms/pseries/hotplug-memory.c
425 +index fdfce7a46d73..a0847be0b035 100644
426 +--- a/arch/powerpc/platforms/pseries/hotplug-memory.c
427 ++++ b/arch/powerpc/platforms/pseries/hotplug-memory.c
428 +@@ -452,8 +452,10 @@ static bool lmb_is_removable(struct of_drconf_cell *lmb)
429 +
430 + for (i = 0; i < scns_per_block; i++) {
431 + pfn = PFN_DOWN(phys_addr);
432 +- if (!pfn_present(pfn))
433 ++ if (!pfn_present(pfn)) {
434 ++ phys_addr += MIN_MEMORY_BLOCK_SIZE;
435 + continue;
436 ++ }
437 +
438 + rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
439 + phys_addr += MIN_MEMORY_BLOCK_SIZE;
440 +diff --git a/arch/powerpc/platforms/pseries/iommu.c b/arch/powerpc/platforms/pseries/iommu.c
441 +index 7c181467d0ad..0e4e22dfa6b5 100644
442 +--- a/arch/powerpc/platforms/pseries/iommu.c
443 ++++ b/arch/powerpc/platforms/pseries/iommu.c
444 +@@ -168,10 +168,10 @@ static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
445 + return be64_to_cpu(*tcep);
446 + }
447 +
448 +-static void tce_free_pSeriesLP(struct iommu_table*, long, long);
449 ++static void tce_free_pSeriesLP(unsigned long liobn, long, long);
450 + static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
451 +
452 +-static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
453 ++static int tce_build_pSeriesLP(unsigned long liobn, long tcenum, long tceshift,
454 + long npages, unsigned long uaddr,
455 + enum dma_data_direction direction,
456 + unsigned long attrs)
457 +@@ -182,25 +182,25 @@ static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
458 + int ret = 0;
459 + long tcenum_start = tcenum, npages_start = npages;
460 +
461 +- rpn = __pa(uaddr) >> TCE_SHIFT;
462 ++ rpn = __pa(uaddr) >> tceshift;
463 + proto_tce = TCE_PCI_READ;
464 + if (direction != DMA_TO_DEVICE)
465 + proto_tce |= TCE_PCI_WRITE;
466 +
467 + while (npages--) {
468 +- tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
469 +- rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
470 ++ tce = proto_tce | (rpn & TCE_RPN_MASK) << tceshift;
471 ++ rc = plpar_tce_put((u64)liobn, (u64)tcenum << tceshift, tce);
472 +
473 + if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
474 + ret = (int)rc;
475 +- tce_free_pSeriesLP(tbl, tcenum_start,
476 ++ tce_free_pSeriesLP(liobn, tcenum_start,
477 + (npages_start - (npages + 1)));
478 + break;
479 + }
480 +
481 + if (rc && printk_ratelimit()) {
482 + printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
483 +- printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
484 ++ printk("\tindex = 0x%llx\n", (u64)liobn);
485 + printk("\ttcenum = 0x%llx\n", (u64)tcenum);
486 + printk("\ttce val = 0x%llx\n", tce );
487 + dump_stack();
488 +@@ -229,7 +229,8 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
489 + unsigned long flags;
490 +
491 + if ((npages == 1) || !firmware_has_feature(FW_FEATURE_MULTITCE)) {
492 +- return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
493 ++ return tce_build_pSeriesLP(tbl->it_index, tcenum,
494 ++ tbl->it_page_shift, npages, uaddr,
495 + direction, attrs);
496 + }
497 +
498 +@@ -245,8 +246,9 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
499 + /* If allocation fails, fall back to the loop implementation */
500 + if (!tcep) {
501 + local_irq_restore(flags);
502 +- return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
503 +- direction, attrs);
504 ++ return tce_build_pSeriesLP(tbl->it_index, tcenum,
505 ++ tbl->it_page_shift,
506 ++ npages, uaddr, direction, attrs);
507 + }
508 + __this_cpu_write(tce_page, tcep);
509 + }
510 +@@ -297,16 +299,16 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
511 + return ret;
512 + }
513 +
514 +-static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
515 ++static void tce_free_pSeriesLP(unsigned long liobn, long tcenum, long npages)
516 + {
517 + u64 rc;
518 +
519 + while (npages--) {
520 +- rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
521 ++ rc = plpar_tce_put((u64)liobn, (u64)tcenum << 12, 0);
522 +
523 + if (rc && printk_ratelimit()) {
524 + printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
525 +- printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
526 ++ printk("\tindex = 0x%llx\n", (u64)liobn);
527 + printk("\ttcenum = 0x%llx\n", (u64)tcenum);
528 + dump_stack();
529 + }
530 +@@ -321,7 +323,7 @@ static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long n
531 + u64 rc;
532 +
533 + if (!firmware_has_feature(FW_FEATURE_MULTITCE))
534 +- return tce_free_pSeriesLP(tbl, tcenum, npages);
535 ++ return tce_free_pSeriesLP(tbl->it_index, tcenum, npages);
536 +
537 + rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
538 +
539 +@@ -436,6 +438,19 @@ static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
540 + u64 rc = 0;
541 + long l, limit;
542 +
543 ++ if (!firmware_has_feature(FW_FEATURE_MULTITCE)) {
544 ++ unsigned long tceshift = be32_to_cpu(maprange->tce_shift);
545 ++ unsigned long dmastart = (start_pfn << PAGE_SHIFT) +
546 ++ be64_to_cpu(maprange->dma_base);
547 ++ unsigned long tcenum = dmastart >> tceshift;
548 ++ unsigned long npages = num_pfn << PAGE_SHIFT >> tceshift;
549 ++ void *uaddr = __va(start_pfn << PAGE_SHIFT);
550 ++
551 ++ return tce_build_pSeriesLP(be32_to_cpu(maprange->liobn),
552 ++ tcenum, tceshift, npages, (unsigned long) uaddr,
553 ++ DMA_BIDIRECTIONAL, 0);
554 ++ }
555 ++
556 + local_irq_disable(); /* to protect tcep and the page behind it */
557 + tcep = __this_cpu_read(tce_page);
558 +
559 +diff --git a/arch/powerpc/platforms/pseries/vio.c b/arch/powerpc/platforms/pseries/vio.c
560 +index d86938260a86..fc778865a412 100644
561 +--- a/arch/powerpc/platforms/pseries/vio.c
562 ++++ b/arch/powerpc/platforms/pseries/vio.c
563 +@@ -1195,6 +1195,8 @@ static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
564 + if (tbl == NULL)
565 + return NULL;
566 +
567 ++ kref_init(&tbl->it_kref);
568 ++
569 + of_parse_dma_window(dev->dev.of_node, dma_window,
570 + &tbl->it_index, &offset, &size);
571 +
572 +diff --git a/arch/powerpc/xmon/xmon.c b/arch/powerpc/xmon/xmon.c
573 +index 51a53fd51722..0885993b2fb4 100644
574 +--- a/arch/powerpc/xmon/xmon.c
575 ++++ b/arch/powerpc/xmon/xmon.c
576 +@@ -1830,15 +1830,14 @@ static void dump_300_sprs(void)
577 +
578 + printf("pidr = %.16lx tidr = %.16lx\n",
579 + mfspr(SPRN_PID), mfspr(SPRN_TIDR));
580 +- printf("asdr = %.16lx psscr = %.16lx\n",
581 +- mfspr(SPRN_ASDR), hv ? mfspr(SPRN_PSSCR)
582 +- : mfspr(SPRN_PSSCR_PR));
583 ++ printf("psscr = %.16lx\n",
584 ++ hv ? mfspr(SPRN_PSSCR) : mfspr(SPRN_PSSCR_PR));
585 +
586 + if (!hv)
587 + return;
588 +
589 +- printf("ptcr = %.16lx\n",
590 +- mfspr(SPRN_PTCR));
591 ++ printf("ptcr = %.16lx asdr = %.16lx\n",
592 ++ mfspr(SPRN_PTCR), mfspr(SPRN_ASDR));
593 + #endif
594 + }
595 +
596 +diff --git a/arch/s390/include/asm/page.h b/arch/s390/include/asm/page.h
597 +index 41e3908b397f..779c589b7089 100644
598 +--- a/arch/s390/include/asm/page.h
599 ++++ b/arch/s390/include/asm/page.h
600 +@@ -33,6 +33,8 @@
601 + #define ARCH_HAS_PREPARE_HUGEPAGE
602 + #define ARCH_HAS_HUGEPAGE_CLEAR_FLUSH
603 +
604 ++#define HAVE_ARCH_HUGETLB_UNMAPPED_AREA
605 ++
606 + #include <asm/setup.h>
607 + #ifndef __ASSEMBLY__
608 +
609 +diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
610 +index 91c24e87fe10..46fee3f4dedd 100644
611 +--- a/arch/s390/kvm/kvm-s390.c
612 ++++ b/arch/s390/kvm/kvm-s390.c
613 +@@ -2384,9 +2384,7 @@ static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
614 + memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
615 + vcpu->arch.sie_block->gcr[0] = 0xE0UL;
616 + vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
617 +- /* make sure the new fpc will be lazily loaded */
618 +- save_fpu_regs();
619 +- current->thread.fpu.fpc = 0;
620 ++ vcpu->run->s.regs.fpc = 0;
621 + vcpu->arch.sie_block->gbea = 1;
622 + vcpu->arch.sie_block->pp = 0;
623 + vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
624 +@@ -3753,7 +3751,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
625 + }
626 + case KVM_S390_STORE_STATUS:
627 + idx = srcu_read_lock(&vcpu->kvm->srcu);
628 +- r = kvm_s390_vcpu_store_status(vcpu, arg);
629 ++ r = kvm_s390_store_status_unloaded(vcpu, arg);
630 + srcu_read_unlock(&vcpu->kvm->srcu, idx);
631 + break;
632 + case KVM_S390_SET_INITIAL_PSW: {
633 +diff --git a/arch/s390/mm/hugetlbpage.c b/arch/s390/mm/hugetlbpage.c
634 +index e804090f4470..e19ea9ebe960 100644
635 +--- a/arch/s390/mm/hugetlbpage.c
636 ++++ b/arch/s390/mm/hugetlbpage.c
637 +@@ -2,7 +2,7 @@
638 + /*
639 + * IBM System z Huge TLB Page Support for Kernel.
640 + *
641 +- * Copyright IBM Corp. 2007,2016
642 ++ * Copyright IBM Corp. 2007,2020
643 + * Author(s): Gerald Schaefer <gerald.schaefer@××××××.com>
644 + */
645 +
646 +@@ -11,6 +11,9 @@
647 +
648 + #include <linux/mm.h>
649 + #include <linux/hugetlb.h>
650 ++#include <linux/mman.h>
651 ++#include <linux/sched/mm.h>
652 ++#include <linux/security.h>
653 +
654 + /*
655 + * If the bit selected by single-bit bitmask "a" is set within "x", move
656 +@@ -243,3 +246,98 @@ static __init int setup_hugepagesz(char *opt)
657 + return 1;
658 + }
659 + __setup("hugepagesz=", setup_hugepagesz);
660 ++
661 ++static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
662 ++ unsigned long addr, unsigned long len,
663 ++ unsigned long pgoff, unsigned long flags)
664 ++{
665 ++ struct hstate *h = hstate_file(file);
666 ++ struct vm_unmapped_area_info info;
667 ++
668 ++ info.flags = 0;
669 ++ info.length = len;
670 ++ info.low_limit = current->mm->mmap_base;
671 ++ info.high_limit = TASK_SIZE;
672 ++ info.align_mask = PAGE_MASK & ~huge_page_mask(h);
673 ++ info.align_offset = 0;
674 ++ return vm_unmapped_area(&info);
675 ++}
676 ++
677 ++static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
678 ++ unsigned long addr0, unsigned long len,
679 ++ unsigned long pgoff, unsigned long flags)
680 ++{
681 ++ struct hstate *h = hstate_file(file);
682 ++ struct vm_unmapped_area_info info;
683 ++ unsigned long addr;
684 ++
685 ++ info.flags = VM_UNMAPPED_AREA_TOPDOWN;
686 ++ info.length = len;
687 ++ info.low_limit = max(PAGE_SIZE, mmap_min_addr);
688 ++ info.high_limit = current->mm->mmap_base;
689 ++ info.align_mask = PAGE_MASK & ~huge_page_mask(h);
690 ++ info.align_offset = 0;
691 ++ addr = vm_unmapped_area(&info);
692 ++
693 ++ /*
694 ++ * A failed mmap() very likely causes application failure,
695 ++ * so fall back to the bottom-up function here. This scenario
696 ++ * can happen with large stack limits and large mmap()
697 ++ * allocations.
698 ++ */
699 ++ if (addr & ~PAGE_MASK) {
700 ++ VM_BUG_ON(addr != -ENOMEM);
701 ++ info.flags = 0;
702 ++ info.low_limit = TASK_UNMAPPED_BASE;
703 ++ info.high_limit = TASK_SIZE;
704 ++ addr = vm_unmapped_area(&info);
705 ++ }
706 ++
707 ++ return addr;
708 ++}
709 ++
710 ++unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
711 ++ unsigned long len, unsigned long pgoff, unsigned long flags)
712 ++{
713 ++ struct hstate *h = hstate_file(file);
714 ++ struct mm_struct *mm = current->mm;
715 ++ struct vm_area_struct *vma;
716 ++ int rc;
717 ++
718 ++ if (len & ~huge_page_mask(h))
719 ++ return -EINVAL;
720 ++ if (len > TASK_SIZE - mmap_min_addr)
721 ++ return -ENOMEM;
722 ++
723 ++ if (flags & MAP_FIXED) {
724 ++ if (prepare_hugepage_range(file, addr, len))
725 ++ return -EINVAL;
726 ++ goto check_asce_limit;
727 ++ }
728 ++
729 ++ if (addr) {
730 ++ addr = ALIGN(addr, huge_page_size(h));
731 ++ vma = find_vma(mm, addr);
732 ++ if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
733 ++ (!vma || addr + len <= vm_start_gap(vma)))
734 ++ goto check_asce_limit;
735 ++ }
736 ++
737 ++ if (mm->get_unmapped_area == arch_get_unmapped_area)
738 ++ addr = hugetlb_get_unmapped_area_bottomup(file, addr, len,
739 ++ pgoff, flags);
740 ++ else
741 ++ addr = hugetlb_get_unmapped_area_topdown(file, addr, len,
742 ++ pgoff, flags);
743 ++ if (addr & ~PAGE_MASK)
744 ++ return addr;
745 ++
746 ++check_asce_limit:
747 ++ if (addr + len > current->mm->context.asce_limit &&
748 ++ addr + len <= TASK_SIZE) {
749 ++ rc = crst_table_upgrade(mm, addr + len);
750 ++ if (rc)
751 ++ return (unsigned long) rc;
752 ++ }
753 ++ return addr;
754 ++}
755 +diff --git a/arch/sparc/include/uapi/asm/ipcbuf.h b/arch/sparc/include/uapi/asm/ipcbuf.h
756 +index 9d0d125500e2..084b8949ddff 100644
757 +--- a/arch/sparc/include/uapi/asm/ipcbuf.h
758 ++++ b/arch/sparc/include/uapi/asm/ipcbuf.h
759 +@@ -15,19 +15,19 @@
760 +
761 + struct ipc64_perm
762 + {
763 +- __kernel_key_t key;
764 +- __kernel_uid_t uid;
765 +- __kernel_gid_t gid;
766 +- __kernel_uid_t cuid;
767 +- __kernel_gid_t cgid;
768 ++ __kernel_key_t key;
769 ++ __kernel_uid32_t uid;
770 ++ __kernel_gid32_t gid;
771 ++ __kernel_uid32_t cuid;
772 ++ __kernel_gid32_t cgid;
773 + #ifndef __arch64__
774 +- unsigned short __pad0;
775 ++ unsigned short __pad0;
776 + #endif
777 +- __kernel_mode_t mode;
778 +- unsigned short __pad1;
779 +- unsigned short seq;
780 +- unsigned long long __unused1;
781 +- unsigned long long __unused2;
782 ++ __kernel_mode_t mode;
783 ++ unsigned short __pad1;
784 ++ unsigned short seq;
785 ++ unsigned long long __unused1;
786 ++ unsigned long long __unused2;
787 + };
788 +
789 + #endif /* __SPARC_IPCBUF_H */
790 +diff --git a/arch/x86/kernel/cpu/tsx.c b/arch/x86/kernel/cpu/tsx.c
791 +index 3e20d322bc98..032509adf9de 100644
792 +--- a/arch/x86/kernel/cpu/tsx.c
793 ++++ b/arch/x86/kernel/cpu/tsx.c
794 +@@ -115,11 +115,12 @@ void __init tsx_init(void)
795 + tsx_disable();
796 +
797 + /*
798 +- * tsx_disable() will change the state of the
799 +- * RTM CPUID bit. Clear it here since it is now
800 +- * expected to be not set.
801 ++ * tsx_disable() will change the state of the RTM and HLE CPUID
802 ++ * bits. Clear them here since they are now expected to be not
803 ++ * set.
804 + */
805 + setup_clear_cpu_cap(X86_FEATURE_RTM);
806 ++ setup_clear_cpu_cap(X86_FEATURE_HLE);
807 + } else if (tsx_ctrl_state == TSX_CTRL_ENABLE) {
808 +
809 + /*
810 +@@ -131,10 +132,10 @@ void __init tsx_init(void)
811 + tsx_enable();
812 +
813 + /*
814 +- * tsx_enable() will change the state of the
815 +- * RTM CPUID bit. Force it here since it is now
816 +- * expected to be set.
817 ++ * tsx_enable() will change the state of the RTM and HLE CPUID
818 ++ * bits. Force them here since they are now expected to be set.
819 + */
820 + setup_force_cpu_cap(X86_FEATURE_RTM);
821 ++ setup_force_cpu_cap(X86_FEATURE_HLE);
822 + }
823 + }
824 +diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
825 +index eb8b843325f4..041b9b05fae1 100644
826 +--- a/arch/x86/kvm/emulate.c
827 ++++ b/arch/x86/kvm/emulate.c
828 +@@ -5094,16 +5094,28 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
829 + ctxt->ad_bytes = def_ad_bytes ^ 6;
830 + break;
831 + case 0x26: /* ES override */
832 ++ has_seg_override = true;
833 ++ ctxt->seg_override = VCPU_SREG_ES;
834 ++ break;
835 + case 0x2e: /* CS override */
836 ++ has_seg_override = true;
837 ++ ctxt->seg_override = VCPU_SREG_CS;
838 ++ break;
839 + case 0x36: /* SS override */
840 ++ has_seg_override = true;
841 ++ ctxt->seg_override = VCPU_SREG_SS;
842 ++ break;
843 + case 0x3e: /* DS override */
844 + has_seg_override = true;
845 +- ctxt->seg_override = (ctxt->b >> 3) & 3;
846 ++ ctxt->seg_override = VCPU_SREG_DS;
847 + break;
848 + case 0x64: /* FS override */
849 ++ has_seg_override = true;
850 ++ ctxt->seg_override = VCPU_SREG_FS;
851 ++ break;
852 + case 0x65: /* GS override */
853 + has_seg_override = true;
854 +- ctxt->seg_override = ctxt->b & 7;
855 ++ ctxt->seg_override = VCPU_SREG_GS;
856 + break;
857 + case 0x40 ... 0x4f: /* REX */
858 + if (mode != X86EMUL_MODE_PROT64)
859 +@@ -5187,10 +5199,15 @@ done_prefixes:
860 + }
861 + break;
862 + case Escape:
863 +- if (ctxt->modrm > 0xbf)
864 +- opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
865 +- else
866 ++ if (ctxt->modrm > 0xbf) {
867 ++ size_t size = ARRAY_SIZE(opcode.u.esc->high);
868 ++ u32 index = array_index_nospec(
869 ++ ctxt->modrm - 0xc0, size);
870 ++
871 ++ opcode = opcode.u.esc->high[index];
872 ++ } else {
873 + opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
874 ++ }
875 + break;
876 + case InstrDual:
877 + if ((ctxt->modrm >> 6) == 3)
878 +diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
879 +index 5d13abecb384..2fba82b06c2d 100644
880 +--- a/arch/x86/kvm/hyperv.c
881 ++++ b/arch/x86/kvm/hyperv.c
882 +@@ -747,11 +747,12 @@ static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
883 + u32 index, u64 *pdata)
884 + {
885 + struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
886 ++ size_t size = ARRAY_SIZE(hv->hv_crash_param);
887 +
888 +- if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
889 ++ if (WARN_ON_ONCE(index >= size))
890 + return -EINVAL;
891 +
892 +- *pdata = hv->hv_crash_param[index];
893 ++ *pdata = hv->hv_crash_param[array_index_nospec(index, size)];
894 + return 0;
895 + }
896 +
897 +@@ -790,11 +791,12 @@ static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
898 + u32 index, u64 data)
899 + {
900 + struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
901 ++ size_t size = ARRAY_SIZE(hv->hv_crash_param);
902 +
903 +- if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
904 ++ if (WARN_ON_ONCE(index >= size))
905 + return -EINVAL;
906 +
907 +- hv->hv_crash_param[index] = data;
908 ++ hv->hv_crash_param[array_index_nospec(index, size)] = data;
909 + return 0;
910 + }
911 +
912 +diff --git a/arch/x86/kvm/i8259.c b/arch/x86/kvm/i8259.c
913 +index bdcd4139eca9..38a36a1cc87f 100644
914 +--- a/arch/x86/kvm/i8259.c
915 ++++ b/arch/x86/kvm/i8259.c
916 +@@ -460,10 +460,14 @@ static int picdev_write(struct kvm_pic *s,
917 + switch (addr) {
918 + case 0x20:
919 + case 0x21:
920 ++ pic_lock(s);
921 ++ pic_ioport_write(&s->pics[0], addr, data);
922 ++ pic_unlock(s);
923 ++ break;
924 + case 0xa0:
925 + case 0xa1:
926 + pic_lock(s);
927 +- pic_ioport_write(&s->pics[addr >> 7], addr, data);
928 ++ pic_ioport_write(&s->pics[1], addr, data);
929 + pic_unlock(s);
930 + break;
931 + case 0x4d0:
932 +diff --git a/arch/x86/kvm/ioapic.c b/arch/x86/kvm/ioapic.c
933 +index 9d270ba9643c..dab6940ea99c 100644
934 +--- a/arch/x86/kvm/ioapic.c
935 ++++ b/arch/x86/kvm/ioapic.c
936 +@@ -36,6 +36,7 @@
937 + #include <linux/io.h>
938 + #include <linux/slab.h>
939 + #include <linux/export.h>
940 ++#include <linux/nospec.h>
941 + #include <asm/processor.h>
942 + #include <asm/page.h>
943 + #include <asm/current.h>
944 +@@ -73,13 +74,14 @@ static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
945 + default:
946 + {
947 + u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
948 +- u64 redir_content;
949 ++ u64 redir_content = ~0ULL;
950 +
951 +- if (redir_index < IOAPIC_NUM_PINS)
952 +- redir_content =
953 +- ioapic->redirtbl[redir_index].bits;
954 +- else
955 +- redir_content = ~0ULL;
956 ++ if (redir_index < IOAPIC_NUM_PINS) {
957 ++ u32 index = array_index_nospec(
958 ++ redir_index, IOAPIC_NUM_PINS);
959 ++
960 ++ redir_content = ioapic->redirtbl[index].bits;
961 ++ }
962 +
963 + result = (ioapic->ioregsel & 0x1) ?
964 + (redir_content >> 32) & 0xffffffff :
965 +@@ -297,6 +299,7 @@ static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
966 + ioapic_debug("change redir index %x val %x\n", index, val);
967 + if (index >= IOAPIC_NUM_PINS)
968 + return;
969 ++ index = array_index_nospec(index, IOAPIC_NUM_PINS);
970 + e = &ioapic->redirtbl[index];
971 + mask_before = e->fields.mask;
972 + /* Preserve read-only fields */
973 +diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
974 +index 2307f63efd20..8715711f2755 100644
975 +--- a/arch/x86/kvm/lapic.c
976 ++++ b/arch/x86/kvm/lapic.c
977 +@@ -1754,15 +1754,20 @@ int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
978 + case APIC_LVTTHMR:
979 + case APIC_LVTPC:
980 + case APIC_LVT1:
981 +- case APIC_LVTERR:
982 ++ case APIC_LVTERR: {
983 + /* TODO: Check vector */
984 ++ size_t size;
985 ++ u32 index;
986 ++
987 + if (!kvm_apic_sw_enabled(apic))
988 + val |= APIC_LVT_MASKED;
989 +-
990 +- val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
991 ++ size = ARRAY_SIZE(apic_lvt_mask);
992 ++ index = array_index_nospec(
993 ++ (reg - APIC_LVTT) >> 4, size);
994 ++ val &= apic_lvt_mask[index];
995 + kvm_lapic_set_reg(apic, reg, val);
996 +-
997 + break;
998 ++ }
999 +
1000 + case APIC_LVTT:
1001 + if (!kvm_apic_sw_enabled(apic))
1002 +diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
1003 +index c0b0135ef07f..e5af08b58132 100644
1004 +--- a/arch/x86/kvm/mmu.c
1005 ++++ b/arch/x86/kvm/mmu.c
1006 +@@ -1165,12 +1165,12 @@ static bool mmu_gfn_lpage_is_disallowed(struct kvm_vcpu *vcpu, gfn_t gfn,
1007 + return __mmu_gfn_lpage_is_disallowed(gfn, level, slot);
1008 + }
1009 +
1010 +-static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
1011 ++static int host_mapping_level(struct kvm_vcpu *vcpu, gfn_t gfn)
1012 + {
1013 + unsigned long page_size;
1014 + int i, ret = 0;
1015 +
1016 +- page_size = kvm_host_page_size(kvm, gfn);
1017 ++ page_size = kvm_host_page_size(vcpu, gfn);
1018 +
1019 + for (i = PT_PAGE_TABLE_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
1020 + if (page_size >= KVM_HPAGE_SIZE(i))
1021 +@@ -1220,7 +1220,7 @@ static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn,
1022 + if (unlikely(*force_pt_level))
1023 + return PT_PAGE_TABLE_LEVEL;
1024 +
1025 +- host_level = host_mapping_level(vcpu->kvm, large_gfn);
1026 ++ host_level = host_mapping_level(vcpu, large_gfn);
1027 +
1028 + if (host_level == PT_PAGE_TABLE_LEVEL)
1029 + return host_level;
1030 +diff --git a/arch/x86/kvm/mtrr.c b/arch/x86/kvm/mtrr.c
1031 +index e9ea2d45ae66..1209447d6014 100644
1032 +--- a/arch/x86/kvm/mtrr.c
1033 ++++ b/arch/x86/kvm/mtrr.c
1034 +@@ -202,11 +202,15 @@ static bool fixed_msr_to_seg_unit(u32 msr, int *seg, int *unit)
1035 + break;
1036 + case MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000:
1037 + *seg = 1;
1038 +- *unit = msr - MSR_MTRRfix16K_80000;
1039 ++ *unit = array_index_nospec(
1040 ++ msr - MSR_MTRRfix16K_80000,
1041 ++ MSR_MTRRfix16K_A0000 - MSR_MTRRfix16K_80000 + 1);
1042 + break;
1043 + case MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000:
1044 + *seg = 2;
1045 +- *unit = msr - MSR_MTRRfix4K_C0000;
1046 ++ *unit = array_index_nospec(
1047 ++ msr - MSR_MTRRfix4K_C0000,
1048 ++ MSR_MTRRfix4K_F8000 - MSR_MTRRfix4K_C0000 + 1);
1049 + break;
1050 + default:
1051 + return false;
1052 +diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
1053 +index a9a62b9a73e2..c67a636b268f 100644
1054 +--- a/arch/x86/kvm/pmu.h
1055 ++++ b/arch/x86/kvm/pmu.h
1056 +@@ -2,6 +2,8 @@
1057 + #ifndef __KVM_X86_PMU_H
1058 + #define __KVM_X86_PMU_H
1059 +
1060 ++#include <linux/nospec.h>
1061 ++
1062 + #define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu)
1063 + #define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu))
1064 + #define pmc_to_pmu(pmc) (&(pmc)->vcpu->arch.pmu)
1065 +@@ -81,8 +83,12 @@ static inline bool pmc_is_enabled(struct kvm_pmc *pmc)
1066 + static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,
1067 + u32 base)
1068 + {
1069 +- if (msr >= base && msr < base + pmu->nr_arch_gp_counters)
1070 +- return &pmu->gp_counters[msr - base];
1071 ++ if (msr >= base && msr < base + pmu->nr_arch_gp_counters) {
1072 ++ u32 index = array_index_nospec(msr - base,
1073 ++ pmu->nr_arch_gp_counters);
1074 ++
1075 ++ return &pmu->gp_counters[index];
1076 ++ }
1077 +
1078 + return NULL;
1079 + }
1080 +@@ -92,8 +98,12 @@ static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)
1081 + {
1082 + int base = MSR_CORE_PERF_FIXED_CTR0;
1083 +
1084 +- if (msr >= base && msr < base + pmu->nr_arch_fixed_counters)
1085 +- return &pmu->fixed_counters[msr - base];
1086 ++ if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) {
1087 ++ u32 index = array_index_nospec(msr - base,
1088 ++ pmu->nr_arch_fixed_counters);
1089 ++
1090 ++ return &pmu->fixed_counters[index];
1091 ++ }
1092 +
1093 + return NULL;
1094 + }
1095 +diff --git a/arch/x86/kvm/pmu_intel.c b/arch/x86/kvm/pmu_intel.c
1096 +index 2729131fe9bf..84ae4dd261ca 100644
1097 +--- a/arch/x86/kvm/pmu_intel.c
1098 ++++ b/arch/x86/kvm/pmu_intel.c
1099 +@@ -87,10 +87,14 @@ static unsigned intel_find_arch_event(struct kvm_pmu *pmu,
1100 +
1101 + static unsigned intel_find_fixed_event(int idx)
1102 + {
1103 +- if (idx >= ARRAY_SIZE(fixed_pmc_events))
1104 ++ u32 event;
1105 ++ size_t size = ARRAY_SIZE(fixed_pmc_events);
1106 ++
1107 ++ if (idx >= size)
1108 + return PERF_COUNT_HW_MAX;
1109 +
1110 +- return intel_arch_events[fixed_pmc_events[idx]].event_type;
1111 ++ event = fixed_pmc_events[array_index_nospec(idx, size)];
1112 ++ return intel_arch_events[event].event_type;
1113 + }
1114 +
1115 + /* check if a PMC is enabled by comparing it with globl_ctrl bits. */
1116 +@@ -131,15 +135,19 @@ static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu,
1117 + struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
1118 + bool fixed = idx & (1u << 30);
1119 + struct kvm_pmc *counters;
1120 ++ unsigned int num_counters;
1121 +
1122 + idx &= ~(3u << 30);
1123 +- if (!fixed && idx >= pmu->nr_arch_gp_counters)
1124 +- return NULL;
1125 +- if (fixed && idx >= pmu->nr_arch_fixed_counters)
1126 ++ if (fixed) {
1127 ++ counters = pmu->fixed_counters;
1128 ++ num_counters = pmu->nr_arch_fixed_counters;
1129 ++ } else {
1130 ++ counters = pmu->gp_counters;
1131 ++ num_counters = pmu->nr_arch_gp_counters;
1132 ++ }
1133 ++ if (idx >= num_counters)
1134 + return NULL;
1135 +- counters = fixed ? pmu->fixed_counters : pmu->gp_counters;
1136 +-
1137 +- return &counters[idx];
1138 ++ return &counters[array_index_nospec(idx, num_counters)];
1139 + }
1140 +
1141 + static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
1142 +diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
1143 +index c579cda1721e..809d1b031fd9 100644
1144 +--- a/arch/x86/kvm/vmx.c
1145 ++++ b/arch/x86/kvm/vmx.c
1146 +@@ -8014,8 +8014,10 @@ static int handle_vmread(struct kvm_vcpu *vcpu)
1147 + /* _system ok, nested_vmx_check_permission has verified cpl=0 */
1148 + if (kvm_write_guest_virt_system(vcpu, gva, &field_value,
1149 + (is_long_mode(vcpu) ? 8 : 4),
1150 +- &e))
1151 ++ &e)) {
1152 + kvm_inject_page_fault(vcpu, &e);
1153 ++ return 1;
1154 ++ }
1155 + }
1156 +
1157 + nested_vmx_succeed(vcpu);
1158 +diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
1159 +new file mode 100644
1160 +index 000000000000..3791ce8d269e
1161 +--- /dev/null
1162 ++++ b/arch/x86/kvm/vmx/vmx.c
1163 +@@ -0,0 +1,8033 @@
1164 ++// SPDX-License-Identifier: GPL-2.0-only
1165 ++/*
1166 ++ * Kernel-based Virtual Machine driver for Linux
1167 ++ *
1168 ++ * This module enables machines with Intel VT-x extensions to run virtual
1169 ++ * machines without emulation or binary translation.
1170 ++ *
1171 ++ * Copyright (C) 2006 Qumranet, Inc.
1172 ++ * Copyright 2010 Red Hat, Inc. and/or its affiliates.
1173 ++ *
1174 ++ * Authors:
1175 ++ * Avi Kivity <avi@××××××××.com>
1176 ++ * Yaniv Kamay <yaniv@××××××××.com>
1177 ++ */
1178 ++
1179 ++#include <linux/frame.h>
1180 ++#include <linux/highmem.h>
1181 ++#include <linux/hrtimer.h>
1182 ++#include <linux/kernel.h>
1183 ++#include <linux/kvm_host.h>
1184 ++#include <linux/module.h>
1185 ++#include <linux/moduleparam.h>
1186 ++#include <linux/mod_devicetable.h>
1187 ++#include <linux/mm.h>
1188 ++#include <linux/sched.h>
1189 ++#include <linux/sched/smt.h>
1190 ++#include <linux/slab.h>
1191 ++#include <linux/tboot.h>
1192 ++#include <linux/trace_events.h>
1193 ++
1194 ++#include <asm/apic.h>
1195 ++#include <asm/asm.h>
1196 ++#include <asm/cpu.h>
1197 ++#include <asm/debugreg.h>
1198 ++#include <asm/desc.h>
1199 ++#include <asm/fpu/internal.h>
1200 ++#include <asm/io.h>
1201 ++#include <asm/irq_remapping.h>
1202 ++#include <asm/kexec.h>
1203 ++#include <asm/perf_event.h>
1204 ++#include <asm/mce.h>
1205 ++#include <asm/mmu_context.h>
1206 ++#include <asm/mshyperv.h>
1207 ++#include <asm/spec-ctrl.h>
1208 ++#include <asm/virtext.h>
1209 ++#include <asm/vmx.h>
1210 ++
1211 ++#include "capabilities.h"
1212 ++#include "cpuid.h"
1213 ++#include "evmcs.h"
1214 ++#include "irq.h"
1215 ++#include "kvm_cache_regs.h"
1216 ++#include "lapic.h"
1217 ++#include "mmu.h"
1218 ++#include "nested.h"
1219 ++#include "ops.h"
1220 ++#include "pmu.h"
1221 ++#include "trace.h"
1222 ++#include "vmcs.h"
1223 ++#include "vmcs12.h"
1224 ++#include "vmx.h"
1225 ++#include "x86.h"
1226 ++
1227 ++MODULE_AUTHOR("Qumranet");
1228 ++MODULE_LICENSE("GPL");
1229 ++
1230 ++static const struct x86_cpu_id vmx_cpu_id[] = {
1231 ++ X86_FEATURE_MATCH(X86_FEATURE_VMX),
1232 ++ {}
1233 ++};
1234 ++MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
1235 ++
1236 ++bool __read_mostly enable_vpid = 1;
1237 ++module_param_named(vpid, enable_vpid, bool, 0444);
1238 ++
1239 ++static bool __read_mostly enable_vnmi = 1;
1240 ++module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
1241 ++
1242 ++bool __read_mostly flexpriority_enabled = 1;
1243 ++module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
1244 ++
1245 ++bool __read_mostly enable_ept = 1;
1246 ++module_param_named(ept, enable_ept, bool, S_IRUGO);
1247 ++
1248 ++bool __read_mostly enable_unrestricted_guest = 1;
1249 ++module_param_named(unrestricted_guest,
1250 ++ enable_unrestricted_guest, bool, S_IRUGO);
1251 ++
1252 ++bool __read_mostly enable_ept_ad_bits = 1;
1253 ++module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
1254 ++
1255 ++static bool __read_mostly emulate_invalid_guest_state = true;
1256 ++module_param(emulate_invalid_guest_state, bool, S_IRUGO);
1257 ++
1258 ++static bool __read_mostly fasteoi = 1;
1259 ++module_param(fasteoi, bool, S_IRUGO);
1260 ++
1261 ++static bool __read_mostly enable_apicv = 1;
1262 ++module_param(enable_apicv, bool, S_IRUGO);
1263 ++
1264 ++/*
1265 ++ * If nested=1, nested virtualization is supported, i.e., guests may use
1266 ++ * VMX and be a hypervisor for its own guests. If nested=0, guests may not
1267 ++ * use VMX instructions.
1268 ++ */
1269 ++static bool __read_mostly nested = 1;
1270 ++module_param(nested, bool, S_IRUGO);
1271 ++
1272 ++bool __read_mostly enable_pml = 1;
1273 ++module_param_named(pml, enable_pml, bool, S_IRUGO);
1274 ++
1275 ++static bool __read_mostly dump_invalid_vmcs = 0;
1276 ++module_param(dump_invalid_vmcs, bool, 0644);
1277 ++
1278 ++#define MSR_BITMAP_MODE_X2APIC 1
1279 ++#define MSR_BITMAP_MODE_X2APIC_APICV 2
1280 ++
1281 ++#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
1282 ++
1283 ++/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
1284 ++static int __read_mostly cpu_preemption_timer_multi;
1285 ++static bool __read_mostly enable_preemption_timer = 1;
1286 ++#ifdef CONFIG_X86_64
1287 ++module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
1288 ++#endif
1289 ++
1290 ++#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
1291 ++#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
1292 ++#define KVM_VM_CR0_ALWAYS_ON \
1293 ++ (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
1294 ++ X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
1295 ++#define KVM_CR4_GUEST_OWNED_BITS \
1296 ++ (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
1297 ++ | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
1298 ++
1299 ++#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
1300 ++#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
1301 ++#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
1302 ++
1303 ++#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
1304 ++
1305 ++#define MSR_IA32_RTIT_STATUS_MASK (~(RTIT_STATUS_FILTEREN | \
1306 ++ RTIT_STATUS_CONTEXTEN | RTIT_STATUS_TRIGGEREN | \
1307 ++ RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED | \
1308 ++ RTIT_STATUS_BYTECNT))
1309 ++
1310 ++#define MSR_IA32_RTIT_OUTPUT_BASE_MASK \
1311 ++ (~((1UL << cpuid_query_maxphyaddr(vcpu)) - 1) | 0x7f)
1312 ++
1313 ++/*
1314 ++ * These 2 parameters are used to config the controls for Pause-Loop Exiting:
1315 ++ * ple_gap: upper bound on the amount of time between two successive
1316 ++ * executions of PAUSE in a loop. Also indicate if ple enabled.
1317 ++ * According to test, this time is usually smaller than 128 cycles.
1318 ++ * ple_window: upper bound on the amount of time a guest is allowed to execute
1319 ++ * in a PAUSE loop. Tests indicate that most spinlocks are held for
1320 ++ * less than 2^12 cycles
1321 ++ * Time is measured based on a counter that runs at the same rate as the TSC,
1322 ++ * refer SDM volume 3b section 21.6.13 & 22.1.3.
1323 ++ */
1324 ++static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
1325 ++module_param(ple_gap, uint, 0444);
1326 ++
1327 ++static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
1328 ++module_param(ple_window, uint, 0444);
1329 ++
1330 ++/* Default doubles per-vcpu window every exit. */
1331 ++static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
1332 ++module_param(ple_window_grow, uint, 0444);
1333 ++
1334 ++/* Default resets per-vcpu window every exit to ple_window. */
1335 ++static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
1336 ++module_param(ple_window_shrink, uint, 0444);
1337 ++
1338 ++/* Default is to compute the maximum so we can never overflow. */
1339 ++static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
1340 ++module_param(ple_window_max, uint, 0444);
1341 ++
1342 ++/* Default is SYSTEM mode, 1 for host-guest mode */
1343 ++int __read_mostly pt_mode = PT_MODE_SYSTEM;
1344 ++module_param(pt_mode, int, S_IRUGO);
1345 ++
1346 ++static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
1347 ++static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
1348 ++static DEFINE_MUTEX(vmx_l1d_flush_mutex);
1349 ++
1350 ++/* Storage for pre module init parameter parsing */
1351 ++static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
1352 ++
1353 ++static const struct {
1354 ++ const char *option;
1355 ++ bool for_parse;
1356 ++} vmentry_l1d_param[] = {
1357 ++ [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
1358 ++ [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
1359 ++ [VMENTER_L1D_FLUSH_COND] = {"cond", true},
1360 ++ [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
1361 ++ [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
1362 ++ [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
1363 ++};
1364 ++
1365 ++#define L1D_CACHE_ORDER 4
1366 ++static void *vmx_l1d_flush_pages;
1367 ++
1368 ++static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
1369 ++{
1370 ++ struct page *page;
1371 ++ unsigned int i;
1372 ++
1373 ++ if (!boot_cpu_has_bug(X86_BUG_L1TF)) {
1374 ++ l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
1375 ++ return 0;
1376 ++ }
1377 ++
1378 ++ if (!enable_ept) {
1379 ++ l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
1380 ++ return 0;
1381 ++ }
1382 ++
1383 ++ if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
1384 ++ u64 msr;
1385 ++
1386 ++ rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
1387 ++ if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
1388 ++ l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
1389 ++ return 0;
1390 ++ }
1391 ++ }
1392 ++
1393 ++ /* If set to auto use the default l1tf mitigation method */
1394 ++ if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
1395 ++ switch (l1tf_mitigation) {
1396 ++ case L1TF_MITIGATION_OFF:
1397 ++ l1tf = VMENTER_L1D_FLUSH_NEVER;
1398 ++ break;
1399 ++ case L1TF_MITIGATION_FLUSH_NOWARN:
1400 ++ case L1TF_MITIGATION_FLUSH:
1401 ++ case L1TF_MITIGATION_FLUSH_NOSMT:
1402 ++ l1tf = VMENTER_L1D_FLUSH_COND;
1403 ++ break;
1404 ++ case L1TF_MITIGATION_FULL:
1405 ++ case L1TF_MITIGATION_FULL_FORCE:
1406 ++ l1tf = VMENTER_L1D_FLUSH_ALWAYS;
1407 ++ break;
1408 ++ }
1409 ++ } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
1410 ++ l1tf = VMENTER_L1D_FLUSH_ALWAYS;
1411 ++ }
1412 ++
1413 ++ if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
1414 ++ !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
1415 ++ /*
1416 ++ * This allocation for vmx_l1d_flush_pages is not tied to a VM
1417 ++ * lifetime and so should not be charged to a memcg.
1418 ++ */
1419 ++ page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
1420 ++ if (!page)
1421 ++ return -ENOMEM;
1422 ++ vmx_l1d_flush_pages = page_address(page);
1423 ++
1424 ++ /*
1425 ++ * Initialize each page with a different pattern in
1426 ++ * order to protect against KSM in the nested
1427 ++ * virtualization case.
1428 ++ */
1429 ++ for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
1430 ++ memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
1431 ++ PAGE_SIZE);
1432 ++ }
1433 ++ }
1434 ++
1435 ++ l1tf_vmx_mitigation = l1tf;
1436 ++
1437 ++ if (l1tf != VMENTER_L1D_FLUSH_NEVER)
1438 ++ static_branch_enable(&vmx_l1d_should_flush);
1439 ++ else
1440 ++ static_branch_disable(&vmx_l1d_should_flush);
1441 ++
1442 ++ if (l1tf == VMENTER_L1D_FLUSH_COND)
1443 ++ static_branch_enable(&vmx_l1d_flush_cond);
1444 ++ else
1445 ++ static_branch_disable(&vmx_l1d_flush_cond);
1446 ++ return 0;
1447 ++}
1448 ++
1449 ++static int vmentry_l1d_flush_parse(const char *s)
1450 ++{
1451 ++ unsigned int i;
1452 ++
1453 ++ if (s) {
1454 ++ for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
1455 ++ if (vmentry_l1d_param[i].for_parse &&
1456 ++ sysfs_streq(s, vmentry_l1d_param[i].option))
1457 ++ return i;
1458 ++ }
1459 ++ }
1460 ++ return -EINVAL;
1461 ++}
1462 ++
1463 ++static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
1464 ++{
1465 ++ int l1tf, ret;
1466 ++
1467 ++ l1tf = vmentry_l1d_flush_parse(s);
1468 ++ if (l1tf < 0)
1469 ++ return l1tf;
1470 ++
1471 ++ if (!boot_cpu_has(X86_BUG_L1TF))
1472 ++ return 0;
1473 ++
1474 ++ /*
1475 ++ * Has vmx_init() run already? If not then this is the pre init
1476 ++ * parameter parsing. In that case just store the value and let
1477 ++ * vmx_init() do the proper setup after enable_ept has been
1478 ++ * established.
1479 ++ */
1480 ++ if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
1481 ++ vmentry_l1d_flush_param = l1tf;
1482 ++ return 0;
1483 ++ }
1484 ++
1485 ++ mutex_lock(&vmx_l1d_flush_mutex);
1486 ++ ret = vmx_setup_l1d_flush(l1tf);
1487 ++ mutex_unlock(&vmx_l1d_flush_mutex);
1488 ++ return ret;
1489 ++}
1490 ++
1491 ++static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
1492 ++{
1493 ++ if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
1494 ++ return sprintf(s, "???\n");
1495 ++
1496 ++ return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
1497 ++}
1498 ++
1499 ++static const struct kernel_param_ops vmentry_l1d_flush_ops = {
1500 ++ .set = vmentry_l1d_flush_set,
1501 ++ .get = vmentry_l1d_flush_get,
1502 ++};
1503 ++module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
1504 ++
1505 ++static bool guest_state_valid(struct kvm_vcpu *vcpu);
1506 ++static u32 vmx_segment_access_rights(struct kvm_segment *var);
1507 ++static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1508 ++ u32 msr, int type);
1509 ++
1510 ++void vmx_vmexit(void);
1511 ++
1512 ++#define vmx_insn_failed(fmt...) \
1513 ++do { \
1514 ++ WARN_ONCE(1, fmt); \
1515 ++ pr_warn_ratelimited(fmt); \
1516 ++} while (0)
1517 ++
1518 ++asmlinkage void vmread_error(unsigned long field, bool fault)
1519 ++{
1520 ++ if (fault)
1521 ++ kvm_spurious_fault();
1522 ++ else
1523 ++ vmx_insn_failed("kvm: vmread failed: field=%lx\n", field);
1524 ++}
1525 ++
1526 ++noinline void vmwrite_error(unsigned long field, unsigned long value)
1527 ++{
1528 ++ vmx_insn_failed("kvm: vmwrite failed: field=%lx val=%lx err=%d\n",
1529 ++ field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1530 ++}
1531 ++
1532 ++noinline void vmclear_error(struct vmcs *vmcs, u64 phys_addr)
1533 ++{
1534 ++ vmx_insn_failed("kvm: vmclear failed: %p/%llx\n", vmcs, phys_addr);
1535 ++}
1536 ++
1537 ++noinline void vmptrld_error(struct vmcs *vmcs, u64 phys_addr)
1538 ++{
1539 ++ vmx_insn_failed("kvm: vmptrld failed: %p/%llx\n", vmcs, phys_addr);
1540 ++}
1541 ++
1542 ++noinline void invvpid_error(unsigned long ext, u16 vpid, gva_t gva)
1543 ++{
1544 ++ vmx_insn_failed("kvm: invvpid failed: ext=0x%lx vpid=%u gva=0x%lx\n",
1545 ++ ext, vpid, gva);
1546 ++}
1547 ++
1548 ++noinline void invept_error(unsigned long ext, u64 eptp, gpa_t gpa)
1549 ++{
1550 ++ vmx_insn_failed("kvm: invept failed: ext=0x%lx eptp=%llx gpa=0x%llx\n",
1551 ++ ext, eptp, gpa);
1552 ++}
1553 ++
1554 ++static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1555 ++DEFINE_PER_CPU(struct vmcs *, current_vmcs);
1556 ++/*
1557 ++ * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1558 ++ * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1559 ++ */
1560 ++static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
1561 ++
1562 ++/*
1563 ++ * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1564 ++ * can find which vCPU should be waken up.
1565 ++ */
1566 ++static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1567 ++static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1568 ++
1569 ++static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1570 ++static DEFINE_SPINLOCK(vmx_vpid_lock);
1571 ++
1572 ++struct vmcs_config vmcs_config;
1573 ++struct vmx_capability vmx_capability;
1574 ++
1575 ++#define VMX_SEGMENT_FIELD(seg) \
1576 ++ [VCPU_SREG_##seg] = { \
1577 ++ .selector = GUEST_##seg##_SELECTOR, \
1578 ++ .base = GUEST_##seg##_BASE, \
1579 ++ .limit = GUEST_##seg##_LIMIT, \
1580 ++ .ar_bytes = GUEST_##seg##_AR_BYTES, \
1581 ++ }
1582 ++
1583 ++static const struct kvm_vmx_segment_field {
1584 ++ unsigned selector;
1585 ++ unsigned base;
1586 ++ unsigned limit;
1587 ++ unsigned ar_bytes;
1588 ++} kvm_vmx_segment_fields[] = {
1589 ++ VMX_SEGMENT_FIELD(CS),
1590 ++ VMX_SEGMENT_FIELD(DS),
1591 ++ VMX_SEGMENT_FIELD(ES),
1592 ++ VMX_SEGMENT_FIELD(FS),
1593 ++ VMX_SEGMENT_FIELD(GS),
1594 ++ VMX_SEGMENT_FIELD(SS),
1595 ++ VMX_SEGMENT_FIELD(TR),
1596 ++ VMX_SEGMENT_FIELD(LDTR),
1597 ++};
1598 ++
1599 ++u64 host_efer;
1600 ++static unsigned long host_idt_base;
1601 ++
1602 ++/*
1603 ++ * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
1604 ++ * will emulate SYSCALL in legacy mode if the vendor string in guest
1605 ++ * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To
1606 ++ * support this emulation, IA32_STAR must always be included in
1607 ++ * vmx_msr_index[], even in i386 builds.
1608 ++ */
1609 ++const u32 vmx_msr_index[] = {
1610 ++#ifdef CONFIG_X86_64
1611 ++ MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
1612 ++#endif
1613 ++ MSR_EFER, MSR_TSC_AUX, MSR_STAR,
1614 ++ MSR_IA32_TSX_CTRL,
1615 ++};
1616 ++
1617 ++#if IS_ENABLED(CONFIG_HYPERV)
1618 ++static bool __read_mostly enlightened_vmcs = true;
1619 ++module_param(enlightened_vmcs, bool, 0444);
1620 ++
1621 ++/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1622 ++static void check_ept_pointer_match(struct kvm *kvm)
1623 ++{
1624 ++ struct kvm_vcpu *vcpu;
1625 ++ u64 tmp_eptp = INVALID_PAGE;
1626 ++ int i;
1627 ++
1628 ++ kvm_for_each_vcpu(i, vcpu, kvm) {
1629 ++ if (!VALID_PAGE(tmp_eptp)) {
1630 ++ tmp_eptp = to_vmx(vcpu)->ept_pointer;
1631 ++ } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1632 ++ to_kvm_vmx(kvm)->ept_pointers_match
1633 ++ = EPT_POINTERS_MISMATCH;
1634 ++ return;
1635 ++ }
1636 ++ }
1637 ++
1638 ++ to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1639 ++}
1640 ++
1641 ++static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush,
1642 ++ void *data)
1643 ++{
1644 ++ struct kvm_tlb_range *range = data;
1645 ++
1646 ++ return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn,
1647 ++ range->pages);
1648 ++}
1649 ++
1650 ++static inline int __hv_remote_flush_tlb_with_range(struct kvm *kvm,
1651 ++ struct kvm_vcpu *vcpu, struct kvm_tlb_range *range)
1652 ++{
1653 ++ u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
1654 ++
1655 ++ /*
1656 ++ * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs address
1657 ++ * of the base of EPT PML4 table, strip off EPT configuration
1658 ++ * information.
1659 ++ */
1660 ++ if (range)
1661 ++ return hyperv_flush_guest_mapping_range(ept_pointer & PAGE_MASK,
1662 ++ kvm_fill_hv_flush_list_func, (void *)range);
1663 ++ else
1664 ++ return hyperv_flush_guest_mapping(ept_pointer & PAGE_MASK);
1665 ++}
1666 ++
1667 ++static int hv_remote_flush_tlb_with_range(struct kvm *kvm,
1668 ++ struct kvm_tlb_range *range)
1669 ++{
1670 ++ struct kvm_vcpu *vcpu;
1671 ++ int ret = 0, i;
1672 ++
1673 ++ spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1674 ++
1675 ++ if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1676 ++ check_ept_pointer_match(kvm);
1677 ++
1678 ++ if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1679 ++ kvm_for_each_vcpu(i, vcpu, kvm) {
1680 ++ /* If ept_pointer is invalid pointer, bypass flush request. */
1681 ++ if (VALID_PAGE(to_vmx(vcpu)->ept_pointer))
1682 ++ ret |= __hv_remote_flush_tlb_with_range(
1683 ++ kvm, vcpu, range);
1684 ++ }
1685 ++ } else {
1686 ++ ret = __hv_remote_flush_tlb_with_range(kvm,
1687 ++ kvm_get_vcpu(kvm, 0), range);
1688 ++ }
1689 ++
1690 ++ spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1691 ++ return ret;
1692 ++}
1693 ++static int hv_remote_flush_tlb(struct kvm *kvm)
1694 ++{
1695 ++ return hv_remote_flush_tlb_with_range(kvm, NULL);
1696 ++}
1697 ++
1698 ++static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)
1699 ++{
1700 ++ struct hv_enlightened_vmcs *evmcs;
1701 ++ struct hv_partition_assist_pg **p_hv_pa_pg =
1702 ++ &vcpu->kvm->arch.hyperv.hv_pa_pg;
1703 ++ /*
1704 ++ * Synthetic VM-Exit is not enabled in current code and so All
1705 ++ * evmcs in singe VM shares same assist page.
1706 ++ */
1707 ++ if (!*p_hv_pa_pg)
1708 ++ *p_hv_pa_pg = kzalloc(PAGE_SIZE, GFP_KERNEL);
1709 ++
1710 ++ if (!*p_hv_pa_pg)
1711 ++ return -ENOMEM;
1712 ++
1713 ++ evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs;
1714 ++
1715 ++ evmcs->partition_assist_page =
1716 ++ __pa(*p_hv_pa_pg);
1717 ++ evmcs->hv_vm_id = (unsigned long)vcpu->kvm;
1718 ++ evmcs->hv_enlightenments_control.nested_flush_hypercall = 1;
1719 ++
1720 ++ return 0;
1721 ++}
1722 ++
1723 ++#endif /* IS_ENABLED(CONFIG_HYPERV) */
1724 ++
1725 ++/*
1726 ++ * Comment's format: document - errata name - stepping - processor name.
1727 ++ * Refer from
1728 ++ * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1729 ++ */
1730 ++static u32 vmx_preemption_cpu_tfms[] = {
1731 ++/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1732 ++0x000206E6,
1733 ++/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1734 ++/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1735 ++/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1736 ++0x00020652,
1737 ++/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1738 ++0x00020655,
1739 ++/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1740 ++/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1741 ++/*
1742 ++ * 320767.pdf - AAP86 - B1 -
1743 ++ * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1744 ++ */
1745 ++0x000106E5,
1746 ++/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1747 ++0x000106A0,
1748 ++/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1749 ++0x000106A1,
1750 ++/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1751 ++0x000106A4,
1752 ++ /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1753 ++ /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1754 ++ /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1755 ++0x000106A5,
1756 ++ /* Xeon E3-1220 V2 */
1757 ++0x000306A8,
1758 ++};
1759 ++
1760 ++static inline bool cpu_has_broken_vmx_preemption_timer(void)
1761 ++{
1762 ++ u32 eax = cpuid_eax(0x00000001), i;
1763 ++
1764 ++ /* Clear the reserved bits */
1765 ++ eax &= ~(0x3U << 14 | 0xfU << 28);
1766 ++ for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
1767 ++ if (eax == vmx_preemption_cpu_tfms[i])
1768 ++ return true;
1769 ++
1770 ++ return false;
1771 ++}
1772 ++
1773 ++static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
1774 ++{
1775 ++ return flexpriority_enabled && lapic_in_kernel(vcpu);
1776 ++}
1777 ++
1778 ++static inline bool report_flexpriority(void)
1779 ++{
1780 ++ return flexpriority_enabled;
1781 ++}
1782 ++
1783 ++static inline int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
1784 ++{
1785 ++ int i;
1786 ++
1787 ++ for (i = 0; i < vmx->nmsrs; ++i)
1788 ++ if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
1789 ++ return i;
1790 ++ return -1;
1791 ++}
1792 ++
1793 ++struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
1794 ++{
1795 ++ int i;
1796 ++
1797 ++ i = __find_msr_index(vmx, msr);
1798 ++ if (i >= 0)
1799 ++ return &vmx->guest_msrs[i];
1800 ++ return NULL;
1801 ++}
1802 ++
1803 ++static int vmx_set_guest_msr(struct vcpu_vmx *vmx, struct shared_msr_entry *msr, u64 data)
1804 ++{
1805 ++ int ret = 0;
1806 ++
1807 ++ u64 old_msr_data = msr->data;
1808 ++ msr->data = data;
1809 ++ if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
1810 ++ preempt_disable();
1811 ++ ret = kvm_set_shared_msr(msr->index, msr->data,
1812 ++ msr->mask);
1813 ++ preempt_enable();
1814 ++ if (ret)
1815 ++ msr->data = old_msr_data;
1816 ++ }
1817 ++ return ret;
1818 ++}
1819 ++
1820 ++void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1821 ++{
1822 ++ vmcs_clear(loaded_vmcs->vmcs);
1823 ++ if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1824 ++ vmcs_clear(loaded_vmcs->shadow_vmcs);
1825 ++ loaded_vmcs->cpu = -1;
1826 ++ loaded_vmcs->launched = 0;
1827 ++}
1828 ++
1829 ++#ifdef CONFIG_KEXEC_CORE
1830 ++/*
1831 ++ * This bitmap is used to indicate whether the vmclear
1832 ++ * operation is enabled on all cpus. All disabled by
1833 ++ * default.
1834 ++ */
1835 ++static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1836 ++
1837 ++static inline void crash_enable_local_vmclear(int cpu)
1838 ++{
1839 ++ cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1840 ++}
1841 ++
1842 ++static inline void crash_disable_local_vmclear(int cpu)
1843 ++{
1844 ++ cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1845 ++}
1846 ++
1847 ++static inline int crash_local_vmclear_enabled(int cpu)
1848 ++{
1849 ++ return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1850 ++}
1851 ++
1852 ++static void crash_vmclear_local_loaded_vmcss(void)
1853 ++{
1854 ++ int cpu = raw_smp_processor_id();
1855 ++ struct loaded_vmcs *v;
1856 ++
1857 ++ if (!crash_local_vmclear_enabled(cpu))
1858 ++ return;
1859 ++
1860 ++ list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1861 ++ loaded_vmcss_on_cpu_link)
1862 ++ vmcs_clear(v->vmcs);
1863 ++}
1864 ++#else
1865 ++static inline void crash_enable_local_vmclear(int cpu) { }
1866 ++static inline void crash_disable_local_vmclear(int cpu) { }
1867 ++#endif /* CONFIG_KEXEC_CORE */
1868 ++
1869 ++static void __loaded_vmcs_clear(void *arg)
1870 ++{
1871 ++ struct loaded_vmcs *loaded_vmcs = arg;
1872 ++ int cpu = raw_smp_processor_id();
1873 ++
1874 ++ if (loaded_vmcs->cpu != cpu)
1875 ++ return; /* vcpu migration can race with cpu offline */
1876 ++ if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
1877 ++ per_cpu(current_vmcs, cpu) = NULL;
1878 ++ crash_disable_local_vmclear(cpu);
1879 ++ list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
1880 ++
1881 ++ /*
1882 ++ * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1883 ++ * is before setting loaded_vmcs->vcpu to -1 which is done in
1884 ++ * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1885 ++ * then adds the vmcs into percpu list before it is deleted.
1886 ++ */
1887 ++ smp_wmb();
1888 ++
1889 ++ loaded_vmcs_init(loaded_vmcs);
1890 ++ crash_enable_local_vmclear(cpu);
1891 ++}
1892 ++
1893 ++void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
1894 ++{
1895 ++ int cpu = loaded_vmcs->cpu;
1896 ++
1897 ++ if (cpu != -1)
1898 ++ smp_call_function_single(cpu,
1899 ++ __loaded_vmcs_clear, loaded_vmcs, 1);
1900 ++}
1901 ++
1902 ++static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1903 ++ unsigned field)
1904 ++{
1905 ++ bool ret;
1906 ++ u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1907 ++
1908 ++ if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) {
1909 ++ kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS);
1910 ++ vmx->segment_cache.bitmask = 0;
1911 ++ }
1912 ++ ret = vmx->segment_cache.bitmask & mask;
1913 ++ vmx->segment_cache.bitmask |= mask;
1914 ++ return ret;
1915 ++}
1916 ++
1917 ++static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1918 ++{
1919 ++ u16 *p = &vmx->segment_cache.seg[seg].selector;
1920 ++
1921 ++ if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1922 ++ *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1923 ++ return *p;
1924 ++}
1925 ++
1926 ++static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1927 ++{
1928 ++ ulong *p = &vmx->segment_cache.seg[seg].base;
1929 ++
1930 ++ if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1931 ++ *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1932 ++ return *p;
1933 ++}
1934 ++
1935 ++static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1936 ++{
1937 ++ u32 *p = &vmx->segment_cache.seg[seg].limit;
1938 ++
1939 ++ if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1940 ++ *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1941 ++ return *p;
1942 ++}
1943 ++
1944 ++static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1945 ++{
1946 ++ u32 *p = &vmx->segment_cache.seg[seg].ar;
1947 ++
1948 ++ if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1949 ++ *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1950 ++ return *p;
1951 ++}
1952 ++
1953 ++void update_exception_bitmap(struct kvm_vcpu *vcpu)
1954 ++{
1955 ++ u32 eb;
1956 ++
1957 ++ eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1958 ++ (1u << DB_VECTOR) | (1u << AC_VECTOR);
1959 ++ /*
1960 ++ * Guest access to VMware backdoor ports could legitimately
1961 ++ * trigger #GP because of TSS I/O permission bitmap.
1962 ++ * We intercept those #GP and allow access to them anyway
1963 ++ * as VMware does.
1964 ++ */
1965 ++ if (enable_vmware_backdoor)
1966 ++ eb |= (1u << GP_VECTOR);
1967 ++ if ((vcpu->guest_debug &
1968 ++ (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1969 ++ (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1970 ++ eb |= 1u << BP_VECTOR;
1971 ++ if (to_vmx(vcpu)->rmode.vm86_active)
1972 ++ eb = ~0;
1973 ++ if (enable_ept)
1974 ++ eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
1975 ++
1976 ++ /* When we are running a nested L2 guest and L1 specified for it a
1977 ++ * certain exception bitmap, we must trap the same exceptions and pass
1978 ++ * them to L1. When running L2, we will only handle the exceptions
1979 ++ * specified above if L1 did not want them.
1980 ++ */
1981 ++ if (is_guest_mode(vcpu))
1982 ++ eb |= get_vmcs12(vcpu)->exception_bitmap;
1983 ++
1984 ++ vmcs_write32(EXCEPTION_BITMAP, eb);
1985 ++}
1986 ++
1987 ++/*
1988 ++ * Check if MSR is intercepted for currently loaded MSR bitmap.
1989 ++ */
1990 ++static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
1991 ++{
1992 ++ unsigned long *msr_bitmap;
1993 ++ int f = sizeof(unsigned long);
1994 ++
1995 ++ if (!cpu_has_vmx_msr_bitmap())
1996 ++ return true;
1997 ++
1998 ++ msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
1999 ++
2000 ++ if (msr <= 0x1fff) {
2001 ++ return !!test_bit(msr, msr_bitmap + 0x800 / f);
2002 ++ } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2003 ++ msr &= 0x1fff;
2004 ++ return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2005 ++ }
2006 ++
2007 ++ return true;
2008 ++}
2009 ++
2010 ++static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2011 ++ unsigned long entry, unsigned long exit)
2012 ++{
2013 ++ vm_entry_controls_clearbit(vmx, entry);
2014 ++ vm_exit_controls_clearbit(vmx, exit);
2015 ++}
2016 ++
2017 ++int vmx_find_msr_index(struct vmx_msrs *m, u32 msr)
2018 ++{
2019 ++ unsigned int i;
2020 ++
2021 ++ for (i = 0; i < m->nr; ++i) {
2022 ++ if (m->val[i].index == msr)
2023 ++ return i;
2024 ++ }
2025 ++ return -ENOENT;
2026 ++}
2027 ++
2028 ++static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2029 ++{
2030 ++ int i;
2031 ++ struct msr_autoload *m = &vmx->msr_autoload;
2032 ++
2033 ++ switch (msr) {
2034 ++ case MSR_EFER:
2035 ++ if (cpu_has_load_ia32_efer()) {
2036 ++ clear_atomic_switch_msr_special(vmx,
2037 ++ VM_ENTRY_LOAD_IA32_EFER,
2038 ++ VM_EXIT_LOAD_IA32_EFER);
2039 ++ return;
2040 ++ }
2041 ++ break;
2042 ++ case MSR_CORE_PERF_GLOBAL_CTRL:
2043 ++ if (cpu_has_load_perf_global_ctrl()) {
2044 ++ clear_atomic_switch_msr_special(vmx,
2045 ++ VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2046 ++ VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2047 ++ return;
2048 ++ }
2049 ++ break;
2050 ++ }
2051 ++ i = vmx_find_msr_index(&m->guest, msr);
2052 ++ if (i < 0)
2053 ++ goto skip_guest;
2054 ++ --m->guest.nr;
2055 ++ m->guest.val[i] = m->guest.val[m->guest.nr];
2056 ++ vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2057 ++
2058 ++skip_guest:
2059 ++ i = vmx_find_msr_index(&m->host, msr);
2060 ++ if (i < 0)
2061 ++ return;
2062 ++
2063 ++ --m->host.nr;
2064 ++ m->host.val[i] = m->host.val[m->host.nr];
2065 ++ vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
2066 ++}
2067 ++
2068 ++static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2069 ++ unsigned long entry, unsigned long exit,
2070 ++ unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2071 ++ u64 guest_val, u64 host_val)
2072 ++{
2073 ++ vmcs_write64(guest_val_vmcs, guest_val);
2074 ++ if (host_val_vmcs != HOST_IA32_EFER)
2075 ++ vmcs_write64(host_val_vmcs, host_val);
2076 ++ vm_entry_controls_setbit(vmx, entry);
2077 ++ vm_exit_controls_setbit(vmx, exit);
2078 ++}
2079 ++
2080 ++static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2081 ++ u64 guest_val, u64 host_val, bool entry_only)
2082 ++{
2083 ++ int i, j = 0;
2084 ++ struct msr_autoload *m = &vmx->msr_autoload;
2085 ++
2086 ++ switch (msr) {
2087 ++ case MSR_EFER:
2088 ++ if (cpu_has_load_ia32_efer()) {
2089 ++ add_atomic_switch_msr_special(vmx,
2090 ++ VM_ENTRY_LOAD_IA32_EFER,
2091 ++ VM_EXIT_LOAD_IA32_EFER,
2092 ++ GUEST_IA32_EFER,
2093 ++ HOST_IA32_EFER,
2094 ++ guest_val, host_val);
2095 ++ return;
2096 ++ }
2097 ++ break;
2098 ++ case MSR_CORE_PERF_GLOBAL_CTRL:
2099 ++ if (cpu_has_load_perf_global_ctrl()) {
2100 ++ add_atomic_switch_msr_special(vmx,
2101 ++ VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2102 ++ VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2103 ++ GUEST_IA32_PERF_GLOBAL_CTRL,
2104 ++ HOST_IA32_PERF_GLOBAL_CTRL,
2105 ++ guest_val, host_val);
2106 ++ return;
2107 ++ }
2108 ++ break;
2109 ++ case MSR_IA32_PEBS_ENABLE:
2110 ++ /* PEBS needs a quiescent period after being disabled (to write
2111 ++ * a record). Disabling PEBS through VMX MSR swapping doesn't
2112 ++ * provide that period, so a CPU could write host's record into
2113 ++ * guest's memory.
2114 ++ */
2115 ++ wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
2116 ++ }
2117 ++
2118 ++ i = vmx_find_msr_index(&m->guest, msr);
2119 ++ if (!entry_only)
2120 ++ j = vmx_find_msr_index(&m->host, msr);
2121 ++
2122 ++ if ((i < 0 && m->guest.nr == NR_LOADSTORE_MSRS) ||
2123 ++ (j < 0 && m->host.nr == NR_LOADSTORE_MSRS)) {
2124 ++ printk_once(KERN_WARNING "Not enough msr switch entries. "
2125 ++ "Can't add msr %x\n", msr);
2126 ++ return;
2127 ++ }
2128 ++ if (i < 0) {
2129 ++ i = m->guest.nr++;
2130 ++ vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2131 ++ }
2132 ++ m->guest.val[i].index = msr;
2133 ++ m->guest.val[i].value = guest_val;
2134 ++
2135 ++ if (entry_only)
2136 ++ return;
2137 ++
2138 ++ if (j < 0) {
2139 ++ j = m->host.nr++;
2140 ++ vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
2141 ++ }
2142 ++ m->host.val[j].index = msr;
2143 ++ m->host.val[j].value = host_val;
2144 ++}
2145 ++
2146 ++static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2147 ++{
2148 ++ u64 guest_efer = vmx->vcpu.arch.efer;
2149 ++ u64 ignore_bits = 0;
2150 ++
2151 ++ /* Shadow paging assumes NX to be available. */
2152 ++ if (!enable_ept)
2153 ++ guest_efer |= EFER_NX;
2154 ++
2155 ++ /*
2156 ++ * LMA and LME handled by hardware; SCE meaningless outside long mode.
2157 ++ */
2158 ++ ignore_bits |= EFER_SCE;
2159 ++#ifdef CONFIG_X86_64
2160 ++ ignore_bits |= EFER_LMA | EFER_LME;
2161 ++ /* SCE is meaningful only in long mode on Intel */
2162 ++ if (guest_efer & EFER_LMA)
2163 ++ ignore_bits &= ~(u64)EFER_SCE;
2164 ++#endif
2165 ++
2166 ++ /*
2167 ++ * On EPT, we can't emulate NX, so we must switch EFER atomically.
2168 ++ * On CPUs that support "load IA32_EFER", always switch EFER
2169 ++ * atomically, since it's faster than switching it manually.
2170 ++ */
2171 ++ if (cpu_has_load_ia32_efer() ||
2172 ++ (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
2173 ++ if (!(guest_efer & EFER_LMA))
2174 ++ guest_efer &= ~EFER_LME;
2175 ++ if (guest_efer != host_efer)
2176 ++ add_atomic_switch_msr(vmx, MSR_EFER,
2177 ++ guest_efer, host_efer, false);
2178 ++ else
2179 ++ clear_atomic_switch_msr(vmx, MSR_EFER);
2180 ++ return false;
2181 ++ } else {
2182 ++ clear_atomic_switch_msr(vmx, MSR_EFER);
2183 ++
2184 ++ guest_efer &= ~ignore_bits;
2185 ++ guest_efer |= host_efer & ignore_bits;
2186 ++
2187 ++ vmx->guest_msrs[efer_offset].data = guest_efer;
2188 ++ vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2189 ++
2190 ++ return true;
2191 ++ }
2192 ++}
2193 ++
2194 ++#ifdef CONFIG_X86_32
2195 ++/*
2196 ++ * On 32-bit kernels, VM exits still load the FS and GS bases from the
2197 ++ * VMCS rather than the segment table. KVM uses this helper to figure
2198 ++ * out the current bases to poke them into the VMCS before entry.
2199 ++ */
2200 ++static unsigned long segment_base(u16 selector)
2201 ++{
2202 ++ struct desc_struct *table;
2203 ++ unsigned long v;
2204 ++
2205 ++ if (!(selector & ~SEGMENT_RPL_MASK))
2206 ++ return 0;
2207 ++
2208 ++ table = get_current_gdt_ro();
2209 ++
2210 ++ if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
2211 ++ u16 ldt_selector = kvm_read_ldt();
2212 ++
2213 ++ if (!(ldt_selector & ~SEGMENT_RPL_MASK))
2214 ++ return 0;
2215 ++
2216 ++ table = (struct desc_struct *)segment_base(ldt_selector);
2217 ++ }
2218 ++ v = get_desc_base(&table[selector >> 3]);
2219 ++ return v;
2220 ++}
2221 ++#endif
2222 ++
2223 ++static inline void pt_load_msr(struct pt_ctx *ctx, u32 addr_range)
2224 ++{
2225 ++ u32 i;
2226 ++
2227 ++ wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
2228 ++ wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
2229 ++ wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
2230 ++ wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
2231 ++ for (i = 0; i < addr_range; i++) {
2232 ++ wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
2233 ++ wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
2234 ++ }
2235 ++}
2236 ++
2237 ++static inline void pt_save_msr(struct pt_ctx *ctx, u32 addr_range)
2238 ++{
2239 ++ u32 i;
2240 ++
2241 ++ rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
2242 ++ rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
2243 ++ rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
2244 ++ rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
2245 ++ for (i = 0; i < addr_range; i++) {
2246 ++ rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
2247 ++ rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
2248 ++ }
2249 ++}
2250 ++
2251 ++static void pt_guest_enter(struct vcpu_vmx *vmx)
2252 ++{
2253 ++ if (pt_mode == PT_MODE_SYSTEM)
2254 ++ return;
2255 ++
2256 ++ /*
2257 ++ * GUEST_IA32_RTIT_CTL is already set in the VMCS.
2258 ++ * Save host state before VM entry.
2259 ++ */
2260 ++ rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
2261 ++ if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
2262 ++ wrmsrl(MSR_IA32_RTIT_CTL, 0);
2263 ++ pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
2264 ++ pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
2265 ++ }
2266 ++}
2267 ++
2268 ++static void pt_guest_exit(struct vcpu_vmx *vmx)
2269 ++{
2270 ++ if (pt_mode == PT_MODE_SYSTEM)
2271 ++ return;
2272 ++
2273 ++ if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
2274 ++ pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
2275 ++ pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
2276 ++ }
2277 ++
2278 ++ /* Reload host state (IA32_RTIT_CTL will be cleared on VM exit). */
2279 ++ wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
2280 ++}
2281 ++
2282 ++void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
2283 ++ unsigned long fs_base, unsigned long gs_base)
2284 ++{
2285 ++ if (unlikely(fs_sel != host->fs_sel)) {
2286 ++ if (!(fs_sel & 7))
2287 ++ vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2288 ++ else
2289 ++ vmcs_write16(HOST_FS_SELECTOR, 0);
2290 ++ host->fs_sel = fs_sel;
2291 ++ }
2292 ++ if (unlikely(gs_sel != host->gs_sel)) {
2293 ++ if (!(gs_sel & 7))
2294 ++ vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2295 ++ else
2296 ++ vmcs_write16(HOST_GS_SELECTOR, 0);
2297 ++ host->gs_sel = gs_sel;
2298 ++ }
2299 ++ if (unlikely(fs_base != host->fs_base)) {
2300 ++ vmcs_writel(HOST_FS_BASE, fs_base);
2301 ++ host->fs_base = fs_base;
2302 ++ }
2303 ++ if (unlikely(gs_base != host->gs_base)) {
2304 ++ vmcs_writel(HOST_GS_BASE, gs_base);
2305 ++ host->gs_base = gs_base;
2306 ++ }
2307 ++}
2308 ++
2309 ++void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
2310 ++{
2311 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
2312 ++ struct vmcs_host_state *host_state;
2313 ++#ifdef CONFIG_X86_64
2314 ++ int cpu = raw_smp_processor_id();
2315 ++#endif
2316 ++ unsigned long fs_base, gs_base;
2317 ++ u16 fs_sel, gs_sel;
2318 ++ int i;
2319 ++
2320 ++ vmx->req_immediate_exit = false;
2321 ++
2322 ++ /*
2323 ++ * Note that guest MSRs to be saved/restored can also be changed
2324 ++ * when guest state is loaded. This happens when guest transitions
2325 ++ * to/from long-mode by setting MSR_EFER.LMA.
2326 ++ */
2327 ++ if (!vmx->guest_msrs_ready) {
2328 ++ vmx->guest_msrs_ready = true;
2329 ++ for (i = 0; i < vmx->save_nmsrs; ++i)
2330 ++ kvm_set_shared_msr(vmx->guest_msrs[i].index,
2331 ++ vmx->guest_msrs[i].data,
2332 ++ vmx->guest_msrs[i].mask);
2333 ++
2334 ++ }
2335 ++ if (vmx->guest_state_loaded)
2336 ++ return;
2337 ++
2338 ++ host_state = &vmx->loaded_vmcs->host_state;
2339 ++
2340 ++ /*
2341 ++ * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2342 ++ * allow segment selectors with cpl > 0 or ti == 1.
2343 ++ */
2344 ++ host_state->ldt_sel = kvm_read_ldt();
2345 ++
2346 ++#ifdef CONFIG_X86_64
2347 ++ savesegment(ds, host_state->ds_sel);
2348 ++ savesegment(es, host_state->es_sel);
2349 ++
2350 ++ gs_base = cpu_kernelmode_gs_base(cpu);
2351 ++ if (likely(is_64bit_mm(current->mm))) {
2352 ++ save_fsgs_for_kvm();
2353 ++ fs_sel = current->thread.fsindex;
2354 ++ gs_sel = current->thread.gsindex;
2355 ++ fs_base = current->thread.fsbase;
2356 ++ vmx->msr_host_kernel_gs_base = current->thread.gsbase;
2357 ++ } else {
2358 ++ savesegment(fs, fs_sel);
2359 ++ savesegment(gs, gs_sel);
2360 ++ fs_base = read_msr(MSR_FS_BASE);
2361 ++ vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2362 ++ }
2363 ++
2364 ++ wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2365 ++#else
2366 ++ savesegment(fs, fs_sel);
2367 ++ savesegment(gs, gs_sel);
2368 ++ fs_base = segment_base(fs_sel);
2369 ++ gs_base = segment_base(gs_sel);
2370 ++#endif
2371 ++
2372 ++ vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
2373 ++ vmx->guest_state_loaded = true;
2374 ++}
2375 ++
2376 ++static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
2377 ++{
2378 ++ struct vmcs_host_state *host_state;
2379 ++
2380 ++ if (!vmx->guest_state_loaded)
2381 ++ return;
2382 ++
2383 ++ host_state = &vmx->loaded_vmcs->host_state;
2384 ++
2385 ++ ++vmx->vcpu.stat.host_state_reload;
2386 ++
2387 ++#ifdef CONFIG_X86_64
2388 ++ rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2389 ++#endif
2390 ++ if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2391 ++ kvm_load_ldt(host_state->ldt_sel);
2392 ++#ifdef CONFIG_X86_64
2393 ++ load_gs_index(host_state->gs_sel);
2394 ++#else
2395 ++ loadsegment(gs, host_state->gs_sel);
2396 ++#endif
2397 ++ }
2398 ++ if (host_state->fs_sel & 7)
2399 ++ loadsegment(fs, host_state->fs_sel);
2400 ++#ifdef CONFIG_X86_64
2401 ++ if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2402 ++ loadsegment(ds, host_state->ds_sel);
2403 ++ loadsegment(es, host_state->es_sel);
2404 ++ }
2405 ++#endif
2406 ++ invalidate_tss_limit();
2407 ++#ifdef CONFIG_X86_64
2408 ++ wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2409 ++#endif
2410 ++ load_fixmap_gdt(raw_smp_processor_id());
2411 ++ vmx->guest_state_loaded = false;
2412 ++ vmx->guest_msrs_ready = false;
2413 ++}
2414 ++
2415 ++#ifdef CONFIG_X86_64
2416 ++static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
2417 ++{
2418 ++ preempt_disable();
2419 ++ if (vmx->guest_state_loaded)
2420 ++ rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2421 ++ preempt_enable();
2422 ++ return vmx->msr_guest_kernel_gs_base;
2423 ++}
2424 ++
2425 ++static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2426 ++{
2427 ++ preempt_disable();
2428 ++ if (vmx->guest_state_loaded)
2429 ++ wrmsrl(MSR_KERNEL_GS_BASE, data);
2430 ++ preempt_enable();
2431 ++ vmx->msr_guest_kernel_gs_base = data;
2432 ++}
2433 ++#endif
2434 ++
2435 ++static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2436 ++{
2437 ++ struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2438 ++ struct pi_desc old, new;
2439 ++ unsigned int dest;
2440 ++
2441 ++ /*
2442 ++ * In case of hot-plug or hot-unplug, we may have to undo
2443 ++ * vmx_vcpu_pi_put even if there is no assigned device. And we
2444 ++ * always keep PI.NDST up to date for simplicity: it makes the
2445 ++ * code easier, and CPU migration is not a fast path.
2446 ++ */
2447 ++ if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
2448 ++ return;
2449 ++
2450 ++ /*
2451 ++ * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2452 ++ * PI.NDST: pi_post_block is the one expected to change PID.NDST and the
2453 ++ * wakeup handler expects the vCPU to be on the blocked_vcpu_list that
2454 ++ * matches PI.NDST. Otherwise, a vcpu may not be able to be woken up
2455 ++ * correctly.
2456 ++ */
2457 ++ if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || vcpu->cpu == cpu) {
2458 ++ pi_clear_sn(pi_desc);
2459 ++ goto after_clear_sn;
2460 ++ }
2461 ++
2462 ++ /* The full case. */
2463 ++ do {
2464 ++ old.control = new.control = pi_desc->control;
2465 ++
2466 ++ dest = cpu_physical_id(cpu);
2467 ++
2468 ++ if (x2apic_enabled())
2469 ++ new.ndst = dest;
2470 ++ else
2471 ++ new.ndst = (dest << 8) & 0xFF00;
2472 ++
2473 ++ new.sn = 0;
2474 ++ } while (cmpxchg64(&pi_desc->control, old.control,
2475 ++ new.control) != old.control);
2476 ++
2477 ++after_clear_sn:
2478 ++
2479 ++ /*
2480 ++ * Clear SN before reading the bitmap. The VT-d firmware
2481 ++ * writes the bitmap and reads SN atomically (5.2.3 in the
2482 ++ * spec), so it doesn't really have a memory barrier that
2483 ++ * pairs with this, but we cannot do that and we need one.
2484 ++ */
2485 ++ smp_mb__after_atomic();
2486 ++
2487 ++ if (!pi_is_pir_empty(pi_desc))
2488 ++ pi_set_on(pi_desc);
2489 ++}
2490 ++
2491 ++void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu)
2492 ++{
2493 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
2494 ++ bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
2495 ++
2496 ++ if (!already_loaded) {
2497 ++ loaded_vmcs_clear(vmx->loaded_vmcs);
2498 ++ local_irq_disable();
2499 ++ crash_disable_local_vmclear(cpu);
2500 ++
2501 ++ /*
2502 ++ * Read loaded_vmcs->cpu should be before fetching
2503 ++ * loaded_vmcs->loaded_vmcss_on_cpu_link.
2504 ++ * See the comments in __loaded_vmcs_clear().
2505 ++ */
2506 ++ smp_rmb();
2507 ++
2508 ++ list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2509 ++ &per_cpu(loaded_vmcss_on_cpu, cpu));
2510 ++ crash_enable_local_vmclear(cpu);
2511 ++ local_irq_enable();
2512 ++ }
2513 ++
2514 ++ if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2515 ++ per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2516 ++ vmcs_load(vmx->loaded_vmcs->vmcs);
2517 ++ indirect_branch_prediction_barrier();
2518 ++ }
2519 ++
2520 ++ if (!already_loaded) {
2521 ++ void *gdt = get_current_gdt_ro();
2522 ++ unsigned long sysenter_esp;
2523 ++
2524 ++ kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2525 ++
2526 ++ /*
2527 ++ * Linux uses per-cpu TSS and GDT, so set these when switching
2528 ++ * processors. See 22.2.4.
2529 ++ */
2530 ++ vmcs_writel(HOST_TR_BASE,
2531 ++ (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
2532 ++ vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
2533 ++
2534 ++ rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2535 ++ vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
2536 ++
2537 ++ vmx->loaded_vmcs->cpu = cpu;
2538 ++ }
2539 ++
2540 ++ /* Setup TSC multiplier */
2541 ++ if (kvm_has_tsc_control &&
2542 ++ vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2543 ++ decache_tsc_multiplier(vmx);
2544 ++}
2545 ++
2546 ++/*
2547 ++ * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2548 ++ * vcpu mutex is already taken.
2549 ++ */
2550 ++void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2551 ++{
2552 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
2553 ++
2554 ++ vmx_vcpu_load_vmcs(vcpu, cpu);
2555 ++
2556 ++ vmx_vcpu_pi_load(vcpu, cpu);
2557 ++
2558 ++ vmx->host_pkru = read_pkru();
2559 ++ vmx->host_debugctlmsr = get_debugctlmsr();
2560 ++}
2561 ++
2562 ++static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2563 ++{
2564 ++ struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2565 ++
2566 ++ if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
2567 ++ !irq_remapping_cap(IRQ_POSTING_CAP) ||
2568 ++ !kvm_vcpu_apicv_active(vcpu))
2569 ++ return;
2570 ++
2571 ++ /* Set SN when the vCPU is preempted */
2572 ++ if (vcpu->preempted)
2573 ++ pi_set_sn(pi_desc);
2574 ++}
2575 ++
2576 ++static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2577 ++{
2578 ++ vmx_vcpu_pi_put(vcpu);
2579 ++
2580 ++ vmx_prepare_switch_to_host(to_vmx(vcpu));
2581 ++}
2582 ++
2583 ++static bool emulation_required(struct kvm_vcpu *vcpu)
2584 ++{
2585 ++ return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2586 ++}
2587 ++
2588 ++static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2589 ++
2590 ++unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2591 ++{
2592 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
2593 ++ unsigned long rflags, save_rflags;
2594 ++
2595 ++ if (!kvm_register_is_available(vcpu, VCPU_EXREG_RFLAGS)) {
2596 ++ kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
2597 ++ rflags = vmcs_readl(GUEST_RFLAGS);
2598 ++ if (vmx->rmode.vm86_active) {
2599 ++ rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2600 ++ save_rflags = vmx->rmode.save_rflags;
2601 ++ rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2602 ++ }
2603 ++ vmx->rflags = rflags;
2604 ++ }
2605 ++ return vmx->rflags;
2606 ++}
2607 ++
2608 ++void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2609 ++{
2610 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
2611 ++ unsigned long old_rflags;
2612 ++
2613 ++ if (enable_unrestricted_guest) {
2614 ++ kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
2615 ++ vmx->rflags = rflags;
2616 ++ vmcs_writel(GUEST_RFLAGS, rflags);
2617 ++ return;
2618 ++ }
2619 ++
2620 ++ old_rflags = vmx_get_rflags(vcpu);
2621 ++ vmx->rflags = rflags;
2622 ++ if (vmx->rmode.vm86_active) {
2623 ++ vmx->rmode.save_rflags = rflags;
2624 ++ rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
2625 ++ }
2626 ++ vmcs_writel(GUEST_RFLAGS, rflags);
2627 ++
2628 ++ if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM)
2629 ++ vmx->emulation_required = emulation_required(vcpu);
2630 ++}
2631 ++
2632 ++u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
2633 ++{
2634 ++ u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2635 ++ int ret = 0;
2636 ++
2637 ++ if (interruptibility & GUEST_INTR_STATE_STI)
2638 ++ ret |= KVM_X86_SHADOW_INT_STI;
2639 ++ if (interruptibility & GUEST_INTR_STATE_MOV_SS)
2640 ++ ret |= KVM_X86_SHADOW_INT_MOV_SS;
2641 ++
2642 ++ return ret;
2643 ++}
2644 ++
2645 ++void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2646 ++{
2647 ++ u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2648 ++ u32 interruptibility = interruptibility_old;
2649 ++
2650 ++ interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2651 ++
2652 ++ if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2653 ++ interruptibility |= GUEST_INTR_STATE_MOV_SS;
2654 ++ else if (mask & KVM_X86_SHADOW_INT_STI)
2655 ++ interruptibility |= GUEST_INTR_STATE_STI;
2656 ++
2657 ++ if ((interruptibility != interruptibility_old))
2658 ++ vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2659 ++}
2660 ++
2661 ++static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data)
2662 ++{
2663 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
2664 ++ unsigned long value;
2665 ++
2666 ++ /*
2667 ++ * Any MSR write that attempts to change bits marked reserved will
2668 ++ * case a #GP fault.
2669 ++ */
2670 ++ if (data & vmx->pt_desc.ctl_bitmask)
2671 ++ return 1;
2672 ++
2673 ++ /*
2674 ++ * Any attempt to modify IA32_RTIT_CTL while TraceEn is set will
2675 ++ * result in a #GP unless the same write also clears TraceEn.
2676 ++ */
2677 ++ if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) &&
2678 ++ ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN))
2679 ++ return 1;
2680 ++
2681 ++ /*
2682 ++ * WRMSR to IA32_RTIT_CTL that sets TraceEn but clears this bit
2683 ++ * and FabricEn would cause #GP, if
2684 ++ * CPUID.(EAX=14H, ECX=0):ECX.SNGLRGNOUT[bit 2] = 0
2685 ++ */
2686 ++ if ((data & RTIT_CTL_TRACEEN) && !(data & RTIT_CTL_TOPA) &&
2687 ++ !(data & RTIT_CTL_FABRIC_EN) &&
2688 ++ !intel_pt_validate_cap(vmx->pt_desc.caps,
2689 ++ PT_CAP_single_range_output))
2690 ++ return 1;
2691 ++
2692 ++ /*
2693 ++ * MTCFreq, CycThresh and PSBFreq encodings check, any MSR write that
2694 ++ * utilize encodings marked reserved will casue a #GP fault.
2695 ++ */
2696 ++ value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods);
2697 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) &&
2698 ++ !test_bit((data & RTIT_CTL_MTC_RANGE) >>
2699 ++ RTIT_CTL_MTC_RANGE_OFFSET, &value))
2700 ++ return 1;
2701 ++ value = intel_pt_validate_cap(vmx->pt_desc.caps,
2702 ++ PT_CAP_cycle_thresholds);
2703 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
2704 ++ !test_bit((data & RTIT_CTL_CYC_THRESH) >>
2705 ++ RTIT_CTL_CYC_THRESH_OFFSET, &value))
2706 ++ return 1;
2707 ++ value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods);
2708 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
2709 ++ !test_bit((data & RTIT_CTL_PSB_FREQ) >>
2710 ++ RTIT_CTL_PSB_FREQ_OFFSET, &value))
2711 ++ return 1;
2712 ++
2713 ++ /*
2714 ++ * If ADDRx_CFG is reserved or the encodings is >2 will
2715 ++ * cause a #GP fault.
2716 ++ */
2717 ++ value = (data & RTIT_CTL_ADDR0) >> RTIT_CTL_ADDR0_OFFSET;
2718 ++ if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2))
2719 ++ return 1;
2720 ++ value = (data & RTIT_CTL_ADDR1) >> RTIT_CTL_ADDR1_OFFSET;
2721 ++ if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2))
2722 ++ return 1;
2723 ++ value = (data & RTIT_CTL_ADDR2) >> RTIT_CTL_ADDR2_OFFSET;
2724 ++ if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2))
2725 ++ return 1;
2726 ++ value = (data & RTIT_CTL_ADDR3) >> RTIT_CTL_ADDR3_OFFSET;
2727 ++ if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2))
2728 ++ return 1;
2729 ++
2730 ++ return 0;
2731 ++}
2732 ++
2733 ++static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
2734 ++{
2735 ++ unsigned long rip;
2736 ++
2737 ++ /*
2738 ++ * Using VMCS.VM_EXIT_INSTRUCTION_LEN on EPT misconfig depends on
2739 ++ * undefined behavior: Intel's SDM doesn't mandate the VMCS field be
2740 ++ * set when EPT misconfig occurs. In practice, real hardware updates
2741 ++ * VM_EXIT_INSTRUCTION_LEN on EPT misconfig, but other hypervisors
2742 ++ * (namely Hyper-V) don't set it due to it being undefined behavior,
2743 ++ * i.e. we end up advancing IP with some random value.
2744 ++ */
2745 ++ if (!static_cpu_has(X86_FEATURE_HYPERVISOR) ||
2746 ++ to_vmx(vcpu)->exit_reason != EXIT_REASON_EPT_MISCONFIG) {
2747 ++ rip = kvm_rip_read(vcpu);
2748 ++ rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
2749 ++ kvm_rip_write(vcpu, rip);
2750 ++ } else {
2751 ++ if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
2752 ++ return 0;
2753 ++ }
2754 ++
2755 ++ /* skipping an emulated instruction also counts */
2756 ++ vmx_set_interrupt_shadow(vcpu, 0);
2757 ++
2758 ++ return 1;
2759 ++}
2760 ++
2761 ++static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2762 ++{
2763 ++ /*
2764 ++ * Ensure that we clear the HLT state in the VMCS. We don't need to
2765 ++ * explicitly skip the instruction because if the HLT state is set,
2766 ++ * then the instruction is already executing and RIP has already been
2767 ++ * advanced.
2768 ++ */
2769 ++ if (kvm_hlt_in_guest(vcpu->kvm) &&
2770 ++ vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2771 ++ vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2772 ++}
2773 ++
2774 ++static void vmx_queue_exception(struct kvm_vcpu *vcpu)
2775 ++{
2776 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
2777 ++ unsigned nr = vcpu->arch.exception.nr;
2778 ++ bool has_error_code = vcpu->arch.exception.has_error_code;
2779 ++ u32 error_code = vcpu->arch.exception.error_code;
2780 ++ u32 intr_info = nr | INTR_INFO_VALID_MASK;
2781 ++
2782 ++ kvm_deliver_exception_payload(vcpu);
2783 ++
2784 ++ if (has_error_code) {
2785 ++ vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
2786 ++ intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2787 ++ }
2788 ++
2789 ++ if (vmx->rmode.vm86_active) {
2790 ++ int inc_eip = 0;
2791 ++ if (kvm_exception_is_soft(nr))
2792 ++ inc_eip = vcpu->arch.event_exit_inst_len;
2793 ++ kvm_inject_realmode_interrupt(vcpu, nr, inc_eip);
2794 ++ return;
2795 ++ }
2796 ++
2797 ++ WARN_ON_ONCE(vmx->emulation_required);
2798 ++
2799 ++ if (kvm_exception_is_soft(nr)) {
2800 ++ vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2801 ++ vmx->vcpu.arch.event_exit_inst_len);
2802 ++ intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2803 ++ } else
2804 ++ intr_info |= INTR_TYPE_HARD_EXCEPTION;
2805 ++
2806 ++ vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
2807 ++
2808 ++ vmx_clear_hlt(vcpu);
2809 ++}
2810 ++
2811 ++static bool vmx_rdtscp_supported(void)
2812 ++{
2813 ++ return cpu_has_vmx_rdtscp();
2814 ++}
2815 ++
2816 ++static bool vmx_invpcid_supported(void)
2817 ++{
2818 ++ return cpu_has_vmx_invpcid();
2819 ++}
2820 ++
2821 ++/*
2822 ++ * Swap MSR entry in host/guest MSR entry array.
2823 ++ */
2824 ++static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
2825 ++{
2826 ++ struct shared_msr_entry tmp;
2827 ++
2828 ++ tmp = vmx->guest_msrs[to];
2829 ++ vmx->guest_msrs[to] = vmx->guest_msrs[from];
2830 ++ vmx->guest_msrs[from] = tmp;
2831 ++}
2832 ++
2833 ++/*
2834 ++ * Set up the vmcs to automatically save and restore system
2835 ++ * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2836 ++ * mode, as fiddling with msrs is very expensive.
2837 ++ */
2838 ++static void setup_msrs(struct vcpu_vmx *vmx)
2839 ++{
2840 ++ int save_nmsrs, index;
2841 ++
2842 ++ save_nmsrs = 0;
2843 ++#ifdef CONFIG_X86_64
2844 ++ /*
2845 ++ * The SYSCALL MSRs are only needed on long mode guests, and only
2846 ++ * when EFER.SCE is set.
2847 ++ */
2848 ++ if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) {
2849 ++ index = __find_msr_index(vmx, MSR_STAR);
2850 ++ if (index >= 0)
2851 ++ move_msr_up(vmx, index, save_nmsrs++);
2852 ++ index = __find_msr_index(vmx, MSR_LSTAR);
2853 ++ if (index >= 0)
2854 ++ move_msr_up(vmx, index, save_nmsrs++);
2855 ++ index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
2856 ++ if (index >= 0)
2857 ++ move_msr_up(vmx, index, save_nmsrs++);
2858 ++ }
2859 ++#endif
2860 ++ index = __find_msr_index(vmx, MSR_EFER);
2861 ++ if (index >= 0 && update_transition_efer(vmx, index))
2862 ++ move_msr_up(vmx, index, save_nmsrs++);
2863 ++ index = __find_msr_index(vmx, MSR_TSC_AUX);
2864 ++ if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
2865 ++ move_msr_up(vmx, index, save_nmsrs++);
2866 ++ index = __find_msr_index(vmx, MSR_IA32_TSX_CTRL);
2867 ++ if (index >= 0)
2868 ++ move_msr_up(vmx, index, save_nmsrs++);
2869 ++
2870 ++ vmx->save_nmsrs = save_nmsrs;
2871 ++ vmx->guest_msrs_ready = false;
2872 ++
2873 ++ if (cpu_has_vmx_msr_bitmap())
2874 ++ vmx_update_msr_bitmap(&vmx->vcpu);
2875 ++}
2876 ++
2877 ++static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
2878 ++{
2879 ++ struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2880 ++
2881 ++ if (is_guest_mode(vcpu) &&
2882 ++ (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING))
2883 ++ return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
2884 ++
2885 ++ return vcpu->arch.tsc_offset;
2886 ++}
2887 ++
2888 ++static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
2889 ++{
2890 ++ struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2891 ++ u64 g_tsc_offset = 0;
2892 ++
2893 ++ /*
2894 ++ * We're here if L1 chose not to trap WRMSR to TSC. According
2895 ++ * to the spec, this should set L1's TSC; The offset that L1
2896 ++ * set for L2 remains unchanged, and still needs to be added
2897 ++ * to the newly set TSC to get L2's TSC.
2898 ++ */
2899 ++ if (is_guest_mode(vcpu) &&
2900 ++ (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING))
2901 ++ g_tsc_offset = vmcs12->tsc_offset;
2902 ++
2903 ++ trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2904 ++ vcpu->arch.tsc_offset - g_tsc_offset,
2905 ++ offset);
2906 ++ vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
2907 ++ return offset + g_tsc_offset;
2908 ++}
2909 ++
2910 ++/*
2911 ++ * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2912 ++ * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2913 ++ * all guests if the "nested" module option is off, and can also be disabled
2914 ++ * for a single guest by disabling its VMX cpuid bit.
2915 ++ */
2916 ++bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2917 ++{
2918 ++ return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
2919 ++}
2920 ++
2921 ++static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2922 ++ uint64_t val)
2923 ++{
2924 ++ uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2925 ++
2926 ++ return !(val & ~valid_bits);
2927 ++}
2928 ++
2929 ++static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
2930 ++{
2931 ++ switch (msr->index) {
2932 ++ case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2933 ++ if (!nested)
2934 ++ return 1;
2935 ++ return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
2936 ++ default:
2937 ++ return 1;
2938 ++ }
2939 ++}
2940 ++
2941 ++/*
2942 ++ * Reads an msr value (of 'msr_index') into 'pdata'.
2943 ++ * Returns 0 on success, non-0 otherwise.
2944 ++ * Assumes vcpu_load() was already called.
2945 ++ */
2946 ++static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
2947 ++{
2948 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
2949 ++ struct shared_msr_entry *msr;
2950 ++ u32 index;
2951 ++
2952 ++ switch (msr_info->index) {
2953 ++#ifdef CONFIG_X86_64
2954 ++ case MSR_FS_BASE:
2955 ++ msr_info->data = vmcs_readl(GUEST_FS_BASE);
2956 ++ break;
2957 ++ case MSR_GS_BASE:
2958 ++ msr_info->data = vmcs_readl(GUEST_GS_BASE);
2959 ++ break;
2960 ++ case MSR_KERNEL_GS_BASE:
2961 ++ msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
2962 ++ break;
2963 ++#endif
2964 ++ case MSR_EFER:
2965 ++ return kvm_get_msr_common(vcpu, msr_info);
2966 ++ case MSR_IA32_TSX_CTRL:
2967 ++ if (!msr_info->host_initiated &&
2968 ++ !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
2969 ++ return 1;
2970 ++ goto find_shared_msr;
2971 ++ case MSR_IA32_UMWAIT_CONTROL:
2972 ++ if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
2973 ++ return 1;
2974 ++
2975 ++ msr_info->data = vmx->msr_ia32_umwait_control;
2976 ++ break;
2977 ++ case MSR_IA32_SPEC_CTRL:
2978 ++ if (!msr_info->host_initiated &&
2979 ++ !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2980 ++ return 1;
2981 ++
2982 ++ msr_info->data = to_vmx(vcpu)->spec_ctrl;
2983 ++ break;
2984 ++ case MSR_IA32_SYSENTER_CS:
2985 ++ msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
2986 ++ break;
2987 ++ case MSR_IA32_SYSENTER_EIP:
2988 ++ msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
2989 ++ break;
2990 ++ case MSR_IA32_SYSENTER_ESP:
2991 ++ msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
2992 ++ break;
2993 ++ case MSR_IA32_BNDCFGS:
2994 ++ if (!kvm_mpx_supported() ||
2995 ++ (!msr_info->host_initiated &&
2996 ++ !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
2997 ++ return 1;
2998 ++ msr_info->data = vmcs_read64(GUEST_BNDCFGS);
2999 ++ break;
3000 ++ case MSR_IA32_MCG_EXT_CTL:
3001 ++ if (!msr_info->host_initiated &&
3002 ++ !(vmx->msr_ia32_feature_control &
3003 ++ FEATURE_CONTROL_LMCE))
3004 ++ return 1;
3005 ++ msr_info->data = vcpu->arch.mcg_ext_ctl;
3006 ++ break;
3007 ++ case MSR_IA32_FEATURE_CONTROL:
3008 ++ msr_info->data = vmx->msr_ia32_feature_control;
3009 ++ break;
3010 ++ case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3011 ++ if (!nested_vmx_allowed(vcpu))
3012 ++ return 1;
3013 ++ return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3014 ++ &msr_info->data);
3015 ++ case MSR_IA32_RTIT_CTL:
3016 ++ if (pt_mode != PT_MODE_HOST_GUEST)
3017 ++ return 1;
3018 ++ msr_info->data = vmx->pt_desc.guest.ctl;
3019 ++ break;
3020 ++ case MSR_IA32_RTIT_STATUS:
3021 ++ if (pt_mode != PT_MODE_HOST_GUEST)
3022 ++ return 1;
3023 ++ msr_info->data = vmx->pt_desc.guest.status;
3024 ++ break;
3025 ++ case MSR_IA32_RTIT_CR3_MATCH:
3026 ++ if ((pt_mode != PT_MODE_HOST_GUEST) ||
3027 ++ !intel_pt_validate_cap(vmx->pt_desc.caps,
3028 ++ PT_CAP_cr3_filtering))
3029 ++ return 1;
3030 ++ msr_info->data = vmx->pt_desc.guest.cr3_match;
3031 ++ break;
3032 ++ case MSR_IA32_RTIT_OUTPUT_BASE:
3033 ++ if ((pt_mode != PT_MODE_HOST_GUEST) ||
3034 ++ (!intel_pt_validate_cap(vmx->pt_desc.caps,
3035 ++ PT_CAP_topa_output) &&
3036 ++ !intel_pt_validate_cap(vmx->pt_desc.caps,
3037 ++ PT_CAP_single_range_output)))
3038 ++ return 1;
3039 ++ msr_info->data = vmx->pt_desc.guest.output_base;
3040 ++ break;
3041 ++ case MSR_IA32_RTIT_OUTPUT_MASK:
3042 ++ if ((pt_mode != PT_MODE_HOST_GUEST) ||
3043 ++ (!intel_pt_validate_cap(vmx->pt_desc.caps,
3044 ++ PT_CAP_topa_output) &&
3045 ++ !intel_pt_validate_cap(vmx->pt_desc.caps,
3046 ++ PT_CAP_single_range_output)))
3047 ++ return 1;
3048 ++ msr_info->data = vmx->pt_desc.guest.output_mask;
3049 ++ break;
3050 ++ case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
3051 ++ index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
3052 ++ if ((pt_mode != PT_MODE_HOST_GUEST) ||
3053 ++ (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
3054 ++ PT_CAP_num_address_ranges)))
3055 ++ return 1;
3056 ++ if (is_noncanonical_address(data, vcpu))
3057 ++ return 1;
3058 ++ if (index % 2)
3059 ++ msr_info->data = vmx->pt_desc.guest.addr_b[index / 2];
3060 ++ else
3061 ++ msr_info->data = vmx->pt_desc.guest.addr_a[index / 2];
3062 ++ break;
3063 ++ case MSR_TSC_AUX:
3064 ++ if (!msr_info->host_initiated &&
3065 ++ !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3066 ++ return 1;
3067 ++ goto find_shared_msr;
3068 ++ default:
3069 ++ find_shared_msr:
3070 ++ msr = find_msr_entry(vmx, msr_info->index);
3071 ++ if (msr) {
3072 ++ msr_info->data = msr->data;
3073 ++ break;
3074 ++ }
3075 ++ return kvm_get_msr_common(vcpu, msr_info);
3076 ++ }
3077 ++
3078 ++ return 0;
3079 ++}
3080 ++
3081 ++/*
3082 ++ * Writes msr value into the appropriate "register".
3083 ++ * Returns 0 on success, non-0 otherwise.
3084 ++ * Assumes vcpu_load() was already called.
3085 ++ */
3086 ++static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3087 ++{
3088 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
3089 ++ struct shared_msr_entry *msr;
3090 ++ int ret = 0;
3091 ++ u32 msr_index = msr_info->index;
3092 ++ u64 data = msr_info->data;
3093 ++ u32 index;
3094 ++
3095 ++ switch (msr_index) {
3096 ++ case MSR_EFER:
3097 ++ ret = kvm_set_msr_common(vcpu, msr_info);
3098 ++ break;
3099 ++#ifdef CONFIG_X86_64
3100 ++ case MSR_FS_BASE:
3101 ++ vmx_segment_cache_clear(vmx);
3102 ++ vmcs_writel(GUEST_FS_BASE, data);
3103 ++ break;
3104 ++ case MSR_GS_BASE:
3105 ++ vmx_segment_cache_clear(vmx);
3106 ++ vmcs_writel(GUEST_GS_BASE, data);
3107 ++ break;
3108 ++ case MSR_KERNEL_GS_BASE:
3109 ++ vmx_write_guest_kernel_gs_base(vmx, data);
3110 ++ break;
3111 ++#endif
3112 ++ case MSR_IA32_SYSENTER_CS:
3113 ++ if (is_guest_mode(vcpu))
3114 ++ get_vmcs12(vcpu)->guest_sysenter_cs = data;
3115 ++ vmcs_write32(GUEST_SYSENTER_CS, data);
3116 ++ break;
3117 ++ case MSR_IA32_SYSENTER_EIP:
3118 ++ if (is_guest_mode(vcpu))
3119 ++ get_vmcs12(vcpu)->guest_sysenter_eip = data;
3120 ++ vmcs_writel(GUEST_SYSENTER_EIP, data);
3121 ++ break;
3122 ++ case MSR_IA32_SYSENTER_ESP:
3123 ++ if (is_guest_mode(vcpu))
3124 ++ get_vmcs12(vcpu)->guest_sysenter_esp = data;
3125 ++ vmcs_writel(GUEST_SYSENTER_ESP, data);
3126 ++ break;
3127 ++ case MSR_IA32_DEBUGCTLMSR:
3128 ++ if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls &
3129 ++ VM_EXIT_SAVE_DEBUG_CONTROLS)
3130 ++ get_vmcs12(vcpu)->guest_ia32_debugctl = data;
3131 ++
3132 ++ ret = kvm_set_msr_common(vcpu, msr_info);
3133 ++ break;
3134 ++
3135 ++ case MSR_IA32_BNDCFGS:
3136 ++ if (!kvm_mpx_supported() ||
3137 ++ (!msr_info->host_initiated &&
3138 ++ !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3139 ++ return 1;
3140 ++ if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
3141 ++ (data & MSR_IA32_BNDCFGS_RSVD))
3142 ++ return 1;
3143 ++ vmcs_write64(GUEST_BNDCFGS, data);
3144 ++ break;
3145 ++ case MSR_IA32_UMWAIT_CONTROL:
3146 ++ if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
3147 ++ return 1;
3148 ++
3149 ++ /* The reserved bit 1 and non-32 bit [63:32] should be zero */
3150 ++ if (data & (BIT_ULL(1) | GENMASK_ULL(63, 32)))
3151 ++ return 1;
3152 ++
3153 ++ vmx->msr_ia32_umwait_control = data;
3154 ++ break;
3155 ++ case MSR_IA32_SPEC_CTRL:
3156 ++ if (!msr_info->host_initiated &&
3157 ++ !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3158 ++ return 1;
3159 ++
3160 ++ /* The STIBP bit doesn't fault even if it's not advertised */
3161 ++ if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
3162 ++ return 1;
3163 ++
3164 ++ vmx->spec_ctrl = data;
3165 ++
3166 ++ if (!data)
3167 ++ break;
3168 ++
3169 ++ /*
3170 ++ * For non-nested:
3171 ++ * When it's written (to non-zero) for the first time, pass
3172 ++ * it through.
3173 ++ *
3174 ++ * For nested:
3175 ++ * The handling of the MSR bitmap for L2 guests is done in
3176 ++ * nested_vmx_prepare_msr_bitmap. We should not touch the
3177 ++ * vmcs02.msr_bitmap here since it gets completely overwritten
3178 ++ * in the merging. We update the vmcs01 here for L1 as well
3179 ++ * since it will end up touching the MSR anyway now.
3180 ++ */
3181 ++ vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3182 ++ MSR_IA32_SPEC_CTRL,
3183 ++ MSR_TYPE_RW);
3184 ++ break;
3185 ++ case MSR_IA32_TSX_CTRL:
3186 ++ if (!msr_info->host_initiated &&
3187 ++ !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
3188 ++ return 1;
3189 ++ if (data & ~(TSX_CTRL_RTM_DISABLE | TSX_CTRL_CPUID_CLEAR))
3190 ++ return 1;
3191 ++ goto find_shared_msr;
3192 ++ case MSR_IA32_PRED_CMD:
3193 ++ if (!msr_info->host_initiated &&
3194 ++ !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3195 ++ return 1;
3196 ++
3197 ++ if (data & ~PRED_CMD_IBPB)
3198 ++ return 1;
3199 ++
3200 ++ if (!data)
3201 ++ break;
3202 ++
3203 ++ wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3204 ++
3205 ++ /*
3206 ++ * For non-nested:
3207 ++ * When it's written (to non-zero) for the first time, pass
3208 ++ * it through.
3209 ++ *
3210 ++ * For nested:
3211 ++ * The handling of the MSR bitmap for L2 guests is done in
3212 ++ * nested_vmx_prepare_msr_bitmap. We should not touch the
3213 ++ * vmcs02.msr_bitmap here since it gets completely overwritten
3214 ++ * in the merging.
3215 ++ */
3216 ++ vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3217 ++ MSR_TYPE_W);
3218 ++ break;
3219 ++ case MSR_IA32_CR_PAT:
3220 ++ if (!kvm_pat_valid(data))
3221 ++ return 1;
3222 ++
3223 ++ if (is_guest_mode(vcpu) &&
3224 ++ get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
3225 ++ get_vmcs12(vcpu)->guest_ia32_pat = data;
3226 ++
3227 ++ if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
3228 ++ vmcs_write64(GUEST_IA32_PAT, data);
3229 ++ vcpu->arch.pat = data;
3230 ++ break;
3231 ++ }
3232 ++ ret = kvm_set_msr_common(vcpu, msr_info);
3233 ++ break;
3234 ++ case MSR_IA32_TSC_ADJUST:
3235 ++ ret = kvm_set_msr_common(vcpu, msr_info);
3236 ++ break;
3237 ++ case MSR_IA32_MCG_EXT_CTL:
3238 ++ if ((!msr_info->host_initiated &&
3239 ++ !(to_vmx(vcpu)->msr_ia32_feature_control &
3240 ++ FEATURE_CONTROL_LMCE)) ||
3241 ++ (data & ~MCG_EXT_CTL_LMCE_EN))
3242 ++ return 1;
3243 ++ vcpu->arch.mcg_ext_ctl = data;
3244 ++ break;
3245 ++ case MSR_IA32_FEATURE_CONTROL:
3246 ++ if (!vmx_feature_control_msr_valid(vcpu, data) ||
3247 ++ (to_vmx(vcpu)->msr_ia32_feature_control &
3248 ++ FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3249 ++ return 1;
3250 ++ vmx->msr_ia32_feature_control = data;
3251 ++ if (msr_info->host_initiated && data == 0)
3252 ++ vmx_leave_nested(vcpu);
3253 ++ break;
3254 ++ case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3255 ++ if (!msr_info->host_initiated)
3256 ++ return 1; /* they are read-only */
3257 ++ if (!nested_vmx_allowed(vcpu))
3258 ++ return 1;
3259 ++ return vmx_set_vmx_msr(vcpu, msr_index, data);
3260 ++ case MSR_IA32_RTIT_CTL:
3261 ++ if ((pt_mode != PT_MODE_HOST_GUEST) ||
3262 ++ vmx_rtit_ctl_check(vcpu, data) ||
3263 ++ vmx->nested.vmxon)
3264 ++ return 1;
3265 ++ vmcs_write64(GUEST_IA32_RTIT_CTL, data);
3266 ++ vmx->pt_desc.guest.ctl = data;
3267 ++ pt_update_intercept_for_msr(vmx);
3268 ++ break;
3269 ++ case MSR_IA32_RTIT_STATUS:
3270 ++ if ((pt_mode != PT_MODE_HOST_GUEST) ||
3271 ++ (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) ||
3272 ++ (data & MSR_IA32_RTIT_STATUS_MASK))
3273 ++ return 1;
3274 ++ vmx->pt_desc.guest.status = data;
3275 ++ break;
3276 ++ case MSR_IA32_RTIT_CR3_MATCH:
3277 ++ if ((pt_mode != PT_MODE_HOST_GUEST) ||
3278 ++ (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) ||
3279 ++ !intel_pt_validate_cap(vmx->pt_desc.caps,
3280 ++ PT_CAP_cr3_filtering))
3281 ++ return 1;
3282 ++ vmx->pt_desc.guest.cr3_match = data;
3283 ++ break;
3284 ++ case MSR_IA32_RTIT_OUTPUT_BASE:
3285 ++ if ((pt_mode != PT_MODE_HOST_GUEST) ||
3286 ++ (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) ||
3287 ++ (!intel_pt_validate_cap(vmx->pt_desc.caps,
3288 ++ PT_CAP_topa_output) &&
3289 ++ !intel_pt_validate_cap(vmx->pt_desc.caps,
3290 ++ PT_CAP_single_range_output)) ||
3291 ++ (data & MSR_IA32_RTIT_OUTPUT_BASE_MASK))
3292 ++ return 1;
3293 ++ vmx->pt_desc.guest.output_base = data;
3294 ++ break;
3295 ++ case MSR_IA32_RTIT_OUTPUT_MASK:
3296 ++ if ((pt_mode != PT_MODE_HOST_GUEST) ||
3297 ++ (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) ||
3298 ++ (!intel_pt_validate_cap(vmx->pt_desc.caps,
3299 ++ PT_CAP_topa_output) &&
3300 ++ !intel_pt_validate_cap(vmx->pt_desc.caps,
3301 ++ PT_CAP_single_range_output)))
3302 ++ return 1;
3303 ++ vmx->pt_desc.guest.output_mask = data;
3304 ++ break;
3305 ++ case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
3306 ++ index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
3307 ++ if ((pt_mode != PT_MODE_HOST_GUEST) ||
3308 ++ (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) ||
3309 ++ (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
3310 ++ PT_CAP_num_address_ranges)))
3311 ++ return 1;
3312 ++ if (is_noncanonical_address(data, vcpu))
3313 ++ return 1;
3314 ++ if (index % 2)
3315 ++ vmx->pt_desc.guest.addr_b[index / 2] = data;
3316 ++ else
3317 ++ vmx->pt_desc.guest.addr_a[index / 2] = data;
3318 ++ break;
3319 ++ case MSR_TSC_AUX:
3320 ++ if (!msr_info->host_initiated &&
3321 ++ !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3322 ++ return 1;
3323 ++ /* Check reserved bit, higher 32 bits should be zero */
3324 ++ if ((data >> 32) != 0)
3325 ++ return 1;
3326 ++ goto find_shared_msr;
3327 ++
3328 ++ default:
3329 ++ find_shared_msr:
3330 ++ msr = find_msr_entry(vmx, msr_index);
3331 ++ if (msr)
3332 ++ ret = vmx_set_guest_msr(vmx, msr, data);
3333 ++ else
3334 ++ ret = kvm_set_msr_common(vcpu, msr_info);
3335 ++ }
3336 ++
3337 ++ return ret;
3338 ++}
3339 ++
3340 ++static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
3341 ++{
3342 ++ kvm_register_mark_available(vcpu, reg);
3343 ++
3344 ++ switch (reg) {
3345 ++ case VCPU_REGS_RSP:
3346 ++ vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3347 ++ break;
3348 ++ case VCPU_REGS_RIP:
3349 ++ vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3350 ++ break;
3351 ++ case VCPU_EXREG_PDPTR:
3352 ++ if (enable_ept)
3353 ++ ept_save_pdptrs(vcpu);
3354 ++ break;
3355 ++ case VCPU_EXREG_CR3:
3356 ++ if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
3357 ++ vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3358 ++ break;
3359 ++ default:
3360 ++ WARN_ON_ONCE(1);
3361 ++ break;
3362 ++ }
3363 ++}
3364 ++
3365 ++static __init int cpu_has_kvm_support(void)
3366 ++{
3367 ++ return cpu_has_vmx();
3368 ++}
3369 ++
3370 ++static __init int vmx_disabled_by_bios(void)
3371 ++{
3372 ++ u64 msr;
3373 ++
3374 ++ rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
3375 ++ if (msr & FEATURE_CONTROL_LOCKED) {
3376 ++ /* launched w/ TXT and VMX disabled */
3377 ++ if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3378 ++ && tboot_enabled())
3379 ++ return 1;
3380 ++ /* launched w/o TXT and VMX only enabled w/ TXT */
3381 ++ if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3382 ++ && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3383 ++ && !tboot_enabled()) {
3384 ++ printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
3385 ++ "activate TXT before enabling KVM\n");
3386 ++ return 1;
3387 ++ }
3388 ++ /* launched w/o TXT and VMX disabled */
3389 ++ if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3390 ++ && !tboot_enabled())
3391 ++ return 1;
3392 ++ }
3393 ++
3394 ++ return 0;
3395 ++}
3396 ++
3397 ++static void kvm_cpu_vmxon(u64 addr)
3398 ++{
3399 ++ cr4_set_bits(X86_CR4_VMXE);
3400 ++ intel_pt_handle_vmx(1);
3401 ++
3402 ++ asm volatile ("vmxon %0" : : "m"(addr));
3403 ++}
3404 ++
3405 ++static int hardware_enable(void)
3406 ++{
3407 ++ int cpu = raw_smp_processor_id();
3408 ++ u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
3409 ++ u64 old, test_bits;
3410 ++
3411 ++ if (cr4_read_shadow() & X86_CR4_VMXE)
3412 ++ return -EBUSY;
3413 ++
3414 ++ /*
3415 ++ * This can happen if we hot-added a CPU but failed to allocate
3416 ++ * VP assist page for it.
3417 ++ */
3418 ++ if (static_branch_unlikely(&enable_evmcs) &&
3419 ++ !hv_get_vp_assist_page(cpu))
3420 ++ return -EFAULT;
3421 ++
3422 ++ INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
3423 ++ INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3424 ++ spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
3425 ++
3426 ++ /*
3427 ++ * Now we can enable the vmclear operation in kdump
3428 ++ * since the loaded_vmcss_on_cpu list on this cpu
3429 ++ * has been initialized.
3430 ++ *
3431 ++ * Though the cpu is not in VMX operation now, there
3432 ++ * is no problem to enable the vmclear operation
3433 ++ * for the loaded_vmcss_on_cpu list is empty!
3434 ++ */
3435 ++ crash_enable_local_vmclear(cpu);
3436 ++
3437 ++ rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
3438 ++
3439 ++ test_bits = FEATURE_CONTROL_LOCKED;
3440 ++ test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3441 ++ if (tboot_enabled())
3442 ++ test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3443 ++
3444 ++ if ((old & test_bits) != test_bits) {
3445 ++ /* enable and lock */
3446 ++ wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3447 ++ }
3448 ++ kvm_cpu_vmxon(phys_addr);
3449 ++ if (enable_ept)
3450 ++ ept_sync_global();
3451 ++
3452 ++ return 0;
3453 ++}
3454 ++
3455 ++static void vmclear_local_loaded_vmcss(void)
3456 ++{
3457 ++ int cpu = raw_smp_processor_id();
3458 ++ struct loaded_vmcs *v, *n;
3459 ++
3460 ++ list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3461 ++ loaded_vmcss_on_cpu_link)
3462 ++ __loaded_vmcs_clear(v);
3463 ++}
3464 ++
3465 ++
3466 ++/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3467 ++ * tricks.
3468 ++ */
3469 ++static void kvm_cpu_vmxoff(void)
3470 ++{
3471 ++ asm volatile (__ex("vmxoff"));
3472 ++
3473 ++ intel_pt_handle_vmx(0);
3474 ++ cr4_clear_bits(X86_CR4_VMXE);
3475 ++}
3476 ++
3477 ++static void hardware_disable(void)
3478 ++{
3479 ++ vmclear_local_loaded_vmcss();
3480 ++ kvm_cpu_vmxoff();
3481 ++}
3482 ++
3483 ++static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
3484 ++ u32 msr, u32 *result)
3485 ++{
3486 ++ u32 vmx_msr_low, vmx_msr_high;
3487 ++ u32 ctl = ctl_min | ctl_opt;
3488 ++
3489 ++ rdmsr(msr, vmx_msr_low, vmx_msr_high);
3490 ++
3491 ++ ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3492 ++ ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3493 ++
3494 ++ /* Ensure minimum (required) set of control bits are supported. */
3495 ++ if (ctl_min & ~ctl)
3496 ++ return -EIO;
3497 ++
3498 ++ *result = ctl;
3499 ++ return 0;
3500 ++}
3501 ++
3502 ++static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
3503 ++ struct vmx_capability *vmx_cap)
3504 ++{
3505 ++ u32 vmx_msr_low, vmx_msr_high;
3506 ++ u32 min, opt, min2, opt2;
3507 ++ u32 _pin_based_exec_control = 0;
3508 ++ u32 _cpu_based_exec_control = 0;
3509 ++ u32 _cpu_based_2nd_exec_control = 0;
3510 ++ u32 _vmexit_control = 0;
3511 ++ u32 _vmentry_control = 0;
3512 ++
3513 ++ memset(vmcs_conf, 0, sizeof(*vmcs_conf));
3514 ++ min = CPU_BASED_HLT_EXITING |
3515 ++#ifdef CONFIG_X86_64
3516 ++ CPU_BASED_CR8_LOAD_EXITING |
3517 ++ CPU_BASED_CR8_STORE_EXITING |
3518 ++#endif
3519 ++ CPU_BASED_CR3_LOAD_EXITING |
3520 ++ CPU_BASED_CR3_STORE_EXITING |
3521 ++ CPU_BASED_UNCOND_IO_EXITING |
3522 ++ CPU_BASED_MOV_DR_EXITING |
3523 ++ CPU_BASED_USE_TSC_OFFSETTING |
3524 ++ CPU_BASED_MWAIT_EXITING |
3525 ++ CPU_BASED_MONITOR_EXITING |
3526 ++ CPU_BASED_INVLPG_EXITING |
3527 ++ CPU_BASED_RDPMC_EXITING;
3528 ++
3529 ++ opt = CPU_BASED_TPR_SHADOW |
3530 ++ CPU_BASED_USE_MSR_BITMAPS |
3531 ++ CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3532 ++ if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3533 ++ &_cpu_based_exec_control) < 0)
3534 ++ return -EIO;
3535 ++#ifdef CONFIG_X86_64
3536 ++ if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3537 ++ _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3538 ++ ~CPU_BASED_CR8_STORE_EXITING;
3539 ++#endif
3540 ++ if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
3541 ++ min2 = 0;
3542 ++ opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
3543 ++ SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3544 ++ SECONDARY_EXEC_WBINVD_EXITING |
3545 ++ SECONDARY_EXEC_ENABLE_VPID |
3546 ++ SECONDARY_EXEC_ENABLE_EPT |
3547 ++ SECONDARY_EXEC_UNRESTRICTED_GUEST |
3548 ++ SECONDARY_EXEC_PAUSE_LOOP_EXITING |
3549 ++ SECONDARY_EXEC_DESC |
3550 ++ SECONDARY_EXEC_RDTSCP |
3551 ++ SECONDARY_EXEC_ENABLE_INVPCID |
3552 ++ SECONDARY_EXEC_APIC_REGISTER_VIRT |
3553 ++ SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3554 ++ SECONDARY_EXEC_SHADOW_VMCS |
3555 ++ SECONDARY_EXEC_XSAVES |
3556 ++ SECONDARY_EXEC_RDSEED_EXITING |
3557 ++ SECONDARY_EXEC_RDRAND_EXITING |
3558 ++ SECONDARY_EXEC_ENABLE_PML |
3559 ++ SECONDARY_EXEC_TSC_SCALING |
3560 ++ SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
3561 ++ SECONDARY_EXEC_PT_USE_GPA |
3562 ++ SECONDARY_EXEC_PT_CONCEAL_VMX |
3563 ++ SECONDARY_EXEC_ENABLE_VMFUNC |
3564 ++ SECONDARY_EXEC_ENCLS_EXITING;
3565 ++ if (adjust_vmx_controls(min2, opt2,
3566 ++ MSR_IA32_VMX_PROCBASED_CTLS2,
3567 ++ &_cpu_based_2nd_exec_control) < 0)
3568 ++ return -EIO;
3569 ++ }
3570 ++#ifndef CONFIG_X86_64
3571 ++ if (!(_cpu_based_2nd_exec_control &
3572 ++ SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3573 ++ _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3574 ++#endif
3575 ++
3576 ++ if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3577 ++ _cpu_based_2nd_exec_control &= ~(
3578 ++ SECONDARY_EXEC_APIC_REGISTER_VIRT |
3579 ++ SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3580 ++ SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3581 ++
3582 ++ rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3583 ++ &vmx_cap->ept, &vmx_cap->vpid);
3584 ++
3585 ++ if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
3586 ++ /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3587 ++ enabled */
3588 ++ _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3589 ++ CPU_BASED_CR3_STORE_EXITING |
3590 ++ CPU_BASED_INVLPG_EXITING);
3591 ++ } else if (vmx_cap->ept) {
3592 ++ vmx_cap->ept = 0;
3593 ++ pr_warn_once("EPT CAP should not exist if not support "
3594 ++ "1-setting enable EPT VM-execution control\n");
3595 ++ }
3596 ++ if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3597 ++ vmx_cap->vpid) {
3598 ++ vmx_cap->vpid = 0;
3599 ++ pr_warn_once("VPID CAP should not exist if not support "
3600 ++ "1-setting enable VPID VM-execution control\n");
3601 ++ }
3602 ++
3603 ++ min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
3604 ++#ifdef CONFIG_X86_64
3605 ++ min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3606 ++#endif
3607 ++ opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
3608 ++ VM_EXIT_LOAD_IA32_PAT |
3609 ++ VM_EXIT_LOAD_IA32_EFER |
3610 ++ VM_EXIT_CLEAR_BNDCFGS |
3611 ++ VM_EXIT_PT_CONCEAL_PIP |
3612 ++ VM_EXIT_CLEAR_IA32_RTIT_CTL;
3613 ++ if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3614 ++ &_vmexit_control) < 0)
3615 ++ return -EIO;
3616 ++
3617 ++ min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3618 ++ opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3619 ++ PIN_BASED_VMX_PREEMPTION_TIMER;
3620 ++ if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3621 ++ &_pin_based_exec_control) < 0)
3622 ++ return -EIO;
3623 ++
3624 ++ if (cpu_has_broken_vmx_preemption_timer())
3625 ++ _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
3626 ++ if (!(_cpu_based_2nd_exec_control &
3627 ++ SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
3628 ++ _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3629 ++
3630 ++ min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
3631 ++ opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
3632 ++ VM_ENTRY_LOAD_IA32_PAT |
3633 ++ VM_ENTRY_LOAD_IA32_EFER |
3634 ++ VM_ENTRY_LOAD_BNDCFGS |
3635 ++ VM_ENTRY_PT_CONCEAL_PIP |
3636 ++ VM_ENTRY_LOAD_IA32_RTIT_CTL;
3637 ++ if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3638 ++ &_vmentry_control) < 0)
3639 ++ return -EIO;
3640 ++
3641 ++ /*
3642 ++ * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
3643 ++ * can't be used due to an errata where VM Exit may incorrectly clear
3644 ++ * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
3645 ++ * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3646 ++ */
3647 ++ if (boot_cpu_data.x86 == 0x6) {
3648 ++ switch (boot_cpu_data.x86_model) {
3649 ++ case 26: /* AAK155 */
3650 ++ case 30: /* AAP115 */
3651 ++ case 37: /* AAT100 */
3652 ++ case 44: /* BC86,AAY89,BD102 */
3653 ++ case 46: /* BA97 */
3654 ++ _vmentry_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
3655 ++ _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
3656 ++ pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3657 ++ "does not work properly. Using workaround\n");
3658 ++ break;
3659 ++ default:
3660 ++ break;
3661 ++ }
3662 ++ }
3663 ++
3664 ++
3665 ++ rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
3666 ++
3667 ++ /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3668 ++ if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
3669 ++ return -EIO;
3670 ++
3671 ++#ifdef CONFIG_X86_64
3672 ++ /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3673 ++ if (vmx_msr_high & (1u<<16))
3674 ++ return -EIO;
3675 ++#endif
3676 ++
3677 ++ /* Require Write-Back (WB) memory type for VMCS accesses. */
3678 ++ if (((vmx_msr_high >> 18) & 15) != 6)
3679 ++ return -EIO;
3680 ++
3681 ++ vmcs_conf->size = vmx_msr_high & 0x1fff;
3682 ++ vmcs_conf->order = get_order(vmcs_conf->size);
3683 ++ vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
3684 ++
3685 ++ vmcs_conf->revision_id = vmx_msr_low;
3686 ++
3687 ++ vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3688 ++ vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
3689 ++ vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
3690 ++ vmcs_conf->vmexit_ctrl = _vmexit_control;
3691 ++ vmcs_conf->vmentry_ctrl = _vmentry_control;
3692 ++
3693 ++ if (static_branch_unlikely(&enable_evmcs))
3694 ++ evmcs_sanitize_exec_ctrls(vmcs_conf);
3695 ++
3696 ++ return 0;
3697 ++}
3698 ++
3699 ++struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags)
3700 ++{
3701 ++ int node = cpu_to_node(cpu);
3702 ++ struct page *pages;
3703 ++ struct vmcs *vmcs;
3704 ++
3705 ++ pages = __alloc_pages_node(node, flags, vmcs_config.order);
3706 ++ if (!pages)
3707 ++ return NULL;
3708 ++ vmcs = page_address(pages);
3709 ++ memset(vmcs, 0, vmcs_config.size);
3710 ++
3711 ++ /* KVM supports Enlightened VMCS v1 only */
3712 ++ if (static_branch_unlikely(&enable_evmcs))
3713 ++ vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
3714 ++ else
3715 ++ vmcs->hdr.revision_id = vmcs_config.revision_id;
3716 ++
3717 ++ if (shadow)
3718 ++ vmcs->hdr.shadow_vmcs = 1;
3719 ++ return vmcs;
3720 ++}
3721 ++
3722 ++void free_vmcs(struct vmcs *vmcs)
3723 ++{
3724 ++ free_pages((unsigned long)vmcs, vmcs_config.order);
3725 ++}
3726 ++
3727 ++/*
3728 ++ * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3729 ++ */
3730 ++void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3731 ++{
3732 ++ if (!loaded_vmcs->vmcs)
3733 ++ return;
3734 ++ loaded_vmcs_clear(loaded_vmcs);
3735 ++ free_vmcs(loaded_vmcs->vmcs);
3736 ++ loaded_vmcs->vmcs = NULL;
3737 ++ if (loaded_vmcs->msr_bitmap)
3738 ++ free_page((unsigned long)loaded_vmcs->msr_bitmap);
3739 ++ WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
3740 ++}
3741 ++
3742 ++int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3743 ++{
3744 ++ loaded_vmcs->vmcs = alloc_vmcs(false);
3745 ++ if (!loaded_vmcs->vmcs)
3746 ++ return -ENOMEM;
3747 ++
3748 ++ loaded_vmcs->shadow_vmcs = NULL;
3749 ++ loaded_vmcs->hv_timer_soft_disabled = false;
3750 ++ loaded_vmcs_init(loaded_vmcs);
3751 ++
3752 ++ if (cpu_has_vmx_msr_bitmap()) {
3753 ++ loaded_vmcs->msr_bitmap = (unsigned long *)
3754 ++ __get_free_page(GFP_KERNEL_ACCOUNT);
3755 ++ if (!loaded_vmcs->msr_bitmap)
3756 ++ goto out_vmcs;
3757 ++ memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3758 ++
3759 ++ if (IS_ENABLED(CONFIG_HYPERV) &&
3760 ++ static_branch_unlikely(&enable_evmcs) &&
3761 ++ (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
3762 ++ struct hv_enlightened_vmcs *evmcs =
3763 ++ (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
3764 ++
3765 ++ evmcs->hv_enlightenments_control.msr_bitmap = 1;
3766 ++ }
3767 ++ }
3768 ++
3769 ++ memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
3770 ++ memset(&loaded_vmcs->controls_shadow, 0,
3771 ++ sizeof(struct vmcs_controls_shadow));
3772 ++
3773 ++ return 0;
3774 ++
3775 ++out_vmcs:
3776 ++ free_loaded_vmcs(loaded_vmcs);
3777 ++ return -ENOMEM;
3778 ++}
3779 ++
3780 ++static void free_kvm_area(void)
3781 ++{
3782 ++ int cpu;
3783 ++
3784 ++ for_each_possible_cpu(cpu) {
3785 ++ free_vmcs(per_cpu(vmxarea, cpu));
3786 ++ per_cpu(vmxarea, cpu) = NULL;
3787 ++ }
3788 ++}
3789 ++
3790 ++static __init int alloc_kvm_area(void)
3791 ++{
3792 ++ int cpu;
3793 ++
3794 ++ for_each_possible_cpu(cpu) {
3795 ++ struct vmcs *vmcs;
3796 ++
3797 ++ vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL);
3798 ++ if (!vmcs) {
3799 ++ free_kvm_area();
3800 ++ return -ENOMEM;
3801 ++ }
3802 ++
3803 ++ /*
3804 ++ * When eVMCS is enabled, alloc_vmcs_cpu() sets
3805 ++ * vmcs->revision_id to KVM_EVMCS_VERSION instead of
3806 ++ * revision_id reported by MSR_IA32_VMX_BASIC.
3807 ++ *
3808 ++ * However, even though not explicitly documented by
3809 ++ * TLFS, VMXArea passed as VMXON argument should
3810 ++ * still be marked with revision_id reported by
3811 ++ * physical CPU.
3812 ++ */
3813 ++ if (static_branch_unlikely(&enable_evmcs))
3814 ++ vmcs->hdr.revision_id = vmcs_config.revision_id;
3815 ++
3816 ++ per_cpu(vmxarea, cpu) = vmcs;
3817 ++ }
3818 ++ return 0;
3819 ++}
3820 ++
3821 ++static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
3822 ++ struct kvm_segment *save)
3823 ++{
3824 ++ if (!emulate_invalid_guest_state) {
3825 ++ /*
3826 ++ * CS and SS RPL should be equal during guest entry according
3827 ++ * to VMX spec, but in reality it is not always so. Since vcpu
3828 ++ * is in the middle of the transition from real mode to
3829 ++ * protected mode it is safe to assume that RPL 0 is a good
3830 ++ * default value.
3831 ++ */
3832 ++ if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
3833 ++ save->selector &= ~SEGMENT_RPL_MASK;
3834 ++ save->dpl = save->selector & SEGMENT_RPL_MASK;
3835 ++ save->s = 1;
3836 ++ }
3837 ++ vmx_set_segment(vcpu, save, seg);
3838 ++}
3839 ++
3840 ++static void enter_pmode(struct kvm_vcpu *vcpu)
3841 ++{
3842 ++ unsigned long flags;
3843 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
3844 ++
3845 ++ /*
3846 ++ * Update real mode segment cache. It may be not up-to-date if sement
3847 ++ * register was written while vcpu was in a guest mode.
3848 ++ */
3849 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3850 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3851 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3852 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3853 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3854 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3855 ++
3856 ++ vmx->rmode.vm86_active = 0;
3857 ++
3858 ++ vmx_segment_cache_clear(vmx);
3859 ++
3860 ++ vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3861 ++
3862 ++ flags = vmcs_readl(GUEST_RFLAGS);
3863 ++ flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3864 ++ flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3865 ++ vmcs_writel(GUEST_RFLAGS, flags);
3866 ++
3867 ++ vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3868 ++ (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
3869 ++
3870 ++ update_exception_bitmap(vcpu);
3871 ++
3872 ++ fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3873 ++ fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3874 ++ fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3875 ++ fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3876 ++ fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3877 ++ fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3878 ++}
3879 ++
3880 ++static void fix_rmode_seg(int seg, struct kvm_segment *save)
3881 ++{
3882 ++ const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
3883 ++ struct kvm_segment var = *save;
3884 ++
3885 ++ var.dpl = 0x3;
3886 ++ if (seg == VCPU_SREG_CS)
3887 ++ var.type = 0x3;
3888 ++
3889 ++ if (!emulate_invalid_guest_state) {
3890 ++ var.selector = var.base >> 4;
3891 ++ var.base = var.base & 0xffff0;
3892 ++ var.limit = 0xffff;
3893 ++ var.g = 0;
3894 ++ var.db = 0;
3895 ++ var.present = 1;
3896 ++ var.s = 1;
3897 ++ var.l = 0;
3898 ++ var.unusable = 0;
3899 ++ var.type = 0x3;
3900 ++ var.avl = 0;
3901 ++ if (save->base & 0xf)
3902 ++ printk_once(KERN_WARNING "kvm: segment base is not "
3903 ++ "paragraph aligned when entering "
3904 ++ "protected mode (seg=%d)", seg);
3905 ++ }
3906 ++
3907 ++ vmcs_write16(sf->selector, var.selector);
3908 ++ vmcs_writel(sf->base, var.base);
3909 ++ vmcs_write32(sf->limit, var.limit);
3910 ++ vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
3911 ++}
3912 ++
3913 ++static void enter_rmode(struct kvm_vcpu *vcpu)
3914 ++{
3915 ++ unsigned long flags;
3916 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
3917 ++ struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
3918 ++
3919 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3920 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3921 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3922 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3923 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3924 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3925 ++ vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3926 ++
3927 ++ vmx->rmode.vm86_active = 1;
3928 ++
3929 ++ /*
3930 ++ * Very old userspace does not call KVM_SET_TSS_ADDR before entering
3931 ++ * vcpu. Warn the user that an update is overdue.
3932 ++ */
3933 ++ if (!kvm_vmx->tss_addr)
3934 ++ printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3935 ++ "called before entering vcpu\n");
3936 ++
3937 ++ vmx_segment_cache_clear(vmx);
3938 ++
3939 ++ vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
3940 ++ vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
3941 ++ vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3942 ++
3943 ++ flags = vmcs_readl(GUEST_RFLAGS);
3944 ++ vmx->rmode.save_rflags = flags;
3945 ++
3946 ++ flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
3947 ++
3948 ++ vmcs_writel(GUEST_RFLAGS, flags);
3949 ++ vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
3950 ++ update_exception_bitmap(vcpu);
3951 ++
3952 ++ fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3953 ++ fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3954 ++ fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3955 ++ fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3956 ++ fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3957 ++ fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3958 ++
3959 ++ kvm_mmu_reset_context(vcpu);
3960 ++}
3961 ++
3962 ++void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3963 ++{
3964 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
3965 ++ struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3966 ++
3967 ++ if (!msr)
3968 ++ return;
3969 ++
3970 ++ vcpu->arch.efer = efer;
3971 ++ if (efer & EFER_LMA) {
3972 ++ vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
3973 ++ msr->data = efer;
3974 ++ } else {
3975 ++ vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
3976 ++
3977 ++ msr->data = efer & ~EFER_LME;
3978 ++ }
3979 ++ setup_msrs(vmx);
3980 ++}
3981 ++
3982 ++#ifdef CONFIG_X86_64
3983 ++
3984 ++static void enter_lmode(struct kvm_vcpu *vcpu)
3985 ++{
3986 ++ u32 guest_tr_ar;
3987 ++
3988 ++ vmx_segment_cache_clear(to_vmx(vcpu));
3989 ++
3990 ++ guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3991 ++ if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
3992 ++ pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3993 ++ __func__);
3994 ++ vmcs_write32(GUEST_TR_AR_BYTES,
3995 ++ (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3996 ++ | VMX_AR_TYPE_BUSY_64_TSS);
3997 ++ }
3998 ++ vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
3999 ++}
4000 ++
4001 ++static void exit_lmode(struct kvm_vcpu *vcpu)
4002 ++{
4003 ++ vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4004 ++ vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
4005 ++}
4006 ++
4007 ++#endif
4008 ++
4009 ++static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
4010 ++{
4011 ++ int vpid = to_vmx(vcpu)->vpid;
4012 ++
4013 ++ if (!vpid_sync_vcpu_addr(vpid, addr))
4014 ++ vpid_sync_context(vpid);
4015 ++
4016 ++ /*
4017 ++ * If VPIDs are not supported or enabled, then the above is a no-op.
4018 ++ * But we don't really need a TLB flush in that case anyway, because
4019 ++ * each VM entry/exit includes an implicit flush when VPID is 0.
4020 ++ */
4021 ++}
4022 ++
4023 ++static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4024 ++{
4025 ++ ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4026 ++
4027 ++ vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4028 ++ vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4029 ++}
4030 ++
4031 ++static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
4032 ++{
4033 ++ ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4034 ++
4035 ++ vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4036 ++ vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
4037 ++}
4038 ++
4039 ++static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4040 ++{
4041 ++ struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4042 ++
4043 ++ if (!kvm_register_is_dirty(vcpu, VCPU_EXREG_PDPTR))
4044 ++ return;
4045 ++
4046 ++ if (is_pae_paging(vcpu)) {
4047 ++ vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4048 ++ vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4049 ++ vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4050 ++ vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
4051 ++ }
4052 ++}
4053 ++
4054 ++void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4055 ++{
4056 ++ struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4057 ++
4058 ++ if (is_pae_paging(vcpu)) {
4059 ++ mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4060 ++ mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4061 ++ mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4062 ++ mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
4063 ++ }
4064 ++
4065 ++ kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
4066 ++}
4067 ++
4068 ++static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4069 ++ unsigned long cr0,
4070 ++ struct kvm_vcpu *vcpu)
4071 ++{
4072 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4073 ++
4074 ++ if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
4075 ++ vmx_cache_reg(vcpu, VCPU_EXREG_CR3);
4076 ++ if (!(cr0 & X86_CR0_PG)) {
4077 ++ /* From paging/starting to nonpaging */
4078 ++ exec_controls_setbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
4079 ++ CPU_BASED_CR3_STORE_EXITING);
4080 ++ vcpu->arch.cr0 = cr0;
4081 ++ vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4082 ++ } else if (!is_paging(vcpu)) {
4083 ++ /* From nonpaging to paging */
4084 ++ exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
4085 ++ CPU_BASED_CR3_STORE_EXITING);
4086 ++ vcpu->arch.cr0 = cr0;
4087 ++ vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4088 ++ }
4089 ++
4090 ++ if (!(cr0 & X86_CR0_WP))
4091 ++ *hw_cr0 &= ~X86_CR0_WP;
4092 ++}
4093 ++
4094 ++void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4095 ++{
4096 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4097 ++ unsigned long hw_cr0;
4098 ++
4099 ++ hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
4100 ++ if (enable_unrestricted_guest)
4101 ++ hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
4102 ++ else {
4103 ++ hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
4104 ++
4105 ++ if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4106 ++ enter_pmode(vcpu);
4107 ++
4108 ++ if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4109 ++ enter_rmode(vcpu);
4110 ++ }
4111 ++
4112 ++#ifdef CONFIG_X86_64
4113 ++ if (vcpu->arch.efer & EFER_LME) {
4114 ++ if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
4115 ++ enter_lmode(vcpu);
4116 ++ if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
4117 ++ exit_lmode(vcpu);
4118 ++ }
4119 ++#endif
4120 ++
4121 ++ if (enable_ept && !enable_unrestricted_guest)
4122 ++ ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4123 ++
4124 ++ vmcs_writel(CR0_READ_SHADOW, cr0);
4125 ++ vmcs_writel(GUEST_CR0, hw_cr0);
4126 ++ vcpu->arch.cr0 = cr0;
4127 ++
4128 ++ /* depends on vcpu->arch.cr0 to be set to a new value */
4129 ++ vmx->emulation_required = emulation_required(vcpu);
4130 ++}
4131 ++
4132 ++static int get_ept_level(struct kvm_vcpu *vcpu)
4133 ++{
4134 ++ if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4135 ++ return 5;
4136 ++ return 4;
4137 ++}
4138 ++
4139 ++u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
4140 ++{
4141 ++ u64 eptp = VMX_EPTP_MT_WB;
4142 ++
4143 ++ eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
4144 ++
4145 ++ if (enable_ept_ad_bits &&
4146 ++ (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
4147 ++ eptp |= VMX_EPTP_AD_ENABLE_BIT;
4148 ++ eptp |= (root_hpa & PAGE_MASK);
4149 ++
4150 ++ return eptp;
4151 ++}
4152 ++
4153 ++void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4154 ++{
4155 ++ struct kvm *kvm = vcpu->kvm;
4156 ++ bool update_guest_cr3 = true;
4157 ++ unsigned long guest_cr3;
4158 ++ u64 eptp;
4159 ++
4160 ++ guest_cr3 = cr3;
4161 ++ if (enable_ept) {
4162 ++ eptp = construct_eptp(vcpu, cr3);
4163 ++ vmcs_write64(EPT_POINTER, eptp);
4164 ++
4165 ++ if (kvm_x86_ops->tlb_remote_flush) {
4166 ++ spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
4167 ++ to_vmx(vcpu)->ept_pointer = eptp;
4168 ++ to_kvm_vmx(kvm)->ept_pointers_match
4169 ++ = EPT_POINTERS_CHECK;
4170 ++ spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
4171 ++ }
4172 ++
4173 ++ /* Loading vmcs02.GUEST_CR3 is handled by nested VM-Enter. */
4174 ++ if (is_guest_mode(vcpu))
4175 ++ update_guest_cr3 = false;
4176 ++ else if (!enable_unrestricted_guest && !is_paging(vcpu))
4177 ++ guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
4178 ++ else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4179 ++ guest_cr3 = vcpu->arch.cr3;
4180 ++ else /* vmcs01.GUEST_CR3 is already up-to-date. */
4181 ++ update_guest_cr3 = false;
4182 ++ ept_load_pdptrs(vcpu);
4183 ++ }
4184 ++
4185 ++ if (update_guest_cr3)
4186 ++ vmcs_writel(GUEST_CR3, guest_cr3);
4187 ++}
4188 ++
4189 ++int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
4190 ++{
4191 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4192 ++ /*
4193 ++ * Pass through host's Machine Check Enable value to hw_cr4, which
4194 ++ * is in force while we are in guest mode. Do not let guests control
4195 ++ * this bit, even if host CR4.MCE == 0.
4196 ++ */
4197 ++ unsigned long hw_cr4;
4198 ++
4199 ++ hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4200 ++ if (enable_unrestricted_guest)
4201 ++ hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4202 ++ else if (vmx->rmode.vm86_active)
4203 ++ hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4204 ++ else
4205 ++ hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
4206 ++
4207 ++ if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
4208 ++ if (cr4 & X86_CR4_UMIP) {
4209 ++ secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC);
4210 ++ hw_cr4 &= ~X86_CR4_UMIP;
4211 ++ } else if (!is_guest_mode(vcpu) ||
4212 ++ !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) {
4213 ++ secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC);
4214 ++ }
4215 ++ }
4216 ++
4217 ++ if (cr4 & X86_CR4_VMXE) {
4218 ++ /*
4219 ++ * To use VMXON (and later other VMX instructions), a guest
4220 ++ * must first be able to turn on cr4.VMXE (see handle_vmon()).
4221 ++ * So basically the check on whether to allow nested VMX
4222 ++ * is here. We operate under the default treatment of SMM,
4223 ++ * so VMX cannot be enabled under SMM.
4224 ++ */
4225 ++ if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
4226 ++ return 1;
4227 ++ }
4228 ++
4229 ++ if (vmx->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
4230 ++ return 1;
4231 ++
4232 ++ vcpu->arch.cr4 = cr4;
4233 ++
4234 ++ if (!enable_unrestricted_guest) {
4235 ++ if (enable_ept) {
4236 ++ if (!is_paging(vcpu)) {
4237 ++ hw_cr4 &= ~X86_CR4_PAE;
4238 ++ hw_cr4 |= X86_CR4_PSE;
4239 ++ } else if (!(cr4 & X86_CR4_PAE)) {
4240 ++ hw_cr4 &= ~X86_CR4_PAE;
4241 ++ }
4242 ++ }
4243 ++
4244 ++ /*
4245 ++ * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4246 ++ * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4247 ++ * to be manually disabled when guest switches to non-paging
4248 ++ * mode.
4249 ++ *
4250 ++ * If !enable_unrestricted_guest, the CPU is always running
4251 ++ * with CR0.PG=1 and CR4 needs to be modified.
4252 ++ * If enable_unrestricted_guest, the CPU automatically
4253 ++ * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
4254 ++ */
4255 ++ if (!is_paging(vcpu))
4256 ++ hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4257 ++ }
4258 ++
4259 ++ vmcs_writel(CR4_READ_SHADOW, cr4);
4260 ++ vmcs_writel(GUEST_CR4, hw_cr4);
4261 ++ return 0;
4262 ++}
4263 ++
4264 ++void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
4265 ++{
4266 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4267 ++ u32 ar;
4268 ++
4269 ++ if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4270 ++ *var = vmx->rmode.segs[seg];
4271 ++ if (seg == VCPU_SREG_TR
4272 ++ || var->selector == vmx_read_guest_seg_selector(vmx, seg))
4273 ++ return;
4274 ++ var->base = vmx_read_guest_seg_base(vmx, seg);
4275 ++ var->selector = vmx_read_guest_seg_selector(vmx, seg);
4276 ++ return;
4277 ++ }
4278 ++ var->base = vmx_read_guest_seg_base(vmx, seg);
4279 ++ var->limit = vmx_read_guest_seg_limit(vmx, seg);
4280 ++ var->selector = vmx_read_guest_seg_selector(vmx, seg);
4281 ++ ar = vmx_read_guest_seg_ar(vmx, seg);
4282 ++ var->unusable = (ar >> 16) & 1;
4283 ++ var->type = ar & 15;
4284 ++ var->s = (ar >> 4) & 1;
4285 ++ var->dpl = (ar >> 5) & 3;
4286 ++ /*
4287 ++ * Some userspaces do not preserve unusable property. Since usable
4288 ++ * segment has to be present according to VMX spec we can use present
4289 ++ * property to amend userspace bug by making unusable segment always
4290 ++ * nonpresent. vmx_segment_access_rights() already marks nonpresent
4291 ++ * segment as unusable.
4292 ++ */
4293 ++ var->present = !var->unusable;
4294 ++ var->avl = (ar >> 12) & 1;
4295 ++ var->l = (ar >> 13) & 1;
4296 ++ var->db = (ar >> 14) & 1;
4297 ++ var->g = (ar >> 15) & 1;
4298 ++}
4299 ++
4300 ++static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4301 ++{
4302 ++ struct kvm_segment s;
4303 ++
4304 ++ if (to_vmx(vcpu)->rmode.vm86_active) {
4305 ++ vmx_get_segment(vcpu, &s, seg);
4306 ++ return s.base;
4307 ++ }
4308 ++ return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
4309 ++}
4310 ++
4311 ++int vmx_get_cpl(struct kvm_vcpu *vcpu)
4312 ++{
4313 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4314 ++
4315 ++ if (unlikely(vmx->rmode.vm86_active))
4316 ++ return 0;
4317 ++ else {
4318 ++ int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
4319 ++ return VMX_AR_DPL(ar);
4320 ++ }
4321 ++}
4322 ++
4323 ++static u32 vmx_segment_access_rights(struct kvm_segment *var)
4324 ++{
4325 ++ u32 ar;
4326 ++
4327 ++ if (var->unusable || !var->present)
4328 ++ ar = 1 << 16;
4329 ++ else {
4330 ++ ar = var->type & 15;
4331 ++ ar |= (var->s & 1) << 4;
4332 ++ ar |= (var->dpl & 3) << 5;
4333 ++ ar |= (var->present & 1) << 7;
4334 ++ ar |= (var->avl & 1) << 12;
4335 ++ ar |= (var->l & 1) << 13;
4336 ++ ar |= (var->db & 1) << 14;
4337 ++ ar |= (var->g & 1) << 15;
4338 ++ }
4339 ++
4340 ++ return ar;
4341 ++}
4342 ++
4343 ++void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
4344 ++{
4345 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4346 ++ const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4347 ++
4348 ++ vmx_segment_cache_clear(vmx);
4349 ++
4350 ++ if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4351 ++ vmx->rmode.segs[seg] = *var;
4352 ++ if (seg == VCPU_SREG_TR)
4353 ++ vmcs_write16(sf->selector, var->selector);
4354 ++ else if (var->s)
4355 ++ fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
4356 ++ goto out;
4357 ++ }
4358 ++
4359 ++ vmcs_writel(sf->base, var->base);
4360 ++ vmcs_write32(sf->limit, var->limit);
4361 ++ vmcs_write16(sf->selector, var->selector);
4362 ++
4363 ++ /*
4364 ++ * Fix the "Accessed" bit in AR field of segment registers for older
4365 ++ * qemu binaries.
4366 ++ * IA32 arch specifies that at the time of processor reset the
4367 ++ * "Accessed" bit in the AR field of segment registers is 1. And qemu
4368 ++ * is setting it to 0 in the userland code. This causes invalid guest
4369 ++ * state vmexit when "unrestricted guest" mode is turned on.
4370 ++ * Fix for this setup issue in cpu_reset is being pushed in the qemu
4371 ++ * tree. Newer qemu binaries with that qemu fix would not need this
4372 ++ * kvm hack.
4373 ++ */
4374 ++ if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
4375 ++ var->type |= 0x1; /* Accessed */
4376 ++
4377 ++ vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
4378 ++
4379 ++out:
4380 ++ vmx->emulation_required = emulation_required(vcpu);
4381 ++}
4382 ++
4383 ++static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4384 ++{
4385 ++ u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
4386 ++
4387 ++ *db = (ar >> 14) & 1;
4388 ++ *l = (ar >> 13) & 1;
4389 ++}
4390 ++
4391 ++static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4392 ++{
4393 ++ dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4394 ++ dt->address = vmcs_readl(GUEST_IDTR_BASE);
4395 ++}
4396 ++
4397 ++static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4398 ++{
4399 ++ vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4400 ++ vmcs_writel(GUEST_IDTR_BASE, dt->address);
4401 ++}
4402 ++
4403 ++static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4404 ++{
4405 ++ dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4406 ++ dt->address = vmcs_readl(GUEST_GDTR_BASE);
4407 ++}
4408 ++
4409 ++static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4410 ++{
4411 ++ vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4412 ++ vmcs_writel(GUEST_GDTR_BASE, dt->address);
4413 ++}
4414 ++
4415 ++static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4416 ++{
4417 ++ struct kvm_segment var;
4418 ++ u32 ar;
4419 ++
4420 ++ vmx_get_segment(vcpu, &var, seg);
4421 ++ var.dpl = 0x3;
4422 ++ if (seg == VCPU_SREG_CS)
4423 ++ var.type = 0x3;
4424 ++ ar = vmx_segment_access_rights(&var);
4425 ++
4426 ++ if (var.base != (var.selector << 4))
4427 ++ return false;
4428 ++ if (var.limit != 0xffff)
4429 ++ return false;
4430 ++ if (ar != 0xf3)
4431 ++ return false;
4432 ++
4433 ++ return true;
4434 ++}
4435 ++
4436 ++static bool code_segment_valid(struct kvm_vcpu *vcpu)
4437 ++{
4438 ++ struct kvm_segment cs;
4439 ++ unsigned int cs_rpl;
4440 ++
4441 ++ vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4442 ++ cs_rpl = cs.selector & SEGMENT_RPL_MASK;
4443 ++
4444 ++ if (cs.unusable)
4445 ++ return false;
4446 ++ if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
4447 ++ return false;
4448 ++ if (!cs.s)
4449 ++ return false;
4450 ++ if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
4451 ++ if (cs.dpl > cs_rpl)
4452 ++ return false;
4453 ++ } else {
4454 ++ if (cs.dpl != cs_rpl)
4455 ++ return false;
4456 ++ }
4457 ++ if (!cs.present)
4458 ++ return false;
4459 ++
4460 ++ /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4461 ++ return true;
4462 ++}
4463 ++
4464 ++static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4465 ++{
4466 ++ struct kvm_segment ss;
4467 ++ unsigned int ss_rpl;
4468 ++
4469 ++ vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4470 ++ ss_rpl = ss.selector & SEGMENT_RPL_MASK;
4471 ++
4472 ++ if (ss.unusable)
4473 ++ return true;
4474 ++ if (ss.type != 3 && ss.type != 7)
4475 ++ return false;
4476 ++ if (!ss.s)
4477 ++ return false;
4478 ++ if (ss.dpl != ss_rpl) /* DPL != RPL */
4479 ++ return false;
4480 ++ if (!ss.present)
4481 ++ return false;
4482 ++
4483 ++ return true;
4484 ++}
4485 ++
4486 ++static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4487 ++{
4488 ++ struct kvm_segment var;
4489 ++ unsigned int rpl;
4490 ++
4491 ++ vmx_get_segment(vcpu, &var, seg);
4492 ++ rpl = var.selector & SEGMENT_RPL_MASK;
4493 ++
4494 ++ if (var.unusable)
4495 ++ return true;
4496 ++ if (!var.s)
4497 ++ return false;
4498 ++ if (!var.present)
4499 ++ return false;
4500 ++ if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
4501 ++ if (var.dpl < rpl) /* DPL < RPL */
4502 ++ return false;
4503 ++ }
4504 ++
4505 ++ /* TODO: Add other members to kvm_segment_field to allow checking for other access
4506 ++ * rights flags
4507 ++ */
4508 ++ return true;
4509 ++}
4510 ++
4511 ++static bool tr_valid(struct kvm_vcpu *vcpu)
4512 ++{
4513 ++ struct kvm_segment tr;
4514 ++
4515 ++ vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4516 ++
4517 ++ if (tr.unusable)
4518 ++ return false;
4519 ++ if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4520 ++ return false;
4521 ++ if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
4522 ++ return false;
4523 ++ if (!tr.present)
4524 ++ return false;
4525 ++
4526 ++ return true;
4527 ++}
4528 ++
4529 ++static bool ldtr_valid(struct kvm_vcpu *vcpu)
4530 ++{
4531 ++ struct kvm_segment ldtr;
4532 ++
4533 ++ vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4534 ++
4535 ++ if (ldtr.unusable)
4536 ++ return true;
4537 ++ if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4538 ++ return false;
4539 ++ if (ldtr.type != 2)
4540 ++ return false;
4541 ++ if (!ldtr.present)
4542 ++ return false;
4543 ++
4544 ++ return true;
4545 ++}
4546 ++
4547 ++static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4548 ++{
4549 ++ struct kvm_segment cs, ss;
4550 ++
4551 ++ vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4552 ++ vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4553 ++
4554 ++ return ((cs.selector & SEGMENT_RPL_MASK) ==
4555 ++ (ss.selector & SEGMENT_RPL_MASK));
4556 ++}
4557 ++
4558 ++/*
4559 ++ * Check if guest state is valid. Returns true if valid, false if
4560 ++ * not.
4561 ++ * We assume that registers are always usable
4562 ++ */
4563 ++static bool guest_state_valid(struct kvm_vcpu *vcpu)
4564 ++{
4565 ++ if (enable_unrestricted_guest)
4566 ++ return true;
4567 ++
4568 ++ /* real mode guest state checks */
4569 ++ if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
4570 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4571 ++ return false;
4572 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4573 ++ return false;
4574 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4575 ++ return false;
4576 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4577 ++ return false;
4578 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4579 ++ return false;
4580 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4581 ++ return false;
4582 ++ } else {
4583 ++ /* protected mode guest state checks */
4584 ++ if (!cs_ss_rpl_check(vcpu))
4585 ++ return false;
4586 ++ if (!code_segment_valid(vcpu))
4587 ++ return false;
4588 ++ if (!stack_segment_valid(vcpu))
4589 ++ return false;
4590 ++ if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4591 ++ return false;
4592 ++ if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4593 ++ return false;
4594 ++ if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4595 ++ return false;
4596 ++ if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4597 ++ return false;
4598 ++ if (!tr_valid(vcpu))
4599 ++ return false;
4600 ++ if (!ldtr_valid(vcpu))
4601 ++ return false;
4602 ++ }
4603 ++ /* TODO:
4604 ++ * - Add checks on RIP
4605 ++ * - Add checks on RFLAGS
4606 ++ */
4607 ++
4608 ++ return true;
4609 ++}
4610 ++
4611 ++static int init_rmode_tss(struct kvm *kvm)
4612 ++{
4613 ++ gfn_t fn;
4614 ++ u16 data = 0;
4615 ++ int idx, r;
4616 ++
4617 ++ idx = srcu_read_lock(&kvm->srcu);
4618 ++ fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
4619 ++ r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4620 ++ if (r < 0)
4621 ++ goto out;
4622 ++ data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
4623 ++ r = kvm_write_guest_page(kvm, fn++, &data,
4624 ++ TSS_IOPB_BASE_OFFSET, sizeof(u16));
4625 ++ if (r < 0)
4626 ++ goto out;
4627 ++ r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4628 ++ if (r < 0)
4629 ++ goto out;
4630 ++ r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4631 ++ if (r < 0)
4632 ++ goto out;
4633 ++ data = ~0;
4634 ++ r = kvm_write_guest_page(kvm, fn, &data,
4635 ++ RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4636 ++ sizeof(u8));
4637 ++out:
4638 ++ srcu_read_unlock(&kvm->srcu, idx);
4639 ++ return r;
4640 ++}
4641 ++
4642 ++static int init_rmode_identity_map(struct kvm *kvm)
4643 ++{
4644 ++ struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
4645 ++ int i, idx, r = 0;
4646 ++ kvm_pfn_t identity_map_pfn;
4647 ++ u32 tmp;
4648 ++
4649 ++ /* Protect kvm_vmx->ept_identity_pagetable_done. */
4650 ++ mutex_lock(&kvm->slots_lock);
4651 ++
4652 ++ if (likely(kvm_vmx->ept_identity_pagetable_done))
4653 ++ goto out2;
4654 ++
4655 ++ if (!kvm_vmx->ept_identity_map_addr)
4656 ++ kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
4657 ++ identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
4658 ++
4659 ++ r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4660 ++ kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
4661 ++ if (r < 0)
4662 ++ goto out2;
4663 ++
4664 ++ idx = srcu_read_lock(&kvm->srcu);
4665 ++ r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4666 ++ if (r < 0)
4667 ++ goto out;
4668 ++ /* Set up identity-mapping pagetable for EPT in real mode */
4669 ++ for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4670 ++ tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4671 ++ _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4672 ++ r = kvm_write_guest_page(kvm, identity_map_pfn,
4673 ++ &tmp, i * sizeof(tmp), sizeof(tmp));
4674 ++ if (r < 0)
4675 ++ goto out;
4676 ++ }
4677 ++ kvm_vmx->ept_identity_pagetable_done = true;
4678 ++
4679 ++out:
4680 ++ srcu_read_unlock(&kvm->srcu, idx);
4681 ++
4682 ++out2:
4683 ++ mutex_unlock(&kvm->slots_lock);
4684 ++ return r;
4685 ++}
4686 ++
4687 ++static void seg_setup(int seg)
4688 ++{
4689 ++ const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4690 ++ unsigned int ar;
4691 ++
4692 ++ vmcs_write16(sf->selector, 0);
4693 ++ vmcs_writel(sf->base, 0);
4694 ++ vmcs_write32(sf->limit, 0xffff);
4695 ++ ar = 0x93;
4696 ++ if (seg == VCPU_SREG_CS)
4697 ++ ar |= 0x08; /* code segment */
4698 ++
4699 ++ vmcs_write32(sf->ar_bytes, ar);
4700 ++}
4701 ++
4702 ++static int alloc_apic_access_page(struct kvm *kvm)
4703 ++{
4704 ++ struct page *page;
4705 ++ int r = 0;
4706 ++
4707 ++ mutex_lock(&kvm->slots_lock);
4708 ++ if (kvm->arch.apic_access_page_done)
4709 ++ goto out;
4710 ++ r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4711 ++ APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
4712 ++ if (r)
4713 ++ goto out;
4714 ++
4715 ++ page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
4716 ++ if (is_error_page(page)) {
4717 ++ r = -EFAULT;
4718 ++ goto out;
4719 ++ }
4720 ++
4721 ++ /*
4722 ++ * Do not pin the page in memory, so that memory hot-unplug
4723 ++ * is able to migrate it.
4724 ++ */
4725 ++ put_page(page);
4726 ++ kvm->arch.apic_access_page_done = true;
4727 ++out:
4728 ++ mutex_unlock(&kvm->slots_lock);
4729 ++ return r;
4730 ++}
4731 ++
4732 ++int allocate_vpid(void)
4733 ++{
4734 ++ int vpid;
4735 ++
4736 ++ if (!enable_vpid)
4737 ++ return 0;
4738 ++ spin_lock(&vmx_vpid_lock);
4739 ++ vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4740 ++ if (vpid < VMX_NR_VPIDS)
4741 ++ __set_bit(vpid, vmx_vpid_bitmap);
4742 ++ else
4743 ++ vpid = 0;
4744 ++ spin_unlock(&vmx_vpid_lock);
4745 ++ return vpid;
4746 ++}
4747 ++
4748 ++void free_vpid(int vpid)
4749 ++{
4750 ++ if (!enable_vpid || vpid == 0)
4751 ++ return;
4752 ++ spin_lock(&vmx_vpid_lock);
4753 ++ __clear_bit(vpid, vmx_vpid_bitmap);
4754 ++ spin_unlock(&vmx_vpid_lock);
4755 ++}
4756 ++
4757 ++static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4758 ++ u32 msr, int type)
4759 ++{
4760 ++ int f = sizeof(unsigned long);
4761 ++
4762 ++ if (!cpu_has_vmx_msr_bitmap())
4763 ++ return;
4764 ++
4765 ++ if (static_branch_unlikely(&enable_evmcs))
4766 ++ evmcs_touch_msr_bitmap();
4767 ++
4768 ++ /*
4769 ++ * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4770 ++ * have the write-low and read-high bitmap offsets the wrong way round.
4771 ++ * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4772 ++ */
4773 ++ if (msr <= 0x1fff) {
4774 ++ if (type & MSR_TYPE_R)
4775 ++ /* read-low */
4776 ++ __clear_bit(msr, msr_bitmap + 0x000 / f);
4777 ++
4778 ++ if (type & MSR_TYPE_W)
4779 ++ /* write-low */
4780 ++ __clear_bit(msr, msr_bitmap + 0x800 / f);
4781 ++
4782 ++ } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4783 ++ msr &= 0x1fff;
4784 ++ if (type & MSR_TYPE_R)
4785 ++ /* read-high */
4786 ++ __clear_bit(msr, msr_bitmap + 0x400 / f);
4787 ++
4788 ++ if (type & MSR_TYPE_W)
4789 ++ /* write-high */
4790 ++ __clear_bit(msr, msr_bitmap + 0xc00 / f);
4791 ++
4792 ++ }
4793 ++}
4794 ++
4795 ++static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4796 ++ u32 msr, int type)
4797 ++{
4798 ++ int f = sizeof(unsigned long);
4799 ++
4800 ++ if (!cpu_has_vmx_msr_bitmap())
4801 ++ return;
4802 ++
4803 ++ if (static_branch_unlikely(&enable_evmcs))
4804 ++ evmcs_touch_msr_bitmap();
4805 ++
4806 ++ /*
4807 ++ * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4808 ++ * have the write-low and read-high bitmap offsets the wrong way round.
4809 ++ * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4810 ++ */
4811 ++ if (msr <= 0x1fff) {
4812 ++ if (type & MSR_TYPE_R)
4813 ++ /* read-low */
4814 ++ __set_bit(msr, msr_bitmap + 0x000 / f);
4815 ++
4816 ++ if (type & MSR_TYPE_W)
4817 ++ /* write-low */
4818 ++ __set_bit(msr, msr_bitmap + 0x800 / f);
4819 ++
4820 ++ } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4821 ++ msr &= 0x1fff;
4822 ++ if (type & MSR_TYPE_R)
4823 ++ /* read-high */
4824 ++ __set_bit(msr, msr_bitmap + 0x400 / f);
4825 ++
4826 ++ if (type & MSR_TYPE_W)
4827 ++ /* write-high */
4828 ++ __set_bit(msr, msr_bitmap + 0xc00 / f);
4829 ++
4830 ++ }
4831 ++}
4832 ++
4833 ++static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
4834 ++ u32 msr, int type, bool value)
4835 ++{
4836 ++ if (value)
4837 ++ vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4838 ++ else
4839 ++ vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4840 ++}
4841 ++
4842 ++static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
4843 ++{
4844 ++ u8 mode = 0;
4845 ++
4846 ++ if (cpu_has_secondary_exec_ctrls() &&
4847 ++ (secondary_exec_controls_get(to_vmx(vcpu)) &
4848 ++ SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4849 ++ mode |= MSR_BITMAP_MODE_X2APIC;
4850 ++ if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4851 ++ mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4852 ++ }
4853 ++
4854 ++ return mode;
4855 ++}
4856 ++
4857 ++static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
4858 ++ u8 mode)
4859 ++{
4860 ++ int msr;
4861 ++
4862 ++ for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
4863 ++ unsigned word = msr / BITS_PER_LONG;
4864 ++ msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
4865 ++ msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
4866 ++ }
4867 ++
4868 ++ if (mode & MSR_BITMAP_MODE_X2APIC) {
4869 ++ /*
4870 ++ * TPR reads and writes can be virtualized even if virtual interrupt
4871 ++ * delivery is not in use.
4872 ++ */
4873 ++ vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
4874 ++ if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
4875 ++ vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
4876 ++ vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
4877 ++ vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
4878 ++ }
4879 ++ }
4880 ++}
4881 ++
4882 ++void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
4883 ++{
4884 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4885 ++ unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4886 ++ u8 mode = vmx_msr_bitmap_mode(vcpu);
4887 ++ u8 changed = mode ^ vmx->msr_bitmap_mode;
4888 ++
4889 ++ if (!changed)
4890 ++ return;
4891 ++
4892 ++ if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
4893 ++ vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
4894 ++
4895 ++ vmx->msr_bitmap_mode = mode;
4896 ++}
4897 ++
4898 ++void pt_update_intercept_for_msr(struct vcpu_vmx *vmx)
4899 ++{
4900 ++ unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4901 ++ bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
4902 ++ u32 i;
4903 ++
4904 ++ vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_STATUS,
4905 ++ MSR_TYPE_RW, flag);
4906 ++ vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_BASE,
4907 ++ MSR_TYPE_RW, flag);
4908 ++ vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_MASK,
4909 ++ MSR_TYPE_RW, flag);
4910 ++ vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_CR3_MATCH,
4911 ++ MSR_TYPE_RW, flag);
4912 ++ for (i = 0; i < vmx->pt_desc.addr_range; i++) {
4913 ++ vmx_set_intercept_for_msr(msr_bitmap,
4914 ++ MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag);
4915 ++ vmx_set_intercept_for_msr(msr_bitmap,
4916 ++ MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag);
4917 ++ }
4918 ++}
4919 ++
4920 ++static bool vmx_get_enable_apicv(struct kvm *kvm)
4921 ++{
4922 ++ return enable_apicv;
4923 ++}
4924 ++
4925 ++static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
4926 ++{
4927 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4928 ++ void *vapic_page;
4929 ++ u32 vppr;
4930 ++ int rvi;
4931 ++
4932 ++ if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
4933 ++ !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
4934 ++ WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn))
4935 ++ return false;
4936 ++
4937 ++ rvi = vmx_get_rvi();
4938 ++
4939 ++ vapic_page = vmx->nested.virtual_apic_map.hva;
4940 ++ vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
4941 ++
4942 ++ return ((rvi & 0xf0) > (vppr & 0xf0));
4943 ++}
4944 ++
4945 ++static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
4946 ++ bool nested)
4947 ++{
4948 ++#ifdef CONFIG_SMP
4949 ++ int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
4950 ++
4951 ++ if (vcpu->mode == IN_GUEST_MODE) {
4952 ++ /*
4953 ++ * The vector of interrupt to be delivered to vcpu had
4954 ++ * been set in PIR before this function.
4955 ++ *
4956 ++ * Following cases will be reached in this block, and
4957 ++ * we always send a notification event in all cases as
4958 ++ * explained below.
4959 ++ *
4960 ++ * Case 1: vcpu keeps in non-root mode. Sending a
4961 ++ * notification event posts the interrupt to vcpu.
4962 ++ *
4963 ++ * Case 2: vcpu exits to root mode and is still
4964 ++ * runnable. PIR will be synced to vIRR before the
4965 ++ * next vcpu entry. Sending a notification event in
4966 ++ * this case has no effect, as vcpu is not in root
4967 ++ * mode.
4968 ++ *
4969 ++ * Case 3: vcpu exits to root mode and is blocked.
4970 ++ * vcpu_block() has already synced PIR to vIRR and
4971 ++ * never blocks vcpu if vIRR is not cleared. Therefore,
4972 ++ * a blocked vcpu here does not wait for any requested
4973 ++ * interrupts in PIR, and sending a notification event
4974 ++ * which has no effect is safe here.
4975 ++ */
4976 ++
4977 ++ apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
4978 ++ return true;
4979 ++ }
4980 ++#endif
4981 ++ return false;
4982 ++}
4983 ++
4984 ++static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4985 ++ int vector)
4986 ++{
4987 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4988 ++
4989 ++ if (is_guest_mode(vcpu) &&
4990 ++ vector == vmx->nested.posted_intr_nv) {
4991 ++ /*
4992 ++ * If a posted intr is not recognized by hardware,
4993 ++ * we will accomplish it in the next vmentry.
4994 ++ */
4995 ++ vmx->nested.pi_pending = true;
4996 ++ kvm_make_request(KVM_REQ_EVENT, vcpu);
4997 ++ /* the PIR and ON have been set by L1. */
4998 ++ if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
4999 ++ kvm_vcpu_kick(vcpu);
5000 ++ return 0;
5001 ++ }
5002 ++ return -1;
5003 ++}
5004 ++/*
5005 ++ * Send interrupt to vcpu via posted interrupt way.
5006 ++ * 1. If target vcpu is running(non-root mode), send posted interrupt
5007 ++ * notification to vcpu and hardware will sync PIR to vIRR atomically.
5008 ++ * 2. If target vcpu isn't running(root mode), kick it to pick up the
5009 ++ * interrupt from PIR in next vmentry.
5010 ++ */
5011 ++static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5012 ++{
5013 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5014 ++ int r;
5015 ++
5016 ++ r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5017 ++ if (!r)
5018 ++ return;
5019 ++
5020 ++ if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5021 ++ return;
5022 ++
5023 ++ /* If a previous notification has sent the IPI, nothing to do. */
5024 ++ if (pi_test_and_set_on(&vmx->pi_desc))
5025 ++ return;
5026 ++
5027 ++ if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
5028 ++ kvm_vcpu_kick(vcpu);
5029 ++}
5030 ++
5031 ++/*
5032 ++ * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5033 ++ * will not change in the lifetime of the guest.
5034 ++ * Note that host-state that does change is set elsewhere. E.g., host-state
5035 ++ * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5036 ++ */
5037 ++void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
5038 ++{
5039 ++ u32 low32, high32;
5040 ++ unsigned long tmpl;
5041 ++ unsigned long cr0, cr3, cr4;
5042 ++
5043 ++ cr0 = read_cr0();
5044 ++ WARN_ON(cr0 & X86_CR0_TS);
5045 ++ vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
5046 ++
5047 ++ /*
5048 ++ * Save the most likely value for this task's CR3 in the VMCS.
5049 ++ * We can't use __get_current_cr3_fast() because we're not atomic.
5050 ++ */
5051 ++ cr3 = __read_cr3();
5052 ++ vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
5053 ++ vmx->loaded_vmcs->host_state.cr3 = cr3;
5054 ++
5055 ++ /* Save the most likely value for this task's CR4 in the VMCS. */
5056 ++ cr4 = cr4_read_shadow();
5057 ++ vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5058 ++ vmx->loaded_vmcs->host_state.cr4 = cr4;
5059 ++
5060 ++ vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
5061 ++#ifdef CONFIG_X86_64
5062 ++ /*
5063 ++ * Load null selectors, so we can avoid reloading them in
5064 ++ * vmx_prepare_switch_to_host(), in case userspace uses
5065 ++ * the null selectors too (the expected case).
5066 ++ */
5067 ++ vmcs_write16(HOST_DS_SELECTOR, 0);
5068 ++ vmcs_write16(HOST_ES_SELECTOR, 0);
5069 ++#else
5070 ++ vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5071 ++ vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5072 ++#endif
5073 ++ vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5074 ++ vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5075 ++
5076 ++ vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */
5077 ++
5078 ++ vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */
5079 ++
5080 ++ rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5081 ++ vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5082 ++ rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5083 ++ vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5084 ++
5085 ++ if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5086 ++ rdmsr(MSR_IA32_CR_PAT, low32, high32);
5087 ++ vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5088 ++ }
5089 ++
5090 ++ if (cpu_has_load_ia32_efer())
5091 ++ vmcs_write64(HOST_IA32_EFER, host_efer);
5092 ++}
5093 ++
5094 ++void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5095 ++{
5096 ++ vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5097 ++ if (enable_ept)
5098 ++ vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
5099 ++ if (is_guest_mode(&vmx->vcpu))
5100 ++ vmx->vcpu.arch.cr4_guest_owned_bits &=
5101 ++ ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
5102 ++ vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5103 ++}
5104 ++
5105 ++u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5106 ++{
5107 ++ u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5108 ++
5109 ++ if (!kvm_vcpu_apicv_active(&vmx->vcpu))
5110 ++ pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
5111 ++
5112 ++ if (!enable_vnmi)
5113 ++ pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5114 ++
5115 ++ if (!enable_preemption_timer)
5116 ++ pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
5117 ++
5118 ++ return pin_based_exec_ctrl;
5119 ++}
5120 ++
5121 ++static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5122 ++{
5123 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5124 ++
5125 ++ pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
5126 ++ if (cpu_has_secondary_exec_ctrls()) {
5127 ++ if (kvm_vcpu_apicv_active(vcpu))
5128 ++ secondary_exec_controls_setbit(vmx,
5129 ++ SECONDARY_EXEC_APIC_REGISTER_VIRT |
5130 ++ SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5131 ++ else
5132 ++ secondary_exec_controls_clearbit(vmx,
5133 ++ SECONDARY_EXEC_APIC_REGISTER_VIRT |
5134 ++ SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5135 ++ }
5136 ++
5137 ++ if (cpu_has_vmx_msr_bitmap())
5138 ++ vmx_update_msr_bitmap(vcpu);
5139 ++}
5140 ++
5141 ++u32 vmx_exec_control(struct vcpu_vmx *vmx)
5142 ++{
5143 ++ u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
5144 ++
5145 ++ if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5146 ++ exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5147 ++
5148 ++ if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
5149 ++ exec_control &= ~CPU_BASED_TPR_SHADOW;
5150 ++#ifdef CONFIG_X86_64
5151 ++ exec_control |= CPU_BASED_CR8_STORE_EXITING |
5152 ++ CPU_BASED_CR8_LOAD_EXITING;
5153 ++#endif
5154 ++ }
5155 ++ if (!enable_ept)
5156 ++ exec_control |= CPU_BASED_CR3_STORE_EXITING |
5157 ++ CPU_BASED_CR3_LOAD_EXITING |
5158 ++ CPU_BASED_INVLPG_EXITING;
5159 ++ if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5160 ++ exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5161 ++ CPU_BASED_MONITOR_EXITING);
5162 ++ if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5163 ++ exec_control &= ~CPU_BASED_HLT_EXITING;
5164 ++ return exec_control;
5165 ++}
5166 ++
5167 ++
5168 ++static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
5169 ++{
5170 ++ struct kvm_vcpu *vcpu = &vmx->vcpu;
5171 ++
5172 ++ u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
5173 ++
5174 ++ if (pt_mode == PT_MODE_SYSTEM)
5175 ++ exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX);
5176 ++ if (!cpu_need_virtualize_apic_accesses(vcpu))
5177 ++ exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5178 ++ if (vmx->vpid == 0)
5179 ++ exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5180 ++ if (!enable_ept) {
5181 ++ exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5182 ++ enable_unrestricted_guest = 0;
5183 ++ }
5184 ++ if (!enable_unrestricted_guest)
5185 ++ exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5186 ++ if (kvm_pause_in_guest(vmx->vcpu.kvm))
5187 ++ exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
5188 ++ if (!kvm_vcpu_apicv_active(vcpu))
5189 ++ exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5190 ++ SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5191 ++ exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
5192 ++
5193 ++ /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5194 ++ * in vmx_set_cr4. */
5195 ++ exec_control &= ~SECONDARY_EXEC_DESC;
5196 ++
5197 ++ /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5198 ++ (handle_vmptrld).
5199 ++ We can NOT enable shadow_vmcs here because we don't have yet
5200 ++ a current VMCS12
5201 ++ */
5202 ++ exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
5203 ++
5204 ++ if (!enable_pml)
5205 ++ exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
5206 ++
5207 ++ if (vmx_xsaves_supported()) {
5208 ++ /* Exposing XSAVES only when XSAVE is exposed */
5209 ++ bool xsaves_enabled =
5210 ++ guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5211 ++ guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5212 ++
5213 ++ vcpu->arch.xsaves_enabled = xsaves_enabled;
5214 ++
5215 ++ if (!xsaves_enabled)
5216 ++ exec_control &= ~SECONDARY_EXEC_XSAVES;
5217 ++
5218 ++ if (nested) {
5219 ++ if (xsaves_enabled)
5220 ++ vmx->nested.msrs.secondary_ctls_high |=
5221 ++ SECONDARY_EXEC_XSAVES;
5222 ++ else
5223 ++ vmx->nested.msrs.secondary_ctls_high &=
5224 ++ ~SECONDARY_EXEC_XSAVES;
5225 ++ }
5226 ++ }
5227 ++
5228 ++ if (vmx_rdtscp_supported()) {
5229 ++ bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5230 ++ if (!rdtscp_enabled)
5231 ++ exec_control &= ~SECONDARY_EXEC_RDTSCP;
5232 ++
5233 ++ if (nested) {
5234 ++ if (rdtscp_enabled)
5235 ++ vmx->nested.msrs.secondary_ctls_high |=
5236 ++ SECONDARY_EXEC_RDTSCP;
5237 ++ else
5238 ++ vmx->nested.msrs.secondary_ctls_high &=
5239 ++ ~SECONDARY_EXEC_RDTSCP;
5240 ++ }
5241 ++ }
5242 ++
5243 ++ if (vmx_invpcid_supported()) {
5244 ++ /* Exposing INVPCID only when PCID is exposed */
5245 ++ bool invpcid_enabled =
5246 ++ guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5247 ++ guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5248 ++
5249 ++ if (!invpcid_enabled) {
5250 ++ exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5251 ++ guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5252 ++ }
5253 ++
5254 ++ if (nested) {
5255 ++ if (invpcid_enabled)
5256 ++ vmx->nested.msrs.secondary_ctls_high |=
5257 ++ SECONDARY_EXEC_ENABLE_INVPCID;
5258 ++ else
5259 ++ vmx->nested.msrs.secondary_ctls_high &=
5260 ++ ~SECONDARY_EXEC_ENABLE_INVPCID;
5261 ++ }
5262 ++ }
5263 ++
5264 ++ if (vmx_rdrand_supported()) {
5265 ++ bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5266 ++ if (rdrand_enabled)
5267 ++ exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
5268 ++
5269 ++ if (nested) {
5270 ++ if (rdrand_enabled)
5271 ++ vmx->nested.msrs.secondary_ctls_high |=
5272 ++ SECONDARY_EXEC_RDRAND_EXITING;
5273 ++ else
5274 ++ vmx->nested.msrs.secondary_ctls_high &=
5275 ++ ~SECONDARY_EXEC_RDRAND_EXITING;
5276 ++ }
5277 ++ }
5278 ++
5279 ++ if (vmx_rdseed_supported()) {
5280 ++ bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5281 ++ if (rdseed_enabled)
5282 ++ exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
5283 ++
5284 ++ if (nested) {
5285 ++ if (rdseed_enabled)
5286 ++ vmx->nested.msrs.secondary_ctls_high |=
5287 ++ SECONDARY_EXEC_RDSEED_EXITING;
5288 ++ else
5289 ++ vmx->nested.msrs.secondary_ctls_high &=
5290 ++ ~SECONDARY_EXEC_RDSEED_EXITING;
5291 ++ }
5292 ++ }
5293 ++
5294 ++ if (vmx_waitpkg_supported()) {
5295 ++ bool waitpkg_enabled =
5296 ++ guest_cpuid_has(vcpu, X86_FEATURE_WAITPKG);
5297 ++
5298 ++ if (!waitpkg_enabled)
5299 ++ exec_control &= ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
5300 ++
5301 ++ if (nested) {
5302 ++ if (waitpkg_enabled)
5303 ++ vmx->nested.msrs.secondary_ctls_high |=
5304 ++ SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
5305 ++ else
5306 ++ vmx->nested.msrs.secondary_ctls_high &=
5307 ++ ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
5308 ++ }
5309 ++ }
5310 ++
5311 ++ vmx->secondary_exec_control = exec_control;
5312 ++}
5313 ++
5314 ++static void ept_set_mmio_spte_mask(void)
5315 ++{
5316 ++ /*
5317 ++ * EPT Misconfigurations can be generated if the value of bits 2:0
5318 ++ * of an EPT paging-structure entry is 110b (write/execute).
5319 ++ */
5320 ++ kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5321 ++ VMX_EPT_MISCONFIG_WX_VALUE, 0);
5322 ++}
5323 ++
5324 ++#define VMX_XSS_EXIT_BITMAP 0
5325 ++
5326 ++/*
5327 ++ * Noting that the initialization of Guest-state Area of VMCS is in
5328 ++ * vmx_vcpu_reset().
5329 ++ */
5330 ++static void init_vmcs(struct vcpu_vmx *vmx)
5331 ++{
5332 ++ if (nested)
5333 ++ nested_vmx_set_vmcs_shadowing_bitmap();
5334 ++
5335 ++ if (cpu_has_vmx_msr_bitmap())
5336 ++ vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
5337 ++
5338 ++ vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5339 ++
5340 ++ /* Control */
5341 ++ pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
5342 ++
5343 ++ exec_controls_set(vmx, vmx_exec_control(vmx));
5344 ++
5345 ++ if (cpu_has_secondary_exec_ctrls()) {
5346 ++ vmx_compute_secondary_exec_control(vmx);
5347 ++ secondary_exec_controls_set(vmx, vmx->secondary_exec_control);
5348 ++ }
5349 ++
5350 ++ if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
5351 ++ vmcs_write64(EOI_EXIT_BITMAP0, 0);
5352 ++ vmcs_write64(EOI_EXIT_BITMAP1, 0);
5353 ++ vmcs_write64(EOI_EXIT_BITMAP2, 0);
5354 ++ vmcs_write64(EOI_EXIT_BITMAP3, 0);
5355 ++
5356 ++ vmcs_write16(GUEST_INTR_STATUS, 0);
5357 ++
5358 ++ vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
5359 ++ vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
5360 ++ }
5361 ++
5362 ++ if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
5363 ++ vmcs_write32(PLE_GAP, ple_gap);
5364 ++ vmx->ple_window = ple_window;
5365 ++ vmx->ple_window_dirty = true;
5366 ++ }
5367 ++
5368 ++ vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5369 ++ vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
5370 ++ vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5371 ++
5372 ++ vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5373 ++ vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
5374 ++ vmx_set_constant_host_state(vmx);
5375 ++ vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5376 ++ vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5377 ++
5378 ++ if (cpu_has_vmx_vmfunc())
5379 ++ vmcs_write64(VM_FUNCTION_CONTROL, 0);
5380 ++
5381 ++ vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5382 ++ vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
5383 ++ vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
5384 ++ vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
5385 ++ vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
5386 ++
5387 ++ if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5388 ++ vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
5389 ++
5390 ++ vm_exit_controls_set(vmx, vmx_vmexit_ctrl());
5391 ++
5392 ++ /* 22.2.1, 20.8.1 */
5393 ++ vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
5394 ++
5395 ++ vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5396 ++ vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5397 ++
5398 ++ set_cr4_guest_host_mask(vmx);
5399 ++
5400 ++ if (vmx->vpid != 0)
5401 ++ vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5402 ++
5403 ++ if (vmx_xsaves_supported())
5404 ++ vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5405 ++
5406 ++ if (enable_pml) {
5407 ++ vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5408 ++ vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5409 ++ }
5410 ++
5411 ++ if (cpu_has_vmx_encls_vmexit())
5412 ++ vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
5413 ++
5414 ++ if (pt_mode == PT_MODE_HOST_GUEST) {
5415 ++ memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc));
5416 ++ /* Bit[6~0] are forced to 1, writes are ignored. */
5417 ++ vmx->pt_desc.guest.output_mask = 0x7F;
5418 ++ vmcs_write64(GUEST_IA32_RTIT_CTL, 0);
5419 ++ }
5420 ++}
5421 ++
5422 ++static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
5423 ++{
5424 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5425 ++ struct msr_data apic_base_msr;
5426 ++ u64 cr0;
5427 ++
5428 ++ vmx->rmode.vm86_active = 0;
5429 ++ vmx->spec_ctrl = 0;
5430 ++
5431 ++ vmx->msr_ia32_umwait_control = 0;
5432 ++
5433 ++ vcpu->arch.microcode_version = 0x100000000ULL;
5434 ++ vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
5435 ++ vmx->hv_deadline_tsc = -1;
5436 ++ kvm_set_cr8(vcpu, 0);
5437 ++
5438 ++ if (!init_event) {
5439 ++ apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5440 ++ MSR_IA32_APICBASE_ENABLE;
5441 ++ if (kvm_vcpu_is_reset_bsp(vcpu))
5442 ++ apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5443 ++ apic_base_msr.host_initiated = true;
5444 ++ kvm_set_apic_base(vcpu, &apic_base_msr);
5445 ++ }
5446 ++
5447 ++ vmx_segment_cache_clear(vmx);
5448 ++
5449 ++ seg_setup(VCPU_SREG_CS);
5450 ++ vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
5451 ++ vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
5452 ++
5453 ++ seg_setup(VCPU_SREG_DS);
5454 ++ seg_setup(VCPU_SREG_ES);
5455 ++ seg_setup(VCPU_SREG_FS);
5456 ++ seg_setup(VCPU_SREG_GS);
5457 ++ seg_setup(VCPU_SREG_SS);
5458 ++
5459 ++ vmcs_write16(GUEST_TR_SELECTOR, 0);
5460 ++ vmcs_writel(GUEST_TR_BASE, 0);
5461 ++ vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5462 ++ vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5463 ++
5464 ++ vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5465 ++ vmcs_writel(GUEST_LDTR_BASE, 0);
5466 ++ vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5467 ++ vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5468 ++
5469 ++ if (!init_event) {
5470 ++ vmcs_write32(GUEST_SYSENTER_CS, 0);
5471 ++ vmcs_writel(GUEST_SYSENTER_ESP, 0);
5472 ++ vmcs_writel(GUEST_SYSENTER_EIP, 0);
5473 ++ vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5474 ++ }
5475 ++
5476 ++ kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
5477 ++ kvm_rip_write(vcpu, 0xfff0);
5478 ++
5479 ++ vmcs_writel(GUEST_GDTR_BASE, 0);
5480 ++ vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5481 ++
5482 ++ vmcs_writel(GUEST_IDTR_BASE, 0);
5483 ++ vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5484 ++
5485 ++ vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
5486 ++ vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
5487 ++ vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
5488 ++ if (kvm_mpx_supported())
5489 ++ vmcs_write64(GUEST_BNDCFGS, 0);
5490 ++
5491 ++ setup_msrs(vmx);
5492 ++
5493 ++ vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5494 ++
5495 ++ if (cpu_has_vmx_tpr_shadow() && !init_event) {
5496 ++ vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
5497 ++ if (cpu_need_tpr_shadow(vcpu))
5498 ++ vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
5499 ++ __pa(vcpu->arch.apic->regs));
5500 ++ vmcs_write32(TPR_THRESHOLD, 0);
5501 ++ }
5502 ++
5503 ++ kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
5504 ++
5505 ++ cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
5506 ++ vmx->vcpu.arch.cr0 = cr0;
5507 ++ vmx_set_cr0(vcpu, cr0); /* enter rmode */
5508 ++ vmx_set_cr4(vcpu, 0);
5509 ++ vmx_set_efer(vcpu, 0);
5510 ++
5511 ++ update_exception_bitmap(vcpu);
5512 ++
5513 ++ vpid_sync_context(vmx->vpid);
5514 ++ if (init_event)
5515 ++ vmx_clear_hlt(vcpu);
5516 ++}
5517 ++
5518 ++static void enable_irq_window(struct kvm_vcpu *vcpu)
5519 ++{
5520 ++ exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
5521 ++}
5522 ++
5523 ++static void enable_nmi_window(struct kvm_vcpu *vcpu)
5524 ++{
5525 ++ if (!enable_vnmi ||
5526 ++ vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5527 ++ enable_irq_window(vcpu);
5528 ++ return;
5529 ++ }
5530 ++
5531 ++ exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
5532 ++}
5533 ++
5534 ++static void vmx_inject_irq(struct kvm_vcpu *vcpu)
5535 ++{
5536 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5537 ++ uint32_t intr;
5538 ++ int irq = vcpu->arch.interrupt.nr;
5539 ++
5540 ++ trace_kvm_inj_virq(irq);
5541 ++
5542 ++ ++vcpu->stat.irq_injections;
5543 ++ if (vmx->rmode.vm86_active) {
5544 ++ int inc_eip = 0;
5545 ++ if (vcpu->arch.interrupt.soft)
5546 ++ inc_eip = vcpu->arch.event_exit_inst_len;
5547 ++ kvm_inject_realmode_interrupt(vcpu, irq, inc_eip);
5548 ++ return;
5549 ++ }
5550 ++ intr = irq | INTR_INFO_VALID_MASK;
5551 ++ if (vcpu->arch.interrupt.soft) {
5552 ++ intr |= INTR_TYPE_SOFT_INTR;
5553 ++ vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5554 ++ vmx->vcpu.arch.event_exit_inst_len);
5555 ++ } else
5556 ++ intr |= INTR_TYPE_EXT_INTR;
5557 ++ vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
5558 ++
5559 ++ vmx_clear_hlt(vcpu);
5560 ++}
5561 ++
5562 ++static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5563 ++{
5564 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5565 ++
5566 ++ if (!enable_vnmi) {
5567 ++ /*
5568 ++ * Tracking the NMI-blocked state in software is built upon
5569 ++ * finding the next open IRQ window. This, in turn, depends on
5570 ++ * well-behaving guests: They have to keep IRQs disabled at
5571 ++ * least as long as the NMI handler runs. Otherwise we may
5572 ++ * cause NMI nesting, maybe breaking the guest. But as this is
5573 ++ * highly unlikely, we can live with the residual risk.
5574 ++ */
5575 ++ vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5576 ++ vmx->loaded_vmcs->vnmi_blocked_time = 0;
5577 ++ }
5578 ++
5579 ++ ++vcpu->stat.nmi_injections;
5580 ++ vmx->loaded_vmcs->nmi_known_unmasked = false;
5581 ++
5582 ++ if (vmx->rmode.vm86_active) {
5583 ++ kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0);
5584 ++ return;
5585 ++ }
5586 ++
5587 ++ vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5588 ++ INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
5589 ++
5590 ++ vmx_clear_hlt(vcpu);
5591 ++}
5592 ++
5593 ++bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5594 ++{
5595 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5596 ++ bool masked;
5597 ++
5598 ++ if (!enable_vnmi)
5599 ++ return vmx->loaded_vmcs->soft_vnmi_blocked;
5600 ++ if (vmx->loaded_vmcs->nmi_known_unmasked)
5601 ++ return false;
5602 ++ masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5603 ++ vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5604 ++ return masked;
5605 ++}
5606 ++
5607 ++void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5608 ++{
5609 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5610 ++
5611 ++ if (!enable_vnmi) {
5612 ++ if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5613 ++ vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5614 ++ vmx->loaded_vmcs->vnmi_blocked_time = 0;
5615 ++ }
5616 ++ } else {
5617 ++ vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5618 ++ if (masked)
5619 ++ vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5620 ++ GUEST_INTR_STATE_NMI);
5621 ++ else
5622 ++ vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5623 ++ GUEST_INTR_STATE_NMI);
5624 ++ }
5625 ++}
5626 ++
5627 ++static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5628 ++{
5629 ++ if (to_vmx(vcpu)->nested.nested_run_pending)
5630 ++ return 0;
5631 ++
5632 ++ if (!enable_vnmi &&
5633 ++ to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5634 ++ return 0;
5635 ++
5636 ++ return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5637 ++ (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5638 ++ | GUEST_INTR_STATE_NMI));
5639 ++}
5640 ++
5641 ++static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5642 ++{
5643 ++ return (!to_vmx(vcpu)->nested.nested_run_pending &&
5644 ++ vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
5645 ++ !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5646 ++ (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
5647 ++}
5648 ++
5649 ++static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5650 ++{
5651 ++ int ret;
5652 ++
5653 ++ if (enable_unrestricted_guest)
5654 ++ return 0;
5655 ++
5656 ++ ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5657 ++ PAGE_SIZE * 3);
5658 ++ if (ret)
5659 ++ return ret;
5660 ++ to_kvm_vmx(kvm)->tss_addr = addr;
5661 ++ return init_rmode_tss(kvm);
5662 ++}
5663 ++
5664 ++static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
5665 ++{
5666 ++ to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
5667 ++ return 0;
5668 ++}
5669 ++
5670 ++static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
5671 ++{
5672 ++ switch (vec) {
5673 ++ case BP_VECTOR:
5674 ++ /*
5675 ++ * Update instruction length as we may reinject the exception
5676 ++ * from user space while in guest debugging mode.
5677 ++ */
5678 ++ to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5679 ++ vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5680 ++ if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5681 ++ return false;
5682 ++ /* fall through */
5683 ++ case DB_VECTOR:
5684 ++ if (vcpu->guest_debug &
5685 ++ (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5686 ++ return false;
5687 ++ /* fall through */
5688 ++ case DE_VECTOR:
5689 ++ case OF_VECTOR:
5690 ++ case BR_VECTOR:
5691 ++ case UD_VECTOR:
5692 ++ case DF_VECTOR:
5693 ++ case SS_VECTOR:
5694 ++ case GP_VECTOR:
5695 ++ case MF_VECTOR:
5696 ++ return true;
5697 ++ break;
5698 ++ }
5699 ++ return false;
5700 ++}
5701 ++
5702 ++static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5703 ++ int vec, u32 err_code)
5704 ++{
5705 ++ /*
5706 ++ * Instruction with address size override prefix opcode 0x67
5707 ++ * Cause the #SS fault with 0 error code in VM86 mode.
5708 ++ */
5709 ++ if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5710 ++ if (kvm_emulate_instruction(vcpu, 0)) {
5711 ++ if (vcpu->arch.halt_request) {
5712 ++ vcpu->arch.halt_request = 0;
5713 ++ return kvm_vcpu_halt(vcpu);
5714 ++ }
5715 ++ return 1;
5716 ++ }
5717 ++ return 0;
5718 ++ }
5719 ++
5720 ++ /*
5721 ++ * Forward all other exceptions that are valid in real mode.
5722 ++ * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5723 ++ * the required debugging infrastructure rework.
5724 ++ */
5725 ++ kvm_queue_exception(vcpu, vec);
5726 ++ return 1;
5727 ++}
5728 ++
5729 ++/*
5730 ++ * Trigger machine check on the host. We assume all the MSRs are already set up
5731 ++ * by the CPU and that we still run on the same CPU as the MCE occurred on.
5732 ++ * We pass a fake environment to the machine check handler because we want
5733 ++ * the guest to be always treated like user space, no matter what context
5734 ++ * it used internally.
5735 ++ */
5736 ++static void kvm_machine_check(void)
5737 ++{
5738 ++#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5739 ++ struct pt_regs regs = {
5740 ++ .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5741 ++ .flags = X86_EFLAGS_IF,
5742 ++ };
5743 ++
5744 ++ do_machine_check(&regs, 0);
5745 ++#endif
5746 ++}
5747 ++
5748 ++static int handle_machine_check(struct kvm_vcpu *vcpu)
5749 ++{
5750 ++ /* handled by vmx_vcpu_run() */
5751 ++ return 1;
5752 ++}
5753 ++
5754 ++static int handle_exception_nmi(struct kvm_vcpu *vcpu)
5755 ++{
5756 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5757 ++ struct kvm_run *kvm_run = vcpu->run;
5758 ++ u32 intr_info, ex_no, error_code;
5759 ++ unsigned long cr2, rip, dr6;
5760 ++ u32 vect_info;
5761 ++
5762 ++ vect_info = vmx->idt_vectoring_info;
5763 ++ intr_info = vmx->exit_intr_info;
5764 ++
5765 ++ if (is_machine_check(intr_info) || is_nmi(intr_info))
5766 ++ return 1; /* handled by handle_exception_nmi_irqoff() */
5767 ++
5768 ++ if (is_invalid_opcode(intr_info))
5769 ++ return handle_ud(vcpu);
5770 ++
5771 ++ error_code = 0;
5772 ++ if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
5773 ++ error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5774 ++
5775 ++ if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
5776 ++ WARN_ON_ONCE(!enable_vmware_backdoor);
5777 ++
5778 ++ /*
5779 ++ * VMware backdoor emulation on #GP interception only handles
5780 ++ * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero
5781 ++ * error code on #GP.
5782 ++ */
5783 ++ if (error_code) {
5784 ++ kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
5785 ++ return 1;
5786 ++ }
5787 ++ return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
5788 ++ }
5789 ++
5790 ++ /*
5791 ++ * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5792 ++ * MMIO, it is better to report an internal error.
5793 ++ * See the comments in vmx_handle_exit.
5794 ++ */
5795 ++ if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5796 ++ !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5797 ++ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5798 ++ vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
5799 ++ vcpu->run->internal.ndata = 3;
5800 ++ vcpu->run->internal.data[0] = vect_info;
5801 ++ vcpu->run->internal.data[1] = intr_info;
5802 ++ vcpu->run->internal.data[2] = error_code;
5803 ++ return 0;
5804 ++ }
5805 ++
5806 ++ if (is_page_fault(intr_info)) {
5807 ++ cr2 = vmcs_readl(EXIT_QUALIFICATION);
5808 ++ /* EPT won't cause page fault directly */
5809 ++ WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
5810 ++ return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
5811 ++ }
5812 ++
5813 ++ ex_no = intr_info & INTR_INFO_VECTOR_MASK;
5814 ++
5815 ++ if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5816 ++ return handle_rmode_exception(vcpu, ex_no, error_code);
5817 ++
5818 ++ switch (ex_no) {
5819 ++ case AC_VECTOR:
5820 ++ kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5821 ++ return 1;
5822 ++ case DB_VECTOR:
5823 ++ dr6 = vmcs_readl(EXIT_QUALIFICATION);
5824 ++ if (!(vcpu->guest_debug &
5825 ++ (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
5826 ++ vcpu->arch.dr6 &= ~DR_TRAP_BITS;
5827 ++ vcpu->arch.dr6 |= dr6 | DR6_RTM;
5828 ++ if (is_icebp(intr_info))
5829 ++ WARN_ON(!skip_emulated_instruction(vcpu));
5830 ++
5831 ++ kvm_queue_exception(vcpu, DB_VECTOR);
5832 ++ return 1;
5833 ++ }
5834 ++ kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5835 ++ kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5836 ++ /* fall through */
5837 ++ case BP_VECTOR:
5838 ++ /*
5839 ++ * Update instruction length as we may reinject #BP from
5840 ++ * user space while in guest debugging mode. Reading it for
5841 ++ * #DB as well causes no harm, it is not used in that case.
5842 ++ */
5843 ++ vmx->vcpu.arch.event_exit_inst_len =
5844 ++ vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5845 ++ kvm_run->exit_reason = KVM_EXIT_DEBUG;
5846 ++ rip = kvm_rip_read(vcpu);
5847 ++ kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5848 ++ kvm_run->debug.arch.exception = ex_no;
5849 ++ break;
5850 ++ default:
5851 ++ kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5852 ++ kvm_run->ex.exception = ex_no;
5853 ++ kvm_run->ex.error_code = error_code;
5854 ++ break;
5855 ++ }
5856 ++ return 0;
5857 ++}
5858 ++
5859 ++static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
5860 ++{
5861 ++ ++vcpu->stat.irq_exits;
5862 ++ return 1;
5863 ++}
5864 ++
5865 ++static int handle_triple_fault(struct kvm_vcpu *vcpu)
5866 ++{
5867 ++ vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
5868 ++ vcpu->mmio_needed = 0;
5869 ++ return 0;
5870 ++}
5871 ++
5872 ++static int handle_io(struct kvm_vcpu *vcpu)
5873 ++{
5874 ++ unsigned long exit_qualification;
5875 ++ int size, in, string;
5876 ++ unsigned port;
5877 ++
5878 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5879 ++ string = (exit_qualification & 16) != 0;
5880 ++
5881 ++ ++vcpu->stat.io_exits;
5882 ++
5883 ++ if (string)
5884 ++ return kvm_emulate_instruction(vcpu, 0);
5885 ++
5886 ++ port = exit_qualification >> 16;
5887 ++ size = (exit_qualification & 7) + 1;
5888 ++ in = (exit_qualification & 8) != 0;
5889 ++
5890 ++ return kvm_fast_pio(vcpu, size, port, in);
5891 ++}
5892 ++
5893 ++static void
5894 ++vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5895 ++{
5896 ++ /*
5897 ++ * Patch in the VMCALL instruction:
5898 ++ */
5899 ++ hypercall[0] = 0x0f;
5900 ++ hypercall[1] = 0x01;
5901 ++ hypercall[2] = 0xc1;
5902 ++}
5903 ++
5904 ++/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
5905 ++static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5906 ++{
5907 ++ if (is_guest_mode(vcpu)) {
5908 ++ struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5909 ++ unsigned long orig_val = val;
5910 ++
5911 ++ /*
5912 ++ * We get here when L2 changed cr0 in a way that did not change
5913 ++ * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
5914 ++ * but did change L0 shadowed bits. So we first calculate the
5915 ++ * effective cr0 value that L1 would like to write into the
5916 ++ * hardware. It consists of the L2-owned bits from the new
5917 ++ * value combined with the L1-owned bits from L1's guest_cr0.
5918 ++ */
5919 ++ val = (val & ~vmcs12->cr0_guest_host_mask) |
5920 ++ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5921 ++
5922 ++ if (!nested_guest_cr0_valid(vcpu, val))
5923 ++ return 1;
5924 ++
5925 ++ if (kvm_set_cr0(vcpu, val))
5926 ++ return 1;
5927 ++ vmcs_writel(CR0_READ_SHADOW, orig_val);
5928 ++ return 0;
5929 ++ } else {
5930 ++ if (to_vmx(vcpu)->nested.vmxon &&
5931 ++ !nested_host_cr0_valid(vcpu, val))
5932 ++ return 1;
5933 ++
5934 ++ return kvm_set_cr0(vcpu, val);
5935 ++ }
5936 ++}
5937 ++
5938 ++static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5939 ++{
5940 ++ if (is_guest_mode(vcpu)) {
5941 ++ struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5942 ++ unsigned long orig_val = val;
5943 ++
5944 ++ /* analogously to handle_set_cr0 */
5945 ++ val = (val & ~vmcs12->cr4_guest_host_mask) |
5946 ++ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5947 ++ if (kvm_set_cr4(vcpu, val))
5948 ++ return 1;
5949 ++ vmcs_writel(CR4_READ_SHADOW, orig_val);
5950 ++ return 0;
5951 ++ } else
5952 ++ return kvm_set_cr4(vcpu, val);
5953 ++}
5954 ++
5955 ++static int handle_desc(struct kvm_vcpu *vcpu)
5956 ++{
5957 ++ WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
5958 ++ return kvm_emulate_instruction(vcpu, 0);
5959 ++}
5960 ++
5961 ++static int handle_cr(struct kvm_vcpu *vcpu)
5962 ++{
5963 ++ unsigned long exit_qualification, val;
5964 ++ int cr;
5965 ++ int reg;
5966 ++ int err;
5967 ++ int ret;
5968 ++
5969 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5970 ++ cr = exit_qualification & 15;
5971 ++ reg = (exit_qualification >> 8) & 15;
5972 ++ switch ((exit_qualification >> 4) & 3) {
5973 ++ case 0: /* mov to cr */
5974 ++ val = kvm_register_readl(vcpu, reg);
5975 ++ trace_kvm_cr_write(cr, val);
5976 ++ switch (cr) {
5977 ++ case 0:
5978 ++ err = handle_set_cr0(vcpu, val);
5979 ++ return kvm_complete_insn_gp(vcpu, err);
5980 ++ case 3:
5981 ++ WARN_ON_ONCE(enable_unrestricted_guest);
5982 ++ err = kvm_set_cr3(vcpu, val);
5983 ++ return kvm_complete_insn_gp(vcpu, err);
5984 ++ case 4:
5985 ++ err = handle_set_cr4(vcpu, val);
5986 ++ return kvm_complete_insn_gp(vcpu, err);
5987 ++ case 8: {
5988 ++ u8 cr8_prev = kvm_get_cr8(vcpu);
5989 ++ u8 cr8 = (u8)val;
5990 ++ err = kvm_set_cr8(vcpu, cr8);
5991 ++ ret = kvm_complete_insn_gp(vcpu, err);
5992 ++ if (lapic_in_kernel(vcpu))
5993 ++ return ret;
5994 ++ if (cr8_prev <= cr8)
5995 ++ return ret;
5996 ++ /*
5997 ++ * TODO: we might be squashing a
5998 ++ * KVM_GUESTDBG_SINGLESTEP-triggered
5999 ++ * KVM_EXIT_DEBUG here.
6000 ++ */
6001 ++ vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
6002 ++ return 0;
6003 ++ }
6004 ++ }
6005 ++ break;
6006 ++ case 2: /* clts */
6007 ++ WARN_ONCE(1, "Guest should always own CR0.TS");
6008 ++ vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6009 ++ trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
6010 ++ return kvm_skip_emulated_instruction(vcpu);
6011 ++ case 1: /*mov from cr*/
6012 ++ switch (cr) {
6013 ++ case 3:
6014 ++ WARN_ON_ONCE(enable_unrestricted_guest);
6015 ++ val = kvm_read_cr3(vcpu);
6016 ++ kvm_register_write(vcpu, reg, val);
6017 ++ trace_kvm_cr_read(cr, val);
6018 ++ return kvm_skip_emulated_instruction(vcpu);
6019 ++ case 8:
6020 ++ val = kvm_get_cr8(vcpu);
6021 ++ kvm_register_write(vcpu, reg, val);
6022 ++ trace_kvm_cr_read(cr, val);
6023 ++ return kvm_skip_emulated_instruction(vcpu);
6024 ++ }
6025 ++ break;
6026 ++ case 3: /* lmsw */
6027 ++ val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
6028 ++ trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
6029 ++ kvm_lmsw(vcpu, val);
6030 ++
6031 ++ return kvm_skip_emulated_instruction(vcpu);
6032 ++ default:
6033 ++ break;
6034 ++ }
6035 ++ vcpu->run->exit_reason = 0;
6036 ++ vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
6037 ++ (int)(exit_qualification >> 4) & 3, cr);
6038 ++ return 0;
6039 ++}
6040 ++
6041 ++static int handle_dr(struct kvm_vcpu *vcpu)
6042 ++{
6043 ++ unsigned long exit_qualification;
6044 ++ int dr, dr7, reg;
6045 ++
6046 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6047 ++ dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6048 ++
6049 ++ /* First, if DR does not exist, trigger UD */
6050 ++ if (!kvm_require_dr(vcpu, dr))
6051 ++ return 1;
6052 ++
6053 ++ /* Do not handle if the CPL > 0, will trigger GP on re-entry */
6054 ++ if (!kvm_require_cpl(vcpu, 0))
6055 ++ return 1;
6056 ++ dr7 = vmcs_readl(GUEST_DR7);
6057 ++ if (dr7 & DR7_GD) {
6058 ++ /*
6059 ++ * As the vm-exit takes precedence over the debug trap, we
6060 ++ * need to emulate the latter, either for the host or the
6061 ++ * guest debugging itself.
6062 ++ */
6063 ++ if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
6064 ++ vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
6065 ++ vcpu->run->debug.arch.dr7 = dr7;
6066 ++ vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
6067 ++ vcpu->run->debug.arch.exception = DB_VECTOR;
6068 ++ vcpu->run->exit_reason = KVM_EXIT_DEBUG;
6069 ++ return 0;
6070 ++ } else {
6071 ++ vcpu->arch.dr6 &= ~DR_TRAP_BITS;
6072 ++ vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
6073 ++ kvm_queue_exception(vcpu, DB_VECTOR);
6074 ++ return 1;
6075 ++ }
6076 ++ }
6077 ++
6078 ++ if (vcpu->guest_debug == 0) {
6079 ++ exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
6080 ++
6081 ++ /*
6082 ++ * No more DR vmexits; force a reload of the debug registers
6083 ++ * and reenter on this instruction. The next vmexit will
6084 ++ * retrieve the full state of the debug registers.
6085 ++ */
6086 ++ vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6087 ++ return 1;
6088 ++ }
6089 ++
6090 ++ reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6091 ++ if (exit_qualification & TYPE_MOV_FROM_DR) {
6092 ++ unsigned long val;
6093 ++
6094 ++ if (kvm_get_dr(vcpu, dr, &val))
6095 ++ return 1;
6096 ++ kvm_register_write(vcpu, reg, val);
6097 ++ } else
6098 ++ if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
6099 ++ return 1;
6100 ++
6101 ++ return kvm_skip_emulated_instruction(vcpu);
6102 ++}
6103 ++
6104 ++static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6105 ++{
6106 ++ return vcpu->arch.dr6;
6107 ++}
6108 ++
6109 ++static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6110 ++{
6111 ++}
6112 ++
6113 ++static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6114 ++{
6115 ++ get_debugreg(vcpu->arch.db[0], 0);
6116 ++ get_debugreg(vcpu->arch.db[1], 1);
6117 ++ get_debugreg(vcpu->arch.db[2], 2);
6118 ++ get_debugreg(vcpu->arch.db[3], 3);
6119 ++ get_debugreg(vcpu->arch.dr6, 6);
6120 ++ vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6121 ++
6122 ++ vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
6123 ++ exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
6124 ++}
6125 ++
6126 ++static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6127 ++{
6128 ++ vmcs_writel(GUEST_DR7, val);
6129 ++}
6130 ++
6131 ++static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
6132 ++{
6133 ++ kvm_apic_update_ppr(vcpu);
6134 ++ return 1;
6135 ++}
6136 ++
6137 ++static int handle_interrupt_window(struct kvm_vcpu *vcpu)
6138 ++{
6139 ++ exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
6140 ++
6141 ++ kvm_make_request(KVM_REQ_EVENT, vcpu);
6142 ++
6143 ++ ++vcpu->stat.irq_window_exits;
6144 ++ return 1;
6145 ++}
6146 ++
6147 ++static int handle_vmcall(struct kvm_vcpu *vcpu)
6148 ++{
6149 ++ return kvm_emulate_hypercall(vcpu);
6150 ++}
6151 ++
6152 ++static int handle_invd(struct kvm_vcpu *vcpu)
6153 ++{
6154 ++ return kvm_emulate_instruction(vcpu, 0);
6155 ++}
6156 ++
6157 ++static int handle_invlpg(struct kvm_vcpu *vcpu)
6158 ++{
6159 ++ unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6160 ++
6161 ++ kvm_mmu_invlpg(vcpu, exit_qualification);
6162 ++ return kvm_skip_emulated_instruction(vcpu);
6163 ++}
6164 ++
6165 ++static int handle_rdpmc(struct kvm_vcpu *vcpu)
6166 ++{
6167 ++ int err;
6168 ++
6169 ++ err = kvm_rdpmc(vcpu);
6170 ++ return kvm_complete_insn_gp(vcpu, err);
6171 ++}
6172 ++
6173 ++static int handle_wbinvd(struct kvm_vcpu *vcpu)
6174 ++{
6175 ++ return kvm_emulate_wbinvd(vcpu);
6176 ++}
6177 ++
6178 ++static int handle_xsetbv(struct kvm_vcpu *vcpu)
6179 ++{
6180 ++ u64 new_bv = kvm_read_edx_eax(vcpu);
6181 ++ u32 index = kvm_rcx_read(vcpu);
6182 ++
6183 ++ if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6184 ++ return kvm_skip_emulated_instruction(vcpu);
6185 ++ return 1;
6186 ++}
6187 ++
6188 ++static int handle_apic_access(struct kvm_vcpu *vcpu)
6189 ++{
6190 ++ if (likely(fasteoi)) {
6191 ++ unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6192 ++ int access_type, offset;
6193 ++
6194 ++ access_type = exit_qualification & APIC_ACCESS_TYPE;
6195 ++ offset = exit_qualification & APIC_ACCESS_OFFSET;
6196 ++ /*
6197 ++ * Sane guest uses MOV to write EOI, with written value
6198 ++ * not cared. So make a short-circuit here by avoiding
6199 ++ * heavy instruction emulation.
6200 ++ */
6201 ++ if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6202 ++ (offset == APIC_EOI)) {
6203 ++ kvm_lapic_set_eoi(vcpu);
6204 ++ return kvm_skip_emulated_instruction(vcpu);
6205 ++ }
6206 ++ }
6207 ++ return kvm_emulate_instruction(vcpu, 0);
6208 ++}
6209 ++
6210 ++static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6211 ++{
6212 ++ unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6213 ++ int vector = exit_qualification & 0xff;
6214 ++
6215 ++ /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6216 ++ kvm_apic_set_eoi_accelerated(vcpu, vector);
6217 ++ return 1;
6218 ++}
6219 ++
6220 ++static int handle_apic_write(struct kvm_vcpu *vcpu)
6221 ++{
6222 ++ unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6223 ++ u32 offset = exit_qualification & 0xfff;
6224 ++
6225 ++ /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6226 ++ kvm_apic_write_nodecode(vcpu, offset);
6227 ++ return 1;
6228 ++}
6229 ++
6230 ++static int handle_task_switch(struct kvm_vcpu *vcpu)
6231 ++{
6232 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6233 ++ unsigned long exit_qualification;
6234 ++ bool has_error_code = false;
6235 ++ u32 error_code = 0;
6236 ++ u16 tss_selector;
6237 ++ int reason, type, idt_v, idt_index;
6238 ++
6239 ++ idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
6240 ++ idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
6241 ++ type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
6242 ++
6243 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6244 ++
6245 ++ reason = (u32)exit_qualification >> 30;
6246 ++ if (reason == TASK_SWITCH_GATE && idt_v) {
6247 ++ switch (type) {
6248 ++ case INTR_TYPE_NMI_INTR:
6249 ++ vcpu->arch.nmi_injected = false;
6250 ++ vmx_set_nmi_mask(vcpu, true);
6251 ++ break;
6252 ++ case INTR_TYPE_EXT_INTR:
6253 ++ case INTR_TYPE_SOFT_INTR:
6254 ++ kvm_clear_interrupt_queue(vcpu);
6255 ++ break;
6256 ++ case INTR_TYPE_HARD_EXCEPTION:
6257 ++ if (vmx->idt_vectoring_info &
6258 ++ VECTORING_INFO_DELIVER_CODE_MASK) {
6259 ++ has_error_code = true;
6260 ++ error_code =
6261 ++ vmcs_read32(IDT_VECTORING_ERROR_CODE);
6262 ++ }
6263 ++ /* fall through */
6264 ++ case INTR_TYPE_SOFT_EXCEPTION:
6265 ++ kvm_clear_exception_queue(vcpu);
6266 ++ break;
6267 ++ default:
6268 ++ break;
6269 ++ }
6270 ++ }
6271 ++ tss_selector = exit_qualification;
6272 ++
6273 ++ if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6274 ++ type != INTR_TYPE_EXT_INTR &&
6275 ++ type != INTR_TYPE_NMI_INTR))
6276 ++ WARN_ON(!skip_emulated_instruction(vcpu));
6277 ++
6278 ++ /*
6279 ++ * TODO: What about debug traps on tss switch?
6280 ++ * Are we supposed to inject them and update dr6?
6281 ++ */
6282 ++ return kvm_task_switch(vcpu, tss_selector,
6283 ++ type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
6284 ++ reason, has_error_code, error_code);
6285 ++}
6286 ++
6287 ++static int handle_ept_violation(struct kvm_vcpu *vcpu)
6288 ++{
6289 ++ unsigned long exit_qualification;
6290 ++ gpa_t gpa;
6291 ++ u64 error_code;
6292 ++
6293 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6294 ++
6295 ++ /*
6296 ++ * EPT violation happened while executing iret from NMI,
6297 ++ * "blocked by NMI" bit has to be set before next VM entry.
6298 ++ * There are errata that may cause this bit to not be set:
6299 ++ * AAK134, BY25.
6300 ++ */
6301 ++ if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6302 ++ enable_vnmi &&
6303 ++ (exit_qualification & INTR_INFO_UNBLOCK_NMI))
6304 ++ vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6305 ++
6306 ++ gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6307 ++ trace_kvm_page_fault(gpa, exit_qualification);
6308 ++
6309 ++ /* Is it a read fault? */
6310 ++ error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
6311 ++ ? PFERR_USER_MASK : 0;
6312 ++ /* Is it a write fault? */
6313 ++ error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
6314 ++ ? PFERR_WRITE_MASK : 0;
6315 ++ /* Is it a fetch fault? */
6316 ++ error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
6317 ++ ? PFERR_FETCH_MASK : 0;
6318 ++ /* ept page table entry is present? */
6319 ++ error_code |= (exit_qualification &
6320 ++ (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6321 ++ EPT_VIOLATION_EXECUTABLE))
6322 ++ ? PFERR_PRESENT_MASK : 0;
6323 ++
6324 ++ error_code |= (exit_qualification & 0x100) != 0 ?
6325 ++ PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
6326 ++
6327 ++ vcpu->arch.exit_qualification = exit_qualification;
6328 ++ return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
6329 ++}
6330 ++
6331 ++static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
6332 ++{
6333 ++ gpa_t gpa;
6334 ++
6335 ++ /*
6336 ++ * A nested guest cannot optimize MMIO vmexits, because we have an
6337 ++ * nGPA here instead of the required GPA.
6338 ++ */
6339 ++ gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6340 ++ if (!is_guest_mode(vcpu) &&
6341 ++ !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
6342 ++ trace_kvm_fast_mmio(gpa);
6343 ++ return kvm_skip_emulated_instruction(vcpu);
6344 ++ }
6345 ++
6346 ++ return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6347 ++}
6348 ++
6349 ++static int handle_nmi_window(struct kvm_vcpu *vcpu)
6350 ++{
6351 ++ WARN_ON_ONCE(!enable_vnmi);
6352 ++ exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
6353 ++ ++vcpu->stat.nmi_window_exits;
6354 ++ kvm_make_request(KVM_REQ_EVENT, vcpu);
6355 ++
6356 ++ return 1;
6357 ++}
6358 ++
6359 ++static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
6360 ++{
6361 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6362 ++ bool intr_window_requested;
6363 ++ unsigned count = 130;
6364 ++
6365 ++ /*
6366 ++ * We should never reach the point where we are emulating L2
6367 ++ * due to invalid guest state as that means we incorrectly
6368 ++ * allowed a nested VMEntry with an invalid vmcs12.
6369 ++ */
6370 ++ WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
6371 ++
6372 ++ intr_window_requested = exec_controls_get(vmx) &
6373 ++ CPU_BASED_INTR_WINDOW_EXITING;
6374 ++
6375 ++ while (vmx->emulation_required && count-- != 0) {
6376 ++ if (intr_window_requested && vmx_interrupt_allowed(vcpu))
6377 ++ return handle_interrupt_window(&vmx->vcpu);
6378 ++
6379 ++ if (kvm_test_request(KVM_REQ_EVENT, vcpu))
6380 ++ return 1;
6381 ++
6382 ++ if (!kvm_emulate_instruction(vcpu, 0))
6383 ++ return 0;
6384 ++
6385 ++ if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6386 ++ vcpu->arch.exception.pending) {
6387 ++ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6388 ++ vcpu->run->internal.suberror =
6389 ++ KVM_INTERNAL_ERROR_EMULATION;
6390 ++ vcpu->run->internal.ndata = 0;
6391 ++ return 0;
6392 ++ }
6393 ++
6394 ++ if (vcpu->arch.halt_request) {
6395 ++ vcpu->arch.halt_request = 0;
6396 ++ return kvm_vcpu_halt(vcpu);
6397 ++ }
6398 ++
6399 ++ /*
6400 ++ * Note, return 1 and not 0, vcpu_run() is responsible for
6401 ++ * morphing the pending signal into the proper return code.
6402 ++ */
6403 ++ if (signal_pending(current))
6404 ++ return 1;
6405 ++
6406 ++ if (need_resched())
6407 ++ schedule();
6408 ++ }
6409 ++
6410 ++ return 1;
6411 ++}
6412 ++
6413 ++static void grow_ple_window(struct kvm_vcpu *vcpu)
6414 ++{
6415 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6416 ++ unsigned int old = vmx->ple_window;
6417 ++
6418 ++ vmx->ple_window = __grow_ple_window(old, ple_window,
6419 ++ ple_window_grow,
6420 ++ ple_window_max);
6421 ++
6422 ++ if (vmx->ple_window != old) {
6423 ++ vmx->ple_window_dirty = true;
6424 ++ trace_kvm_ple_window_update(vcpu->vcpu_id,
6425 ++ vmx->ple_window, old);
6426 ++ }
6427 ++}
6428 ++
6429 ++static void shrink_ple_window(struct kvm_vcpu *vcpu)
6430 ++{
6431 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6432 ++ unsigned int old = vmx->ple_window;
6433 ++
6434 ++ vmx->ple_window = __shrink_ple_window(old, ple_window,
6435 ++ ple_window_shrink,
6436 ++ ple_window);
6437 ++
6438 ++ if (vmx->ple_window != old) {
6439 ++ vmx->ple_window_dirty = true;
6440 ++ trace_kvm_ple_window_update(vcpu->vcpu_id,
6441 ++ vmx->ple_window, old);
6442 ++ }
6443 ++}
6444 ++
6445 ++/*
6446 ++ * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6447 ++ */
6448 ++static void wakeup_handler(void)
6449 ++{
6450 ++ struct kvm_vcpu *vcpu;
6451 ++ int cpu = smp_processor_id();
6452 ++
6453 ++ spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6454 ++ list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6455 ++ blocked_vcpu_list) {
6456 ++ struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6457 ++
6458 ++ if (pi_test_on(pi_desc) == 1)
6459 ++ kvm_vcpu_kick(vcpu);
6460 ++ }
6461 ++ spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6462 ++}
6463 ++
6464 ++static void vmx_enable_tdp(void)
6465 ++{
6466 ++ kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6467 ++ enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6468 ++ enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6469 ++ 0ull, VMX_EPT_EXECUTABLE_MASK,
6470 ++ cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
6471 ++ VMX_EPT_RWX_MASK, 0ull);
6472 ++
6473 ++ ept_set_mmio_spte_mask();
6474 ++ kvm_enable_tdp();
6475 ++}
6476 ++
6477 ++/*
6478 ++ * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6479 ++ * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6480 ++ */
6481 ++static int handle_pause(struct kvm_vcpu *vcpu)
6482 ++{
6483 ++ if (!kvm_pause_in_guest(vcpu->kvm))
6484 ++ grow_ple_window(vcpu);
6485 ++
6486 ++ /*
6487 ++ * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
6488 ++ * VM-execution control is ignored if CPL > 0. OTOH, KVM
6489 ++ * never set PAUSE_EXITING and just set PLE if supported,
6490 ++ * so the vcpu must be CPL=0 if it gets a PAUSE exit.
6491 ++ */
6492 ++ kvm_vcpu_on_spin(vcpu, true);
6493 ++ return kvm_skip_emulated_instruction(vcpu);
6494 ++}
6495 ++
6496 ++static int handle_nop(struct kvm_vcpu *vcpu)
6497 ++{
6498 ++ return kvm_skip_emulated_instruction(vcpu);
6499 ++}
6500 ++
6501 ++static int handle_mwait(struct kvm_vcpu *vcpu)
6502 ++{
6503 ++ printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6504 ++ return handle_nop(vcpu);
6505 ++}
6506 ++
6507 ++static int handle_invalid_op(struct kvm_vcpu *vcpu)
6508 ++{
6509 ++ kvm_queue_exception(vcpu, UD_VECTOR);
6510 ++ return 1;
6511 ++}
6512 ++
6513 ++static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6514 ++{
6515 ++ return 1;
6516 ++}
6517 ++
6518 ++static int handle_monitor(struct kvm_vcpu *vcpu)
6519 ++{
6520 ++ printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6521 ++ return handle_nop(vcpu);
6522 ++}
6523 ++
6524 ++static int handle_invpcid(struct kvm_vcpu *vcpu)
6525 ++{
6526 ++ u32 vmx_instruction_info;
6527 ++ unsigned long type;
6528 ++ bool pcid_enabled;
6529 ++ gva_t gva;
6530 ++ struct x86_exception e;
6531 ++ unsigned i;
6532 ++ unsigned long roots_to_free = 0;
6533 ++ struct {
6534 ++ u64 pcid;
6535 ++ u64 gla;
6536 ++ } operand;
6537 ++
6538 ++ if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
6539 ++ kvm_queue_exception(vcpu, UD_VECTOR);
6540 ++ return 1;
6541 ++ }
6542 ++
6543 ++ vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6544 ++ type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
6545 ++
6546 ++ if (type > 3) {
6547 ++ kvm_inject_gp(vcpu, 0);
6548 ++ return 1;
6549 ++ }
6550 ++
6551 ++ /* According to the Intel instruction reference, the memory operand
6552 ++ * is read even if it isn't needed (e.g., for type==all)
6553 ++ */
6554 ++ if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6555 ++ vmx_instruction_info, false,
6556 ++ sizeof(operand), &gva))
6557 ++ return 1;
6558 ++
6559 ++ if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
6560 ++ kvm_inject_page_fault(vcpu, &e);
6561 ++ return 1;
6562 ++ }
6563 ++
6564 ++ if (operand.pcid >> 12 != 0) {
6565 ++ kvm_inject_gp(vcpu, 0);
6566 ++ return 1;
6567 ++ }
6568 ++
6569 ++ pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
6570 ++
6571 ++ switch (type) {
6572 ++ case INVPCID_TYPE_INDIV_ADDR:
6573 ++ if ((!pcid_enabled && (operand.pcid != 0)) ||
6574 ++ is_noncanonical_address(operand.gla, vcpu)) {
6575 ++ kvm_inject_gp(vcpu, 0);
6576 ++ return 1;
6577 ++ }
6578 ++ kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
6579 ++ return kvm_skip_emulated_instruction(vcpu);
6580 ++
6581 ++ case INVPCID_TYPE_SINGLE_CTXT:
6582 ++ if (!pcid_enabled && (operand.pcid != 0)) {
6583 ++ kvm_inject_gp(vcpu, 0);
6584 ++ return 1;
6585 ++ }
6586 ++
6587 ++ if (kvm_get_active_pcid(vcpu) == operand.pcid) {
6588 ++ kvm_mmu_sync_roots(vcpu);
6589 ++ kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
6590 ++ }
6591 ++
6592 ++ for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
6593 ++ if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
6594 ++ == operand.pcid)
6595 ++ roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
6596 ++
6597 ++ kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
6598 ++ /*
6599 ++ * If neither the current cr3 nor any of the prev_roots use the
6600 ++ * given PCID, then nothing needs to be done here because a
6601 ++ * resync will happen anyway before switching to any other CR3.
6602 ++ */
6603 ++
6604 ++ return kvm_skip_emulated_instruction(vcpu);
6605 ++
6606 ++ case INVPCID_TYPE_ALL_NON_GLOBAL:
6607 ++ /*
6608 ++ * Currently, KVM doesn't mark global entries in the shadow
6609 ++ * page tables, so a non-global flush just degenerates to a
6610 ++ * global flush. If needed, we could optimize this later by
6611 ++ * keeping track of global entries in shadow page tables.
6612 ++ */
6613 ++
6614 ++ /* fall-through */
6615 ++ case INVPCID_TYPE_ALL_INCL_GLOBAL:
6616 ++ kvm_mmu_unload(vcpu);
6617 ++ return kvm_skip_emulated_instruction(vcpu);
6618 ++
6619 ++ default:
6620 ++ BUG(); /* We have already checked above that type <= 3 */
6621 ++ }
6622 ++}
6623 ++
6624 ++static int handle_pml_full(struct kvm_vcpu *vcpu)
6625 ++{
6626 ++ unsigned long exit_qualification;
6627 ++
6628 ++ trace_kvm_pml_full(vcpu->vcpu_id);
6629 ++
6630 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6631 ++
6632 ++ /*
6633 ++ * PML buffer FULL happened while executing iret from NMI,
6634 ++ * "blocked by NMI" bit has to be set before next VM entry.
6635 ++ */
6636 ++ if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6637 ++ enable_vnmi &&
6638 ++ (exit_qualification & INTR_INFO_UNBLOCK_NMI))
6639 ++ vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6640 ++ GUEST_INTR_STATE_NMI);
6641 ++
6642 ++ /*
6643 ++ * PML buffer already flushed at beginning of VMEXIT. Nothing to do
6644 ++ * here.., and there's no userspace involvement needed for PML.
6645 ++ */
6646 ++ return 1;
6647 ++}
6648 ++
6649 ++static int handle_preemption_timer(struct kvm_vcpu *vcpu)
6650 ++{
6651 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6652 ++
6653 ++ if (!vmx->req_immediate_exit &&
6654 ++ !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled))
6655 ++ kvm_lapic_expired_hv_timer(vcpu);
6656 ++
6657 ++ return 1;
6658 ++}
6659 ++
6660 ++/*
6661 ++ * When nested=0, all VMX instruction VM Exits filter here. The handlers
6662 ++ * are overwritten by nested_vmx_setup() when nested=1.
6663 ++ */
6664 ++static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
6665 ++{
6666 ++ kvm_queue_exception(vcpu, UD_VECTOR);
6667 ++ return 1;
6668 ++}
6669 ++
6670 ++static int handle_encls(struct kvm_vcpu *vcpu)
6671 ++{
6672 ++ /*
6673 ++ * SGX virtualization is not yet supported. There is no software
6674 ++ * enable bit for SGX, so we have to trap ENCLS and inject a #UD
6675 ++ * to prevent the guest from executing ENCLS.
6676 ++ */
6677 ++ kvm_queue_exception(vcpu, UD_VECTOR);
6678 ++ return 1;
6679 ++}
6680 ++
6681 ++/*
6682 ++ * The exit handlers return 1 if the exit was handled fully and guest execution
6683 ++ * may resume. Otherwise they set the kvm_run parameter to indicate what needs
6684 ++ * to be done to userspace and return 0.
6685 ++ */
6686 ++static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
6687 ++ [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
6688 ++ [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
6689 ++ [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
6690 ++ [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
6691 ++ [EXIT_REASON_IO_INSTRUCTION] = handle_io,
6692 ++ [EXIT_REASON_CR_ACCESS] = handle_cr,
6693 ++ [EXIT_REASON_DR_ACCESS] = handle_dr,
6694 ++ [EXIT_REASON_CPUID] = kvm_emulate_cpuid,
6695 ++ [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr,
6696 ++ [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr,
6697 ++ [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window,
6698 ++ [EXIT_REASON_HLT] = kvm_emulate_halt,
6699 ++ [EXIT_REASON_INVD] = handle_invd,
6700 ++ [EXIT_REASON_INVLPG] = handle_invlpg,
6701 ++ [EXIT_REASON_RDPMC] = handle_rdpmc,
6702 ++ [EXIT_REASON_VMCALL] = handle_vmcall,
6703 ++ [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
6704 ++ [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
6705 ++ [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
6706 ++ [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
6707 ++ [EXIT_REASON_VMREAD] = handle_vmx_instruction,
6708 ++ [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
6709 ++ [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
6710 ++ [EXIT_REASON_VMOFF] = handle_vmx_instruction,
6711 ++ [EXIT_REASON_VMON] = handle_vmx_instruction,
6712 ++ [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
6713 ++ [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
6714 ++ [EXIT_REASON_APIC_WRITE] = handle_apic_write,
6715 ++ [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
6716 ++ [EXIT_REASON_WBINVD] = handle_wbinvd,
6717 ++ [EXIT_REASON_XSETBV] = handle_xsetbv,
6718 ++ [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
6719 ++ [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
6720 ++ [EXIT_REASON_GDTR_IDTR] = handle_desc,
6721 ++ [EXIT_REASON_LDTR_TR] = handle_desc,
6722 ++ [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
6723 ++ [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
6724 ++ [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
6725 ++ [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
6726 ++ [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
6727 ++ [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
6728 ++ [EXIT_REASON_INVEPT] = handle_vmx_instruction,
6729 ++ [EXIT_REASON_INVVPID] = handle_vmx_instruction,
6730 ++ [EXIT_REASON_RDRAND] = handle_invalid_op,
6731 ++ [EXIT_REASON_RDSEED] = handle_invalid_op,
6732 ++ [EXIT_REASON_PML_FULL] = handle_pml_full,
6733 ++ [EXIT_REASON_INVPCID] = handle_invpcid,
6734 ++ [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
6735 ++ [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
6736 ++ [EXIT_REASON_ENCLS] = handle_encls,
6737 ++};
6738 ++
6739 ++static const int kvm_vmx_max_exit_handlers =
6740 ++ ARRAY_SIZE(kvm_vmx_exit_handlers);
6741 ++
6742 ++static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
6743 ++{
6744 ++ *info1 = vmcs_readl(EXIT_QUALIFICATION);
6745 ++ *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
6746 ++}
6747 ++
6748 ++static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
6749 ++{
6750 ++ if (vmx->pml_pg) {
6751 ++ __free_page(vmx->pml_pg);
6752 ++ vmx->pml_pg = NULL;
6753 ++ }
6754 ++}
6755 ++
6756 ++static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
6757 ++{
6758 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6759 ++ u64 *pml_buf;
6760 ++ u16 pml_idx;
6761 ++
6762 ++ pml_idx = vmcs_read16(GUEST_PML_INDEX);
6763 ++
6764 ++ /* Do nothing if PML buffer is empty */
6765 ++ if (pml_idx == (PML_ENTITY_NUM - 1))
6766 ++ return;
6767 ++
6768 ++ /* PML index always points to next available PML buffer entity */
6769 ++ if (pml_idx >= PML_ENTITY_NUM)
6770 ++ pml_idx = 0;
6771 ++ else
6772 ++ pml_idx++;
6773 ++
6774 ++ pml_buf = page_address(vmx->pml_pg);
6775 ++ for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
6776 ++ u64 gpa;
6777 ++
6778 ++ gpa = pml_buf[pml_idx];
6779 ++ WARN_ON(gpa & (PAGE_SIZE - 1));
6780 ++ kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
6781 ++ }
6782 ++
6783 ++ /* reset PML index */
6784 ++ vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6785 ++}
6786 ++
6787 ++/*
6788 ++ * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
6789 ++ * Called before reporting dirty_bitmap to userspace.
6790 ++ */
6791 ++static void kvm_flush_pml_buffers(struct kvm *kvm)
6792 ++{
6793 ++ int i;
6794 ++ struct kvm_vcpu *vcpu;
6795 ++ /*
6796 ++ * We only need to kick vcpu out of guest mode here, as PML buffer
6797 ++ * is flushed at beginning of all VMEXITs, and it's obvious that only
6798 ++ * vcpus running in guest are possible to have unflushed GPAs in PML
6799 ++ * buffer.
6800 ++ */
6801 ++ kvm_for_each_vcpu(i, vcpu, kvm)
6802 ++ kvm_vcpu_kick(vcpu);
6803 ++}
6804 ++
6805 ++static void vmx_dump_sel(char *name, uint32_t sel)
6806 ++{
6807 ++ pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
6808 ++ name, vmcs_read16(sel),
6809 ++ vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
6810 ++ vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
6811 ++ vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
6812 ++}
6813 ++
6814 ++static void vmx_dump_dtsel(char *name, uint32_t limit)
6815 ++{
6816 ++ pr_err("%s limit=0x%08x, base=0x%016lx\n",
6817 ++ name, vmcs_read32(limit),
6818 ++ vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
6819 ++}
6820 ++
6821 ++void dump_vmcs(void)
6822 ++{
6823 ++ u32 vmentry_ctl, vmexit_ctl;
6824 ++ u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
6825 ++ unsigned long cr4;
6826 ++ u64 efer;
6827 ++ int i, n;
6828 ++
6829 ++ if (!dump_invalid_vmcs) {
6830 ++ pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n");
6831 ++ return;
6832 ++ }
6833 ++
6834 ++ vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
6835 ++ vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
6836 ++ cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6837 ++ pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
6838 ++ cr4 = vmcs_readl(GUEST_CR4);
6839 ++ efer = vmcs_read64(GUEST_IA32_EFER);
6840 ++ secondary_exec_control = 0;
6841 ++ if (cpu_has_secondary_exec_ctrls())
6842 ++ secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6843 ++
6844 ++ pr_err("*** Guest State ***\n");
6845 ++ pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
6846 ++ vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
6847 ++ vmcs_readl(CR0_GUEST_HOST_MASK));
6848 ++ pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
6849 ++ cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
6850 ++ pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
6851 ++ if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
6852 ++ (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
6853 ++ {
6854 ++ pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
6855 ++ vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
6856 ++ pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
6857 ++ vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
6858 ++ }
6859 ++ pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
6860 ++ vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
6861 ++ pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
6862 ++ vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
6863 ++ pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
6864 ++ vmcs_readl(GUEST_SYSENTER_ESP),
6865 ++ vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
6866 ++ vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
6867 ++ vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
6868 ++ vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
6869 ++ vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
6870 ++ vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
6871 ++ vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
6872 ++ vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
6873 ++ vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
6874 ++ vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
6875 ++ vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
6876 ++ if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
6877 ++ (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
6878 ++ pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
6879 ++ efer, vmcs_read64(GUEST_IA32_PAT));
6880 ++ pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
6881 ++ vmcs_read64(GUEST_IA32_DEBUGCTL),
6882 ++ vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
6883 ++ if (cpu_has_load_perf_global_ctrl() &&
6884 ++ vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
6885 ++ pr_err("PerfGlobCtl = 0x%016llx\n",
6886 ++ vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
6887 ++ if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
6888 ++ pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
6889 ++ pr_err("Interruptibility = %08x ActivityState = %08x\n",
6890 ++ vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
6891 ++ vmcs_read32(GUEST_ACTIVITY_STATE));
6892 ++ if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
6893 ++ pr_err("InterruptStatus = %04x\n",
6894 ++ vmcs_read16(GUEST_INTR_STATUS));
6895 ++
6896 ++ pr_err("*** Host State ***\n");
6897 ++ pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
6898 ++ vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
6899 ++ pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
6900 ++ vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
6901 ++ vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
6902 ++ vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
6903 ++ vmcs_read16(HOST_TR_SELECTOR));
6904 ++ pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
6905 ++ vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
6906 ++ vmcs_readl(HOST_TR_BASE));
6907 ++ pr_err("GDTBase=%016lx IDTBase=%016lx\n",
6908 ++ vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
6909 ++ pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
6910 ++ vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
6911 ++ vmcs_readl(HOST_CR4));
6912 ++ pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
6913 ++ vmcs_readl(HOST_IA32_SYSENTER_ESP),
6914 ++ vmcs_read32(HOST_IA32_SYSENTER_CS),
6915 ++ vmcs_readl(HOST_IA32_SYSENTER_EIP));
6916 ++ if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
6917 ++ pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
6918 ++ vmcs_read64(HOST_IA32_EFER),
6919 ++ vmcs_read64(HOST_IA32_PAT));
6920 ++ if (cpu_has_load_perf_global_ctrl() &&
6921 ++ vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
6922 ++ pr_err("PerfGlobCtl = 0x%016llx\n",
6923 ++ vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
6924 ++
6925 ++ pr_err("*** Control State ***\n");
6926 ++ pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
6927 ++ pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
6928 ++ pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
6929 ++ pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
6930 ++ vmcs_read32(EXCEPTION_BITMAP),
6931 ++ vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
6932 ++ vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
6933 ++ pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
6934 ++ vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6935 ++ vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
6936 ++ vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
6937 ++ pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
6938 ++ vmcs_read32(VM_EXIT_INTR_INFO),
6939 ++ vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
6940 ++ vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
6941 ++ pr_err(" reason=%08x qualification=%016lx\n",
6942 ++ vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
6943 ++ pr_err("IDTVectoring: info=%08x errcode=%08x\n",
6944 ++ vmcs_read32(IDT_VECTORING_INFO_FIELD),
6945 ++ vmcs_read32(IDT_VECTORING_ERROR_CODE));
6946 ++ pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
6947 ++ if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
6948 ++ pr_err("TSC Multiplier = 0x%016llx\n",
6949 ++ vmcs_read64(TSC_MULTIPLIER));
6950 ++ if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) {
6951 ++ if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
6952 ++ u16 status = vmcs_read16(GUEST_INTR_STATUS);
6953 ++ pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff);
6954 ++ }
6955 ++ pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
6956 ++ if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
6957 ++ pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR));
6958 ++ pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR));
6959 ++ }
6960 ++ if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
6961 ++ pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
6962 ++ if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
6963 ++ pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
6964 ++ n = vmcs_read32(CR3_TARGET_COUNT);
6965 ++ for (i = 0; i + 1 < n; i += 4)
6966 ++ pr_err("CR3 target%u=%016lx target%u=%016lx\n",
6967 ++ i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
6968 ++ i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
6969 ++ if (i < n)
6970 ++ pr_err("CR3 target%u=%016lx\n",
6971 ++ i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
6972 ++ if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
6973 ++ pr_err("PLE Gap=%08x Window=%08x\n",
6974 ++ vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
6975 ++ if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
6976 ++ pr_err("Virtual processor ID = 0x%04x\n",
6977 ++ vmcs_read16(VIRTUAL_PROCESSOR_ID));
6978 ++}
6979 ++
6980 ++/*
6981 ++ * The guest has exited. See if we can fix it or if we need userspace
6982 ++ * assistance.
6983 ++ */
6984 ++static int vmx_handle_exit(struct kvm_vcpu *vcpu,
6985 ++ enum exit_fastpath_completion exit_fastpath)
6986 ++{
6987 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6988 ++ u32 exit_reason = vmx->exit_reason;
6989 ++ u32 vectoring_info = vmx->idt_vectoring_info;
6990 ++
6991 ++ trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
6992 ++
6993 ++ /*
6994 ++ * Flush logged GPAs PML buffer, this will make dirty_bitmap more
6995 ++ * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
6996 ++ * querying dirty_bitmap, we only need to kick all vcpus out of guest
6997 ++ * mode as if vcpus is in root mode, the PML buffer must has been
6998 ++ * flushed already.
6999 ++ */
7000 ++ if (enable_pml)
7001 ++ vmx_flush_pml_buffer(vcpu);
7002 ++
7003 ++ /* If guest state is invalid, start emulating */
7004 ++ if (vmx->emulation_required)
7005 ++ return handle_invalid_guest_state(vcpu);
7006 ++
7007 ++ if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
7008 ++ return nested_vmx_reflect_vmexit(vcpu, exit_reason);
7009 ++
7010 ++ if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
7011 ++ dump_vmcs();
7012 ++ vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
7013 ++ vcpu->run->fail_entry.hardware_entry_failure_reason
7014 ++ = exit_reason;
7015 ++ return 0;
7016 ++ }
7017 ++
7018 ++ if (unlikely(vmx->fail)) {
7019 ++ dump_vmcs();
7020 ++ vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
7021 ++ vcpu->run->fail_entry.hardware_entry_failure_reason
7022 ++ = vmcs_read32(VM_INSTRUCTION_ERROR);
7023 ++ return 0;
7024 ++ }
7025 ++
7026 ++ /*
7027 ++ * Note:
7028 ++ * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
7029 ++ * delivery event since it indicates guest is accessing MMIO.
7030 ++ * The vm-exit can be triggered again after return to guest that
7031 ++ * will cause infinite loop.
7032 ++ */
7033 ++ if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
7034 ++ (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
7035 ++ exit_reason != EXIT_REASON_EPT_VIOLATION &&
7036 ++ exit_reason != EXIT_REASON_PML_FULL &&
7037 ++ exit_reason != EXIT_REASON_TASK_SWITCH)) {
7038 ++ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7039 ++ vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
7040 ++ vcpu->run->internal.ndata = 3;
7041 ++ vcpu->run->internal.data[0] = vectoring_info;
7042 ++ vcpu->run->internal.data[1] = exit_reason;
7043 ++ vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
7044 ++ if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
7045 ++ vcpu->run->internal.ndata++;
7046 ++ vcpu->run->internal.data[3] =
7047 ++ vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7048 ++ }
7049 ++ return 0;
7050 ++ }
7051 ++
7052 ++ if (unlikely(!enable_vnmi &&
7053 ++ vmx->loaded_vmcs->soft_vnmi_blocked)) {
7054 ++ if (vmx_interrupt_allowed(vcpu)) {
7055 ++ vmx->loaded_vmcs->soft_vnmi_blocked = 0;
7056 ++ } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
7057 ++ vcpu->arch.nmi_pending) {
7058 ++ /*
7059 ++ * This CPU don't support us in finding the end of an
7060 ++ * NMI-blocked window if the guest runs with IRQs
7061 ++ * disabled. So we pull the trigger after 1 s of
7062 ++ * futile waiting, but inform the user about this.
7063 ++ */
7064 ++ printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
7065 ++ "state on VCPU %d after 1 s timeout\n",
7066 ++ __func__, vcpu->vcpu_id);
7067 ++ vmx->loaded_vmcs->soft_vnmi_blocked = 0;
7068 ++ }
7069 ++ }
7070 ++
7071 ++ if (exit_fastpath == EXIT_FASTPATH_SKIP_EMUL_INS) {
7072 ++ kvm_skip_emulated_instruction(vcpu);
7073 ++ return 1;
7074 ++ } else if (exit_reason < kvm_vmx_max_exit_handlers
7075 ++ && kvm_vmx_exit_handlers[exit_reason]) {
7076 ++#ifdef CONFIG_RETPOLINE
7077 ++ if (exit_reason == EXIT_REASON_MSR_WRITE)
7078 ++ return kvm_emulate_wrmsr(vcpu);
7079 ++ else if (exit_reason == EXIT_REASON_PREEMPTION_TIMER)
7080 ++ return handle_preemption_timer(vcpu);
7081 ++ else if (exit_reason == EXIT_REASON_INTERRUPT_WINDOW)
7082 ++ return handle_interrupt_window(vcpu);
7083 ++ else if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
7084 ++ return handle_external_interrupt(vcpu);
7085 ++ else if (exit_reason == EXIT_REASON_HLT)
7086 ++ return kvm_emulate_halt(vcpu);
7087 ++ else if (exit_reason == EXIT_REASON_EPT_MISCONFIG)
7088 ++ return handle_ept_misconfig(vcpu);
7089 ++#endif
7090 ++ return kvm_vmx_exit_handlers[exit_reason](vcpu);
7091 ++ } else {
7092 ++ vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
7093 ++ exit_reason);
7094 ++ dump_vmcs();
7095 ++ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7096 ++ vcpu->run->internal.suberror =
7097 ++ KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
7098 ++ vcpu->run->internal.ndata = 1;
7099 ++ vcpu->run->internal.data[0] = exit_reason;
7100 ++ return 0;
7101 ++ }
7102 ++}
7103 ++
7104 ++/*
7105 ++ * Software based L1D cache flush which is used when microcode providing
7106 ++ * the cache control MSR is not loaded.
7107 ++ *
7108 ++ * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
7109 ++ * flush it is required to read in 64 KiB because the replacement algorithm
7110 ++ * is not exactly LRU. This could be sized at runtime via topology
7111 ++ * information but as all relevant affected CPUs have 32KiB L1D cache size
7112 ++ * there is no point in doing so.
7113 ++ */
7114 ++static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
7115 ++{
7116 ++ int size = PAGE_SIZE << L1D_CACHE_ORDER;
7117 ++
7118 ++ /*
7119 ++ * This code is only executed when the the flush mode is 'cond' or
7120 ++ * 'always'
7121 ++ */
7122 ++ if (static_branch_likely(&vmx_l1d_flush_cond)) {
7123 ++ bool flush_l1d;
7124 ++
7125 ++ /*
7126 ++ * Clear the per-vcpu flush bit, it gets set again
7127 ++ * either from vcpu_run() or from one of the unsafe
7128 ++ * VMEXIT handlers.
7129 ++ */
7130 ++ flush_l1d = vcpu->arch.l1tf_flush_l1d;
7131 ++ vcpu->arch.l1tf_flush_l1d = false;
7132 ++
7133 ++ /*
7134 ++ * Clear the per-cpu flush bit, it gets set again from
7135 ++ * the interrupt handlers.
7136 ++ */
7137 ++ flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
7138 ++ kvm_clear_cpu_l1tf_flush_l1d();
7139 ++
7140 ++ if (!flush_l1d)
7141 ++ return;
7142 ++ }
7143 ++
7144 ++ vcpu->stat.l1d_flush++;
7145 ++
7146 ++ if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
7147 ++ wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
7148 ++ return;
7149 ++ }
7150 ++
7151 ++ asm volatile(
7152 ++ /* First ensure the pages are in the TLB */
7153 ++ "xorl %%eax, %%eax\n"
7154 ++ ".Lpopulate_tlb:\n\t"
7155 ++ "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
7156 ++ "addl $4096, %%eax\n\t"
7157 ++ "cmpl %%eax, %[size]\n\t"
7158 ++ "jne .Lpopulate_tlb\n\t"
7159 ++ "xorl %%eax, %%eax\n\t"
7160 ++ "cpuid\n\t"
7161 ++ /* Now fill the cache */
7162 ++ "xorl %%eax, %%eax\n"
7163 ++ ".Lfill_cache:\n"
7164 ++ "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
7165 ++ "addl $64, %%eax\n\t"
7166 ++ "cmpl %%eax, %[size]\n\t"
7167 ++ "jne .Lfill_cache\n\t"
7168 ++ "lfence\n"
7169 ++ :: [flush_pages] "r" (vmx_l1d_flush_pages),
7170 ++ [size] "r" (size)
7171 ++ : "eax", "ebx", "ecx", "edx");
7172 ++}
7173 ++
7174 ++static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
7175 ++{
7176 ++ struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7177 ++ int tpr_threshold;
7178 ++
7179 ++ if (is_guest_mode(vcpu) &&
7180 ++ nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
7181 ++ return;
7182 ++
7183 ++ tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
7184 ++ if (is_guest_mode(vcpu))
7185 ++ to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
7186 ++ else
7187 ++ vmcs_write32(TPR_THRESHOLD, tpr_threshold);
7188 ++}
7189 ++
7190 ++void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
7191 ++{
7192 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7193 ++ u32 sec_exec_control;
7194 ++
7195 ++ if (!lapic_in_kernel(vcpu))
7196 ++ return;
7197 ++
7198 ++ if (!flexpriority_enabled &&
7199 ++ !cpu_has_vmx_virtualize_x2apic_mode())
7200 ++ return;
7201 ++
7202 ++ /* Postpone execution until vmcs01 is the current VMCS. */
7203 ++ if (is_guest_mode(vcpu)) {
7204 ++ vmx->nested.change_vmcs01_virtual_apic_mode = true;
7205 ++ return;
7206 ++ }
7207 ++
7208 ++ sec_exec_control = secondary_exec_controls_get(vmx);
7209 ++ sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
7210 ++ SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
7211 ++
7212 ++ switch (kvm_get_apic_mode(vcpu)) {
7213 ++ case LAPIC_MODE_INVALID:
7214 ++ WARN_ONCE(true, "Invalid local APIC state");
7215 ++ case LAPIC_MODE_DISABLED:
7216 ++ break;
7217 ++ case LAPIC_MODE_XAPIC:
7218 ++ if (flexpriority_enabled) {
7219 ++ sec_exec_control |=
7220 ++ SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7221 ++ vmx_flush_tlb(vcpu, true);
7222 ++ }
7223 ++ break;
7224 ++ case LAPIC_MODE_X2APIC:
7225 ++ if (cpu_has_vmx_virtualize_x2apic_mode())
7226 ++ sec_exec_control |=
7227 ++ SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
7228 ++ break;
7229 ++ }
7230 ++ secondary_exec_controls_set(vmx, sec_exec_control);
7231 ++
7232 ++ vmx_update_msr_bitmap(vcpu);
7233 ++}
7234 ++
7235 ++static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
7236 ++{
7237 ++ if (!is_guest_mode(vcpu)) {
7238 ++ vmcs_write64(APIC_ACCESS_ADDR, hpa);
7239 ++ vmx_flush_tlb(vcpu, true);
7240 ++ }
7241 ++}
7242 ++
7243 ++static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
7244 ++{
7245 ++ u16 status;
7246 ++ u8 old;
7247 ++
7248 ++ if (max_isr == -1)
7249 ++ max_isr = 0;
7250 ++
7251 ++ status = vmcs_read16(GUEST_INTR_STATUS);
7252 ++ old = status >> 8;
7253 ++ if (max_isr != old) {
7254 ++ status &= 0xff;
7255 ++ status |= max_isr << 8;
7256 ++ vmcs_write16(GUEST_INTR_STATUS, status);
7257 ++ }
7258 ++}
7259 ++
7260 ++static void vmx_set_rvi(int vector)
7261 ++{
7262 ++ u16 status;
7263 ++ u8 old;
7264 ++
7265 ++ if (vector == -1)
7266 ++ vector = 0;
7267 ++
7268 ++ status = vmcs_read16(GUEST_INTR_STATUS);
7269 ++ old = (u8)status & 0xff;
7270 ++ if ((u8)vector != old) {
7271 ++ status &= ~0xff;
7272 ++ status |= (u8)vector;
7273 ++ vmcs_write16(GUEST_INTR_STATUS, status);
7274 ++ }
7275 ++}
7276 ++
7277 ++static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
7278 ++{
7279 ++ /*
7280 ++ * When running L2, updating RVI is only relevant when
7281 ++ * vmcs12 virtual-interrupt-delivery enabled.
7282 ++ * However, it can be enabled only when L1 also
7283 ++ * intercepts external-interrupts and in that case
7284 ++ * we should not update vmcs02 RVI but instead intercept
7285 ++ * interrupt. Therefore, do nothing when running L2.
7286 ++ */
7287 ++ if (!is_guest_mode(vcpu))
7288 ++ vmx_set_rvi(max_irr);
7289 ++}
7290 ++
7291 ++static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
7292 ++{
7293 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7294 ++ int max_irr;
7295 ++ bool max_irr_updated;
7296 ++
7297 ++ WARN_ON(!vcpu->arch.apicv_active);
7298 ++ if (pi_test_on(&vmx->pi_desc)) {
7299 ++ pi_clear_on(&vmx->pi_desc);
7300 ++ /*
7301 ++ * IOMMU can write to PID.ON, so the barrier matters even on UP.
7302 ++ * But on x86 this is just a compiler barrier anyway.
7303 ++ */
7304 ++ smp_mb__after_atomic();
7305 ++ max_irr_updated =
7306 ++ kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
7307 ++
7308 ++ /*
7309 ++ * If we are running L2 and L1 has a new pending interrupt
7310 ++ * which can be injected, we should re-evaluate
7311 ++ * what should be done with this new L1 interrupt.
7312 ++ * If L1 intercepts external-interrupts, we should
7313 ++ * exit from L2 to L1. Otherwise, interrupt should be
7314 ++ * delivered directly to L2.
7315 ++ */
7316 ++ if (is_guest_mode(vcpu) && max_irr_updated) {
7317 ++ if (nested_exit_on_intr(vcpu))
7318 ++ kvm_vcpu_exiting_guest_mode(vcpu);
7319 ++ else
7320 ++ kvm_make_request(KVM_REQ_EVENT, vcpu);
7321 ++ }
7322 ++ } else {
7323 ++ max_irr = kvm_lapic_find_highest_irr(vcpu);
7324 ++ }
7325 ++ vmx_hwapic_irr_update(vcpu, max_irr);
7326 ++ return max_irr;
7327 ++}
7328 ++
7329 ++static bool vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
7330 ++{
7331 ++ struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7332 ++
7333 ++ return pi_test_on(pi_desc) ||
7334 ++ (pi_test_sn(pi_desc) && !pi_is_pir_empty(pi_desc));
7335 ++}
7336 ++
7337 ++static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
7338 ++{
7339 ++ if (!kvm_vcpu_apicv_active(vcpu))
7340 ++ return;
7341 ++
7342 ++ vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
7343 ++ vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
7344 ++ vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
7345 ++ vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
7346 ++}
7347 ++
7348 ++static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
7349 ++{
7350 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7351 ++
7352 ++ pi_clear_on(&vmx->pi_desc);
7353 ++ memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
7354 ++}
7355 ++
7356 ++static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
7357 ++{
7358 ++ vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7359 ++
7360 ++ /* if exit due to PF check for async PF */
7361 ++ if (is_page_fault(vmx->exit_intr_info))
7362 ++ vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
7363 ++
7364 ++ /* Handle machine checks before interrupts are enabled */
7365 ++ if (is_machine_check(vmx->exit_intr_info))
7366 ++ kvm_machine_check();
7367 ++
7368 ++ /* We need to handle NMIs before interrupts are enabled */
7369 ++ if (is_nmi(vmx->exit_intr_info)) {
7370 ++ kvm_before_interrupt(&vmx->vcpu);
7371 ++ asm("int $2");
7372 ++ kvm_after_interrupt(&vmx->vcpu);
7373 ++ }
7374 ++}
7375 ++
7376 ++static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
7377 ++{
7378 ++ unsigned int vector;
7379 ++ unsigned long entry;
7380 ++#ifdef CONFIG_X86_64
7381 ++ unsigned long tmp;
7382 ++#endif
7383 ++ gate_desc *desc;
7384 ++ u32 intr_info;
7385 ++
7386 ++ intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7387 ++ if (WARN_ONCE(!is_external_intr(intr_info),
7388 ++ "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
7389 ++ return;
7390 ++
7391 ++ vector = intr_info & INTR_INFO_VECTOR_MASK;
7392 ++ desc = (gate_desc *)host_idt_base + vector;
7393 ++ entry = gate_offset(desc);
7394 ++
7395 ++ kvm_before_interrupt(vcpu);
7396 ++
7397 ++ asm volatile(
7398 ++#ifdef CONFIG_X86_64
7399 ++ "mov %%" _ASM_SP ", %[sp]\n\t"
7400 ++ "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
7401 ++ "push $%c[ss]\n\t"
7402 ++ "push %[sp]\n\t"
7403 ++#endif
7404 ++ "pushf\n\t"
7405 ++ __ASM_SIZE(push) " $%c[cs]\n\t"
7406 ++ CALL_NOSPEC
7407 ++ :
7408 ++#ifdef CONFIG_X86_64
7409 ++ [sp]"=&r"(tmp),
7410 ++#endif
7411 ++ ASM_CALL_CONSTRAINT
7412 ++ :
7413 ++ THUNK_TARGET(entry),
7414 ++ [ss]"i"(__KERNEL_DS),
7415 ++ [cs]"i"(__KERNEL_CS)
7416 ++ );
7417 ++
7418 ++ kvm_after_interrupt(vcpu);
7419 ++}
7420 ++STACK_FRAME_NON_STANDARD(handle_external_interrupt_irqoff);
7421 ++
7422 ++static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu,
7423 ++ enum exit_fastpath_completion *exit_fastpath)
7424 ++{
7425 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7426 ++
7427 ++ if (vmx->exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
7428 ++ handle_external_interrupt_irqoff(vcpu);
7429 ++ else if (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)
7430 ++ handle_exception_nmi_irqoff(vmx);
7431 ++ else if (!is_guest_mode(vcpu) &&
7432 ++ vmx->exit_reason == EXIT_REASON_MSR_WRITE)
7433 ++ *exit_fastpath = handle_fastpath_set_msr_irqoff(vcpu);
7434 ++}
7435 ++
7436 ++static bool vmx_has_emulated_msr(int index)
7437 ++{
7438 ++ switch (index) {
7439 ++ case MSR_IA32_SMBASE:
7440 ++ /*
7441 ++ * We cannot do SMM unless we can run the guest in big
7442 ++ * real mode.
7443 ++ */
7444 ++ return enable_unrestricted_guest || emulate_invalid_guest_state;
7445 ++ case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
7446 ++ return nested;
7447 ++ case MSR_AMD64_VIRT_SPEC_CTRL:
7448 ++ /* This is AMD only. */
7449 ++ return false;
7450 ++ default:
7451 ++ return true;
7452 ++ }
7453 ++}
7454 ++
7455 ++static bool vmx_pt_supported(void)
7456 ++{
7457 ++ return pt_mode == PT_MODE_HOST_GUEST;
7458 ++}
7459 ++
7460 ++static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
7461 ++{
7462 ++ u32 exit_intr_info;
7463 ++ bool unblock_nmi;
7464 ++ u8 vector;
7465 ++ bool idtv_info_valid;
7466 ++
7467 ++ idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
7468 ++
7469 ++ if (enable_vnmi) {
7470 ++ if (vmx->loaded_vmcs->nmi_known_unmasked)
7471 ++ return;
7472 ++ /*
7473 ++ * Can't use vmx->exit_intr_info since we're not sure what
7474 ++ * the exit reason is.
7475 ++ */
7476 ++ exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7477 ++ unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
7478 ++ vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7479 ++ /*
7480 ++ * SDM 3: 27.7.1.2 (September 2008)
7481 ++ * Re-set bit "block by NMI" before VM entry if vmexit caused by
7482 ++ * a guest IRET fault.
7483 ++ * SDM 3: 23.2.2 (September 2008)
7484 ++ * Bit 12 is undefined in any of the following cases:
7485 ++ * If the VM exit sets the valid bit in the IDT-vectoring
7486 ++ * information field.
7487 ++ * If the VM exit is due to a double fault.
7488 ++ */
7489 ++ if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
7490 ++ vector != DF_VECTOR && !idtv_info_valid)
7491 ++ vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7492 ++ GUEST_INTR_STATE_NMI);
7493 ++ else
7494 ++ vmx->loaded_vmcs->nmi_known_unmasked =
7495 ++ !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
7496 ++ & GUEST_INTR_STATE_NMI);
7497 ++ } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
7498 ++ vmx->loaded_vmcs->vnmi_blocked_time +=
7499 ++ ktime_to_ns(ktime_sub(ktime_get(),
7500 ++ vmx->loaded_vmcs->entry_time));
7501 ++}
7502 ++
7503 ++static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
7504 ++ u32 idt_vectoring_info,
7505 ++ int instr_len_field,
7506 ++ int error_code_field)
7507 ++{
7508 ++ u8 vector;
7509 ++ int type;
7510 ++ bool idtv_info_valid;
7511 ++
7512 ++ idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
7513 ++
7514 ++ vcpu->arch.nmi_injected = false;
7515 ++ kvm_clear_exception_queue(vcpu);
7516 ++ kvm_clear_interrupt_queue(vcpu);
7517 ++
7518 ++ if (!idtv_info_valid)
7519 ++ return;
7520 ++
7521 ++ kvm_make_request(KVM_REQ_EVENT, vcpu);
7522 ++
7523 ++ vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
7524 ++ type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
7525 ++
7526 ++ switch (type) {
7527 ++ case INTR_TYPE_NMI_INTR:
7528 ++ vcpu->arch.nmi_injected = true;
7529 ++ /*
7530 ++ * SDM 3: 27.7.1.2 (September 2008)
7531 ++ * Clear bit "block by NMI" before VM entry if a NMI
7532 ++ * delivery faulted.
7533 ++ */
7534 ++ vmx_set_nmi_mask(vcpu, false);
7535 ++ break;
7536 ++ case INTR_TYPE_SOFT_EXCEPTION:
7537 ++ vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
7538 ++ /* fall through */
7539 ++ case INTR_TYPE_HARD_EXCEPTION:
7540 ++ if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
7541 ++ u32 err = vmcs_read32(error_code_field);
7542 ++ kvm_requeue_exception_e(vcpu, vector, err);
7543 ++ } else
7544 ++ kvm_requeue_exception(vcpu, vector);
7545 ++ break;
7546 ++ case INTR_TYPE_SOFT_INTR:
7547 ++ vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
7548 ++ /* fall through */
7549 ++ case INTR_TYPE_EXT_INTR:
7550 ++ kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
7551 ++ break;
7552 ++ default:
7553 ++ break;
7554 ++ }
7555 ++}
7556 ++
7557 ++static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
7558 ++{
7559 ++ __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
7560 ++ VM_EXIT_INSTRUCTION_LEN,
7561 ++ IDT_VECTORING_ERROR_CODE);
7562 ++}
7563 ++
7564 ++static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
7565 ++{
7566 ++ __vmx_complete_interrupts(vcpu,
7567 ++ vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
7568 ++ VM_ENTRY_INSTRUCTION_LEN,
7569 ++ VM_ENTRY_EXCEPTION_ERROR_CODE);
7570 ++
7571 ++ vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
7572 ++}
7573 ++
7574 ++static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
7575 ++{
7576 ++ int i, nr_msrs;
7577 ++ struct perf_guest_switch_msr *msrs;
7578 ++
7579 ++ msrs = perf_guest_get_msrs(&nr_msrs);
7580 ++
7581 ++ if (!msrs)
7582 ++ return;
7583 ++
7584 ++ for (i = 0; i < nr_msrs; i++)
7585 ++ if (msrs[i].host == msrs[i].guest)
7586 ++ clear_atomic_switch_msr(vmx, msrs[i].msr);
7587 ++ else
7588 ++ add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
7589 ++ msrs[i].host, false);
7590 ++}
7591 ++
7592 ++static void atomic_switch_umwait_control_msr(struct vcpu_vmx *vmx)
7593 ++{
7594 ++ u32 host_umwait_control;
7595 ++
7596 ++ if (!vmx_has_waitpkg(vmx))
7597 ++ return;
7598 ++
7599 ++ host_umwait_control = get_umwait_control_msr();
7600 ++
7601 ++ if (vmx->msr_ia32_umwait_control != host_umwait_control)
7602 ++ add_atomic_switch_msr(vmx, MSR_IA32_UMWAIT_CONTROL,
7603 ++ vmx->msr_ia32_umwait_control,
7604 ++ host_umwait_control, false);
7605 ++ else
7606 ++ clear_atomic_switch_msr(vmx, MSR_IA32_UMWAIT_CONTROL);
7607 ++}
7608 ++
7609 ++static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
7610 ++{
7611 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7612 ++ u64 tscl;
7613 ++ u32 delta_tsc;
7614 ++
7615 ++ if (vmx->req_immediate_exit) {
7616 ++ vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0);
7617 ++ vmx->loaded_vmcs->hv_timer_soft_disabled = false;
7618 ++ } else if (vmx->hv_deadline_tsc != -1) {
7619 ++ tscl = rdtsc();
7620 ++ if (vmx->hv_deadline_tsc > tscl)
7621 ++ /* set_hv_timer ensures the delta fits in 32-bits */
7622 ++ delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
7623 ++ cpu_preemption_timer_multi);
7624 ++ else
7625 ++ delta_tsc = 0;
7626 ++
7627 ++ vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
7628 ++ vmx->loaded_vmcs->hv_timer_soft_disabled = false;
7629 ++ } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) {
7630 ++ vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1);
7631 ++ vmx->loaded_vmcs->hv_timer_soft_disabled = true;
7632 ++ }
7633 ++}
7634 ++
7635 ++void vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
7636 ++{
7637 ++ if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
7638 ++ vmx->loaded_vmcs->host_state.rsp = host_rsp;
7639 ++ vmcs_writel(HOST_RSP, host_rsp);
7640 ++ }
7641 ++}
7642 ++
7643 ++bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs, bool launched);
7644 ++
7645 ++static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
7646 ++{
7647 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7648 ++ unsigned long cr3, cr4;
7649 ++
7650 ++ /* Record the guest's net vcpu time for enforced NMI injections. */
7651 ++ if (unlikely(!enable_vnmi &&
7652 ++ vmx->loaded_vmcs->soft_vnmi_blocked))
7653 ++ vmx->loaded_vmcs->entry_time = ktime_get();
7654 ++
7655 ++ /* Don't enter VMX if guest state is invalid, let the exit handler
7656 ++ start emulation until we arrive back to a valid state */
7657 ++ if (vmx->emulation_required)
7658 ++ return;
7659 ++
7660 ++ if (vmx->ple_window_dirty) {
7661 ++ vmx->ple_window_dirty = false;
7662 ++ vmcs_write32(PLE_WINDOW, vmx->ple_window);
7663 ++ }
7664 ++
7665 ++ if (vmx->nested.need_vmcs12_to_shadow_sync)
7666 ++ nested_sync_vmcs12_to_shadow(vcpu);
7667 ++
7668 ++ if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP))
7669 ++ vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
7670 ++ if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP))
7671 ++ vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
7672 ++
7673 ++ cr3 = __get_current_cr3_fast();
7674 ++ if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
7675 ++ vmcs_writel(HOST_CR3, cr3);
7676 ++ vmx->loaded_vmcs->host_state.cr3 = cr3;
7677 ++ }
7678 ++
7679 ++ cr4 = cr4_read_shadow();
7680 ++ if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
7681 ++ vmcs_writel(HOST_CR4, cr4);
7682 ++ vmx->loaded_vmcs->host_state.cr4 = cr4;
7683 ++ }
7684 ++
7685 ++ /* When single-stepping over STI and MOV SS, we must clear the
7686 ++ * corresponding interruptibility bits in the guest state. Otherwise
7687 ++ * vmentry fails as it then expects bit 14 (BS) in pending debug
7688 ++ * exceptions being set, but that's not correct for the guest debugging
7689 ++ * case. */
7690 ++ if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
7691 ++ vmx_set_interrupt_shadow(vcpu, 0);
7692 ++
7693 ++ kvm_load_guest_xsave_state(vcpu);
7694 ++
7695 ++ if (static_cpu_has(X86_FEATURE_PKU) &&
7696 ++ kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
7697 ++ vcpu->arch.pkru != vmx->host_pkru)
7698 ++ __write_pkru(vcpu->arch.pkru);
7699 ++
7700 ++ pt_guest_enter(vmx);
7701 ++
7702 ++ atomic_switch_perf_msrs(vmx);
7703 ++ atomic_switch_umwait_control_msr(vmx);
7704 ++
7705 ++ if (enable_preemption_timer)
7706 ++ vmx_update_hv_timer(vcpu);
7707 ++
7708 ++ if (lapic_in_kernel(vcpu) &&
7709 ++ vcpu->arch.apic->lapic_timer.timer_advance_ns)
7710 ++ kvm_wait_lapic_expire(vcpu);
7711 ++
7712 ++ /*
7713 ++ * If this vCPU has touched SPEC_CTRL, restore the guest's value if
7714 ++ * it's non-zero. Since vmentry is serialising on affected CPUs, there
7715 ++ * is no need to worry about the conditional branch over the wrmsr
7716 ++ * being speculatively taken.
7717 ++ */
7718 ++ x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
7719 ++
7720 ++ /* L1D Flush includes CPU buffer clear to mitigate MDS */
7721 ++ if (static_branch_unlikely(&vmx_l1d_should_flush))
7722 ++ vmx_l1d_flush(vcpu);
7723 ++ else if (static_branch_unlikely(&mds_user_clear))
7724 ++ mds_clear_cpu_buffers();
7725 ++
7726 ++ if (vcpu->arch.cr2 != read_cr2())
7727 ++ write_cr2(vcpu->arch.cr2);
7728 ++
7729 ++ vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
7730 ++ vmx->loaded_vmcs->launched);
7731 ++
7732 ++ vcpu->arch.cr2 = read_cr2();
7733 ++
7734 ++ /*
7735 ++ * We do not use IBRS in the kernel. If this vCPU has used the
7736 ++ * SPEC_CTRL MSR it may have left it on; save the value and
7737 ++ * turn it off. This is much more efficient than blindly adding
7738 ++ * it to the atomic save/restore list. Especially as the former
7739 ++ * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
7740 ++ *
7741 ++ * For non-nested case:
7742 ++ * If the L01 MSR bitmap does not intercept the MSR, then we need to
7743 ++ * save it.
7744 ++ *
7745 ++ * For nested case:
7746 ++ * If the L02 MSR bitmap does not intercept the MSR, then we need to
7747 ++ * save it.
7748 ++ */
7749 ++ if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
7750 ++ vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
7751 ++
7752 ++ x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
7753 ++
7754 ++ /* All fields are clean at this point */
7755 ++ if (static_branch_unlikely(&enable_evmcs))
7756 ++ current_evmcs->hv_clean_fields |=
7757 ++ HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
7758 ++
7759 ++ if (static_branch_unlikely(&enable_evmcs))
7760 ++ current_evmcs->hv_vp_id = vcpu->arch.hyperv.vp_index;
7761 ++
7762 ++ /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
7763 ++ if (vmx->host_debugctlmsr)
7764 ++ update_debugctlmsr(vmx->host_debugctlmsr);
7765 ++
7766 ++#ifndef CONFIG_X86_64
7767 ++ /*
7768 ++ * The sysexit path does not restore ds/es, so we must set them to
7769 ++ * a reasonable value ourselves.
7770 ++ *
7771 ++ * We can't defer this to vmx_prepare_switch_to_host() since that
7772 ++ * function may be executed in interrupt context, which saves and
7773 ++ * restore segments around it, nullifying its effect.
7774 ++ */
7775 ++ loadsegment(ds, __USER_DS);
7776 ++ loadsegment(es, __USER_DS);
7777 ++#endif
7778 ++
7779 ++ vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
7780 ++ | (1 << VCPU_EXREG_RFLAGS)
7781 ++ | (1 << VCPU_EXREG_PDPTR)
7782 ++ | (1 << VCPU_EXREG_SEGMENTS)
7783 ++ | (1 << VCPU_EXREG_CR3));
7784 ++ vcpu->arch.regs_dirty = 0;
7785 ++
7786 ++ pt_guest_exit(vmx);
7787 ++
7788 ++ /*
7789 ++ * eager fpu is enabled if PKEY is supported and CR4 is switched
7790 ++ * back on host, so it is safe to read guest PKRU from current
7791 ++ * XSAVE.
7792 ++ */
7793 ++ if (static_cpu_has(X86_FEATURE_PKU) &&
7794 ++ kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
7795 ++ vcpu->arch.pkru = rdpkru();
7796 ++ if (vcpu->arch.pkru != vmx->host_pkru)
7797 ++ __write_pkru(vmx->host_pkru);
7798 ++ }
7799 ++
7800 ++ kvm_load_host_xsave_state(vcpu);
7801 ++
7802 ++ vmx->nested.nested_run_pending = 0;
7803 ++ vmx->idt_vectoring_info = 0;
7804 ++
7805 ++ vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
7806 ++ if ((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
7807 ++ kvm_machine_check();
7808 ++
7809 ++ if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
7810 ++ return;
7811 ++
7812 ++ vmx->loaded_vmcs->launched = 1;
7813 ++ vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
7814 ++
7815 ++ vmx_recover_nmi_blocking(vmx);
7816 ++ vmx_complete_interrupts(vmx);
7817 ++}
7818 ++
7819 ++static struct kvm *vmx_vm_alloc(void)
7820 ++{
7821 ++ struct kvm_vmx *kvm_vmx = __vmalloc(sizeof(struct kvm_vmx),
7822 ++ GFP_KERNEL_ACCOUNT | __GFP_ZERO,
7823 ++ PAGE_KERNEL);
7824 ++ return &kvm_vmx->kvm;
7825 ++}
7826 ++
7827 ++static void vmx_vm_free(struct kvm *kvm)
7828 ++{
7829 ++ kfree(kvm->arch.hyperv.hv_pa_pg);
7830 ++ vfree(to_kvm_vmx(kvm));
7831 ++}
7832 ++
7833 ++static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
7834 ++{
7835 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7836 ++
7837 ++ if (enable_pml)
7838 ++ vmx_destroy_pml_buffer(vmx);
7839 ++ free_vpid(vmx->vpid);
7840 ++ nested_vmx_free_vcpu(vcpu);
7841 ++ free_loaded_vmcs(vmx->loaded_vmcs);
7842 ++ kvm_vcpu_uninit(vcpu);
7843 ++ kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.user_fpu);
7844 ++ kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
7845 ++ kmem_cache_free(kvm_vcpu_cache, vmx);
7846 ++}
7847 ++
7848 ++static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
7849 ++{
7850 ++ int err;
7851 ++ struct vcpu_vmx *vmx;
7852 ++ unsigned long *msr_bitmap;
7853 ++ int i, cpu;
7854 ++
7855 ++ BUILD_BUG_ON_MSG(offsetof(struct vcpu_vmx, vcpu) != 0,
7856 ++ "struct kvm_vcpu must be at offset 0 for arch usercopy region");
7857 ++
7858 ++ vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
7859 ++ if (!vmx)
7860 ++ return ERR_PTR(-ENOMEM);
7861 ++
7862 ++ vmx->vcpu.arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
7863 ++ GFP_KERNEL_ACCOUNT);
7864 ++ if (!vmx->vcpu.arch.user_fpu) {
7865 ++ printk(KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n");
7866 ++ err = -ENOMEM;
7867 ++ goto free_partial_vcpu;
7868 ++ }
7869 ++
7870 ++ vmx->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
7871 ++ GFP_KERNEL_ACCOUNT);
7872 ++ if (!vmx->vcpu.arch.guest_fpu) {
7873 ++ printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n");
7874 ++ err = -ENOMEM;
7875 ++ goto free_user_fpu;
7876 ++ }
7877 ++
7878 ++ vmx->vpid = allocate_vpid();
7879 ++
7880 ++ err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
7881 ++ if (err)
7882 ++ goto free_vcpu;
7883 ++
7884 ++ err = -ENOMEM;
7885 ++
7886 ++ /*
7887 ++ * If PML is turned on, failure on enabling PML just results in failure
7888 ++ * of creating the vcpu, therefore we can simplify PML logic (by
7889 ++ * avoiding dealing with cases, such as enabling PML partially on vcpus
7890 ++ * for the guest), etc.
7891 ++ */
7892 ++ if (enable_pml) {
7893 ++ vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
7894 ++ if (!vmx->pml_pg)
7895 ++ goto uninit_vcpu;
7896 ++ }
7897 ++
7898 ++ BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) != NR_SHARED_MSRS);
7899 ++
7900 ++ for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
7901 ++ u32 index = vmx_msr_index[i];
7902 ++ u32 data_low, data_high;
7903 ++ int j = vmx->nmsrs;
7904 ++
7905 ++ if (rdmsr_safe(index, &data_low, &data_high) < 0)
7906 ++ continue;
7907 ++ if (wrmsr_safe(index, data_low, data_high) < 0)
7908 ++ continue;
7909 ++
7910 ++ vmx->guest_msrs[j].index = i;
7911 ++ vmx->guest_msrs[j].data = 0;
7912 ++ switch (index) {
7913 ++ case MSR_IA32_TSX_CTRL:
7914 ++ /*
7915 ++ * No need to pass TSX_CTRL_CPUID_CLEAR through, so
7916 ++ * let's avoid changing CPUID bits under the host
7917 ++ * kernel's feet.
7918 ++ */
7919 ++ vmx->guest_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
7920 ++ break;
7921 ++ default:
7922 ++ vmx->guest_msrs[j].mask = -1ull;
7923 ++ break;
7924 ++ }
7925 ++ ++vmx->nmsrs;
7926 ++ }
7927 ++
7928 ++ err = alloc_loaded_vmcs(&vmx->vmcs01);
7929 ++ if (err < 0)
7930 ++ goto free_pml;
7931 ++
7932 ++ msr_bitmap = vmx->vmcs01.msr_bitmap;
7933 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
7934 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
7935 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
7936 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
7937 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
7938 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
7939 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
7940 ++ if (kvm_cstate_in_guest(kvm)) {
7941 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
7942 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
7943 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
7944 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
7945 ++ }
7946 ++ vmx->msr_bitmap_mode = 0;
7947 ++
7948 ++ vmx->loaded_vmcs = &vmx->vmcs01;
7949 ++ cpu = get_cpu();
7950 ++ vmx_vcpu_load(&vmx->vcpu, cpu);
7951 ++ vmx->vcpu.cpu = cpu;
7952 ++ init_vmcs(vmx);
7953 ++ vmx_vcpu_put(&vmx->vcpu);
7954 ++ put_cpu();
7955 ++ if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
7956 ++ err = alloc_apic_access_page(kvm);
7957 ++ if (err)
7958 ++ goto free_vmcs;
7959 ++ }
7960 ++
7961 ++ if (enable_ept && !enable_unrestricted_guest) {
7962 ++ err = init_rmode_identity_map(kvm);
7963 ++ if (err)
7964 ++ goto free_vmcs;
7965 ++ }
7966 ++
7967 ++ if (nested)
7968 ++ nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
7969 ++ vmx_capability.ept,
7970 ++ kvm_vcpu_apicv_active(&vmx->vcpu));
7971 ++ else
7972 ++ memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
7973 ++
7974 ++ vmx->nested.posted_intr_nv = -1;
7975 ++ vmx->nested.current_vmptr = -1ull;
7976 ++
7977 ++ vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
7978 ++
7979 ++ /*
7980 ++ * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
7981 ++ * or POSTED_INTR_WAKEUP_VECTOR.
7982 ++ */
7983 ++ vmx->pi_desc.nv = POSTED_INTR_VECTOR;
7984 ++ vmx->pi_desc.sn = 1;
7985 ++
7986 ++ vmx->ept_pointer = INVALID_PAGE;
7987 ++
7988 ++ return &vmx->vcpu;
7989 ++
7990 ++free_vmcs:
7991 ++ free_loaded_vmcs(vmx->loaded_vmcs);
7992 ++free_pml:
7993 ++ vmx_destroy_pml_buffer(vmx);
7994 ++uninit_vcpu:
7995 ++ kvm_vcpu_uninit(&vmx->vcpu);
7996 ++free_vcpu:
7997 ++ free_vpid(vmx->vpid);
7998 ++ kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
7999 ++free_user_fpu:
8000 ++ kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.user_fpu);
8001 ++free_partial_vcpu:
8002 ++ kmem_cache_free(kvm_vcpu_cache, vmx);
8003 ++ return ERR_PTR(err);
8004 ++}
8005 ++
8006 ++#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
8007 ++#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
8008 ++
8009 ++static int vmx_vm_init(struct kvm *kvm)
8010 ++{
8011 ++ spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
8012 ++
8013 ++ if (!ple_gap)
8014 ++ kvm->arch.pause_in_guest = true;
8015 ++
8016 ++ if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
8017 ++ switch (l1tf_mitigation) {
8018 ++ case L1TF_MITIGATION_OFF:
8019 ++ case L1TF_MITIGATION_FLUSH_NOWARN:
8020 ++ /* 'I explicitly don't care' is set */
8021 ++ break;
8022 ++ case L1TF_MITIGATION_FLUSH:
8023 ++ case L1TF_MITIGATION_FLUSH_NOSMT:
8024 ++ case L1TF_MITIGATION_FULL:
8025 ++ /*
8026 ++ * Warn upon starting the first VM in a potentially
8027 ++ * insecure environment.
8028 ++ */
8029 ++ if (sched_smt_active())
8030 ++ pr_warn_once(L1TF_MSG_SMT);
8031 ++ if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
8032 ++ pr_warn_once(L1TF_MSG_L1D);
8033 ++ break;
8034 ++ case L1TF_MITIGATION_FULL_FORCE:
8035 ++ /* Flush is enforced */
8036 ++ break;
8037 ++ }
8038 ++ }
8039 ++ return 0;
8040 ++}
8041 ++
8042 ++static int __init vmx_check_processor_compat(void)
8043 ++{
8044 ++ struct vmcs_config vmcs_conf;
8045 ++ struct vmx_capability vmx_cap;
8046 ++
8047 ++ if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
8048 ++ return -EIO;
8049 ++ if (nested)
8050 ++ nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept,
8051 ++ enable_apicv);
8052 ++ if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
8053 ++ printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
8054 ++ smp_processor_id());
8055 ++ return -EIO;
8056 ++ }
8057 ++ return 0;
8058 ++}
8059 ++
8060 ++static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
8061 ++{
8062 ++ u8 cache;
8063 ++ u64 ipat = 0;
8064 ++
8065 ++ /* For VT-d and EPT combination
8066 ++ * 1. MMIO: always map as UC
8067 ++ * 2. EPT with VT-d:
8068 ++ * a. VT-d without snooping control feature: can't guarantee the
8069 ++ * result, try to trust guest.
8070 ++ * b. VT-d with snooping control feature: snooping control feature of
8071 ++ * VT-d engine can guarantee the cache correctness. Just set it
8072 ++ * to WB to keep consistent with host. So the same as item 3.
8073 ++ * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
8074 ++ * consistent with host MTRR
8075 ++ */
8076 ++ if (is_mmio) {
8077 ++ cache = MTRR_TYPE_UNCACHABLE;
8078 ++ goto exit;
8079 ++ }
8080 ++
8081 ++ if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
8082 ++ ipat = VMX_EPT_IPAT_BIT;
8083 ++ cache = MTRR_TYPE_WRBACK;
8084 ++ goto exit;
8085 ++ }
8086 ++
8087 ++ if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
8088 ++ ipat = VMX_EPT_IPAT_BIT;
8089 ++ if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
8090 ++ cache = MTRR_TYPE_WRBACK;
8091 ++ else
8092 ++ cache = MTRR_TYPE_UNCACHABLE;
8093 ++ goto exit;
8094 ++ }
8095 ++
8096 ++ cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
8097 ++
8098 ++exit:
8099 ++ return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
8100 ++}
8101 ++
8102 ++static int vmx_get_lpage_level(void)
8103 ++{
8104 ++ if (enable_ept && !cpu_has_vmx_ept_1g_page())
8105 ++ return PT_DIRECTORY_LEVEL;
8106 ++ else
8107 ++ /* For shadow and EPT supported 1GB page */
8108 ++ return PT_PDPE_LEVEL;
8109 ++}
8110 ++
8111 ++static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx)
8112 ++{
8113 ++ /*
8114 ++ * These bits in the secondary execution controls field
8115 ++ * are dynamic, the others are mostly based on the hypervisor
8116 ++ * architecture and the guest's CPUID. Do not touch the
8117 ++ * dynamic bits.
8118 ++ */
8119 ++ u32 mask =
8120 ++ SECONDARY_EXEC_SHADOW_VMCS |
8121 ++ SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
8122 ++ SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
8123 ++ SECONDARY_EXEC_DESC;
8124 ++
8125 ++ u32 new_ctl = vmx->secondary_exec_control;
8126 ++ u32 cur_ctl = secondary_exec_controls_get(vmx);
8127 ++
8128 ++ secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask));
8129 ++}
8130 ++
8131 ++/*
8132 ++ * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
8133 ++ * (indicating "allowed-1") if they are supported in the guest's CPUID.
8134 ++ */
8135 ++static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
8136 ++{
8137 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
8138 ++ struct kvm_cpuid_entry2 *entry;
8139 ++
8140 ++ vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
8141 ++ vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
8142 ++
8143 ++#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
8144 ++ if (entry && (entry->_reg & (_cpuid_mask))) \
8145 ++ vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
8146 ++} while (0)
8147 ++
8148 ++ entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
8149 ++ cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
8150 ++ cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
8151 ++ cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
8152 ++ cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
8153 ++ cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
8154 ++ cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
8155 ++ cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
8156 ++ cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
8157 ++ cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
8158 ++ cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
8159 ++ cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
8160 ++ cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
8161 ++ cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
8162 ++ cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
8163 ++
8164 ++ entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
8165 ++ cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
8166 ++ cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
8167 ++ cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
8168 ++ cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
8169 ++ cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
8170 ++ cr4_fixed1_update(X86_CR4_LA57, ecx, bit(X86_FEATURE_LA57));
8171 ++
8172 ++#undef cr4_fixed1_update
8173 ++}
8174 ++
8175 ++static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
8176 ++{
8177 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
8178 ++
8179 ++ if (kvm_mpx_supported()) {
8180 ++ bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
8181 ++
8182 ++ if (mpx_enabled) {
8183 ++ vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
8184 ++ vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
8185 ++ } else {
8186 ++ vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
8187 ++ vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
8188 ++ }
8189 ++ }
8190 ++}
8191 ++
8192 ++static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
8193 ++{
8194 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
8195 ++ struct kvm_cpuid_entry2 *best = NULL;
8196 ++ int i;
8197 ++
8198 ++ for (i = 0; i < PT_CPUID_LEAVES; i++) {
8199 ++ best = kvm_find_cpuid_entry(vcpu, 0x14, i);
8200 ++ if (!best)
8201 ++ return;
8202 ++ vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax;
8203 ++ vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx;
8204 ++ vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx;
8205 ++ vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx;
8206 ++ }
8207 ++
8208 ++ /* Get the number of configurable Address Ranges for filtering */
8209 ++ vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps,
8210 ++ PT_CAP_num_address_ranges);
8211 ++
8212 ++ /* Initialize and clear the no dependency bits */
8213 ++ vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS |
8214 ++ RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC);
8215 ++
8216 ++ /*
8217 ++ * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise
8218 ++ * will inject an #GP
8219 ++ */
8220 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering))
8221 ++ vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN;
8222 ++
8223 ++ /*
8224 ++ * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and
8225 ++ * PSBFreq can be set
8226 ++ */
8227 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc))
8228 ++ vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC |
8229 ++ RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ);
8230 ++
8231 ++ /*
8232 ++ * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and
8233 ++ * MTCFreq can be set
8234 ++ */
8235 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc))
8236 ++ vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN |
8237 ++ RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE);
8238 ++
8239 ++ /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */
8240 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite))
8241 ++ vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW |
8242 ++ RTIT_CTL_PTW_EN);
8243 ++
8244 ++ /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */
8245 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace))
8246 ++ vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN;
8247 ++
8248 ++ /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */
8249 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output))
8250 ++ vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA;
8251 ++
8252 ++ /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabircEn can be set */
8253 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys))
8254 ++ vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN;
8255 ++
8256 ++ /* unmask address range configure area */
8257 ++ for (i = 0; i < vmx->pt_desc.addr_range; i++)
8258 ++ vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
8259 ++}
8260 ++
8261 ++static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
8262 ++{
8263 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
8264 ++
8265 ++ /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
8266 ++ vcpu->arch.xsaves_enabled = false;
8267 ++
8268 ++ if (cpu_has_secondary_exec_ctrls()) {
8269 ++ vmx_compute_secondary_exec_control(vmx);
8270 ++ vmcs_set_secondary_exec_control(vmx);
8271 ++ }
8272 ++
8273 ++ if (nested_vmx_allowed(vcpu))
8274 ++ to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
8275 ++ FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX |
8276 ++ FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
8277 ++ else
8278 ++ to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
8279 ++ ~(FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX |
8280 ++ FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX);
8281 ++
8282 ++ if (nested_vmx_allowed(vcpu)) {
8283 ++ nested_vmx_cr_fixed1_bits_update(vcpu);
8284 ++ nested_vmx_entry_exit_ctls_update(vcpu);
8285 ++ }
8286 ++
8287 ++ if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&
8288 ++ guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
8289 ++ update_intel_pt_cfg(vcpu);
8290 ++
8291 ++ if (boot_cpu_has(X86_FEATURE_RTM)) {
8292 ++ struct shared_msr_entry *msr;
8293 ++ msr = find_msr_entry(vmx, MSR_IA32_TSX_CTRL);
8294 ++ if (msr) {
8295 ++ bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM);
8296 ++ vmx_set_guest_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE);
8297 ++ }
8298 ++ }
8299 ++}
8300 ++
8301 ++static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
8302 ++{
8303 ++ if (func == 1 && nested)
8304 ++ entry->ecx |= bit(X86_FEATURE_VMX);
8305 ++}
8306 ++
8307 ++static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
8308 ++{
8309 ++ to_vmx(vcpu)->req_immediate_exit = true;
8310 ++}
8311 ++
8312 ++static int vmx_check_intercept(struct kvm_vcpu *vcpu,
8313 ++ struct x86_instruction_info *info,
8314 ++ enum x86_intercept_stage stage)
8315 ++{
8316 ++ struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8317 ++ struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
8318 ++
8319 ++ /*
8320 ++ * RDPID causes #UD if disabled through secondary execution controls.
8321 ++ * Because it is marked as EmulateOnUD, we need to intercept it here.
8322 ++ */
8323 ++ if (info->intercept == x86_intercept_rdtscp &&
8324 ++ !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
8325 ++ ctxt->exception.vector = UD_VECTOR;
8326 ++ ctxt->exception.error_code_valid = false;
8327 ++ return X86EMUL_PROPAGATE_FAULT;
8328 ++ }
8329 ++
8330 ++ /* TODO: check more intercepts... */
8331 ++ return X86EMUL_CONTINUE;
8332 ++}
8333 ++
8334 ++#ifdef CONFIG_X86_64
8335 ++/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
8336 ++static inline int u64_shl_div_u64(u64 a, unsigned int shift,
8337 ++ u64 divisor, u64 *result)
8338 ++{
8339 ++ u64 low = a << shift, high = a >> (64 - shift);
8340 ++
8341 ++ /* To avoid the overflow on divq */
8342 ++ if (high >= divisor)
8343 ++ return 1;
8344 ++
8345 ++ /* Low hold the result, high hold rem which is discarded */
8346 ++ asm("divq %2\n\t" : "=a" (low), "=d" (high) :
8347 ++ "rm" (divisor), "0" (low), "1" (high));
8348 ++ *result = low;
8349 ++
8350 ++ return 0;
8351 ++}
8352 ++
8353 ++static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
8354 ++ bool *expired)
8355 ++{
8356 ++ struct vcpu_vmx *vmx;
8357 ++ u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
8358 ++ struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
8359 ++
8360 ++ if (kvm_mwait_in_guest(vcpu->kvm) ||
8361 ++ kvm_can_post_timer_interrupt(vcpu))
8362 ++ return -EOPNOTSUPP;
8363 ++
8364 ++ vmx = to_vmx(vcpu);
8365 ++ tscl = rdtsc();
8366 ++ guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
8367 ++ delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
8368 ++ lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
8369 ++ ktimer->timer_advance_ns);
8370 ++
8371 ++ if (delta_tsc > lapic_timer_advance_cycles)
8372 ++ delta_tsc -= lapic_timer_advance_cycles;
8373 ++ else
8374 ++ delta_tsc = 0;
8375 ++
8376 ++ /* Convert to host delta tsc if tsc scaling is enabled */
8377 ++ if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
8378 ++ delta_tsc && u64_shl_div_u64(delta_tsc,
8379 ++ kvm_tsc_scaling_ratio_frac_bits,
8380 ++ vcpu->arch.tsc_scaling_ratio, &delta_tsc))
8381 ++ return -ERANGE;
8382 ++
8383 ++ /*
8384 ++ * If the delta tsc can't fit in the 32 bit after the multi shift,
8385 ++ * we can't use the preemption timer.
8386 ++ * It's possible that it fits on later vmentries, but checking
8387 ++ * on every vmentry is costly so we just use an hrtimer.
8388 ++ */
8389 ++ if (delta_tsc >> (cpu_preemption_timer_multi + 32))
8390 ++ return -ERANGE;
8391 ++
8392 ++ vmx->hv_deadline_tsc = tscl + delta_tsc;
8393 ++ *expired = !delta_tsc;
8394 ++ return 0;
8395 ++}
8396 ++
8397 ++static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
8398 ++{
8399 ++ to_vmx(vcpu)->hv_deadline_tsc = -1;
8400 ++}
8401 ++#endif
8402 ++
8403 ++static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
8404 ++{
8405 ++ if (!kvm_pause_in_guest(vcpu->kvm))
8406 ++ shrink_ple_window(vcpu);
8407 ++}
8408 ++
8409 ++static void vmx_slot_enable_log_dirty(struct kvm *kvm,
8410 ++ struct kvm_memory_slot *slot)
8411 ++{
8412 ++ kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
8413 ++ kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
8414 ++}
8415 ++
8416 ++static void vmx_slot_disable_log_dirty(struct kvm *kvm,
8417 ++ struct kvm_memory_slot *slot)
8418 ++{
8419 ++ kvm_mmu_slot_set_dirty(kvm, slot);
8420 ++}
8421 ++
8422 ++static void vmx_flush_log_dirty(struct kvm *kvm)
8423 ++{
8424 ++ kvm_flush_pml_buffers(kvm);
8425 ++}
8426 ++
8427 ++static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
8428 ++{
8429 ++ struct vmcs12 *vmcs12;
8430 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
8431 ++ gpa_t gpa, dst;
8432 ++
8433 ++ if (is_guest_mode(vcpu)) {
8434 ++ WARN_ON_ONCE(vmx->nested.pml_full);
8435 ++
8436 ++ /*
8437 ++ * Check if PML is enabled for the nested guest.
8438 ++ * Whether eptp bit 6 is set is already checked
8439 ++ * as part of A/D emulation.
8440 ++ */
8441 ++ vmcs12 = get_vmcs12(vcpu);
8442 ++ if (!nested_cpu_has_pml(vmcs12))
8443 ++ return 0;
8444 ++
8445 ++ if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
8446 ++ vmx->nested.pml_full = true;
8447 ++ return 1;
8448 ++ }
8449 ++
8450 ++ gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
8451 ++ dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
8452 ++
8453 ++ if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
8454 ++ offset_in_page(dst), sizeof(gpa)))
8455 ++ return 0;
8456 ++
8457 ++ vmcs12->guest_pml_index--;
8458 ++ }
8459 ++
8460 ++ return 0;
8461 ++}
8462 ++
8463 ++static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
8464 ++ struct kvm_memory_slot *memslot,
8465 ++ gfn_t offset, unsigned long mask)
8466 ++{
8467 ++ kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
8468 ++}
8469 ++
8470 ++static void __pi_post_block(struct kvm_vcpu *vcpu)
8471 ++{
8472 ++ struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
8473 ++ struct pi_desc old, new;
8474 ++ unsigned int dest;
8475 ++
8476 ++ do {
8477 ++ old.control = new.control = pi_desc->control;
8478 ++ WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
8479 ++ "Wakeup handler not enabled while the VCPU is blocked\n");
8480 ++
8481 ++ dest = cpu_physical_id(vcpu->cpu);
8482 ++
8483 ++ if (x2apic_enabled())
8484 ++ new.ndst = dest;
8485 ++ else
8486 ++ new.ndst = (dest << 8) & 0xFF00;
8487 ++
8488 ++ /* set 'NV' to 'notification vector' */
8489 ++ new.nv = POSTED_INTR_VECTOR;
8490 ++ } while (cmpxchg64(&pi_desc->control, old.control,
8491 ++ new.control) != old.control);
8492 ++
8493 ++ if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
8494 ++ spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8495 ++ list_del(&vcpu->blocked_vcpu_list);
8496 ++ spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8497 ++ vcpu->pre_pcpu = -1;
8498 ++ }
8499 ++}
8500 ++
8501 ++/*
8502 ++ * This routine does the following things for vCPU which is going
8503 ++ * to be blocked if VT-d PI is enabled.
8504 ++ * - Store the vCPU to the wakeup list, so when interrupts happen
8505 ++ * we can find the right vCPU to wake up.
8506 ++ * - Change the Posted-interrupt descriptor as below:
8507 ++ * 'NDST' <-- vcpu->pre_pcpu
8508 ++ * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
8509 ++ * - If 'ON' is set during this process, which means at least one
8510 ++ * interrupt is posted for this vCPU, we cannot block it, in
8511 ++ * this case, return 1, otherwise, return 0.
8512 ++ *
8513 ++ */
8514 ++static int pi_pre_block(struct kvm_vcpu *vcpu)
8515 ++{
8516 ++ unsigned int dest;
8517 ++ struct pi_desc old, new;
8518 ++ struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
8519 ++
8520 ++ if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
8521 ++ !irq_remapping_cap(IRQ_POSTING_CAP) ||
8522 ++ !kvm_vcpu_apicv_active(vcpu))
8523 ++ return 0;
8524 ++
8525 ++ WARN_ON(irqs_disabled());
8526 ++ local_irq_disable();
8527 ++ if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
8528 ++ vcpu->pre_pcpu = vcpu->cpu;
8529 ++ spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8530 ++ list_add_tail(&vcpu->blocked_vcpu_list,
8531 ++ &per_cpu(blocked_vcpu_on_cpu,
8532 ++ vcpu->pre_pcpu));
8533 ++ spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8534 ++ }
8535 ++
8536 ++ do {
8537 ++ old.control = new.control = pi_desc->control;
8538 ++
8539 ++ WARN((pi_desc->sn == 1),
8540 ++ "Warning: SN field of posted-interrupts "
8541 ++ "is set before blocking\n");
8542 ++
8543 ++ /*
8544 ++ * Since vCPU can be preempted during this process,
8545 ++ * vcpu->cpu could be different with pre_pcpu, we
8546 ++ * need to set pre_pcpu as the destination of wakeup
8547 ++ * notification event, then we can find the right vCPU
8548 ++ * to wakeup in wakeup handler if interrupts happen
8549 ++ * when the vCPU is in blocked state.
8550 ++ */
8551 ++ dest = cpu_physical_id(vcpu->pre_pcpu);
8552 ++
8553 ++ if (x2apic_enabled())
8554 ++ new.ndst = dest;
8555 ++ else
8556 ++ new.ndst = (dest << 8) & 0xFF00;
8557 ++
8558 ++ /* set 'NV' to 'wakeup vector' */
8559 ++ new.nv = POSTED_INTR_WAKEUP_VECTOR;
8560 ++ } while (cmpxchg64(&pi_desc->control, old.control,
8561 ++ new.control) != old.control);
8562 ++
8563 ++ /* We should not block the vCPU if an interrupt is posted for it. */
8564 ++ if (pi_test_on(pi_desc) == 1)
8565 ++ __pi_post_block(vcpu);
8566 ++
8567 ++ local_irq_enable();
8568 ++ return (vcpu->pre_pcpu == -1);
8569 ++}
8570 ++
8571 ++static int vmx_pre_block(struct kvm_vcpu *vcpu)
8572 ++{
8573 ++ if (pi_pre_block(vcpu))
8574 ++ return 1;
8575 ++
8576 ++ if (kvm_lapic_hv_timer_in_use(vcpu))
8577 ++ kvm_lapic_switch_to_sw_timer(vcpu);
8578 ++
8579 ++ return 0;
8580 ++}
8581 ++
8582 ++static void pi_post_block(struct kvm_vcpu *vcpu)
8583 ++{
8584 ++ if (vcpu->pre_pcpu == -1)
8585 ++ return;
8586 ++
8587 ++ WARN_ON(irqs_disabled());
8588 ++ local_irq_disable();
8589 ++ __pi_post_block(vcpu);
8590 ++ local_irq_enable();
8591 ++}
8592 ++
8593 ++static void vmx_post_block(struct kvm_vcpu *vcpu)
8594 ++{
8595 ++ if (kvm_x86_ops->set_hv_timer)
8596 ++ kvm_lapic_switch_to_hv_timer(vcpu);
8597 ++
8598 ++ pi_post_block(vcpu);
8599 ++}
8600 ++
8601 ++/*
8602 ++ * vmx_update_pi_irte - set IRTE for Posted-Interrupts
8603 ++ *
8604 ++ * @kvm: kvm
8605 ++ * @host_irq: host irq of the interrupt
8606 ++ * @guest_irq: gsi of the interrupt
8607 ++ * @set: set or unset PI
8608 ++ * returns 0 on success, < 0 on failure
8609 ++ */
8610 ++static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
8611 ++ uint32_t guest_irq, bool set)
8612 ++{
8613 ++ struct kvm_kernel_irq_routing_entry *e;
8614 ++ struct kvm_irq_routing_table *irq_rt;
8615 ++ struct kvm_lapic_irq irq;
8616 ++ struct kvm_vcpu *vcpu;
8617 ++ struct vcpu_data vcpu_info;
8618 ++ int idx, ret = 0;
8619 ++
8620 ++ if (!kvm_arch_has_assigned_device(kvm) ||
8621 ++ !irq_remapping_cap(IRQ_POSTING_CAP) ||
8622 ++ !kvm_vcpu_apicv_active(kvm->vcpus[0]))
8623 ++ return 0;
8624 ++
8625 ++ idx = srcu_read_lock(&kvm->irq_srcu);
8626 ++ irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
8627 ++ if (guest_irq >= irq_rt->nr_rt_entries ||
8628 ++ hlist_empty(&irq_rt->map[guest_irq])) {
8629 ++ pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
8630 ++ guest_irq, irq_rt->nr_rt_entries);
8631 ++ goto out;
8632 ++ }
8633 ++
8634 ++ hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
8635 ++ if (e->type != KVM_IRQ_ROUTING_MSI)
8636 ++ continue;
8637 ++ /*
8638 ++ * VT-d PI cannot support posting multicast/broadcast
8639 ++ * interrupts to a vCPU, we still use interrupt remapping
8640 ++ * for these kind of interrupts.
8641 ++ *
8642 ++ * For lowest-priority interrupts, we only support
8643 ++ * those with single CPU as the destination, e.g. user
8644 ++ * configures the interrupts via /proc/irq or uses
8645 ++ * irqbalance to make the interrupts single-CPU.
8646 ++ *
8647 ++ * We will support full lowest-priority interrupt later.
8648 ++ *
8649 ++ * In addition, we can only inject generic interrupts using
8650 ++ * the PI mechanism, refuse to route others through it.
8651 ++ */
8652 ++
8653 ++ kvm_set_msi_irq(kvm, e, &irq);
8654 ++ if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
8655 ++ !kvm_irq_is_postable(&irq)) {
8656 ++ /*
8657 ++ * Make sure the IRTE is in remapped mode if
8658 ++ * we don't handle it in posted mode.
8659 ++ */
8660 ++ ret = irq_set_vcpu_affinity(host_irq, NULL);
8661 ++ if (ret < 0) {
8662 ++ printk(KERN_INFO
8663 ++ "failed to back to remapped mode, irq: %u\n",
8664 ++ host_irq);
8665 ++ goto out;
8666 ++ }
8667 ++
8668 ++ continue;
8669 ++ }
8670 ++
8671 ++ vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
8672 ++ vcpu_info.vector = irq.vector;
8673 ++
8674 ++ trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
8675 ++ vcpu_info.vector, vcpu_info.pi_desc_addr, set);
8676 ++
8677 ++ if (set)
8678 ++ ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
8679 ++ else
8680 ++ ret = irq_set_vcpu_affinity(host_irq, NULL);
8681 ++
8682 ++ if (ret < 0) {
8683 ++ printk(KERN_INFO "%s: failed to update PI IRTE\n",
8684 ++ __func__);
8685 ++ goto out;
8686 ++ }
8687 ++ }
8688 ++
8689 ++ ret = 0;
8690 ++out:
8691 ++ srcu_read_unlock(&kvm->irq_srcu, idx);
8692 ++ return ret;
8693 ++}
8694 ++
8695 ++static void vmx_setup_mce(struct kvm_vcpu *vcpu)
8696 ++{
8697 ++ if (vcpu->arch.mcg_cap & MCG_LMCE_P)
8698 ++ to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
8699 ++ FEATURE_CONTROL_LMCE;
8700 ++ else
8701 ++ to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
8702 ++ ~FEATURE_CONTROL_LMCE;
8703 ++}
8704 ++
8705 ++static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
8706 ++{
8707 ++ /* we need a nested vmexit to enter SMM, postpone if run is pending */
8708 ++ if (to_vmx(vcpu)->nested.nested_run_pending)
8709 ++ return 0;
8710 ++ return 1;
8711 ++}
8712 ++
8713 ++static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
8714 ++{
8715 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
8716 ++
8717 ++ vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
8718 ++ if (vmx->nested.smm.guest_mode)
8719 ++ nested_vmx_vmexit(vcpu, -1, 0, 0);
8720 ++
8721 ++ vmx->nested.smm.vmxon = vmx->nested.vmxon;
8722 ++ vmx->nested.vmxon = false;
8723 ++ vmx_clear_hlt(vcpu);
8724 ++ return 0;
8725 ++}
8726 ++
8727 ++static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
8728 ++{
8729 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
8730 ++ int ret;
8731 ++
8732 ++ if (vmx->nested.smm.vmxon) {
8733 ++ vmx->nested.vmxon = true;
8734 ++ vmx->nested.smm.vmxon = false;
8735 ++ }
8736 ++
8737 ++ if (vmx->nested.smm.guest_mode) {
8738 ++ ret = nested_vmx_enter_non_root_mode(vcpu, false);
8739 ++ if (ret)
8740 ++ return ret;
8741 ++
8742 ++ vmx->nested.smm.guest_mode = false;
8743 ++ }
8744 ++ return 0;
8745 ++}
8746 ++
8747 ++static int enable_smi_window(struct kvm_vcpu *vcpu)
8748 ++{
8749 ++ return 0;
8750 ++}
8751 ++
8752 ++static bool vmx_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
8753 ++{
8754 ++ return false;
8755 ++}
8756 ++
8757 ++static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
8758 ++{
8759 ++ return to_vmx(vcpu)->nested.vmxon;
8760 ++}
8761 ++
8762 ++static __init int hardware_setup(void)
8763 ++{
8764 ++ unsigned long host_bndcfgs;
8765 ++ struct desc_ptr dt;
8766 ++ int r, i;
8767 ++
8768 ++ rdmsrl_safe(MSR_EFER, &host_efer);
8769 ++
8770 ++ store_idt(&dt);
8771 ++ host_idt_base = dt.address;
8772 ++
8773 ++ for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
8774 ++ kvm_define_shared_msr(i, vmx_msr_index[i]);
8775 ++
8776 ++ if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
8777 ++ return -EIO;
8778 ++
8779 ++ if (boot_cpu_has(X86_FEATURE_NX))
8780 ++ kvm_enable_efer_bits(EFER_NX);
8781 ++
8782 ++ if (boot_cpu_has(X86_FEATURE_MPX)) {
8783 ++ rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
8784 ++ WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
8785 ++ }
8786 ++
8787 ++ if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
8788 ++ !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
8789 ++ enable_vpid = 0;
8790 ++
8791 ++ if (!cpu_has_vmx_ept() ||
8792 ++ !cpu_has_vmx_ept_4levels() ||
8793 ++ !cpu_has_vmx_ept_mt_wb() ||
8794 ++ !cpu_has_vmx_invept_global())
8795 ++ enable_ept = 0;
8796 ++
8797 ++ if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
8798 ++ enable_ept_ad_bits = 0;
8799 ++
8800 ++ if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
8801 ++ enable_unrestricted_guest = 0;
8802 ++
8803 ++ if (!cpu_has_vmx_flexpriority())
8804 ++ flexpriority_enabled = 0;
8805 ++
8806 ++ if (!cpu_has_virtual_nmis())
8807 ++ enable_vnmi = 0;
8808 ++
8809 ++ /*
8810 ++ * set_apic_access_page_addr() is used to reload apic access
8811 ++ * page upon invalidation. No need to do anything if not
8812 ++ * using the APIC_ACCESS_ADDR VMCS field.
8813 ++ */
8814 ++ if (!flexpriority_enabled)
8815 ++ kvm_x86_ops->set_apic_access_page_addr = NULL;
8816 ++
8817 ++ if (!cpu_has_vmx_tpr_shadow())
8818 ++ kvm_x86_ops->update_cr8_intercept = NULL;
8819 ++
8820 ++ if (enable_ept && !cpu_has_vmx_ept_2m_page())
8821 ++ kvm_disable_largepages();
8822 ++
8823 ++#if IS_ENABLED(CONFIG_HYPERV)
8824 ++ if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
8825 ++ && enable_ept) {
8826 ++ kvm_x86_ops->tlb_remote_flush = hv_remote_flush_tlb;
8827 ++ kvm_x86_ops->tlb_remote_flush_with_range =
8828 ++ hv_remote_flush_tlb_with_range;
8829 ++ }
8830 ++#endif
8831 ++
8832 ++ if (!cpu_has_vmx_ple()) {
8833 ++ ple_gap = 0;
8834 ++ ple_window = 0;
8835 ++ ple_window_grow = 0;
8836 ++ ple_window_max = 0;
8837 ++ ple_window_shrink = 0;
8838 ++ }
8839 ++
8840 ++ if (!cpu_has_vmx_apicv()) {
8841 ++ enable_apicv = 0;
8842 ++ kvm_x86_ops->sync_pir_to_irr = NULL;
8843 ++ }
8844 ++
8845 ++ if (cpu_has_vmx_tsc_scaling()) {
8846 ++ kvm_has_tsc_control = true;
8847 ++ kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
8848 ++ kvm_tsc_scaling_ratio_frac_bits = 48;
8849 ++ }
8850 ++
8851 ++ set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
8852 ++
8853 ++ if (enable_ept)
8854 ++ vmx_enable_tdp();
8855 ++ else
8856 ++ kvm_disable_tdp();
8857 ++
8858 ++ /*
8859 ++ * Only enable PML when hardware supports PML feature, and both EPT
8860 ++ * and EPT A/D bit features are enabled -- PML depends on them to work.
8861 ++ */
8862 ++ if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
8863 ++ enable_pml = 0;
8864 ++
8865 ++ if (!enable_pml) {
8866 ++ kvm_x86_ops->slot_enable_log_dirty = NULL;
8867 ++ kvm_x86_ops->slot_disable_log_dirty = NULL;
8868 ++ kvm_x86_ops->flush_log_dirty = NULL;
8869 ++ kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
8870 ++ }
8871 ++
8872 ++ if (!cpu_has_vmx_preemption_timer())
8873 ++ enable_preemption_timer = false;
8874 ++
8875 ++ if (enable_preemption_timer) {
8876 ++ u64 use_timer_freq = 5000ULL * 1000 * 1000;
8877 ++ u64 vmx_msr;
8878 ++
8879 ++ rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8880 ++ cpu_preemption_timer_multi =
8881 ++ vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8882 ++
8883 ++ if (tsc_khz)
8884 ++ use_timer_freq = (u64)tsc_khz * 1000;
8885 ++ use_timer_freq >>= cpu_preemption_timer_multi;
8886 ++
8887 ++ /*
8888 ++ * KVM "disables" the preemption timer by setting it to its max
8889 ++ * value. Don't use the timer if it might cause spurious exits
8890 ++ * at a rate faster than 0.1 Hz (of uninterrupted guest time).
8891 ++ */
8892 ++ if (use_timer_freq > 0xffffffffu / 10)
8893 ++ enable_preemption_timer = false;
8894 ++ }
8895 ++
8896 ++ if (!enable_preemption_timer) {
8897 ++ kvm_x86_ops->set_hv_timer = NULL;
8898 ++ kvm_x86_ops->cancel_hv_timer = NULL;
8899 ++ kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
8900 ++ }
8901 ++
8902 ++ kvm_set_posted_intr_wakeup_handler(wakeup_handler);
8903 ++
8904 ++ kvm_mce_cap_supported |= MCG_LMCE_P;
8905 ++
8906 ++ if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
8907 ++ return -EINVAL;
8908 ++ if (!enable_ept || !cpu_has_vmx_intel_pt())
8909 ++ pt_mode = PT_MODE_SYSTEM;
8910 ++
8911 ++ if (nested) {
8912 ++ nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
8913 ++ vmx_capability.ept, enable_apicv);
8914 ++
8915 ++ r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
8916 ++ if (r)
8917 ++ return r;
8918 ++ }
8919 ++
8920 ++ r = alloc_kvm_area();
8921 ++ if (r)
8922 ++ nested_vmx_hardware_unsetup();
8923 ++ return r;
8924 ++}
8925 ++
8926 ++static __exit void hardware_unsetup(void)
8927 ++{
8928 ++ if (nested)
8929 ++ nested_vmx_hardware_unsetup();
8930 ++
8931 ++ free_kvm_area();
8932 ++}
8933 ++
8934 ++static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
8935 ++ .cpu_has_kvm_support = cpu_has_kvm_support,
8936 ++ .disabled_by_bios = vmx_disabled_by_bios,
8937 ++ .hardware_setup = hardware_setup,
8938 ++ .hardware_unsetup = hardware_unsetup,
8939 ++ .check_processor_compatibility = vmx_check_processor_compat,
8940 ++ .hardware_enable = hardware_enable,
8941 ++ .hardware_disable = hardware_disable,
8942 ++ .cpu_has_accelerated_tpr = report_flexpriority,
8943 ++ .has_emulated_msr = vmx_has_emulated_msr,
8944 ++
8945 ++ .vm_init = vmx_vm_init,
8946 ++ .vm_alloc = vmx_vm_alloc,
8947 ++ .vm_free = vmx_vm_free,
8948 ++
8949 ++ .vcpu_create = vmx_create_vcpu,
8950 ++ .vcpu_free = vmx_free_vcpu,
8951 ++ .vcpu_reset = vmx_vcpu_reset,
8952 ++
8953 ++ .prepare_guest_switch = vmx_prepare_switch_to_guest,
8954 ++ .vcpu_load = vmx_vcpu_load,
8955 ++ .vcpu_put = vmx_vcpu_put,
8956 ++
8957 ++ .update_bp_intercept = update_exception_bitmap,
8958 ++ .get_msr_feature = vmx_get_msr_feature,
8959 ++ .get_msr = vmx_get_msr,
8960 ++ .set_msr = vmx_set_msr,
8961 ++ .get_segment_base = vmx_get_segment_base,
8962 ++ .get_segment = vmx_get_segment,
8963 ++ .set_segment = vmx_set_segment,
8964 ++ .get_cpl = vmx_get_cpl,
8965 ++ .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
8966 ++ .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
8967 ++ .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
8968 ++ .set_cr0 = vmx_set_cr0,
8969 ++ .set_cr3 = vmx_set_cr3,
8970 ++ .set_cr4 = vmx_set_cr4,
8971 ++ .set_efer = vmx_set_efer,
8972 ++ .get_idt = vmx_get_idt,
8973 ++ .set_idt = vmx_set_idt,
8974 ++ .get_gdt = vmx_get_gdt,
8975 ++ .set_gdt = vmx_set_gdt,
8976 ++ .get_dr6 = vmx_get_dr6,
8977 ++ .set_dr6 = vmx_set_dr6,
8978 ++ .set_dr7 = vmx_set_dr7,
8979 ++ .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
8980 ++ .cache_reg = vmx_cache_reg,
8981 ++ .get_rflags = vmx_get_rflags,
8982 ++ .set_rflags = vmx_set_rflags,
8983 ++
8984 ++ .tlb_flush = vmx_flush_tlb,
8985 ++ .tlb_flush_gva = vmx_flush_tlb_gva,
8986 ++
8987 ++ .run = vmx_vcpu_run,
8988 ++ .handle_exit = vmx_handle_exit,
8989 ++ .skip_emulated_instruction = skip_emulated_instruction,
8990 ++ .set_interrupt_shadow = vmx_set_interrupt_shadow,
8991 ++ .get_interrupt_shadow = vmx_get_interrupt_shadow,
8992 ++ .patch_hypercall = vmx_patch_hypercall,
8993 ++ .set_irq = vmx_inject_irq,
8994 ++ .set_nmi = vmx_inject_nmi,
8995 ++ .queue_exception = vmx_queue_exception,
8996 ++ .cancel_injection = vmx_cancel_injection,
8997 ++ .interrupt_allowed = vmx_interrupt_allowed,
8998 ++ .nmi_allowed = vmx_nmi_allowed,
8999 ++ .get_nmi_mask = vmx_get_nmi_mask,
9000 ++ .set_nmi_mask = vmx_set_nmi_mask,
9001 ++ .enable_nmi_window = enable_nmi_window,
9002 ++ .enable_irq_window = enable_irq_window,
9003 ++ .update_cr8_intercept = update_cr8_intercept,
9004 ++ .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
9005 ++ .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
9006 ++ .get_enable_apicv = vmx_get_enable_apicv,
9007 ++ .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
9008 ++ .load_eoi_exitmap = vmx_load_eoi_exitmap,
9009 ++ .apicv_post_state_restore = vmx_apicv_post_state_restore,
9010 ++ .hwapic_irr_update = vmx_hwapic_irr_update,
9011 ++ .hwapic_isr_update = vmx_hwapic_isr_update,
9012 ++ .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
9013 ++ .sync_pir_to_irr = vmx_sync_pir_to_irr,
9014 ++ .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
9015 ++ .dy_apicv_has_pending_interrupt = vmx_dy_apicv_has_pending_interrupt,
9016 ++
9017 ++ .set_tss_addr = vmx_set_tss_addr,
9018 ++ .set_identity_map_addr = vmx_set_identity_map_addr,
9019 ++ .get_tdp_level = get_ept_level,
9020 ++ .get_mt_mask = vmx_get_mt_mask,
9021 ++
9022 ++ .get_exit_info = vmx_get_exit_info,
9023 ++
9024 ++ .get_lpage_level = vmx_get_lpage_level,
9025 ++
9026 ++ .cpuid_update = vmx_cpuid_update,
9027 ++
9028 ++ .rdtscp_supported = vmx_rdtscp_supported,
9029 ++ .invpcid_supported = vmx_invpcid_supported,
9030 ++
9031 ++ .set_supported_cpuid = vmx_set_supported_cpuid,
9032 ++
9033 ++ .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
9034 ++
9035 ++ .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
9036 ++ .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
9037 ++
9038 ++ .set_tdp_cr3 = vmx_set_cr3,
9039 ++
9040 ++ .check_intercept = vmx_check_intercept,
9041 ++ .handle_exit_irqoff = vmx_handle_exit_irqoff,
9042 ++ .mpx_supported = vmx_mpx_supported,
9043 ++ .xsaves_supported = vmx_xsaves_supported,
9044 ++ .umip_emulated = vmx_umip_emulated,
9045 ++ .pt_supported = vmx_pt_supported,
9046 ++
9047 ++ .request_immediate_exit = vmx_request_immediate_exit,
9048 ++
9049 ++ .sched_in = vmx_sched_in,
9050 ++
9051 ++ .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
9052 ++ .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
9053 ++ .flush_log_dirty = vmx_flush_log_dirty,
9054 ++ .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
9055 ++ .write_log_dirty = vmx_write_pml_buffer,
9056 ++
9057 ++ .pre_block = vmx_pre_block,
9058 ++ .post_block = vmx_post_block,
9059 ++
9060 ++ .pmu_ops = &intel_pmu_ops,
9061 ++
9062 ++ .update_pi_irte = vmx_update_pi_irte,
9063 ++
9064 ++#ifdef CONFIG_X86_64
9065 ++ .set_hv_timer = vmx_set_hv_timer,
9066 ++ .cancel_hv_timer = vmx_cancel_hv_timer,
9067 ++#endif
9068 ++
9069 ++ .setup_mce = vmx_setup_mce,
9070 ++
9071 ++ .smi_allowed = vmx_smi_allowed,
9072 ++ .pre_enter_smm = vmx_pre_enter_smm,
9073 ++ .pre_leave_smm = vmx_pre_leave_smm,
9074 ++ .enable_smi_window = enable_smi_window,
9075 ++
9076 ++ .check_nested_events = NULL,
9077 ++ .get_nested_state = NULL,
9078 ++ .set_nested_state = NULL,
9079 ++ .get_vmcs12_pages = NULL,
9080 ++ .nested_enable_evmcs = NULL,
9081 ++ .nested_get_evmcs_version = NULL,
9082 ++ .need_emulation_on_page_fault = vmx_need_emulation_on_page_fault,
9083 ++ .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
9084 ++};
9085 ++
9086 ++static void vmx_cleanup_l1d_flush(void)
9087 ++{
9088 ++ if (vmx_l1d_flush_pages) {
9089 ++ free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
9090 ++ vmx_l1d_flush_pages = NULL;
9091 ++ }
9092 ++ /* Restore state so sysfs ignores VMX */
9093 ++ l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
9094 ++}
9095 ++
9096 ++static void vmx_exit(void)
9097 ++{
9098 ++#ifdef CONFIG_KEXEC_CORE
9099 ++ RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
9100 ++ synchronize_rcu();
9101 ++#endif
9102 ++
9103 ++ kvm_exit();
9104 ++
9105 ++#if IS_ENABLED(CONFIG_HYPERV)
9106 ++ if (static_branch_unlikely(&enable_evmcs)) {
9107 ++ int cpu;
9108 ++ struct hv_vp_assist_page *vp_ap;
9109 ++ /*
9110 ++ * Reset everything to support using non-enlightened VMCS
9111 ++ * access later (e.g. when we reload the module with
9112 ++ * enlightened_vmcs=0)
9113 ++ */
9114 ++ for_each_online_cpu(cpu) {
9115 ++ vp_ap = hv_get_vp_assist_page(cpu);
9116 ++
9117 ++ if (!vp_ap)
9118 ++ continue;
9119 ++
9120 ++ vp_ap->nested_control.features.directhypercall = 0;
9121 ++ vp_ap->current_nested_vmcs = 0;
9122 ++ vp_ap->enlighten_vmentry = 0;
9123 ++ }
9124 ++
9125 ++ static_branch_disable(&enable_evmcs);
9126 ++ }
9127 ++#endif
9128 ++ vmx_cleanup_l1d_flush();
9129 ++}
9130 ++module_exit(vmx_exit);
9131 ++
9132 ++static int __init vmx_init(void)
9133 ++{
9134 ++ int r;
9135 ++
9136 ++#if IS_ENABLED(CONFIG_HYPERV)
9137 ++ /*
9138 ++ * Enlightened VMCS usage should be recommended and the host needs
9139 ++ * to support eVMCS v1 or above. We can also disable eVMCS support
9140 ++ * with module parameter.
9141 ++ */
9142 ++ if (enlightened_vmcs &&
9143 ++ ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
9144 ++ (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
9145 ++ KVM_EVMCS_VERSION) {
9146 ++ int cpu;
9147 ++
9148 ++ /* Check that we have assist pages on all online CPUs */
9149 ++ for_each_online_cpu(cpu) {
9150 ++ if (!hv_get_vp_assist_page(cpu)) {
9151 ++ enlightened_vmcs = false;
9152 ++ break;
9153 ++ }
9154 ++ }
9155 ++
9156 ++ if (enlightened_vmcs) {
9157 ++ pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
9158 ++ static_branch_enable(&enable_evmcs);
9159 ++ }
9160 ++
9161 ++ if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH)
9162 ++ vmx_x86_ops.enable_direct_tlbflush
9163 ++ = hv_enable_direct_tlbflush;
9164 ++
9165 ++ } else {
9166 ++ enlightened_vmcs = false;
9167 ++ }
9168 ++#endif
9169 ++
9170 ++ r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
9171 ++ __alignof__(struct vcpu_vmx), THIS_MODULE);
9172 ++ if (r)
9173 ++ return r;
9174 ++
9175 ++ /*
9176 ++ * Must be called after kvm_init() so enable_ept is properly set
9177 ++ * up. Hand the parameter mitigation value in which was stored in
9178 ++ * the pre module init parser. If no parameter was given, it will
9179 ++ * contain 'auto' which will be turned into the default 'cond'
9180 ++ * mitigation mode.
9181 ++ */
9182 ++ r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
9183 ++ if (r) {
9184 ++ vmx_exit();
9185 ++ return r;
9186 ++ }
9187 ++
9188 ++#ifdef CONFIG_KEXEC_CORE
9189 ++ rcu_assign_pointer(crash_vmclear_loaded_vmcss,
9190 ++ crash_vmclear_local_loaded_vmcss);
9191 ++#endif
9192 ++ vmx_check_vmcs12_offsets();
9193 ++
9194 ++ return 0;
9195 ++}
9196 ++module_init(vmx_init);
9197 +diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
9198 +index 8a51442247c5..d6851636edab 100644
9199 +--- a/arch/x86/kvm/x86.c
9200 ++++ b/arch/x86/kvm/x86.c
9201 +@@ -924,9 +924,11 @@ static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
9202 +
9203 + static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
9204 + {
9205 ++ size_t size = ARRAY_SIZE(vcpu->arch.db);
9206 ++
9207 + switch (dr) {
9208 + case 0 ... 3:
9209 +- vcpu->arch.db[dr] = val;
9210 ++ vcpu->arch.db[array_index_nospec(dr, size)] = val;
9211 + if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
9212 + vcpu->arch.eff_db[dr] = val;
9213 + break;
9214 +@@ -963,9 +965,11 @@ EXPORT_SYMBOL_GPL(kvm_set_dr);
9215 +
9216 + int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
9217 + {
9218 ++ size_t size = ARRAY_SIZE(vcpu->arch.db);
9219 ++
9220 + switch (dr) {
9221 + case 0 ... 3:
9222 +- *val = vcpu->arch.db[dr];
9223 ++ *val = vcpu->arch.db[array_index_nospec(dr, size)];
9224 + break;
9225 + case 4:
9226 + /* fall through */
9227 +@@ -2161,7 +2165,10 @@ static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
9228 + default:
9229 + if (msr >= MSR_IA32_MC0_CTL &&
9230 + msr < MSR_IA32_MCx_CTL(bank_num)) {
9231 +- u32 offset = msr - MSR_IA32_MC0_CTL;
9232 ++ u32 offset = array_index_nospec(
9233 ++ msr - MSR_IA32_MC0_CTL,
9234 ++ MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
9235 ++
9236 + /* only 0 or all 1s can be written to IA32_MCi_CTL
9237 + * some Linux kernels though clear bit 10 in bank 4 to
9238 + * workaround a BIOS/GART TBL issue on AMD K8s, ignore
9239 +@@ -2545,7 +2552,10 @@ static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
9240 + default:
9241 + if (msr >= MSR_IA32_MC0_CTL &&
9242 + msr < MSR_IA32_MCx_CTL(bank_num)) {
9243 +- u32 offset = msr - MSR_IA32_MC0_CTL;
9244 ++ u32 offset = array_index_nospec(
9245 ++ msr - MSR_IA32_MC0_CTL,
9246 ++ MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
9247 ++
9248 + data = vcpu->arch.mce_banks[offset];
9249 + break;
9250 + }
9251 +@@ -6304,7 +6314,7 @@ static void kvm_set_mmio_spte_mask(void)
9252 + * If reserved bit is not supported, clear the present bit to disable
9253 + * mmio page fault.
9254 + */
9255 +- if (IS_ENABLED(CONFIG_X86_64) && maxphyaddr == 52)
9256 ++ if (maxphyaddr == 52)
9257 + mask &= ~1ull;
9258 +
9259 + kvm_mmu_set_mmio_spte_mask(mask, mask);
9260 +@@ -7667,6 +7677,9 @@ int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
9261 + int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
9262 + struct kvm_mp_state *mp_state)
9263 + {
9264 ++ if (kvm_mpx_supported())
9265 ++ kvm_load_guest_fpu(vcpu);
9266 ++
9267 + kvm_apic_accept_events(vcpu);
9268 + if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
9269 + vcpu->arch.pv.pv_unhalted)
9270 +@@ -7674,6 +7687,8 @@ int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
9271 + else
9272 + mp_state->mp_state = vcpu->arch.mp_state;
9273 +
9274 ++ if (kvm_mpx_supported())
9275 ++ kvm_put_guest_fpu(vcpu);
9276 + return 0;
9277 + }
9278 +
9279 +@@ -8053,7 +8068,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
9280 + kvm_mmu_unload(vcpu);
9281 + vcpu_put(vcpu);
9282 +
9283 +- kvm_x86_ops->vcpu_free(vcpu);
9284 ++ kvm_arch_vcpu_free(vcpu);
9285 + }
9286 +
9287 + void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
9288 +diff --git a/crypto/algapi.c b/crypto/algapi.c
9289 +index 50eb828db767..603d2d637209 100644
9290 +--- a/crypto/algapi.c
9291 ++++ b/crypto/algapi.c
9292 +@@ -652,11 +652,9 @@ EXPORT_SYMBOL_GPL(crypto_grab_spawn);
9293 +
9294 + void crypto_drop_spawn(struct crypto_spawn *spawn)
9295 + {
9296 +- if (!spawn->alg)
9297 +- return;
9298 +-
9299 + down_write(&crypto_alg_sem);
9300 +- list_del(&spawn->list);
9301 ++ if (spawn->alg)
9302 ++ list_del(&spawn->list);
9303 + up_write(&crypto_alg_sem);
9304 + }
9305 + EXPORT_SYMBOL_GPL(crypto_drop_spawn);
9306 +@@ -664,22 +662,16 @@ EXPORT_SYMBOL_GPL(crypto_drop_spawn);
9307 + static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
9308 + {
9309 + struct crypto_alg *alg;
9310 +- struct crypto_alg *alg2;
9311 +
9312 + down_read(&crypto_alg_sem);
9313 + alg = spawn->alg;
9314 +- alg2 = alg;
9315 +- if (alg2)
9316 +- alg2 = crypto_mod_get(alg2);
9317 +- up_read(&crypto_alg_sem);
9318 +-
9319 +- if (!alg2) {
9320 +- if (alg)
9321 +- crypto_shoot_alg(alg);
9322 +- return ERR_PTR(-EAGAIN);
9323 ++ if (alg && !crypto_mod_get(alg)) {
9324 ++ alg->cra_flags |= CRYPTO_ALG_DYING;
9325 ++ alg = NULL;
9326 + }
9327 ++ up_read(&crypto_alg_sem);
9328 +
9329 +- return alg;
9330 ++ return alg ?: ERR_PTR(-EAGAIN);
9331 + }
9332 +
9333 + struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
9334 +diff --git a/crypto/api.c b/crypto/api.c
9335 +index e485aed11ad0..187795a6687d 100644
9336 +--- a/crypto/api.c
9337 ++++ b/crypto/api.c
9338 +@@ -339,13 +339,12 @@ static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
9339 + return len;
9340 + }
9341 +
9342 +-void crypto_shoot_alg(struct crypto_alg *alg)
9343 ++static void crypto_shoot_alg(struct crypto_alg *alg)
9344 + {
9345 + down_write(&crypto_alg_sem);
9346 + alg->cra_flags |= CRYPTO_ALG_DYING;
9347 + up_write(&crypto_alg_sem);
9348 + }
9349 +-EXPORT_SYMBOL_GPL(crypto_shoot_alg);
9350 +
9351 + struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
9352 + u32 mask)
9353 +diff --git a/crypto/internal.h b/crypto/internal.h
9354 +index f07320423191..6262ec0435b4 100644
9355 +--- a/crypto/internal.h
9356 ++++ b/crypto/internal.h
9357 +@@ -84,7 +84,6 @@ void crypto_alg_tested(const char *name, int err);
9358 + void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
9359 + struct crypto_alg *nalg);
9360 + void crypto_remove_final(struct list_head *list);
9361 +-void crypto_shoot_alg(struct crypto_alg *alg);
9362 + struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
9363 + u32 mask);
9364 + void *crypto_create_tfm(struct crypto_alg *alg,
9365 +diff --git a/crypto/pcrypt.c b/crypto/pcrypt.c
9366 +index 1348541da463..85082574c515 100644
9367 +--- a/crypto/pcrypt.c
9368 ++++ b/crypto/pcrypt.c
9369 +@@ -130,7 +130,6 @@ static void pcrypt_aead_done(struct crypto_async_request *areq, int err)
9370 + struct padata_priv *padata = pcrypt_request_padata(preq);
9371 +
9372 + padata->info = err;
9373 +- req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
9374 +
9375 + padata_do_serial(padata);
9376 + }
9377 +diff --git a/drivers/acpi/video_detect.c b/drivers/acpi/video_detect.c
9378 +index 43587ac680e4..214c4e2e8ade 100644
9379 +--- a/drivers/acpi/video_detect.c
9380 ++++ b/drivers/acpi/video_detect.c
9381 +@@ -328,6 +328,11 @@ static const struct dmi_system_id video_detect_dmi_table[] = {
9382 + DMI_MATCH(DMI_PRODUCT_NAME, "Precision 7510"),
9383 + },
9384 + },
9385 ++
9386 ++ /*
9387 ++ * Desktops which falsely report a backlight and which our heuristics
9388 ++ * for this do not catch.
9389 ++ */
9390 + {
9391 + .callback = video_detect_force_none,
9392 + .ident = "Dell OptiPlex 9020M",
9393 +@@ -336,6 +341,14 @@ static const struct dmi_system_id video_detect_dmi_table[] = {
9394 + DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 9020M"),
9395 + },
9396 + },
9397 ++ {
9398 ++ .callback = video_detect_force_none,
9399 ++ .ident = "MSI MS-7721",
9400 ++ .matches = {
9401 ++ DMI_MATCH(DMI_SYS_VENDOR, "MSI"),
9402 ++ DMI_MATCH(DMI_PRODUCT_NAME, "MS-7721"),
9403 ++ },
9404 ++ },
9405 + { },
9406 + };
9407 +
9408 +diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
9409 +index a30ff97632a5..0e7fa1f27ad4 100644
9410 +--- a/drivers/base/power/main.c
9411 ++++ b/drivers/base/power/main.c
9412 +@@ -269,10 +269,38 @@ static void dpm_wait_for_suppliers(struct device *dev, bool async)
9413 + device_links_read_unlock(idx);
9414 + }
9415 +
9416 +-static void dpm_wait_for_superior(struct device *dev, bool async)
9417 ++static bool dpm_wait_for_superior(struct device *dev, bool async)
9418 + {
9419 +- dpm_wait(dev->parent, async);
9420 ++ struct device *parent;
9421 ++
9422 ++ /*
9423 ++ * If the device is resumed asynchronously and the parent's callback
9424 ++ * deletes both the device and the parent itself, the parent object may
9425 ++ * be freed while this function is running, so avoid that by reference
9426 ++ * counting the parent once more unless the device has been deleted
9427 ++ * already (in which case return right away).
9428 ++ */
9429 ++ mutex_lock(&dpm_list_mtx);
9430 ++
9431 ++ if (!device_pm_initialized(dev)) {
9432 ++ mutex_unlock(&dpm_list_mtx);
9433 ++ return false;
9434 ++ }
9435 ++
9436 ++ parent = get_device(dev->parent);
9437 ++
9438 ++ mutex_unlock(&dpm_list_mtx);
9439 ++
9440 ++ dpm_wait(parent, async);
9441 ++ put_device(parent);
9442 ++
9443 + dpm_wait_for_suppliers(dev, async);
9444 ++
9445 ++ /*
9446 ++ * If the parent's callback has deleted the device, attempting to resume
9447 ++ * it would be invalid, so avoid doing that then.
9448 ++ */
9449 ++ return device_pm_initialized(dev);
9450 + }
9451 +
9452 + static void dpm_wait_for_consumers(struct device *dev, bool async)
9453 +@@ -551,7 +579,8 @@ static int device_resume_noirq(struct device *dev, pm_message_t state, bool asyn
9454 + if (!dev->power.is_noirq_suspended)
9455 + goto Out;
9456 +
9457 +- dpm_wait_for_superior(dev, async);
9458 ++ if (!dpm_wait_for_superior(dev, async))
9459 ++ goto Out;
9460 +
9461 + if (dev->pm_domain) {
9462 + info = "noirq power domain ";
9463 +@@ -691,7 +720,8 @@ static int device_resume_early(struct device *dev, pm_message_t state, bool asyn
9464 + if (!dev->power.is_late_suspended)
9465 + goto Out;
9466 +
9467 +- dpm_wait_for_superior(dev, async);
9468 ++ if (!dpm_wait_for_superior(dev, async))
9469 ++ goto Out;
9470 +
9471 + if (dev->pm_domain) {
9472 + info = "early power domain ";
9473 +@@ -823,7 +853,9 @@ static int device_resume(struct device *dev, pm_message_t state, bool async)
9474 + goto Complete;
9475 + }
9476 +
9477 +- dpm_wait_for_superior(dev, async);
9478 ++ if (!dpm_wait_for_superior(dev, async))
9479 ++ goto Complete;
9480 ++
9481 + dpm_watchdog_set(&wd, dev);
9482 + device_lock(dev);
9483 +
9484 +diff --git a/drivers/clk/tegra/clk-tegra-periph.c b/drivers/clk/tegra/clk-tegra-periph.c
9485 +index 848255cc0209..d300a256fcac 100644
9486 +--- a/drivers/clk/tegra/clk-tegra-periph.c
9487 ++++ b/drivers/clk/tegra/clk-tegra-periph.c
9488 +@@ -825,7 +825,11 @@ static struct tegra_periph_init_data gate_clks[] = {
9489 + GATE("vcp", "clk_m", 29, 0, tegra_clk_vcp, 0),
9490 + GATE("apbdma", "clk_m", 34, 0, tegra_clk_apbdma, 0),
9491 + GATE("kbc", "clk_32k", 36, TEGRA_PERIPH_ON_APB | TEGRA_PERIPH_NO_RESET, tegra_clk_kbc, 0),
9492 +- GATE("fuse", "clk_m", 39, TEGRA_PERIPH_ON_APB, tegra_clk_fuse, 0),
9493 ++ /*
9494 ++ * Critical for RAM re-repair operation, which must occur on resume
9495 ++ * from LP1 system suspend and as part of CCPLEX cluster switching.
9496 ++ */
9497 ++ GATE("fuse", "clk_m", 39, TEGRA_PERIPH_ON_APB, tegra_clk_fuse, CLK_IS_CRITICAL),
9498 + GATE("fuse_burn", "clk_m", 39, TEGRA_PERIPH_ON_APB, tegra_clk_fuse_burn, 0),
9499 + GATE("kfuse", "clk_m", 40, TEGRA_PERIPH_ON_APB, tegra_clk_kfuse, 0),
9500 + GATE("apbif", "clk_m", 107, TEGRA_PERIPH_ON_APB, tegra_clk_apbif, 0),
9501 +diff --git a/drivers/crypto/atmel-aes.c b/drivers/crypto/atmel-aes.c
9502 +index 11129b796dda..b8153142bcc6 100644
9503 +--- a/drivers/crypto/atmel-aes.c
9504 ++++ b/drivers/crypto/atmel-aes.c
9505 +@@ -91,7 +91,6 @@
9506 + struct atmel_aes_caps {
9507 + bool has_dualbuff;
9508 + bool has_cfb64;
9509 +- bool has_ctr32;
9510 + bool has_gcm;
9511 + bool has_xts;
9512 + bool has_authenc;
9513 +@@ -990,8 +989,9 @@ static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
9514 + struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
9515 + struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
9516 + struct scatterlist *src, *dst;
9517 +- u32 ctr, blocks;
9518 + size_t datalen;
9519 ++ u32 ctr;
9520 ++ u16 blocks, start, end;
9521 + bool use_dma, fragmented = false;
9522 +
9523 + /* Check for transfer completion. */
9524 +@@ -1003,27 +1003,17 @@ static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
9525 + datalen = req->nbytes - ctx->offset;
9526 + blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
9527 + ctr = be32_to_cpu(ctx->iv[3]);
9528 +- if (dd->caps.has_ctr32) {
9529 +- /* Check 32bit counter overflow. */
9530 +- u32 start = ctr;
9531 +- u32 end = start + blocks - 1;
9532 +-
9533 +- if (end < start) {
9534 +- ctr |= 0xffffffff;
9535 +- datalen = AES_BLOCK_SIZE * -start;
9536 +- fragmented = true;
9537 +- }
9538 +- } else {
9539 +- /* Check 16bit counter overflow. */
9540 +- u16 start = ctr & 0xffff;
9541 +- u16 end = start + (u16)blocks - 1;
9542 +-
9543 +- if (blocks >> 16 || end < start) {
9544 +- ctr |= 0xffff;
9545 +- datalen = AES_BLOCK_SIZE * (0x10000-start);
9546 +- fragmented = true;
9547 +- }
9548 ++
9549 ++ /* Check 16bit counter overflow. */
9550 ++ start = ctr & 0xffff;
9551 ++ end = start + blocks - 1;
9552 ++
9553 ++ if (blocks >> 16 || end < start) {
9554 ++ ctr |= 0xffff;
9555 ++ datalen = AES_BLOCK_SIZE * (0x10000 - start);
9556 ++ fragmented = true;
9557 + }
9558 ++
9559 + use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD);
9560 +
9561 + /* Jump to offset. */
9562 +@@ -2536,7 +2526,6 @@ static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
9563 + {
9564 + dd->caps.has_dualbuff = 0;
9565 + dd->caps.has_cfb64 = 0;
9566 +- dd->caps.has_ctr32 = 0;
9567 + dd->caps.has_gcm = 0;
9568 + dd->caps.has_xts = 0;
9569 + dd->caps.has_authenc = 0;
9570 +@@ -2547,7 +2536,6 @@ static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
9571 + case 0x500:
9572 + dd->caps.has_dualbuff = 1;
9573 + dd->caps.has_cfb64 = 1;
9574 +- dd->caps.has_ctr32 = 1;
9575 + dd->caps.has_gcm = 1;
9576 + dd->caps.has_xts = 1;
9577 + dd->caps.has_authenc = 1;
9578 +@@ -2556,7 +2544,6 @@ static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
9579 + case 0x200:
9580 + dd->caps.has_dualbuff = 1;
9581 + dd->caps.has_cfb64 = 1;
9582 +- dd->caps.has_ctr32 = 1;
9583 + dd->caps.has_gcm = 1;
9584 + dd->caps.max_burst_size = 4;
9585 + break;
9586 +diff --git a/drivers/crypto/atmel-sha.c b/drivers/crypto/atmel-sha.c
9587 +index 3e2f41b3eaf3..15e68774034a 100644
9588 +--- a/drivers/crypto/atmel-sha.c
9589 ++++ b/drivers/crypto/atmel-sha.c
9590 +@@ -1921,12 +1921,7 @@ static int atmel_sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
9591 + {
9592 + struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
9593 +
9594 +- if (atmel_sha_hmac_key_set(&hmac->hkey, key, keylen)) {
9595 +- crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
9596 +- return -EINVAL;
9597 +- }
9598 +-
9599 +- return 0;
9600 ++ return atmel_sha_hmac_key_set(&hmac->hkey, key, keylen);
9601 + }
9602 +
9603 + static int atmel_sha_hmac_init(struct ahash_request *req)
9604 +diff --git a/drivers/crypto/axis/artpec6_crypto.c b/drivers/crypto/axis/artpec6_crypto.c
9605 +index 9f82e14983f6..a886245b931e 100644
9606 +--- a/drivers/crypto/axis/artpec6_crypto.c
9607 ++++ b/drivers/crypto/axis/artpec6_crypto.c
9608 +@@ -1256,7 +1256,7 @@ static int artpec6_crypto_aead_set_key(struct crypto_aead *tfm, const u8 *key,
9609 +
9610 + if (len != 16 && len != 24 && len != 32) {
9611 + crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
9612 +- return -1;
9613 ++ return -EINVAL;
9614 + }
9615 +
9616 + ctx->key_length = len;
9617 +diff --git a/drivers/crypto/ccp/ccp-dev-v3.c b/drivers/crypto/ccp/ccp-dev-v3.c
9618 +index 240bebbcb8ac..ae0cc0a4dc5c 100644
9619 +--- a/drivers/crypto/ccp/ccp-dev-v3.c
9620 ++++ b/drivers/crypto/ccp/ccp-dev-v3.c
9621 +@@ -590,6 +590,7 @@ const struct ccp_vdata ccpv3_platform = {
9622 + .setup = NULL,
9623 + .perform = &ccp3_actions,
9624 + .offset = 0,
9625 ++ .rsamax = CCP_RSA_MAX_WIDTH,
9626 + };
9627 +
9628 + const struct ccp_vdata ccpv3 = {
9629 +diff --git a/drivers/crypto/picoxcell_crypto.c b/drivers/crypto/picoxcell_crypto.c
9630 +index b6f14844702e..7eaeb8507e06 100644
9631 +--- a/drivers/crypto/picoxcell_crypto.c
9632 ++++ b/drivers/crypto/picoxcell_crypto.c
9633 +@@ -1616,6 +1616,11 @@ static const struct of_device_id spacc_of_id_table[] = {
9634 + MODULE_DEVICE_TABLE(of, spacc_of_id_table);
9635 + #endif /* CONFIG_OF */
9636 +
9637 ++static void spacc_tasklet_kill(void *data)
9638 ++{
9639 ++ tasklet_kill(data);
9640 ++}
9641 ++
9642 + static int spacc_probe(struct platform_device *pdev)
9643 + {
9644 + int i, err, ret = -EINVAL;
9645 +@@ -1659,6 +1664,14 @@ static int spacc_probe(struct platform_device *pdev)
9646 + return -ENXIO;
9647 + }
9648 +
9649 ++ tasklet_init(&engine->complete, spacc_spacc_complete,
9650 ++ (unsigned long)engine);
9651 ++
9652 ++ ret = devm_add_action(&pdev->dev, spacc_tasklet_kill,
9653 ++ &engine->complete);
9654 ++ if (ret)
9655 ++ return ret;
9656 ++
9657 + if (devm_request_irq(&pdev->dev, irq->start, spacc_spacc_irq, 0,
9658 + engine->name, engine)) {
9659 + dev_err(engine->dev, "failed to request IRQ\n");
9660 +@@ -1721,8 +1734,6 @@ static int spacc_probe(struct platform_device *pdev)
9661 + INIT_LIST_HEAD(&engine->completed);
9662 + INIT_LIST_HEAD(&engine->in_progress);
9663 + engine->in_flight = 0;
9664 +- tasklet_init(&engine->complete, spacc_spacc_complete,
9665 +- (unsigned long)engine);
9666 +
9667 + platform_set_drvdata(pdev, engine);
9668 +
9669 +diff --git a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
9670 +index d73281095fac..976109c20d49 100644
9671 +--- a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
9672 ++++ b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
9673 +@@ -79,7 +79,11 @@ static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c)
9674 + struct videomode vm;
9675 + unsigned long prate;
9676 + unsigned int cfg;
9677 +- int div;
9678 ++ int div, ret;
9679 ++
9680 ++ ret = clk_prepare_enable(crtc->dc->hlcdc->sys_clk);
9681 ++ if (ret)
9682 ++ return;
9683 +
9684 + vm.vfront_porch = adj->crtc_vsync_start - adj->crtc_vdisplay;
9685 + vm.vback_porch = adj->crtc_vtotal - adj->crtc_vsync_end;
9686 +@@ -138,6 +142,8 @@ static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c)
9687 + ATMEL_HLCDC_VSPSU | ATMEL_HLCDC_VSPHO |
9688 + ATMEL_HLCDC_GUARDTIME_MASK | ATMEL_HLCDC_MODE_MASK,
9689 + cfg);
9690 ++
9691 ++ clk_disable_unprepare(crtc->dc->hlcdc->sys_clk);
9692 + }
9693 +
9694 + static enum drm_mode_status
9695 +diff --git a/drivers/hv/hv_balloon.c b/drivers/hv/hv_balloon.c
9696 +index 0824405f93fb..2d93c8f454bc 100644
9697 +--- a/drivers/hv/hv_balloon.c
9698 ++++ b/drivers/hv/hv_balloon.c
9699 +@@ -1170,10 +1170,7 @@ static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
9700 + unsigned int i = 0;
9701 + struct page *pg;
9702 +
9703 +- if (num_pages < alloc_unit)
9704 +- return 0;
9705 +-
9706 +- for (i = 0; (i * alloc_unit) < num_pages; i++) {
9707 ++ for (i = 0; i < num_pages / alloc_unit; i++) {
9708 + if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
9709 + PAGE_SIZE)
9710 + return i * alloc_unit;
9711 +@@ -1207,7 +1204,7 @@ static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
9712 +
9713 + }
9714 +
9715 +- return num_pages;
9716 ++ return i * alloc_unit;
9717 + }
9718 +
9719 + static void balloon_up(struct work_struct *dummy)
9720 +@@ -1222,9 +1219,6 @@ static void balloon_up(struct work_struct *dummy)
9721 + long avail_pages;
9722 + unsigned long floor;
9723 +
9724 +- /* The host balloons pages in 2M granularity. */
9725 +- WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0);
9726 +-
9727 + /*
9728 + * We will attempt 2M allocations. However, if we fail to
9729 + * allocate 2M chunks, we will go back to 4k allocations.
9730 +@@ -1234,14 +1228,13 @@ static void balloon_up(struct work_struct *dummy)
9731 + avail_pages = si_mem_available();
9732 + floor = compute_balloon_floor();
9733 +
9734 +- /* Refuse to balloon below the floor, keep the 2M granularity. */
9735 ++ /* Refuse to balloon below the floor. */
9736 + if (avail_pages < num_pages || avail_pages - num_pages < floor) {
9737 + pr_warn("Balloon request will be partially fulfilled. %s\n",
9738 + avail_pages < num_pages ? "Not enough memory." :
9739 + "Balloon floor reached.");
9740 +
9741 + num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
9742 +- num_pages -= num_pages % PAGES_IN_2M;
9743 + }
9744 +
9745 + while (!done) {
9746 +diff --git a/drivers/infiniband/core/addr.c b/drivers/infiniband/core/addr.c
9747 +index aadaa9e84eee..c2bbe0df0931 100644
9748 +--- a/drivers/infiniband/core/addr.c
9749 ++++ b/drivers/infiniband/core/addr.c
9750 +@@ -140,7 +140,7 @@ int ib_nl_handle_ip_res_resp(struct sk_buff *skb,
9751 + if (ib_nl_is_good_ip_resp(nlh))
9752 + ib_nl_process_good_ip_rsep(nlh);
9753 +
9754 +- return skb->len;
9755 ++ return 0;
9756 + }
9757 +
9758 + static int ib_nl_ip_send_msg(struct rdma_dev_addr *dev_addr,
9759 +diff --git a/drivers/infiniband/core/sa_query.c b/drivers/infiniband/core/sa_query.c
9760 +index 50068b0a91fa..83dad5401c93 100644
9761 +--- a/drivers/infiniband/core/sa_query.c
9762 ++++ b/drivers/infiniband/core/sa_query.c
9763 +@@ -1078,7 +1078,7 @@ int ib_nl_handle_set_timeout(struct sk_buff *skb,
9764 + }
9765 +
9766 + settimeout_out:
9767 +- return skb->len;
9768 ++ return 0;
9769 + }
9770 +
9771 + static inline int ib_nl_is_good_resolve_resp(const struct nlmsghdr *nlh)
9772 +@@ -1149,7 +1149,7 @@ int ib_nl_handle_resolve_resp(struct sk_buff *skb,
9773 + }
9774 +
9775 + resp_out:
9776 +- return skb->len;
9777 ++ return 0;
9778 + }
9779 +
9780 + static void free_sm_ah(struct kref *kref)
9781 +diff --git a/drivers/infiniband/core/umem_odp.c b/drivers/infiniband/core/umem_odp.c
9782 +index 55e8f5ed8b3c..57b41125b146 100644
9783 +--- a/drivers/infiniband/core/umem_odp.c
9784 ++++ b/drivers/infiniband/core/umem_odp.c
9785 +@@ -637,7 +637,7 @@ int ib_umem_odp_map_dma_pages(struct ib_umem *umem, u64 user_virt, u64 bcnt,
9786 +
9787 + while (bcnt > 0) {
9788 + const size_t gup_num_pages = min_t(size_t,
9789 +- (bcnt + BIT(page_shift) - 1) >> page_shift,
9790 ++ ALIGN(bcnt, PAGE_SIZE) / PAGE_SIZE,
9791 + PAGE_SIZE / sizeof(struct page *));
9792 +
9793 + down_read(&owning_mm->mmap_sem);
9794 +diff --git a/drivers/infiniband/hw/mlx5/gsi.c b/drivers/infiniband/hw/mlx5/gsi.c
9795 +index 79e6309460dc..262c18b2f525 100644
9796 +--- a/drivers/infiniband/hw/mlx5/gsi.c
9797 ++++ b/drivers/infiniband/hw/mlx5/gsi.c
9798 +@@ -507,8 +507,7 @@ int mlx5_ib_gsi_post_send(struct ib_qp *qp, struct ib_send_wr *wr,
9799 + ret = ib_post_send(tx_qp, &cur_wr.wr, bad_wr);
9800 + if (ret) {
9801 + /* Undo the effect of adding the outstanding wr */
9802 +- gsi->outstanding_pi = (gsi->outstanding_pi - 1) %
9803 +- gsi->cap.max_send_wr;
9804 ++ gsi->outstanding_pi--;
9805 + goto err;
9806 + }
9807 + spin_unlock_irqrestore(&gsi->lock, flags);
9808 +diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
9809 +index 94b8d81f6020..d9a67759fdb5 100644
9810 +--- a/drivers/md/dm-crypt.c
9811 ++++ b/drivers/md/dm-crypt.c
9812 +@@ -485,8 +485,14 @@ static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
9813 + static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
9814 + const char *opts)
9815 + {
9816 +- unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
9817 +- int log = ilog2(bs);
9818 ++ unsigned bs;
9819 ++ int log;
9820 ++
9821 ++ if (test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags))
9822 ++ bs = crypto_aead_blocksize(any_tfm_aead(cc));
9823 ++ else
9824 ++ bs = crypto_skcipher_blocksize(any_tfm(cc));
9825 ++ log = ilog2(bs);
9826 +
9827 + /* we need to calculate how far we must shift the sector count
9828 + * to get the cipher block count, we use this shift in _gen */
9829 +diff --git a/drivers/md/dm-zoned-metadata.c b/drivers/md/dm-zoned-metadata.c
9830 +index 9b78f4a74a12..e3b67b145027 100644
9831 +--- a/drivers/md/dm-zoned-metadata.c
9832 ++++ b/drivers/md/dm-zoned-metadata.c
9833 +@@ -132,6 +132,7 @@ struct dmz_metadata {
9834 +
9835 + sector_t zone_bitmap_size;
9836 + unsigned int zone_nr_bitmap_blocks;
9837 ++ unsigned int zone_bits_per_mblk;
9838 +
9839 + unsigned int nr_bitmap_blocks;
9840 + unsigned int nr_map_blocks;
9841 +@@ -1165,7 +1166,10 @@ static int dmz_init_zones(struct dmz_metadata *zmd)
9842 +
9843 + /* Init */
9844 + zmd->zone_bitmap_size = dev->zone_nr_blocks >> 3;
9845 +- zmd->zone_nr_bitmap_blocks = zmd->zone_bitmap_size >> DMZ_BLOCK_SHIFT;
9846 ++ zmd->zone_nr_bitmap_blocks =
9847 ++ max_t(sector_t, 1, zmd->zone_bitmap_size >> DMZ_BLOCK_SHIFT);
9848 ++ zmd->zone_bits_per_mblk = min_t(sector_t, dev->zone_nr_blocks,
9849 ++ DMZ_BLOCK_SIZE_BITS);
9850 +
9851 + /* Allocate zone array */
9852 + zmd->zones = kcalloc(dev->nr_zones, sizeof(struct dm_zone), GFP_KERNEL);
9853 +@@ -1982,7 +1986,7 @@ int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
9854 + dmz_release_mblock(zmd, to_mblk);
9855 + dmz_release_mblock(zmd, from_mblk);
9856 +
9857 +- chunk_block += DMZ_BLOCK_SIZE_BITS;
9858 ++ chunk_block += zmd->zone_bits_per_mblk;
9859 + }
9860 +
9861 + to_zone->weight = from_zone->weight;
9862 +@@ -2043,7 +2047,7 @@ int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
9863 +
9864 + /* Set bits */
9865 + bit = chunk_block & DMZ_BLOCK_MASK_BITS;
9866 +- nr_bits = min(nr_blocks, DMZ_BLOCK_SIZE_BITS - bit);
9867 ++ nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
9868 +
9869 + count = dmz_set_bits((unsigned long *)mblk->data, bit, nr_bits);
9870 + if (count) {
9871 +@@ -2122,7 +2126,7 @@ int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
9872 +
9873 + /* Clear bits */
9874 + bit = chunk_block & DMZ_BLOCK_MASK_BITS;
9875 +- nr_bits = min(nr_blocks, DMZ_BLOCK_SIZE_BITS - bit);
9876 ++ nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
9877 +
9878 + count = dmz_clear_bits((unsigned long *)mblk->data,
9879 + bit, nr_bits);
9880 +@@ -2182,6 +2186,7 @@ static int dmz_to_next_set_block(struct dmz_metadata *zmd, struct dm_zone *zone,
9881 + {
9882 + struct dmz_mblock *mblk;
9883 + unsigned int bit, set_bit, nr_bits;
9884 ++ unsigned int zone_bits = zmd->zone_bits_per_mblk;
9885 + unsigned long *bitmap;
9886 + int n = 0;
9887 +
9888 +@@ -2196,15 +2201,15 @@ static int dmz_to_next_set_block(struct dmz_metadata *zmd, struct dm_zone *zone,
9889 + /* Get offset */
9890 + bitmap = (unsigned long *) mblk->data;
9891 + bit = chunk_block & DMZ_BLOCK_MASK_BITS;
9892 +- nr_bits = min(nr_blocks, DMZ_BLOCK_SIZE_BITS - bit);
9893 ++ nr_bits = min(nr_blocks, zone_bits - bit);
9894 + if (set)
9895 +- set_bit = find_next_bit(bitmap, DMZ_BLOCK_SIZE_BITS, bit);
9896 ++ set_bit = find_next_bit(bitmap, zone_bits, bit);
9897 + else
9898 +- set_bit = find_next_zero_bit(bitmap, DMZ_BLOCK_SIZE_BITS, bit);
9899 ++ set_bit = find_next_zero_bit(bitmap, zone_bits, bit);
9900 + dmz_release_mblock(zmd, mblk);
9901 +
9902 + n += set_bit - bit;
9903 +- if (set_bit < DMZ_BLOCK_SIZE_BITS)
9904 ++ if (set_bit < zone_bits)
9905 + break;
9906 +
9907 + nr_blocks -= nr_bits;
9908 +@@ -2307,7 +2312,7 @@ static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone)
9909 + /* Count bits in this block */
9910 + bitmap = mblk->data;
9911 + bit = chunk_block & DMZ_BLOCK_MASK_BITS;
9912 +- nr_bits = min(nr_blocks, DMZ_BLOCK_SIZE_BITS - bit);
9913 ++ nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
9914 + n += dmz_count_bits(bitmap, bit, nr_bits);
9915 +
9916 + dmz_release_mblock(zmd, mblk);
9917 +diff --git a/drivers/md/dm.c b/drivers/md/dm.c
9918 +index a56008b2e7c2..02ba6849f89d 100644
9919 +--- a/drivers/md/dm.c
9920 ++++ b/drivers/md/dm.c
9921 +@@ -1647,7 +1647,6 @@ void dm_init_md_queue(struct mapped_device *md)
9922 + * - must do so here (in alloc_dev callchain) before queue is used
9923 + */
9924 + md->queue->queuedata = md;
9925 +- md->queue->backing_dev_info->congested_data = md;
9926 + }
9927 +
9928 + void dm_init_normal_md_queue(struct mapped_device *md)
9929 +@@ -1658,6 +1657,7 @@ void dm_init_normal_md_queue(struct mapped_device *md)
9930 + /*
9931 + * Initialize aspects of queue that aren't relevant for blk-mq
9932 + */
9933 ++ md->queue->backing_dev_info->congested_data = md;
9934 + md->queue->backing_dev_info->congested_fn = dm_any_congested;
9935 + }
9936 +
9937 +@@ -1750,6 +1750,12 @@ static struct mapped_device *alloc_dev(int minor)
9938 + goto bad;
9939 +
9940 + dm_init_md_queue(md);
9941 ++ /*
9942 ++ * default to bio-based required ->make_request_fn until DM
9943 ++ * table is loaded and md->type established. If request-based
9944 ++ * table is loaded: blk-mq will override accordingly.
9945 ++ */
9946 ++ blk_queue_make_request(md->queue, dm_make_request);
9947 +
9948 + md->disk = alloc_disk_node(1, numa_node_id);
9949 + if (!md->disk)
9950 +@@ -2055,7 +2061,6 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
9951 + case DM_TYPE_BIO_BASED:
9952 + case DM_TYPE_DAX_BIO_BASED:
9953 + dm_init_normal_md_queue(md);
9954 +- blk_queue_make_request(md->queue, dm_make_request);
9955 + /*
9956 + * DM handles splitting bios as needed. Free the bio_split bioset
9957 + * since it won't be used (saves 1 process per bio-based DM device).
9958 +diff --git a/drivers/md/persistent-data/dm-space-map-common.c b/drivers/md/persistent-data/dm-space-map-common.c
9959 +index 829b4ce057d8..97f16fe14f54 100644
9960 +--- a/drivers/md/persistent-data/dm-space-map-common.c
9961 ++++ b/drivers/md/persistent-data/dm-space-map-common.c
9962 +@@ -382,6 +382,33 @@ int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
9963 + return -ENOSPC;
9964 + }
9965 +
9966 ++int sm_ll_find_common_free_block(struct ll_disk *old_ll, struct ll_disk *new_ll,
9967 ++ dm_block_t begin, dm_block_t end, dm_block_t *b)
9968 ++{
9969 ++ int r;
9970 ++ uint32_t count;
9971 ++
9972 ++ do {
9973 ++ r = sm_ll_find_free_block(new_ll, begin, new_ll->nr_blocks, b);
9974 ++ if (r)
9975 ++ break;
9976 ++
9977 ++ /* double check this block wasn't used in the old transaction */
9978 ++ if (*b >= old_ll->nr_blocks)
9979 ++ count = 0;
9980 ++ else {
9981 ++ r = sm_ll_lookup(old_ll, *b, &count);
9982 ++ if (r)
9983 ++ break;
9984 ++
9985 ++ if (count)
9986 ++ begin = *b + 1;
9987 ++ }
9988 ++ } while (count);
9989 ++
9990 ++ return r;
9991 ++}
9992 ++
9993 + static int sm_ll_mutate(struct ll_disk *ll, dm_block_t b,
9994 + int (*mutator)(void *context, uint32_t old, uint32_t *new),
9995 + void *context, enum allocation_event *ev)
9996 +diff --git a/drivers/md/persistent-data/dm-space-map-common.h b/drivers/md/persistent-data/dm-space-map-common.h
9997 +index b3078d5eda0c..8de63ce39bdd 100644
9998 +--- a/drivers/md/persistent-data/dm-space-map-common.h
9999 ++++ b/drivers/md/persistent-data/dm-space-map-common.h
10000 +@@ -109,6 +109,8 @@ int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result);
10001 + int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result);
10002 + int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
10003 + dm_block_t end, dm_block_t *result);
10004 ++int sm_ll_find_common_free_block(struct ll_disk *old_ll, struct ll_disk *new_ll,
10005 ++ dm_block_t begin, dm_block_t end, dm_block_t *result);
10006 + int sm_ll_insert(struct ll_disk *ll, dm_block_t b, uint32_t ref_count, enum allocation_event *ev);
10007 + int sm_ll_inc(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev);
10008 + int sm_ll_dec(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev);
10009 +diff --git a/drivers/md/persistent-data/dm-space-map-disk.c b/drivers/md/persistent-data/dm-space-map-disk.c
10010 +index 32adf6b4a9c7..bf4c5e2ccb6f 100644
10011 +--- a/drivers/md/persistent-data/dm-space-map-disk.c
10012 ++++ b/drivers/md/persistent-data/dm-space-map-disk.c
10013 +@@ -167,8 +167,10 @@ static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
10014 + enum allocation_event ev;
10015 + struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
10016 +
10017 +- /* FIXME: we should loop round a couple of times */
10018 +- r = sm_ll_find_free_block(&smd->old_ll, smd->begin, smd->old_ll.nr_blocks, b);
10019 ++ /*
10020 ++ * Any block we allocate has to be free in both the old and current ll.
10021 ++ */
10022 ++ r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, smd->begin, smd->ll.nr_blocks, b);
10023 + if (r)
10024 + return r;
10025 +
10026 +diff --git a/drivers/md/persistent-data/dm-space-map-metadata.c b/drivers/md/persistent-data/dm-space-map-metadata.c
10027 +index b23cac2c4738..31a999458be9 100644
10028 +--- a/drivers/md/persistent-data/dm-space-map-metadata.c
10029 ++++ b/drivers/md/persistent-data/dm-space-map-metadata.c
10030 +@@ -447,7 +447,10 @@ static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
10031 + enum allocation_event ev;
10032 + struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
10033 +
10034 +- r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
10035 ++ /*
10036 ++ * Any block we allocate has to be free in both the old and current ll.
10037 ++ */
10038 ++ r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, smm->begin, smm->ll.nr_blocks, b);
10039 + if (r)
10040 + return r;
10041 +
10042 +diff --git a/drivers/media/i2c/adv748x/adv748x.h b/drivers/media/i2c/adv748x/adv748x.h
10043 +index 296c5f8a8c63..1991c22be51a 100644
10044 +--- a/drivers/media/i2c/adv748x/adv748x.h
10045 ++++ b/drivers/media/i2c/adv748x/adv748x.h
10046 +@@ -372,10 +372,10 @@ int adv748x_write_block(struct adv748x_state *state, int client_page,
10047 +
10048 + #define io_read(s, r) adv748x_read(s, ADV748X_PAGE_IO, r)
10049 + #define io_write(s, r, v) adv748x_write(s, ADV748X_PAGE_IO, r, v)
10050 +-#define io_clrset(s, r, m, v) io_write(s, r, (io_read(s, r) & ~m) | v)
10051 ++#define io_clrset(s, r, m, v) io_write(s, r, (io_read(s, r) & ~(m)) | (v))
10052 +
10053 + #define hdmi_read(s, r) adv748x_read(s, ADV748X_PAGE_HDMI, r)
10054 +-#define hdmi_read16(s, r, m) (((hdmi_read(s, r) << 8) | hdmi_read(s, r+1)) & m)
10055 ++#define hdmi_read16(s, r, m) (((hdmi_read(s, r) << 8) | hdmi_read(s, (r)+1)) & (m))
10056 + #define hdmi_write(s, r, v) adv748x_write(s, ADV748X_PAGE_HDMI, r, v)
10057 +
10058 + #define repeater_read(s, r) adv748x_read(s, ADV748X_PAGE_REPEATER, r)
10059 +@@ -383,11 +383,11 @@ int adv748x_write_block(struct adv748x_state *state, int client_page,
10060 +
10061 + #define sdp_read(s, r) adv748x_read(s, ADV748X_PAGE_SDP, r)
10062 + #define sdp_write(s, r, v) adv748x_write(s, ADV748X_PAGE_SDP, r, v)
10063 +-#define sdp_clrset(s, r, m, v) sdp_write(s, r, (sdp_read(s, r) & ~m) | v)
10064 ++#define sdp_clrset(s, r, m, v) sdp_write(s, r, (sdp_read(s, r) & ~(m)) | (v))
10065 +
10066 + #define cp_read(s, r) adv748x_read(s, ADV748X_PAGE_CP, r)
10067 + #define cp_write(s, r, v) adv748x_write(s, ADV748X_PAGE_CP, r, v)
10068 +-#define cp_clrset(s, r, m, v) cp_write(s, r, (cp_read(s, r) & ~m) | v)
10069 ++#define cp_clrset(s, r, m, v) cp_write(s, r, (cp_read(s, r) & ~(m)) | (v))
10070 +
10071 + #define txa_read(s, r) adv748x_read(s, ADV748X_PAGE_TXA, r)
10072 + #define txb_read(s, r) adv748x_read(s, ADV748X_PAGE_TXB, r)
10073 +diff --git a/drivers/media/rc/iguanair.c b/drivers/media/rc/iguanair.c
10074 +index 3c2e248ceca8..03dbbfba71fc 100644
10075 +--- a/drivers/media/rc/iguanair.c
10076 ++++ b/drivers/media/rc/iguanair.c
10077 +@@ -427,7 +427,7 @@ static int iguanair_probe(struct usb_interface *intf,
10078 + int ret, pipein, pipeout;
10079 + struct usb_host_interface *idesc;
10080 +
10081 +- idesc = intf->altsetting;
10082 ++ idesc = intf->cur_altsetting;
10083 + if (idesc->desc.bNumEndpoints < 2)
10084 + return -ENODEV;
10085 +
10086 +diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c
10087 +index 6445b638f207..5899593dabaf 100644
10088 +--- a/drivers/media/usb/uvc/uvc_driver.c
10089 ++++ b/drivers/media/usb/uvc/uvc_driver.c
10090 +@@ -1446,6 +1446,11 @@ static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
10091 + break;
10092 + if (forward == prev)
10093 + continue;
10094 ++ if (forward->chain.next || forward->chain.prev) {
10095 ++ uvc_trace(UVC_TRACE_DESCR, "Found reference to "
10096 ++ "entity %d already in chain.\n", forward->id);
10097 ++ return -EINVAL;
10098 ++ }
10099 +
10100 + switch (UVC_ENTITY_TYPE(forward)) {
10101 + case UVC_VC_EXTENSION_UNIT:
10102 +@@ -1527,6 +1532,13 @@ static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
10103 + return -1;
10104 + }
10105 +
10106 ++ if (term->chain.next || term->chain.prev) {
10107 ++ uvc_trace(UVC_TRACE_DESCR, "Found reference to "
10108 ++ "entity %d already in chain.\n",
10109 ++ term->id);
10110 ++ return -EINVAL;
10111 ++ }
10112 ++
10113 + if (uvc_trace_param & UVC_TRACE_PROBE)
10114 + printk(KERN_CONT " %d", term->id);
10115 +
10116 +diff --git a/drivers/media/v4l2-core/videobuf-dma-sg.c b/drivers/media/v4l2-core/videobuf-dma-sg.c
10117 +index f412429cf5ba..c55e607f5631 100644
10118 +--- a/drivers/media/v4l2-core/videobuf-dma-sg.c
10119 ++++ b/drivers/media/v4l2-core/videobuf-dma-sg.c
10120 +@@ -352,8 +352,11 @@ int videobuf_dma_free(struct videobuf_dmabuf *dma)
10121 + BUG_ON(dma->sglen);
10122 +
10123 + if (dma->pages) {
10124 +- for (i = 0; i < dma->nr_pages; i++)
10125 ++ for (i = 0; i < dma->nr_pages; i++) {
10126 ++ if (dma->direction == DMA_FROM_DEVICE)
10127 ++ set_page_dirty_lock(dma->pages[i]);
10128 + put_page(dma->pages[i]);
10129 ++ }
10130 + kfree(dma->pages);
10131 + dma->pages = NULL;
10132 + }
10133 +diff --git a/drivers/mfd/da9062-core.c b/drivers/mfd/da9062-core.c
10134 +index fe1811523e4a..eff6ae5073c8 100644
10135 +--- a/drivers/mfd/da9062-core.c
10136 ++++ b/drivers/mfd/da9062-core.c
10137 +@@ -257,7 +257,7 @@ static const struct mfd_cell da9062_devs[] = {
10138 + .name = "da9062-watchdog",
10139 + .num_resources = ARRAY_SIZE(da9062_wdt_resources),
10140 + .resources = da9062_wdt_resources,
10141 +- .of_compatible = "dlg,da9062-wdt",
10142 ++ .of_compatible = "dlg,da9062-watchdog",
10143 + },
10144 + {
10145 + .name = "da9062-thermal",
10146 +diff --git a/drivers/mfd/dln2.c b/drivers/mfd/dln2.c
10147 +index 704e189ca162..95d0f2df0ad4 100644
10148 +--- a/drivers/mfd/dln2.c
10149 ++++ b/drivers/mfd/dln2.c
10150 +@@ -729,6 +729,8 @@ static int dln2_probe(struct usb_interface *interface,
10151 + const struct usb_device_id *usb_id)
10152 + {
10153 + struct usb_host_interface *hostif = interface->cur_altsetting;
10154 ++ struct usb_endpoint_descriptor *epin;
10155 ++ struct usb_endpoint_descriptor *epout;
10156 + struct device *dev = &interface->dev;
10157 + struct dln2_dev *dln2;
10158 + int ret;
10159 +@@ -738,12 +740,19 @@ static int dln2_probe(struct usb_interface *interface,
10160 + hostif->desc.bNumEndpoints < 2)
10161 + return -ENODEV;
10162 +
10163 ++ epin = &hostif->endpoint[0].desc;
10164 ++ epout = &hostif->endpoint[1].desc;
10165 ++ if (!usb_endpoint_is_bulk_out(epout))
10166 ++ return -ENODEV;
10167 ++ if (!usb_endpoint_is_bulk_in(epin))
10168 ++ return -ENODEV;
10169 ++
10170 + dln2 = kzalloc(sizeof(*dln2), GFP_KERNEL);
10171 + if (!dln2)
10172 + return -ENOMEM;
10173 +
10174 +- dln2->ep_out = hostif->endpoint[0].desc.bEndpointAddress;
10175 +- dln2->ep_in = hostif->endpoint[1].desc.bEndpointAddress;
10176 ++ dln2->ep_out = epout->bEndpointAddress;
10177 ++ dln2->ep_in = epin->bEndpointAddress;
10178 + dln2->usb_dev = usb_get_dev(interface_to_usbdev(interface));
10179 + dln2->interface = interface;
10180 + usb_set_intfdata(interface, dln2);
10181 +diff --git a/drivers/mfd/rn5t618.c b/drivers/mfd/rn5t618.c
10182 +index f4037d42a60f..dd4251f105e0 100644
10183 +--- a/drivers/mfd/rn5t618.c
10184 ++++ b/drivers/mfd/rn5t618.c
10185 +@@ -32,6 +32,7 @@ static bool rn5t618_volatile_reg(struct device *dev, unsigned int reg)
10186 + case RN5T618_WATCHDOGCNT:
10187 + case RN5T618_DCIRQ:
10188 + case RN5T618_ILIMDATAH ... RN5T618_AIN0DATAL:
10189 ++ case RN5T618_ADCCNT3:
10190 + case RN5T618_IR_ADC1 ... RN5T618_IR_ADC3:
10191 + case RN5T618_IR_GPR:
10192 + case RN5T618_IR_GPF:
10193 +diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
10194 +index ea254d00541f..24795454d106 100644
10195 +--- a/drivers/mmc/host/mmc_spi.c
10196 ++++ b/drivers/mmc/host/mmc_spi.c
10197 +@@ -1154,17 +1154,22 @@ static void mmc_spi_initsequence(struct mmc_spi_host *host)
10198 + * SPI protocol. Another is that when chipselect is released while
10199 + * the card returns BUSY status, the clock must issue several cycles
10200 + * with chipselect high before the card will stop driving its output.
10201 ++ *
10202 ++ * SPI_CS_HIGH means "asserted" here. In some cases like when using
10203 ++ * GPIOs for chip select, SPI_CS_HIGH is set but this will be logically
10204 ++ * inverted by gpiolib, so if we want to ascertain to drive it high
10205 ++ * we should toggle the default with an XOR as we do here.
10206 + */
10207 +- host->spi->mode |= SPI_CS_HIGH;
10208 ++ host->spi->mode ^= SPI_CS_HIGH;
10209 + if (spi_setup(host->spi) != 0) {
10210 + /* Just warn; most cards work without it. */
10211 + dev_warn(&host->spi->dev,
10212 + "can't change chip-select polarity\n");
10213 +- host->spi->mode &= ~SPI_CS_HIGH;
10214 ++ host->spi->mode ^= SPI_CS_HIGH;
10215 + } else {
10216 + mmc_spi_readbytes(host, 18);
10217 +
10218 +- host->spi->mode &= ~SPI_CS_HIGH;
10219 ++ host->spi->mode ^= SPI_CS_HIGH;
10220 + if (spi_setup(host->spi) != 0) {
10221 + /* Wot, we can't get the same setup we had before? */
10222 + dev_err(&host->spi->dev,
10223 +diff --git a/drivers/mmc/host/sdhci-of-at91.c b/drivers/mmc/host/sdhci-of-at91.c
10224 +index 564e7be21e06..1dadd460cc8f 100644
10225 +--- a/drivers/mmc/host/sdhci-of-at91.c
10226 ++++ b/drivers/mmc/host/sdhci-of-at91.c
10227 +@@ -331,19 +331,22 @@ static int sdhci_at91_probe(struct platform_device *pdev)
10228 + priv->mainck = devm_clk_get(&pdev->dev, "baseclk");
10229 + if (IS_ERR(priv->mainck)) {
10230 + dev_err(&pdev->dev, "failed to get baseclk\n");
10231 +- return PTR_ERR(priv->mainck);
10232 ++ ret = PTR_ERR(priv->mainck);
10233 ++ goto sdhci_pltfm_free;
10234 + }
10235 +
10236 + priv->hclock = devm_clk_get(&pdev->dev, "hclock");
10237 + if (IS_ERR(priv->hclock)) {
10238 + dev_err(&pdev->dev, "failed to get hclock\n");
10239 +- return PTR_ERR(priv->hclock);
10240 ++ ret = PTR_ERR(priv->hclock);
10241 ++ goto sdhci_pltfm_free;
10242 + }
10243 +
10244 + priv->gck = devm_clk_get(&pdev->dev, "multclk");
10245 + if (IS_ERR(priv->gck)) {
10246 + dev_err(&pdev->dev, "failed to get multclk\n");
10247 +- return PTR_ERR(priv->gck);
10248 ++ ret = PTR_ERR(priv->gck);
10249 ++ goto sdhci_pltfm_free;
10250 + }
10251 +
10252 + ret = sdhci_at91_set_clks_presets(&pdev->dev);
10253 +diff --git a/drivers/mtd/ubi/fastmap.c b/drivers/mtd/ubi/fastmap.c
10254 +index 63e8527f7b65..18aba1cf8acc 100644
10255 +--- a/drivers/mtd/ubi/fastmap.c
10256 ++++ b/drivers/mtd/ubi/fastmap.c
10257 +@@ -73,7 +73,7 @@ static int self_check_seen(struct ubi_device *ubi, unsigned long *seen)
10258 + return 0;
10259 +
10260 + for (pnum = 0; pnum < ubi->peb_count; pnum++) {
10261 +- if (test_bit(pnum, seen) && ubi->lookuptbl[pnum]) {
10262 ++ if (!test_bit(pnum, seen) && ubi->lookuptbl[pnum]) {
10263 + ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum);
10264 + ret = -EINVAL;
10265 + }
10266 +@@ -1147,7 +1147,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
10267 + struct rb_node *tmp_rb;
10268 + int ret, i, j, free_peb_count, used_peb_count, vol_count;
10269 + int scrub_peb_count, erase_peb_count;
10270 +- unsigned long *seen_pebs = NULL;
10271 ++ unsigned long *seen_pebs;
10272 +
10273 + fm_raw = ubi->fm_buf;
10274 + memset(ubi->fm_buf, 0, ubi->fm_size);
10275 +@@ -1161,7 +1161,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
10276 + dvbuf = new_fm_vbuf(ubi, UBI_FM_DATA_VOLUME_ID);
10277 + if (!dvbuf) {
10278 + ret = -ENOMEM;
10279 +- goto out_kfree;
10280 ++ goto out_free_avbuf;
10281 + }
10282 +
10283 + avhdr = ubi_get_vid_hdr(avbuf);
10284 +@@ -1170,7 +1170,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
10285 + seen_pebs = init_seen(ubi);
10286 + if (IS_ERR(seen_pebs)) {
10287 + ret = PTR_ERR(seen_pebs);
10288 +- goto out_kfree;
10289 ++ goto out_free_dvbuf;
10290 + }
10291 +
10292 + spin_lock(&ubi->volumes_lock);
10293 +@@ -1338,7 +1338,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
10294 + ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avbuf);
10295 + if (ret) {
10296 + ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
10297 +- goto out_kfree;
10298 ++ goto out_free_seen;
10299 + }
10300 +
10301 + for (i = 0; i < new_fm->used_blocks; i++) {
10302 +@@ -1360,7 +1360,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
10303 + if (ret) {
10304 + ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
10305 + new_fm->e[i]->pnum);
10306 +- goto out_kfree;
10307 ++ goto out_free_seen;
10308 + }
10309 + }
10310 +
10311 +@@ -1370,7 +1370,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
10312 + if (ret) {
10313 + ubi_err(ubi, "unable to write fastmap to PEB %i!",
10314 + new_fm->e[i]->pnum);
10315 +- goto out_kfree;
10316 ++ goto out_free_seen;
10317 + }
10318 + }
10319 +
10320 +@@ -1380,10 +1380,13 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
10321 + ret = self_check_seen(ubi, seen_pebs);
10322 + dbg_bld("fastmap written!");
10323 +
10324 +-out_kfree:
10325 +- ubi_free_vid_buf(avbuf);
10326 +- ubi_free_vid_buf(dvbuf);
10327 ++out_free_seen:
10328 + free_seen(seen_pebs);
10329 ++out_free_dvbuf:
10330 ++ ubi_free_vid_buf(dvbuf);
10331 ++out_free_avbuf:
10332 ++ ubi_free_vid_buf(avbuf);
10333 ++
10334 + out:
10335 + return ret;
10336 + }
10337 +diff --git a/drivers/net/bonding/bond_alb.c b/drivers/net/bonding/bond_alb.c
10338 +index 60666db31886..755d588bbcb1 100644
10339 +--- a/drivers/net/bonding/bond_alb.c
10340 ++++ b/drivers/net/bonding/bond_alb.c
10341 +@@ -1403,26 +1403,31 @@ int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
10342 + bool do_tx_balance = true;
10343 + u32 hash_index = 0;
10344 + const u8 *hash_start = NULL;
10345 +- struct ipv6hdr *ip6hdr;
10346 +
10347 + skb_reset_mac_header(skb);
10348 + eth_data = eth_hdr(skb);
10349 +
10350 + switch (ntohs(skb->protocol)) {
10351 + case ETH_P_IP: {
10352 +- const struct iphdr *iph = ip_hdr(skb);
10353 ++ const struct iphdr *iph;
10354 +
10355 + if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast) ||
10356 +- (iph->daddr == ip_bcast) ||
10357 +- (iph->protocol == IPPROTO_IGMP)) {
10358 ++ (!pskb_network_may_pull(skb, sizeof(*iph)))) {
10359 ++ do_tx_balance = false;
10360 ++ break;
10361 ++ }
10362 ++ iph = ip_hdr(skb);
10363 ++ if (iph->daddr == ip_bcast || iph->protocol == IPPROTO_IGMP) {
10364 + do_tx_balance = false;
10365 + break;
10366 + }
10367 + hash_start = (char *)&(iph->daddr);
10368 + hash_size = sizeof(iph->daddr);
10369 +- }
10370 + break;
10371 +- case ETH_P_IPV6:
10372 ++ }
10373 ++ case ETH_P_IPV6: {
10374 ++ const struct ipv6hdr *ip6hdr;
10375 ++
10376 + /* IPv6 doesn't really use broadcast mac address, but leave
10377 + * that here just in case.
10378 + */
10379 +@@ -1439,7 +1444,11 @@ int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
10380 + break;
10381 + }
10382 +
10383 +- /* Additianally, DAD probes should not be tx-balanced as that
10384 ++ if (!pskb_network_may_pull(skb, sizeof(*ip6hdr))) {
10385 ++ do_tx_balance = false;
10386 ++ break;
10387 ++ }
10388 ++ /* Additionally, DAD probes should not be tx-balanced as that
10389 + * will lead to false positives for duplicate addresses and
10390 + * prevent address configuration from working.
10391 + */
10392 +@@ -1449,17 +1458,26 @@ int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
10393 + break;
10394 + }
10395 +
10396 +- hash_start = (char *)&(ipv6_hdr(skb)->daddr);
10397 +- hash_size = sizeof(ipv6_hdr(skb)->daddr);
10398 ++ hash_start = (char *)&ip6hdr->daddr;
10399 ++ hash_size = sizeof(ip6hdr->daddr);
10400 + break;
10401 +- case ETH_P_IPX:
10402 +- if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
10403 ++ }
10404 ++ case ETH_P_IPX: {
10405 ++ const struct ipxhdr *ipxhdr;
10406 ++
10407 ++ if (pskb_network_may_pull(skb, sizeof(*ipxhdr))) {
10408 ++ do_tx_balance = false;
10409 ++ break;
10410 ++ }
10411 ++ ipxhdr = (struct ipxhdr *)skb_network_header(skb);
10412 ++
10413 ++ if (ipxhdr->ipx_checksum != IPX_NO_CHECKSUM) {
10414 + /* something is wrong with this packet */
10415 + do_tx_balance = false;
10416 + break;
10417 + }
10418 +
10419 +- if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
10420 ++ if (ipxhdr->ipx_type != IPX_TYPE_NCP) {
10421 + /* The only protocol worth balancing in
10422 + * this family since it has an "ARP" like
10423 + * mechanism
10424 +@@ -1468,9 +1486,11 @@ int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
10425 + break;
10426 + }
10427 +
10428 ++ eth_data = eth_hdr(skb);
10429 + hash_start = (char *)eth_data->h_dest;
10430 + hash_size = ETH_ALEN;
10431 + break;
10432 ++ }
10433 + case ETH_P_ARP:
10434 + do_tx_balance = false;
10435 + if (bond_info->rlb_enabled)
10436 +diff --git a/drivers/net/dsa/bcm_sf2.c b/drivers/net/dsa/bcm_sf2.c
10437 +index 05440b727261..747062f04bb5 100644
10438 +--- a/drivers/net/dsa/bcm_sf2.c
10439 ++++ b/drivers/net/dsa/bcm_sf2.c
10440 +@@ -137,7 +137,9 @@ static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
10441 +
10442 + /* Force link status for IMP port */
10443 + reg = core_readl(priv, offset);
10444 +- reg |= (MII_SW_OR | LINK_STS | GMII_SPEED_UP_2G);
10445 ++ reg |= (MII_SW_OR | LINK_STS);
10446 ++ if (priv->type == BCM7278_DEVICE_ID)
10447 ++ reg |= GMII_SPEED_UP_2G;
10448 + core_writel(priv, reg, offset);
10449 +
10450 + /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
10451 +diff --git a/drivers/net/ethernet/broadcom/bcmsysport.c b/drivers/net/ethernet/broadcom/bcmsysport.c
10452 +index 69b2f99b0c19..f48f7d104af2 100644
10453 +--- a/drivers/net/ethernet/broadcom/bcmsysport.c
10454 ++++ b/drivers/net/ethernet/broadcom/bcmsysport.c
10455 +@@ -2329,6 +2329,9 @@ static int bcm_sysport_resume(struct device *d)
10456 +
10457 + umac_reset(priv);
10458 +
10459 ++ /* Disable the UniMAC RX/TX */
10460 ++ umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
10461 ++
10462 + /* We may have been suspended and never received a WOL event that
10463 + * would turn off MPD detection, take care of that now
10464 + */
10465 +diff --git a/drivers/net/ethernet/broadcom/bnxt/bnxt.c b/drivers/net/ethernet/broadcom/bnxt/bnxt.c
10466 +index 7461e7b9eaae..41bc7820d2dd 100644
10467 +--- a/drivers/net/ethernet/broadcom/bnxt/bnxt.c
10468 ++++ b/drivers/net/ethernet/broadcom/bnxt/bnxt.c
10469 +@@ -5375,7 +5375,7 @@ static void bnxt_setup_msix(struct bnxt *bp)
10470 + int tcs, i;
10471 +
10472 + tcs = netdev_get_num_tc(dev);
10473 +- if (tcs > 1) {
10474 ++ if (tcs) {
10475 + int i, off, count;
10476 +
10477 + for (i = 0; i < tcs; i++) {
10478 +diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
10479 +index 5aff1b460151..b01b242c2bf0 100644
10480 +--- a/drivers/net/ethernet/cadence/macb_main.c
10481 ++++ b/drivers/net/ethernet/cadence/macb_main.c
10482 +@@ -66,7 +66,11 @@
10483 + /* Max length of transmit frame must be a multiple of 8 bytes */
10484 + #define MACB_TX_LEN_ALIGN 8
10485 + #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
10486 +-#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
10487 ++/* Limit maximum TX length as per Cadence TSO errata. This is to avoid a
10488 ++ * false amba_error in TX path from the DMA assuming there is not enough
10489 ++ * space in the SRAM (16KB) even when there is.
10490 ++ */
10491 ++#define GEM_MAX_TX_LEN (unsigned int)(0x3FC0)
10492 +
10493 + #define GEM_MTU_MIN_SIZE ETH_MIN_MTU
10494 + #define MACB_NETIF_LSO NETIF_F_TSO
10495 +@@ -1577,16 +1581,14 @@ static netdev_features_t macb_features_check(struct sk_buff *skb,
10496 +
10497 + /* Validate LSO compatibility */
10498 +
10499 +- /* there is only one buffer */
10500 +- if (!skb_is_nonlinear(skb))
10501 ++ /* there is only one buffer or protocol is not UDP */
10502 ++ if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
10503 + return features;
10504 +
10505 + /* length of header */
10506 + hdrlen = skb_transport_offset(skb);
10507 +- if (ip_hdr(skb)->protocol == IPPROTO_TCP)
10508 +- hdrlen += tcp_hdrlen(skb);
10509 +
10510 +- /* For LSO:
10511 ++ /* For UFO only:
10512 + * When software supplies two or more payload buffers all payload buffers
10513 + * apart from the last must be a multiple of 8 bytes in size.
10514 + */
10515 +diff --git a/drivers/net/ethernet/dec/tulip/dmfe.c b/drivers/net/ethernet/dec/tulip/dmfe.c
10516 +index 07e10a45beaa..cd5309668186 100644
10517 +--- a/drivers/net/ethernet/dec/tulip/dmfe.c
10518 ++++ b/drivers/net/ethernet/dec/tulip/dmfe.c
10519 +@@ -2224,15 +2224,16 @@ static int __init dmfe_init_module(void)
10520 + if (cr6set)
10521 + dmfe_cr6_user_set = cr6set;
10522 +
10523 +- switch(mode) {
10524 +- case DMFE_10MHF:
10525 ++ switch (mode) {
10526 ++ case DMFE_10MHF:
10527 + case DMFE_100MHF:
10528 + case DMFE_10MFD:
10529 + case DMFE_100MFD:
10530 + case DMFE_1M_HPNA:
10531 + dmfe_media_mode = mode;
10532 + break;
10533 +- default:dmfe_media_mode = DMFE_AUTO;
10534 ++ default:
10535 ++ dmfe_media_mode = DMFE_AUTO;
10536 + break;
10537 + }
10538 +
10539 +diff --git a/drivers/net/ethernet/dec/tulip/uli526x.c b/drivers/net/ethernet/dec/tulip/uli526x.c
10540 +index 7fc248efc4ba..9779555eea25 100644
10541 +--- a/drivers/net/ethernet/dec/tulip/uli526x.c
10542 ++++ b/drivers/net/ethernet/dec/tulip/uli526x.c
10543 +@@ -1819,8 +1819,8 @@ static int __init uli526x_init_module(void)
10544 + if (cr6set)
10545 + uli526x_cr6_user_set = cr6set;
10546 +
10547 +- switch (mode) {
10548 +- case ULI526X_10MHF:
10549 ++ switch (mode) {
10550 ++ case ULI526X_10MHF:
10551 + case ULI526X_100MHF:
10552 + case ULI526X_10MFD:
10553 + case ULI526X_100MFD:
10554 +diff --git a/drivers/net/ethernet/smsc/smc911x.c b/drivers/net/ethernet/smsc/smc911x.c
10555 +index 42d35a87bcc9..f4f52a64f450 100644
10556 +--- a/drivers/net/ethernet/smsc/smc911x.c
10557 ++++ b/drivers/net/ethernet/smsc/smc911x.c
10558 +@@ -948,7 +948,7 @@ static void smc911x_phy_configure(struct work_struct *work)
10559 + if (lp->ctl_rspeed != 100)
10560 + my_ad_caps &= ~(ADVERTISE_100BASE4|ADVERTISE_100FULL|ADVERTISE_100HALF);
10561 +
10562 +- if (!lp->ctl_rfduplx)
10563 ++ if (!lp->ctl_rfduplx)
10564 + my_ad_caps &= ~(ADVERTISE_100FULL|ADVERTISE_10FULL);
10565 +
10566 + /* Update our Auto-Neg Advertisement Register */
10567 +diff --git a/drivers/net/gtp.c b/drivers/net/gtp.c
10568 +index 3840f21dd635..92e4e5d53053 100644
10569 +--- a/drivers/net/gtp.c
10570 ++++ b/drivers/net/gtp.c
10571 +@@ -771,11 +771,13 @@ static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
10572 + {
10573 + int i;
10574 +
10575 +- gtp->addr_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
10576 ++ gtp->addr_hash = kmalloc(sizeof(struct hlist_head) * hsize,
10577 ++ GFP_KERNEL | __GFP_NOWARN);
10578 + if (gtp->addr_hash == NULL)
10579 + return -ENOMEM;
10580 +
10581 +- gtp->tid_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
10582 ++ gtp->tid_hash = kmalloc(sizeof(struct hlist_head) * hsize,
10583 ++ GFP_KERNEL | __GFP_NOWARN);
10584 + if (gtp->tid_hash == NULL)
10585 + goto err1;
10586 +
10587 +diff --git a/drivers/net/ppp/ppp_async.c b/drivers/net/ppp/ppp_async.c
10588 +index 814fd8fae67d..297a986e6653 100644
10589 +--- a/drivers/net/ppp/ppp_async.c
10590 ++++ b/drivers/net/ppp/ppp_async.c
10591 +@@ -878,15 +878,15 @@ ppp_async_input(struct asyncppp *ap, const unsigned char *buf,
10592 + skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2);
10593 + if (!skb)
10594 + goto nomem;
10595 +- ap->rpkt = skb;
10596 +- }
10597 +- if (skb->len == 0) {
10598 +- /* Try to get the payload 4-byte aligned.
10599 +- * This should match the
10600 +- * PPP_ALLSTATIONS/PPP_UI/compressed tests in
10601 +- * process_input_packet, but we do not have
10602 +- * enough chars here to test buf[1] and buf[2].
10603 +- */
10604 ++ ap->rpkt = skb;
10605 ++ }
10606 ++ if (skb->len == 0) {
10607 ++ /* Try to get the payload 4-byte aligned.
10608 ++ * This should match the
10609 ++ * PPP_ALLSTATIONS/PPP_UI/compressed tests in
10610 ++ * process_input_packet, but we do not have
10611 ++ * enough chars here to test buf[1] and buf[2].
10612 ++ */
10613 + if (buf[0] != PPP_ALLSTATIONS)
10614 + skb_reserve(skb, 2 + (buf[0] & 1));
10615 + }
10616 +diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c
10617 +index 2eb5fe7367c6..4ad830b7b1c9 100644
10618 +--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c
10619 ++++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c
10620 +@@ -441,6 +441,7 @@ fail:
10621 + usb_free_urb(req->urb);
10622 + list_del(q->next);
10623 + }
10624 ++ kfree(reqs);
10625 + return NULL;
10626 +
10627 + }
10628 +diff --git a/drivers/net/wireless/intel/iwlwifi/mvm/nvm.c b/drivers/net/wireless/intel/iwlwifi/mvm/nvm.c
10629 +index ca2d66ce8424..8f3032b7174d 100644
10630 +--- a/drivers/net/wireless/intel/iwlwifi/mvm/nvm.c
10631 ++++ b/drivers/net/wireless/intel/iwlwifi/mvm/nvm.c
10632 +@@ -298,7 +298,7 @@ iwl_parse_nvm_sections(struct iwl_mvm *mvm)
10633 + int regulatory_type;
10634 +
10635 + /* Checking for required sections */
10636 +- if (mvm->trans->cfg->nvm_type != IWL_NVM_EXT) {
10637 ++ if (mvm->trans->cfg->nvm_type == IWL_NVM) {
10638 + if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
10639 + !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) {
10640 + IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n");
10641 +diff --git a/drivers/net/wireless/intel/iwlwifi/mvm/sta.c b/drivers/net/wireless/intel/iwlwifi/mvm/sta.c
10642 +index 684c0f65a052..d9ab85c8eb6a 100644
10643 +--- a/drivers/net/wireless/intel/iwlwifi/mvm/sta.c
10644 ++++ b/drivers/net/wireless/intel/iwlwifi/mvm/sta.c
10645 +@@ -2981,6 +2981,10 @@ static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
10646 + igtk_cmd.sta_id = cpu_to_le32(sta_id);
10647 +
10648 + if (remove_key) {
10649 ++ /* This is a valid situation for IGTK */
10650 ++ if (sta_id == IWL_MVM_INVALID_STA)
10651 ++ return 0;
10652 ++
10653 + igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
10654 + } else {
10655 + struct ieee80211_key_seq seq;
10656 +@@ -3285,9 +3289,9 @@ int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
10657 + IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
10658 + keyconf->keyidx, sta_id);
10659 +
10660 +- if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
10661 +- keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
10662 +- keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
10663 ++ if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
10664 ++ keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
10665 ++ keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
10666 + return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
10667 +
10668 + if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
10669 +diff --git a/drivers/net/wireless/marvell/libertas/cfg.c b/drivers/net/wireless/marvell/libertas/cfg.c
10670 +index 4ffc188d2ffd..fbeb12018c3d 100644
10671 +--- a/drivers/net/wireless/marvell/libertas/cfg.c
10672 ++++ b/drivers/net/wireless/marvell/libertas/cfg.c
10673 +@@ -1788,6 +1788,8 @@ static int lbs_ibss_join_existing(struct lbs_private *priv,
10674 + rates_max = rates_eid[1];
10675 + if (rates_max > MAX_RATES) {
10676 + lbs_deb_join("invalid rates");
10677 ++ rcu_read_unlock();
10678 ++ ret = -EINVAL;
10679 + goto out;
10680 + }
10681 + rates = cmd.bss.rates;
10682 +diff --git a/drivers/net/wireless/marvell/mwifiex/scan.c b/drivers/net/wireless/marvell/mwifiex/scan.c
10683 +index c013c94fbf15..0071c40afe81 100644
10684 +--- a/drivers/net/wireless/marvell/mwifiex/scan.c
10685 ++++ b/drivers/net/wireless/marvell/mwifiex/scan.c
10686 +@@ -2890,6 +2890,13 @@ mwifiex_cmd_append_vsie_tlv(struct mwifiex_private *priv,
10687 + vs_param_set->header.len =
10688 + cpu_to_le16((((u16) priv->vs_ie[id].ie[1])
10689 + & 0x00FF) + 2);
10690 ++ if (le16_to_cpu(vs_param_set->header.len) >
10691 ++ MWIFIEX_MAX_VSIE_LEN) {
10692 ++ mwifiex_dbg(priv->adapter, ERROR,
10693 ++ "Invalid param length!\n");
10694 ++ break;
10695 ++ }
10696 ++
10697 + memcpy(vs_param_set->ie, priv->vs_ie[id].ie,
10698 + le16_to_cpu(vs_param_set->header.len));
10699 + *buffer += le16_to_cpu(vs_param_set->header.len) +
10700 +diff --git a/drivers/net/wireless/marvell/mwifiex/sta_ioctl.c b/drivers/net/wireless/marvell/mwifiex/sta_ioctl.c
10701 +index f88a953b3cd5..652acafca136 100644
10702 +--- a/drivers/net/wireless/marvell/mwifiex/sta_ioctl.c
10703 ++++ b/drivers/net/wireless/marvell/mwifiex/sta_ioctl.c
10704 +@@ -274,6 +274,7 @@ static int mwifiex_process_country_ie(struct mwifiex_private *priv,
10705 +
10706 + if (country_ie_len >
10707 + (IEEE80211_COUNTRY_STRING_LEN + MWIFIEX_MAX_TRIPLET_802_11D)) {
10708 ++ rcu_read_unlock();
10709 + mwifiex_dbg(priv->adapter, ERROR,
10710 + "11D: country_ie_len overflow!, deauth AP\n");
10711 + return -EINVAL;
10712 +diff --git a/drivers/net/wireless/marvell/mwifiex/wmm.c b/drivers/net/wireless/marvell/mwifiex/wmm.c
10713 +index 7fba4d940131..a13b05ec8fc0 100644
10714 +--- a/drivers/net/wireless/marvell/mwifiex/wmm.c
10715 ++++ b/drivers/net/wireless/marvell/mwifiex/wmm.c
10716 +@@ -976,6 +976,10 @@ int mwifiex_ret_wmm_get_status(struct mwifiex_private *priv,
10717 + "WMM Parameter Set Count: %d\n",
10718 + wmm_param_ie->qos_info_bitmap & mask);
10719 +
10720 ++ if (wmm_param_ie->vend_hdr.len + 2 >
10721 ++ sizeof(struct ieee_types_wmm_parameter))
10722 ++ break;
10723 ++
10724 + memcpy((u8 *) &priv->curr_bss_params.bss_descriptor.
10725 + wmm_ie, wmm_param_ie,
10726 + wmm_param_ie->vend_hdr.len + 2);
10727 +diff --git a/drivers/nfc/pn544/pn544.c b/drivers/nfc/pn544/pn544.c
10728 +index 70e898e38b16..f30bdf95610f 100644
10729 +--- a/drivers/nfc/pn544/pn544.c
10730 ++++ b/drivers/nfc/pn544/pn544.c
10731 +@@ -704,7 +704,7 @@ static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
10732 + target->nfcid1_len != 10)
10733 + return -EOPNOTSUPP;
10734 +
10735 +- return nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
10736 ++ return nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
10737 + PN544_RF_READER_CMD_ACTIVATE_NEXT,
10738 + target->nfcid1, target->nfcid1_len, NULL);
10739 + } else if (target->supported_protocols & (NFC_PROTO_JEWEL_MASK |
10740 +diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
10741 +index ba7b034b2b91..6b8646db110c 100644
10742 +--- a/drivers/of/Kconfig
10743 ++++ b/drivers/of/Kconfig
10744 +@@ -112,4 +112,8 @@ config OF_OVERLAY
10745 + config OF_NUMA
10746 + bool
10747 +
10748 ++config OF_DMA_DEFAULT_COHERENT
10749 ++ # arches should select this if DMA is coherent by default for OF devices
10750 ++ bool
10751 ++
10752 + endif # OF
10753 +diff --git a/drivers/of/address.c b/drivers/of/address.c
10754 +index 792722e7d458..456339c19aed 100644
10755 +--- a/drivers/of/address.c
10756 ++++ b/drivers/of/address.c
10757 +@@ -894,12 +894,16 @@ EXPORT_SYMBOL_GPL(of_dma_get_range);
10758 + * @np: device node
10759 + *
10760 + * It returns true if "dma-coherent" property was found
10761 +- * for this device in DT.
10762 ++ * for this device in the DT, or if DMA is coherent by
10763 ++ * default for OF devices on the current platform.
10764 + */
10765 + bool of_dma_is_coherent(struct device_node *np)
10766 + {
10767 + struct device_node *node = of_node_get(np);
10768 +
10769 ++ if (IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT))
10770 ++ return true;
10771 ++
10772 + while (node) {
10773 + if (of_property_read_bool(node, "dma-coherent")) {
10774 + of_node_put(node);
10775 +diff --git a/drivers/pci/dwc/pci-keystone-dw.c b/drivers/pci/dwc/pci-keystone-dw.c
10776 +index 2fb20b887d2a..4cf2662930d8 100644
10777 +--- a/drivers/pci/dwc/pci-keystone-dw.c
10778 ++++ b/drivers/pci/dwc/pci-keystone-dw.c
10779 +@@ -510,7 +510,7 @@ void ks_dw_pcie_initiate_link_train(struct keystone_pcie *ks_pcie)
10780 + /* Disable Link training */
10781 + val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
10782 + val &= ~LTSSM_EN_VAL;
10783 +- ks_dw_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
10784 ++ ks_dw_app_writel(ks_pcie, CMD_STATUS, val);
10785 +
10786 + /* Initiate Link Training */
10787 + val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
10788 +diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
10789 +index 958da7db9033..fb73e975d22b 100644
10790 +--- a/drivers/pci/setup-bus.c
10791 ++++ b/drivers/pci/setup-bus.c
10792 +@@ -1824,12 +1824,18 @@ again:
10793 + /* restore size and flags */
10794 + list_for_each_entry(fail_res, &fail_head, list) {
10795 + struct resource *res = fail_res->res;
10796 ++ int idx;
10797 +
10798 + res->start = fail_res->start;
10799 + res->end = fail_res->end;
10800 + res->flags = fail_res->flags;
10801 +- if (fail_res->dev->subordinate)
10802 +- res->flags = 0;
10803 ++
10804 ++ if (pci_is_bridge(fail_res->dev)) {
10805 ++ idx = res - &fail_res->dev->resource[0];
10806 ++ if (idx >= PCI_BRIDGE_RESOURCES &&
10807 ++ idx <= PCI_BRIDGE_RESOURCE_END)
10808 ++ res->flags = 0;
10809 ++ }
10810 + }
10811 + free_list(&fail_head);
10812 +
10813 +@@ -1895,12 +1901,18 @@ again:
10814 + /* restore size and flags */
10815 + list_for_each_entry(fail_res, &fail_head, list) {
10816 + struct resource *res = fail_res->res;
10817 ++ int idx;
10818 +
10819 + res->start = fail_res->start;
10820 + res->end = fail_res->end;
10821 + res->flags = fail_res->flags;
10822 +- if (fail_res->dev->subordinate)
10823 +- res->flags = 0;
10824 ++
10825 ++ if (pci_is_bridge(fail_res->dev)) {
10826 ++ idx = res - &fail_res->dev->resource[0];
10827 ++ if (idx >= PCI_BRIDGE_RESOURCES &&
10828 ++ idx <= PCI_BRIDGE_RESOURCE_END)
10829 ++ res->flags = 0;
10830 ++ }
10831 + }
10832 + free_list(&fail_head);
10833 +
10834 +diff --git a/drivers/pci/switch/switchtec.c b/drivers/pci/switch/switchtec.c
10835 +index 73dba2739849..bf229b442e72 100644
10836 +--- a/drivers/pci/switch/switchtec.c
10837 ++++ b/drivers/pci/switch/switchtec.c
10838 +@@ -1399,7 +1399,7 @@ static int switchtec_init_isr(struct switchtec_dev *stdev)
10839 + if (nvecs < 0)
10840 + return nvecs;
10841 +
10842 +- event_irq = ioread32(&stdev->mmio_part_cfg->vep_vector_number);
10843 ++ event_irq = ioread16(&stdev->mmio_part_cfg->vep_vector_number);
10844 + if (event_irq < 0 || event_irq >= nvecs)
10845 + return -EFAULT;
10846 +
10847 +diff --git a/drivers/phy/qualcomm/phy-qcom-apq8064-sata.c b/drivers/phy/qualcomm/phy-qcom-apq8064-sata.c
10848 +index 69ce2afac015..c6925e3e878b 100644
10849 +--- a/drivers/phy/qualcomm/phy-qcom-apq8064-sata.c
10850 ++++ b/drivers/phy/qualcomm/phy-qcom-apq8064-sata.c
10851 +@@ -88,7 +88,7 @@ static int read_poll_timeout(void __iomem *addr, u32 mask)
10852 + if (readl_relaxed(addr) & mask)
10853 + return 0;
10854 +
10855 +- usleep_range(DELAY_INTERVAL_US, DELAY_INTERVAL_US + 50);
10856 ++ usleep_range(DELAY_INTERVAL_US, DELAY_INTERVAL_US + 50);
10857 + } while (!time_after(jiffies, timeout));
10858 +
10859 + return (readl_relaxed(addr) & mask) ? 0 : -ETIMEDOUT;
10860 +diff --git a/drivers/pinctrl/sh-pfc/pfc-r8a7778.c b/drivers/pinctrl/sh-pfc/pfc-r8a7778.c
10861 +index c3af9ebee4af..28c0405ba396 100644
10862 +--- a/drivers/pinctrl/sh-pfc/pfc-r8a7778.c
10863 ++++ b/drivers/pinctrl/sh-pfc/pfc-r8a7778.c
10864 +@@ -2325,7 +2325,7 @@ static const struct pinmux_cfg_reg pinmux_config_regs[] = {
10865 + FN_ATAG0_A, 0, FN_REMOCON_B, 0,
10866 + /* IP0_11_8 [4] */
10867 + FN_SD1_DAT2_A, FN_MMC_D2, 0, FN_BS,
10868 +- FN_ATADIR0_A, 0, FN_SDSELF_B, 0,
10869 ++ FN_ATADIR0_A, 0, FN_SDSELF_A, 0,
10870 + FN_PWM4_B, 0, 0, 0,
10871 + 0, 0, 0, 0,
10872 + /* IP0_7_5 [3] */
10873 +@@ -2367,7 +2367,7 @@ static const struct pinmux_cfg_reg pinmux_config_regs[] = {
10874 + FN_TS_SDAT0_A, 0, 0, 0,
10875 + 0, 0, 0, 0,
10876 + /* IP1_10_8 [3] */
10877 +- FN_SD1_CLK_B, FN_MMC_D6, 0, FN_A24,
10878 ++ FN_SD1_CD_A, FN_MMC_D6, 0, FN_A24,
10879 + FN_DREQ1_A, 0, FN_HRX0_B, FN_TS_SPSYNC0_A,
10880 + /* IP1_7_5 [3] */
10881 + FN_A23, FN_HTX0_B, FN_TX2_B, FN_DACK2_A,
10882 +diff --git a/drivers/platform/x86/intel_mid_powerbtn.c b/drivers/platform/x86/intel_mid_powerbtn.c
10883 +index 5ad44204a9c3..10dbd6cac48a 100644
10884 +--- a/drivers/platform/x86/intel_mid_powerbtn.c
10885 ++++ b/drivers/platform/x86/intel_mid_powerbtn.c
10886 +@@ -158,9 +158,10 @@ static int mid_pb_probe(struct platform_device *pdev)
10887 +
10888 + input_set_capability(input, EV_KEY, KEY_POWER);
10889 +
10890 +- ddata = (struct mid_pb_ddata *)id->driver_data;
10891 ++ ddata = devm_kmemdup(&pdev->dev, (void *)id->driver_data,
10892 ++ sizeof(*ddata), GFP_KERNEL);
10893 + if (!ddata)
10894 +- return -ENODATA;
10895 ++ return -ENOMEM;
10896 +
10897 + ddata->dev = &pdev->dev;
10898 + ddata->irq = irq;
10899 +diff --git a/drivers/platform/x86/intel_scu_ipc.c b/drivers/platform/x86/intel_scu_ipc.c
10900 +index 2c85f75e32b0..2434ce8bead6 100644
10901 +--- a/drivers/platform/x86/intel_scu_ipc.c
10902 ++++ b/drivers/platform/x86/intel_scu_ipc.c
10903 +@@ -69,26 +69,22 @@
10904 + struct intel_scu_ipc_pdata_t {
10905 + u32 i2c_base;
10906 + u32 i2c_len;
10907 +- u8 irq_mode;
10908 + };
10909 +
10910 + static const struct intel_scu_ipc_pdata_t intel_scu_ipc_lincroft_pdata = {
10911 + .i2c_base = 0xff12b000,
10912 + .i2c_len = 0x10,
10913 +- .irq_mode = 0,
10914 + };
10915 +
10916 + /* Penwell and Cloverview */
10917 + static const struct intel_scu_ipc_pdata_t intel_scu_ipc_penwell_pdata = {
10918 + .i2c_base = 0xff12b000,
10919 + .i2c_len = 0x10,
10920 +- .irq_mode = 1,
10921 + };
10922 +
10923 + static const struct intel_scu_ipc_pdata_t intel_scu_ipc_tangier_pdata = {
10924 + .i2c_base = 0xff00d000,
10925 + .i2c_len = 0x10,
10926 +- .irq_mode = 0,
10927 + };
10928 +
10929 + struct intel_scu_ipc_dev {
10930 +@@ -101,6 +97,9 @@ struct intel_scu_ipc_dev {
10931 +
10932 + static struct intel_scu_ipc_dev ipcdev; /* Only one for now */
10933 +
10934 ++#define IPC_STATUS 0x04
10935 ++#define IPC_STATUS_IRQ BIT(2)
10936 ++
10937 + /*
10938 + * IPC Read Buffer (Read Only):
10939 + * 16 byte buffer for receiving data from SCU, if IPC command
10940 +@@ -122,11 +121,8 @@ static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */
10941 + */
10942 + static inline void ipc_command(struct intel_scu_ipc_dev *scu, u32 cmd)
10943 + {
10944 +- if (scu->irq_mode) {
10945 +- reinit_completion(&scu->cmd_complete);
10946 +- writel(cmd | IPC_IOC, scu->ipc_base);
10947 +- }
10948 +- writel(cmd, scu->ipc_base);
10949 ++ reinit_completion(&scu->cmd_complete);
10950 ++ writel(cmd | IPC_IOC, scu->ipc_base);
10951 + }
10952 +
10953 + /*
10954 +@@ -612,9 +608,10 @@ EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl);
10955 + static irqreturn_t ioc(int irq, void *dev_id)
10956 + {
10957 + struct intel_scu_ipc_dev *scu = dev_id;
10958 ++ int status = ipc_read_status(scu);
10959 +
10960 +- if (scu->irq_mode)
10961 +- complete(&scu->cmd_complete);
10962 ++ writel(status | IPC_STATUS_IRQ, scu->ipc_base + IPC_STATUS);
10963 ++ complete(&scu->cmd_complete);
10964 +
10965 + return IRQ_HANDLED;
10966 + }
10967 +@@ -640,8 +637,6 @@ static int ipc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
10968 + if (!pdata)
10969 + return -ENODEV;
10970 +
10971 +- scu->irq_mode = pdata->irq_mode;
10972 +-
10973 + err = pcim_enable_device(pdev);
10974 + if (err)
10975 + return err;
10976 +diff --git a/drivers/power/supply/ltc2941-battery-gauge.c b/drivers/power/supply/ltc2941-battery-gauge.c
10977 +index 9621d6dd88c6..50bdf2d5248b 100644
10978 +--- a/drivers/power/supply/ltc2941-battery-gauge.c
10979 ++++ b/drivers/power/supply/ltc2941-battery-gauge.c
10980 +@@ -406,7 +406,7 @@ static int ltc294x_i2c_remove(struct i2c_client *client)
10981 + {
10982 + struct ltc294x_info *info = i2c_get_clientdata(client);
10983 +
10984 +- cancel_delayed_work(&info->work);
10985 ++ cancel_delayed_work_sync(&info->work);
10986 + power_supply_unregister(info->supply);
10987 + return 0;
10988 + }
10989 +diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
10990 +index 9dca53df3584..5b7c16b85dc0 100644
10991 +--- a/drivers/rtc/rtc-cmos.c
10992 ++++ b/drivers/rtc/rtc-cmos.c
10993 +@@ -806,7 +806,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
10994 + rtc_cmos_int_handler = cmos_interrupt;
10995 +
10996 + retval = request_irq(rtc_irq, rtc_cmos_int_handler,
10997 +- IRQF_SHARED, dev_name(&cmos_rtc.rtc->dev),
10998 ++ 0, dev_name(&cmos_rtc.rtc->dev),
10999 + cmos_rtc.rtc);
11000 + if (retval < 0) {
11001 + dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
11002 +diff --git a/drivers/rtc/rtc-hym8563.c b/drivers/rtc/rtc-hym8563.c
11003 +index e5ad527cb75e..a8c2d38b2411 100644
11004 +--- a/drivers/rtc/rtc-hym8563.c
11005 ++++ b/drivers/rtc/rtc-hym8563.c
11006 +@@ -105,7 +105,7 @@ static int hym8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
11007 +
11008 + if (!hym8563->valid) {
11009 + dev_warn(&client->dev, "no valid clock/calendar values available\n");
11010 +- return -EPERM;
11011 ++ return -EINVAL;
11012 + }
11013 +
11014 + ret = i2c_smbus_read_i2c_block_data(client, HYM8563_SEC, 7, buf);
11015 +diff --git a/drivers/scsi/csiostor/csio_scsi.c b/drivers/scsi/csiostor/csio_scsi.c
11016 +index e09c7f360dbd..0cb585759de6 100644
11017 +--- a/drivers/scsi/csiostor/csio_scsi.c
11018 ++++ b/drivers/scsi/csiostor/csio_scsi.c
11019 +@@ -1383,7 +1383,7 @@ csio_device_reset(struct device *dev,
11020 + return -EINVAL;
11021 +
11022 + /* Delete NPIV lnodes */
11023 +- csio_lnodes_exit(hw, 1);
11024 ++ csio_lnodes_exit(hw, 1);
11025 +
11026 + /* Block upper IOs */
11027 + csio_lnodes_block_request(hw);
11028 +diff --git a/drivers/scsi/megaraid/megaraid_sas_base.c b/drivers/scsi/megaraid/megaraid_sas_base.c
11029 +index 6abad63b127a..42d876034741 100644
11030 +--- a/drivers/scsi/megaraid/megaraid_sas_base.c
11031 ++++ b/drivers/scsi/megaraid/megaraid_sas_base.c
11032 +@@ -4109,7 +4109,8 @@ dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
11033 + if (instance->adapter_type == MFI_SERIES)
11034 + return KILL_ADAPTER;
11035 + else if (instance->unload ||
11036 +- test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
11037 ++ test_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE,
11038 ++ &instance->reset_flags))
11039 + return IGNORE_TIMEOUT;
11040 + else
11041 + return INITIATE_OCR;
11042 +diff --git a/drivers/scsi/megaraid/megaraid_sas_fusion.c b/drivers/scsi/megaraid/megaraid_sas_fusion.c
11043 +index 7be2b9e11332..b13721290f4b 100644
11044 +--- a/drivers/scsi/megaraid/megaraid_sas_fusion.c
11045 ++++ b/drivers/scsi/megaraid/megaraid_sas_fusion.c
11046 +@@ -4212,6 +4212,7 @@ int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
11047 + if (instance->requestorId && !instance->skip_heartbeat_timer_del)
11048 + del_timer_sync(&instance->sriov_heartbeat_timer);
11049 + set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
11050 ++ set_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
11051 + atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
11052 + instance->instancet->disable_intr(instance);
11053 + megasas_sync_irqs((unsigned long)instance);
11054 +@@ -4399,7 +4400,7 @@ fail_kill_adapter:
11055 + atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
11056 + }
11057 + out:
11058 +- clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
11059 ++ clear_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
11060 + mutex_unlock(&instance->reset_mutex);
11061 + return retval;
11062 + }
11063 +diff --git a/drivers/scsi/megaraid/megaraid_sas_fusion.h b/drivers/scsi/megaraid/megaraid_sas_fusion.h
11064 +index 7c1f7ccf031d..40724df20780 100644
11065 +--- a/drivers/scsi/megaraid/megaraid_sas_fusion.h
11066 ++++ b/drivers/scsi/megaraid/megaraid_sas_fusion.h
11067 +@@ -100,6 +100,7 @@ enum MR_RAID_FLAGS_IO_SUB_TYPE {
11068 +
11069 + #define MEGASAS_FP_CMD_LEN 16
11070 + #define MEGASAS_FUSION_IN_RESET 0
11071 ++#define MEGASAS_FUSION_OCR_NOT_POSSIBLE 1
11072 + #define THRESHOLD_REPLY_COUNT 50
11073 + #define RAID_1_PEER_CMDS 2
11074 + #define JBOD_MAPS_COUNT 2
11075 +diff --git a/drivers/scsi/qla2xxx/qla_dbg.c b/drivers/scsi/qla2xxx/qla_dbg.c
11076 +index 3e9dc54b89a3..91e185731b1e 100644
11077 +--- a/drivers/scsi/qla2xxx/qla_dbg.c
11078 ++++ b/drivers/scsi/qla2xxx/qla_dbg.c
11079 +@@ -2517,12 +2517,6 @@ qla83xx_fw_dump_failed:
11080 + /* Driver Debug Functions. */
11081 + /****************************************************************************/
11082 +
11083 +-static inline int
11084 +-ql_mask_match(uint32_t level)
11085 +-{
11086 +- return (level & ql2xextended_error_logging) == level;
11087 +-}
11088 +-
11089 + /*
11090 + * This function is for formatting and logging debug information.
11091 + * It is to be used when vha is available. It formats the message
11092 +diff --git a/drivers/scsi/qla2xxx/qla_dbg.h b/drivers/scsi/qla2xxx/qla_dbg.h
11093 +index 8877aa97d829..ceca6dd34db1 100644
11094 +--- a/drivers/scsi/qla2xxx/qla_dbg.h
11095 ++++ b/drivers/scsi/qla2xxx/qla_dbg.h
11096 +@@ -374,3 +374,9 @@ extern int qla24xx_dump_ram(struct qla_hw_data *, uint32_t, uint32_t *,
11097 + extern void qla24xx_pause_risc(struct device_reg_24xx __iomem *,
11098 + struct qla_hw_data *);
11099 + extern int qla24xx_soft_reset(struct qla_hw_data *);
11100 ++
11101 ++static inline int
11102 ++ql_mask_match(uint level)
11103 ++{
11104 ++ return (level & ql2xextended_error_logging) == level;
11105 ++}
11106 +diff --git a/drivers/scsi/qla2xxx/qla_isr.c b/drivers/scsi/qla2xxx/qla_isr.c
11107 +index 648916a9082c..b39faf2bfa0d 100644
11108 +--- a/drivers/scsi/qla2xxx/qla_isr.c
11109 ++++ b/drivers/scsi/qla2xxx/qla_isr.c
11110 +@@ -1853,6 +1853,18 @@ qla24xx_nvme_iocb_entry(scsi_qla_host_t *vha, struct req_que *req, void *tsk)
11111 + inbuf = (uint32_t *)&sts->nvme_ersp_data;
11112 + outbuf = (uint32_t *)fd->rspaddr;
11113 + iocb->u.nvme.rsp_pyld_len = le16_to_cpu(sts->nvme_rsp_pyld_len);
11114 ++ if (unlikely(iocb->u.nvme.rsp_pyld_len >
11115 ++ sizeof(struct nvme_fc_ersp_iu))) {
11116 ++ if (ql_mask_match(ql_dbg_io)) {
11117 ++ WARN_ONCE(1, "Unexpected response payload length %u.\n",
11118 ++ iocb->u.nvme.rsp_pyld_len);
11119 ++ ql_log(ql_log_warn, fcport->vha, 0x5100,
11120 ++ "Unexpected response payload length %u.\n",
11121 ++ iocb->u.nvme.rsp_pyld_len);
11122 ++ }
11123 ++ iocb->u.nvme.rsp_pyld_len =
11124 ++ sizeof(struct nvme_fc_ersp_iu);
11125 ++ }
11126 + iter = iocb->u.nvme.rsp_pyld_len >> 2;
11127 + for (; iter; iter--)
11128 + *outbuf++ = swab32(*inbuf++);
11129 +diff --git a/drivers/scsi/qla2xxx/qla_mbx.c b/drivers/scsi/qla2xxx/qla_mbx.c
11130 +index 459481ce5872..5e8ae510aef8 100644
11131 +--- a/drivers/scsi/qla2xxx/qla_mbx.c
11132 ++++ b/drivers/scsi/qla2xxx/qla_mbx.c
11133 +@@ -5853,9 +5853,8 @@ qla2x00_dump_mctp_data(scsi_qla_host_t *vha, dma_addr_t req_dma, uint32_t addr,
11134 + mcp->mb[7] = LSW(MSD(req_dma));
11135 + mcp->mb[8] = MSW(addr);
11136 + /* Setting RAM ID to valid */
11137 +- mcp->mb[10] |= BIT_7;
11138 + /* For MCTP RAM ID is 0x40 */
11139 +- mcp->mb[10] |= 0x40;
11140 ++ mcp->mb[10] = BIT_7 | 0x40;
11141 +
11142 + mcp->out_mb |= MBX_10|MBX_8|MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|
11143 + MBX_0;
11144 +diff --git a/drivers/scsi/qla2xxx/qla_nx.c b/drivers/scsi/qla2xxx/qla_nx.c
11145 +index a77c33987703..a5b8313cf491 100644
11146 +--- a/drivers/scsi/qla2xxx/qla_nx.c
11147 ++++ b/drivers/scsi/qla2xxx/qla_nx.c
11148 +@@ -1605,8 +1605,7 @@ qla82xx_get_bootld_offset(struct qla_hw_data *ha)
11149 + return (u8 *)&ha->hablob->fw->data[offset];
11150 + }
11151 +
11152 +-static __le32
11153 +-qla82xx_get_fw_size(struct qla_hw_data *ha)
11154 ++static u32 qla82xx_get_fw_size(struct qla_hw_data *ha)
11155 + {
11156 + struct qla82xx_uri_data_desc *uri_desc = NULL;
11157 +
11158 +@@ -1617,7 +1616,7 @@ qla82xx_get_fw_size(struct qla_hw_data *ha)
11159 + return cpu_to_le32(uri_desc->size);
11160 + }
11161 +
11162 +- return cpu_to_le32(*(u32 *)&ha->hablob->fw->data[FW_SIZE_OFFSET]);
11163 ++ return get_unaligned_le32(&ha->hablob->fw->data[FW_SIZE_OFFSET]);
11164 + }
11165 +
11166 + static u8 *
11167 +@@ -1808,7 +1807,7 @@ qla82xx_fw_load_from_blob(struct qla_hw_data *ha)
11168 + }
11169 +
11170 + flashaddr = FLASH_ADDR_START;
11171 +- size = (__force u32)qla82xx_get_fw_size(ha) / 8;
11172 ++ size = qla82xx_get_fw_size(ha) / 8;
11173 + ptr64 = (u64 *)qla82xx_get_fw_offs(ha);
11174 +
11175 + for (i = 0; i < size; i++) {
11176 +diff --git a/drivers/scsi/qla4xxx/ql4_os.c b/drivers/scsi/qla4xxx/ql4_os.c
11177 +index b0ad60565fe9..fb3abaf817a3 100644
11178 +--- a/drivers/scsi/qla4xxx/ql4_os.c
11179 ++++ b/drivers/scsi/qla4xxx/ql4_os.c
11180 +@@ -4150,7 +4150,7 @@ static void qla4xxx_mem_free(struct scsi_qla_host *ha)
11181 + dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
11182 + ha->queues_dma);
11183 +
11184 +- if (ha->fw_dump)
11185 ++ if (ha->fw_dump)
11186 + vfree(ha->fw_dump);
11187 +
11188 + ha->queues_len = 0;
11189 +diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
11190 +index d25082e573e0..ce40de334f11 100644
11191 +--- a/drivers/scsi/ufs/ufshcd.c
11192 ++++ b/drivers/scsi/ufs/ufshcd.c
11193 +@@ -4812,6 +4812,7 @@ static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
11194 +
11195 + hba->auto_bkops_enabled = false;
11196 + trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
11197 ++ hba->is_urgent_bkops_lvl_checked = false;
11198 + out:
11199 + return err;
11200 + }
11201 +@@ -4836,6 +4837,7 @@ static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
11202 + hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
11203 + ufshcd_disable_auto_bkops(hba);
11204 + }
11205 ++ hba->is_urgent_bkops_lvl_checked = false;
11206 + }
11207 +
11208 + static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
11209 +@@ -4882,6 +4884,7 @@ static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
11210 + err = ufshcd_enable_auto_bkops(hba);
11211 + else
11212 + err = ufshcd_disable_auto_bkops(hba);
11213 ++ hba->urgent_bkops_lvl = curr_status;
11214 + out:
11215 + return err;
11216 + }
11217 +@@ -6412,7 +6415,8 @@ static int ufshcd_probe_hba(struct ufs_hba *hba)
11218 + ufshcd_init_icc_levels(hba);
11219 +
11220 + /* Add required well known logical units to scsi mid layer */
11221 +- if (ufshcd_scsi_add_wlus(hba))
11222 ++ ret = ufshcd_scsi_add_wlus(hba);
11223 ++ if (ret)
11224 + goto out;
11225 +
11226 + /* Initialize devfreq after UFS device is detected */
11227 +diff --git a/drivers/tty/serial/xilinx_uartps.c b/drivers/tty/serial/xilinx_uartps.c
11228 +index 0dbfd02e3b19..81657f09761c 100644
11229 +--- a/drivers/tty/serial/xilinx_uartps.c
11230 ++++ b/drivers/tty/serial/xilinx_uartps.c
11231 +@@ -31,6 +31,7 @@
11232 + #include <linux/of.h>
11233 + #include <linux/module.h>
11234 + #include <linux/pm_runtime.h>
11235 ++#include <linux/iopoll.h>
11236 +
11237 + #define CDNS_UART_TTY_NAME "ttyPS"
11238 + #define CDNS_UART_NAME "xuartps"
11239 +@@ -39,6 +40,7 @@
11240 + #define CDNS_UART_NR_PORTS 2
11241 + #define CDNS_UART_FIFO_SIZE 64 /* FIFO size */
11242 + #define CDNS_UART_REGISTER_SPACE 0x1000
11243 ++#define TX_TIMEOUT 500000
11244 +
11245 + /* Rx Trigger level */
11246 + static int rx_trigger_level = 56;
11247 +@@ -685,18 +687,21 @@ static void cdns_uart_set_termios(struct uart_port *port,
11248 + unsigned int cval = 0;
11249 + unsigned int baud, minbaud, maxbaud;
11250 + unsigned long flags;
11251 +- unsigned int ctrl_reg, mode_reg;
11252 +-
11253 +- spin_lock_irqsave(&port->lock, flags);
11254 ++ unsigned int ctrl_reg, mode_reg, val;
11255 ++ int err;
11256 +
11257 + /* Wait for the transmit FIFO to empty before making changes */
11258 + if (!(readl(port->membase + CDNS_UART_CR) &
11259 + CDNS_UART_CR_TX_DIS)) {
11260 +- while (!(readl(port->membase + CDNS_UART_SR) &
11261 +- CDNS_UART_SR_TXEMPTY)) {
11262 +- cpu_relax();
11263 ++ err = readl_poll_timeout(port->membase + CDNS_UART_SR,
11264 ++ val, (val & CDNS_UART_SR_TXEMPTY),
11265 ++ 1000, TX_TIMEOUT);
11266 ++ if (err) {
11267 ++ dev_err(port->dev, "timed out waiting for tx empty");
11268 ++ return;
11269 + }
11270 + }
11271 ++ spin_lock_irqsave(&port->lock, flags);
11272 +
11273 + /* Disable the TX and RX to set baud rate */
11274 + ctrl_reg = readl(port->membase + CDNS_UART_CR);
11275 +diff --git a/drivers/usb/gadget/function/f_ecm.c b/drivers/usb/gadget/function/f_ecm.c
11276 +index dc99ed94f03d..8e3e44382785 100644
11277 +--- a/drivers/usb/gadget/function/f_ecm.c
11278 ++++ b/drivers/usb/gadget/function/f_ecm.c
11279 +@@ -56,6 +56,7 @@ struct f_ecm {
11280 + struct usb_ep *notify;
11281 + struct usb_request *notify_req;
11282 + u8 notify_state;
11283 ++ atomic_t notify_count;
11284 + bool is_open;
11285 +
11286 + /* FIXME is_open needs some irq-ish locking
11287 +@@ -384,7 +385,7 @@ static void ecm_do_notify(struct f_ecm *ecm)
11288 + int status;
11289 +
11290 + /* notification already in flight? */
11291 +- if (!req)
11292 ++ if (atomic_read(&ecm->notify_count))
11293 + return;
11294 +
11295 + event = req->buf;
11296 +@@ -424,10 +425,10 @@ static void ecm_do_notify(struct f_ecm *ecm)
11297 + event->bmRequestType = 0xA1;
11298 + event->wIndex = cpu_to_le16(ecm->ctrl_id);
11299 +
11300 +- ecm->notify_req = NULL;
11301 ++ atomic_inc(&ecm->notify_count);
11302 + status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
11303 + if (status < 0) {
11304 +- ecm->notify_req = req;
11305 ++ atomic_dec(&ecm->notify_count);
11306 + DBG(cdev, "notify --> %d\n", status);
11307 + }
11308 + }
11309 +@@ -452,17 +453,19 @@ static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
11310 + switch (req->status) {
11311 + case 0:
11312 + /* no fault */
11313 ++ atomic_dec(&ecm->notify_count);
11314 + break;
11315 + case -ECONNRESET:
11316 + case -ESHUTDOWN:
11317 ++ atomic_set(&ecm->notify_count, 0);
11318 + ecm->notify_state = ECM_NOTIFY_NONE;
11319 + break;
11320 + default:
11321 + DBG(cdev, "event %02x --> %d\n",
11322 + event->bNotificationType, req->status);
11323 ++ atomic_dec(&ecm->notify_count);
11324 + break;
11325 + }
11326 +- ecm->notify_req = req;
11327 + ecm_do_notify(ecm);
11328 + }
11329 +
11330 +@@ -909,6 +912,11 @@ static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
11331 +
11332 + usb_free_all_descriptors(f);
11333 +
11334 ++ if (atomic_read(&ecm->notify_count)) {
11335 ++ usb_ep_dequeue(ecm->notify, ecm->notify_req);
11336 ++ atomic_set(&ecm->notify_count, 0);
11337 ++ }
11338 ++
11339 + kfree(ecm->notify_req->buf);
11340 + usb_ep_free_request(ecm->notify, ecm->notify_req);
11341 + }
11342 +diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c
11343 +index 45b334ceaf2e..5c2d39232bb0 100644
11344 +--- a/drivers/usb/gadget/function/f_ncm.c
11345 ++++ b/drivers/usb/gadget/function/f_ncm.c
11346 +@@ -58,6 +58,7 @@ struct f_ncm {
11347 + struct usb_ep *notify;
11348 + struct usb_request *notify_req;
11349 + u8 notify_state;
11350 ++ atomic_t notify_count;
11351 + bool is_open;
11352 +
11353 + const struct ndp_parser_opts *parser_opts;
11354 +@@ -553,7 +554,7 @@ static void ncm_do_notify(struct f_ncm *ncm)
11355 + int status;
11356 +
11357 + /* notification already in flight? */
11358 +- if (!req)
11359 ++ if (atomic_read(&ncm->notify_count))
11360 + return;
11361 +
11362 + event = req->buf;
11363 +@@ -593,7 +594,8 @@ static void ncm_do_notify(struct f_ncm *ncm)
11364 + event->bmRequestType = 0xA1;
11365 + event->wIndex = cpu_to_le16(ncm->ctrl_id);
11366 +
11367 +- ncm->notify_req = NULL;
11368 ++ atomic_inc(&ncm->notify_count);
11369 ++
11370 + /*
11371 + * In double buffering if there is a space in FIFO,
11372 + * completion callback can be called right after the call,
11373 +@@ -603,7 +605,7 @@ static void ncm_do_notify(struct f_ncm *ncm)
11374 + status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
11375 + spin_lock(&ncm->lock);
11376 + if (status < 0) {
11377 +- ncm->notify_req = req;
11378 ++ atomic_dec(&ncm->notify_count);
11379 + DBG(cdev, "notify --> %d\n", status);
11380 + }
11381 + }
11382 +@@ -638,17 +640,19 @@ static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
11383 + case 0:
11384 + VDBG(cdev, "Notification %02x sent\n",
11385 + event->bNotificationType);
11386 ++ atomic_dec(&ncm->notify_count);
11387 + break;
11388 + case -ECONNRESET:
11389 + case -ESHUTDOWN:
11390 ++ atomic_set(&ncm->notify_count, 0);
11391 + ncm->notify_state = NCM_NOTIFY_NONE;
11392 + break;
11393 + default:
11394 + DBG(cdev, "event %02x --> %d\n",
11395 + event->bNotificationType, req->status);
11396 ++ atomic_dec(&ncm->notify_count);
11397 + break;
11398 + }
11399 +- ncm->notify_req = req;
11400 + ncm_do_notify(ncm);
11401 + spin_unlock(&ncm->lock);
11402 + }
11403 +@@ -1632,6 +1636,11 @@ static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
11404 + ncm_string_defs[0].id = 0;
11405 + usb_free_all_descriptors(f);
11406 +
11407 ++ if (atomic_read(&ncm->notify_count)) {
11408 ++ usb_ep_dequeue(ncm->notify, ncm->notify_req);
11409 ++ atomic_set(&ncm->notify_count, 0);
11410 ++ }
11411 ++
11412 + kfree(ncm->notify_req->buf);
11413 + usb_ep_free_request(ncm->notify, ncm->notify_req);
11414 + }
11415 +diff --git a/drivers/usb/gadget/legacy/cdc2.c b/drivers/usb/gadget/legacy/cdc2.c
11416 +index 51c08682de84..5ee25beb52f0 100644
11417 +--- a/drivers/usb/gadget/legacy/cdc2.c
11418 ++++ b/drivers/usb/gadget/legacy/cdc2.c
11419 +@@ -229,7 +229,7 @@ static struct usb_composite_driver cdc_driver = {
11420 + .name = "g_cdc",
11421 + .dev = &device_desc,
11422 + .strings = dev_strings,
11423 +- .max_speed = USB_SPEED_HIGH,
11424 ++ .max_speed = USB_SPEED_SUPER,
11425 + .bind = cdc_bind,
11426 + .unbind = cdc_unbind,
11427 + };
11428 +diff --git a/drivers/usb/gadget/legacy/g_ffs.c b/drivers/usb/gadget/legacy/g_ffs.c
11429 +index 6da7316f8e87..54ee4e31645b 100644
11430 +--- a/drivers/usb/gadget/legacy/g_ffs.c
11431 ++++ b/drivers/usb/gadget/legacy/g_ffs.c
11432 +@@ -153,7 +153,7 @@ static struct usb_composite_driver gfs_driver = {
11433 + .name = DRIVER_NAME,
11434 + .dev = &gfs_dev_desc,
11435 + .strings = gfs_dev_strings,
11436 +- .max_speed = USB_SPEED_HIGH,
11437 ++ .max_speed = USB_SPEED_SUPER,
11438 + .bind = gfs_bind,
11439 + .unbind = gfs_unbind,
11440 + };
11441 +diff --git a/drivers/usb/gadget/legacy/multi.c b/drivers/usb/gadget/legacy/multi.c
11442 +index a70a406580ea..3b7fc5c7e9c3 100644
11443 +--- a/drivers/usb/gadget/legacy/multi.c
11444 ++++ b/drivers/usb/gadget/legacy/multi.c
11445 +@@ -486,7 +486,7 @@ static struct usb_composite_driver multi_driver = {
11446 + .name = "g_multi",
11447 + .dev = &device_desc,
11448 + .strings = dev_strings,
11449 +- .max_speed = USB_SPEED_HIGH,
11450 ++ .max_speed = USB_SPEED_SUPER,
11451 + .bind = multi_bind,
11452 + .unbind = multi_unbind,
11453 + .needs_serial = 1,
11454 +diff --git a/drivers/usb/gadget/legacy/ncm.c b/drivers/usb/gadget/legacy/ncm.c
11455 +index 0aba68253e3d..2fb4a847dd52 100644
11456 +--- a/drivers/usb/gadget/legacy/ncm.c
11457 ++++ b/drivers/usb/gadget/legacy/ncm.c
11458 +@@ -203,7 +203,7 @@ static struct usb_composite_driver ncm_driver = {
11459 + .name = "g_ncm",
11460 + .dev = &device_desc,
11461 + .strings = dev_strings,
11462 +- .max_speed = USB_SPEED_HIGH,
11463 ++ .max_speed = USB_SPEED_SUPER,
11464 + .bind = gncm_bind,
11465 + .unbind = gncm_unbind,
11466 + };
11467 +diff --git a/drivers/xen/xen-balloon.c b/drivers/xen/xen-balloon.c
11468 +index cf8ef8cee5a0..112e8b5e6fee 100644
11469 +--- a/drivers/xen/xen-balloon.c
11470 ++++ b/drivers/xen/xen-balloon.c
11471 +@@ -82,7 +82,7 @@ static void watch_target(struct xenbus_watch *watch,
11472 + "%llu", &static_max) == 1))
11473 + static_max >>= PAGE_SHIFT - 10;
11474 + else
11475 +- static_max = new_target;
11476 ++ static_max = balloon_stats.current_pages;
11477 +
11478 + target_diff = (xen_pv_domain() || xen_initial_domain()) ? 0
11479 + : static_max - balloon_stats.target_pages;
11480 +diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
11481 +index 740ef428acdd..f5a8c0d26cf3 100644
11482 +--- a/fs/btrfs/ctree.c
11483 ++++ b/fs/btrfs/ctree.c
11484 +@@ -334,26 +334,6 @@ struct tree_mod_elem {
11485 + struct tree_mod_root old_root;
11486 + };
11487 +
11488 +-static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
11489 +-{
11490 +- read_lock(&fs_info->tree_mod_log_lock);
11491 +-}
11492 +-
11493 +-static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
11494 +-{
11495 +- read_unlock(&fs_info->tree_mod_log_lock);
11496 +-}
11497 +-
11498 +-static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
11499 +-{
11500 +- write_lock(&fs_info->tree_mod_log_lock);
11501 +-}
11502 +-
11503 +-static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
11504 +-{
11505 +- write_unlock(&fs_info->tree_mod_log_lock);
11506 +-}
11507 +-
11508 + /*
11509 + * Pull a new tree mod seq number for our operation.
11510 + */
11511 +@@ -373,14 +353,12 @@ static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
11512 + u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
11513 + struct seq_list *elem)
11514 + {
11515 +- tree_mod_log_write_lock(fs_info);
11516 +- spin_lock(&fs_info->tree_mod_seq_lock);
11517 ++ write_lock(&fs_info->tree_mod_log_lock);
11518 + if (!elem->seq) {
11519 + elem->seq = btrfs_inc_tree_mod_seq(fs_info);
11520 + list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
11521 + }
11522 +- spin_unlock(&fs_info->tree_mod_seq_lock);
11523 +- tree_mod_log_write_unlock(fs_info);
11524 ++ write_unlock(&fs_info->tree_mod_log_lock);
11525 +
11526 + return elem->seq;
11527 + }
11528 +@@ -399,7 +377,7 @@ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
11529 + if (!seq_putting)
11530 + return;
11531 +
11532 +- spin_lock(&fs_info->tree_mod_seq_lock);
11533 ++ write_lock(&fs_info->tree_mod_log_lock);
11534 + list_del(&elem->list);
11535 + elem->seq = 0;
11536 +
11537 +@@ -410,19 +388,17 @@ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
11538 + * blocker with lower sequence number exists, we
11539 + * cannot remove anything from the log
11540 + */
11541 +- spin_unlock(&fs_info->tree_mod_seq_lock);
11542 ++ write_unlock(&fs_info->tree_mod_log_lock);
11543 + return;
11544 + }
11545 + min_seq = cur_elem->seq;
11546 + }
11547 + }
11548 +- spin_unlock(&fs_info->tree_mod_seq_lock);
11549 +
11550 + /*
11551 + * anything that's lower than the lowest existing (read: blocked)
11552 + * sequence number can be removed from the tree.
11553 + */
11554 +- tree_mod_log_write_lock(fs_info);
11555 + tm_root = &fs_info->tree_mod_log;
11556 + for (node = rb_first(tm_root); node; node = next) {
11557 + next = rb_next(node);
11558 +@@ -432,7 +408,7 @@ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
11559 + rb_erase(node, tm_root);
11560 + kfree(tm);
11561 + }
11562 +- tree_mod_log_write_unlock(fs_info);
11563 ++ write_unlock(&fs_info->tree_mod_log_lock);
11564 + }
11565 +
11566 + /*
11567 +@@ -443,7 +419,7 @@ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
11568 + * for root replace operations, or the logical address of the affected
11569 + * block for all other operations.
11570 + *
11571 +- * Note: must be called with write lock (tree_mod_log_write_lock).
11572 ++ * Note: must be called with write lock for fs_info::tree_mod_log_lock.
11573 + */
11574 + static noinline int
11575 + __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
11576 +@@ -481,7 +457,7 @@ __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
11577 + * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
11578 + * returns zero with the tree_mod_log_lock acquired. The caller must hold
11579 + * this until all tree mod log insertions are recorded in the rb tree and then
11580 +- * call tree_mod_log_write_unlock() to release.
11581 ++ * write unlock fs_info::tree_mod_log_lock.
11582 + */
11583 + static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
11584 + struct extent_buffer *eb) {
11585 +@@ -491,9 +467,9 @@ static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
11586 + if (eb && btrfs_header_level(eb) == 0)
11587 + return 1;
11588 +
11589 +- tree_mod_log_write_lock(fs_info);
11590 ++ write_lock(&fs_info->tree_mod_log_lock);
11591 + if (list_empty(&(fs_info)->tree_mod_seq_list)) {
11592 +- tree_mod_log_write_unlock(fs_info);
11593 ++ write_unlock(&fs_info->tree_mod_log_lock);
11594 + return 1;
11595 + }
11596 +
11597 +@@ -557,7 +533,7 @@ tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
11598 + }
11599 +
11600 + ret = __tree_mod_log_insert(fs_info, tm);
11601 +- tree_mod_log_write_unlock(fs_info);
11602 ++ write_unlock(&eb->fs_info->tree_mod_log_lock);
11603 + if (ret)
11604 + kfree(tm);
11605 +
11606 +@@ -621,7 +597,7 @@ tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
11607 + ret = __tree_mod_log_insert(fs_info, tm);
11608 + if (ret)
11609 + goto free_tms;
11610 +- tree_mod_log_write_unlock(fs_info);
11611 ++ write_unlock(&eb->fs_info->tree_mod_log_lock);
11612 + kfree(tm_list);
11613 +
11614 + return 0;
11615 +@@ -632,7 +608,7 @@ free_tms:
11616 + kfree(tm_list[i]);
11617 + }
11618 + if (locked)
11619 +- tree_mod_log_write_unlock(fs_info);
11620 ++ write_unlock(&eb->fs_info->tree_mod_log_lock);
11621 + kfree(tm_list);
11622 + kfree(tm);
11623 +
11624 +@@ -713,7 +689,7 @@ tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
11625 + if (!ret)
11626 + ret = __tree_mod_log_insert(fs_info, tm);
11627 +
11628 +- tree_mod_log_write_unlock(fs_info);
11629 ++ write_unlock(&fs_info->tree_mod_log_lock);
11630 + if (ret)
11631 + goto free_tms;
11632 + kfree(tm_list);
11633 +@@ -740,7 +716,7 @@ __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
11634 + struct tree_mod_elem *cur = NULL;
11635 + struct tree_mod_elem *found = NULL;
11636 +
11637 +- tree_mod_log_read_lock(fs_info);
11638 ++ read_lock(&fs_info->tree_mod_log_lock);
11639 + tm_root = &fs_info->tree_mod_log;
11640 + node = tm_root->rb_node;
11641 + while (node) {
11642 +@@ -768,7 +744,7 @@ __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
11643 + break;
11644 + }
11645 + }
11646 +- tree_mod_log_read_unlock(fs_info);
11647 ++ read_unlock(&fs_info->tree_mod_log_lock);
11648 +
11649 + return found;
11650 + }
11651 +@@ -849,7 +825,7 @@ tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
11652 + goto free_tms;
11653 + }
11654 +
11655 +- tree_mod_log_write_unlock(fs_info);
11656 ++ write_unlock(&fs_info->tree_mod_log_lock);
11657 + kfree(tm_list);
11658 +
11659 + return 0;
11660 +@@ -861,7 +837,7 @@ free_tms:
11661 + kfree(tm_list[i]);
11662 + }
11663 + if (locked)
11664 +- tree_mod_log_write_unlock(fs_info);
11665 ++ write_unlock(&fs_info->tree_mod_log_lock);
11666 + kfree(tm_list);
11667 +
11668 + return ret;
11669 +@@ -921,7 +897,7 @@ tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
11670 + goto free_tms;
11671 +
11672 + ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
11673 +- tree_mod_log_write_unlock(fs_info);
11674 ++ write_unlock(&eb->fs_info->tree_mod_log_lock);
11675 + if (ret)
11676 + goto free_tms;
11677 + kfree(tm_list);
11678 +@@ -1279,7 +1255,7 @@ __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
11679 + unsigned long p_size = sizeof(struct btrfs_key_ptr);
11680 +
11681 + n = btrfs_header_nritems(eb);
11682 +- tree_mod_log_read_lock(fs_info);
11683 ++ read_lock(&fs_info->tree_mod_log_lock);
11684 + while (tm && tm->seq >= time_seq) {
11685 + /*
11686 + * all the operations are recorded with the operator used for
11687 +@@ -1334,7 +1310,7 @@ __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
11688 + if (tm->logical != first_tm->logical)
11689 + break;
11690 + }
11691 +- tree_mod_log_read_unlock(fs_info);
11692 ++ read_unlock(&fs_info->tree_mod_log_lock);
11693 + btrfs_set_header_nritems(eb, n);
11694 + }
11695 +
11696 +diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
11697 +index 588760c49fe2..5412b12491cb 100644
11698 +--- a/fs/btrfs/ctree.h
11699 ++++ b/fs/btrfs/ctree.h
11700 +@@ -869,14 +869,12 @@ struct btrfs_fs_info {
11701 + struct list_head delayed_iputs;
11702 + struct mutex cleaner_delayed_iput_mutex;
11703 +
11704 +- /* this protects tree_mod_seq_list */
11705 +- spinlock_t tree_mod_seq_lock;
11706 + atomic64_t tree_mod_seq;
11707 +- struct list_head tree_mod_seq_list;
11708 +
11709 +- /* this protects tree_mod_log */
11710 ++ /* this protects tree_mod_log and tree_mod_seq_list */
11711 + rwlock_t tree_mod_log_lock;
11712 + struct rb_root tree_mod_log;
11713 ++ struct list_head tree_mod_seq_list;
11714 +
11715 + atomic_t nr_async_submits;
11716 + atomic_t async_submit_draining;
11717 +@@ -2408,32 +2406,6 @@ static inline u32 btrfs_file_extent_inline_item_len(
11718 + return btrfs_item_size(eb, e) - BTRFS_FILE_EXTENT_INLINE_DATA_START;
11719 + }
11720 +
11721 +-/* this returns the number of file bytes represented by the inline item.
11722 +- * If an item is compressed, this is the uncompressed size
11723 +- */
11724 +-static inline u32 btrfs_file_extent_inline_len(const struct extent_buffer *eb,
11725 +- int slot,
11726 +- const struct btrfs_file_extent_item *fi)
11727 +-{
11728 +- struct btrfs_map_token token;
11729 +-
11730 +- btrfs_init_map_token(&token);
11731 +- /*
11732 +- * return the space used on disk if this item isn't
11733 +- * compressed or encoded
11734 +- */
11735 +- if (btrfs_token_file_extent_compression(eb, fi, &token) == 0 &&
11736 +- btrfs_token_file_extent_encryption(eb, fi, &token) == 0 &&
11737 +- btrfs_token_file_extent_other_encoding(eb, fi, &token) == 0) {
11738 +- return btrfs_file_extent_inline_item_len(eb,
11739 +- btrfs_item_nr(slot));
11740 +- }
11741 +-
11742 +- /* otherwise use the ram bytes field */
11743 +- return btrfs_token_file_extent_ram_bytes(eb, fi, &token);
11744 +-}
11745 +-
11746 +-
11747 + /* btrfs_dev_stats_item */
11748 + static inline u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
11749 + const struct btrfs_dev_stats_item *ptr,
11750 +diff --git a/fs/btrfs/delayed-ref.c b/fs/btrfs/delayed-ref.c
11751 +index d56bd3625468..45714f1c43a3 100644
11752 +--- a/fs/btrfs/delayed-ref.c
11753 ++++ b/fs/btrfs/delayed-ref.c
11754 +@@ -281,7 +281,7 @@ void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
11755 + if (head->is_data)
11756 + return;
11757 +
11758 +- spin_lock(&fs_info->tree_mod_seq_lock);
11759 ++ read_lock(&fs_info->tree_mod_log_lock);
11760 + if (!list_empty(&fs_info->tree_mod_seq_list)) {
11761 + struct seq_list *elem;
11762 +
11763 +@@ -289,7 +289,7 @@ void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
11764 + struct seq_list, list);
11765 + seq = elem->seq;
11766 + }
11767 +- spin_unlock(&fs_info->tree_mod_seq_lock);
11768 ++ read_unlock(&fs_info->tree_mod_log_lock);
11769 +
11770 + ref = list_first_entry(&head->ref_list, struct btrfs_delayed_ref_node,
11771 + list);
11772 +@@ -317,7 +317,7 @@ int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
11773 + struct seq_list *elem;
11774 + int ret = 0;
11775 +
11776 +- spin_lock(&fs_info->tree_mod_seq_lock);
11777 ++ read_lock(&fs_info->tree_mod_log_lock);
11778 + if (!list_empty(&fs_info->tree_mod_seq_list)) {
11779 + elem = list_first_entry(&fs_info->tree_mod_seq_list,
11780 + struct seq_list, list);
11781 +@@ -331,7 +331,7 @@ int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
11782 + }
11783 + }
11784 +
11785 +- spin_unlock(&fs_info->tree_mod_seq_lock);
11786 ++ read_unlock(&fs_info->tree_mod_log_lock);
11787 + return ret;
11788 + }
11789 +
11790 +diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
11791 +index a8ea56218d6b..44b15617c7b9 100644
11792 +--- a/fs/btrfs/disk-io.c
11793 ++++ b/fs/btrfs/disk-io.c
11794 +@@ -2051,7 +2051,7 @@ static void free_root_extent_buffers(struct btrfs_root *root)
11795 + }
11796 +
11797 + /* helper to cleanup tree roots */
11798 +-static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root)
11799 ++static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root)
11800 + {
11801 + free_root_extent_buffers(info->tree_root);
11802 +
11803 +@@ -2060,7 +2060,7 @@ static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root)
11804 + free_root_extent_buffers(info->csum_root);
11805 + free_root_extent_buffers(info->quota_root);
11806 + free_root_extent_buffers(info->uuid_root);
11807 +- if (chunk_root)
11808 ++ if (free_chunk_root)
11809 + free_root_extent_buffers(info->chunk_root);
11810 + free_root_extent_buffers(info->free_space_root);
11811 + }
11812 +@@ -2455,7 +2455,6 @@ int open_ctree(struct super_block *sb,
11813 + spin_lock_init(&fs_info->fs_roots_radix_lock);
11814 + spin_lock_init(&fs_info->delayed_iput_lock);
11815 + spin_lock_init(&fs_info->defrag_inodes_lock);
11816 +- spin_lock_init(&fs_info->tree_mod_seq_lock);
11817 + spin_lock_init(&fs_info->super_lock);
11818 + spin_lock_init(&fs_info->qgroup_op_lock);
11819 + spin_lock_init(&fs_info->buffer_lock);
11820 +@@ -3069,7 +3068,7 @@ fail_block_groups:
11821 + btrfs_put_block_group_cache(fs_info);
11822 +
11823 + fail_tree_roots:
11824 +- free_root_pointers(fs_info, 1);
11825 ++ free_root_pointers(fs_info, true);
11826 + invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
11827 +
11828 + fail_sb_buffer:
11829 +@@ -3097,7 +3096,7 @@ recovery_tree_root:
11830 + if (!btrfs_test_opt(fs_info, USEBACKUPROOT))
11831 + goto fail_tree_roots;
11832 +
11833 +- free_root_pointers(fs_info, 0);
11834 ++ free_root_pointers(fs_info, false);
11835 +
11836 + /* don't use the log in recovery mode, it won't be valid */
11837 + btrfs_set_super_log_root(disk_super, 0);
11838 +@@ -3761,10 +3760,17 @@ void close_ctree(struct btrfs_fs_info *fs_info)
11839 + invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
11840 + btrfs_stop_all_workers(fs_info);
11841 +
11842 +- btrfs_free_block_groups(fs_info);
11843 +-
11844 + clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
11845 +- free_root_pointers(fs_info, 1);
11846 ++ free_root_pointers(fs_info, true);
11847 ++
11848 ++ /*
11849 ++ * We must free the block groups after dropping the fs_roots as we could
11850 ++ * have had an IO error and have left over tree log blocks that aren't
11851 ++ * cleaned up until the fs roots are freed. This makes the block group
11852 ++ * accounting appear to be wrong because there's pending reserved bytes,
11853 ++ * so make sure we do the block group cleanup afterwards.
11854 ++ */
11855 ++ btrfs_free_block_groups(fs_info);
11856 +
11857 + iput(fs_info->btree_inode);
11858 +
11859 +diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
11860 +index fced434bbddc..a8be9478ca3e 100644
11861 +--- a/fs/btrfs/extent_io.c
11862 ++++ b/fs/btrfs/extent_io.c
11863 +@@ -4048,6 +4048,14 @@ retry:
11864 + */
11865 + scanned = 1;
11866 + index = 0;
11867 ++
11868 ++ /*
11869 ++ * If we're looping we could run into a page that is locked by a
11870 ++ * writer and that writer could be waiting on writeback for a
11871 ++ * page in our current bio, and thus deadlock, so flush the
11872 ++ * write bio here.
11873 ++ */
11874 ++ flush_write_bio(data);
11875 + goto retry;
11876 + }
11877 +
11878 +diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
11879 +index fdcb41002623..702b3606ad0e 100644
11880 +--- a/fs/btrfs/file-item.c
11881 ++++ b/fs/btrfs/file-item.c
11882 +@@ -955,7 +955,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
11883 + btrfs_file_extent_num_bytes(leaf, fi);
11884 + } else if (type == BTRFS_FILE_EXTENT_INLINE) {
11885 + size_t size;
11886 +- size = btrfs_file_extent_inline_len(leaf, slot, fi);
11887 ++ size = btrfs_file_extent_ram_bytes(leaf, fi);
11888 + extent_end = ALIGN(extent_start + size,
11889 + fs_info->sectorsize);
11890 + }
11891 +diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
11892 +index c68ce3412dc1..725544ec9c84 100644
11893 +--- a/fs/btrfs/file.c
11894 ++++ b/fs/btrfs/file.c
11895 +@@ -784,8 +784,7 @@ next_slot:
11896 + btrfs_file_extent_num_bytes(leaf, fi);
11897 + } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
11898 + extent_end = key.offset +
11899 +- btrfs_file_extent_inline_len(leaf,
11900 +- path->slots[0], fi);
11901 ++ btrfs_file_extent_ram_bytes(leaf, fi);
11902 + } else {
11903 + /* can't happen */
11904 + BUG();
11905 +diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
11906 +index f2dc517768f0..abecc4724a3b 100644
11907 +--- a/fs/btrfs/inode.c
11908 ++++ b/fs/btrfs/inode.c
11909 +@@ -1476,8 +1476,7 @@ next_slot:
11910 + nocow = 1;
11911 + } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
11912 + extent_end = found_key.offset +
11913 +- btrfs_file_extent_inline_len(leaf,
11914 +- path->slots[0], fi);
11915 ++ btrfs_file_extent_ram_bytes(leaf, fi);
11916 + extent_end = ALIGN(extent_end,
11917 + fs_info->sectorsize);
11918 + } else {
11919 +@@ -4651,8 +4650,8 @@ search_again:
11920 + BTRFS_I(inode), leaf, fi,
11921 + found_key.offset);
11922 + } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
11923 +- item_end += btrfs_file_extent_inline_len(leaf,
11924 +- path->slots[0], fi);
11925 ++ item_end += btrfs_file_extent_ram_bytes(leaf,
11926 ++ fi);
11927 +
11928 + trace_btrfs_truncate_show_fi_inline(
11929 + BTRFS_I(inode), leaf, fi, path->slots[0],
11930 +@@ -7167,7 +7166,8 @@ again:
11931 + extent_start);
11932 + } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
11933 + size_t size;
11934 +- size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
11935 ++
11936 ++ size = btrfs_file_extent_ram_bytes(leaf, item);
11937 + extent_end = ALIGN(extent_start + size,
11938 + fs_info->sectorsize);
11939 +
11940 +@@ -7218,7 +7218,7 @@ next:
11941 + if (new_inline)
11942 + goto out;
11943 +
11944 +- size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
11945 ++ size = btrfs_file_extent_ram_bytes(leaf, item);
11946 + extent_offset = page_offset(page) + pg_offset - extent_start;
11947 + copy_size = min_t(u64, PAGE_SIZE - pg_offset,
11948 + size - extent_offset);
11949 +diff --git a/fs/btrfs/print-tree.c b/fs/btrfs/print-tree.c
11950 +index 569205e651c7..47336d4b19d8 100644
11951 +--- a/fs/btrfs/print-tree.c
11952 ++++ b/fs/btrfs/print-tree.c
11953 +@@ -259,8 +259,8 @@ void btrfs_print_leaf(struct extent_buffer *l)
11954 + struct btrfs_file_extent_item);
11955 + if (btrfs_file_extent_type(l, fi) ==
11956 + BTRFS_FILE_EXTENT_INLINE) {
11957 +- pr_info("\t\tinline extent data size %u\n",
11958 +- btrfs_file_extent_inline_len(l, i, fi));
11959 ++ pr_info("\t\tinline extent data size %llu\n",
11960 ++ btrfs_file_extent_ram_bytes(l, fi));
11961 + break;
11962 + }
11963 + pr_info("\t\textent data disk bytenr %llu nr %llu\n",
11964 +diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
11965 +index 1211fdcd425d..ca15d65a2070 100644
11966 +--- a/fs/btrfs/send.c
11967 ++++ b/fs/btrfs/send.c
11968 +@@ -1545,7 +1545,7 @@ static int read_symlink(struct btrfs_root *root,
11969 + BUG_ON(compression);
11970 +
11971 + off = btrfs_file_extent_inline_start(ei);
11972 +- len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
11973 ++ len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
11974 +
11975 + ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
11976 +
11977 +@@ -5195,7 +5195,7 @@ static int clone_range(struct send_ctx *sctx,
11978 + ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
11979 + type = btrfs_file_extent_type(leaf, ei);
11980 + if (type == BTRFS_FILE_EXTENT_INLINE) {
11981 +- ext_len = btrfs_file_extent_inline_len(leaf, slot, ei);
11982 ++ ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
11983 + ext_len = PAGE_ALIGN(ext_len);
11984 + } else {
11985 + ext_len = btrfs_file_extent_num_bytes(leaf, ei);
11986 +@@ -5271,8 +5271,7 @@ static int send_write_or_clone(struct send_ctx *sctx,
11987 + struct btrfs_file_extent_item);
11988 + type = btrfs_file_extent_type(path->nodes[0], ei);
11989 + if (type == BTRFS_FILE_EXTENT_INLINE) {
11990 +- len = btrfs_file_extent_inline_len(path->nodes[0],
11991 +- path->slots[0], ei);
11992 ++ len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
11993 + /*
11994 + * it is possible the inline item won't cover the whole page,
11995 + * but there may be items after this page. Make
11996 +@@ -5405,7 +5404,7 @@ static int is_extent_unchanged(struct send_ctx *sctx,
11997 + }
11998 +
11999 + if (right_type == BTRFS_FILE_EXTENT_INLINE) {
12000 +- right_len = btrfs_file_extent_inline_len(eb, slot, ei);
12001 ++ right_len = btrfs_file_extent_ram_bytes(eb, ei);
12002 + right_len = PAGE_ALIGN(right_len);
12003 + } else {
12004 + right_len = btrfs_file_extent_num_bytes(eb, ei);
12005 +@@ -5526,8 +5525,7 @@ static int get_last_extent(struct send_ctx *sctx, u64 offset)
12006 + struct btrfs_file_extent_item);
12007 + type = btrfs_file_extent_type(path->nodes[0], fi);
12008 + if (type == BTRFS_FILE_EXTENT_INLINE) {
12009 +- u64 size = btrfs_file_extent_inline_len(path->nodes[0],
12010 +- path->slots[0], fi);
12011 ++ u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
12012 + extent_end = ALIGN(key.offset + size,
12013 + sctx->send_root->fs_info->sectorsize);
12014 + } else {
12015 +@@ -5590,7 +5588,7 @@ static int range_is_hole_in_parent(struct send_ctx *sctx,
12016 + fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
12017 + if (btrfs_file_extent_type(leaf, fi) ==
12018 + BTRFS_FILE_EXTENT_INLINE) {
12019 +- u64 size = btrfs_file_extent_inline_len(leaf, slot, fi);
12020 ++ u64 size = btrfs_file_extent_ram_bytes(leaf, fi);
12021 +
12022 + extent_end = ALIGN(key.offset + size,
12023 + root->fs_info->sectorsize);
12024 +@@ -5636,8 +5634,7 @@ static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
12025 + struct btrfs_file_extent_item);
12026 + type = btrfs_file_extent_type(path->nodes[0], fi);
12027 + if (type == BTRFS_FILE_EXTENT_INLINE) {
12028 +- u64 size = btrfs_file_extent_inline_len(path->nodes[0],
12029 +- path->slots[0], fi);
12030 ++ u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
12031 + extent_end = ALIGN(key->offset + size,
12032 + sctx->send_root->fs_info->sectorsize);
12033 + } else {
12034 +diff --git a/fs/btrfs/tests/btrfs-tests.c b/fs/btrfs/tests/btrfs-tests.c
12035 +index d3f25376a0f8..6c92101e8092 100644
12036 +--- a/fs/btrfs/tests/btrfs-tests.c
12037 ++++ b/fs/btrfs/tests/btrfs-tests.c
12038 +@@ -115,7 +115,6 @@ struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
12039 + spin_lock_init(&fs_info->qgroup_op_lock);
12040 + spin_lock_init(&fs_info->super_lock);
12041 + spin_lock_init(&fs_info->fs_roots_radix_lock);
12042 +- spin_lock_init(&fs_info->tree_mod_seq_lock);
12043 + mutex_init(&fs_info->qgroup_ioctl_lock);
12044 + mutex_init(&fs_info->qgroup_rescan_lock);
12045 + rwlock_init(&fs_info->tree_mod_log_lock);
12046 +diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
12047 +index fa8f56e6f665..a066ad581976 100644
12048 +--- a/fs/btrfs/transaction.c
12049 ++++ b/fs/btrfs/transaction.c
12050 +@@ -1948,6 +1948,14 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
12051 + struct btrfs_transaction *prev_trans = NULL;
12052 + int ret;
12053 +
12054 ++ /*
12055 ++ * Some places just start a transaction to commit it. We need to make
12056 ++ * sure that if this commit fails that the abort code actually marks the
12057 ++ * transaction as failed, so set trans->dirty to make the abort code do
12058 ++ * the right thing.
12059 ++ */
12060 ++ trans->dirty = true;
12061 ++
12062 + /* Stop the commit early if ->aborted is set */
12063 + if (unlikely(READ_ONCE(cur_trans->aborted))) {
12064 + ret = cur_trans->aborted;
12065 +diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
12066 +index 98c397eb054c..0b62c8080af0 100644
12067 +--- a/fs/btrfs/tree-log.c
12068 ++++ b/fs/btrfs/tree-log.c
12069 +@@ -619,7 +619,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
12070 + if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
12071 + nbytes = 0;
12072 + } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
12073 +- size = btrfs_file_extent_inline_len(eb, slot, item);
12074 ++ size = btrfs_file_extent_ram_bytes(eb, item);
12075 + nbytes = btrfs_file_extent_ram_bytes(eb, item);
12076 + extent_end = ALIGN(start + size,
12077 + fs_info->sectorsize);
12078 +@@ -3758,7 +3758,7 @@ static int log_inode_item(struct btrfs_trans_handle *trans,
12079 + static noinline int copy_items(struct btrfs_trans_handle *trans,
12080 + struct btrfs_inode *inode,
12081 + struct btrfs_path *dst_path,
12082 +- struct btrfs_path *src_path, u64 *last_extent,
12083 ++ struct btrfs_path *src_path,
12084 + int start_slot, int nr, int inode_only,
12085 + u64 logged_isize)
12086 + {
12087 +@@ -3769,7 +3769,6 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
12088 + struct btrfs_file_extent_item *extent;
12089 + struct btrfs_inode_item *inode_item;
12090 + struct extent_buffer *src = src_path->nodes[0];
12091 +- struct btrfs_key first_key, last_key, key;
12092 + int ret;
12093 + struct btrfs_key *ins_keys;
12094 + u32 *ins_sizes;
12095 +@@ -3777,9 +3776,6 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
12096 + int i;
12097 + struct list_head ordered_sums;
12098 + int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
12099 +- bool has_extents = false;
12100 +- bool need_find_last_extent = true;
12101 +- bool done = false;
12102 +
12103 + INIT_LIST_HEAD(&ordered_sums);
12104 +
12105 +@@ -3788,8 +3784,6 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
12106 + if (!ins_data)
12107 + return -ENOMEM;
12108 +
12109 +- first_key.objectid = (u64)-1;
12110 +-
12111 + ins_sizes = (u32 *)ins_data;
12112 + ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
12113 +
12114 +@@ -3810,9 +3804,6 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
12115 +
12116 + src_offset = btrfs_item_ptr_offset(src, start_slot + i);
12117 +
12118 +- if (i == nr - 1)
12119 +- last_key = ins_keys[i];
12120 +-
12121 + if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
12122 + inode_item = btrfs_item_ptr(dst_path->nodes[0],
12123 + dst_path->slots[0],
12124 +@@ -3826,20 +3817,6 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
12125 + src_offset, ins_sizes[i]);
12126 + }
12127 +
12128 +- /*
12129 +- * We set need_find_last_extent here in case we know we were
12130 +- * processing other items and then walk into the first extent in
12131 +- * the inode. If we don't hit an extent then nothing changes,
12132 +- * we'll do the last search the next time around.
12133 +- */
12134 +- if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
12135 +- has_extents = true;
12136 +- if (first_key.objectid == (u64)-1)
12137 +- first_key = ins_keys[i];
12138 +- } else {
12139 +- need_find_last_extent = false;
12140 +- }
12141 +-
12142 + /* take a reference on file data extents so that truncates
12143 + * or deletes of this inode don't have to relog the inode
12144 + * again
12145 +@@ -3905,169 +3882,6 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
12146 + kfree(sums);
12147 + }
12148 +
12149 +- if (!has_extents)
12150 +- return ret;
12151 +-
12152 +- if (need_find_last_extent && *last_extent == first_key.offset) {
12153 +- /*
12154 +- * We don't have any leafs between our current one and the one
12155 +- * we processed before that can have file extent items for our
12156 +- * inode (and have a generation number smaller than our current
12157 +- * transaction id).
12158 +- */
12159 +- need_find_last_extent = false;
12160 +- }
12161 +-
12162 +- /*
12163 +- * Because we use btrfs_search_forward we could skip leaves that were
12164 +- * not modified and then assume *last_extent is valid when it really
12165 +- * isn't. So back up to the previous leaf and read the end of the last
12166 +- * extent before we go and fill in holes.
12167 +- */
12168 +- if (need_find_last_extent) {
12169 +- u64 len;
12170 +-
12171 +- ret = btrfs_prev_leaf(inode->root, src_path);
12172 +- if (ret < 0)
12173 +- return ret;
12174 +- if (ret)
12175 +- goto fill_holes;
12176 +- if (src_path->slots[0])
12177 +- src_path->slots[0]--;
12178 +- src = src_path->nodes[0];
12179 +- btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
12180 +- if (key.objectid != btrfs_ino(inode) ||
12181 +- key.type != BTRFS_EXTENT_DATA_KEY)
12182 +- goto fill_holes;
12183 +- extent = btrfs_item_ptr(src, src_path->slots[0],
12184 +- struct btrfs_file_extent_item);
12185 +- if (btrfs_file_extent_type(src, extent) ==
12186 +- BTRFS_FILE_EXTENT_INLINE) {
12187 +- len = btrfs_file_extent_inline_len(src,
12188 +- src_path->slots[0],
12189 +- extent);
12190 +- *last_extent = ALIGN(key.offset + len,
12191 +- fs_info->sectorsize);
12192 +- } else {
12193 +- len = btrfs_file_extent_num_bytes(src, extent);
12194 +- *last_extent = key.offset + len;
12195 +- }
12196 +- }
12197 +-fill_holes:
12198 +- /* So we did prev_leaf, now we need to move to the next leaf, but a few
12199 +- * things could have happened
12200 +- *
12201 +- * 1) A merge could have happened, so we could currently be on a leaf
12202 +- * that holds what we were copying in the first place.
12203 +- * 2) A split could have happened, and now not all of the items we want
12204 +- * are on the same leaf.
12205 +- *
12206 +- * So we need to adjust how we search for holes, we need to drop the
12207 +- * path and re-search for the first extent key we found, and then walk
12208 +- * forward until we hit the last one we copied.
12209 +- */
12210 +- if (need_find_last_extent) {
12211 +- /* btrfs_prev_leaf could return 1 without releasing the path */
12212 +- btrfs_release_path(src_path);
12213 +- ret = btrfs_search_slot(NULL, inode->root, &first_key,
12214 +- src_path, 0, 0);
12215 +- if (ret < 0)
12216 +- return ret;
12217 +- ASSERT(ret == 0);
12218 +- src = src_path->nodes[0];
12219 +- i = src_path->slots[0];
12220 +- } else {
12221 +- i = start_slot;
12222 +- }
12223 +-
12224 +- /*
12225 +- * Ok so here we need to go through and fill in any holes we may have
12226 +- * to make sure that holes are punched for those areas in case they had
12227 +- * extents previously.
12228 +- */
12229 +- while (!done) {
12230 +- u64 offset, len;
12231 +- u64 extent_end;
12232 +-
12233 +- if (i >= btrfs_header_nritems(src_path->nodes[0])) {
12234 +- ret = btrfs_next_leaf(inode->root, src_path);
12235 +- if (ret < 0)
12236 +- return ret;
12237 +- ASSERT(ret == 0);
12238 +- src = src_path->nodes[0];
12239 +- i = 0;
12240 +- need_find_last_extent = true;
12241 +- }
12242 +-
12243 +- btrfs_item_key_to_cpu(src, &key, i);
12244 +- if (!btrfs_comp_cpu_keys(&key, &last_key))
12245 +- done = true;
12246 +- if (key.objectid != btrfs_ino(inode) ||
12247 +- key.type != BTRFS_EXTENT_DATA_KEY) {
12248 +- i++;
12249 +- continue;
12250 +- }
12251 +- extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
12252 +- if (btrfs_file_extent_type(src, extent) ==
12253 +- BTRFS_FILE_EXTENT_INLINE) {
12254 +- len = btrfs_file_extent_inline_len(src, i, extent);
12255 +- extent_end = ALIGN(key.offset + len,
12256 +- fs_info->sectorsize);
12257 +- } else {
12258 +- len = btrfs_file_extent_num_bytes(src, extent);
12259 +- extent_end = key.offset + len;
12260 +- }
12261 +- i++;
12262 +-
12263 +- if (*last_extent == key.offset) {
12264 +- *last_extent = extent_end;
12265 +- continue;
12266 +- }
12267 +- offset = *last_extent;
12268 +- len = key.offset - *last_extent;
12269 +- ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
12270 +- offset, 0, 0, len, 0, len, 0, 0, 0);
12271 +- if (ret)
12272 +- break;
12273 +- *last_extent = extent_end;
12274 +- }
12275 +-
12276 +- /*
12277 +- * Check if there is a hole between the last extent found in our leaf
12278 +- * and the first extent in the next leaf. If there is one, we need to
12279 +- * log an explicit hole so that at replay time we can punch the hole.
12280 +- */
12281 +- if (ret == 0 &&
12282 +- key.objectid == btrfs_ino(inode) &&
12283 +- key.type == BTRFS_EXTENT_DATA_KEY &&
12284 +- i == btrfs_header_nritems(src_path->nodes[0])) {
12285 +- ret = btrfs_next_leaf(inode->root, src_path);
12286 +- need_find_last_extent = true;
12287 +- if (ret > 0) {
12288 +- ret = 0;
12289 +- } else if (ret == 0) {
12290 +- btrfs_item_key_to_cpu(src_path->nodes[0], &key,
12291 +- src_path->slots[0]);
12292 +- if (key.objectid == btrfs_ino(inode) &&
12293 +- key.type == BTRFS_EXTENT_DATA_KEY &&
12294 +- *last_extent < key.offset) {
12295 +- const u64 len = key.offset - *last_extent;
12296 +-
12297 +- ret = btrfs_insert_file_extent(trans, log,
12298 +- btrfs_ino(inode),
12299 +- *last_extent, 0,
12300 +- 0, len, 0, len,
12301 +- 0, 0, 0);
12302 +- *last_extent += len;
12303 +- }
12304 +- }
12305 +- }
12306 +- /*
12307 +- * Need to let the callers know we dropped the path so they should
12308 +- * re-search.
12309 +- */
12310 +- if (!ret && need_find_last_extent)
12311 +- ret = 1;
12312 + return ret;
12313 + }
12314 +
12315 +@@ -4340,7 +4154,7 @@ static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
12316 + const u64 i_size = i_size_read(&inode->vfs_inode);
12317 + const u64 ino = btrfs_ino(inode);
12318 + struct btrfs_path *dst_path = NULL;
12319 +- u64 last_extent = (u64)-1;
12320 ++ bool dropped_extents = false;
12321 + int ins_nr = 0;
12322 + int start_slot;
12323 + int ret;
12324 +@@ -4362,8 +4176,7 @@ static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
12325 + if (slot >= btrfs_header_nritems(leaf)) {
12326 + if (ins_nr > 0) {
12327 + ret = copy_items(trans, inode, dst_path, path,
12328 +- &last_extent, start_slot,
12329 +- ins_nr, 1, 0);
12330 ++ start_slot, ins_nr, 1, 0);
12331 + if (ret < 0)
12332 + goto out;
12333 + ins_nr = 0;
12334 +@@ -4387,8 +4200,7 @@ static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
12335 + path->slots[0]++;
12336 + continue;
12337 + }
12338 +- if (last_extent == (u64)-1) {
12339 +- last_extent = key.offset;
12340 ++ if (!dropped_extents) {
12341 + /*
12342 + * Avoid logging extent items logged in past fsync calls
12343 + * and leading to duplicate keys in the log tree.
12344 +@@ -4402,6 +4214,7 @@ static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
12345 + } while (ret == -EAGAIN);
12346 + if (ret)
12347 + goto out;
12348 ++ dropped_extents = true;
12349 + }
12350 + if (ins_nr == 0)
12351 + start_slot = slot;
12352 +@@ -4416,7 +4229,7 @@ static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
12353 + }
12354 + }
12355 + if (ins_nr > 0) {
12356 +- ret = copy_items(trans, inode, dst_path, path, &last_extent,
12357 ++ ret = copy_items(trans, inode, dst_path, path,
12358 + start_slot, ins_nr, 1, 0);
12359 + if (ret > 0)
12360 + ret = 0;
12361 +@@ -4610,13 +4423,8 @@ static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
12362 +
12363 + if (slot >= nritems) {
12364 + if (ins_nr > 0) {
12365 +- u64 last_extent = 0;
12366 +-
12367 + ret = copy_items(trans, inode, dst_path, path,
12368 +- &last_extent, start_slot,
12369 +- ins_nr, 1, 0);
12370 +- /* can't be 1, extent items aren't processed */
12371 +- ASSERT(ret <= 0);
12372 ++ start_slot, ins_nr, 1, 0);
12373 + if (ret < 0)
12374 + return ret;
12375 + ins_nr = 0;
12376 +@@ -4640,13 +4448,8 @@ static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
12377 + cond_resched();
12378 + }
12379 + if (ins_nr > 0) {
12380 +- u64 last_extent = 0;
12381 +-
12382 + ret = copy_items(trans, inode, dst_path, path,
12383 +- &last_extent, start_slot,
12384 +- ins_nr, 1, 0);
12385 +- /* can't be 1, extent items aren't processed */
12386 +- ASSERT(ret <= 0);
12387 ++ start_slot, ins_nr, 1, 0);
12388 + if (ret < 0)
12389 + return ret;
12390 + }
12391 +@@ -4655,109 +4458,119 @@ static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
12392 + }
12393 +
12394 + /*
12395 +- * If the no holes feature is enabled we need to make sure any hole between the
12396 +- * last extent and the i_size of our inode is explicitly marked in the log. This
12397 +- * is to make sure that doing something like:
12398 +- *
12399 +- * 1) create file with 128Kb of data
12400 +- * 2) truncate file to 64Kb
12401 +- * 3) truncate file to 256Kb
12402 +- * 4) fsync file
12403 +- * 5) <crash/power failure>
12404 +- * 6) mount fs and trigger log replay
12405 +- *
12406 +- * Will give us a file with a size of 256Kb, the first 64Kb of data match what
12407 +- * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
12408 +- * file correspond to a hole. The presence of explicit holes in a log tree is
12409 +- * what guarantees that log replay will remove/adjust file extent items in the
12410 +- * fs/subvol tree.
12411 +- *
12412 +- * Here we do not need to care about holes between extents, that is already done
12413 +- * by copy_items(). We also only need to do this in the full sync path, where we
12414 +- * lookup for extents from the fs/subvol tree only. In the fast path case, we
12415 +- * lookup the list of modified extent maps and if any represents a hole, we
12416 +- * insert a corresponding extent representing a hole in the log tree.
12417 ++ * When using the NO_HOLES feature if we punched a hole that causes the
12418 ++ * deletion of entire leafs or all the extent items of the first leaf (the one
12419 ++ * that contains the inode item and references) we may end up not processing
12420 ++ * any extents, because there are no leafs with a generation matching the
12421 ++ * current transaction that have extent items for our inode. So we need to find
12422 ++ * if any holes exist and then log them. We also need to log holes after any
12423 ++ * truncate operation that changes the inode's size.
12424 + */
12425 +-static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
12426 +- struct btrfs_root *root,
12427 +- struct btrfs_inode *inode,
12428 +- struct btrfs_path *path)
12429 ++static int btrfs_log_holes(struct btrfs_trans_handle *trans,
12430 ++ struct btrfs_root *root,
12431 ++ struct btrfs_inode *inode,
12432 ++ struct btrfs_path *path)
12433 + {
12434 + struct btrfs_fs_info *fs_info = root->fs_info;
12435 +- int ret;
12436 + struct btrfs_key key;
12437 +- u64 hole_start;
12438 +- u64 hole_size;
12439 +- struct extent_buffer *leaf;
12440 +- struct btrfs_root *log = root->log_root;
12441 + const u64 ino = btrfs_ino(inode);
12442 + const u64 i_size = i_size_read(&inode->vfs_inode);
12443 ++ u64 prev_extent_end = 0;
12444 ++ int ret;
12445 +
12446 +- if (!btrfs_fs_incompat(fs_info, NO_HOLES))
12447 ++ if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
12448 + return 0;
12449 +
12450 + key.objectid = ino;
12451 + key.type = BTRFS_EXTENT_DATA_KEY;
12452 +- key.offset = (u64)-1;
12453 ++ key.offset = 0;
12454 +
12455 + ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
12456 +- ASSERT(ret != 0);
12457 + if (ret < 0)
12458 + return ret;
12459 +
12460 +- ASSERT(path->slots[0] > 0);
12461 +- path->slots[0]--;
12462 +- leaf = path->nodes[0];
12463 +- btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
12464 +-
12465 +- if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
12466 +- /* inode does not have any extents */
12467 +- hole_start = 0;
12468 +- hole_size = i_size;
12469 +- } else {
12470 ++ while (true) {
12471 + struct btrfs_file_extent_item *extent;
12472 ++ struct extent_buffer *leaf = path->nodes[0];
12473 + u64 len;
12474 +
12475 +- /*
12476 +- * If there's an extent beyond i_size, an explicit hole was
12477 +- * already inserted by copy_items().
12478 +- */
12479 +- if (key.offset >= i_size)
12480 +- return 0;
12481 ++ if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
12482 ++ ret = btrfs_next_leaf(root, path);
12483 ++ if (ret < 0)
12484 ++ return ret;
12485 ++ if (ret > 0) {
12486 ++ ret = 0;
12487 ++ break;
12488 ++ }
12489 ++ leaf = path->nodes[0];
12490 ++ }
12491 ++
12492 ++ btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
12493 ++ if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
12494 ++ break;
12495 ++
12496 ++ /* We have a hole, log it. */
12497 ++ if (prev_extent_end < key.offset) {
12498 ++ const u64 hole_len = key.offset - prev_extent_end;
12499 ++
12500 ++ /*
12501 ++ * Release the path to avoid deadlocks with other code
12502 ++ * paths that search the root while holding locks on
12503 ++ * leafs from the log root.
12504 ++ */
12505 ++ btrfs_release_path(path);
12506 ++ ret = btrfs_insert_file_extent(trans, root->log_root,
12507 ++ ino, prev_extent_end, 0,
12508 ++ 0, hole_len, 0, hole_len,
12509 ++ 0, 0, 0);
12510 ++ if (ret < 0)
12511 ++ return ret;
12512 ++
12513 ++ /*
12514 ++ * Search for the same key again in the root. Since it's
12515 ++ * an extent item and we are holding the inode lock, the
12516 ++ * key must still exist. If it doesn't just emit warning
12517 ++ * and return an error to fall back to a transaction
12518 ++ * commit.
12519 ++ */
12520 ++ ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
12521 ++ if (ret < 0)
12522 ++ return ret;
12523 ++ if (WARN_ON(ret > 0))
12524 ++ return -ENOENT;
12525 ++ leaf = path->nodes[0];
12526 ++ }
12527 +
12528 + extent = btrfs_item_ptr(leaf, path->slots[0],
12529 + struct btrfs_file_extent_item);
12530 +-
12531 + if (btrfs_file_extent_type(leaf, extent) ==
12532 + BTRFS_FILE_EXTENT_INLINE) {
12533 +- len = btrfs_file_extent_inline_len(leaf,
12534 +- path->slots[0],
12535 +- extent);
12536 +- ASSERT(len == i_size ||
12537 +- (len == fs_info->sectorsize &&
12538 +- btrfs_file_extent_compression(leaf, extent) !=
12539 +- BTRFS_COMPRESS_NONE) ||
12540 +- (len < i_size && i_size < fs_info->sectorsize));
12541 +- return 0;
12542 ++ len = btrfs_file_extent_ram_bytes(leaf, extent);
12543 ++ prev_extent_end = ALIGN(key.offset + len,
12544 ++ fs_info->sectorsize);
12545 ++ } else {
12546 ++ len = btrfs_file_extent_num_bytes(leaf, extent);
12547 ++ prev_extent_end = key.offset + len;
12548 + }
12549 +
12550 +- len = btrfs_file_extent_num_bytes(leaf, extent);
12551 +- /* Last extent goes beyond i_size, no need to log a hole. */
12552 +- if (key.offset + len > i_size)
12553 +- return 0;
12554 +- hole_start = key.offset + len;
12555 +- hole_size = i_size - hole_start;
12556 ++ path->slots[0]++;
12557 ++ cond_resched();
12558 + }
12559 +- btrfs_release_path(path);
12560 +
12561 +- /* Last extent ends at i_size. */
12562 +- if (hole_size == 0)
12563 +- return 0;
12564 ++ if (prev_extent_end < i_size) {
12565 ++ u64 hole_len;
12566 +
12567 +- hole_size = ALIGN(hole_size, fs_info->sectorsize);
12568 +- ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
12569 +- hole_size, 0, hole_size, 0, 0, 0);
12570 +- return ret;
12571 ++ btrfs_release_path(path);
12572 ++ hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
12573 ++ ret = btrfs_insert_file_extent(trans, root->log_root,
12574 ++ ino, prev_extent_end, 0, 0,
12575 ++ hole_len, 0, hole_len,
12576 ++ 0, 0, 0);
12577 ++ if (ret < 0)
12578 ++ return ret;
12579 ++ }
12580 ++
12581 ++ return 0;
12582 + }
12583 +
12584 + /*
12585 +@@ -4925,7 +4738,6 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
12586 + struct btrfs_root *log = root->log_root;
12587 + struct extent_buffer *src = NULL;
12588 + LIST_HEAD(logged_list);
12589 +- u64 last_extent = 0;
12590 + int err = 0;
12591 + int ret;
12592 + int nritems;
12593 +@@ -5099,7 +4911,7 @@ again:
12594 + ins_start_slot = path->slots[0];
12595 + }
12596 + ret = copy_items(trans, inode, dst_path, path,
12597 +- &last_extent, ins_start_slot,
12598 ++ ins_start_slot,
12599 + ins_nr, inode_only,
12600 + logged_isize);
12601 + if (ret < 0) {
12602 +@@ -5153,17 +4965,13 @@ again:
12603 + if (ins_nr == 0)
12604 + goto next_slot;
12605 + ret = copy_items(trans, inode, dst_path, path,
12606 +- &last_extent, ins_start_slot,
12607 ++ ins_start_slot,
12608 + ins_nr, inode_only, logged_isize);
12609 + if (ret < 0) {
12610 + err = ret;
12611 + goto out_unlock;
12612 + }
12613 + ins_nr = 0;
12614 +- if (ret) {
12615 +- btrfs_release_path(path);
12616 +- continue;
12617 +- }
12618 + goto next_slot;
12619 + }
12620 +
12621 +@@ -5177,18 +4985,13 @@ again:
12622 + goto next_slot;
12623 + }
12624 +
12625 +- ret = copy_items(trans, inode, dst_path, path, &last_extent,
12626 ++ ret = copy_items(trans, inode, dst_path, path,
12627 + ins_start_slot, ins_nr, inode_only,
12628 + logged_isize);
12629 + if (ret < 0) {
12630 + err = ret;
12631 + goto out_unlock;
12632 + }
12633 +- if (ret) {
12634 +- ins_nr = 0;
12635 +- btrfs_release_path(path);
12636 +- continue;
12637 +- }
12638 + ins_nr = 1;
12639 + ins_start_slot = path->slots[0];
12640 + next_slot:
12641 +@@ -5202,13 +5005,12 @@ next_slot:
12642 + }
12643 + if (ins_nr) {
12644 + ret = copy_items(trans, inode, dst_path, path,
12645 +- &last_extent, ins_start_slot,
12646 ++ ins_start_slot,
12647 + ins_nr, inode_only, logged_isize);
12648 + if (ret < 0) {
12649 + err = ret;
12650 + goto out_unlock;
12651 + }
12652 +- ret = 0;
12653 + ins_nr = 0;
12654 + }
12655 + btrfs_release_path(path);
12656 +@@ -5223,14 +5025,13 @@ next_key:
12657 + }
12658 + }
12659 + if (ins_nr) {
12660 +- ret = copy_items(trans, inode, dst_path, path, &last_extent,
12661 ++ ret = copy_items(trans, inode, dst_path, path,
12662 + ins_start_slot, ins_nr, inode_only,
12663 + logged_isize);
12664 + if (ret < 0) {
12665 + err = ret;
12666 + goto out_unlock;
12667 + }
12668 +- ret = 0;
12669 + ins_nr = 0;
12670 + }
12671 +
12672 +@@ -5243,7 +5044,7 @@ next_key:
12673 + if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
12674 + btrfs_release_path(path);
12675 + btrfs_release_path(dst_path);
12676 +- err = btrfs_log_trailing_hole(trans, root, inode, path);
12677 ++ err = btrfs_log_holes(trans, root, inode, path);
12678 + if (err)
12679 + goto out_unlock;
12680 + }
12681 +diff --git a/fs/cifs/smb2pdu.c b/fs/cifs/smb2pdu.c
12682 +index 4eb0a9e7194b..1c87a429ce72 100644
12683 +--- a/fs/cifs/smb2pdu.c
12684 ++++ b/fs/cifs/smb2pdu.c
12685 +@@ -257,9 +257,14 @@ smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
12686 + }
12687 +
12688 + rc = cifs_negotiate_protocol(0, tcon->ses);
12689 +- if (!rc && tcon->ses->need_reconnect)
12690 ++ if (!rc && tcon->ses->need_reconnect) {
12691 + rc = cifs_setup_session(0, tcon->ses, nls_codepage);
12692 +-
12693 ++ if ((rc == -EACCES) && !tcon->retry) {
12694 ++ rc = -EHOSTDOWN;
12695 ++ mutex_unlock(&tcon->ses->session_mutex);
12696 ++ goto failed;
12697 ++ }
12698 ++ }
12699 + if (rc || !tcon->need_reconnect) {
12700 + mutex_unlock(&tcon->ses->session_mutex);
12701 + goto out;
12702 +@@ -301,6 +306,7 @@ out:
12703 + case SMB2_SET_INFO:
12704 + rc = -EAGAIN;
12705 + }
12706 ++failed:
12707 + unload_nls(nls_codepage);
12708 + return rc;
12709 + }
12710 +diff --git a/fs/ext2/super.c b/fs/ext2/super.c
12711 +index 13f470636672..4a338576ebb1 100644
12712 +--- a/fs/ext2/super.c
12713 ++++ b/fs/ext2/super.c
12714 +@@ -1077,9 +1077,9 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
12715 +
12716 + if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
12717 + goto cantfind_ext2;
12718 +- sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
12719 +- le32_to_cpu(es->s_first_data_block) - 1)
12720 +- / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
12721 ++ sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
12722 ++ le32_to_cpu(es->s_first_data_block) - 1)
12723 ++ / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
12724 + db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
12725 + EXT2_DESC_PER_BLOCK(sb);
12726 + sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
12727 +diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
12728 +index db7590178dfc..9cc79b7b0df1 100644
12729 +--- a/fs/ext4/page-io.c
12730 ++++ b/fs/ext4/page-io.c
12731 +@@ -481,17 +481,26 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
12732 + nr_to_submit) {
12733 + gfp_t gfp_flags = GFP_NOFS;
12734 +
12735 ++ /*
12736 ++ * Since bounce page allocation uses a mempool, we can only use
12737 ++ * a waiting mask (i.e. request guaranteed allocation) on the
12738 ++ * first page of the bio. Otherwise it can deadlock.
12739 ++ */
12740 ++ if (io->io_bio)
12741 ++ gfp_flags = GFP_NOWAIT | __GFP_NOWARN;
12742 + retry_encrypt:
12743 + data_page = fscrypt_encrypt_page(inode, page, PAGE_SIZE, 0,
12744 + page->index, gfp_flags);
12745 + if (IS_ERR(data_page)) {
12746 + ret = PTR_ERR(data_page);
12747 +- if (ret == -ENOMEM && wbc->sync_mode == WB_SYNC_ALL) {
12748 +- if (io->io_bio) {
12749 ++ if (ret == -ENOMEM &&
12750 ++ (io->io_bio || wbc->sync_mode == WB_SYNC_ALL)) {
12751 ++ gfp_flags = GFP_NOFS;
12752 ++ if (io->io_bio)
12753 + ext4_io_submit(io);
12754 +- congestion_wait(BLK_RW_ASYNC, HZ/50);
12755 +- }
12756 +- gfp_flags |= __GFP_NOFAIL;
12757 ++ else
12758 ++ gfp_flags |= __GFP_NOFAIL;
12759 ++ congestion_wait(BLK_RW_ASYNC, HZ/50);
12760 + goto retry_encrypt;
12761 + }
12762 + data_page = NULL;
12763 +diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
12764 +index e4aabfc21bd4..2d021a33914a 100644
12765 +--- a/fs/f2fs/super.c
12766 ++++ b/fs/f2fs/super.c
12767 +@@ -912,9 +912,11 @@ static int f2fs_statfs_project(struct super_block *sb,
12768 + return PTR_ERR(dquot);
12769 + spin_lock(&dq_data_lock);
12770 +
12771 +- limit = (dquot->dq_dqb.dqb_bsoftlimit ?
12772 +- dquot->dq_dqb.dqb_bsoftlimit :
12773 +- dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
12774 ++ limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
12775 ++ dquot->dq_dqb.dqb_bhardlimit);
12776 ++ if (limit)
12777 ++ limit >>= sb->s_blocksize_bits;
12778 ++
12779 + if (limit && buf->f_blocks > limit) {
12780 + curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
12781 + buf->f_blocks = limit;
12782 +@@ -923,9 +925,9 @@ static int f2fs_statfs_project(struct super_block *sb,
12783 + (buf->f_blocks - curblock) : 0;
12784 + }
12785 +
12786 +- limit = dquot->dq_dqb.dqb_isoftlimit ?
12787 +- dquot->dq_dqb.dqb_isoftlimit :
12788 +- dquot->dq_dqb.dqb_ihardlimit;
12789 ++ limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
12790 ++ dquot->dq_dqb.dqb_ihardlimit);
12791 ++
12792 + if (limit && buf->f_files > limit) {
12793 + buf->f_files = limit;
12794 + buf->f_ffree =
12795 +diff --git a/fs/nfs/Kconfig b/fs/nfs/Kconfig
12796 +index 5f93cfacb3d1..ac3e06367cb6 100644
12797 +--- a/fs/nfs/Kconfig
12798 ++++ b/fs/nfs/Kconfig
12799 +@@ -89,7 +89,7 @@ config NFS_V4
12800 + config NFS_SWAP
12801 + bool "Provide swap over NFS support"
12802 + default n
12803 +- depends on NFS_FS
12804 ++ depends on NFS_FS && SWAP
12805 + select SUNRPC_SWAP
12806 + help
12807 + This option enables swapon to work on files located on NFS mounts.
12808 +diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c
12809 +index 50c181fa0025..673d89bb817e 100644
12810 +--- a/fs/nfs/dir.c
12811 ++++ b/fs/nfs/dir.c
12812 +@@ -169,6 +169,17 @@ typedef struct {
12813 + bool eof;
12814 + } nfs_readdir_descriptor_t;
12815 +
12816 ++static
12817 ++void nfs_readdir_init_array(struct page *page)
12818 ++{
12819 ++ struct nfs_cache_array *array;
12820 ++
12821 ++ array = kmap_atomic(page);
12822 ++ memset(array, 0, sizeof(struct nfs_cache_array));
12823 ++ array->eof_index = -1;
12824 ++ kunmap_atomic(array);
12825 ++}
12826 ++
12827 + /*
12828 + * we are freeing strings created by nfs_add_to_readdir_array()
12829 + */
12830 +@@ -181,6 +192,7 @@ void nfs_readdir_clear_array(struct page *page)
12831 + array = kmap_atomic(page);
12832 + for (i = 0; i < array->size; i++)
12833 + kfree(array->array[i].string.name);
12834 ++ array->size = 0;
12835 + kunmap_atomic(array);
12836 + }
12837 +
12838 +@@ -617,6 +629,8 @@ int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page,
12839 + int status = -ENOMEM;
12840 + unsigned int array_size = ARRAY_SIZE(pages);
12841 +
12842 ++ nfs_readdir_init_array(page);
12843 ++
12844 + entry.prev_cookie = 0;
12845 + entry.cookie = desc->last_cookie;
12846 + entry.eof = 0;
12847 +@@ -633,8 +647,6 @@ int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page,
12848 + }
12849 +
12850 + array = kmap(page);
12851 +- memset(array, 0, sizeof(struct nfs_cache_array));
12852 +- array->eof_index = -1;
12853 +
12854 + status = nfs_readdir_alloc_pages(pages, array_size);
12855 + if (status < 0)
12856 +@@ -688,6 +700,7 @@ int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
12857 + unlock_page(page);
12858 + return 0;
12859 + error:
12860 ++ nfs_readdir_clear_array(page);
12861 + unlock_page(page);
12862 + return ret;
12863 + }
12864 +@@ -695,8 +708,6 @@ int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
12865 + static
12866 + void cache_page_release(nfs_readdir_descriptor_t *desc)
12867 + {
12868 +- if (!desc->page->mapping)
12869 +- nfs_readdir_clear_array(desc->page);
12870 + put_page(desc->page);
12871 + desc->page = NULL;
12872 + }
12873 +@@ -710,19 +721,28 @@ struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
12874 +
12875 + /*
12876 + * Returns 0 if desc->dir_cookie was found on page desc->page_index
12877 ++ * and locks the page to prevent removal from the page cache.
12878 + */
12879 + static
12880 +-int find_cache_page(nfs_readdir_descriptor_t *desc)
12881 ++int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc)
12882 + {
12883 + int res;
12884 +
12885 + desc->page = get_cache_page(desc);
12886 + if (IS_ERR(desc->page))
12887 + return PTR_ERR(desc->page);
12888 +-
12889 +- res = nfs_readdir_search_array(desc);
12890 ++ res = lock_page_killable(desc->page);
12891 + if (res != 0)
12892 +- cache_page_release(desc);
12893 ++ goto error;
12894 ++ res = -EAGAIN;
12895 ++ if (desc->page->mapping != NULL) {
12896 ++ res = nfs_readdir_search_array(desc);
12897 ++ if (res == 0)
12898 ++ return 0;
12899 ++ }
12900 ++ unlock_page(desc->page);
12901 ++error:
12902 ++ cache_page_release(desc);
12903 + return res;
12904 + }
12905 +
12906 +@@ -737,7 +757,7 @@ int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
12907 + desc->last_cookie = 0;
12908 + }
12909 + do {
12910 +- res = find_cache_page(desc);
12911 ++ res = find_and_lock_cache_page(desc);
12912 + } while (res == -EAGAIN);
12913 + return res;
12914 + }
12915 +@@ -776,7 +796,6 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
12916 + desc->eof = 1;
12917 +
12918 + kunmap(desc->page);
12919 +- cache_page_release(desc);
12920 + dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
12921 + (unsigned long long)*desc->dir_cookie, res);
12922 + return res;
12923 +@@ -822,13 +841,13 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc)
12924 +
12925 + status = nfs_do_filldir(desc);
12926 +
12927 ++ out_release:
12928 ++ nfs_readdir_clear_array(desc->page);
12929 ++ cache_page_release(desc);
12930 + out:
12931 + dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
12932 + __func__, status);
12933 + return status;
12934 +- out_release:
12935 +- cache_page_release(desc);
12936 +- goto out;
12937 + }
12938 +
12939 + /* The file offset position represents the dirent entry number. A
12940 +@@ -893,6 +912,8 @@ static int nfs_readdir(struct file *file, struct dir_context *ctx)
12941 + break;
12942 +
12943 + res = nfs_do_filldir(desc);
12944 ++ unlock_page(desc->page);
12945 ++ cache_page_release(desc);
12946 + if (res < 0)
12947 + break;
12948 + } while (!desc->eof);
12949 +diff --git a/fs/nfs/direct.c b/fs/nfs/direct.c
12950 +index 9cdac9945483..9d07b53e1647 100644
12951 +--- a/fs/nfs/direct.c
12952 ++++ b/fs/nfs/direct.c
12953 +@@ -261,10 +261,10 @@ static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq,
12954 + data->ds_commit_index);
12955 +
12956 + /* verifier not set so always fail */
12957 +- if (verfp->committed < 0)
12958 ++ if (verfp->committed < 0 || data->res.verf->committed <= NFS_UNSTABLE)
12959 + return 1;
12960 +
12961 +- return nfs_direct_cmp_verf(verfp, &data->verf);
12962 ++ return nfs_direct_cmp_verf(verfp, data->res.verf);
12963 + }
12964 +
12965 + /**
12966 +diff --git a/fs/nfs/nfs3xdr.c b/fs/nfs/nfs3xdr.c
12967 +index 6cd33bd5da87..f1cb0b7eb05f 100644
12968 +--- a/fs/nfs/nfs3xdr.c
12969 ++++ b/fs/nfs/nfs3xdr.c
12970 +@@ -2373,6 +2373,7 @@ static int nfs3_xdr_dec_commit3res(struct rpc_rqst *req,
12971 + void *data)
12972 + {
12973 + struct nfs_commitres *result = data;
12974 ++ struct nfs_writeverf *verf = result->verf;
12975 + enum nfs_stat status;
12976 + int error;
12977 +
12978 +@@ -2385,7 +2386,9 @@ static int nfs3_xdr_dec_commit3res(struct rpc_rqst *req,
12979 + result->op_status = status;
12980 + if (status != NFS3_OK)
12981 + goto out_status;
12982 +- error = decode_writeverf3(xdr, &result->verf->verifier);
12983 ++ error = decode_writeverf3(xdr, &verf->verifier);
12984 ++ if (!error)
12985 ++ verf->committed = NFS_FILE_SYNC;
12986 + out:
12987 + return error;
12988 + out_status:
12989 +diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c
12990 +index 3dd403943b07..4d45786738ab 100644
12991 +--- a/fs/nfs/nfs4proc.c
12992 ++++ b/fs/nfs/nfs4proc.c
12993 +@@ -2923,6 +2923,11 @@ static struct nfs4_state *nfs4_do_open(struct inode *dir,
12994 + exception.retry = 1;
12995 + continue;
12996 + }
12997 ++ if (status == -NFS4ERR_EXPIRED) {
12998 ++ nfs4_schedule_lease_recovery(server->nfs_client);
12999 ++ exception.retry = 1;
13000 ++ continue;
13001 ++ }
13002 + if (status == -EAGAIN) {
13003 + /* We must have found a delegation */
13004 + exception.retry = 1;
13005 +diff --git a/fs/nfs/nfs4xdr.c b/fs/nfs/nfs4xdr.c
13006 +index 525684b0056f..0b2d051990e9 100644
13007 +--- a/fs/nfs/nfs4xdr.c
13008 ++++ b/fs/nfs/nfs4xdr.c
13009 +@@ -4409,11 +4409,14 @@ static int decode_write_verifier(struct xdr_stream *xdr, struct nfs_write_verifi
13010 +
13011 + static int decode_commit(struct xdr_stream *xdr, struct nfs_commitres *res)
13012 + {
13013 ++ struct nfs_writeverf *verf = res->verf;
13014 + int status;
13015 +
13016 + status = decode_op_hdr(xdr, OP_COMMIT);
13017 + if (!status)
13018 +- status = decode_write_verifier(xdr, &res->verf->verifier);
13019 ++ status = decode_write_verifier(xdr, &verf->verifier);
13020 ++ if (!status)
13021 ++ verf->committed = NFS_FILE_SYNC;
13022 + return status;
13023 + }
13024 +
13025 +diff --git a/fs/nfs/pnfs_nfs.c b/fs/nfs/pnfs_nfs.c
13026 +index 4a3dd66175fe..b0ef37f3e2dd 100644
13027 +--- a/fs/nfs/pnfs_nfs.c
13028 ++++ b/fs/nfs/pnfs_nfs.c
13029 +@@ -30,12 +30,11 @@ EXPORT_SYMBOL_GPL(pnfs_generic_rw_release);
13030 + /* Fake up some data that will cause nfs_commit_release to retry the writes. */
13031 + void pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data *data)
13032 + {
13033 +- struct nfs_page *first = nfs_list_entry(data->pages.next);
13034 ++ struct nfs_writeverf *verf = data->res.verf;
13035 +
13036 + data->task.tk_status = 0;
13037 +- memcpy(&data->verf.verifier, &first->wb_verf,
13038 +- sizeof(data->verf.verifier));
13039 +- data->verf.verifier.data[0]++; /* ensure verifier mismatch */
13040 ++ memset(&verf->verifier, 0, sizeof(verf->verifier));
13041 ++ verf->committed = NFS_UNSTABLE;
13042 + }
13043 + EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes);
13044 +
13045 +diff --git a/fs/nfs/write.c b/fs/nfs/write.c
13046 +index ed3f5afc4ff7..89f36040adf6 100644
13047 +--- a/fs/nfs/write.c
13048 ++++ b/fs/nfs/write.c
13049 +@@ -1807,6 +1807,7 @@ static void nfs_commit_done(struct rpc_task *task, void *calldata)
13050 +
13051 + static void nfs_commit_release_pages(struct nfs_commit_data *data)
13052 + {
13053 ++ const struct nfs_writeverf *verf = data->res.verf;
13054 + struct nfs_page *req;
13055 + int status = data->task.tk_status;
13056 + struct nfs_commit_info cinfo;
13057 +@@ -1833,7 +1834,8 @@ static void nfs_commit_release_pages(struct nfs_commit_data *data)
13058 +
13059 + /* Okay, COMMIT succeeded, apparently. Check the verifier
13060 + * returned by the server against all stored verfs. */
13061 +- if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) {
13062 ++ if (verf->committed > NFS_UNSTABLE &&
13063 ++ !nfs_write_verifier_cmp(&req->wb_verf, &verf->verifier)) {
13064 + /* We have a match */
13065 + if (req->wb_page)
13066 + nfs_inode_remove_request(req);
13067 +diff --git a/fs/nfsd/nfs4layouts.c b/fs/nfsd/nfs4layouts.c
13068 +index ea45d954e8d7..99add0cf20ff 100644
13069 +--- a/fs/nfsd/nfs4layouts.c
13070 ++++ b/fs/nfsd/nfs4layouts.c
13071 +@@ -683,7 +683,7 @@ nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task)
13072 +
13073 + /* Client gets 2 lease periods to return it */
13074 + cutoff = ktime_add_ns(task->tk_start,
13075 +- nn->nfsd4_lease * NSEC_PER_SEC * 2);
13076 ++ (u64)nn->nfsd4_lease * NSEC_PER_SEC * 2);
13077 +
13078 + if (ktime_before(now, cutoff)) {
13079 + rpc_delay(task, HZ/100); /* 10 mili-seconds */
13080 +diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
13081 +index fc13236d1be1..fca8b2e7fbeb 100644
13082 +--- a/fs/nfsd/nfs4state.c
13083 ++++ b/fs/nfsd/nfs4state.c
13084 +@@ -6040,7 +6040,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
13085 + }
13086 +
13087 + if (fl_flags & FL_SLEEP) {
13088 +- nbl->nbl_time = jiffies;
13089 ++ nbl->nbl_time = get_seconds();
13090 + spin_lock(&nn->blocked_locks_lock);
13091 + list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
13092 + list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
13093 +diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h
13094 +index 133d8bf62a5c..7872b1ead885 100644
13095 +--- a/fs/nfsd/state.h
13096 ++++ b/fs/nfsd/state.h
13097 +@@ -591,7 +591,7 @@ static inline bool nfsd4_stateid_generation_after(stateid_t *a, stateid_t *b)
13098 + struct nfsd4_blocked_lock {
13099 + struct list_head nbl_list;
13100 + struct list_head nbl_lru;
13101 +- unsigned long nbl_time;
13102 ++ time_t nbl_time;
13103 + struct file_lock nbl_lock;
13104 + struct knfsd_fh nbl_fh;
13105 + struct nfsd4_callback nbl_cb;
13106 +diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
13107 +index 4e6e32c0c08a..358abc26dbc0 100644
13108 +--- a/fs/ubifs/dir.c
13109 ++++ b/fs/ubifs/dir.c
13110 +@@ -253,6 +253,8 @@ static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
13111 + if (nm.hash) {
13112 + ubifs_assert(fname_len(&nm) == 0);
13113 + ubifs_assert(fname_name(&nm) == NULL);
13114 ++ if (nm.hash & ~UBIFS_S_KEY_HASH_MASK)
13115 ++ goto done; /* ENOENT */
13116 + dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
13117 + err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
13118 + } else {
13119 +diff --git a/fs/ubifs/file.c b/fs/ubifs/file.c
13120 +index a02aa59d1e24..46e5a58c4b05 100644
13121 +--- a/fs/ubifs/file.c
13122 ++++ b/fs/ubifs/file.c
13123 +@@ -797,7 +797,9 @@ static int ubifs_do_bulk_read(struct ubifs_info *c, struct bu_info *bu,
13124 +
13125 + if (page_offset > end_index)
13126 + break;
13127 +- page = find_or_create_page(mapping, page_offset, ra_gfp_mask);
13128 ++ page = pagecache_get_page(mapping, page_offset,
13129 ++ FGP_LOCK|FGP_ACCESSED|FGP_CREAT|FGP_NOWAIT,
13130 ++ ra_gfp_mask);
13131 + if (!page)
13132 + break;
13133 + if (!PageUptodate(page))
13134 +diff --git a/fs/ubifs/ioctl.c b/fs/ubifs/ioctl.c
13135 +index fdc311246807..1f6d16105990 100644
13136 +--- a/fs/ubifs/ioctl.c
13137 ++++ b/fs/ubifs/ioctl.c
13138 +@@ -28,6 +28,11 @@
13139 + #include <linux/mount.h>
13140 + #include "ubifs.h"
13141 +
13142 ++/* Need to be kept consistent with checked flags in ioctl2ubifs() */
13143 ++#define UBIFS_SUPPORTED_IOCTL_FLAGS \
13144 ++ (FS_COMPR_FL | FS_SYNC_FL | FS_APPEND_FL | \
13145 ++ FS_IMMUTABLE_FL | FS_DIRSYNC_FL)
13146 ++
13147 + /**
13148 + * ubifs_set_inode_flags - set VFS inode flags.
13149 + * @inode: VFS inode to set flags for
13150 +@@ -124,7 +129,8 @@ static int setflags(struct inode *inode, int flags)
13151 + }
13152 + }
13153 +
13154 +- ui->flags = ioctl2ubifs(flags);
13155 ++ ui->flags &= ~ioctl2ubifs(UBIFS_SUPPORTED_IOCTL_FLAGS);
13156 ++ ui->flags |= ioctl2ubifs(flags);
13157 + ubifs_set_inode_flags(inode);
13158 + inode->i_ctime = current_time(inode);
13159 + release = ui->dirty;
13160 +@@ -166,6 +172,9 @@ long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
13161 + if (get_user(flags, (int __user *) arg))
13162 + return -EFAULT;
13163 +
13164 ++ if (flags & ~UBIFS_SUPPORTED_IOCTL_FLAGS)
13165 ++ return -EOPNOTSUPP;
13166 ++
13167 + if (!S_ISDIR(inode->i_mode))
13168 + flags &= ~FS_DIRSYNC_FL;
13169 +
13170 +diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
13171 +index 7668c68ddb5b..30376715a607 100644
13172 +--- a/include/linux/kvm_host.h
13173 ++++ b/include/linux/kvm_host.h
13174 +@@ -695,7 +695,7 @@ int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
13175 + int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
13176 + struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
13177 + bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
13178 +-unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn);
13179 ++unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn);
13180 + void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
13181 +
13182 + struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu);
13183 +diff --git a/include/media/v4l2-rect.h b/include/media/v4l2-rect.h
13184 +index d2125f0cc7cd..1584c760b993 100644
13185 +--- a/include/media/v4l2-rect.h
13186 ++++ b/include/media/v4l2-rect.h
13187 +@@ -75,10 +75,10 @@ static inline void v4l2_rect_map_inside(struct v4l2_rect *r,
13188 + r->left = boundary->left;
13189 + if (r->top < boundary->top)
13190 + r->top = boundary->top;
13191 +- if (r->left + r->width > boundary->width)
13192 +- r->left = boundary->width - r->width;
13193 +- if (r->top + r->height > boundary->height)
13194 +- r->top = boundary->height - r->height;
13195 ++ if (r->left + r->width > boundary->left + boundary->width)
13196 ++ r->left = boundary->left + boundary->width - r->width;
13197 ++ if (r->top + r->height > boundary->top + boundary->height)
13198 ++ r->top = boundary->top + boundary->height - r->height;
13199 + }
13200 +
13201 + /**
13202 +diff --git a/include/trace/events/btrfs.h b/include/trace/events/btrfs.h
13203 +index 32d0c1fe2bfa..3ebada29a313 100644
13204 +--- a/include/trace/events/btrfs.h
13205 ++++ b/include/trace/events/btrfs.h
13206 +@@ -325,7 +325,7 @@ DECLARE_EVENT_CLASS(
13207 + __entry->extent_type = btrfs_file_extent_type(l, fi);
13208 + __entry->compression = btrfs_file_extent_compression(l, fi);
13209 + __entry->extent_start = start;
13210 +- __entry->extent_end = (start + btrfs_file_extent_inline_len(l, slot, fi));
13211 ++ __entry->extent_end = (start + btrfs_file_extent_ram_bytes(l, fi));
13212 + ),
13213 +
13214 + TP_printk_btrfs(
13215 +diff --git a/kernel/events/core.c b/kernel/events/core.c
13216 +index 2ac73b4cb8a9..845c8a1a9d30 100644
13217 +--- a/kernel/events/core.c
13218 ++++ b/kernel/events/core.c
13219 +@@ -5441,7 +5441,15 @@ accounting:
13220 + */
13221 + user_lock_limit *= num_online_cpus();
13222 +
13223 +- user_locked = atomic_long_read(&user->locked_vm) + user_extra;
13224 ++ user_locked = atomic_long_read(&user->locked_vm);
13225 ++
13226 ++ /*
13227 ++ * sysctl_perf_event_mlock may have changed, so that
13228 ++ * user->locked_vm > user_lock_limit
13229 ++ */
13230 ++ if (user_locked > user_lock_limit)
13231 ++ user_locked = user_lock_limit;
13232 ++ user_locked += user_extra;
13233 +
13234 + if (user_locked > user_lock_limit)
13235 + extra = user_locked - user_lock_limit;
13236 +diff --git a/kernel/irq/irqdomain.c b/kernel/irq/irqdomain.c
13237 +index 0f0e7975a309..b269ae16b10c 100644
13238 +--- a/kernel/irq/irqdomain.c
13239 ++++ b/kernel/irq/irqdomain.c
13240 +@@ -1538,6 +1538,7 @@ int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
13241 + if (rv) {
13242 + /* Restore the original irq_data. */
13243 + *root_irq_data = *child_irq_data;
13244 ++ kfree(child_irq_data);
13245 + goto error;
13246 + }
13247 +
13248 +diff --git a/kernel/module.c b/kernel/module.c
13249 +index feb1e0fbc3e8..2806c9b6577c 100644
13250 +--- a/kernel/module.c
13251 ++++ b/kernel/module.c
13252 +@@ -1730,6 +1730,8 @@ static int module_add_modinfo_attrs(struct module *mod)
13253 + error_out:
13254 + if (i > 0)
13255 + module_remove_modinfo_attrs(mod, --i);
13256 ++ else
13257 ++ kfree(mod->modinfo_attrs);
13258 + return error;
13259 + }
13260 +
13261 +diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
13262 +index f4255a65c44b..9eece67f29f3 100644
13263 +--- a/kernel/time/alarmtimer.c
13264 ++++ b/kernel/time/alarmtimer.c
13265 +@@ -91,6 +91,7 @@ static int alarmtimer_rtc_add_device(struct device *dev,
13266 + unsigned long flags;
13267 + struct rtc_device *rtc = to_rtc_device(dev);
13268 + struct wakeup_source *__ws;
13269 ++ int ret = 0;
13270 +
13271 + if (rtcdev)
13272 + return -EBUSY;
13273 +@@ -105,8 +106,8 @@ static int alarmtimer_rtc_add_device(struct device *dev,
13274 + spin_lock_irqsave(&rtcdev_lock, flags);
13275 + if (!rtcdev) {
13276 + if (!try_module_get(rtc->owner)) {
13277 +- spin_unlock_irqrestore(&rtcdev_lock, flags);
13278 +- return -1;
13279 ++ ret = -1;
13280 ++ goto unlock;
13281 + }
13282 +
13283 + rtcdev = rtc;
13284 +@@ -115,11 +116,12 @@ static int alarmtimer_rtc_add_device(struct device *dev,
13285 + ws = __ws;
13286 + __ws = NULL;
13287 + }
13288 ++unlock:
13289 + spin_unlock_irqrestore(&rtcdev_lock, flags);
13290 +
13291 + wakeup_source_unregister(__ws);
13292 +
13293 +- return 0;
13294 ++ return ret;
13295 + }
13296 +
13297 + static inline void alarmtimer_rtc_timer_init(void)
13298 +diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
13299 +index 3b71d859ee38..825d24df921a 100644
13300 +--- a/kernel/time/clocksource.c
13301 ++++ b/kernel/time/clocksource.c
13302 +@@ -280,8 +280,15 @@ static void clocksource_watchdog(unsigned long data)
13303 + next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
13304 + if (next_cpu >= nr_cpu_ids)
13305 + next_cpu = cpumask_first(cpu_online_mask);
13306 +- watchdog_timer.expires += WATCHDOG_INTERVAL;
13307 +- add_timer_on(&watchdog_timer, next_cpu);
13308 ++
13309 ++ /*
13310 ++ * Arm timer if not already pending: could race with concurrent
13311 ++ * pair clocksource_stop_watchdog() clocksource_start_watchdog().
13312 ++ */
13313 ++ if (!timer_pending(&watchdog_timer)) {
13314 ++ watchdog_timer.expires += WATCHDOG_INTERVAL;
13315 ++ add_timer_on(&watchdog_timer, next_cpu);
13316 ++ }
13317 + out:
13318 + spin_unlock(&watchdog_lock);
13319 + }
13320 +diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
13321 +index 3864d2341442..8974ecbcca3c 100644
13322 +--- a/kernel/trace/ftrace.c
13323 ++++ b/kernel/trace/ftrace.c
13324 +@@ -5146,8 +5146,8 @@ static const struct file_operations ftrace_notrace_fops = {
13325 +
13326 + static DEFINE_MUTEX(graph_lock);
13327 +
13328 +-struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
13329 +-struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
13330 ++struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
13331 ++struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
13332 +
13333 + enum graph_filter_type {
13334 + GRAPH_FILTER_NOTRACE = 0,
13335 +@@ -5419,8 +5419,15 @@ ftrace_graph_release(struct inode *inode, struct file *file)
13336 +
13337 + mutex_unlock(&graph_lock);
13338 +
13339 +- /* Wait till all users are no longer using the old hash */
13340 +- synchronize_sched();
13341 ++ /*
13342 ++ * We need to do a hard force of sched synchronization.
13343 ++ * This is because we use preempt_disable() to do RCU, but
13344 ++ * the function tracers can be called where RCU is not watching
13345 ++ * (like before user_exit()). We can not rely on the RCU
13346 ++ * infrastructure to do the synchronization, thus we must do it
13347 ++ * ourselves.
13348 ++ */
13349 ++ schedule_on_each_cpu(ftrace_sync);
13350 +
13351 + free_ftrace_hash(old_hash);
13352 + }
13353 +diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
13354 +index dbb212c40a41..c4c61ebb8d05 100644
13355 +--- a/kernel/trace/trace.h
13356 ++++ b/kernel/trace/trace.h
13357 +@@ -868,22 +868,31 @@ extern void __trace_graph_return(struct trace_array *tr,
13358 + unsigned long flags, int pc);
13359 +
13360 + #ifdef CONFIG_DYNAMIC_FTRACE
13361 +-extern struct ftrace_hash *ftrace_graph_hash;
13362 +-extern struct ftrace_hash *ftrace_graph_notrace_hash;
13363 ++extern struct ftrace_hash __rcu *ftrace_graph_hash;
13364 ++extern struct ftrace_hash __rcu *ftrace_graph_notrace_hash;
13365 +
13366 + static inline int ftrace_graph_addr(struct ftrace_graph_ent *trace)
13367 + {
13368 + unsigned long addr = trace->func;
13369 + int ret = 0;
13370 ++ struct ftrace_hash *hash;
13371 +
13372 + preempt_disable_notrace();
13373 +
13374 +- if (ftrace_hash_empty(ftrace_graph_hash)) {
13375 ++ /*
13376 ++ * Have to open code "rcu_dereference_sched()" because the
13377 ++ * function graph tracer can be called when RCU is not
13378 ++ * "watching".
13379 ++ * Protected with schedule_on_each_cpu(ftrace_sync)
13380 ++ */
13381 ++ hash = rcu_dereference_protected(ftrace_graph_hash, !preemptible());
13382 ++
13383 ++ if (ftrace_hash_empty(hash)) {
13384 + ret = 1;
13385 + goto out;
13386 + }
13387 +
13388 +- if (ftrace_lookup_ip(ftrace_graph_hash, addr)) {
13389 ++ if (ftrace_lookup_ip(hash, addr)) {
13390 +
13391 + /*
13392 + * This needs to be cleared on the return functions
13393 +@@ -919,10 +928,20 @@ static inline void ftrace_graph_addr_finish(struct ftrace_graph_ret *trace)
13394 + static inline int ftrace_graph_notrace_addr(unsigned long addr)
13395 + {
13396 + int ret = 0;
13397 ++ struct ftrace_hash *notrace_hash;
13398 +
13399 + preempt_disable_notrace();
13400 +
13401 +- if (ftrace_lookup_ip(ftrace_graph_notrace_hash, addr))
13402 ++ /*
13403 ++ * Have to open code "rcu_dereference_sched()" because the
13404 ++ * function graph tracer can be called when RCU is not
13405 ++ * "watching".
13406 ++ * Protected with schedule_on_each_cpu(ftrace_sync)
13407 ++ */
13408 ++ notrace_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
13409 ++ !preemptible());
13410 ++
13411 ++ if (ftrace_lookup_ip(notrace_hash, addr))
13412 + ret = 1;
13413 +
13414 + preempt_enable_notrace();
13415 +diff --git a/kernel/trace/trace_sched_switch.c b/kernel/trace/trace_sched_switch.c
13416 +index e288168661e1..e304196d7c28 100644
13417 +--- a/kernel/trace/trace_sched_switch.c
13418 ++++ b/kernel/trace/trace_sched_switch.c
13419 +@@ -89,8 +89,10 @@ static void tracing_sched_unregister(void)
13420 +
13421 + static void tracing_start_sched_switch(int ops)
13422 + {
13423 +- bool sched_register = (!sched_cmdline_ref && !sched_tgid_ref);
13424 ++ bool sched_register;
13425 ++
13426 + mutex_lock(&sched_register_mutex);
13427 ++ sched_register = (!sched_cmdline_ref && !sched_tgid_ref);
13428 +
13429 + switch (ops) {
13430 + case RECORD_CMDLINE:
13431 +diff --git a/lib/test_kasan.c b/lib/test_kasan.c
13432 +index d6e46dd1350b..1399d1000130 100644
13433 +--- a/lib/test_kasan.c
13434 ++++ b/lib/test_kasan.c
13435 +@@ -126,6 +126,7 @@ static noinline void __init kmalloc_oob_krealloc_more(void)
13436 + if (!ptr1 || !ptr2) {
13437 + pr_err("Allocation failed\n");
13438 + kfree(ptr1);
13439 ++ kfree(ptr2);
13440 + return;
13441 + }
13442 +
13443 +diff --git a/net/hsr/hsr_slave.c b/net/hsr/hsr_slave.c
13444 +index 56080da4aa77..5fee6ec7c93d 100644
13445 +--- a/net/hsr/hsr_slave.c
13446 ++++ b/net/hsr/hsr_slave.c
13447 +@@ -32,6 +32,8 @@ static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
13448 +
13449 + rcu_read_lock(); /* hsr->node_db, hsr->ports */
13450 + port = hsr_port_get_rcu(skb->dev);
13451 ++ if (!port)
13452 ++ goto finish_pass;
13453 +
13454 + if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
13455 + /* Directly kill frames sent by ourselves */
13456 +diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
13457 +index db1eceda2359..0c69b66d93d7 100644
13458 +--- a/net/ipv4/tcp.c
13459 ++++ b/net/ipv4/tcp.c
13460 +@@ -2363,9 +2363,11 @@ int tcp_disconnect(struct sock *sk, int flags)
13461 + tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
13462 + tp->snd_cwnd_cnt = 0;
13463 + tp->window_clamp = 0;
13464 ++ tp->delivered = 0;
13465 + tcp_set_ca_state(sk, TCP_CA_Open);
13466 + tp->is_sack_reneg = 0;
13467 + tcp_clear_retrans(tp);
13468 ++ tp->total_retrans = 0;
13469 + inet_csk_delack_init(sk);
13470 + /* Initialize rcv_mss to TCP_MIN_MSS to avoid division by 0
13471 + * issue in __tcp_select_window()
13472 +@@ -2377,8 +2379,12 @@ int tcp_disconnect(struct sock *sk, int flags)
13473 + dst_release(sk->sk_rx_dst);
13474 + sk->sk_rx_dst = NULL;
13475 + tcp_saved_syn_free(tp);
13476 ++ tp->segs_in = 0;
13477 ++ tp->segs_out = 0;
13478 + tp->bytes_acked = 0;
13479 + tp->bytes_received = 0;
13480 ++ tp->data_segs_in = 0;
13481 ++ tp->data_segs_out = 0;
13482 +
13483 + /* Clean up fastopen related fields */
13484 + tcp_free_fastopen_req(tp);
13485 +diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c
13486 +index b9be0360ab94..b8c90f8d1a57 100644
13487 +--- a/net/l2tp/l2tp_core.c
13488 ++++ b/net/l2tp/l2tp_core.c
13489 +@@ -358,8 +358,13 @@ static int l2tp_session_add_to_tunnel(struct l2tp_tunnel *tunnel,
13490 +
13491 + spin_lock_bh(&pn->l2tp_session_hlist_lock);
13492 +
13493 ++ /* IP encap expects session IDs to be globally unique, while
13494 ++ * UDP encap doesn't.
13495 ++ */
13496 + hlist_for_each_entry(session_walk, g_head, global_hlist)
13497 +- if (session_walk->session_id == session->session_id) {
13498 ++ if (session_walk->session_id == session->session_id &&
13499 ++ (session_walk->tunnel->encap == L2TP_ENCAPTYPE_IP ||
13500 ++ tunnel->encap == L2TP_ENCAPTYPE_IP)) {
13501 + err = -EEXIST;
13502 + goto err_tlock_pnlock;
13503 + }
13504 +diff --git a/net/rxrpc/ar-internal.h b/net/rxrpc/ar-internal.h
13505 +index 71c7f1dd4599..b5581b0b9480 100644
13506 +--- a/net/rxrpc/ar-internal.h
13507 ++++ b/net/rxrpc/ar-internal.h
13508 +@@ -451,6 +451,7 @@ enum rxrpc_call_flag {
13509 + RXRPC_CALL_SEND_PING, /* A ping will need to be sent */
13510 + RXRPC_CALL_PINGING, /* Ping in process */
13511 + RXRPC_CALL_RETRANS_TIMEOUT, /* Retransmission due to timeout occurred */
13512 ++ RXRPC_CALL_DISCONNECTED, /* The call has been disconnected */
13513 + };
13514 +
13515 + /*
13516 +diff --git a/net/rxrpc/call_object.c b/net/rxrpc/call_object.c
13517 +index ddaa471a2607..7021725fa38a 100644
13518 +--- a/net/rxrpc/call_object.c
13519 ++++ b/net/rxrpc/call_object.c
13520 +@@ -505,7 +505,7 @@ void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
13521 +
13522 + _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
13523 +
13524 +- if (conn)
13525 ++ if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
13526 + rxrpc_disconnect_call(call);
13527 +
13528 + for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
13529 +@@ -639,6 +639,7 @@ static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
13530 + {
13531 + struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
13532 +
13533 ++ rxrpc_put_connection(call->conn);
13534 + rxrpc_put_peer(call->peer);
13535 + kfree(call->rxtx_buffer);
13536 + kfree(call->rxtx_annotations);
13537 +@@ -660,7 +661,6 @@ void rxrpc_cleanup_call(struct rxrpc_call *call)
13538 +
13539 + ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
13540 + ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
13541 +- ASSERTCMP(call->conn, ==, NULL);
13542 +
13543 + /* Clean up the Rx/Tx buffer */
13544 + for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++)
13545 +diff --git a/net/rxrpc/conn_client.c b/net/rxrpc/conn_client.c
13546 +index 0aa4bf09fb9c..05d17ec63635 100644
13547 +--- a/net/rxrpc/conn_client.c
13548 ++++ b/net/rxrpc/conn_client.c
13549 +@@ -762,9 +762,9 @@ void rxrpc_disconnect_client_call(struct rxrpc_call *call)
13550 + struct rxrpc_net *rxnet = rxrpc_net(sock_net(&call->socket->sk));
13551 +
13552 + trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
13553 +- call->conn = NULL;
13554 +
13555 + spin_lock(&conn->channel_lock);
13556 ++ set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
13557 +
13558 + /* Calls that have never actually been assigned a channel can simply be
13559 + * discarded. If the conn didn't get used either, it will follow
13560 +@@ -863,7 +863,6 @@ out:
13561 + spin_unlock(&rxnet->client_conn_cache_lock);
13562 + out_2:
13563 + spin_unlock(&conn->channel_lock);
13564 +- rxrpc_put_connection(conn);
13565 + _leave("");
13566 + return;
13567 +
13568 +diff --git a/net/rxrpc/conn_object.c b/net/rxrpc/conn_object.c
13569 +index a48c817b792b..af0232820597 100644
13570 +--- a/net/rxrpc/conn_object.c
13571 ++++ b/net/rxrpc/conn_object.c
13572 +@@ -207,9 +207,8 @@ void rxrpc_disconnect_call(struct rxrpc_call *call)
13573 + __rxrpc_disconnect_call(conn, call);
13574 + spin_unlock(&conn->channel_lock);
13575 +
13576 +- call->conn = NULL;
13577 ++ set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
13578 + conn->idle_timestamp = jiffies;
13579 +- rxrpc_put_connection(conn);
13580 + }
13581 +
13582 + /*
13583 +diff --git a/net/rxrpc/input.c b/net/rxrpc/input.c
13584 +index ea506a77f3c8..18ce6f97462b 100644
13585 +--- a/net/rxrpc/input.c
13586 ++++ b/net/rxrpc/input.c
13587 +@@ -585,8 +585,7 @@ ack:
13588 + immediate_ack, true,
13589 + rxrpc_propose_ack_input_data);
13590 +
13591 +- if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
13592 +- rxrpc_notify_socket(call);
13593 ++ rxrpc_notify_socket(call);
13594 + _leave(" [queued]");
13595 + }
13596 +
13597 +diff --git a/net/rxrpc/output.c b/net/rxrpc/output.c
13598 +index edddbacf33bc..9619c56ef4cd 100644
13599 +--- a/net/rxrpc/output.c
13600 ++++ b/net/rxrpc/output.c
13601 +@@ -96,7 +96,7 @@ static size_t rxrpc_fill_out_ack(struct rxrpc_call *call,
13602 + */
13603 + int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping)
13604 + {
13605 +- struct rxrpc_connection *conn = NULL;
13606 ++ struct rxrpc_connection *conn;
13607 + struct rxrpc_ack_buffer *pkt;
13608 + struct msghdr msg;
13609 + struct kvec iov[2];
13610 +@@ -106,18 +106,14 @@ int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping)
13611 + int ret;
13612 + u8 reason;
13613 +
13614 +- spin_lock_bh(&call->lock);
13615 +- if (call->conn)
13616 +- conn = rxrpc_get_connection_maybe(call->conn);
13617 +- spin_unlock_bh(&call->lock);
13618 +- if (!conn)
13619 ++ if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
13620 + return -ECONNRESET;
13621 +
13622 + pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
13623 +- if (!pkt) {
13624 +- rxrpc_put_connection(conn);
13625 ++ if (!pkt)
13626 + return -ENOMEM;
13627 +- }
13628 ++
13629 ++ conn = call->conn;
13630 +
13631 + msg.msg_name = &call->peer->srx.transport;
13632 + msg.msg_namelen = call->peer->srx.transport_len;
13633 +@@ -204,7 +200,6 @@ int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping)
13634 + }
13635 +
13636 + out:
13637 +- rxrpc_put_connection(conn);
13638 + kfree(pkt);
13639 + return ret;
13640 + }
13641 +@@ -214,20 +209,18 @@ out:
13642 + */
13643 + int rxrpc_send_abort_packet(struct rxrpc_call *call)
13644 + {
13645 +- struct rxrpc_connection *conn = NULL;
13646 ++ struct rxrpc_connection *conn;
13647 + struct rxrpc_abort_buffer pkt;
13648 + struct msghdr msg;
13649 + struct kvec iov[1];
13650 + rxrpc_serial_t serial;
13651 + int ret;
13652 +
13653 +- spin_lock_bh(&call->lock);
13654 +- if (call->conn)
13655 +- conn = rxrpc_get_connection_maybe(call->conn);
13656 +- spin_unlock_bh(&call->lock);
13657 +- if (!conn)
13658 ++ if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
13659 + return -ECONNRESET;
13660 +
13661 ++ conn = call->conn;
13662 ++
13663 + msg.msg_name = &call->peer->srx.transport;
13664 + msg.msg_namelen = call->peer->srx.transport_len;
13665 + msg.msg_control = NULL;
13666 +@@ -255,7 +248,6 @@ int rxrpc_send_abort_packet(struct rxrpc_call *call)
13667 + ret = kernel_sendmsg(conn->params.local->socket,
13668 + &msg, iov, 1, sizeof(pkt));
13669 +
13670 +- rxrpc_put_connection(conn);
13671 + return ret;
13672 + }
13673 +
13674 +diff --git a/net/sched/cls_rsvp.h b/net/sched/cls_rsvp.h
13675 +index cf325625c99d..89259819e9ed 100644
13676 +--- a/net/sched/cls_rsvp.h
13677 ++++ b/net/sched/cls_rsvp.h
13678 +@@ -475,10 +475,8 @@ static u32 gen_tunnel(struct rsvp_head *data)
13679 +
13680 + static const struct nla_policy rsvp_policy[TCA_RSVP_MAX + 1] = {
13681 + [TCA_RSVP_CLASSID] = { .type = NLA_U32 },
13682 +- [TCA_RSVP_DST] = { .type = NLA_BINARY,
13683 +- .len = RSVP_DST_LEN * sizeof(u32) },
13684 +- [TCA_RSVP_SRC] = { .type = NLA_BINARY,
13685 +- .len = RSVP_DST_LEN * sizeof(u32) },
13686 ++ [TCA_RSVP_DST] = { .len = RSVP_DST_LEN * sizeof(u32) },
13687 ++ [TCA_RSVP_SRC] = { .len = RSVP_DST_LEN * sizeof(u32) },
13688 + [TCA_RSVP_PINFO] = { .len = sizeof(struct tc_rsvp_pinfo) },
13689 + };
13690 +
13691 +diff --git a/net/sched/cls_tcindex.c b/net/sched/cls_tcindex.c
13692 +index 75c7c7cc7499..796b4e1beb12 100644
13693 +--- a/net/sched/cls_tcindex.c
13694 ++++ b/net/sched/cls_tcindex.c
13695 +@@ -351,12 +351,31 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
13696 + cp->fall_through = p->fall_through;
13697 + cp->tp = tp;
13698 +
13699 ++ if (tb[TCA_TCINDEX_HASH])
13700 ++ cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
13701 ++
13702 ++ if (tb[TCA_TCINDEX_MASK])
13703 ++ cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
13704 ++
13705 ++ if (tb[TCA_TCINDEX_SHIFT])
13706 ++ cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
13707 ++
13708 ++ if (!cp->hash) {
13709 ++ /* Hash not specified, use perfect hash if the upper limit
13710 ++ * of the hashing index is below the threshold.
13711 ++ */
13712 ++ if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
13713 ++ cp->hash = (cp->mask >> cp->shift) + 1;
13714 ++ else
13715 ++ cp->hash = DEFAULT_HASH_SIZE;
13716 ++ }
13717 ++
13718 + if (p->perfect) {
13719 + int i;
13720 +
13721 + if (tcindex_alloc_perfect_hash(cp) < 0)
13722 + goto errout;
13723 +- for (i = 0; i < cp->hash; i++)
13724 ++ for (i = 0; i < min(cp->hash, p->hash); i++)
13725 + cp->perfect[i].res = p->perfect[i].res;
13726 + balloc = 1;
13727 + }
13728 +@@ -364,19 +383,10 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
13729 +
13730 + err = tcindex_filter_result_init(&new_filter_result);
13731 + if (err < 0)
13732 +- goto errout1;
13733 ++ goto errout_alloc;
13734 + if (old_r)
13735 + cr = r->res;
13736 +
13737 +- if (tb[TCA_TCINDEX_HASH])
13738 +- cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
13739 +-
13740 +- if (tb[TCA_TCINDEX_MASK])
13741 +- cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
13742 +-
13743 +- if (tb[TCA_TCINDEX_SHIFT])
13744 +- cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
13745 +-
13746 + err = -EBUSY;
13747 +
13748 + /* Hash already allocated, make sure that we still meet the
13749 +@@ -394,16 +404,6 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
13750 + if (tb[TCA_TCINDEX_FALL_THROUGH])
13751 + cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
13752 +
13753 +- if (!cp->hash) {
13754 +- /* Hash not specified, use perfect hash if the upper limit
13755 +- * of the hashing index is below the threshold.
13756 +- */
13757 +- if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
13758 +- cp->hash = (cp->mask >> cp->shift) + 1;
13759 +- else
13760 +- cp->hash = DEFAULT_HASH_SIZE;
13761 +- }
13762 +-
13763 + if (!cp->perfect && !cp->h)
13764 + cp->alloc_hash = cp->hash;
13765 +
13766 +@@ -502,7 +502,6 @@ errout_alloc:
13767 + tcindex_free_perfect_hash(cp);
13768 + else if (balloc == 2)
13769 + kfree(cp->h);
13770 +-errout1:
13771 + tcf_exts_destroy(&new_filter_result.exts);
13772 + errout:
13773 + kfree(cp);
13774 +diff --git a/net/sunrpc/auth_gss/svcauth_gss.c b/net/sunrpc/auth_gss/svcauth_gss.c
13775 +index cc08cb1292a9..a457e7afb768 100644
13776 +--- a/net/sunrpc/auth_gss/svcauth_gss.c
13777 ++++ b/net/sunrpc/auth_gss/svcauth_gss.c
13778 +@@ -1188,6 +1188,7 @@ static int gss_proxy_save_rsc(struct cache_detail *cd,
13779 + dprintk("RPC: No creds found!\n");
13780 + goto out;
13781 + } else {
13782 ++ struct timespec64 boot;
13783 +
13784 + /* steal creds */
13785 + rsci.cred = ud->creds;
13786 +@@ -1208,6 +1209,9 @@ static int gss_proxy_save_rsc(struct cache_detail *cd,
13787 + &expiry, GFP_KERNEL);
13788 + if (status)
13789 + goto out;
13790 ++
13791 ++ getboottime64(&boot);
13792 ++ expiry -= boot.tv_sec;
13793 + }
13794 +
13795 + rsci.h.expiry_time = expiry;
13796 +diff --git a/net/vmw_vsock/hyperv_transport.c b/net/vmw_vsock/hyperv_transport.c
13797 +index 6614512f8180..736b76ec8cf0 100644
13798 +--- a/net/vmw_vsock/hyperv_transport.c
13799 ++++ b/net/vmw_vsock/hyperv_transport.c
13800 +@@ -144,28 +144,15 @@ struct hvsock {
13801 + ****************************************************************************
13802 + * The only valid Service GUIDs, from the perspectives of both the host and *
13803 + * Linux VM, that can be connected by the other end, must conform to this *
13804 +- * format: <port>-facb-11e6-bd58-64006a7986d3, and the "port" must be in *
13805 +- * this range [0, 0x7FFFFFFF]. *
13806 ++ * format: <port>-facb-11e6-bd58-64006a7986d3. *
13807 + ****************************************************************************
13808 + *
13809 + * When we write apps on the host to connect(), the GUID ServiceID is used.
13810 + * When we write apps in Linux VM to connect(), we only need to specify the
13811 + * port and the driver will form the GUID and use that to request the host.
13812 + *
13813 +- * From the perspective of Linux VM:
13814 +- * 1. the local ephemeral port (i.e. the local auto-bound port when we call
13815 +- * connect() without explicit bind()) is generated by __vsock_bind_stream(),
13816 +- * and the range is [1024, 0xFFFFFFFF).
13817 +- * 2. the remote ephemeral port (i.e. the auto-generated remote port for
13818 +- * a connect request initiated by the host's connect()) is generated by
13819 +- * hvs_remote_addr_init() and the range is [0x80000000, 0xFFFFFFFF).
13820 + */
13821 +
13822 +-#define MAX_LISTEN_PORT ((u32)0x7FFFFFFF)
13823 +-#define MAX_VM_LISTEN_PORT MAX_LISTEN_PORT
13824 +-#define MAX_HOST_LISTEN_PORT MAX_LISTEN_PORT
13825 +-#define MIN_HOST_EPHEMERAL_PORT (MAX_HOST_LISTEN_PORT + 1)
13826 +-
13827 + /* 00000000-facb-11e6-bd58-64006a7986d3 */
13828 + static const uuid_le srv_id_template =
13829 + UUID_LE(0x00000000, 0xfacb, 0x11e6, 0xbd, 0x58,
13830 +@@ -188,33 +175,6 @@ static void hvs_addr_init(struct sockaddr_vm *addr, const uuid_le *svr_id)
13831 + vsock_addr_init(addr, VMADDR_CID_ANY, port);
13832 + }
13833 +
13834 +-static void hvs_remote_addr_init(struct sockaddr_vm *remote,
13835 +- struct sockaddr_vm *local)
13836 +-{
13837 +- static u32 host_ephemeral_port = MIN_HOST_EPHEMERAL_PORT;
13838 +- struct sock *sk;
13839 +-
13840 +- vsock_addr_init(remote, VMADDR_CID_ANY, VMADDR_PORT_ANY);
13841 +-
13842 +- while (1) {
13843 +- /* Wrap around ? */
13844 +- if (host_ephemeral_port < MIN_HOST_EPHEMERAL_PORT ||
13845 +- host_ephemeral_port == VMADDR_PORT_ANY)
13846 +- host_ephemeral_port = MIN_HOST_EPHEMERAL_PORT;
13847 +-
13848 +- remote->svm_port = host_ephemeral_port++;
13849 +-
13850 +- sk = vsock_find_connected_socket(remote, local);
13851 +- if (!sk) {
13852 +- /* Found an available ephemeral port */
13853 +- return;
13854 +- }
13855 +-
13856 +- /* Release refcnt got in vsock_find_connected_socket */
13857 +- sock_put(sk);
13858 +- }
13859 +-}
13860 +-
13861 + static void hvs_set_channel_pending_send_size(struct vmbus_channel *chan)
13862 + {
13863 + set_channel_pending_send_size(chan,
13864 +@@ -342,12 +302,7 @@ static void hvs_open_connection(struct vmbus_channel *chan)
13865 + if_type = &chan->offermsg.offer.if_type;
13866 + if_instance = &chan->offermsg.offer.if_instance;
13867 + conn_from_host = chan->offermsg.offer.u.pipe.user_def[0];
13868 +-
13869 +- /* The host or the VM should only listen on a port in
13870 +- * [0, MAX_LISTEN_PORT]
13871 +- */
13872 +- if (!is_valid_srv_id(if_type) ||
13873 +- get_port_by_srv_id(if_type) > MAX_LISTEN_PORT)
13874 ++ if (!is_valid_srv_id(if_type))
13875 + return;
13876 +
13877 + hvs_addr_init(&addr, conn_from_host ? if_type : if_instance);
13878 +@@ -372,6 +327,13 @@ static void hvs_open_connection(struct vmbus_channel *chan)
13879 +
13880 + new->sk_state = TCP_SYN_SENT;
13881 + vnew = vsock_sk(new);
13882 ++
13883 ++ hvs_addr_init(&vnew->local_addr, if_type);
13884 ++
13885 ++ /* Remote peer is always the host */
13886 ++ vsock_addr_init(&vnew->remote_addr,
13887 ++ VMADDR_CID_HOST, VMADDR_PORT_ANY);
13888 ++ vnew->remote_addr.svm_port = get_port_by_srv_id(if_instance);
13889 + hvs_new = vnew->trans;
13890 + hvs_new->chan = chan;
13891 + } else {
13892 +@@ -411,8 +373,6 @@ static void hvs_open_connection(struct vmbus_channel *chan)
13893 + sk->sk_ack_backlog++;
13894 +
13895 + hvs_addr_init(&vnew->local_addr, if_type);
13896 +- hvs_remote_addr_init(&vnew->remote_addr, &vnew->local_addr);
13897 +-
13898 + hvs_new->vm_srv_id = *if_type;
13899 + hvs_new->host_srv_id = *if_instance;
13900 +
13901 +@@ -717,16 +677,6 @@ static bool hvs_stream_is_active(struct vsock_sock *vsk)
13902 +
13903 + static bool hvs_stream_allow(u32 cid, u32 port)
13904 + {
13905 +- /* The host's port range [MIN_HOST_EPHEMERAL_PORT, 0xFFFFFFFF) is
13906 +- * reserved as ephemeral ports, which are used as the host's ports
13907 +- * when the host initiates connections.
13908 +- *
13909 +- * Perform this check in the guest so an immediate error is produced
13910 +- * instead of a timeout.
13911 +- */
13912 +- if (port > MAX_HOST_LISTEN_PORT)
13913 +- return false;
13914 +-
13915 + if (cid == VMADDR_CID_HOST)
13916 + return true;
13917 +
13918 +diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
13919 +index c1dc632d4ea4..3460036621e4 100644
13920 +--- a/samples/bpf/Makefile
13921 ++++ b/samples/bpf/Makefile
13922 +@@ -184,7 +184,7 @@ all: $(LIBBPF)
13923 +
13924 + clean:
13925 + $(MAKE) -C ../../ M=$(CURDIR) clean
13926 +- @rm -f *~
13927 ++ @find $(CURDIR) -type f -name '*~' -delete
13928 +
13929 + $(LIBBPF): FORCE
13930 + $(MAKE) -C $(dir $@) $(notdir $@)
13931 +diff --git a/sound/drivers/dummy.c b/sound/drivers/dummy.c
13932 +index c0939a0164a6..aeb65d7d4cb3 100644
13933 +--- a/sound/drivers/dummy.c
13934 ++++ b/sound/drivers/dummy.c
13935 +@@ -933,7 +933,7 @@ static void print_formats(struct snd_dummy *dummy,
13936 + {
13937 + int i;
13938 +
13939 +- for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
13940 ++ for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
13941 + if (dummy->pcm_hw.formats & (1ULL << i))
13942 + snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
13943 + }
13944 +diff --git a/sound/soc/soc-pcm.c b/sound/soc/soc-pcm.c
13945 +index 70e1a60a2e98..89f772ed4705 100644
13946 +--- a/sound/soc/soc-pcm.c
13947 ++++ b/sound/soc/soc-pcm.c
13948 +@@ -2123,42 +2123,81 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
13949 + }
13950 + EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
13951 +
13952 ++static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
13953 ++ int cmd, bool fe_first)
13954 ++{
13955 ++ struct snd_soc_pcm_runtime *fe = substream->private_data;
13956 ++ int ret;
13957 ++
13958 ++ /* call trigger on the frontend before the backend. */
13959 ++ if (fe_first) {
13960 ++ dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
13961 ++ fe->dai_link->name, cmd);
13962 ++
13963 ++ ret = soc_pcm_trigger(substream, cmd);
13964 ++ if (ret < 0)
13965 ++ return ret;
13966 ++
13967 ++ ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
13968 ++ return ret;
13969 ++ }
13970 ++
13971 ++ /* call trigger on the frontend after the backend. */
13972 ++ ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
13973 ++ if (ret < 0)
13974 ++ return ret;
13975 ++
13976 ++ dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
13977 ++ fe->dai_link->name, cmd);
13978 ++
13979 ++ ret = soc_pcm_trigger(substream, cmd);
13980 ++
13981 ++ return ret;
13982 ++}
13983 ++
13984 + static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
13985 + {
13986 + struct snd_soc_pcm_runtime *fe = substream->private_data;
13987 +- int stream = substream->stream, ret;
13988 ++ int stream = substream->stream;
13989 ++ int ret = 0;
13990 + enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
13991 +
13992 + fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
13993 +
13994 + switch (trigger) {
13995 + case SND_SOC_DPCM_TRIGGER_PRE:
13996 +- /* call trigger on the frontend before the backend. */
13997 +-
13998 +- dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
13999 +- fe->dai_link->name, cmd);
14000 +-
14001 +- ret = soc_pcm_trigger(substream, cmd);
14002 +- if (ret < 0) {
14003 +- dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
14004 +- goto out;
14005 ++ switch (cmd) {
14006 ++ case SNDRV_PCM_TRIGGER_START:
14007 ++ case SNDRV_PCM_TRIGGER_RESUME:
14008 ++ case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
14009 ++ ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
14010 ++ break;
14011 ++ case SNDRV_PCM_TRIGGER_STOP:
14012 ++ case SNDRV_PCM_TRIGGER_SUSPEND:
14013 ++ case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
14014 ++ ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
14015 ++ break;
14016 ++ default:
14017 ++ ret = -EINVAL;
14018 ++ break;
14019 + }
14020 +-
14021 +- ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
14022 + break;
14023 + case SND_SOC_DPCM_TRIGGER_POST:
14024 +- /* call trigger on the frontend after the backend. */
14025 +-
14026 +- ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
14027 +- if (ret < 0) {
14028 +- dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
14029 +- goto out;
14030 ++ switch (cmd) {
14031 ++ case SNDRV_PCM_TRIGGER_START:
14032 ++ case SNDRV_PCM_TRIGGER_RESUME:
14033 ++ case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
14034 ++ ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
14035 ++ break;
14036 ++ case SNDRV_PCM_TRIGGER_STOP:
14037 ++ case SNDRV_PCM_TRIGGER_SUSPEND:
14038 ++ case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
14039 ++ ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
14040 ++ break;
14041 ++ default:
14042 ++ ret = -EINVAL;
14043 ++ break;
14044 + }
14045 +-
14046 +- dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
14047 +- fe->dai_link->name, cmd);
14048 +-
14049 +- ret = soc_pcm_trigger(substream, cmd);
14050 + break;
14051 + case SND_SOC_DPCM_TRIGGER_BESPOKE:
14052 + /* bespoke trigger() - handles both FE and BEs */
14053 +@@ -2167,10 +2206,6 @@ static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
14054 + fe->dai_link->name, cmd);
14055 +
14056 + ret = soc_pcm_bespoke_trigger(substream, cmd);
14057 +- if (ret < 0) {
14058 +- dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
14059 +- goto out;
14060 +- }
14061 + break;
14062 + default:
14063 + dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
14064 +@@ -2179,6 +2214,12 @@ static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
14065 + goto out;
14066 + }
14067 +
14068 ++ if (ret < 0) {
14069 ++ dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
14070 ++ cmd, ret);
14071 ++ goto out;
14072 ++ }
14073 ++
14074 + switch (cmd) {
14075 + case SNDRV_PCM_TRIGGER_START:
14076 + case SNDRV_PCM_TRIGGER_RESUME:
14077 +diff --git a/tools/kvm/kvm_stat/kvm_stat b/tools/kvm/kvm_stat/kvm_stat
14078 +index c0d653d36c0f..fb02aa4591eb 100755
14079 +--- a/tools/kvm/kvm_stat/kvm_stat
14080 ++++ b/tools/kvm/kvm_stat/kvm_stat
14081 +@@ -261,6 +261,7 @@ class ArchX86(Arch):
14082 + def __init__(self, exit_reasons):
14083 + self.sc_perf_evt_open = 298
14084 + self.ioctl_numbers = IOCTL_NUMBERS
14085 ++ self.exit_reason_field = 'exit_reason'
14086 + self.exit_reasons = exit_reasons
14087 +
14088 +
14089 +@@ -276,6 +277,7 @@ class ArchPPC(Arch):
14090 + # numbers depend on the wordsize.
14091 + char_ptr_size = ctypes.sizeof(ctypes.c_char_p)
14092 + self.ioctl_numbers['SET_FILTER'] = 0x80002406 | char_ptr_size << 16
14093 ++ self.exit_reason_field = 'exit_nr'
14094 + self.exit_reasons = {}
14095 +
14096 +
14097 +@@ -283,6 +285,7 @@ class ArchA64(Arch):
14098 + def __init__(self):
14099 + self.sc_perf_evt_open = 241
14100 + self.ioctl_numbers = IOCTL_NUMBERS
14101 ++ self.exit_reason_field = 'esr_ec'
14102 + self.exit_reasons = AARCH64_EXIT_REASONS
14103 +
14104 +
14105 +@@ -290,6 +293,7 @@ class ArchS390(Arch):
14106 + def __init__(self):
14107 + self.sc_perf_evt_open = 331
14108 + self.ioctl_numbers = IOCTL_NUMBERS
14109 ++ self.exit_reason_field = None
14110 + self.exit_reasons = None
14111 +
14112 + ARCH = Arch.get_arch()
14113 +@@ -513,8 +517,8 @@ class TracepointProvider(Provider):
14114 + """
14115 + filters = {}
14116 + filters['kvm_userspace_exit'] = ('reason', USERSPACE_EXIT_REASONS)
14117 +- if ARCH.exit_reasons:
14118 +- filters['kvm_exit'] = ('exit_reason', ARCH.exit_reasons)
14119 ++ if ARCH.exit_reason_field and ARCH.exit_reasons:
14120 ++ filters['kvm_exit'] = (ARCH.exit_reason_field, ARCH.exit_reasons)
14121 + return filters
14122 +
14123 + def get_available_fields(self):
14124 +diff --git a/tools/power/acpi/Makefile.config b/tools/power/acpi/Makefile.config
14125 +index f304be71c278..fc116c060b98 100644
14126 +--- a/tools/power/acpi/Makefile.config
14127 ++++ b/tools/power/acpi/Makefile.config
14128 +@@ -18,7 +18,7 @@ include $(srctree)/../../scripts/Makefile.include
14129 +
14130 + OUTPUT=$(srctree)/
14131 + ifeq ("$(origin O)", "command line")
14132 +- OUTPUT := $(O)/power/acpi/
14133 ++ OUTPUT := $(O)/tools/power/acpi/
14134 + endif
14135 + #$(info Determined 'OUTPUT' to be $(OUTPUT))
14136 +
14137 +diff --git a/virt/kvm/arm/mmio.c b/virt/kvm/arm/mmio.c
14138 +index 3caee91bca08..878e0edb2e1b 100644
14139 +--- a/virt/kvm/arm/mmio.c
14140 ++++ b/virt/kvm/arm/mmio.c
14141 +@@ -117,6 +117,9 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
14142 + data = (data ^ mask) - mask;
14143 + }
14144 +
14145 ++ if (!vcpu->arch.mmio_decode.sixty_four)
14146 ++ data = data & 0xffffffff;
14147 ++
14148 + trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
14149 + &data);
14150 + data = vcpu_data_host_to_guest(vcpu, data, len);
14151 +@@ -137,6 +140,7 @@ static int decode_hsr(struct kvm_vcpu *vcpu, bool *is_write, int *len)
14152 + unsigned long rt;
14153 + int access_size;
14154 + bool sign_extend;
14155 ++ bool sixty_four;
14156 +
14157 + if (kvm_vcpu_dabt_iss1tw(vcpu)) {
14158 + /* page table accesses IO mem: tell guest to fix its TTBR */
14159 +@@ -150,11 +154,13 @@ static int decode_hsr(struct kvm_vcpu *vcpu, bool *is_write, int *len)
14160 +
14161 + *is_write = kvm_vcpu_dabt_iswrite(vcpu);
14162 + sign_extend = kvm_vcpu_dabt_issext(vcpu);
14163 ++ sixty_four = kvm_vcpu_dabt_issf(vcpu);
14164 + rt = kvm_vcpu_dabt_get_rd(vcpu);
14165 +
14166 + *len = access_size;
14167 + vcpu->arch.mmio_decode.sign_extend = sign_extend;
14168 + vcpu->arch.mmio_decode.rt = rt;
14169 ++ vcpu->arch.mmio_decode.sixty_four = sixty_four;
14170 +
14171 + return 0;
14172 + }
14173 +diff --git a/virt/kvm/arm/mmu.c b/virt/kvm/arm/mmu.c
14174 +index 9f69202d8e49..3814cdad643a 100644
14175 +--- a/virt/kvm/arm/mmu.c
14176 ++++ b/virt/kvm/arm/mmu.c
14177 +@@ -1736,7 +1736,8 @@ int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
14178 + if (!kvm->arch.pgd)
14179 + return 0;
14180 + trace_kvm_test_age_hva(hva);
14181 +- return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
14182 ++ return handle_hva_to_gpa(kvm, hva, hva + PAGE_SIZE,
14183 ++ kvm_test_age_hva_handler, NULL);
14184 + }
14185 +
14186 + void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
14187 +diff --git a/virt/kvm/arm/vgic/vgic-its.c b/virt/kvm/arm/vgic/vgic-its.c
14188 +index 526d808ecbbd..8354ec4ef912 100644
14189 +--- a/virt/kvm/arm/vgic/vgic-its.c
14190 ++++ b/virt/kvm/arm/vgic/vgic-its.c
14191 +@@ -2210,7 +2210,8 @@ static int vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa, int esz)
14192 + target_addr = (u32)(val >> KVM_ITS_CTE_RDBASE_SHIFT);
14193 + coll_id = val & KVM_ITS_CTE_ICID_MASK;
14194 +
14195 +- if (target_addr >= atomic_read(&kvm->online_vcpus))
14196 ++ if (target_addr != COLLECTION_NOT_MAPPED &&
14197 ++ target_addr >= atomic_read(&kvm->online_vcpus))
14198 + return -EINVAL;
14199 +
14200 + collection = find_collection(its, coll_id);
14201 +diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
14202 +index deff4b3eb972..745ee09083dd 100644
14203 +--- a/virt/kvm/kvm_main.c
14204 ++++ b/virt/kvm/kvm_main.c
14205 +@@ -1277,14 +1277,14 @@ bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
14206 + }
14207 + EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
14208 +
14209 +-unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
14210 ++unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn)
14211 + {
14212 + struct vm_area_struct *vma;
14213 + unsigned long addr, size;
14214 +
14215 + size = PAGE_SIZE;
14216 +
14217 +- addr = gfn_to_hva(kvm, gfn);
14218 ++ addr = kvm_vcpu_gfn_to_hva_prot(vcpu, gfn, NULL);
14219 + if (kvm_is_error_hva(addr))
14220 + return PAGE_SIZE;
14221 +