Multifd 🔀 device state transfer support with VFIO consumer
v5 cover Multifd 🔀 device state transfer support with VFIO consumer
From: "Maciej S. Szmigiero" <[email protected]>
This is an updated v5 patch series of the v4 series located here:
https://lore.kernel.org/qemu-devel/[email protected]/
Changes from v3:
* Use "unsigned long" for VFIO bytes transferred counter to fixes test issues on 32-bit platforms.
* Instead of adding BQL holding around qemu_loadvm_state_main() in postcopy_ram_listen_thread() just add a TODO comment there.
* Drop patches for gracefully handling improperly terminated TLS session and rely on recent Fabiano's changes to handle this case instead.
* Adapt how MULTIFD_FLAG_DEVICE_STATE value is defined for consistency with neighboring flags.
* Return Error type and use migrate_set_error() to set it also for save threads, much like it was previously done for load threads.
* Export SaveLiveCompletePrecopyThreadData and make save threads take it directly instead of passing individual parameters stored there to a thread entry point.
* Group all multifd device state save variables in multifd_send_device_state variable allocated on demand instead of using multifd-device-state.c globals.
* Export save threads abort flag via multifd_device_state_save_thread_should_exit() getter function rather than passing it directly.
* Separate VFIO multifd stuff into migration-multifd.{c,h} files. Needed moving VFIO migration channel flags to vfio-common.h header file.
* Move x-migration-load-config-after-iter feature to a separate patch near the end of the series.
* Move x-migration-max-queued-buffers feature to a yet another separate patch near the end of the series.
* Introduce save/load common vfio_multifd_transfer_setup() and a getter function for multifd transfer switch called vfio_multifd_transfer_enabled().
* Move introduction of VFIOMigration->multifd_transfer and VFIODevice->migration_multifd_transfer into the very patch that introduces the x-migration-multifd-transfer property.
* Introduce vfio_multifd_cleanup() for clearing migration->multifd.
* Split making x-migration-multifd-transfer mutable at runtime into a separate patch.
* Rename vfio_switchover_start() to vfio_multifd_switchover_start() and add a new vfio_switchover_start() in migration.c that calls that vfio_multifd_switchover_start().
* Introduce VFIO_DEVICE_STATE_PACKET_VER_CURRENT.
* Don't print UINT32_MAX value in vfio_load_state_buffer().
* Introduce a new routine for parsing VFIO_MIG_FLAG_DEV_CONFIG_LOAD_READY.
* Add an Error parameter to vfio_save_complete_precopy_thread_config_state() and propagate it from vfio_save_device_config_state() function that it calls.
* Update the VFIO developer doc in docs/devel/migration/vfio.rst.
* Add comments about how VFIO multifd threads are launched and from where this happens. Also add comments how they are terminated.
* Other small changes, like renamed functions, added review tags, code formatting, rebased on top of the latest QEMU git master, etc.
========================================================================
This patch set is targeting QEMU 10.0.
========================================================================
Maciej S. Szmigiero (35):
migration: Clarify that {load,save}_cleanup handlers can run without
setup
thread-pool: Remove thread_pool_submit() function
thread-pool: Rename AIO pool functions to *_aio() and data types to
*Aio
thread-pool: Implement generic (non-AIO) pool support
migration: Add MIG_CMD_SWITCHOVER_START and its load handler
migration: Add qemu_loadvm_load_state_buffer() and its handler
migration: postcopy_ram_listen_thread() should take BQL for some calls
error: define g_autoptr() cleanup function for the Error type
migration: Add thread pool of optional load threads migration/multifd: Split packet into header and RAM data migration/multifd: Device state transfer support - receive side migration/multifd: Make multifd_send() thread safe migration/multifd: Add an explicit MultiFDSendData destructor migration/multifd: Device state transfer support - send side migration/multifd: Add multifd_device_state_supported()
migration: Add save_live_complete_precopy_thread handler vfio/migration: Add load_device_config_state_start trace event vfio/migration: Convert bytes_transferred counter to atomic vfio/migration: Add vfio_add_bytes_transferred() vfio/migration: Move migration channel flags to vfio-common.h header
file
vfio/migration: Multifd device state transfer support - basic types vfio/migration: Multifd device state transfer support -
VFIOStateBuffer(s)
vfio/migration: Multifd device state transfer - add support checking
function
vfio/migration: Multifd device state transfer support - receive
init/cleanup
vfio/migration: Multifd device state transfer support - received
buffers queuing
vfio/migration: Multifd device state transfer support - load thread vfio/migration: Multifd device state transfer support - config loading
support
migration/qemu-file: Define g_autoptr() cleanup function for QEMUFile vfio/migration: Multifd device state transfer support - send side vfio/migration: Add x-migration-multifd-transfer VFIO property vfio/migration: Make x-migration-multifd-transfer VFIO property
mutable
hw/core/machine: Add compat for x-migration-multifd-transfer VFIO
property
vfio/migration: Max in-flight VFIO device state buffer count limit vfio/migration: Add x-migration-load-config-after-iter VFIO property vfio/migration: Update VFIO migration documentation
Peter Xu (1):
migration/multifd: Make MultiFDSendData a struct
docs/devel/migration/vfio.rst | 80 ++- hw/core/machine.c | 2 + hw/vfio/meson.build | 1 + hw/vfio/migration-multifd.c | 757 +++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 38 ++ hw/vfio/migration.c | 119 +++-- hw/vfio/pci.c | 14 + hw/vfio/trace-events | 11 +- include/block/aio.h | 8 +- include/block/thread-pool.h | 62 ++- include/hw/vfio/vfio-common.h | 36 ++ include/migration/client-options.h | 4 + include/migration/misc.h | 25 + include/migration/register.h | 52 +- include/qapi/error.h | 2 + include/qemu/typedefs.h | 5 + migration/colo.c | 3 + migration/meson.build | 1 + migration/migration-hmp-cmds.c | 2 + migration/migration.c | 4 +- migration/migration.h | 7 + migration/multifd-device-state.c | 202 ++++++++ migration/multifd-nocomp.c | 30 +- migration/multifd.c | 246 ++++++++-- migration/multifd.h | 74 ++- migration/options.c | 9 + migration/qemu-file.h | 2 + migration/savevm.c | 190 +++++++- migration/savevm.h | 6 +- migration/trace-events | 1 + scripts/analyze-migration.py | 11 + tests/unit/test-thread-pool.c | 6 +- util/async.c | 6 +- util/thread-pool.c | 184 +++++-- util/trace-events | 6 +- 35 files changed, 2039 insertions(+), 167 deletions(-) create mode 100644 hw/vfio/migration-multifd.c create mode 100644 hw/vfio/migration-multifd.h create mode 100644 migration/multifd-device-state.c
PATCH v5 1/36 migration: Clarify that {load, save}_cleanup handlers can run without setup
From: "Maciej S. Szmigiero" <[email protected]>
It's possible for {load,save}_cleanup SaveVMHandlers to get called without the corresponding {load,save}_setup handler being called first.
One such example is if {load,save}_setup handler of a proceeding device returns error.
In this case the migration core cleanup code will call all corresponding cleanup handlers, even for these devices which haven't had its setup handler called.
Since this behavior can generate some surprises let's clearly document it in these SaveVMHandlers description.
--- include/migration/register.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
include/migration/register.h +5 -1
@@ -69,7 +69,9 @@ typedef struct SaveVMHandlers {/** * @save_cleanup * -* Uninitializes the data structures on the source +* Uninitializes the data structures on the source. +* Note that this handler can be called even if save_setup +* wasn't called earlier. * * @opaque: data pointer passed to register_savevm_live() */
@@ -244,6 +246,8 @@ typedef struct SaveVMHandlers {* @load_cleanup * * Uninitializes the data structures on the destination. +* Note that this handler can be called even if load_setup +* wasn't called earlier. * * @opaque: data pointer passed to register_savevm_live() *
PATCH v5 2/36 thread-pool: Remove thread_pool_submit() function
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
This function name conflicts with one used by a future generic thread pool function and it was only used by one test anyway.
Update the trace event name in thread_pool_submit_aio() accordingly.
--- include/block/thread-pool.h | 3 +-- tests/unit/test-thread-pool.c | 6 +++--- util/thread-pool.c | 7 +------ util/trace-events | 2 +- 4 files changed, 6 insertions(+), 12 deletions(-)
include/block/thread-pool.h +1 -2
@@ -30,13 +30,12 @@ ThreadPool *thread_pool_new(struct AioContext *ctx);void thread_pool_free (ThreadPool *pool);/* -* thread_pool_submit* API: submit I/O requests in the thread's +* thread_pool_submit_{aio,co} API: submit I/O requests in the thread's * current AioContext. */ BlockAIOCB *thread_pool_submit_aio (ThreadPoolFunc *func,void *arg,BlockCompletionFunc *cb,void *opaque); int coroutine_fn thread_pool_submit_co(ThreadPoolFunc *func, void *arg); -void thread_pool_submit (ThreadPoolFunc *func,void *arg);void thread_pool_update_params (ThreadPool *pool,struct AioContext *ctx);
tests/unit/test-thread-pool.c +3 -3
@@ -43,10 +43,10 @@ static void done_cb(void *opaque, int ret) active--; } -static void test_submit (void ) +static void test_submit_no_complete (void ) {WorkerTestData data = { .n =0 }; -thread_pool_submit (worker_cb, &data); +thread_pool_submit_aio (worker_cb, &data,NULL ,NULL );while (data.n ==0 ) {aio_poll (ctx, true); }
@@ -236,7 +236,7 @@ int main(int argc, char **argv) ctx =qemu_get_current_aio_context ();g_test_init (&argc, &argv,NULL ); -g_test_add_func ("/thread-pool/submit" , test_submit); +g_test_add_func ("/thread-pool/submit-no-complete" , test_submit_no_complete);g_test_add_func ("/thread-pool/submit-aio" , test_submit_aio);g_test_add_func ("/thread-pool/submit-co" , test_submit_co);g_test_add_func ("/thread-pool/submit-many" , test_submit_many);
util/thread-pool.c +1 -6
@@ -256,7 +256,7 @@ BlockAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, void *arg,QLIST_INSERT_HEAD (&pool->head, req, all); -trace_thread_pool_submit (pool, req, arg); +trace_thread_pool_submit_aio (pool, req, arg);qemu_mutex_lock (&pool->lock);if (pool->idle_threads ==0 && pool->cur_threads < pool->max_threads) {
@@ -290,11 +290,6 @@ int coroutine_fn thread_pool_submit_co(ThreadPoolFunc *func, void *arg)return tpc.ret; } -void thread_pool_submit (ThreadPoolFunc *func,void *arg) -{ -thread_pool_submit_aio (func, arg,NULL ,NULL ); -} -void thread_pool_update_params (ThreadPool *pool,AioContext *ctx) {qemu_mutex_lock (&pool->lock);
util/trace-events +1 -1
@@ -14,7 +14,7 @@ aio_co_schedule_bh_cb(void *ctx, void *co) "ctx %p co %p" reentrant_aio(void *ctx, const char *name) "ctx %p name %s" # thread-pool.c -thread_pool_submit(void *pool, void *req, void *opaque) "pool %p req %p opaque %p" +thread_pool_submit_aio(void *pool, void *req, void *opaque) "pool %p req %p opaque %p" thread_pool_complete(void *pool, void *req, void *opaque, int ret) "pool %p req %p opaque %p ret %d" thread_pool_cancel(void *req, void *opaque) "req %p opaque %p"
PATCH v5 3/36 thread-pool: Rename AIO pool functions to *_aio() and data types to *Aio
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
These names conflict with ones used by future generic thread pool equivalents.
Generic names should belong to the generic pool type, not specific (AIO) type.
--- include/block/aio.h | 8 ++--- include/block/thread-pool.h | 8 ++--- util/async.c | 6 ++-- util/thread-pool.c | 58 ++++++++++++++++++------------------- util/trace-events | 4 +-- 5 files changed, 42 insertions(+), 42 deletions(-)
include/block/aio.h +4 -4
@@ -54,7 +54,7 @@ typedef void QEMUBHFunc(void *opaque);typedef bool AioPollFn (void *opaque);typedef void IOHandler (void *opaque); -struct ThreadPool ; +struct ThreadPoolAio ;struct LinuxAioState ;typedef struct LuringState LuringState ;
@@ -207,7 +207,7 @@ struct AioContext {/* Thread pool for performing work and receiving completion callbacks. * Has its own locking. */ -struct ThreadPool *thread_pool; +struct ThreadPoolAio *thread_pool;#ifdef CONFIG_LINUX_AIO struct LinuxAioState *linux_aio;
@@ -500,8 +500,8 @@ void aio_set_event_notifier_poll(AioContext *ctx,*/ GSource *aio_get_g_source (AioContext *ctx); -/* Return the ThreadPool bound to this AioContext */ -struct ThreadPool *aio_get_thread_pool (AioContext *ctx); +/* Return the ThreadPoolAio bound to this AioContext */ +struct ThreadPoolAio *aio_get_thread_pool (AioContext *ctx);/* Setup the LinuxAioState bound to this AioContext */ struct LinuxAioState *aio_setup_linux_aio (AioContext *ctx,Error **errp);
include/block/thread-pool.h +4 -4
@@ -24,10 +24,10 @@typedef int ThreadPoolFunc (void *opaque); -typedef struct ThreadPool ThreadPool ; +typedef struct ThreadPoolAio ThreadPoolAio ; -ThreadPool *thread_pool_new (struct AioContext *ctx); -void thread_pool_free (ThreadPool *pool); +ThreadPoolAio *thread_pool_new_aio (struct AioContext *ctx); +void thread_pool_free_aio (ThreadPoolAio *pool);/* * thread_pool_submit_{aio,co} API: submit I/O requests in the thread's
@@ -36,7 +36,7 @@ void thread_pool_free(ThreadPool *pool);BlockAIOCB *thread_pool_submit_aio (ThreadPoolFunc *func,void *arg,BlockCompletionFunc *cb,void *opaque); int coroutine_fn thread_pool_submit_co(ThreadPoolFunc *func, void *arg); +void thread_pool_update_params (ThreadPoolAio *pool,struct AioContext *ctx); -void thread_pool_update_params (ThreadPool *pool,struct AioContext *ctx);#endif
util/async.c +3 -3
@@ -369,7 +369,7 @@ aio_ctx_finalize(GSource *source)QEMUBH *bh;unsigned flags; -thread_pool_free (ctx->thread_pool); +thread_pool_free_aio (ctx->thread_pool);#ifdef CONFIG_LINUX_AIO if (ctx->linux_aio) {
@@ -435,10 +435,10 @@ GSource *aio_get_g_source(AioContext *ctx)return &ctx->source; } -ThreadPool *aio_get_thread_pool (AioContext *ctx) +ThreadPoolAio *aio_get_thread_pool (AioContext *ctx) {if (!ctx->thread_pool) { - ctx->thread_pool =thread_pool_new (ctx); + ctx->thread_pool =thread_pool_new_aio (ctx); }return ctx->thread_pool; }
util/thread-pool.c +29 -29
175 lines not on this page · raw message
util/trace-events +2 -2
@@ -15,8 +15,8 @@ reentrant_aio(void *ctx, const char *name) "ctx %p name %s" # thread-pool.c thread_pool_submit_aio(void *pool, void *req, void *opaque) "pool %p req %p opaque %p" -thread_pool_complete(void *pool, void *req, void *opaque, int ret) "pool %p req %p opaque %p ret %d" -thread_pool_cancel(void *req, void *opaque) "req %p opaque %p" +thread_pool_complete_aio(void *pool, void *req, void *opaque, int ret) "pool %p req %p opaque %p ret %d" +thread_pool_cancel_aio(void *req, void *opaque) "req %p opaque %p" # buffer.c buffer_resize(const char *buf, size_t olen, size_t len) "%s: old %zd, new %zd"
PATCH v5 4/36 thread-pool: Implement generic (non-AIO) pool support
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Migration code wants to manage device data sending threads in one place.
QEMU has an existing thread pool implementation, however it is limited to queuing AIO operations only and essentially has a 1:1 mapping between the current AioContext and the AIO ThreadPool in use.
Implement generic (non-AIO) ThreadPool by essentially wrapping Glib's GThreadPool.
This brings a few new operations on a pool:
* thread_pool_wait() operation waits until all the submitted work requests have finished.
* thread_pool_set_max_threads() explicitly sets the maximum thread count in the pool.
* thread_pool_adjust_max_threads_to_work() adjusts the maximum thread count in the pool to equal the number of still waiting in queue or unfinished work.
--- include/block/thread-pool.h | 51 ++++++++++++++++ util/thread-pool.c | 119 ++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+)
include/block/thread-pool.h +51 -0
@@ -38,5 +38,56 @@ BlockAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, void *arg, int coroutine_fn thread_pool_submit_co(ThreadPoolFunc *func, void *arg);void thread_pool_update_params (ThreadPoolAio *pool,struct AioContext *ctx); +/* ------------------------------------------- */ +/* Generic thread pool types and methods below */ +typedef struct ThreadPool ThreadPool ; + +/* Create a new thread pool. Never returns NULL. */ +ThreadPool *thread_pool_new (void ); + +/* +* Free the thread pool. +* Waits for all the previously submitted work to complete before performing +* the actual freeing operation. +*/ +void thread_pool_free (ThreadPool *pool); + +/* +* Submit a new work (task) for the pool. +* +* @opaque_destroy is an optional GDestroyNotify for the @opaque argument +* to the work function at @func. +*/ +void thread_pool_submit (ThreadPool *pool,ThreadPoolFunc *func, +void *opaque,GDestroyNotify opaque_destroy); + +/* +* Submit a new work (task) for the pool, making sure it starts getting +* processed immediately, launching a new thread for it if necessary. +* +* @opaque_destroy is an optional GDestroyNotify for the @opaque argument +* to the work function at @func. +*/ +void thread_pool_submit_immediate (ThreadPool *pool,ThreadPoolFunc *func, +void *opaque,GDestroyNotify opaque_destroy); + +/* +* Wait for all previously submitted work to complete before returning. +* +* Can be used as a barrier between two sets of tasks executed on a thread +* pool without destroying it or in a performance sensitive path where the +* caller just wants to wait for all tasks to complete while deferring the +* pool free operation for later, less performance sensitive time. +*/ +void thread_pool_wait (ThreadPool *pool); + +/* Set the maximum number of threads in the pool. */ +bool thread_pool_set_max_threads (ThreadPool *pool,int max_threads); + +/* +* Adjust the maximum number of threads in the pool to give each task its +* own thread (exactly one thread per task). +*/ +bool thread_pool_adjust_max_threads_to_work (ThreadPool *pool);#endif
util/thread-pool.c +119 -0
122 lines not on this page · raw message
PATCH v5 5/36 migration: Add MIG_CMD_SWITCHOVER_START and its load handler
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
This QEMU_VM_COMMAND sub-command and its switchover_start SaveVMHandler is used to mark the switchover point in main migration stream.
It can be used to inform the destination that all pre-switchover main migration stream data has been sent/received so it can start to process post-switchover data that it might have received via other migration channels like the multifd ones.
Add also the relevant MigrationState bit stream compatibility property and its hw_compat entry.
--- hw/core/machine.c | 1 + include/migration/client-options.h | 4 +++ include/migration/register.h | 12 +++++++++ migration/colo.c | 3 +++ migration/migration-hmp-cmds.c | 2 ++ migration/migration.c | 2 ++ migration/migration.h | 2 ++ migration/options.c | 9 +++++++ migration/savevm.c | 39 ++++++++++++++++++++++++++++++ migration/savevm.h | 1 + migration/trace-events | 1 + scripts/analyze-migration.py | 11 +++++++++ 12 files changed, 87 insertions(+)
hw/core/machine.c +1 -0
@@ -43,6 +43,7 @@ GlobalProperty hw_compat_9_2[] = { { "virtio-balloon-pci-non-transitional", "vectors", "0" }, { "virtio-mem-pci", "vectors", "0" }, { "migration", "multifd-clean-tls-termination", "false" }, + { "migration", "send-switchover-start", "off"}, };const size_t hw_compat_9_2_len =G_N_ELEMENTS (hw_compat_9_2);
include/migration/client-options.h +4 -0
@@ -10,6 +10,10 @@#ifndef QEMU_MIGRATION_CLIENT_OPTIONS_H #define QEMU_MIGRATION_CLIENT_OPTIONS_H + +/* properties */ +bool migrate_send_switchover_start (void ); +/* capabilities */ bool migrate_background_snapshot (void );
include/migration/register.h +12 -0
@@ -279,6 +279,18 @@ typedef struct SaveVMHandlers {* otherwise */ bool (*switchover_ack_needed)(void *opaque); + +/** +* @switchover_start +* +* Notifies that the switchover has started. Called only on +* the destination. +* +* @opaque: data pointer passed to register_savevm_live() +* +* Returns zero to indicate success and negative for error +*/ +int (*switchover_start)(void *opaque); } SaveVMHandlers;/**
migration/colo.c +3 -0
@@ -452,6 +452,9 @@ static int colo_do_checkpoint_transaction(MigrationState *s,bql_unlock (); goto out; } + +qemu_savevm_maybe_send_switchover_start (s->to_dst_file); +/* Note: device state is saved into buffer */ ret =qemu_save_device_state (fb);
migration/migration-hmp-cmds.c +2 -0
@@ -46,6 +46,8 @@ static void migration_global_dump(Monitor *mon) ms->send_configuration ? "on" : "off");monitor_printf (mon,"send-section-footer: %s\n" , ms->send_section_footer ?"on" :"off" ); +monitor_printf (mon,"send-switchover-start: %s\n" , + ms->send_switchover_start ?"on" :"off" );monitor_printf (mon,"clear-bitmap-shift: %u\n" , ms->clear_bitmap_shift); }
migration/migration.c +2 -0
@@ -2891,6 +2891,8 @@ static bool migration_switchover_start(MigrationState *s, Error **errp)precopy_notify_complete (); +qemu_savevm_maybe_send_switchover_start (s->to_dst_file); +return true; }
migration/migration.h +2 -0
@@ -400,6 +400,8 @@ struct MigrationState {bool send_configuration;/* Whether we send section footer during migration */ bool send_section_footer; +/* Whether we send switchover start notification during migration */ +bool send_switchover_start;/* Needed by postcopy-pause state */ QemuSemaphore postcopy_pause_sem;
migration/options.c +9 -0
@@ -93,6 +93,8 @@ const Property migration_properties[] = { send_configuration, true),DEFINE_PROP_BOOL ("send-section-footer" , MigrationState, send_section_footer, true), +DEFINE_PROP_BOOL ("send-switchover-start" , MigrationState, + send_switchover_start, true),DEFINE_PROP_BOOL ("multifd-flush-after-each-section" , MigrationState, multifd_flush_after_each_section, false), DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
@@ -209,6 +211,13 @@ bool migrate_auto_converge(void)return s->capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE ]; } +bool migrate_send_switchover_start (void ) +{ +MigrationState *s =migrate_get_current (); + +return s->send_switchover_start; +} +bool migrate_background_snapshot (void ) {MigrationState *s =migrate_get_current ();
migration/savevm.c +39 -0
@@ -90,6 +90,7 @@ enum qemu_vm_cmd {MIG_CMD_ENABLE_COLO ,/* Enable COLO */ MIG_CMD_POSTCOPY_RESUME ,/* resume postcopy on dest */ MIG_CMD_RECV_BITMAP ,/* Request for recved bitmap on dst */ +MIG_CMD_SWITCHOVER_START ,/* Switchover start notification */ MIG_CMD_MAX };
@@ -109,6 +110,7 @@ static struct mig_cmd_args { [MIG_CMD_POSTCOPY_RESUME] = { .len = 0, .name = "POSTCOPY_RESUME" }, [MIG_CMD_PACKAGED] = { .len = 4, .name = "PACKAGED" }, [MIG_CMD_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" }, + [MIG_CMD_SWITCHOVER_START] = { .len = 0, .name = "SWITCHOVER_START" }, [MIG_CMD_MAX] = { .len = -1, .name = "MAX" }, };
@@ -1201,6 +1203,19 @@ void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name)qemu_savevm_command_send (f,MIG_CMD_RECV_BITMAP , len +1 , (uint8_t *)buf); } +static void qemu_savevm_send_switchover_start (QEMUFile *f) +{ +trace_savevm_send_switchover_start (); +qemu_savevm_command_send (f,MIG_CMD_SWITCHOVER_START ,0 ,NULL ); +} + +void qemu_savevm_maybe_send_switchover_start (QEMUFile *f) +{ +if (migrate_send_switchover_start ()) { +qemu_savevm_send_switchover_start (f); + } +} +bool qemu_savevm_state_blocked (Error **errp) {SaveStateEntry *se;
@@ -1687,6 +1702,7 @@ static int qemu_savevm_state(QEMUFile *f, Error **errp) ret =qemu_file_get_error (f);if (ret ==0 ) { +qemu_savevm_maybe_send_switchover_start (f);qemu_savevm_state_complete_precopy (f, false); ret =qemu_file_get_error (f); }
@@ -2383,6 +2399,26 @@ static int loadvm_process_enable_colo(MigrationIncomingState *mis)return ret; } +static int loadvm_postcopy_handle_switchover_start (void ) +{ +SaveStateEntry *se; + + QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { +int ret; + +if (!se->ops || !se->ops->switchover_start) { +continue ; + } + + ret = se->ops->switchover_start (se->opaque); +if (ret <0 ) { +return ret; + } + } + +return 0 ; +} +/* * Process an incoming 'QEMU_VM_COMMAND' * 0 just a normal return
@@ -2481,6 +2517,9 @@ static int loadvm_process_command(QEMUFile *f)case MIG_CMD_ENABLE_COLO :return loadvm_process_enable_colo (mis); + +case MIG_CMD_SWITCHOVER_START : +return loadvm_postcopy_handle_switchover_start (); }return 0 ;
migration/savevm.h +1 -0
@@ -53,6 +53,7 @@ void qemu_savevm_send_postcopy_listen(QEMUFile *f);void qemu_savevm_send_postcopy_run (QEMUFile *f);void qemu_savevm_send_postcopy_resume (QEMUFile *f);void qemu_savevm_send_recv_bitmap (QEMUFile *f,char *block_name); +void qemu_savevm_maybe_send_switchover_start (QEMUFile *f); void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name, uint16_t len,
migration/trace-events +1 -0
@@ -39,6 +39,7 @@ savevm_send_postcopy_run(void) "" savevm_send_postcopy_resume(void) "" savevm_send_colo_enable(void) "" savevm_send_recv_bitmap(char *name) "%s" +savevm_send_switchover_start(void) "" savevm_state_setup(void) "" savevm_state_resume_prepare(void) "" savevm_state_header(void) ""
scripts/analyze-migration.py +11 -0
@@ -620,7 +620,9 @@ class MigrationDump(object): QEMU_VM_SUBSECTION = 0x05 QEMU_VM_VMDESCRIPTION = 0x06 QEMU_VM_CONFIGURATION = 0x07 + QEMU_VM_COMMAND = 0x08 QEMU_VM_SECTION_FOOTER= 0x7e + QEMU_MIG_CMD_SWITCHOVER_START = 0x0b def __init__(self, filename): self.section_classes = {
@@ -685,6 +687,15 @@ def read(self, desc_only = False, dump_memory = False, elif section_type == self.QEMU_VM_SECTION_PART or section_type == self.QEMU_VM_SECTION_END: section_id = file.read32() self.sections[section_id].read() + elif section_type == self.QEMU_VM_COMMAND: + command_type = file.read16() + command_data_len = file.read16() + if command_type != self.QEMU_MIG_CMD_SWITCHOVER_START: + raise Exception("Unknown QEMU_VM_COMMAND: %x" % + (command_type)) + if command_data_len != 0: + raise Exception("Invalid SWITCHOVER_START length: %x" % + (command_data_len)) elif section_type == self.QEMU_VM_SECTION_FOOTER: read_section_id = file.read32() if read_section_id != section_id:
PATCH v5 6/36 migration: Add qemu_loadvm_load_state_buffer() and its handler
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
qemu_loadvm_load_state_buffer() and its load_state_buffer SaveVMHandler allow providing device state buffer to explicitly specified device via its idstr and instance id.
--- include/migration/register.h | 15 +++++++++++++++ migration/savevm.c | 23 +++++++++++++++++++++++ migration/savevm.h | 3 +++ 3 files changed, 41 insertions(+)
include/migration/register.h +15 -0
@@ -229,6 +229,21 @@ typedef struct SaveVMHandlers {*/ int (*load_state)(QEMUFile *f,void *opaque,int version_id); +/** +* @load_state_buffer (invoked outside the BQL) +* +* Load device state buffer provided to qemu_loadvm_load_state_buffer(). +* +* @opaque: data pointer passed to register_savevm_live() +* @buf: the data buffer to load +* @len: the data length in buffer +* @errp: pointer to Error*, to store an error if it happens. +* +* Returns true to indicate success and false for errors. +*/ +bool (*load_state_buffer)(void *opaque,char *buf,size_t len, +Error **errp); +/** * @load_setup *
migration/savevm.c +23 -0
@@ -3060,6 +3060,29 @@ int qemu_loadvm_approve_switchover(void)return migrate_send_rp_switchover_ack (mis); } +bool qemu_loadvm_load_state_buffer (const char *idstr,uint32_t instance_id, +char *buf,size_t len,Error **errp) +{ +SaveStateEntry *se; + + se =find_se (idstr, instance_id); +if (!se) { +error_setg (errp, +"Unknown idstr %s or instance id %u for load state buffer" , + idstr, instance_id); +return false; + } + +if (!se->ops || !se->ops->load_state_buffer) { +error_setg (errp, +"idstr %s / instance %u has no load state buffer operation" , + idstr, instance_id); +return false; + } + +return se->ops->load_state_buffer (se->opaque, buf, len, errp); +} +bool save_snapshot (const char *name,bool overwrite,const char *vmstate,bool has_devices,strList *devices,Error **errp) {
migration/savevm.h +3 -0
@@ -71,4 +71,7 @@ int qemu_loadvm_approve_switchover(void);int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f,bool in_postcopy); +bool qemu_loadvm_load_state_buffer (const char *idstr,uint32_t instance_id, +char *buf,size_t len,Error **errp); +#endif
PATCH v5 7/36 migration: postcopy_ram_listen_thread() should take BQL for some calls
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
All callers to migration_incoming_state_destroy() other than postcopy_ram_listen_thread() do this call with BQL held.
Since migration_incoming_state_destroy() ultimately calls "load_cleanup" SaveVMHandlers and it will soon call BQL-sensitive code it makes sense to always call that function under BQL rather than to have it deal with both cases (with BQL and without BQL).
Add the necessary bql_lock() and bql_unlock() to postcopy_ram_listen_thread().
qemu_loadvm_state_main() in postcopy_ram_listen_thread() could call "load_state" SaveVMHandlers that are expecting BQL to be held.
In principle, the only devices that should be arriving on migration channel serviced by postcopy_ram_listen_thread() are those that are postcopiable and whose load handlers are safe to be called without BQL being held.
But nothing currently prevents the source from sending data for "unsafe" devices which would cause trouble there.
Add a TODO comment there so it's clear that it would be good to improve handling of such (erroneous) case in the future.
--- migration/savevm.c | 4 ++++ 1 file changed, 4 insertions(+)
migration/savevm.c +4 -0
@@ -1986,6 +1986,8 @@ static void *postcopy_ram_listen_thread(void *opaque)* in qemu_file, and thus we must be blocking now. */ qemu_file_set_blocking (f, true); + +/* TODO: sanity check that only postcopiable data will be loaded here */ load_res =qemu_loadvm_state_main (f, mis);/*
@@ -2046,7 +2048,9 @@ static void *postcopy_ram_listen_thread(void *opaque)* (If something broke then qemu will have to exit anyway since it's * got a bad migration state). */ +bql_lock ();migration_incoming_state_destroy (); +bql_unlock ();rcu_unregister_thread (); mis->have_listen_thread = false;
Re: [PATCH v5 07/36] migration: postcopy_ram_listen_thread() should take BQL for some calls
Quoted 11 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
All callers to
migration_incoming_state_destroy()other thanpostcopy_ram_listen_thread()do this call with BQL held.Since
migration_incoming_state_destroy()ultimately calls "load_cleanup" SaveVMHandlers and it will soon call BQL-sensitive code it makes sense to always call that function under BQL rather than to have it deal with both cases (with BQL and without BQL).
Add the necessarybql_lock()andbql_unlock()topostcopy_ram_listen_thread().
We can do that, but let's be explicit on what needs BQL to be taken.
Could you add an assertion in migration_incoming_state_destroy() on bql_locked(), then add a rich comment above it listing what needs the BQL? We may consider dropping it some day when it's not needed.
Thanks,
Quoted 43 lines
qemu_loadvm_state_main()inpostcopy_ram_listen_thread()could call "load_state" SaveVMHandlers that are expecting BQL to be held.In principle, the only devices that should be arriving on migration channel serviced by
postcopy_ram_listen_thread()are those that are postcopiable and whose load handlers are safe to be called without BQL being held.But nothing currently prevents the source from sending data for "unsafe" devices which would cause trouble there.
Add a TODO comment there so it's clear that it would be good to improve handling of such (erroneous) case in the future.Signed-off-by Maciej S. Szmigiero <[email protected]>--- migration/savevm.c | 4 ++++ 1 file changed, 4 insertions(+)migration/savevm.c +4
-0@@ -1986,6 +1986,8 @@ static void *postcopy_ram_listen_thread(void *opaque)* in qemu_file, and thus we must be blocking now. */ qemu_file_set_blocking (f, true); + +/* TODO: sanity check that only postcopiable data will be loaded here */ load_res =qemu_loadvm_state_main (f, mis);/* @@ -2046,7 +2048,9 @@ static void *postcopy_ram_listen_thread(void *opaque)* (If something broke then qemu will have to exit anyway since it's * got a bad migration state). */ +bql_lock ();migration_incoming_state_destroy (); +bql_unlock ();rcu_unregister_thread (); mis->have_listen_thread = false;
Signature · 1 line
Peter Xu
Re: [PATCH v5 07/36] migration: postcopy_ram_listen_thread() should take BQL for some calls
Quoted 13 lines · Peter Xu
Quoted 11 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
All callers to
migration_incoming_state_destroy()other thanpostcopy_ram_listen_thread()do this call with BQL held.Since
migration_incoming_state_destroy()ultimately calls "load_cleanup" SaveVMHandlers and it will soon call BQL-sensitive code it makes sense to always call that function under BQL rather than to have it deal with both cases (with BQL and without BQL).
Add the necessarybql_lock()andbql_unlock()topostcopy_ram_listen_thread().
Quoted 5 lines
We can do that, but let's be explicit on what needs BQL to be taken.
Could you add an assertion in
migration_incoming_state_destroy()onbql_locked(), then add a rich comment above it listing what needs the BQL? We may consider dropping it some day when it's not needed.
Sure, good idea.
Updated this commit now, and tests (make check) still pass.
Quoted 1 line
Thanks,
Thanks,
Maciej
PATCH v5 8/36 error: define g_autoptr() cleanup function for the Error type
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Automatic memory management helps avoid memory safety issues.
--- include/qapi/error.h | 2 ++ 1 file changed, 2 insertions(+)
include/qapi/error.h +2 -0
@@ -437,6 +437,8 @@ Error *error_copy(const Error *err);*/ void error_free (Error *err); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (Error, error_free) +/* * Convenience function to assert that *@errp is set, then silently free it. */
PATCH v5 9/36 migration: Add thread pool of optional load threads
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Some drivers might want to make use of auxiliary helper threads during VM state loading, for example to make sure that their blocking (sync) I/O operations don't block the rest of the migration process.
Add a migration core managed thread pool to facilitate this use case.
The migration core will wait for these threads to finish before (re)starting the VM at destination.
--- include/migration/misc.h | 3 ++ include/qemu/typedefs.h | 2 + migration/migration.c | 2 +- migration/migration.h | 5 +++ migration/savevm.c | 89 +++++++++++++++++++++++++++++++++++++++- migration/savevm.h | 2 +- 6 files changed, 99 insertions(+), 4 deletions(-)
include/migration/misc.h +3 -0
@@ -45,9 +45,12 @@ bool migrate_ram_is_ignored(RAMBlock *block);/* migration/block.c */ AnnounceParameters *migrate_announce_params (void ); +/* migration/savevm.c */ void dump_vmstate_json_to_file (FILE *out_fp); +void qemu_loadvm_start_load_thread (MigrationLoadThread function, +void *opaque);/* migration/migration.c */ void migration_object_init (void );
include/qemu/typedefs.h +2 -0
@@ -131,5 +131,7 @@ typedef struct IRQState *qemu_irq;* Function types */ typedef void (*qemu_irq_handler )(void *opaque,int n,int level); +typedef bool (*MigrationLoadThread )(void *opaque,bool *should_quit, +Error **errp);#endif /* QEMU_TYPEDEFS_H */
migration/migration.c +1 -1
@@ -406,7 +406,7 @@ void migration_incoming_state_destroy(void)* RAM state cleanup needs to happen after multifd cleanup, because * multifd threads can use some of its states (receivedmap). */ -qemu_loadvm_state_cleanup (); +qemu_loadvm_state_cleanup (mis);if (mis->to_src_file) {/* Tell source that we are done */
migration/migration.h +5 -0
@@ -43,6 +43,7 @@#define MIGRATION_THREAD_DST_PREEMPT "mig/dst/preempt"struct PostcopyBlocktimeContext ; +typedef struct ThreadPool ThreadPool ;#define MIGRATION_RESUME_ACK_VALUE (1)
@@ -187,6 +188,10 @@ struct MigrationIncomingState {Coroutine *colo_incoming_co;QemuSemaphore colo_incoming_sem; +/* Optional load threads pool and its thread exit request flag */ +ThreadPool *load_threads; +bool load_threads_abort; +/* * PostcopyBlocktimeContext to keep information for postcopy * live migration, to calculate vCPU block time
migration/savevm.c +87 -2
139 lines not on this page · raw message
migration/savevm.h +1 -1
@@ -64,7 +64,7 @@ void qemu_savevm_live_state(QEMUFile *f);int qemu_save_device_state (QEMUFile *f);int qemu_loadvm_state (QEMUFile *f); -void qemu_loadvm_state_cleanup (void ); +void qemu_loadvm_state_cleanup (MigrationIncomingState *mis);int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis);int qemu_load_device_state (QEMUFile *f);int qemu_loadvm_approve_switchover (void );
PATCH v5 10/36 migration/multifd: Split packet into header and RAM data
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Read packet header first so in the future we will be able to differentiate between a RAM multifd packet and a device state multifd packet.
Since these two are of different size we can't read the packet body until we know which packet type it is.
--- migration/multifd.c | 55 ++++++++++++++++++++++++++++++++++++--------- migration/multifd.h | 5 +++++ 2 files changed, 49 insertions(+), 11 deletions(-)
migration/multifd.c +44 -11
@@ -209,10 +209,10 @@ void multifd_send_fill_packet(MultiFDSendParams *p)memset (packet,0 , p->packet_len); - packet->magic =cpu_to_be32 (MULTIFD_MAGIC ); - packet->version =cpu_to_be32 (MULTIFD_VERSION ); + packet->hdr.magic =cpu_to_be32 (MULTIFD_MAGIC ); + packet->hdr.version =cpu_to_be32 (MULTIFD_VERSION ); - packet->flags =cpu_to_be32 (p->flags); + packet->hdr.flags =cpu_to_be32 (p->flags); packet->next_packet_size =cpu_to_be32 (p->next_packet_size); packet_num =qatomic_fetch_inc (&multifd_send_state->packet_num);
@@ -228,12 +228,12 @@ void multifd_send_fill_packet(MultiFDSendParams *p) p->flags, p->next_packet_size); } -static int multifd_recv_unfill_packet (MultiFDRecvParams *p,Error **errp) +static int multifd_recv_unfill_packet_header (MultiFDRecvParams *p, +const MultiFDPacketHdr_t *hdr, +Error **errp) { -const MultiFDPacket_t *packet = p->packet; -uint32_t magic =be32_to_cpu (packet->magic); -uint32_t version =be32_to_cpu (packet->version); -int ret =0 ; +uint32_t magic =be32_to_cpu (hdr->magic); +uint32_t version =be32_to_cpu (hdr->version);if (magic !=MULTIFD_MAGIC ) { error_setg(errp, "multifd: received packet magic %x, expected %x",
@@ -247,7 +247,16 @@ static int multifd_recv_unfill_packet(MultiFDRecvParams *p, Error **errp)return -1 ; } - p->flags =be32_to_cpu (packet->flags); + p->flags =be32_to_cpu (hdr->flags); + +return 0 ; +} + +static int multifd_recv_unfill_packet (MultiFDRecvParams *p,Error **errp) +{ +const MultiFDPacket_t *packet = p->packet; +int ret =0 ; + p->next_packet_size =be32_to_cpu (packet->next_packet_size); p->packet_num =be64_to_cpu (packet->packet_num); p->packets_recved++;
@@ -1165,14 +1174,18 @@ static void *multifd_recv_thread(void *opaque) }while (true) { +MultiFDPacketHdr_t hdr;uint32_t flags =0 ;bool has_data = false; +uint8_t *pkt_buf; +size_t pkt_len; + p->normal_num =0 ;if (use_packets) {struct iovec iov = { - .iov_base = (void *)p->packet, - .iov_len = p->packet_len + .iov_base = (void *)&hdr, + .iov_len =sizeof (hdr) };if (multifd_recv_should_exit ()) {
@@ -1191,6 +1204,26 @@ static void *multifd_recv_thread(void *opaque)break ; } + ret =multifd_recv_unfill_packet_header (p, &hdr, &local_err); +if (ret) { +break ; + } + + pkt_buf = (uint8_t *)p->packet +sizeof (hdr); + pkt_len = p->packet_len -sizeof (hdr); + + ret =qio_channel_read_all_eof (p->c, (char *)pkt_buf, pkt_len, + &local_err); +if (!ret) { +/* EOF */ +error_setg (&local_err,"multifd: unexpected EOF after packet header" ); +break ; + } + +if (ret ==-1 ) { +break ; + } +qemu_mutex_lock (&p->mutex); ret =multifd_recv_unfill_packet (p, &local_err);if (ret) {
migration/multifd.h +5 -0
@@ -69,6 +69,11 @@ typedef struct {uint32_t magic;uint32_t version;uint32_t flags; +} __attribute__((packed)) MultiFDPacketHdr_t; + +typedef struct { + MultiFDPacketHdr_t hdr; + /* maximum number of allocated pages */ uint32_t pages_alloc; /* non zero pages */
PATCH v5 11/36 migration/multifd: Device state transfer support - receive side
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Add a basic support for receiving device state via multifd channels - channels that are shared with RAM transfers.
Depending whether MULTIFD_FLAG_DEVICE_STATE flag is present or not in the packet header either device state (MultiFDPacketDeviceState_t) or RAM data (existing MultiFDPacket_t) is read.
The received device state data is provided to qemu_loadvm_load_state_buffer() function for processing in the device's load_state_buffer handler.
--- migration/multifd.c | 99 ++++++++++++++++++++++++++++++++++++++++----- migration/multifd.h | 26 +++++++++++- 2 files changed, 113 insertions(+), 12 deletions(-)
migration/multifd.c +88 -11
@@ -21,6 +21,7 @@#include "file.h" #include "migration.h" #include "migration-stats.h" +#include "savevm.h" #include "socket.h" #include "tls.h" #include "qemu-file.h"
@@ -252,14 +253,24 @@ static int multifd_recv_unfill_packet_header(MultiFDRecvParams *p,return 0 ; } -static int multifd_recv_unfill_packet (MultiFDRecvParams *p,Error **errp) +static int multifd_recv_unfill_packet_device_state (MultiFDRecvParams *p, +Error **errp) +{ +MultiFDPacketDeviceState_t *packet = p->packet_dev_state; + + packet->instance_id =be32_to_cpu (packet->instance_id); + p->next_packet_size =be32_to_cpu (packet->next_packet_size); + +return 0 ; +} + +static int multifd_recv_unfill_packet_ram (MultiFDRecvParams *p,Error **errp) {const MultiFDPacket_t *packet = p->packet;int ret =0 ; p->next_packet_size =be32_to_cpu (packet->next_packet_size); p->packet_num =be64_to_cpu (packet->packet_num); - p->packets_recved++;/* Always unfill, old QEMUs (<9.0) send data along with SYNC */ ret =multifd_ram_unfill_packet (p, errp);
@@ -270,6 +281,17 @@ static int multifd_recv_unfill_packet(MultiFDRecvParams *p, Error **errp)return ret; } +static int multifd_recv_unfill_packet (MultiFDRecvParams *p,Error **errp) +{ + p->packets_recved++; + +if (p->flags &MULTIFD_FLAG_DEVICE_STATE ) { +return multifd_recv_unfill_packet_device_state (p, errp); + } + +return multifd_recv_unfill_packet_ram (p, errp); +} +static bool multifd_send_should_exit (void ) {return qatomic_read (&multifd_send_state->exiting);
@@ -1057,6 +1079,7 @@ static void multifd_recv_cleanup_channel(MultiFDRecvParams *p) p->packet_len =0 ;g_free (p->packet); p->packet =NULL ; +g_clear_pointer (&p->packet_dev_state, g_free);g_free (p->normal); p->normal =NULL ;g_free (p->zero);
@@ -1158,6 +1181,32 @@ void multifd_recv_sync_main(void)trace_multifd_recv_sync_main (multifd_recv_state->packet_num); } +static int multifd_device_state_recv (MultiFDRecvParams *p,Error **errp) +{ + g_autofree char *idstr = NULL; + g_autofree char *dev_state_buf = NULL; +int ret; + + dev_state_buf =g_malloc (p->next_packet_size); + + ret =qio_channel_read_all (p->c, dev_state_buf, p->next_packet_size, errp); +if (ret !=0 ) { +return ret; + } + + idstr =g_strndup (p->packet_dev_state->idstr, +sizeof (p->packet_dev_state->idstr)); + +if (!qemu_loadvm_load_state_buffer (idstr, + p->packet_dev_state->instance_id, + dev_state_buf, p->next_packet_size, + errp)) { + ret =-1 ; + } + +return ret; +} +static void *multifd_recv_thread (void *opaque) {MigrationState *s =migrate_get_current ();
@@ -1176,6 +1225,7 @@ static void *multifd_recv_thread(void *opaque)while (true) {MultiFDPacketHdr_t hdr;uint32_t flags =0 ; +bool is_device_state = false;bool has_data = false;uint8_t *pkt_buf;size_t pkt_len;
@@ -1209,8 +1259,14 @@ static void *multifd_recv_thread(void *opaque)break ; } - pkt_buf = (uint8_t *)p->packet +sizeof (hdr); - pkt_len = p->packet_len -sizeof (hdr); + is_device_state = p->flags &MULTIFD_FLAG_DEVICE_STATE ; +if (is_device_state) { + pkt_buf = (uint8_t *)p->packet_dev_state +sizeof (hdr); + pkt_len =sizeof (*p->packet_dev_state) -sizeof (hdr); + }else { + pkt_buf = (uint8_t *)p->packet +sizeof (hdr); + pkt_len = p->packet_len -sizeof (hdr); + } ret =qio_channel_read_all_eof (p->c, (char *)pkt_buf, pkt_len, &local_err);
@@ -1235,12 +1291,17 @@ static void *multifd_recv_thread(void *opaque)/* recv methods don't know how to handle the SYNC flag */ p->flags &= ~MULTIFD_FLAG_SYNC ; -/* -* Even if it's a SYNC packet, this needs to be set -* because older QEMUs (<9.0) still send data along with -* the SYNC packet. -*/ - has_data = p->normal_num || p->zero_num; +if (is_device_state) { + has_data = p->next_packet_size >0 ; + }else { +/* +* Even if it's a SYNC packet, this needs to be set +* because older QEMUs (<9.0) still send data along with +* the SYNC packet. +*/ + has_data = p->normal_num || p->zero_num; + } +qemu_mutex_unlock (&p->mutex); }else {/*
@@ -1269,14 +1330,29 @@ static void *multifd_recv_thread(void *opaque) }if (has_data) { - ret = multifd_recv_state->ops->recv (p, &local_err); +if (is_device_state) { +assert (use_packets); + ret =multifd_device_state_recv (p, &local_err); + }else { + ret = multifd_recv_state->ops->recv (p, &local_err); + }if (ret !=0 ) {break ; } + }else if (is_device_state) { +error_setg (&local_err, +"multifd: received empty device state packet" ); +break ; }if (use_packets) {if (flags &MULTIFD_FLAG_SYNC ) { +if (is_device_state) { +error_setg (&local_err, +"multifd: received SYNC device state packet" ); +break ; + } +qemu_sem_post (&multifd_recv_state->sem_sync);qemu_sem_wait (&p->sem_sync); }
@@ -1345,6 +1421,7 @@ int multifd_recv_setup(Error **errp) p->packet_len =sizeof (MultiFDPacket_t) +sizeof (uint64_t ) * page_count; p->packet =g_malloc0 (p->packet_len); + p->packet_dev_state =g_malloc0 (sizeof (*p->packet_dev_state)); } p->name =g_strdup_printf (MIGRATION_THREAD_DST_MULTIFD , i); p->normal =g_new0 (ram_addr_t, page_count);
migration/multifd.h +25 -1
@@ -62,6 +62,12 @@ MultiFDRecvData *multifd_get_recv_data(void);#define MULTIFD_FLAG_UADK (8 << 1)#define MULTIFD_FLAG_QATZIP (16 << 1) +/* +* If set it means that this packet contains device state +* (MultiFDPacketDeviceState_t), not RAM data (MultiFDPacket_t). +*/ +#define MULTIFD_FLAG_DEVICE_STATE (32 << 1) +/* This value needs to be a multiple of qemu_target_page_size() */ #define MULTIFD_PACKET_SIZE (512 * 1024)
@@ -94,6 +100,16 @@ typedef struct {uint64_t offset[]; } __attribute__((packed)) MultiFDPacket_t; +typedef struct { +MultiFDPacketHdr_t hdr; + + char idstr[256] QEMU_NONSTRING; +uint32_t instance_id; + +/* size of the next packet that contains the actual data */ +uint32_t next_packet_size; +} __attribute__((packed))MultiFDPacketDeviceState_t ; + typedef struct { /* number of used pages */ uint32_t num;
@@ -111,6 +127,13 @@ struct MultiFDRecvData {off_t file_offset; }; +typedef struct { +char *idstr; +uint32_t instance_id; +char *buf; +size_t buf_len; +}MultiFDDeviceState_t ; + typedef enum { MULTIFD_PAYLOAD_NONE, MULTIFD_PAYLOAD_RAM,
@@ -227,8 +250,9 @@ typedef struct {/* thread local variables. No locking required */ -/* pointer to the packet */ +/* pointers to the possible packet types */ MultiFDPacket_t *packet; +MultiFDPacketDeviceState_t *packet_dev_state;/* size of the next packet that contains pages */ uint32_t next_packet_size;/* packets received through this channel */
Re: [PATCH v5 11/36] migration/multifd: Device state transfer support - receive side
Hi Maciej,
Sorry for the long delay, I have been busy with other tasks. I got some small comments for the series.
Quoted 244 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
Add a basic support for receiving device state via multifd channels - channels that are shared with RAM transfers.
Depending whether
MULTIFD_FLAG_DEVICE_STATEflag is present or not in the packet header either device state (MultiFDPacketDeviceState_t) or RAM data (existingMultiFDPacket_t) is read.The received device state data is provided to
qemu_loadvm_load_state_buffer()function for processing in the device'sload_state_bufferhandler.Reviewed-by Peter Xu <[email protected]>Signed-off-by Maciej S. Szmigiero <[email protected]>--- migration/multifd.c | 99 ++++++++++++++++++++++++++++++++++++++++----- migration/multifd.h | 26 +++++++++++- 2 files changed, 113 insertions(+), 12 deletions(-)migration/multifd.c +88
-11@@ -21,6 +21,7 @@#include "file.h" #include "migration.h" #include "migration-stats.h" +#include "savevm.h" #include "socket.h" #include "tls.h" #include "qemu-file.h" @@ -252,14 +253,24 @@ static int multifd_recv_unfill_packet_header(MultiFDRecvParams *p,return 0 ; } -static int multifd_recv_unfill_packet (MultiFDRecvParams *p,Error **errp) +static int multifd_recv_unfill_packet_device_state (MultiFDRecvParams *p, +Error **errp) +{ +MultiFDPacketDeviceState_t *packet = p->packet_dev_state; + + packet->instance_id =be32_to_cpu (packet->instance_id); + p->next_packet_size =be32_to_cpu (packet->next_packet_size); + +return 0 ; +} + +static int multifd_recv_unfill_packet_ram (MultiFDRecvParams *p,Error **errp) {const MultiFDPacket_t *packet = p->packet;int ret =0 ; p->next_packet_size =be32_to_cpu (packet->next_packet_size); p->packet_num =be64_to_cpu (packet->packet_num); - p->packets_recved++;/* Always unfill, old QEMUs (<9.0) send data along with SYNC */ ret =multifd_ram_unfill_packet (p, errp);@@ -270,6 +281,17 @@ static int multifd_recv_unfill_packet(MultiFDRecvParams *p, Error **errp)return ret; } +static int multifd_recv_unfill_packet (MultiFDRecvParams *p,Error **errp) +{ + p->packets_recved++; + +if (p->flags &MULTIFD_FLAG_DEVICE_STATE ) { +return multifd_recv_unfill_packet_device_state (p, errp); + } + +return multifd_recv_unfill_packet_ram (p, errp); +} +static bool multifd_send_should_exit (void ) {return qatomic_read (&multifd_send_state->exiting);@@ -1057,6 +1079,7 @@ static void multifd_recv_cleanup_channel(MultiFDRecvParams *p) p->packet_len =0 ;g_free (p->packet); p->packet =NULL ; +g_clear_pointer (&p->packet_dev_state, g_free);g_free (p->normal); p->normal =NULL ;g_free (p->zero);@@ -1158,6 +1181,32 @@ void multifd_recv_sync_main(void)trace_multifd_recv_sync_main (multifd_recv_state->packet_num); } +static int multifd_device_state_recv (MultiFDRecvParams *p,Error **errp) +{ + g_autofree char *idstr = NULL; + g_autofree char *dev_state_buf = NULL; +int ret; + + dev_state_buf =g_malloc (p->next_packet_size); + + ret =qio_channel_read_all (p->c, dev_state_buf, p->next_packet_size, errp); +if (ret !=0 ) { +return ret; + } + + idstr =g_strndup (p->packet_dev_state->idstr, +sizeof (p->packet_dev_state->idstr)); + +if (!qemu_loadvm_load_state_buffer (idstr, + p->packet_dev_state->instance_id, + dev_state_buf, p->next_packet_size, + errp)) { + ret =-1 ; + } + +return ret; +} +static void *multifd_recv_thread (void *opaque) {MigrationState *s =migrate_get_current ();@@ -1176,6 +1225,7 @@ static void *multifd_recv_thread(void *opaque)while (true) {MultiFDPacketHdr_t hdr;uint32_t flags =0 ; +bool is_device_state = false;bool has_data = false;uint8_t *pkt_buf;size_t pkt_len;@@ -1209,8 +1259,14 @@ static void *multifd_recv_thread(void *opaque)break ; } - pkt_buf = (uint8_t *)p->packet +sizeof (hdr); - pkt_len = p->packet_len -sizeof (hdr); + is_device_state = p->flags &MULTIFD_FLAG_DEVICE_STATE ; +if (is_device_state) { + pkt_buf = (uint8_t *)p->packet_dev_state +sizeof (hdr); + pkt_len =sizeof (*p->packet_dev_state) -sizeof (hdr); + }else { + pkt_buf = (uint8_t *)p->packet +sizeof (hdr); + pkt_len = p->packet_len -sizeof (hdr); + } ret =qio_channel_read_all_eof (p->c, (char *)pkt_buf, pkt_len, &local_err);@@ -1235,12 +1291,17 @@ static void *multifd_recv_thread(void *opaque)/* recv methods don't know how to handle the SYNC flag */ p->flags &= ~MULTIFD_FLAG_SYNC ; -/* -* Even if it's a SYNC packet, this needs to be set -* because older QEMUs (<9.0) still send data along with -* the SYNC packet. -*/ - has_data = p->normal_num || p->zero_num; +if (is_device_state) { + has_data = p->next_packet_size >0 ; + }else { +/* +* Even if it's a SYNC packet, this needs to be set +* because older QEMUs (<9.0) still send data along with +* the SYNC packet. +*/ + has_data = p->normal_num || p->zero_num; + } +qemu_mutex_unlock (&p->mutex); }else {/* @@ -1269,14 +1330,29 @@ static void *multifd_recv_thread(void *opaque) }if (has_data) { - ret = multifd_recv_state->ops->recv (p, &local_err); +if (is_device_state) { +assert (use_packets); + ret =multifd_device_state_recv (p, &local_err); + }else { + ret = multifd_recv_state->ops->recv (p, &local_err); + }if (ret !=0 ) {break ; } + }else if (is_device_state) { +error_setg (&local_err, +"multifd: received empty device state packet" ); +break ; }if (use_packets) {if (flags &MULTIFD_FLAG_SYNC ) { +if (is_device_state) { +error_setg (&local_err, +"multifd: received SYNC device state packet" ); +break ; + } +qemu_sem_post (&multifd_recv_state->sem_sync);qemu_sem_wait (&p->sem_sync); }@@ -1345,6 +1421,7 @@ int multifd_recv_setup(Error **errp) p->packet_len =sizeof (MultiFDPacket_t) +sizeof (uint64_t ) * page_count; p->packet =g_malloc0 (p->packet_len); + p->packet_dev_state =g_malloc0 (sizeof (*p->packet_dev_state)); } p->name =g_strdup_printf (MIGRATION_THREAD_DST_MULTIFD , i); p->normal =g_new0 (ram_addr_t, page_count);migration/multifd.h +16
-0@@ -62,6 +62,12 @@ MultiFDRecvData *multifd_get_recv_data(void);#define MULTIFD_FLAG_UADK (8 << 1)#define MULTIFD_FLAG_QATZIP (16 << 1) +/* +* If set it means that this packet contains device state +* (MultiFDPacketDeviceState_t), not RAM data (MultiFDPacket_t). +*/ +#define MULTIFD_FLAG_DEVICE_STATE (32 << 1) +/* This value needs to be a multiple of qemu_target_page_size() */ #define MULTIFD_PACKET_SIZE (512 * 1024)@@ -94,6 +100,16 @@ typedef struct {uint64_t offset[]; } __attribute__((packed)) MultiFDPacket_t; +typedef struct { +MultiFDPacketHdr_t hdr; + + char idstr[256] QEMU_NONSTRING; +uint32_t instance_id; + +/* size of the next packet that contains the actual data */ +uint32_t next_packet_size; +} __attribute__((packed))MultiFDPacketDeviceState_t ; + typedef struct { /* number of used pages */ uint32_t num;
migration/multifd.h struct MultiFDRecvData { · line 135
Quoted 9 lines
migration/multifd.h +6
-0@@ -111,6 +127,13 @@ struct MultiFDRecvData {off_t file_offset; }; +typedef struct { +char *idstr; +uint32_t instance_id; +char *buf; +size_t buf_len; +}MultiFDDeviceState_t ;
This is only used in patch #14. Maybe move it there?
Thanks.
Quoted 15 lines
migration/multifd.h +3
-1+ typedef enum { MULTIFD_PAYLOAD_NONE, MULTIFD_PAYLOAD_RAM,@@ -227,8 +250,9 @@ typedef struct {/* thread local variables. No locking required */ -/* pointer to the packet */ +/* pointers to the possible packet types */ MultiFDPacket_t *packet; +MultiFDPacketDeviceState_t *packet_dev_state;/* size of the next packet that contains pages */ uint32_t next_packet_size;/* packets received through this channel */
Re: [PATCH v5 11/36] migration/multifd: Device state transfer support - receive side
Quoted 29 lines · Avihai Horon
Hi Maciej,
Sorry for the long delay, I have been busy with other tasks. I got some small comments for the series.
Quoted 23 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
Add a basic support for receiving device state via multifd channels - channels that are shared with RAM transfers.
Depending whether
MULTIFD_FLAG_DEVICE_STATEflag is present or not in the packet header either device state (MultiFDPacketDeviceState_t) or RAM data (existingMultiFDPacket_t) is read.The received device state data is provided to
qemu_loadvm_load_state_buffer()function for processing in the device'sload_state_bufferhandler.Reviewed-by Peter Xu <[email protected]>Signed-off-by Maciej S. Szmigiero <[email protected]>---
migration/multifd.c| 99 ++++++++++++++++++++++++++++++++++++++++-----migration/multifd.h| 26 +++++++++++-2 files changed, 113 insertions(+), 12 deletions(-)
(..)
Quoted 37 lines
Quoted 37 lines
index
f7156f66c0f6..c2ebef2d319e100644migration/multifd.h +16
-0@@ -62,6 +62,12 @@ MultiFDRecvData *multifd_get_recv_data(void); #define MULTIFD_FLAG_UADK (8 << 1) #define MULTIFD_FLAG_QATZIP (16 << 1) +/* +* If set it means that this packet contains device state +* (MultiFDPacketDeviceState_t), not RAM data (MultiFDPacket_t). +*/ +#define MULTIFD_FLAG_DEVICE_STATE (32 << 1) + /* This value needs to be a multiple of qemu_target_page_size() */ #define MULTIFD_PACKET_SIZE (512 * 1024)@@ -94,6 +100,16 @@ typedef struct { uint64_t offset[]; } __attribute__((packed)) MultiFDPacket_t; +typedef struct { + MultiFDPacketHdr_t hdr; + + char idstr[256] QEMU_NONSTRING; + uint32_t instance_id; + + /* size of the next packet that contains the actual data */ + uint32_t next_packet_size; +} __attribute__((packed))MultiFDPacketDeviceState_t ; + typedef struct { /* number of used pages */ uint32_t num;@@ -111,6 +127,13 @@ struct MultiFDRecvData { off_t file_offset; };
Quoted 8 lines
migration/multifd.h struct MultiFDRecvData { · line 135
Quoted 6 lines
migration/multifd.h +6
-0+typedef struct { + char *idstr; + uint32_t instance_id; + char *buf; + size_t buf_len; +}MultiFDDeviceState_t ;This is only used in patch #14. Maybe move it there?
Moved it to "send side" patch.
Quoted 2 lines
Thanks.
Thanks,
Maciej
PATCH v5 12/36 migration/multifd: Make multifd_send() thread safe
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
multifd_send() function is currently not thread safe, make it thread safe by holding a lock during its execution.
This way it will be possible to safely call it concurrently from multiple threads.
--- migration/multifd.c | 8 ++++++++ 1 file changed, 8 insertions(+)
migration/multifd.c +8 -0
@@ -50,6 +50,10 @@ typedef struct {struct {MultiFDSendParams *params; + +/* multifd_send() body is not thread safe, needs serialization */ +QemuMutex multifd_send_mutex; +/* * Global number of generated multifd packets. *
@@ -339,6 +343,8 @@ bool multifd_send(MultiFDSendData **send_data)return false; } +QEMU_LOCK_GUARD (&multifd_send_state->multifd_send_mutex); +/* We wait here, until at least one channel is ready */ qemu_sem_wait (&multifd_send_state->channels_ready);
@@ -507,6 +513,7 @@ static void multifd_send_cleanup_state(void)socket_cleanup_outgoing_migration ();qemu_sem_destroy (&multifd_send_state->channels_created);qemu_sem_destroy (&multifd_send_state->channels_ready); +qemu_mutex_destroy (&multifd_send_state->multifd_send_mutex);g_free (multifd_send_state->params); multifd_send_state->params =NULL ;g_free (multifd_send_state);
@@ -887,6 +894,7 @@ bool multifd_send_setup(void) thread_count =migrate_multifd_channels (); multifd_send_state =g_malloc0 (sizeof (*multifd_send_state)); multifd_send_state->params =g_new0 (MultiFDSendParams, thread_count); +qemu_mutex_init (&multifd_send_state->multifd_send_mutex);qemu_sem_init (&multifd_send_state->channels_created,0 );qemu_sem_init (&multifd_send_state->channels_ready,0 );qatomic_set (&multifd_send_state->exiting,0 );
PATCH v5 13/36 migration/multifd: Add an explicit MultiFDSendData destructor
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
This way if there are fields there that needs explicit disposal (like, for example, some attached buffers) they will be handled appropriately.
Add a related assert to multifd_set_payload_type() in order to make sure that this function is only used to fill a previously empty MultiFDSendData with some payload, not the other way around.
--- migration/multifd-nocomp.c | 3 +-- migration/multifd.c | 31 ++++++++++++++++++++++++++++--- migration/multifd.h | 5 +++++ 3 files changed, 34 insertions(+), 5 deletions(-)
migration/multifd-nocomp.c +1 -2
@@ -42,8 +42,7 @@ void multifd_ram_save_setup(void)void multifd_ram_save_cleanup (void ) { -g_free (multifd_ram_send); - multifd_ram_send =NULL ; +g_clear_pointer (&multifd_ram_send, multifd_send_data_free); }static void multifd_set_file_bitmap (MultiFDSendParams *p)
migration/multifd.c +28 -3
@@ -123,6 +123,32 @@ MultiFDSendData *multifd_send_data_alloc(void)return g_malloc0 (size_minus_payload + max_payload_size); } +void multifd_send_data_clear (MultiFDSendData *data) +{ +if (multifd_payload_empty (data)) { +return ; + } + +switch (data->type) { +default : +/* Nothing to do */ +break ; + } + + data->type =MULTIFD_PAYLOAD_NONE ; +} + +void multifd_send_data_free (MultiFDSendData *data) +{ +if (!data) { +return ; + } + +multifd_send_data_clear (data); + +g_free (data); +} +static bool multifd_use_packets (void ) {return !migrate_mapped_ram ();
@@ -496,8 +522,7 @@ static bool multifd_send_cleanup_channel(MultiFDSendParams *p, Error **errp)qemu_sem_destroy (&p->sem_sync);g_free (p->name); p->name =NULL ; -g_free (p->data); - p->data =NULL ; +g_clear_pointer (&p->data, multifd_send_data_free); p->packet_len =0 ;g_free (p->packet); p->packet =NULL ;
@@ -695,7 +720,7 @@ static void *multifd_send_thread(void *opaque) (uint64_t)p->next_packet_size + p->packet_len); p->next_packet_size =0 ; -multifd_set_payload_type (p->data,MULTIFD_PAYLOAD_NONE ); +multifd_send_data_clear (p->data);/* * Making sure p->data is published before saying "we're
migration/multifd.h +5 -0
@@ -156,6 +156,9 @@ static inline bool multifd_payload_empty(MultiFDSendData *data)static inline void multifd_set_payload_type (MultiFDSendData *data,MultiFDPayloadType type) { +assert (multifd_payload_empty (data)); +assert (type !=MULTIFD_PAYLOAD_NONE ); + data->type = type; }
@@ -372,6 +375,8 @@ static inline void multifd_send_prepare_header(MultiFDSendParams *p)void multifd_channel_connect (MultiFDSendParams *p,QIOChannel *ioc);bool multifd_send (MultiFDSendData **send_data);MultiFDSendData *multifd_send_data_alloc (void ); +void multifd_send_data_clear (MultiFDSendData *data); +void multifd_send_data_free (MultiFDSendData *data);static inline uint32_t multifd_ram_page_size (void ) {
PATCH v5 14/36 migration/multifd: Device state transfer support - send side
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
A new function multifd_queue_device_state() is provided for device to queue its state for transmission via a multifd channel.
--- include/migration/misc.h | 4 ++ migration/meson.build | 1 + migration/multifd-device-state.c | 115 +++++++++++++++++++++++++++++++ migration/multifd-nocomp.c | 14 +++- migration/multifd.c | 42 +++++++++-- migration/multifd.h | 27 +++++--- 6 files changed, 187 insertions(+), 16 deletions(-) create mode 100644 migration/multifd-device-state.c
include/migration/misc.h +4 -0
@@ -118,4 +118,8 @@ bool migrate_is_uri(const char *uri);bool migrate_uri_parse (const char *uri,MigrationChannel **channel,Error **errp); +/* migration/multifd-device-state.c */ +bool multifd_queue_device_state (char *idstr,uint32_t instance_id, +char *data,size_t len); +#endif
migration/meson.build +1 -0
@@ -25,6 +25,7 @@ system_ss.add(files( 'migration-hmp-cmds.c', 'migration.c', 'multifd.c', + 'multifd-device-state.c', 'multifd-nocomp.c', 'multifd-zlib.c', 'multifd-zero-page.c',
migration/multifd-device-state.c new file +115 -0
115 lines not on this page · raw message
migration/multifd-nocomp.c +12 -2
@@ -14,6 +14,7 @@#include "exec/ramblock.h" #include "exec/target_page.h" #include "file.h" +#include "migration-stats.h" #include "multifd.h" #include "options.h" #include "qapi/error.h"
@@ -85,6 +86,13 @@ static void multifd_nocomp_send_cleanup(MultiFDSendParams *p, Error **errp)return ; } +static void multifd_ram_prepare_header (MultiFDSendParams *p) +{ + p->iov[0 ].iov_len = p->packet_len; + p->iov[0 ].iov_base = p->packet; + p->iovs_num++; +} +static void multifd_send_prepare_iovs (MultiFDSendParams *p) {MultiFDPages_t *pages = &p->data->u.ram;
@@ -118,7 +126,7 @@ static int multifd_nocomp_send_prepare(MultiFDSendParams *p, Error **errp)* Only !zerocopy needs the header in IOV; zerocopy will * send it separately. */ -multifd_send_prepare_header (p); +multifd_ram_prepare_header (p); }multifd_send_prepare_iovs (p);
@@ -133,6 +141,8 @@ static int multifd_nocomp_send_prepare(MultiFDSendParams *p, Error **errp)if (ret !=0 ) {return -1 ; } + +stat64_add (&mig_stats.multifd_bytes, p->packet_len); }return 0 ;
@@ -431,7 +441,7 @@ int multifd_ram_flush_and_sync(QEMUFile *f)bool multifd_send_prepare_common (MultiFDSendParams *p) {MultiFDPages_t *pages = &p->data->u.ram; -multifd_send_prepare_header (p); +multifd_ram_prepare_header (p);multifd_send_zero_page_detect (p);if (!pages->normal_num) {
migration/multifd.c +36 -6
115 lines not on this page · raw message
migration/multifd.h +19 -8
@@ -137,10 +137,12 @@ typedef struct {typedef enum {MULTIFD_PAYLOAD_NONE ,MULTIFD_PAYLOAD_RAM , +MULTIFD_PAYLOAD_DEVICE_STATE , }MultiFDPayloadType ;typedef union MultiFDPayload {MultiFDPages_t ram; +MultiFDDeviceState_t device_state; }MultiFDPayload ;struct MultiFDSendData {
@@ -153,6 +155,11 @@ static inline bool multifd_payload_empty(MultiFDSendData *data)return data->type ==MULTIFD_PAYLOAD_NONE ; } +static inline bool multifd_payload_device_state (MultiFDSendData *data) +{ +return data->type ==MULTIFD_PAYLOAD_DEVICE_STATE ; +} +static inline void multifd_set_payload_type (MultiFDSendData *data,MultiFDPayloadType type) {
@@ -205,8 +212,9 @@ typedef struct {/* thread local variables. No locking required */ -/* pointer to the packet */ +/* pointers to the possible packet types */ MultiFDPacket_t *packet; +MultiFDPacketDeviceState_t *packet_device_state;/* size of the next packet that contains pages */ uint32_t next_packet_size;/* packets sent through this channel */
@@ -365,13 +373,6 @@ bool multifd_send_prepare_common(MultiFDSendParams *p);void multifd_send_zero_page_detect (MultiFDSendParams *p);void multifd_recv_zero_page_process (MultiFDRecvParams *p); -static inline void multifd_send_prepare_header (MultiFDSendParams *p) -{ - p->iov[0 ].iov_len = p->packet_len; - p->iov[0 ].iov_base = p->packet; - p->iovs_num++; -} -void multifd_channel_connect (MultiFDSendParams *p,QIOChannel *ioc);bool multifd_send (MultiFDSendData **send_data);MultiFDSendData *multifd_send_data_alloc (void );
@@ -396,4 +397,14 @@ bool multifd_ram_sync_per_section(void);size_t multifd_ram_payload_size (void );void multifd_ram_fill_packet (MultiFDSendParams *p);int multifd_ram_unfill_packet (MultiFDRecvParams *p,Error **errp); + +size_t multifd_device_state_payload_size (void ); + +void multifd_send_data_clear_device_state (MultiFDDeviceState_t *device_state); + +void multifd_device_state_send_setup (void ); +void multifd_device_state_send_cleanup (void ); + +void multifd_device_state_send_prepare (MultiFDSendParams *p); +#endif
Re: [PATCH v5 14/36] migration/multifd: Device state transfer support - send side
Quoted 99 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
A new function
multifd_queue_device_state()is provided for device to queue its state for transmission via a multifd channel.Reviewed-by Peter Xu <[email protected]>Signed-off-by Maciej S. Szmigiero <[email protected]>--- include/migration/misc.h | 4 ++ migration/meson.build | 1 + migration/multifd-device-state.c | 115 +++++++++++++++++++++++++++++++ migration/multifd-nocomp.c | 14 +++- migration/multifd.c | 42 +++++++++-- migration/multifd.h | 27 +++++--- 6 files changed, 187 insertions(+), 16 deletions(-)create mode 100644
migration/multifd-device-state.cinclude/migration/misc.h +4
-0@@ -118,4 +118,8 @@ bool migrate_is_uri(const char *uri);bool migrate_uri_parse (const char *uri,MigrationChannel **channel,Error **errp); +/* migration/multifd-device-state.c */ +bool multifd_queue_device_state (char *idstr,uint32_t instance_id, +char *data,size_t len); +#endif migration/meson.build +1
-0@@ -25,6 +25,7 @@ system_ss.add(files( 'migration-hmp-cmds.c', 'migration.c', 'multifd.c', + 'multifd-device-state.c', 'multifd-nocomp.c', 'multifd-zlib.c', 'multifd-zero-page.c',migration/multifd-device-state.c new file +48
-0@@ -0,0 +1,115 @@ +/* +* Multifd device state migration +* +* Copyright (C) 2024,2025 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qemu/lockable.h" +#include "migration/misc.h" +#include "multifd.h" + +static struct { +QemuMutex queue_job_mutex; + +MultiFDSendData *send_data; +} *multifd_send_device_state; + +size_t multifd_device_state_payload_size (void ) +{ +return sizeof (MultiFDDeviceState_t); +} + +void multifd_device_state_send_setup (void ) +{ +assert (!multifd_send_device_state); + multifd_send_device_state =g_malloc (sizeof (*multifd_send_device_state)); + +qemu_mutex_init (&multifd_send_device_state->queue_job_mutex); + + multifd_send_device_state->send_data =multifd_send_data_alloc (); +} + +void multifd_device_state_send_cleanup (void ) +{ +g_clear_pointer (&multifd_send_device_state->send_data, + multifd_send_data_free); + +qemu_mutex_destroy (&multifd_send_device_state->queue_job_mutex); + +g_clear_pointer (&multifd_send_device_state, g_free); +} + +void multifd_send_data_clear_device_state (MultiFDDeviceState_t *device_state) +{ +g_clear_pointer (&device_state->idstr, g_free);
migration/multifd-device-state.c line 58
Quoted 10 lines
migration/multifd-device-state.c new file +10
-0+g_clear_pointer (&device_state->buf, g_free); +} + +static void multifd_device_state_fill_packet (MultiFDSendParams *p) +{ +MultiFDDeviceState_t *device_state = &p->data->u.device_state; +MultiFDPacketDeviceState_t *packet = p->packet_device_state; + + packet->hdr.flags =cpu_to_be32 (p->flags); +strncpy (packet->idstr, device_state->idstr,sizeof (packet->idstr));
(I think we talked about this in v2):
Looking at idstr creation code, idstr is always NULL terminated. It's also treated everywhere as a NULL terminated string.
For consistency and to avoid confusion, I'd treat it as a NULL terminated string here too (use strcpy, remove the QEMU_NONSTRING from its definition, etc.). This will also avoid strncpy() unnecessary zeroing of the extra bytes.
Thanks.
Quoted 309 lines
migration/multifd-device-state.c new file +57
-0+ packet->instance_id =cpu_to_be32 (device_state->instance_id); + packet->next_packet_size =cpu_to_be32 (p->next_packet_size); +} + +static void multifd_prepare_header_device_state (MultiFDSendParams *p) +{ + p->iov[0 ].iov_len =sizeof (*p->packet_device_state); + p->iov[0 ].iov_base = p->packet_device_state; + p->iovs_num++; +} + +void multifd_device_state_send_prepare (MultiFDSendParams *p) +{ +MultiFDDeviceState_t *device_state = &p->data->u.device_state; + +assert (multifd_payload_device_state (p->data)); + +multifd_prepare_header_device_state (p); + +assert (!(p->flags &MULTIFD_FLAG_SYNC )); + + p->next_packet_size = device_state->buf_len; +if (p->next_packet_size >0 ) { + p->iov[p->iovs_num].iov_base = device_state->buf; + p->iov[p->iovs_num].iov_len = p->next_packet_size; + p->iovs_num++; + } + + p->flags |=MULTIFD_FLAG_NOCOMP |MULTIFD_FLAG_DEVICE_STATE ; + +multifd_device_state_fill_packet (p); +} + +bool multifd_queue_device_state (char *idstr,uint32_t instance_id, +char *data,size_t len) +{ +/* Device state submissions can come from multiple threads */ +QEMU_LOCK_GUARD (&multifd_send_device_state->queue_job_mutex); +MultiFDDeviceState_t *device_state; + +assert (multifd_payload_empty (multifd_send_device_state->send_data)); + +multifd_set_payload_type (multifd_send_device_state->send_data, +MULTIFD_PAYLOAD_DEVICE_STATE ); + device_state = &multifd_send_device_state->send_data->u.device_state; + device_state->idstr =g_strdup (idstr); + device_state->instance_id = instance_id; + device_state->buf =g_memdup2 (data, len); + device_state->buf_len = len; + +if (!multifd_send (&multifd_send_device_state->send_data)) { +multifd_send_data_clear (multifd_send_device_state->send_data); +return false; + } + +return true; +}migration/multifd-nocomp.c +12
-2@@ -14,6 +14,7 @@#include "exec/ramblock.h" #include "exec/target_page.h" #include "file.h" +#include "migration-stats.h" #include "multifd.h" #include "options.h" #include "qapi/error.h" @@ -85,6 +86,13 @@ static void multifd_nocomp_send_cleanup(MultiFDSendParams *p, Error **errp)return ; } +static void multifd_ram_prepare_header (MultiFDSendParams *p) +{ + p->iov[0 ].iov_len = p->packet_len; + p->iov[0 ].iov_base = p->packet; + p->iovs_num++; +} +static void multifd_send_prepare_iovs (MultiFDSendParams *p) {MultiFDPages_t *pages = &p->data->u.ram;@@ -118,7 +126,7 @@ static int multifd_nocomp_send_prepare(MultiFDSendParams *p, Error **errp)* Only !zerocopy needs the header in IOV; zerocopy will * send it separately. */ -multifd_send_prepare_header (p); +multifd_ram_prepare_header (p); }multifd_send_prepare_iovs (p);@@ -133,6 +141,8 @@ static int multifd_nocomp_send_prepare(MultiFDSendParams *p, Error **errp)if (ret !=0 ) {return -1 ; } + +stat64_add (&mig_stats.multifd_bytes, p->packet_len); }return 0 ;@@ -431,7 +441,7 @@ int multifd_ram_flush_and_sync(QEMUFile *f)bool multifd_send_prepare_common (MultiFDSendParams *p) {MultiFDPages_t *pages = &p->data->u.ram; -multifd_send_prepare_header (p); +multifd_ram_prepare_header (p);multifd_send_zero_page_detect (p);if (!pages->normal_num) {migration/multifd.c +36
-6@@ -12,6 +12,7 @@#include "qemu/osdep.h" #include "qemu/cutils.h" +#include "qemu/iov.h" #include "qemu/rcu.h" #include "exec/target_page.h" #include "system/system.h" @@ -19,6 +20,7 @@#include "qemu/error-report.h" #include "qapi/error.h" #include "file.h" +#include "migration/misc.h" #include "migration.h" #include "migration-stats.h" #include "savevm.h" @@ -111,7 +113,9 @@ MultiFDSendData *multifd_send_data_alloc(void)* added to the union in the future are larger than * (MultiFDPages_t + flex array). */ - max_payload_size =MAX (multifd_ram_payload_size (),sizeof (MultiFDPayload)); + max_payload_size =MAX (multifd_ram_payload_size (), +multifd_device_state_payload_size ()); + max_payload_size =MAX (max_payload_size,sizeof (MultiFDPayload));/* * Account for any holes the compiler might insert. We can't pack @@ -130,6 +134,9 @@ void multifd_send_data_clear(MultiFDSendData *data) }switch (data->type) { +case MULTIFD_PAYLOAD_DEVICE_STATE : +multifd_send_data_clear_device_state (&data->u.device_state); +break ;default :/* Nothing to do */ break ;@@ -232,6 +239,7 @@ static int multifd_recv_initial_packet(QIOChannel *c, Error **errp)return msg.id; } +/* Fills a RAM multifd packet */ void multifd_send_fill_packet (MultiFDSendParams *p) {MultiFDPacket_t *packet = p->packet;@@ -524,6 +532,7 @@ static bool multifd_send_cleanup_channel(MultiFDSendParams *p, Error **errp) p->name =NULL ;g_clear_pointer (&p->data, multifd_send_data_free); p->packet_len =0 ; +g_clear_pointer (&p->packet_device_state, g_free);g_free (p->packet); p->packet =NULL ; multifd_send_state->ops->send_cleanup (p, errp);@@ -536,6 +545,7 @@ static void multifd_send_cleanup_state(void) {file_cleanup_outgoing_migration ();socket_cleanup_outgoing_migration (); +multifd_device_state_send_cleanup ();qemu_sem_destroy (&multifd_send_state->channels_created);qemu_sem_destroy (&multifd_send_state->channels_ready);qemu_mutex_destroy (&multifd_send_state->multifd_send_mutex);@@ -694,16 +704,32 @@ static void *multifd_send_thread(void *opaque)* qatomic_store_release() in multifd_send(). */ if (qatomic_load_acquire (&p->pending_job)) { +bool is_device_state =multifd_payload_device_state (p->data); +size_t total_size; + p->flags =0 ; p->iovs_num =0 ;assert (!multifd_payload_empty (p->data)); - ret = multifd_send_state->ops->send_prepare (p, &local_err); -if (ret !=0 ) { -break ; +if (is_device_state) { +multifd_device_state_send_prepare (p); + }else { + ret = multifd_send_state->ops->send_prepare (p, &local_err); +if (ret !=0 ) { +break ; + } } +/* +* The packet header in the zerocopy RAM case is accounted for +* in multifd_nocomp_send_prepare() - where it is actually +* being sent. +*/ + total_size =iov_size (p->iov, p->iovs_num); +if (migrate_mapped_ram ()) { +assert (!is_device_state); + ret =file_write_ramblock_iov (p->c, p->iov, p->iovs_num, &p->data->u.ram, &local_err); }else {@@ -716,8 +742,7 @@ static void *multifd_send_thread(void *opaque)break ; } -stat64_add (&mig_stats.multifd_bytes, - (uint64_t )p->next_packet_size + p->packet_len); +stat64_add (&mig_stats.multifd_bytes, total_size); p->next_packet_size =0 ;multifd_send_data_clear (p->data);@@ -938,6 +963,9 @@ bool multifd_send_setup(void) p->packet_len =sizeof (MultiFDPacket_t) +sizeof (uint64_t ) * page_count; p->packet =g_malloc0 (p->packet_len); + p->packet_device_state =g_malloc0 (sizeof (*p->packet_device_state)); + p->packet_device_state->hdr.magic =cpu_to_be32 (MULTIFD_MAGIC ); + p->packet_device_state->hdr.version =cpu_to_be32 (MULTIFD_VERSION ); } p->name =g_strdup_printf (MIGRATION_THREAD_SRC_MULTIFD , i); p->write_flags =0 ;@@ -973,6 +1001,8 @@ bool multifd_send_setup(void)assert (p->iov); } +multifd_device_state_send_setup (); +return true; err:migration/multifd.h +19
-8@@ -137,10 +137,12 @@ typedef struct {typedef enum {MULTIFD_PAYLOAD_NONE ,MULTIFD_PAYLOAD_RAM , +MULTIFD_PAYLOAD_DEVICE_STATE , }MultiFDPayloadType ;typedef union MultiFDPayload {MultiFDPages_t ram; +MultiFDDeviceState_t device_state; }MultiFDPayload ;struct MultiFDSendData {@@ -153,6 +155,11 @@ static inline bool multifd_payload_empty(MultiFDSendData *data)return data->type ==MULTIFD_PAYLOAD_NONE ; } +static inline bool multifd_payload_device_state (MultiFDSendData *data) +{ +return data->type ==MULTIFD_PAYLOAD_DEVICE_STATE ; +} +static inline void multifd_set_payload_type (MultiFDSendData *data,MultiFDPayloadType type) {@@ -205,8 +212,9 @@ typedef struct {/* thread local variables. No locking required */ -/* pointer to the packet */ +/* pointers to the possible packet types */ MultiFDPacket_t *packet; +MultiFDPacketDeviceState_t *packet_device_state;/* size of the next packet that contains pages */ uint32_t next_packet_size;/* packets sent through this channel */ @@ -365,13 +373,6 @@ bool multifd_send_prepare_common(MultiFDSendParams *p);void multifd_send_zero_page_detect (MultiFDSendParams *p);void multifd_recv_zero_page_process (MultiFDRecvParams *p); -static inline void multifd_send_prepare_header (MultiFDSendParams *p) -{ - p->iov[0 ].iov_len = p->packet_len; - p->iov[0 ].iov_base = p->packet; - p->iovs_num++; -} -void multifd_channel_connect (MultiFDSendParams *p,QIOChannel *ioc);bool multifd_send (MultiFDSendData **send_data);MultiFDSendData *multifd_send_data_alloc (void );@@ -396,4 +397,14 @@ bool multifd_ram_sync_per_section(void);size_t multifd_ram_payload_size (void );void multifd_ram_fill_packet (MultiFDSendParams *p);int multifd_ram_unfill_packet (MultiFDRecvParams *p,Error **errp); + +size_t multifd_device_state_payload_size (void ); + +void multifd_send_data_clear_device_state (MultiFDDeviceState_t *device_state); + +void multifd_device_state_send_setup (void ); +void multifd_device_state_send_cleanup (void ); + +void multifd_device_state_send_prepare (MultiFDSendParams *p); +#endif
Re: [PATCH v5 14/36] migration/multifd: Device state transfer support - send side
Quoted 107 lines · Avihai Horon
Quoted 105 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
A new function
multifd_queue_device_state()is provided for device to queue its state for transmission via a multifd channel.Reviewed-by Peter Xu <[email protected]>Signed-off-by Maciej S. Szmigiero <[email protected]>---
include/migration/misc.h | 4 ++ migration/meson.build | 1 +migration/multifd-device-state.c| 115 +++++++++++++++++++++++++++++++ migration/multifd-nocomp.c | 14 +++- migration/multifd.c | 42 +++++++++-- migration/multifd.h | 27 +++++---6 files changed, 187 insertions(+), 16 deletions(-)create mode 100644
migration/multifd-device-state.cinclude/migration/misc.h +4
-0@@ -118,4 +118,8 @@ bool migrate_is_uri(const char *uri); bool migrate_uri_parse(const char *uri, MigrationChannel **channel, Error **errp); +/* migration/multifd-device-state.c */ +bool multifd_queue_device_state (char *idstr,uint32_t instance_id, + char *data, size_t len); + #endifmigration/meson.build +1
-0@@ -25,6 +25,7 @@ system_ss.add(files( 'migration-hmp-cmds.c', 'migration.c', 'multifd.c', + 'multifd-device-state.c', 'multifd-nocomp.c', 'multifd-zlib.c', 'multifd-zero-page.c',migration/multifd-device-state.c new file +54
-0@@ -0,0 +1,115 @@ +/* +* Multifd device state migration +* +* Copyright (C) 2024,2025 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qemu/lockable.h" +#include "migration/misc.h" +#include "multifd.h" + +static struct { + QemuMutex queue_job_mutex; + + MultiFDSendData *send_data; +} *multifd_send_device_state; + +size_t multifd_device_state_payload_size (void ) +{ + return sizeof(MultiFDDeviceState_t); +} + +void multifd_device_state_send_setup (void ) +{ + assert(!multifd_send_device_state); + multifd_send_device_state = g_malloc(sizeof(*multifd_send_device_state)); + + qemu_mutex_init(&multifd_send_device_state->queue_job_mutex); + + multifd_send_device_state->send_data = multifd_send_data_alloc(); +} + +void multifd_device_state_send_cleanup (void ) +{ + g_clear_pointer(&multifd_send_device_state->send_data, + multifd_send_data_free); + + qemu_mutex_destroy(&multifd_send_device_state->queue_job_mutex); + + g_clear_pointer(&multifd_send_device_state, g_free); +} + +void multifd_send_data_clear_device_state (MultiFDDeviceState_t *device_state) +{ + g_clear_pointer(&device_state->idstr, g_free); + g_clear_pointer(&device_state->buf, g_free); +} + +static void multifd_device_state_fill_packet (MultiFDSendParams *p) +{ + MultiFDDeviceState_t *device_state = &p->data->u.device_state;
Quoted 8 lines
migration/multifd-device-state.c line 58
Quoted 4 lines
migration/multifd-device-state.c new file +4
-0+ MultiFDPacketDeviceState_t *packet = p->packet_device_state; + + packet->hdr.flags = cpu_to_be32(p->flags); + strncpy(packet->idstr, device_state->idstr, sizeof(packet->idstr));(I think we talked about this in v2):
Looking at idstr creation code, idstr is always NULL terminated. It's also treated everywhere as a NULL terminated string.
For consistency and to avoid confusion, I'd treat it as a NULL terminated string here too (use strcpy, remove theQEMU_NONSTRINGfrom its definition, etc.).
Changed to NULL-terminated since AFAIK RAM idstr was also changed to such in the meantime.
Quoted 1 line
This will also avoid
strncpy()unnecessary zeroing of the extra bytes.
Zeroing of remaining space is still necessary since it's a wire packet data structure that's re-used between packets so it still can contain remainder of previous longer idstr.
Quoted 2 lines
Thanks.
Thanks,
Maciej
PATCH v5 15/36 migration/multifd: Make MultiFDSendData a struct
In reply to Maciej S. SzmigieroFrom: Peter Xu <[email protected]>
The newly introduced device state buffer can be used for either storing VFIO's read() raw data, but already also possible to store generic device states. After noticing that device states may not easily provide a max buffer size (also the fact that RAM MultiFDPages_t after all also want to have flexibility on managing offset[] array), it may not be a good idea to stick with union on MultiFDSendData.. as it won't play well with such flexibility.
Switch MultiFDSendData to a struct.
It won't consume a lot more space in reality, after all the real buffers were already dynamically allocated, so it's so far only about the two structs (pages, device_state) that will be duplicated, but they're small.
With this, we can remove the pretty hard to understand alloc size logic. Because now we can allocate offset[] together with the SendData, and properly free it when the SendData is freed.
[MSS: Make sure to clear possible device state payload before freeing MultiFDSendData, remove placeholders for other patches not included]
--- migration/multifd-device-state.c | 5 ----- migration/multifd-nocomp.c | 13 ++++++------- migration/multifd.c | 25 +++++++------------------ migration/multifd.h | 15 +++++++++------ 4 files changed, 22 insertions(+), 36 deletions(-)
migration/multifd-device-state.c +0 -5
@@ -18,11 +18,6 @@ static struct {MultiFDSendData *send_data; } *multifd_send_device_state; -size_t multifd_device_state_payload_size (void ) -{ -return sizeof (MultiFDDeviceState_t); -} -void multifd_device_state_send_setup (void ) {assert (!multifd_send_device_state);
migration/multifd-nocomp.c +6 -7
@@ -25,15 +25,14 @@static MultiFDSendData *multifd_ram_send; -size_t multifd_ram_payload_size (void ) +void multifd_ram_payload_alloc (MultiFDPages_t *pages) { -uint32_t n =multifd_ram_page_count (); + pages->offset =g_new0 (ram_addr_t,multifd_ram_page_count ()); +} -/* -* We keep an array of page offsets at the end of MultiFDPages_t, -* add space for it in the allocation. -*/ -return sizeof (MultiFDPages_t) + n *sizeof (ram_addr_t); +void multifd_ram_payload_free (MultiFDPages_t *pages) +{ +g_clear_pointer (&pages->offset, g_free); }void multifd_ram_save_setup (void )
migration/multifd.c +7 -18
@@ -105,26 +105,12 @@ struct {MultiFDSendData *multifd_send_data_alloc (void ) { -size_t max_payload_size, size_minus_payload; +MultiFDSendData *new =g_new0 (MultiFDSendData,1 ); -/* -* MultiFDPages_t has a flexible array at the end, account for it -* when allocating MultiFDSendData. Use max() in case other types -* added to the union in the future are larger than -* (MultiFDPages_t + flex array). -*/ - max_payload_size =MAX (multifd_ram_payload_size (), -multifd_device_state_payload_size ()); - max_payload_size =MAX (max_payload_size,sizeof (MultiFDPayload)); - -/* -* Account for any holes the compiler might insert. We can't pack -* the structure because that misaligns the members and triggers -* Waddress-of-packed-member. -*/ - size_minus_payload =sizeof (MultiFDSendData) -sizeof (MultiFDPayload); +multifd_ram_payload_alloc (&new->u.ram); +/* Device state allocates its payload on-demand */ -return g_malloc0 (size_minus_payload + max_payload_size); +return new; }void multifd_send_data_clear (MultiFDSendData *data)
@@ -151,8 +137,11 @@ void multifd_send_data_free(MultiFDSendData *data)return ; } +/* This also free's device state payload */ multifd_send_data_clear (data); +multifd_ram_payload_free (&data->u.ram); +g_free (data); }
migration/multifd.h +9 -6
@@ -115,9 +115,13 @@ typedef struct {uint32_t num;/* number of normal pages */ uint32_t normal_num; +/* +* Pointer to the ramblock. NOTE: it's caller's responsibility to make +* sure the pointer is always valid! +*/ RAMBlock *block; -/* offset of each page */ -ram_addr_t offset[]; +/* offset array of each page, managed by multifd */ +ram_addr_t *offset; } MultiFDPages_t;struct MultiFDRecvData {
@@ -140,7 +144,7 @@ typedef enum {MULTIFD_PAYLOAD_DEVICE_STATE , } MultiFDPayloadType; -typedef union MultiFDPayload { +typedef struct MultiFDPayload {MultiFDPages_t ram;MultiFDDeviceState_t device_state; }MultiFDPayload ;
@@ -394,12 +398,11 @@ void multifd_ram_save_cleanup(void);int multifd_ram_flush_and_sync (QEMUFile *f);bool multifd_ram_sync_per_round (void );bool multifd_ram_sync_per_section (void ); -size_t multifd_ram_payload_size (void ); +void multifd_ram_payload_alloc (MultiFDPages_t *pages); +void multifd_ram_payload_free (MultiFDPages_t *pages);void multifd_ram_fill_packet (MultiFDSendParams *p);int multifd_ram_unfill_packet (MultiFDRecvParams *p,Error **errp); -size_t multifd_device_state_payload_size (void ); -void multifd_send_data_clear_device_state (MultiFDDeviceState_t *device_state);void multifd_device_state_send_setup (void );
PATCH v5 16/36 migration/multifd: Add multifd_device_state_supported()
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Since device state transfer via multifd channels requires multifd channels with packets and is currently not compatible with multifd compression add an appropriate query function so device can learn whether it can actually make use of it.
--- include/migration/misc.h | 1 + migration/multifd-device-state.c | 7 +++++++ 2 files changed, 8 insertions(+)
include/migration/misc.h +1 -0
@@ -121,5 +121,6 @@ bool migrate_uri_parse(const char *uri, MigrationChannel **channel,/* migration/multifd-device-state.c */ bool multifd_queue_device_state (char *idstr,uint32_t instance_id,char *data,size_t len); +bool multifd_device_state_supported (void );#endif
migration/multifd-device-state.c +7 -0
@@ -11,6 +11,7 @@#include "qemu/lockable.h" #include "migration/misc.h" #include "multifd.h" +#include "options.h" static struct { QemuMutex queue_job_mutex;
@@ -108,3 +109,9 @@ bool multifd_queue_device_state(char *idstr, uint32_t instance_id,return true; } + +bool multifd_device_state_supported (void ) +{ +return migrate_multifd () && !migrate_mapped_ram () && +migrate_multifd_compression () ==MULTIFD_COMPRESSION_NONE ; +}
PATCH v5 17/36 migration: Add save_live_complete_precopy_thread handler
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
This SaveVMHandler helps device provide its own asynchronous transmission of the remaining data at the end of a precopy phase via multifd channels, in parallel with the transfer done by save_live_complete_precopy handlers.
These threads are launched only when multifd device state transfer is supported.
Management of these threads in done in the multifd migration code, wrapping them in the generic thread pool.
--- include/migration/misc.h | 17 +++++++ include/migration/register.h | 19 +++++++ include/qemu/typedefs.h | 3 ++ migration/multifd-device-state.c | 85 ++++++++++++++++++++++++++++++++ migration/savevm.c | 35 ++++++++++++- 5 files changed, 158 insertions(+), 1 deletion(-)
include/migration/misc.h +17 -0
@@ -119,8 +119,25 @@ bool migrate_uri_parse(const char *uri, MigrationChannel **channel, Error **errp);/* migration/multifd-device-state.c */ +typedef struct SaveLiveCompletePrecopyThreadData { +SaveLiveCompletePrecopyThreadHandler hdlr; +char *idstr; +uint32_t instance_id; +void *handler_opaque; +}SaveLiveCompletePrecopyThreadData ; +bool multifd_queue_device_state (char *idstr,uint32_t instance_id,char *data,size_t len);bool multifd_device_state_supported (void ); +void +multifd_spawn_device_state_save_thread (SaveLiveCompletePrecopyThreadHandler hdlr, +char *idstr,uint32_t instance_id, +void *opaque); + +bool multifd_device_state_save_thread_should_exit (void ); + +void multifd_abort_device_state_save_threads (void ); +bool multifd_join_device_state_save_threads (void ); +#endif
include/migration/register.h +19 -0
@@ -105,6 +105,25 @@ typedef struct SaveVMHandlers {*/ int (*save_live_complete_precopy)(QEMUFile *f,void *opaque); +/** +* @save_live_complete_precopy_thread (invoked in a separate thread) +* +* Called at the end of a precopy phase from a separate worker thread +* in configurations where multifd device state transfer is supported +* in order to perform asynchronous transmission of the remaining data in +* parallel with @save_live_complete_precopy handlers. +* When postcopy is enabled, devices that support postcopy will skip this +* step. +* +* @d: a #SaveLiveCompletePrecopyThreadData containing parameters that the +* handler may need, including this device section idstr and instance_id, +* and opaque data pointer passed to register_savevm_live(). +* @errp: pointer to Error*, to store an error if it happens. +* +* Returns true to indicate success and false for errors. +*/ +SaveLiveCompletePrecopyThreadHandler save_live_complete_precopy_thread; +/* This runs both outside and inside the BQL. */ /**
include/qemu/typedefs.h +3 -0
@@ -108,6 +108,7 @@ typedef struct QString QString;typedef struct RAMBlock RAMBlock ;typedef struct Range Range ;typedef struct ReservedRegion ReservedRegion ; +typedef struct SaveLiveCompletePrecopyThreadData SaveLiveCompletePrecopyThreadData ;typedef struct SHPCDevice SHPCDevice ;typedef struct SSIBus SSIBus ;typedef struct TCGCPUOps TCGCPUOps ;
@@ -133,5 +134,7 @@ typedef struct IRQState *qemu_irq;typedef void (*qemu_irq_handler )(void *opaque,int n,int level);typedef bool (*MigrationLoadThread )(void *opaque,bool *should_quit,Error **errp); +typedef bool (*SaveLiveCompletePrecopyThreadHandler )(SaveLiveCompletePrecopyThreadData *d, +Error **errp);#endif /* QEMU_TYPEDEFS_H */
migration/multifd-device-state.c +85 -0
111 lines not on this page · raw message
migration/savevm.c +34 -1
@@ -37,6 +37,7 @@#include "migration/register.h" #include "migration/global_state.h" #include "migration/channel-block.h" +#include "multifd.h" #include "ram.h" #include "qemu-file.h" #include "savevm.h"
@@ -1527,6 +1528,24 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)int64_t start_ts_each, end_ts_each;SaveStateEntry *se;int ret; +bool multifd_device_state =multifd_device_state_supported (); + +if (multifd_device_state) { + QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { +SaveLiveCompletePrecopyThreadHandler hdlr; + +if (!se->ops || (in_postcopy && se->ops->has_postcopy && + se->ops->has_postcopy (se->opaque)) || + !se->ops->save_live_complete_precopy_thread) { +continue ; + } + + hdlr = se->ops->save_live_complete_precopy_thread; +multifd_spawn_device_state_save_thread (hdlr, + se->idstr, se->instance_id, + se->opaque); + } + } QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { if (!se->ops ||
@@ -1552,16 +1571,30 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)save_section_footer (f, se);if (ret <0 ) {qemu_file_set_error (f, ret); -return -1 ; + goto ret_fail_abort_threads; } end_ts_each =qemu_clock_get_us (QEMU_CLOCK_REALTIME );trace_vmstate_downtime_save ("iterable" , se->idstr, se->instance_id, end_ts_each - start_ts_each); } +if (multifd_device_state && + !multifd_join_device_state_save_threads ()) { +qemu_file_set_error (f, -EINVAL ); +return -1 ; + } +trace_vmstate_downtime_checkpoint ("src-iterable-saved" );return 0 ; + +ret_fail_abort_threads: +if (multifd_device_state) { +multifd_abort_device_state_save_threads (); +multifd_join_device_state_save_threads (); + } + +return -1 ; } int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,
Re: [PATCH v5 17/36] migration: Add save_live_complete_precopy_thread handler
Quoted 159 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
This SaveVMHandler helps device provide its own asynchronous transmission of the remaining data at the end of a precopy phase via multifd channels, in parallel with the transfer done by
save_live_complete_precopyhandlers.These threads are launched only when multifd device state transfer is supported.
Management of these threads in done in the multifd migration code, wrapping them in the generic thread pool.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- include/migration/misc.h | 17 +++++++ include/migration/register.h | 19 +++++++ include/qemu/typedefs.h | 3 ++ migration/multifd-device-state.c | 85 ++++++++++++++++++++++++++++++++ migration/savevm.c | 35 ++++++++++++- 5 files changed, 158 insertions(+), 1 deletion(-)include/migration/misc.h +17
-0@@ -119,8 +119,25 @@ bool migrate_uri_parse(const char *uri, MigrationChannel **channel, Error **errp);/* migration/multifd-device-state.c */ +typedef struct SaveLiveCompletePrecopyThreadData { +SaveLiveCompletePrecopyThreadHandler hdlr; +char *idstr; +uint32_t instance_id; +void *handler_opaque; +}SaveLiveCompletePrecopyThreadData ; +bool multifd_queue_device_state (char *idstr,uint32_t instance_id,char *data,size_t len);bool multifd_device_state_supported (void ); +void +multifd_spawn_device_state_save_thread (SaveLiveCompletePrecopyThreadHandler hdlr, +char *idstr,uint32_t instance_id, +void *opaque); + +bool multifd_device_state_save_thread_should_exit (void ); + +void multifd_abort_device_state_save_threads (void ); +bool multifd_join_device_state_save_threads (void ); +#endif include/migration/register.h +19
-0@@ -105,6 +105,25 @@ typedef struct SaveVMHandlers {*/ int (*save_live_complete_precopy)(QEMUFile *f,void *opaque); +/** +* @save_live_complete_precopy_thread (invoked in a separate thread) +* +* Called at the end of a precopy phase from a separate worker thread +* in configurations where multifd device state transfer is supported +* in order to perform asynchronous transmission of the remaining data in +* parallel with @save_live_complete_precopy handlers. +* When postcopy is enabled, devices that support postcopy will skip this +* step. +* +* @d: a #SaveLiveCompletePrecopyThreadData containing parameters that the +* handler may need, including this device section idstr and instance_id, +* and opaque data pointer passed to register_savevm_live(). +* @errp: pointer to Error*, to store an error if it happens. +* +* Returns true to indicate success and false for errors. +*/ +SaveLiveCompletePrecopyThreadHandler save_live_complete_precopy_thread; +/* This runs both outside and inside the BQL. */ /** include/qemu/typedefs.h +3
-0@@ -108,6 +108,7 @@ typedef struct QString QString;typedef struct RAMBlock RAMBlock ;typedef struct Range Range ;typedef struct ReservedRegion ReservedRegion ; +typedef struct SaveLiveCompletePrecopyThreadData SaveLiveCompletePrecopyThreadData ;typedef struct SHPCDevice SHPCDevice ;typedef struct SSIBus SSIBus ;typedef struct TCGCPUOps TCGCPUOps ;@@ -133,5 +134,7 @@ typedef struct IRQState *qemu_irq;typedef void (*qemu_irq_handler )(void *opaque,int n,int level);typedef bool (*MigrationLoadThread )(void *opaque,bool *should_quit,Error **errp); +typedef bool (*SaveLiveCompletePrecopyThreadHandler )(SaveLiveCompletePrecopyThreadData *d, +Error **errp);#endif /* QEMU_TYPEDEFS_H */ migration/multifd-device-state.c +24
-0@@ -8,7 +8,10 @@*/ #include "qemu/osdep.h" +#include "qapi/error.h" #include "qemu/lockable.h" +#include "block/thread-pool.h" +#include "migration.h" #include "migration/misc.h" #include "multifd.h" #include "options.h" @@ -17,6 +20,9 @@ static struct {QemuMutex queue_job_mutex;MultiFDSendData *send_data; + +ThreadPool *threads; +bool threads_abort; } *multifd_send_device_state;void multifd_device_state_send_setup (void )@@ -27,10 +33,14 @@ void multifd_device_state_send_setup(void)qemu_mutex_init (&multifd_send_device_state->queue_job_mutex); multifd_send_device_state->send_data =multifd_send_data_alloc (); + + multifd_send_device_state->threads =thread_pool_new (); + multifd_send_device_state->threads_abort = false; }void multifd_device_state_send_cleanup (void ) { +g_clear_pointer (&multifd_send_device_state->threads, thread_pool_free);g_clear_pointer (&multifd_send_device_state->send_data, multifd_send_data_free);@@ -115,3 +125,78 @@ bool multifd_device_state_supported(void)return migrate_multifd () && !migrate_mapped_ram () &&migrate_multifd_compression () ==MULTIFD_COMPRESSION_NONE ; } + +static void multifd_device_state_save_thread_data_free (void *opaque) +{ +SaveLiveCompletePrecopyThreadData *data = opaque; + +g_clear_pointer (&data->idstr, g_free); +g_free (data); +} + +static int multifd_device_state_save_thread (void *opaque) +{ +SaveLiveCompletePrecopyThreadData *data = opaque; +g_autoptr (Error) local_err =NULL ; +
migration/multifd-device-state.c bool multifd_device_state_supported(void) · line 151
Quoted 10 lines
migration/multifd-device-state.c +10
-0+if (!data->hdlr (data, &local_err)) { +MigrationState *s =migrate_get_current (); + +assert (local_err); + +/* +* In case of multiple save threads failing which thread error +* return we end setting is purely arbitrary. +*/ +migrate_set_error (s, local_err);
Where did you kick off all the threads when one hit error? I wonder if migrate_set_error() should just set quit flag for everything, but for this series it might be easier to use multifd_abort_device_state_save_threads().
Other than that, looks good to me, thanks.
Quoted 121 lines
migration/multifd-device-state.c +51
-0+ } + +return 0 ; +} + +bool multifd_device_state_save_thread_should_exit (void ) +{ +return qatomic_read (&multifd_send_device_state->threads_abort); +} + +void +multifd_spawn_device_state_save_thread (SaveLiveCompletePrecopyThreadHandler hdlr, +char *idstr,uint32_t instance_id, +void *opaque) +{ +SaveLiveCompletePrecopyThreadData *data; + +assert (multifd_device_state_supported ()); +assert (multifd_send_device_state); + +assert (!qatomic_read (&multifd_send_device_state->threads_abort)); + + data =g_new (SaveLiveCompletePrecopyThreadData,1 ); + data->hdlr = hdlr; + data->idstr =g_strdup (idstr); + data->instance_id = instance_id; + data->handler_opaque = opaque; + +thread_pool_submit_immediate (multifd_send_device_state->threads, + multifd_device_state_save_thread, + data, + multifd_device_state_save_thread_data_free); +} + +void multifd_abort_device_state_save_threads (void ) +{ +assert (multifd_device_state_supported ()); + +qatomic_set (&multifd_send_device_state->threads_abort, true); +} + +bool multifd_join_device_state_save_threads (void ) +{ +MigrationState *s =migrate_get_current (); + +assert (multifd_device_state_supported ()); + +thread_pool_wait (multifd_send_device_state->threads); + +return !migrate_has_error (s); +}migration/savevm.c +34
-1@@ -37,6 +37,7 @@#include "migration/register.h" #include "migration/global_state.h" #include "migration/channel-block.h" +#include "multifd.h" #include "ram.h" #include "qemu-file.h" #include "savevm.h" @@ -1527,6 +1528,24 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)int64_t start_ts_each, end_ts_each;SaveStateEntry *se;int ret; +bool multifd_device_state =multifd_device_state_supported (); + +if (multifd_device_state) { + QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { +SaveLiveCompletePrecopyThreadHandler hdlr; + +if (!se->ops || (in_postcopy && se->ops->has_postcopy && + se->ops->has_postcopy (se->opaque)) || + !se->ops->save_live_complete_precopy_thread) { +continue ; + } + + hdlr = se->ops->save_live_complete_precopy_thread; +multifd_spawn_device_state_save_thread (hdlr, + se->idstr, se->instance_id, + se->opaque); + } + } QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { if (!se->ops ||@@ -1552,16 +1571,30 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)save_section_footer (f, se);if (ret <0 ) {qemu_file_set_error (f, ret); -return -1 ; + goto ret_fail_abort_threads; } end_ts_each =qemu_clock_get_us (QEMU_CLOCK_REALTIME );trace_vmstate_downtime_save ("iterable" , se->idstr, se->instance_id, end_ts_each - start_ts_each); } +if (multifd_device_state && + !multifd_join_device_state_save_threads ()) { +qemu_file_set_error (f, -EINVAL ); +return -1 ; + } +trace_vmstate_downtime_checkpoint ("src-iterable-saved" );return 0 ; + +ret_fail_abort_threads: +if (multifd_device_state) { +multifd_abort_device_state_save_threads (); +multifd_join_device_state_save_threads (); + } + +return -1 ; } int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,
Signature · 1 line
Peter Xu
Re: [PATCH v5 17/36] migration: Add save_live_complete_precopy_thread handler
Quoted 166 lines · Peter Xu
Quoted 165 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
This SaveVMHandler helps device provide its own asynchronous transmission of the remaining data at the end of a precopy phase via multifd channels, in parallel with the transfer done by
save_live_complete_precopyhandlers.These threads are launched only when multifd device state transfer is supported.
Management of these threads in done in the multifd migration code, wrapping them in the generic thread pool.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- include/migration/misc.h | 17 +++++++ include/migration/register.h | 19 +++++++ include/qemu/typedefs.h | 3 ++ migration/multifd-device-state.c | 85 ++++++++++++++++++++++++++++++++ migration/savevm.c | 35 ++++++++++++- 5 files changed, 158 insertions(+), 1 deletion(-)include/migration/misc.h +17
-0@@ -119,8 +119,25 @@ bool migrate_uri_parse(const char *uri, MigrationChannel **channel, Error **errp);/* migration/multifd-device-state.c */ +typedef struct SaveLiveCompletePrecopyThreadData { +SaveLiveCompletePrecopyThreadHandler hdlr; +char *idstr; +uint32_t instance_id; +void *handler_opaque; +}SaveLiveCompletePrecopyThreadData ; +bool multifd_queue_device_state (char *idstr,uint32_t instance_id,char *data,size_t len);bool multifd_device_state_supported (void ); +void +multifd_spawn_device_state_save_thread (SaveLiveCompletePrecopyThreadHandler hdlr, +char *idstr,uint32_t instance_id, +void *opaque); + +bool multifd_device_state_save_thread_should_exit (void ); + +void multifd_abort_device_state_save_threads (void ); +bool multifd_join_device_state_save_threads (void ); +#endif include/migration/register.h +19
-0@@ -105,6 +105,25 @@ typedef struct SaveVMHandlers {*/ int (*save_live_complete_precopy)(QEMUFile *f,void *opaque); +/** +* @save_live_complete_precopy_thread (invoked in a separate thread) +* +* Called at the end of a precopy phase from a separate worker thread +* in configurations where multifd device state transfer is supported +* in order to perform asynchronous transmission of the remaining data in +* parallel with @save_live_complete_precopy handlers. +* When postcopy is enabled, devices that support postcopy will skip this +* step. +* +* @d: a #SaveLiveCompletePrecopyThreadData containing parameters that the +* handler may need, including this device section idstr and instance_id, +* and opaque data pointer passed to register_savevm_live(). +* @errp: pointer to Error*, to store an error if it happens. +* +* Returns true to indicate success and false for errors. +*/ +SaveLiveCompletePrecopyThreadHandler save_live_complete_precopy_thread; +/* This runs both outside and inside the BQL. */ /** include/qemu/typedefs.h +3
-0@@ -108,6 +108,7 @@ typedef struct QString QString;typedef struct RAMBlock RAMBlock ;typedef struct Range Range ;typedef struct ReservedRegion ReservedRegion ; +typedef struct SaveLiveCompletePrecopyThreadData SaveLiveCompletePrecopyThreadData ;typedef struct SHPCDevice SHPCDevice ;typedef struct SSIBus SSIBus ;typedef struct TCGCPUOps TCGCPUOps ;@@ -133,5 +134,7 @@ typedef struct IRQState *qemu_irq;typedef void (*qemu_irq_handler )(void *opaque,int n,int level);typedef bool (*MigrationLoadThread )(void *opaque,bool *should_quit,Error **errp); +typedef bool (*SaveLiveCompletePrecopyThreadHandler )(SaveLiveCompletePrecopyThreadData *d, +Error **errp);#endif /* QEMU_TYPEDEFS_H */ migration/multifd-device-state.c +30
-0@@ -8,7 +8,10 @@*/ #include "qemu/osdep.h" +#include "qapi/error.h" #include "qemu/lockable.h" +#include "block/thread-pool.h" +#include "migration.h" #include "migration/misc.h" #include "multifd.h" #include "options.h" @@ -17,6 +20,9 @@ static struct {QemuMutex queue_job_mutex;MultiFDSendData *send_data; + +ThreadPool *threads; +bool threads_abort; } *multifd_send_device_state;void multifd_device_state_send_setup (void )@@ -27,10 +33,14 @@ void multifd_device_state_send_setup(void)qemu_mutex_init (&multifd_send_device_state->queue_job_mutex); multifd_send_device_state->send_data =multifd_send_data_alloc (); + + multifd_send_device_state->threads =thread_pool_new (); + multifd_send_device_state->threads_abort = false; }void multifd_device_state_send_cleanup (void ) { +g_clear_pointer (&multifd_send_device_state->threads, thread_pool_free);g_clear_pointer (&multifd_send_device_state->send_data, multifd_send_data_free);@@ -115,3 +125,78 @@ bool multifd_device_state_supported(void)return migrate_multifd () && !migrate_mapped_ram () &&migrate_multifd_compression () ==MULTIFD_COMPRESSION_NONE ; } + +static void multifd_device_state_save_thread_data_free (void *opaque) +{ +SaveLiveCompletePrecopyThreadData *data = opaque; + +g_clear_pointer (&data->idstr, g_free); +g_free (data); +} + +static int multifd_device_state_save_thread (void *opaque) +{ +SaveLiveCompletePrecopyThreadData *data = opaque; +g_autoptr (Error) local_err =NULL ; + +if (!data->hdlr (data, &local_err)) { +MigrationState *s =migrate_get_current (); + +assert (local_err); + +/*
Quoted 8 lines
migration/multifd-device-state.c bool multifd_device_state_supported(void) · line 151
Quoted 4 lines
migration/multifd-device-state.c +4
-0+* In case of multiple save threads failing which thread error +* return we end setting is purely arbitrary. +*/ +migrate_set_error (s, local_err);Where did you kick off all the threads when one hit error? I wonder if
migrate_set_error()should just set quit flag for everything, but for this series it might be easier to usemultifd_abort_device_state_save_threads().
I've now added call to multifd_abort_device_state_save_threads() if a migration error is already set to avoid needlessly waiting for the remaining threads to do all of their work.
Quoted 2 lines
Other than that, looks good to me, thanks.
Thanks,
Maciej
Peter Xu
Reviewed-byRe: [PATCH v5 17/36] migration: Add save_live_complete_precopy_thread handler
Quoted 165 lines · Maciej S. Szmigiero
Quoted 155 lines · Peter Xu
Quoted 154 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
This SaveVMHandler helps device provide its own asynchronous transmission of the remaining data at the end of a precopy phase via multifd channels, in parallel with the transfer done by
save_live_complete_precopyhandlers.These threads are launched only when multifd device state transfer is supported.
Management of these threads in done in the multifd migration code, wrapping them in the generic thread pool.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- include/migration/misc.h | 17 +++++++ include/migration/register.h | 19 +++++++ include/qemu/typedefs.h | 3 ++ migration/multifd-device-state.c | 85 ++++++++++++++++++++++++++++++++ migration/savevm.c | 35 ++++++++++++- 5 files changed, 158 insertions(+), 1 deletion(-)include/migration/misc.h +17
-0@@ -119,8 +119,25 @@ bool migrate_uri_parse(const char *uri, MigrationChannel **channel, Error **errp);/* migration/multifd-device-state.c */ +typedef struct SaveLiveCompletePrecopyThreadData { +SaveLiveCompletePrecopyThreadHandler hdlr; +char *idstr; +uint32_t instance_id; +void *handler_opaque; +}SaveLiveCompletePrecopyThreadData ; +bool multifd_queue_device_state (char *idstr,uint32_t instance_id,char *data,size_t len);bool multifd_device_state_supported (void ); +void +multifd_spawn_device_state_save_thread (SaveLiveCompletePrecopyThreadHandler hdlr, +char *idstr,uint32_t instance_id, +void *opaque); + +bool multifd_device_state_save_thread_should_exit (void ); + +void multifd_abort_device_state_save_threads (void ); +bool multifd_join_device_state_save_threads (void ); +#endif include/migration/register.h +19
-0@@ -105,6 +105,25 @@ typedef struct SaveVMHandlers {*/ int (*save_live_complete_precopy)(QEMUFile *f,void *opaque); +/** +* @save_live_complete_precopy_thread (invoked in a separate thread) +* +* Called at the end of a precopy phase from a separate worker thread +* in configurations where multifd device state transfer is supported +* in order to perform asynchronous transmission of the remaining data in +* parallel with @save_live_complete_precopy handlers. +* When postcopy is enabled, devices that support postcopy will skip this +* step. +* +* @d: a #SaveLiveCompletePrecopyThreadData containing parameters that the +* handler may need, including this device section idstr and instance_id, +* and opaque data pointer passed to register_savevm_live(). +* @errp: pointer to Error*, to store an error if it happens. +* +* Returns true to indicate success and false for errors. +*/ +SaveLiveCompletePrecopyThreadHandler save_live_complete_precopy_thread; +/* This runs both outside and inside the BQL. */ /** include/qemu/typedefs.h +3
-0@@ -108,6 +108,7 @@ typedef struct QString QString;typedef struct RAMBlock RAMBlock ;typedef struct Range Range ;typedef struct ReservedRegion ReservedRegion ; +typedef struct SaveLiveCompletePrecopyThreadData SaveLiveCompletePrecopyThreadData ;typedef struct SHPCDevice SHPCDevice ;typedef struct SSIBus SSIBus ;typedef struct TCGCPUOps TCGCPUOps ;@@ -133,5 +134,7 @@ typedef struct IRQState *qemu_irq;typedef void (*qemu_irq_handler )(void *opaque,int n,int level);typedef bool (*MigrationLoadThread )(void *opaque,bool *should_quit,Error **errp); +typedef bool (*SaveLiveCompletePrecopyThreadHandler )(SaveLiveCompletePrecopyThreadData *d, +Error **errp);#endif /* QEMU_TYPEDEFS_H */ migration/multifd-device-state.c +30
-0@@ -8,7 +8,10 @@*/ #include "qemu/osdep.h" +#include "qapi/error.h" #include "qemu/lockable.h" +#include "block/thread-pool.h" +#include "migration.h" #include "migration/misc.h" #include "multifd.h" #include "options.h" @@ -17,6 +20,9 @@ static struct {QemuMutex queue_job_mutex;MultiFDSendData *send_data; + +ThreadPool *threads; +bool threads_abort; } *multifd_send_device_state;void multifd_device_state_send_setup (void )@@ -27,10 +33,14 @@ void multifd_device_state_send_setup(void)qemu_mutex_init (&multifd_send_device_state->queue_job_mutex); multifd_send_device_state->send_data =multifd_send_data_alloc (); + + multifd_send_device_state->threads =thread_pool_new (); + multifd_send_device_state->threads_abort = false; }void multifd_device_state_send_cleanup (void ) { +g_clear_pointer (&multifd_send_device_state->threads, thread_pool_free);g_clear_pointer (&multifd_send_device_state->send_data, multifd_send_data_free);@@ -115,3 +125,78 @@ bool multifd_device_state_supported(void)return migrate_multifd () && !migrate_mapped_ram () &&migrate_multifd_compression () ==MULTIFD_COMPRESSION_NONE ; } + +static void multifd_device_state_save_thread_data_free (void *opaque) +{ +SaveLiveCompletePrecopyThreadData *data = opaque; + +g_clear_pointer (&data->idstr, g_free); +g_free (data); +} + +static int multifd_device_state_save_thread (void *opaque) +{ +SaveLiveCompletePrecopyThreadData *data = opaque; +g_autoptr (Error) local_err =NULL ; + +if (!data->hdlr (data, &local_err)) { +MigrationState *s =migrate_get_current (); + +assert (local_err); + +/* Quoted 8 lines
migration/multifd-device-state.c bool multifd_device_state_supported(void) · line 151
Quoted 4 lines
migration/multifd-device-state.c +4
-0+* In case of multiple save threads failing which thread error +* return we end setting is purely arbitrary. +*/ +migrate_set_error (s, local_err);Where did you kick off all the threads when one hit error? I wonder if
migrate_set_error()should just set quit flag for everything, but for this series it might be easier to usemultifd_abort_device_state_save_threads().
Quoted 3 lines
I've now added call to
multifd_abort_device_state_save_threads()if a migration error is already set to avoid needlessly waiting for the remaining threads to do all of their work.
With that, feel free to take:
Signature · 1 line
Peter Xu
PATCH v5 18/36 vfio/migration: Add load_device_config_state_start trace event
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
And rename existing load_device_config_state trace event to load_device_config_state_end for consistency since it is triggered at the end of loading of the VFIO device config state.
This way both the start and end points of particular device config loading operation (a long, BQL-serialized operation) are known.
--- hw/vfio/migration.c | 4 +++- hw/vfio/trace-events | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-)
hw/vfio/migration.c +3 -1
@@ -285,6 +285,8 @@ static int vfio_load_device_config_state(QEMUFile *f, void *opaque)VFIODevice *vbasedev = opaque;uint64_t data; +trace_vfio_load_device_config_state_start (vbasedev->name); +if (vbasedev->ops && vbasedev->ops->vfio_load_config) {int ret;
@@ -303,7 +305,7 @@ static int vfio_load_device_config_state(QEMUFile *f, void *opaque)return -EINVAL ; } -trace_vfio_load_device_config_state (vbasedev->name); +trace_vfio_load_device_config_state_end (vbasedev->name);return qemu_file_get_error (f); }
hw/vfio/trace-events +2 -1
@@ -149,7 +149,8 @@ vfio_display_edid_write_error(void) "" # migration.c vfio_load_cleanup(const char *name) " (%s)" -vfio_load_device_config_state(const char *name) " (%s)" +vfio_load_device_config_state_start(const char *name) " (%s)" +vfio_load_device_config_state_end(const char *name) " (%s)" vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size %"PRIu64" ret %d" vfio_migration_realize(const char *name) " (%s)"
PATCH v5 19/36 vfio/migration: Convert bytes_transferred counter to atomic
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
So it can be safety accessed from multiple threads.
This variable type needs to be changed to unsigned long since 32-bit host platforms lack the necessary addition atomics on 64-bit variables.
Using 32-bit counters on 32-bit host platforms should not be a problem in practice since they can't realistically address more memory anyway.
--- hw/vfio/migration.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
hw/vfio/migration.c +4 -4
@@ -55,7 +55,7 @@*/ #define VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE (1 * MiB) -static int64_t bytes_transferred; +static unsigned long bytes_transferred;static const char *mig_state_to_str (enum vfio_device_mig_state state) {
@@ -391,7 +391,7 @@ static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration)qemu_put_be64 (f,VFIO_MIG_FLAG_DEV_DATA_STATE );qemu_put_be64 (f, data_size);qemu_put_buffer (f, migration->data_buffer, data_size); - bytes_transferred += data_size; +qatomic_add (&bytes_transferred, data_size);trace_vfio_save_block (migration->vbasedev->name, data_size);
@@ -1013,12 +1013,12 @@ static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)int64_t vfio_mig_bytes_transferred (void ) { -return bytes_transferred; +return MIN (qatomic_read (&bytes_transferred),INT64_MAX ); }void vfio_reset_bytes_transferred (void ) { - bytes_transferred =0 ; +qatomic_set (&bytes_transferred,0 ); }/*
Re: [PATCH v5 19/36] vfio/migration: Convert bytes_transferred counter to atomic
Quoted 10 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
So it can be safety accessed from multiple threads.
This variable type needs to be changed to unsigned long since 32-bit host platforms lack the necessary addition atomics on 64-bit variables.
Using 32-bit counters on 32-bit host platforms should not be a problem in practice since they can't realistically address more memory anyway.
Is it useful to have VFIO on 32-bit host platforms ?
If not, VFIO PCI should depend on (AARCH64 || PPC64 || X86_64) and we could drop this patch. Let's address that independently.
Thanks,
C.
Quoted 44 lines
Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)hw/vfio/migration.c +4
-4@@ -55,7 +55,7 @@*/ #define VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE (1 * MiB) -static int64_t bytes_transferred; +static unsigned long bytes_transferred;static const char *mig_state_to_str (enum vfio_device_mig_state state) {@@ -391,7 +391,7 @@ static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration)qemu_put_be64 (f,VFIO_MIG_FLAG_DEV_DATA_STATE );qemu_put_be64 (f, data_size);qemu_put_buffer (f, migration->data_buffer, data_size); - bytes_transferred += data_size; +qatomic_add (&bytes_transferred, data_size);trace_vfio_save_block (migration->vbasedev->name, data_size);@@ -1013,12 +1013,12 @@ static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)int64_t vfio_mig_bytes_transferred (void ) { -return bytes_transferred; +return MIN (qatomic_read (&bytes_transferred),INT64_MAX ); }void vfio_reset_bytes_transferred (void ) { - bytes_transferred =0 ; +qatomic_set (&bytes_transferred,0 ); }/*
Re: [PATCH v5 19/36] vfio/migration: Convert bytes_transferred counter to atomic
Quoted 9 lines · Cédric Le Goater
Quoted 8 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
So it can be safety accessed from multiple threads.
This variable type needs to be changed to unsigned long since 32-bit host platforms lack the necessary addition atomics on 64-bit variables.
Quoted 7 lines
Quoted 2 lines
Using 32-bit counters on 32-bit host platforms should not be a problem in practice since they can't realistically address more memory anyway.
Is it useful to have VFIO on 32-bit host platforms ?
If not, VFIO PCI should depend on (AARCH64 || PPC64 ||
X86_64) and we could drop this patch. Let's address that independently.
Not sure how much use VFIO gets on 32-bit host platforms,
however totally disabling it on these would be a major functional regression - at least if taken at its face value.
Especially considering that making it work on 32-bit platform requires just this tiny variable type change here.
Quoted 3 lines
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 19/36] vfio/migration: Convert bytes_transferred counter to atomic
Quoted 13 lines · Maciej S. Szmigiero
Quoted 12 lines · Cédric Le Goater
Quoted 10 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
So it can be safety accessed from multiple threads.
This variable type needs to be changed to unsigned long since 32-bit host platforms lack the necessary addition atomics on 64-bit variables.
Using 32-bit counters on 32-bit host platforms should not be a problem in practice since they can't realistically address more memory anyway.
Quoted 8 lines
Quoted 4 lines
Is it useful to have VFIO on 32-bit host platforms ?
If not, VFIO PCI should depend on (AARCH64 || PPC64 ||
X86_64) and we could drop this patch. Let's address that independently.Not sure how much use VFIO gets on 32-bit host platforms,
however totally disabling it on these would be a major functional regression - at least if taken at its face value.
32-bit host platform support is being deprecated in QEMU 10.0 and should be removed in QEMU 10.2.
Quoted 2 lines
Especially considering that making it work on 32-bit platform requires just this tiny variable type change here.
yes. It raised my attention because x86 32-bit was the only host platform I was not sure about and Alex confirmed it worked. We should simply wait for removal.
Thanks,
C.
Re: [PATCH v5 19/36] vfio/migration: Convert bytes_transferred counter to atomic
In reply to Maciej S. SzmigieroQuoted 4 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
So it can be safety accessed from multiple threads.
Quoted 8 lines
This variable type needs to be changed to unsigned long since 32-bit host platforms lack the necessary addition atomics on 64-bit variables.
Using 32-bit counters on 32-bit host platforms should not be a problem in practice since they can't realistically address more memory anyway.
Signed-off-by Maciej S. Szmigiero <[email protected]>
Thanks,
C.
Quoted 42 lines
--- hw/vfio/migration.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)hw/vfio/migration.c +4
-4@@ -55,7 +55,7 @@*/ #define VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE (1 * MiB) -static int64_t bytes_transferred; +static unsigned long bytes_transferred;static const char *mig_state_to_str (enum vfio_device_mig_state state) {@@ -391,7 +391,7 @@ static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration)qemu_put_be64 (f,VFIO_MIG_FLAG_DEV_DATA_STATE );qemu_put_be64 (f, data_size);qemu_put_buffer (f, migration->data_buffer, data_size); - bytes_transferred += data_size; +qatomic_add (&bytes_transferred, data_size);trace_vfio_save_block (migration->vbasedev->name, data_size);@@ -1013,12 +1013,12 @@ static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)int64_t vfio_mig_bytes_transferred (void ) { -return bytes_transferred; +return MIN (qatomic_read (&bytes_transferred),INT64_MAX ); }void vfio_reset_bytes_transferred (void ) { - bytes_transferred =0 ; +qatomic_set (&bytes_transferred,0 ); }/*
PATCH v5 20/36 vfio/migration: Add vfio_add_bytes_transferred()
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
This way bytes_transferred can also be incremented in other translation units than migration.c.
--- hw/vfio/migration.c | 7 ++++++- include/hw/vfio/vfio-common.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-)
hw/vfio/migration.c +6 -1
@@ -391,7 +391,7 @@ static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration)qemu_put_be64 (f,VFIO_MIG_FLAG_DEV_DATA_STATE );qemu_put_be64 (f, data_size);qemu_put_buffer (f, migration->data_buffer, data_size); -qatomic_add (&bytes_transferred, data_size); +vfio_add_bytes_transferred (data_size);trace_vfio_save_block (migration->vbasedev->name, data_size);
@@ -1021,6 +1021,11 @@ void vfio_reset_bytes_transferred(void)qatomic_set (&bytes_transferred,0 ); } +void vfio_add_bytes_transferred (unsigned long val) +{ +qatomic_add (&bytes_transferred, val); +} +/* * Return true when either migration initialized or blocker registered. * Currently only return false when adding blocker fails which will
include/hw/vfio/vfio-common.h +1 -0
@@ -274,6 +274,7 @@ void vfio_unblock_multiple_devices_migration(void);bool vfio_viommu_preset (VFIODevice *vbasedev);int64_t vfio_mig_bytes_transferred (void );void vfio_reset_bytes_transferred (void ); +void vfio_add_bytes_transferred (unsigned long val);bool vfio_device_state_is_running (VFIODevice *vbasedev);bool vfio_device_state_is_precopy (VFIODevice *vbasedev);
Re: [PATCH v5 20/36] vfio/migration: Add vfio_add_bytes_transferred()
Quoted 6 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
This way
bytes_transferredcan also be incremented in other translation units thanmigration.c.Signed-off-by Maciej S. Szmigiero <[email protected]>
Looks good. Just a small aesthetic issue.
Quoted 19 lines
--- hw/vfio/migration.c | 7 ++++++- include/hw/vfio/vfio-common.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-)hw/vfio/migration.c +1
-1@@ -391,7 +391,7 @@ static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration)qemu_put_be64 (f,VFIO_MIG_FLAG_DEV_DATA_STATE );qemu_put_be64 (f, data_size);qemu_put_buffer (f, migration->data_buffer, data_size); -qatomic_add (&bytes_transferred, data_size); +vfio_add_bytes_transferred (data_size);trace_vfio_save_block (migration->vbasedev->name, data_size);
hw/vfio/migration.c void vfio_reset_bytes_transferred(void) · line 1024
Quoted 4 lines
hw/vfio/migration.c +1
-0@@ -1021,6 +1021,11 @@ void vfio_reset_bytes_transferred(void)qatomic_set (&bytes_transferred,0 ); } +void vfio_add_bytes_transferred (unsigned long val)
vfio_migration_add_bytes_transferred()
Thanks,
C.
Quoted 20 lines
hw/vfio/migration.c +4
-0+{ +qatomic_add (&bytes_transferred, val); +} +/* * Return true when either migration initialized or blocker registered. * Currently only return false when adding blocker fails which will include/hw/vfio/vfio-common.h +1
-0@@ -274,6 +274,7 @@ void vfio_unblock_multiple_devices_migration(void);bool vfio_viommu_preset (VFIODevice *vbasedev);int64_t vfio_mig_bytes_transferred (void );void vfio_reset_bytes_transferred (void ); +void vfio_add_bytes_transferred (unsigned long val);bool vfio_device_state_is_running (VFIODevice *vbasedev);bool vfio_device_state_is_precopy (VFIODevice *vbasedev);
Re: [PATCH v5 20/36] vfio/migration: Add vfio_add_bytes_transferred()
Quoted 24 lines · Cédric Le Goater
Quoted 6 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
This way
bytes_transferredcan also be incremented in other translation units thanmigration.c.Signed-off-by Maciej S. Szmigiero <[email protected]>Looks good. Just a small aesthetic issue.
Quoted 14 lines
---
hw/vfio/migration.c | 7 ++++++-
include/hw/vfio/vfio-common.h| 1 +2 files changed, 7 insertions(+), 1 deletion(-)hw/vfio/migration.c +0
-1@@ -391,7 +391,7 @@ static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration) qemu_put_be64(f, VFIO_MIG_FLAG_DEV_DATA_STATE); qemu_put_be64(f, data_size); qemu_put_buffer(f, migration->data_buffer, data_size); - qatomic_add(&bytes_transferred, data_size);
Quoted 9 lines
hw/vfio/migration.c void vfio_reset_bytes_transferred(void) · line 1023
Quoted 6 lines
hw/vfio/migration.c +2
-0+ vfio_add_bytes_transferred(data_size); trace_vfio_save_block(migration->vbasedev->name, data_size);@@ -1021,6 +1021,11 @@ void vfio_reset_bytes_transferred(void) qatomic_set(&bytes_transferred, 0); } +void vfio_add_bytes_transferred (unsigned long val)
vfio_migration_add_bytes_transferred()
Renamed into vfio_mig_add_bytes_transferred() for consistency with vfio_mig_bytes_transferred().
Quoted 3 lines
Thanks,
C.
Thanks,
Maciej
PATCH v5 21/36 vfio/migration: Move migration channel flags to vfio-common.h header file
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
This way they can also be referenced in other translation units than migration.c.
--- hw/vfio/migration.c | 17 ----------------- include/hw/vfio/vfio-common.h | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 17 deletions(-)
hw/vfio/migration.c +0 -17
@@ -31,23 +31,6 @@#include "trace.h" #include "hw/hw.h" -/* -* Flags to be used as unique delimiters for VFIO devices in the migration -* stream. These flags are composed as: -* 0xffffffff => MSB 32-bit all 1s -* 0xef10 => Magic ID, represents emulated (virtual) function IO -* 0x0000 => 16-bits reserved for flags -* -* The beginning of state information is marked by _DEV_CONFIG_STATE, -* _DEV_SETUP_STATE, or _DEV_DATA_STATE, respectively. The end of a -* certain state information is marked by _END_OF_STATE. -*/ -#define VFIO_MIG_FLAG_END_OF_STATE (0xffffffffef100001ULL) -#define VFIO_MIG_FLAG_DEV_CONFIG_STATE (0xffffffffef100002ULL) -#define VFIO_MIG_FLAG_DEV_SETUP_STATE (0xffffffffef100003ULL) -#define VFIO_MIG_FLAG_DEV_DATA_STATE (0xffffffffef100004ULL) -#define VFIO_MIG_FLAG_DEV_INIT_DATA_SENT (0xffffffffef100005ULL) -/* * This is an arbitrary size based on migration of mlx5 devices, where typically * total device migration size is on the order of 100s of MB. Testing with
include/hw/vfio/vfio-common.h +17 -0
@@ -36,6 +36,23 @@#define VFIO_MSG_PREFIX "vfio %s: " +/* +* Flags to be used as unique delimiters for VFIO devices in the migration +* stream. These flags are composed as: +* 0xffffffff => MSB 32-bit all 1s +* 0xef10 => Magic ID, represents emulated (virtual) function IO +* 0x0000 => 16-bits reserved for flags +* +* The beginning of state information is marked by _DEV_CONFIG_STATE, +* _DEV_SETUP_STATE, or _DEV_DATA_STATE, respectively. The end of a +* certain state information is marked by _END_OF_STATE. +*/ +#define VFIO_MIG_FLAG_END_OF_STATE (0xffffffffef100001ULL) +#define VFIO_MIG_FLAG_DEV_CONFIG_STATE (0xffffffffef100002ULL) +#define VFIO_MIG_FLAG_DEV_SETUP_STATE (0xffffffffef100003ULL) +#define VFIO_MIG_FLAG_DEV_DATA_STATE (0xffffffffef100004ULL) +#define VFIO_MIG_FLAG_DEV_INIT_DATA_SENT (0xffffffffef100005ULL) +enum {VFIO_DEVICE_TYPE_PCI =0 ,VFIO_DEVICE_TYPE_PLATFORM =1 ,
Re: [PATCH v5 21/36] vfio/migration: Move migration channel flags to vfio-common.h header file
Quoted 6 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
This way they can also be referenced in other translation units than
migration.c.Signed-off-by Maciej S. Szmigiero <[email protected]>
Thanks,
C.
Quoted 62 lines
--- hw/vfio/migration.c | 17 ----------------- include/hw/vfio/vfio-common.h | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 17 deletions(-)hw/vfio/migration.c +0
-17@@ -31,23 +31,6 @@#include "trace.h" #include "hw/hw.h" -/* -* Flags to be used as unique delimiters for VFIO devices in the migration -* stream. These flags are composed as: -* 0xffffffff => MSB 32-bit all 1s -* 0xef10 => Magic ID, represents emulated (virtual) function IO -* 0x0000 => 16-bits reserved for flags -* -* The beginning of state information is marked by _DEV_CONFIG_STATE, -* _DEV_SETUP_STATE, or _DEV_DATA_STATE, respectively. The end of a -* certain state information is marked by _END_OF_STATE. -*/ -#define VFIO_MIG_FLAG_END_OF_STATE (0xffffffffef100001ULL) -#define VFIO_MIG_FLAG_DEV_CONFIG_STATE (0xffffffffef100002ULL) -#define VFIO_MIG_FLAG_DEV_SETUP_STATE (0xffffffffef100003ULL) -#define VFIO_MIG_FLAG_DEV_DATA_STATE (0xffffffffef100004ULL) -#define VFIO_MIG_FLAG_DEV_INIT_DATA_SENT (0xffffffffef100005ULL) -/* * This is an arbitrary size based on migration of mlx5 devices, where typically * total device migration size is on the order of 100s of MB. Testing with include/hw/vfio/vfio-common.h +17
-0@@ -36,6 +36,23 @@#define VFIO_MSG_PREFIX "vfio %s: " +/* +* Flags to be used as unique delimiters for VFIO devices in the migration +* stream. These flags are composed as: +* 0xffffffff => MSB 32-bit all 1s +* 0xef10 => Magic ID, represents emulated (virtual) function IO +* 0x0000 => 16-bits reserved for flags +* +* The beginning of state information is marked by _DEV_CONFIG_STATE, +* _DEV_SETUP_STATE, or _DEV_DATA_STATE, respectively. The end of a +* certain state information is marked by _END_OF_STATE. +*/ +#define VFIO_MIG_FLAG_END_OF_STATE (0xffffffffef100001ULL) +#define VFIO_MIG_FLAG_DEV_CONFIG_STATE (0xffffffffef100002ULL) +#define VFIO_MIG_FLAG_DEV_SETUP_STATE (0xffffffffef100003ULL) +#define VFIO_MIG_FLAG_DEV_DATA_STATE (0xffffffffef100004ULL) +#define VFIO_MIG_FLAG_DEV_INIT_DATA_SENT (0xffffffffef100005ULL) +enum {VFIO_DEVICE_TYPE_PCI =0 ,VFIO_DEVICE_TYPE_PLATFORM =1 ,
PATCH v5 22/36 vfio/migration: Multifd device state transfer support - basic types
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Add basic types and flags used by VFIO multifd device state transfer support.
Since we'll be introducing a lot of multifd transfer specific code, add a new file migration-multifd.c to home it, wired into main VFIO migration code (migration.c) via migration-multifd.h header file.
--- hw/vfio/meson.build | 1 + hw/vfio/migration-multifd.c | 31 +++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 15 +++++++++++++++ hw/vfio/migration.c | 1 + 4 files changed, 48 insertions(+) create mode 100644 hw/vfio/migration-multifd.c create mode 100644 hw/vfio/migration-multifd.h
hw/vfio/meson.build +1 -0
@@ -5,6 +5,7 @@ vfio_ss.add(files( 'container-base.c', 'container.c', 'migration.c', + 'migration-multifd.c', 'cpr.c', )) vfio_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr.c'))
hw/vfio/migration-multifd.c new file +31 -0
@@ -0,0 +1,31 @@ +/* +* Multifd VFIO migration +* +* Copyright (C) 2024,2025 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "hw/vfio/vfio-common.h" +#include "migration/misc.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/lockable.h" +#include "qemu/main-loop.h" +#include "qemu/thread.h" +#include "migration/qemu-file.h" +#include "migration-multifd.h" +#include "trace.h" + +#define VFIO_DEVICE_STATE_CONFIG_STATE (1) + +#define VFIO_DEVICE_STATE_PACKET_VER_CURRENT (0) + +typedef struct VFIODeviceStatePacket { +uint32_t version; +uint32_t idx; +uint32_t flags; +uint8_t data[0 ]; +} QEMU_PACKED VFIODeviceStatePacket;
hw/vfio/migration-multifd.h new file +15 -0
@@ -0,0 +1,15 @@ +/* +* Multifd VFIO migration +* +* Copyright (C) 2024,2025 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#ifndef HW_VFIO_MIGRATION_MULTIFD_H +#define HW_VFIO_MIGRATION_MULTIFD_H + +#include "hw/vfio/vfio-common.h" + +#endif
hw/vfio/migration.c +1 -0
@@ -23,6 +23,7 @@#include "migration/qemu-file.h" #include "migration/register.h" #include "migration/blocker.h" +#include "migration-multifd.h" #include "qapi/error.h" #include "qapi/qapi-events-vfio.h" #include "exec/ramlist.h"
Re: [PATCH v5 22/36] vfio/migration: Multifd device state transfer support - basic types
Quoted 37 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Add basic types and flags used by VFIO multifd device state transfer support.
Since we'll be introducing a lot of multifd transfer specific code, add a new file
migration-multifd.cto home it, wired into main VFIO migration code (migration.c) viamigration-multifd.hheader file.Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/meson.build | 1 + hw/vfio/migration-multifd.c | 31 +++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 15 +++++++++++++++ hw/vfio/migration.c | 1 + 4 files changed, 48 insertions(+)create mode 100644
hw/vfio/migration-multifd.c
create mode 100644hw/vfio/migration-multifd.hhw/vfio/meson.build +1
-0@@ -5,6 +5,7 @@ vfio_ss.add(files( 'container-base.c', 'container.c', 'migration.c', + 'migration-multifd.c', 'cpr.c', )) vfio_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr.c'))hw/vfio/migration-multifd.c new file +0
-0
hw/vfio/migration-multifd.c line 1
Quoted 1 line
hw/vfio/migration-multifd.c new file +1
-0@@ -0,0 +1,31 @@ +/*
Please add :
SPDX-License-Identifier: GPL-2.0-or-later
in new files.
Thanks,
C.
Quoted 64 lines
hw/vfio/migration-multifd.c new file +30
-0+* Multifd VFIO migration +* +* Copyright (C) 2024,2025 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "hw/vfio/vfio-common.h" +#include "migration/misc.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/lockable.h" +#include "qemu/main-loop.h" +#include "qemu/thread.h" +#include "migration/qemu-file.h" +#include "migration-multifd.h" +#include "trace.h" + +#define VFIO_DEVICE_STATE_CONFIG_STATE (1) + +#define VFIO_DEVICE_STATE_PACKET_VER_CURRENT (0) + +typedef struct VFIODeviceStatePacket { +uint32_t version; +uint32_t idx; +uint32_t flags; +uint8_t data[0 ]; +} QEMU_PACKED VFIODeviceStatePacket;hw/vfio/migration-multifd.h new file +15
-0@@ -0,0 +1,15 @@ +/* +* Multifd VFIO migration +* +* Copyright (C) 2024,2025 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#ifndef HW_VFIO_MIGRATION_MULTIFD_H +#define HW_VFIO_MIGRATION_MULTIFD_H + +#include "hw/vfio/vfio-common.h" + +#endif hw/vfio/migration.c +1
-0@@ -23,6 +23,7 @@#include "migration/qemu-file.h" #include "migration/register.h" #include "migration/blocker.h" +#include "migration-multifd.h" #include "qapi/error.h" #include "qapi/qapi-events-vfio.h" #include "exec/ramlist.h"
Re: [PATCH v5 22/36] vfio/migration: Multifd device state transfer support - basic types
Quoted 42 lines · Cédric Le Goater
Quoted 37 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Add basic types and flags used by VFIO multifd device state transfer support.
Since we'll be introducing a lot of multifd transfer specific code, add a new file
migration-multifd.cto home it, wired into main VFIO migration code (migration.c) viamigration-multifd.hheader file.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/meson.build | 1 +hw/vfio/migration-multifd.c| 31 +++++++++++++++++++++++++++++++hw/vfio/migration-multifd.h| 15 +++++++++++++++ hw/vfio/migration.c | 1 +4 files changed, 48 insertions(+)create mode 100644
hw/vfio/migration-multifd.c
create mode 100644hw/vfio/migration-multifd.hhw/vfio/meson.build +1
-0@@ -5,6 +5,7 @@ vfio_ss.add(files( 'container-base.c', 'container.c', 'migration.c', + 'migration-multifd.c', 'cpr.c', )) vfio_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr.c'))hw/vfio/migration-multifd.c new file +0
-0hw/vfio/migration-multifd.c line 1
Quoted 1 line
hw/vfio/migration-multifd.c new file +1
-0@@ -0,0 +1,31 @@ +/* Please add :
Quoted 3 lines
SPDX-License-Identifier: GPL-2.0-or-later
in new files.
Done, also to migration/multifd-device-state.c
outside VFIO.
Quoted 3 lines
Thanks,
C.
Thanks,
Maciej
PATCH v5 23/36 vfio/migration: Multifd device state transfer support - VFIOStateBuffer(s)
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Add VFIOStateBuffer(s) types and the associated methods.
These store received device state buffers and config state waiting to get loaded into the device.
--- hw/vfio/migration-multifd.c | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+)
hw/vfio/migration-multifd.c +54 -0
@@ -29,3 +29,57 @@ typedef struct VFIODeviceStatePacket {uint32_t flags;uint8_t data[0 ]; }QEMU_PACKED VFIODeviceStatePacket; + +/* type safety */ +typedef struct VFIOStateBuffers { +GArray *array; +}VFIOStateBuffers ; + +typedef struct VFIOStateBuffer { +bool is_present; +char *data; +size_t len; +}VFIOStateBuffer ; + +static void vfio_state_buffer_clear (gpointer data) +{ +VFIOStateBuffer *lb = data; + +if (!lb->is_present) { +return ; + } + +g_clear_pointer (&lb->data, g_free); + lb->is_present = false; +} + +static void vfio_state_buffers_init (VFIOStateBuffers *bufs) +{ + bufs->array =g_array_new (FALSE, TRUE,sizeof (VFIOStateBuffer)); +g_array_set_clear_func (bufs->array, vfio_state_buffer_clear); +} + +static void vfio_state_buffers_destroy (VFIOStateBuffers *bufs) +{ +g_clear_pointer (&bufs->array, g_array_unref); +} + +static void vfio_state_buffers_assert_init (VFIOStateBuffers *bufs) +{ +assert (bufs->array); +} + +static guint vfio_state_buffers_size_get (VFIOStateBuffers *bufs) +{ +return bufs->array->len; +} + +static void vfio_state_buffers_size_set (VFIOStateBuffers *bufs,guint size) +{ +g_array_set_size (bufs->array, size); +} + +static VFIOStateBuffer *vfio_state_buffers_at (VFIOStateBuffers *bufs,guint idx) +{ +return &g_array_index (bufs->array, VFIOStateBuffer, idx); +}
Re: [PATCH v5 23/36] vfio/migration: Multifd device state transfer support - VFIOStateBuffer(s)
Quoted 8 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Add VFIOStateBuffer(s) types and the associated methods.
These store received device state buffers and config state waiting to get loaded into the device.
Signed-off-by Maciej S. Szmigiero <[email protected]>
Thanks,
C.
Quoted 67 lines
--- hw/vfio/migration-multifd.c | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+)hw/vfio/migration-multifd.c +54
-0@@ -29,3 +29,57 @@ typedef struct VFIODeviceStatePacket {uint32_t flags;uint8_t data[0 ]; }QEMU_PACKED VFIODeviceStatePacket; + +/* type safety */ +typedef struct VFIOStateBuffers { +GArray *array; +}VFIOStateBuffers ; + +typedef struct VFIOStateBuffer { +bool is_present; +char *data; +size_t len; +}VFIOStateBuffer ; + +static void vfio_state_buffer_clear (gpointer data) +{ +VFIOStateBuffer *lb = data; + +if (!lb->is_present) { +return ; + } + +g_clear_pointer (&lb->data, g_free); + lb->is_present = false; +} + +static void vfio_state_buffers_init (VFIOStateBuffers *bufs) +{ + bufs->array =g_array_new (FALSE, TRUE,sizeof (VFIOStateBuffer)); +g_array_set_clear_func (bufs->array, vfio_state_buffer_clear); +} + +static void vfio_state_buffers_destroy (VFIOStateBuffers *bufs) +{ +g_clear_pointer (&bufs->array, g_array_unref); +} + +static void vfio_state_buffers_assert_init (VFIOStateBuffers *bufs) +{ +assert (bufs->array); +} + +static guint vfio_state_buffers_size_get (VFIOStateBuffers *bufs) +{ +return bufs->array->len; +} + +static void vfio_state_buffers_size_set (VFIOStateBuffers *bufs,guint size) +{ +g_array_set_size (bufs->array, size); +} + +static VFIOStateBuffer *vfio_state_buffers_at (VFIOStateBuffers *bufs,guint idx) +{ +return &g_array_index (bufs->array, VFIOStateBuffer, idx); +}
Re: [PATCH v5 23/36] vfio/migration: Multifd device state transfer support - VFIOStateBuffer(s)
In reply to Maciej S. SzmigieroQuoted 67 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
Add VFIOStateBuffer(s) types and the associated methods.
These store received device state buffers and config state waiting to get loaded into the device.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+)hw/vfio/migration-multifd.c +44
-0@@ -29,3 +29,57 @@ typedef struct VFIODeviceStatePacket {uint32_t flags;uint8_t data[0 ]; }QEMU_PACKED VFIODeviceStatePacket; + +/* type safety */ +typedef struct VFIOStateBuffers { +GArray *array; +}VFIOStateBuffers ; + +typedef struct VFIOStateBuffer { +bool is_present; +char *data; +size_t len; +}VFIOStateBuffer ; + +static void vfio_state_buffer_clear (gpointer data) +{ +VFIOStateBuffer *lb = data; + +if (!lb->is_present) { +return ; + } + +g_clear_pointer (&lb->data, g_free); + lb->is_present = false; +} + +static void vfio_state_buffers_init (VFIOStateBuffers *bufs) +{ + bufs->array =g_array_new (FALSE, TRUE,sizeof (VFIOStateBuffer)); +g_array_set_clear_func (bufs->array, vfio_state_buffer_clear); +} + +static void vfio_state_buffers_destroy (VFIOStateBuffers *bufs) +{ +g_clear_pointer (&bufs->array, g_array_unref); +} + +static void vfio_state_buffers_assert_init (VFIOStateBuffers *bufs) +{ +assert (bufs->array); +} + +static guint vfio_state_buffers_size_get (VFIOStateBuffers *bufs) +{ +return bufs->array->len; +}
hw/vfio/migration-multifd.c typedef struct VFIODeviceStatePacket { · line 85
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ +static void vfio_state_buffers_size_set (VFIOStateBuffers *bufs,guint size) +{ +g_array_set_size (bufs->array, size); +} + +static VFIOStateBuffer *vfio_state_buffers_at (VFIOStateBuffers *bufs,guint idx) +{ +return &g_array_index (bufs->array, VFIOStateBuffer, idx); +}
This patch breaks compilation as non of the functions are used, e.g.: error: ‘vfio_state_buffers_init’ defined but not used I can think of three options to solve it: 1. Move these functions to their own file and export them, e.g., hw/vfio/state-buffer.{c,h}. But this seems like an overkill for such a small API. 2. Add __attribute__((unused)) tags and remove them in patch #26 where the functions are actually used. A bit ugly. 3. Squash this patch into patch #26. I prefer option 3 as this is a small API closely related to patch #26 (and patch #26 will still remain rather small).
Thanks.
Re: [PATCH v5 23/36] vfio/migration: Multifd device state transfer support - VFIOStateBuffer(s)
Quoted 73 lines · Avihai Horon
Quoted 71 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
Add VFIOStateBuffer(s) types and the associated methods.
These store received device state buffers and config state waiting to get loaded into the device.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 54 +++++++++++++++++++++++++++++++++++++1 file changed, 54 insertions(+)hw/vfio/migration-multifd.c +48
-0@@ -29,3 +29,57 @@ typedef struct VFIODeviceStatePacket { uint32_t flags; uint8_t data[0]; } QEMU_PACKED VFIODeviceStatePacket; + +/* type safety */ +typedef struct VFIOStateBuffers { + GArray *array; +}VFIOStateBuffers ; + +typedef struct VFIOStateBuffer { + bool is_present; + char *data; + size_t len; +}VFIOStateBuffer ; + +static void vfio_state_buffer_clear (gpointer data) +{ + VFIOStateBuffer *lb = data; + + if (!lb->is_present) { + return; + } + + g_clear_pointer(&lb->data, g_free); + lb->is_present = false; +} + +static void vfio_state_buffers_init (VFIOStateBuffers *bufs) +{ + bufs->array = g_array_new(FALSE, TRUE, sizeof(VFIOStateBuffer)); + g_array_set_clear_func(bufs->array, vfio_state_buffer_clear); +} + +static void vfio_state_buffers_destroy (VFIOStateBuffers *bufs) +{ + g_clear_pointer(&bufs->array, g_array_unref); +} + +static void vfio_state_buffers_assert_init (VFIOStateBuffers *bufs) +{ + assert(bufs->array); +} + +static guint vfio_state_buffers_size_get (VFIOStateBuffers *bufs) +{ + return bufs->array->len; +} + +static void vfio_state_buffers_size_set (VFIOStateBuffers *bufs,guint size) +{ + g_array_set_size(bufs->array, size);
Quoted 8 lines
hw/vfio/migration-multifd.c typedef struct VFIODeviceStatePacket { · line 85
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+} + +static VFIOStateBuffer *vfio_state_buffers_at (VFIOStateBuffers *bufs,guint idx) +{ + return &g_array_index(bufs->array, VFIOStateBuffer, idx); +}This patch breaks compilation as non of the functions are used, e.g.: error: ‘vfio_state_buffers_init’ defined but not used I can think of three options to solve it: 1. Move these functions to their own file and export them, e.g., hw/vfio/state-buffer.{c,h}. But this seems like an overkill for such a small API. 2. Add __attribute__((unused)) tags and remove them in patch #26 where the functions are actually used. A bit ugly. 3. Squash this patch into patch #26. I prefer option 3 as this is a small API closely related to patch #26 (and patch #26 will still remain rather small).
Looks like some build configs use -Werror, as unused functions aren't normally an error.
Will have look at this tomorrow.
Quoted 2 lines
Thanks.
Thanks,
Maciej
Re: [PATCH v5 23/36] vfio/migration: Multifd device state transfer support - VFIOStateBuffer(s)
In reply to Avihai HoronQuoted 83 lines · Avihai Horon
Quoted 67 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
Add VFIOStateBuffer(s) types and the associated methods.
These store received device state buffers and config state waiting to get loaded into the device.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 54 +++++++++++++++++++++++++++++++++++++1 file changed, 54 insertions(+)hw/vfio/migration-multifd.c +44
-0@@ -29,3 +29,57 @@ typedef struct VFIODeviceStatePacket { uint32_t flags; uint8_t data[0]; } QEMU_PACKED VFIODeviceStatePacket; + +/* type safety */ +typedef struct VFIOStateBuffers { + GArray *array; +}VFIOStateBuffers ; + +typedef struct VFIOStateBuffer { + bool is_present; + char *data; + size_t len; +}VFIOStateBuffer ; + +static void vfio_state_buffer_clear (gpointer data) +{ + VFIOStateBuffer *lb = data; + + if (!lb->is_present) { + return; + } + + g_clear_pointer(&lb->data, g_free); + lb->is_present = false; +} + +static void vfio_state_buffers_init (VFIOStateBuffers *bufs) +{ + bufs->array = g_array_new(FALSE, TRUE, sizeof(VFIOStateBuffer)); + g_array_set_clear_func(bufs->array, vfio_state_buffer_clear); +} + +static void vfio_state_buffers_destroy (VFIOStateBuffers *bufs) +{ + g_clear_pointer(&bufs->array, g_array_unref); +} + +static void vfio_state_buffers_assert_init (VFIOStateBuffers *bufs) +{ + assert(bufs->array); +} + +static guint vfio_state_buffers_size_get (VFIOStateBuffers *bufs) +{ + return bufs->array->len; +}hw/vfio/migration-multifd.c typedef struct VFIODeviceStatePacket { · line 85
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ +static void vfio_state_buffers_size_set (VFIOStateBuffers *bufs,guint size) +{ + g_array_set_size(bufs->array, size); +} + +static VFIOStateBuffer *vfio_state_buffers_at (VFIOStateBuffers *bufs,guint idx) +{ + return &g_array_index(bufs->array, VFIOStateBuffer, idx); +}This patch breaks compilation as non of the functions are used, e.g.: error: ‘vfio_state_buffers_init’ defined but not used I can think of three options to solve it: 1. Move these functions to their own file and export them, e.g., hw/vfio/state-buffer.{c,h}. But this seems like an overkill for such a small API. 2. Add __attribute__((unused)) tags and remove them in patch #26 where the functions are actually used. A bit ugly.
Quoted 1 line
3. Squash this patch into patch #26. I prefer option 3 as this is a small API closely related to patch #26 (and patch #26 will still remain rather small).
I vote for option 3 too.
vfio_state_buffers_init is only called once, it's 2 lines, it could be merged in vfio_multifd_new() too.
Thanks,
C.
Re: [PATCH v5 23/36] vfio/migration: Multifd device state transfer support - VFIOStateBuffer(s)
Quoted 84 lines · Cédric Le Goater
Quoted 83 lines · Avihai Horon
Quoted 67 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
Add VFIOStateBuffer(s) types and the associated methods.
These store received device state buffers and config state waiting to get loaded into the device.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 54 +++++++++++++++++++++++++++++++++++++1 file changed, 54 insertions(+)hw/vfio/migration-multifd.c +44
-0@@ -29,3 +29,57 @@ typedef struct VFIODeviceStatePacket { uint32_t flags; uint8_t data[0]; } QEMU_PACKED VFIODeviceStatePacket; + +/* type safety */ +typedef struct VFIOStateBuffers { + GArray *array; +}VFIOStateBuffers ; + +typedef struct VFIOStateBuffer { + bool is_present; + char *data; + size_t len; +}VFIOStateBuffer ; + +static void vfio_state_buffer_clear (gpointer data) +{ + VFIOStateBuffer *lb = data; + + if (!lb->is_present) { + return; + } + + g_clear_pointer(&lb->data, g_free); + lb->is_present = false; +} + +static void vfio_state_buffers_init (VFIOStateBuffers *bufs) +{ + bufs->array = g_array_new(FALSE, TRUE, sizeof(VFIOStateBuffer)); + g_array_set_clear_func(bufs->array, vfio_state_buffer_clear); +} + +static void vfio_state_buffers_destroy (VFIOStateBuffers *bufs) +{ + g_clear_pointer(&bufs->array, g_array_unref); +} + +static void vfio_state_buffers_assert_init (VFIOStateBuffers *bufs) +{ + assert(bufs->array); +} + +static guint vfio_state_buffers_size_get (VFIOStateBuffers *bufs) +{ + return bufs->array->len; +}hw/vfio/migration-multifd.c typedef struct VFIODeviceStatePacket { · line 85
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ +static void vfio_state_buffers_size_set (VFIOStateBuffers *bufs,guint size) +{ + g_array_set_size(bufs->array, size); +} + +static VFIOStateBuffer *vfio_state_buffers_at (VFIOStateBuffers *bufs,guint idx) +{ + return &g_array_index(bufs->array, VFIOStateBuffer, idx); +}This patch breaks compilation as non of the functions are used, e.g.: error: ‘vfio_state_buffers_init’ defined but not used I can think of three options to solve it: 1. Move these functions to their own file and export them, e.g., hw/vfio/state-buffer.{c,h}. But this seems like an overkill for such a small API. 2. Add __attribute__((unused)) tags and remove them in patch #26 where the functions are actually used. A bit ugly.
Quoted 3 lines
Quoted 1 line
3. Squash this patch into patch #26. I prefer option 3 as this is a small API closely related to patch #26 (and patch #26 will still remain rather small).
I vote for option 3 too.
Merged this patch into the "received buffers queuing" one (#26) now.
Quoted 2 lines
vfio_state_buffers_initis only called once, it's 2 lines, it could be merged invfio_multifd_new()too.
Most of these helpers are even shorter (1 line), but the whole point of them is to abstract the GArray rather than open-code these accesses.
This was discussed two versions ago:
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 5 lines
Thanks,
C.
Thanks,
Maciej
PATCH v5 24/36 vfio/migration: Multifd device state transfer - add support checking function
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Add vfio_multifd_transfer_supported() function that tells whether the multifd device state transfer is supported.
--- hw/vfio/migration-multifd.c | 6 ++++++ hw/vfio/migration-multifd.h | 2 ++ 2 files changed, 8 insertions(+)
hw/vfio/migration-multifd.c +6 -0
@@ -83,3 +83,9 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) {return &g_array_index (bufs->array, VFIOStateBuffer, idx); } + +bool vfio_multifd_transfer_supported (void ) +{ +return multifd_device_state_supported () && +migrate_send_switchover_start (); +}
hw/vfio/migration-multifd.h +2 -0
@@ -12,4 +12,6 @@#include "hw/vfio/vfio-common.h" +bool vfio_multifd_transfer_supported (void ); +#endif
Re: [PATCH v5 24/36] vfio/migration: Multifd device state transfer - add support checking function
Quoted 6 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Add
vfio_multifd_transfer_supported()function that tells whether the multifd device state transfer is supported.Signed-off-by Maciej S. Szmigiero <[email protected]>
Thanks,
C.
Quoted 31 lines
--- hw/vfio/migration-multifd.c | 6 ++++++ hw/vfio/migration-multifd.h | 2 ++ 2 files changed, 8 insertions(+)hw/vfio/migration-multifd.c +6
-0@@ -83,3 +83,9 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) {return &g_array_index (bufs->array, VFIOStateBuffer, idx); } + +bool vfio_multifd_transfer_supported (void ) +{ +return multifd_device_state_supported () && +migrate_send_switchover_start (); +}hw/vfio/migration-multifd.h +2
-0@@ -12,4 +12,6 @@#include "hw/vfio/vfio-common.h" +bool vfio_multifd_transfer_supported (void ); +#endif
PATCH v5 25/36 vfio/migration: Multifd device state transfer support - receive init/cleanup
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Add support for VFIOMultifd data structure that will contain most of the receive-side data together with its init/cleanup methods.
--- hw/vfio/migration-multifd.c | 33 +++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 8 ++++++++ hw/vfio/migration.c | 29 +++++++++++++++++++++++++++-- include/hw/vfio/vfio-common.h | 3 +++ 4 files changed, 71 insertions(+), 2 deletions(-)
hw/vfio/migration-multifd.c +33 -0
@@ -41,6 +41,9 @@ typedef struct VFIOStateBuffer {size_t len; } VFIOStateBuffer; +typedef struct VFIOMultifd { +}VFIOMultifd ; +static void vfio_state_buffer_clear (gpointer data) {VFIOStateBuffer *lb = data;
@@ -84,8 +87,38 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx)return &g_array_index (bufs->array, VFIOStateBuffer, idx); } +VFIOMultifd *vfio_multifd_new (void ) +{ +VFIOMultifd *multifd =g_new (VFIOMultifd,1 ); + +return multifd; +} + +void vfio_multifd_free (VFIOMultifd *multifd) +{ +g_free (multifd); +} +bool vfio_multifd_transfer_supported (void ) {return multifd_device_state_supported () &&migrate_send_switchover_start (); } + +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev) +{ +return false; +} + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp) +{ +if (vfio_multifd_transfer_enabled (vbasedev) && + !vfio_multifd_transfer_supported ()) { +error_setg (errp, +"%s: Multifd device transfer requested but unsupported in the current config" , + vbasedev->name); +return false; + } + +return true; +}
hw/vfio/migration-multifd.h +8 -0
@@ -12,6 +12,14 @@#include "hw/vfio/vfio-common.h" +typedef struct VFIOMultifd VFIOMultifd ; + +VFIOMultifd *vfio_multifd_new (void ); +void vfio_multifd_free (VFIOMultifd *multifd); +bool vfio_multifd_transfer_supported (void ); +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev); + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp);#endif
hw/vfio/migration.c +27 -2
@@ -674,15 +674,40 @@ static void vfio_save_state(QEMUFile *f, void *opaque)static int vfio_load_setup (QEMUFile *f,void *opaque,Error **errp) {VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +int ret; + +if (!vfio_multifd_transfer_setup (vbasedev, errp)) { +return -EINVAL ; + } + + ret =vfio_migration_set_state (vbasedev,VFIO_DEVICE_STATE_RESUMING , + migration->device_state, errp); +if (ret) { +return ret; + } -return vfio_migration_set_state (vbasedev,VFIO_DEVICE_STATE_RESUMING , - vbasedev->migration->device_state, errp); +if (vfio_multifd_transfer_enabled (vbasedev)) { +assert (!migration->multifd); + migration->multifd =vfio_multifd_new (); + } + +return 0 ; +} + +static void vfio_multifd_cleanup (VFIODevice *vbasedev) +{ +VFIOMigration *migration = vbasedev->migration; + +g_clear_pointer (&migration->multifd, vfio_multifd_free); }static int vfio_load_cleanup (void *opaque) {VFIODevice *vbasedev = opaque; +vfio_multifd_cleanup (vbasedev); +vfio_migration_cleanup (vbasedev);trace_vfio_load_cleanup (vbasedev->name);
include/hw/vfio/vfio-common.h +3 -0
@@ -78,6 +78,8 @@ typedef struct VFIORegion {uint8_t nr;/* cache the region number for debug */ } VFIORegion; +typedef struct VFIOMultifd VFIOMultifd ; + typedef struct VFIOMigration { struct VFIODevice *vbasedev; VMChangeStateEntry *vm_state;
@@ -89,6 +91,7 @@ typedef struct VFIOMigration {uint64_t mig_flags;uint64_t precopy_init_size;uint64_t precopy_dirty_size; +VFIOMultifd *multifd;bool initial_data_sent;bool event_save_iterate_started;
Re: [PATCH v5 25/36] vfio/migration: Multifd device state transfer support - receive init/cleanup
Quoted 90 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Add support for VFIOMultifd data structure that will contain most of the receive-side data together with its init/cleanup methods.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 33 +++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 8 ++++++++ hw/vfio/migration.c | 29 +++++++++++++++++++++++++++-- include/hw/vfio/vfio-common.h | 3 +++ 4 files changed, 71 insertions(+), 2 deletions(-)hw/vfio/migration-multifd.c +33
-0@@ -41,6 +41,9 @@ typedef struct VFIOStateBuffer {size_t len; } VFIOStateBuffer; +typedef struct VFIOMultifd { +}VFIOMultifd ; +static void vfio_state_buffer_clear (gpointer data) {VFIOStateBuffer *lb = data;@@ -84,8 +87,38 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx)return &g_array_index (bufs->array, VFIOStateBuffer, idx); } +VFIOMultifd *vfio_multifd_new (void ) +{ +VFIOMultifd *multifd =g_new (VFIOMultifd,1 ); + +return multifd; +} + +void vfio_multifd_free (VFIOMultifd *multifd) +{ +g_free (multifd); +} +bool vfio_multifd_transfer_supported (void ) {return multifd_device_state_supported () &&migrate_send_switchover_start (); } + +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev) +{ +return false; +} + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp) +{ +if (vfio_multifd_transfer_enabled (vbasedev) && + !vfio_multifd_transfer_supported ()) { +error_setg (errp, +"%s: Multifd device transfer requested but unsupported in the current config" , + vbasedev->name); +return false; + } + +return true; +}hw/vfio/migration-multifd.h +8
-0@@ -12,6 +12,14 @@#include "hw/vfio/vfio-common.h" +typedef struct VFIOMultifd VFIOMultifd ; + +VFIOMultifd *vfio_multifd_new (void ); +void vfio_multifd_free (VFIOMultifd *multifd); +bool vfio_multifd_transfer_supported (void ); +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev); + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp);#endif hw/vfio/migration.c +0
-0
hw/vfio/migration.c static void vfio_save_state(QEMUFile *f, void *opaque) · line 682
Quoted 9 lines
hw/vfio/migration.c +6
-0@@ -674,15 +674,40 @@ static void vfio_save_state(QEMUFile *f, void *opaque)static int vfio_load_setup (QEMUFile *f,void *opaque,Error **errp) {VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +int ret; + +if (!vfio_multifd_transfer_setup (vbasedev, errp)) { +return -EINVAL ; + }
This check on the consistency of the settings confused me a little. Even if simple, I would have put it in a separate patch for better understanding.
The rest looks good.
Thanks,
C.
Quoted 54 lines
hw/vfio/migration.c +20
-2+ ret =vfio_migration_set_state (vbasedev,VFIO_DEVICE_STATE_RESUMING , + migration->device_state, errp); +if (ret) { +return ret; + } -return vfio_migration_set_state (vbasedev,VFIO_DEVICE_STATE_RESUMING , - vbasedev->migration->device_state, errp); +if (vfio_multifd_transfer_enabled (vbasedev)) { +assert (!migration->multifd); + migration->multifd =vfio_multifd_new (); + } + +return 0 ; +} + +static void vfio_multifd_cleanup (VFIODevice *vbasedev) +{ +VFIOMigration *migration = vbasedev->migration; + +g_clear_pointer (&migration->multifd, vfio_multifd_free); }static int vfio_load_cleanup (void *opaque) {VFIODevice *vbasedev = opaque; +vfio_multifd_cleanup (vbasedev); +vfio_migration_cleanup (vbasedev);trace_vfio_load_cleanup (vbasedev->name);include/hw/vfio/vfio-common.h +3
-0@@ -78,6 +78,8 @@ typedef struct VFIORegion {uint8_t nr;/* cache the region number for debug */ } VFIORegion; +typedef struct VFIOMultifd VFIOMultifd ; + typedef struct VFIOMigration { struct VFIODevice *vbasedev; VMChangeStateEntry *vm_state;@@ -89,6 +91,7 @@ typedef struct VFIOMigration {uint64_t mig_flags;uint64_t precopy_init_size;uint64_t precopy_dirty_size; +VFIOMultifd *multifd;bool initial_data_sent;bool event_save_iterate_started;
Re: [PATCH v5 25/36] vfio/migration: Multifd device state transfer support - receive init/cleanup
Quoted 54 lines · Cédric Le Goater
Quoted 53 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Add support for VFIOMultifd data structure that will contain most of the receive-side data together with its init/cleanup methods.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 33 +++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 8 ++++++++ hw/vfio/migration.c | 29 +++++++++++++++++++++++++++--include/hw/vfio/vfio-common.h| 3 +++4 files changed, 71 insertions(+), 2 deletions(-)hw/vfio/migration-multifd.c +15
-0@@ -41,6 +41,9 @@ typedef struct VFIOStateBuffer { size_t len; } VFIOStateBuffer; +typedef struct VFIOMultifd { +}VFIOMultifd ; + static void vfio_state_buffer_clear(gpointer data) { VFIOStateBuffer *lb = data;@@ -84,8 +87,38 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) return &g_array_index(bufs->array, VFIOStateBuffer, idx); } +VFIOMultifd *vfio_multifd_new (void ) +{ + VFIOMultifd *multifd = g_new(VFIOMultifd, 1); + + return multifd; +} + +void vfio_multifd_free (VFIOMultifd *multifd) +{ + g_free(multifd); +} + bool vfio_multifd_transfer_supported(void) {return
multifd_device_state_supported()&&
migrate_send_switchover_start();
}hw/vfio/migration-multifd.c +7
-0+ +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev) +{ + return false; +} + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp)
Quoted 8 lines
Quoted 8 lines
+{
+ if (vfio_multifd_transfer_enabled(vbasedev)&&
+ !vfio_multifd_transfer_supported()) {
+ error_setg(errp,
+ "%s: Multifd device transfer requested but unsupported in the current config", +vbasedev->name);
+ return false;
+ }
now that I have reached patch 31 I understand better. I would put the check above in patch 31 and simply return true for now.
Thanks,
C.
Quoted 93 lines
Quoted 23 lines
+ return true;
hw/vfio/migration-multifd.c +1
-0+}hw/vfio/migration-multifd.h +8
-0@@ -12,6 +12,14 @@ #include "hw/vfio/vfio-common.h" +typedef struct VFIOMultifd VFIOMultifd ; + +VFIOMultifd *vfio_multifd_new (void ); +void vfio_multifd_free (VFIOMultifd *multifd); + bool vfio_multifd_transfer_supported(void); +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev); + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp); #endifhw/vfio/migration.c +0
-0hw/vfio/migration.c static void vfio_save_state(QEMUFile *f, void *opaque) · line 682
Quoted 9 lines
hw/vfio/migration.c +6
-0@@ -674,15 +674,40 @@ static void vfio_save_state(QEMUFile *f, void *opaque) static int vfio_load_setup(QEMUFile *f, void *opaque, Error **errp) { VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + int ret; + + if (!vfio_multifd_transfer_setup(vbasedev, errp)) { + return -EINVAL; + }This check on the consistency of the settings confused me a little. Even if simple, I would have put it in a separate patch for better understanding. The rest looks good.
Thanks,
C.
Quoted 48 lines
hw/vfio/migration.c +20
-2+ ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RESUMING, + migration->device_state, errp); + if (ret) { + return ret; + } - return vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RESUMING, - vbasedev->migration->device_state, errp); + if (vfio_multifd_transfer_enabled(vbasedev)) { + assert(!migration->multifd); + migration->multifd = vfio_multifd_new(); + } + + return 0; +} + +static void vfio_multifd_cleanup (VFIODevice *vbasedev) +{ + VFIOMigration *migration = vbasedev->migration; + + g_clear_pointer(&migration->multifd, vfio_multifd_free); } static int vfio_load_cleanup(void *opaque) { VFIODevice *vbasedev = opaque; + vfio_multifd_cleanup(vbasedev); + vfio_migration_cleanup(vbasedev); trace_vfio_load_cleanup(vbasedev->name);include/hw/vfio/vfio-common.h +0
-0@@ -78,6 +78,8 @@ typedef struct VFIORegion {
uint8_tnr; /* cache the region number for debug */ } VFIORegion;include/hw/vfio/vfio-common.h +2
-0+typedef struct VFIOMultifd VFIOMultifd ; +typedef struct VFIOMigration {
struct VFIODevice *vbasedev;
VMChangeStateEntry*vm_state;include/hw/vfio/vfio-common.h +1
-0@@ -89,6 +91,7 @@ typedef struct VFIOMigration { uint64_t mig_flags; uint64_t precopy_init_size; uint64_t precopy_dirty_size; + VFIOMultifd *multifd; bool initial_data_sent; bool event_save_iterate_started;
Re: [PATCH v5 25/36] vfio/migration: Multifd device state transfer support - receive init/cleanup
Quoted 67 lines · Cédric Le Goater
Quoted 57 lines · Cédric Le Goater
Quoted 56 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Add support for VFIOMultifd data structure that will contain most of the receive-side data together with its init/cleanup methods.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 33 +++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 8 ++++++++ hw/vfio/migration.c | 29 +++++++++++++++++++++++++++--include/hw/vfio/vfio-common.h| 3 +++4 files changed, 71 insertions(+), 2 deletions(-)hw/vfio/migration-multifd.c +15
-0@@ -41,6 +41,9 @@ typedef struct VFIOStateBuffer { size_t len; } VFIOStateBuffer; +typedef struct VFIOMultifd { +}VFIOMultifd ; + static void vfio_state_buffer_clear(gpointer data) { VFIOStateBuffer *lb = data;@@ -84,8 +87,38 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) return &g_array_index(bufs->array, VFIOStateBuffer, idx); } +VFIOMultifd *vfio_multifd_new (void ) +{ + VFIOMultifd *multifd = g_new(VFIOMultifd, 1); + + return multifd; +} + +void vfio_multifd_free (VFIOMultifd *multifd) +{ + g_free(multifd); +} + bool vfio_multifd_transfer_supported(void) {return
multifd_device_state_supported()&&
migrate_send_switchover_start();
}hw/vfio/migration-multifd.c +10
-0+ +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev) +{ + return false; +} + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp) +{ + if (vfio_multifd_transfer_enabled(vbasedev) && + !vfio_multifd_transfer_supported()) {Quoted 8 lines
Quoted 5 lines
+ error_setg(errp,
+ "%s: Multifd device transfer requested but unsupported in the current config", +vbasedev->name);
+ return false;
+ }This check on the consistency of the settings confused me a little. Even if simple, I would have put it in a separate patch for better understanding. The rest looks good.
Quoted 3 lines
now that I have reached patch 31 I understand better. I would put the check above in patch 31 and simply return true for now.
Moved it to that patch then ("Add x-migration-multifd-transfer VFIO property").
Quoted 4 lines
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 25/36] vfio/migration: Multifd device state transfer support - receive init/cleanup
In reply to Maciej S. SzmigieroQuoted 112 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Add support for VFIOMultifd data structure that will contain most of the receive-side data together with its init/cleanup methods.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 33 +++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 8 ++++++++ hw/vfio/migration.c | 29 +++++++++++++++++++++++++++-- include/hw/vfio/vfio-common.h | 3 +++ 4 files changed, 71 insertions(+), 2 deletions(-)hw/vfio/migration-multifd.c +33
-0@@ -41,6 +41,9 @@ typedef struct VFIOStateBuffer {size_t len; } VFIOStateBuffer; +typedef struct VFIOMultifd { +}VFIOMultifd ; +static void vfio_state_buffer_clear (gpointer data) {VFIOStateBuffer *lb = data;@@ -84,8 +87,38 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx)return &g_array_index (bufs->array, VFIOStateBuffer, idx); } +VFIOMultifd *vfio_multifd_new (void ) +{ +VFIOMultifd *multifd =g_new (VFIOMultifd,1 ); + +return multifd; +} + +void vfio_multifd_free (VFIOMultifd *multifd) +{ +g_free (multifd); +} +bool vfio_multifd_transfer_supported (void ) {return multifd_device_state_supported () &&migrate_send_switchover_start (); } + +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev) +{ +return false; +} + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp) +{ +if (vfio_multifd_transfer_enabled (vbasedev) && + !vfio_multifd_transfer_supported ()) { +error_setg (errp, +"%s: Multifd device transfer requested but unsupported in the current config" , + vbasedev->name); +return false; + } + +return true; +}hw/vfio/migration-multifd.h +8
-0@@ -12,6 +12,14 @@#include "hw/vfio/vfio-common.h" +typedef struct VFIOMultifd VFIOMultifd ; + +VFIOMultifd *vfio_multifd_new (void ); +void vfio_multifd_free (VFIOMultifd *multifd); +bool vfio_multifd_transfer_supported (void ); +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev); + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp);#endif hw/vfio/migration.c +16
-2@@ -674,15 +674,40 @@ static void vfio_save_state(QEMUFile *f, void *opaque)static int vfio_load_setup (QEMUFile *f,void *opaque,Error **errp) {VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +int ret; + +if (!vfio_multifd_transfer_setup (vbasedev, errp)) { +return -EINVAL ; + } + + ret =vfio_migration_set_state (vbasedev,VFIO_DEVICE_STATE_RESUMING , + migration->device_state, errp); +if (ret) { +return ret; + } -return vfio_migration_set_state (vbasedev,VFIO_DEVICE_STATE_RESUMING , - vbasedev->migration->device_state, errp); +if (vfio_multifd_transfer_enabled (vbasedev)) { +assert (!migration->multifd); + migration->multifd =vfio_multifd_new (); + }
hw/vfio/migration.c static void vfio_save_state(QEMUFile *f, void *opaque) · line 703
Quoted 10 lines
hw/vfio/migration.c +9
-0+ +return 0 ; +} + +static void vfio_multifd_cleanup (VFIODevice *vbasedev) +{ +VFIOMigration *migration = vbasedev->migration; + +g_clear_pointer (&migration->multifd, vfio_multifd_free); }
Please move vfio_multifd_cleanup() to migration-multifd.c.
Thanks,
C.
Quoted 31 lines
hw/vfio/migration.c +2
-0static int vfio_load_cleanup (void *opaque) {VFIODevice *vbasedev = opaque; +vfio_multifd_cleanup (vbasedev); +vfio_migration_cleanup (vbasedev);trace_vfio_load_cleanup (vbasedev->name);include/hw/vfio/vfio-common.h +3
-0@@ -78,6 +78,8 @@ typedef struct VFIORegion {uint8_t nr;/* cache the region number for debug */ } VFIORegion; +typedef struct VFIOMultifd VFIOMultifd ; + typedef struct VFIOMigration { struct VFIODevice *vbasedev; VMChangeStateEntry *vm_state;@@ -89,6 +91,7 @@ typedef struct VFIOMigration {uint64_t mig_flags;uint64_t precopy_init_size;uint64_t precopy_dirty_size; +VFIOMultifd *multifd;bool initial_data_sent;bool event_save_iterate_started;
Re: [PATCH v5 25/36] vfio/migration: Multifd device state transfer support - receive init/cleanup
Quoted 111 lines · Cédric Le Goater
Quoted 110 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Add support for VFIOMultifd data structure that will contain most of the receive-side data together with its init/cleanup methods.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 33 +++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 8 ++++++++ hw/vfio/migration.c | 29 +++++++++++++++++++++++++++--include/hw/vfio/vfio-common.h| 3 +++4 files changed, 71 insertions(+), 2 deletions(-)hw/vfio/migration-multifd.c +15
-0@@ -41,6 +41,9 @@ typedef struct VFIOStateBuffer { size_t len; } VFIOStateBuffer; +typedef struct VFIOMultifd { +}VFIOMultifd ; + static void vfio_state_buffer_clear(gpointer data) { VFIOStateBuffer *lb = data;@@ -84,8 +87,38 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) return &g_array_index(bufs->array, VFIOStateBuffer, idx); } +VFIOMultifd *vfio_multifd_new (void ) +{ + VFIOMultifd *multifd = g_new(VFIOMultifd, 1); + + return multifd; +} + +void vfio_multifd_free (VFIOMultifd *multifd) +{ + g_free(multifd); +} + bool vfio_multifd_transfer_supported(void) {return
multifd_device_state_supported()&&
migrate_send_switchover_start();
}hw/vfio/migration-multifd.c +18
-0+ +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev) +{ + return false; +} + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp) +{ + if (vfio_multifd_transfer_enabled(vbasedev) && + !vfio_multifd_transfer_supported()) { + error_setg(errp, + "%s: Multifd device transfer requested but unsupported in the current config", + vbasedev->name); + return false; + } + + return true; +}hw/vfio/migration-multifd.h +8
-0@@ -12,6 +12,14 @@ #include "hw/vfio/vfio-common.h" +typedef struct VFIOMultifd VFIOMultifd ; + +VFIOMultifd *vfio_multifd_new (void ); +void vfio_multifd_free (VFIOMultifd *multifd); + bool vfio_multifd_transfer_supported(void); +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev); + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp); #endifhw/vfio/migration.c +20
-2@@ -674,15 +674,40 @@ static void vfio_save_state(QEMUFile *f, void *opaque) static int vfio_load_setup(QEMUFile *f, void *opaque, Error **errp) { VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + int ret; + + if (!vfio_multifd_transfer_setup(vbasedev, errp)) { + return -EINVAL; + } + + ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RESUMING, + migration->device_state, errp); + if (ret) { + return ret; + } - return vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RESUMING, - vbasedev->migration->device_state, errp); + if (vfio_multifd_transfer_enabled(vbasedev)) { + assert(!migration->multifd); + migration->multifd = vfio_multifd_new(); + } + + return 0; +} +
Quoted 8 lines
hw/vfio/migration.c static void vfio_save_state(QEMUFile *f, void *opaque) · line 703
Quoted 6 lines
hw/vfio/migration.c +5
-0+static void vfio_multifd_cleanup (VFIODevice *vbasedev) +{ + VFIOMigration *migration = vbasedev->migration; + + g_clear_pointer(&migration->multifd, vfio_multifd_free); }Please move
vfio_multifd_cleanup()tomigration-multifd.c.
Done now.
Quoted 4 lines
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 25/36] vfio/migration: Multifd device state transfer support - receive init/cleanup
In reply to Maciej S. SzmigieroQuoted 101 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Add support for VFIOMultifd data structure that will contain most of the receive-side data together with its init/cleanup methods.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 33 +++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 8 ++++++++ hw/vfio/migration.c | 29 +++++++++++++++++++++++++++-- include/hw/vfio/vfio-common.h | 3 +++ 4 files changed, 71 insertions(+), 2 deletions(-)hw/vfio/migration-multifd.c +33
-0@@ -41,6 +41,9 @@ typedef struct VFIOStateBuffer {size_t len; } VFIOStateBuffer; +typedef struct VFIOMultifd { +}VFIOMultifd ; +static void vfio_state_buffer_clear (gpointer data) {VFIOStateBuffer *lb = data;@@ -84,8 +87,38 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx)return &g_array_index (bufs->array, VFIOStateBuffer, idx); } +VFIOMultifd *vfio_multifd_new (void ) +{ +VFIOMultifd *multifd =g_new (VFIOMultifd,1 ); + +return multifd; +} + +void vfio_multifd_free (VFIOMultifd *multifd) +{ +g_free (multifd); +} +bool vfio_multifd_transfer_supported (void ) {return multifd_device_state_supported () &&migrate_send_switchover_start (); } + +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev) +{ +return false; +} + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp) +{ +if (vfio_multifd_transfer_enabled (vbasedev) && + !vfio_multifd_transfer_supported ()) { +error_setg (errp, +"%s: Multifd device transfer requested but unsupported in the current config" , + vbasedev->name); +return false; + } + +return true; +}hw/vfio/migration-multifd.h +8
-0@@ -12,6 +12,14 @@#include "hw/vfio/vfio-common.h" +typedef struct VFIOMultifd VFIOMultifd ; + +VFIOMultifd *vfio_multifd_new (void ); +void vfio_multifd_free (VFIOMultifd *multifd); +bool vfio_multifd_transfer_supported (void ); +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev); + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp);#endif hw/vfio/migration.c +8
-0@@ -674,15 +674,40 @@ static void vfio_save_state(QEMUFile *f, void *opaque)static int vfio_load_setup (QEMUFile *f,void *opaque,Error **errp) {VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +int ret; + +if (!vfio_multifd_transfer_setup (vbasedev, errp)) { +return -EINVAL ; + } + + ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RESUMING,
hw/vfio/migration.c static void vfio_save_state(QEMUFile *f, void *opaque) · line 692
Quoted 10 lines
hw/vfio/migration.c +7
-2+ migration->device_state, errp); +if (ret) { +return ret; + } -return vfio_migration_set_state (vbasedev,VFIO_DEVICE_STATE_RESUMING , - vbasedev->migration->device_state, errp); +if (vfio_multifd_transfer_enabled (vbasedev)) { +assert (!migration->multifd); + migration->multifd =vfio_multifd_new ();
When called from vfio_load_setup(), I think vfio_multifd_transfer_setup() should allocate migration->multifd at the same time. It would simplify the setup to one step. Maybe we could add a bool parameter ? because, IIRC, you didn't like the idea of allocating it always, that is in vfio_save_setup() too.
For symmetry, could vfio_save_cleanup() call vfio_multifd_cleanup() too ? a setup implies a cleanup.
Thanks,
C.
Quoted 43 lines
hw/vfio/migration.c +12
-0+ } + +return 0 ; +} + +static void vfio_multifd_cleanup (VFIODevice *vbasedev) +{ +VFIOMigration *migration = vbasedev->migration; + +g_clear_pointer (&migration->multifd, vfio_multifd_free); }static int vfio_load_cleanup (void *opaque) {VFIODevice *vbasedev = opaque; +vfio_multifd_cleanup (vbasedev); +vfio_migration_cleanup (vbasedev);trace_vfio_load_cleanup (vbasedev->name);include/hw/vfio/vfio-common.h +3
-0@@ -78,6 +78,8 @@ typedef struct VFIORegion {uint8_t nr;/* cache the region number for debug */ } VFIORegion; +typedef struct VFIOMultifd VFIOMultifd ; + typedef struct VFIOMigration { struct VFIODevice *vbasedev; VMChangeStateEntry *vm_state;@@ -89,6 +91,7 @@ typedef struct VFIOMigration {uint64_t mig_flags;uint64_t precopy_init_size;uint64_t precopy_dirty_size; +VFIOMultifd *multifd;bool initial_data_sent;bool event_save_iterate_started;
Re: [PATCH v5 25/36] vfio/migration: Multifd device state transfer support - receive init/cleanup
Quoted 104 lines · Cédric Le Goater
Quoted 103 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Add support for VFIOMultifd data structure that will contain most of the receive-side data together with its init/cleanup methods.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 33 +++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 8 ++++++++ hw/vfio/migration.c | 29 +++++++++++++++++++++++++++--include/hw/vfio/vfio-common.h| 3 +++4 files changed, 71 insertions(+), 2 deletions(-)hw/vfio/migration-multifd.c +15
-0@@ -41,6 +41,9 @@ typedef struct VFIOStateBuffer { size_t len; } VFIOStateBuffer; +typedef struct VFIOMultifd { +}VFIOMultifd ; + static void vfio_state_buffer_clear(gpointer data) { VFIOStateBuffer *lb = data;@@ -84,8 +87,38 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) return &g_array_index(bufs->array, VFIOStateBuffer, idx); } +VFIOMultifd *vfio_multifd_new (void ) +{ + VFIOMultifd *multifd = g_new(VFIOMultifd, 1); + + return multifd; +} + +void vfio_multifd_free (VFIOMultifd *multifd) +{ + g_free(multifd); +} + bool vfio_multifd_transfer_supported(void) {return
multifd_device_state_supported()&&
migrate_send_switchover_start();
}hw/vfio/migration-multifd.c +18
-0+ +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev) +{ + return false; +} + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp) +{ + if (vfio_multifd_transfer_enabled(vbasedev) && + !vfio_multifd_transfer_supported()) { + error_setg(errp, + "%s: Multifd device transfer requested but unsupported in the current config", + vbasedev->name); + return false; + } + + return true; +}hw/vfio/migration-multifd.h +8
-0@@ -12,6 +12,14 @@ #include "hw/vfio/vfio-common.h" +typedef struct VFIOMultifd VFIOMultifd ; + +VFIOMultifd *vfio_multifd_new (void ); +void vfio_multifd_free (VFIOMultifd *multifd); + bool vfio_multifd_transfer_supported(void); +bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev); + +bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp); #endifhw/vfio/migration.c +13
-2@@ -674,15 +674,40 @@ static void vfio_save_state(QEMUFile *f, void *opaque) static int vfio_load_setup(QEMUFile *f, void *opaque, Error **errp) { VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + int ret; + + if (!vfio_multifd_transfer_setup(vbasedev, errp)) { + return -EINVAL; + } + + ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RESUMING, + migration->device_state, errp); + if (ret) { + return ret; + } - return vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RESUMING, - vbasedev->migration->device_state, errp); + if (vfio_multifd_transfer_enabled(vbasedev)) {
Quoted 8 lines
hw/vfio/migration.c static void vfio_save_state(QEMUFile *f, void *opaque) · line 692
Quoted 2 lines
hw/vfio/migration.c +2
-0+ assert(!migration->multifd); + migration->multifd = vfio_multifd_new();When called from
vfio_load_setup(), I thinkvfio_multifd_transfer_setup()should allocatemigration->multifdat the same time. It would simplify the setup to one step. Maybe we could add a bool parameter ? because, IIRC, you didn't like the idea of allocating it always, that is invfio_save_setup()too.
I have added a "bool alloc_multifd" parameter to vfio_multifd_transfer_setup() and renamed it to vfio_multifd_setup() for consistency with vfio_multifd_cleanup().
Unexported vfio_multifd_new() now that it is called only from vfio_multifd_setup() in the same translation unit.
Quoted 3 lines
For symmetry, could
vfio_save_cleanup()callvfio_multifd_cleanup()too ? a setup implies a cleanup.
Added vfio_multifd_cleanup() call to vfio_save_cleanup() with a comment describing that it is currently a NOP.
Quoted 3 lines
Thanks,
C.
Thanks,
Maciej
PATCH v5 26/36 vfio/migration: Multifd device state transfer support - received buffers queuing
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
The multifd received data needs to be reassembled since device state packets sent via different multifd channels can arrive out-of-order.
Therefore, each VFIO device state packet carries a header indicating its position in the stream.
The raw device state data is saved into a VFIOStateBuffer for later in-order loading into the device.
The last such VFIO device state packet should have VFIO_DEVICE_STATE_CONFIG_STATE flag set and carry the device config state.
--- hw/vfio/migration-multifd.c | 103 ++++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 3 ++ hw/vfio/migration.c | 1 + hw/vfio/trace-events | 1 + 4 files changed, 108 insertions(+)
hw/vfio/migration-multifd.c +103 -0
@@ -42,6 +42,11 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer;typedef struct VFIOMultifd { +VFIOStateBuffers load_bufs; +QemuCond load_bufs_buffer_ready_cond; +QemuMutex load_bufs_mutex;/* Lock order: this lock -> BQL */ +uint32_t load_buf_idx; +uint32_t load_buf_idx_last; }VFIOMultifd ;static void vfio_state_buffer_clear (gpointer data)
@@ -87,15 +92,113 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx)return &g_array_index (bufs->array, VFIOStateBuffer, idx); } +static bool vfio_load_state_buffer_insert (VFIODevice *vbasedev, +VFIODeviceStatePacket *packet, +size_t packet_total_size, +Error **errp) +{ +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +VFIOStateBuffer *lb; + +vfio_state_buffers_assert_init (&multifd->load_bufs); +if (packet->idx >=vfio_state_buffers_size_get (&multifd->load_bufs)) { +vfio_state_buffers_size_set (&multifd->load_bufs, packet->idx +1 ); + } + + lb =vfio_state_buffers_at (&multifd->load_bufs, packet->idx); +if (lb->is_present) { +error_setg (errp,"state buffer %" PRIu32" already filled" , + packet->idx); +return false; + } + +assert (packet->idx >= multifd->load_buf_idx); + + lb->data =g_memdup2 (&packet->data, packet_total_size -sizeof (*packet)); + lb->len = packet_total_size -sizeof (*packet); + lb->is_present = true; + +return true; +} + +bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size, +Error **errp) +{ +VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +VFIODeviceStatePacket *packet = (VFIODeviceStatePacket *)data; + +/* +* Holding BQL here would violate the lock order and can cause +* a deadlock once we attempt to lock load_bufs_mutex below. +*/ +assert (!bql_locked ()); + +if (!vfio_multifd_transfer_enabled (vbasedev)) { +error_setg (errp, +"got device state packet but not doing multifd transfer" ); +return false; + } + +assert (multifd); + +if (data_size <sizeof (*packet)) { +error_setg (errp,"packet too short at %zu (min is %zu)" , + data_size,sizeof (*packet)); +return false; + } + +if (packet->version !=VFIO_DEVICE_STATE_PACKET_VER_CURRENT ) { +error_setg (errp,"packet has unknown version %" PRIu32, + packet->version); +return false; + } + +if (packet->idx ==UINT32_MAX ) { +error_setg (errp,"packet has too high idx" ); +return false; + } + +trace_vfio_load_state_device_buffer_incoming (vbasedev->name, packet->idx); + +QEMU_LOCK_GUARD (&multifd->load_bufs_mutex); + +/* config state packet should be the last one in the stream */ +if (packet->flags &VFIO_DEVICE_STATE_CONFIG_STATE ) { + multifd->load_buf_idx_last = packet->idx; + } + +if (!vfio_load_state_buffer_insert (vbasedev, packet, data_size, errp)) { +return false; + } + +qemu_cond_signal (&multifd->load_bufs_buffer_ready_cond); + +return true; +} +VFIOMultifd *vfio_multifd_new (void ) {VFIOMultifd *multifd =g_new (VFIOMultifd,1 ); +vfio_state_buffers_init (&multifd->load_bufs); + +qemu_mutex_init (&multifd->load_bufs_mutex); + + multifd->load_buf_idx =0 ; + multifd->load_buf_idx_last =UINT32_MAX ; +qemu_cond_init (&multifd->load_bufs_buffer_ready_cond); +return multifd; }void vfio_multifd_free (VFIOMultifd *multifd) { +qemu_cond_destroy (&multifd->load_bufs_buffer_ready_cond); +qemu_mutex_destroy (&multifd->load_bufs_mutex); +g_free (multifd); }
hw/vfio/migration-multifd.h +3 -0
@@ -22,4 +22,7 @@ bool vfio_multifd_transfer_enabled(VFIODevice *vbasedev);bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp); +bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size, +Error **errp); +#endif
hw/vfio/migration.c +1 -0
@@ -806,6 +806,7 @@ static const SaveVMHandlers savevm_vfio_handlers = { .load_setup = vfio_load_setup, .load_cleanup = vfio_load_cleanup, .load_state = vfio_load_state, + .load_state_buffer = vfio_load_state_buffer, .switchover_ack_needed = vfio_switchover_ack_needed, };
hw/vfio/trace-events +1 -0
@@ -153,6 +153,7 @@ vfio_load_device_config_state_start(const char *name) " (%s)" vfio_load_device_config_state_end(const char *name) " (%s)" vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size %"PRIu64" ret %d" +vfio_load_state_device_buffer_incoming(const char *name, uint32_t idx) " (%s) idx %"PRIu32 vfio_migration_realize(const char *name) " (%s)" vfio_migration_set_device_state(const char *name, const char *state) " (%s) state %s" vfio_migration_set_state(const char *name, const char *new_state, const char *recover_state) " (%s) new state %s, recover state %s"
Re: [PATCH v5 26/36] vfio/migration: Multifd device state transfer support - received buffers queuing
Quoted 38 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
The multifd received data needs to be reassembled since device state packets sent via different multifd channels can arrive out-of-order.
Therefore, each VFIO device state packet carries a header indicating its position in the stream.
The raw device state data is saved into a VFIOStateBuffer for later in-order loading into the device.The last such VFIO device state packet should have
VFIO_DEVICE_STATE_CONFIG_STATEflag set and carry the device config state.Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 103 ++++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 3 ++ hw/vfio/migration.c | 1 + hw/vfio/trace-events | 1 + 4 files changed, 108 insertions(+)hw/vfio/migration-multifd.c +5
-0@@ -42,6 +42,11 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer;typedef struct VFIOMultifd { +VFIOStateBuffers load_bufs; +QemuCond load_bufs_buffer_ready_cond; +QemuMutex load_bufs_mutex;/* Lock order: this lock -> BQL */ +uint32_t load_buf_idx; +uint32_t load_buf_idx_last; }VFIOMultifd ;static void vfio_state_buffer_clear (gpointer data)
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 94
Quoted 3 lines
hw/vfio/migration-multifd.c +0
-0@@ -87,15 +92,113 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx)return &g_array_index (bufs->array, VFIOStateBuffer, idx); }
this routine expects load_bufs_mutex to be locked ? May be say so.
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 95
Quoted 1 line
hw/vfio/migration-multifd.c +1
-0+static bool vfio_load_state_buffer_insert(VFIODevice *vbasedev,
could you pass VFIOMultifd* instead ?
Quoted 21 lines
hw/vfio/migration-multifd.c +21
-0+VFIODeviceStatePacket *packet, + size_t packet_total_size, + Error **errp) +{ + VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +VFIOStateBuffer *lb; + +vfio_state_buffers_assert_init (&multifd->load_bufs); +if (packet->idx >=vfio_state_buffers_size_get (&multifd->load_bufs)) { +vfio_state_buffers_size_set (&multifd->load_bufs, packet->idx +1 ); + } + + lb =vfio_state_buffers_at (&multifd->load_bufs, packet->idx); +if (lb->is_present) { +error_setg (errp,"state buffer %" PRIu32" already filled" , + packet->idx); +return false; + } + +assert (packet->idx >= multifd->load_buf_idx);
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 126
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ + lb->data =g_memdup2 (&packet->data, packet_total_size -sizeof (*packet)); + lb->len = packet_total_size -sizeof (*packet); + lb->is_present = true; + +return true; +} + +bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size, +Error **errp)
AFAICS, the only users of the .load_state_buffer() handlers is multifd_device_state_recv().
Please rename to vfio_multifd_load_state_buffer().
Quoted 24 lines
hw/vfio/migration-multifd.c +24
-0+{ +VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +VFIODeviceStatePacket *packet = (VFIODeviceStatePacket *)data; + +/* +* Holding BQL here would violate the lock order and can cause +* a deadlock once we attempt to lock load_bufs_mutex below. +*/ +assert (!bql_locked ()); + +if (!vfio_multifd_transfer_enabled (vbasedev)) { +error_setg (errp, +"got device state packet but not doing multifd transfer" ); +return false; + } + +assert (multifd); + +if (data_size <sizeof (*packet)) { +error_setg (errp,"packet too short at %zu (min is %zu)" , + data_size,sizeof (*packet)); +return false;
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 160
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ } + +if (packet->version !=VFIO_DEVICE_STATE_PACKET_VER_CURRENT ) { +error_setg (errp,"packet has unknown version %" PRIu32, + packet->version); +return false; + } + +if (packet->idx ==UINT32_MAX ) { +error_setg (errp,"packet has too high idx" );
or "packet index is invalid" ?
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 166
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+return false; + } + +trace_vfio_load_state_device_buffer_incoming (vbasedev->name, packet->idx); + +QEMU_LOCK_GUARD (&multifd->load_bufs_mutex);
Using WITH_QEMU_LOCK_GUARD() would be cleaner I think.
Quoted 67 lines
hw/vfio/migration-multifd.c +26
-0+ +/* config state packet should be the last one in the stream */ +if (packet->flags &VFIO_DEVICE_STATE_CONFIG_STATE ) { + multifd->load_buf_idx_last = packet->idx; + } + +if (!vfio_load_state_buffer_insert (vbasedev, packet, data_size, errp)) { +return false; + } + +qemu_cond_signal (&multifd->load_bufs_buffer_ready_cond); + +return true; +} +VFIOMultifd *vfio_multifd_new (void ) {VFIOMultifd *multifd =g_new (VFIOMultifd,1 ); +vfio_state_buffers_init (&multifd->load_bufs); + +qemu_mutex_init (&multifd->load_bufs_mutex); + + multifd->load_buf_idx =0 ; + multifd->load_buf_idx_last =UINT32_MAX ; +qemu_cond_init (&multifd->load_bufs_buffer_ready_cond); +return multifd; }void vfio_multifd_free (VFIOMultifd *multifd) { +qemu_cond_destroy (&multifd->load_bufs_buffer_ready_cond); +qemu_mutex_destroy (&multifd->load_bufs_mutex); +g_free (multifd); }hw/vfio/migration-multifd.h +3
-0@@ -22,4 +22,7 @@ bool vfio_multifd_transfer_enabled(VFIODevice *vbasedev);bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp); +bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size, +Error **errp); +#endif hw/vfio/migration.c +1
-0@@ -806,6 +806,7 @@ static const SaveVMHandlers savevm_vfio_handlers = { .load_setup = vfio_load_setup, .load_cleanup = vfio_load_cleanup, .load_state = vfio_load_state, + .load_state_buffer = vfio_load_state_buffer, .switchover_ack_needed = vfio_switchover_ack_needed,> };hw/vfio/trace-events +0
-0
hw/vfio/trace-events vfio_load_device_config_state_start(const char *name) " (%s)" · line 159
Quoted 7 lines
hw/vfio/trace-events +1
-0@@ -153,6 +153,7 @@ vfio_load_device_config_state_start(const char *name) " (%s)" vfio_load_device_config_state_end(const char *name) " (%s)" vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size %"PRIu64" ret %d" +vfio_load_state_device_buffer_incoming(const char *name, uint32_t idx) " (%s) idx %"PRIu32 vfio_migration_realize(const char *name) " (%s)" vfio_migration_set_device_state(const char *name, const char *state) " (%s) state %s" vfio_migration_set_state(const char *name, const char *new_state, const char *recover_state) " (%s) new state %s, recover state %s"
Thanks,
C.
Re: [PATCH v5 26/36] vfio/migration: Multifd device state transfer support - received buffers queuing
Quoted 33 lines · Cédric Le Goater
Quoted 32 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
The multifd received data needs to be reassembled since device state packets sent via different multifd channels can arrive out-of-order.
Therefore, each VFIO device state packet carries a header indicating its position in the stream.
The raw device state data is saved into a VFIOStateBuffer for later in-order loading into the device.The last such VFIO device state packet should have
VFIO_DEVICE_STATE_CONFIG_STATEflag set and carry the device config state.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 103 ++++++++++++++++++++++++++++++++++++hw/vfio/migration-multifd.h| 3 ++
hw/vfio/migration.c | 1 +
hw/vfio/trace-events | 1 +4 files changed, 108 insertions(+)hw/vfio/migration-multifd.c +4
-0@@ -42,6 +42,11 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer; typedef struct VFIOMultifd { + VFIOStateBuffers load_bufs; + QemuCond load_bufs_buffer_ready_cond; + QemuMutex load_bufs_mutex; /* Lock order: this lock -> BQL */ + uint32_t load_buf_idx;
Quoted 8 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 93
Quoted 6 lines
hw/vfio/migration-multifd.c +1
-0+ uint32_t load_buf_idx_last; } VFIOMultifd; static void vfio_state_buffer_clear(gpointer data)@@ -87,15 +92,113 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) return &g_array_index(bufs->array, VFIOStateBuffer, idx); }this routine expects
load_bufs_mutexto be locked ? May be say so.
I guess the comment above pertains to the vfio_load_state_buffer_insert() below.
Do you mean it should have a comment that it expects to be called under load_bufs_mutex?
Quoted 3 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 95
Quoted 1 line
hw/vfio/migration-multifd.c +1
-0+static bool vfio_load_state_buffer_insert(VFIODevice *vbasedev,could you pass VFIOMultifd* instead ?
No, it needs vbasedev->migration_max_queued_buffers too (introduced in later patch).
Also, most of VFIO routines (besides very small helpers/wrappers) take VFIODevice *.
Quoted 36 lines
Quoted 21 lines
hw/vfio/migration-multifd.c +21
-0+ VFIODeviceStatePacket *packet, + size_t packet_total_size, + Error **errp) +{ + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + VFIOStateBuffer *lb; + + vfio_state_buffers_assert_init(&multifd->load_bufs); + if (packet->idx >= vfio_state_buffers_size_get(&multifd->load_bufs)) { + vfio_state_buffers_size_set(&multifd->load_bufs, packet->idx + 1); + } + + lb = vfio_state_buffers_at(&multifd->load_bufs, packet->idx); + if (lb->is_present) { + error_setg(errp, "state buffer %" PRIu32 " already filled", + packet->idx); + return false; + } + + assert(packet->idx >= multifd->load_buf_idx);hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 126
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ + lb->data = g_memdup2(&packet->data, packet_total_size - sizeof(*packet)); + lb->len = packet_total_size - sizeof(*packet); + lb->is_present = true; + + return true; +} + +bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size, + Error **errp)AFAICS, the only users of the .load_state_buffer() handlers is
multifd_device_state_recv().
Quoted 1 line
Please rename to
vfio_multifd_load_state_buffer().
Renamed it accordingly.
Quoted 28 lines
Quoted 28 lines
hw/vfio/migration-multifd.c +28
-0+{ + VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + VFIODeviceStatePacket *packet = (VFIODeviceStatePacket *)data; + + /* + * Holding BQL here would violate the lock order and can cause + * a deadlock once we attempt to lock load_bufs_mutex below. + */ + assert(!bql_locked()); + + if (!vfio_multifd_transfer_enabled(vbasedev)) { + error_setg(errp, + "got device state packet but not doing multifd transfer"); + return false; + } + + assert(multifd); + + if (data_size < sizeof(*packet)) { + error_setg(errp, "packet too short at %zu (min is %zu)", + data_size, sizeof(*packet)); + return false; + } + + if (packet->version != VFIO_DEVICE_STATE_PACKET_VER_CURRENT) { + error_setg(errp, "packet has unknown version %" PRIu32,
Quoted 8 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 160
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ packet->version); + return false; + } + + if (packet->idx == UINT32_MAX) { + error_setg(errp, "packet has too high idx");or "packet index is invalid" ?
Changed the error message.
Quoted 8 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 166
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ return false; + } + + trace_vfio_load_state_device_buffer_incoming(vbasedev->name, packet->idx); + + QEMU_LOCK_GUARD(&multifd->load_bufs_mutex);Using
WITH_QEMU_LOCK_GUARD()would be cleaner I think.
Changed into a WITH_QEMU_LOCK_GUARD() block.
Quoted 5 lines
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 26/36] vfio/migration: Multifd device state transfer support - received buffers queuing
Quoted 43 lines · Maciej S. Szmigiero
Quoted 33 lines · Cédric Le Goater
Quoted 32 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
The multifd received data needs to be reassembled since device state packets sent via different multifd channels can arrive out-of-order.
Therefore, each VFIO device state packet carries a header indicating its position in the stream.
The raw device state data is saved into a VFIOStateBuffer for later in-order loading into the device.The last such VFIO device state packet should have
VFIO_DEVICE_STATE_CONFIG_STATEflag set and carry the device config state.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 103 ++++++++++++++++++++++++++++++++++++hw/vfio/migration-multifd.h| 3 ++
hw/vfio/migration.c | 1 +
hw/vfio/trace-events | 1 +4 files changed, 108 insertions(+)hw/vfio/migration-multifd.c +4
-0@@ -42,6 +42,11 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer; typedef struct VFIOMultifd { + VFIOStateBuffers load_bufs; + QemuCond load_bufs_buffer_ready_cond; + QemuMutex load_bufs_mutex; /* Lock order: this lock -> BQL */ + uint32_t load_buf_idx;Quoted 8 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 93
Quoted 6 lines
hw/vfio/migration-multifd.c +1
-0+ uint32_t load_buf_idx_last; } VFIOMultifd; static void vfio_state_buffer_clear(gpointer data)@@ -87,15 +92,113 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) return &g_array_index(bufs->array, VFIOStateBuffer, idx); }this routine expects
load_bufs_mutexto be locked ? May be say so.
Quoted 5 lines
I guess the comment above pertains to the
vfio_load_state_buffer_insert()below.Do you mean it should have a comment that it expects to be called under
load_bufs_mutex?
Just a one liner like :
/* called with load_bufs_mutex locked */
?
It's good to have for the future generations.
Quoted 9 lines
Quoted 4 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 95
Quoted 2 lines
hw/vfio/migration-multifd.c +1
-0+static bool vfio_load_state_buffer_insert(VFIODevice *vbasedev,could you pass VFIOMultifd* instead ?
No, it needs
vbasedev->migration_max_queued_bufferstoo (introduced in later patch).Quoted 1 line
Also, most of VFIO routines (besides very small helpers/wrappers)
take VFIODevice *.
OK. It's minor but I prefer when parameters are limited to the minimum. Having 'VFIODevice *' opens doors to a lot of state.
Thanks,
C.
Quoted 100 lines
Quoted 37 lines
Quoted 22 lines
hw/vfio/migration-multifd.c +21
-0+ VFIODeviceStatePacket *packet, + size_t packet_total_size, + Error **errp) +{ + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + VFIOStateBuffer *lb; + + vfio_state_buffers_assert_init(&multifd->load_bufs); + if (packet->idx >= vfio_state_buffers_size_get(&multifd->load_bufs)) { + vfio_state_buffers_size_set(&multifd->load_bufs, packet->idx + 1); + } + + lb = vfio_state_buffers_at(&multifd->load_bufs, packet->idx); + if (lb->is_present) { + error_setg(errp, "state buffer %" PRIu32 " already filled", + packet->idx); + return false; + } + + assert(packet->idx >= multifd->load_buf_idx);hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 126
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ + lb->data = g_memdup2(&packet->data, packet_total_size - sizeof(*packet)); + lb->len = packet_total_size - sizeof(*packet); + lb->is_present = true; + + return true; +} + +bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size, + Error **errp)AFAICS, the only users of the .load_state_buffer() handlers is
multifd_device_state_recv().Quoted 1 line
Please rename to
vfio_multifd_load_state_buffer().Renamed it accordingly.
Quoted 28 lines
Quoted 28 lines
hw/vfio/migration-multifd.c +28
-0+{ + VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + VFIODeviceStatePacket *packet = (VFIODeviceStatePacket *)data; + + /* + * Holding BQL here would violate the lock order and can cause + * a deadlock once we attempt to lock load_bufs_mutex below. + */ + assert(!bql_locked()); + + if (!vfio_multifd_transfer_enabled(vbasedev)) { + error_setg(errp, + "got device state packet but not doing multifd transfer"); + return false; + } + + assert(multifd); + + if (data_size < sizeof(*packet)) { + error_setg(errp, "packet too short at %zu (min is %zu)", + data_size, sizeof(*packet)); + return false; + } + + if (packet->version != VFIO_DEVICE_STATE_PACKET_VER_CURRENT) { + error_setg(errp, "packet has unknown version %" PRIu32,Quoted 8 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 160
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ packet->version); + return false; + } + + if (packet->idx == UINT32_MAX) { + error_setg(errp, "packet has too high idx");or "packet index is invalid" ?
Changed the error message.
Quoted 8 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 166
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ return false; + } + + trace_vfio_load_state_device_buffer_incoming(vbasedev->name, packet->idx); + + QEMU_LOCK_GUARD(&multifd->load_bufs_mutex);Using
WITH_QEMU_LOCK_GUARD()would be cleaner I think.Changed into a
WITH_QEMU_LOCK_GUARD()block.Quoted 5 lines
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 26/36] vfio/migration: Multifd device state transfer support - received buffers queuing
Quoted 50 lines · Cédric Le Goater
Quoted 43 lines · Maciej S. Szmigiero
Quoted 33 lines · Cédric Le Goater
Quoted 32 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
The multifd received data needs to be reassembled since device state packets sent via different multifd channels can arrive out-of-order.
Therefore, each VFIO device state packet carries a header indicating its position in the stream.
The raw device state data is saved into a VFIOStateBuffer for later in-order loading into the device.The last such VFIO device state packet should have
VFIO_DEVICE_STATE_CONFIG_STATEflag set and carry the device config state.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 103 ++++++++++++++++++++++++++++++++++++hw/vfio/migration-multifd.h| 3 ++
hw/vfio/migration.c | 1 +
hw/vfio/trace-events | 1 +4 files changed, 108 insertions(+)hw/vfio/migration-multifd.c +4
-0@@ -42,6 +42,11 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer; typedef struct VFIOMultifd { + VFIOStateBuffers load_bufs; + QemuCond load_bufs_buffer_ready_cond; + QemuMutex load_bufs_mutex; /* Lock order: this lock -> BQL */ + uint32_t load_buf_idx;Quoted 8 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 93
Quoted 6 lines
hw/vfio/migration-multifd.c +1
-0+ uint32_t load_buf_idx_last; } VFIOMultifd; static void vfio_state_buffer_clear(gpointer data)@@ -87,15 +92,113 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) return &g_array_index(bufs->array, VFIOStateBuffer, idx); }this routine expects
load_bufs_mutexto be locked ? May be say so.Quoted 5 lines
I guess the comment above pertains to the
vfio_load_state_buffer_insert()below.Do you mean it should have a comment that it expects to be called under
load_bufs_mutex?
Quoted 7 lines
Just a one liner like :
/* called with
load_bufs_mutexlocked */?
It's good to have for the future generations.
Okay, done.
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 95
Quoted 2 lines
hw/vfio/migration-multifd.c +1
-0+static bool vfio_load_state_buffer_insert(VFIODevice *vbasedev,could you pass VFIOMultifd* instead ?
No, it needs
vbasedev->migration_max_queued_bufferstoo (introduced in later patch).Quoted 1 line
Also, most of VFIO routines (besides very small helpers/wrappers)
take VFIODevice *.
Quoted 8 lines
OK. It's minor but I prefer when parameters are limited to the minimum. Having 'VFIODevice *' opens doors to a lot of state.
Thanks,
C.
Thanks.
Maciej
Re: [PATCH v5 26/36] vfio/migration: Multifd device state transfer support - received buffers queuing
In reply to Maciej S. SzmigieroQuoted 52 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
The multifd received data needs to be reassembled since device state packets sent via different multifd channels can arrive out-of-order.
Therefore, each VFIO device state packet carries a header indicating its position in the stream.
The raw device state data is saved into a VFIOStateBuffer for later in-order loading into the device.The last such VFIO device state packet should have
VFIO_DEVICE_STATE_CONFIG_STATEflag set and carry the device config state.Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 103 ++++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 3 ++ hw/vfio/migration.c | 1 + hw/vfio/trace-events | 1 + 4 files changed, 108 insertions(+)hw/vfio/migration-multifd.c +13
-0@@ -42,6 +42,11 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer;typedef struct VFIOMultifd { +VFIOStateBuffers load_bufs; +QemuCond load_bufs_buffer_ready_cond; +QemuMutex load_bufs_mutex;/* Lock order: this lock -> BQL */ +uint32_t load_buf_idx; +uint32_t load_buf_idx_last; }VFIOMultifd ;static void vfio_state_buffer_clear (gpointer data)@@ -87,15 +92,113 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx)return &g_array_index (bufs->array, VFIOStateBuffer, idx); } +static bool vfio_load_state_buffer_insert (VFIODevice *vbasedev, +VFIODeviceStatePacket *packet, +size_t packet_total_size, +Error **errp) +{ +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +VFIOStateBuffer *lb;
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 112
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ +vfio_state_buffers_assert_init (&multifd->load_bufs); +if (packet->idx >=vfio_state_buffers_size_get (&multifd->load_bufs)) { +vfio_state_buffers_size_set (&multifd->load_bufs, packet->idx +1 ); + } + + lb =vfio_state_buffers_at (&multifd->load_bufs, packet->idx); +if (lb->is_present) { +error_setg (errp,"state buffer %" PRIu32" already filled" , + packet->idx);
Let's add vbasedev->name to the error message so we know which device caused the error.
Quoted 15 lines
hw/vfio/migration-multifd.c +15
-0+return false; + } + +assert (packet->idx >= multifd->load_buf_idx); + + lb->data =g_memdup2 (&packet->data, packet_total_size -sizeof (*packet)); + lb->len = packet_total_size -sizeof (*packet); + lb->is_present = true; + +return true; +} + +bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size, +Error **errp) +{
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 137
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +VFIODeviceStatePacket *packet = (VFIODeviceStatePacket *)data; + +/* +* Holding BQL here would violate the lock order and can cause +* a deadlock once we attempt to lock load_bufs_mutex below. +*/ +assert (!bql_locked ());
To be clearer, I'd move the assert down to be just above "QEMU_LOCK_GUARD(&multifd->load_bufs_mutex);".
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 141
Quoted 4 lines
hw/vfio/migration-multifd.c +4
-0+ +if (!vfio_multifd_transfer_enabled (vbasedev)) { +error_setg (errp, +"got device state packet but not doing multifd transfer" );
Let's add vbasedev->name to the error message so we know which device caused the error.
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 149
Quoted 8 lines
hw/vfio/migration-multifd.c +8
-0+return false; + } + +assert (multifd); + +if (data_size <sizeof (*packet)) { +error_setg (errp,"packet too short at %zu (min is %zu)" , + data_size,sizeof (*packet));
Ditto.
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 155
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+return false; + } + +if (packet->version !=VFIO_DEVICE_STATE_PACKET_VER_CURRENT ) { +error_setg (errp,"packet has unknown version %" PRIu32, + packet->version);
Ditto.
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 160
Quoted 5 lines
hw/vfio/migration-multifd.c +5
-0+return false; + } + +if (packet->idx ==UINT32_MAX ) { +error_setg (errp,"packet has too high idx" );
Ditto.
Quoted 18 lines
hw/vfio/migration-multifd.c +18
-0+return false; + } + +trace_vfio_load_state_device_buffer_incoming (vbasedev->name, packet->idx); + +QEMU_LOCK_GUARD (&multifd->load_bufs_mutex); + +/* config state packet should be the last one in the stream */ +if (packet->flags &VFIO_DEVICE_STATE_CONFIG_STATE ) { + multifd->load_buf_idx_last = packet->idx; + } + +if (!vfio_load_state_buffer_insert (vbasedev, packet, data_size, errp)) { +return false; + } + +qemu_cond_signal (&multifd->load_bufs_buffer_ready_cond); +
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 188
Quoted 10 lines
hw/vfio/migration-multifd.c +6
-0+return true; +} +VFIOMultifd *vfio_multifd_new (void ) {VFIOMultifd *multifd =g_new (VFIOMultifd,1 ); +vfio_state_buffers_init (&multifd->load_bufs); + +qemu_mutex_init (&multifd->load_bufs_mutex);
Nit: move qemu_mutex_init() just above qemu_cond_init()?
Thanks.
Quoted 52 lines
hw/vfio/migration-multifd.c +8
-0+ + multifd->load_buf_idx =0 ; + multifd->load_buf_idx_last =UINT32_MAX ; +qemu_cond_init (&multifd->load_bufs_buffer_ready_cond); +return multifd; }void vfio_multifd_free (VFIOMultifd *multifd) { +qemu_cond_destroy (&multifd->load_bufs_buffer_ready_cond); +qemu_mutex_destroy (&multifd->load_bufs_mutex); +g_free (multifd); }hw/vfio/migration-multifd.h +3
-0@@ -22,4 +22,7 @@ bool vfio_multifd_transfer_enabled(VFIODevice *vbasedev);bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp); +bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size, +Error **errp); +#endif hw/vfio/migration.c +1
-0@@ -806,6 +806,7 @@ static const SaveVMHandlers savevm_vfio_handlers = { .load_setup = vfio_load_setup, .load_cleanup = vfio_load_cleanup, .load_state = vfio_load_state, + .load_state_buffer = vfio_load_state_buffer, .switchover_ack_needed = vfio_switchover_ack_needed, };hw/vfio/trace-events +1
-0@@ -153,6 +153,7 @@ vfio_load_device_config_state_start(const char *name) " (%s)" vfio_load_device_config_state_end(const char *name) " (%s)" vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size %"PRIu64" ret %d" +vfio_load_state_device_buffer_incoming(const char *name, uint32_t idx) " (%s) idx %"PRIu32 vfio_migration_realize(const char *name) " (%s)" vfio_migration_set_device_state(const char *name, const char *state) " (%s) state %s" vfio_migration_set_state(const char *name, const char *new_state, const char *recover_state) " (%s) new state %s, recover state %s"
Re: [PATCH v5 26/36] vfio/migration: Multifd device state transfer support - received buffers queuing
Quoted 58 lines · Avihai Horon
Quoted 56 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
The multifd received data needs to be reassembled since device state packets sent via different multifd channels can arrive out-of-order.
Therefore, each VFIO device state packet carries a header indicating its position in the stream.
The raw device state data is saved into a VFIOStateBuffer for later in-order loading into the device.The last such VFIO device state packet should have
VFIO_DEVICE_STATE_CONFIG_STATEflag set and carry the device config state.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 103 ++++++++++++++++++++++++++++++++++++hw/vfio/migration-multifd.h| 3 ++
hw/vfio/migration.c | 1 +
hw/vfio/trace-events | 1 +4 files changed, 108 insertions(+)hw/vfio/migration-multifd.c +17
-0@@ -42,6 +42,11 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer; typedef struct VFIOMultifd { + VFIOStateBuffers load_bufs; + QemuCond load_bufs_buffer_ready_cond; + QemuMutex load_bufs_mutex; /* Lock order: this lock -> BQL */ + uint32_t load_buf_idx; + uint32_t load_buf_idx_last; } VFIOMultifd; static void vfio_state_buffer_clear(gpointer data)@@ -87,15 +92,113 @@ static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) return &g_array_index(bufs->array, VFIOStateBuffer, idx); } +static bool vfio_load_state_buffer_insert (VFIODevice *vbasedev, + VFIODeviceStatePacket *packet, + size_t packet_total_size, + Error **errp) +{ + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + VFIOStateBuffer *lb; + + vfio_state_buffers_assert_init(&multifd->load_bufs); + if (packet->idx >= vfio_state_buffers_size_get(&multifd->load_bufs)) { + vfio_state_buffers_size_set(&multifd->load_bufs, packet->idx + 1);
Quoted 8 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 112
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ } + + lb = vfio_state_buffers_at(&multifd->load_bufs, packet->idx); + if (lb->is_present) { + error_setg(errp, "state buffer %" PRIu32 " already filled", + packet->idx);Let's add
vbasedev->nameto the error message so we know which device caused the error.
Done.
Quoted 19 lines
Quoted 19 lines
hw/vfio/migration-multifd.c +19
-0+ return false; + } + + assert(packet->idx >= multifd->load_buf_idx); + + lb->data = g_memdup2(&packet->data, packet_total_size - sizeof(*packet)); + lb->len = packet_total_size - sizeof(*packet); + lb->is_present = true; + + return true; +} + +bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size, + Error **errp) +{ + VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + VFIODeviceStatePacket *packet = (VFIODeviceStatePacket *)data;
Quoted 8 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 137
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ + /* + * Holding BQL here would violate the lock order and can cause + * a deadlock once we attempt to lock load_bufs_mutex below. + */ + assert(!bql_locked());To be clearer, I'd move the assert down to be just above "
QEMU_LOCK_GUARD(&multifd->load_bufs_mutex);".
Moved there.
Quoted 6 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 141
Quoted 4 lines
hw/vfio/migration-multifd.c +4
-0+ + if (!vfio_multifd_transfer_enabled(vbasedev)) { + error_setg(errp, + "got device state packet but not doing multifd transfer");Let's add
vbasedev->nameto the error message so we know which device caused the error.
Done.
Quoted 10 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 149
Quoted 8 lines
hw/vfio/migration-multifd.c +8
-0+ return false; + } + + assert(multifd); + + if (data_size < sizeof(*packet)) { + error_setg(errp, "packet too short at %zu (min is %zu)", + data_size, sizeof(*packet));Ditto.
Done.
Quoted 8 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 155
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ return false; + } + + if (packet->version != VFIO_DEVICE_STATE_PACKET_VER_CURRENT) { + error_setg(errp, "packet has unknown version %" PRIu32, + packet->version);Ditto.
Done.
Quoted 7 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 160
Quoted 5 lines
hw/vfio/migration-multifd.c +5
-0+ return false; + } + + if (packet->idx == UINT32_MAX) { + error_setg(errp, "packet has too high idx");Ditto.
Done.
Quoted 25 lines
Quoted 25 lines
hw/vfio/migration-multifd.c +21
-0+ return false; + } + + trace_vfio_load_state_device_buffer_incoming(vbasedev->name, packet->idx); + + QEMU_LOCK_GUARD(&multifd->load_bufs_mutex); + + /* config state packet should be the last one in the stream */ + if (packet->flags & VFIO_DEVICE_STATE_CONFIG_STATE) { + multifd->load_buf_idx_last = packet->idx; + } + + if (!vfio_load_state_buffer_insert(vbasedev, packet, data_size, errp)) { + return false; + } + + qemu_cond_signal(&multifd->load_bufs_buffer_ready_cond); + + return true; +} + VFIOMultifd *vfio_multifd_new(void) { VFIOMultifd *multifd = g_new(VFIOMultifd, 1);
Quoted 5 lines
hw/vfio/migration-multifd.c static VFIOStateBuffer *vfio_state_buffers_at(VFIOStateBuffers *bufs, guint idx) · line 188
Quoted 3 lines
hw/vfio/migration-multifd.c +3
-0+ vfio_state_buffers_init(&multifd->load_bufs); + + qemu_mutex_init(&multifd->load_bufs_mutex);Nit: move
qemu_mutex_init()just aboveqemu_cond_init()?
It's in a separate "block" because it is common for all 3 conditions in the ultimate form of code (and most of the later variables too), rather just this single condition.
Quoted 1 line
Thanks.
Thanks,
Maciej
Quoted 8 lines
Quoted 8 lines
hw/vfio/migration-multifd.c +5
-0+ + multifd->load_buf_idx = 0; + multifd->load_buf_idx_last = UINT32_MAX; + qemu_cond_init(&multifd->load_bufs_buffer_ready_cond); + return multifd; }
PATCH v5 27/36 vfio/migration: Multifd device state transfer support - load thread
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Since it's important to finish loading device state transferred via the main migration channel (via save_live_iterate SaveVMHandler) before starting loading the data asynchronously transferred via multifd the thread doing the actual loading of the multifd transferred data is only started from switchover_start SaveVMHandler.
switchover_start handler is called when MIG_CMD_SWITCHOVER_START sub-command of QEMU_VM_COMMAND is received via the main migration channel.
This sub-command is only sent after all save_live_iterate data have already been posted so it is safe to commence loading of the multifd-transferred device state upon receiving it - loading of save_live_iterate data happens synchronously in the main migration thread (much like the processing of MIG_CMD_SWITCHOVER_START) so by the time MIG_CMD_SWITCHOVER_START is processed all the proceeding data must have already been loaded.
--- hw/vfio/migration-multifd.c | 225 ++++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 2 + hw/vfio/migration.c | 12 ++ hw/vfio/trace-events | 5 + 4 files changed, 244 insertions(+)
hw/vfio/migration-multifd.c +225 -0
@@ -42,8 +42,13 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer; typedef struct VFIOMultifd { + QemuThread load_bufs_thread; + bool load_bufs_thread_running; + bool load_bufs_thread_want_exit; + VFIOStateBuffers load_bufs; QemuCond load_bufs_buffer_ready_cond; + QemuCond load_bufs_thread_finished_cond; QemuMutex load_bufs_mutex; /* Lock order: this lock -> BQL */ uint32_t load_buf_idx; uint32_t load_buf_idx_last;
@@ -179,6 +184,175 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size,return true; } +static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev) +{ +return -EINVAL ; +} + +static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd) +{ +VFIOStateBuffer *lb; +guint bufs_len; + + bufs_len =vfio_state_buffers_size_get (&multifd->load_bufs); +if (multifd->load_buf_idx >= bufs_len) { +assert (multifd->load_buf_idx == bufs_len); +return NULL ; + } + + lb =vfio_state_buffers_at (&multifd->load_bufs, + multifd->load_buf_idx); +if (!lb->is_present) { +return NULL ; + } + +return lb; +} + +static bool vfio_load_state_buffer_write (VFIODevice *vbasedev, +VFIOStateBuffer *lb, +Error **errp) +{ +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; + g_autofree char *buf = NULL; +char *buf_cur; +size_t buf_len; + +if (!lb->len) { +return true; + } + +trace_vfio_load_state_device_buffer_load_start (vbasedev->name, + multifd->load_buf_idx); + +/* lb might become re-allocated when we drop the lock */ + buf =g_steal_pointer (&lb->data); + buf_cur = buf; + buf_len = lb->len; +while (buf_len >0 ) { +ssize_t wr_ret; +int errno_save; + +/* +* Loading data to the device takes a while, +* drop the lock during this process. +*/ +qemu_mutex_unlock (&multifd->load_bufs_mutex); + wr_ret =write (migration->data_fd, buf_cur, buf_len); + errno_save = errno; +qemu_mutex_lock (&multifd->load_bufs_mutex); + +if (wr_ret <0 ) { +error_setg (errp, +"writing state buffer %" PRIu32" failed: %d" , + multifd->load_buf_idx, errno_save); +return false; + } + +assert (wr_ret <= buf_len); + buf_len -= wr_ret; + buf_cur += wr_ret; + } + +trace_vfio_load_state_device_buffer_load_end (vbasedev->name, + multifd->load_buf_idx); + +return true; +} + +static bool vfio_load_bufs_thread_want_exit (VFIOMultifd *multifd, +bool *should_quit) +{ +return multifd->load_bufs_thread_want_exit ||qatomic_read (should_quit); +} + +/* +* This thread is spawned by vfio_multifd_switchover_start() which gets +* called upon encountering the switchover point marker in main migration +* stream. +* +* It exits after either: +* * completing loading the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by it setting should_quit +* or by vfio_load_cleanup_load_bufs_thread() setting +* multifd->load_bufs_thread_want_exit. +*/ +static bool vfio_load_bufs_thread (void *opaque,bool *should_quit,Error **errp) +{ +VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +bool ret = true; +int config_ret; + +assert (multifd); +QEMU_LOCK_GUARD (&multifd->load_bufs_mutex); + +assert (multifd->load_bufs_thread_running); + +while (true) { +VFIOStateBuffer *lb; + +/* +* Always check cancellation first after the buffer_ready wait below in +* case that cond was signalled by vfio_load_cleanup_load_bufs_thread(). +*/ +if (vfio_load_bufs_thread_want_exit (multifd, should_quit)) { +error_setg (errp,"operation cancelled" ); + ret = false; + goto ret_signal; + } + +assert (multifd->load_buf_idx <= multifd->load_buf_idx_last); + + lb =vfio_load_state_buffer_get (multifd); +if (!lb) { +trace_vfio_load_state_device_buffer_starved (vbasedev->name, + multifd->load_buf_idx); +qemu_cond_wait (&multifd->load_bufs_buffer_ready_cond, + &multifd->load_bufs_mutex); +continue ; + } + +if (multifd->load_buf_idx == multifd->load_buf_idx_last) { +break ; + } + +if (multifd->load_buf_idx ==0 ) { +trace_vfio_load_state_device_buffer_start (vbasedev->name); + } + +if (!vfio_load_state_buffer_write (vbasedev, lb, errp)) { + ret = false; + goto ret_signal; + } + +if (multifd->load_buf_idx == multifd->load_buf_idx_last -1 ) { +trace_vfio_load_state_device_buffer_end (vbasedev->name); + } + + multifd->load_buf_idx++; + } + + config_ret =vfio_load_bufs_thread_load_config (vbasedev); +if (config_ret) { +error_setg (errp,"load config state failed: %d" , config_ret); + ret = false; + } + +ret_signal: +/* +* Notify possibly waiting vfio_load_cleanup_load_bufs_thread() that +* this thread is exiting. +*/ + multifd->load_bufs_thread_running = false; +qemu_cond_signal (&multifd->load_bufs_thread_finished_cond); + +return ret; +} +VFIOMultifd *vfio_multifd_new (void ) {VFIOMultifd *multifd =g_new (VFIOMultifd,1 );
@@ -191,11 +365,42 @@ VFIOMultifd *vfio_multifd_new(void) multifd->load_buf_idx_last =UINT32_MAX ;qemu_cond_init (&multifd->load_bufs_buffer_ready_cond); + multifd->load_bufs_thread_running = false; + multifd->load_bufs_thread_want_exit = false; +qemu_cond_init (&multifd->load_bufs_thread_finished_cond); +return multifd; } +/* +* Terminates vfio_load_bufs_thread by setting +* multifd->load_bufs_thread_want_exit and signalling all the conditions +* the thread could be blocked on. +* +* Waits for the thread to signal that it had finished. +*/ +static void vfio_load_cleanup_load_bufs_thread (VFIOMultifd *multifd) +{ +/* The lock order is load_bufs_mutex -> BQL so unlock BQL here first */ +bql_unlock (); + WITH_QEMU_LOCK_GUARD(&multifd->load_bufs_mutex) { +while (multifd->load_bufs_thread_running) { + multifd->load_bufs_thread_want_exit = true; + +qemu_cond_signal (&multifd->load_bufs_buffer_ready_cond); +qemu_cond_wait (&multifd->load_bufs_thread_finished_cond, + &multifd->load_bufs_mutex); + } + } +bql_lock (); +} +void vfio_multifd_free (VFIOMultifd *multifd) { +vfio_load_cleanup_load_bufs_thread (multifd); + +qemu_cond_destroy (&multifd->load_bufs_thread_finished_cond); +vfio_state_buffers_destroy (&multifd->load_bufs);qemu_cond_destroy (&multifd->load_bufs_buffer_ready_cond);qemu_mutex_destroy (&multifd->load_bufs_mutex);
@@ -225,3 +430,23 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp)return true; } + +int vfio_multifd_switchover_start (VFIODevice *vbasedev) +{ +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; + +assert (multifd); + +/* The lock order is load_bufs_mutex -> BQL so unlock BQL here first */ +bql_unlock (); + WITH_QEMU_LOCK_GUARD(&multifd->load_bufs_mutex) { +assert (!multifd->load_bufs_thread_running); + multifd->load_bufs_thread_running = true; + } +bql_lock (); + +qemu_loadvm_start_load_thread (vfio_load_bufs_thread, vbasedev); + +return 0 ; +}
hw/vfio/migration-multifd.h +2 -0
@@ -25,4 +25,6 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp);bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size,Error **errp); +int vfio_multifd_switchover_start (VFIODevice *vbasedev); +#endif
hw/vfio/migration.c +12 -0
@@ -793,6 +793,17 @@ static bool vfio_switchover_ack_needed(void *opaque)return vfio_precopy_supported (vbasedev); } +static int vfio_switchover_start (void *opaque) +{ +VFIODevice *vbasedev = opaque; + +if (vfio_multifd_transfer_enabled (vbasedev)) { +return vfio_multifd_switchover_start (vbasedev); + } + +return 0 ; +} +static const SaveVMHandlers savevm_vfio_handlers = { .save_prepare = vfio_save_prepare, .save_setup = vfio_save_setup,
@@ -808,6 +819,7 @@ static const SaveVMHandlers savevm_vfio_handlers = { .load_state = vfio_load_state, .load_state_buffer = vfio_load_state_buffer, .switchover_ack_needed = vfio_switchover_ack_needed, + .switchover_start = vfio_switchover_start, };/* ---------------------------------------------------------------------- */
hw/vfio/trace-events +5 -0
@@ -154,6 +154,11 @@ vfio_load_device_config_state_end(const char *name) " (%s)" vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size %"PRIu64" ret %d" vfio_load_state_device_buffer_incoming(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_start(const char *name) " (%s)" +vfio_load_state_device_buffer_starved(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_load_start(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_load_end(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_end(const char *name) " (%s)" vfio_migration_realize(const char *name) " (%s)" vfio_migration_set_device_state(const char *name, const char *state) " (%s) state %s" vfio_migration_set_state(const char *name, const char *new_state, const char *recover_state) " (%s) new state %s, recover state %s"
Re: [PATCH v5 27/36] vfio/migration: Multifd device state transfer support - load thread
Quoted 45 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Since it's important to finish loading device state transferred via the main migration channel (via
save_live_iterateSaveVMHandler) before starting loading the data asynchronously transferred via multifd the thread doing the actual loading of the multifd transferred data is only started fromswitchover_startSaveVMHandler.
switchover_starthandler is called whenMIG_CMD_SWITCHOVER_STARTsub-command ofQEMU_VM_COMMANDis received via the main migration channel.This sub-command is only sent after all
save_live_iteratedata have already been posted so it is safe to commence loading of the multifd-transferred device state upon receiving it - loading ofsave_live_iteratedata happens synchronously in the main migration thread (much like the processing ofMIG_CMD_SWITCHOVER_START) so by the timeMIG_CMD_SWITCHOVER_STARTis processed all the proceeding data must have already been loaded.Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 225 ++++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 2 + hw/vfio/migration.c | 12 ++ hw/vfio/trace-events | 5 + 4 files changed, 244 insertions(+)hw/vfio/migration-multifd.c +5
-0@@ -42,8 +42,13 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer; typedef struct VFIOMultifd { + QemuThread load_bufs_thread; + bool load_bufs_thread_running; + bool load_bufs_thread_want_exit; + VFIOStateBuffers load_bufs; QemuCond load_bufs_buffer_ready_cond; + QemuCond load_bufs_thread_finished_cond; QemuMutex load_bufs_mutex; /* Lock order: this lock -> BQL */ uint32_t load_buf_idx; uint32_t load_buf_idx_last;
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 190
Quoted 7 lines
hw/vfio/migration-multifd.c +4
-0@@ -179,6 +184,175 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size,return true; } +static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev) +{ +return -EINVAL ; +}
please move to next patch.
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 195
Quoted 4 lines
hw/vfio/migration-multifd.c +4
-0+static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd) +{ +VFIOStateBuffer *lb; +guint bufs_len;
guint: I guess it's ok to use here. It is not common practice in VFIO.
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 204
Quoted 9 lines
hw/vfio/migration-multifd.c +9
-0+ + bufs_len =vfio_state_buffers_size_get (&multifd->load_bufs); +if (multifd->load_buf_idx >= bufs_len) { +assert (multifd->load_buf_idx == bufs_len); +return NULL ; + } + + lb =vfio_state_buffers_at (&multifd->load_bufs, + multifd->load_buf_idx);
Could be one line. minor.
Quoted 13 lines
hw/vfio/migration-multifd.c +13
-0+if (!lb->is_present) { +return NULL ; + } + +return lb; +} + +static bool vfio_load_state_buffer_write (VFIODevice *vbasedev, +VFIOStateBuffer *lb, +Error **errp) +{ +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd;
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 227
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ g_autofree char *buf = NULL; +char *buf_cur; +size_t buf_len; + +if (!lb->len) { +return true; + } + +trace_vfio_load_state_device_buffer_load_start (vbasedev->name, + multifd->load_buf_idx);
I thin we can move this trace event to vfio_load_bufs_thread()
Quoted 20 lines
hw/vfio/migration-multifd.c +20
-0+/* lb might become re-allocated when we drop the lock */ + buf =g_steal_pointer (&lb->data); + buf_cur = buf; + buf_len = lb->len; +while (buf_len >0 ) { +ssize_t wr_ret; +int errno_save; + +/* +* Loading data to the device takes a while, +* drop the lock during this process. +*/ +qemu_mutex_unlock (&multifd->load_bufs_mutex); + wr_ret = write(migration->data_fd, buf_cur, buf_len);> + errno_save = errno; +qemu_mutex_lock (&multifd->load_bufs_mutex); + +if (wr_ret <0 ) { +error_setg (errp, +"writing state buffer %" PRIu32" failed: %d" , + multifd->load_buf_idx, errno_save);
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 259
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+return false; + } + +assert (wr_ret <= buf_len); + buf_len -= wr_ret; + buf_cur += wr_ret; + } + +trace_vfio_load_state_device_buffer_load_end (vbasedev->name, + multifd->load_buf_idx);
and drop this trace event.
In which case, we can modify the parameters of vfio_load_state_buffer_write() to use directly a 'VFIOMultifd *multifd'and an fd instead of "migration->data_fd".
Quoted 19 lines
hw/vfio/migration-multifd.c +19
-0+ +return true; +} + +static bool vfio_load_bufs_thread_want_exit (VFIOMultifd *multifd, +bool *should_quit) +{ +return multifd->load_bufs_thread_want_exit ||qatomic_read (should_quit); +} + +/* +* This thread is spawned by vfio_multifd_switchover_start() which gets +* called upon encountering the switchover point marker in main migration +* stream. +* +* It exits after either: +* * completing loading the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by it setting should_quit
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 288
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+* or by vfio_load_cleanup_load_bufs_thread() setting +* multifd->load_bufs_thread_want_exit. +*/ +static bool vfio_load_bufs_thread (void *opaque,bool *should_quit,Error **errp) +{ +VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +bool ret = true; +int config_ret;
No needed IMO. see below.
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 293
Quoted 5 lines
hw/vfio/migration-multifd.c +5
-0+ +assert (multifd); +QEMU_LOCK_GUARD (&multifd->load_bufs_mutex); + +assert (multifd->load_bufs_thread_running);
We could add a trace event for the start and the end of the thread.
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 305
Quoted 11 lines
hw/vfio/migration-multifd.c +11
-0+while (true) { +VFIOStateBuffer *lb; + +/* +* Always check cancellation first after the buffer_ready wait below in +* case that cond was signalled by vfio_load_cleanup_load_bufs_thread(). +*/ +if (vfio_load_bufs_thread_want_exit (multifd, should_quit)) { +error_setg (errp,"operation cancelled" ); + ret = false; + goto ret_signal;
goto thread_exit ?
Quoted 22 lines
hw/vfio/migration-multifd.c +22
-0+ } + +assert (multifd->load_buf_idx <= multifd->load_buf_idx_last); + + lb =vfio_load_state_buffer_get (multifd); +if (!lb) { +trace_vfio_load_state_device_buffer_starved (vbasedev->name, + multifd->load_buf_idx); +qemu_cond_wait (&multifd->load_bufs_buffer_ready_cond, + &multifd->load_bufs_mutex); +continue ; + } + +if (multifd->load_buf_idx == multifd->load_buf_idx_last) { +break ; + } + +if (multifd->load_buf_idx ==0 ) { +trace_vfio_load_state_device_buffer_start (vbasedev->name); + } + +if (!vfio_load_state_buffer_write (vbasedev, lb, errp)) {
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 337
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ ret = false; + goto ret_signal; + } + +if (multifd->load_buf_idx == multifd->load_buf_idx_last -1 ) { +trace_vfio_load_state_device_buffer_end (vbasedev->name); + } + + multifd->load_buf_idx++; + }
if ret is assigned to true here, the "ret = false" can dropped
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 343
Quoted 5 lines
hw/vfio/migration-multifd.c +5
-0+ config_ret =vfio_load_bufs_thread_load_config (vbasedev); +if (config_ret) { +error_setg (errp,"load config state failed: %d" , config_ret); + ret = false; + }
please move to next patch. This is adding nothing to this patch since it's returning -EINVAL.
Thanks,
C.
Quoted 139 lines
hw/vfio/migration-multifd.c +62
-0+ret_signal: +/* +* Notify possibly waiting vfio_load_cleanup_load_bufs_thread() that +* this thread is exiting. +*/ + multifd->load_bufs_thread_running = false; +qemu_cond_signal (&multifd->load_bufs_thread_finished_cond); + +return ret; +} +VFIOMultifd *vfio_multifd_new (void ) {VFIOMultifd *multifd =g_new (VFIOMultifd,1 );@@ -191,11 +365,42 @@ VFIOMultifd *vfio_multifd_new(void) multifd->load_buf_idx_last =UINT32_MAX ;qemu_cond_init (&multifd->load_bufs_buffer_ready_cond); + multifd->load_bufs_thread_running = false; + multifd->load_bufs_thread_want_exit = false; +qemu_cond_init (&multifd->load_bufs_thread_finished_cond); +return multifd; } +/* +* Terminates vfio_load_bufs_thread by setting +* multifd->load_bufs_thread_want_exit and signalling all the conditions +* the thread could be blocked on. +* +* Waits for the thread to signal that it had finished. +*/ +static void vfio_load_cleanup_load_bufs_thread (VFIOMultifd *multifd) +{ +/* The lock order is load_bufs_mutex -> BQL so unlock BQL here first */ +bql_unlock (); + WITH_QEMU_LOCK_GUARD(&multifd->load_bufs_mutex) { +while (multifd->load_bufs_thread_running) { + multifd->load_bufs_thread_want_exit = true; + +qemu_cond_signal (&multifd->load_bufs_buffer_ready_cond); +qemu_cond_wait (&multifd->load_bufs_thread_finished_cond, + &multifd->load_bufs_mutex); + } + } +bql_lock (); +} +void vfio_multifd_free (VFIOMultifd *multifd) { +vfio_load_cleanup_load_bufs_thread (multifd); + +qemu_cond_destroy (&multifd->load_bufs_thread_finished_cond); +vfio_state_buffers_destroy (&multifd->load_bufs);qemu_cond_destroy (&multifd->load_bufs_buffer_ready_cond);qemu_mutex_destroy (&multifd->load_bufs_mutex);@@ -225,3 +430,23 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp)return true; } + +int vfio_multifd_switchover_start (VFIODevice *vbasedev) +{ +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; + +assert (multifd); + +/* The lock order is load_bufs_mutex -> BQL so unlock BQL here first */ +bql_unlock (); + WITH_QEMU_LOCK_GUARD(&multifd->load_bufs_mutex) { +assert (!multifd->load_bufs_thread_running); + multifd->load_bufs_thread_running = true; + } +bql_lock (); + +qemu_loadvm_start_load_thread (vfio_load_bufs_thread, vbasedev); + +return 0 ; +}hw/vfio/migration-multifd.h +2
-0@@ -25,4 +25,6 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp);bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size,Error **errp); +int vfio_multifd_switchover_start (VFIODevice *vbasedev); +#endif hw/vfio/migration.c +12
-0@@ -793,6 +793,17 @@ static bool vfio_switchover_ack_needed(void *opaque)return vfio_precopy_supported (vbasedev); } +static int vfio_switchover_start (void *opaque) +{ +VFIODevice *vbasedev = opaque; + +if (vfio_multifd_transfer_enabled (vbasedev)) { +return vfio_multifd_switchover_start (vbasedev); + } + +return 0 ; +} +static const SaveVMHandlers savevm_vfio_handlers = { .save_prepare = vfio_save_prepare, .save_setup = vfio_save_setup,@@ -808,6 +819,7 @@ static const SaveVMHandlers savevm_vfio_handlers = { .load_state = vfio_load_state, .load_state_buffer = vfio_load_state_buffer, .switchover_ack_needed = vfio_switchover_ack_needed, + .switchover_start = vfio_switchover_start, };/* ---------------------------------------------------------------------- */ hw/vfio/trace-events +5
-0@@ -154,6 +154,11 @@ vfio_load_device_config_state_end(const char *name) " (%s)" vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size %"PRIu64" ret %d" vfio_load_state_device_buffer_incoming(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_start(const char *name) " (%s)" +vfio_load_state_device_buffer_starved(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_load_start(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_load_end(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_end(const char *name) " (%s)" vfio_migration_realize(const char *name) " (%s)" vfio_migration_set_device_state(const char *name, const char *state) " (%s) state %s" vfio_migration_set_state(const char *name, const char *new_state, const char *recover_state) " (%s) new state %s, recover state %s"
Re: [PATCH v5 27/36] vfio/migration: Multifd device state transfer support - load thread
Quoted 46 lines · Cédric Le Goater
Quoted 45 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Since it's important to finish loading device state transferred via the main migration channel (via
save_live_iterateSaveVMHandler) before starting loading the data asynchronously transferred via multifd the thread doing the actual loading of the multifd transferred data is only started fromswitchover_startSaveVMHandler.
switchover_starthandler is called whenMIG_CMD_SWITCHOVER_STARTsub-command ofQEMU_VM_COMMANDis received via the main migration channel.This sub-command is only sent after all
save_live_iteratedata have already been posted so it is safe to commence loading of the multifd-transferred device state upon receiving it - loading ofsave_live_iteratedata happens synchronously in the main migration thread (much like the processing ofMIG_CMD_SWITCHOVER_START) so by the timeMIG_CMD_SWITCHOVER_STARTis processed all the proceeding data must have already been loaded.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 225 ++++++++++++++++++++++++++++++++++++hw/vfio/migration-multifd.h| 2 +
hw/vfio/migration.c | 12 ++
hw/vfio/trace-events | 5 +4 files changed, 244 insertions(+)hw/vfio/migration-multifd.c +5
-0@@ -42,8 +42,13 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer; typedef struct VFIOMultifd { + QemuThread load_bufs_thread; + bool load_bufs_thread_running; + bool load_bufs_thread_want_exit; + VFIOStateBuffers load_bufs; QemuCond load_bufs_buffer_ready_cond; + QemuCond load_bufs_thread_finished_cond;QemuMutex
load_bufs_mutex; /* Lock order: this lock -> BQL */uint32_tload_buf_idx;
uint32_tload_buf_idx_last;hw/vfio/migration-multifd.c +0
-0@@ -179,6 +184,175 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, return true;
Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 190
Quoted 5 lines
hw/vfio/migration-multifd.c +4
-0} +static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev) +{ + return -EINVAL; +}please move to next patch.
As I wrote on the previous version of the patch set at
https://lore.kernel.org/qemu-devel/[email protected]/:
Quoted 7 lines
The dummy call has to be there, otherwise the code at the previous commit time wouldn't compile since that
vfio_load_bufs_thread_load_config()call is a part ofvfio_load_bufs_thread().This is an artifact of splitting the whole load operation in multiple commits.
I think adding empty dummy implementations is the typical way to do this - much like you asked today to leave vfio_multifd_transfer_setup() returning true unconditionally before being filled with true implementation in later patch.
See also my response at the end of this e-mail message, below the call to vfio_load_bufs_thread_load_config().
Quoted 36 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 195
Quoted 4 lines
hw/vfio/migration-multifd.c +4
-0+static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd) +{ + VFIOStateBuffer *lb; + guint bufs_len;guint: I guess it's ok to use here. It is not common practice in VFIO.
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 204
Quoted 9 lines
hw/vfio/migration-multifd.c +9
-0+ + bufs_len = vfio_state_buffers_size_get(&multifd->load_bufs); + if (multifd->load_buf_idx >= bufs_len) { + assert(multifd->load_buf_idx == bufs_len); + return NULL; + } + + lb = vfio_state_buffers_at(&multifd->load_bufs, + multifd->load_buf_idx);Could be one line. minor.
Quoted 17 lines
hw/vfio/migration-multifd.c +17
-0+ if (!lb->is_present) { + return NULL; + } + + return lb; +} + +static bool vfio_load_state_buffer_write (VFIODevice *vbasedev, + VFIOStateBuffer *lb, + Error **errp) +{ + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + g_autofree char *buf = NULL; + char *buf_cur; + size_t buf_len; +
Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 227
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ if (!lb->len) { + return true; + } + + trace_vfio_load_state_device_buffer_load_start(vbasedev->name, + multifd->load_buf_idx);I thin we can move this trace event to
vfio_load_bufs_thread()
It would get messy since we don't load empty buffers, so we we don't print this trace point (and its _end sibling) for empty buffers.
If we print this in vfio_load_bufs_thread() then it would need to duplicate that !lb->len check.
Quoted 24 lines
Quoted 24 lines
hw/vfio/migration-multifd.c +24
-0+ /* lb might become re-allocated when we drop the lock */ + buf = g_steal_pointer(&lb->data); + buf_cur = buf; + buf_len = lb->len; + while (buf_len > 0) { + ssize_t wr_ret; + int errno_save; + + /* + * Loading data to the device takes a while, + * drop the lock during this process. + */ + qemu_mutex_unlock(&multifd->load_bufs_mutex); + wr_ret = write(migration->data_fd, buf_cur, buf_len);> + errno_save = errno; + qemu_mutex_lock(&multifd->load_bufs_mutex); + + if (wr_ret < 0) { + error_setg(errp, + "writing state buffer %" PRIu32 " failed: %d", + multifd->load_buf_idx, errno_save); + return false; + } + + assert(wr_ret <= buf_len);
Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 259
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ buf_len -= wr_ret; + buf_cur += wr_ret; + } + + trace_vfio_load_state_device_buffer_load_end(vbasedev->name, + multifd->load_buf_idx);and drop this trace event.
That's important data since it provides for how long it took to load that buffer (_end - _start).
It's not the same information as _start(next buffer) - _start(current buffer) since the next buffer might not have arrived yet so its loading won't start immediately after the end of loading of the previous one.
Quoted 35 lines
In which case, we can modify the parameters of
vfio_load_state_buffer_write()to use directly a 'VFIOMultifd *multifd'and an fd instead of "migration->data_fd".Quoted 19 lines
hw/vfio/migration-multifd.c +19
-0+ + return true; +} + +static bool vfio_load_bufs_thread_want_exit (VFIOMultifd *multifd, + bool *should_quit) +{ + return multifd->load_bufs_thread_want_exit || qatomic_read(should_quit); +} + +/* +* This thread is spawned by vfio_multifd_switchover_start() which gets +* called upon encountering the switchover point marker in main migration +* stream. +* +* It exits after either: +* * completing loading the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by it setting should_quit hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 288
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+* or by vfio_load_cleanup_load_bufs_thread() setting +* multifd->load_bufs_thread_want_exit. +*/ +static bool vfio_load_bufs_thread (void *opaque,bool *should_quit,Error **errp) +{ + VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + bool ret = true; + int config_ret;No needed IMO. see below.
Quoted 7 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 293
Quoted 5 lines
hw/vfio/migration-multifd.c +5
-0+ + assert(multifd); + QEMU_LOCK_GUARD(&multifd->load_bufs_mutex); + + assert(multifd->load_bufs_thread_running);We could add a trace event for the start and the end of the thread.
Added vfio_load_bufs_thread_{start,end} trace events now.
Quoted 12 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 305
Quoted 11 lines
hw/vfio/migration-multifd.c +11
-0+ while (true) { + VFIOStateBuffer *lb; + + /* + * Always check cancellation first after the buffer_ready wait below in + * case that cond was signalled by vfio_load_cleanup_load_bufs_thread(). + */ + if (vfio_load_bufs_thread_want_exit(multifd, should_quit)) { + error_setg(errp, "operation cancelled"); + ret = false; + goto ret_signal;
Quoted 1 line
goto
thread_exit?
I'm not sure that I fully understand this comment. Do you mean to rename ret_signal label to thread_exit?
Quoted 26 lines
Quoted 26 lines
hw/vfio/migration-multifd.c +26
-0+ } + + assert(multifd->load_buf_idx <= multifd->load_buf_idx_last); + + lb = vfio_load_state_buffer_get(multifd); + if (!lb) { + trace_vfio_load_state_device_buffer_starved(vbasedev->name, + multifd->load_buf_idx); + qemu_cond_wait(&multifd->load_bufs_buffer_ready_cond, + &multifd->load_bufs_mutex); + continue; + } + + if (multifd->load_buf_idx == multifd->load_buf_idx_last) { + break; + } + + if (multifd->load_buf_idx == 0) { + trace_vfio_load_state_device_buffer_start(vbasedev->name); + } + + if (!vfio_load_state_buffer_write(vbasedev, lb, errp)) { + ret = false; + goto ret_signal; + } +
Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 337
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ if (multifd->load_buf_idx == multifd->load_buf_idx_last - 1) { + trace_vfio_load_state_device_buffer_end(vbasedev->name); + } + + multifd->load_buf_idx++; + }if ret is assigned to true here, the "ret = false" can dropped
I inverted the "ret" logic here now - initialized ret to false at definition, removed "ret = false" at every failure/early exit block and added "ret = true" just before the "ret_signal" label.
Quoted 9 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 343
Quoted 5 lines
hw/vfio/migration-multifd.c +5
-0+ config_ret = vfio_load_bufs_thread_load_config(vbasedev); + if (config_ret) { + error_setg(errp, "load config state failed: %d", config_ret); + ret = false; + }please move to next patch. This is adding nothing to this patch since it's returning
-EINVAL.
That's the whole point - if someone were to accidentally enable this (for example by forgetting to apply the next patch when backporting the series) it would fail safely with EINVAL instead of having a half-broken implementation.
Another option would be to simply integrate the next patch into this one as these are two parts of the same single operation and I think splitting them in two in the end brings little value.
Quoted 3 lines
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 27/36] vfio/migration: Multifd device state transfer support - load thread
Quoted 223 lines · Maciej S. Szmigiero
Quoted 46 lines · Cédric Le Goater
Quoted 45 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Since it's important to finish loading device state transferred via the main migration channel (via
save_live_iterateSaveVMHandler) before starting loading the data asynchronously transferred via multifd the thread doing the actual loading of the multifd transferred data is only started fromswitchover_startSaveVMHandler.
switchover_starthandler is called whenMIG_CMD_SWITCHOVER_STARTsub-command ofQEMU_VM_COMMANDis received via the main migration channel.This sub-command is only sent after all
save_live_iteratedata have already been posted so it is safe to commence loading of the multifd-transferred device state upon receiving it - loading ofsave_live_iteratedata happens synchronously in the main migration thread (much like the processing ofMIG_CMD_SWITCHOVER_START) so by the timeMIG_CMD_SWITCHOVER_STARTis processed all the proceeding data must have already been loaded.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 225 ++++++++++++++++++++++++++++++++++++hw/vfio/migration-multifd.h| 2 +
hw/vfio/migration.c | 12 ++
hw/vfio/trace-events | 5 +4 files changed, 244 insertions(+)hw/vfio/migration-multifd.c +5
-0@@ -42,8 +42,13 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer; typedef struct VFIOMultifd { + QemuThread load_bufs_thread; + bool load_bufs_thread_running; + bool load_bufs_thread_want_exit; + VFIOStateBuffers load_bufs; QemuCond load_bufs_buffer_ready_cond; + QemuCond load_bufs_thread_finished_cond;QemuMutex
load_bufs_mutex; /* Lock order: this lock -> BQL */uint32_tload_buf_idx;
uint32_tload_buf_idx_last;hw/vfio/migration-multifd.c +0
-0@@ -179,6 +184,175 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, return true;Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 190
Quoted 5 lines
hw/vfio/migration-multifd.c +4
-0} +static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev) +{ + return -EINVAL; +}please move to next patch.
As I wrote on the previous version of the patch set at
https://lore.kernel.org/qemu-devel/[email protected]/:Quoted 7 lines
The dummy call has to be there, otherwise the code at the previous commit time wouldn't compile since that
vfio_load_bufs_thread_load_config()call is a part ofvfio_load_bufs_thread().This is an artifact of splitting the whole load operation in multiple commits.
I think adding empty dummy implementations is the typical way to do this - much like you asked today to leave
vfio_multifd_transfer_setup()returning true unconditionally before being filled with true implementation in later patch.See also my response at the end of this e-mail message, below the call to
vfio_load_bufs_thread_load_config().Quoted 36 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 195
Quoted 4 lines
hw/vfio/migration-multifd.c +4
-0+static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd) +{ + VFIOStateBuffer *lb; + guint bufs_len;guint: I guess it's ok to use here. It is not common practice in VFIO.
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 204
Quoted 9 lines
hw/vfio/migration-multifd.c +9
-0+ + bufs_len = vfio_state_buffers_size_get(&multifd->load_bufs); + if (multifd->load_buf_idx >= bufs_len) { + assert(multifd->load_buf_idx == bufs_len); + return NULL; + } + + lb = vfio_state_buffers_at(&multifd->load_bufs, + multifd->load_buf_idx);Could be one line. minor.
Quoted 17 lines
hw/vfio/migration-multifd.c +17
-0+ if (!lb->is_present) { + return NULL; + } + + return lb; +} + +static bool vfio_load_state_buffer_write (VFIODevice *vbasedev, + VFIOStateBuffer *lb, + Error **errp) +{ + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + g_autofree char *buf = NULL; + char *buf_cur; + size_t buf_len; +Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 227
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ if (!lb->len) { + return true; + } + + trace_vfio_load_state_device_buffer_load_start(vbasedev->name, + multifd->load_buf_idx);I thin we can move this trace event to
vfio_load_bufs_thread()It would get messy since we don't load empty buffers, so we we don't print this trace point (and its
_endsibling) for empty buffers.If we print this in
vfio_load_bufs_thread()then it would need to duplicate that !lb->len check.Quoted 24 lines
Quoted 24 lines
hw/vfio/migration-multifd.c +24
-0+ /* lb might become re-allocated when we drop the lock */ + buf = g_steal_pointer(&lb->data); + buf_cur = buf; + buf_len = lb->len; + while (buf_len > 0) { + ssize_t wr_ret; + int errno_save; + + /* + * Loading data to the device takes a while, + * drop the lock during this process. + */ + qemu_mutex_unlock(&multifd->load_bufs_mutex); + wr_ret = write(migration->data_fd, buf_cur, buf_len);> + errno_save = errno; + qemu_mutex_lock(&multifd->load_bufs_mutex); + + if (wr_ret < 0) { + error_setg(errp, + "writing state buffer %" PRIu32 " failed: %d", + multifd->load_buf_idx, errno_save); + return false; + } + + assert(wr_ret <= buf_len);Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 259
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ buf_len -= wr_ret; + buf_cur += wr_ret; + } + + trace_vfio_load_state_device_buffer_load_end(vbasedev->name, + multifd->load_buf_idx);and drop this trace event.
That's important data since it provides for how long it took to load that buffer (
_end-_start).It's not the same information as
_start(next buffer)-_start(current buffer)since the next buffer might not have arrived yet so its loading won't start immediately after the end of loading of the previous one.Quoted 35 lines
In which case, we can modify the parameters of
vfio_load_state_buffer_write()to use directly a 'VFIOMultifd *multifd'and an fd instead of "migration->data_fd".Quoted 19 lines
hw/vfio/migration-multifd.c +19
-0+ + return true; +} + +static bool vfio_load_bufs_thread_want_exit (VFIOMultifd *multifd, + bool *should_quit) +{ + return multifd->load_bufs_thread_want_exit || qatomic_read(should_quit); +} + +/* +* This thread is spawned by vfio_multifd_switchover_start() which gets +* called upon encountering the switchover point marker in main migration +* stream. +* +* It exits after either: +* * completing loading the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by it setting should_quit hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 288
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+* or by vfio_load_cleanup_load_bufs_thread() setting +* multifd->load_bufs_thread_want_exit. +*/ +static bool vfio_load_bufs_thread (void *opaque,bool *should_quit,Error **errp) +{ + VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + bool ret = true; + int config_ret;No needed IMO. see below.
Quoted 7 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 293
Quoted 5 lines
hw/vfio/migration-multifd.c +5
-0+ + assert(multifd); + QEMU_LOCK_GUARD(&multifd->load_bufs_mutex); + + assert(multifd->load_bufs_thread_running);We could add a trace event for the start and the end of the thread.
Added vfio_load_bufs_thread_{start,end} trace events now.
Quoted 12 lines
Quoted 11 lines
hw/vfio/migration-multifd.c +11
-0+ while (true) { + VFIOStateBuffer *lb; + + /* + * Always check cancellation first after the buffer_ready wait below in + * case that cond was signalled by vfio_load_cleanup_load_bufs_thread(). + */ + if (vfio_load_bufs_thread_want_exit(multifd, should_quit)) { + error_setg(errp, "operation cancelled"); + ret = false; + goto ret_signal;
Quoted 4 lines
Quoted 1 line
goto
thread_exit?I'm not sure that I fully understand this comment. Do you mean to rename
ret_signallabel tothread_exit?
Yes. I find label 'thread_exit' more meaning full. This is minor since there is only one 'exit' label.
Quoted 46 lines
Quoted 27 lines
Quoted 27 lines
hw/vfio/migration-multifd.c +26
-0+ } + + assert(multifd->load_buf_idx <= multifd->load_buf_idx_last); + + lb = vfio_load_state_buffer_get(multifd); + if (!lb) { + trace_vfio_load_state_device_buffer_starved(vbasedev->name, + multifd->load_buf_idx); + qemu_cond_wait(&multifd->load_bufs_buffer_ready_cond, + &multifd->load_bufs_mutex); + continue; + } + + if (multifd->load_buf_idx == multifd->load_buf_idx_last) { + break; + } + + if (multifd->load_buf_idx == 0) { + trace_vfio_load_state_device_buffer_start(vbasedev->name); + } + + if (!vfio_load_state_buffer_write(vbasedev, lb, errp)) { + ret = false; + goto ret_signal; + } +Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 337
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ if (multifd->load_buf_idx == multifd->load_buf_idx_last - 1) { + trace_vfio_load_state_device_buffer_end(vbasedev->name); + } + + multifd->load_buf_idx++; + }if ret is assigned to true here, the "ret = false" can dropped
I inverted the "ret" logic here now - initialized ret to false at definition, removed "ret = false" at every failure/early exit block and added "ret = true" just before the "
ret_signal" label.Quoted 6 lines
Quoted 5 lines
hw/vfio/migration-multifd.c +5
-0+ config_ret = vfio_load_bufs_thread_load_config(vbasedev); + if (config_ret) { + error_setg(errp, "load config state failed: %d", config_ret); + ret = false; + }
Quoted 8 lines
Quoted 3 lines
please move to next patch. This is adding nothing to this patch since it's returning
-EINVAL.That's the whole point - if someone were to accidentally enable this (for example by forgetting to apply the next patch when backporting the series) it would fail safely with EINVAL instead of having a half-broken implementation.
OK. Let's keep it that way.
Thanks,
C.
Quoted 12 lines
Another option would be to simply integrate the next patch into this one as these are two parts of the same single operation and I think splitting them in two in the end brings little value.
Quoted 3 lines
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 27/36] vfio/migration: Multifd device state transfer support - load thread
Quoted 33 lines · Cédric Le Goater
Quoted 32 lines · Maciej S. Szmigiero
Quoted 31 lines · Cédric Le Goater
Quoted 30 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Since it's important to finish loading device state transferred via the main migration channel (via
save_live_iterateSaveVMHandler) before starting loading the data asynchronously transferred via multifd the thread doing the actual loading of the multifd transferred data is only started fromswitchover_startSaveVMHandler.
switchover_starthandler is called whenMIG_CMD_SWITCHOVER_STARTsub-command ofQEMU_VM_COMMANDis received via the main migration channel.This sub-command is only sent after all
save_live_iteratedata have already been posted so it is safe to commence loading of the multifd-transferred device state upon receiving it - loading ofsave_live_iteratedata happens synchronously in the main migration thread (much like the processing ofMIG_CMD_SWITCHOVER_START) so by the timeMIG_CMD_SWITCHOVER_STARTis processed all the proceeding data must have already been loaded.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 225 ++++++++++++++++++++++++++++++++++++hw/vfio/migration-multifd.h| 2 +
hw/vfio/migration.c | 12 ++
hw/vfio/trace-events | 5 +4 files changed, 244 insertions(+)hw/vfio/migration-multifd.c +0
-0
(..)
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
+ while (true) {
+ VFIOStateBuffer *lb;
+
+ /*
+ * Always check cancellation first after thebuffer_readywait below in + * case that cond was signalled byvfio_load_cleanup_load_bufs_thread(). + */
+ if (vfio_load_bufs_thread_want_exit(multifd, should_quit)) { +error_setg(errp, "operation cancelled");
+ ret = false;
+ gotoret_signal;
Quoted 9 lines
Quoted 4 lines
Quoted 1 line
goto
thread_exit?I'm not sure that I fully understand this comment. Do you mean to rename
ret_signallabel tothread_exit?Yes. I find label '
thread_exit' more meaning full. This is minor since there is only one 'exit' label.
Renamed ret_signal to thread_exit then.
(..)
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
+
config_ret=vfio_load_bufs_thread_load_config(vbasedev); + if (config_ret) {
+error_setg(errp, "load config state failed: %d", config_ret); + ret = false;
+ }Quoted 8 lines
Quoted 3 lines
please move to next patch. This is adding nothing to this patch since it's returning
-EINVAL.That's the whole point - if someone were to accidentally enable this (for example by forgetting to apply the next patch when backporting the series) it would fail safely with EINVAL instead of having a half-broken implementation.
Quoted 6 lines
OK. Let's keep it that way.
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 27/36] vfio/migration: Multifd device state transfer support - load thread
In reply to Cédric Le GoaterQuoted 61 lines · Cédric Le Goater
External email: Use caution opening links or attachments
Quoted 45 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Since it's important to finish loading device state transferred via the main migration channel (via
save_live_iterateSaveVMHandler) before starting loading the data asynchronously transferred via multifd the thread doing the actual loading of the multifd transferred data is only started fromswitchover_startSaveVMHandler.
switchover_starthandler is called whenMIG_CMD_SWITCHOVER_STARTsub-command ofQEMU_VM_COMMANDis received via the main migration channel.This sub-command is only sent after all
save_live_iteratedata have already been posted so it is safe to commence loading of the multifd-transferred device state upon receiving it - loading ofsave_live_iteratedata happens synchronously in the main migration thread (much like the processing ofMIG_CMD_SWITCHOVER_START) so by the timeMIG_CMD_SWITCHOVER_STARTis processed all the proceeding data must have already been loaded.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 225 ++++++++++++++++++++++++++++++++++++hw/vfio/migration-multifd.h| 2 +
hw/vfio/migration.c | 12 ++
hw/vfio/trace-events | 5 +4 files changed, 244 insertions(+)hw/vfio/migration-multifd.c +5
-0@@ -42,8 +42,13 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer; typedef struct VFIOMultifd { + QemuThread load_bufs_thread; + bool load_bufs_thread_running; + bool load_bufs_thread_want_exit; + VFIOStateBuffers load_bufs; QemuCond load_bufs_buffer_ready_cond; + QemuCond load_bufs_thread_finished_cond;QemuMutex
load_bufs_mutex; /* Lock order: this lock -> BQL */uint32_tload_buf_idx;
uint32_tload_buf_idx_last;hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char · line 191
Quoted 8 lines
hw/vfio/migration-multifd.c +4
-0@@ -179,6 +184,175 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, return true; } +static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev) +{ + return -EINVAL; +}please move to next patch.
Quoted 7 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 195
Quoted 5 lines
hw/vfio/migration-multifd.c +4
-0+static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd) +{ + VFIOStateBuffer *lb; + guint bufs_len;guint: I guess it's ok to use here. It is not common practice in VFIO.
Glib documentation says that in new code unsigned int is preferred over guint [1].
Thanks.
Re: [PATCH v5 27/36] vfio/migration: Multifd device state transfer support - load thread
Quoted 62 lines · Avihai Horon
Quoted 60 lines · Cédric Le Goater
External email: Use caution opening links or attachments
Quoted 45 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Since it's important to finish loading device state transferred via the main migration channel (via
save_live_iterateSaveVMHandler) before starting loading the data asynchronously transferred via multifd the thread doing the actual loading of the multifd transferred data is only started fromswitchover_startSaveVMHandler.
switchover_starthandler is called whenMIG_CMD_SWITCHOVER_STARTsub-command ofQEMU_VM_COMMANDis received via the main migration channel.This sub-command is only sent after all
save_live_iteratedata have already been posted so it is safe to commence loading of the multifd-transferred device state upon receiving it - loading ofsave_live_iteratedata happens synchronously in the main migration thread (much like the processing ofMIG_CMD_SWITCHOVER_START) so by the timeMIG_CMD_SWITCHOVER_STARTis processed all the proceeding data must have already been loaded.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 225 ++++++++++++++++++++++++++++++++++++hw/vfio/migration-multifd.h| 2 +
hw/vfio/migration.c | 12 ++
hw/vfio/trace-events | 5 +4 files changed, 244 insertions(+)hw/vfio/migration-multifd.c +5
-0@@ -42,8 +42,13 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer; typedef struct VFIOMultifd { + QemuThread load_bufs_thread; + bool load_bufs_thread_running; + bool load_bufs_thread_want_exit; + VFIOStateBuffers load_bufs; QemuCond load_bufs_buffer_ready_cond; + QemuCond load_bufs_thread_finished_cond;QemuMutex
load_bufs_mutex; /* Lock order: this lock -> BQL */uint32_tload_buf_idx;
uint32_tload_buf_idx_last;hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 190
Quoted 7 lines
hw/vfio/migration-multifd.c +4
-0@@ -179,6 +184,175 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, return true; } +static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev) +{ + return -EINVAL; +}please move to next patch.
Quoted 8 lines
Quoted 6 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 195
Quoted 4 lines
hw/vfio/migration-multifd.c +4
-0+static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd) +{ + VFIOStateBuffer *lb; + guint bufs_len;guint: I guess it's ok to use here. It is not common practice in VFIO.
Glib documentation says that in new code unsigned int is preferred over guint [1].
I turned guints into unsigned ints where I spotted them in this patch set.
Quoted 1 line
Thanks.
Thanks,
Maciej
Quoted 2 lines
Re: [PATCH v5 27/36] vfio/migration: Multifd device state transfer support - load thread
In reply to Maciej S. SzmigieroQuoted 4 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
Maybe add a sentence talking about the load thread itself first? E.g.:
Add a thread which loads the VFIO device state buffers that were received and via multifd.
Each VFIO device that has multifd device state transfer enabled has one such thread, which is created using migration core API qemu_loadvm_start_load_thread().
Since it's important to finish...
Quoted 29 lines
Since it's important to finish loading device state transferred via the main migration channel (via
save_live_iterateSaveVMHandler) before starting loading the data asynchronously transferred via multifd the thread doing the actual loading of the multifd transferred data is only started fromswitchover_startSaveVMHandler.
switchover_starthandler is called whenMIG_CMD_SWITCHOVER_STARTsub-command ofQEMU_VM_COMMANDis received via the main migration channel.This sub-command is only sent after all
save_live_iteratedata have already been posted so it is safe to commence loading of the multifd-transferred device state upon receiving it - loading ofsave_live_iteratedata happens synchronously in the main migration thread (much like the processing ofMIG_CMD_SWITCHOVER_START) so by the timeMIG_CMD_SWITCHOVER_STARTis processed all the proceeding data must have already been loaded.Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 225 ++++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 2 + hw/vfio/migration.c | 12 ++ hw/vfio/trace-events | 5 + 4 files changed, 244 insertions(+)hw/vfio/migration-multifd.c +0
-0
hw/vfio/migration-multifd.c typedef struct VFIOStateBuffer { · line 45
Quoted 4 lines
hw/vfio/migration-multifd.c +1
-0@@ -42,8 +42,13 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer; typedef struct VFIOMultifd { + QemuThread load_bufs_thread;
This can be dropped.
Quoted 66 lines
hw/vfio/migration-multifd.c +57
-0+bool load_bufs_thread_running; +bool load_bufs_thread_want_exit; +VFIOStateBuffers load_bufs;QemuCond load_bufs_buffer_ready_cond; +QemuCond load_bufs_thread_finished_cond;QemuMutex load_bufs_mutex;/* Lock order: this lock -> BQL */ uint32_t load_buf_idx;uint32_t load_buf_idx_last;@@ -179,6 +184,175 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size,return true; } +static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev) +{ +return -EINVAL ; +} + +static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd) +{ +VFIOStateBuffer *lb; +guint bufs_len; + + bufs_len =vfio_state_buffers_size_get (&multifd->load_bufs); +if (multifd->load_buf_idx >= bufs_len) { +assert (multifd->load_buf_idx == bufs_len); +return NULL ; + } + + lb =vfio_state_buffers_at (&multifd->load_bufs, + multifd->load_buf_idx); +if (!lb->is_present) { +return NULL ; + } + +return lb; +} + +static bool vfio_load_state_buffer_write (VFIODevice *vbasedev, +VFIOStateBuffer *lb, +Error **errp) +{ +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; + g_autofree char *buf = NULL; +char *buf_cur; +size_t buf_len; + +if (!lb->len) { +return true; + } + +trace_vfio_load_state_device_buffer_load_start (vbasedev->name, + multifd->load_buf_idx); + +/* lb might become re-allocated when we drop the lock */ + buf =g_steal_pointer (&lb->data); + buf_cur = buf; + buf_len = lb->len; +while (buf_len >0 ) { +ssize_t wr_ret; +int errno_save; + +/* +* Loading data to the device takes a while, +* drop the lock during this process.
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 249
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+*/ +qemu_mutex_unlock (&multifd->load_bufs_mutex); + wr_ret =write (migration->data_fd, buf_cur, buf_len); + errno_save = errno; +qemu_mutex_lock (&multifd->load_bufs_mutex); + +if (wr_ret <0 ) { +error_setg (errp, +"writing state buffer %" PRIu32" failed: %d" , + multifd->load_buf_idx, errno_save);
Let's add vbasedev->name to the error message so we know which device caused the error.
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 253
Quoted 4 lines
hw/vfio/migration-multifd.c +4
-0+return false; + } + +assert (wr_ret <= buf_len);
I think this assert is redundant: we write buf_len bytes and by definition of write() wr_ret will be <= buf_len.
Quoted 42 lines
hw/vfio/migration-multifd.c +42
-0+ buf_len -= wr_ret; + buf_cur += wr_ret; + } + +trace_vfio_load_state_device_buffer_load_end (vbasedev->name, + multifd->load_buf_idx); + +return true; +} + +static bool vfio_load_bufs_thread_want_exit (VFIOMultifd *multifd, +bool *should_quit) +{ +return multifd->load_bufs_thread_want_exit ||qatomic_read (should_quit); +} + +/* +* This thread is spawned by vfio_multifd_switchover_start() which gets +* called upon encountering the switchover point marker in main migration +* stream. +* +* It exits after either: +* * completing loading the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by it setting should_quit +* or by vfio_load_cleanup_load_bufs_thread() setting +* multifd->load_bufs_thread_want_exit. +*/ +static bool vfio_load_bufs_thread (void *opaque,bool *should_quit,Error **errp) +{ +VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +bool ret = true; +int config_ret; + +assert (multifd); +QEMU_LOCK_GUARD (&multifd->load_bufs_mutex); + +assert (multifd->load_bufs_thread_running); + +while (true) {
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 305
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+VFIOStateBuffer *lb; + +/* +* Always check cancellation first after the buffer_ready wait below in +* case that cond was signalled by vfio_load_cleanup_load_bufs_thread(). +*/ +if (vfio_load_bufs_thread_want_exit (multifd, should_quit)) { +error_setg (errp,"operation cancelled" ); + ret = false; + goto ret_signal;
IIUC, if vfio_load_bufs_thread_want_exit() returns true, it means that some other code part already failed and set migration error, no? If so, shouldn't we return true here? After all, vfio_load_bufs_thread didn't really fail, it just got signal to terminate itself.
Quoted 26 lines
hw/vfio/migration-multifd.c +26
-0+ } + +assert (multifd->load_buf_idx <= multifd->load_buf_idx_last); + + lb =vfio_load_state_buffer_get (multifd); +if (!lb) { +trace_vfio_load_state_device_buffer_starved (vbasedev->name, + multifd->load_buf_idx); +qemu_cond_wait (&multifd->load_bufs_buffer_ready_cond, + &multifd->load_bufs_mutex); +continue ; + } + +if (multifd->load_buf_idx == multifd->load_buf_idx_last) { +break ; + } + +if (multifd->load_buf_idx ==0 ) { +trace_vfio_load_state_device_buffer_start (vbasedev->name); + } + +if (!vfio_load_state_buffer_write (vbasedev, lb, errp)) { + ret = false; + goto ret_signal; + } +
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 341
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+if (multifd->load_buf_idx == multifd->load_buf_idx_last -1 ) { +trace_vfio_load_state_device_buffer_end (vbasedev->name); + } + + multifd->load_buf_idx++; + } + + config_ret =vfio_load_bufs_thread_load_config (vbasedev); +if (config_ret) { +error_setg (errp,"load config state failed: %d" , config_ret);
Let's add vbasedev->name to the error message so we know which device caused the error.
Quoted 47 lines
hw/vfio/migration-multifd.c +37
-0+ ret = false; + } + +ret_signal: +/* +* Notify possibly waiting vfio_load_cleanup_load_bufs_thread() that +* this thread is exiting. +*/ + multifd->load_bufs_thread_running = false; +qemu_cond_signal (&multifd->load_bufs_thread_finished_cond); + +return ret; +} +VFIOMultifd *vfio_multifd_new (void ) {VFIOMultifd *multifd =g_new (VFIOMultifd,1 );@@ -191,11 +365,42 @@ VFIOMultifd *vfio_multifd_new(void) multifd->load_buf_idx_last =UINT32_MAX ;qemu_cond_init (&multifd->load_bufs_buffer_ready_cond); + multifd->load_bufs_thread_running = false; + multifd->load_bufs_thread_want_exit = false; +qemu_cond_init (&multifd->load_bufs_thread_finished_cond); +return multifd; } +/* +* Terminates vfio_load_bufs_thread by setting +* multifd->load_bufs_thread_want_exit and signalling all the conditions +* the thread could be blocked on. +* +* Waits for the thread to signal that it had finished. +*/ +static void vfio_load_cleanup_load_bufs_thread (VFIOMultifd *multifd) +{ +/* The lock order is load_bufs_mutex -> BQL so unlock BQL here first */ +bql_unlock (); + WITH_QEMU_LOCK_GUARD(&multifd->load_bufs_mutex) { +while (multifd->load_bufs_thread_running) { + multifd->load_bufs_thread_want_exit = true; + +qemu_cond_signal (&multifd->load_bufs_buffer_ready_cond); +qemu_cond_wait (&multifd->load_bufs_thread_finished_cond, + &multifd->load_bufs_mutex); + }
hw/vfio/migration-multifd.c VFIOMultifd *vfio_multifd_new(void) · line 403
Quoted 10 lines
hw/vfio/migration-multifd.c +8
-0+ } +bql_lock (); +} +void vfio_multifd_free (VFIOMultifd *multifd) { +vfio_load_cleanup_load_bufs_thread (multifd); + +qemu_cond_destroy (&multifd->load_bufs_thread_finished_cond); +vfio_state_buffers_destroy (&multifd->load_bufs);
vfio_state_buffers_destroy(&multifd->load_bufs); belongs to patch #26, no?
Thanks.
Quoted 84 lines
hw/vfio/migration-multifd.c +20
-0qemu_cond_destroy (&multifd->load_bufs_buffer_ready_cond);qemu_mutex_destroy (&multifd->load_bufs_mutex);@@ -225,3 +430,23 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp)return true; } + +int vfio_multifd_switchover_start (VFIODevice *vbasedev) +{ +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; + +assert (multifd); + +/* The lock order is load_bufs_mutex -> BQL so unlock BQL here first */ +bql_unlock (); + WITH_QEMU_LOCK_GUARD(&multifd->load_bufs_mutex) { +assert (!multifd->load_bufs_thread_running); + multifd->load_bufs_thread_running = true; + } +bql_lock (); + +qemu_loadvm_start_load_thread (vfio_load_bufs_thread, vbasedev); + +return 0 ; +}hw/vfio/migration-multifd.h +2
-0@@ -25,4 +25,6 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp);bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size,Error **errp); +int vfio_multifd_switchover_start (VFIODevice *vbasedev); +#endif hw/vfio/migration.c +12
-0@@ -793,6 +793,17 @@ static bool vfio_switchover_ack_needed(void *opaque)return vfio_precopy_supported (vbasedev); } +static int vfio_switchover_start (void *opaque) +{ +VFIODevice *vbasedev = opaque; + +if (vfio_multifd_transfer_enabled (vbasedev)) { +return vfio_multifd_switchover_start (vbasedev); + } + +return 0 ; +} +static const SaveVMHandlers savevm_vfio_handlers = { .save_prepare = vfio_save_prepare, .save_setup = vfio_save_setup,@@ -808,6 +819,7 @@ static const SaveVMHandlers savevm_vfio_handlers = { .load_state = vfio_load_state, .load_state_buffer = vfio_load_state_buffer, .switchover_ack_needed = vfio_switchover_ack_needed, + .switchover_start = vfio_switchover_start, };/* ---------------------------------------------------------------------- */ hw/vfio/trace-events +5
-0@@ -154,6 +154,11 @@ vfio_load_device_config_state_end(const char *name) " (%s)" vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size %"PRIu64" ret %d" vfio_load_state_device_buffer_incoming(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_start(const char *name) " (%s)" +vfio_load_state_device_buffer_starved(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_load_start(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_load_end(const char *name, uint32_t idx) " (%s) idx %"PRIu32 +vfio_load_state_device_buffer_end(const char *name) " (%s)" vfio_migration_realize(const char *name) " (%s)" vfio_migration_set_device_state(const char *name, const char *state) " (%s) state %s" vfio_migration_set_state(const char *name, const char *new_state, const char *recover_state) " (%s) new state %s, recover state %s"
Re: [PATCH v5 27/36] vfio/migration: Multifd device state transfer support - load thread
Quoted 5 lines · Avihai Horon
Quoted 3 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
Quoted 8 lines
Quoted 1 line
From: "Maciej S. Szmigiero" <[email protected]>
Maybe add a sentence talking about the load thread itself first? E.g.:
Add a thread which loads the VFIO device state buffers that were received and via multifd.
Each VFIO device that has multifd device state transfer enabled has one such thread, which is created using migration core APIqemu_loadvm_start_load_thread().Since it's important to finish...
Added such leading text to the commit message for this patch.
Quoted 31 lines
Quoted 31 lines
Since it's important to finish loading device state transferred via the main migration channel (via
save_live_iterateSaveVMHandler) before starting loading the data asynchronously transferred via multifd the thread doing the actual loading of the multifd transferred data is only started fromswitchover_startSaveVMHandler.
switchover_starthandler is called whenMIG_CMD_SWITCHOVER_STARTsub-command ofQEMU_VM_COMMANDis received via the main migration channel.This sub-command is only sent after all
save_live_iteratedata have already been posted so it is safe to commence loading of the multifd-transferred device state upon receiving it - loading ofsave_live_iteratedata happens synchronously in the main migration thread (much like the processing ofMIG_CMD_SWITCHOVER_START) so by the timeMIG_CMD_SWITCHOVER_STARTis processed all the proceeding data must have already been loaded.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 225 ++++++++++++++++++++++++++++++++++++hw/vfio/migration-multifd.h| 2 +
hw/vfio/migration.c | 12 ++
hw/vfio/trace-events | 5 +4 files changed, 244 insertions(+)hw/vfio/migration-multifd.c +0
-0@@ -42,8 +42,13 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer;
Quoted 4 lines
hw/vfio/migration-multifd.c typedef struct VFIOStateBuffer { · line 45
Quoted 2 lines
hw/vfio/migration-multifd.c +1
-0typedef struct VFIOMultifd { + QemuThread load_bufs_thread;This can be dropped.
Yeah - it was a remainder from pre-load-thread days of v2.
Dropped now.
Quoted 70 lines
Quoted 70 lines
hw/vfio/migration-multifd.c +4
-0+ bool load_bufs_thread_running; + bool load_bufs_thread_want_exit; + VFIOStateBuffers load_bufs; QemuCond load_bufs_buffer_ready_cond; + QemuCond load_bufs_thread_finished_cond;QemuMutex
load_bufs_mutex; /* Lock order: this lock -> BQL */uint32_tload_buf_idx;
uint32_tload_buf_idx_last;hw/vfio/migration-multifd.c +57
-0@@ -179,6 +184,175 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, return true; } +static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev) +{ + return -EINVAL; +} + +static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd) +{ + VFIOStateBuffer *lb; + guint bufs_len; + + bufs_len = vfio_state_buffers_size_get(&multifd->load_bufs); + if (multifd->load_buf_idx >= bufs_len) { + assert(multifd->load_buf_idx == bufs_len); + return NULL; + } + + lb = vfio_state_buffers_at(&multifd->load_bufs, + multifd->load_buf_idx); + if (!lb->is_present) { + return NULL; + } + + return lb; +} + +static bool vfio_load_state_buffer_write (VFIODevice *vbasedev, + VFIOStateBuffer *lb, + Error **errp) +{ + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + g_autofree char *buf = NULL; + char *buf_cur; + size_t buf_len; + + if (!lb->len) { + return true; + } + + trace_vfio_load_state_device_buffer_load_start(vbasedev->name, + multifd->load_buf_idx); + + /* lb might become re-allocated when we drop the lock */ + buf = g_steal_pointer(&lb->data); + buf_cur = buf; + buf_len = lb->len; + while (buf_len > 0) { + ssize_t wr_ret; + int errno_save; + + /* + * Loading data to the device takes a while, + * drop the lock during this process. + */ + qemu_mutex_unlock(&multifd->load_bufs_mutex); + wr_ret = write(migration->data_fd, buf_cur, buf_len); + errno_save = errno;
Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 249
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ qemu_mutex_lock(&multifd->load_bufs_mutex); + + if (wr_ret < 0) { + error_setg(errp, + "writing state buffer %" PRIu32 " failed: %d", + multifd->load_buf_idx, errno_save);Let's add
vbasedev->nameto the error message so we know which device caused the error.
Done.
Quoted 6 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 253
Quoted 4 lines
hw/vfio/migration-multifd.c +4
-0+ return false; + } + + assert(wr_ret <= buf_len);I think this assert is redundant: we write
buf_lenbytes and by definition ofwrite()wr_retwill be <=buf_len.
It's for catching when the "definition" for some reason does not match reality since this would result in a reading well past the buffer.
That's why it's an assert, not an error return.
Quoted 47 lines
Quoted 47 lines
hw/vfio/migration-multifd.c +47
-0+ buf_len -= wr_ret; + buf_cur += wr_ret; + } + + trace_vfio_load_state_device_buffer_load_end(vbasedev->name, + multifd->load_buf_idx); + + return true; +} + +static bool vfio_load_bufs_thread_want_exit (VFIOMultifd *multifd, + bool *should_quit) +{ + return multifd->load_bufs_thread_want_exit || qatomic_read(should_quit); +} + +/* +* This thread is spawned by vfio_multifd_switchover_start() which gets +* called upon encountering the switchover point marker in main migration +* stream. +* +* It exits after either: +* * completing loading the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by it setting should_quit +* or by vfio_load_cleanup_load_bufs_thread() setting +* multifd->load_bufs_thread_want_exit. +*/ +static bool vfio_load_bufs_thread (void *opaque,bool *should_quit,Error **errp) +{ + VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + bool ret = true; + int config_ret; + + assert(multifd); + QEMU_LOCK_GUARD(&multifd->load_bufs_mutex); + + assert(multifd->load_bufs_thread_running); + + while (true) { + VFIOStateBuffer *lb; + + /* +* Always check cancellation first after the buffer_ready wait below in +* case that cond was signalled by vfio_load_cleanup_load_bufs_thread().
Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 305
Quoted 5 lines
hw/vfio/migration-multifd.c +5
-0+*/ + if (vfio_load_bufs_thread_want_exit(multifd, should_quit)) { + error_setg(errp, "operation cancelled"); + ret = false; + goto ret_signal;IIUC, if
vfio_load_bufs_thread_want_exit()returns true, it means that some other code part already failed and set migration error, no? If so, shouldn't we return true here? After all,vfio_load_bufs_threaddidn't really fail, it just got signal to terminate itself.
The thread didn't succeed with loading all the data either, but got cancelled.
It's a similar logic as Glib's GIO returning G_IO_ERROR_CANCELLED if the operation got cancelled.
In a GTask a pending cancellation will even overwrite any other error or value that the task tried to return (at least by default).
Quoted 30 lines
Quoted 30 lines
hw/vfio/migration-multifd.c +30
-0+ } + + assert(multifd->load_buf_idx <= multifd->load_buf_idx_last); + + lb = vfio_load_state_buffer_get(multifd); + if (!lb) { + trace_vfio_load_state_device_buffer_starved(vbasedev->name, + multifd->load_buf_idx); + qemu_cond_wait(&multifd->load_bufs_buffer_ready_cond, + &multifd->load_bufs_mutex); + continue; + } + + if (multifd->load_buf_idx == multifd->load_buf_idx_last) { + break; + } + + if (multifd->load_buf_idx == 0) { + trace_vfio_load_state_device_buffer_start(vbasedev->name); + } + + if (!vfio_load_state_buffer_write(vbasedev, lb, errp)) { + ret = false; + goto ret_signal; + } + + if (multifd->load_buf_idx == multifd->load_buf_idx_last - 1) { + trace_vfio_load_state_device_buffer_end(vbasedev->name); + } +
Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 341
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ multifd->load_buf_idx++; + } + + config_ret = vfio_load_bufs_thread_load_config(vbasedev); + if (config_ret) { + error_setg(errp, "load config state failed: %d", config_ret);Let's add
vbasedev->nameto the error message so we know which device caused the error.
This line is not present anymore in the current version of the code,
but applied such change to all error_setg() calls in vfio_load_bufs_thread_load_config() instead.
Quoted 51 lines
Quoted 51 lines
hw/vfio/migration-multifd.c +41
-0+ ret = false; + } + +ret_signal: + /* + * Notify possibly waiting vfio_load_cleanup_load_bufs_thread() that + * this thread is exiting. + */ + multifd->load_bufs_thread_running = false; + qemu_cond_signal(&multifd->load_bufs_thread_finished_cond); + + return ret; +} + VFIOMultifd *vfio_multifd_new(void) { VFIOMultifd *multifd = g_new(VFIOMultifd, 1);@@ -191,11 +365,42 @@ VFIOMultifd *vfio_multifd_new(void) multifd->load_buf_idx_last = UINT32_MAX; qemu_cond_init(&multifd->load_bufs_buffer_ready_cond); + multifd->load_bufs_thread_running = false; + multifd->load_bufs_thread_want_exit = false; + qemu_cond_init(&multifd->load_bufs_thread_finished_cond); + return multifd; } +/* +* Terminates vfio_load_bufs_thread by setting +* multifd->load_bufs_thread_want_exit and signalling all the conditions +* the thread could be blocked on. +* +* Waits for the thread to signal that it had finished. +*/ +static void vfio_load_cleanup_load_bufs_thread (VFIOMultifd *multifd) +{ + /* The lock order is load_bufs_mutex -> BQL so unlock BQL here first */ + bql_unlock(); + WITH_QEMU_LOCK_GUARD(&multifd->load_bufs_mutex) { + while (multifd->load_bufs_thread_running) { + multifd->load_bufs_thread_want_exit = true; + + qemu_cond_signal(&multifd->load_bufs_buffer_ready_cond); + qemu_cond_wait(&multifd->load_bufs_thread_finished_cond, + &multifd->load_bufs_mutex); + } + } + bql_lock(); +} +
Quoted 8 lines
hw/vfio/migration-multifd.c VFIOMultifd *vfio_multifd_new(void) · line 403
Quoted 6 lines
hw/vfio/migration-multifd.c +4
-0void vfio_multifd_free(VFIOMultifd *multifd) { + vfio_load_cleanup_load_bufs_thread(multifd); + + qemu_cond_destroy(&multifd->load_bufs_thread_finished_cond); + vfio_state_buffers_destroy(&multifd->load_bufs);
vfio_state_buffers_destroy(&multifd->load_bufs); belongs to patch #26, no?
Yeah - moved it there.
Quoted 1 line
Thanks.
Thanks,
Maciej
Re: [PATCH v5 27/36] vfio/migration: Multifd device state transfer support - load thread
Quoted 149 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
Quoted 5 lines · Avihai Horon
Quoted 3 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
Quoted 8 lines
Quoted 1 line
From: "Maciej S. Szmigiero" <[email protected]>
Maybe add a sentence talking about the load thread itself first? E.g.:
Add a thread which loads the VFIO device state buffers that were received and via multifd.
Each VFIO device that has multifd device state transfer enabled has one such thread, which is created using migration core APIqemu_loadvm_start_load_thread().Since it's important to finish...
Added such leading text to the commit message for this patch.
Quoted 32 lines
Quoted 32 lines
Since it's important to finish loading device state transferred via the main migration channel (via
save_live_iterateSaveVMHandler) before starting loading the data asynchronously transferred via multifd the thread doing the actual loading of the multifd transferred data is only started fromswitchover_startSaveVMHandler.
switchover_starthandler is called whenMIG_CMD_SWITCHOVER_STARTsub-command ofQEMU_VM_COMMANDis received via the main migration channel.This sub-command is only sent after all
save_live_iteratedata have already been posted so it is safe to commence loading of the multifd-transferred device state upon receiving it - loading ofsave_live_iteratedata happens synchronously in the main migration thread (much like the processing ofMIG_CMD_SWITCHOVER_START) so by the timeMIG_CMD_SWITCHOVER_STARTis processed all the proceeding data must have already been loaded.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c| 225
++++++++++++++++++++++++++++++++++++
hw/vfio/migration-multifd.h| 2 +
hw/vfio/migration.c | 12 ++
hw/vfio/trace-events | 5 +4 files changed, 244 insertions(+)hw/vfio/migration-multifd.c +0
-0@@ -42,8 +42,13 @@ typedef struct VFIOStateBuffer { } VFIOStateBuffer;Quoted 4 lines
hw/vfio/migration-multifd.c typedef struct VFIOStateBuffer { · line 45
Quoted 2 lines
hw/vfio/migration-multifd.c +1
-0typedef struct VFIOMultifd { + QemuThread load_bufs_thread;This can be dropped.
Yeah - it was a remainder from pre-load-thread days of v2.
Dropped now.
Quoted 72 lines
Quoted 72 lines
hw/vfio/migration-multifd.c +4
-0+ bool load_bufs_thread_running; + bool load_bufs_thread_want_exit; + VFIOStateBuffers load_bufs; QemuCond load_bufs_buffer_ready_cond; + QemuCond load_bufs_thread_finished_cond;QemuMutex
load_bufs_mutex; /* Lock order: this lock -> BQL */uint32_tload_buf_idx;
uint32_tload_buf_idx_last;hw/vfio/migration-multifd.c +57
-0@@ -179,6 +184,175 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, return true; } +static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev) +{ + return -EINVAL; +} + +static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd) +{ + VFIOStateBuffer *lb; + guint bufs_len; + + bufs_len = vfio_state_buffers_size_get(&multifd->load_bufs); + if (multifd->load_buf_idx >= bufs_len) { + assert(multifd->load_buf_idx == bufs_len); + return NULL; + } + + lb = vfio_state_buffers_at(&multifd->load_bufs, + multifd->load_buf_idx); + if (!lb->is_present) { + return NULL; + } + + return lb; +} + +static bool vfio_load_state_buffer_write (VFIODevice *vbasedev, + VFIOStateBuffer *lb, + Error **errp) +{ + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + g_autofree char *buf = NULL; + char *buf_cur; + size_t buf_len; + + if (!lb->len) { + return true; + } + +trace_vfio_load_state_device_buffer_load_start (vbasedev->name, + multifd->load_buf_idx); + + /* lb might become re-allocated when we drop the lock */ + buf = g_steal_pointer(&lb->data); + buf_cur = buf; + buf_len = lb->len; + while (buf_len > 0) { + ssize_t wr_ret; + int errno_save; + + /* + * Loading data to the device takes a while, + * drop the lock during this process. + */ + qemu_mutex_unlock(&multifd->load_bufs_mutex); + wr_ret = write(migration->data_fd, buf_cur, buf_len); + errno_save = errno;Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 249
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ qemu_mutex_lock(&multifd->load_bufs_mutex); + + if (wr_ret < 0) { + error_setg(errp, + "writing state buffer %" PRIu32 " failed: %d", + multifd->load_buf_idx, errno_save);Let's add
vbasedev->nameto the error message so we know which device caused the error.Done.
Quoted 5 lines
Quoted 4 lines
hw/vfio/migration-multifd.c +4
-0+ return false; + } + + assert(wr_ret <= buf_len);
Quoted 6 lines
Quoted 1 line
I think this assert is redundant: we write
buf_lenbytes and by definition ofwrite()wr_retwill be <=buf_len.It's for catching when the "definition" for some reason does not match reality since this would result in a reading well past the buffer.
That's why it's an assert, not an error return.
Yes, but it's highly unlikely that write() will not match reality. But that's a minor, so whatever you prefer.
Quoted 65 lines
Quoted 56 lines
Quoted 56 lines
hw/vfio/migration-multifd.c +18
-0+ buf_len -= wr_ret; + buf_cur += wr_ret; + } + +trace_vfio_load_state_device_buffer_load_end (vbasedev->name, + multifd->load_buf_idx); + + return true; +} + +static bool vfio_load_bufs_thread_want_exit (VFIOMultifd *multifd, + bool *should_quit) +{ + return multifd->load_bufs_thread_want_exit ||qatomic_read (should_quit); +} + +/* +* This thread is spawned by vfio_multifd_switchover_start() which gets
+ * called upon encountering the switchover point marker in main migration
+ * stream.
+ *
+ * It exits after either:
+ * * completing loading the remaining device state and device config, OR:
+ * * encountering some error while doing the above, OR:
+ * * being forcefully aborted by the migration core by it settingshould_quit
+ * or byvfio_load_cleanup_load_bufs_thread()setting
+ *multifd->load_bufs_thread_want_exit.
+ */
+static boolvfio_load_bufs_thread(void *opaque, bool *should_quit, Error **errp)hw/vfio/migration-multifd.c +17
-0+{ + VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + bool ret = true; + int config_ret; + + assert(multifd); + QEMU_LOCK_GUARD(&multifd->load_bufs_mutex); + + assert(multifd->load_bufs_thread_running); + + while (true) { + VFIOStateBuffer *lb; + + /* +* Always check cancellation first after the buffer_ready wait below in
+ * case that cond was signalled byvfio_load_cleanup_load_bufs_thread().Quoted 8 lines
Quoted 5 lines
+ */
+ if (vfio_load_bufs_thread_want_exit(multifd, should_quit)) { +error_setg(errp, "operation cancelled");
+ ret = false;
+ gotoret_signal;IIUC, if
vfio_load_bufs_thread_want_exit()returns true, it means that some other code part already failed and set migration error, no? If so, shouldn't we return true here? After all,vfio_load_bufs_threaddidn't really fail, it just got signal to terminate itself.
Quoted 7 lines
The thread didn't succeed with loading all the data either, but got cancelled.
It's a similar logic as Glib's GIO returning
G_IO_ERROR_CANCELLEDif the operation got cancelled.In a GTask a pending cancellation will even overwrite any other error or value that the task tried to return (at least by default).
Ah I see.
I was looking on multifd_{send,recv}_thread and there they don't set an error if cancelled.
Anyway, what confused me is that we set an error here only so qemu_loadvm_load_thread() will try to migrate_set_error(), but that won't work because a migration error is already expected to be present. If that's indeed so, then to me it looks a bit redundant to set this error here.
Thanks.
PATCH v5 28/36 vfio/migration: Multifd device state transfer support - config loading support
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Load device config received via multifd using the existing machinery behind vfio_load_device_config_state().
Also, make sure to process the relevant main migration channel flags.
--- hw/vfio/migration-multifd.c | 47 ++++++++++++++++++++++++++++++++++- hw/vfio/migration.c | 8 +++++- include/hw/vfio/vfio-common.h | 2 ++ 3 files changed, 55 insertions(+), 2 deletions(-)
hw/vfio/migration-multifd.c +46 -1
@@ -15,6 +15,7 @@#include "qemu/lockable.h" #include "qemu/main-loop.h" #include "qemu/thread.h" +#include "io/channel-buffer.h" #include "migration/qemu-file.h" #include "migration-multifd.h" #include "trace.h"
@@ -186,7 +187,51 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size,static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev) { -return -EINVAL ; +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +VFIOStateBuffer *lb; +g_autoptr (QIOChannelBuffer) bioc =NULL ; +QEMUFile *f_out =NULL , *f_in =NULL ; +uint64_t mig_header; +int ret; + +assert (multifd->load_buf_idx == multifd->load_buf_idx_last); + lb =vfio_state_buffers_at (&multifd->load_bufs, multifd->load_buf_idx); +assert (lb->is_present); + + bioc =qio_channel_buffer_new (lb->len); +qio_channel_set_name (QIO_CHANNEL (bioc),"vfio-device-config-load" ); + + f_out =qemu_file_new_output (QIO_CHANNEL (bioc)); +qemu_put_buffer (f_out, (uint8_t *)lb->data, lb->len); + + ret =qemu_fflush (f_out); +if (ret) { +g_clear_pointer (&f_out, qemu_fclose); +return ret; + } + +qio_channel_io_seek (QIO_CHANNEL (bioc),0 ,0 ,NULL ); + f_in =qemu_file_new_input (QIO_CHANNEL (bioc)); + + mig_header =qemu_get_be64 (f_in); +if (mig_header !=VFIO_MIG_FLAG_DEV_CONFIG_STATE ) { +g_clear_pointer (&f_out, qemu_fclose); +g_clear_pointer (&f_in, qemu_fclose); +return -EINVAL ; + } + +bql_lock (); + ret =vfio_load_device_config_state (f_in, vbasedev); +bql_unlock (); + +g_clear_pointer (&f_out, qemu_fclose); +g_clear_pointer (&f_in, qemu_fclose); +if (ret <0 ) { +return ret; + } + +return 0 ; }static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd)
hw/vfio/migration.c +7 -1
@@ -264,7 +264,7 @@ static int vfio_save_device_config_state(QEMUFile *f, void *opaque,return ret; } -static int vfio_load_device_config_state (QEMUFile *f,void *opaque) +int vfio_load_device_config_state (QEMUFile *f,void *opaque) {VFIODevice *vbasedev = opaque;uint64_t data;
@@ -728,6 +728,12 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)switch (data) {case VFIO_MIG_FLAG_DEV_CONFIG_STATE : { +if (vfio_multifd_transfer_enabled (vbasedev)) { +error_report ("%s: got DEV_CONFIG_STATE but doing multifd transfer" , + vbasedev->name); +return -EINVAL ; + } +return vfio_load_device_config_state (f, opaque); }case VFIO_MIG_FLAG_DEV_SETUP_STATE :
include/hw/vfio/vfio-common.h +2 -0
@@ -298,6 +298,8 @@ void vfio_add_bytes_transferred(unsigned long val);bool vfio_device_state_is_running (VFIODevice *vbasedev);bool vfio_device_state_is_precopy (VFIODevice *vbasedev); +int vfio_load_device_config_state (QEMUFile *f,void *opaque); +#ifdef CONFIG_LINUX int vfio_get_region_info (VFIODevice *vbasedev,int index,struct vfio_region_info **info);
Re: [PATCH v5 28/36] vfio/migration: Multifd device state transfer support - config loading support
Quoted 27 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Load device config received via multifd using the existing machinery behind
vfio_load_device_config_state().Also, make sure to process the relevant main migration channel flags.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 47 ++++++++++++++++++++++++++++++++++- hw/vfio/migration.c | 8 +++++- include/hw/vfio/vfio-common.h | 2 ++ 3 files changed, 55 insertions(+), 2 deletions(-)hw/vfio/migration-multifd.c +1
-0@@ -15,6 +15,7 @@#include "qemu/lockable.h" #include "qemu/main-loop.h" #include "qemu/thread.h" +#include "io/channel-buffer.h" #include "migration/qemu-file.h" #include "migration-multifd.h" #include "trace.h"
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 188
Quoted 2 lines
hw/vfio/migration-multifd.c +0
-0@@ -186,7 +187,51 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size,static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev)
please modify to return a bool and take a "Error **errp" parameter.
Thanks,
C.
Quoted 90 lines
hw/vfio/migration-multifd.c +45
-1{ -return -EINVAL ; +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +VFIOStateBuffer *lb; +g_autoptr (QIOChannelBuffer) bioc =NULL ; +QEMUFile *f_out =NULL , *f_in =NULL ; +uint64_t mig_header; +int ret; + +assert (multifd->load_buf_idx == multifd->load_buf_idx_last); + lb =vfio_state_buffers_at (&multifd->load_bufs, multifd->load_buf_idx); +assert (lb->is_present); + + bioc =qio_channel_buffer_new (lb->len); +qio_channel_set_name (QIO_CHANNEL (bioc),"vfio-device-config-load" ); + + f_out =qemu_file_new_output (QIO_CHANNEL (bioc)); +qemu_put_buffer (f_out, (uint8_t *)lb->data, lb->len); + + ret =qemu_fflush (f_out); +if (ret) { +g_clear_pointer (&f_out, qemu_fclose); +return ret; + } + +qio_channel_io_seek (QIO_CHANNEL (bioc),0 ,0 ,NULL ); + f_in =qemu_file_new_input (QIO_CHANNEL (bioc)); + + mig_header =qemu_get_be64 (f_in); +if (mig_header !=VFIO_MIG_FLAG_DEV_CONFIG_STATE ) { +g_clear_pointer (&f_out, qemu_fclose); +g_clear_pointer (&f_in, qemu_fclose); +return -EINVAL ; + } + +bql_lock (); + ret =vfio_load_device_config_state (f_in, vbasedev); +bql_unlock (); + +g_clear_pointer (&f_out, qemu_fclose); +g_clear_pointer (&f_in, qemu_fclose); +if (ret <0 ) { +return ret; + } + +return 0 ; }static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd)hw/vfio/migration.c +7
-1@@ -264,7 +264,7 @@ static int vfio_save_device_config_state(QEMUFile *f, void *opaque,return ret; } -static int vfio_load_device_config_state (QEMUFile *f,void *opaque) +int vfio_load_device_config_state (QEMUFile *f,void *opaque) {VFIODevice *vbasedev = opaque;uint64_t data;@@ -728,6 +728,12 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)switch (data) {case VFIO_MIG_FLAG_DEV_CONFIG_STATE : { +if (vfio_multifd_transfer_enabled (vbasedev)) { +error_report ("%s: got DEV_CONFIG_STATE but doing multifd transfer" , + vbasedev->name); +return -EINVAL ; + } +return vfio_load_device_config_state (f, opaque); }case VFIO_MIG_FLAG_DEV_SETUP_STATE :include/hw/vfio/vfio-common.h +2
-0@@ -298,6 +298,8 @@ void vfio_add_bytes_transferred(unsigned long val);bool vfio_device_state_is_running (VFIODevice *vbasedev);bool vfio_device_state_is_precopy (VFIODevice *vbasedev); +int vfio_load_device_config_state (QEMUFile *f,void *opaque); +#ifdef CONFIG_LINUX int vfio_get_region_info (VFIODevice *vbasedev,int index,struct vfio_region_info **info);
Re: [PATCH v5 28/36] vfio/migration: Multifd device state transfer support - config loading support
Quoted 15 lines · Cédric Le Goater
Quoted 14 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Load device config received via multifd using the existing machinery behind
vfio_load_device_config_state().Also, make sure to process the relevant main migration channel flags.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 47 ++++++++++++++++++++++++++++++++++- hw/vfio/migration.c | 8 +++++-include/hw/vfio/vfio-common.h| 2 ++3 files changed, 55 insertions(+), 2 deletions(-)
Quoted 17 lines
Quoted 13 lines
hw/vfio/migration-multifd.c +1
-0@@ -15,6 +15,7 @@ #include "qemu/lockable.h" #include "qemu/main-loop.h" #include "qemu/thread.h" +#include "io/channel-buffer.h" #include "migration/qemu-file.h" #include "migration-multifd.h" #include "trace.h"hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 187
Quoted 1 line
hw/vfio/migration-multifd.c +0
-0@@ -186,7 +187,51 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, static int vfio_load_bufs_thread_load_config(VFIODevice *vbasedev)please modify to return a bool and take a "Error **errp" parameter.
Done.
Quoted 3 lines
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 28/36] vfio/migration: Multifd device state transfer support - config loading support
In reply to Maciej S. SzmigieroQuoted 30 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
Load device config received via multifd using the existing machinery behind
vfio_load_device_config_state().Also, make sure to process the relevant main migration channel flags.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 47 ++++++++++++++++++++++++++++++++++- hw/vfio/migration.c | 8 +++++- include/hw/vfio/vfio-common.h | 2 ++ 3 files changed, 55 insertions(+), 2 deletions(-)hw/vfio/migration-multifd.c +1
-0@@ -15,6 +15,7 @@#include "qemu/lockable.h" #include "qemu/main-loop.h" #include "qemu/thread.h" +#include "io/channel-buffer.h" #include "migration/qemu-file.h" #include "migration-multifd.h" #include "trace.h"
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 194
Quoted 9 lines
hw/vfio/migration-multifd.c +5
-1@@ -186,7 +187,51 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size,static int vfio_load_bufs_thread_load_config (VFIODevice *vbasedev) { -return -EINVAL ; +VFIOMigration *migration = vbasedev->migration; +VFIOMultifd *multifd = migration->multifd; +VFIOStateBuffer *lb; +g_autoptr (QIOChannelBuffer) bioc =NULL ; +QEMUFile *f_out =NULL , *f_in =NULL ;
Can we move patch #29 before this one and use g_autoptr() for f_out an f_in?
Quoted 57 lines
hw/vfio/migration-multifd.c +40
-0+uint64_t mig_header; +int ret; + +assert (multifd->load_buf_idx == multifd->load_buf_idx_last); + lb =vfio_state_buffers_at (&multifd->load_bufs, multifd->load_buf_idx); +assert (lb->is_present); + + bioc =qio_channel_buffer_new (lb->len); +qio_channel_set_name (QIO_CHANNEL (bioc),"vfio-device-config-load" ); + + f_out =qemu_file_new_output (QIO_CHANNEL (bioc)); +qemu_put_buffer (f_out, (uint8_t *)lb->data, lb->len); + + ret =qemu_fflush (f_out); +if (ret) { +g_clear_pointer (&f_out, qemu_fclose); +return ret; + } + +qio_channel_io_seek (QIO_CHANNEL (bioc),0 ,0 ,NULL ); + f_in =qemu_file_new_input (QIO_CHANNEL (bioc)); + + mig_header =qemu_get_be64 (f_in); +if (mig_header !=VFIO_MIG_FLAG_DEV_CONFIG_STATE ) { +g_clear_pointer (&f_out, qemu_fclose); +g_clear_pointer (&f_in, qemu_fclose); +return -EINVAL ; + } + +bql_lock (); + ret =vfio_load_device_config_state (f_in, vbasedev); +bql_unlock (); + +g_clear_pointer (&f_out, qemu_fclose); +g_clear_pointer (&f_in, qemu_fclose); +if (ret <0 ) { +return ret; + } + +return 0 ; }static VFIOStateBuffer *vfio_load_state_buffer_get (VFIOMultifd *multifd)hw/vfio/migration.c +1
-1@@ -264,7 +264,7 @@ static int vfio_save_device_config_state(QEMUFile *f, void *opaque,return ret; } -static int vfio_load_device_config_state (QEMUFile *f,void *opaque) +int vfio_load_device_config_state (QEMUFile *f,void *opaque) {VFIODevice *vbasedev = opaque;uint64_t data;
hw/vfio/migration.c static int vfio_load_state(QEMUFile *f, void *opaque, int version_id) · line 733
Quoted 6 lines
hw/vfio/migration.c +3
-0@@ -728,6 +728,12 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)switch (data) {case VFIO_MIG_FLAG_DEV_CONFIG_STATE : { +if (vfio_multifd_transfer_enabled (vbasedev)) { +error_report ("%s: got DEV_CONFIG_STATE but doing multifd transfer" , + vbasedev->name);
To make clearer, maybe change to:
"%s: got DEV_CONFIG_STATE in main migration channel but doing multifd transfer"
Thanks.
Quoted 19 lines
hw/vfio/migration.c +3
-0+return -EINVAL ; + } +return vfio_load_device_config_state (f, opaque); }case VFIO_MIG_FLAG_DEV_SETUP_STATE :include/hw/vfio/vfio-common.h +2
-0@@ -298,6 +298,8 @@ void vfio_add_bytes_transferred(unsigned long val);bool vfio_device_state_is_running (VFIODevice *vbasedev);bool vfio_device_state_is_precopy (VFIODevice *vbasedev); +int vfio_load_device_config_state (QEMUFile *f,void *opaque); +#ifdef CONFIG_LINUX int vfio_get_region_info (VFIODevice *vbasedev,int index,struct vfio_region_info **info);
Re: [PATCH v5 28/36] vfio/migration: Multifd device state transfer support - config loading support
Quoted 33 lines · Avihai Horon
Quoted 31 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
Load device config received via multifd using the existing machinery behind
vfio_load_device_config_state().Also, make sure to process the relevant main migration channel flags.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 47 ++++++++++++++++++++++++++++++++++- hw/vfio/migration.c | 8 +++++-include/hw/vfio/vfio-common.h| 2 ++3 files changed, 55 insertions(+), 2 deletions(-)hw/vfio/migration-multifd.c +1
-0@@ -15,6 +15,7 @@ #include "qemu/lockable.h" #include "qemu/main-loop.h" #include "qemu/thread.h" +#include "io/channel-buffer.h" #include "migration/qemu-file.h" #include "migration-multifd.h" #include "trace.h"@@ -186,7 +187,51 @@ bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size,
Quoted 10 lines
hw/vfio/migration-multifd.c bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, · line 194
Quoted 8 lines
hw/vfio/migration-multifd.c +5
-1static int vfio_load_bufs_thread_load_config(VFIODevice *vbasedev) { - return -EINVAL; + VFIOMigration *migration = vbasedev->migration; + VFIOMultifd *multifd = migration->multifd; + VFIOStateBuffer *lb; + g_autoptr(QIOChannelBuffer) bioc = NULL; + QEMUFile *f_out = NULL, *f_in = NULL;Can we move patch #29 before this one and use
g_autoptr()forf_outanf_in?
Sure, that's a good idea - done now.
Quoted 51 lines
Quoted 51 lines
hw/vfio/migration-multifd.c +40
-0+ uint64_t mig_header; + int ret; + + assert(multifd->load_buf_idx == multifd->load_buf_idx_last); + lb = vfio_state_buffers_at(&multifd->load_bufs, multifd->load_buf_idx); + assert(lb->is_present); + + bioc = qio_channel_buffer_new(lb->len); + qio_channel_set_name(QIO_CHANNEL(bioc), "vfio-device-config-load"); + + f_out = qemu_file_new_output(QIO_CHANNEL(bioc)); + qemu_put_buffer(f_out, (uint8_t *)lb->data, lb->len); + + ret = qemu_fflush(f_out); + if (ret) { + g_clear_pointer(&f_out, qemu_fclose); + return ret; + } + + qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL); + f_in = qemu_file_new_input(QIO_CHANNEL(bioc)); + + mig_header = qemu_get_be64(f_in); + if (mig_header != VFIO_MIG_FLAG_DEV_CONFIG_STATE) { + g_clear_pointer(&f_out, qemu_fclose); + g_clear_pointer(&f_in, qemu_fclose); + return -EINVAL; + } + + bql_lock(); + ret = vfio_load_device_config_state(f_in, vbasedev); + bql_unlock(); + + g_clear_pointer(&f_out, qemu_fclose); + g_clear_pointer(&f_in, qemu_fclose); + if (ret < 0) { + return ret; + } + + return 0; } static VFIOStateBuffer *vfio_load_state_buffer_get(VFIOMultifd *multifd)hw/vfio/migration.c +0
-0@@ -264,7 +264,7 @@ static int vfio_save_device_config_state(QEMUFile *f, void *opaque, return ret; }
Quoted 15 lines
Quoted 7 lines
hw/vfio/migration.c +1
-1-static int vfio_load_device_config_state (QEMUFile *f,void *opaque) +int vfio_load_device_config_state (QEMUFile *f,void *opaque) { VFIODevice *vbasedev = opaque; uint64_t data;@@ -728,6 +728,12 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id) switch (data) {Quoted 5 lines
case
VFIO_MIG_FLAG_DEV_CONFIG_STATE:
{
+ if (vfio_multifd_transfer_enabled(vbasedev)) {
+ error_report("%s: gotDEV_CONFIG_STATEbut doing multifd transfer", +vbasedev->name);To make clearer, maybe change to:
"%s: got DEV_CONFIG_STATE in main migration channel but doing multifd transfer"
That normally would be good idea, however we are already at 83 characters in this line here and will not fit that many more words to this string.
Quoted 1 line
Thanks.
Thanks,
Maciej
Re: [PATCH v5 28/36] vfio/migration: Multifd device state transfer support - config loading support
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
hw/vfio/migration.c +0
-0@@ -728,6 +728,12 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id) switch (data) {case
VFIO_MIG_FLAG_DEV_CONFIG_STATE:
{
+ if (vfio_multifd_transfer_enabled(vbasedev)) {
+ error_report("%s: gotDEV_CONFIG_STATEbut doing multifd transfer", +vbasedev->name);To make clearer, maybe change to:
"%s: got DEV_CONFIG_STATE in main migration channel but doing multifd transfer"
Quoted 2 lines
That normally would be good idea, however we are already at 83 characters in this line here and will not fit that many more words to this string.
The 80 characters "rule" is not strict. A clear error report is always good to have !
Thanks,
C.
Re: [PATCH v5 28/36] vfio/migration: Multifd device state transfer support - config loading support
Quoted 11 lines · Cédric Le Goater
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
hw/vfio/migration.c +0
-0@@ -728,6 +728,12 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id) switch (data) {case
VFIO_MIG_FLAG_DEV_CONFIG_STATE:
{
+ if (vfio_multifd_transfer_enabled(vbasedev)) {
+ error_report("%s: gotDEV_CONFIG_STATEbut doing multifd transfer", +vbasedev->name);To make clearer, maybe change to:
"%s: got DEV_CONFIG_STATE in main migration channel but doing multifd transfer"
Quoted 5 lines
Quoted 2 lines
That normally would be good idea, however we are already at 83 characters in this line here and will not fit that many more words to this string.
The 80 characters "rule" is not strict. A clear error report is always good to have !
Changed the message to the suggested by splitting that string into two to avoid going above 100 characters per line.
Quoted 5 lines
Thanks,
C.
Thanks,
Maciej
PATCH v5 29/36 migration/qemu-file: Define g_autoptr() cleanup function for QEMUFile
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Automatic memory management helps avoid memory safety issues.
--- migration/qemu-file.h | 2 ++ 1 file changed, 2 insertions(+)
migration/qemu-file.h +2 -0
@@ -33,6 +33,8 @@ QEMUFile *qemu_file_new_input(QIOChannel *ioc);QEMUFile *qemu_file_new_output (QIOChannel *ioc);int qemu_fclose (QEMUFile *f); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (QEMUFile, qemu_fclose) +/* * qemu_file_transferred: *
PATCH v5 30/36 vfio/migration: Multifd device state transfer support - send side
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Implement the multifd device state transfer via additional per-device thread inside save_live_complete_precopy_thread handler.
Switch between doing the data transfer in the new handler and doing it in the old save_state handler depending on the x-migration-multifd-transfer device property value.
--- hw/vfio/migration-multifd.c | 139 ++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 5 ++ hw/vfio/migration.c | 26 +++++-- hw/vfio/trace-events | 2 + include/hw/vfio/vfio-common.h | 8 ++ 5 files changed, 174 insertions(+), 6 deletions(-)
hw/vfio/migration-multifd.c +139 -0
@@ -476,6 +476,145 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp)return true; } +void vfio_multifd_emit_dummy_eos (VFIODevice *vbasedev,QEMUFile *f) +{ +assert (vfio_multifd_transfer_enabled (vbasedev)); + +/* +* Emit dummy NOP data on the main migration channel since the actual +* device state transfer is done via multifd channels. +*/ +qemu_put_be64 (f,VFIO_MIG_FLAG_END_OF_STATE ); +} + +static bool +vfio_save_complete_precopy_thread_config_state (VFIODevice *vbasedev, +char *idstr, +uint32_t instance_id, +uint32_t idx, +Error **errp) +{ +g_autoptr (QIOChannelBuffer) bioc =NULL ; +g_autoptr (QEMUFile) f =NULL ; +int ret; + g_autofree VFIODeviceStatePacket *packet = NULL; +size_t packet_len; + + bioc =qio_channel_buffer_new (0 ); +qio_channel_set_name (QIO_CHANNEL (bioc),"vfio-device-config-save" ); + + f =qemu_file_new_output (QIO_CHANNEL (bioc)); + +if (vfio_save_device_config_state (f, vbasedev, errp)) { +return false; + } + + ret =qemu_fflush (f); +if (ret) { +error_setg (errp,"save config state flush failed: %d" , ret); +return false; + } + + packet_len =sizeof (*packet) + bioc->usage; + packet =g_malloc0 (packet_len); + packet->version =VFIO_DEVICE_STATE_PACKET_VER_CURRENT ; + packet->idx = idx; + packet->flags =VFIO_DEVICE_STATE_CONFIG_STATE ; +memcpy (&packet->data, bioc->data, bioc->usage); + +if (!multifd_queue_device_state (idstr, instance_id, + (char *)packet, packet_len)) { +error_setg (errp,"multifd config data queuing failed" ); +return false; + } + +vfio_add_bytes_transferred (packet_len); + +return true; +} + +/* +* This thread is spawned by the migration core directly via +* .save_live_complete_precopy_thread SaveVMHandler. +* +* It exits after either: +* * completing saving the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by +* multifd_device_state_save_thread_should_exit() returning true. +*/ +bool vfio_save_complete_precopy_thread (SaveLiveCompletePrecopyThreadData *d, +Error **errp) +{ +VFIODevice *vbasedev = d->handler_opaque; +VFIOMigration *migration = vbasedev->migration; +bool ret; + g_autofree VFIODeviceStatePacket *packet = NULL; +uint32_t idx; + +if (!vfio_multifd_transfer_enabled (vbasedev)) { +/* Nothing to do, vfio_save_complete_precopy() does the transfer. */ +return true; + } + +trace_vfio_save_complete_precopy_thread_start (vbasedev->name, + d->idstr, d->instance_id); + +/* We reach here with device state STOP or STOP_COPY only */ +if (vfio_migration_set_state (vbasedev,VFIO_DEVICE_STATE_STOP_COPY , +VFIO_DEVICE_STATE_STOP , errp)) { + ret = false; + goto ret_finish; + } + + packet =g_malloc0 (sizeof (*packet) + migration->data_buffer_size); + packet->version =VFIO_DEVICE_STATE_PACKET_VER_CURRENT ; + +for (idx =0 ; ; idx++) { +ssize_t data_size; +size_t packet_size; + +if (multifd_device_state_save_thread_should_exit ()) { +error_setg (errp,"operation cancelled" ); + ret = false; + goto ret_finish; + } + + data_size =read (migration->data_fd, &packet->data, + migration->data_buffer_size); +if (data_size <0 ) { +error_setg (errp,"reading state buffer %" PRIu32" failed: %d" , + idx, errno); + ret = false; + goto ret_finish; + }else if (data_size ==0 ) { +break ; + } + + packet->idx = idx; + packet_size =sizeof (*packet) + data_size; + +if (!multifd_queue_device_state (d->idstr, d->instance_id, + (char *)packet, packet_size)) { +error_setg (errp,"multifd data queuing failed" ); + ret = false; + goto ret_finish; + } + +vfio_add_bytes_transferred (packet_size); + } + + ret =vfio_save_complete_precopy_thread_config_state (vbasedev, + d->idstr, + d->instance_id, + idx, errp); + +ret_finish: +trace_vfio_save_complete_precopy_thread_end (vbasedev->name, ret); + +return ret; +} +int vfio_multifd_switchover_start (VFIODevice *vbasedev) {VFIOMigration *migration = vbasedev->migration;
hw/vfio/migration-multifd.h +5 -0
@@ -25,6 +25,11 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp);bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size,Error **errp); +void vfio_multifd_emit_dummy_eos (VFIODevice *vbasedev,QEMUFile *f); + +bool vfio_save_complete_precopy_thread (SaveLiveCompletePrecopyThreadData *d, +Error **errp); +int vfio_multifd_switchover_start (VFIODevice *vbasedev);#endif
hw/vfio/migration.c +20 -6
@@ -120,10 +120,10 @@ static void vfio_migration_set_device_state(VFIODevice *vbasedev,vfio_migration_send_event (vbasedev); } -static int vfio_migration_set_state (VFIODevice *vbasedev, -enum vfio_device_mig_state new_state, -enum vfio_device_mig_state recover_state, -Error **errp) +int vfio_migration_set_state (VFIODevice *vbasedev, +enum vfio_device_mig_state new_state, +enum vfio_device_mig_state recover_state, +Error **errp) {VFIOMigration *migration = vbasedev->migration; uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +
@@ -238,8 +238,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev,return ret; } -static int vfio_save_device_config_state (QEMUFile *f,void *opaque, -Error **errp) +int vfio_save_device_config_state (QEMUFile *f,void *opaque,Error **errp) {VFIODevice *vbasedev = opaque;int ret;
@@ -453,6 +452,10 @@ static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp)uint64_t stop_copy_size =VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE ;int ret; +if (!vfio_multifd_transfer_setup (vbasedev, errp)) { +return -EINVAL ; + } +qemu_put_be64 (f,VFIO_MIG_FLAG_DEV_SETUP_STATE );vfio_query_stop_copy_size (vbasedev, &stop_copy_size);
@@ -631,6 +634,11 @@ static int vfio_save_complete_precopy(QEMUFile *f, void *opaque)int ret;Error *local_err =NULL ; +if (vfio_multifd_transfer_enabled (vbasedev)) { +vfio_multifd_emit_dummy_eos (vbasedev, f); +return 0 ; + } +trace_vfio_save_complete_precopy_start (vbasedev->name);/* We reach here with device state STOP or STOP_COPY only */
@@ -662,6 +670,11 @@ static void vfio_save_state(QEMUFile *f, void *opaque)Error *local_err =NULL ;int ret; +if (vfio_multifd_transfer_enabled (vbasedev)) { +vfio_multifd_emit_dummy_eos (vbasedev, f); +return ; + } + ret =vfio_save_device_config_state (f, opaque, &local_err);if (ret) { error_prepend(&local_err,
@@ -819,6 +832,7 @@ static const SaveVMHandlers savevm_vfio_handlers = { .is_active_iterate = vfio_is_active_iterate, .save_live_iterate = vfio_save_iterate, .save_live_complete_precopy = vfio_save_complete_precopy, + .save_live_complete_precopy_thread = vfio_save_complete_precopy_thread, .save_state = vfio_save_state, .load_setup = vfio_load_setup, .load_cleanup = vfio_load_cleanup,
hw/vfio/trace-events +2 -0
@@ -168,6 +168,8 @@ vfio_save_block_precopy_empty_hit(const char *name) " (%s)" vfio_save_cleanup(const char *name) " (%s)" vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d" vfio_save_complete_precopy_start(const char *name) " (%s)" +vfio_save_complete_precopy_thread_start(const char *name, const char *idstr, uint32_t instance_id) " (%s) idstr %s instance %"PRIu32 +vfio_save_complete_precopy_thread_end(const char *name, int ret) " (%s) ret %d" vfio_save_device_config_state(const char *name) " (%s)" vfio_save_iterate(const char *name, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy initial size %"PRIu64" precopy dirty size %"PRIu64 vfio_save_iterate_start(const char *name) " (%s)"
include/hw/vfio/vfio-common.h +8 -0
@@ -298,6 +298,14 @@ void vfio_add_bytes_transferred(unsigned long val);bool vfio_device_state_is_running (VFIODevice *vbasedev);bool vfio_device_state_is_precopy (VFIODevice *vbasedev); +#ifdef CONFIG_LINUX +int vfio_migration_set_state (VFIODevice *vbasedev, +enum vfio_device_mig_state new_state, +enum vfio_device_mig_state recover_state, +Error **errp); +#endif + +int vfio_save_device_config_state (QEMUFile *f,void *opaque,Error **errp);int vfio_load_device_config_state (QEMUFile *f,void *opaque);#ifdef CONFIG_LINUX
Re: [PATCH v5 30/36] vfio/migration: Multifd device state transfer support - send side
Quoted 85 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Implement the multifd device state transfer via additional per-device thread inside
save_live_complete_precopy_threadhandler.Switch between doing the data transfer in the new handler and doing it in the old
save_statehandler depending on the x-migration-multifd-transfer device property value.Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 139 ++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 5 ++ hw/vfio/migration.c | 26 +++++-- hw/vfio/trace-events | 2 + include/hw/vfio/vfio-common.h | 8 ++ 5 files changed, 174 insertions(+), 6 deletions(-)hw/vfio/migration-multifd.c +59
-0@@ -476,6 +476,145 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp)return true; } +void vfio_multifd_emit_dummy_eos (VFIODevice *vbasedev,QEMUFile *f) +{ +assert (vfio_multifd_transfer_enabled (vbasedev)); + +/* +* Emit dummy NOP data on the main migration channel since the actual +* device state transfer is done via multifd channels. +*/ +qemu_put_be64 (f,VFIO_MIG_FLAG_END_OF_STATE ); +} + +static bool +vfio_save_complete_precopy_thread_config_state (VFIODevice *vbasedev, +char *idstr, +uint32_t instance_id, +uint32_t idx, +Error **errp) +{ +g_autoptr (QIOChannelBuffer) bioc =NULL ; +g_autoptr (QEMUFile) f =NULL ; +int ret; + g_autofree VFIODeviceStatePacket *packet = NULL; +size_t packet_len; + + bioc =qio_channel_buffer_new (0 ); +qio_channel_set_name (QIO_CHANNEL (bioc),"vfio-device-config-save" ); + + f =qemu_file_new_output (QIO_CHANNEL (bioc)); + +if (vfio_save_device_config_state (f, vbasedev, errp)) { +return false; + } + + ret =qemu_fflush (f); +if (ret) { +error_setg (errp,"save config state flush failed: %d" , ret); +return false; + } + + packet_len =sizeof (*packet) + bioc->usage; + packet =g_malloc0 (packet_len); + packet->version =VFIO_DEVICE_STATE_PACKET_VER_CURRENT ; + packet->idx = idx; + packet->flags =VFIO_DEVICE_STATE_CONFIG_STATE ; +memcpy (&packet->data, bioc->data, bioc->usage); + +if (!multifd_queue_device_state (idstr, instance_id, + (char *)packet, packet_len)) { +error_setg (errp,"multifd config data queuing failed" ); +return false; + } + +vfio_add_bytes_transferred (packet_len); + +return true; +} + +/* +* This thread is spawned by the migration core directly via
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 547
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+* .save_live_complete_precopy_thread SaveVMHandler. +* +* It exits after either: +* * completing saving the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by +* multifd_device_state_save_thread_should_exit() returning true. +*/ +bool vfio_save_complete_precopy_thread (SaveLiveCompletePrecopyThreadData *d, +Error **errp)
In qemu_savevm_state_complete_precopy_iterable(), this handler is called :
....
if (multifd_device_state) {
QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
SaveLiveCompletePrecopyThreadHandler hdlr;
if (!se->ops || (in_postcopy && se->ops->has_postcopy &&
se->ops->has_postcopy(se->opaque)) ||
!se->ops->save_live_complete_precopy_thread) {
continue;
}
hdlr = se->ops->save_live_complete_precopy_thread;
multifd_spawn_device_state_save_thread(hdlr,
se->idstr, se->instance_id,
se->opaque);
}
}I suggest naming it : vfio_multifd_save_complete_precopy_thread()
Quoted 9 lines
hw/vfio/migration-multifd.c +9
-0+{ +VFIODevice *vbasedev = d->handler_opaque; +VFIOMigration *migration = vbasedev->migration; +bool ret; + g_autofree VFIODeviceStatePacket *packet = NULL; +uint32_t idx; + +if (!vfio_multifd_transfer_enabled (vbasedev)) { +/* Nothing to do, vfio_save_complete_precopy() does the transfer. */
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 566
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+return true; + } + +trace_vfio_save_complete_precopy_thread_start (vbasedev->name, + d->idstr, d->instance_id); + +/* We reach here with device state STOP or STOP_COPY only */ +if (vfio_migration_set_state (vbasedev,VFIO_DEVICE_STATE_STOP_COPY , +VFIO_DEVICE_STATE_STOP , errp)) { + ret = false;
These "ret = false" can be avoided if the variable is set at the top of the function.
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 567
Quoted 1 line
hw/vfio/migration-multifd.c +1
-0+ goto ret_finish;
goto thread_exit ?
Quoted 98 lines
hw/vfio/migration-multifd.c +49
-0+ } + + packet =g_malloc0 (sizeof (*packet) + migration->data_buffer_size); + packet->version =VFIO_DEVICE_STATE_PACKET_VER_CURRENT ; + +for (idx =0 ; ; idx++) { +ssize_t data_size; +size_t packet_size; + +if (multifd_device_state_save_thread_should_exit ()) { +error_setg (errp,"operation cancelled" ); + ret = false; + goto ret_finish; + }> + + data_size =read (migration->data_fd, &packet->data, + migration->data_buffer_size); +if (data_size <0 ) { +error_setg (errp,"reading state buffer %" PRIu32" failed: %d" , + idx, errno); + ret = false; + goto ret_finish; + }else if (data_size ==0 ) { +break ; + } + + packet->idx = idx; + packet_size =sizeof (*packet) + data_size; + +if (!multifd_queue_device_state (d->idstr, d->instance_id, + (char *)packet, packet_size)) { +error_setg (errp,"multifd data queuing failed" ); + ret = false; + goto ret_finish; + } + +vfio_add_bytes_transferred (packet_size); + } + + ret =vfio_save_complete_precopy_thread_config_state (vbasedev, + d->idstr, + d->instance_id, + idx, errp); + +ret_finish: +trace_vfio_save_complete_precopy_thread_end (vbasedev->name, ret); + +return ret; +} +int vfio_multifd_switchover_start (VFIODevice *vbasedev) {VFIOMigration *migration = vbasedev->migration;hw/vfio/migration-multifd.h +5
-0@@ -25,6 +25,11 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp);bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size,Error **errp); +void vfio_multifd_emit_dummy_eos (VFIODevice *vbasedev,QEMUFile *f); + +bool vfio_save_complete_precopy_thread (SaveLiveCompletePrecopyThreadData *d, +Error **errp); +int vfio_multifd_switchover_start (VFIODevice *vbasedev);#endif hw/vfio/migration.c +5
-6@@ -120,10 +120,10 @@ static void vfio_migration_set_device_state(VFIODevice *vbasedev,vfio_migration_send_event (vbasedev); } -static int vfio_migration_set_state (VFIODevice *vbasedev, -enum vfio_device_mig_state new_state, -enum vfio_device_mig_state recover_state, -Error **errp) +int vfio_migration_set_state (VFIODevice *vbasedev, +enum vfio_device_mig_state new_state, +enum vfio_device_mig_state recover_state, +Error **errp) {VFIOMigration *migration = vbasedev->migration; uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +@@ -238,8 +238,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev,return ret; } -static int vfio_save_device_config_state (QEMUFile *f,void *opaque, -Error **errp) +int vfio_save_device_config_state (QEMUFile *f,void *opaque,Error **errp) {VFIODevice *vbasedev = opaque;int ret;
hw/vfio/migration.c static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp) · line 458
Quoted 7 lines
hw/vfio/migration.c +4
-0@@ -453,6 +452,10 @@ static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp)uint64_t stop_copy_size =VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE ;int ret; +if (!vfio_multifd_transfer_setup (vbasedev, errp)) { +return -EINVAL ; + } +
please move to another patch with the similar change of patch 25.
Quoted 53 lines
hw/vfio/migration.c +11
-0qemu_put_be64 (f,VFIO_MIG_FLAG_DEV_SETUP_STATE );vfio_query_stop_copy_size (vbasedev, &stop_copy_size);@@ -631,6 +634,11 @@ static int vfio_save_complete_precopy(QEMUFile *f, void *opaque)int ret;Error *local_err =NULL ; +if (vfio_multifd_transfer_enabled (vbasedev)) { +vfio_multifd_emit_dummy_eos (vbasedev, f); +return 0 ; + } +trace_vfio_save_complete_precopy_start (vbasedev->name);/* We reach here with device state STOP or STOP_COPY only */ @@ -662,6 +670,11 @@ static void vfio_save_state(QEMUFile *f, void *opaque)Error *local_err =NULL ;int ret; +if (vfio_multifd_transfer_enabled (vbasedev)) { +vfio_multifd_emit_dummy_eos (vbasedev, f); +return ; + } + ret =vfio_save_device_config_state (f, opaque, &local_err);if (ret) { error_prepend(&local_err,@@ -819,6 +832,7 @@ static const SaveVMHandlers savevm_vfio_handlers = { .is_active_iterate = vfio_is_active_iterate, .save_live_iterate = vfio_save_iterate, .save_live_complete_precopy = vfio_save_complete_precopy, + .save_live_complete_precopy_thread = vfio_save_complete_precopy_thread, .save_state = vfio_save_state, .load_setup = vfio_load_setup, .load_cleanup = vfio_load_cleanup,hw/vfio/trace-events +2
-0@@ -168,6 +168,8 @@ vfio_save_block_precopy_empty_hit(const char *name) " (%s)" vfio_save_cleanup(const char *name) " (%s)" vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d" vfio_save_complete_precopy_start(const char *name) " (%s)" +vfio_save_complete_precopy_thread_start(const char *name, const char *idstr, uint32_t instance_id) " (%s) idstr %s instance %"PRIu32 +vfio_save_complete_precopy_thread_end(const char *name, int ret) " (%s) ret %d" vfio_save_device_config_state(const char *name) " (%s)" vfio_save_iterate(const char *name, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy initial size %"PRIu64" precopy dirty size %"PRIu64 vfio_save_iterate_start(const char *name) " (%s)"include/hw/vfio/vfio-common.h +0
-0
include/hw/vfio/vfio-common.h void vfio_add_bytes_transferred(unsigned long val); · line 305
Quoted 8 lines
include/hw/vfio/vfio-common.h +5
-0@@ -298,6 +298,14 @@ void vfio_add_bytes_transferred(unsigned long val);bool vfio_device_state_is_running (VFIODevice *vbasedev);bool vfio_device_state_is_precopy (VFIODevice *vbasedev); +#ifdef CONFIG_LINUX +int vfio_migration_set_state (VFIODevice *vbasedev, +enum vfio_device_mig_state new_state, +enum vfio_device_mig_state recover_state, +Error **errp);
please move below with the other declarations under #ifdef CONFIG_LINUX.
include/hw/vfio/vfio-common.h void vfio_add_bytes_transferred(unsigned long val); · line 311
Quoted 7 lines
include/hw/vfio/vfio-common.h +3
-0+#endif + +int vfio_save_device_config_state (QEMUFile *f,void *opaque,Error **errp);int vfio_load_device_config_state (QEMUFile *f,void *opaque);#ifdef CONFIG_LINUX
Thanks,
C.
Re: [PATCH v5 30/36] vfio/migration: Multifd device state transfer support - send side
Quoted 118 lines · Cédric Le Goater
Quoted 84 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Implement the multifd device state transfer via additional per-device thread inside
save_live_complete_precopy_threadhandler.Switch between doing the data transfer in the new handler and doing it in the old
save_statehandler depending on the x-migration-multifd-transfer device property value.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 139 ++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 5 ++
hw/vfio/migration.c | 26 +++++-- hw/vfio/trace-events | 2 +include/hw/vfio/vfio-common.h| 8 ++5 files changed, 174 insertions(+), 6 deletions(-)hw/vfio/migration-multifd.c +59
-0@@ -476,6 +476,145 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) return true; } +void vfio_multifd_emit_dummy_eos (VFIODevice *vbasedev,QEMUFile *f) +{ + assert(vfio_multifd_transfer_enabled(vbasedev)); + + /* + * Emit dummy NOP data on the main migration channel since the actual + * device state transfer is done via multifd channels. + */ + qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE); +} + +static bool +vfio_save_complete_precopy_thread_config_state (VFIODevice *vbasedev, + char *idstr, + uint32_t instance_id, + uint32_t idx, + Error **errp) +{ + g_autoptr(QIOChannelBuffer) bioc = NULL; + g_autoptr(QEMUFile) f = NULL; + int ret; + g_autofree VFIODeviceStatePacket *packet = NULL; + size_t packet_len; + + bioc = qio_channel_buffer_new(0); + qio_channel_set_name(QIO_CHANNEL(bioc), "vfio-device-config-save"); + + f = qemu_file_new_output(QIO_CHANNEL(bioc)); + + if (vfio_save_device_config_state(f, vbasedev, errp)) { + return false; + } + + ret = qemu_fflush(f); + if (ret) { + error_setg(errp, "save config state flush failed: %d", ret); + return false; + } + + packet_len = sizeof(*packet) + bioc->usage; + packet = g_malloc0(packet_len); + packet->version = VFIO_DEVICE_STATE_PACKET_VER_CURRENT; + packet->idx = idx; + packet->flags = VFIO_DEVICE_STATE_CONFIG_STATE; + memcpy(&packet->data, bioc->data, bioc->usage); + + if (!multifd_queue_device_state(idstr, instance_id, + (char *)packet, packet_len)) { + error_setg(errp, "multifd config data queuing failed"); + return false; + } + + vfio_add_bytes_transferred(packet_len); + + return true; +} + +/* +* This thread is spawned by the migration core directly via hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 546
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+* .save_live_complete_precopy_thread SaveVMHandler. +* +* It exits after either: +* * completing saving the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by +* multifd_device_state_save_thread_should_exit() returning true. +*/ +bool vfio_save_complete_precopy_thread (SaveLiveCompletePrecopyThreadData *d, + Error **errp)In
qemu_savevm_state_complete_precopy_iterable(), this handler is called :....
if (multifd_device_state) {QTAILQ_FOREACH(se, &savevm_state.handlers, entry){ SaveLiveCompletePrecopyThreadHandler hdlr;if (!se->ops || (
in_postcopy&&se->ops->has_postcopy&&se->ops->has_postcopy(se->opaque)) || !se->ops->save_live_complete_precopy_thread) { continue;
}hdlr =
se->ops->save_live_complete_precopy_thread;multifd_spawn_device_state_save_thread(hdlr, se->idstr, se->instance_id, se->opaque);
}
}
Quoted 1 line
I suggest naming it :
vfio_multifd_save_complete_precopy_thread()
Renamed accordingly.
Quoted 14 lines
Quoted 14 lines
hw/vfio/migration-multifd.c +14
-0+{ + VFIODevice *vbasedev = d->handler_opaque; + VFIOMigration *migration = vbasedev->migration; + bool ret; + g_autofree VFIODeviceStatePacket *packet = NULL; + uint32_t idx; + + if (!vfio_multifd_transfer_enabled(vbasedev)) { + /* Nothing to do, vfio_save_complete_precopy() does the transfer. */ + return true; + } + + trace_vfio_save_complete_precopy_thread_start(vbasedev->name, + d->idstr, d->instance_id);
Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 566
Quoted 5 lines
hw/vfio/migration-multifd.c +5
-0+ + /* We reach here with device state STOP or STOP_COPY only */ + if (vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP_COPY, + VFIO_DEVICE_STATE_STOP, errp)) { + ret = false;These "ret = false" can be avoided if the variable is set at the top of the function.
I inverted the "ret" logic here as in vfio_load_bufs_thread() to make it false by default and set to true just before early exit label.
Quoted 4 lines
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 566
Quoted 1 line
hw/vfio/migration-multifd.c +1
-0+ goto ret_finish;goto
thread_exit?
As I asked in one of the previous patches,
do this comment mean that your want to rename ret_finish label to thread_exit?
Quoted 70 lines
Quoted 70 lines
hw/vfio/migration-multifd.c +49
-0+ } + + packet = g_malloc0(sizeof(*packet) + migration->data_buffer_size); + packet->version = VFIO_DEVICE_STATE_PACKET_VER_CURRENT; + + for (idx = 0; ; idx++) { + ssize_t data_size; + size_t packet_size; + + if (multifd_device_state_save_thread_should_exit()) { + error_setg(errp, "operation cancelled"); + ret = false; + goto ret_finish; + }> + + data_size = read(migration->data_fd, &packet->data, + migration->data_buffer_size); + if (data_size < 0) { + error_setg(errp, "reading state buffer %" PRIu32 " failed: %d", + idx, errno); + ret = false; + goto ret_finish; + } else if (data_size == 0) { + break; + } + + packet->idx = idx; + packet_size = sizeof(*packet) + data_size; + + if (!multifd_queue_device_state(d->idstr, d->instance_id, + (char *)packet, packet_size)) { + error_setg(errp, "multifd data queuing failed"); + ret = false; + goto ret_finish; + } + + vfio_add_bytes_transferred(packet_size); + } + + ret = vfio_save_complete_precopy_thread_config_state(vbasedev, + d->idstr, + d->instance_id, + idx, errp); + +ret_finish: + trace_vfio_save_complete_precopy_thread_end(vbasedev->name, ret); + + return ret; +} + int vfio_multifd_switchover_start(VFIODevice *vbasedev) { VFIOMigration *migration = vbasedev->migration;hw/vfio/migration-multifd.h +5
-0@@ -25,6 +25,11 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp); bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, Error **errp); +void vfio_multifd_emit_dummy_eos (VFIODevice *vbasedev,QEMUFile *f); + +bool vfio_save_complete_precopy_thread (SaveLiveCompletePrecopyThreadData *d, + Error **errp); + int vfio_multifd_switchover_start(VFIODevice *vbasedev); #endifhw/vfio/migration.c +0
-0
(..)
Quoted 10 lines
Quoted 10 lines
hw/vfio/migration.c +1
-2@@ -238,8 +238,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev, return ret; } -static int vfio_save_device_config_state (QEMUFile *f,void *opaque, - Error **errp) +int vfio_save_device_config_state (QEMUFile *f,void *opaque,Error **errp) { VFIODevice *vbasedev = opaque; int ret;@@ -453,6 +452,10 @@ static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp)
Quoted 9 lines
hw/vfio/migration.c static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp) · line 458
Quoted 6 lines
hw/vfio/migration.c +4
-0uint64_t stop_copy_size = VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE; int ret; + if (!vfio_multifd_transfer_setup(vbasedev, errp)) { + return -EINVAL; + } +please move to another patch with the similar change of patch 25.
This patch is about the send/save side while patch 25 is called "*receive* init/cleanup".
So adding save setup to patch called "receive init" wouldn't be consistent with that patch subject.
Quoted 2 lines
Quoted 2 lines
hw/vfio/migration.c +0
-0qemu_put_be64(f, VFIO_MIG_FLAG_DEV_SETUP_STATE); vfio_query_stop_copy_size(vbasedev, &stop_copy_size);
(..)
Quoted 14 lines
include/hw/vfio/vfio-common.h void vfio_add_bytes_transferred(unsigned long val); · line 304
Quoted 11 lines
index
ce2bdea8a2c2..ba851917f9fc100644include/hw/vfio/vfio-common.h +5
-0@@ -298,6 +298,14 @@ void vfio_add_bytes_transferred(unsigned long val); bool vfio_device_state_is_running(VFIODevice *vbasedev); bool vfio_device_state_is_precopy(VFIODevice *vbasedev); +#ifdef CONFIG_LINUX +int vfio_migration_set_state (VFIODevice *vbasedev, + enum vfio_device_mig_state new_state, + enum vfio_device_mig_state recover_state, + Error **errp);please move below with the other declarations under #ifdef
CONFIG_LINUX.
Quoted 7 lines
include/hw/vfio/vfio-common.h void vfio_add_bytes_transferred(unsigned long val);
Quoted 6 lines
include/hw/vfio/vfio-common.h +3
-0+#endif + +int vfio_save_device_config_state (QEMUFile *f,void *opaque,Error **errp); int vfio_load_device_config_state(QEMUFile *f, void *opaque); #ifdef CONFIG_LINUX
Done.
Quoted 4 lines
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 30/36] vfio/migration: Multifd device state transfer support - send side
Quoted 146 lines · Maciej S. Szmigiero
Quoted 118 lines · Cédric Le Goater
Quoted 84 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Implement the multifd device state transfer via additional per-device thread inside
save_live_complete_precopy_threadhandler.Switch between doing the data transfer in the new handler and doing it in the old
save_statehandler depending on the x-migration-multifd-transfer device property value.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 139 ++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 5 ++
hw/vfio/migration.c | 26 +++++-- hw/vfio/trace-events | 2 +include/hw/vfio/vfio-common.h| 8 ++5 files changed, 174 insertions(+), 6 deletions(-)hw/vfio/migration-multifd.c +59
-0@@ -476,6 +476,145 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) return true; } +void vfio_multifd_emit_dummy_eos (VFIODevice *vbasedev,QEMUFile *f) +{ + assert(vfio_multifd_transfer_enabled(vbasedev)); + + /* + * Emit dummy NOP data on the main migration channel since the actual + * device state transfer is done via multifd channels. + */ + qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE); +} + +static bool +vfio_save_complete_precopy_thread_config_state (VFIODevice *vbasedev, + char *idstr, + uint32_t instance_id, + uint32_t idx, + Error **errp) +{ + g_autoptr(QIOChannelBuffer) bioc = NULL; + g_autoptr(QEMUFile) f = NULL; + int ret; + g_autofree VFIODeviceStatePacket *packet = NULL; + size_t packet_len; + + bioc = qio_channel_buffer_new(0); + qio_channel_set_name(QIO_CHANNEL(bioc), "vfio-device-config-save"); + + f = qemu_file_new_output(QIO_CHANNEL(bioc)); + + if (vfio_save_device_config_state(f, vbasedev, errp)) { + return false; + } + + ret = qemu_fflush(f); + if (ret) { + error_setg(errp, "save config state flush failed: %d", ret); + return false; + } + + packet_len = sizeof(*packet) + bioc->usage; + packet = g_malloc0(packet_len); + packet->version = VFIO_DEVICE_STATE_PACKET_VER_CURRENT; + packet->idx = idx; + packet->flags = VFIO_DEVICE_STATE_CONFIG_STATE; + memcpy(&packet->data, bioc->data, bioc->usage); + + if (!multifd_queue_device_state(idstr, instance_id, + (char *)packet, packet_len)) { + error_setg(errp, "multifd config data queuing failed"); + return false; + } + + vfio_add_bytes_transferred(packet_len); + + return true; +} + +/* +* This thread is spawned by the migration core directly via hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 546
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+* .save_live_complete_precopy_thread SaveVMHandler. +* +* It exits after either: +* * completing saving the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by +* multifd_device_state_save_thread_should_exit() returning true. +*/ +bool vfio_save_complete_precopy_thread (SaveLiveCompletePrecopyThreadData *d, + Error **errp)In
qemu_savevm_state_complete_precopy_iterable(), this handler is called :....
if (multifd_device_state) {QTAILQ_FOREACH(se, &savevm_state.handlers, entry){ SaveLiveCompletePrecopyThreadHandler hdlr;if (!se->ops || (
in_postcopy&&se->ops->has_postcopy&&se->ops->has_postcopy(se->opaque)) || !se->ops->save_live_complete_precopy_thread) { continue;
}hdlr =
se->ops->save_live_complete_precopy_thread;multifd_spawn_device_state_save_thread(hdlr, se->idstr, se->instance_id, se->opaque);
}
}Quoted 1 line
I suggest naming it :
vfio_multifd_save_complete_precopy_thread()Renamed accordingly.
Quoted 14 lines
Quoted 14 lines
hw/vfio/migration-multifd.c +14
-0+{ + VFIODevice *vbasedev = d->handler_opaque; + VFIOMigration *migration = vbasedev->migration; + bool ret; + g_autofree VFIODeviceStatePacket *packet = NULL; + uint32_t idx; + + if (!vfio_multifd_transfer_enabled(vbasedev)) { + /* Nothing to do, vfio_save_complete_precopy() does the transfer. */ + return true; + } + + trace_vfio_save_complete_precopy_thread_start(vbasedev->name, + d->idstr, d->instance_id);Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 566
Quoted 5 lines
hw/vfio/migration-multifd.c +5
-0+ + /* We reach here with device state STOP or STOP_COPY only */ + if (vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP_COPY, + VFIO_DEVICE_STATE_STOP, errp)) { + ret = false;These "ret = false" can be avoided if the variable is set at the top of the function.
Quoted 3 lines
I inverted the "ret" logic here as in
vfio_load_bufs_thread()to make it false by default and set to true just before early exit label.
ok. Let's see what it looks like in v6.
Quoted 8 lines
Quoted 4 lines
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 566
Quoted 1 line
hw/vfio/migration-multifd.c +1
-0+ goto ret_finish;goto
thread_exit?As I asked in one of the previous patches,
do this comment mean that your want to renameret_finishlabel tothread_exit?
Yes. I find label 'thread_exit' more meaning full. This is minor since there is only one 'exit' label.
Quoted 92 lines
Quoted 71 lines
Quoted 71 lines
hw/vfio/migration-multifd.c +49
-0+ } + + packet = g_malloc0(sizeof(*packet) + migration->data_buffer_size); + packet->version = VFIO_DEVICE_STATE_PACKET_VER_CURRENT; + + for (idx = 0; ; idx++) { + ssize_t data_size; + size_t packet_size; + + if (multifd_device_state_save_thread_should_exit()) { + error_setg(errp, "operation cancelled"); + ret = false; + goto ret_finish; + }> + + data_size = read(migration->data_fd, &packet->data, + migration->data_buffer_size); + if (data_size < 0) { + error_setg(errp, "reading state buffer %" PRIu32 " failed: %d", + idx, errno); + ret = false; + goto ret_finish; + } else if (data_size == 0) { + break; + } + + packet->idx = idx; + packet_size = sizeof(*packet) + data_size; + + if (!multifd_queue_device_state(d->idstr, d->instance_id, + (char *)packet, packet_size)) { + error_setg(errp, "multifd data queuing failed"); + ret = false; + goto ret_finish; + } + + vfio_add_bytes_transferred(packet_size); + } + + ret = vfio_save_complete_precopy_thread_config_state(vbasedev, + d->idstr, + d->instance_id, + idx, errp); + +ret_finish: + trace_vfio_save_complete_precopy_thread_end(vbasedev->name, ret); + + return ret; +} + int vfio_multifd_switchover_start(VFIODevice *vbasedev) { VFIOMigration *migration = vbasedev->migration;hw/vfio/migration-multifd.h +5
-0@@ -25,6 +25,11 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp); bool vfio_load_state_buffer(void *opaque, char *data, size_t data_size, Error **errp); +void vfio_multifd_emit_dummy_eos (VFIODevice *vbasedev,QEMUFile *f); + +bool vfio_save_complete_precopy_thread (SaveLiveCompletePrecopyThreadData *d, + Error **errp); + int vfio_multifd_switchover_start(VFIODevice *vbasedev); #endifhw/vfio/migration.c +0
-0(..)
Quoted 10 lines
Quoted 10 lines
hw/vfio/migration.c +1
-2@@ -238,8 +238,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev, return ret; } -static int vfio_save_device_config_state (QEMUFile *f,void *opaque, - Error **errp) +int vfio_save_device_config_state (QEMUFile *f,void *opaque,Error **errp) { VFIODevice *vbasedev = opaque; int ret;@@ -453,6 +452,10 @@ static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp)Quoted 9 lines
hw/vfio/migration.c static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp) · line 458
Quoted 6 lines
hw/vfio/migration.c +4
-0uint64_t stop_copy_size = VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE; int ret; + if (!vfio_multifd_transfer_setup(vbasedev, errp)) { + return -EINVAL; + } +please move to another patch with the similar change of patch 25.
Quoted 5 lines
This patch is about the send/save side while patch 25 is called "*receive* init/cleanup".
So adding save setup to patch called "receive init" wouldn't be consistent with that patch subject.
In that case, could please add an extra patch checking for the consistency of the settings ?
Thanks,
C.
Quoted 37 lines
Quoted 3 lines
Quoted 3 lines
hw/vfio/migration.c +0
-0qemu_put_be64(f, VFIO_MIG_FLAG_DEV_SETUP_STATE); vfio_query_stop_copy_size(vbasedev, &stop_copy_size);(..)
Quoted 14 lines
include/hw/vfio/vfio-common.h void vfio_add_bytes_transferred(unsigned long val); · line 304
Quoted 11 lines
index
ce2bdea8a2c2..ba851917f9fc100644include/hw/vfio/vfio-common.h +5
-0@@ -298,6 +298,14 @@ void vfio_add_bytes_transferred(unsigned long val); bool vfio_device_state_is_running(VFIODevice *vbasedev); bool vfio_device_state_is_precopy(VFIODevice *vbasedev); +#ifdef CONFIG_LINUX +int vfio_migration_set_state (VFIODevice *vbasedev, + enum vfio_device_mig_state new_state, + enum vfio_device_mig_state recover_state, + Error **errp);please move below with the other declarations under #ifdef
CONFIG_LINUX.Quoted 7 lines
include/hw/vfio/vfio-common.h void vfio_add_bytes_transferred(unsigned long val);
Quoted 6 lines
include/hw/vfio/vfio-common.h +3
-0+#endif + +int vfio_save_device_config_state (QEMUFile *f,void *opaque,Error **errp); int vfio_load_device_config_state(QEMUFile *f, void *opaque); #ifdef CONFIG_LINUXDone.
Quoted 4 lines
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 30/36] vfio/migration: Multifd device state transfer support - send side
Quoted 25 lines · Cédric Le Goater
Quoted 24 lines · Maciej S. Szmigiero
Quoted 23 lines · Cédric Le Goater
Quoted 22 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
Implement the multifd device state transfer via additional per-device thread inside
save_live_complete_precopy_threadhandler.Switch between doing the data transfer in the new handler and doing it in the old
save_statehandler depending on the x-migration-multifd-transfer device property value.Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 139 ++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 5 ++
hw/vfio/migration.c | 26 +++++-- hw/vfio/trace-events | 2 +include/hw/vfio/vfio-common.h| 8 ++5 files changed, 174 insertions(+), 6 deletions(-)hw/vfio/migration-multifd.c +0
-0
(..)
Quoted 32 lines
Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
hw/vfio/migration-multifd.c +14
-0+{ + VFIODevice *vbasedev = d->handler_opaque; + VFIOMigration *migration = vbasedev->migration; + bool ret; + g_autofree VFIODeviceStatePacket *packet = NULL; + uint32_t idx; + + if (!vfio_multifd_transfer_enabled(vbasedev)) { + /* Nothing to do, vfio_save_complete_precopy() does the transfer. */ + return true; + } + + trace_vfio_save_complete_precopy_thread_start(vbasedev->name, + d->idstr, d->instance_id);Quoted 8 lines
Quoted 5 lines
+
+ /* We reach here with device state STOP orSTOP_COPYonly */ + if (vfio_migration_set_state(vbasedev,VFIO_DEVICE_STATE_STOP_COPY, +VFIO_DEVICE_STATE_STOP, errp)) { + ret = false;These "ret = false" can be avoided if the variable is set at the top of the function.
Quoted 3 lines
I inverted the "ret" logic here as in
vfio_load_bufs_thread()to make it false by default and set to true just before early exit label.ok. Let's see what it looks like in v6.
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
+ goto
ret_finish;
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
goto
thread_exit?As I asked in one of the previous patches,
do this comment mean that your want to renameret_finishlabel tothread_exit?Yes. I find label '
thread_exit' more meaning full. This is minor since there is only one 'exit' label.
Renamed ret_finish to thread_exit then.
Quoted 2 lines
Quoted 2 lines
(..)
Quoted 25 lines
Quoted 25 lines
Quoted 4 lines
Quoted 4 lines
hw/vfio/migration.c +0
-0(..)
Quoted 19 lines
Quoted 10 lines
hw/vfio/migration.c +1
-2@@ -238,8 +238,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev, return ret; } -static int vfio_save_device_config_state (QEMUFile *f,void *opaque, - Error **errp) +int vfio_save_device_config_state (QEMUFile *f,void *opaque,Error **errp) { VFIODevice *vbasedev = opaque; int ret;hw/vfio/migration.c static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp) · line 457
Quoted 6 lines
hw/vfio/migration.c +4
-0@@ -453,6 +452,10 @@ static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp) uint64_t stop_copy_size = VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE; int ret; + if (!vfio_multifd_transfer_setup(vbasedev, errp)) { + return -EINVAL; + } +please move to another patch with the similar change of patch 25.
Quoted 8 lines
Quoted 5 lines
This patch is about the send/save side while patch 25 is called "*receive* init/cleanup".
So adding save setup to patch called "receive init" wouldn't be consistent with that patch subject.
In that case, could please add an extra patch checking for the consistency of the settings ?
I split out wiring vfio_multifd_setup() and vfio_multifd_cleanup() into general VFIO load/save setup and cleanup methods from this patch and patch "Multifd device state transfer support - receive init/cleanup" into a brand new patch/commit.
By the way, due to changes discussed over the last two days vfio_multifd_setup() (aka vfio_multifd_transfer_setup()) not only does consistency checking but also allocates VFIOMultifd:
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 7 lines
Thanks,
C.
Thanks,
Maciej
Re: [PATCH v5 30/36] vfio/migration: Multifd device state transfer support - send side
In reply to Maciej S. SzmigieroQuoted 11 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
Implement the multifd device state transfer via additional per-device thread inside
save_live_complete_precopy_threadhandler.Switch between doing the data transfer in the new handler and doing it in the old
save_statehandler depending on the x-migration-multifd-transfer device property value.
x-migration-multifd-transfer is not yet introduced. Maybe rephrase to:
... depending if VFIO multifd transfer is enabled or not.
Quoted 44 lines
Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 139 ++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 5 ++ hw/vfio/migration.c | 26 +++++-- hw/vfio/trace-events | 2 + include/hw/vfio/vfio-common.h | 8 ++ 5 files changed, 174 insertions(+), 6 deletions(-)hw/vfio/migration-multifd.c +26
-0@@ -476,6 +476,145 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp)return true; } +void vfio_multifd_emit_dummy_eos (VFIODevice *vbasedev,QEMUFile *f) +{ +assert (vfio_multifd_transfer_enabled (vbasedev)); + +/* +* Emit dummy NOP data on the main migration channel since the actual +* device state transfer is done via multifd channels. +*/ +qemu_put_be64 (f,VFIO_MIG_FLAG_END_OF_STATE ); +} + +static bool +vfio_save_complete_precopy_thread_config_state (VFIODevice *vbasedev, +char *idstr, +uint32_t instance_id, +uint32_t idx, +Error **errp) +{ +g_autoptr (QIOChannelBuffer) bioc =NULL ; +g_autoptr (QEMUFile) f =NULL ; +int ret; + g_autofree VFIODeviceStatePacket *packet = NULL; +size_t packet_len; + + bioc =qio_channel_buffer_new (0 ); +qio_channel_set_name (QIO_CHANNEL (bioc),"vfio-device-config-save" );
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 514
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ + f =qemu_file_new_output (QIO_CHANNEL (bioc)); + +if (vfio_save_device_config_state (f, vbasedev, errp)) { +return false; + } + + ret =qemu_fflush (f); +if (ret) { +error_setg (errp,"save config state flush failed: %d" , ret);
Let's add vbasedev->name to the error message so we know which device caused the error.
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 527
Quoted 13 lines
hw/vfio/migration-multifd.c +13
-0+return false; + } + + packet_len =sizeof (*packet) + bioc->usage; + packet =g_malloc0 (packet_len); + packet->version =VFIO_DEVICE_STATE_PACKET_VER_CURRENT ; + packet->idx = idx; + packet->flags =VFIO_DEVICE_STATE_CONFIG_STATE ; +memcpy (&packet->data, bioc->data, bioc->usage); + +if (!multifd_queue_device_state (idstr, instance_id, + (char *)packet, packet_len)) { +error_setg (errp,"multifd config data queuing failed" );
Ditto.
Quoted 41 lines
hw/vfio/migration-multifd.c +41
-0+return false; + } + +vfio_add_bytes_transferred (packet_len); + +return true; +} + +/* +* This thread is spawned by the migration core directly via +* .save_live_complete_precopy_thread SaveVMHandler. +* +* It exits after either: +* * completing saving the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by +* multifd_device_state_save_thread_should_exit() returning true. +*/ +bool vfio_save_complete_precopy_thread (SaveLiveCompletePrecopyThreadData *d, +Error **errp) +{ +VFIODevice *vbasedev = d->handler_opaque; +VFIOMigration *migration = vbasedev->migration; +bool ret; + g_autofree VFIODeviceStatePacket *packet = NULL; +uint32_t idx; + +if (!vfio_multifd_transfer_enabled (vbasedev)) { +/* Nothing to do, vfio_save_complete_precopy() does the transfer. */ +return true; + } + +trace_vfio_save_complete_precopy_thread_start (vbasedev->name, + d->idstr, d->instance_id); + +/* We reach here with device state STOP or STOP_COPY only */ +if (vfio_migration_set_state (vbasedev,VFIO_DEVICE_STATE_STOP_COPY , +VFIO_DEVICE_STATE_STOP , errp)) { + ret = false; + goto ret_finish; + }
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 578
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ + packet =g_malloc0 (sizeof (*packet) + migration->data_buffer_size); + packet->version =VFIO_DEVICE_STATE_PACKET_VER_CURRENT ; + +for (idx =0 ; ; idx++) { +ssize_t data_size; +size_t packet_size; + +if (multifd_device_state_save_thread_should_exit ()) { +error_setg (errp,"operation cancelled" );
Same comment as in patch #27:
IIUC, if multifd_device_state_save_thread_should_exit() returns true, it means that some other code part already failed and set migration error, no? If so, shouldn't we return true here? After all, vfio_save_complete_precopy_thread didn't really fail, it just got signal to terminate itself
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 587
Quoted 9 lines
hw/vfio/migration-multifd.c +9
-0+ ret = false; + goto ret_finish; + } + + data_size =read (migration->data_fd, &packet->data, + migration->data_buffer_size); +if (data_size <0 ) { +error_setg (errp,"reading state buffer %" PRIu32" failed: %d" , + idx, errno);
Let's add vbasedev->name to the error message so we know which device caused the error.
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 599
Quoted 12 lines
hw/vfio/migration-multifd.c +12
-0+ ret = false; + goto ret_finish; + }else if (data_size ==0 ) { +break ; + } + + packet->idx = idx; + packet_size =sizeof (*packet) + data_size; + +if (!multifd_queue_device_state (d->idstr, d->instance_id, + (char *)packet, packet_size)) { +error_setg (errp,"multifd data queuing failed" );
Ditto.
Thanks.
Quoted 141 lines
hw/vfio/migration-multifd.c +18
-0+ ret = false; + goto ret_finish; + } + +vfio_add_bytes_transferred (packet_size); + } + + ret =vfio_save_complete_precopy_thread_config_state (vbasedev, + d->idstr, + d->instance_id, + idx, errp); + +ret_finish: +trace_vfio_save_complete_precopy_thread_end (vbasedev->name, ret); + +return ret; +} +int vfio_multifd_switchover_start (VFIODevice *vbasedev) {VFIOMigration *migration = vbasedev->migration;hw/vfio/migration-multifd.h +5
-0@@ -25,6 +25,11 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp);bool vfio_load_state_buffer (void *opaque,char *data,size_t data_size,Error **errp); +void vfio_multifd_emit_dummy_eos (VFIODevice *vbasedev,QEMUFile *f); + +bool vfio_save_complete_precopy_thread (SaveLiveCompletePrecopyThreadData *d, +Error **errp); +int vfio_multifd_switchover_start (VFIODevice *vbasedev);#endif hw/vfio/migration.c +20
-6@@ -120,10 +120,10 @@ static void vfio_migration_set_device_state(VFIODevice *vbasedev,vfio_migration_send_event (vbasedev); } -static int vfio_migration_set_state (VFIODevice *vbasedev, -enum vfio_device_mig_state new_state, -enum vfio_device_mig_state recover_state, -Error **errp) +int vfio_migration_set_state (VFIODevice *vbasedev, +enum vfio_device_mig_state new_state, +enum vfio_device_mig_state recover_state, +Error **errp) {VFIOMigration *migration = vbasedev->migration; uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +@@ -238,8 +238,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev,return ret; } -static int vfio_save_device_config_state (QEMUFile *f,void *opaque, -Error **errp) +int vfio_save_device_config_state (QEMUFile *f,void *opaque,Error **errp) {VFIODevice *vbasedev = opaque;int ret;@@ -453,6 +452,10 @@ static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp)uint64_t stop_copy_size =VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE ;int ret; +if (!vfio_multifd_transfer_setup (vbasedev, errp)) { +return -EINVAL ; + } +qemu_put_be64 (f,VFIO_MIG_FLAG_DEV_SETUP_STATE );vfio_query_stop_copy_size (vbasedev, &stop_copy_size);@@ -631,6 +634,11 @@ static int vfio_save_complete_precopy(QEMUFile *f, void *opaque)int ret;Error *local_err =NULL ; +if (vfio_multifd_transfer_enabled (vbasedev)) { +vfio_multifd_emit_dummy_eos (vbasedev, f); +return 0 ; + } +trace_vfio_save_complete_precopy_start (vbasedev->name);/* We reach here with device state STOP or STOP_COPY only */ @@ -662,6 +670,11 @@ static void vfio_save_state(QEMUFile *f, void *opaque)Error *local_err =NULL ;int ret; +if (vfio_multifd_transfer_enabled (vbasedev)) { +vfio_multifd_emit_dummy_eos (vbasedev, f); +return ; + } + ret =vfio_save_device_config_state (f, opaque, &local_err);if (ret) { error_prepend(&local_err,@@ -819,6 +832,7 @@ static const SaveVMHandlers savevm_vfio_handlers = { .is_active_iterate = vfio_is_active_iterate, .save_live_iterate = vfio_save_iterate, .save_live_complete_precopy = vfio_save_complete_precopy, + .save_live_complete_precopy_thread = vfio_save_complete_precopy_thread, .save_state = vfio_save_state, .load_setup = vfio_load_setup, .load_cleanup = vfio_load_cleanup,hw/vfio/trace-events +2
-0@@ -168,6 +168,8 @@ vfio_save_block_precopy_empty_hit(const char *name) " (%s)" vfio_save_cleanup(const char *name) " (%s)" vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d" vfio_save_complete_precopy_start(const char *name) " (%s)" +vfio_save_complete_precopy_thread_start(const char *name, const char *idstr, uint32_t instance_id) " (%s) idstr %s instance %"PRIu32 +vfio_save_complete_precopy_thread_end(const char *name, int ret) " (%s) ret %d" vfio_save_device_config_state(const char *name) " (%s)" vfio_save_iterate(const char *name, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy initial size %"PRIu64" precopy dirty size %"PRIu64 vfio_save_iterate_start(const char *name) " (%s)"include/hw/vfio/vfio-common.h +8
-0@@ -298,6 +298,14 @@ void vfio_add_bytes_transferred(unsigned long val);bool vfio_device_state_is_running (VFIODevice *vbasedev);bool vfio_device_state_is_precopy (VFIODevice *vbasedev); +#ifdef CONFIG_LINUX +int vfio_migration_set_state (VFIODevice *vbasedev, +enum vfio_device_mig_state new_state, +enum vfio_device_mig_state recover_state, +Error **errp); +#endif + +int vfio_save_device_config_state (QEMUFile *f,void *opaque,Error **errp);int vfio_load_device_config_state (QEMUFile *f,void *opaque);#ifdef CONFIG_LINUX
Re: [PATCH v5 30/36] vfio/migration: Multifd device state transfer support - send side
Quoted 10 lines · Avihai Horon
Quoted 8 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
Implement the multifd device state transfer via additional per-device thread inside
save_live_complete_precopy_threadhandler.
Quoted 7 lines
Quoted 3 lines
Switch between doing the data transfer in the new handler and doing it in the old
save_statehandler depending on the x-migration-multifd-transfer device property value.x-migration-multifd-transfer is not yet introduced. Maybe rephrase to:
... depending if VFIO multifd transfer is enabled or not.
Changed accordingly.
Quoted 48 lines
Quoted 48 lines
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 139 ++++++++++++++++++++++++++++++++++ hw/vfio/migration-multifd.h | 5 ++
hw/vfio/migration.c | 26 +++++-- hw/vfio/trace-events | 2 +include/hw/vfio/vfio-common.h| 8 ++5 files changed, 174 insertions(+), 6 deletions(-)hw/vfio/migration-multifd.c +30
-0@@ -476,6 +476,145 @@ bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) return true; } +void vfio_multifd_emit_dummy_eos (VFIODevice *vbasedev,QEMUFile *f) +{ + assert(vfio_multifd_transfer_enabled(vbasedev)); + + /* + * Emit dummy NOP data on the main migration channel since the actual + * device state transfer is done via multifd channels. + */ + qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE); +} + +static bool +vfio_save_complete_precopy_thread_config_state (VFIODevice *vbasedev, + char *idstr, + uint32_t instance_id, + uint32_t idx, + Error **errp) +{ + g_autoptr(QIOChannelBuffer) bioc = NULL; + g_autoptr(QEMUFile) f = NULL; + int ret; + g_autofree VFIODeviceStatePacket *packet = NULL; + size_t packet_len; + + bioc = qio_channel_buffer_new(0); + qio_channel_set_name(QIO_CHANNEL(bioc), "vfio-device-config-save"); + + f = qemu_file_new_output(QIO_CHANNEL(bioc)); + + if (vfio_save_device_config_state(f, vbasedev, errp)) {
Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 514
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ return false; + } + + ret = qemu_fflush(f); + if (ret) { + error_setg(errp, "save config state flush failed: %d", ret);Let's add
vbasedev->nameto the error message so we know which device caused the error.
Done.
Quoted 7 lines
Quoted 7 lines
hw/vfio/migration-multifd.c +7
-0+ return false; + } + + packet_len = sizeof(*packet) + bioc->usage; + packet = g_malloc0(packet_len); + packet->version = VFIO_DEVICE_STATE_PACKET_VER_CURRENT; + packet->idx = idx;
Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 527
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ packet->flags = VFIO_DEVICE_STATE_CONFIG_STATE; + memcpy(&packet->data, bioc->data, bioc->usage); + + if (!multifd_queue_device_state(idstr, instance_id, + (char *)packet, packet_len)) { + error_setg(errp, "multifd config data queuing failed");Ditto.
Done.
Quoted 54 lines
Quoted 41 lines
hw/vfio/migration-multifd.c +41
-0+ return false; + } + + vfio_add_bytes_transferred(packet_len); + + return true; +} + +/* +* This thread is spawned by the migration core directly via +* .save_live_complete_precopy_thread SaveVMHandler. +* +* It exits after either: +* * completing saving the remaining device state and device config, OR: +* * encountering some error while doing the above, OR: +* * being forcefully aborted by the migration core by +* multifd_device_state_save_thread_should_exit() returning true. +*/ +bool vfio_save_complete_precopy_thread (SaveLiveCompletePrecopyThreadData *d, + Error **errp) +{ + VFIODevice *vbasedev = d->handler_opaque; + VFIOMigration *migration = vbasedev->migration; + bool ret; + g_autofree VFIODeviceStatePacket *packet = NULL; + uint32_t idx; + + if (!vfio_multifd_transfer_enabled(vbasedev)) { + /* Nothing to do, vfio_save_complete_precopy() does the transfer. */ + return true; + } + + trace_vfio_save_complete_precopy_thread_start(vbasedev->name, + d->idstr, d->instance_id); + + /* We reach here with device state STOP or STOP_COPY only */ + if (vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP_COPY, + VFIO_DEVICE_STATE_STOP, errp)) { + ret = false; + goto ret_finish; + }hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 578
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ + packet = g_malloc0(sizeof(*packet) + migration->data_buffer_size); + packet->version = VFIO_DEVICE_STATE_PACKET_VER_CURRENT; + + for (idx = 0; ; idx++) { + ssize_t data_size; + size_t packet_size; + + if (multifd_device_state_save_thread_should_exit()) { + error_setg(errp, "operation cancelled");Same comment as in patch #27:
Quoted 2 lines
IIUC, if
multifd_device_state_save_thread_should_exit()returns true, it means that some other code part already failed and set migration error, no? If so, shouldn't we return true here? After all,vfio_save_complete_precopy_threaddidn't really fail, it just got signal to terminate itself
Same as in the "load thread" case - the thread didn't succeed with saving all the data either, but got cancelled.
Quoted 11 lines
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 587
Quoted 10 lines
hw/vfio/migration-multifd.c +9
-0+ ret = false; + goto ret_finish; + } + + data_size = read(migration->data_fd, &packet->data, + migration->data_buffer_size); + if (data_size < 0) { + error_setg(errp, "reading state buffer %" PRIu32 " failed: %d", + idx, errno);
Quoted 1 line
Let's add
vbasedev->nameto the error message so we know which device caused the error.
Done.
Quoted 6 lines
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ ret = false; + goto ret_finish; + } else if (data_size == 0) { + break; + } +
Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) · line 599
Quoted 6 lines
hw/vfio/migration-multifd.c +6
-0+ packet->idx = idx; + packet_size = sizeof(*packet) + data_size; + + if (!multifd_queue_device_state(d->idstr, d->instance_id, + (char *)packet, packet_size)) { + error_setg(errp, "multifd data queuing failed");Ditto.
Done.
Quoted 2 lines
Thanks.
Thanks,
Maciej
PATCH v5 31/36 vfio/migration: Add x-migration-multifd-transfer VFIO property
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
This property allows configuring at runtime whether to transfer the particular device state via multifd channels when live migrating that device.
It defaults to AUTO, which means that VFIO device state transfer via multifd channels is attempted in configurations that otherwise support it.
--- hw/vfio/migration-multifd.c | 17 ++++++++++++++++- hw/vfio/pci.c | 3 +++ include/hw/vfio/vfio-common.h | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-)
hw/vfio/migration-multifd.c +16 -1
@@ -460,11 +460,26 @@ bool vfio_multifd_transfer_supported(void)bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev) { -return false; +VFIOMigration *migration = vbasedev->migration; + +return migration->multifd_transfer; }bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp) { +VFIOMigration *migration = vbasedev->migration; + +/* +* Make a copy of this setting at the start in case it is changed +* mid-migration. +*/ +if (vbasedev->migration_multifd_transfer ==ON_OFF_AUTO_AUTO ) { + migration->multifd_transfer =vfio_multifd_transfer_supported (); + }else { + migration->multifd_transfer = + vbasedev->migration_multifd_transfer ==ON_OFF_AUTO_ON ; + } +if (vfio_multifd_transfer_enabled (vbasedev) && !vfio_multifd_transfer_supported ()) { error_setg(errp,
hw/vfio/pci.c +3 -0
@@ -3377,6 +3377,9 @@ static const Property vfio_pci_dev_properties[] = { VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT, false),DEFINE_PROP_ON_OFF_AUTO ("enable-migration" , VFIOPCIDevice, vbasedev.enable_migration,ON_OFF_AUTO_AUTO ), +DEFINE_PROP_ON_OFF_AUTO ("x-migration-multifd-transfer" , VFIOPCIDevice, + vbasedev.migration_multifd_transfer, +ON_OFF_AUTO_AUTO ),DEFINE_PROP_BOOL ("migration-events" , VFIOPCIDevice, vbasedev.migration_events, false), DEFINE_PROP_BOOL("x-no-mmap", VFIOPCIDevice, vbasedev.no_mmap, false),
include/hw/vfio/vfio-common.h +2 -0
@@ -91,6 +91,7 @@ typedef struct VFIOMigration {uint64_t mig_flags;uint64_t precopy_init_size;uint64_t precopy_dirty_size; +bool multifd_transfer;VFIOMultifd *multifd;bool initial_data_sent;
@@ -153,6 +154,7 @@ typedef struct VFIODevice {bool no_mmap;bool ram_block_discard_allowed;OnOffAuto enable_migration; +OnOffAuto migration_multifd_transfer;bool migration_events;VFIODeviceOps *ops;unsigned int num_irqs;
Re: [PATCH v5 31/36] vfio/migration: Add x-migration-multifd-transfer VFIO property
Quoted 53 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
This property allows configuring at runtime whether to transfer the particular device state via multifd channels when live migrating that device.
It defaults to AUTO, which means that VFIO device state transfer via multifd channels is attempted in configurations that otherwise support it.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 17 ++++++++++++++++- hw/vfio/pci.c | 3 +++ include/hw/vfio/vfio-common.h | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-)hw/vfio/migration-multifd.c +16
-1@@ -460,11 +460,26 @@ bool vfio_multifd_transfer_supported(void)bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev) { -return false; +VFIOMigration *migration = vbasedev->migration; + +return migration->multifd_transfer; }bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp) { +VFIOMigration *migration = vbasedev->migration; + +/* +* Make a copy of this setting at the start in case it is changed +* mid-migration. +*/ +if (vbasedev->migration_multifd_transfer ==ON_OFF_AUTO_AUTO ) { + migration->multifd_transfer =vfio_multifd_transfer_supported (); + }else { + migration->multifd_transfer = + vbasedev->migration_multifd_transfer ==ON_OFF_AUTO_ON ; + } +if (vfio_multifd_transfer_enabled (vbasedev) && !vfio_multifd_transfer_supported ()) { error_setg(errp,hw/vfio/pci.c +0
-0
hw/vfio/pci.c static const Property vfio_pci_dev_properties[] = { · line 3385
Quoted 9 lines
hw/vfio/pci.c +3
-0@@ -3377,6 +3377,9 @@ static const Property vfio_pci_dev_properties[] = { VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT, false),DEFINE_PROP_ON_OFF_AUTO ("enable-migration" , VFIOPCIDevice, vbasedev.enable_migration,ON_OFF_AUTO_AUTO ), +DEFINE_PROP_ON_OFF_AUTO ("x-migration-multifd-transfer" , VFIOPCIDevice, + vbasedev.migration_multifd_transfer, +ON_OFF_AUTO_AUTO ),DEFINE_PROP_BOOL ("migration-events" , VFIOPCIDevice, vbasedev.migration_events, false), DEFINE_PROP_BOOL("x-no-mmap", VFIOPCIDevice, vbasedev.no_mmap, false),
Please add property documentation in vfio_pci_dev_class_init()
Thanks,
C.
Quoted 21 lines
include/hw/vfio/vfio-common.h +2
-0@@ -91,6 +91,7 @@ typedef struct VFIOMigration {uint64_t mig_flags;uint64_t precopy_init_size;uint64_t precopy_dirty_size; +bool multifd_transfer;VFIOMultifd *multifd;bool initial_data_sent;@@ -153,6 +154,7 @@ typedef struct VFIODevice {bool no_mmap;bool ram_block_discard_allowed;OnOffAuto enable_migration; +OnOffAuto migration_multifd_transfer;bool migration_events;VFIODeviceOps *ops;unsigned int num_irqs;
Re: [PATCH v5 31/36] vfio/migration: Add x-migration-multifd-transfer VFIO property
In reply to Maciej S. SzmigieroQuoted 6 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
This property allows configuring at runtime whether to transfer the
IIUC, in this patch it's not configurable at runtime, so let's drop "at runtime".
Quoted 31 lines
particular device state via multifd channels when live migrating that device.
It defaults to AUTO, which means that VFIO device state transfer via multifd channels is attempted in configurations that otherwise support it.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration-multifd.c | 17 ++++++++++++++++- hw/vfio/pci.c | 3 +++ include/hw/vfio/vfio-common.h | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-)hw/vfio/migration-multifd.c +5
-1@@ -460,11 +460,26 @@ bool vfio_multifd_transfer_supported(void)bool vfio_multifd_transfer_enabled (VFIODevice *vbasedev) { -return false; +VFIOMigration *migration = vbasedev->migration; + +return migration->multifd_transfer; }bool vfio_multifd_transfer_setup (VFIODevice *vbasedev,Error **errp) { +VFIOMigration *migration = vbasedev->migration; +
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_supported(void) · line 481
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+/* +* Make a copy of this setting at the start in case it is changed +* mid-migration. +*/ +if (vbasedev->migration_multifd_transfer ==ON_OFF_AUTO_AUTO ) { + migration->multifd_transfer =vfio_multifd_transfer_supported (); + }else { + migration->multifd_transfer = + vbasedev->migration_multifd_transfer ==ON_OFF_AUTO_ON ; + }
Making a copy of this value is only relevant for the next patch where it's turned mutable, so let's move this code to patch #32.
Thanks.
Quoted 38 lines
hw/vfio/migration-multifd.c +1
-0+if (vfio_multifd_transfer_enabled (vbasedev) && !vfio_multifd_transfer_supported ()) { error_setg(errp,hw/vfio/pci.c +3
-0@@ -3377,6 +3377,9 @@ static const Property vfio_pci_dev_properties[] = { VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT, false),DEFINE_PROP_ON_OFF_AUTO ("enable-migration" , VFIOPCIDevice, vbasedev.enable_migration,ON_OFF_AUTO_AUTO ), +DEFINE_PROP_ON_OFF_AUTO ("x-migration-multifd-transfer" , VFIOPCIDevice, + vbasedev.migration_multifd_transfer, +ON_OFF_AUTO_AUTO ),DEFINE_PROP_BOOL ("migration-events" , VFIOPCIDevice, vbasedev.migration_events, false), DEFINE_PROP_BOOL("x-no-mmap", VFIOPCIDevice, vbasedev.no_mmap, false),include/hw/vfio/vfio-common.h +2
-0@@ -91,6 +91,7 @@ typedef struct VFIOMigration {uint64_t mig_flags;uint64_t precopy_init_size;uint64_t precopy_dirty_size; +bool multifd_transfer;VFIOMultifd *multifd;bool initial_data_sent;@@ -153,6 +154,7 @@ typedef struct VFIODevice {bool no_mmap;bool ram_block_discard_allowed;OnOffAuto enable_migration; +OnOffAuto migration_multifd_transfer;bool migration_events;VFIODeviceOps *ops;unsigned int num_irqs;
Re: [PATCH v5 31/36] vfio/migration: Add x-migration-multifd-transfer VFIO property
Quoted 10 lines · Avihai Horon
Quoted 6 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
This property allows configuring at runtime whether to transfer the
IIUC, in this patch it's not configurable at runtime, so let's drop "at runtime".
Dropped this expression from this patch description.
Quoted 27 lines
Quoted 27 lines
particular device state via multifd channels when live migrating that device.
It defaults to AUTO, which means that VFIO device state transfer via multifd channels is attempted in configurations that otherwise support it.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 17 ++++++++++++++++- hw/vfio/pci.c | 3 +++include/hw/vfio/vfio-common.h| 2 ++3 files changed, 21 insertions(+), 1 deletion(-)hw/vfio/migration-multifd.c +3
-1@@ -460,11 +460,26 @@ bool vfio_multifd_transfer_supported(void) bool vfio_multifd_transfer_enabled(VFIODevice *vbasedev) { - return false; + VFIOMigration *migration = vbasedev->migration; + + return migration->multifd_transfer; }
Quoted 16 lines
Quoted 4 lines
hw/vfio/migration-multifd.c +2
-0bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) { + VFIOMigration *migration = vbasedev->migration; +hw/vfio/migration-multifd.c bool vfio_multifd_transfer_supported(void) · line 481
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ /* + * Make a copy of this setting at the start in case it is changed + * mid-migration. + */ + if (vbasedev->migration_multifd_transfer == ON_OFF_AUTO_AUTO) { + migration->multifd_transfer = vfio_multifd_transfer_supported(); + } else { + migration->multifd_transfer = + vbasedev->migration_multifd_transfer == ON_OFF_AUTO_ON; + }Making a copy of this value is only relevant for the next patch where it's turned mutable, so let's move this code to patch #32.
But we still need to handle the "AUTO" condition so it would need very similar code just to get reworked into the above in the next patch.
I think that's just not worth code churn between patches.
Quoted 1 line
Thanks.
Thanks,
Maciej
Re: [PATCH v5 31/36] vfio/migration: Add x-migration-multifd-transfer VFIO property
Quoted 62 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
Quoted 10 lines · Avihai Horon
Quoted 6 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
This property allows configuring at runtime whether to transfer the
IIUC, in this patch it's not configurable at runtime, so let's drop "at runtime".
Dropped this expression from this patch description.
Quoted 36 lines
Quoted 36 lines
particular device state via multifd channels when live migrating that device.
It defaults to AUTO, which means that VFIO device state transfer via multifd channels is attempted in configurations that otherwise support it.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 17 ++++++++++++++++- hw/vfio/pci.c | 3 +++include/hw/vfio/vfio-common.h| 2 ++3 files changed, 21 insertions(+), 1 deletion(-)hw/vfio/migration-multifd.c +10
-1@@ -460,11 +460,26 @@ bool vfio_multifd_transfer_supported(void) bool vfio_multifd_transfer_enabled(VFIODevice *vbasedev) { - return false; + VFIOMigration *migration = vbasedev->migration; + + return migration->multifd_transfer; } bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) { + VFIOMigration *migration = vbasedev->migration; + + /* + * Make a copy of this setting at the start in case it is changed + * mid-migration. + */ + if (vbasedev->migration_multifd_transfer == ON_OFF_AUTO_AUTO) {Quoted 8 lines
hw/vfio/migration-multifd.c bool vfio_multifd_transfer_supported(void) · line 481
Quoted 6 lines
hw/vfio/migration-multifd.c +5
-0+ migration->multifd_transfer =vfio_multifd_transfer_supported (); + } else { + migration->multifd_transfer = + vbasedev->migration_multifd_transfer == ON_OFF_AUTO_ON; + }Making a copy of this value is only relevant for the next patch where it's turned mutable, so let's move this code to patch #32.
Quoted 4 lines
But we still need to handle the "AUTO" condition so it would need very similar code just to get reworked into the above in the next patch.
I think that's just not worth code churn between patches.
Ah, I understand.
In that case, we can move only the comment "Make a copy of this setting ..." to patch #32.
Thanks.
Re: [PATCH v5 31/36] vfio/migration: Add x-migration-multifd-transfer VFIO property
Quoted 63 lines · Avihai Horon
Quoted 61 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
Quoted 10 lines · Avihai Horon
Quoted 6 lines · Maciej S. Szmigiero
External email: Use caution opening links or attachments
From: "Maciej S. Szmigiero" <[email protected]>
This property allows configuring at runtime whether to transfer the
IIUC, in this patch it's not configurable at runtime, so let's drop "at runtime".
Dropped this expression from this patch description.
Quoted 43 lines
Quoted 31 lines
particular device state via multifd channels when live migrating that device.
It defaults to AUTO, which means that VFIO device state transfer via multifd channels is attempted in configurations that otherwise support it.
Signed-off-by Maciej S. Szmigiero <[email protected]>---
hw/vfio/migration-multifd.c | 17 ++++++++++++++++- hw/vfio/pci.c | 3 +++include/hw/vfio/vfio-common.h| 2 ++3 files changed, 21 insertions(+), 1 deletion(-)hw/vfio/migration-multifd.c +5
-1@@ -460,11 +460,26 @@ bool vfio_multifd_transfer_supported(void) bool vfio_multifd_transfer_enabled(VFIODevice *vbasedev) { - return false; + VFIOMigration *migration = vbasedev->migration; + + return migration->multifd_transfer; } bool vfio_multifd_transfer_setup(VFIODevice *vbasedev, Error **errp) { + VFIOMigration *migration = vbasedev->migration; +hw/vfio/migration-multifd.c bool vfio_multifd_transfer_supported(void) · line 481
Quoted 10 lines
hw/vfio/migration-multifd.c +10
-0+ /* + * Make a copy of this setting at the start in case it is changed + * mid-migration. + */ + if (vbasedev->migration_multifd_transfer == ON_OFF_AUTO_AUTO) { + migration->multifd_transfer = vfio_multifd_transfer_supported(); + } else { + migration->multifd_transfer = + vbasedev->migration_multifd_transfer == ON_OFF_AUTO_ON; + }Making a copy of this value is only relevant for the next patch where it's turned mutable, so let's move this code to patch #32.
Quoted 7 lines
Quoted 4 lines
But we still need to handle the "AUTO" condition so it would need very similar code just to get reworked into the above in the next patch.
I think that's just not worth code churn between patches.Ah, I understand.
In that case, we can move only the comment "Make a copy of this setting ..." to patch #32.
All right, comment moved.
Quoted 2 lines
Thanks.
Thanks,
Maciej
PATCH v5 32/36 vfio/migration: Make x-migration-multifd-transfer VFIO property mutable
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
DEFINE_PROP_ON_OFF_AUTO() property isn't runtime-mutable so using it would mean that the source VM would need to decide upfront at startup time whether it wants to do a multifd device state transfer at some point.
Source VM can run for a long time before being migrated so it is desirable to have a fallback mechanism to the old way of transferring VFIO device state if it turns to be necessary.
This brings this property to the same mutability level as ordinary migration parameters, which too can be adjusted at the run time.
--- hw/vfio/pci.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
hw/vfio/pci.c +9 -3
@@ -3353,6 +3353,8 @@ static void vfio_instance_init(Object *obj) pci_dev->cap_present |=QEMU_PCI_CAP_EXPRESS ; } +static PropertyInfo qdev_prop_on_off_auto_mutable; +static const Property vfio_pci_dev_properties[] = {DEFINE_PROP_PCI_HOST_DEVADDR ("host" , VFIOPCIDevice, host),DEFINE_PROP_UUID_NODEFAULT ("vf-token" , VFIOPCIDevice, vf_token),
@@ -3377,9 +3379,10 @@ static const Property vfio_pci_dev_properties[] = { VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT, false),DEFINE_PROP_ON_OFF_AUTO ("enable-migration" , VFIOPCIDevice, vbasedev.enable_migration,ON_OFF_AUTO_AUTO ), -DEFINE_PROP_ON_OFF_AUTO ("x-migration-multifd-transfer" , VFIOPCIDevice, - vbasedev.migration_multifd_transfer, -ON_OFF_AUTO_AUTO ), +DEFINE_PROP ("x-migration-multifd-transfer" , VFIOPCIDevice, + vbasedev.migration_multifd_transfer, + qdev_prop_on_off_auto_mutable, OnOffAuto, + .set_default = true, .defval.i = ON_OFF_AUTO_AUTO),DEFINE_PROP_BOOL ("migration-events" , VFIOPCIDevice, vbasedev.migration_events, false), DEFINE_PROP_BOOL("x-no-mmap", VFIOPCIDevice, vbasedev.no_mmap, false),
@@ -3475,6 +3478,9 @@ static const TypeInfo vfio_pci_nohotplug_dev_info = {static void register_vfio_pci_dev_type (void ) { + qdev_prop_on_off_auto_mutable = qdev_prop_on_off_auto; + qdev_prop_on_off_auto_mutable.realized_set_allowed = true; +type_register_static (&vfio_pci_dev_info);type_register_static (&vfio_pci_nohotplug_dev_info); }