-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlinux-v5.4.17-2136.333.5.patch
4067 lines (3878 loc) · 195 KB
/
linux-v5.4.17-2136.333.5.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
From f1205fc45d2d158b4f0562c979a778e96f0670b0 Mon Sep 17 00:00:00 2001
From: Dongli Zhang <dongli.zhang0129@gmail.com>
Date: Thu, 9 Jan 2025 08:31:37 -0800
Subject: [PATCH 1/1] linux-v5.4.17-2136.333.5
Signed-off-by: Dongli Zhang <dongli.zhang0129@gmail.com>
---
arch/arm64/kvm/pmu-emul.c | 5 +
arch/x86/kvm/pmu.c | 16 +
arch/x86/kvm/pmu.h | 16 +
arch/x86/kvm/svm/pmu.c | 11 +
arch/x86/kvm/x86.c | 69 ++
drivers/gpu/drm/i915/gvt/kvmgt.c | 9 +
drivers/iommu/iommu.c | 45 +
drivers/vfio/vfio.c | 26 +
drivers/vfio/vfio_iommu_type1.c | 1540 ++++++++++++++++++++++++++++++
include/linux/mm_types.h | 18 +
include/linux/padata.h | 21 +
include/linux/vfio.h | 8 +
kernel/events/core.c | 129 +++
kernel/padata.c | 368 +++++++
mm/gup.c | 285 ++++++
mm/util.c | 54 ++
16 files changed, 2620 insertions(+)
diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
index 68409559aabd..f8db3d75c598 100644
--- a/arch/arm64/kvm/pmu-emul.c
+++ b/arch/arm64/kvm/pmu-emul.c
@@ -82,6 +82,11 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
* The real counter value is equal to the value of counter register plus
* the value perf event counts.
*/
+ /*
+ * called by:
+ * - arch/arm64/kvm/pmu-emul.c|86| <<kvm_pmu_get_counter_value>> counter += perf_event_read_value(pmc->perf_event, &enabled, &running);
+ * - arch/x86/kvm/pmu.h|56| <<pmc_read_counter>> counter += perf_event_read_value(pmc->perf_event, &enabled, &running);
+ */
if (pmc->perf_event)
counter += perf_event_read_value(pmc->perf_event, &enabled,
&running);
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index b8eba33c0cf7..5c863fd1cd03 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -384,6 +384,13 @@ void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu)
kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC);
}
+/*
+ * called by:
+ * - arch/x86/kvm/x86.c|3073| <<kvm_set_msr_common>> if (kvm_pmu_is_valid_msr(vcpu, msr))
+ * - arch/x86/kvm/x86.c|3134| <<kvm_set_msr_common>> if (kvm_pmu_is_valid_msr(vcpu, msr))
+ * - arch/x86/kvm/x86.c|3220| <<kvm_get_msr_common>> if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
+ * - arch/x86/kvm/x86.c|3403| <<kvm_get_msr_common>> if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
+ */
bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
{
return kvm_x86_ops.pmu_ops->msr_idx_to_pmc(vcpu, msr) ||
@@ -404,9 +411,18 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
return kvm_x86_ops.pmu_ops->get_msr(vcpu, msr_info);
}
+/*
+ * called by:
+ * - arch/x86/kvm/x86.c|3074| <<kvm_set_msr_common>> return kvm_pmu_set_msr(vcpu, msr_info);
+ * - arch/x86/kvm/x86.c|3135| <<kvm_set_msr_common>> return kvm_pmu_set_msr(vcpu, msr_info);
+ */
int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
{
kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
+ /*
+ * intel_pmu_set_msr()
+ * amd_pmu_set_msr()
+ */
return kvm_x86_ops.pmu_ops->set_msr(vcpu, msr_info);
}
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index f4fde6bcc8cc..4d63bd0c73de 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -47,11 +47,27 @@ static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
return pmu->counter_bitmask[pmc->type];
}
+/*
+ * called by:
+ * - arch/x86/kvm/pmu.c|377| <<kvm_pmu_rdpmc>> *data = pmc_read_counter(pmc) & mask;
+ * - arch/x86/kvm/pmu.h|75| <<pmc_stop_counter>> pmc->counter = pmc_read_counter(pmc);
+ * - arch/x86/kvm/svm/pmu.c|249| <<amd_pmu_get_msr>> msr_info->data = pmc_read_counter(pmc);
+ * - arch/x86/kvm/svm/pmu.c|272| <<amd_pmu_set_msr>> pmc->counter += data - pmc_read_counter(pmc);
+ * - arch/x86/kvm/vmx/pmu_intel.c|228| <<intel_pmu_get_msr>> u64 val = pmc_read_counter(pmc);
+ * - arch/x86/kvm/vmx/pmu_intel.c|233| <<intel_pmu_get_msr>> u64 val = pmc_read_counter(pmc);
+ * - arch/x86/kvm/vmx/pmu_intel.c|293| <<intel_pmu_set_msr>> pmc->counter += data - pmc_read_counter(pmc);
+ * - arch/x86/kvm/vmx/pmu_intel.c|297| <<intel_pmu_set_msr>> pmc->counter += data - pmc_read_counter(pmc);
+ */
static inline u64 pmc_read_counter(struct kvm_pmc *pmc)
{
u64 counter, enabled, running;
counter = pmc->counter;
+ /*
+ * called by:
+ * - arch/arm64/kvm/pmu-emul.c|86| <<kvm_pmu_get_counter_value>> counter += perf_event_read_value(pmc->perf_event, &enabled, &running);
+ * - arch/x86/kvm/pmu.h|56| <<pmc_read_counter>> counter += perf_event_read_value(pmc->perf_event, &enabled, &running);
+ */
if (pmc->perf_event && !pmc->is_paused)
counter += perf_event_read_value(pmc->perf_event,
&enabled, &running);
diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
index 663d943f85db..c2e35782cabd 100644
--- a/arch/x86/kvm/svm/pmu.c
+++ b/arch/x86/kvm/svm/pmu.c
@@ -269,6 +269,17 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
/* MSR_PERFCTRn */
pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);
if (pmc) {
+ /*
+ * called by:
+ * - arch/x86/kvm/pmu.c|377| <<kvm_pmu_rdpmc>> *data = pmc_read_counter(pmc) & mask;
+ * - arch/x86/kvm/pmu.h|75| <<pmc_stop_counter>> pmc->counter = pmc_read_counter(pmc);
+ * - arch/x86/kvm/svm/pmu.c|249| <<amd_pmu_get_msr>> msr_info->data = pmc_read_counter(pmc);
+ * - arch/x86/kvm/svm/pmu.c|272| <<amd_pmu_set_msr>> pmc->counter += data - pmc_read_counter(pmc);
+ * - arch/x86/kvm/vmx/pmu_intel.c|228| <<intel_pmu_get_msr>> u64 val = pmc_read_counter(pmc);
+ * - arch/x86/kvm/vmx/pmu_intel.c|233| <<intel_pmu_get_msr>> u64 val = pmc_read_counter(pmc);
+ * - arch/x86/kvm/vmx/pmu_intel.c|293| <<intel_pmu_set_msr>> pmc->counter += data - pmc_read_counter(pmc);
+ * - arch/x86/kvm/vmx/pmu_intel.c|297| <<intel_pmu_set_msr>> pmc->counter += data - pmc_read_counter(pmc);
+ */
pmc->counter += data - pmc_read_counter(pmc);
pmc_update_sample_period(pmc);
return 0;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 45da303ced50..d5d638d9f4ee 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -278,6 +278,12 @@ EXPORT_SYMBOL_GPL(x86_fpu_cache);
* Return 0 if we want to ignore/silent this failed msr access, or 1 if we want
* to fail the caller.
*/
+/*
+ * called by:
+ * - arch/x86/kvm/x86.c|1464| <<do_get_msr_feature>> r = kvm_msr_ignored_check(vcpu, index, 0, false);
+ * - arch/x86/kvm/x86.c|1592| <<kvm_set_msr_ignored_check>> ret = kvm_msr_ignored_check(vcpu, index, data, true);
+ * - arch/x86/kvm/x86.c|1626| <<kvm_get_msr_ignored_check>> ret = kvm_msr_ignored_check(vcpu, index, 0, false);
+ */
static int kvm_msr_ignored_check(struct kvm_vcpu *vcpu, u32 msr,
u64 data, bool write)
{
@@ -1545,6 +1551,10 @@ EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
* Returns 0 on success, non-0 otherwise.
* Assumes vcpu_load() was already called.
*/
+/*
+ * called by:
+ * - arch/x86/kvm/x86.c|1589| <<kvm_set_msr_ignored_check>> int ret = __kvm_set_msr(vcpu, index, data, host_initiated);
+ */
static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
bool host_initiated)
{
@@ -1580,14 +1590,29 @@ static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
msr.index = index;
msr.host_initiated = host_initiated;
+ /*
+ * vmx_set_msr()
+ * svm_set_msr()
+ */
return kvm_x86_ops.set_msr(vcpu, &msr);
}
+/*
+ * called by:
+ * - arch/x86/kvm/x86.c|1640| <<kvm_set_msr>> return kvm_set_msr_ignored_check(vcpu, index, data, false);
+ * - arch/x86/kvm/x86.c|1758| <<do_set_msr>> return kvm_set_msr_ignored_check(vcpu, index, *data, true);
+ */
static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu,
u32 index, u64 data, bool host_initiated)
{
int ret = __kvm_set_msr(vcpu, index, data, host_initiated);
+ /*
+ * called by:
+ * - arch/x86/kvm/x86.c|1464| <<do_get_msr_feature>> r = kvm_msr_ignored_check(vcpu, index, 0, false);
+ * - arch/x86/kvm/x86.c|1592| <<kvm_set_msr_ignored_check>> ret = kvm_msr_ignored_check(vcpu, index, data, true);
+ * - arch/x86/kvm/x86.c|1626| <<kvm_get_msr_ignored_check>> ret = kvm_msr_ignored_check(vcpu, index, 0, false);
+ */
if (ret == KVM_MSR_RET_INVALID)
ret = kvm_msr_ignored_check(vcpu, index, data, true);
@@ -1753,6 +1778,10 @@ static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
return kvm_get_msr_ignored_check(vcpu, index, data, true);
}
+/*
+ * 处理KVM_SET_MSRS:
+ * - arch/x86/kvm/x86.c|4550| <<kvm_arch_vcpu_ioctl(KVM_SET_MSRS)>> r = msr_io(vcpu, argp, do_set_msr, 0);
+ */
static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
{
return kvm_set_msr_ignored_check(vcpu, index, *data, true);
@@ -2857,6 +2886,15 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
}
+/*
+ * called by:
+ * - arch/x86/kvm/svm/svm.c|2700| <<svm_set_msr>> return kvm_set_msr_common(vcpu, msr);
+ * - arch/x86/kvm/vmx/vmx.c|2069| <<vmx_set_msr>> ret = kvm_set_msr_common(vcpu, msr_info);
+ * - arch/x86/kvm/vmx/vmx.c|2104| <<vmx_set_msr>> ret = kvm_set_msr_common(vcpu, msr_info);
+ * - arch/x86/kvm/vmx/vmx.c|2202| <<vmx_set_msr>> ret = kvm_set_msr_common(vcpu, msr_info);
+ * - arch/x86/kvm/vmx/vmx.c|2205| <<vmx_set_msr>> ret = kvm_set_msr_common(vcpu, msr_info);
+ * - arch/x86/kvm/vmx/vmx.c|2305| <<vmx_set_msr>> ret = kvm_set_msr_common(vcpu, msr_info);
+ */
int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
{
bool pr = false;
@@ -3131,6 +3169,13 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
default:
if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
return xen_hvm_config(vcpu, data);
+ /*
+ * called by:
+ * - arch/x86/kvm/x86.c|3073| <<kvm_set_msr_common>> if (kvm_pmu_is_valid_msr(vcpu, msr))
+ * - arch/x86/kvm/x86.c|3134| <<kvm_set_msr_common>> if (kvm_pmu_is_valid_msr(vcpu, msr))
+ * - arch/x86/kvm/x86.c|3220| <<kvm_get_msr_common>> if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
+ * - arch/x86/kvm/x86.c|3403| <<kvm_get_msr_common>> if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
+ */
if (kvm_pmu_is_valid_msr(vcpu, msr))
return kvm_pmu_set_msr(vcpu, msr_info);
return KVM_MSR_RET_INVALID;
@@ -3432,6 +3477,12 @@ static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
*
* @return number of msrs set successfully.
*/
+/*
+ * called by:
+ * - arch/x86/kvm/x86.c|3697| <<kvm_arch_dev_ioctl(KVM_GET_MSRS)>> r = msr_io(NULL, argp, do_get_msr_feature, 1);
+ * - arch/x86/kvm/x86.c|4544| <<kvm_arch_vcpu_ioctl(KVM_GET_MSRS)>> r = msr_io(vcpu, argp, do_get_msr, 1);
+ * - arch/x86/kvm/x86.c|4550| <<kvm_arch_vcpu_ioctl(KVM_SET_MSRS)>> r = msr_io(vcpu, argp, do_set_msr, 0);
+ */
static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
int (*do_msr)(struct kvm_vcpu *vcpu,
unsigned index, u64 *data),
@@ -3694,6 +3745,12 @@ long kvm_arch_dev_ioctl(struct file *filp,
break;
}
case KVM_GET_MSRS:
+ /*
+ * called by:
+ * - arch/x86/kvm/x86.c|3697| <<kvm_arch_dev_ioctl(KVM_GET_MSRS)>> r = msr_io(NULL, argp, do_get_msr_feature, 1);
+ * - arch/x86/kvm/x86.c|4544| <<kvm_arch_vcpu_ioctl(KVM_GET_MSRS)>> r = msr_io(vcpu, argp, do_get_msr, 1);
+ * - arch/x86/kvm/x86.c|4550| <<kvm_arch_vcpu_ioctl(KVM_SET_MSRS)>> r = msr_io(vcpu, argp, do_set_msr, 0);
+ */
r = msr_io(NULL, argp, do_get_msr_feature, 1);
break;
default:
@@ -4541,12 +4598,24 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
}
case KVM_GET_MSRS: {
int idx = srcu_read_lock(&vcpu->kvm->srcu);
+ /*
+ * called by:
+ * - arch/x86/kvm/x86.c|3697| <<kvm_arch_dev_ioctl(KVM_GET_MSRS)>> r = msr_io(NULL, argp, do_get_msr_feature, 1);
+ * - arch/x86/kvm/x86.c|4544| <<kvm_arch_vcpu_ioctl(KVM_GET_MSRS)>> r = msr_io(vcpu, argp, do_get_msr, 1);
+ * - arch/x86/kvm/x86.c|4550| <<kvm_arch_vcpu_ioctl(KVM_SET_MSRS)>> r = msr_io(vcpu, argp, do_set_msr, 0);
+ */
r = msr_io(vcpu, argp, do_get_msr, 1);
srcu_read_unlock(&vcpu->kvm->srcu, idx);
break;
}
case KVM_SET_MSRS: {
int idx = srcu_read_lock(&vcpu->kvm->srcu);
+ /*
+ * called by:
+ * - arch/x86/kvm/x86.c|3697| <<kvm_arch_dev_ioctl(KVM_GET_MSRS)>> r = msr_io(NULL, argp, do_get_msr_feature, 1);
+ * - arch/x86/kvm/x86.c|4544| <<kvm_arch_vcpu_ioctl(KVM_GET_MSRS)>> r = msr_io(vcpu, argp, do_get_msr, 1);
+ * - arch/x86/kvm/x86.c|4550| <<kvm_arch_vcpu_ioctl(KVM_SET_MSRS)>> r = msr_io(vcpu, argp, do_set_msr, 0);
+ */
r = msr_io(vcpu, argp, do_set_msr, 0);
srcu_read_unlock(&vcpu->kvm->srcu, idx);
break;
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index e98aa33dd58f..a2f32f8296a2 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -134,6 +134,10 @@ static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
}
}
+/*
+ * called by:
+ * - drivers/gpu/drm/i915/gvt/kvmgt.c|194| <<gvt_dma_map_page>> ret = gvt_pin_guest_page(vgpu, gfn, size, &page);
+ */
/* Pin a normal or compound guest page for dma. */
static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
unsigned long size, struct page **page)
@@ -184,6 +188,11 @@ static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
return ret;
}
+/*
+ * called by:
+ * - drivers/gpu/drm/i915/gvt/kvmgt.c|1902| <<kvmgt_dma_map_guest_page>> ret = gvt_dma_map_page(vgpu, gfn, dma_addr, size);
+ * - drivers/gpu/drm/i915/gvt/kvmgt.c|1914| <<kvmgt_dma_map_guest_page>> ret = gvt_dma_map_page(vgpu, gfn, dma_addr, size);
+ */
static int gvt_dma_map_page(struct intel_vgpu *vgpu, unsigned long gfn,
dma_addr_t *dma_addr, unsigned long size)
{
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index c5758fb696cc..c21d72a82755 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1612,6 +1612,27 @@ static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
return domain;
}
+/*
+ * called by:
+ * - arch/arm/mm/dma-mapping.c|2113| <<arm_iommu_create_mapping>> mapping->domain = iommu_domain_alloc(bus);
+ * - drivers/gpu/drm/msm/adreno/a6xx_gmu.c|998| <<a6xx_gmu_memory_probe>> gmu->domain = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c|741| <<_dpu_kms_mmu_init>> domain = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c|566| <<mdp4_get_config>> config.iommu = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/gpu/drm/msm/disp/mdp5/mdp5_cfg.c|845| <<mdp5_get_config>> config.iommu = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/gpu/drm/msm/msm_gpu.c|819| <<msm_gpu_create_address_space>> struct iommu_domain *iommu = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/gpu/drm/nouveau/nvkm/engine/device/tegra.c|127| <<nvkm_device_tegra_probe_iommu>> tdev->iommu.domain = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/gpu/drm/rockchip/rockchip_drm_drv.c|84| <<rockchip_drm_init_iommu>> private->domain = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/gpu/drm/tegra/drm.c|100| <<tegra_drm_load>> tegra->domain = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/gpu/host1x/dev.c|286| <<host1x_probe>> host->domain = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/infiniband/hw/usnic/usnic_uiom.c|451| <<usnic_uiom_alloc_pd>> pd->domain = domain = iommu_domain_alloc(&pci_bus_type);
+ * - drivers/iommu/amd_iommu_v2.c|781| <<amd_iommu_init_device>> dev_state->domain = iommu_domain_alloc(&pci_bus_type);
+ * - drivers/media/platform/qcom/venus/firmware.c|255| <<venus_firmware_init>> iommu_dom = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/remoteproc/remoteproc_core.c|109| <<rproc_enable_iommu>> domain = iommu_domain_alloc(dev->bus);
+ * - drivers/soc/fsl/qbman/qman_portal.c|54| <<portal_set_cpu>> pcfg->iommu_domain = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/staging/media/tegra-vde/iommu.c|81| <<tegra_vde_iommu_init>> vde->domain = iommu_domain_alloc(&platform_bus_type);
+ * - drivers/vfio/vfio_iommu_type1.c|3261| <<vfio_iommu_type1_attach_group>> domain->domain = iommu_domain_alloc(bus);
+ * - drivers/vhost/vdpa.c|1159| <<vhost_vdpa_alloc_domain>> v->domain = iommu_domain_alloc(bus);
+ */
struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
{
return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
@@ -1858,6 +1879,30 @@ static size_t iommu_pgsize(struct iommu_domain *domain,
return pgsize;
}
+/*
+ * called by:
+ * - arch/arm/mm/dma-mapping.c|1374| <<__iommu_create_mapping>> ret = iommu_map(mapping->domain, iova, phys, len, __dma_info_to_prot(DMA_BIDIRECTIONAL, attrs));
+ * - arch/arm/mm/dma-mapping.c|1643| <<__map_sg_chunk>> ret = iommu_map(mapping->domain, iova, phys, len, prot);
+ * - arch/arm/mm/dma-mapping.c|1851| <<arm_coherent_iommu_map_page>> ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
+ * - arch/arm/mm/dma-mapping.c|1957| <<arm_iommu_map_resource>> ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
+ * - drivers/gpu/drm/msm/adreno/a6xx_gmu.c|947| <<a6xx_gmu_memory_alloc>> ret = iommu_map(gmu->domain, bo->iova + (PAGE_SIZE * i), page_to_phys(bo->pages[i]), PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
+ * - drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c|477| <<gk20a_instobj_ctor_iommu>> ret = iommu_map(imem->domain, offset, node->dma_addrs[i], PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
+ * - drivers/gpu/drm/tegra/drm.c|1168| <<tegra_drm_alloc>> err = iommu_map(tegra->domain, *dma, virt_to_phys(virt), size, IOMMU_READ | IOMMU_WRITE);
+ * - drivers/gpu/host1x/cdma.c|107| <<host1x_pushbuffer_init>> err = iommu_map(host1x->domain, pb->dma, pb->phys, size, IOMMU_READ);
+ * - drivers/infiniband/hw/usnic/usnic_uiom.c|284| <<usnic_uiom_map_sorted_intervals>> err = iommu_map(pd->domain, va_start, pa_start,
+ * - drivers/infiniband/hw/usnic/usnic_uiom.c|301| <<usnic_uiom_map_sorted_intervals>> err = iommu_map(pd->domain, va_start, pa_start,
+ * - drivers/iommu/dma-iommu.c|479| <<__iommu_dma_map>> if (iommu_map(domain, iova, phys - iova_off, size, prot)) {
+ * - drivers/iommu/dma-iommu.c|1163| <<iommu_dma_get_msi_page>> if (iommu_map(domain, iova, msi_addr, size, prot))
+ * - drivers/iommu/iommu.c|663| <<iommu_group_create_direct_mappings>> ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
+ * - drivers/iommu/iommu.c|2010| <<iommu_map_sg>> ret = iommu_map(domain, iova + mapped, start, len, prot);
+ * - drivers/media/platform/qcom/venus/firmware.c|144| <<venus_boot_no_tz>> ret = iommu_map(iommu, VENUS_FW_START_ADDR, mem_phys, mem_size,
+ * - drivers/remoteproc/remoteproc_core.c|703| <<rproc_handle_devmem>> ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
+ * - drivers/remoteproc/remoteproc_core.c|793| <<rproc_alloc_carveout>> ret = iommu_map(rproc->domain, mem->da, dma, mem->len,
+ * - drivers/vfio/vfio_iommu_type1.c|1245| <<vfio_iommu_map>> ret = iommu_map(d->domain, iova, (phys_addr_t)pfn << PAGE_SHIFT,
+ * - drivers/vfio/vfio_iommu_type1.c|1675| <<vfio_iommu_replay>> ret = iommu_map(domain->domain, iova, phys,
+ * - drivers/vfio/vfio_iommu_type1.c|1759| <<vfio_test_domain_fgsp>> ret = iommu_map(domain->domain, 0, page_to_phys(pages), PAGE_SIZE * 2,
+ * - drivers/vhost/vdpa.c|825| <<vhost_vdpa_map>> r = iommu_map(v->domain, iova, pa, size,
+ */
int iommu_map(struct iommu_domain *domain, unsigned long iova,
phys_addr_t paddr, size_t size, int prot)
{
diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index e3016e1f4041..2f974d93c9ca 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -1074,6 +1074,14 @@ static int __vfio_container_attach_groups(struct vfio_container *container,
int ret = -ENODEV;
list_for_each_entry(group, &container->group_list, container_next) {
+ /*
+ * 在以下使用vfio_iommu_driver_ops->attach_group:
+ * - drivers/vfio/vfio.c|213| <<global>> .attach_group = vfio_noiommu_attach_group,
+ * - drivers/vfio/vfio_iommu_spapr_tce.c|1368| <<global>> .attach_group = tce_iommu_attach_group,
+ * - drivers/vfio/vfio_iommu_type1.c|3129| <<global>> .attach_group = vfio_iommu_type1_attach_group,
+ * - drivers/vfio/vfio.c|1077| <<__vfio_container_attach_groups>> ret = driver->ops->attach_group(data, group->iommu_group);
+ * - drivers/vfio/vfio.c|1402| <<vfio_group_set_container>> ret = driver->ops->attach_group(container->iommu_data,
+ */
ret = driver->ops->attach_group(data, group->iommu_group);
if (ret)
goto unwind;
@@ -1362,6 +1370,10 @@ static void vfio_group_try_dissolve_container(struct vfio_group *group)
__vfio_group_unset_container(group);
}
+/*
+ * called by:
+ * - drivers/vfio/vfio.c|1569| <<vfio_group_fops_unl_ioctl(VFIO_GROUP_SET_CONTAINER)>> ret = vfio_group_set_container(group, fd);
+ */
static int vfio_group_set_container(struct vfio_group *group, int container_fd)
{
struct fd f;
@@ -1399,6 +1411,14 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd)
driver = container->iommu_driver;
if (driver) {
+ /*
+ * 在以下使用vfio_iommu_driver_ops->attach_group:
+ * - drivers/vfio/vfio.c|213| <<global>> .attach_group = vfio_noiommu_attach_group,
+ * - drivers/vfio/vfio_iommu_spapr_tce.c|1368| <<global>> .attach_group = tce_iommu_attach_group,
+ * - drivers/vfio/vfio_iommu_type1.c|3129| <<global>> .attach_group = vfio_iommu_type1_attach_group,
+ * - drivers/vfio/vfio.c|1077| <<__vfio_container_attach_groups>> ret = driver->ops->attach_group(data, group->iommu_group);
+ * - drivers/vfio/vfio.c|1402| <<vfio_group_set_container>> ret = driver->ops->attach_group(container->iommu_data,
+ */
ret = driver->ops->attach_group(container->iommu_data,
group->iommu_group);
if (ret)
@@ -1770,6 +1790,12 @@ struct vfio_group *vfio_group_get_external_user(struct file *filep)
}
EXPORT_SYMBOL_GPL(vfio_group_get_external_user);
+/*
+ * 在以下使用vfio_group_put_external_user():
+ * - drivers/vfio/pci/vfio_pci.c|1235| <<vfio_pci_ioctl>> vfio_group_put_external_user(groups[group_idx].group);
+ * - virt/kvm/vfio.c|71| <<kvm_vfio_group_put_external_user>> fn = symbol_get(vfio_group_put_external_user);
+ * - virt/kvm/vfio.c|77| <<kvm_vfio_group_put_external_user>> symbol_put(vfio_group_put_external_user);
+ */
void vfio_group_put_external_user(struct vfio_group *group)
{
vfio_group_try_dissolve_container(group);
diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index 0befe9185b16..4921109294de 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -43,6 +43,80 @@
#include <linux/irqdomain.h>
#include <linux/padata.h>
+/*
+ * QEMU通过ioctl(VFIO_IOMMU_MAP_DMA)要求KVM去map一段内存.
+ *
+ * struct vfio_iommu_type1_dma_map {
+ * __u32 argsz;
+ * __u32 flags;
+ * #define VFIO_DMA_MAP_FLAG_READ (1 << 0) // readable from device
+ * #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1) // writable from device
+ * #define VFIO_DMA_MAP_FLAG_VADDR (1 << 2)
+ * __u64 vaddr; // Process virtual address
+ * __u64 iova; // IO virtual address
+ * __u64 size; // Size of mapping (bytes)
+ * };
+ *
+ * 下面是KVM的callstack.
+ *
+ * vfio_iommu_type1_ioctl
+ * vfio_fops_unl_ioctl
+ * do_vfs_ioctl
+ * ksys_ioctl
+ * __x64_sys_ioctl
+ * do_syscall_64
+ * entry_SYSCALL_64_after_hwframe
+ *
+ * 最终在KVM生成vfio_dma, 插入到vfio_iommu->dma_list.
+ *
+ * struct vfio_dma {
+ * struct rb_node node;
+ * dma_addr_t iova; // Device address
+ * unsigned long vaddr; // Process virtual addr
+ * size_t size; // Map size (bytes)
+ * int prot; // IOMMU_READ/WRITE
+ * bool iommu_mapped;
+ * bool lock_cap; // capable(CAP_IPC_LOCK)
+ * bool vaddr_invalid;
+ * struct task_struct *task;
+ * struct rb_root pfn_list; // Ex-user pinned pfn list
+ * };
+ *
+ * 所以如果ioctl完成的时候, 这段内存要么全被pin, 要么全部unpin.
+ * 并且, 要么vfio_dma在vfio_iommu->dma_list, 要么被移除被free.
+ *
+ *
+ * PADATA会把这个range分割成好几个部分并行pin. 只有全部成功了才会设置/增加dma->size.
+ * 有一个失败了也不会设置dma->size.
+ *
+ * 2225 ret = padata_do_multithreaded(&job);
+ * 2226
+ * 2227 dma->iommu_mapped = true;
+ * 2228
+ * 2229 if (ret)
+ * 2230 vfio_remove_dma(iommu, dma);
+ * 2231 else
+ * 2232 dma->size += map_size;
+ * 2233
+ * 2234 return ret;
+ *
+ *
+ * 1. 把pin的range分成好几个parallel的helper.
+ *
+ * 2. 每个helper执行vfio_pin_map_dma_chunk().
+ * 如果失败了在vfio_pin_map_dma_chunk()内部调用.
+ *
+ * 3. 对于那些成功了的, 会在__padata_do_multithreaded()->padata_undo()统一unpin.
+ *
+ * 4. 关于每一个helper的vfio_pin_map_dma_chunk()
+ *
+ * vfio_pin_map_dma_chunk()
+ * -> while (unmapped_size)
+ * -> vfio_pin_pages_remote()
+ * unpin_out: unpin当前while iteration的page
+ * -> vfio_pin_map_dma_undo() unpin之前成功的iteration.
+ */
+
#define DRIVER_VERSION "0.2"
#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
#define DRIVER_DESC "Type1 IOMMU driver for VFIO"
@@ -65,17 +139,102 @@ MODULE_PARM_DESC(dma_entry_limit,
"Maximum number of user DMA mappings per container (65535).");
struct vfio_iommu {
+ /*
+ * 在以下使用vfio_iommu->domain_list:
+ * - drivers/vfio/vfio_iommu_type1.c|182| <<IS_IOMMU_CAP_DOMAIN_IN_CONTAINER>> (!list_empty(&iommu->domain_list))
+ * - drivers/vfio/vfio_iommu_type1.c|1454| <<vfio_unmap_unpin>> domain = d = list_first_entry(&iommu->domain_list,
+ * - drivers/vfio/vfio_iommu_type1.c|1457| <<vfio_unmap_unpin>> list_for_each_entry_continue(d, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|1575| <<vfio_pgsize_bitmap>> list_for_each_entry(domain, &iommu->domain_list, next)
+ * - drivers/vfio/vfio_iommu_type1.c|1754| <<vfio_iommu_map>> list_for_each_entry(d, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|1766| <<vfio_iommu_map>> list_for_each_entry_continue_reverse(d, &iommu->domain_list, next)
+ * - drivers/vfio/vfio_iommu_type1.c|2249| <<vfio_iommu_replay>> if (!list_empty(&iommu->domain_list))
+ * - drivers/vfio/vfio_iommu_type1.c|2250| <<vfio_iommu_replay>> d = list_first_entry(&iommu->domain_list,
+ * - drivers/vfio/vfio_iommu_type1.c|2845| <<vfio_iommu_type1_attach_group>> list_for_each_entry(d, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|2979| <<vfio_iommu_type1_attach_group>> list_for_each_entry(d, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|3009| <<vfio_iommu_type1_attach_group>> list_add(&domain->next, &iommu->domain_list);
+ * - drivers/vfio/vfio_iommu_type1.c|3113| <<vfio_iommu_aper_expand>> list_for_each_entry(domain, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|3148| <<vfio_iommu_resv_refresh>> list_for_each_entry(d, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|3230| <<vfio_iommu_type1_detach_group>> list_for_each_entry(domain, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|3246| <<vfio_iommu_type1_detach_group>> if (list_is_singular(&iommu->domain_list)) {
+ * - drivers/vfio/vfio_iommu_type1.c|3311| <<vfio_iommu_type1_open>> INIT_LIST_HEAD(&iommu->domain_list);
+ * - drivers/vfio/vfio_iommu_type1.c|3379| <<vfio_iommu_type1_release>> list_for_each_entry_safe(domain, domain_tmp, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|3396| <<vfio_domains_have_iommu_cache>> list_for_each_entry(domain, &iommu->domain_list, next) {
+ */
struct list_head domain_list;
+ /*
+ * 在以下使用vfio_iommu->iova_list:
+ * - drivers/vfio/vfio_iommu_type1.c|2047| <<vfio_iommu_iova_dma_valid>> struct list_head *iova = &iommu->iova_list;
+ * - drivers/vfio/vfio_iommu_type1.c|2664| <<vfio_iommu_aper_conflict>> struct list_head *iova = &iommu->iova_list;
+ * - drivers/vfio/vfio_iommu_type1.c|2826| <<vfio_iommu_iova_get_copy>> struct list_head *iova = &iommu->iova_list;
+ * - drivers/vfio/vfio_iommu_type1.c|2846| <<vfio_iommu_iova_insert_copy>> struct list_head *iova = &iommu->iova_list;
+ * - drivers/vfio/vfio_iommu_type1.c|3360| <<vfio_iommu_type1_open>> INIT_LIST_HEAD(&iommu->iova_list);
+ * - drivers/vfio/vfio_iommu_type1.c|3433| <<vfio_iommu_type1_release>> vfio_iommu_iova_free(&iommu->iova_list);
+ * - drivers/vfio/vfio_iommu_type1.c|3486| <<vfio_iommu_iova_build_caps>> list_for_each_entry(iova, &iommu->iova_list, list)
+ * - drivers/vfio/vfio_iommu_type1.c|3508| <<vfio_iommu_iova_build_caps>> list_for_each_entry(iova, &iommu->iova_list, list) {
+ */
struct list_head iova_list;
+ /*
+ * 在以下使用vfio_iommu->external_domain:
+ * - drivers/vfio/vfio_iommu_type1.c|3166| <<vfio_iommu_type1_attach_group>> if (iommu->external_domain) {
+ * - drivers/vfio/vfio_iommu_type1.c|3167| <<vfio_iommu_type1_attach_group>> if (find_iommu_group(iommu->external_domain, iommu_group)) {
+ * - drivers/vfio/vfio_iommu_type1.c|3196| <<vfio_iommu_type1_attach_group>> if (!iommu->external_domain) {
+ * - drivers/vfio/vfio_iommu_type1.c|3198| <<vfio_iommu_type1_attach_group>> iommu->external_domain = domain;
+ * - drivers/vfio/vfio_iommu_type1.c|3204| <<vfio_iommu_type1_attach_group>> list_add(&group->next, &iommu->external_domain->group_list);
+ * - drivers/vfio/vfio_iommu_type1.c|3557| <<vfio_iommu_type1_detach_group>> if (iommu->external_domain) {
+ * - drivers/vfio/vfio_iommu_type1.c|3558| <<vfio_iommu_type1_detach_group>> group = find_iommu_group(iommu->external_domain, iommu_group);
+ * - drivers/vfio/vfio_iommu_type1.c|3563| <<vfio_iommu_type1_detach_group>> if (list_empty(&iommu->external_domain->group_list)) {
+ * - drivers/vfio/vfio_iommu_type1.c|3569| <<vfio_iommu_type1_detach_group>> kfree(iommu->external_domain);
+ * - 11 drivers/vfio/vfio_iommu_type1.c|3570| <<vfio_iommu_type1_detach_group>> iommu->external_domain = NULL;
+ * - drivers/vfio/vfio_iommu_type1.c|3600| <<vfio_iommu_type1_detach_group>> if (!iommu->external_domain) {
+ * - drivers/vfio/vfio_iommu_type1.c|3732| <<vfio_iommu_type1_release>> if (iommu->external_domain) {
+ * - drivers/vfio/vfio_iommu_type1.c|3733| <<vfio_iommu_type1_release>> vfio_release_domain(iommu->external_domain, true);
+ * - drivers/vfio/vfio_iommu_type1.c|3734| <<vfio_iommu_type1_release>> kfree(iommu->external_domain);
+ */
struct vfio_domain *external_domain; /* domain for external user */
struct mutex lock;
+ /*
+ * 在以下使用vfio_iommu->dma_list:
+ * - drivers/vfio/vfio_iommu_type1.c|214| <<vfio_find_dma>> struct rb_node *node = iommu->dma_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|238| <<vfio_find_dma_first_node>> struct rb_node *node = iommu->dma_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|270| <<vfio_link_dma>> struct rb_node **link = &iommu->dma_list.rb_node, *parent = NULL;
+ * - drivers/vfio/vfio_iommu_type1.c|284| <<vfio_link_dma>> rb_insert_color(&new->node, &iommu->dma_list);
+ * - drivers/vfio/vfio_iommu_type1.c|293| <<vfio_unlink_dma>> rb_erase(&old->node, &iommu->dma_list);
+ * - drivers/vfio/vfio_iommu_type1.c|2227| <<vfio_iommu_replay>> n = rb_first(&iommu->dma_list);
+ * - drivers/vfio/vfio_iommu_type1.c|2326| <<vfio_iommu_replay>> for (n = rb_first(&iommu->dma_list); n; n = rb_next(n)) {
+ * - drivers/vfio/vfio_iommu_type1.c|3015| <<vfio_iommu_unmap_unpin_all>> while ((node = rb_first(&iommu->dma_list)))
+ * - drivers/vfio/vfio_iommu_type1.c|3027| <<vfio_iommu_unmap_unpin_reaccount>> n = rb_first(&iommu->dma_list);
+ * - drivers/vfio/vfio_iommu_type1.c|3274| <<vfio_iommu_type1_open>> iommu->dma_list = RB_ROOT;
+ */
struct rb_root dma_list;
struct blocking_notifier_head notifier;
+ /*
+ * 在以下使用vfio_iommu->dma_avail:
+ * - drivers/vfio/vfio_iommu_type1.c|1636| <<vfio_remove_dma>> iommu->dma_avail++;
+ * - drivers/vfio/vfio_iommu_type1.c|2255| <<vfio_dma_do_map>> if (!iommu->dma_avail) {
+ * - drivers/vfio/vfio_iommu_type1.c|2274| <<vfio_dma_do_map>> iommu->dma_avail--;
+ * - drivers/vfio/vfio_iommu_type1.c|3448| <<vfio_iommu_type1_open>> iommu->dma_avail = dma_entry_limit;
+ * - drivers/vfio/vfio_iommu_type1.c|3618| <<vfio_iommu_dma_avail_build_caps>> cap_dma_avail.avail = iommu->dma_avail;
+ */
unsigned int dma_avail;
unsigned int vaddr_invalid_count;
+ /*
+ * 在以下使用vfio_iommu->vaddr_wait:
+ * - drivers/vfio/vfio_iommu_type1.c|634| <<vfio_wait>> prepare_to_wait(&iommu->vaddr_wait, &wait, TASK_KILLABLE);
+ * - drivers/vfio/vfio_iommu_type1.c|638| <<vfio_wait>> finish_wait(&iommu->vaddr_wait, &wait);
+ * - drivers/vfio/vfio_iommu_type1.c|1296| <<vfio_remove_dma>> wake_up_all(&iommu->vaddr_wait);
+ * - drivers/vfio/vfio_iommu_type1.c|1853| <<vfio_dma_do_map>> wake_up_all(&iommu->vaddr_wait);
+ * - drivers/vfio/vfio_iommu_type1.c|2982| <<vfio_iommu_type1_open>> init_waitqueue_head(&iommu->vaddr_wait);
+ * - drivers/vfio/vfio_iommu_type1.c|3353| <<vfio_iommu_type1_notify>> wake_up_all(&iommu->vaddr_wait);
+ */
wait_queue_head_t vaddr_wait;
bool v2;
bool nesting;
+ /*
+ * 在以下使用vfio_iommu->container_open:
+ * - drivers/vfio/vfio_iommu_type1.c|792| <<vfio_wait>> if (kthread_should_stop() || !iommu->container_open ||
+ * - drivers/vfio/vfio_iommu_type1.c|3374| <<vfio_iommu_type1_open>> iommu->container_open = true;
+ * - drivers/vfio/vfio_iommu_type1.c|3808| <<vfio_iommu_type1_notify>> iommu->container_open = false;
+ */
bool container_open;
};
@@ -93,10 +252,37 @@ struct vfio_dma {
unsigned long vaddr; /* Process virtual addr */
size_t size; /* Map size (bytes) */
int prot; /* IOMMU_READ/WRITE */
+ /*
+ * 在以下使用vfio_dma->iommu_mapped:
+ * - drivers/vfio/vfio_iommu_type1.c|1125| <<vfio_unmap_unpin>> dma->iommu_mapped = false;
+ * - drivers/vfio/vfio_iommu_type1.c|1556| <<vfio_pin_map_dma>> dma->iommu_mapped = true;
+ * - drivers/vfio/vfio_iommu_type1.c|1809| <<vfio_iommu_replay>> if (dma->iommu_mapped) {
+ * - drivers/vfio/vfio_iommu_type1.c|1859| <<vfio_iommu_replay>> if (!dma->iommu_mapped)
+ * - drivers/vfio/vfio_iommu_type1.c|1875| <<vfio_iommu_replay>> dma->iommu_mapped = true;
+ * - drivers/vfio/vfio_iommu_type1.c|1885| <<vfio_iommu_replay>> if (dma->iommu_mapped) {
+ * - drivers/vfio/vfio_iommu_type1.c|2532| <<vfio_iommu_unmap_unpin_reaccount>> dma->iommu_mapped = false;
+ */
bool iommu_mapped;
bool lock_cap; /* capable(CAP_IPC_LOCK) */
bool vaddr_invalid;
struct task_struct *task;
+ /*
+ * 在以下使用vfio_dma->pfn_list:
+ * - drivers/vfio/vfio_iommu_type1.c|266| <<vfio_find_vpfn>> struct rb_node *node = dma->pfn_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|287| <<vfio_link_pfn>> link = &dma->pfn_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|299| <<vfio_link_pfn>> rb_insert_color(&new->node, &dma->pfn_list);
+ * - drivers/vfio/vfio_iommu_type1.c|304| <<vfio_unlink_pfn>> rb_erase(&old->node, &dma->pfn_list);
+ * - drivers/vfio/vfio_iommu_type1.c|1178| <<vfio_remove_dma>> WARN_ON(!RB_EMPTY_ROOT(&dma->pfn_list));
+ * - drivers/vfio/vfio_iommu_type1.c|1325| <<vfio_dma_do_unmap>> if (!RB_EMPTY_ROOT(&dma->pfn_list)) {
+ * - drivers/vfio/vfio_iommu_type1.c|1789| <<vfio_dma_do_map>> dma->pfn_list = RB_ROOT;
+ * - drivers/vfio/vfio_iommu_type1.c|2633| <<vfio_iommu_unmap_unpin_reaccount>> p = rb_first(&dma->pfn_list);
+ *
+ * 一般没有调用
+ * struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1.pin_pages = vfio_iommu_type1_pin_pages()
+ * -> vfio_add_to_pfn_list()
+ * -> vfio_link_pfn()
+ * -> rb_insert_color(&new->node, &dma->pfn_list);
+ */
struct rb_root pfn_list; /* Ex-user pinned pfn list */
};
@@ -129,6 +315,27 @@ struct vfio_regions {
size_t len;
};
+/*
+ * 在以下使用vfio_iommu->domain_list:
+ * - drivers/vfio/vfio_iommu_type1.c|182| <<IS_IOMMU_CAP_DOMAIN_IN_CONTAINER>> (!list_empty(&iommu->domain_list))
+ * - drivers/vfio/vfio_iommu_type1.c|1454| <<vfio_unmap_unpin>> domain = d = list_first_entry(&iommu->domain_list,
+ * - drivers/vfio/vfio_iommu_type1.c|1457| <<vfio_unmap_unpin>> list_for_each_entry_continue(d, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|1575| <<vfio_pgsize_bitmap>> list_for_each_entry(domain, &iommu->domain_list, next)
+ * - drivers/vfio/vfio_iommu_type1.c|1754| <<vfio_iommu_map>> list_for_each_entry(d, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|1766| <<vfio_iommu_map>> list_for_each_entry_continue_reverse(d, &iommu->domain_list, next)
+ * - drivers/vfio/vfio_iommu_type1.c|2249| <<vfio_iommu_replay>> if (!list_empty(&iommu->domain_list))
+ * - drivers/vfio/vfio_iommu_type1.c|2250| <<vfio_iommu_replay>> d = list_first_entry(&iommu->domain_list,
+ * - drivers/vfio/vfio_iommu_type1.c|2845| <<vfio_iommu_type1_attach_group>> list_for_each_entry(d, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|2979| <<vfio_iommu_type1_attach_group>> list_for_each_entry(d, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|3009| <<vfio_iommu_type1_attach_group>> list_add(&domain->next, &iommu->domain_list);
+ * - drivers/vfio/vfio_iommu_type1.c|3113| <<vfio_iommu_aper_expand>> list_for_each_entry(domain, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|3148| <<vfio_iommu_resv_refresh>> list_for_each_entry(d, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|3230| <<vfio_iommu_type1_detach_group>> list_for_each_entry(domain, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|3246| <<vfio_iommu_type1_detach_group>> if (list_is_singular(&iommu->domain_list)) {
+ * - drivers/vfio/vfio_iommu_type1.c|3311| <<vfio_iommu_type1_open>> INIT_LIST_HEAD(&iommu->domain_list);
+ * - drivers/vfio/vfio_iommu_type1.c|3379| <<vfio_iommu_type1_release>> list_for_each_entry_safe(domain, domain_tmp, &iommu->domain_list, next) {
+ * - drivers/vfio/vfio_iommu_type1.c|3396| <<vfio_domains_have_iommu_cache>> list_for_each_entry(domain, &iommu->domain_list, next) {
+ */
#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu) \
(!list_empty(&iommu->domain_list))
@@ -141,9 +348,40 @@ static int put_pfn(unsigned long pfn, int prot);
* into DMA'ble space using the IOMMU
*/
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|513| <<vfio_find_dma_valid>> *dma_p = vfio_find_dma(iommu, start, size);
+ * - drivers/vfio/vfio_iommu_type1.c|757| <<vfio_iommu_type1_pin_pages>> dma = vfio_find_dma(iommu, iova, PAGE_SIZE);
+ * - drivers/vfio/vfio_iommu_type1.c|797| <<vfio_iommu_type1_pin_pages>> dma = vfio_find_dma(iommu, iova, PAGE_SIZE);
+ * - drivers/vfio/vfio_iommu_type1.c|829| <<vfio_iommu_type1_unpin_pages>> dma = vfio_find_dma(iommu, iova, PAGE_SIZE);
+ * - drivers/vfio/vfio_iommu_type1.c|1117| <<vfio_dma_do_unmap>> dma = vfio_find_dma(iommu, iova, 1);
+ * - drivers/vfio/vfio_iommu_type1.c|1121| <<vfio_dma_do_unmap>> dma = vfio_find_dma(iommu, iova + size - 1, 0);
+ * - drivers/vfio/vfio_iommu_type1.c|1410| <<vfio_dma_do_map>> dma = vfio_find_dma(iommu, iova, size);
+ * - drivers/vfio/vfio_iommu_type1.c|1865| <<vfio_iommu_aper_conflict>> if (vfio_find_dma(iommu, first->start, start - first->start))
+ * - drivers/vfio/vfio_iommu_type1.c|1871| <<vfio_iommu_aper_conflict>> if (vfio_find_dma(iommu, end + 1, last->end - end))
+ * - drivers/vfio/vfio_iommu_type1.c|1932| <<vfio_iommu_resv_conflict>> if (vfio_find_dma(iommu, region->start, region->length))
+ *
+ * 90 struct vfio_dma {
+ * 91 struct rb_node node;
+ * 92 dma_addr_t iova; // Device address
+ * 93 unsigned long vaddr; // Process virtual addr
+ * 94 size_t size; // Map size (bytes)
+ * 95 int prot; // IOMMU_READ/WRITE
+ * 96 bool iommu_mapped;
+ * 97 bool lock_cap; // capable(CAP_IPC_LOCK)
+ * 98 bool vaddr_invalid;
+ * 99 struct task_struct *task;
+ * 100 struct rb_root pfn_list; // Ex-user pinned pfn list
+ * 101 };
+ */
static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu,
dma_addr_t start, size_t size)
{
+ /*
+ * struct vfio_iommu *iommu:
+ * -> struct rb_root dma_list;
+ * -> struct rb_node *rb_node;
+ */
struct rb_node *node = iommu->dma_list.rb_node;
while (node) {
@@ -160,6 +398,10 @@ static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu,
return NULL;
}
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|1288| <<vfio_dma_do_unmap>> n = first_n = vfio_find_dma_first_node(iommu, iova, size);
+ */
static struct rb_node *vfio_find_dma_first_node(struct vfio_iommu *iommu,
dma_addr_t start, u64 size)
{
@@ -185,6 +427,15 @@ static struct rb_node *vfio_find_dma_first_node(struct vfio_iommu *iommu,
return res;
}
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|1482| <<vfio_dma_do_map>> vfio_link_dma(iommu, dma);
+ *
+ * 把vfio_dma插入到vfio_mmu
+ * struct vfio_iommu *iommu:
+ * -> struct rb_root dma_list;
+ * -> struct rb_node *rb_node;
+ */
static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new)
{
struct rb_node **link = &iommu->dma_list.rb_node, *parent = NULL;
@@ -200,10 +451,27 @@ static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new)
link = &(*link)->rb_right;
}
+ /*
+ * 在以下使用vfio_iommu->dma_list:
+ * - drivers/vfio/vfio_iommu_type1.c|214| <<vfio_find_dma>> struct rb_node *node = iommu->dma_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|238| <<vfio_find_dma_first_node>> struct rb_node *node = iommu->dma_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|270| <<vfio_link_dma>> struct rb_node **link = &iommu->dma_list.rb_node, *parent = NULL;
+ * - drivers/vfio/vfio_iommu_type1.c|284| <<vfio_link_dma>> rb_insert_color(&new->node, &iommu->dma_list);
+ * - drivers/vfio/vfio_iommu_type1.c|293| <<vfio_unlink_dma>> rb_erase(&old->node, &iommu->dma_list);
+ * - drivers/vfio/vfio_iommu_type1.c|2227| <<vfio_iommu_replay>> n = rb_first(&iommu->dma_list);
+ * - drivers/vfio/vfio_iommu_type1.c|2326| <<vfio_iommu_replay>> for (n = rb_first(&iommu->dma_list); n; n = rb_next(n)) {
+ * - drivers/vfio/vfio_iommu_type1.c|3015| <<vfio_iommu_unmap_unpin_all>> while ((node = rb_first(&iommu->dma_list)))
+ * - drivers/vfio/vfio_iommu_type1.c|3027| <<vfio_iommu_unmap_unpin_reaccount>> n = rb_first(&iommu->dma_list);
+ * - drivers/vfio/vfio_iommu_type1.c|3274| <<vfio_iommu_type1_open>> iommu->dma_list = RB_ROOT;
+ */
rb_link_node(&new->node, parent, link);
rb_insert_color(&new->node, &iommu->dma_list);
}
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|1180| <<vfio_remove_dma>> vfio_unlink_dma(iommu, dma);
+ */
static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old)
{
rb_erase(&old->node, &iommu->dma_list);
@@ -212,9 +480,30 @@ static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old)
/*
* Helper Functions for host iova-pfn list
*/
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|315| <<vfio_iova_get_vfio_pfn>> struct vfio_pfn *vpfn = vfio_find_vpfn(dma, iova);
+ * - drivers/vfio/vfio_iommu_type1.c|616| <<vfio_pin_pages_remote>> if (!rsvd && !vfio_find_vpfn(dma, iova)) {
+ * - drivers/vfio/vfio_iommu_type1.c|647| <<vfio_pin_pages_remote>> if (!rsvd && !vfio_find_vpfn(dma, iova)) {
+ * - drivers/vfio/vfio_iommu_type1.c|703| <<vfio_unpin_pages_remote>> if (vfio_find_vpfn(dma, iova))
+ * - drivers/vfio/vfio_iommu_type1.c|745| <<vfio_unpin_page_external>> struct vfio_pfn *vpfn = vfio_find_vpfn(dma, iova);
+ */
static struct vfio_pfn *vfio_find_vpfn(struct vfio_dma *dma, dma_addr_t iova)
{
struct vfio_pfn *vpfn;
+ /*
+ * 在以下使用vfio_dma->pfn_list:
+ * - drivers/vfio/vfio_iommu_type1.c|266| <<vfio_find_vpfn>> struct rb_node *node = dma->pfn_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|287| <<vfio_link_pfn>> link = &dma->pfn_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|299| <<vfio_link_pfn>> rb_insert_color(&new->node, &dma->pfn_list);
+ * - drivers/vfio/vfio_iommu_type1.c|304| <<vfio_unlink_pfn>> rb_erase(&old->node, &dma->pfn_list);
+ * - drivers/vfio/vfio_iommu_type1.c|1178| <<vfio_remove_dma>> WARN_ON(!RB_EMPTY_ROOT(&dma->pfn_list));
+ * - drivers/vfio/vfio_iommu_type1.c|1325| <<vfio_dma_do_unmap>> if (!RB_EMPTY_ROOT(&dma->pfn_list)) {
+ * - drivers/vfio/vfio_iommu_type1.c|1789| <<vfio_dma_do_map>> dma->pfn_list = RB_ROOT;
+ * - drivers/vfio/vfio_iommu_type1.c|2633| <<vfio_iommu_unmap_unpin_reaccount>> p = rb_first(&dma->pfn_list);
+ *
+ * Ex-user pinned pfn list
+ */
struct rb_node *node = dma->pfn_list.rb_node;
while (node) {
@@ -230,12 +519,36 @@ static struct vfio_pfn *vfio_find_vpfn(struct vfio_dma *dma, dma_addr_t iova)
return NULL;
}
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|381| <<vfio_add_to_pfn_list>> vfio_link_pfn(dma, vpfn);
+ *
+ * struct vfio_pfn {
+ * struct rb_node node;
+ * dma_addr_t iova; // Device address
+ * unsigned long pfn; // Host pfn
+ * atomic_t ref_count;
+ * };
+ */
static void vfio_link_pfn(struct vfio_dma *dma,
struct vfio_pfn *new)
{
struct rb_node **link, *parent = NULL;
struct vfio_pfn *vpfn;
+ /*
+ * 在以下使用vfio_dma->pfn_list:
+ * - drivers/vfio/vfio_iommu_type1.c|266| <<vfio_find_vpfn>> struct rb_node *node = dma->pfn_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|287| <<vfio_link_pfn>> link = &dma->pfn_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|299| <<vfio_link_pfn>> rb_insert_color(&new->node, &dma->pfn_list);
+ * - drivers/vfio/vfio_iommu_type1.c|304| <<vfio_unlink_pfn>> rb_erase(&old->node, &dma->pfn_list);
+ * - drivers/vfio/vfio_iommu_type1.c|1178| <<vfio_remove_dma>> WARN_ON(!RB_EMPTY_ROOT(&dma->pfn_list));
+ * - drivers/vfio/vfio_iommu_type1.c|1325| <<vfio_dma_do_unmap>> if (!RB_EMPTY_ROOT(&dma->pfn_list)) {
+ * - drivers/vfio/vfio_iommu_type1.c|1789| <<vfio_dma_do_map>> dma->pfn_list = RB_ROOT;
+ * - drivers/vfio/vfio_iommu_type1.c|2633| <<vfio_iommu_unmap_unpin_reaccount>> p = rb_first(&dma->pfn_list);
+ *
+ * Ex-user pinned pfn list
+ */
link = &dma->pfn_list.rb_node;
while (*link) {
parent = *link;
@@ -248,14 +561,43 @@ static void vfio_link_pfn(struct vfio_dma *dma,
}
rb_link_node(&new->node, parent, link);
+ /*
+ * 一般没有调用
+ * struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1.pin_pages = vfio_iommu_type1_pin_pages()
+ * -> vfio_add_to_pfn_list()
+ * -> vfio_link_pfn()
+ * -> rb_insert_color(&new->node, &dma->pfn_list);
+ */
rb_insert_color(&new->node, &dma->pfn_list);
}
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|388| <<vfio_remove_from_pfn_list>> vfio_unlink_pfn(dma, vpfn);
+ */
static void vfio_unlink_pfn(struct vfio_dma *dma, struct vfio_pfn *old)
{
+ /*
+ * 在以下使用vfio_dma->pfn_list:
+ * - drivers/vfio/vfio_iommu_type1.c|266| <<vfio_find_vpfn>> struct rb_node *node = dma->pfn_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|287| <<vfio_link_pfn>> link = &dma->pfn_list.rb_node;
+ * - drivers/vfio/vfio_iommu_type1.c|299| <<vfio_link_pfn>> rb_insert_color(&new->node, &dma->pfn_list);
+ * - drivers/vfio/vfio_iommu_type1.c|304| <<vfio_unlink_pfn>> rb_erase(&old->node, &dma->pfn_list);
+ * - drivers/vfio/vfio_iommu_type1.c|1178| <<vfio_remove_dma>> WARN_ON(!RB_EMPTY_ROOT(&dma->pfn_list));
+ * - drivers/vfio/vfio_iommu_type1.c|1325| <<vfio_dma_do_unmap>> if (!RB_EMPTY_ROOT(&dma->pfn_list)) {
+ * - drivers/vfio/vfio_iommu_type1.c|1789| <<vfio_dma_do_map>> dma->pfn_list = RB_ROOT;
+ * - drivers/vfio/vfio_iommu_type1.c|2633| <<vfio_iommu_unmap_unpin_reaccount>> p = rb_first(&dma->pfn_list);
+ */
rb_erase(&old->node, &dma->pfn_list);
}
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|1000| <<vfio_iommu_type1_pin_pages>> ret = vfio_add_to_pfn_list(dma, iova, phys_pfn[i]);
+ *
+ * vfio_add_to_pfn_list()只被一般没有调用的vfio_iommu_type1_pin_pages()调用
+ * 所以vfio_add_to_pfn_list()一般不被调用
+ */
static int vfio_add_to_pfn_list(struct vfio_dma *dma, dma_addr_t iova,
unsigned long pfn)
{
@@ -268,10 +610,17 @@ static int vfio_add_to_pfn_list(struct vfio_dma *dma, dma_addr_t iova,
vpfn->iova = iova;
vpfn->pfn = pfn;
atomic_set(&vpfn->ref_count, 1);
+ /*
+ * 只在此处调用
+ */
vfio_link_pfn(dma, vpfn);
return 0;
}
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|408| <<vfio_iova_put_vfio_pfn>> vfio_remove_from_pfn_list(dma, vpfn);
+ */
static void vfio_remove_from_pfn_list(struct vfio_dma *dma,
struct vfio_pfn *vpfn)
{
@@ -279,6 +628,10 @@ static void vfio_remove_from_pfn_list(struct vfio_dma *dma,
kfree(vpfn);
}
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|988| <<vfio_iommu_type1_pin_pages>> vpfn = vfio_iova_get_vfio_pfn(dma, iova);
+ */
static struct vfio_pfn *vfio_iova_get_vfio_pfn(struct vfio_dma *dma,
unsigned long iova)
{
@@ -289,6 +642,10 @@ static struct vfio_pfn *vfio_iova_get_vfio_pfn(struct vfio_dma *dma,
return vpfn;
}
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|910| <<vfio_unpin_page_external>> unlocked = vfio_iova_put_vfio_pfn(dma, vpfn);
+ */
static int vfio_iova_put_vfio_pfn(struct vfio_dma *dma, struct vfio_pfn *vpfn)
{
int ret = 0;
@@ -300,6 +657,18 @@ static int vfio_iova_put_vfio_pfn(struct vfio_dma *dma, struct vfio_pfn *vpfn)
return ret;
}
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|611| <<vfio_pin_pages_remote>> ret = vfio_lock_acct(dma, lock_acct, false);
+ * - drivers/vfio/vfio_iommu_type1.c|642| <<vfio_unpin_pages_remote>> vfio_lock_acct(dma, locked - unlocked, true);
+ * - drivers/vfio/vfio_iommu_type1.c|659| <<vfio_pin_page_external>> ret = vfio_lock_acct(dma, 1, true);
+ * - drivers/vfio/vfio_iommu_type1.c|686| <<vfio_unpin_page_external>> vfio_lock_acct(dma, -unlocked, true);
+ * - drivers/vfio/vfio_iommu_type1.c|771| <<vfio_iommu_type1_pin_pages>> vfio_lock_acct(dma, -1, true);
+ * - drivers/vfio/vfio_iommu_type1.c|997| <<vfio_unmap_unpin>> vfio_lock_acct(dma, -unlocked, true);
+ * - drivers/vfio/vfio_iommu_type1.c|1255| <<vfio_pin_map_dma_chunk>> ret = vfio_lock_acct(dma, cache_size, false);
+ * - drivers/vfio/vfio_iommu_type1.c|1285| <<vfio_pin_map_dma_chunk>> vfio_lock_acct(dma, -lock_cache, false);
+ * - drivers/vfio/vfio_iommu_type1.c|2246| <<vfio_iommu_unmap_unpin_reaccount>> vfio_lock_acct(dma, locked - unlocked, true);
+ */
static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)
{
struct mm_struct *mm;
@@ -313,7 +682,16 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)
return -ESRCH; /* process exited */
ret = down_write_killable(&mm->mmap_sem);
+ /*
+ * 返回0说明lock成功了
+ */
if (!ret) {
+ /*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|359| <<vfio_lock_acct>> ret = __account_locked_vm(mm, abs(npage), npage > 0,
+ * dma->task, dma->lock_cap);
+ * - mm/util.c|507| <<account_locked_vm>> ret = __account_locked_vm(mm, pages, inc, current, capable(CAP_IPC_LOCK));
+ */
ret = __account_locked_vm(mm, abs(npage), npage > 0, dma->task,
dma->lock_cap);
if (!ret)
@@ -332,6 +710,20 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)
* MMIO range for our own or another device. These use a different
* pfn conversion and shouldn't be tracked as locked pages.
*/
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|413| <<put_pfn>> if (!is_invalid_reserved_pfn(pfn)) {
+ * - drivers/vfio/vfio_iommu_type1.c|512| <<vaddr_get_pfn>> if (!ret && !is_invalid_reserved_pfn(*pfn))
+ * - drivers/vfio/vfio_iommu_type1.c|610| <<vfio_pin_pages_remote>> rsvd = is_invalid_reserved_pfn(*pfn_base);
+ * - drivers/vfio/vfio_iommu_type1.c|642| <<vfio_pin_pages_remote>> rsvd != is_invalid_reserved_pfn(pfn)) {
+ * - drivers/vfio/vfio_iommu_type1.c|725| <<vfio_pin_page_external>> if (!ret && do_accounting && !is_invalid_reserved_pfn(*pfn_base)) {
+ * - drivers/vfio/vfio_iommu_type1.c|2404| <<vfio_iommu_unmap_unpin_reaccount>> if (!is_invalid_reserved_pfn(vpfn->pfn))
+ *
+ * 注释:
+ * Some mappings aren't backed by a struct page, for example an mmap'd
+ * MMIO range for our own or another device. These use a different
+ * pfn conversion and shouldn't be tracked as locked pages.
+ */
static bool is_invalid_reserved_pfn(unsigned long pfn)
{
if (pfn_valid(pfn)) {
@@ -360,6 +752,17 @@ static bool is_invalid_reserved_pfn(unsigned long pfn)
return true;
}
+/*
+ * called by:
+ * - drivers/vfio/vfio_iommu_type1.c|345| <<vfio_iova_put_vfio_pfn>> ret = put_pfn(vpfn->pfn, dma->prot);
+ * - drivers/vfio/vfio_iommu_type1.c|672| <<vfio_pin_pages_remote>> put_pfn(*pfn_base, dma->prot);
+ * - drivers/vfio/vfio_iommu_type1.c|705| <<vfio_pin_pages_remote>> put_pfn(pfn, dma->prot);
+ * - drivers/vfio/vfio_iommu_type1.c|720| <<vfio_pin_pages_remote>> put_pfn(pfn, dma->prot);
+ * - drivers/vfio/vfio_iommu_type1.c|754| <<vfio_pin_pages_remote>> put_pfn(pfn, dma->prot);