-
Notifications
You must be signed in to change notification settings - Fork 4
/
block-comment-for-block-and-drivers-for-mainline-v5.1-rc5.patch
2207 lines (2067 loc) · 83.4 KB
/
block-comment-for-block-and-drivers-for-mainline-v5.1-rc5.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 c986a7a6365788c5f51c7d54995e8f445f16d35a Mon Sep 17 00:00:00 2001
From: Dongli Zhang <[email protected]>
Date: Fri, 19 Apr 2019 05:52:00 +0800
Subject: [PATCH 1/1] block comment for block and drivers for mainline v5.1-rc5
This is for mainline v5.1-rc5
Signed-off-by: Dongli Zhang <[email protected]>
---
drivers/scsi/scsi_scan.c | 6 +
drivers/scsi/sd.c | 34 ++
drivers/scsi/virtio_scsi.c | 52 ++++
fs/io_uring.c | 664 ++++++++++++++++++++++++++++++++++++++++
include/linux/percpu-refcount.h | 90 ++++++
include/uapi/linux/io_uring.h | 139 ++++++++-
lib/percpu-refcount.c | 163 ++++++++++
7 files changed, 1147 insertions(+), 1 deletion(-)
diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c
index 53380e0..4a3a7d6 100644
--- a/drivers/scsi/scsi_scan.c
+++ b/drivers/scsi/scsi_scan.c
@@ -1654,6 +1654,12 @@ static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
id, lun, rescan);
}
+/*
+ * called by:
+ * - drivers/scsi/scsi_proc.c|255| <<scsi_add_single_device>> error = scsi_scan_host_selected(shost, channel, id, lun,
+ * - drivers/scsi/scsi_scan.c|1816| <<do_scsi_scan_host>> scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
+ * - drivers/scsi/scsi_sysfs.c|149| <<scsi_scan>> res = scsi_scan_host_selected(shost, channel, id, lun,
+ */
int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
unsigned int id, u64 lun,
enum scsi_scan_mode rescan)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 2b2bc4b..9e99f61 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -3364,6 +3364,40 @@ static void sd_probe_async(void *data, async_cookie_t cookie)
* Assume sd_probe is not re-entrant (for time being)
* Also think about sd_probe() and sd_remove() running coincidentally.
**/
+/*
+ * virtio scsi的调用
+ * [0] sd_probe
+ * [0] really_probe
+ * [0] driver_probe_device
+ * [0] device_driver_attach
+ * [0] __driver_attach
+ * [0] bus_for_each_dev
+ * [0] bus_add_driver
+ * [0] driver_register
+ * [0] init_sd
+ * [0] do_one_initcall
+ * [0] kernel_init_freeable
+ * [0] kernel_init
+ * [0] ret_from_fork
+ *
+ * ata的调用
+ * [0] sd_probe
+ * [0] really_probe
+ * [0] driver_probe_device
+ * [0] bus_for_each_drv
+ * [0] __device_attach
+ * [0] bus_probe_device
+ * [0] device_add
+ * [0] scsi_sysfs_add_sdev
+ * [0] scsi_probe_and_add_lun
+ * [0] __scsi_add_device
+ * [0] ata_scsi_scan_host
+ * [0] async_run_entry_fn
+ * [0] process_one_work
+ * [0] worker_thread
+ * [0] kthread
+ * [0] ret_from_fork
+ */
static int sd_probe(struct device *dev)
{
struct scsi_device *sdp = to_scsi_device(dev);
diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c
index f8cb7c2..0a4bea6 100644
--- a/drivers/scsi/virtio_scsi.c
+++ b/drivers/scsi/virtio_scsi.c
@@ -109,6 +109,12 @@ static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
*
* Called with vq_lock held.
*/
+/*
+ * called by:
+ * - drivers/scsi/virtio_scsi.c|203| <<virtscsi_req_done>> virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd);
+ * - drivers/scsi/virtio_scsi.c|213| <<virtscsi_poll_requests>> virtscsi_complete_cmd);
+ * - drivers/scsi/virtio_scsi.c|538| <<virtscsi_queuecommand>> virtscsi_complete_cmd(vscsi, cmd);
+ */
static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
{
struct virtio_scsi_cmd *cmd = buf;
@@ -172,6 +178,13 @@ static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
sc->scsi_done(sc);
}
+/*
+ * called by:
+ * - drivers/scsi/virtio_scsi.c|209| <<virtscsi_req_done>> virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd);
+ * - drivers/scsi/virtio_scsi.c|218| <<virtscsi_poll_requests>> virtscsi_vq_done(vscsi, &vscsi->req_vqs[i],
+ * - drivers/scsi/virtio_scsi.c|235| <<virtscsi_ctrl_done>> virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free);
+ * - drivers/scsi/virtio_scsi.c|381| <<virtscsi_event_done>> virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
+ */
static void virtscsi_vq_done(struct virtio_scsi *vscsi,
struct virtio_scsi_vq *virtscsi_vq,
void (*fn)(struct virtio_scsi *vscsi, void *buf))
@@ -496,6 +509,37 @@ static struct virtio_scsi_vq *virtscsi_pick_vq_mq(struct virtio_scsi *vscsi,
return &vscsi->req_vqs[hwq];
}
+/*
+ * [0] virtscsi_queuecommand
+ * [0] scsi_queue_rq
+ * [0] blk_mq_dispatch_rq_list
+ * [0] blk_mq_sched_dispatch_requests
+ * [0] __blk_mq_run_hw_queue
+ * [0] __blk_mq_delay_run_hw_queue
+ * [0] blk_mq_run_hw_queue
+ * [0] blk_mq_sched_insert_request
+ * [0] blk_execute_rq
+ * [0] __scsi_execute
+ * [0] scsi_probe_and_add_lun
+ * [0] __scsi_scan_target
+ * [0] scsi_scan_channel
+ * [0] scsi_scan_host_selected
+ * [0] scsi_scan_host
+ * [0] virtscsi_probe
+ * [0] virtio_dev_probe
+ * [0] really_probe
+ * [0] driver_probe_device
+ * [0] device_driver_attach
+ * [0] __driver_attach
+ * [0] bus_for_each_dev
+ * [0] bus_add_driver
+ * [0] driver_register
+ * [0] init
+ * [0] do_one_initcall
+ * [0] kernel_init_freeable
+ * [0] kernel_init
+ * [0] ret_from_fork
+ */
static int virtscsi_queuecommand(struct Scsi_Host *shost,
struct scsi_cmnd *sc)
{
@@ -720,6 +764,11 @@ static void virtscsi_remove_vqs(struct virtio_device *vdev)
vdev->config->del_vqs(vdev);
}
+/*
+ * called by:
+ * - drivers/scsi/virtio_scsi.c|818| <<virtscsi_probe>> err = virtscsi_init(vdev, vscsi);
+ * - drivers/scsi/virtio_scsi.c|895| <<virtscsi_restore>> err = virtscsi_init(vdev, vscsi);
+ */
static int virtscsi_init(struct virtio_device *vdev,
struct virtio_scsi *vscsi)
{
@@ -747,6 +796,9 @@ static int virtscsi_init(struct virtio_device *vdev,
names[0] = "control";
names[1] = "event";
for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) {
+ /*
+ * 设置重要的中断函数
+ */
callbacks[i] = virtscsi_req_done;
names[i] = "request";
}
diff --git a/fs/io_uring.c b/fs/io_uring.c
index 89aa841..1b18fa3 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -62,9 +62,39 @@
#include "internal.h"
+/*
+ * 对于sq
+ * user修改tail, kernel修改head
+ * The head and tail values are used to manage entries in the ring; if the
+ * two values are equal, the ring is empty. User-space code adds an entry
+ * by putting its index into array[r.tail] and incrementing the tail
+ * pointer; only the kernel side should change r.head. Once one or more
+ * entries have been placed in the ring, they can be submitted with a call
+ * to:
+ *
+ * int io_uring_enter(unsigned int fd, u32 to_submit, u32 min_complete, u32 flags);
+ *
+ * 对于cq
+ * kernel修改tail, user修改head
+ * In this ring, the r.head index points to the first available completion
+ * event, while r.tail points to the last; user space should only change
+ * r.head.
+ */
+
#define IORING_MAX_ENTRIES 4096
#define IORING_MAX_FIXED_FILES 1024
+/*
+ * The head and tail values are used to manage entries in the ring; if the
+ * two values are equal, the ring is empty. User-space code adds an entry
+ * by putting its index into array[r.tail] and incrementing the tail
+ * pointer; only the kernel side should change r.head. Once one or more
+ * entries have been placed in the ring, they can be submitted with a call
+ * to:
+ *
+ * int io_uring_enter(unsigned int fd, u32 to_submit, u32 min_complete, u32 flags);
+ */
+
struct io_uring {
u32 head ____cacheline_aligned_in_smp;
u32 tail ____cacheline_aligned_in_smp;
@@ -104,6 +134,13 @@ struct async_list {
size_t io_pages;
};
+/*
+ * io_allocate_scq_urings()执行后:
+ * struct io_ring_ctx
+ * struct io_sq_ring *sq_ring; --> 指向struct io_sq_ring + 一组u32 array[]
+ * struct io_uring_sqe *sq_sqes; --> 指向一组struct io_uring_sqe[]
+ * struct io_cq_ring *cq_ring; --> 指向struct io_cq_ring + 一组struct io_uring_cqe[]
+ */
struct io_ring_ctx {
struct {
struct percpu_ref refs;
@@ -116,8 +153,21 @@ struct io_ring_ctx {
/* SQ ring */
struct io_sq_ring *sq_ring;
+ /*
+ * ctx->cached_sq_head修改的地方:
+ * - fs/io_uring.c|2085| <<io_drop_sqring>> ctx->cached_sq_head--;
+ * - fs/io_uring.c|2119| <<io_get_sqring>> ctx->cached_sq_head++;
+ * - fs/io_uring.c|2124| <<io_get_sqring>> ctx->cached_sq_head++;
+ *
+ * 对于sq
+ * user修改tail, kernel修改head
+ */
unsigned cached_sq_head;
unsigned sq_entries;
+ /*
+ * 唯一修改的地方:
+ * - fs/io_uring.c|2792| <<io_allocate_scq_urings>> ctx->sq_mask = sq_ring->ring_mask;
+ */
unsigned sq_mask;
unsigned sq_thread_idle;
struct io_uring_sqe *sq_sqes;
@@ -127,6 +177,17 @@ struct io_ring_ctx {
struct workqueue_struct *sqo_wq;
struct task_struct *sqo_thread; /* if using sq thread polling */
struct mm_struct *sqo_mm;
+ /*
+ * sqo_wait在以下使用:
+ * - fs/io_uring.c|587| <<io_cqring_ev_posted>> if (waitqueue_active(&ctx->sqo_wait))
+ * - fs/io_uring.c|588| <<io_cqring_ev_posted>> wake_up(&ctx->sqo_wait);
+ * - fs/io_uring.c|2077| <<io_sq_thread>> prepare_to_wait(&ctx->sqo_wait, &wait,
+ * - fs/io_uring.c|2086| <<io_sq_thread>> finish_wait(&ctx->sqo_wait, &wait);
+ * - fs/io_uring.c|2092| <<io_sq_thread>> finish_wait(&ctx->sqo_wait, &wait);
+ * - fs/io_uring.c|2098| <<io_sq_thread>> finish_wait(&ctx->sqo_wait, &wait);
+ * - fs/io_uring.c|2448| <<io_sq_offload_start>> init_waitqueue_head(&ctx->sqo_wait);
+ * - fs/io_uring.c|2891| <<SYSCALL_DEFINE6>> wake_up(&ctx->sqo_wait);
+ */
wait_queue_head_t sqo_wait;
unsigned sqo_stop;
@@ -135,7 +196,17 @@ struct io_ring_ctx {
struct io_cq_ring *cq_ring;
unsigned cached_cq_tail;
unsigned cq_entries;
+ /*
+ * 唯一修改的地方:
+ * - fs/io_uring.c|2815| <<io_allocate_scq_urings>> ctx->cq_mask = cq_ring->ring_mask;
+ */
unsigned cq_mask;
+ /*
+ * cq_wait在以下使用:
+ * - fs/io_uring.c|468| <<io_commit_cqring>> if (wq_has_sleeper(&ctx->cq_wait)) {
+ * - fs/io_uring.c|469| <<io_commit_cqring>> wake_up_interruptible(&ctx->cq_wait);
+ * - fs/io_uring.c|2713| <<io_uring_poll>> poll_wait(file, &ctx->cq_wait, wait);
+ */
struct wait_queue_head cq_wait;
struct fasync_struct *cq_fasync;
} ____cacheline_aligned_in_smp;
@@ -154,10 +225,28 @@ struct io_ring_ctx {
struct user_struct *user;
+ /*
+ * used by:
+ * - fs/io_uring.c|326| <<io_ring_ctx_ref_free>> complete(&ctx->ctx_done);
+ * - fs/io_uring.c|345| <<io_ring_ctx_alloc>> init_completion(&ctx->ctx_done);
+ * - fs/io_uring.c|2649| <<io_ring_ctx_wait_and_kill>> wait_for_completion(&ctx->ctx_done);
+ * - fs/io_uring.c|2985| <<__io_uring_register>> wait_for_completion(&ctx->ctx_done);
+ * - fs/io_uring.c|3012| <<__io_uring_register>> reinit_completion(&ctx->ctx_done);
+ */
struct completion ctx_done;
struct {
struct mutex uring_lock;
+ /*
+ * wait在以下被使用:
+ * - fs/io_uring.c|450| <<io_ring_ctx_alloc>> init_waitqueue_head(&ctx->wait);
+ * - fs/io_uring.c|556| <<io_cqring_ev_posted>> if (waitqueue_active(&ctx->wait))
+ * - fs/io_uring.c|557| <<io_cqring_ev_posted>> wake_up(&ctx->wait);
+ * - fs/io_uring.c|579| <<io_ring_drop_ctx_refs>> if (waitqueue_active(&ctx->wait))
+ * - fs/io_uring.c|580| <<io_ring_drop_ctx_refs>> wake_up(&ctx->wait);
+ * - fs/io_uring.c|2183| <<io_cqring_wait>> prepare_to_wait(&ctx->wait, &wait, TASK_INTERRUPTIBLE);
+ * - fs/io_uring.c|2198| <<io_cqring_wait>> finish_wait(&ctx->wait, &wait);
+ */
wait_queue_head_t wait;
} ____cacheline_aligned_in_smp;
@@ -170,10 +259,39 @@ struct io_ring_ctx {
* For SQPOLL, only the single threaded io_sq_thread() will
* manipulate the list, hence no extra locking is needed there.
*/
+ /*
+ * poll_list在以下被使用:
+ * - fs/io_uring.c|370| <<io_ring_ctx_alloc>> INIT_LIST_HEAD(&ctx->poll_list);
+ * - fs/io_uring.c|587| <<io_do_iopoll>> list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
+ * - fs/io_uring.c|625| <<io_iopoll_getevents>> while (!list_empty(&ctx->poll_list)) {
+ * - fs/io_uring.c|648| <<io_iopoll_reap_events>> while (!list_empty(&ctx->poll_list)) {
+ * - fs/io_uring.c|727| <<io_iopoll_req_issued>> if (list_empty(&ctx->poll_list)) {
+ * - fs/io_uring.c|732| <<io_iopoll_req_issued>> list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
+ * - fs/io_uring.c|743| <<io_iopoll_req_issued>> list_add(&req->list, &ctx->poll_list);
+ * - fs/io_uring.c|745| <<io_iopoll_req_issued>> list_add_tail(&req->list, &ctx->poll_list);
+ */
struct list_head poll_list;
+ /*
+ * cancel_list在以下被使用:
+ * - fs/io_uring.c|382| <<io_ring_ctx_alloc>> INIT_LIST_HEAD(&ctx->cancel_list);
+ * - fs/io_uring.c|1219| <<io_poll_remove_all>> while (!list_empty(&ctx->cancel_list)) {
+ * - fs/io_uring.c|1220| <<io_poll_remove_all>> req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
+ * - fs/io_uring.c|1243| <<io_poll_remove>> list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
+ * - fs/io_uring.c|1397| <<io_poll_add>> list_add_tail(&req->list, &ctx->cancel_list);
+ */
struct list_head cancel_list;
} ____cacheline_aligned_in_smp;
+ /*
+ * pending_async在以下被使用:
+ * - fs/io_uring.c|383| <<io_ring_ctx_alloc>> for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
+ * - fs/io_uring.c|384| <<io_ring_ctx_alloc>> spin_lock_init(&ctx->pending_async[i].lock);
+ * - fs/io_uring.c|385| <<io_ring_ctx_alloc>> INIT_LIST_HEAD(&ctx->pending_async[i].list);
+ * - fs/io_uring.c|386| <<io_ring_ctx_alloc>> atomic_set(&ctx->pending_async[i].cnt, 0);
+ * - fs/io_uring.c|991| <<io_async_list_note>> struct async_list *async_list = &req->ctx->pending_async[rw];
+ * - fs/io_uring.c|1491| <<io_async_list_from_sqe>> return &ctx->pending_async[READ];
+ * - fs/io_uring.c|1494| <<io_async_list_from_sqe>> return &ctx->pending_async[WRITE];
+ */
struct async_list pending_async[2];
#if defined(CONFIG_UNIX)
@@ -182,7 +300,13 @@ struct io_ring_ctx {
};
struct sqe_submit {
+ /*
+ * 例子是&ctx->sq_sqes[head]
+ */
const struct io_uring_sqe *sqe;
+ /*
+ * 例子是READ_ONCE(ring->array[head & ctx->sq_mask])
+ */
unsigned short index;
bool has_user;
bool needs_lock;
@@ -211,6 +335,11 @@ struct io_poll_iocb {
struct io_kiocb {
union {
struct file *file;
+ /*
+ * 有两处设置ki_complete:
+ * - fs/io_uring.c|872| <<io_prep_rw>> kiocb->ki_complete = io_complete_rw_iopoll;
+ * - fs/io_uring.c|876| <<io_prep_rw>> kiocb->ki_complete = io_complete_rw;
+ */
struct kiocb rw;
struct io_poll_iocb poll;
};
@@ -252,13 +381,33 @@ struct io_submit_state {
unsigned int fd;
unsigned int has_refs;
unsigned int used_refs;
+ /*
+ * 修改ios_left的地方:
+ * - fs/io_uring.c|814| <<io_file_get>> state->ios_left--;
+ * - fs/io_uring.c|826| <<io_file_get>> state->ios_left--;
+ * - fs/io_uring.c|1792| <<io_submit_state_start>> state->ios_left = max_ios;
+ */
unsigned int ios_left;
};
+/*
+ * used by:
+ * - fs/io_uring.c|518| <<io_get_req>> req = kmem_cache_alloc(req_cachep, gfp);
+ * - fs/io_uring.c|526| <<io_get_req>> ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
+ * - fs/io_uring.c|533| <<io_get_req>> state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
+ * - fs/io_uring.c|560| <<io_free_req_many>> kmem_cache_free_bulk(req_cachep, *nr, reqs);
+ * - fs/io_uring.c|571| <<io_free_req>> kmem_cache_free(req_cachep, req);
+ * - fs/io_uring.c|1785| <<io_submit_state_end>> kmem_cache_free_bulk(req_cachep, state->free_reqs,
+ * - fs/io_uring.c|3128| <<io_uring_init>> req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
+ */
static struct kmem_cache *req_cachep;
static const struct file_operations io_uring_fops;
+/*
+ * called only by:
+ * - net/unix/scm.c|38| <<unix_get_socket>> u_sock = io_uring_get_socket(filp);
+ */
struct sock *io_uring_get_socket(struct file *file)
{
#if defined(CONFIG_UNIX)
@@ -272,13 +421,44 @@ struct sock *io_uring_get_socket(struct file *file)
}
EXPORT_SYMBOL(io_uring_get_socket);
+/*
+ * callstack的一个例子
+ * [0] io_ring_ctx_ref_free
+ * [0] percpu_ref_switch_to_atomic_rcu
+ * [0] rcu_core
+ * [0] __do_softirq
+ * [0] irq_exit
+ * [0] smp_apic_timer_interrupt
+ * [0] apic_timer_interrupt
+ *
+ * used only by:
+ * - fs/io_uring.c|410| <<io_ring_ctx_alloc>> if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, 0, GFP_KERNEL)) {
+ *
+ * 被用做io_ring_ctx->refs这个percpu_ref的release函数
+ */
static void io_ring_ctx_ref_free(struct percpu_ref *ref)
{
struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
+ /*
+ * used by:
+ * - fs/io_uring.c|326| <<io_ring_ctx_ref_free>> complete(&ctx->ctx_done);
+ * - fs/io_uring.c|345| <<io_ring_ctx_alloc>> init_completion(&ctx->ctx_done);
+ * - fs/io_uring.c|2649| <<io_ring_ctx_wait_and_kill>> wait_for_completion(&ctx->ctx_done);
+ * - fs/io_uring.c|2985| <<__io_uring_register>> wait_for_completion(&ctx->ctx_done);
+ * - fs/io_uring.c|3012| <<__io_uring_register>> reinit_completion(&ctx->ctx_done);
+ */
complete(&ctx->ctx_done);
}
+/*
+ * 分配和简单初始化io_ring_ctx
+ *
+ * called by only:
+ * - fs/io_uring.c|2972| <<io_uring_create>> ctx = io_ring_ctx_alloc(p);
+ *
+ * SYSCALL_DEFINE2(io_uring_setup)-->io_uring_setup()-->io_uring_create()-->io_ring_ctx_alloc()
+ */
static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
{
struct io_ring_ctx *ctx;
@@ -309,12 +489,21 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
return ctx;
}
+/*
+ * called by:
+ * - fs/io_uring.c|527| <<io_cqring_add_event>> io_commit_cqring(ctx);
+ * - fs/io_uring.c|647| <<io_iopoll_complete>> io_commit_cqring(ctx);
+ * - fs/io_uring.c|1330| <<io_poll_complete>> io_commit_cqring(ctx);
+ */
static void io_commit_cqring(struct io_ring_ctx *ctx)
{
struct io_cq_ring *ring = ctx->cq_ring;
if (ctx->cached_cq_tail != READ_ONCE(ring->r.tail)) {
/* order cqe stores with ring update */
+ /*
+ * barrier之后调用WRITE_ONCE(*p, v)
+ */
smp_store_release(&ring->r.tail, ctx->cached_cq_tail);
/*
@@ -324,12 +513,25 @@ static void io_commit_cqring(struct io_ring_ctx *ctx)
smp_wmb();
if (wq_has_sleeper(&ctx->cq_wait)) {
+ /*
+ * cq_wait在以下使用:
+ * - fs/io_uring.c|468| <<io_commit_cqring>> if (wq_has_sleeper(&ctx->cq_wait)) {
+ * - fs/io_uring.c|469| <<io_commit_cqring>> wake_up_interruptible(&ctx->cq_wait);
+ * - fs/io_uring.c|2713| <<io_uring_poll>> poll_wait(file, &ctx->cq_wait, wait);
+ */
wake_up_interruptible(&ctx->cq_wait);
+ /*
+ * 通知用户进程???
+ */
kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
}
}
}
+/*
+ * called by only:
+ * - fs/io_uring.c|500| <<io_cqring_fill_event>> cqe = io_get_cqring(ctx);
+ */
static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
{
struct io_cq_ring *ring = ctx->cq_ring;
@@ -338,6 +540,9 @@ static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
tail = ctx->cached_cq_tail;
/* See comment at the top of the file */
smp_rmb();
+ /*
+ * 为什么还有一个不可以??
+ */
if (tail + 1 == READ_ONCE(ring->r.head))
return NULL;
@@ -345,6 +550,14 @@ static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
return &ring->cqes[tail & ctx->cq_mask];
}
+/*
+ * called by:
+ * - fs/io_uring.c|556| <<io_cqring_add_event>> io_cqring_fill_event(ctx, user_data, res, ev_flags);
+ * - fs/io_uring.c|658| <<io_iopoll_complete>> io_cqring_fill_event(ctx, req->user_data, req->error, 0);
+ * - fs/io_uring.c|1359| <<io_poll_complete>> io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask), 0);
+ *
+ * 这个函数是把返回的数据结果写入cq的某一个entry
+ */
static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
long res, unsigned ev_flags)
{
@@ -355,6 +568,7 @@ static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
* submission (by quite a lot). Increment the overflow count in
* the ring.
*/
+ /* 只在这里被调用 */
cqe = io_get_cqring(ctx);
if (cqe) {
WRITE_ONCE(cqe->user_data, ki_user_data);
@@ -367,14 +581,53 @@ static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
}
}
+/*
+ * called by:
+ * - fs/io_uring.c|572| <<io_cqring_add_event>> io_cqring_ev_posted(ctx);
+ * - fs/io_uring.c|1403| <<io_poll_complete_work>> io_cqring_ev_posted(ctx);
+ * - fs/io_uring.c|1428| <<io_poll_wake>> io_cqring_ev_posted(ctx);
+ * - fs/io_uring.c|1518| <<io_poll_add>> io_cqring_ev_posted(ctx);
+ *
+ * 唤醒ctx->wait和ctx->sqo_wait
+ */
static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
{
+ /*
+ * wait在以下被使用:
+ * - fs/io_uring.c|450| <<io_ring_ctx_alloc>> init_waitqueue_head(&ctx->wait);
+ * - fs/io_uring.c|556| <<io_cqring_ev_posted>> if (waitqueue_active(&ctx->wait))
+ * - fs/io_uring.c|557| <<io_cqring_ev_posted>> wake_up(&ctx->wait);
+ * - fs/io_uring.c|579| <<io_ring_drop_ctx_refs>> if (waitqueue_active(&ctx->wait))
+ * - fs/io_uring.c|580| <<io_ring_drop_ctx_refs>> wake_up(&ctx->wait);
+ * - fs/io_uring.c|2183| <<io_cqring_wait>> prepare_to_wait(&ctx->wait, &wait, TASK_INTERRUPTIBLE);
+ * - fs/io_uring.c|2198| <<io_cqring_wait>> finish_wait(&ctx->wait, &wait);
+ */
if (waitqueue_active(&ctx->wait))
wake_up(&ctx->wait);
+ /*
+ * sqo_wait在以下使用:
+ * - fs/io_uring.c|587| <<io_cqring_ev_posted>> if (waitqueue_active(&ctx->sqo_wait))
+ * - fs/io_uring.c|588| <<io_cqring_ev_posted>> wake_up(&ctx->sqo_wait);
+ * - fs/io_uring.c|2077| <<io_sq_thread>> prepare_to_wait(&ctx->sqo_wait, &wait,
+ * - fs/io_uring.c|2086| <<io_sq_thread>> finish_wait(&ctx->sqo_wait, &wait);
+ * - fs/io_uring.c|2092| <<io_sq_thread>> finish_wait(&ctx->sqo_wait, &wait);
+ * - fs/io_uring.c|2098| <<io_sq_thread>> finish_wait(&ctx->sqo_wait, &wait);
+ * - fs/io_uring.c|2448| <<io_sq_offload_start>> init_waitqueue_head(&ctx->sqo_wait);
+ * - fs/io_uring.c|2891| <<SYSCALL_DEFINE6>> wake_up(&ctx->sqo_wait);
+ */
if (waitqueue_active(&ctx->sqo_wait))
wake_up(&ctx->sqo_wait);
}
+/*
+ * called by:
+ * - fs/io_uring.c|869| <<io_complete_rw>> io_cqring_add_event(req->ctx, req->user_data, res, 0);
+ * - fs/io_uring.c|1306| <<io_nop>> io_cqring_add_event(ctx, user_data, err, 0);
+ * - fs/io_uring.c|1355| <<io_fsync>> io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
+ * - fs/io_uring.c|1413| <<io_poll_remove>> io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
+ * - fs/io_uring.c|1713| <<io_sq_wq_submit_work>> io_cqring_add_event(ctx, sqe->user_data, ret, 0);
+ * - fs/io_uring.c|2023| <<io_submit_sqes>> io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret, 0);
+ */
static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
long res, unsigned ev_flags)
{
@@ -388,6 +641,13 @@ static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
io_cqring_ev_posted(ctx);
}
+/*
+ * called by:
+ * - fs/io_uring.c|691| <<io_get_req>> io_ring_drop_ctx_refs(ctx, 1);
+ * - fs/io_uring.c|699| <<io_free_req_many>> io_ring_drop_ctx_refs(ctx, *nr);
+ * - fs/io_uring.c|708| <<io_free_req>> io_ring_drop_ctx_refs(req->ctx, 1);
+ * - fs/io_uring.c|2965| <<SYSCALL_DEFINE6(io_uring_enter)>> io_ring_drop_ctx_refs(ctx, 1);
+ */
static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
{
percpu_ref_put_many(&ctx->refs, refs);
@@ -396,6 +656,11 @@ static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
wake_up(&ctx->wait);
}
+/*
+ * called by:
+ * - called by only:
+ * - fs/io_uring.c|1656| <<io_submit_sqe>> req = io_get_req(ctx, state);
+ */
static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
struct io_submit_state *state)
{
@@ -413,6 +678,12 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
size_t sz;
int ret;
+ /*
+ * 修改ios_left的地方:
+ * - fs/io_uring.c|814| <<io_file_get>> state->ios_left--;
+ * - fs/io_uring.c|826| <<io_file_get>> state->ios_left--;
+ * - fs/io_uring.c|1792| <<io_submit_state_start>> state->ios_left = max_ios;
+ */
sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
@@ -462,6 +733,20 @@ static void io_free_req(struct io_kiocb *req)
kmem_cache_free(req_cachep, req);
}
+/*
+ * called by:
+ * - fs/io_uring.c|879| <<io_complete_rw>> io_put_req(req);
+ * - fs/io_uring.c|1316| <<io_nop>> io_put_req(req);
+ * - fs/io_uring.c|1365| <<io_fsync>> io_put_req(req);
+ * - fs/io_uring.c|1423| <<io_poll_remove>> io_put_req(req);
+ * - fs/io_uring.c|1464| <<io_poll_complete_work>> io_put_req(req);
+ * - fs/io_uring.c|1489| <<io_poll_wake>> io_put_req(req);
+ * - fs/io_uring.c|1579| <<io_poll_add>> io_put_req(req);
+ * - fs/io_uring.c|1719| <<io_sq_wq_submit_work>> io_put_req(req);
+ * - fs/io_uring.c|1723| <<io_sq_wq_submit_work>> io_put_req(req);
+ * - fs/io_uring.c|1903| <<io_submit_sqe>> io_put_req(req);
+ * - fs/io_uring.c|1907| <<io_submit_sqe>> io_put_req(req);
+ */
static void io_put_req(struct io_kiocb *req)
{
if (refcount_dec_and_test(&req->refs))
@@ -506,6 +791,10 @@ static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
io_free_req_many(ctx, reqs, &to_free);
}
+/*
+ * called only by:
+ * - fs/io_uring.c|841| <<io_iopoll_getevents>> ret = io_do_iopoll(ctx, nr_events, min);
+ */
static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
long min)
{
@@ -556,6 +845,11 @@ static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
* non-spinning poll check - we'll still enter the driver poll loop, but only
* as a non-spinning completion check.
*/
+/*
+ * called by:
+ * - fs/io_uring.c|864| <<io_iopoll_reap_events>> io_iopoll_getevents(ctx, &nr_events, 1);
+ * - fs/io_uring.c|880| <<io_iopoll_check>> ret = io_iopoll_getevents(ctx, nr_events, tmin);
+ */
static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
long min)
{
@@ -590,6 +884,11 @@ static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
mutex_unlock(&ctx->uring_lock);
}
+/*
+ * called by:
+ * - fs/io_uring.c|2102| <<io_sq_thread>> io_iopoll_check(ctx, &nr_events, 0);
+ * - fs/io_uring.c|2986| <<SYSCALL_DEFINE6(io_uring_enter)>> ret = io_iopoll_check(ctx, &nr_events, min_complete);
+ */
static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
long min)
{
@@ -610,6 +909,11 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
return ret;
}
+/*
+ * called by:
+ * - fs/io_uring.c|922| <<io_complete_rw>> kiocb_end_write(kiocb);
+ * - fs/io_uring.c|932| <<io_complete_rw_iopoll>> kiocb_end_write(kiocb);
+ */
static void kiocb_end_write(struct kiocb *kiocb)
{
if (kiocb->ki_flags & IOCB_WRITE) {
@@ -741,6 +1045,11 @@ static bool io_file_supports_async(struct file *file)
return false;
}
+/*
+ * called by:
+ * - fs/io_uring.c|1242| <<io_read>> ret = io_prep_rw(req, s, force_nonblock, state);
+ * - fs/io_uring.c|1289| <<io_write>> ret = io_prep_rw(req, s, force_nonblock, state);
+ */
static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
bool force_nonblock, struct io_submit_state *state)
{
@@ -939,6 +1248,13 @@ static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
async_list->io_end = io_end;
}
+/*
+ * called by:
+ * - fs/io_uring.c|1658| <<__io_submit_sqe>> ret = io_read(req, s, force_nonblock, state);
+ * - fs/io_uring.c|1666| <<__io_submit_sqe>> ret = io_read(req, s, force_nonblock, state);
+ *
+ * 处理IORING_OP_READV或者IORING_OP_READ_FIXED
+ */
static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
bool force_nonblock, struct io_submit_state *state)
{
@@ -986,6 +1302,13 @@ static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
return ret;
}
+/*
+ * called by:
+ * - fs/io_uring.c|1670| <<__io_submit_sqe>> ret = io_write(req, s, force_nonblock, state);
+ * - fs/io_uring.c|1676| <<__io_submit_sqe>> ret = io_write(req, s, force_nonblock, state);
+ *
+ * 处理IORING_OP_WRITEV或者IORING_OP_WRITE_FIXED
+ */
static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
bool force_nonblock, struct io_submit_state *state)
{
@@ -1060,6 +1383,10 @@ static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
/*
* IORING_OP_NOP just posts a completion event, nothing else.
*/
+/*
+ * 处理IORING_OP_NOP:
+ * - fs/io_uring.c|1660| <<__io_submit_sqe>> ret = io_nop(req, req->user_data);
+ */
static int io_nop(struct io_kiocb *req, u64 user_data)
{
struct io_ring_ctx *ctx = req->ctx;
@@ -1073,6 +1400,10 @@ static int io_nop(struct io_kiocb *req, u64 user_data)
return 0;
}
+/*
+ * called by only:
+ * - fs/io_uring.c|1415| <<io_fsync>> ret = io_prep_fsync(req, sqe);
+ */
static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
{
struct io_ring_ctx *ctx = req->ctx;
@@ -1337,6 +1668,11 @@ static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
return ipt.error;
}
+/*
+ * called by:
+ * - fs/io_uring.c|1769| <<io_sq_wq_submit_work>> ret = __io_submit_sqe(ctx, req, s, false, NULL);
+ * - fs/io_uring.c|1935| <<io_submit_sqe>> ret = __io_submit_sqe(ctx, req, s, true, state);
+ */
static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
const struct sqe_submit *s, bool force_nonblock,
struct io_submit_state *state)
@@ -1345,6 +1681,9 @@ static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
if (unlikely(s->index >= ctx->sq_entries))
return -EINVAL;
+ /*
+ * req->user_data是u64 user_data;
+ */
req->user_data = READ_ONCE(s->sqe->user_data);
opcode = READ_ONCE(s->sqe->opcode);
@@ -1415,6 +1754,11 @@ static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
}
}
+/*
+ * called by:
+ * - fs/io_uring.c|1777| <<io_sq_wq_submit_work>> if (io_sqe_needs_user(sqe) && !cur_mm) {
+ * - fs/io_uring.c|2224| <<io_sq_thread>> if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
+ */
static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
{
u8 opcode = READ_ONCE(sqe->opcode);
@@ -1423,6 +1767,10 @@ static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
opcode == IORING_OP_WRITE_FIXED);
}
+/*
+ * used by:
+ * - fs/io_uring.c|1974| <<io_submit_sqe>> INIT_WORK(&req->work, io_sq_wq_submit_work);
+ */
static void io_sq_wq_submit_work(struct work_struct *work)
{
struct io_kiocb *req = container_of(work, struct io_kiocb, work);
@@ -1607,6 +1955,11 @@ static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
return 0;
}
+/*
+ * called by:
+ * - fs/io_uring.c|2120| <<io_submit_sqes>> ret = io_submit_sqe(ctx, &sqes[i], statep);
+ * - fs/io_uring.c|2285| <<io_ring_submit>> ret = io_submit_sqe(ctx, &s, statep);
+ */
static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
struct io_submit_state *state)
{
@@ -1625,6 +1978,9 @@ static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
if (unlikely(ret))
goto out;
+ /*
+ * 核心的函数!!!
+ */
ret = __io_submit_sqe(ctx, req, s, true, state);
if (ret == -EAGAIN) {
struct io_uring_sqe *sqe_copy;
@@ -1668,6 +2024,13 @@ static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
/*
* Batched submission is done, ensure local IO is flushed out.
*/
+/*
+ * called by:
+ * - fs/io_uring.c|2139| <<io_submit_sqes>> io_submit_state_end(&state);
+ * - fs/io_uring.c|2304| <<io_ring_submit>> io_submit_state_end(statep);
+ *
+ * 核心思想是blk_finish_plug(&state->plug);
+ */
static void io_submit_state_end(struct io_submit_state *state)
{
blk_finish_plug(&state->plug);
@@ -1680,6 +2043,13 @@ static void io_submit_state_end(struct io_submit_state *state)
/*
* Start submission side cache.
*/
+/*
+ * called by:
+ * - fs/io_uring.c|2117| <<io_submit_sqes>> io_submit_state_start(&state, ctx, nr);
+ * - fs/io_uring.c|2279| <<io_ring_submit>> io_submit_state_start(&state, ctx, to_submit);
+ *
+ * 核心思想是blk_start_plug(&state->plug);
+ */
static void io_submit_state_start(struct io_submit_state *state,
struct io_ring_ctx *ctx, unsigned max_ios)
{
@@ -1689,6 +2059,13 @@ static void io_submit_state_start(struct io_submit_state *state,
state->ios_left = max_ios;
}
+/*
+ * called by:
+ * - fs/io_uring.c|2262| <<io_sq_thread>> io_commit_sqring(ctx);
+ * - fs/io_uring.c|2301| <<io_ring_submit>> io_commit_sqring(ctx);
+ *
+ * 将ctx->cached_sq_head同步到io_sq_ring->r.head
+ */
static void io_commit_sqring(struct io_ring_ctx *ctx)
{
struct io_sq_ring *ring = ctx->sq_ring;
@@ -1725,6 +2102,16 @@ static void io_drop_sqring(struct io_ring_ctx *ctx)
* used, it's important that those reads are done through READ_ONCE() to
* prevent a re-load down the line.
*/
+/*
+ * called by:
+ * - fs/io_uring.c|2209| <<io_sq_thread>> if (!io_get_sqring(ctx, &sqes[0])) {
+ * - fs/io_uring.c|2239| <<io_sq_thread>> if (!io_get_sqring(ctx, &sqes[0])) {
+ * - fs/io_uring.c|2268| <<io_sq_thread>> } while (io_get_sqring(ctx, &sqes[i]));
+ * - fs/io_uring.c|2307| <<io_ring_submit>> if (!io_get_sqring(ctx, &s))
+ *
+ * 核心思想, 取出head = ring->array[ctx->cached_sq_head & ctx->sq_mask]
+ * 把结果存入参数的sqe_submit, 增加ctx->cached_sq_head
+ */
static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
{
struct io_sq_ring *ring = ctx->sq_ring;
@@ -1738,14 +2125,30 @@ static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
* 2) allows the kernel side to track the head on its own, even
* though the application is the one updating it.
*/
+ /*
+ * ctx->cached_sq_head修改的地方:
+ * - fs/io_uring.c|2085| <<io_drop_sqring>> ctx->cached_sq_head--;
+ * - fs/io_uring.c|2119| <<io_get_sqring>> ctx->cached_sq_head++;
+ * - fs/io_uring.c|2124| <<io_get_sqring>> ctx->cached_sq_head++;
+ *
+ * 对于sq
+ * user修改tail, kernel修改head
+ */
head = ctx->cached_sq_head;
/* See comment at the top of this file */
smp_rmb();
+ /*
+ * r的类型: struct io_uring r;
+ */
if (head == READ_ONCE(ring->r.tail))
return false;
+ /*
+ * array是最后的位置: u32 array[]
+ */
head = READ_ONCE(ring->array[head & ctx->sq_mask]);
if (head < ctx->sq_entries) {
+ /* s是参数的struct sqe_submit *s */
s->index = head;
s->sqe = &ctx->sq_sqes[head];
ctx->cached_sq_head++;
@@ -1760,6 +2163,10 @@ static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
return false;
}
+/*
+ * called by:
+ * - fs/io_uring.c|2279| <<io_sq_thread>> inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
+ */
static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
unsigned int nr, bool has_user, bool mm_fault)
{
@@ -1794,6 +2201,11 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
return submitted;
}
+/*
+ * called by:
+ * - fs/io_uring.c|2624| <<io_sq_offload_start>> ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
+ * - fs/io_uring.c|2628| <<io_sq_offload_start>> ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
+ */
static int io_sq_thread(void *data)
{
struct sqe_submit sqes[IO_IOPOLL_BATCH];
@@ -1923,12 +2335,17 @@ static int io_sq_thread(void *data)
return 0;
}
+/*
+ * called by:
+ * - fs/io_uring.c|3100| <<SYSCALL_DEFINE6(io_uring_enter)>> submitted = io_ring_submit(ctx, to_submit);
+ */
static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
{
struct io_submit_state state, *statep = NULL;
int i, ret = 0, submit = 0;
if (to_submit > IO_PLUG_THRESHOLD) {
+ /* 核心思想是blk_start_plug(&state->plug); */
io_submit_state_start(&state, ctx, to_submit);
statep = &state;
}
@@ -1936,23 +2353,39 @@ static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
for (i = 0; i < to_submit; i++) {
struct sqe_submit s;
+ /*
+ * 核心思想, 取出head = ring->array[ctx->cached_sq_head & ctx->sq_mask]
+ * 把结果存入参数的sqe_submit, 增加ctx->cached_sq_head
+ */
if (!io_get_sqring(ctx, &s))
break;
+ /*
+ * 此时
+ * s.sqe = &ctx->sq_sqes[head]
+ * s.index = READ_ONCE(ring->array[head & ctx->sq_mask])
+ */
+
s.has_user = true;
s.needs_lock = false;
s.needs_fixed_file = false;
+ /*
+ * 猜测这里是下发下去
+ */
ret = io_submit_sqe(ctx, &s, statep);
if (ret) {
+ /* ctx->cached_sq_head-- */
io_drop_sqring(ctx);
break;
}
submit++;
}
+ /* 将ctx->cached_sq_head同步到io_sq_ring->r.head */
io_commit_sqring(ctx);
+ /* 核心思想是blk_finish_plug(&state->plug); */