master
h 211 lines 7.28 KB
Raw
1 /*
2 * Postcopy migration for RAM
3 *
4 * Copyright 2013 Red Hat, Inc. and/or its affiliates
5 *
6 * Authors:
7 * Dave Gilbert <dgilbert@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 *
12 */
13 #ifndef QEMU_POSTCOPY_RAM_H
14 #define QEMU_POSTCOPY_RAM_H
15
16 #include "qapi/qapi-types-migration.h"
17
18 /* Return true if the host supports everything we need to do postcopy-ram */
19 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis,
20 Error **errp);
21
22 /*
23 * Make all of RAM sensitive to accesses to areas that haven't yet been written
24 * and wire up anything necessary to deal with it.
25 */
26 int postcopy_ram_incoming_setup(MigrationIncomingState *mis, Error **errp);
27
28 /*
29 * Initialise postcopy-ram, setting the RAM to a state where we can go into
30 * postcopy later; must be called prior to any precopy.
31 * called from ram.c's similarly named ram_postcopy_incoming_init
32 */
33 int postcopy_ram_incoming_init(MigrationIncomingState *mis, Error **errp);
34
35 /*
36 * At the end of a migration where postcopy_ram_incoming_init was called.
37 */
38 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis);
39
40 /*
41 * Userfault requires us to mark RAM as NOHUGEPAGE prior to discard
42 * however leaving it until after precopy means that most of the precopy
43 * data is still THPd
44 */
45 int postcopy_ram_prepare_discard(MigrationIncomingState *mis);
46
47 /*
48 * Called at the start of each RAMBlock by the bitmap code.
49 */
50 void postcopy_discard_send_init(MigrationState *ms, const char *name);
51
52 /*
53 * Called by the bitmap code for each chunk to discard.
54 * May send a discard message, may just leave it queued to
55 * be sent later.
56 * @start,@length: a range of pages in the migration bitmap in the
57 * RAM block passed to postcopy_discard_send_init() (length=1 is one page)
58 */
59 void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
60 unsigned long length);
61
62 /*
63 * Called at the end of each RAMBlock by the bitmap code.
64 * Sends any outstanding discard messages.
65 */
66 void postcopy_discard_send_finish(MigrationState *ms);
67
68 /*
69 * Place a page (from) at (host) efficiently
70 * There are restrictions on how 'from' must be mapped, in general best
71 * to use other postcopy_ routines to allocate.
72 * returns 0 on success
73 */
74 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
75 RAMBlock *rb);
76
77 /*
78 * Place a zero page at (host) atomically
79 * returns 0 on success
80 */
81 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
82 RAMBlock *rb);
83
84 /* The current postcopy state is read/set by postcopy_state_get/set
85 * which update it atomically.
86 * The state is updated as postcopy messages are received, and
87 * in general only one thread should be writing to the state at any one
88 * time, initially the main thread and then the listen thread;
89 * Corner cases are where either thread finishes early and/or errors.
90 * The state is checked as messages are received to ensure that
91 * the source is sending us messages in the correct order.
92 * The state is also used by the RAM reception code to know if it
93 * has to place pages atomically, and the cleanup code at the end of
94 * the main thread to know if it has to delay cleanup until the end
95 * of postcopy.
96 */
97 typedef enum {
98 POSTCOPY_INCOMING_NONE = 0, /* Initial state - no postcopy */
99 POSTCOPY_INCOMING_ADVISE,
100 POSTCOPY_INCOMING_DISCARD,
101 POSTCOPY_INCOMING_LISTENING,
102 POSTCOPY_INCOMING_RUNNING,
103 POSTCOPY_INCOMING_END
104 } PostcopyState;
105
106 PostcopyState postcopy_state_get(void);
107 /* Set the state and return the old state */
108 PostcopyState postcopy_state_set(PostcopyState new_state);
109
110 void postcopy_fault_thread_notify(MigrationIncomingState *mis);
111
112 /*
113 * To be called once at the start before any device initialisation
114 */
115 void postcopy_infrastructure_init(void);
116
117 /* Add a notifier to a list to be called when checking whether the devices
118 * can support postcopy.
119 * It's data is a *PostcopyNotifyData
120 * It should return 0 if OK, or a negative value on failure.
121 * On failure it must set the data->errp to an error.
122 *
123 */
124 enum PostcopyNotifyReason {
125 POSTCOPY_NOTIFY_PROBE = 0,
126 POSTCOPY_NOTIFY_INBOUND_ADVISE,
127 POSTCOPY_NOTIFY_INBOUND_LISTEN,
128 POSTCOPY_NOTIFY_INBOUND_END,
129 };
130
131 struct PostcopyNotifyData {
132 enum PostcopyNotifyReason reason;
133 };
134
135 void postcopy_add_notifier(NotifierWithReturn *nn);
136 void postcopy_remove_notifier(NotifierWithReturn *n);
137 /* Call the notifier list set by postcopy_add_start_notifier */
138 int postcopy_notify(enum PostcopyNotifyReason reason, Error **errp);
139 bool postcopy_notifier_list_empty(void);
140
141 void postcopy_thread_create(MigrationIncomingState *mis,
142 QemuThread *thread, const char *name,
143 void *(*fn)(void *), int joinable);
144
145 struct PostCopyFD;
146
147 /* ufd is a pointer to the struct uffd_msg *TODO: more Portable! */
148 typedef int (*pcfdhandler)(struct PostCopyFD *pcfd, void *ufd);
149 /* Notification to wake, either on place or on reception of
150 * a fault on something that's already arrived (race)
151 */
152 typedef int (*pcfdwake)(struct PostCopyFD *pcfd, RAMBlock *rb, uint64_t offset);
153
154 struct PostCopyFD {
155 int fd;
156 /* Data to pass to handler */
157 void *data;
158 /* Handler to be called whenever we get a poll event */
159 pcfdhandler handler;
160 /* Notification to wake shared client */
161 pcfdwake waker;
162 /* A string to use in error messages */
163 const char *idstr;
164 };
165
166 /* Register a userfaultfd owned by an external process for
167 * shared memory.
168 */
169 void postcopy_register_shared_ufd(struct PostCopyFD *pcfd);
170 void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd);
171 /* Call each of the shared 'waker's registered telling them of
172 * availability of a block.
173 */
174 int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset);
175 /* postcopy_wake_shared: Notify a client ufd that a page is available
176 *
177 * Returns 0 on success
178 *
179 * @pcfd: Structure with fd, handler and name as above
180 * @client_addr: Address in the client program, not QEMU
181 * @rb: The RAMBlock the page is in
182 */
183 int postcopy_wake_shared(struct PostCopyFD *pcfd, uint64_t client_addr,
184 RAMBlock *rb);
185 /* Callback from shared fault handlers to ask for a page */
186 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
187 uint64_t client_addr, uint64_t offset);
188
189 /* Hard-code channels for now for postcopy preemption */
190 enum PostcopyChannels {
191 RAM_CHANNEL_PRECOPY = 0,
192 RAM_CHANNEL_POSTCOPY = 1,
193 RAM_CHANNEL_MAX,
194 };
195
196 void postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file);
197 void postcopy_preempt_setup(MigrationState *s);
198 int postcopy_preempt_establish_channel(MigrationState *s);
199 bool postcopy_is_paused(MigrationStatus status);
200 bool try_mark_postcopy_blocktime_begin(MigrationIncomingState *mis,
201 RAMBlock *rb, ram_addr_t start,
202 uint64_t haddr, uint32_t tid);
203 void mark_postcopy_blocktime_begin(uintptr_t addr, uint32_t ptid,
204 RAMBlock *rb);
205
206 int postcopy_incoming_setup(MigrationIncomingState *mis, Error **errp);
207 int postcopy_incoming_cleanup(MigrationIncomingState *mis);
208
209 void postcopy_ram_eager_load_setup(MigrationIncomingState *mis);
210
211 #endif