qemu/block
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
..
export vhost-user: fully use new backend/frontend naming 2023-06-26 09:50:00 -04:00
monitor meson: Replace softmmu_ss -> system_ss 2023-06-20 10:01:30 +02:00
accounting.c block: add missed block_acct_setup with new block device init procedure 2022-09-30 18:42:34 +02:00
aio_task.c block/aio_task: assert max_busy_tasks is greater than 0 2021-10-05 18:56:41 +02:00
amend.c block: Mark BlockDriver callbacks for amend job GRAPH_RDLOCK 2023-05-10 14:16:54 +02:00
backup.c Block layer patches 2023-02-24 15:09:39 +00:00
blkdebug.c blkdebug: add missing coroutine_fn annotation 2023-04-25 13:17:28 +02:00
blkio.c block/blkio: add more comments on the fd passing handling 2023-08-03 11:28:43 -04:00
blklogwrites.c block: Mark bdrv_co_refresh_total_sectors() and callers GRAPH_RDLOCK 2023-02-23 19:49:33 +01:00
blkreplay.c block: Mark bdrv_co_refresh_total_sectors() and callers GRAPH_RDLOCK 2023-02-23 19:49:33 +01:00
blkverify.c block: Mark bdrv_recurse_can_replace() and callers GRAPH_RDLOCK 2023-05-10 14:16:54 +02:00
block-backend.c block: add blk_io_plug_call() API 2023-06-01 07:34:03 -04:00
block-copy.c block: Mark bdrv_co_pwrite_zeroes() and callers GRAPH_RDLOCK 2023-02-23 19:49:14 +01:00
block-gen.h block-coroutine-wrapper.py: support also basic return types 2022-12-15 16:07:43 +01:00
block-ram-registrar.c block: add BlockRAMRegistrar 2022-10-26 14:56:42 -04:00
bochs.c bochs: mark more functions as coroutine_fns and GRAPH_RDLOCK 2023-06-28 09:46:24 +02:00
cloop.c cloop: mark more functions as coroutine_fns and GRAPH_RDLOCK 2023-06-28 09:46:27 +02:00
commit.c blockjob: Adhere to rate limit even when reentered early 2023-05-19 19:12:12 +02:00
copy-before-write.c copy-before-write: Fix open with child in iothread 2023-05-30 17:29:35 +02:00
copy-before-write.h block/copy-before-write.h: global state API + assertions 2022-03-04 18:18:25 +01:00
copy-on-read.c block: remove has_variable_length from filters 2023-04-11 16:38:56 +02:00
copy-on-read.h Clean up ill-advised or unusual header guards 2022-05-11 16:50:01 +02:00
coroutines.h nbd: Mark nbd_co_do_establish_connection() and callers GRAPH_RDLOCK 2023-05-10 14:16:53 +02:00
create.c block: Call .bdrv_co_create(_opts) unlocked 2023-05-19 19:12:12 +02:00
crypto.c block: Call .bdrv_co_create(_opts) unlocked 2023-05-19 19:12:12 +02:00
crypto.h nomaintainer: Fix Lesser GPL version number 2020-11-15 17:04:40 +01:00
curl.c aio: remove aio_disable_external() API 2023-05-30 17:37:26 +02:00
dirty-bitmap.c block: Mark bdrv_*_dirty_bitmap() and callers GRAPH_RDLOCK 2023-02-23 19:49:32 +01:00
dmg-bz2.c Include qemu-common.h exactly where needed 2019-06-12 13:20:20 +02:00
dmg-lzfse.c block/dmg: Ignore C99 prototype declaration mismatch from <lzfse.h> 2023-03-30 15:03:36 +02:00
dmg.c dmg: mark more functions as coroutine_fns and GRAPH_RDLOCK 2023-06-28 09:46:28 +02:00
dmg.h block/dmg: Declare a type definition for DMG uncompress function 2023-04-24 13:53:44 -04:00
file-posix.c block/file-posix: fix g_file_get_contents return path 2023-07-27 09:46:09 -04:00
file-win32.c thread-pool: avoid passing the pool parameter every time 2023-04-25 13:17:28 +02:00
filter-compress.c block: remove has_variable_length from filters 2023-04-11 16:38:56 +02:00
gluster.c cutils: Adjust signature of parse_uint[_full] 2023-06-02 12:27:19 -05:00
graph-lock.c Revert "graph-lock: Disable locking for now" 2023-06-28 08:46:26 +02:00
io.c block/io: align requests to subcluster_size 2023-08-30 07:39:10 -04:00
io_uring.c block/io_uring: convert to blk_io_plug_call() API 2023-06-01 07:34:03 -04:00
iscsi-opts.c modules: add block module annotations 2021-07-09 18:20:27 +02:00
iscsi.c aio: remove aio_disable_external() API 2023-05-30 17:37:26 +02:00
linux-aio.c block/linux-aio: convert to blk_io_plug_call() API 2023-06-01 07:34:03 -04:00
meson.build meson: Replace softmmu_ss -> system_ss 2023-06-20 10:01:30 +02:00
mirror.c block/io: align requests to subcluster_size 2023-08-30 07:39:10 -04:00
nbd.c io: follow coroutine AioContext in qio_channel_yield() 2023-09-07 20:32:11 -05:00
nfs.c cutils: Adjust signature of parse_uint[_full] 2023-06-02 12:27:19 -05:00
null.c block: Convert bdrv_get_allocated_file_size() to co_wrapper 2023-02-01 16:52:32 +01:00
nvme.c block/nvme: invoke blk_io_plug_call() outside q->lock 2023-07-17 09:17:41 -04:00
parallels-ext.c include/block: Untangle inclusion loops 2023-01-20 07:24:28 +01:00
parallels.c parallels: Add data_off repairing to parallels_open() 2023-09-06 17:36:49 +02:00
parallels.h parallels: Add data_start field to BDRVParallelsState 2023-09-06 17:36:49 +02:00
plug.c block: add blk_io_plug_call() API 2023-06-01 07:34:03 -04:00
preallocate.c block: remove has_variable_length from filters 2023-04-11 16:38:56 +02:00
progress_meter.c coroutine: Clean up superfluous inclusion of qemu/lockable.h 2023-01-19 10:18:28 +01:00
qapi-sysemu.c block: Take main AioContext lock when calling bdrv_open() 2023-05-30 17:21:23 +02:00
qapi.c block: add accounting for zone append operation 2023-05-15 08:18:10 -04:00
qcow.c block: use bdrv_co_debug_event in coroutine context 2023-06-28 09:46:34 +02:00
qcow2-bitmap.c qcow2: mark more functions as coroutine_fns and GRAPH_RDLOCK 2023-06-28 09:46:32 +02:00
qcow2-cache.c include/block: Untangle inclusion loops 2023-01-20 07:24:28 +01:00
qcow2-cluster.c block: use bdrv_co_debug_event in coroutine context 2023-06-28 09:46:34 +02:00
qcow2-refcount.c block: use bdrv_co_debug_event in coroutine context 2023-06-28 09:46:34 +02:00
qcow2-snapshot.c qcow2: mark various functions as coroutine_fn and GRAPH_RDLOCK 2023-04-25 13:17:28 +02:00
qcow2-threads.c thread-pool: avoid passing the pool parameter every time 2023-04-25 13:17:28 +02:00
qcow2.c block: add subcluster_size field to BlockDriverInfo 2023-08-30 07:39:10 -04:00
qcow2.h qcow2: mark more functions as coroutine_fns and GRAPH_RDLOCK 2023-06-28 09:46:32 +02:00
qed-check.c qed: mark more functions as coroutine_fns and GRAPH_RDLOCK 2023-06-28 09:46:20 +02:00
qed-cluster.c qed: protect table cache with CoMutex 2017-07-17 11:34:11 +08:00
qed-l2-cache.c osdep: Move memalign-related functions to their own header 2022-03-07 13:16:49 +00:00
qed-table.c block: use bdrv_co_debug_event in coroutine context 2023-06-28 09:46:34 +02:00
qed.c block: use bdrv_co_debug_event in coroutine context 2023-06-28 09:46:34 +02:00
qed.h block: Mark public read/write functions GRAPH_RDLOCK 2023-02-23 19:49:17 +01:00
quorum.c block: Mark bdrv_recurse_can_replace() and callers GRAPH_RDLOCK 2023-05-10 14:16:54 +02:00
raw-format.c block: use bdrv_co_debug_event in coroutine context 2023-06-28 09:46:34 +02:00
rbd.c block/rbd: Add support for layered encryption 2023-02-23 19:49:35 +01:00
replication.c block: remove has_variable_length from filters 2023-04-11 16:38:56 +02:00
reqlist.c block/reqlist: add reqlist_wait_all() 2022-03-07 09:33:30 +01:00
snapshot-access.c block: Mark preadv_snapshot/snapshot_block_status GRAPH_RDLOCK 2023-02-23 19:49:21 +01:00
snapshot.c block/snapshot: drop indirection around bdrv_snapshot_fallback_ptr 2022-10-27 20:14:11 +02:00
ssh.c aio: remove aio_disable_external() API 2023-05-30 17:37:26 +02:00
stream.c blockjob: Adhere to rate limit even when reentered early 2023-05-19 19:12:12 +02:00
throttle-groups.c block/throttle-groups: throttle_group_co_io_limits_intercept(): 64bit bytes 2021-02-03 08:14:00 -06:00
throttle.c block: Mark bdrv_co_refresh_total_sectors() and callers GRAPH_RDLOCK 2023-02-23 19:49:33 +01:00
trace-events block/io_uring: convert to blk_io_plug_call() API 2023-06-01 07:34:03 -04:00
trace.h trace: switch position of headers to what Meson requires 2020-08-21 06:18:24 -04:00
vdi.c block: Call .bdrv_co_create(_opts) unlocked 2023-05-19 19:12:12 +02:00
vhdx-endian.c Include qemu-common.h exactly where needed 2019-06-12 13:20:20 +02:00
vhdx-log.c vhdx: mark more functions as coroutine_fns and GRAPH_RDLOCK 2023-06-28 09:46:30 +02:00
vhdx.c vhdx: mark more functions as coroutine_fns and GRAPH_RDLOCK 2023-06-28 09:46:30 +02:00
vhdx.h vhdx: mark more functions as coroutine_fns and GRAPH_RDLOCK 2023-06-28 09:46:30 +02:00
vmdk.c block: use bdrv_co_debug_event in coroutine context 2023-06-28 09:46:34 +02:00
vpc.c vpc: mark more functions as coroutine_fns and GRAPH_RDLOCK 2023-06-28 09:46:23 +02:00
vvfat.c vvfat: mark various functions as coroutine_fn 2023-04-25 13:17:28 +02:00
win32-aio.c aio: remove aio_disable_external() API 2023-05-30 17:37:26 +02:00
write-threshold.c include/block: Untangle inclusion loops 2023-01-20 07:24:28 +01:00