1 /* 2 * QEMU aio implementation 3 * 4 * Copyright IBM, Corp. 2008 5 * 6 * Authors: 7 * Anthony Liguori <aliguori@us.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 */ 13 14 #ifndef QEMU_AIO_H 15 #define QEMU_AIO_H 16 17 #include "qemu/typedefs.h" 18 #include "qemu-common.h" 19 #include "qemu/queue.h" 20 #include "qemu/event_notifier.h" 21 #include "qemu/thread.h" 22 #include "qemu/rfifolock.h" 23 #include "qemu/timer.h" 24 25 typedef struct BlockAIOCB BlockAIOCB; 26 typedef void BlockCompletionFunc(void *opaque, int ret); 27 28 typedef struct AIOCBInfo { 29 void (*cancel_async)(BlockAIOCB *acb); 30 AioContext *(*get_aio_context)(BlockAIOCB *acb); 31 size_t aiocb_size; 32 } AIOCBInfo; 33 34 struct BlockAIOCB { 35 const AIOCBInfo *aiocb_info; 36 BlockDriverState *bs; 37 BlockCompletionFunc *cb; 38 void *opaque; 39 int refcnt; 40 }; 41 42 void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, 43 BlockCompletionFunc *cb, void *opaque); 44 void qemu_aio_unref(void *p); 45 void qemu_aio_ref(void *p); 46 47 typedef struct AioHandler AioHandler; 48 typedef void QEMUBHFunc(void *opaque); 49 typedef void IOHandler(void *opaque); 50 51 struct AioContext { 52 GSource source; 53 54 /* Protects all fields from multi-threaded access */ 55 RFifoLock lock; 56 57 /* The list of registered AIO handlers */ 58 QLIST_HEAD(, AioHandler) aio_handlers; 59 60 /* This is a simple lock used to protect the aio_handlers list. 61 * Specifically, it's used to ensure that no callbacks are removed while 62 * we're walking and dispatching callbacks. 63 */ 64 int walking_handlers; 65 66 /* Used to avoid unnecessary event_notifier_set calls in aio_notify; 67 * accessed with atomic primitives. If this field is 0, everything 68 * (file descriptors, bottom halves, timers) will be re-evaluated 69 * before the next blocking poll(), thus the event_notifier_set call 70 * can be skipped. If it is non-zero, you may need to wake up a 71 * concurrent aio_poll or the glib main event loop, making 72 * event_notifier_set necessary. 73 * 74 * Bit 0 is reserved for GSource usage of the AioContext, and is 1 75 * between a call to aio_ctx_check and the next call to aio_ctx_dispatch. 76 * Bits 1-31 simply count the number of active calls to aio_poll 77 * that are in the prepare or poll phase. 78 * 79 * The GSource and aio_poll must use a different mechanism because 80 * there is no certainty that a call to GSource's prepare callback 81 * (via g_main_context_prepare) is indeed followed by check and 82 * dispatch. It's not clear whether this would be a bug, but let's 83 * play safe and allow it---it will just cause extra calls to 84 * event_notifier_set until the next call to dispatch. 85 * 86 * Instead, the aio_poll calls include both the prepare and the 87 * dispatch phase, hence a simple counter is enough for them. 88 */ 89 uint32_t notify_me; 90 91 /* lock to protect between bh's adders and deleter */ 92 QemuMutex bh_lock; 93 94 /* Anchor of the list of Bottom Halves belonging to the context */ 95 struct QEMUBH *first_bh; 96 97 /* A simple lock used to protect the first_bh list, and ensure that 98 * no callbacks are removed while we're walking and dispatching callbacks. 99 */ 100 int walking_bh; 101 102 /* Used by aio_notify. 103 * 104 * "notified" is used to avoid expensive event_notifier_test_and_clear 105 * calls. When it is clear, the EventNotifier is clear, or one thread 106 * is going to clear "notified" before processing more events. False 107 * positives are possible, i.e. "notified" could be set even though the 108 * EventNotifier is clear. 109 * 110 * Note that event_notifier_set *cannot* be optimized the same way. For 111 * more information on the problem that would result, see "#ifdef BUG2" 112 * in the docs/aio_notify_accept.promela formal model. 113 */ 114 bool notified; 115 EventNotifier notifier; 116 117 /* Scheduling this BH forces the event loop it iterate */ 118 QEMUBH *notify_dummy_bh; 119 120 /* Thread pool for performing work and receiving completion callbacks */ 121 struct ThreadPool *thread_pool; 122 123 /* TimerLists for calling timers - one per clock type */ 124 QEMUTimerListGroup tlg; 125 126 int external_disable_cnt; 127 }; 128 129 /** 130 * aio_context_new: Allocate a new AioContext. 131 * 132 * AioContext provide a mini event-loop that can be waited on synchronously. 133 * They also provide bottom halves, a service to execute a piece of code 134 * as soon as possible. 135 */ 136 AioContext *aio_context_new(Error **errp); 137 138 /** 139 * aio_context_ref: 140 * @ctx: The AioContext to operate on. 141 * 142 * Add a reference to an AioContext. 143 */ 144 void aio_context_ref(AioContext *ctx); 145 146 /** 147 * aio_context_unref: 148 * @ctx: The AioContext to operate on. 149 * 150 * Drop a reference to an AioContext. 151 */ 152 void aio_context_unref(AioContext *ctx); 153 154 /* Take ownership of the AioContext. If the AioContext will be shared between 155 * threads, and a thread does not want to be interrupted, it will have to 156 * take ownership around calls to aio_poll(). Otherwise, aio_poll() 157 * automatically takes care of calling aio_context_acquire and 158 * aio_context_release. 159 * 160 * Access to timers and BHs from a thread that has not acquired AioContext 161 * is possible. Access to callbacks for now must be done while the AioContext 162 * is owned by the thread (FIXME). 163 */ 164 void aio_context_acquire(AioContext *ctx); 165 166 /* Relinquish ownership of the AioContext. */ 167 void aio_context_release(AioContext *ctx); 168 169 /** 170 * aio_bh_new: Allocate a new bottom half structure. 171 * 172 * Bottom halves are lightweight callbacks whose invocation is guaranteed 173 * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure 174 * is opaque and must be allocated prior to its use. 175 */ 176 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque); 177 178 /** 179 * aio_notify: Force processing of pending events. 180 * 181 * Similar to signaling a condition variable, aio_notify forces 182 * aio_wait to exit, so that the next call will re-examine pending events. 183 * The caller of aio_notify will usually call aio_wait again very soon, 184 * or go through another iteration of the GLib main loop. Hence, aio_notify 185 * also has the side effect of recalculating the sets of file descriptors 186 * that the main loop waits for. 187 * 188 * Calling aio_notify is rarely necessary, because for example scheduling 189 * a bottom half calls it already. 190 */ 191 void aio_notify(AioContext *ctx); 192 193 /** 194 * aio_notify_accept: Acknowledge receiving an aio_notify. 195 * 196 * aio_notify() uses an EventNotifier in order to wake up a sleeping 197 * aio_poll() or g_main_context_iteration(). Calls to aio_notify() are 198 * usually rare, but the AioContext has to clear the EventNotifier on 199 * every aio_poll() or g_main_context_iteration() in order to avoid 200 * busy waiting. This event_notifier_test_and_clear() cannot be done 201 * using the usual aio_context_set_event_notifier(), because it must 202 * be done before processing all events (file descriptors, bottom halves, 203 * timers). 204 * 205 * aio_notify_accept() is an optimized event_notifier_test_and_clear() 206 * that is specific to an AioContext's notifier; it is used internally 207 * to clear the EventNotifier only if aio_notify() had been called. 208 */ 209 void aio_notify_accept(AioContext *ctx); 210 211 /** 212 * aio_bh_call: Executes callback function of the specified BH. 213 */ 214 void aio_bh_call(QEMUBH *bh); 215 216 /** 217 * aio_bh_poll: Poll bottom halves for an AioContext. 218 * 219 * These are internal functions used by the QEMU main loop. 220 * And notice that multiple occurrences of aio_bh_poll cannot 221 * be called concurrently 222 */ 223 int aio_bh_poll(AioContext *ctx); 224 225 /** 226 * qemu_bh_schedule: Schedule a bottom half. 227 * 228 * Scheduling a bottom half interrupts the main loop and causes the 229 * execution of the callback that was passed to qemu_bh_new. 230 * 231 * Bottom halves that are scheduled from a bottom half handler are instantly 232 * invoked. This can create an infinite loop if a bottom half handler 233 * schedules itself. 234 * 235 * @bh: The bottom half to be scheduled. 236 */ 237 void qemu_bh_schedule(QEMUBH *bh); 238 239 /** 240 * qemu_bh_cancel: Cancel execution of a bottom half. 241 * 242 * Canceling execution of a bottom half undoes the effect of calls to 243 * qemu_bh_schedule without freeing its resources yet. While cancellation 244 * itself is also wait-free and thread-safe, it can of course race with the 245 * loop that executes bottom halves unless you are holding the iothread 246 * mutex. This makes it mostly useless if you are not holding the mutex. 247 * 248 * @bh: The bottom half to be canceled. 249 */ 250 void qemu_bh_cancel(QEMUBH *bh); 251 252 /** 253 *qemu_bh_delete: Cancel execution of a bottom half and free its resources. 254 * 255 * Deleting a bottom half frees the memory that was allocated for it by 256 * qemu_bh_new. It also implies canceling the bottom half if it was 257 * scheduled. 258 * This func is async. The bottom half will do the delete action at the finial 259 * end. 260 * 261 * @bh: The bottom half to be deleted. 262 */ 263 void qemu_bh_delete(QEMUBH *bh); 264 265 /* Return whether there are any pending callbacks from the GSource 266 * attached to the AioContext, before g_poll is invoked. 267 * 268 * This is used internally in the implementation of the GSource. 269 */ 270 bool aio_prepare(AioContext *ctx); 271 272 /* Return whether there are any pending callbacks from the GSource 273 * attached to the AioContext, after g_poll is invoked. 274 * 275 * This is used internally in the implementation of the GSource. 276 */ 277 bool aio_pending(AioContext *ctx); 278 279 /* Dispatch any pending callbacks from the GSource attached to the AioContext. 280 * 281 * This is used internally in the implementation of the GSource. 282 */ 283 bool aio_dispatch(AioContext *ctx); 284 285 /* Progress in completing AIO work to occur. This can issue new pending 286 * aio as a result of executing I/O completion or bh callbacks. 287 * 288 * Return whether any progress was made by executing AIO or bottom half 289 * handlers. If @blocking == true, this should always be true except 290 * if someone called aio_notify. 291 * 292 * If there are no pending bottom halves, but there are pending AIO 293 * operations, it may not be possible to make any progress without 294 * blocking. If @blocking is true, this function will wait until one 295 * or more AIO events have completed, to ensure something has moved 296 * before returning. 297 */ 298 bool aio_poll(AioContext *ctx, bool blocking); 299 300 /* Register a file descriptor and associated callbacks. Behaves very similarly 301 * to qemu_set_fd_handler. Unlike qemu_set_fd_handler, these callbacks will 302 * be invoked when using aio_poll(). 303 * 304 * Code that invokes AIO completion functions should rely on this function 305 * instead of qemu_set_fd_handler[2]. 306 */ 307 void aio_set_fd_handler(AioContext *ctx, 308 int fd, 309 bool is_external, 310 IOHandler *io_read, 311 IOHandler *io_write, 312 void *opaque); 313 314 /* Register an event notifier and associated callbacks. Behaves very similarly 315 * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks 316 * will be invoked when using aio_poll(). 317 * 318 * Code that invokes AIO completion functions should rely on this function 319 * instead of event_notifier_set_handler. 320 */ 321 void aio_set_event_notifier(AioContext *ctx, 322 EventNotifier *notifier, 323 bool is_external, 324 EventNotifierHandler *io_read); 325 326 /* Return a GSource that lets the main loop poll the file descriptors attached 327 * to this AioContext. 328 */ 329 GSource *aio_get_g_source(AioContext *ctx); 330 331 /* Return the ThreadPool bound to this AioContext */ 332 struct ThreadPool *aio_get_thread_pool(AioContext *ctx); 333 334 /** 335 * aio_timer_new: 336 * @ctx: the aio context 337 * @type: the clock type 338 * @scale: the scale 339 * @cb: the callback to call on timer expiry 340 * @opaque: the opaque pointer to pass to the callback 341 * 342 * Allocate a new timer attached to the context @ctx. 343 * The function is responsible for memory allocation. 344 * 345 * The preferred interface is aio_timer_init. Use that 346 * unless you really need dynamic memory allocation. 347 * 348 * Returns: a pointer to the new timer 349 */ 350 static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type, 351 int scale, 352 QEMUTimerCB *cb, void *opaque) 353 { 354 return timer_new_tl(ctx->tlg.tl[type], scale, cb, opaque); 355 } 356 357 /** 358 * aio_timer_init: 359 * @ctx: the aio context 360 * @ts: the timer 361 * @type: the clock type 362 * @scale: the scale 363 * @cb: the callback to call on timer expiry 364 * @opaque: the opaque pointer to pass to the callback 365 * 366 * Initialise a new timer attached to the context @ctx. 367 * The caller is responsible for memory allocation. 368 */ 369 static inline void aio_timer_init(AioContext *ctx, 370 QEMUTimer *ts, QEMUClockType type, 371 int scale, 372 QEMUTimerCB *cb, void *opaque) 373 { 374 timer_init_tl(ts, ctx->tlg.tl[type], scale, cb, opaque); 375 } 376 377 /** 378 * aio_compute_timeout: 379 * @ctx: the aio context 380 * 381 * Compute the timeout that a blocking aio_poll should use. 382 */ 383 int64_t aio_compute_timeout(AioContext *ctx); 384 385 /** 386 * aio_disable_external: 387 * @ctx: the aio context 388 * 389 * Disable the further processing of external clients. 390 */ 391 static inline void aio_disable_external(AioContext *ctx) 392 { 393 atomic_inc(&ctx->external_disable_cnt); 394 } 395 396 /** 397 * aio_enable_external: 398 * @ctx: the aio context 399 * 400 * Enable the processing of external clients. 401 */ 402 static inline void aio_enable_external(AioContext *ctx) 403 { 404 assert(ctx->external_disable_cnt > 0); 405 atomic_dec(&ctx->external_disable_cnt); 406 } 407 408 /** 409 * aio_node_check: 410 * @ctx: the aio context 411 * @is_external: Whether or not the checked node is an external event source. 412 * 413 * Check if the node's is_external flag is okay to be polled by the ctx at this 414 * moment. True means green light. 415 */ 416 static inline bool aio_node_check(AioContext *ctx, bool is_external) 417 { 418 return !is_external || !atomic_read(&ctx->external_disable_cnt); 419 } 420 421 #endif 422