From: "Cédric Le Goater" <clg@redhat.com>
To: "Maciej S. Szmigiero" <mail@maciej.szmigiero.name>
Cc: "Alex Williamson" <alex.williamson@redhat.com>,
"Peter Xu" <peterx@redhat.com>, "Fabiano Rosas" <farosas@suse.de>,
"Eric Blake" <eblake@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Daniel P . Berrangé" <berrange@redhat.com>,
"Avihai Horon" <avihaih@nvidia.com>,
"Joao Martins" <joao.m.martins@oracle.com>,
qemu-devel@nongnu.org
Subject: Re: [PATCH v5 27/36] vfio/migration: Multifd device state transfer support - load thread
Date: Fri, 28 Feb 2025 10:11:39 +0100 [thread overview]
Message-ID: <9659a377-0132-4449-8592-1c8ef8b2d8d0@redhat.com> (raw)
In-Reply-To: <438ee2cb-d29a-4975-806e-2e2f2dff1a44@maciej.szmigiero.name>
On 2/26/25 22:05, Maciej S. Szmigiero wrote:
> On 26.02.2025 14:49, Cédric Le Goater wrote:
>> On 2/19/25 21:34, Maciej S. Szmigiero wrote:
>>> From: "Maciej S. Szmigiero" <maciej.szmigiero@oracle.com>
>>>
>>> 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.
>>>
>>> Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com>
>>> ---
>>> 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(+)
>>>
>>> diff --git a/hw/vfio/migration-multifd.c b/hw/vfio/migration-multifd.c
>>> index 5d5ee1393674..b3a88c062769 100644
>>> --- a/hw/vfio/migration-multifd.c
>>> +++ b/hw/vfio/migration-multifd.c
>>> @@ -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;
>>> +}
>>
>>
>> please move to next patch.
>
> As I wrote on the previous version of the patch set at
> https://lore.kernel.org/qemu-devel/4f335de0-ba9f-4537-b230-2cf8af1c160b@maciej.szmigiero.name/:
>> 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 of
>> vfio_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().
>
>>> +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.
>>
>>> +
>>> + 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.
>>
>>> + 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);
>>
>> 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.
>
>>> + /* 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);
>>
>> 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.
>
>> 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".
>>
>>> +
>>> + 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;
>>
>> No needed IMO. see below.
>>
>>> +
>>> + 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.
>
>>> + 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 ?
>
> I'm not sure that I fully understand this comment.
> Do you mean to rename ret_signal label to thread_exit?
Yes. I find label 'thread_exit' more meaning full. This is minor since
there is only one 'exit' label.
>
>>> + }
>>> +
>>> + 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++;
>>> + }
>>
>> 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.
>
>>> + 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.
OK. Let's keep it that way.
Thanks,
C.
>
> 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.
>
>> Thanks,
>>
>> C.
>
> Thanks,
> Maciej
>
next prev parent reply other threads:[~2025-02-28 9:12 UTC|newest]
Thread overview: 120+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-19 20:33 [PATCH v5 00/36] Multifd 🔀 device state transfer support with VFIO consumer Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 01/36] migration: Clarify that {load, save}_cleanup handlers can run without setup Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 02/36] thread-pool: Remove thread_pool_submit() function Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 03/36] thread-pool: Rename AIO pool functions to *_aio() and data types to *Aio Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 04/36] thread-pool: Implement generic (non-AIO) pool support Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 05/36] migration: Add MIG_CMD_SWITCHOVER_START and its load handler Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 06/36] migration: Add qemu_loadvm_load_state_buffer() and its handler Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 07/36] migration: postcopy_ram_listen_thread() should take BQL for some calls Maciej S. Szmigiero
2025-02-25 17:16 ` Peter Xu
2025-02-25 21:08 ` Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 08/36] error: define g_autoptr() cleanup function for the Error type Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 09/36] migration: Add thread pool of optional load threads Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 10/36] migration/multifd: Split packet into header and RAM data Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 11/36] migration/multifd: Device state transfer support - receive side Maciej S. Szmigiero
2025-03-02 12:42 ` Avihai Horon
2025-03-03 22:14 ` Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 12/36] migration/multifd: Make multifd_send() thread safe Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 13/36] migration/multifd: Add an explicit MultiFDSendData destructor Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 14/36] migration/multifd: Device state transfer support - send side Maciej S. Szmigiero
2025-03-02 12:46 ` Avihai Horon
2025-03-03 22:15 ` Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 15/36] migration/multifd: Make MultiFDSendData a struct Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 16/36] migration/multifd: Add multifd_device_state_supported() Maciej S. Szmigiero
2025-02-19 20:33 ` [PATCH v5 17/36] migration: Add save_live_complete_precopy_thread handler Maciej S. Szmigiero
2025-02-26 16:43 ` Peter Xu
2025-03-04 21:50 ` Maciej S. Szmigiero
2025-03-04 22:03 ` Peter Xu
2025-02-19 20:34 ` [PATCH v5 18/36] vfio/migration: Add load_device_config_state_start trace event Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 19/36] vfio/migration: Convert bytes_transferred counter to atomic Maciej S. Szmigiero
2025-02-26 7:52 ` Cédric Le Goater
2025-02-26 13:55 ` Maciej S. Szmigiero
2025-02-26 15:56 ` Cédric Le Goater
2025-02-26 16:20 ` Cédric Le Goater
2025-02-19 20:34 ` [PATCH v5 20/36] vfio/migration: Add vfio_add_bytes_transferred() Maciej S. Szmigiero
2025-02-26 8:06 ` Cédric Le Goater
2025-02-26 15:45 ` Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 21/36] vfio/migration: Move migration channel flags to vfio-common.h header file Maciej S. Szmigiero
2025-02-26 8:19 ` Cédric Le Goater
2025-02-19 20:34 ` [PATCH v5 22/36] vfio/migration: Multifd device state transfer support - basic types Maciej S. Szmigiero
2025-02-26 8:52 ` Cédric Le Goater
2025-02-26 16:06 ` Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 23/36] vfio/migration: Multifd device state transfer support - VFIOStateBuffer(s) Maciej S. Szmigiero
2025-02-26 8:54 ` Cédric Le Goater
2025-03-02 13:00 ` Avihai Horon
2025-03-02 15:14 ` Maciej S. Szmigiero
2025-03-03 6:42 ` Cédric Le Goater
2025-03-03 22:14 ` Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 24/36] vfio/migration: Multifd device state transfer - add support checking function Maciej S. Szmigiero
2025-02-26 8:54 ` Cédric Le Goater
2025-02-19 20:34 ` [PATCH v5 25/36] vfio/migration: Multifd device state transfer support - receive init/cleanup Maciej S. Szmigiero
2025-02-26 10:14 ` Cédric Le Goater
2025-02-26 17:22 ` Cédric Le Goater
2025-02-26 17:28 ` Maciej S. Szmigiero
2025-02-26 17:28 ` Cédric Le Goater
2025-02-27 22:00 ` Maciej S. Szmigiero
2025-02-26 17:46 ` Cédric Le Goater
2025-02-27 22:00 ` Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 26/36] vfio/migration: Multifd device state transfer support - received buffers queuing Maciej S. Szmigiero
2025-02-26 10:43 ` Cédric Le Goater
2025-02-26 21:04 ` Maciej S. Szmigiero
2025-02-28 8:09 ` Cédric Le Goater
2025-02-28 20:47 ` Maciej S. Szmigiero
2025-03-02 13:12 ` Avihai Horon
2025-03-03 22:15 ` Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 27/36] vfio/migration: Multifd device state transfer support - load thread Maciej S. Szmigiero
2025-02-26 13:49 ` Cédric Le Goater
2025-02-26 21:05 ` Maciej S. Szmigiero
2025-02-28 9:11 ` Cédric Le Goater [this message]
2025-02-28 20:48 ` Maciej S. Szmigiero
2025-03-02 14:19 ` Avihai Horon
2025-03-03 22:16 ` Maciej S. Szmigiero
2025-03-02 14:15 ` Avihai Horon
2025-03-03 22:16 ` Maciej S. Szmigiero
2025-03-04 11:21 ` Avihai Horon
2025-02-19 20:34 ` [PATCH v5 28/36] vfio/migration: Multifd device state transfer support - config loading support Maciej S. Szmigiero
2025-02-26 13:52 ` Cédric Le Goater
2025-02-26 21:05 ` Maciej S. Szmigiero
2025-03-02 14:25 ` Avihai Horon
2025-03-03 22:17 ` Maciej S. Szmigiero
2025-03-04 7:41 ` Cédric Le Goater
2025-03-04 21:50 ` Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 29/36] migration/qemu-file: Define g_autoptr() cleanup function for QEMUFile Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 30/36] vfio/migration: Multifd device state transfer support - send side Maciej S. Szmigiero
2025-02-26 16:43 ` Cédric Le Goater
2025-02-26 21:05 ` Maciej S. Szmigiero
2025-02-28 9:13 ` Cédric Le Goater
2025-02-28 20:49 ` Maciej S. Szmigiero
2025-03-02 14:41 ` Avihai Horon
2025-03-03 22:17 ` Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 31/36] vfio/migration: Add x-migration-multifd-transfer VFIO property Maciej S. Szmigiero
2025-02-27 6:45 ` Cédric Le Goater
2025-03-02 14:48 ` Avihai Horon
2025-03-03 22:17 ` Maciej S. Szmigiero
2025-03-04 11:29 ` Avihai Horon
2025-03-04 21:50 ` Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 32/36] vfio/migration: Make x-migration-multifd-transfer VFIO property mutable Maciej S. Szmigiero
2025-02-26 17:59 ` Cédric Le Goater
2025-02-26 21:05 ` Maciej S. Szmigiero
2025-02-28 8:44 ` Cédric Le Goater
2025-02-28 20:47 ` Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 33/36] hw/core/machine: Add compat for x-migration-multifd-transfer VFIO property Maciej S. Szmigiero
2025-02-26 17:59 ` Cédric Le Goater
2025-02-19 20:34 ` [PATCH v5 34/36] vfio/migration: Max in-flight VFIO device state buffer count limit Maciej S. Szmigiero
2025-02-27 6:48 ` Cédric Le Goater
2025-02-27 22:01 ` Maciej S. Szmigiero
2025-02-28 8:53 ` Cédric Le Goater
2025-02-28 20:48 ` Maciej S. Szmigiero
2025-03-02 14:53 ` Avihai Horon
2025-03-02 14:54 ` Maciej S. Szmigiero
2025-03-02 14:59 ` Maciej S. Szmigiero
2025-03-02 16:28 ` Avihai Horon
2025-02-19 20:34 ` [PATCH v5 35/36] vfio/migration: Add x-migration-load-config-after-iter VFIO property Maciej S. Szmigiero
2025-02-19 20:34 ` [PATCH v5 36/36] vfio/migration: Update VFIO migration documentation Maciej S. Szmigiero
2025-02-27 6:59 ` Cédric Le Goater
2025-02-27 22:01 ` Maciej S. Szmigiero
2025-02-28 10:05 ` Cédric Le Goater
2025-02-28 20:49 ` Maciej S. Szmigiero
2025-02-28 23:38 ` Fabiano Rosas
2025-03-03 9:34 ` Cédric Le Goater
2025-03-03 22:14 ` Maciej S. Szmigiero
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9659a377-0132-4449-8592-1c8ef8b2d8d0@redhat.com \
--to=clg@redhat.com \
--cc=alex.williamson@redhat.com \
--cc=armbru@redhat.com \
--cc=avihaih@nvidia.com \
--cc=berrange@redhat.com \
--cc=eblake@redhat.com \
--cc=farosas@suse.de \
--cc=joao.m.martins@oracle.com \
--cc=mail@maciej.szmigiero.name \
--cc=peterx@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).