2
0

async.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * Data plane event loop
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2009-2017 QEMU contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qapi/error.h"
  27. #include "block/aio.h"
  28. #include "block/thread-pool.h"
  29. #include "block/graph-lock.h"
  30. #include "qemu/main-loop.h"
  31. #include "qemu/atomic.h"
  32. #include "qemu/rcu_queue.h"
  33. #include "block/raw-aio.h"
  34. #include "qemu/coroutine_int.h"
  35. #include "qemu/coroutine-tls.h"
  36. #include "sysemu/cpu-timers.h"
  37. #include "trace.h"
  38. /***********************************************************/
  39. /* bottom halves (can be seen as timers which expire ASAP) */
  40. /* QEMUBH::flags values */
  41. enum {
  42. /* Already enqueued and waiting for aio_bh_poll() */
  43. BH_PENDING = (1 << 0),
  44. /* Invoke the callback */
  45. BH_SCHEDULED = (1 << 1),
  46. /* Delete without invoking callback */
  47. BH_DELETED = (1 << 2),
  48. /* Delete after invoking callback */
  49. BH_ONESHOT = (1 << 3),
  50. /* Schedule periodically when the event loop is idle */
  51. BH_IDLE = (1 << 4),
  52. };
  53. struct QEMUBH {
  54. AioContext *ctx;
  55. const char *name;
  56. QEMUBHFunc *cb;
  57. void *opaque;
  58. QSLIST_ENTRY(QEMUBH) next;
  59. unsigned flags;
  60. };
  61. /* Called concurrently from any thread */
  62. static void aio_bh_enqueue(QEMUBH *bh, unsigned new_flags)
  63. {
  64. AioContext *ctx = bh->ctx;
  65. unsigned old_flags;
  66. /*
  67. * Synchronizes with atomic_fetch_and() in aio_bh_dequeue(), ensuring that
  68. * insertion starts after BH_PENDING is set.
  69. */
  70. old_flags = qatomic_fetch_or(&bh->flags, BH_PENDING | new_flags);
  71. if (!(old_flags & BH_PENDING)) {
  72. /*
  73. * At this point the bottom half becomes visible to aio_bh_poll().
  74. * This insertion thus synchronizes with QSLIST_MOVE_ATOMIC in
  75. * aio_bh_poll(), ensuring that:
  76. * 1. any writes needed by the callback are visible from the callback
  77. * after aio_bh_dequeue() returns bh.
  78. * 2. ctx is loaded before the callback has a chance to execute and bh
  79. * could be freed.
  80. */
  81. QSLIST_INSERT_HEAD_ATOMIC(&ctx->bh_list, bh, next);
  82. }
  83. aio_notify(ctx);
  84. /*
  85. * Workaround for record/replay.
  86. * vCPU execution should be suspended when new BH is set.
  87. * This is needed to avoid guest timeouts caused
  88. * by the long cycles of the execution.
  89. */
  90. icount_notify_exit();
  91. }
  92. /* Only called from aio_bh_poll() and aio_ctx_finalize() */
  93. static QEMUBH *aio_bh_dequeue(BHList *head, unsigned *flags)
  94. {
  95. QEMUBH *bh = QSLIST_FIRST_RCU(head);
  96. if (!bh) {
  97. return NULL;
  98. }
  99. QSLIST_REMOVE_HEAD(head, next);
  100. /*
  101. * Synchronizes with qatomic_fetch_or() in aio_bh_enqueue(), ensuring that
  102. * the removal finishes before BH_PENDING is reset.
  103. */
  104. *flags = qatomic_fetch_and(&bh->flags,
  105. ~(BH_PENDING | BH_SCHEDULED | BH_IDLE));
  106. return bh;
  107. }
  108. void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb,
  109. void *opaque, const char *name)
  110. {
  111. QEMUBH *bh;
  112. bh = g_new(QEMUBH, 1);
  113. *bh = (QEMUBH){
  114. .ctx = ctx,
  115. .cb = cb,
  116. .opaque = opaque,
  117. .name = name,
  118. };
  119. aio_bh_enqueue(bh, BH_SCHEDULED | BH_ONESHOT);
  120. }
  121. QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
  122. const char *name)
  123. {
  124. QEMUBH *bh;
  125. bh = g_new(QEMUBH, 1);
  126. *bh = (QEMUBH){
  127. .ctx = ctx,
  128. .cb = cb,
  129. .opaque = opaque,
  130. .name = name,
  131. };
  132. return bh;
  133. }
  134. void aio_bh_call(QEMUBH *bh)
  135. {
  136. bh->cb(bh->opaque);
  137. }
  138. /* Multiple occurrences of aio_bh_poll cannot be called concurrently. */
  139. int aio_bh_poll(AioContext *ctx)
  140. {
  141. BHListSlice slice;
  142. BHListSlice *s;
  143. int ret = 0;
  144. /* Synchronizes with QSLIST_INSERT_HEAD_ATOMIC in aio_bh_enqueue(). */
  145. QSLIST_MOVE_ATOMIC(&slice.bh_list, &ctx->bh_list);
  146. /*
  147. * GCC13 [-Werror=dangling-pointer=] complains that the local variable
  148. * 'slice' is being stored in the global 'ctx->bh_slice_list' but the
  149. * list is emptied before this function returns.
  150. */
  151. #if !defined(__clang__)
  152. #pragma GCC diagnostic push
  153. #pragma GCC diagnostic ignored "-Wpragmas"
  154. #pragma GCC diagnostic ignored "-Wdangling-pointer="
  155. #endif
  156. QSIMPLEQ_INSERT_TAIL(&ctx->bh_slice_list, &slice, next);
  157. #if !defined(__clang__)
  158. #pragma GCC diagnostic pop
  159. #endif
  160. while ((s = QSIMPLEQ_FIRST(&ctx->bh_slice_list))) {
  161. QEMUBH *bh;
  162. unsigned flags;
  163. bh = aio_bh_dequeue(&s->bh_list, &flags);
  164. if (!bh) {
  165. QSIMPLEQ_REMOVE_HEAD(&ctx->bh_slice_list, next);
  166. continue;
  167. }
  168. if ((flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
  169. /* Idle BHs don't count as progress */
  170. if (!(flags & BH_IDLE)) {
  171. ret = 1;
  172. }
  173. aio_bh_call(bh);
  174. }
  175. if (flags & (BH_DELETED | BH_ONESHOT)) {
  176. g_free(bh);
  177. }
  178. }
  179. return ret;
  180. }
  181. void qemu_bh_schedule_idle(QEMUBH *bh)
  182. {
  183. aio_bh_enqueue(bh, BH_SCHEDULED | BH_IDLE);
  184. }
  185. void qemu_bh_schedule(QEMUBH *bh)
  186. {
  187. aio_bh_enqueue(bh, BH_SCHEDULED);
  188. }
  189. /* This func is async.
  190. */
  191. void qemu_bh_cancel(QEMUBH *bh)
  192. {
  193. qatomic_and(&bh->flags, ~BH_SCHEDULED);
  194. }
  195. /* This func is async.The bottom half will do the delete action at the finial
  196. * end.
  197. */
  198. void qemu_bh_delete(QEMUBH *bh)
  199. {
  200. aio_bh_enqueue(bh, BH_DELETED);
  201. }
  202. static int64_t aio_compute_bh_timeout(BHList *head, int timeout)
  203. {
  204. QEMUBH *bh;
  205. QSLIST_FOREACH_RCU(bh, head, next) {
  206. if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
  207. if (bh->flags & BH_IDLE) {
  208. /* idle bottom halves will be polled at least
  209. * every 10ms */
  210. timeout = 10000000;
  211. } else {
  212. /* non-idle bottom halves will be executed
  213. * immediately */
  214. return 0;
  215. }
  216. }
  217. }
  218. return timeout;
  219. }
  220. int64_t
  221. aio_compute_timeout(AioContext *ctx)
  222. {
  223. BHListSlice *s;
  224. int64_t deadline;
  225. int timeout = -1;
  226. timeout = aio_compute_bh_timeout(&ctx->bh_list, timeout);
  227. if (timeout == 0) {
  228. return 0;
  229. }
  230. QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) {
  231. timeout = aio_compute_bh_timeout(&s->bh_list, timeout);
  232. if (timeout == 0) {
  233. return 0;
  234. }
  235. }
  236. deadline = timerlistgroup_deadline_ns(&ctx->tlg);
  237. if (deadline == 0) {
  238. return 0;
  239. } else {
  240. return qemu_soonest_timeout(timeout, deadline);
  241. }
  242. }
  243. static gboolean
  244. aio_ctx_prepare(GSource *source, gint *timeout)
  245. {
  246. AioContext *ctx = (AioContext *) source;
  247. qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) | 1);
  248. /*
  249. * Write ctx->notify_me before computing the timeout
  250. * (reading bottom half flags, etc.). Pairs with
  251. * smp_mb in aio_notify().
  252. */
  253. smp_mb();
  254. /* We assume there is no timeout already supplied */
  255. *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
  256. if (aio_prepare(ctx)) {
  257. *timeout = 0;
  258. }
  259. return *timeout == 0;
  260. }
  261. static gboolean
  262. aio_ctx_check(GSource *source)
  263. {
  264. AioContext *ctx = (AioContext *) source;
  265. QEMUBH *bh;
  266. BHListSlice *s;
  267. /* Finish computing the timeout before clearing the flag. */
  268. qatomic_store_release(&ctx->notify_me, qatomic_read(&ctx->notify_me) & ~1);
  269. aio_notify_accept(ctx);
  270. QSLIST_FOREACH_RCU(bh, &ctx->bh_list, next) {
  271. if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
  272. return true;
  273. }
  274. }
  275. QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) {
  276. QSLIST_FOREACH_RCU(bh, &s->bh_list, next) {
  277. if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
  278. return true;
  279. }
  280. }
  281. }
  282. return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
  283. }
  284. static gboolean
  285. aio_ctx_dispatch(GSource *source,
  286. GSourceFunc callback,
  287. gpointer user_data)
  288. {
  289. AioContext *ctx = (AioContext *) source;
  290. assert(callback == NULL);
  291. aio_dispatch(ctx);
  292. return true;
  293. }
  294. static void
  295. aio_ctx_finalize(GSource *source)
  296. {
  297. AioContext *ctx = (AioContext *) source;
  298. QEMUBH *bh;
  299. unsigned flags;
  300. thread_pool_free(ctx->thread_pool);
  301. #ifdef CONFIG_LINUX_AIO
  302. if (ctx->linux_aio) {
  303. laio_detach_aio_context(ctx->linux_aio, ctx);
  304. laio_cleanup(ctx->linux_aio);
  305. ctx->linux_aio = NULL;
  306. }
  307. #endif
  308. #ifdef CONFIG_LINUX_IO_URING
  309. if (ctx->linux_io_uring) {
  310. luring_detach_aio_context(ctx->linux_io_uring, ctx);
  311. luring_cleanup(ctx->linux_io_uring);
  312. ctx->linux_io_uring = NULL;
  313. }
  314. #endif
  315. assert(QSLIST_EMPTY(&ctx->scheduled_coroutines));
  316. qemu_bh_delete(ctx->co_schedule_bh);
  317. /* There must be no aio_bh_poll() calls going on */
  318. assert(QSIMPLEQ_EMPTY(&ctx->bh_slice_list));
  319. while ((bh = aio_bh_dequeue(&ctx->bh_list, &flags))) {
  320. /*
  321. * qemu_bh_delete() must have been called on BHs in this AioContext. In
  322. * many cases memory leaks, hangs, or inconsistent state occur when a
  323. * BH is leaked because something still expects it to run.
  324. *
  325. * If you hit this, fix the lifecycle of the BH so that
  326. * qemu_bh_delete() and any associated cleanup is called before the
  327. * AioContext is finalized.
  328. */
  329. if (unlikely(!(flags & BH_DELETED))) {
  330. fprintf(stderr, "%s: BH '%s' leaked, aborting...\n",
  331. __func__, bh->name);
  332. abort();
  333. }
  334. g_free(bh);
  335. }
  336. aio_set_event_notifier(ctx, &ctx->notifier, false, NULL, NULL, NULL);
  337. event_notifier_cleanup(&ctx->notifier);
  338. qemu_rec_mutex_destroy(&ctx->lock);
  339. qemu_lockcnt_destroy(&ctx->list_lock);
  340. timerlistgroup_deinit(&ctx->tlg);
  341. unregister_aiocontext(ctx);
  342. aio_context_destroy(ctx);
  343. }
  344. static GSourceFuncs aio_source_funcs = {
  345. aio_ctx_prepare,
  346. aio_ctx_check,
  347. aio_ctx_dispatch,
  348. aio_ctx_finalize
  349. };
  350. GSource *aio_get_g_source(AioContext *ctx)
  351. {
  352. aio_context_use_g_source(ctx);
  353. g_source_ref(&ctx->source);
  354. return &ctx->source;
  355. }
  356. ThreadPool *aio_get_thread_pool(AioContext *ctx)
  357. {
  358. if (!ctx->thread_pool) {
  359. ctx->thread_pool = thread_pool_new(ctx);
  360. }
  361. return ctx->thread_pool;
  362. }
  363. #ifdef CONFIG_LINUX_AIO
  364. LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp)
  365. {
  366. if (!ctx->linux_aio) {
  367. ctx->linux_aio = laio_init(errp);
  368. if (ctx->linux_aio) {
  369. laio_attach_aio_context(ctx->linux_aio, ctx);
  370. }
  371. }
  372. return ctx->linux_aio;
  373. }
  374. LinuxAioState *aio_get_linux_aio(AioContext *ctx)
  375. {
  376. assert(ctx->linux_aio);
  377. return ctx->linux_aio;
  378. }
  379. #endif
  380. #ifdef CONFIG_LINUX_IO_URING
  381. LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp)
  382. {
  383. if (ctx->linux_io_uring) {
  384. return ctx->linux_io_uring;
  385. }
  386. ctx->linux_io_uring = luring_init(errp);
  387. if (!ctx->linux_io_uring) {
  388. return NULL;
  389. }
  390. luring_attach_aio_context(ctx->linux_io_uring, ctx);
  391. return ctx->linux_io_uring;
  392. }
  393. LuringState *aio_get_linux_io_uring(AioContext *ctx)
  394. {
  395. assert(ctx->linux_io_uring);
  396. return ctx->linux_io_uring;
  397. }
  398. #endif
  399. void aio_notify(AioContext *ctx)
  400. {
  401. /*
  402. * Write e.g. ctx->bh_list before writing ctx->notified. Pairs with
  403. * smp_mb() in aio_notify_accept().
  404. */
  405. smp_wmb();
  406. qatomic_set(&ctx->notified, true);
  407. /*
  408. * Write ctx->notified (and also ctx->bh_list) before reading ctx->notify_me.
  409. * Pairs with smp_mb() in aio_ctx_prepare or aio_poll.
  410. */
  411. smp_mb();
  412. if (qatomic_read(&ctx->notify_me)) {
  413. event_notifier_set(&ctx->notifier);
  414. }
  415. }
  416. void aio_notify_accept(AioContext *ctx)
  417. {
  418. qatomic_set(&ctx->notified, false);
  419. /*
  420. * Order reads of ctx->notified (in aio_context_notifier_poll()) and the
  421. * above clearing of ctx->notified before reads of e.g. bh->flags. Pairs
  422. * with smp_wmb() in aio_notify.
  423. */
  424. smp_mb();
  425. }
  426. static void aio_timerlist_notify(void *opaque, QEMUClockType type)
  427. {
  428. aio_notify(opaque);
  429. }
  430. static void aio_context_notifier_cb(EventNotifier *e)
  431. {
  432. AioContext *ctx = container_of(e, AioContext, notifier);
  433. event_notifier_test_and_clear(&ctx->notifier);
  434. }
  435. /* Returns true if aio_notify() was called (e.g. a BH was scheduled) */
  436. static bool aio_context_notifier_poll(void *opaque)
  437. {
  438. EventNotifier *e = opaque;
  439. AioContext *ctx = container_of(e, AioContext, notifier);
  440. /*
  441. * No need for load-acquire because we just want to kick the
  442. * event loop. aio_notify_accept() takes care of synchronizing
  443. * the event loop with the producers.
  444. */
  445. return qatomic_read(&ctx->notified);
  446. }
  447. static void aio_context_notifier_poll_ready(EventNotifier *e)
  448. {
  449. /* Do nothing, we just wanted to kick the event loop */
  450. }
  451. static void co_schedule_bh_cb(void *opaque)
  452. {
  453. AioContext *ctx = opaque;
  454. QSLIST_HEAD(, Coroutine) straight, reversed;
  455. QSLIST_MOVE_ATOMIC(&reversed, &ctx->scheduled_coroutines);
  456. QSLIST_INIT(&straight);
  457. while (!QSLIST_EMPTY(&reversed)) {
  458. Coroutine *co = QSLIST_FIRST(&reversed);
  459. QSLIST_REMOVE_HEAD(&reversed, co_scheduled_next);
  460. QSLIST_INSERT_HEAD(&straight, co, co_scheduled_next);
  461. }
  462. while (!QSLIST_EMPTY(&straight)) {
  463. Coroutine *co = QSLIST_FIRST(&straight);
  464. QSLIST_REMOVE_HEAD(&straight, co_scheduled_next);
  465. trace_aio_co_schedule_bh_cb(ctx, co);
  466. aio_context_acquire(ctx);
  467. /* Protected by write barrier in qemu_aio_coroutine_enter */
  468. qatomic_set(&co->scheduled, NULL);
  469. qemu_aio_coroutine_enter(ctx, co);
  470. aio_context_release(ctx);
  471. }
  472. }
  473. AioContext *aio_context_new(Error **errp)
  474. {
  475. int ret;
  476. AioContext *ctx;
  477. ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
  478. QSLIST_INIT(&ctx->bh_list);
  479. QSIMPLEQ_INIT(&ctx->bh_slice_list);
  480. aio_context_setup(ctx);
  481. ret = event_notifier_init(&ctx->notifier, false);
  482. if (ret < 0) {
  483. error_setg_errno(errp, -ret, "Failed to initialize event notifier");
  484. goto fail;
  485. }
  486. g_source_set_can_recurse(&ctx->source, true);
  487. qemu_lockcnt_init(&ctx->list_lock);
  488. ctx->co_schedule_bh = aio_bh_new(ctx, co_schedule_bh_cb, ctx);
  489. QSLIST_INIT(&ctx->scheduled_coroutines);
  490. aio_set_event_notifier(ctx, &ctx->notifier,
  491. false,
  492. aio_context_notifier_cb,
  493. aio_context_notifier_poll,
  494. aio_context_notifier_poll_ready);
  495. #ifdef CONFIG_LINUX_AIO
  496. ctx->linux_aio = NULL;
  497. #endif
  498. #ifdef CONFIG_LINUX_IO_URING
  499. ctx->linux_io_uring = NULL;
  500. #endif
  501. ctx->thread_pool = NULL;
  502. qemu_rec_mutex_init(&ctx->lock);
  503. timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
  504. ctx->poll_ns = 0;
  505. ctx->poll_max_ns = 0;
  506. ctx->poll_grow = 0;
  507. ctx->poll_shrink = 0;
  508. ctx->aio_max_batch = 0;
  509. ctx->thread_pool_min = 0;
  510. ctx->thread_pool_max = THREAD_POOL_MAX_THREADS_DEFAULT;
  511. register_aiocontext(ctx);
  512. return ctx;
  513. fail:
  514. g_source_destroy(&ctx->source);
  515. return NULL;
  516. }
  517. void aio_co_schedule(AioContext *ctx, Coroutine *co)
  518. {
  519. trace_aio_co_schedule(ctx, co);
  520. const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL,
  521. __func__);
  522. if (scheduled) {
  523. fprintf(stderr,
  524. "%s: Co-routine was already scheduled in '%s'\n",
  525. __func__, scheduled);
  526. abort();
  527. }
  528. /* The coroutine might run and release the last ctx reference before we
  529. * invoke qemu_bh_schedule(). Take a reference to keep ctx alive until
  530. * we're done.
  531. */
  532. aio_context_ref(ctx);
  533. QSLIST_INSERT_HEAD_ATOMIC(&ctx->scheduled_coroutines,
  534. co, co_scheduled_next);
  535. qemu_bh_schedule(ctx->co_schedule_bh);
  536. aio_context_unref(ctx);
  537. }
  538. typedef struct AioCoRescheduleSelf {
  539. Coroutine *co;
  540. AioContext *new_ctx;
  541. } AioCoRescheduleSelf;
  542. static void aio_co_reschedule_self_bh(void *opaque)
  543. {
  544. AioCoRescheduleSelf *data = opaque;
  545. aio_co_schedule(data->new_ctx, data->co);
  546. }
  547. void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx)
  548. {
  549. AioContext *old_ctx = qemu_get_current_aio_context();
  550. if (old_ctx != new_ctx) {
  551. AioCoRescheduleSelf data = {
  552. .co = qemu_coroutine_self(),
  553. .new_ctx = new_ctx,
  554. };
  555. /*
  556. * We can't directly schedule the coroutine in the target context
  557. * because this would be racy: The other thread could try to enter the
  558. * coroutine before it has yielded in this one.
  559. */
  560. aio_bh_schedule_oneshot(old_ctx, aio_co_reschedule_self_bh, &data);
  561. qemu_coroutine_yield();
  562. }
  563. }
  564. void aio_co_wake(Coroutine *co)
  565. {
  566. AioContext *ctx;
  567. /* Read coroutine before co->ctx. Matches smp_wmb in
  568. * qemu_coroutine_enter.
  569. */
  570. smp_read_barrier_depends();
  571. ctx = qatomic_read(&co->ctx);
  572. aio_co_enter(ctx, co);
  573. }
  574. void aio_co_enter(AioContext *ctx, Coroutine *co)
  575. {
  576. if (ctx != qemu_get_current_aio_context()) {
  577. aio_co_schedule(ctx, co);
  578. return;
  579. }
  580. if (qemu_in_coroutine()) {
  581. Coroutine *self = qemu_coroutine_self();
  582. assert(self != co);
  583. QSIMPLEQ_INSERT_TAIL(&self->co_queue_wakeup, co, co_queue_next);
  584. } else {
  585. aio_context_acquire(ctx);
  586. qemu_aio_coroutine_enter(ctx, co);
  587. aio_context_release(ctx);
  588. }
  589. }
  590. void aio_context_ref(AioContext *ctx)
  591. {
  592. g_source_ref(&ctx->source);
  593. }
  594. void aio_context_unref(AioContext *ctx)
  595. {
  596. g_source_unref(&ctx->source);
  597. }
  598. void aio_context_acquire(AioContext *ctx)
  599. {
  600. qemu_rec_mutex_lock(&ctx->lock);
  601. }
  602. void aio_context_release(AioContext *ctx)
  603. {
  604. qemu_rec_mutex_unlock(&ctx->lock);
  605. }
  606. QEMU_DEFINE_STATIC_CO_TLS(AioContext *, my_aiocontext)
  607. AioContext *qemu_get_current_aio_context(void)
  608. {
  609. AioContext *ctx = get_my_aiocontext();
  610. if (ctx) {
  611. return ctx;
  612. }
  613. if (qemu_mutex_iothread_locked()) {
  614. /* Possibly in a vCPU thread. */
  615. return qemu_get_aio_context();
  616. }
  617. return NULL;
  618. }
  619. void qemu_set_current_aio_context(AioContext *ctx)
  620. {
  621. assert(!get_my_aiocontext());
  622. set_my_aiocontext(ctx);
  623. }
  624. void aio_context_set_thread_pool_params(AioContext *ctx, int64_t min,
  625. int64_t max, Error **errp)
  626. {
  627. if (min > max || !max || min > INT_MAX || max > INT_MAX) {
  628. error_setg(errp, "bad thread-pool-min/thread-pool-max values");
  629. return;
  630. }
  631. ctx->thread_pool_min = min;
  632. ctx->thread_pool_max = max;
  633. if (ctx->thread_pool) {
  634. thread_pool_update_params(ctx->thread_pool, ctx);
  635. }
  636. }