2015-11-05 19:10:55 +01:00
|
|
|
/*
|
|
|
|
* Postcopy migration for RAM
|
|
|
|
*
|
|
|
|
* Copyright 2013 Red Hat, Inc. and/or its affiliates
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Dave Gilbert <dgilbert@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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#ifndef QEMU_POSTCOPY_RAM_H
|
|
|
|
#define QEMU_POSTCOPY_RAM_H
|
|
|
|
|
|
|
|
/* Return true if the host supports everything we need to do postcopy-ram */
|
2023-04-26 03:15:14 +02:00
|
|
|
bool postcopy_ram_supported_by_host(MigrationIncomingState *mis,
|
|
|
|
Error **errp);
|
2015-11-05 19:10:55 +01:00
|
|
|
|
2015-11-05 19:11:04 +01:00
|
|
|
/*
|
|
|
|
* Make all of RAM sensitive to accesses to areas that haven't yet been written
|
|
|
|
* and wire up anything necessary to deal with it.
|
|
|
|
*/
|
2019-10-10 03:13:15 +02:00
|
|
|
int postcopy_ram_incoming_setup(MigrationIncomingState *mis);
|
2015-11-05 19:11:04 +01:00
|
|
|
|
2015-11-05 19:11:03 +01:00
|
|
|
/*
|
|
|
|
* Initialise postcopy-ram, setting the RAM to a state where we can go into
|
|
|
|
* postcopy later; must be called prior to any precopy.
|
|
|
|
* called from ram.c's similarly named ram_postcopy_incoming_init
|
|
|
|
*/
|
2018-06-20 22:27:36 +02:00
|
|
|
int postcopy_ram_incoming_init(MigrationIncomingState *mis);
|
2015-11-05 19:11:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* At the end of a migration where postcopy_ram_incoming_init was called.
|
|
|
|
*/
|
|
|
|
int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis);
|
|
|
|
|
2015-11-05 19:11:20 +01:00
|
|
|
/*
|
|
|
|
* Userfault requires us to mark RAM as NOHUGEPAGE prior to discard
|
|
|
|
* however leaving it until after precopy means that most of the precopy
|
|
|
|
* data is still THPd
|
|
|
|
*/
|
|
|
|
int postcopy_ram_prepare_discard(MigrationIncomingState *mis);
|
2015-11-05 19:11:02 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Called at the start of each RAMBlock by the bitmap code.
|
|
|
|
*/
|
2019-07-24 03:07:21 +02:00
|
|
|
void postcopy_discard_send_init(MigrationState *ms, const char *name);
|
2015-11-05 19:11:02 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the bitmap code for each chunk to discard.
|
|
|
|
* May send a discard message, may just leave it queued to
|
|
|
|
* be sent later.
|
|
|
|
* @start,@length: a range of pages in the migration bitmap in the
|
|
|
|
* RAM block passed to postcopy_discard_send_init() (length=1 is one page)
|
|
|
|
*/
|
2019-07-24 03:07:21 +02:00
|
|
|
void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
|
|
|
|
unsigned long length);
|
2015-11-05 19:11:02 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Called at the end of each RAMBlock by the bitmap code.
|
2019-07-24 03:07:21 +02:00
|
|
|
* Sends any outstanding discard messages.
|
2015-11-05 19:11:02 +01:00
|
|
|
*/
|
2019-07-24 03:07:21 +02:00
|
|
|
void postcopy_discard_send_finish(MigrationState *ms);
|
2015-11-05 19:11:02 +01:00
|
|
|
|
2015-11-05 19:11:10 +01:00
|
|
|
/*
|
|
|
|
* Place a page (from) at (host) efficiently
|
|
|
|
* There are restrictions on how 'from' must be mapped, in general best
|
|
|
|
* to use other postcopy_ routines to allocate.
|
|
|
|
* returns 0 on success
|
|
|
|
*/
|
2017-02-24 19:28:35 +01:00
|
|
|
int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
|
2017-10-05 13:13:18 +02:00
|
|
|
RAMBlock *rb);
|
2015-11-05 19:11:10 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Place a zero page at (host) atomically
|
|
|
|
* returns 0 on success
|
|
|
|
*/
|
2017-02-24 19:28:35 +01:00
|
|
|
int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
|
2017-10-05 13:13:18 +02:00
|
|
|
RAMBlock *rb);
|
2015-11-05 19:11:10 +01:00
|
|
|
|
2017-04-24 16:50:35 +02:00
|
|
|
/* The current postcopy state is read/set by postcopy_state_get/set
|
|
|
|
* which update it atomically.
|
|
|
|
* The state is updated as postcopy messages are received, and
|
|
|
|
* in general only one thread should be writing to the state at any one
|
|
|
|
* time, initially the main thread and then the listen thread;
|
|
|
|
* Corner cases are where either thread finishes early and/or errors.
|
|
|
|
* The state is checked as messages are received to ensure that
|
|
|
|
* the source is sending us messages in the correct order.
|
|
|
|
* The state is also used by the RAM reception code to know if it
|
|
|
|
* has to place pages atomically, and the cleanup code at the end of
|
|
|
|
* the main thread to know if it has to delay cleanup until the end
|
|
|
|
* of postcopy.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
POSTCOPY_INCOMING_NONE = 0, /* Initial state - no postcopy */
|
|
|
|
POSTCOPY_INCOMING_ADVISE,
|
|
|
|
POSTCOPY_INCOMING_DISCARD,
|
|
|
|
POSTCOPY_INCOMING_LISTENING,
|
|
|
|
POSTCOPY_INCOMING_RUNNING,
|
|
|
|
POSTCOPY_INCOMING_END
|
|
|
|
} PostcopyState;
|
|
|
|
|
|
|
|
PostcopyState postcopy_state_get(void);
|
|
|
|
/* Set the state and return the old state */
|
|
|
|
PostcopyState postcopy_state_set(PostcopyState new_state);
|
|
|
|
|
2018-02-08 11:31:07 +01:00
|
|
|
void postcopy_fault_thread_notify(MigrationIncomingState *mis);
|
|
|
|
|
2018-03-12 18:20:59 +01:00
|
|
|
/*
|
|
|
|
* To be called once at the start before any device initialisation
|
|
|
|
*/
|
|
|
|
void postcopy_infrastructure_init(void);
|
|
|
|
|
|
|
|
/* Add a notifier to a list to be called when checking whether the devices
|
|
|
|
* can support postcopy.
|
|
|
|
* It's data is a *PostcopyNotifyData
|
|
|
|
* It should return 0 if OK, or a negative value on failure.
|
|
|
|
* On failure it must set the data->errp to an error.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
enum PostcopyNotifyReason {
|
|
|
|
POSTCOPY_NOTIFY_PROBE = 0,
|
2018-03-12 18:21:01 +01:00
|
|
|
POSTCOPY_NOTIFY_INBOUND_ADVISE,
|
2018-03-12 18:21:06 +01:00
|
|
|
POSTCOPY_NOTIFY_INBOUND_LISTEN,
|
2018-03-12 18:21:20 +01:00
|
|
|
POSTCOPY_NOTIFY_INBOUND_END,
|
2018-03-12 18:20:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PostcopyNotifyData {
|
|
|
|
enum PostcopyNotifyReason reason;
|
|
|
|
};
|
|
|
|
|
|
|
|
void postcopy_add_notifier(NotifierWithReturn *nn);
|
|
|
|
void postcopy_remove_notifier(NotifierWithReturn *n);
|
|
|
|
/* Call the notifier list set by postcopy_add_start_notifier */
|
|
|
|
int postcopy_notify(enum PostcopyNotifyReason reason, Error **errp);
|
|
|
|
|
2022-03-01 09:39:06 +01:00
|
|
|
void postcopy_thread_create(MigrationIncomingState *mis,
|
|
|
|
QemuThread *thread, const char *name,
|
|
|
|
void *(*fn)(void *), int joinable);
|
|
|
|
|
2018-03-12 18:21:04 +01:00
|
|
|
struct PostCopyFD;
|
|
|
|
|
|
|
|
/* ufd is a pointer to the struct uffd_msg *TODO: more Portable! */
|
|
|
|
typedef int (*pcfdhandler)(struct PostCopyFD *pcfd, void *ufd);
|
2018-03-12 18:21:15 +01:00
|
|
|
/* Notification to wake, either on place or on reception of
|
|
|
|
* a fault on something that's already arrived (race)
|
|
|
|
*/
|
|
|
|
typedef int (*pcfdwake)(struct PostCopyFD *pcfd, RAMBlock *rb, uint64_t offset);
|
2018-03-12 18:21:04 +01:00
|
|
|
|
|
|
|
struct PostCopyFD {
|
|
|
|
int fd;
|
|
|
|
/* Data to pass to handler */
|
|
|
|
void *data;
|
|
|
|
/* Handler to be called whenever we get a poll event */
|
|
|
|
pcfdhandler handler;
|
2018-03-12 18:21:15 +01:00
|
|
|
/* Notification to wake shared client */
|
|
|
|
pcfdwake waker;
|
2018-03-12 18:21:04 +01:00
|
|
|
/* A string to use in error messages */
|
|
|
|
const char *idstr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Register a userfaultfd owned by an external process for
|
|
|
|
* shared memory.
|
|
|
|
*/
|
|
|
|
void postcopy_register_shared_ufd(struct PostCopyFD *pcfd);
|
|
|
|
void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd);
|
2020-09-17 09:50:21 +02:00
|
|
|
/* Call each of the shared 'waker's registered telling them of
|
2018-03-12 18:21:15 +01:00
|
|
|
* availability of a block.
|
|
|
|
*/
|
|
|
|
int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset);
|
2018-03-12 18:21:14 +01:00
|
|
|
/* postcopy_wake_shared: Notify a client ufd that a page is available
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*
|
|
|
|
* @pcfd: Structure with fd, handler and name as above
|
|
|
|
* @client_addr: Address in the client program, not QEMU
|
|
|
|
* @rb: The RAMBlock the page is in
|
|
|
|
*/
|
|
|
|
int postcopy_wake_shared(struct PostCopyFD *pcfd, uint64_t client_addr,
|
|
|
|
RAMBlock *rb);
|
2018-03-12 18:21:12 +01:00
|
|
|
/* Callback from shared fault handlers to ask for a page */
|
|
|
|
int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
|
|
|
|
uint64_t client_addr, uint64_t offset);
|
2018-03-12 18:21:04 +01:00
|
|
|
|
2022-07-07 20:55:02 +02:00
|
|
|
/* Hard-code channels for now for postcopy preemption */
|
|
|
|
enum PostcopyChannels {
|
|
|
|
RAM_CHANNEL_PRECOPY = 0,
|
|
|
|
RAM_CHANNEL_POSTCOPY = 1,
|
|
|
|
RAM_CHANNEL_MAX,
|
|
|
|
};
|
|
|
|
|
2022-12-20 19:44:18 +01:00
|
|
|
void postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file);
|
2023-02-08 21:28:11 +01:00
|
|
|
void postcopy_preempt_setup(MigrationState *s);
|
2023-02-08 21:28:13 +01:00
|
|
|
int postcopy_preempt_establish_channel(MigrationState *s);
|
2022-07-07 20:55:02 +02:00
|
|
|
|
2015-11-05 19:10:55 +01:00
|
|
|
#endif
|