xref: /openbmc/qemu/include/block/aio.h (revision e4c7e2d12d7b1c4501ab3397218a206d4953e633)
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 
176a1751b7SAlex Bligh #include "qemu/typedefs.h"
18737e150eSPaolo Bonzini #include "qemu-common.h"
191de7afc9SPaolo Bonzini #include "qemu/queue.h"
201de7afc9SPaolo Bonzini #include "qemu/event_notifier.h"
21dcc772e2SLiu Ping Fan #include "qemu/thread.h"
2298563fc3SStefan Hajnoczi #include "qemu/rfifolock.h"
23dae21b98SAlex Bligh #include "qemu/timer.h"
24737e150eSPaolo Bonzini 
25737e150eSPaolo Bonzini typedef struct BlockDriverAIOCB BlockDriverAIOCB;
26737e150eSPaolo Bonzini typedef void BlockDriverCompletionFunc(void *opaque, int ret);
27737e150eSPaolo Bonzini 
28737e150eSPaolo Bonzini typedef struct AIOCBInfo {
29737e150eSPaolo Bonzini     void (*cancel)(BlockDriverAIOCB *acb);
30737e150eSPaolo Bonzini     size_t aiocb_size;
31737e150eSPaolo Bonzini } AIOCBInfo;
32737e150eSPaolo Bonzini 
33737e150eSPaolo Bonzini struct BlockDriverAIOCB {
34737e150eSPaolo Bonzini     const AIOCBInfo *aiocb_info;
35737e150eSPaolo Bonzini     BlockDriverState *bs;
36737e150eSPaolo Bonzini     BlockDriverCompletionFunc *cb;
37737e150eSPaolo Bonzini     void *opaque;
38737e150eSPaolo Bonzini };
39737e150eSPaolo Bonzini 
40737e150eSPaolo Bonzini void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
41737e150eSPaolo Bonzini                    BlockDriverCompletionFunc *cb, void *opaque);
42737e150eSPaolo Bonzini void qemu_aio_release(void *p);
43737e150eSPaolo Bonzini 
44737e150eSPaolo Bonzini typedef struct AioHandler AioHandler;
45737e150eSPaolo Bonzini typedef void QEMUBHFunc(void *opaque);
46737e150eSPaolo Bonzini typedef void IOHandler(void *opaque);
47737e150eSPaolo Bonzini 
486a1751b7SAlex Bligh struct AioContext {
49737e150eSPaolo Bonzini     GSource source;
50737e150eSPaolo Bonzini 
5198563fc3SStefan Hajnoczi     /* Protects all fields from multi-threaded access */
5298563fc3SStefan Hajnoczi     RFifoLock lock;
5398563fc3SStefan Hajnoczi 
54737e150eSPaolo Bonzini     /* The list of registered AIO handlers */
55737e150eSPaolo Bonzini     QLIST_HEAD(, AioHandler) aio_handlers;
56737e150eSPaolo Bonzini 
57737e150eSPaolo Bonzini     /* This is a simple lock used to protect the aio_handlers list.
58737e150eSPaolo Bonzini      * Specifically, it's used to ensure that no callbacks are removed while
59737e150eSPaolo Bonzini      * we're walking and dispatching callbacks.
60737e150eSPaolo Bonzini      */
61737e150eSPaolo Bonzini     int walking_handlers;
62737e150eSPaolo Bonzini 
630ceb849bSPaolo Bonzini     /* Used to avoid unnecessary event_notifier_set calls in aio_notify.
640ceb849bSPaolo Bonzini      * Writes protected by lock or BQL, reads are lockless.
650ceb849bSPaolo Bonzini      */
660ceb849bSPaolo Bonzini     bool dispatching;
670ceb849bSPaolo Bonzini 
68dcc772e2SLiu Ping Fan     /* lock to protect between bh's adders and deleter */
69dcc772e2SLiu Ping Fan     QemuMutex bh_lock;
700ceb849bSPaolo Bonzini 
71737e150eSPaolo Bonzini     /* Anchor of the list of Bottom Halves belonging to the context */
72737e150eSPaolo Bonzini     struct QEMUBH *first_bh;
73737e150eSPaolo Bonzini 
74737e150eSPaolo Bonzini     /* A simple lock used to protect the first_bh list, and ensure that
75737e150eSPaolo Bonzini      * no callbacks are removed while we're walking and dispatching callbacks.
76737e150eSPaolo Bonzini      */
77737e150eSPaolo Bonzini     int walking_bh;
78737e150eSPaolo Bonzini 
79737e150eSPaolo Bonzini     /* Used for aio_notify.  */
80737e150eSPaolo Bonzini     EventNotifier notifier;
816b5f8762SStefan Hajnoczi 
826b5f8762SStefan Hajnoczi     /* GPollFDs for aio_poll() */
836b5f8762SStefan Hajnoczi     GArray *pollfds;
849b34277dSStefan Hajnoczi 
859b34277dSStefan Hajnoczi     /* Thread pool for performing work and receiving completion callbacks */
869b34277dSStefan Hajnoczi     struct ThreadPool *thread_pool;
87dae21b98SAlex Bligh 
88dae21b98SAlex Bligh     /* TimerLists for calling timers - one per clock type */
89dae21b98SAlex Bligh     QEMUTimerListGroup tlg;
906a1751b7SAlex Bligh };
91737e150eSPaolo Bonzini 
920ceb849bSPaolo Bonzini /* Used internally to synchronize aio_poll against qemu_bh_schedule.  */
930ceb849bSPaolo Bonzini void aio_set_dispatching(AioContext *ctx, bool dispatching);
940ceb849bSPaolo Bonzini 
95737e150eSPaolo Bonzini /**
96737e150eSPaolo Bonzini  * aio_context_new: Allocate a new AioContext.
97737e150eSPaolo Bonzini  *
98737e150eSPaolo Bonzini  * AioContext provide a mini event-loop that can be waited on synchronously.
99737e150eSPaolo Bonzini  * They also provide bottom halves, a service to execute a piece of code
100737e150eSPaolo Bonzini  * as soon as possible.
101737e150eSPaolo Bonzini  */
102737e150eSPaolo Bonzini AioContext *aio_context_new(void);
103737e150eSPaolo Bonzini 
104737e150eSPaolo Bonzini /**
105737e150eSPaolo Bonzini  * aio_context_ref:
106737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
107737e150eSPaolo Bonzini  *
108737e150eSPaolo Bonzini  * Add a reference to an AioContext.
109737e150eSPaolo Bonzini  */
110737e150eSPaolo Bonzini void aio_context_ref(AioContext *ctx);
111737e150eSPaolo Bonzini 
112737e150eSPaolo Bonzini /**
113737e150eSPaolo Bonzini  * aio_context_unref:
114737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
115737e150eSPaolo Bonzini  *
116737e150eSPaolo Bonzini  * Drop a reference to an AioContext.
117737e150eSPaolo Bonzini  */
118737e150eSPaolo Bonzini void aio_context_unref(AioContext *ctx);
119737e150eSPaolo Bonzini 
12098563fc3SStefan Hajnoczi /* Take ownership of the AioContext.  If the AioContext will be shared between
12198563fc3SStefan Hajnoczi  * threads, a thread must have ownership when calling aio_poll().
12298563fc3SStefan Hajnoczi  *
12398563fc3SStefan Hajnoczi  * Note that multiple threads calling aio_poll() means timers, BHs, and
12498563fc3SStefan Hajnoczi  * callbacks may be invoked from a different thread than they were registered
12598563fc3SStefan Hajnoczi  * from.  Therefore, code must use AioContext acquire/release or use
12698563fc3SStefan Hajnoczi  * fine-grained synchronization to protect shared state if other threads will
12798563fc3SStefan Hajnoczi  * be accessing it simultaneously.
12898563fc3SStefan Hajnoczi  */
12998563fc3SStefan Hajnoczi void aio_context_acquire(AioContext *ctx);
13098563fc3SStefan Hajnoczi 
13198563fc3SStefan Hajnoczi /* Relinquish ownership of the AioContext. */
13298563fc3SStefan Hajnoczi void aio_context_release(AioContext *ctx);
13398563fc3SStefan Hajnoczi 
134737e150eSPaolo Bonzini /**
135737e150eSPaolo Bonzini  * aio_bh_new: Allocate a new bottom half structure.
136737e150eSPaolo Bonzini  *
137737e150eSPaolo Bonzini  * Bottom halves are lightweight callbacks whose invocation is guaranteed
138737e150eSPaolo Bonzini  * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
139737e150eSPaolo Bonzini  * is opaque and must be allocated prior to its use.
140737e150eSPaolo Bonzini  */
141737e150eSPaolo Bonzini QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
142737e150eSPaolo Bonzini 
143737e150eSPaolo Bonzini /**
144737e150eSPaolo Bonzini  * aio_notify: Force processing of pending events.
145737e150eSPaolo Bonzini  *
146737e150eSPaolo Bonzini  * Similar to signaling a condition variable, aio_notify forces
147737e150eSPaolo Bonzini  * aio_wait to exit, so that the next call will re-examine pending events.
148737e150eSPaolo Bonzini  * The caller of aio_notify will usually call aio_wait again very soon,
149737e150eSPaolo Bonzini  * or go through another iteration of the GLib main loop.  Hence, aio_notify
150737e150eSPaolo Bonzini  * also has the side effect of recalculating the sets of file descriptors
151737e150eSPaolo Bonzini  * that the main loop waits for.
152737e150eSPaolo Bonzini  *
153737e150eSPaolo Bonzini  * Calling aio_notify is rarely necessary, because for example scheduling
154737e150eSPaolo Bonzini  * a bottom half calls it already.
155737e150eSPaolo Bonzini  */
156737e150eSPaolo Bonzini void aio_notify(AioContext *ctx);
157737e150eSPaolo Bonzini 
158737e150eSPaolo Bonzini /**
159737e150eSPaolo Bonzini  * aio_bh_poll: Poll bottom halves for an AioContext.
160737e150eSPaolo Bonzini  *
161737e150eSPaolo Bonzini  * These are internal functions used by the QEMU main loop.
162dcc772e2SLiu Ping Fan  * And notice that multiple occurrences of aio_bh_poll cannot
163dcc772e2SLiu Ping Fan  * be called concurrently
164737e150eSPaolo Bonzini  */
165737e150eSPaolo Bonzini int aio_bh_poll(AioContext *ctx);
166737e150eSPaolo Bonzini 
167737e150eSPaolo Bonzini /**
168737e150eSPaolo Bonzini  * qemu_bh_schedule: Schedule a bottom half.
169737e150eSPaolo Bonzini  *
170737e150eSPaolo Bonzini  * Scheduling a bottom half interrupts the main loop and causes the
171737e150eSPaolo Bonzini  * execution of the callback that was passed to qemu_bh_new.
172737e150eSPaolo Bonzini  *
173737e150eSPaolo Bonzini  * Bottom halves that are scheduled from a bottom half handler are instantly
174737e150eSPaolo Bonzini  * invoked.  This can create an infinite loop if a bottom half handler
175737e150eSPaolo Bonzini  * schedules itself.
176737e150eSPaolo Bonzini  *
177737e150eSPaolo Bonzini  * @bh: The bottom half to be scheduled.
178737e150eSPaolo Bonzini  */
179737e150eSPaolo Bonzini void qemu_bh_schedule(QEMUBH *bh);
180737e150eSPaolo Bonzini 
181737e150eSPaolo Bonzini /**
182737e150eSPaolo Bonzini  * qemu_bh_cancel: Cancel execution of a bottom half.
183737e150eSPaolo Bonzini  *
184737e150eSPaolo Bonzini  * Canceling execution of a bottom half undoes the effect of calls to
185737e150eSPaolo Bonzini  * qemu_bh_schedule without freeing its resources yet.  While cancellation
186737e150eSPaolo Bonzini  * itself is also wait-free and thread-safe, it can of course race with the
187737e150eSPaolo Bonzini  * loop that executes bottom halves unless you are holding the iothread
188737e150eSPaolo Bonzini  * mutex.  This makes it mostly useless if you are not holding the mutex.
189737e150eSPaolo Bonzini  *
190737e150eSPaolo Bonzini  * @bh: The bottom half to be canceled.
191737e150eSPaolo Bonzini  */
192737e150eSPaolo Bonzini void qemu_bh_cancel(QEMUBH *bh);
193737e150eSPaolo Bonzini 
194737e150eSPaolo Bonzini /**
195737e150eSPaolo Bonzini  *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
196737e150eSPaolo Bonzini  *
197737e150eSPaolo Bonzini  * Deleting a bottom half frees the memory that was allocated for it by
198737e150eSPaolo Bonzini  * qemu_bh_new.  It also implies canceling the bottom half if it was
199737e150eSPaolo Bonzini  * scheduled.
200dcc772e2SLiu Ping Fan  * This func is async. The bottom half will do the delete action at the finial
201dcc772e2SLiu Ping Fan  * end.
202737e150eSPaolo Bonzini  *
203737e150eSPaolo Bonzini  * @bh: The bottom half to be deleted.
204737e150eSPaolo Bonzini  */
205737e150eSPaolo Bonzini void qemu_bh_delete(QEMUBH *bh);
206737e150eSPaolo Bonzini 
207737e150eSPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
208737e150eSPaolo Bonzini  * attached to the AioContext.
209737e150eSPaolo Bonzini  *
210737e150eSPaolo Bonzini  * This is used internally in the implementation of the GSource.
211737e150eSPaolo Bonzini  */
212737e150eSPaolo Bonzini bool aio_pending(AioContext *ctx);
213737e150eSPaolo Bonzini 
214*e4c7e2d1SPaolo Bonzini /* Dispatch any pending callbacks from the GSource attached to the AioContext.
215*e4c7e2d1SPaolo Bonzini  *
216*e4c7e2d1SPaolo Bonzini  * This is used internally in the implementation of the GSource.
217*e4c7e2d1SPaolo Bonzini  */
218*e4c7e2d1SPaolo Bonzini bool aio_dispatch(AioContext *ctx);
219*e4c7e2d1SPaolo Bonzini 
220737e150eSPaolo Bonzini /* Progress in completing AIO work to occur.  This can issue new pending
221737e150eSPaolo Bonzini  * aio as a result of executing I/O completion or bh callbacks.
222737e150eSPaolo Bonzini  *
223acfb23adSPaolo Bonzini  * Return whether any progress was made by executing AIO or bottom half
224acfb23adSPaolo Bonzini  * handlers.  If @blocking == true, this should always be true except
225acfb23adSPaolo Bonzini  * if someone called aio_notify.
226737e150eSPaolo Bonzini  *
227737e150eSPaolo Bonzini  * If there are no pending bottom halves, but there are pending AIO
228737e150eSPaolo Bonzini  * operations, it may not be possible to make any progress without
229737e150eSPaolo Bonzini  * blocking.  If @blocking is true, this function will wait until one
230737e150eSPaolo Bonzini  * or more AIO events have completed, to ensure something has moved
231737e150eSPaolo Bonzini  * before returning.
232737e150eSPaolo Bonzini  */
233737e150eSPaolo Bonzini bool aio_poll(AioContext *ctx, bool blocking);
234737e150eSPaolo Bonzini 
235737e150eSPaolo Bonzini #ifdef CONFIG_POSIX
236737e150eSPaolo Bonzini /* Register a file descriptor and associated callbacks.  Behaves very similarly
237737e150eSPaolo Bonzini  * to qemu_set_fd_handler2.  Unlike qemu_set_fd_handler2, these callbacks will
23887f68d31SPaolo Bonzini  * be invoked when using aio_poll().
239737e150eSPaolo Bonzini  *
240737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
241737e150eSPaolo Bonzini  * instead of qemu_set_fd_handler[2].
242737e150eSPaolo Bonzini  */
243737e150eSPaolo Bonzini void aio_set_fd_handler(AioContext *ctx,
244737e150eSPaolo Bonzini                         int fd,
245737e150eSPaolo Bonzini                         IOHandler *io_read,
246737e150eSPaolo Bonzini                         IOHandler *io_write,
247737e150eSPaolo Bonzini                         void *opaque);
248737e150eSPaolo Bonzini #endif
249737e150eSPaolo Bonzini 
250737e150eSPaolo Bonzini /* Register an event notifier and associated callbacks.  Behaves very similarly
251737e150eSPaolo Bonzini  * to event_notifier_set_handler.  Unlike event_notifier_set_handler, these callbacks
25287f68d31SPaolo Bonzini  * will be invoked when using aio_poll().
253737e150eSPaolo Bonzini  *
254737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
255737e150eSPaolo Bonzini  * instead of event_notifier_set_handler.
256737e150eSPaolo Bonzini  */
257737e150eSPaolo Bonzini void aio_set_event_notifier(AioContext *ctx,
258737e150eSPaolo Bonzini                             EventNotifier *notifier,
259f2e5dca4SStefan Hajnoczi                             EventNotifierHandler *io_read);
260737e150eSPaolo Bonzini 
261737e150eSPaolo Bonzini /* Return a GSource that lets the main loop poll the file descriptors attached
262737e150eSPaolo Bonzini  * to this AioContext.
263737e150eSPaolo Bonzini  */
264737e150eSPaolo Bonzini GSource *aio_get_g_source(AioContext *ctx);
265737e150eSPaolo Bonzini 
2669b34277dSStefan Hajnoczi /* Return the ThreadPool bound to this AioContext */
2679b34277dSStefan Hajnoczi struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
2689b34277dSStefan Hajnoczi 
2694e29e831SAlex Bligh /**
2704e29e831SAlex Bligh  * aio_timer_new:
2714e29e831SAlex Bligh  * @ctx: the aio context
2724e29e831SAlex Bligh  * @type: the clock type
2734e29e831SAlex Bligh  * @scale: the scale
2744e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
2754e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
2764e29e831SAlex Bligh  *
2774e29e831SAlex Bligh  * Allocate a new timer attached to the context @ctx.
2784e29e831SAlex Bligh  * The function is responsible for memory allocation.
2794e29e831SAlex Bligh  *
2804e29e831SAlex Bligh  * The preferred interface is aio_timer_init. Use that
2814e29e831SAlex Bligh  * unless you really need dynamic memory allocation.
2824e29e831SAlex Bligh  *
2834e29e831SAlex Bligh  * Returns: a pointer to the new timer
2844e29e831SAlex Bligh  */
2854e29e831SAlex Bligh static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
2864e29e831SAlex Bligh                                        int scale,
2874e29e831SAlex Bligh                                        QEMUTimerCB *cb, void *opaque)
2884e29e831SAlex Bligh {
2894e29e831SAlex Bligh     return timer_new_tl(ctx->tlg.tl[type], scale, cb, opaque);
2904e29e831SAlex Bligh }
2914e29e831SAlex Bligh 
2924e29e831SAlex Bligh /**
2934e29e831SAlex Bligh  * aio_timer_init:
2944e29e831SAlex Bligh  * @ctx: the aio context
2954e29e831SAlex Bligh  * @ts: the timer
2964e29e831SAlex Bligh  * @type: the clock type
2974e29e831SAlex Bligh  * @scale: the scale
2984e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
2994e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
3004e29e831SAlex Bligh  *
3014e29e831SAlex Bligh  * Initialise a new timer attached to the context @ctx.
3024e29e831SAlex Bligh  * The caller is responsible for memory allocation.
3034e29e831SAlex Bligh  */
3044e29e831SAlex Bligh static inline void aio_timer_init(AioContext *ctx,
3054e29e831SAlex Bligh                                   QEMUTimer *ts, QEMUClockType type,
3064e29e831SAlex Bligh                                   int scale,
3074e29e831SAlex Bligh                                   QEMUTimerCB *cb, void *opaque)
3084e29e831SAlex Bligh {
3094e29e831SAlex Bligh     timer_init(ts, ctx->tlg.tl[type], scale, cb, opaque);
3104e29e831SAlex Bligh }
3114e29e831SAlex Bligh 
312845ca10dSPaolo Bonzini /**
313845ca10dSPaolo Bonzini  * aio_compute_timeout:
314845ca10dSPaolo Bonzini  * @ctx: the aio context
315845ca10dSPaolo Bonzini  *
316845ca10dSPaolo Bonzini  * Compute the timeout that a blocking aio_poll should use.
317845ca10dSPaolo Bonzini  */
318845ca10dSPaolo Bonzini int64_t aio_compute_timeout(AioContext *ctx);
319845ca10dSPaolo Bonzini 
320737e150eSPaolo Bonzini #endif
321