xref: /openbmc/qemu/include/block/aio.h (revision 4749079ce033a94784cbe20a661abeac598ff057)
1737e150eSPaolo Bonzini /*
2737e150eSPaolo Bonzini  * QEMU aio implementation
3737e150eSPaolo Bonzini  *
4737e150eSPaolo Bonzini  * Copyright IBM, Corp. 2008
5737e150eSPaolo Bonzini  *
6737e150eSPaolo Bonzini  * Authors:
7737e150eSPaolo Bonzini  *  Anthony Liguori   <aliguori@us.ibm.com>
8737e150eSPaolo Bonzini  *
9737e150eSPaolo Bonzini  * This work is licensed under the terms of the GNU GPL, version 2.  See
10737e150eSPaolo Bonzini  * the COPYING file in the top-level directory.
11737e150eSPaolo Bonzini  *
12737e150eSPaolo Bonzini  */
13737e150eSPaolo Bonzini 
14737e150eSPaolo Bonzini #ifndef QEMU_AIO_H
15737e150eSPaolo Bonzini #define QEMU_AIO_H
16737e150eSPaolo Bonzini 
171de7afc9SPaolo Bonzini #include "qemu/queue.h"
181de7afc9SPaolo Bonzini #include "qemu/event_notifier.h"
19dcc772e2SLiu Ping Fan #include "qemu/thread.h"
20dae21b98SAlex Bligh #include "qemu/timer.h"
21737e150eSPaolo Bonzini 
227c84b1b8SMarkus Armbruster typedef struct BlockAIOCB BlockAIOCB;
23097310b5SMarkus Armbruster typedef void BlockCompletionFunc(void *opaque, int ret);
24737e150eSPaolo Bonzini 
25737e150eSPaolo Bonzini typedef struct AIOCBInfo {
267c84b1b8SMarkus Armbruster     void (*cancel_async)(BlockAIOCB *acb);
277c84b1b8SMarkus Armbruster     AioContext *(*get_aio_context)(BlockAIOCB *acb);
28737e150eSPaolo Bonzini     size_t aiocb_size;
29737e150eSPaolo Bonzini } AIOCBInfo;
30737e150eSPaolo Bonzini 
317c84b1b8SMarkus Armbruster struct BlockAIOCB {
32737e150eSPaolo Bonzini     const AIOCBInfo *aiocb_info;
33737e150eSPaolo Bonzini     BlockDriverState *bs;
34097310b5SMarkus Armbruster     BlockCompletionFunc *cb;
35737e150eSPaolo Bonzini     void *opaque;
36f197fe2bSFam Zheng     int refcnt;
37737e150eSPaolo Bonzini };
38737e150eSPaolo Bonzini 
39737e150eSPaolo Bonzini void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
40097310b5SMarkus Armbruster                    BlockCompletionFunc *cb, void *opaque);
418007429aSFam Zheng void qemu_aio_unref(void *p);
42f197fe2bSFam Zheng void qemu_aio_ref(void *p);
43737e150eSPaolo Bonzini 
44737e150eSPaolo Bonzini typedef struct AioHandler AioHandler;
45*4749079cSStefan Hajnoczi typedef QLIST_HEAD(, AioHandler) AioHandlerList;
46737e150eSPaolo Bonzini typedef void QEMUBHFunc(void *opaque);
47f6a51c84SStefan Hajnoczi typedef bool AioPollFn(void *opaque);
48737e150eSPaolo Bonzini typedef void IOHandler(void *opaque);
49737e150eSPaolo Bonzini 
500c330a73SPaolo Bonzini struct Coroutine;
510187f5c9SPaolo Bonzini struct ThreadPool;
520187f5c9SPaolo Bonzini struct LinuxAioState;
536663a0a3SAarushi Mehta struct LuringState;
540187f5c9SPaolo Bonzini 
558c6b0356SStefan Hajnoczi /*
568c6b0356SStefan Hajnoczi  * Each aio_bh_poll() call carves off a slice of the BH list, so that newly
578c6b0356SStefan Hajnoczi  * scheduled BHs are not processed until the next aio_bh_poll() call.  All
588c6b0356SStefan Hajnoczi  * active aio_bh_poll() calls chain their slices together in a list, so that
598c6b0356SStefan Hajnoczi  * nested aio_bh_poll() calls process all scheduled bottom halves.
608c6b0356SStefan Hajnoczi  */
618c6b0356SStefan Hajnoczi typedef QSLIST_HEAD(, QEMUBH) BHList;
628c6b0356SStefan Hajnoczi typedef struct BHListSlice BHListSlice;
638c6b0356SStefan Hajnoczi struct BHListSlice {
648c6b0356SStefan Hajnoczi     BHList bh_list;
658c6b0356SStefan Hajnoczi     QSIMPLEQ_ENTRY(BHListSlice) next;
668c6b0356SStefan Hajnoczi };
678c6b0356SStefan Hajnoczi 
686a1751b7SAlex Bligh struct AioContext {
69737e150eSPaolo Bonzini     GSource source;
70737e150eSPaolo Bonzini 
717c690fd1SPaolo Bonzini     /* Used by AioContext users to protect from multi-threaded access.  */
723fe71223SPaolo Bonzini     QemuRecMutex lock;
7398563fc3SStefan Hajnoczi 
747c690fd1SPaolo Bonzini     /* The list of registered AIO handlers.  Protected by ctx->list_lock. */
75*4749079cSStefan Hajnoczi     AioHandlerList aio_handlers;
76*4749079cSStefan Hajnoczi 
77*4749079cSStefan Hajnoczi     /* The list of AIO handlers to be deleted.  Protected by ctx->list_lock. */
78*4749079cSStefan Hajnoczi     AioHandlerList deleted_aio_handlers;
79737e150eSPaolo Bonzini 
80eabc9779SPaolo Bonzini     /* Used to avoid unnecessary event_notifier_set calls in aio_notify;
81eabc9779SPaolo Bonzini      * accessed with atomic primitives.  If this field is 0, everything
82eabc9779SPaolo Bonzini      * (file descriptors, bottom halves, timers) will be re-evaluated
83eabc9779SPaolo Bonzini      * before the next blocking poll(), thus the event_notifier_set call
84eabc9779SPaolo Bonzini      * can be skipped.  If it is non-zero, you may need to wake up a
85eabc9779SPaolo Bonzini      * concurrent aio_poll or the glib main event loop, making
86eabc9779SPaolo Bonzini      * event_notifier_set necessary.
87eabc9779SPaolo Bonzini      *
88eabc9779SPaolo Bonzini      * Bit 0 is reserved for GSource usage of the AioContext, and is 1
8954a16a63SCao jin      * between a call to aio_ctx_prepare and the next call to aio_ctx_check.
90eabc9779SPaolo Bonzini      * Bits 1-31 simply count the number of active calls to aio_poll
91eabc9779SPaolo Bonzini      * that are in the prepare or poll phase.
92eabc9779SPaolo Bonzini      *
93eabc9779SPaolo Bonzini      * The GSource and aio_poll must use a different mechanism because
94eabc9779SPaolo Bonzini      * there is no certainty that a call to GSource's prepare callback
95eabc9779SPaolo Bonzini      * (via g_main_context_prepare) is indeed followed by check and
96eabc9779SPaolo Bonzini      * dispatch.  It's not clear whether this would be a bug, but let's
97eabc9779SPaolo Bonzini      * play safe and allow it---it will just cause extra calls to
98eabc9779SPaolo Bonzini      * event_notifier_set until the next call to dispatch.
99eabc9779SPaolo Bonzini      *
100eabc9779SPaolo Bonzini      * Instead, the aio_poll calls include both the prepare and the
101eabc9779SPaolo Bonzini      * dispatch phase, hence a simple counter is enough for them.
1020ceb849bSPaolo Bonzini      */
103eabc9779SPaolo Bonzini     uint32_t notify_me;
1040ceb849bSPaolo Bonzini 
1057c690fd1SPaolo Bonzini     /* A lock to protect between QEMUBH and AioHandler adders and deleter,
1067c690fd1SPaolo Bonzini      * and to ensure that no callbacks are removed while we're walking and
1077c690fd1SPaolo Bonzini      * dispatching them.
108d7c99a12SPaolo Bonzini      */
109d7c99a12SPaolo Bonzini     QemuLockCnt list_lock;
1100ceb849bSPaolo Bonzini 
1118c6b0356SStefan Hajnoczi     /* Bottom Halves pending aio_bh_poll() processing */
1128c6b0356SStefan Hajnoczi     BHList bh_list;
1138c6b0356SStefan Hajnoczi 
1148c6b0356SStefan Hajnoczi     /* Chained BH list slices for each nested aio_bh_poll() call */
1158c6b0356SStefan Hajnoczi     QSIMPLEQ_HEAD(, BHListSlice) bh_slice_list;
116737e150eSPaolo Bonzini 
11705e514b1SPaolo Bonzini     /* Used by aio_notify.
11805e514b1SPaolo Bonzini      *
11905e514b1SPaolo Bonzini      * "notified" is used to avoid expensive event_notifier_test_and_clear
12005e514b1SPaolo Bonzini      * calls.  When it is clear, the EventNotifier is clear, or one thread
12105e514b1SPaolo Bonzini      * is going to clear "notified" before processing more events.  False
12205e514b1SPaolo Bonzini      * positives are possible, i.e. "notified" could be set even though the
12305e514b1SPaolo Bonzini      * EventNotifier is clear.
12405e514b1SPaolo Bonzini      *
12505e514b1SPaolo Bonzini      * Note that event_notifier_set *cannot* be optimized the same way.  For
12605e514b1SPaolo Bonzini      * more information on the problem that would result, see "#ifdef BUG2"
12705e514b1SPaolo Bonzini      * in the docs/aio_notify_accept.promela formal model.
12805e514b1SPaolo Bonzini      */
12905e514b1SPaolo Bonzini     bool notified;
130737e150eSPaolo Bonzini     EventNotifier notifier;
1316b5f8762SStefan Hajnoczi 
1320c330a73SPaolo Bonzini     QSLIST_HEAD(, Coroutine) scheduled_coroutines;
1330c330a73SPaolo Bonzini     QEMUBH *co_schedule_bh;
1340c330a73SPaolo Bonzini 
1357c690fd1SPaolo Bonzini     /* Thread pool for performing work and receiving completion callbacks.
1367c690fd1SPaolo Bonzini      * Has its own locking.
1377c690fd1SPaolo Bonzini      */
1389b34277dSStefan Hajnoczi     struct ThreadPool *thread_pool;
139dae21b98SAlex Bligh 
1400187f5c9SPaolo Bonzini #ifdef CONFIG_LINUX_AIO
1416663a0a3SAarushi Mehta     /*
1426663a0a3SAarushi Mehta      * State for native Linux AIO.  Uses aio_context_acquire/release for
1430187f5c9SPaolo Bonzini      * locking.
1440187f5c9SPaolo Bonzini      */
1450187f5c9SPaolo Bonzini     struct LinuxAioState *linux_aio;
1460187f5c9SPaolo Bonzini #endif
1476663a0a3SAarushi Mehta #ifdef CONFIG_LINUX_IO_URING
1486663a0a3SAarushi Mehta     /*
1496663a0a3SAarushi Mehta      * State for Linux io_uring.  Uses aio_context_acquire/release for
1506663a0a3SAarushi Mehta      * locking.
1516663a0a3SAarushi Mehta      */
1526663a0a3SAarushi Mehta     struct LuringState *linux_io_uring;
1536663a0a3SAarushi Mehta #endif
1540187f5c9SPaolo Bonzini 
1557c690fd1SPaolo Bonzini     /* TimerLists for calling timers - one per clock type.  Has its own
1567c690fd1SPaolo Bonzini      * locking.
1577c690fd1SPaolo Bonzini      */
158dae21b98SAlex Bligh     QEMUTimerListGroup tlg;
159c1e1e5faSFam Zheng 
160c1e1e5faSFam Zheng     int external_disable_cnt;
161fbe3fc5cSFam Zheng 
1624a1cba38SStefan Hajnoczi     /* Number of AioHandlers without .io_poll() */
1634a1cba38SStefan Hajnoczi     int poll_disable_cnt;
1644a1cba38SStefan Hajnoczi 
16582a41186SStefan Hajnoczi     /* Polling mode parameters */
16682a41186SStefan Hajnoczi     int64_t poll_ns;        /* current polling time in nanoseconds */
16782a41186SStefan Hajnoczi     int64_t poll_max_ns;    /* maximum polling time in nanoseconds */
16882a41186SStefan Hajnoczi     int64_t poll_grow;      /* polling time growth factor */
16982a41186SStefan Hajnoczi     int64_t poll_shrink;    /* polling time shrink factor */
1704a1cba38SStefan Hajnoczi 
171684e508cSStefan Hajnoczi     /* Are we in polling mode or monitoring file descriptors? */
172684e508cSStefan Hajnoczi     bool poll_started;
173684e508cSStefan Hajnoczi 
174fbe3fc5cSFam Zheng     /* epoll(7) state used when built with CONFIG_EPOLL */
175fbe3fc5cSFam Zheng     int epollfd;
176fbe3fc5cSFam Zheng     bool epoll_enabled;
177fbe3fc5cSFam Zheng     bool epoll_available;
1786a1751b7SAlex Bligh };
179737e150eSPaolo Bonzini 
180737e150eSPaolo Bonzini /**
181737e150eSPaolo Bonzini  * aio_context_new: Allocate a new AioContext.
182737e150eSPaolo Bonzini  *
183737e150eSPaolo Bonzini  * AioContext provide a mini event-loop that can be waited on synchronously.
184737e150eSPaolo Bonzini  * They also provide bottom halves, a service to execute a piece of code
185737e150eSPaolo Bonzini  * as soon as possible.
186737e150eSPaolo Bonzini  */
1872f78e491SChrysostomos Nanakos AioContext *aio_context_new(Error **errp);
188737e150eSPaolo Bonzini 
189737e150eSPaolo Bonzini /**
190737e150eSPaolo Bonzini  * aio_context_ref:
191737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
192737e150eSPaolo Bonzini  *
193737e150eSPaolo Bonzini  * Add a reference to an AioContext.
194737e150eSPaolo Bonzini  */
195737e150eSPaolo Bonzini void aio_context_ref(AioContext *ctx);
196737e150eSPaolo Bonzini 
197737e150eSPaolo Bonzini /**
198737e150eSPaolo Bonzini  * aio_context_unref:
199737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
200737e150eSPaolo Bonzini  *
201737e150eSPaolo Bonzini  * Drop a reference to an AioContext.
202737e150eSPaolo Bonzini  */
203737e150eSPaolo Bonzini void aio_context_unref(AioContext *ctx);
204737e150eSPaolo Bonzini 
20598563fc3SStefan Hajnoczi /* Take ownership of the AioContext.  If the AioContext will be shared between
20649110174SPaolo Bonzini  * threads, and a thread does not want to be interrupted, it will have to
20749110174SPaolo Bonzini  * take ownership around calls to aio_poll().  Otherwise, aio_poll()
20849110174SPaolo Bonzini  * automatically takes care of calling aio_context_acquire and
20949110174SPaolo Bonzini  * aio_context_release.
21098563fc3SStefan Hajnoczi  *
2117c690fd1SPaolo Bonzini  * Note that this is separate from bdrv_drained_begin/bdrv_drained_end.  A
2127c690fd1SPaolo Bonzini  * thread still has to call those to avoid being interrupted by the guest.
2137c690fd1SPaolo Bonzini  *
2147c690fd1SPaolo Bonzini  * Bottom halves, timers and callbacks can be created or removed without
2157c690fd1SPaolo Bonzini  * acquiring the AioContext.
21698563fc3SStefan Hajnoczi  */
21798563fc3SStefan Hajnoczi void aio_context_acquire(AioContext *ctx);
21898563fc3SStefan Hajnoczi 
21998563fc3SStefan Hajnoczi /* Relinquish ownership of the AioContext. */
22098563fc3SStefan Hajnoczi void aio_context_release(AioContext *ctx);
22198563fc3SStefan Hajnoczi 
222737e150eSPaolo Bonzini /**
2235b8bb359SPaolo Bonzini  * aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run
2245b8bb359SPaolo Bonzini  * only once and as soon as possible.
2255b8bb359SPaolo Bonzini  */
2265b8bb359SPaolo Bonzini void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
2275b8bb359SPaolo Bonzini 
2285b8bb359SPaolo Bonzini /**
229737e150eSPaolo Bonzini  * aio_bh_new: Allocate a new bottom half structure.
230737e150eSPaolo Bonzini  *
231737e150eSPaolo Bonzini  * Bottom halves are lightweight callbacks whose invocation is guaranteed
232737e150eSPaolo Bonzini  * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
233737e150eSPaolo Bonzini  * is opaque and must be allocated prior to its use.
234737e150eSPaolo Bonzini  */
235737e150eSPaolo Bonzini QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
236737e150eSPaolo Bonzini 
237737e150eSPaolo Bonzini /**
238737e150eSPaolo Bonzini  * aio_notify: Force processing of pending events.
239737e150eSPaolo Bonzini  *
240737e150eSPaolo Bonzini  * Similar to signaling a condition variable, aio_notify forces
241722f8d90SYaowei Bai  * aio_poll to exit, so that the next call will re-examine pending events.
242722f8d90SYaowei Bai  * The caller of aio_notify will usually call aio_poll again very soon,
243737e150eSPaolo Bonzini  * or go through another iteration of the GLib main loop.  Hence, aio_notify
244737e150eSPaolo Bonzini  * also has the side effect of recalculating the sets of file descriptors
245737e150eSPaolo Bonzini  * that the main loop waits for.
246737e150eSPaolo Bonzini  *
247737e150eSPaolo Bonzini  * Calling aio_notify is rarely necessary, because for example scheduling
248737e150eSPaolo Bonzini  * a bottom half calls it already.
249737e150eSPaolo Bonzini  */
250737e150eSPaolo Bonzini void aio_notify(AioContext *ctx);
251737e150eSPaolo Bonzini 
252737e150eSPaolo Bonzini /**
25305e514b1SPaolo Bonzini  * aio_notify_accept: Acknowledge receiving an aio_notify.
25405e514b1SPaolo Bonzini  *
25505e514b1SPaolo Bonzini  * aio_notify() uses an EventNotifier in order to wake up a sleeping
25605e514b1SPaolo Bonzini  * aio_poll() or g_main_context_iteration().  Calls to aio_notify() are
25705e514b1SPaolo Bonzini  * usually rare, but the AioContext has to clear the EventNotifier on
25805e514b1SPaolo Bonzini  * every aio_poll() or g_main_context_iteration() in order to avoid
25905e514b1SPaolo Bonzini  * busy waiting.  This event_notifier_test_and_clear() cannot be done
26005e514b1SPaolo Bonzini  * using the usual aio_context_set_event_notifier(), because it must
26105e514b1SPaolo Bonzini  * be done before processing all events (file descriptors, bottom halves,
26205e514b1SPaolo Bonzini  * timers).
26305e514b1SPaolo Bonzini  *
26405e514b1SPaolo Bonzini  * aio_notify_accept() is an optimized event_notifier_test_and_clear()
26505e514b1SPaolo Bonzini  * that is specific to an AioContext's notifier; it is used internally
26605e514b1SPaolo Bonzini  * to clear the EventNotifier only if aio_notify() had been called.
26705e514b1SPaolo Bonzini  */
26805e514b1SPaolo Bonzini void aio_notify_accept(AioContext *ctx);
26905e514b1SPaolo Bonzini 
27005e514b1SPaolo Bonzini /**
271df281b80SPavel Dovgalyuk  * aio_bh_call: Executes callback function of the specified BH.
272df281b80SPavel Dovgalyuk  */
273df281b80SPavel Dovgalyuk void aio_bh_call(QEMUBH *bh);
274df281b80SPavel Dovgalyuk 
275df281b80SPavel Dovgalyuk /**
276737e150eSPaolo Bonzini  * aio_bh_poll: Poll bottom halves for an AioContext.
277737e150eSPaolo Bonzini  *
278737e150eSPaolo Bonzini  * These are internal functions used by the QEMU main loop.
279dcc772e2SLiu Ping Fan  * And notice that multiple occurrences of aio_bh_poll cannot
280dcc772e2SLiu Ping Fan  * be called concurrently
281737e150eSPaolo Bonzini  */
282737e150eSPaolo Bonzini int aio_bh_poll(AioContext *ctx);
283737e150eSPaolo Bonzini 
284737e150eSPaolo Bonzini /**
285737e150eSPaolo Bonzini  * qemu_bh_schedule: Schedule a bottom half.
286737e150eSPaolo Bonzini  *
287737e150eSPaolo Bonzini  * Scheduling a bottom half interrupts the main loop and causes the
288737e150eSPaolo Bonzini  * execution of the callback that was passed to qemu_bh_new.
289737e150eSPaolo Bonzini  *
290737e150eSPaolo Bonzini  * Bottom halves that are scheduled from a bottom half handler are instantly
291737e150eSPaolo Bonzini  * invoked.  This can create an infinite loop if a bottom half handler
292737e150eSPaolo Bonzini  * schedules itself.
293737e150eSPaolo Bonzini  *
294737e150eSPaolo Bonzini  * @bh: The bottom half to be scheduled.
295737e150eSPaolo Bonzini  */
296737e150eSPaolo Bonzini void qemu_bh_schedule(QEMUBH *bh);
297737e150eSPaolo Bonzini 
298737e150eSPaolo Bonzini /**
299737e150eSPaolo Bonzini  * qemu_bh_cancel: Cancel execution of a bottom half.
300737e150eSPaolo Bonzini  *
301737e150eSPaolo Bonzini  * Canceling execution of a bottom half undoes the effect of calls to
302737e150eSPaolo Bonzini  * qemu_bh_schedule without freeing its resources yet.  While cancellation
303737e150eSPaolo Bonzini  * itself is also wait-free and thread-safe, it can of course race with the
304737e150eSPaolo Bonzini  * loop that executes bottom halves unless you are holding the iothread
305737e150eSPaolo Bonzini  * mutex.  This makes it mostly useless if you are not holding the mutex.
306737e150eSPaolo Bonzini  *
307737e150eSPaolo Bonzini  * @bh: The bottom half to be canceled.
308737e150eSPaolo Bonzini  */
309737e150eSPaolo Bonzini void qemu_bh_cancel(QEMUBH *bh);
310737e150eSPaolo Bonzini 
311737e150eSPaolo Bonzini /**
312737e150eSPaolo Bonzini  *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
313737e150eSPaolo Bonzini  *
314737e150eSPaolo Bonzini  * Deleting a bottom half frees the memory that was allocated for it by
315737e150eSPaolo Bonzini  * qemu_bh_new.  It also implies canceling the bottom half if it was
316737e150eSPaolo Bonzini  * scheduled.
317dcc772e2SLiu Ping Fan  * This func is async. The bottom half will do the delete action at the finial
318dcc772e2SLiu Ping Fan  * end.
319737e150eSPaolo Bonzini  *
320737e150eSPaolo Bonzini  * @bh: The bottom half to be deleted.
321737e150eSPaolo Bonzini  */
322737e150eSPaolo Bonzini void qemu_bh_delete(QEMUBH *bh);
323737e150eSPaolo Bonzini 
324737e150eSPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
325a3462c65SPaolo Bonzini  * attached to the AioContext, before g_poll is invoked.
326a3462c65SPaolo Bonzini  *
327a3462c65SPaolo Bonzini  * This is used internally in the implementation of the GSource.
328a3462c65SPaolo Bonzini  */
329a3462c65SPaolo Bonzini bool aio_prepare(AioContext *ctx);
330a3462c65SPaolo Bonzini 
331a3462c65SPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
332a3462c65SPaolo Bonzini  * attached to the AioContext, after g_poll is invoked.
333737e150eSPaolo Bonzini  *
334737e150eSPaolo Bonzini  * This is used internally in the implementation of the GSource.
335737e150eSPaolo Bonzini  */
336737e150eSPaolo Bonzini bool aio_pending(AioContext *ctx);
337737e150eSPaolo Bonzini 
338e4c7e2d1SPaolo Bonzini /* Dispatch any pending callbacks from the GSource attached to the AioContext.
339e4c7e2d1SPaolo Bonzini  *
340e4c7e2d1SPaolo Bonzini  * This is used internally in the implementation of the GSource.
341e4c7e2d1SPaolo Bonzini  */
342a153bf52SPaolo Bonzini void aio_dispatch(AioContext *ctx);
343e4c7e2d1SPaolo Bonzini 
344737e150eSPaolo Bonzini /* Progress in completing AIO work to occur.  This can issue new pending
345737e150eSPaolo Bonzini  * aio as a result of executing I/O completion or bh callbacks.
346737e150eSPaolo Bonzini  *
347acfb23adSPaolo Bonzini  * Return whether any progress was made by executing AIO or bottom half
348acfb23adSPaolo Bonzini  * handlers.  If @blocking == true, this should always be true except
349acfb23adSPaolo Bonzini  * if someone called aio_notify.
350737e150eSPaolo Bonzini  *
351737e150eSPaolo Bonzini  * If there are no pending bottom halves, but there are pending AIO
352737e150eSPaolo Bonzini  * operations, it may not be possible to make any progress without
353737e150eSPaolo Bonzini  * blocking.  If @blocking is true, this function will wait until one
354737e150eSPaolo Bonzini  * or more AIO events have completed, to ensure something has moved
355737e150eSPaolo Bonzini  * before returning.
356737e150eSPaolo Bonzini  */
357737e150eSPaolo Bonzini bool aio_poll(AioContext *ctx, bool blocking);
358737e150eSPaolo Bonzini 
359737e150eSPaolo Bonzini /* Register a file descriptor and associated callbacks.  Behaves very similarly
3606484e422SFam Zheng  * to qemu_set_fd_handler.  Unlike qemu_set_fd_handler, these callbacks will
36187f68d31SPaolo Bonzini  * be invoked when using aio_poll().
362737e150eSPaolo Bonzini  *
363737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
364737e150eSPaolo Bonzini  * instead of qemu_set_fd_handler[2].
365737e150eSPaolo Bonzini  */
366737e150eSPaolo Bonzini void aio_set_fd_handler(AioContext *ctx,
367737e150eSPaolo Bonzini                         int fd,
368dca21ef2SFam Zheng                         bool is_external,
369737e150eSPaolo Bonzini                         IOHandler *io_read,
370737e150eSPaolo Bonzini                         IOHandler *io_write,
371f6a51c84SStefan Hajnoczi                         AioPollFn *io_poll,
372737e150eSPaolo Bonzini                         void *opaque);
373737e150eSPaolo Bonzini 
374684e508cSStefan Hajnoczi /* Set polling begin/end callbacks for a file descriptor that has already been
375684e508cSStefan Hajnoczi  * registered with aio_set_fd_handler.  Do nothing if the file descriptor is
376684e508cSStefan Hajnoczi  * not registered.
377684e508cSStefan Hajnoczi  */
378684e508cSStefan Hajnoczi void aio_set_fd_poll(AioContext *ctx, int fd,
379684e508cSStefan Hajnoczi                      IOHandler *io_poll_begin,
380684e508cSStefan Hajnoczi                      IOHandler *io_poll_end);
381684e508cSStefan Hajnoczi 
382737e150eSPaolo Bonzini /* Register an event notifier and associated callbacks.  Behaves very similarly
383737e150eSPaolo Bonzini  * to event_notifier_set_handler.  Unlike event_notifier_set_handler, these callbacks
38487f68d31SPaolo Bonzini  * will be invoked when using aio_poll().
385737e150eSPaolo Bonzini  *
386737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
387737e150eSPaolo Bonzini  * instead of event_notifier_set_handler.
388737e150eSPaolo Bonzini  */
389737e150eSPaolo Bonzini void aio_set_event_notifier(AioContext *ctx,
390737e150eSPaolo Bonzini                             EventNotifier *notifier,
391dca21ef2SFam Zheng                             bool is_external,
392f6a51c84SStefan Hajnoczi                             EventNotifierHandler *io_read,
393f6a51c84SStefan Hajnoczi                             AioPollFn *io_poll);
394737e150eSPaolo Bonzini 
395684e508cSStefan Hajnoczi /* Set polling begin/end callbacks for an event notifier that has already been
396684e508cSStefan Hajnoczi  * registered with aio_set_event_notifier.  Do nothing if the event notifier is
397684e508cSStefan Hajnoczi  * not registered.
398684e508cSStefan Hajnoczi  */
399684e508cSStefan Hajnoczi void aio_set_event_notifier_poll(AioContext *ctx,
400684e508cSStefan Hajnoczi                                  EventNotifier *notifier,
401684e508cSStefan Hajnoczi                                  EventNotifierHandler *io_poll_begin,
402684e508cSStefan Hajnoczi                                  EventNotifierHandler *io_poll_end);
403684e508cSStefan Hajnoczi 
404737e150eSPaolo Bonzini /* Return a GSource that lets the main loop poll the file descriptors attached
405737e150eSPaolo Bonzini  * to this AioContext.
406737e150eSPaolo Bonzini  */
407737e150eSPaolo Bonzini GSource *aio_get_g_source(AioContext *ctx);
408737e150eSPaolo Bonzini 
4099b34277dSStefan Hajnoczi /* Return the ThreadPool bound to this AioContext */
4109b34277dSStefan Hajnoczi struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
4119b34277dSStefan Hajnoczi 
412ed6e2161SNishanth Aravamudan /* Setup the LinuxAioState bound to this AioContext */
413ed6e2161SNishanth Aravamudan struct LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp);
414ed6e2161SNishanth Aravamudan 
4150187f5c9SPaolo Bonzini /* Return the LinuxAioState bound to this AioContext */
4160187f5c9SPaolo Bonzini struct LinuxAioState *aio_get_linux_aio(AioContext *ctx);
4170187f5c9SPaolo Bonzini 
4186663a0a3SAarushi Mehta /* Setup the LuringState bound to this AioContext */
4196663a0a3SAarushi Mehta struct LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp);
4206663a0a3SAarushi Mehta 
4216663a0a3SAarushi Mehta /* Return the LuringState bound to this AioContext */
4226663a0a3SAarushi Mehta struct LuringState *aio_get_linux_io_uring(AioContext *ctx);
4234e29e831SAlex Bligh /**
42489a603a0SArtem Pisarenko  * aio_timer_new_with_attrs:
42589a603a0SArtem Pisarenko  * @ctx: the aio context
42689a603a0SArtem Pisarenko  * @type: the clock type
42789a603a0SArtem Pisarenko  * @scale: the scale
42889a603a0SArtem Pisarenko  * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values
42989a603a0SArtem Pisarenko  *              to assign
43089a603a0SArtem Pisarenko  * @cb: the callback to call on timer expiry
43189a603a0SArtem Pisarenko  * @opaque: the opaque pointer to pass to the callback
43289a603a0SArtem Pisarenko  *
43389a603a0SArtem Pisarenko  * Allocate a new timer (with attributes) attached to the context @ctx.
43489a603a0SArtem Pisarenko  * The function is responsible for memory allocation.
43589a603a0SArtem Pisarenko  *
43689a603a0SArtem Pisarenko  * The preferred interface is aio_timer_init or aio_timer_init_with_attrs.
43789a603a0SArtem Pisarenko  * Use that unless you really need dynamic memory allocation.
43889a603a0SArtem Pisarenko  *
43989a603a0SArtem Pisarenko  * Returns: a pointer to the new timer
44089a603a0SArtem Pisarenko  */
44189a603a0SArtem Pisarenko static inline QEMUTimer *aio_timer_new_with_attrs(AioContext *ctx,
44289a603a0SArtem Pisarenko                                                   QEMUClockType type,
44389a603a0SArtem Pisarenko                                                   int scale, int attributes,
44489a603a0SArtem Pisarenko                                                   QEMUTimerCB *cb, void *opaque)
44589a603a0SArtem Pisarenko {
44689a603a0SArtem Pisarenko     return timer_new_full(&ctx->tlg, type, scale, attributes, cb, opaque);
44789a603a0SArtem Pisarenko }
44889a603a0SArtem Pisarenko 
44989a603a0SArtem Pisarenko /**
4504e29e831SAlex Bligh  * aio_timer_new:
4514e29e831SAlex Bligh  * @ctx: the aio context
4524e29e831SAlex Bligh  * @type: the clock type
4534e29e831SAlex Bligh  * @scale: the scale
4544e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
4554e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
4564e29e831SAlex Bligh  *
4574e29e831SAlex Bligh  * Allocate a new timer attached to the context @ctx.
45889a603a0SArtem Pisarenko  * See aio_timer_new_with_attrs for details.
4594e29e831SAlex Bligh  *
4604e29e831SAlex Bligh  * Returns: a pointer to the new timer
4614e29e831SAlex Bligh  */
4624e29e831SAlex Bligh static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
4634e29e831SAlex Bligh                                        int scale,
4644e29e831SAlex Bligh                                        QEMUTimerCB *cb, void *opaque)
4654e29e831SAlex Bligh {
46689a603a0SArtem Pisarenko     return timer_new_full(&ctx->tlg, type, scale, 0, cb, opaque);
46789a603a0SArtem Pisarenko }
46889a603a0SArtem Pisarenko 
46989a603a0SArtem Pisarenko /**
47089a603a0SArtem Pisarenko  * aio_timer_init_with_attrs:
47189a603a0SArtem Pisarenko  * @ctx: the aio context
47289a603a0SArtem Pisarenko  * @ts: the timer
47389a603a0SArtem Pisarenko  * @type: the clock type
47489a603a0SArtem Pisarenko  * @scale: the scale
47589a603a0SArtem Pisarenko  * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values
47689a603a0SArtem Pisarenko  *              to assign
47789a603a0SArtem Pisarenko  * @cb: the callback to call on timer expiry
47889a603a0SArtem Pisarenko  * @opaque: the opaque pointer to pass to the callback
47989a603a0SArtem Pisarenko  *
48089a603a0SArtem Pisarenko  * Initialise a new timer (with attributes) attached to the context @ctx.
48189a603a0SArtem Pisarenko  * The caller is responsible for memory allocation.
48289a603a0SArtem Pisarenko  */
48389a603a0SArtem Pisarenko static inline void aio_timer_init_with_attrs(AioContext *ctx,
48489a603a0SArtem Pisarenko                                              QEMUTimer *ts, QEMUClockType type,
48589a603a0SArtem Pisarenko                                              int scale, int attributes,
48689a603a0SArtem Pisarenko                                              QEMUTimerCB *cb, void *opaque)
48789a603a0SArtem Pisarenko {
48889a603a0SArtem Pisarenko     timer_init_full(ts, &ctx->tlg, type, scale, attributes, cb, opaque);
4894e29e831SAlex Bligh }
4904e29e831SAlex Bligh 
4914e29e831SAlex Bligh /**
4924e29e831SAlex Bligh  * aio_timer_init:
4934e29e831SAlex Bligh  * @ctx: the aio context
4944e29e831SAlex Bligh  * @ts: the timer
4954e29e831SAlex Bligh  * @type: the clock type
4964e29e831SAlex Bligh  * @scale: the scale
4974e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
4984e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
4994e29e831SAlex Bligh  *
5004e29e831SAlex Bligh  * Initialise a new timer attached to the context @ctx.
50189a603a0SArtem Pisarenko  * See aio_timer_init_with_attrs for details.
5024e29e831SAlex Bligh  */
5034e29e831SAlex Bligh static inline void aio_timer_init(AioContext *ctx,
5044e29e831SAlex Bligh                                   QEMUTimer *ts, QEMUClockType type,
5054e29e831SAlex Bligh                                   int scale,
5064e29e831SAlex Bligh                                   QEMUTimerCB *cb, void *opaque)
5074e29e831SAlex Bligh {
50889a603a0SArtem Pisarenko     timer_init_full(ts, &ctx->tlg, type, scale, 0, cb, opaque);
5094e29e831SAlex Bligh }
5104e29e831SAlex Bligh 
511845ca10dSPaolo Bonzini /**
512845ca10dSPaolo Bonzini  * aio_compute_timeout:
513845ca10dSPaolo Bonzini  * @ctx: the aio context
514845ca10dSPaolo Bonzini  *
515845ca10dSPaolo Bonzini  * Compute the timeout that a blocking aio_poll should use.
516845ca10dSPaolo Bonzini  */
517845ca10dSPaolo Bonzini int64_t aio_compute_timeout(AioContext *ctx);
518845ca10dSPaolo Bonzini 
519c1e1e5faSFam Zheng /**
520c1e1e5faSFam Zheng  * aio_disable_external:
521c1e1e5faSFam Zheng  * @ctx: the aio context
522c1e1e5faSFam Zheng  *
523c1e1e5faSFam Zheng  * Disable the further processing of external clients.
524c1e1e5faSFam Zheng  */
525c1e1e5faSFam Zheng static inline void aio_disable_external(AioContext *ctx)
526c1e1e5faSFam Zheng {
527c1e1e5faSFam Zheng     atomic_inc(&ctx->external_disable_cnt);
528c1e1e5faSFam Zheng }
529c1e1e5faSFam Zheng 
530c1e1e5faSFam Zheng /**
531c1e1e5faSFam Zheng  * aio_enable_external:
532c1e1e5faSFam Zheng  * @ctx: the aio context
533c1e1e5faSFam Zheng  *
534c1e1e5faSFam Zheng  * Enable the processing of external clients.
535c1e1e5faSFam Zheng  */
536c1e1e5faSFam Zheng static inline void aio_enable_external(AioContext *ctx)
537c1e1e5faSFam Zheng {
538321d1dbaSStefan Hajnoczi     int old;
539321d1dbaSStefan Hajnoczi 
540321d1dbaSStefan Hajnoczi     old = atomic_fetch_dec(&ctx->external_disable_cnt);
541321d1dbaSStefan Hajnoczi     assert(old > 0);
542321d1dbaSStefan Hajnoczi     if (old == 1) {
543321d1dbaSStefan Hajnoczi         /* Kick event loop so it re-arms file descriptors */
544321d1dbaSStefan Hajnoczi         aio_notify(ctx);
545321d1dbaSStefan Hajnoczi     }
546c1e1e5faSFam Zheng }
547c1e1e5faSFam Zheng 
548c1e1e5faSFam Zheng /**
5495ceb9e39SFam Zheng  * aio_external_disabled:
5505ceb9e39SFam Zheng  * @ctx: the aio context
5515ceb9e39SFam Zheng  *
5525ceb9e39SFam Zheng  * Return true if the external clients are disabled.
5535ceb9e39SFam Zheng  */
5545ceb9e39SFam Zheng static inline bool aio_external_disabled(AioContext *ctx)
5555ceb9e39SFam Zheng {
5565ceb9e39SFam Zheng     return atomic_read(&ctx->external_disable_cnt);
5575ceb9e39SFam Zheng }
5585ceb9e39SFam Zheng 
5595ceb9e39SFam Zheng /**
560c1e1e5faSFam Zheng  * aio_node_check:
561c1e1e5faSFam Zheng  * @ctx: the aio context
562c1e1e5faSFam Zheng  * @is_external: Whether or not the checked node is an external event source.
563c1e1e5faSFam Zheng  *
564c1e1e5faSFam Zheng  * Check if the node's is_external flag is okay to be polled by the ctx at this
565c1e1e5faSFam Zheng  * moment. True means green light.
566c1e1e5faSFam Zheng  */
567c1e1e5faSFam Zheng static inline bool aio_node_check(AioContext *ctx, bool is_external)
568c1e1e5faSFam Zheng {
569c1e1e5faSFam Zheng     return !is_external || !atomic_read(&ctx->external_disable_cnt);
570c1e1e5faSFam Zheng }
571c1e1e5faSFam Zheng 
57237fcee5dSFam Zheng /**
5730c330a73SPaolo Bonzini  * aio_co_schedule:
5740c330a73SPaolo Bonzini  * @ctx: the aio context
5750c330a73SPaolo Bonzini  * @co: the coroutine
5760c330a73SPaolo Bonzini  *
5770c330a73SPaolo Bonzini  * Start a coroutine on a remote AioContext.
5780c330a73SPaolo Bonzini  *
5790c330a73SPaolo Bonzini  * The coroutine must not be entered by anyone else while aio_co_schedule()
5800c330a73SPaolo Bonzini  * is active.  In addition the coroutine must have yielded unless ctx
5810c330a73SPaolo Bonzini  * is the context in which the coroutine is running (i.e. the value of
5820c330a73SPaolo Bonzini  * qemu_get_current_aio_context() from the coroutine itself).
5830c330a73SPaolo Bonzini  */
5840c330a73SPaolo Bonzini void aio_co_schedule(AioContext *ctx, struct Coroutine *co);
5850c330a73SPaolo Bonzini 
5860c330a73SPaolo Bonzini /**
5870c330a73SPaolo Bonzini  * aio_co_wake:
5880c330a73SPaolo Bonzini  * @co: the coroutine
5890c330a73SPaolo Bonzini  *
5900c330a73SPaolo Bonzini  * Restart a coroutine on the AioContext where it was running last, thus
5910c330a73SPaolo Bonzini  * preventing coroutines from jumping from one context to another when they
5920c330a73SPaolo Bonzini  * go to sleep.
5930c330a73SPaolo Bonzini  *
5940c330a73SPaolo Bonzini  * aio_co_wake may be executed either in coroutine or non-coroutine
5950c330a73SPaolo Bonzini  * context.  The coroutine must not be entered by anyone else while
5960c330a73SPaolo Bonzini  * aio_co_wake() is active.
5970c330a73SPaolo Bonzini  */
5980c330a73SPaolo Bonzini void aio_co_wake(struct Coroutine *co);
5990c330a73SPaolo Bonzini 
6000c330a73SPaolo Bonzini /**
6018865852eSFam Zheng  * aio_co_enter:
6028865852eSFam Zheng  * @ctx: the context to run the coroutine
6038865852eSFam Zheng  * @co: the coroutine to run
6048865852eSFam Zheng  *
6058865852eSFam Zheng  * Enter a coroutine in the specified AioContext.
6068865852eSFam Zheng  */
6078865852eSFam Zheng void aio_co_enter(AioContext *ctx, struct Coroutine *co);
6088865852eSFam Zheng 
6098865852eSFam Zheng /**
610e4370165SPaolo Bonzini  * Return the AioContext whose event loop runs in the current thread.
611e4370165SPaolo Bonzini  *
612e4370165SPaolo Bonzini  * If called from an IOThread this will be the IOThread's AioContext.  If
613e4370165SPaolo Bonzini  * called from another thread it will be the main loop AioContext.
614e4370165SPaolo Bonzini  */
615e4370165SPaolo Bonzini AioContext *qemu_get_current_aio_context(void);
616e4370165SPaolo Bonzini 
617e4370165SPaolo Bonzini /**
618d2b63ba8SStefan Hajnoczi  * in_aio_context_home_thread:
619e4370165SPaolo Bonzini  * @ctx: the aio context
620e4370165SPaolo Bonzini  *
621d2b63ba8SStefan Hajnoczi  * Return whether we are running in the thread that normally runs @ctx.  Note
622d2b63ba8SStefan Hajnoczi  * that acquiring/releasing ctx does not affect the outcome, each AioContext
623d2b63ba8SStefan Hajnoczi  * still only has one home thread that is responsible for running it.
624e4370165SPaolo Bonzini  */
625d2b63ba8SStefan Hajnoczi static inline bool in_aio_context_home_thread(AioContext *ctx)
626e4370165SPaolo Bonzini {
627e4370165SPaolo Bonzini     return ctx == qemu_get_current_aio_context();
628e4370165SPaolo Bonzini }
629e4370165SPaolo Bonzini 
630e4370165SPaolo Bonzini /**
63137fcee5dSFam Zheng  * aio_context_setup:
63237fcee5dSFam Zheng  * @ctx: the aio context
63337fcee5dSFam Zheng  *
63437fcee5dSFam Zheng  * Initialize the aio context.
63537fcee5dSFam Zheng  */
6367e003465SCao jin void aio_context_setup(AioContext *ctx);
63737fcee5dSFam Zheng 
6384a1cba38SStefan Hajnoczi /**
639cd0a6d2bSJie Wang  * aio_context_destroy:
640cd0a6d2bSJie Wang  * @ctx: the aio context
641cd0a6d2bSJie Wang  *
642cd0a6d2bSJie Wang  * Destroy the aio context.
643cd0a6d2bSJie Wang  */
644cd0a6d2bSJie Wang void aio_context_destroy(AioContext *ctx);
645cd0a6d2bSJie Wang 
646cd0a6d2bSJie Wang /**
6474a1cba38SStefan Hajnoczi  * aio_context_set_poll_params:
6484a1cba38SStefan Hajnoczi  * @ctx: the aio context
6494a1cba38SStefan Hajnoczi  * @max_ns: how long to busy poll for, in nanoseconds
65082a41186SStefan Hajnoczi  * @grow: polling time growth factor
65182a41186SStefan Hajnoczi  * @shrink: polling time shrink factor
6524a1cba38SStefan Hajnoczi  *
6534a1cba38SStefan Hajnoczi  * Poll mode can be disabled by setting poll_max_ns to 0.
6544a1cba38SStefan Hajnoczi  */
6554a1cba38SStefan Hajnoczi void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
65682a41186SStefan Hajnoczi                                  int64_t grow, int64_t shrink,
6574a1cba38SStefan Hajnoczi                                  Error **errp);
6584a1cba38SStefan Hajnoczi 
659737e150eSPaolo Bonzini #endif
660