2020-01-22 18:16:07 +03:00
|
|
|
/*
|
|
|
|
* Multifd common code
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019-2020 Red Hat Inc
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Juan Quintela <quintela@redhat.com>
|
|
|
|
*
|
|
|
|
* 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"
|
2024-03-11 21:00:12 +03:00
|
|
|
#include "qemu/cutils.h"
|
2020-01-22 18:16:07 +03:00
|
|
|
#include "qemu/rcu.h"
|
|
|
|
#include "exec/target_page.h"
|
|
|
|
#include "sysemu/sysemu.h"
|
|
|
|
#include "exec/ramblock.h"
|
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#include "qapi/error.h"
|
2024-02-29 18:30:11 +03:00
|
|
|
#include "file.h"
|
2020-01-22 18:16:07 +03:00
|
|
|
#include "migration.h"
|
2023-04-26 20:04:06 +03:00
|
|
|
#include "migration-stats.h"
|
2020-01-22 18:16:07 +03:00
|
|
|
#include "socket.h"
|
2020-09-15 06:04:01 +03:00
|
|
|
#include "tls.h"
|
2020-01-22 18:16:07 +03:00
|
|
|
#include "qemu-file.h"
|
|
|
|
#include "trace.h"
|
|
|
|
#include "multifd.h"
|
2023-02-03 10:35:19 +03:00
|
|
|
#include "threadinfo.h"
|
2023-03-02 00:17:14 +03:00
|
|
|
#include "options.h"
|
2020-12-28 18:08:52 +03:00
|
|
|
#include "qemu/yank.h"
|
2024-02-29 18:30:11 +03:00
|
|
|
#include "io/channel-file.h"
|
2020-12-28 18:08:52 +03:00
|
|
|
#include "io/channel-socket.h"
|
2021-03-23 20:52:42 +03:00
|
|
|
#include "yank_functions.h"
|
2020-12-28 18:08:52 +03:00
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
/* Multiple fd's */
|
|
|
|
|
|
|
|
#define MULTIFD_MAGIC 0x11223344U
|
|
|
|
#define MULTIFD_VERSION 1
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t version;
|
|
|
|
unsigned char uuid[16]; /* QemuUUID */
|
|
|
|
uint8_t id;
|
|
|
|
uint8_t unused1[7]; /* Reserved for future use */
|
|
|
|
uint64_t unused2[4]; /* Reserved for future use */
|
|
|
|
} __attribute__((packed)) MultiFDInit_t;
|
|
|
|
|
2024-02-02 13:28:56 +03:00
|
|
|
struct {
|
|
|
|
MultiFDSendParams *params;
|
|
|
|
/*
|
|
|
|
* Global number of generated multifd packets.
|
|
|
|
*
|
|
|
|
* Note that we used 'uintptr_t' because it'll naturally support atomic
|
|
|
|
* operations on both 32bit / 64 bits hosts. It means on 32bit systems
|
|
|
|
* multifd will overflow the packet_num easier, but that should be
|
|
|
|
* fine.
|
|
|
|
*
|
|
|
|
* Another option is to use QEMU's Stat64 then it'll be 64 bits on all
|
|
|
|
* hosts, however so far it does not support atomic fetch_add() yet.
|
|
|
|
* Make it easy for now.
|
|
|
|
*/
|
|
|
|
uintptr_t packet_num;
|
2024-02-07 00:51:18 +03:00
|
|
|
/*
|
|
|
|
* Synchronization point past which no more channels will be
|
|
|
|
* created.
|
|
|
|
*/
|
|
|
|
QemuSemaphore channels_created;
|
2024-02-02 13:28:56 +03:00
|
|
|
/* send channels ready */
|
|
|
|
QemuSemaphore channels_ready;
|
|
|
|
/*
|
|
|
|
* Have we already run terminate threads. There is a race when it
|
|
|
|
* happens that we got one error while we are exiting.
|
|
|
|
* We will use atomic operations. Only valid values are 0 and 1.
|
|
|
|
*/
|
|
|
|
int exiting;
|
|
|
|
/* multifd ops */
|
|
|
|
MultiFDMethods *ops;
|
|
|
|
} *multifd_send_state;
|
|
|
|
|
2024-02-21 01:41:08 +03:00
|
|
|
struct {
|
|
|
|
MultiFDRecvParams *params;
|
2024-02-29 18:30:09 +03:00
|
|
|
MultiFDRecvData *data;
|
2024-02-21 01:41:08 +03:00
|
|
|
/* number of created threads */
|
|
|
|
int count;
|
2024-02-29 18:30:09 +03:00
|
|
|
/*
|
|
|
|
* This is always posted by the recv threads, the migration thread
|
|
|
|
* uses it to wait for recv threads to finish assigned tasks.
|
|
|
|
*/
|
2024-02-21 01:41:08 +03:00
|
|
|
QemuSemaphore sem_sync;
|
|
|
|
/* global number of generated multifd packets */
|
|
|
|
uint64_t packet_num;
|
|
|
|
int exiting;
|
|
|
|
/* multifd ops */
|
|
|
|
MultiFDMethods *ops;
|
|
|
|
} *multifd_recv_state;
|
|
|
|
|
2024-08-27 20:46:03 +03:00
|
|
|
MultiFDSendData *multifd_send_data_alloc(void)
|
2024-08-27 20:45:54 +03:00
|
|
|
{
|
|
|
|
size_t max_payload_size, size_minus_payload;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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(), 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);
|
|
|
|
|
|
|
|
return g_malloc0(size_minus_payload + max_payload_size);
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:30:08 +03:00
|
|
|
static bool multifd_use_packets(void)
|
|
|
|
{
|
|
|
|
return !migrate_mapped_ram();
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:30:10 +03:00
|
|
|
void multifd_send_channel_created(void)
|
|
|
|
{
|
|
|
|
qemu_sem_post(&multifd_send_state->channels_created);
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:46:02 +03:00
|
|
|
static MultiFDMethods *multifd_ops[MULTIFD_COMPRESSION__MAX] = {};
|
2019-05-15 14:37:46 +03:00
|
|
|
|
2019-01-04 17:30:06 +03:00
|
|
|
void multifd_register_ops(int method, MultiFDMethods *ops)
|
|
|
|
{
|
2024-08-27 20:46:02 +03:00
|
|
|
assert(0 <= method && method < MULTIFD_COMPRESSION__MAX);
|
|
|
|
assert(!multifd_ops[method]);
|
2019-01-04 17:30:06 +03:00
|
|
|
multifd_ops[method] = ops;
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
static int multifd_send_initial_packet(MultiFDSendParams *p, Error **errp)
|
|
|
|
{
|
|
|
|
MultiFDInit_t msg = {};
|
2023-05-15 22:57:09 +03:00
|
|
|
size_t size = sizeof(msg);
|
2020-01-22 18:16:07 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
msg.magic = cpu_to_be32(MULTIFD_MAGIC);
|
|
|
|
msg.version = cpu_to_be32(MULTIFD_VERSION);
|
|
|
|
msg.id = p->id;
|
|
|
|
memcpy(msg.uuid, &qemu_uuid.data, sizeof(msg.uuid));
|
|
|
|
|
2023-05-15 22:57:09 +03:00
|
|
|
ret = qio_channel_write_all(p->c, (char *)&msg, size, errp);
|
2020-01-22 18:16:07 +03:00
|
|
|
if (ret != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-05-15 22:57:09 +03:00
|
|
|
stat64_add(&mig_stats.multifd_bytes, size);
|
2020-01-22 18:16:07 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int multifd_recv_initial_packet(QIOChannel *c, Error **errp)
|
|
|
|
{
|
|
|
|
MultiFDInit_t msg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = qio_channel_read_all(c, (char *)&msg, sizeof(msg), errp);
|
|
|
|
if (ret != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.magic = be32_to_cpu(msg.magic);
|
|
|
|
msg.version = be32_to_cpu(msg.version);
|
|
|
|
|
|
|
|
if (msg.magic != MULTIFD_MAGIC) {
|
|
|
|
error_setg(errp, "multifd: received packet magic %x "
|
|
|
|
"expected %x", msg.magic, MULTIFD_MAGIC);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.version != MULTIFD_VERSION) {
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd: received packet version %u "
|
|
|
|
"expected %u", msg.version, MULTIFD_VERSION);
|
2020-01-22 18:16:07 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(msg.uuid, &qemu_uuid, sizeof(qemu_uuid))) {
|
|
|
|
char *uuid = qemu_uuid_unparse_strdup(&qemu_uuid);
|
|
|
|
char *msg_uuid = qemu_uuid_unparse_strdup((const QemuUUID *)msg.uuid);
|
|
|
|
|
|
|
|
error_setg(errp, "multifd: received uuid '%s' and expected "
|
|
|
|
"uuid '%s' for channel %hhd", msg_uuid, uuid, msg.id);
|
|
|
|
g_free(uuid);
|
|
|
|
g_free(msg_uuid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.id > migrate_multifd_channels()) {
|
2023-12-31 12:30:10 +03:00
|
|
|
error_setg(errp, "multifd: received channel id %u is greater than "
|
|
|
|
"number of channels %u", msg.id, migrate_multifd_channels());
|
2020-01-22 18:16:07 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg.id;
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:45:57 +03:00
|
|
|
void multifd_send_fill_packet(MultiFDSendParams *p)
|
|
|
|
{
|
|
|
|
MultiFDPacket_t *packet = p->packet;
|
|
|
|
uint64_t packet_num;
|
2024-08-27 20:45:58 +03:00
|
|
|
bool sync_packet = p->flags & MULTIFD_FLAG_SYNC;
|
2024-08-27 20:45:57 +03:00
|
|
|
|
|
|
|
memset(packet, 0, p->packet_len);
|
|
|
|
|
|
|
|
packet->magic = cpu_to_be32(MULTIFD_MAGIC);
|
|
|
|
packet->version = cpu_to_be32(MULTIFD_VERSION);
|
|
|
|
|
|
|
|
packet->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);
|
|
|
|
packet->packet_num = cpu_to_be64(packet_num);
|
|
|
|
|
2024-02-02 13:28:43 +03:00
|
|
|
p->packets_sent++;
|
2024-02-02 13:28:45 +03:00
|
|
|
|
2024-08-27 20:45:58 +03:00
|
|
|
if (!sync_packet) {
|
|
|
|
multifd_ram_fill_packet(p);
|
|
|
|
}
|
2024-08-27 20:45:57 +03:00
|
|
|
|
|
|
|
trace_multifd_send_fill(p->id, packet_num,
|
|
|
|
p->flags, p->next_packet_size);
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
2024-08-27 20:45:57 +03:00
|
|
|
static int multifd_recv_unfill_packet(MultiFDRecvParams *p, Error **errp)
|
|
|
|
{
|
|
|
|
MultiFDPacket_t *packet = p->packet;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
packet->magic = be32_to_cpu(packet->magic);
|
|
|
|
if (packet->magic != MULTIFD_MAGIC) {
|
|
|
|
error_setg(errp, "multifd: received packet "
|
|
|
|
"magic %x and expected magic %x",
|
|
|
|
packet->magic, MULTIFD_MAGIC);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet->version = be32_to_cpu(packet->version);
|
|
|
|
if (packet->version != MULTIFD_VERSION) {
|
|
|
|
error_setg(errp, "multifd: received packet "
|
|
|
|
"version %u and expected version %u",
|
|
|
|
packet->version, MULTIFD_VERSION);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->flags = be32_to_cpu(packet->flags);
|
|
|
|
p->next_packet_size = be32_to_cpu(packet->next_packet_size);
|
|
|
|
p->packet_num = be64_to_cpu(packet->packet_num);
|
|
|
|
p->packets_recved++;
|
|
|
|
|
2024-08-27 20:45:58 +03:00
|
|
|
if (!(p->flags & MULTIFD_FLAG_SYNC)) {
|
|
|
|
ret = multifd_ram_unfill_packet(p, errp);
|
|
|
|
}
|
2024-08-27 20:45:57 +03:00
|
|
|
|
|
|
|
trace_multifd_recv_unfill(p->id, p->packet_num, p->flags,
|
|
|
|
p->next_packet_size);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:37 +03:00
|
|
|
static bool multifd_send_should_exit(void)
|
|
|
|
{
|
|
|
|
return qatomic_read(&multifd_send_state->exiting);
|
|
|
|
}
|
|
|
|
|
2024-02-21 01:41:08 +03:00
|
|
|
static bool multifd_recv_should_exit(void)
|
|
|
|
{
|
|
|
|
return qatomic_read(&multifd_recv_state->exiting);
|
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:36 +03:00
|
|
|
/*
|
|
|
|
* The migration thread can wait on either of the two semaphores. This
|
|
|
|
* function can be used to kick the main thread out of waiting on either of
|
|
|
|
* them. Should mostly only be called when something wrong happened with
|
|
|
|
* the current multifd send thread.
|
|
|
|
*/
|
|
|
|
static void multifd_send_kick_main(MultiFDSendParams *p)
|
|
|
|
{
|
|
|
|
qemu_sem_post(&p->sem_sync);
|
|
|
|
qemu_sem_post(&multifd_send_state->channels_ready);
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
/*
|
2024-08-27 20:45:59 +03:00
|
|
|
* multifd_send() works by exchanging the MultiFDSendData object
|
|
|
|
* provided by the caller with an unused MultiFDSendData object from
|
|
|
|
* the next channel that is found to be idle.
|
2020-01-22 18:16:07 +03:00
|
|
|
*
|
2024-08-27 20:45:59 +03:00
|
|
|
* The channel owns the data until it finishes transmitting and the
|
|
|
|
* caller owns the empty object until it fills it with data and calls
|
|
|
|
* this function again. No locking necessary.
|
2020-01-22 18:16:07 +03:00
|
|
|
*
|
2024-08-27 20:45:59 +03:00
|
|
|
* Switching is safe because both the migration thread and the channel
|
|
|
|
* thread have barriers in place to serialize access.
|
2024-02-02 13:28:51 +03:00
|
|
|
*
|
|
|
|
* Returns true if succeed, false otherwise.
|
2020-01-22 18:16:07 +03:00
|
|
|
*/
|
2024-08-27 20:46:03 +03:00
|
|
|
bool multifd_send(MultiFDSendData **send_data)
|
2020-01-22 18:16:07 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static int next_channel;
|
|
|
|
MultiFDSendParams *p = NULL; /* make happy gcc */
|
2024-08-27 20:45:54 +03:00
|
|
|
MultiFDSendData *tmp;
|
2020-01-22 18:16:07 +03:00
|
|
|
|
2024-02-02 13:28:37 +03:00
|
|
|
if (multifd_send_should_exit()) {
|
2024-02-02 13:28:51 +03:00
|
|
|
return false;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:41 +03:00
|
|
|
/* We wait here, until at least one channel is ready */
|
2020-01-22 18:16:07 +03:00
|
|
|
qemu_sem_wait(&multifd_send_state->channels_ready);
|
2024-02-02 13:28:41 +03:00
|
|
|
|
2020-06-17 14:31:54 +03:00
|
|
|
/*
|
|
|
|
* next_channel can remain from a previous migration that was
|
|
|
|
* using more channels, so ensure it doesn't overflow if the
|
|
|
|
* limit is lower now.
|
|
|
|
*/
|
|
|
|
next_channel %= migrate_multifd_channels();
|
2020-01-22 18:16:07 +03:00
|
|
|
for (i = next_channel;; i = (i + 1) % migrate_multifd_channels()) {
|
2024-02-02 13:28:37 +03:00
|
|
|
if (multifd_send_should_exit()) {
|
2024-02-02 13:28:51 +03:00
|
|
|
return false;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
2024-02-02 13:28:37 +03:00
|
|
|
p = &multifd_send_state->params[i];
|
2024-02-02 13:28:41 +03:00
|
|
|
/*
|
|
|
|
* Lockless read to p->pending_job is safe, because only multifd
|
|
|
|
* sender thread can clear it.
|
|
|
|
*/
|
migration/multifd: Separate SYNC request with normal jobs
Multifd provide a threaded model for processing jobs. On sender side,
there can be two kinds of job: (1) a list of pages to send, or (2) a sync
request.
The sync request is a very special kind of job. It never contains a page
array, but only a multifd packet telling the dest side to synchronize with
sent pages.
Before this patch, both requests use the pending_job field, no matter what
the request is, it will boost pending_job, while multifd sender thread will
decrement it after it finishes one job.
However this should be racy, because SYNC is special in that it needs to
set p->flags with MULTIFD_FLAG_SYNC, showing that this is a sync request.
Consider a sequence of operations where:
- migration thread enqueue a job to send some pages, pending_job++ (0->1)
- [...before the selected multifd sender thread wakes up...]
- migration thread enqueue another job to sync, pending_job++ (1->2),
setup p->flags=MULTIFD_FLAG_SYNC
- multifd sender thread wakes up, found pending_job==2
- send the 1st packet with MULTIFD_FLAG_SYNC and list of pages
- send the 2nd packet with flags==0 and no pages
This is not expected, because MULTIFD_FLAG_SYNC should hopefully be done
after all the pages are received. Meanwhile, the 2nd packet will be
completely useless, which contains zero information.
I didn't verify above, but I think this issue is still benign in that at
least on the recv side we always receive pages before handling
MULTIFD_FLAG_SYNC. However that's not always guaranteed and just tricky.
One other reason I want to separate it is using p->flags to communicate
between the two threads is also not clearly defined, it's very hard to read
and understand why accessing p->flags is always safe; see the current impl
of multifd_send_thread() where we tried to cache only p->flags. It doesn't
need to be that complicated.
This patch introduces pending_sync, a separate flag just to show that the
requester needs a sync. Alongside, we remove the tricky caching of
p->flags now because after this patch p->flags should only be used by
multifd sender thread now, which will be crystal clear. So it is always
thread safe to access p->flags.
With that, we can also safely convert the pending_job into a boolean,
because we don't support >1 pending jobs anyway.
Always use atomic ops to access both flags to make sure no cache effect.
When at it, drop the initial setting of "pending_job = 0" because it's
always allocated using g_new0().
Reviewed-by: Fabiano Rosas <farosas@suse.de>
Link: https://lore.kernel.org/r/20240202102857.110210-7-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
2024-02-02 13:28:40 +03:00
|
|
|
if (qatomic_read(&p->pending_job) == false) {
|
2020-01-22 18:16:07 +03:00
|
|
|
next_channel = (i + 1) % migrate_multifd_channels();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-02-02 13:28:41 +03:00
|
|
|
|
|
|
|
/*
|
2024-02-02 13:28:57 +03:00
|
|
|
* Make sure we read p->pending_job before all the rest. Pairs with
|
|
|
|
* qatomic_store_release() in multifd_send_thread().
|
2024-02-02 13:28:41 +03:00
|
|
|
*/
|
2024-02-02 13:28:57 +03:00
|
|
|
smp_mb_acquire();
|
2024-08-27 20:45:54 +03:00
|
|
|
|
2024-08-27 20:45:59 +03:00
|
|
|
assert(multifd_payload_empty(p->data));
|
2024-08-27 20:45:54 +03:00
|
|
|
|
2024-08-27 20:45:59 +03:00
|
|
|
/*
|
|
|
|
* Swap the pointers. The channel gets the client data for
|
|
|
|
* transferring and the client gets back an unused data slot.
|
|
|
|
*/
|
|
|
|
tmp = *send_data;
|
|
|
|
*send_data = p->data;
|
2024-08-27 20:45:54 +03:00
|
|
|
p->data = tmp;
|
2024-08-27 20:45:59 +03:00
|
|
|
|
2024-02-02 13:28:57 +03:00
|
|
|
/*
|
2024-08-27 20:45:54 +03:00
|
|
|
* Making sure p->data is setup before marking pending_job=true. Pairs
|
2024-02-02 13:28:57 +03:00
|
|
|
* with the qatomic_load_acquire() in multifd_send_thread().
|
|
|
|
*/
|
|
|
|
qatomic_store_release(&p->pending_job, true);
|
2020-01-22 18:16:07 +03:00
|
|
|
qemu_sem_post(&p->sem);
|
|
|
|
|
2024-02-02 13:28:51 +03:00
|
|
|
return true;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:49 +03:00
|
|
|
/* Multifd send side hit an error; remember it and prepare to quit */
|
|
|
|
static void multifd_send_set_error(Error *err)
|
2020-01-22 18:16:07 +03:00
|
|
|
{
|
2024-02-02 13:28:37 +03:00
|
|
|
/*
|
|
|
|
* We don't want to exit each threads twice. Depending on where
|
|
|
|
* we get the error, or if there are two independent errors in two
|
|
|
|
* threads at the same time, we can end calling this function
|
|
|
|
* twice.
|
|
|
|
*/
|
|
|
|
if (qatomic_xchg(&multifd_send_state->exiting, 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
if (err) {
|
|
|
|
MigrationState *s = migrate_get_current();
|
|
|
|
migrate_set_error(s, err);
|
|
|
|
if (s->state == MIGRATION_STATUS_SETUP ||
|
|
|
|
s->state == MIGRATION_STATUS_PRE_SWITCHOVER ||
|
|
|
|
s->state == MIGRATION_STATUS_DEVICE ||
|
|
|
|
s->state == MIGRATION_STATUS_ACTIVE) {
|
|
|
|
migrate_set_state(&s->state, s->state,
|
|
|
|
MIGRATION_STATUS_FAILED);
|
|
|
|
}
|
|
|
|
}
|
2024-02-02 13:28:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void multifd_send_terminate_threads(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
trace_multifd_send_terminate_threads();
|
2020-01-22 18:16:07 +03:00
|
|
|
|
2024-02-02 13:28:49 +03:00
|
|
|
/*
|
|
|
|
* Tell everyone we're quitting. No xchg() needed here; we simply
|
|
|
|
* always set it.
|
|
|
|
*/
|
|
|
|
qatomic_set(&multifd_send_state->exiting, 1);
|
2024-02-02 13:28:53 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Firstly, kick all threads out; no matter whether they are just idle,
|
|
|
|
* or blocked in an IO system call.
|
|
|
|
*/
|
2020-01-22 18:16:07 +03:00
|
|
|
for (i = 0; i < migrate_multifd_channels(); i++) {
|
|
|
|
MultiFDSendParams *p = &multifd_send_state->params[i];
|
|
|
|
|
|
|
|
qemu_sem_post(&p->sem);
|
2021-12-03 14:55:33 +03:00
|
|
|
if (p->c) {
|
|
|
|
qio_channel_shutdown(p->c, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
|
|
|
|
}
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
2024-02-02 13:28:53 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Finally recycle all the threads.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < migrate_multifd_channels(); i++) {
|
|
|
|
MultiFDSendParams *p = &multifd_send_state->params[i];
|
|
|
|
|
2024-02-07 00:51:13 +03:00
|
|
|
if (p->tls_thread_created) {
|
|
|
|
qemu_thread_join(&p->tls_thread);
|
|
|
|
}
|
|
|
|
|
2024-02-07 00:51:14 +03:00
|
|
|
if (p->thread_created) {
|
2024-02-02 13:28:53 +03:00
|
|
|
qemu_thread_join(&p->thread);
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:53 +03:00
|
|
|
static bool multifd_send_cleanup_channel(MultiFDSendParams *p, Error **errp)
|
|
|
|
{
|
2024-02-22 12:52:58 +03:00
|
|
|
if (p->c) {
|
2024-02-02 13:28:53 +03:00
|
|
|
migration_ioc_unregister_yank(p->c);
|
2024-03-01 12:15:24 +03:00
|
|
|
/*
|
2024-03-05 22:56:29 +03:00
|
|
|
* The object_unref() cannot guarantee the fd will always be
|
|
|
|
* released because finalize() of the iochannel is only
|
|
|
|
* triggered on the last reference and it's not guaranteed
|
|
|
|
* that we always hold the last refcount when reaching here.
|
2024-03-01 12:15:24 +03:00
|
|
|
*
|
2024-03-05 22:56:29 +03:00
|
|
|
* Closing the fd explicitly has the benefit that if there is any
|
|
|
|
* registered I/O handler callbacks on such fd, that will get a
|
|
|
|
* POLLNVAL event and will further trigger the cleanup to finally
|
|
|
|
* release the IOC.
|
|
|
|
*
|
|
|
|
* FIXME: It should logically be guaranteed that all multifd
|
|
|
|
* channels have no I/O handler callback registered when reaching
|
|
|
|
* here, because migration thread will wait for all multifd channel
|
|
|
|
* establishments to complete during setup. Since
|
|
|
|
* migrate_fd_cleanup() will be scheduled in main thread too, all
|
|
|
|
* previous callbacks should guarantee to be completed when
|
|
|
|
* reaching here. See multifd_send_state.channels_created and its
|
|
|
|
* usage. In the future, we could replace this with an assert
|
|
|
|
* making sure we're the last reference, or simply drop it if above
|
|
|
|
* is more clear to be justified.
|
2024-03-01 12:15:24 +03:00
|
|
|
*/
|
2024-02-29 18:30:11 +03:00
|
|
|
qio_channel_close(p->c, &error_abort);
|
2024-02-22 12:53:01 +03:00
|
|
|
object_unref(OBJECT(p->c));
|
2024-02-22 12:52:58 +03:00
|
|
|
p->c = NULL;
|
2024-02-02 13:28:53 +03:00
|
|
|
}
|
|
|
|
qemu_sem_destroy(&p->sem);
|
|
|
|
qemu_sem_destroy(&p->sem_sync);
|
|
|
|
g_free(p->name);
|
|
|
|
p->name = NULL;
|
2024-08-27 20:45:54 +03:00
|
|
|
g_free(p->data);
|
|
|
|
p->data = NULL;
|
2024-02-02 13:28:53 +03:00
|
|
|
p->packet_len = 0;
|
|
|
|
g_free(p->packet);
|
|
|
|
p->packet = NULL;
|
|
|
|
multifd_send_state->ops->send_cleanup(p, errp);
|
|
|
|
|
|
|
|
return *errp == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void multifd_send_cleanup_state(void)
|
|
|
|
{
|
2024-02-29 18:30:11 +03:00
|
|
|
file_cleanup_outgoing_migration();
|
2024-02-22 12:53:00 +03:00
|
|
|
socket_cleanup_outgoing_migration();
|
2024-02-07 00:51:18 +03:00
|
|
|
qemu_sem_destroy(&multifd_send_state->channels_created);
|
2024-02-02 13:28:53 +03:00
|
|
|
qemu_sem_destroy(&multifd_send_state->channels_ready);
|
|
|
|
g_free(multifd_send_state->params);
|
|
|
|
multifd_send_state->params = NULL;
|
|
|
|
g_free(multifd_send_state);
|
|
|
|
multifd_send_state = NULL;
|
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:55 +03:00
|
|
|
void multifd_send_shutdown(void)
|
2020-01-22 18:16:07 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2023-03-02 00:10:29 +03:00
|
|
|
if (!migrate_multifd()) {
|
2020-01-22 18:16:07 +03:00
|
|
|
return;
|
|
|
|
}
|
2024-02-02 13:28:53 +03:00
|
|
|
|
2024-02-02 13:28:49 +03:00
|
|
|
multifd_send_terminate_threads();
|
2020-01-22 18:16:07 +03:00
|
|
|
|
|
|
|
for (i = 0; i < migrate_multifd_channels(); i++) {
|
|
|
|
MultiFDSendParams *p = &multifd_send_state->params[i];
|
2019-05-15 14:37:46 +03:00
|
|
|
Error *local_err = NULL;
|
2020-01-22 18:16:07 +03:00
|
|
|
|
2024-02-02 13:28:53 +03:00
|
|
|
if (!multifd_send_cleanup_channel(p, &local_err)) {
|
2019-05-15 14:37:46 +03:00
|
|
|
migrate_set_error(migrate_get_current(), local_err);
|
2020-05-06 12:54:16 +03:00
|
|
|
error_free(local_err);
|
2019-05-15 14:37:46 +03:00
|
|
|
}
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
2024-02-02 13:28:53 +03:00
|
|
|
|
|
|
|
multifd_send_cleanup_state();
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
2022-10-25 07:47:28 +03:00
|
|
|
static int multifd_zero_copy_flush(QIOChannel *c)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
Error *err = NULL;
|
|
|
|
|
|
|
|
ret = qio_channel_flush(c, &err);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_report_err(err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret == 1) {
|
2023-04-26 20:37:19 +03:00
|
|
|
stat64_add(&mig_stats.dirty_sync_missed_zero_copy, 1);
|
2022-10-25 07:47:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:46:00 +03:00
|
|
|
int multifd_send_sync_main(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
bool flush_zero_copy;
|
|
|
|
|
2023-03-02 00:17:14 +03:00
|
|
|
flush_zero_copy = migrate_zero_copy_send();
|
2022-05-13 09:28:37 +03:00
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
for (i = 0; i < migrate_multifd_channels(); i++) {
|
|
|
|
MultiFDSendParams *p = &multifd_send_state->params[i];
|
|
|
|
|
2024-02-02 13:28:37 +03:00
|
|
|
if (multifd_send_should_exit()) {
|
2022-05-13 09:28:35 +03:00
|
|
|
return -1;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:37 +03:00
|
|
|
trace_multifd_send_sync_main_signal(p->id);
|
|
|
|
|
migration/multifd: Separate SYNC request with normal jobs
Multifd provide a threaded model for processing jobs. On sender side,
there can be two kinds of job: (1) a list of pages to send, or (2) a sync
request.
The sync request is a very special kind of job. It never contains a page
array, but only a multifd packet telling the dest side to synchronize with
sent pages.
Before this patch, both requests use the pending_job field, no matter what
the request is, it will boost pending_job, while multifd sender thread will
decrement it after it finishes one job.
However this should be racy, because SYNC is special in that it needs to
set p->flags with MULTIFD_FLAG_SYNC, showing that this is a sync request.
Consider a sequence of operations where:
- migration thread enqueue a job to send some pages, pending_job++ (0->1)
- [...before the selected multifd sender thread wakes up...]
- migration thread enqueue another job to sync, pending_job++ (1->2),
setup p->flags=MULTIFD_FLAG_SYNC
- multifd sender thread wakes up, found pending_job==2
- send the 1st packet with MULTIFD_FLAG_SYNC and list of pages
- send the 2nd packet with flags==0 and no pages
This is not expected, because MULTIFD_FLAG_SYNC should hopefully be done
after all the pages are received. Meanwhile, the 2nd packet will be
completely useless, which contains zero information.
I didn't verify above, but I think this issue is still benign in that at
least on the recv side we always receive pages before handling
MULTIFD_FLAG_SYNC. However that's not always guaranteed and just tricky.
One other reason I want to separate it is using p->flags to communicate
between the two threads is also not clearly defined, it's very hard to read
and understand why accessing p->flags is always safe; see the current impl
of multifd_send_thread() where we tried to cache only p->flags. It doesn't
need to be that complicated.
This patch introduces pending_sync, a separate flag just to show that the
requester needs a sync. Alongside, we remove the tricky caching of
p->flags now because after this patch p->flags should only be used by
multifd sender thread now, which will be crystal clear. So it is always
thread safe to access p->flags.
With that, we can also safely convert the pending_job into a boolean,
because we don't support >1 pending jobs anyway.
Always use atomic ops to access both flags to make sure no cache effect.
When at it, drop the initial setting of "pending_job = 0" because it's
always allocated using g_new0().
Reviewed-by: Fabiano Rosas <farosas@suse.de>
Link: https://lore.kernel.org/r/20240202102857.110210-7-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
2024-02-02 13:28:40 +03:00
|
|
|
/*
|
|
|
|
* We should be the only user so far, so not possible to be set by
|
|
|
|
* others concurrently.
|
|
|
|
*/
|
|
|
|
assert(qatomic_read(&p->pending_sync) == false);
|
|
|
|
qatomic_set(&p->pending_sync, true);
|
2020-01-22 18:16:07 +03:00
|
|
|
qemu_sem_post(&p->sem);
|
|
|
|
}
|
|
|
|
for (i = 0; i < migrate_multifd_channels(); i++) {
|
|
|
|
MultiFDSendParams *p = &multifd_send_state->params[i];
|
|
|
|
|
2024-02-02 13:28:37 +03:00
|
|
|
if (multifd_send_should_exit()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-04-26 13:20:36 +03:00
|
|
|
qemu_sem_wait(&multifd_send_state->channels_ready);
|
2020-01-22 18:16:07 +03:00
|
|
|
trace_multifd_send_sync_main_wait(p->id);
|
|
|
|
qemu_sem_wait(&p->sem_sync);
|
2022-10-17 10:53:51 +03:00
|
|
|
|
|
|
|
if (flush_zero_copy && p->c && (multifd_zero_copy_flush(p->c) < 0)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
trace_multifd_send_sync_main(multifd_send_state->packet_num);
|
2022-05-13 09:28:35 +03:00
|
|
|
|
|
|
|
return 0;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *multifd_send_thread(void *opaque)
|
|
|
|
{
|
|
|
|
MultiFDSendParams *p = opaque;
|
2023-02-03 10:35:19 +03:00
|
|
|
MigrationThread *thread = NULL;
|
2020-01-22 18:16:07 +03:00
|
|
|
Error *local_err = NULL;
|
|
|
|
int ret = 0;
|
2024-02-29 18:30:08 +03:00
|
|
|
bool use_packets = multifd_use_packets();
|
2020-01-22 18:16:07 +03:00
|
|
|
|
2023-06-07 19:13:04 +03:00
|
|
|
thread = migration_threads_add(p->name, qemu_get_thread_id());
|
2023-02-03 10:35:19 +03:00
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
trace_multifd_send_thread_start(p->id);
|
|
|
|
rcu_register_thread();
|
|
|
|
|
2024-02-29 18:30:08 +03:00
|
|
|
if (use_packets) {
|
|
|
|
if (multifd_send_initial_packet(p, &local_err) < 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
2023-04-26 13:20:36 +03:00
|
|
|
qemu_sem_post(&multifd_send_state->channels_ready);
|
2020-01-22 18:16:07 +03:00
|
|
|
qemu_sem_wait(&p->sem);
|
|
|
|
|
2024-02-02 13:28:37 +03:00
|
|
|
if (multifd_send_should_exit()) {
|
2020-01-22 18:16:07 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:57 +03:00
|
|
|
/*
|
2024-08-27 20:45:54 +03:00
|
|
|
* Read pending_job flag before p->data. Pairs with the
|
2024-08-27 20:45:59 +03:00
|
|
|
* qatomic_store_release() in multifd_send().
|
2024-02-02 13:28:57 +03:00
|
|
|
*/
|
|
|
|
if (qatomic_load_acquire(&p->pending_job)) {
|
2024-02-02 13:28:46 +03:00
|
|
|
p->iovs_num = 0;
|
2024-08-27 20:45:59 +03:00
|
|
|
assert(!multifd_payload_empty(p->data));
|
2024-02-02 13:28:42 +03:00
|
|
|
|
|
|
|
ret = multifd_send_state->ops->send_prepare(p, &local_err);
|
|
|
|
if (ret != 0) {
|
|
|
|
break;
|
2019-05-15 14:37:46 +03:00
|
|
|
}
|
2024-02-02 13:28:42 +03:00
|
|
|
|
2024-02-29 18:30:14 +03:00
|
|
|
if (migrate_mapped_ram()) {
|
|
|
|
ret = file_write_ramblock_iov(p->c, p->iov, p->iovs_num,
|
2024-08-27 20:45:54 +03:00
|
|
|
&p->data->u.ram, &local_err);
|
2024-02-29 18:30:14 +03:00
|
|
|
} else {
|
|
|
|
ret = qio_channel_writev_full_all(p->c, p->iov, p->iovs_num,
|
|
|
|
NULL, 0, p->write_flags,
|
|
|
|
&local_err);
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
if (ret != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-11 21:43:57 +03:00
|
|
|
stat64_add(&mig_stats.multifd_bytes,
|
|
|
|
p->next_packet_size + p->packet_len);
|
2024-02-02 13:28:38 +03:00
|
|
|
|
2023-10-11 21:43:58 +03:00
|
|
|
p->next_packet_size = 0;
|
2024-08-27 20:45:59 +03:00
|
|
|
multifd_set_payload_type(p->data, MULTIFD_PAYLOAD_NONE);
|
2024-02-02 13:28:57 +03:00
|
|
|
|
|
|
|
/*
|
2024-08-27 20:45:54 +03:00
|
|
|
* Making sure p->data is published before saying "we're
|
2024-02-02 13:28:57 +03:00
|
|
|
* free". Pairs with the smp_mb_acquire() in
|
2024-08-27 20:45:59 +03:00
|
|
|
* multifd_send().
|
2024-02-02 13:28:57 +03:00
|
|
|
*/
|
|
|
|
qatomic_store_release(&p->pending_job, false);
|
2024-02-02 13:28:48 +03:00
|
|
|
} else {
|
2024-02-02 13:28:57 +03:00
|
|
|
/*
|
|
|
|
* If not a normal job, must be a sync request. Note that
|
|
|
|
* pending_sync is a standalone flag (unlike pending_job), so
|
|
|
|
* it doesn't require explicit memory barriers.
|
|
|
|
*/
|
2024-02-02 13:28:48 +03:00
|
|
|
assert(qatomic_read(&p->pending_sync));
|
2024-02-29 18:30:08 +03:00
|
|
|
|
|
|
|
if (use_packets) {
|
|
|
|
p->flags = MULTIFD_FLAG_SYNC;
|
|
|
|
multifd_send_fill_packet(p);
|
|
|
|
ret = qio_channel_write_all(p->c, (void *)p->packet,
|
|
|
|
p->packet_len, &local_err);
|
|
|
|
if (ret != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* p->next_packet_size will always be zero for a SYNC packet */
|
|
|
|
stat64_add(&mig_stats.multifd_bytes, p->packet_len);
|
|
|
|
p->flags = 0;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
2024-02-29 18:30:08 +03:00
|
|
|
|
migration/multifd: Separate SYNC request with normal jobs
Multifd provide a threaded model for processing jobs. On sender side,
there can be two kinds of job: (1) a list of pages to send, or (2) a sync
request.
The sync request is a very special kind of job. It never contains a page
array, but only a multifd packet telling the dest side to synchronize with
sent pages.
Before this patch, both requests use the pending_job field, no matter what
the request is, it will boost pending_job, while multifd sender thread will
decrement it after it finishes one job.
However this should be racy, because SYNC is special in that it needs to
set p->flags with MULTIFD_FLAG_SYNC, showing that this is a sync request.
Consider a sequence of operations where:
- migration thread enqueue a job to send some pages, pending_job++ (0->1)
- [...before the selected multifd sender thread wakes up...]
- migration thread enqueue another job to sync, pending_job++ (1->2),
setup p->flags=MULTIFD_FLAG_SYNC
- multifd sender thread wakes up, found pending_job==2
- send the 1st packet with MULTIFD_FLAG_SYNC and list of pages
- send the 2nd packet with flags==0 and no pages
This is not expected, because MULTIFD_FLAG_SYNC should hopefully be done
after all the pages are received. Meanwhile, the 2nd packet will be
completely useless, which contains zero information.
I didn't verify above, but I think this issue is still benign in that at
least on the recv side we always receive pages before handling
MULTIFD_FLAG_SYNC. However that's not always guaranteed and just tricky.
One other reason I want to separate it is using p->flags to communicate
between the two threads is also not clearly defined, it's very hard to read
and understand why accessing p->flags is always safe; see the current impl
of multifd_send_thread() where we tried to cache only p->flags. It doesn't
need to be that complicated.
This patch introduces pending_sync, a separate flag just to show that the
requester needs a sync. Alongside, we remove the tricky caching of
p->flags now because after this patch p->flags should only be used by
multifd sender thread now, which will be crystal clear. So it is always
thread safe to access p->flags.
With that, we can also safely convert the pending_job into a boolean,
because we don't support >1 pending jobs anyway.
Always use atomic ops to access both flags to make sure no cache effect.
When at it, drop the initial setting of "pending_job = 0" because it's
always allocated using g_new0().
Reviewed-by: Fabiano Rosas <farosas@suse.de>
Link: https://lore.kernel.org/r/20240202102857.110210-7-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
2024-02-02 13:28:40 +03:00
|
|
|
qatomic_set(&p->pending_sync, false);
|
|
|
|
qemu_sem_post(&p->sem_sync);
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2023-10-12 16:43:42 +03:00
|
|
|
if (ret) {
|
|
|
|
assert(local_err);
|
2020-01-22 18:16:07 +03:00
|
|
|
trace_multifd_send_error(p->id);
|
2024-02-02 13:28:49 +03:00
|
|
|
multifd_send_set_error(local_err);
|
2024-02-02 13:28:36 +03:00
|
|
|
multifd_send_kick_main(p);
|
2023-10-12 16:43:42 +03:00
|
|
|
error_free(local_err);
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rcu_unregister_thread();
|
2023-06-07 19:13:04 +03:00
|
|
|
migration_threads_remove(thread);
|
2024-08-27 20:45:56 +03:00
|
|
|
trace_multifd_send_thread_end(p->id, p->packets_sent);
|
2020-01-22 18:16:07 +03:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-02-07 00:51:17 +03:00
|
|
|
static void multifd_new_send_channel_async(QIOTask *task, gpointer opaque);
|
2020-09-15 06:04:01 +03:00
|
|
|
|
2024-02-22 12:52:57 +03:00
|
|
|
typedef struct {
|
|
|
|
MultiFDSendParams *p;
|
|
|
|
QIOChannelTLS *tioc;
|
|
|
|
} MultiFDTLSThreadArgs;
|
|
|
|
|
2020-11-06 09:24:53 +03:00
|
|
|
static void *multifd_tls_handshake_thread(void *opaque)
|
|
|
|
{
|
2024-02-22 12:52:57 +03:00
|
|
|
MultiFDTLSThreadArgs *args = opaque;
|
2020-11-06 09:24:53 +03:00
|
|
|
|
2024-02-22 12:52:57 +03:00
|
|
|
qio_channel_tls_handshake(args->tioc,
|
2024-02-07 00:51:17 +03:00
|
|
|
multifd_new_send_channel_async,
|
2024-02-22 12:52:57 +03:00
|
|
|
args->p,
|
2020-11-06 09:24:53 +03:00
|
|
|
NULL,
|
|
|
|
NULL);
|
2024-02-22 12:52:57 +03:00
|
|
|
g_free(args);
|
|
|
|
|
2020-11-06 09:24:53 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-10-12 16:43:43 +03:00
|
|
|
static bool multifd_tls_channel_connect(MultiFDSendParams *p,
|
2020-09-15 06:04:01 +03:00
|
|
|
QIOChannel *ioc,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
MigrationState *s = migrate_get_current();
|
2022-03-31 18:08:40 +03:00
|
|
|
const char *hostname = s->hostname;
|
2024-02-22 12:52:57 +03:00
|
|
|
MultiFDTLSThreadArgs *args;
|
2020-09-15 06:04:01 +03:00
|
|
|
QIOChannelTLS *tioc;
|
|
|
|
|
2023-04-26 22:33:08 +03:00
|
|
|
tioc = migration_tls_client_create(ioc, hostname, errp);
|
2020-09-15 06:04:01 +03:00
|
|
|
if (!tioc) {
|
2023-10-12 16:43:43 +03:00
|
|
|
return false;
|
2020-09-15 06:04:01 +03:00
|
|
|
}
|
|
|
|
|
2024-02-07 00:51:17 +03:00
|
|
|
/*
|
|
|
|
* Ownership of the socket channel now transfers to the newly
|
|
|
|
* created TLS channel, which has already taken a reference.
|
|
|
|
*/
|
2020-11-11 17:26:03 +03:00
|
|
|
object_unref(OBJECT(ioc));
|
2020-09-15 06:04:02 +03:00
|
|
|
trace_multifd_tls_outgoing_handshake_start(ioc, tioc, hostname);
|
2020-09-15 06:04:01 +03:00
|
|
|
qio_channel_set_name(QIO_CHANNEL(tioc), "multifd-tls-outgoing");
|
2024-02-22 12:52:57 +03:00
|
|
|
|
|
|
|
args = g_new0(MultiFDTLSThreadArgs, 1);
|
|
|
|
args->tioc = tioc;
|
|
|
|
args->p = p;
|
2024-02-07 00:51:13 +03:00
|
|
|
|
|
|
|
p->tls_thread_created = true;
|
migration: Rename thread debug names
The postcopy thread names on dest QEMU are slightly confusing, partly I'll
need to blame myself on 36f62f11e4 ("migration: Postcopy preemption
preparation on channel creation"). E.g., "fault-fast" reads like a fast
version of "fault-default", but it's actually the fast version of
"postcopy/listen".
Taking this chance, rename all the migration threads with proper rules.
Considering we only have 15 chars usable, prefix all threads with "mig/",
meanwhile identify src/dst threads properly this time. So now most thread
names will look like "mig/DIR/xxx", where DIR will be "src"/"dst", except
the bg-snapshot thread which doesn't have a direction.
For multifd threads, making them "mig/{src|dst}/{send|recv}_%d".
We used to have "live_migration" thread for a very long time, now it's
called "mig/src/main". We may hope to have "mig/dst/main" soon but not
yet.
Reviewed-by: Fabiano Rosas <farosas@suse.de>
Reviewed-by: Zhijian Li (Fujitsu) <lizhijian@fujitsu.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
2024-06-20 01:30:37 +03:00
|
|
|
qemu_thread_create(&p->tls_thread, "mig/src/tls",
|
2024-02-22 12:52:57 +03:00
|
|
|
multifd_tls_handshake_thread, args,
|
2020-11-06 09:24:53 +03:00
|
|
|
QEMU_THREAD_JOINABLE);
|
2023-10-12 16:43:43 +03:00
|
|
|
return true;
|
2020-09-15 06:04:01 +03:00
|
|
|
}
|
|
|
|
|
2024-02-29 18:30:11 +03:00
|
|
|
void multifd_channel_connect(MultiFDSendParams *p, QIOChannel *ioc)
|
2020-09-15 06:04:01 +03:00
|
|
|
{
|
2024-02-07 00:51:17 +03:00
|
|
|
qio_channel_set_delay(ioc, false);
|
2023-12-31 12:30:11 +03:00
|
|
|
|
|
|
|
migration_ioc_register_yank(ioc);
|
2024-02-22 12:52:57 +03:00
|
|
|
/* Setup p->c only if the channel is completely setup */
|
2023-12-31 12:30:11 +03:00
|
|
|
p->c = ioc;
|
2024-02-07 00:51:14 +03:00
|
|
|
|
|
|
|
p->thread_created = true;
|
2023-12-31 12:30:11 +03:00
|
|
|
qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
|
|
|
|
QEMU_THREAD_JOINABLE);
|
2020-09-15 06:04:01 +03:00
|
|
|
}
|
|
|
|
|
2024-02-07 00:51:17 +03:00
|
|
|
/*
|
|
|
|
* When TLS is enabled this function is called once to establish the
|
|
|
|
* TLS connection and a second time after the TLS handshake to create
|
|
|
|
* the multifd channel. Without TLS it goes straight into the channel
|
|
|
|
* creation.
|
|
|
|
*/
|
2020-01-22 18:16:07 +03:00
|
|
|
static void multifd_new_send_channel_async(QIOTask *task, gpointer opaque)
|
|
|
|
{
|
|
|
|
MultiFDSendParams *p = opaque;
|
2023-10-12 16:43:41 +03:00
|
|
|
QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
|
2020-01-22 18:16:07 +03:00
|
|
|
Error *local_err = NULL;
|
2024-02-07 00:51:17 +03:00
|
|
|
bool ret;
|
2020-01-22 18:16:07 +03:00
|
|
|
|
|
|
|
trace_multifd_new_send_channel_async(p->id);
|
2024-02-07 00:51:17 +03:00
|
|
|
|
|
|
|
if (qio_task_propagate_error(task, &local_err)) {
|
|
|
|
ret = false;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_multifd_set_outgoing_channel(ioc, object_get_typename(OBJECT(ioc)),
|
|
|
|
migrate_get_current()->hostname);
|
|
|
|
|
|
|
|
if (migrate_channel_requires_tls_upgrade(ioc)) {
|
|
|
|
ret = multifd_tls_channel_connect(p, ioc, &local_err);
|
2024-02-07 00:51:18 +03:00
|
|
|
if (ret) {
|
|
|
|
return;
|
|
|
|
}
|
2024-02-07 00:51:17 +03:00
|
|
|
} else {
|
2024-02-22 12:52:59 +03:00
|
|
|
multifd_channel_connect(p, ioc);
|
|
|
|
ret = true;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
2020-09-15 06:04:00 +03:00
|
|
|
|
2024-02-07 00:51:18 +03:00
|
|
|
out:
|
|
|
|
/*
|
|
|
|
* Here we're not interested whether creation succeeded, only that
|
|
|
|
* it happened at all.
|
|
|
|
*/
|
2024-02-29 18:30:10 +03:00
|
|
|
multifd_send_channel_created();
|
2024-02-07 00:51:18 +03:00
|
|
|
|
2024-02-07 00:51:17 +03:00
|
|
|
if (ret) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-12 16:43:43 +03:00
|
|
|
trace_multifd_new_send_channel_async_error(p->id, local_err);
|
2024-02-02 13:28:49 +03:00
|
|
|
multifd_send_set_error(local_err);
|
2024-02-22 12:52:57 +03:00
|
|
|
/*
|
|
|
|
* For error cases (TLS or non-TLS), IO channel is always freed here
|
|
|
|
* rather than when cleanup multifd: since p->c is not set, multifd
|
|
|
|
* cleanup code doesn't even know its existence.
|
|
|
|
*/
|
|
|
|
object_unref(OBJECT(ioc));
|
2024-02-02 13:28:37 +03:00
|
|
|
error_free(local_err);
|
2023-10-12 16:43:41 +03:00
|
|
|
}
|
|
|
|
|
2024-02-29 18:30:11 +03:00
|
|
|
static bool multifd_new_send_channel_create(gpointer opaque, Error **errp)
|
2023-10-12 16:43:41 +03:00
|
|
|
{
|
2024-02-29 18:30:11 +03:00
|
|
|
if (!multifd_use_packets()) {
|
|
|
|
return file_send_channel_create(opaque, errp);
|
|
|
|
}
|
|
|
|
|
2023-10-12 16:43:41 +03:00
|
|
|
socket_send_channel_create(multifd_new_send_channel_async, opaque);
|
2024-02-29 18:30:11 +03:00
|
|
|
return true;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
2024-02-07 00:51:15 +03:00
|
|
|
bool multifd_send_setup(void)
|
2020-01-22 18:16:07 +03:00
|
|
|
{
|
2024-02-07 00:51:15 +03:00
|
|
|
MigrationState *s = migrate_get_current();
|
|
|
|
int thread_count, ret = 0;
|
2024-08-27 20:45:49 +03:00
|
|
|
uint32_t page_count = multifd_ram_page_count();
|
2024-02-29 18:30:08 +03:00
|
|
|
bool use_packets = multifd_use_packets();
|
2020-01-22 18:16:07 +03:00
|
|
|
uint8_t i;
|
|
|
|
|
2023-03-02 00:10:29 +03:00
|
|
|
if (!migrate_multifd()) {
|
2024-02-07 00:51:15 +03:00
|
|
|
return true;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
2021-09-09 10:29:50 +03:00
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
thread_count = migrate_multifd_channels();
|
|
|
|
multifd_send_state = g_malloc0(sizeof(*multifd_send_state));
|
|
|
|
multifd_send_state->params = g_new0(MultiFDSendParams, thread_count);
|
2024-02-07 00:51:18 +03:00
|
|
|
qemu_sem_init(&multifd_send_state->channels_created, 0);
|
2020-01-22 18:16:07 +03:00
|
|
|
qemu_sem_init(&multifd_send_state->channels_ready, 0);
|
2020-09-23 13:56:46 +03:00
|
|
|
qatomic_set(&multifd_send_state->exiting, 0);
|
2019-05-15 14:37:46 +03:00
|
|
|
multifd_send_state->ops = multifd_ops[migrate_multifd_compression()];
|
2020-01-22 18:16:07 +03:00
|
|
|
|
|
|
|
for (i = 0; i < thread_count; i++) {
|
|
|
|
MultiFDSendParams *p = &multifd_send_state->params[i];
|
2024-08-01 20:41:01 +03:00
|
|
|
Error *local_err = NULL;
|
2020-01-22 18:16:07 +03:00
|
|
|
|
|
|
|
qemu_sem_init(&p->sem, 0);
|
|
|
|
qemu_sem_init(&p->sem_sync, 0);
|
|
|
|
p->id = i;
|
2024-08-27 20:45:54 +03:00
|
|
|
p->data = multifd_send_data_alloc();
|
2024-02-29 18:30:08 +03:00
|
|
|
|
|
|
|
if (use_packets) {
|
|
|
|
p->packet_len = sizeof(MultiFDPacket_t)
|
|
|
|
+ sizeof(uint64_t) * page_count;
|
|
|
|
p->packet = g_malloc0(p->packet_len);
|
|
|
|
}
|
migration: Rename thread debug names
The postcopy thread names on dest QEMU are slightly confusing, partly I'll
need to blame myself on 36f62f11e4 ("migration: Postcopy preemption
preparation on channel creation"). E.g., "fault-fast" reads like a fast
version of "fault-default", but it's actually the fast version of
"postcopy/listen".
Taking this chance, rename all the migration threads with proper rules.
Considering we only have 15 chars usable, prefix all threads with "mig/",
meanwhile identify src/dst threads properly this time. So now most thread
names will look like "mig/DIR/xxx", where DIR will be "src"/"dst", except
the bg-snapshot thread which doesn't have a direction.
For multifd threads, making them "mig/{src|dst}/{send|recv}_%d".
We used to have "live_migration" thread for a very long time, now it's
called "mig/src/main". We may hope to have "mig/dst/main" soon but not
yet.
Reviewed-by: Fabiano Rosas <farosas@suse.de>
Reviewed-by: Zhijian Li (Fujitsu) <lizhijian@fujitsu.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
2024-06-20 01:30:37 +03:00
|
|
|
p->name = g_strdup_printf("mig/src/send_%d", i);
|
2024-02-02 13:28:47 +03:00
|
|
|
p->write_flags = 0;
|
2024-02-29 18:30:11 +03:00
|
|
|
|
|
|
|
if (!multifd_new_send_channel_create(p, &local_err)) {
|
2024-08-01 20:41:01 +03:00
|
|
|
migrate_set_error(s, local_err);
|
|
|
|
ret = -1;
|
2024-02-29 18:30:11 +03:00
|
|
|
}
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
2019-05-15 14:37:46 +03:00
|
|
|
|
2024-02-07 00:51:18 +03:00
|
|
|
/*
|
|
|
|
* Wait until channel creation has started for all channels. The
|
|
|
|
* creation can still fail, but no more channels will be created
|
|
|
|
* past this point.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < thread_count; i++) {
|
|
|
|
qemu_sem_wait(&multifd_send_state->channels_created);
|
|
|
|
}
|
|
|
|
|
2024-08-01 20:41:01 +03:00
|
|
|
if (ret) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-05-15 14:37:46 +03:00
|
|
|
for (i = 0; i < thread_count; i++) {
|
|
|
|
MultiFDSendParams *p = &multifd_send_state->params[i];
|
2024-08-01 20:41:01 +03:00
|
|
|
Error *local_err = NULL;
|
2019-05-15 14:37:46 +03:00
|
|
|
|
2024-02-07 00:51:15 +03:00
|
|
|
ret = multifd_send_state->ops->send_setup(p, &local_err);
|
2019-05-15 14:37:46 +03:00
|
|
|
if (ret) {
|
2024-08-01 20:41:01 +03:00
|
|
|
migrate_set_error(s, local_err);
|
|
|
|
goto err;
|
2019-05-15 14:37:46 +03:00
|
|
|
}
|
|
|
|
}
|
2024-02-07 00:51:15 +03:00
|
|
|
|
|
|
|
return true;
|
2024-08-01 20:41:01 +03:00
|
|
|
|
|
|
|
err:
|
|
|
|
migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
|
|
|
|
MIGRATION_STATUS_FAILED);
|
|
|
|
return false;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
2024-02-29 18:30:09 +03:00
|
|
|
bool multifd_recv(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static int next_recv_channel;
|
|
|
|
MultiFDRecvParams *p = NULL;
|
|
|
|
MultiFDRecvData *data = multifd_recv_state->data;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* next_channel can remain from a previous migration that was
|
|
|
|
* using more channels, so ensure it doesn't overflow if the
|
|
|
|
* limit is lower now.
|
|
|
|
*/
|
|
|
|
next_recv_channel %= migrate_multifd_channels();
|
|
|
|
for (i = next_recv_channel;; i = (i + 1) % migrate_multifd_channels()) {
|
|
|
|
if (multifd_recv_should_exit()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = &multifd_recv_state->params[i];
|
|
|
|
|
|
|
|
if (qatomic_read(&p->pending_job) == false) {
|
|
|
|
next_recv_channel = (i + 1) % migrate_multifd_channels();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Order pending_job read before manipulating p->data below. Pairs
|
|
|
|
* with qatomic_store_release() at multifd_recv_thread().
|
|
|
|
*/
|
|
|
|
smp_mb_acquire();
|
|
|
|
|
|
|
|
assert(!p->data->size);
|
|
|
|
multifd_recv_state->data = p->data;
|
|
|
|
p->data = data;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Order p->data update before setting pending_job. Pairs with
|
|
|
|
* qatomic_load_acquire() at multifd_recv_thread().
|
|
|
|
*/
|
|
|
|
qatomic_store_release(&p->pending_job, true);
|
|
|
|
qemu_sem_post(&p->sem);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiFDRecvData *multifd_get_recv_data(void)
|
|
|
|
{
|
|
|
|
return multifd_recv_state->data;
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
static void multifd_recv_terminate_threads(Error *err)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
trace_multifd_recv_terminate_threads(err != NULL);
|
|
|
|
|
2024-02-21 01:41:08 +03:00
|
|
|
if (qatomic_xchg(&multifd_recv_state->exiting, 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
if (err) {
|
|
|
|
MigrationState *s = migrate_get_current();
|
|
|
|
migrate_set_error(s, err);
|
|
|
|
if (s->state == MIGRATION_STATUS_SETUP ||
|
|
|
|
s->state == MIGRATION_STATUS_ACTIVE) {
|
|
|
|
migrate_set_state(&s->state, s->state,
|
|
|
|
MIGRATION_STATUS_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < migrate_multifd_channels(); i++) {
|
|
|
|
MultiFDRecvParams *p = &multifd_recv_state->params[i];
|
|
|
|
|
2024-02-21 01:41:09 +03:00
|
|
|
/*
|
2024-02-29 18:30:09 +03:00
|
|
|
* The migration thread and channels interact differently
|
|
|
|
* depending on the presence of packets.
|
2024-02-21 01:41:09 +03:00
|
|
|
*/
|
2024-02-29 18:30:08 +03:00
|
|
|
if (multifd_use_packets()) {
|
2024-02-29 18:30:09 +03:00
|
|
|
/*
|
|
|
|
* The channel receives as long as there are packets. When
|
|
|
|
* packets end (i.e. MULTIFD_FLAG_SYNC is reached), the
|
|
|
|
* channel waits for the migration thread to sync. If the
|
|
|
|
* sync never happens, do it here.
|
|
|
|
*/
|
2024-02-29 18:30:08 +03:00
|
|
|
qemu_sem_post(&p->sem_sync);
|
2024-02-29 18:30:09 +03:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The channel waits for the migration thread to give it
|
|
|
|
* work. When the migration thread runs out of work, it
|
|
|
|
* releases the channel and waits for any pending work to
|
|
|
|
* finish. If we reach here (e.g. due to error) before the
|
|
|
|
* work runs out, release the channel.
|
|
|
|
*/
|
|
|
|
qemu_sem_post(&p->sem);
|
2024-02-29 18:30:08 +03:00
|
|
|
}
|
2024-02-21 01:41:09 +03:00
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
/*
|
|
|
|
* We could arrive here for two reasons:
|
|
|
|
* - normal quit, i.e. everything went fine, just finished
|
|
|
|
* - error quit: We close the channels so the channel threads
|
|
|
|
* finish the qio_channel_read_all_eof()
|
|
|
|
*/
|
|
|
|
if (p->c) {
|
|
|
|
qio_channel_shutdown(p->c, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:55 +03:00
|
|
|
void multifd_recv_shutdown(void)
|
2023-02-10 09:36:31 +03:00
|
|
|
{
|
2023-03-02 00:10:29 +03:00
|
|
|
if (migrate_multifd()) {
|
2023-02-10 09:36:31 +03:00
|
|
|
multifd_recv_terminate_threads(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:54 +03:00
|
|
|
static void multifd_recv_cleanup_channel(MultiFDRecvParams *p)
|
|
|
|
{
|
|
|
|
migration_ioc_unregister_yank(p->c);
|
|
|
|
object_unref(OBJECT(p->c));
|
|
|
|
p->c = NULL;
|
|
|
|
qemu_mutex_destroy(&p->mutex);
|
|
|
|
qemu_sem_destroy(&p->sem_sync);
|
2024-02-29 18:30:09 +03:00
|
|
|
qemu_sem_destroy(&p->sem);
|
2024-08-20 17:44:29 +03:00
|
|
|
g_free(p->data);
|
|
|
|
p->data = NULL;
|
2024-02-02 13:28:54 +03:00
|
|
|
g_free(p->name);
|
|
|
|
p->name = NULL;
|
|
|
|
p->packet_len = 0;
|
|
|
|
g_free(p->packet);
|
|
|
|
p->packet = NULL;
|
|
|
|
g_free(p->normal);
|
|
|
|
p->normal = NULL;
|
2024-03-11 21:00:12 +03:00
|
|
|
g_free(p->zero);
|
|
|
|
p->zero = NULL;
|
2024-02-02 13:28:54 +03:00
|
|
|
multifd_recv_state->ops->recv_cleanup(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void multifd_recv_cleanup_state(void)
|
|
|
|
{
|
|
|
|
qemu_sem_destroy(&multifd_recv_state->sem_sync);
|
|
|
|
g_free(multifd_recv_state->params);
|
|
|
|
multifd_recv_state->params = NULL;
|
2024-02-29 18:30:09 +03:00
|
|
|
g_free(multifd_recv_state->data);
|
|
|
|
multifd_recv_state->data = NULL;
|
2024-02-02 13:28:54 +03:00
|
|
|
g_free(multifd_recv_state);
|
|
|
|
multifd_recv_state = NULL;
|
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:55 +03:00
|
|
|
void multifd_recv_cleanup(void)
|
2020-01-22 18:16:07 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2023-03-02 00:10:29 +03:00
|
|
|
if (!migrate_multifd()) {
|
2023-02-10 09:36:28 +03:00
|
|
|
return;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
multifd_recv_terminate_threads(NULL);
|
|
|
|
for (i = 0; i < migrate_multifd_channels(); i++) {
|
|
|
|
MultiFDRecvParams *p = &multifd_recv_state->params[i];
|
|
|
|
|
2024-02-07 00:51:14 +03:00
|
|
|
if (p->thread_created) {
|
|
|
|
qemu_thread_join(&p->thread);
|
|
|
|
}
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
for (i = 0; i < migrate_multifd_channels(); i++) {
|
2024-02-02 13:28:54 +03:00
|
|
|
multifd_recv_cleanup_channel(&multifd_recv_state->params[i]);
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
2024-02-02 13:28:54 +03:00
|
|
|
multifd_recv_cleanup_state();
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void multifd_recv_sync_main(void)
|
|
|
|
{
|
2024-02-29 18:29:55 +03:00
|
|
|
int thread_count = migrate_multifd_channels();
|
2024-02-29 18:30:15 +03:00
|
|
|
bool file_based = !multifd_use_packets();
|
2020-01-22 18:16:07 +03:00
|
|
|
int i;
|
|
|
|
|
2024-02-29 18:30:15 +03:00
|
|
|
if (!migrate_multifd()) {
|
2020-01-22 18:16:07 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:30:15 +03:00
|
|
|
/*
|
|
|
|
* File-based channels don't use packets and therefore need to
|
|
|
|
* wait for more work. Release them to start the sync.
|
|
|
|
*/
|
|
|
|
if (file_based) {
|
|
|
|
for (i = 0; i < thread_count; i++) {
|
|
|
|
MultiFDRecvParams *p = &multifd_recv_state->params[i];
|
|
|
|
|
|
|
|
trace_multifd_recv_sync_main_signal(p->id);
|
|
|
|
qemu_sem_post(&p->sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:29:55 +03:00
|
|
|
/*
|
|
|
|
* Initiate the synchronization by waiting for all channels.
|
2024-02-29 18:30:15 +03:00
|
|
|
*
|
2024-02-29 18:29:55 +03:00
|
|
|
* For socket-based migration this means each channel has received
|
|
|
|
* the SYNC packet on the stream.
|
2024-02-29 18:30:15 +03:00
|
|
|
*
|
|
|
|
* For file-based migration this means each channel is done with
|
|
|
|
* the work (pending_job=false).
|
2024-02-29 18:29:55 +03:00
|
|
|
*/
|
|
|
|
for (i = 0; i < thread_count; i++) {
|
|
|
|
trace_multifd_recv_sync_main_wait(i);
|
2020-01-22 18:16:07 +03:00
|
|
|
qemu_sem_wait(&multifd_recv_state->sem_sync);
|
|
|
|
}
|
2024-02-29 18:29:55 +03:00
|
|
|
|
2024-02-29 18:30:15 +03:00
|
|
|
if (file_based) {
|
|
|
|
/*
|
|
|
|
* For file-based loading is done in one iteration. We're
|
|
|
|
* done.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:29:55 +03:00
|
|
|
/*
|
|
|
|
* Sync done. Release the channels for the next iteration.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < thread_count; i++) {
|
2020-01-22 18:16:07 +03:00
|
|
|
MultiFDRecvParams *p = &multifd_recv_state->params[i];
|
|
|
|
|
2020-04-04 07:21:08 +03:00
|
|
|
WITH_QEMU_LOCK_GUARD(&p->mutex) {
|
|
|
|
if (multifd_recv_state->packet_num < p->packet_num) {
|
|
|
|
multifd_recv_state->packet_num = p->packet_num;
|
|
|
|
}
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
trace_multifd_recv_sync_main_signal(p->id);
|
|
|
|
qemu_sem_post(&p->sem_sync);
|
|
|
|
}
|
|
|
|
trace_multifd_recv_sync_main(multifd_recv_state->packet_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *multifd_recv_thread(void *opaque)
|
|
|
|
{
|
|
|
|
MultiFDRecvParams *p = opaque;
|
|
|
|
Error *local_err = NULL;
|
2024-02-29 18:30:08 +03:00
|
|
|
bool use_packets = multifd_use_packets();
|
2020-01-22 18:16:07 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
trace_multifd_recv_thread_start(p->id);
|
|
|
|
rcu_register_thread();
|
|
|
|
|
|
|
|
while (true) {
|
2024-02-29 18:30:08 +03:00
|
|
|
uint32_t flags = 0;
|
2024-02-29 18:30:07 +03:00
|
|
|
bool has_data = false;
|
|
|
|
p->normal_num = 0;
|
2020-01-22 18:16:07 +03:00
|
|
|
|
2024-02-29 18:30:08 +03:00
|
|
|
if (use_packets) {
|
2024-02-29 18:30:09 +03:00
|
|
|
if (multifd_recv_should_exit()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:30:08 +03:00
|
|
|
ret = qio_channel_read_all_eof(p->c, (void *)p->packet,
|
|
|
|
p->packet_len, &local_err);
|
|
|
|
if (ret == 0 || ret == -1) { /* 0: EOF -1: Error */
|
|
|
|
break;
|
|
|
|
}
|
2020-01-22 18:16:07 +03:00
|
|
|
|
2024-02-29 18:30:08 +03:00
|
|
|
qemu_mutex_lock(&p->mutex);
|
|
|
|
ret = multifd_recv_unfill_packet(p, &local_err);
|
|
|
|
if (ret) {
|
|
|
|
qemu_mutex_unlock(&p->mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = p->flags;
|
|
|
|
/* recv methods don't know how to handle the SYNC flag */
|
|
|
|
p->flags &= ~MULTIFD_FLAG_SYNC;
|
2024-08-27 20:45:58 +03:00
|
|
|
if (!(flags & MULTIFD_FLAG_SYNC)) {
|
|
|
|
has_data = p->normal_num || p->zero_num;
|
|
|
|
}
|
2020-01-22 18:16:07 +03:00
|
|
|
qemu_mutex_unlock(&p->mutex);
|
2024-02-29 18:30:09 +03:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* No packets, so we need to wait for the vmstate code to
|
|
|
|
* give us work.
|
|
|
|
*/
|
|
|
|
qemu_sem_wait(&p->sem);
|
|
|
|
|
|
|
|
if (multifd_recv_should_exit()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pairs with qatomic_store_release() at multifd_recv() */
|
|
|
|
if (!qatomic_load_acquire(&p->pending_job)) {
|
|
|
|
/*
|
|
|
|
* Migration thread did not send work, this is
|
|
|
|
* equivalent to pending_sync on the sending
|
|
|
|
* side. Post sem_sync to notify we reached this
|
|
|
|
* point.
|
|
|
|
*/
|
|
|
|
qemu_sem_post(&multifd_recv_state->sem_sync);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
has_data = !!p->data->size;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
2024-02-29 18:30:07 +03:00
|
|
|
if (has_data) {
|
|
|
|
ret = multifd_recv_state->ops->recv(p, &local_err);
|
2020-01-22 18:16:07 +03:00
|
|
|
if (ret != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:30:08 +03:00
|
|
|
if (use_packets) {
|
|
|
|
if (flags & MULTIFD_FLAG_SYNC) {
|
|
|
|
qemu_sem_post(&multifd_recv_state->sem_sync);
|
|
|
|
qemu_sem_wait(&p->sem_sync);
|
|
|
|
}
|
2024-02-29 18:30:09 +03:00
|
|
|
} else {
|
|
|
|
p->data->size = 0;
|
|
|
|
/*
|
|
|
|
* Order data->size update before clearing
|
|
|
|
* pending_job. Pairs with smp_mb_acquire() at
|
|
|
|
* multifd_recv().
|
|
|
|
*/
|
|
|
|
qatomic_store_release(&p->pending_job, false);
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (local_err) {
|
|
|
|
multifd_recv_terminate_threads(local_err);
|
2020-05-06 12:54:16 +03:00
|
|
|
error_free(local_err);
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rcu_unregister_thread();
|
2024-08-27 20:45:56 +03:00
|
|
|
trace_multifd_recv_thread_end(p->id, p->packets_recved);
|
2020-01-22 18:16:07 +03:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-02-02 13:28:55 +03:00
|
|
|
int multifd_recv_setup(Error **errp)
|
2020-01-22 18:16:07 +03:00
|
|
|
{
|
|
|
|
int thread_count;
|
2024-08-27 20:45:49 +03:00
|
|
|
uint32_t page_count = multifd_ram_page_count();
|
2024-02-29 18:30:08 +03:00
|
|
|
bool use_packets = multifd_use_packets();
|
2020-01-22 18:16:07 +03:00
|
|
|
uint8_t i;
|
|
|
|
|
2022-12-20 21:44:18 +03:00
|
|
|
/*
|
|
|
|
* Return successfully if multiFD recv state is already initialised
|
|
|
|
* or multiFD is not enabled.
|
|
|
|
*/
|
2023-03-02 00:10:29 +03:00
|
|
|
if (multifd_recv_state || !migrate_multifd()) {
|
2020-01-22 18:16:07 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2022-12-20 21:44:18 +03:00
|
|
|
|
2020-01-22 18:16:07 +03:00
|
|
|
thread_count = migrate_multifd_channels();
|
|
|
|
multifd_recv_state = g_malloc0(sizeof(*multifd_recv_state));
|
|
|
|
multifd_recv_state->params = g_new0(MultiFDRecvParams, thread_count);
|
2024-02-29 18:30:09 +03:00
|
|
|
|
|
|
|
multifd_recv_state->data = g_new0(MultiFDRecvData, 1);
|
|
|
|
multifd_recv_state->data->size = 0;
|
|
|
|
|
2020-09-23 13:56:46 +03:00
|
|
|
qatomic_set(&multifd_recv_state->count, 0);
|
2024-02-21 01:41:08 +03:00
|
|
|
qatomic_set(&multifd_recv_state->exiting, 0);
|
2020-01-22 18:16:07 +03:00
|
|
|
qemu_sem_init(&multifd_recv_state->sem_sync, 0);
|
2019-05-15 14:37:46 +03:00
|
|
|
multifd_recv_state->ops = multifd_ops[migrate_multifd_compression()];
|
2020-01-22 18:16:07 +03:00
|
|
|
|
|
|
|
for (i = 0; i < thread_count; i++) {
|
|
|
|
MultiFDRecvParams *p = &multifd_recv_state->params[i];
|
|
|
|
|
|
|
|
qemu_mutex_init(&p->mutex);
|
|
|
|
qemu_sem_init(&p->sem_sync, 0);
|
2024-02-29 18:30:09 +03:00
|
|
|
qemu_sem_init(&p->sem, 0);
|
|
|
|
p->pending_job = false;
|
2020-01-22 18:16:07 +03:00
|
|
|
p->id = i;
|
2024-02-29 18:30:08 +03:00
|
|
|
|
2024-02-29 18:30:09 +03:00
|
|
|
p->data = g_new0(MultiFDRecvData, 1);
|
|
|
|
p->data->size = 0;
|
|
|
|
|
2024-02-29 18:30:08 +03:00
|
|
|
if (use_packets) {
|
|
|
|
p->packet_len = sizeof(MultiFDPacket_t)
|
|
|
|
+ sizeof(uint64_t) * page_count;
|
|
|
|
p->packet = g_malloc0(p->packet_len);
|
|
|
|
}
|
migration: Rename thread debug names
The postcopy thread names on dest QEMU are slightly confusing, partly I'll
need to blame myself on 36f62f11e4 ("migration: Postcopy preemption
preparation on channel creation"). E.g., "fault-fast" reads like a fast
version of "fault-default", but it's actually the fast version of
"postcopy/listen".
Taking this chance, rename all the migration threads with proper rules.
Considering we only have 15 chars usable, prefix all threads with "mig/",
meanwhile identify src/dst threads properly this time. So now most thread
names will look like "mig/DIR/xxx", where DIR will be "src"/"dst", except
the bg-snapshot thread which doesn't have a direction.
For multifd threads, making them "mig/{src|dst}/{send|recv}_%d".
We used to have "live_migration" thread for a very long time, now it's
called "mig/src/main". We may hope to have "mig/dst/main" soon but not
yet.
Reviewed-by: Fabiano Rosas <farosas@suse.de>
Reviewed-by: Zhijian Li (Fujitsu) <lizhijian@fujitsu.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
2024-06-20 01:30:37 +03:00
|
|
|
p->name = g_strdup_printf("mig/dst/recv_%d", i);
|
2021-11-22 15:41:06 +03:00
|
|
|
p->normal = g_new0(ram_addr_t, page_count);
|
2024-03-11 21:00:12 +03:00
|
|
|
p->zero = g_new0(ram_addr_t, page_count);
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
2019-05-15 14:37:46 +03:00
|
|
|
|
|
|
|
for (i = 0; i < thread_count; i++) {
|
|
|
|
MultiFDRecvParams *p = &multifd_recv_state->params[i];
|
|
|
|
int ret;
|
|
|
|
|
2023-12-31 12:30:16 +03:00
|
|
|
ret = multifd_recv_state->ops->recv_setup(p, errp);
|
2019-05-15 14:37:46 +03:00
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 18:16:07 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool multifd_recv_all_channels_created(void)
|
|
|
|
{
|
|
|
|
int thread_count = migrate_multifd_channels();
|
|
|
|
|
2023-03-02 00:10:29 +03:00
|
|
|
if (!migrate_multifd()) {
|
2020-01-22 18:16:07 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-21 14:28:33 +03:00
|
|
|
if (!multifd_recv_state) {
|
|
|
|
/* Called before any connections created */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:56:46 +03:00
|
|
|
return thread_count == qatomic_read(&multifd_recv_state->count);
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to receive all multifd channels to get ready for the migration.
|
2022-12-20 21:44:18 +03:00
|
|
|
* Sets @errp when failing to receive the current channel.
|
2020-01-22 18:16:07 +03:00
|
|
|
*/
|
2022-12-20 21:44:18 +03:00
|
|
|
void multifd_recv_new_channel(QIOChannel *ioc, Error **errp)
|
2020-01-22 18:16:07 +03:00
|
|
|
{
|
|
|
|
MultiFDRecvParams *p;
|
|
|
|
Error *local_err = NULL;
|
2024-02-29 18:30:08 +03:00
|
|
|
bool use_packets = multifd_use_packets();
|
2020-01-22 18:16:07 +03:00
|
|
|
int id;
|
|
|
|
|
2024-02-29 18:30:08 +03:00
|
|
|
if (use_packets) {
|
|
|
|
id = multifd_recv_initial_packet(ioc, &local_err);
|
|
|
|
if (id < 0) {
|
|
|
|
multifd_recv_terminate_threads(local_err);
|
|
|
|
error_propagate_prepend(errp, local_err,
|
|
|
|
"failed to receive packet"
|
|
|
|
" via multifd channel %d: ",
|
|
|
|
qatomic_read(&multifd_recv_state->count));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trace_multifd_recv_new_channel(id);
|
|
|
|
} else {
|
2024-02-29 18:30:12 +03:00
|
|
|
id = qatomic_read(&multifd_recv_state->count);
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
p = &multifd_recv_state->params[id];
|
|
|
|
if (p->c != NULL) {
|
|
|
|
error_setg(&local_err, "multifd: received id '%d' already setup'",
|
|
|
|
id);
|
|
|
|
multifd_recv_terminate_threads(local_err);
|
|
|
|
error_propagate(errp, local_err);
|
2022-12-20 21:44:18 +03:00
|
|
|
return;
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|
|
|
|
p->c = ioc;
|
|
|
|
object_ref(OBJECT(ioc));
|
|
|
|
|
2024-02-07 00:51:14 +03:00
|
|
|
p->thread_created = true;
|
2020-01-22 18:16:07 +03:00
|
|
|
qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p,
|
|
|
|
QEMU_THREAD_JOINABLE);
|
2020-09-23 13:56:46 +03:00
|
|
|
qatomic_inc(&multifd_recv_state->count);
|
2020-01-22 18:16:07 +03:00
|
|
|
}
|