BACKPORT: util/async: use qemu_aio_coroutine_enter in co_schedule_bh_cb

AIO Coroutines shouldn't by managed by an AioContext different than the
one assigned when they are created. aio_co_enter avoids entering a
coroutine from a different AioContext, calling aio_co_schedule instead.

Scheduled coroutines are then entered by co_schedule_bh_cb using
qemu_coroutine_enter, which just calls qemu_aio_coroutine_enter with the
current AioContext obtained with qemu_get_current_aio_context.
Eventually, co->ctx will be set to the AioContext passed as an argument
to qemu_aio_coroutine_enter.

This means that, if an IO Thread's AioConext is being processed by the
Main Thread (due to aio_poll being called with a BDS AioContext, as it
happens in AIO_WAIT_WHILE among other places), the AioContext from some
coroutines may be wrongly replaced with the one from the Main Thread.

This is the root cause behind some crashes, mainly triggered by the
drain code at block/io.c. The most common are these abort and failed
assertion:

util/async.c:aio_co_schedule
456     if (scheduled) {
457         fprintf(stderr,
458                 "%s: Co-routine was already scheduled in '%s'\n",
459                 __func__, scheduled);
460         abort();
461     }

util/qemu-coroutine-lock.c:
286     assert(mutex->holder == self);

But it's also known to cause random errors at different locations, and
even SIGSEGV with broken coroutine backtraces.

By using qemu_aio_coroutine_enter directly in co_schedule_bh_cb, we can
pass the correct AioContext as an argument, making sure co->ctx is not
wrongly altered.

Change-Id: I81c72900f3c82d58513c3a360be9a53ed72b35af
Signed-off-by: Sergio Lopez <slp@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1 file changed
tree: 8aaed64fedfe5003d2325a0f2e9bb0152cebc957
  1. .vscode/
  2. accel/
  3. android/
  4. android-qemu2-glue/
  5. audio/
  6. backends/
  7. block/
  8. bsd-user/
  9. chardev/
  10. contrib/
  11. crypto/
  12. default-configs/
  13. disas/
  14. docs/
  15. fpu/
  16. fsdev/
  17. gdb-xml/
  18. hw/
  19. include/
  20. io/
  21. libdecnumber/
  22. linux-headers/
  23. linux-user/
  24. migration/
  25. nbd/
  26. net/
  27. pc-bios/
  28. po/
  29. qapi/
  30. qemu2-auto-generated/
  31. qga/
  32. qobject/
  33. qom/
  34. replay/
  35. roms/
  36. scripts/
  37. scsi/
  38. slirp/
  39. stubs/
  40. target/
  41. tcg/
  42. tests/
  43. trace/
  44. ui/
  45. util/
  46. .dir-locals.el
  47. .editorconfig
  48. .exrc
  49. .gdbinit
  50. .gitignore
  51. .gitmodules
  52. .gitpublish
  53. .mailmap
  54. .shippable.yml
  55. .travis.yml
  56. android-info.txt
  57. arch_init.c
  58. balloon.c
  59. block.c
  60. blockdev-nbd.c
  61. blockdev.c
  62. blockjob.c
  63. bootdevice.c
  64. bt-host.c
  65. bt-vhci.c
  66. build.gradle
  67. Changelog
  68. cmake-main.darwin-x86_64.inc
  69. cmake-main.linux-x86_64.inc
  70. cmake-main.windows-x86.inc
  71. cmake-main.windows-x86_64.inc
  72. CMakeLists.txt
  73. CODING_STYLE
  74. configure
  75. COPYING
  76. COPYING.LIB
  77. COPYING.PYTHON
  78. cpus-common.c
  79. cpus.c
  80. device-hotplug.c
  81. device_tree.c
  82. disas.c
  83. dma-helpers.c
  84. dump.c
  85. exec.c
  86. gdbstub.c
  87. GNUmakefile
  88. HACKING
  89. hmp-commands-info.hx
  90. hmp-commands.hx
  91. hmp.c
  92. hmp.h
  93. hvf-stub.c
  94. ioport.c
  95. iothread.c
  96. LICENSE
  97. mac.source.properties
  98. MAINTAINERS
  99. Makefile
  100. Makefile.objs
  101. Makefile.target
  102. memory.c
  103. memory_ldst.inc.c
  104. memory_mapping.c
  105. module-common.c
  106. MODULE_LICENSE_GPL
  107. monitor.c
  108. NOTICE
  109. numa.c
  110. os-posix.c
  111. os-win32.c
  112. qdev-monitor.c
  113. qdict-test-data.txt
  114. qemu-bridge-helper.c
  115. qemu-char.c
  116. qemu-doc.texi
  117. qemu-ga.texi
  118. qemu-img-cmds.hx
  119. qemu-img.c
  120. qemu-img.texi
  121. qemu-io-cmds.c
  122. qemu-io.c
  123. qemu-keymap.c
  124. qemu-nbd.c
  125. qemu-nbd.texi
  126. qemu-option-trace.texi
  127. qemu-options-wrapper.h
  128. qemu-options.def
  129. qemu-options.h
  130. qemu-options.hx
  131. qemu-seccomp.c
  132. qemu-tech.texi
  133. qemu.nsi
  134. qemu.sasl
  135. qmp.c
  136. qtest.c
  137. README
  138. README.ANDROID
  139. replication.c
  140. replication.h
  141. rules.mak
  142. source.properties
  143. thunk.c
  144. tpm.c
  145. trace-events
  146. VERSION
  147. version.rc
  148. vl.c
  149. whpx-stub.c
  150. win.source.properties