Debian Codegraph @

src/dex-uring-aio-backend.c

raw symbols SCIP
1 /* dex-uring-aio-backend.c
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #include <errno.h>
24 #include <sys/eventfd.h>
25 #include <sys/utsname.h>
26 #include <stdio.h>
27 #include <unistd.h>
28
29 #include <liburing.h>
30
31 #include "dex-thread-storage-private.h"
32 #include "dex-uring-aio-backend-private.h"
33 #include "dex-uring-future-private.h"
34 #include "dex-uring-version.h"
35
36 #define DEFAULT_URING_SIZE 32
37
38 struct _DexUringAioBackend
39 {
40 DexAioBackend parent_instance;
41 DexAioContext *first;
42 };
43
44 struct _DexUringAioBackendClass
45 {
46 DexAioBackendClass parent_class;
47 };
48
49 typedef struct _DexUringAioContext
50 {
51 DexAioContext parent;
52 struct io_uring ring;
53 int eventfd;
54 gpointer eventfdtag;
55 GMutex mutex;
56 GQueue queued;
57 guint ring_initialized : 1;
58 } DexUringAioContext;
59
60 DEX_DEFINE_FINAL_TYPE (DexUringAioBackend, dex_uring_aio_backend, DEX_TYPE_AIO_BACKEND)
61
62 G_GNUC_UNUSED static gboolean
63 dex_uring_check_kernel_version (int major,
64 int minor)
65 {
66 static gsize initialized;
67 static int kernel_major;
68 static int kernel_minor;
69
70 if (g_once_init_enter (&initialized))
71 {
72 static struct utsname u;
73
74 if (uname (&u) == 0)
75 {
76 int maj, min;
77
78 if (sscanf (u.release, "%u.%u.", &maj, &min) == 2)
79 {
80 kernel_major = maj;
81 kernel_minor = min;
82 }
83 }
84
85 g_once_init_leave (&initialized, TRUE);
86 }
87
88 if (kernel_major == 0)
89 return FALSE;
90
91 return kernel_major >= major ||
92 (kernel_major == major && kernel_minor >= minor);
93 }
94
95 static gboolean
96 dex_uring_aio_context_dispatch (GSource *source,
97 GSourceFunc callback,
98 gpointer user_data)
99 {
100 DexUringAioContext *aio_context = (DexUringAioContext *)source;
101 DexUringFuture *handledstack[32];
102 struct io_uring_cqe *cqe;
103 gint64 counter;
104 guint n_handled;
105
106 if (g_source_query_unix_fd (source, aio_context->eventfdtag) & G_IO_IN)
107 {
108 if (read (aio_context->eventfd, &counter, sizeof counter) <= 0)
109 {
110 /* Do mothing */
111 }
112 }
113
114 again:
115 n_handled = 0;
116 while (io_uring_peek_cqe (&aio_context->ring, &cqe) == 0)
117 {
118 DexUringFuture *future = io_uring_cqe_get_data (cqe);
119 dex_uring_future_cqe (future, cqe);
120 io_uring_cqe_seen (&aio_context->ring, cqe);
121 handledstack[n_handled++] = future;
122
123 if G_UNLIKELY (n_handled == G_N_ELEMENTS (handledstack))
124 break;
125 }
126
127 for (guint i = 0; i < n_handled; i++)
128 {
129 DexUringFuture *future = handledstack[i];
130 dex_uring_future_complete (future);
131 dex_unref (future);
132 }
133
134 if G_UNLIKELY (n_handled == G_N_ELEMENTS (handledstack))
135 goto again;
136
137 return G_SOURCE_CONTINUE;
138 }
139
140 static gboolean
141 dex_uring_aio_context_prepare (GSource *source,
142 int *timeout)
143 {
144 DexUringAioContext *aio_context = (DexUringAioContext *)source;
145 gboolean do_submit;
146
147 g_assert (aio_context != NULL);
148 g_assert (DEX_IS_URING_AIO_BACKEND (aio_context->parent.aio_backend));
149
150 *timeout = -1;
151
152 g_mutex_lock (&aio_context->mutex);
153
154 do_submit = aio_context->queued.length > 0;
155
156 while (aio_context->queued.length)
157 {
158 struct io_uring_sqe *sqe;
159 DexUringFuture *future;
160
161 /* Try to get the next sqe, and submit if we can't get
162 * one right away. If we still fail to get an sqe, then
163 * we'll wait for completions to come in to advance this.
164 */
165 if G_UNLIKELY (!(sqe = io_uring_get_sqe (&aio_context->ring)))
166 {
167 io_uring_submit (&aio_context->ring);
168
169 if (!(sqe = io_uring_get_sqe (&aio_context->ring)))
170 break;
171 }
172
173 future = g_queue_pop_head (&aio_context->queued);
174 dex_uring_future_sqe (future, sqe);
175 io_uring_sqe_set_data (sqe, dex_ref (future));
176 dex_unref (future);
177 }
178
179 if (do_submit || io_uring_sq_ready (&aio_context->ring) > 0)
180 io_uring_submit (&aio_context->ring);
181
182 g_mutex_unlock (&aio_context->mutex);
183
184 return io_uring_cq_ready (&aio_context->ring) > 0;
185 }
186
187 static gboolean
188 dex_uring_aio_context_check (GSource *source)
189 {
190 DexUringAioContext *aio_context = (DexUringAioContext *)source;
191
192 g_assert (aio_context != NULL);
193 g_assert (DEX_IS_URING_AIO_BACKEND (aio_context->parent.aio_backend));
194
195 return io_uring_cq_ready (&aio_context->ring) > 0;
196 }
197
198 static void
199 dex_uring_aio_context_finalize (GSource *source)
200 {
201 DexUringAioContext *aio_context = (DexUringAioContext *)source;
202
203 g_assert (aio_context != NULL);
204 g_assert (DEX_IS_URING_AIO_BACKEND (aio_context->parent.aio_backend));
205
206 if (aio_context->queued.length > 0)
207 g_critical ("Destroying DexAioContext with queued items!");
208
209 if (aio_context->ring_initialized)
210 io_uring_queue_exit (&aio_context->ring);
211
212 dex_clear (&aio_context->parent.aio_backend);
213 g_mutex_clear (&aio_context->mutex);
214
215 if (aio_context->eventfd != -1)
216 {
217 close (aio_context->eventfd);
218 aio_context->eventfd = -1;
219 }
220 }
221
222 static GSourceFuncs dex_uring_aio_context_source_funcs = {
223 .check = dex_uring_aio_context_check,
224 .prepare = dex_uring_aio_context_prepare,
225 .dispatch = dex_uring_aio_context_dispatch,
226 .finalize = dex_uring_aio_context_finalize,
227 };
228
229 static DexFuture *
230 dex_uring_aio_context_queue (DexUringAioContext *aio_context,
231 DexUringFuture *future)
232 {
233 gboolean is_same_thread;
234 struct io_uring_sqe *sqe;
235
236 g_assert (aio_context != NULL);
237 g_assert (DEX_IS_URING_AIO_BACKEND (aio_context->parent.aio_backend));
238 g_assert (DEX_IS_URING_FUTURE (future));
239
240 is_same_thread = dex_thread_storage_get ()->aio_context == (DexAioContext *)aio_context;
241
242 g_mutex_lock (&aio_context->mutex);
243 if G_LIKELY (is_same_thread &&
244 aio_context->queued.length == 0 &&
245 (sqe = io_uring_get_sqe (&aio_context->ring)))
246 {
247 dex_uring_future_sqe (future, sqe);
248 io_uring_sqe_set_data (sqe, dex_ref (future));
249 }
250 else
251 {
252 g_queue_push_tail (&aio_context->queued, dex_ref (future));
253 }
254 g_mutex_unlock (&aio_context->mutex);
255
256 if (!is_same_thread)
257 g_main_context_wakeup (g_source_get_context ((GSource *)aio_context));
258
259 return DEX_FUTURE (future);
260 }
261
262 static DexAioContext *
263 dex_uring_aio_backend_create_context (DexAioBackend *aio_backend)
264 {
265 DexUringAioContext *aio_context;
266 guint uring_flags = 0;
267
268 g_assert (DEX_IS_URING_AIO_BACKEND (aio_backend));
269
270 if ((aio_context = g_atomic_pointer_exchange (&((DexUringAioBackend *)aio_backend)->first, NULL)))
271 return (DexAioContext *)aio_context;
272
273 aio_context = (DexUringAioContext *)
274 g_source_new (&dex_uring_aio_context_source_funcs,
275 sizeof *aio_context);
276 g_source_set_can_recurse ((GSource *)aio_context, TRUE);
277 aio_context->parent.aio_backend = dex_ref (aio_backend);
278 g_mutex_init (&aio_context->mutex);
279
280 #if DEX_URING_CHECK_VERSION(2, 2)
281 if (dex_uring_check_kernel_version (5, 19))
282 uring_flags |= IORING_SETUP_COOP_TASKRUN;
283 #endif
284
285 #if DEX_URING_CHECK_VERSION(2, 3)
286 if (dex_uring_check_kernel_version (6, 0))
287 uring_flags |= IORING_SETUP_SINGLE_ISSUER;
288 #endif
289
290 aio_context->eventfd = -1;
291
292 /* Setup uring submission/completion queue */
293 if (io_uring_queue_init (DEFAULT_URING_SIZE, &aio_context->ring, uring_flags) != 0)
294 goto failure;
295
296 aio_context->ring_initialized = TRUE;
297
298 #if DEX_URING_CHECK_VERSION(2, 2)
299 /* Register the ring FD so we don't have to on every io_ring_enter() */
300 if (io_uring_register_ring_fd (&aio_context->ring) < 0)
301 goto failure;
302 #endif
303
304 /* Create eventfd() we can poll() on with GMainContext since GMainContext
305 * knows nothing of uring and how to drive the loop using that.
306 */
307 if (-1 == (aio_context->eventfd = eventfd (0, EFD_CLOEXEC)) ||
308 io_uring_register_eventfd (&aio_context->ring, aio_context->eventfd) != 0)
309 goto failure;
310
311 /* Add the eventfd() to our set of pollfds and keep the tag around so
312 * we can check the condition directly.
313 */
314 aio_context->eventfdtag = g_source_add_unix_fd ((GSource *)aio_context,
315 aio_context->eventfd,
316 G_IO_IN);
317
318 return (DexAioContext *)aio_context;
319
320 failure:
321 g_source_unref ((GSource *)aio_context);
322
323 return NULL;
324 }
325
326 static DexFuture *
327 dex_uring_aio_backend_close (DexAioBackend *aio_backend,
328 DexAioContext *aio_context,
329 int fd)
330 {
331 return dex_uring_aio_context_queue ((DexUringAioContext *)aio_context,
332 dex_uring_future_new_close (fd));
333 }
334
335 static DexFuture *
336 dex_uring_aio_backend_open (DexAioBackend *aio_backend,
337 DexAioContext *aio_context,
338 const char *path,
339 int flags,
340 int mode)
341 {
342 return dex_uring_aio_context_queue ((DexUringAioContext *)aio_context,
343 dex_uring_future_new_open (path, flags, mode));
344 }
345
346 static DexFuture *
347 dex_uring_aio_backend_read (DexAioBackend *aio_backend,
348 DexAioContext *aio_context,
349 int fd,
350 gpointer buffer,
351 gsize count,
352 goffset offset)
353 {
354 return dex_uring_aio_context_queue ((DexUringAioContext *)aio_context,
355 dex_uring_future_new_read (fd, buffer, count, offset));
356 }
357
358 static DexFuture *
359 dex_uring_aio_backend_write (DexAioBackend *aio_backend,
360 DexAioContext *aio_context,
361 int fd,
362 gconstpointer buffer,
363 gsize count,
364 goffset offset)
365 {
366 return dex_uring_aio_context_queue ((DexUringAioContext *)aio_context,
367 dex_uring_future_new_write (fd, buffer, count, offset));
368 }
369
370 static void
371 dex_uring_aio_backend_class_init (DexUringAioBackendClass *uring_aio_backend_class)
372 {
373 DexAioBackendClass *aio_backend_class = DEX_AIO_BACKEND_CLASS (uring_aio_backend_class);
374
375 aio_backend_class->create_context = dex_uring_aio_backend_create_context;
376 aio_backend_class->close = dex_uring_aio_backend_close;
377 aio_backend_class->open = dex_uring_aio_backend_open;
378 aio_backend_class->read = dex_uring_aio_backend_read;
379 aio_backend_class->write = dex_uring_aio_backend_write;
380 }
381
382 static void
383 dex_uring_aio_backend_init (DexUringAioBackend *uring_aio_backend)
384 {
385 }
386
387 DexAioBackend *
388 dex_uring_aio_backend_new (void)
389 {
390 DexAioBackend *aio_backend;
391 DexAioContext *aio_context;
392
393 /* We run into a number of issues with io_uring on older kernels which
394 * makes it hard to detect up-front if things will work. So just bail
395 * out if we have a kernel older than 6.1.
396 *
397 * See https://gitlab.gnome.org/GNOME/libdex/-/issues/17
398 */
399 if (!dex_uring_check_kernel_version (6, 1))
400 return NULL;
401
402 aio_backend = (DexAioBackend *)dex_object_create_instance (DEX_TYPE_URING_AIO_BACKEND);
403
404 /* Make sure we are capable of creating an aio_context */
405 if (!(aio_context = dex_aio_backend_create_context (aio_backend)))
406 {
407 dex_unref (aio_backend);
408 return NULL;
409 }
410
411 /* Save it for the first request */
412 ((DexUringAioBackend *)aio_backend)->first = g_steal_pointer (&aio_context);
413
414 return aio_backend;
415 }