File size: 5,721 Bytes
590b382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
diff --git a/flashinfer/experimental/b12x/loader/_bounce.c b/flashinfer/experimental/b12x/loader/_bounce.c
index 4220bbc9..531f9169 100644
--- a/flashinfer/experimental/b12x/loader/_bounce.c
+++ b/flashinfer/experimental/b12x/loader/_bounce.c
@@ -146,17 +146,17 @@ static bounce_ring_t* bounce_create(int device, unsigned depth, failure_t* failu
      it faults in can come from CMA, and a page it holds cannot migrate, so a later
      io_uring registration fails with EFAULT. */
   error = io_uring_queue_init(BOUNCE_SLOTS, &ring->ring, 0);
-  if (error < 0) {
+  if (error < 0 && error != -ENOSYS) {
     error = -error;
     goto failed;
   }
-  ring->ring_ready = true;
+  ring->ring_ready = (error == 0);
   struct iovec buffers[BOUNCE_SLOTS];
   for (unsigned i = 0; i < BOUNCE_SLOTS; i++) {
     buffers[i] = (struct iovec){ring->buffers + (size_t)i * BOUNCE_SLOT_BYTES, BOUNCE_SLOT_BYTES};
     ring->free_slots[ring->free_count++] = i;
   }
-  error = io_uring_register_buffers(&ring->ring, buffers, BOUNCE_SLOTS);
+  error = ring->ring_ready ? io_uring_register_buffers(&ring->ring, buffers, BOUNCE_SLOTS) : 0;
   if (error < 0) {
     error = -error;
     goto failed;
@@ -184,6 +184,29 @@ failed:
 #endif
 }
 
+#ifdef B12X_HAVE_LIBURING
+/* A padded read at EOF is sufficient only if it contains the whole logical span. */
+static void bounce_read_complete(bounce_ring_t* ring, unsigned index, int status,
+                                 failure_t* failure) {
+  ring->reads++;
+  if (status > 0) ring->physical_bytes += status;
+  bool success = status >= 0 && (size_t)status >= ring->slots[index].required;
+  if (!success && !failure->message[0])
+    snprintf(failure->message, sizeof(failure->message), "%s: %s",
+             ring->ring_ready ? "io_uring bounce read" : "bounce pread",
+             status < 0 ? strerror(-status) : "short O_DIRECT read");
+  pthread_mutex_lock(&ring->mutex);
+  if (success) {
+    ring->ready_slots[ring->tail++ % BOUNCE_SLOTS] = index;
+    ring->ready_count++;
+    ring->copying++;
+    pthread_cond_signal(&ring->ready);
+  } else
+    ring->free_slots[ring->free_count++] = index;
+  pthread_mutex_unlock(&ring->mutex);
+}
+#endif
+
 static bool bounce_execute(bounce_ring_t* ring, read_job_t* jobs, size_t count,
                            failure_t* failure) {
 #ifndef B12X_HAVE_LIBURING
@@ -254,17 +277,25 @@ static bool bounce_execute(bounce_ring_t* ring, read_job_t* jobs, size_t count,
         slot->job.rows = BOUNCE_SLOT_BYTES / (2 * slot->job.bytes);
       slot->required = slot->delta + (slot->job.rows - 1) * job->source_stride + slot->job.bytes;
       unsigned length = (slot->required + IO_ALIGNMENT - 1) & ~(size_t)(IO_ALIGNMENT - 1);
-      struct io_uring_sqe* sqe = io_uring_get_sqe(&ring->ring);
-      if (!sqe) {
-        snprintf(failure->message, sizeof(failure->message),
-                 "io_uring bounce SQ capacity exhausted");
-        ring->poisoned = true;
-        break;
+      char* buffer = ring->buffers + (size_t)index * BOUNCE_SLOT_BYTES;
+      if (ring->ring_ready) {
+        struct io_uring_sqe* sqe = io_uring_get_sqe(&ring->ring);
+        if (!sqe) {
+          snprintf(failure->message, sizeof(failure->message), "io_uring bounce SQ capacity exhausted");
+          ring->poisoned = true;
+          break;
+        }
+        io_uring_prep_read_fixed(sqe, job->fd, buffer, length, aligned, index);
+        io_uring_sqe_set_data64(sqe, index);
+        pending++;
+      } else {
+        ssize_t bytes;
+        do {
+          bytes = pread(job->fd, buffer, length, aligned);
+        } while (bytes < 0 && errno == EINTR);
+        ring->max_inflight = 1;
+        bounce_read_complete(ring, index, bytes < 0 ? -errno : (int)bytes, failure);
       }
-      io_uring_prep_read_fixed(sqe, job->fd, ring->buffers + (size_t)index * BOUNCE_SLOT_BYTES,
-                               length, aligned, index);
-      io_uring_sqe_set_data64(sqe, index);
-      pending++;
       within += slot->job.bytes;
       if (within == job->bytes) {
         row += slot->job.rows;
@@ -305,21 +336,7 @@ static bool bounce_execute(bounce_ring_t* ring, read_job_t* jobs, size_t count,
       status = cqe->res;
       io_uring_cqe_seen(&ring->ring, cqe);
       outstanding--;
-      ring->reads++;
-      if (status > 0) ring->physical_bytes += status;
-      bool success = status >= 0 && (size_t)status >= ring->slots[index].required;
-      if (!success && !failure->message[0])
-        snprintf(failure->message, sizeof(failure->message), "io_uring bounce read: %s",
-                 status < 0 ? strerror(-status) : "short O_DIRECT read");
-      pthread_mutex_lock(&ring->mutex);
-      if (success) {
-        ring->ready_slots[ring->tail++ % BOUNCE_SLOTS] = index;
-        ring->ready_count++;
-        ring->copying++;
-        pthread_cond_signal(&ring->ready);
-      } else
-        ring->free_slots[ring->free_count++] = index;
-      pthread_mutex_unlock(&ring->mutex);
+      bounce_read_complete(ring, index, status, failure);
     } else if (!failure->message[0] && next < count) {
       pthread_mutex_lock(&ring->mutex);
       while (!ring->free_count) pthread_cond_wait(&ring->available, &ring->mutex);
@@ -335,7 +352,7 @@ static bool bounce_execute(bounce_ring_t* ring, read_job_t* jobs, size_t count,
   pthread_mutex_unlock(&ring->mutex);
   if (!cuda_ok(cudaStreamSynchronize(ring->stream), "complete checkpoint metadata copies", failure))
     ring->poisoned = true;
-  if (ring->poisoned) {
+  if (ring->poisoned && ring->ring_ready) {
     /* Drain submitted reads above; never submit SQEs left by a failed submit. */
     io_uring_queue_exit(&ring->ring);
     ring->ring_ready = false;