qemu/include/qemu
Stefan Hajnoczi 06e0f098d6 io: follow coroutine AioContext in qio_channel_yield()
The ongoing QEMU multi-queue block layer effort makes it possible for multiple
threads to process I/O in parallel. The nbd block driver is not compatible with
the multi-queue block layer yet because QIOChannel cannot be used easily from
coroutines running in multiple threads. This series changes the QIOChannel API
to make that possible.

In the current API, calling qio_channel_attach_aio_context() sets the
AioContext where qio_channel_yield() installs an fd handler prior to yielding:

  qio_channel_attach_aio_context(ioc, my_ctx);
  ...
  qio_channel_yield(ioc); // my_ctx is used here
  ...
  qio_channel_detach_aio_context(ioc);

This API design has limitations: reading and writing must be done in the same
AioContext and moving between AioContexts involves a cumbersome sequence of API
calls that is not suitable for doing on a per-request basis.

There is no fundamental reason why a QIOChannel needs to run within the
same AioContext every time qio_channel_yield() is called. QIOChannel
only uses the AioContext while inside qio_channel_yield(). The rest of
the time, QIOChannel is independent of any AioContext.

In the new API, qio_channel_yield() queries the AioContext from the current
coroutine using qemu_coroutine_get_aio_context(). There is no need to
explicitly attach/detach AioContexts anymore and
qio_channel_attach_aio_context() and qio_channel_detach_aio_context() are gone.
One coroutine can read from the QIOChannel while another coroutine writes from
a different AioContext.

This API change allows the nbd block driver to use QIOChannel from any thread.
It's important to keep in mind that the block driver already synchronizes
QIOChannel access and ensures that two coroutines never read simultaneously or
write simultaneously.

This patch updates all users of qio_channel_attach_aio_context() to the
new API. Most conversions are simple, but vhost-user-server requires a
new qemu_coroutine_yield() call to quiesce the vu_client_trip()
coroutine when not attached to any AioContext.

While the API is has become simpler, there is one wart: QIOChannel has a
special case for the iohandler AioContext (used for handlers that must not run
in nested event loops). I didn't find an elegant way preserve that behavior, so
I added a new API called qio_channel_set_follow_coroutine_ctx(ioc, true|false)
for opting in to the new AioContext model. By default QIOChannel uses the
iohandler AioHandler. Code that formerly called
qio_channel_attach_aio_context() now calls
qio_channel_set_follow_coroutine_ctx(ioc, true) once after the QIOChannel is
created.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Acked-by: Daniel P. Berrangé <berrange@redhat.com>
Message-ID: <20230830224802.493686-5-stefanha@redhat.com>
[eblake: also fix migration/rdma.c]
Signed-off-by: Eric Blake <eblake@redhat.com>
2023-09-07 20:32:11 -05:00
..
accel.h typedefs: Forward-declare AccelState 2023-01-06 00:50:32 +01:00
async-teardown.h Don't include headers already included by qemu/osdep.h 2023-02-08 07:28:05 +01:00
atomic.h atomics: eliminate mb_read/mb_set 2023-06-06 09:42:14 +02:00
atomic128.h include/qemu: Move CONFIG_ATOMIC128_OPT handling to atomic128.h 2023-05-23 16:51:18 -07:00
base64.h nomaintainer: Fix Lesser GPL version number 2020-11-15 17:04:40 +01:00
bcd.h Clean up decorations and whitespace around header guards 2016-07-12 16:20:46 +02:00
bitmap.h replace TABs with spaces 2023-03-20 12:43:50 +01:00
bitops.h qemu/bitops.h: Limit rotate amounts 2023-05-02 12:36:46 -07:00
bswap.h bswap: Add the ability to store to an unaligned 24 bit field 2023-06-22 18:55:14 -04:00
buffer.h Replace GCC_FMT_ATTR with G_GNUC_PRINTF 2022-03-22 14:40:51 +04:00
cacheflush.h util: Enhance flush_icache_range with separate data pointer 2021-01-07 05:09:41 -10:00
cacheinfo.h include: Move qemu_[id]cache_* declarations to new qemu/cacheinfo.h 2022-02-21 13:30:20 +00:00
clang-tsa.h clang-tsa: Add macros for shared locks 2022-12-15 16:07:43 +01:00
co-shared-resource.h co-shared-resource: protect with a mutex 2021-06-25 14:24:24 +03:00
compiler.h bsd-user: Move PRAGMA_DISABLE_PACKED_WARNING etc to qemu.h 2023-08-30 08:57:08 -06:00
config-file.h qemu-config: Make config_parse_qdict() return bool 2022-12-14 16:19:35 +01:00
coroutine-core.h coroutine: Split qemu/coroutine-core.h off qemu/coroutine.h 2023-01-20 07:21:46 +01:00
coroutine-tls.h tls: add macros for coroutine-safe TLS variables 2022-03-04 18:14:40 +01:00
coroutine.h coroutine: Split qemu/coroutine-core.h off qemu/coroutine.h 2023-01-20 07:21:46 +01:00
coroutine_int.h coroutine: support SafeStack in ucontext backend 2020-06-23 15:46:05 +01:00
cpu-float.h Clean up ill-advised or unusual header guards 2022-05-11 16:50:01 +02:00
cpuid.h tcg/i386: Add have_atomic16 2023-05-16 15:21:39 -07:00
crc-ccitt.h bulk: Do not declare function prototypes using 'extern' keyword 2023-08-31 19:47:43 +02:00
crc32c.h igb: Implement Rx SCTP CSO 2023-05-23 15:20:15 +08:00
ctype.h qemu-common: Move qemu_isalnum() etc. to qemu/ctype.h 2019-06-11 20:22:09 +02:00
cutils.h cutils: Adjust signature of parse_uint[_full] 2023-06-02 12:27:19 -05:00
datadir.h vl: extract softmmu/datadir.c 2020-12-10 12:15:18 -05:00
dbus.h Don't include headers already included by qemu/osdep.h 2023-02-08 07:28:05 +01:00
drm.h Clean up ill-advised or unusual header guards 2019-05-13 08:58:55 +02:00
envlist.h remove unnecessary extern "C" blocks 2023-02-10 14:12:06 +01:00
error-report.h Move error_printf_unless_qmp() with monitor unit 2022-04-21 17:09:09 +04:00
event_notifier.h event_notifier: add event_notifier_get_wfd() 2022-03-06 06:19:47 -05:00
fifo8.h util/fifo8: Fix typo in fifo8_push_all() description 2023-08-31 19:47:43 +02:00
fifo32.h hw: Clean up includes 2016-06-07 18:19:23 +03:00
filemonitor.h nomaintainer: Fix Lesser GPL version number 2020-11-15 17:04:40 +01:00
futex.h futex: add missing header guards 2017-10-16 20:57:13 +03:00
guest-random.h util: Add qemu_guest_getrandom and associated routines 2019-05-22 12:38:54 -04:00
hbitmap.h hbitmap: fix hbitmap_status() return value for first dirty bit case 2023-02-17 14:34:24 +01:00
help-texts.h Update copyright dates to 2023 2023-05-30 15:50:17 +01:00
help_option.h keyval: Parse help options 2020-10-15 16:06:27 +02:00
host-utils.h host-utils: Add muldiv64_round_up 2023-09-06 11:19:33 +02:00
hw-version.h include: Move hardware version declarations to new qemu/hw-version.h 2022-02-21 13:30:20 +00:00
id.h net: Use id_generate() in the network subsystem, too 2021-03-09 21:47:45 +01:00
int128.h meson: Split test for __int128_t type from __int128_t arithmetic 2023-05-30 09:51:11 -07:00
interval-tree.h util: Add interval-tree.c 2022-12-20 17:09:41 -08:00
iov.h util/iov: Remove qemu_iovec_init_extended() 2023-06-05 13:11:24 +02:00
iova-tree.h util: spelling fixes 2023-08-31 19:47:43 +02:00
jhash.h Clean up ill-advised or unusual header guards 2019-05-13 08:58:55 +02:00
job.h job: remove unused functions 2022-10-07 12:11:41 +02:00
keyval.h Clean up ill-advised or unusual header guards 2022-05-11 16:50:01 +02:00
lockable.h coroutine: Split qemu/coroutine-core.h off qemu/coroutine.h 2023-01-20 07:21:46 +01:00
log-for-trace.h util/log: Drop return value from qemu_log 2022-04-20 10:51:11 -07:00
log.h util/log: Add vector registers to log 2023-06-13 17:42:01 +10:00
madvise.h include: Move qemu_madvise() and related #defines to new qemu/madvise.h 2022-02-21 13:30:20 +00:00
main-loop.h async: Add an optional reentrancy guard to the BH API 2023-04-28 11:31:07 +02:00
memalign.h osdep: Move memalign-related functions to their own header 2022-03-07 13:16:49 +00:00
memfd.h linux-user: add memfd_create 2019-09-11 08:46:17 +02:00
mmap-alloc.h util/mmap-alloc: qemu_fd_getfs() 2023-04-24 11:29:00 +02:00
module.h module: add Error arguments to module_load and module_load_qom 2022-11-06 09:48:50 +01:00
mprotect.h include: Move qemu_mprotect_*() to new qemu/mprotect.h 2022-02-21 13:30:20 +00:00
notify.h xen / notify: introduce a new XenWatchList abstraction 2019-09-24 12:18:47 +01:00
nvdimm-utils.h Clean up includes 2020-12-10 17:16:44 +01:00
option.h include: add qemu/keyval.h 2022-04-21 17:03:51 +04:00
option_int.h qemu/queue.h: simplify reverse access to QTAILQ 2019-01-11 15:46:55 +01:00
osdep.h bulk: Do not declare function prototypes using 'extern' keyword 2023-08-31 19:47:43 +02:00
path.h Clean up decorations and whitespace around header guards 2016-07-12 16:20:46 +02:00
plugin-event.h include/qemu: Split out plugin-event.h 2023-03-22 15:06:57 +00:00
plugin-memory.h accel: Replace target_ulong in tlb_*() 2023-06-26 17:32:59 +02:00
plugin.h include/qemu/plugin: Inline qemu_plugin_disable_mem_helpers 2023-03-22 15:06:57 +00:00
pmem.h Clean up decorations and whitespace around header guards 2019-05-13 08:58:55 +02:00
processor.h qemu/processor: Remove unused 'qemu/atomic.h' header 2023-08-31 19:47:43 +02:00
progress_meter.h coroutine: Clean up superfluous inclusion of qemu/lockable.h 2023-01-19 10:18:28 +01:00
qdist.h Include qemu-common.h exactly where needed 2019-06-12 13:20:20 +02:00
qemu-plugin.h include/qemu: add documentation for memory callbacks 2023-03-22 15:06:57 +00:00
qemu-print.h Replace GCC_FMT_ATTR with G_GNUC_PRINTF 2022-03-22 14:40:51 +04:00
qemu-progress.h include: move progress API to qemu-progress.h 2022-04-06 14:31:43 +02:00
qht.h qht: constify qht_statistics_init 2018-09-26 08:55:54 -07:00
qsp.h qsp: Simplify how qsp_report() prints 2019-04-18 22:18:59 +02:00
qtree.h util: import GTree as QTree 2023-03-28 15:23:10 -07:00
queue.h qemu/atomic.h: rename atomic_ to qatomic_ 2020-09-23 16:07:44 +01:00
range.h compiler.h: replace QEMU_WARN_UNUSED_RESULT with G_GNUC_WARN_UNUSED_RESULT 2022-03-22 14:40:51 +04:00
ratelimit.h ratelimit: treat zero speed as unlimited 2021-06-25 14:22:21 +03:00
rcu.h bulk: Do not declare function prototypes using 'extern' keyword 2023-08-31 19:47:43 +02:00
rcu_queue.h remove unnecessary extern "C" blocks 2023-02-10 14:12:06 +01:00
readline.h readline: Extract readline_add_completion_of() from monitor 2023-02-04 07:56:54 +01:00
selfmap.h util/selfmap: Use dev_t and ino_t in MapInfo 2023-09-01 13:34:03 -07:00
seqlock.h qemu/atomic.h: rename atomic_ to qatomic_ 2020-09-23 16:07:44 +01:00
sockets.h osdep: implement qemu_socketpair() for win32 2023-03-13 15:39:31 +04:00
stats64.h stat64: Add stat64_set() operation 2023-04-27 16:39:43 +02:00
sys_membarrier.h bulk: Do not declare function prototypes using 'extern' keyword 2023-08-31 19:47:43 +02:00
systemd.h Normalize header guard symbol definition. 2019-05-13 08:58:55 +02:00
thread-context.h util: Introduce ThreadContext user-creatable object 2022-10-27 11:00:43 +02:00
thread-posix.h thread-posix: implement Semaphore with QemuCond and QemuMutex 2022-04-06 14:31:56 +02:00
thread-win32.h util: Use real functions for thread-posix QemuRecMutex 2021-06-16 15:03:26 +02:00
thread.h util/qemu-thread-posix: use TSA_NO_TSA to suppress clang TSA warnings in FreeBSD 2023-02-17 11:22:19 +01:00
throttle-options.h block: add throttle block filter driver 2017-09-06 10:12:02 +02:00
throttle.h Include qemu-common.h exactly where needed 2019-06-12 13:20:20 +02:00
timed-average.h include: Clean up includes 2016-02-23 12:43:05 +00:00
timer.h accel/tcg: remove CONFIG_PROFILER 2023-06-26 17:33:00 +02:00
transactions.h transactions: Invoke clean() after everything else 2021-11-16 09:43:44 +01:00
tsan.h include/qemu: Added tsan.h for annotations. 2020-06-16 14:49:05 +01:00
typedefs.h accel: Rename HAX 'struct hax_vcpu_state' -> AccelCPUState 2023-06-28 13:55:35 +02:00
unicode.h json: Reject invalid UTF-8 sequences 2018-08-24 20:26:37 +02:00
units.h block: Eliminate the S_1KiB, S_2KiB, ... macros 2019-02-01 13:46:45 +01:00
uri.h bulk: Do not declare function prototypes using 'extern' keyword 2023-08-31 19:47:43 +02:00
userfaultfd.h migration: Clean up includes 2023-02-08 07:27:20 +01:00
uuid.h qemu/uuid: Add UUID static initializer 2023-03-02 19:13:52 -05:00
vfio-helpers.h util/vfio-helpers: Pass Error handle to qemu_vfio_dma_map() 2021-09-07 09:08:24 +01:00
vhost-user-server.h io: follow coroutine AioContext in qio_channel_yield() 2023-09-07 20:32:11 -05:00
win_dump_defs.h include/qemu: add 32-bit Windows dump structures 2022-04-22 13:36:04 +04:00
xattr.h qemu/xattr.h: Exclude <sys/xattr.h> for Windows 2022-12-23 11:48:13 +01:00
xxhash.h accel/tcg: include cs_base in our hash calculations 2023-06-01 11:05:05 -04:00
yank.h util: spelling fixes 2023-08-31 19:47:43 +02:00