-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheCS.patch
3540 lines (3336 loc) · 105 KB
/
eCS.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 323cb06..be83b9b 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -747,6 +747,15 @@ config PARAVIRT_SPINLOCKS
If you are unsure how to answer this question, answer Y.
+# eCS
+config PARAVIRT_WAIT_HYPERCALL
+ bool "wait hypercall"
+ depends on PARAVIRT_SPINLOCKS
+ default n
+ ---help---
+ Paravirtualized wait hypercall.
+###
+
config QUEUED_LOCK_STAT
bool "Paravirt queued spinlock statistics"
depends on PARAVIRT_SPINLOCKS && DEBUG_FS
@@ -755,6 +764,16 @@ config QUEUED_LOCK_STAT
behavior of paravirtualized queued spinlocks and report
them on debugfs.
+# eCS
+config QUEUED_NUMA_LOCK_STEAL
+ bool "NUMA aware lock stealing"
+ depends on ARCH_USE_QUEUED_SPINLOCKS && PARAVIRT_SPINLOCKS
+ default n
+ ---help---
+ Only allow the steals from the same NUMA domain to steal the lock
+ to mitigate the cacheline bouncing.
+###
+
source "arch/x86/xen/Kconfig"
config KVM_GUEST
@@ -780,6 +799,86 @@ config KVM_DEBUG_FS
source "arch/x86/lguest/Kconfig"
+# eCS
+config PARAVIRT_ONLY_VCS
+ bool "Only expose virtualized critical sections, disable other pvops"
+ depends on PARAVIRT
+ select PARAVIRT_VCS
+ default n
+ ---help---
+ This option enables sharing the information about the critical sections
+ that are defined by the guest OS to be effectively scheduled as the hypervisor
+ will provide an extra schedule and will later penalize the guest OS. This can
+ be beneficial if the guest OS properly defines those VCS.
+
+config PARAVIRT_VCS
+ bool "Expose virtualized critical sections"
+ depends on PARAVIRT
+ select PARAVIRT_TIME_ACCOUNTING
+ default n
+ ---help---
+ This option enables sharing the information about the critical sections
+ that are defined by the guest OS to be effectively scheduled as the hypervisor
+ will provide an extra schedule and will later penalize the guest OS. This can
+ be beneficial if the guest OS properly defines those VCS.
+
+config PARAVIRT_RCU_VCS
+ bool "Expose RCU operation"
+ depends on PARAVIRT_VCS
+ default y
+ ---help---
+ Expose only RCU readers to the hypervisor
+
+config PARAVIRT_SPINLOCK_VCS
+ bool "Expose spinlock operation"
+ depends on PARAVIRT_VCS
+ default y
+ ---help---
+ Expose spinlock operations to the hypervisor
+
+config PARAVIRT_RWLOCK_WR_VCS
+ bool "Expose RWLOCK writers"
+ depends on PARAVIRT_VCS
+ default y
+ ---help---
+ Expose rwlock writers to the hypervisor
+
+config PARAVIRT_RWLOCK_RD_VCS
+ bool "Expose RWLOCK readers"
+ depends on PARAVIRT_VCS
+ default y
+ ---help---
+ Expose rwlock readers to the hypervisor
+
+config PARAVIRT_MUTEX_VCS
+ bool "Expose mutex operation"
+ depends on PARAVIRT_VCS
+ default y
+ ---help---
+ Expose mutex to the hypervisor
+
+config PARAVIRT_RWSEM_WR_VCS
+ bool "Expose RWSEM writers"
+ depends on PARAVIRT_VCS
+ default y
+ ---help---
+ Expose rwsem writers to the hypervisor
+
+config PARAVIRT_RWSEM_RD_VCS
+ bool "Expose readers of rwsem"
+ depends on PARAVIRT_VCS
+ default y
+ ---help---
+ Only expose readers of the rwsem primitive
+
+config PARAVIRT_INTR_CTX_VCS
+ bool "Expose interrupt context"
+ depends on PARAVIRT_VCS
+ default y
+ ---help---
+ Expose the interrupt context to the hypervisor
+###
+
config PARAVIRT_TIME_ACCOUNTING
bool "Paravirtual steal time accounting"
depends on PARAVIRT
@@ -792,6 +891,25 @@ config PARAVIRT_TIME_ACCOUNTING
If in doubt, say N here.
+#eCS
+config PARAVIRT_IPI
+ bool "Enable paravirtual interface for IPI"
+ depends on PARAVIRT
+ default n
+ ---help---
+ This option enables paravirtual interface for the IPI via the hypercall.
+ This is beneficial for the multicast IPI as it amortizes the multicast IPI
+ to a single multicast IPI. Moreover, it also enables asynchronous approach
+ to waiting for the ack if the VM has been preempted.
+
+config PARAVIRT_TLB
+ bool "Enable paravirtual interface for TLB shootdown"
+ depends on PARAVIRT_IPI
+ default n
+ ---help---
+ This option uses a single hypercall and issues invvpid on the hypervisor side.
+###
+
config PARAVIRT_CLOCK
bool
diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h
index 766a521..89e084f 100644
--- a/arch/x86/boot/compressed/misc.h
+++ b/arch/x86/boot/compressed/misc.h
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
#ifndef BOOT_COMPRESSED_MISC_H
#define BOOT_COMPRESSED_MISC_H
@@ -9,6 +10,13 @@
*/
#undef CONFIG_PARAVIRT
#undef CONFIG_PARAVIRT_SPINLOCKS
+/* eCS */
+#undef CONFIG_PARAVIRT_VCS
+#undef CONFIG_PARAVIRT_RCU_VCS
+#undef CONFIG_PARAVIRT_SPINLOCK_VCS
+#undef CONFIG_PARAVIRT_RWLOCK_WR_VCS
+#undef CONFIG_PARAVIRT_RWLOCK_RD_VCS
+/*******/
#undef CONFIG_KASAN
#include <linux/linkage.h>
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 92c9032..06ed475 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Kernel-based Virtual Machine driver for Linux
*
@@ -35,8 +36,10 @@
#include <asm/asm.h>
#include <asm/kvm_page_track.h>
-#define KVM_MAX_VCPUS 288
-#define KVM_SOFT_MAX_VCPUS 240
+/* eCS */
+#define KVM_MAX_VCPUS 512
+#define KVM_SOFT_MAX_VCPUS 384
+/*******/
#define KVM_MAX_VCPU_ID 1023
#define KVM_USER_MEM_SLOTS 509
/* memory slots that are not exposed to userspace */
@@ -590,6 +593,15 @@ struct kvm_vcpu_arch {
struct kvm_steal_time steal;
} st;
+/* eCS */
+#ifdef CONFIG_PARAVIRT_IPI
+ struct {
+ struct gfn_to_hva_cache cpus;
+ struct kvm_ipi_cpu_list cpu_list;
+ } ipi_cpu_list;
+#endif
+/*******/
+
u64 tsc_offset;
u64 last_guest_tsc;
u64 last_host_tsc;
@@ -874,6 +886,11 @@ struct kvm_vcpu_stat {
u64 irq_injections;
u64 nmi_injections;
u64 req_event;
+/* eCS */
+ u64 fake_vcs;
+ u64 vcs;
+ u64 avoided_preempts;
+/*******/
};
struct x86_instruction_info;
@@ -950,6 +967,11 @@ struct kvm_x86_ops {
u32 (*get_pkru)(struct kvm_vcpu *vcpu);
void (*tlb_flush)(struct kvm_vcpu *vcpu);
+/* eCS */
+ void (*tlb_flush_addr)(struct kvm_vcpu *vcpu, unsigned long addr);
+
+ void (*get_vpid)(struct kvm_vcpu *vcpu);
+/*******/
void (*run)(struct kvm_vcpu *vcpu);
int (*handle_exit)(struct kvm_vcpu *vcpu);
diff --git a/arch/x86/include/asm/kvm_para.h b/arch/x86/include/asm/kvm_para.h
index bc62e7c..1ecc34c 100644
--- a/arch/x86/include/asm/kvm_para.h
+++ b/arch/x86/include/asm/kvm_para.h
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _ASM_X86_KVM_PARA_H
#define _ASM_X86_KVM_PARA_H
@@ -101,6 +102,29 @@ static inline void kvm_spinlock_init(void)
}
#endif /* CONFIG_PARAVIRT_SPINLOCKS */
+/* eCS */
+#ifdef CONFIG_PARAVIRT_VCS
+void __init kvm_sched_init(void);
+#else
+static inline void __init kvm_sched_init(void)
+{
+}
+#endif /* CONFIG_PARAVIRT_VCS */
+
+#ifdef CONFIG_PARAVIRT_IPI
+void kvm_ipi_init(void);
+void kvm_disable_ipi(void);
+#else
+static inline void kvm_ipi_init(void)
+{
+}
+static inline void kvm_disable_ipi(void)
+{
+ return;
+}
+#endif
+/*******/
+
#else /* CONFIG_KVM_GUEST */
#define kvm_guest_init() do {} while (0)
#define kvm_async_pf_task_wait(T) do {} while(0)
diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravirt.h
index 9ccac19..9009c7e 100644
--- a/arch/x86/include/asm/paravirt.h
+++ b/arch/x86/include/asm/paravirt.h
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _ASM_X86_PARAVIRT_H
#define _ASM_X86_PARAVIRT_H
/* Various instructions on x86 need to be replaced for
@@ -15,6 +16,21 @@
#include <linux/cpumask.h>
#include <asm/frame.h>
+/* eCS */
+#define KVM_NO_CS (0)
+#define KVM_RCU_READER (1 << 1)
+#define KVM_RCU_WRITER (1 << 2)
+#define KVM_RWL_READER (1 << 3)
+#define KVM_SPN_HOLDER (1 << 5)
+#define KVM_SPN_WAITER (1 << 6)
+#define KVM_RWL_WRITER (1 << 7)
+#define KVM_INTR_CNTXT (1 << 8)
+
+#define KVM_MUTEX_HOLDER (1 << 1)
+#define KVM_RWSEM_WRITER (1 << 2)
+#define KVM_RWSEM_READER (1 << 3)
+/*******/
+
static inline void load_sp0(struct tss_struct *tss,
struct thread_struct *thread)
{
@@ -727,6 +743,143 @@ static __always_inline bool pv_vcpu_is_preempted(long cpu)
#endif /* SMP && PARAVIRT_SPINLOCKS */
+/* eCS */
+#if defined(CONFIG_PARAVIRT_VCS)
+
+static __always_inline bool pv_vcpu_is_preempted_other(long cpu)
+{
+ return PVOP_CALLEE1(bool, pv_sched_ops.vcpu_is_preempted, cpu);
+}
+
+static __always_inline bool pv_pcpu_is_overloaded(long cpu)
+{
+ return PVOP_CALLEE1(bool, pv_sched_ops.pcpu_is_overloaded, cpu);
+}
+
+static __always_inline int pv_vcpu_get_fake_preempt_count(long cpu)
+{
+ return PVOP_CALLEE1(__u32, pv_sched_ops.vcpu_get_fake_preempt_count,
+ cpu);
+}
+
+static __always_inline void pv_vcpu_preempt_count(long cpu, int update,
+ uint16_t type)
+{
+ PVOP_VCALL3(pv_sched_ops.vcpu_preempt_count, cpu, update, type);
+}
+
+static __always_inline void pv_vcpu_fake_preempt_count(long cpu, int update,
+ uint8_t type)
+{
+ PVOP_VCALL3(pv_sched_ops.vcpu_fake_preempt_count, cpu, update, type);
+}
+
+static __always_inline void pv_vcpu_inc_fake_preempt_count(long cpu, uint8_t type)
+{
+ pv_vcpu_fake_preempt_count(cpu, 1, type);
+}
+
+static __always_inline void pv_vcpu_dec_fake_preempt_count(long cpu, uint8_t type)
+{
+ pv_vcpu_fake_preempt_count(cpu, -1, type);
+}
+
+#define vcpu_get_fake_preempt_count vcpu_get_fake_preempt_count
+static inline int vcpu_get_fake_preempt_count(long cpu)
+{
+ return pv_vcpu_get_fake_preempt_count(cpu);
+}
+
+#define vcpu_is_preempted_other vcpu_is_preempted_other
+static inline bool vcpu_is_preempted_other(long cpu)
+{
+ return pv_vcpu_is_preempted_other(cpu);
+}
+
+#define vcpu_pcpu_is_overloaded vcpu_pcpu_is_overloaded
+static inline bool vcpu_pcpu_is_overloaded(long cpu)
+{
+ return pv_pcpu_is_overloaded(cpu);
+}
+
+#define vcpu_preempt_count vcpu_preempt_count
+static inline void vcpu_preempt_count(long cpu, int update, uint16_t type)
+{
+ pv_vcpu_preempt_count(cpu, update, type);
+}
+
+#define vcpu_inc_fake_preempt_count vcpu_inc_fake_preempt_count
+static inline void vcpu_inc_fake_preempt_count(long cpu, uint8_t type)
+{
+ pv_vcpu_inc_fake_preempt_count(cpu, type);
+}
+
+#define vcpu_dec_fake_preempt_count vcpu_dec_fake_preempt_count
+static inline void vcpu_dec_fake_preempt_count(long cpu, uint8_t type)
+{
+ pv_vcpu_dec_fake_preempt_count(cpu, type);
+}
+
+#define vcpu_update_fake_preempt_count vcpu_update_fake_preempt_count
+static inline void vcpu_update_fake_preempt_count(long cpu, int update,
+ uint16_t type)
+{
+ pv_vcpu_fake_preempt_count(cpu, update, type);
+}
+
+#endif /* CONFIG_PARAVIRT_VCS */
+
+#ifdef CONFIG_PARAVIRT_IPI
+
+#define KVM_VCPU_IPI_CPULIST_REG 1
+#define KVM_VCPU_IPI_CPULIST_DEREG 2
+#define KVM_VCPU_IPI_ISSUE 3
+#define KVM_VCPU_APICID_REG 4
+
+static __always_inline void pv_vcpu_handle_ipi(int op, u64 val1, u64 val2)
+{
+ PVOP_VCALL3(pv_ipi_ops.handle_ipi, op, val1, val2);
+}
+
+#define vcpu_setup_ipi vcpu_setup_ipi
+static inline void vcpu_setup_ipi(u64 paddr)
+{
+ pv_vcpu_handle_ipi(KVM_VCPU_IPI_CPULIST_REG, paddr, 0);
+}
+
+#define vcpu_deinit_ipi vcpu_deinit_ipi
+static inline void vcpu_deinit_ipi(void)
+{
+ pv_vcpu_handle_ipi(KVM_VCPU_IPI_CPULIST_DEREG, 0, 0);
+}
+
+#define vcpu_update_apicid vcpu_update_apicid
+static inline void vcpu_update_apicid(u64 vcpu_id, u64 apicid)
+{
+ pv_vcpu_handle_ipi(KVM_VCPU_APICID_REG, vcpu_id, apicid);
+}
+
+#define vcpu_issue_ipi vcpu_issue_ipi
+static inline void vcpu_issue_ipi(u32 msr, u64 data)
+{
+ pv_vcpu_handle_ipi(KVM_VCPU_IPI_ISSUE, msr, data);
+}
+
+#define send_call_function_ipi_mask send_call_function_ipi_mask
+static inline void send_call_function_ipi_mask(const struct cpumask *mask)
+{
+ PVOP_VCALL1(pv_ipi_ops.issue_ipi, mask);
+}
+
+#define update_ipi_cpumask update_ipi_cpumask
+static inline void update_ipi_cpumask(struct cpumask *mask)
+{
+ PVOP_VCALL1(pv_ipi_ops.update_cpumask, mask);
+}
+
+#endif /* CONFIG_PARAVIRT_IPI */
+/*******/
+
#ifdef CONFIG_X86_32
#define PV_SAVE_REGS "pushl %ecx; pushl %edx;"
#define PV_RESTORE_REGS "popl %edx; popl %ecx;"
diff --git a/arch/x86/include/asm/paravirt_types.h b/arch/x86/include/asm/paravirt_types.h
index 9ffc36b..c601d2c 100644
--- a/arch/x86/include/asm/paravirt_types.h
+++ b/arch/x86/include/asm/paravirt_types.h
@@ -1,3 +1,4 @@
+/* SPDX-Licence-Identifier: GPL-2.0-only */
#ifndef _ASM_X86_PARAVIRT_TYPES_H
#define _ASM_X86_PARAVIRT_TYPES_H
@@ -307,6 +308,26 @@ struct pv_mmu_ops {
phys_addr_t phys, pgprot_t flags);
} __no_randomize_layout;
+/* eCS */
+#ifdef CONFIG_PARAVIRT_VCS
+struct pv_sched_ops {
+ struct paravirt_callee_save vcpu_is_preempted;
+ struct paravirt_callee_save pcpu_is_overloaded;
+ struct paravirt_callee_save vcpu_get_fake_preempt_count;
+ struct paravirt_callee_save vcpu_preempt_count;
+ struct paravirt_callee_save vcpu_fake_preempt_count;
+} __no_randomize_layout;
+#endif
+
+#ifdef CONFIG_PARAVIRT_IPI
+struct pv_ipi_ops {
+ void (*handle_ipi)(int op, u64 val1, u64 val2);
+ void (*issue_ipi)(const struct cpumask *mask);
+ void (*update_cpumask)(struct cpumask *mask);
+} __no_randomize_layout;
+#endif
+/*******/
+
struct arch_spinlock;
#ifdef CONFIG_SMP
#include <asm/spinlock_types.h>
@@ -334,6 +355,14 @@ struct paravirt_patch_template {
struct pv_irq_ops pv_irq_ops;
struct pv_mmu_ops pv_mmu_ops;
struct pv_lock_ops pv_lock_ops;
+/* eCS */
+#ifdef CONFIG_PARAVIRT_VCS
+ struct pv_sched_ops pv_sched_ops;
+#endif
+#ifdef CONFIG_PARAVIRT_IPI
+ struct pv_ipi_ops pv_ipi_ops;
+#endif
+/*******/
} __no_randomize_layout;
extern struct pv_info pv_info;
@@ -343,6 +372,14 @@ struct paravirt_patch_template {
extern struct pv_irq_ops pv_irq_ops;
extern struct pv_mmu_ops pv_mmu_ops;
extern struct pv_lock_ops pv_lock_ops;
+/* eCS */
+#ifdef CONFIG_PARAVIRT_VCS
+extern struct pv_sched_ops pv_sched_ops;
+#endif
+#ifdef CONFIG_PARAVIRT_IPI
+extern struct pv_ipi_ops pv_ipi_ops;
+#endif
+/*******/
#define PARAVIRT_PATCH(x) \
(offsetof(struct paravirt_patch_template, x) / sizeof(void *))
diff --git a/arch/x86/include/asm/qspinlock.h b/arch/x86/include/asm/qspinlock.h
index 48a706f..57cd785 100644
--- a/arch/x86/include/asm/qspinlock.h
+++ b/arch/x86/include/asm/qspinlock.h
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _ASM_X86_QSPINLOCK_H
#define _ASM_X86_QSPINLOCK_H
@@ -17,6 +18,12 @@ static inline void native_queued_spin_unlock(struct qspinlock *lock)
smp_store_release((u8 *)lock, 0);
}
+/* eCS */
+#if defined(CONFIG_PARAVIRT_SPINLOCKS) || defined(CONFIG_PARAVIRT_VCS)
+DECLARE_PER_CPU_READ_MOSTLY(int, cpu_number);
+#endif
+/*******/
+
#ifdef CONFIG_PARAVIRT_SPINLOCKS
extern void native_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
extern void __pv_init_lock_hash(void);
@@ -26,11 +33,21 @@ static inline void native_queued_spin_unlock(struct qspinlock *lock)
static inline void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
{
pv_queued_spin_lock_slowpath(lock, val);
+/* eCS */
+#ifdef CONFIG_PARAVIRT_SPINLOCK_VCS
+ vcpu_preempt_count(this_cpu_read(cpu_number), 1, KVM_SPN_HOLDER);
+#endif
+/*******/
}
static inline void queued_spin_unlock(struct qspinlock *lock)
{
pv_queued_spin_unlock(lock);
+/* eCS */
+#ifdef CONFIG_PARAVIRT_SPINLOCK_VCS
+ vcpu_preempt_count(this_cpu_read(cpu_number), -1, KVM_NO_CS);
+#endif
+/*******/
}
#define vcpu_is_preempted vcpu_is_preempted
@@ -38,10 +55,16 @@ static inline bool vcpu_is_preempted(long cpu)
{
return pv_vcpu_is_preempted(cpu);
}
+
#else
static inline void queued_spin_unlock(struct qspinlock *lock)
{
native_queued_spin_unlock(lock);
+/* eCS */
+#ifdef CONFIG_PARAVIRT_SPINLOCK_VCS
+ vcpu_preempt_count(this_cpu_read(cpu_number), -1, KVM_NO_CS);
+#endif
+/*******/
}
#endif
diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
index 50ea348..b4bd5d0 100644
--- a/arch/x86/include/asm/tlbflush.h
+++ b/arch/x86/include/asm/tlbflush.h
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _ASM_X86_TLBFLUSH_H
#define _ASM_X86_TLBFLUSH_H
@@ -256,6 +257,13 @@ static inline void flush_tlb_page(struct vm_area_struct *vma, unsigned long a)
void native_flush_tlb_others(const struct cpumask *cpumask,
const struct flush_tlb_info *info);
+/* eCS */
+#ifdef CONFIG_PARAVIRT_TLB
+void paravirt_flush_tlb_others(const struct cpumask *cpumask,
+ const struct flush_tlb_info *info);
+#endif
+/*******/
+
#define TLBSTATE_OK 1
#define TLBSTATE_LAZY 2
diff --git a/arch/x86/include/uapi/asm/kvm_para.h b/arch/x86/include/uapi/asm/kvm_para.h
index a965e5b..0cdc4d7 100644
--- a/arch/x86/include/uapi/asm/kvm_para.h
+++ b/arch/x86/include/uapi/asm/kvm_para.h
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _UAPI_ASM_X86_KVM_PARA_H
#define _UAPI_ASM_X86_KVM_PARA_H
@@ -41,14 +42,24 @@
#define MSR_KVM_STEAL_TIME 0x4b564d03
#define MSR_KVM_PV_EOI_EN 0x4b564d04
+/* eCS */
struct kvm_steal_time {
- __u64 steal;
- __u32 version;
- __u32 flags;
- __u8 preempted;
- __u8 u8_pad[3];
- __u32 pad[11];
+ __u64 steal;
+ __u32 version;
+ __u32 flags;
+ __u32 preempt_count; /* used by lock holders and rcu readers */
+ __u32 fake_preempt_count; /* used by rwsem, mutex lock holders */
+ __u32 user_preempt_count; /* to be used by glibc mutex/rwsem */
+ __u32 ordo_boundary; /* ORDO_BOUNDARY for the applications */
+ __u16 preempt_type;
+ __u16 fake_preempt_type;
+ __u8 preempt_toggle;
+ __u8 preempted;
+ __u8 overloaded;
+ __u8 pad1[1];
+ __u32 pad[6];
};
+/*******/
#define KVM_CLOCK_PAIRING_WALLCLOCK 0
struct kvm_clock_pairing {
@@ -109,5 +120,17 @@ struct kvm_vcpu_pv_apf_data {
#define KVM_PV_EOI_ENABLED KVM_PV_EOI_MASK
#define KVM_PV_EOI_DISABLED 0x0
+/* eCS */
+struct kvm_ipi_cpu_list {
+ cpumask_t cpus;
+} ____cacheline_aligned_in_smp;
+
+
+struct kvm_tlb_info {
+ struct kvm_vcpu *vcpu;
+ unsigned long flush_start;
+ unsigned long flush_end;
+};
+/*******/
#endif /* _UAPI_ASM_X86_KVM_PARA_H */
diff --git a/arch/x86/kernel/asm-offsets_64.c b/arch/x86/kernel/asm-offsets_64.c
index 99332f5..b707a58 100644
--- a/arch/x86/kernel/asm-offsets_64.c
+++ b/arch/x86/kernel/asm-offsets_64.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0-only
#ifndef __LINUX_KBUILD_H
# error "Please do not build this file directly, build asm-offsets.c instead"
#endif
@@ -13,7 +14,9 @@
#include <asm/syscalls_32.h>
};
-#if defined(CONFIG_KVM_GUEST) && defined(CONFIG_PARAVIRT_SPINLOCKS)
+/* eCS */
+#if defined(CONFIG_KVM_GUEST) && (defined(CONFIG_PARAVIRT_SPINLOCKS) || defined(CONFIG_PARAVIRT_VCS))
+/*******/
#include <asm/kvm_para.h>
#endif
@@ -29,6 +32,13 @@ int main(void)
#if defined(CONFIG_KVM_GUEST) && defined(CONFIG_PARAVIRT_SPINLOCKS)
OFFSET(KVM_STEAL_TIME_preempted, kvm_steal_time, preempted);
BLANK();
+/* eCS */
+#endif
+
+#if defined(CONFIG_KVM_GUEST) && defined(CONFIG_PARAVIRT_VCS)
+ OFFSET(KVM_STEAL_TIME_preempted, kvm_steal_time, preempted);
+ BLANK();
+/*******/
#endif
#define ENTRY(entry) OFFSET(pt_regs_ ## entry, pt_regs, entry)
diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
index d04e30e..62e1d78 100644
--- a/arch/x86/kernel/kvm.c
+++ b/arch/x86/kernel/kvm.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* KVM paravirt_ops implementation
*
@@ -79,6 +80,12 @@ static int parse_no_kvmclock_vsyscall(char *arg)
static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64);
static int has_steal_clock = 0;
+/* eCS */
+#ifdef CONFIG_PARAVIRT_IPI
+DEFINE_PER_CPU(struct kvm_ipi_cpu_list, ipi_cpu_list) __aligned(64);
+#endif
+/*******/
+
/*
* No need for any "IO delay" on KVM
*/
@@ -325,7 +332,10 @@ static void kvm_guest_cpu_init(void)
{
if (!kvm_para_available())
return;
-
+/* eCS */
+#if defined(CONFIG_PARAVIRT_ONLY_VCS)
+#else
+/*******/
if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
@@ -342,7 +352,9 @@ static void kvm_guest_cpu_init(void)
printk(KERN_INFO"KVM setup async PF for cpu %d\n",
smp_processor_id());
}
-
+/* eCS */
+#endif
+/*******/
if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
unsigned long pa;
/* Size alignment is implied but just to make it explicit. */
@@ -352,9 +364,12 @@ static void kvm_guest_cpu_init(void)
| KVM_MSR_ENABLED;
wrmsrl(MSR_KVM_PV_EOI_EN, pa);
}
-
if (has_steal_clock)
kvm_register_steal_time();
+/* eCS */
+
+ kvm_ipi_init();
+/*******/
}
static void kvm_pv_disable_apf(void)
@@ -378,7 +393,15 @@ static void kvm_pv_guest_cpu_reboot(void *unused)
*/
if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
wrmsrl(MSR_KVM_PV_EOI_EN, 0);
+/* eCS */
+
+#if defined(CONFIG_PARAVIRT_ONLY_VCS)
+#else
+/*******/
kvm_pv_disable_apf();
+/* eCS */
+#endif
+/*******/
kvm_disable_steal_time();
}
@@ -425,15 +448,28 @@ static void __init kvm_smp_prepare_boot_cpu(void)
kvm_guest_cpu_init();
native_smp_prepare_boot_cpu();
kvm_spinlock_init();
+/* eCS */
+ kvm_sched_init();
+/*******/
}
static void kvm_guest_cpu_offline(void)
{
kvm_disable_steal_time();
+/* eCS */
+ kvm_disable_ipi();
+/*******/
if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
wrmsrl(MSR_KVM_PV_EOI_EN, 0);
kvm_pv_disable_apf();
+/* eCS */
+#if defined(CONFIG_PARAVIRT_ONLY_VCS)
+#else
+/*******/
apf_task_wake_all();
+/* eCS */
+#endif
+/*******/
}
static int kvm_cpu_online(unsigned int cpu)
@@ -453,10 +489,16 @@ static int kvm_cpu_down_prepare(unsigned int cpu)
}
#endif
+/* eCS */
+#ifndef CONFIG_PARAVIRT_ONLY_VCS
+/*******/
static void __init kvm_apf_trap_init(void)
{
set_intr_gate(14, async_page_fault);
}
+/* eCS */
+#endif
+/*******/
void __init kvm_guest_init(void)
{
@@ -469,8 +511,16 @@ void __init kvm_guest_init(void)
register_reboot_notifier(&kvm_pv_reboot_nb);
for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
raw_spin_lock_init(&async_pf_sleepers[i].lock);
+
if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
+/* eCS */
+#ifdef CONFIG_PARAVIRT_ONLY_VCS
+#else
+/*******/
x86_init.irqs.trap_init = kvm_apf_trap_init;
+/* eCS */
+#endif
+/*******/
if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
has_steal_clock = 1;
@@ -556,6 +606,150 @@ static __init int activate_jump_labels(void)
}
arch_initcall(activate_jump_labels);
+/* eCS */
+#ifdef CONFIG_PARAVIRT_IPI
+
+static void kvm_handle_ipi(int op, u64 val1, u64 val2)
+{
+ kvm_hypercall3(KVM_HC_IPI_DELIVERY, op, val1, val2);
+}
+
+static void kvm_issue_ipi(const struct cpumask *mask)
+{
+ int cpu = smp_processor_id();
+ struct kvm_ipi_cpu_list *icl = &per_cpu(ipi_cpu_list, cpu);
+
+ smp_mb();
+ cpumask_clear(&icl->cpus);
+ cpumask_copy(&icl->cpus, mask);
+ vcpu_issue_ipi(APIC_BASE_MSR + (APIC_ICR >> 4),
+ APIC_DEST_PHYSICAL | CALL_FUNCTION_VECTOR |
+ APIC_DM_FIXED);
+}
+
+static void kvm_ipi_update_cpumask(struct cpumask *mask)
+{
+ int cpu = smp_processor_id();
+ struct kvm_ipi_cpu_list *icl = &per_cpu(ipi_cpu_list, cpu);
+
+ cpumask_copy(mask, &icl->cpus);
+}
+
+void kvm_ipi_init(void)
+{
+ int cpu = smp_processor_id();
+ struct kvm_ipi_cpu_list *icl = &per_cpu(ipi_cpu_list, cpu);
+ u64 data = per_cpu(x86_cpu_to_apicid, cpu);
+
+ if (!kvm_para_available())
+ return;
+
+ pv_ipi_ops.handle_ipi = kvm_handle_ipi;
+ pv_ipi_ops.issue_ipi = kvm_issue_ipi;
+ pv_ipi_ops.update_cpumask = kvm_ipi_update_cpumask;
+
+#ifdef CONFIG_PARAVIRT_TLB
+ pv_mmu_ops.flush_tlb_others = paravirt_flush_tlb_others;
+#endif
+
+ vcpu_setup_ipi(slow_virt_to_phys(icl));
+ vcpu_update_apicid(cpu, data);
+ pr_info("kvm-paravirt-ipi: cpu %d, msr: %llx\n",
+ cpu, (unsigned long long) slow_virt_to_phys(icl));
+}
+
+void kvm_disable_ipi(void)
+{
+ vcpu_deinit_ipi();
+}
+
+#endif
+
+#ifdef CONFIG_PARAVIRT_VCS
+#ifdef CONFIG_X86_32
+__visible bool __kvm_vcpu_is_preempted_other(long cpu)
+{
+ struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
+
+ return !!src->preempted;
+}
+PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted_other);
+
+#else
+
+#include <asm/asm-offsets.h>
+
+extern bool __raw_callee_save___kvm_vcpu_is_preempted_other(long);
+
+/*
+ * Hand-optimize version for x86-64 to avoid 8 64-bit register saving and
+ * restoring to/from the stack.
+ */
+asm(
+".pushsection .text;"
+".global __raw_callee_save___kvm_vcpu_is_preempted_other;"
+".type __raw_callee_save___kvm_vcpu_is_preempted_other, @function;"
+"__raw_callee_save___kvm_vcpu_is_preempted_other:"
+"movq __per_cpu_offset(,%rdi,8), %rax;"
+"cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax);"
+"setne %al;"
+"ret;"
+".popsection");
+
+#endif
+
+/* XXX: hand optimization needed */
+__visible int __kvm_vcpu_get_fake_preempt_count(long cpu)
+{
+ struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
+
+ return src->fake_preempt_count;
+}
+PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_get_fake_preempt_count);
+
+__visible bool __kvm_vcpu_pcpu_is_overloaded(long cpu)
+{
+ struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
+ return !!src->overloaded;
+}
+PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_pcpu_is_overloaded);
+
+__visible void __kvm_vcpu_preempt_count(long cpu, int update, uint16_t type)
+{
+ struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
+ src->preempt_count += update;
+ /* src->preempt_type = type; */
+}
+PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_preempt_count);
+
+__visible void __kvm_vcpu_fake_preempt_count(long cpu, int update, uint8_t type)
+{
+ struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
+ src->fake_preempt_count += update;
+ /* src->fake_preempt_type = type; */
+}
+PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_fake_preempt_count);
+
+void __init kvm_sched_init(void)
+{
+ if (!kvm_para_available())
+ return;
+ if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
+ pv_sched_ops.vcpu_is_preempted =
+ PV_CALLEE_SAVE(__kvm_vcpu_is_preempted_other);
+ pv_sched_ops.pcpu_is_overloaded =
+ PV_CALLEE_SAVE(__kvm_vcpu_pcpu_is_overloaded);
+ pv_sched_ops.vcpu_get_fake_preempt_count =
+ PV_CALLEE_SAVE(__kvm_vcpu_get_fake_preempt_count);
+ pv_sched_ops.vcpu_preempt_count =
+ PV_CALLEE_SAVE(__kvm_vcpu_preempt_count);
+ pv_sched_ops.vcpu_fake_preempt_count =
+ PV_CALLEE_SAVE(__kvm_vcpu_fake_preempt_count);
+ }
+}
+#endif
+/*******/
+
#ifdef CONFIG_PARAVIRT_SPINLOCKS
/* Kick a cpu by its apicid. Used to wake up a halted vcpu */
@@ -587,10 +781,20 @@ static void kvm_wait(u8 *ptr, u8 val)
* for irq enabled case to avoid hang when lock info is overwritten
* in irq spinlock slowpath and no spurious interrupt occur to save us.
*/
+/* eCS */
+#ifndef CONFIG_PARAVIRT_WAIT_HYPERCALL
+/*******/
if (arch_irqs_disabled_flags(flags))
halt();
else
safe_halt();
+/* eCS */
+#else
+ if (!arch_irqs_disabled_flags(flags))
+ local_irq_disable();
+ kvm_hypercall0(KVM_HC_WAIT);
+#endif
+/*******/
out: