async.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. QSIMPLEQ_INSERT_TAIL(&ctx->bh_slice_list, &slice, next);
  147. while ((s = QSIMPLEQ_FIRST(&ctx->bh_slice_list))) {
  148. QEMUBH *bh;
  149. unsigned flags;
  150. bh = aio_bh_dequeue(&s->bh_list, &flags);
  151. if (!bh) {
  152. QSIMPLEQ_REMOVE_HEAD(&ctx->bh_slice_list, next);
  153. continue;
  154. }
  155. if ((flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
  156. /* Idle BHs don't count as progress */
  157. if (!(flags & BH_IDLE)) {
  158. ret = 1;
  159. }
  160. aio_bh_call(bh);
  161. }
  162. if (flags & (BH_DELETED | BH_ONESHOT)) {
  163. g_free(bh);
  164. }
  165. }
  166. return ret;
  167. }
  168. void qemu_bh_schedule_idle(QEMUBH *bh)
  169. {
  170. aio_bh_enqueue(bh, BH_SCHEDULED | BH_IDLE);
  171. }
  172. void qemu_bh_schedule(QEMUBH *bh)
  173. {
  174. aio_bh_enqueue(bh, BH_SCHEDULED);
  175. }
  176. /* This func is async.
  177. */
  178. void qemu_bh_cancel(QEMUBH *bh)
  179. {
  180. qatomic_and(&bh->flags, ~BH_SCHEDULED);
  181. }
  182. /* This func is async.The bottom half will do the delete action at the finial
  183. * end.
  184. */
  185. void qemu_bh_delete(QEMUBH *bh)
  186. {
  187. aio_bh_enqueue(bh, BH_DELETED);
  188. }
  189. static int64_t aio_compute_bh_timeout(BHList *head, int timeout)
  190. {
  191. QEMUBH *bh;
  192. QSLIST_FOREACH_RCU(bh, head, next) {
  193. if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
  194. if (bh->flags & BH_IDLE) {
  195. /* idle bottom halves will be polled at least
  196. * every 10ms */
  197. timeout = 10000000;
  198. } else {
  199. /* non-idle bottom halves will be executed
  200. * immediately */
  201. return 0;
  202. }
  203. }
  204. }
  205. return timeout;
  206. }
  207. int64_t
  208. aio_compute_timeout(AioContext *ctx)
  209. {
  210. BHListSlice *s;
  211. int64_t deadline;
  212. int timeout = -1;
  213. timeout = aio_compute_bh_timeout(&ctx->bh_list, timeout);
  214. if (timeout == 0) {
  215. return 0;
  216. }
  217. QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) {
  218. timeout = aio_compute_bh_timeout(&s->bh_list, timeout);
  219. if (timeout == 0) {
  220. return 0;
  221. }
  222. }
  223. deadline = timerlistgroup_deadline_ns(&ctx->tlg);
  224. if (deadline == 0) {
  225. return 0;
  226. } else {
  227. return qemu_soonest_timeout(timeout, deadline);
  228. }
  229. }
  230. static gboolean
  231. aio_ctx_prepare(GSource *source, gint *timeout)
  232. {
  233. AioContext *ctx = (AioContext *) source;
  234. qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) | 1);
  235. /*
  236. * Write ctx->notify_me before computing the timeout
  237. * (reading bottom half flags, etc.). Pairs with
  238. * smp_mb in aio_notify().
  239. */
  240. smp_mb();
  241. /* We assume there is no timeout already supplied */
  242. *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
  243. if (aio_prepare(ctx)) {
  244. *timeout = 0;
  245. }
  246. return *timeout == 0;
  247. }
  248. static gboolean
  249. aio_ctx_check(GSource *source)
  250. {
  251. AioContext *ctx = (AioContext *) source;
  252. QEMUBH *bh;
  253. BHListSlice *s;
  254. /* Finish computing the timeout before clearing the flag. */
  255. qatomic_store_release(&ctx->notify_me, qatomic_read(&ctx->notify_me) & ~1);
  256. aio_notify_accept(ctx);
  257. QSLIST_FOREACH_RCU(bh, &ctx->bh_list, next) {
  258. if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
  259. return true;
  260. }
  261. }
  262. QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) {
  263. QSLIST_FOREACH_RCU(bh, &s->bh_list, next) {
  264. if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
  265. return true;
  266. }
  267. }
  268. }
  269. return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
  270. }
  271. static gboolean
  272. aio_ctx_dispatch(GSource *source,
  273. GSourceFunc callback,
  274. gpointer user_data)
  275. {
  276. AioContext *ctx = (AioContext *) source;
  277. assert(callback == NULL);
  278. aio_dispatch(ctx);
  279. return true;
  280. }
  281. static void
  282. aio_ctx_finalize(GSource *source)
  283. {
  284. AioContext *ctx = (AioContext *) source;
  285. QEMUBH *bh;
  286. unsigned flags;
  287. thread_pool_free(ctx->thread_pool);
  288. #ifdef CONFIG_LINUX_AIO
  289. if (ctx->linux_aio) {
  290. laio_detach_aio_context(ctx->linux_aio, ctx);
  291. laio_cleanup(ctx->linux_aio);
  292. ctx->linux_aio = NULL;
  293. }
  294. #endif
  295. #ifdef CONFIG_LINUX_IO_URING
  296. if (ctx->linux_io_uring) {
  297. luring_detach_aio_context(ctx->linux_io_uring, ctx);
  298. luring_cleanup(ctx->linux_io_uring);
  299. ctx->linux_io_uring = NULL;
  300. }
  301. #endif
  302. assert(QSLIST_EMPTY(&ctx->scheduled_coroutines));
  303. qemu_bh_delete(ctx->co_schedule_bh);
  304. /* There must be no aio_bh_poll() calls going on */
  305. assert(QSIMPLEQ_EMPTY(&ctx->bh_slice_list));
  306. while ((bh = aio_bh_dequeue(&ctx->bh_list, &flags))) {
  307. /*
  308. * qemu_bh_delete() must have been called on BHs in this AioContext. In
  309. * many cases memory leaks, hangs, or inconsistent state occur when a
  310. * BH is leaked because something still expects it to run.
  311. *
  312. * If you hit this, fix the lifecycle of the BH so that
  313. * qemu_bh_delete() and any associated cleanup is called before the
  314. * AioContext is finalized.
  315. */
  316. if (unlikely(!(flags & BH_DELETED))) {
  317. fprintf(stderr, "%s: BH '%s' leaked, aborting...\n",
  318. __func__, bh->name);
  319. abort();
  320. }
  321. g_free(bh);
  322. }
  323. aio_set_event_notifier(ctx, &ctx->notifier, false, NULL, NULL, NULL);
  324. event_notifier_cleanup(&ctx->notifier);
  325. qemu_rec_mutex_destroy(&ctx->lock);
  326. qemu_lockcnt_destroy(&ctx->list_lock);
  327. timerlistgroup_deinit(&ctx->tlg);
  328. unregister_aiocontext(ctx);
  329. aio_context_destroy(ctx);
  330. }
  331. static GSourceFuncs aio_source_funcs = {
  332. aio_ctx_prepare,
  333. aio_ctx_check,
  334. aio_ctx_dispatch,
  335. aio_ctx_finalize
  336. };
  337. GSource *aio_get_g_source(AioContext *ctx)
  338. {
  339. aio_context_use_g_source(ctx);
  340. g_source_ref(&ctx->source);
  341. return &ctx->source;
  342. }
  343. ThreadPool *aio_get_thread_pool(AioContext *ctx)
  344. {
  345. if (!ctx->thread_pool) {
  346. ctx->thread_pool = thread_pool_new(ctx);
  347. }
  348. return ctx->thread_pool;
  349. }
  350. #ifdef CONFIG_LINUX_AIO
  351. LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp)
  352. {
  353. if (!ctx->linux_aio) {
  354. ctx->linux_aio = laio_init(errp);
  355. if (ctx->linux_aio) {
  356. laio_attach_aio_context(ctx->linux_aio, ctx);
  357. }
  358. }
  359. return ctx->linux_aio;
  360. }
  361. LinuxAioState *aio_get_linux_aio(AioContext *ctx)
  362. {
  363. assert(ctx->linux_aio);
  364. return ctx->linux_aio;
  365. }
  366. #endif
  367. #ifdef CONFIG_LINUX_IO_URING
  368. LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp)
  369. {
  370. if (ctx->linux_io_uring) {
  371. return ctx->linux_io_uring;
  372. }
  373. ctx->linux_io_uring = luring_init(errp);
  374. if (!ctx->linux_io_uring) {
  375. return NULL;
  376. }
  377. luring_attach_aio_context(ctx->linux_io_uring, ctx);
  378. return ctx->linux_io_uring;
  379. }
  380. LuringState *aio_get_linux_io_uring(AioContext *ctx)
  381. {
  382. assert(ctx->linux_io_uring);
  383. return ctx->linux_io_uring;
  384. }
  385. #endif
  386. void aio_notify(AioContext *ctx)
  387. {
  388. /*
  389. * Write e.g. ctx->bh_list before writing ctx->notified. Pairs with
  390. * smp_mb() in aio_notify_accept().
  391. */
  392. smp_wmb();
  393. qatomic_set(&ctx->notified, true);
  394. /*
  395. * Write ctx->notified (and also ctx->bh_list) before reading ctx->notify_me.
  396. * Pairs with smp_mb() in aio_ctx_prepare or aio_poll.
  397. */
  398. smp_mb();
  399. if (qatomic_read(&ctx->notify_me)) {
  400. event_notifier_set(&ctx->notifier);
  401. }
  402. }
  403. void aio_notify_accept(AioContext *ctx)
  404. {
  405. qatomic_set(&ctx->notified, false);
  406. /*
  407. * Order reads of ctx->notified (in aio_context_notifier_poll()) and the
  408. * above clearing of ctx->notified before reads of e.g. bh->flags. Pairs
  409. * with smp_wmb() in aio_notify.
  410. */
  411. smp_mb();
  412. }
  413. static void aio_timerlist_notify(void *opaque, QEMUClockType type)
  414. {
  415. aio_notify(opaque);
  416. }
  417. static void aio_context_notifier_cb(EventNotifier *e)
  418. {
  419. AioContext *ctx = container_of(e, AioContext, notifier);
  420. event_notifier_test_and_clear(&ctx->notifier);
  421. }
  422. /* Returns true if aio_notify() was called (e.g. a BH was scheduled) */
  423. static bool aio_context_notifier_poll(void *opaque)
  424. {
  425. EventNotifier *e = opaque;
  426. AioContext *ctx = container_of(e, AioContext, notifier);
  427. /*
  428. * No need for load-acquire because we just want to kick the
  429. * event loop. aio_notify_accept() takes care of synchronizing
  430. * the event loop with the producers.
  431. */
  432. return qatomic_read(&ctx->notified);
  433. }
  434. static void aio_context_notifier_poll_ready(EventNotifier *e)
  435. {
  436. /* Do nothing, we just wanted to kick the event loop */
  437. }
  438. static void co_schedule_bh_cb(void *opaque)
  439. {
  440. AioContext *ctx = opaque;
  441. QSLIST_HEAD(, Coroutine) straight, reversed;
  442. QSLIST_MOVE_ATOMIC(&reversed, &ctx->scheduled_coroutines);
  443. QSLIST_INIT(&straight);
  444. while (!QSLIST_EMPTY(&reversed)) {
  445. Coroutine *co = QSLIST_FIRST(&reversed);
  446. QSLIST_REMOVE_HEAD(&reversed, co_scheduled_next);
  447. QSLIST_INSERT_HEAD(&straight, co, co_scheduled_next);
  448. }
  449. while (!QSLIST_EMPTY(&straight)) {
  450. Coroutine *co = QSLIST_FIRST(&straight);
  451. QSLIST_REMOVE_HEAD(&straight, co_scheduled_next);
  452. trace_aio_co_schedule_bh_cb(ctx, co);
  453. aio_context_acquire(ctx);
  454. /* Protected by write barrier in qemu_aio_coroutine_enter */
  455. qatomic_set(&co->scheduled, NULL);
  456. qemu_aio_coroutine_enter(ctx, co);
  457. aio_context_release(ctx);
  458. }
  459. }
  460. AioContext *aio_context_new(Error **errp)
  461. {
  462. int ret;
  463. AioContext *ctx;
  464. ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
  465. QSLIST_INIT(&ctx->bh_list);
  466. QSIMPLEQ_INIT(&ctx->bh_slice_list);
  467. aio_context_setup(ctx);
  468. ret = event_notifier_init(&ctx->notifier, false);
  469. if (ret < 0) {
  470. error_setg_errno(errp, -ret, "Failed to initialize event notifier");
  471. goto fail;
  472. }
  473. g_source_set_can_recurse(&ctx->source, true);
  474. qemu_lockcnt_init(&ctx->list_lock);
  475. ctx->co_schedule_bh = aio_bh_new(ctx, co_schedule_bh_cb, ctx);
  476. QSLIST_INIT(&ctx->scheduled_coroutines);
  477. aio_set_event_notifier(ctx, &ctx->notifier,
  478. false,
  479. aio_context_notifier_cb,
  480. aio_context_notifier_poll,
  481. aio_context_notifier_poll_ready);
  482. #ifdef CONFIG_LINUX_AIO
  483. ctx->linux_aio = NULL;
  484. #endif
  485. #ifdef CONFIG_LINUX_IO_URING
  486. ctx->linux_io_uring = NULL;
  487. #endif
  488. ctx->thread_pool = NULL;
  489. qemu_rec_mutex_init(&ctx->lock);
  490. timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
  491. ctx->poll_ns = 0;
  492. ctx->poll_max_ns = 0;
  493. ctx->poll_grow = 0;
  494. ctx->poll_shrink = 0;
  495. ctx->aio_max_batch = 0;
  496. ctx->thread_pool_min = 0;
  497. ctx->thread_pool_max = THREAD_POOL_MAX_THREADS_DEFAULT;
  498. register_aiocontext(ctx);
  499. return ctx;
  500. fail:
  501. g_source_destroy(&ctx->source);
  502. return NULL;
  503. }
  504. void aio_co_schedule(AioContext *ctx, Coroutine *co)
  505. {
  506. trace_aio_co_schedule(ctx, co);
  507. const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL,
  508. __func__);
  509. if (scheduled) {
  510. fprintf(stderr,
  511. "%s: Co-routine was already scheduled in '%s'\n",
  512. __func__, scheduled);
  513. abort();
  514. }
  515. /* The coroutine might run and release the last ctx reference before we
  516. * invoke qemu_bh_schedule(). Take a reference to keep ctx alive until
  517. * we're done.
  518. */
  519. aio_context_ref(ctx);
  520. QSLIST_INSERT_HEAD_ATOMIC(&ctx->scheduled_coroutines,
  521. co, co_scheduled_next);
  522. qemu_bh_schedule(ctx->co_schedule_bh);
  523. aio_context_unref(ctx);
  524. }
  525. typedef struct AioCoRescheduleSelf {
  526. Coroutine *co;
  527. AioContext *new_ctx;
  528. } AioCoRescheduleSelf;
  529. static void aio_co_reschedule_self_bh(void *opaque)
  530. {
  531. AioCoRescheduleSelf *data = opaque;
  532. aio_co_schedule(data->new_ctx, data->co);
  533. }
  534. void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx)
  535. {
  536. AioContext *old_ctx = qemu_get_current_aio_context();
  537. if (old_ctx != new_ctx) {
  538. AioCoRescheduleSelf data = {
  539. .co = qemu_coroutine_self(),
  540. .new_ctx = new_ctx,
  541. };
  542. /*
  543. * We can't directly schedule the coroutine in the target context
  544. * because this would be racy: The other thread could try to enter the
  545. * coroutine before it has yielded in this one.
  546. */
  547. aio_bh_schedule_oneshot(old_ctx, aio_co_reschedule_self_bh, &data);
  548. qemu_coroutine_yield();
  549. }
  550. }
  551. void aio_co_wake(Coroutine *co)
  552. {
  553. AioContext *ctx;
  554. /* Read coroutine before co->ctx. Matches smp_wmb in
  555. * qemu_coroutine_enter.
  556. */
  557. smp_read_barrier_depends();
  558. ctx = qatomic_read(&co->ctx);
  559. aio_co_enter(ctx, co);
  560. }
  561. void aio_co_enter(AioContext *ctx, Coroutine *co)
  562. {
  563. if (ctx != qemu_get_current_aio_context()) {
  564. aio_co_schedule(ctx, co);
  565. return;
  566. }
  567. if (qemu_in_coroutine()) {
  568. Coroutine *self = qemu_coroutine_self();
  569. assert(self != co);
  570. QSIMPLEQ_INSERT_TAIL(&self->co_queue_wakeup, co, co_queue_next);
  571. } else {
  572. aio_context_acquire(ctx);
  573. qemu_aio_coroutine_enter(ctx, co);
  574. aio_context_release(ctx);
  575. }
  576. }
  577. void aio_context_ref(AioContext *ctx)
  578. {
  579. g_source_ref(&ctx->source);
  580. }
  581. void aio_context_unref(AioContext *ctx)
  582. {
  583. g_source_unref(&ctx->source);
  584. }
  585. void aio_context_acquire(AioContext *ctx)
  586. {
  587. qemu_rec_mutex_lock(&ctx->lock);
  588. }
  589. void aio_context_release(AioContext *ctx)
  590. {
  591. qemu_rec_mutex_unlock(&ctx->lock);
  592. }
  593. QEMU_DEFINE_STATIC_CO_TLS(AioContext *, my_aiocontext)
  594. AioContext *qemu_get_current_aio_context(void)
  595. {
  596. AioContext *ctx = get_my_aiocontext();
  597. if (ctx) {
  598. return ctx;
  599. }
  600. if (qemu_mutex_iothread_locked()) {
  601. /* Possibly in a vCPU thread. */
  602. return qemu_get_aio_context();
  603. }
  604. return NULL;
  605. }
  606. void qemu_set_current_aio_context(AioContext *ctx)
  607. {
  608. assert(!get_my_aiocontext());
  609. set_my_aiocontext(ctx);
  610. }
  611. void aio_context_set_thread_pool_params(AioContext *ctx, int64_t min,
  612. int64_t max, Error **errp)
  613. {
  614. if (min > max || !max || min > INT_MAX || max > INT_MAX) {
  615. error_setg(errp, "bad thread-pool-min/thread-pool-max values");
  616. return;
  617. }
  618. ctx->thread_pool_min = min;
  619. ctx->thread_pool_max = max;
  620. if (ctx->thread_pool) {
  621. thread_pool_update_params(ctx->thread_pool, ctx);
  622. }
  623. }