]>
Commit | Line | Data |
---|---|---|
eb59db53 DDAG |
1 | /* |
2 | * Postcopy migration for RAM | |
3 | * | |
4 | * Copyright 2013-2015 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 | ||
14 | /* | |
15 | * Postcopy is a migration technique where the execution flips from the | |
16 | * source to the destination before all the data has been copied. | |
17 | */ | |
18 | ||
1393a485 | 19 | #include "qemu/osdep.h" |
eb59db53 DDAG |
20 | |
21 | #include "qemu-common.h" | |
22 | #include "migration/migration.h" | |
23 | #include "migration/postcopy-ram.h" | |
24 | #include "sysemu/sysemu.h" | |
371ff5a3 | 25 | #include "sysemu/balloon.h" |
eb59db53 DDAG |
26 | #include "qemu/error-report.h" |
27 | #include "trace.h" | |
28 | ||
e0b266f0 DDAG |
29 | /* Arbitrary limit on size of each discard command, |
30 | * keeps them around ~200 bytes | |
31 | */ | |
32 | #define MAX_DISCARDS_PER_COMMAND 12 | |
33 | ||
34 | struct PostcopyDiscardState { | |
35 | const char *ramblock_name; | |
36 | uint64_t offset; /* Bitmap entry for the 1st bit of this RAMBlock */ | |
37 | uint16_t cur_entry; | |
38 | /* | |
39 | * Start and length of a discard range (bytes) | |
40 | */ | |
41 | uint64_t start_list[MAX_DISCARDS_PER_COMMAND]; | |
42 | uint64_t length_list[MAX_DISCARDS_PER_COMMAND]; | |
43 | unsigned int nsentwords; | |
44 | unsigned int nsentcmds; | |
45 | }; | |
46 | ||
eb59db53 DDAG |
47 | /* Postcopy needs to detect accesses to pages that haven't yet been copied |
48 | * across, and efficiently map new pages in, the techniques for doing this | |
49 | * are target OS specific. | |
50 | */ | |
51 | #if defined(__linux__) | |
52 | ||
c4faeed2 | 53 | #include <poll.h> |
eb59db53 DDAG |
54 | #include <sys/ioctl.h> |
55 | #include <sys/syscall.h> | |
eb59db53 DDAG |
56 | #include <asm/types.h> /* for __u64 */ |
57 | #endif | |
58 | ||
d8b9d771 MF |
59 | #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD) |
60 | #include <sys/eventfd.h> | |
eb59db53 DDAG |
61 | #include <linux/userfaultfd.h> |
62 | ||
63 | static bool ufd_version_check(int ufd) | |
64 | { | |
65 | struct uffdio_api api_struct; | |
66 | uint64_t ioctl_mask; | |
67 | ||
68 | api_struct.api = UFFD_API; | |
69 | api_struct.features = 0; | |
70 | if (ioctl(ufd, UFFDIO_API, &api_struct)) { | |
71 | error_report("postcopy_ram_supported_by_host: UFFDIO_API failed: %s", | |
72 | strerror(errno)); | |
73 | return false; | |
74 | } | |
75 | ||
76 | ioctl_mask = (__u64)1 << _UFFDIO_REGISTER | | |
77 | (__u64)1 << _UFFDIO_UNREGISTER; | |
78 | if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) { | |
79 | error_report("Missing userfault features: %" PRIx64, | |
80 | (uint64_t)(~api_struct.ioctls & ioctl_mask)); | |
81 | return false; | |
82 | } | |
83 | ||
84 | return true; | |
85 | } | |
86 | ||
5cf0f48d DDAG |
87 | /* |
88 | * Check for things that postcopy won't support; returns 0 if the block | |
89 | * is fine. | |
90 | */ | |
91 | static int check_range(const char *block_name, void *host_addr, | |
92 | ram_addr_t offset, ram_addr_t length, void *opaque) | |
93 | { | |
94 | RAMBlock *rb = qemu_ram_block_by_name(block_name); | |
95 | ||
96 | if (qemu_ram_pagesize(rb) > getpagesize()) { | |
97 | error_report("Postcopy doesn't support large page sizes yet (%s)", | |
98 | block_name); | |
99 | return -E2BIG; | |
100 | } | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
58b7c17e DDAG |
105 | /* |
106 | * Note: This has the side effect of munlock'ing all of RAM, that's | |
107 | * normally fine since if the postcopy succeeds it gets turned back on at the | |
108 | * end. | |
109 | */ | |
eb59db53 DDAG |
110 | bool postcopy_ram_supported_by_host(void) |
111 | { | |
112 | long pagesize = getpagesize(); | |
113 | int ufd = -1; | |
114 | bool ret = false; /* Error unless we change it */ | |
115 | void *testarea = NULL; | |
116 | struct uffdio_register reg_struct; | |
117 | struct uffdio_range range_struct; | |
118 | uint64_t feature_mask; | |
119 | ||
120 | if ((1ul << qemu_target_page_bits()) > pagesize) { | |
121 | error_report("Target page size bigger than host page size"); | |
122 | goto out; | |
123 | } | |
124 | ||
5cf0f48d DDAG |
125 | /* Check for anything about the RAMBlocks we don't support */ |
126 | if (qemu_ram_foreach_block(check_range, NULL)) { | |
127 | /* check_range will have printed its own error */ | |
128 | goto out; | |
129 | } | |
130 | ||
eb59db53 DDAG |
131 | ufd = syscall(__NR_userfaultfd, O_CLOEXEC); |
132 | if (ufd == -1) { | |
133 | error_report("%s: userfaultfd not available: %s", __func__, | |
134 | strerror(errno)); | |
135 | goto out; | |
136 | } | |
137 | ||
138 | /* Version and features check */ | |
139 | if (!ufd_version_check(ufd)) { | |
140 | goto out; | |
141 | } | |
142 | ||
58b7c17e DDAG |
143 | /* |
144 | * userfault and mlock don't go together; we'll put it back later if | |
145 | * it was enabled. | |
146 | */ | |
147 | if (munlockall()) { | |
148 | error_report("%s: munlockall: %s", __func__, strerror(errno)); | |
149 | return -1; | |
150 | } | |
151 | ||
eb59db53 DDAG |
152 | /* |
153 | * We need to check that the ops we need are supported on anon memory | |
154 | * To do that we need to register a chunk and see the flags that | |
155 | * are returned. | |
156 | */ | |
157 | testarea = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | | |
158 | MAP_ANONYMOUS, -1, 0); | |
159 | if (testarea == MAP_FAILED) { | |
160 | error_report("%s: Failed to map test area: %s", __func__, | |
161 | strerror(errno)); | |
162 | goto out; | |
163 | } | |
164 | g_assert(((size_t)testarea & (pagesize-1)) == 0); | |
165 | ||
166 | reg_struct.range.start = (uintptr_t)testarea; | |
167 | reg_struct.range.len = pagesize; | |
168 | reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING; | |
169 | ||
170 | if (ioctl(ufd, UFFDIO_REGISTER, ®_struct)) { | |
171 | error_report("%s userfault register: %s", __func__, strerror(errno)); | |
172 | goto out; | |
173 | } | |
174 | ||
175 | range_struct.start = (uintptr_t)testarea; | |
176 | range_struct.len = pagesize; | |
177 | if (ioctl(ufd, UFFDIO_UNREGISTER, &range_struct)) { | |
178 | error_report("%s userfault unregister: %s", __func__, strerror(errno)); | |
179 | goto out; | |
180 | } | |
181 | ||
182 | feature_mask = (__u64)1 << _UFFDIO_WAKE | | |
183 | (__u64)1 << _UFFDIO_COPY | | |
184 | (__u64)1 << _UFFDIO_ZEROPAGE; | |
185 | if ((reg_struct.ioctls & feature_mask) != feature_mask) { | |
186 | error_report("Missing userfault map features: %" PRIx64, | |
187 | (uint64_t)(~reg_struct.ioctls & feature_mask)); | |
188 | goto out; | |
189 | } | |
190 | ||
191 | /* Success! */ | |
192 | ret = true; | |
193 | out: | |
194 | if (testarea) { | |
195 | munmap(testarea, pagesize); | |
196 | } | |
197 | if (ufd != -1) { | |
198 | close(ufd); | |
199 | } | |
200 | return ret; | |
201 | } | |
202 | ||
1caddf8a DDAG |
203 | /* |
204 | * Setup an area of RAM so that it *can* be used for postcopy later; this | |
205 | * must be done right at the start prior to pre-copy. | |
206 | * opaque should be the MIS. | |
207 | */ | |
208 | static int init_range(const char *block_name, void *host_addr, | |
209 | ram_addr_t offset, ram_addr_t length, void *opaque) | |
210 | { | |
211 | MigrationIncomingState *mis = opaque; | |
212 | ||
213 | trace_postcopy_init_range(block_name, host_addr, offset, length); | |
214 | ||
215 | /* | |
216 | * We need the whole of RAM to be truly empty for postcopy, so things | |
217 | * like ROMs and any data tables built during init must be zero'd | |
218 | * - we're going to get the copy from the source anyway. | |
219 | * (Precopy will just overwrite this data, so doesn't need the discard) | |
220 | */ | |
d3a5038c | 221 | if (ram_discard_range(mis, block_name, 0, length)) { |
1caddf8a DDAG |
222 | return -1; |
223 | } | |
224 | ||
225 | return 0; | |
226 | } | |
227 | ||
228 | /* | |
229 | * At the end of migration, undo the effects of init_range | |
230 | * opaque should be the MIS. | |
231 | */ | |
232 | static int cleanup_range(const char *block_name, void *host_addr, | |
233 | ram_addr_t offset, ram_addr_t length, void *opaque) | |
234 | { | |
235 | MigrationIncomingState *mis = opaque; | |
236 | struct uffdio_range range_struct; | |
237 | trace_postcopy_cleanup_range(block_name, host_addr, offset, length); | |
238 | ||
239 | /* | |
240 | * We turned off hugepage for the precopy stage with postcopy enabled | |
241 | * we can turn it back on now. | |
242 | */ | |
1d741439 | 243 | qemu_madvise(host_addr, length, QEMU_MADV_HUGEPAGE); |
1caddf8a DDAG |
244 | |
245 | /* | |
246 | * We can also turn off userfault now since we should have all the | |
247 | * pages. It can be useful to leave it on to debug postcopy | |
248 | * if you're not sure it's always getting every page. | |
249 | */ | |
250 | range_struct.start = (uintptr_t)host_addr; | |
251 | range_struct.len = length; | |
252 | ||
253 | if (ioctl(mis->userfault_fd, UFFDIO_UNREGISTER, &range_struct)) { | |
254 | error_report("%s: userfault unregister %s", __func__, strerror(errno)); | |
255 | ||
256 | return -1; | |
257 | } | |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
262 | /* | |
263 | * Initialise postcopy-ram, setting the RAM to a state where we can go into | |
264 | * postcopy later; must be called prior to any precopy. | |
265 | * called from arch_init's similarly named ram_postcopy_incoming_init | |
266 | */ | |
267 | int postcopy_ram_incoming_init(MigrationIncomingState *mis, size_t ram_pages) | |
268 | { | |
269 | if (qemu_ram_foreach_block(init_range, mis)) { | |
270 | return -1; | |
271 | } | |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
276 | /* | |
277 | * At the end of a migration where postcopy_ram_incoming_init was called. | |
278 | */ | |
279 | int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis) | |
280 | { | |
c4faeed2 DDAG |
281 | trace_postcopy_ram_incoming_cleanup_entry(); |
282 | ||
283 | if (mis->have_fault_thread) { | |
284 | uint64_t tmp64; | |
285 | ||
286 | if (qemu_ram_foreach_block(cleanup_range, mis)) { | |
287 | return -1; | |
288 | } | |
289 | /* | |
290 | * Tell the fault_thread to exit, it's an eventfd that should | |
291 | * currently be at 0, we're going to increment it to 1 | |
292 | */ | |
293 | tmp64 = 1; | |
294 | if (write(mis->userfault_quit_fd, &tmp64, 8) == 8) { | |
295 | trace_postcopy_ram_incoming_cleanup_join(); | |
296 | qemu_thread_join(&mis->fault_thread); | |
297 | } else { | |
298 | /* Not much we can do here, but may as well report it */ | |
299 | error_report("%s: incrementing userfault_quit_fd: %s", __func__, | |
300 | strerror(errno)); | |
301 | } | |
302 | trace_postcopy_ram_incoming_cleanup_closeuf(); | |
303 | close(mis->userfault_fd); | |
304 | close(mis->userfault_quit_fd); | |
305 | mis->have_fault_thread = false; | |
1caddf8a DDAG |
306 | } |
307 | ||
371ff5a3 DDAG |
308 | qemu_balloon_inhibit(false); |
309 | ||
58b7c17e DDAG |
310 | if (enable_mlock) { |
311 | if (os_mlock() < 0) { | |
312 | error_report("mlock: %s", strerror(errno)); | |
313 | /* | |
314 | * It doesn't feel right to fail at this point, we have a valid | |
315 | * VM state. | |
316 | */ | |
317 | } | |
318 | } | |
319 | ||
c4faeed2 DDAG |
320 | postcopy_state_set(POSTCOPY_INCOMING_END); |
321 | migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0); | |
322 | ||
696ed9a9 | 323 | if (mis->postcopy_tmp_page) { |
df9ff5e1 | 324 | munmap(mis->postcopy_tmp_page, mis->largest_page_size); |
696ed9a9 DDAG |
325 | mis->postcopy_tmp_page = NULL; |
326 | } | |
c4faeed2 | 327 | trace_postcopy_ram_incoming_cleanup_exit(); |
1caddf8a DDAG |
328 | return 0; |
329 | } | |
330 | ||
f9527107 DDAG |
331 | /* |
332 | * Disable huge pages on an area | |
333 | */ | |
334 | static int nhp_range(const char *block_name, void *host_addr, | |
335 | ram_addr_t offset, ram_addr_t length, void *opaque) | |
336 | { | |
337 | trace_postcopy_nhp_range(block_name, host_addr, offset, length); | |
338 | ||
339 | /* | |
340 | * Before we do discards we need to ensure those discards really | |
341 | * do delete areas of the page, even if THP thinks a hugepage would | |
342 | * be a good idea, so force hugepages off. | |
343 | */ | |
1d741439 | 344 | qemu_madvise(host_addr, length, QEMU_MADV_NOHUGEPAGE); |
f9527107 DDAG |
345 | |
346 | return 0; | |
347 | } | |
348 | ||
349 | /* | |
350 | * Userfault requires us to mark RAM as NOHUGEPAGE prior to discard | |
351 | * however leaving it until after precopy means that most of the precopy | |
352 | * data is still THPd | |
353 | */ | |
354 | int postcopy_ram_prepare_discard(MigrationIncomingState *mis) | |
355 | { | |
356 | if (qemu_ram_foreach_block(nhp_range, mis)) { | |
357 | return -1; | |
358 | } | |
359 | ||
360 | postcopy_state_set(POSTCOPY_INCOMING_DISCARD); | |
361 | ||
362 | return 0; | |
363 | } | |
364 | ||
f0a227ad DDAG |
365 | /* |
366 | * Mark the given area of RAM as requiring notification to unwritten areas | |
367 | * Used as a callback on qemu_ram_foreach_block. | |
368 | * host_addr: Base of area to mark | |
369 | * offset: Offset in the whole ram arena | |
370 | * length: Length of the section | |
371 | * opaque: MigrationIncomingState pointer | |
372 | * Returns 0 on success | |
373 | */ | |
374 | static int ram_block_enable_notify(const char *block_name, void *host_addr, | |
375 | ram_addr_t offset, ram_addr_t length, | |
376 | void *opaque) | |
377 | { | |
378 | MigrationIncomingState *mis = opaque; | |
379 | struct uffdio_register reg_struct; | |
380 | ||
381 | reg_struct.range.start = (uintptr_t)host_addr; | |
382 | reg_struct.range.len = length; | |
383 | reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING; | |
384 | ||
385 | /* Now tell our userfault_fd that it's responsible for this area */ | |
386 | if (ioctl(mis->userfault_fd, UFFDIO_REGISTER, ®_struct)) { | |
387 | error_report("%s userfault register: %s", __func__, strerror(errno)); | |
388 | return -1; | |
389 | } | |
390 | ||
391 | return 0; | |
392 | } | |
393 | ||
394 | /* | |
395 | * Handle faults detected by the USERFAULT markings | |
396 | */ | |
397 | static void *postcopy_ram_fault_thread(void *opaque) | |
398 | { | |
399 | MigrationIncomingState *mis = opaque; | |
c4faeed2 DDAG |
400 | struct uffd_msg msg; |
401 | int ret; | |
402 | size_t hostpagesize = getpagesize(); | |
403 | RAMBlock *rb = NULL; | |
404 | RAMBlock *last_rb = NULL; /* last RAMBlock we sent part of */ | |
f0a227ad | 405 | |
c4faeed2 | 406 | trace_postcopy_ram_fault_thread_entry(); |
f0a227ad | 407 | qemu_sem_post(&mis->fault_thread_sem); |
f0a227ad | 408 | |
c4faeed2 DDAG |
409 | while (true) { |
410 | ram_addr_t rb_offset; | |
c4faeed2 DDAG |
411 | struct pollfd pfd[2]; |
412 | ||
413 | /* | |
414 | * We're mainly waiting for the kernel to give us a faulting HVA, | |
415 | * however we can be told to quit via userfault_quit_fd which is | |
416 | * an eventfd | |
417 | */ | |
418 | pfd[0].fd = mis->userfault_fd; | |
419 | pfd[0].events = POLLIN; | |
420 | pfd[0].revents = 0; | |
421 | pfd[1].fd = mis->userfault_quit_fd; | |
422 | pfd[1].events = POLLIN; /* Waiting for eventfd to go positive */ | |
423 | pfd[1].revents = 0; | |
424 | ||
425 | if (poll(pfd, 2, -1 /* Wait forever */) == -1) { | |
426 | error_report("%s: userfault poll: %s", __func__, strerror(errno)); | |
427 | break; | |
428 | } | |
429 | ||
430 | if (pfd[1].revents) { | |
431 | trace_postcopy_ram_fault_thread_quit(); | |
432 | break; | |
433 | } | |
434 | ||
435 | ret = read(mis->userfault_fd, &msg, sizeof(msg)); | |
436 | if (ret != sizeof(msg)) { | |
437 | if (errno == EAGAIN) { | |
438 | /* | |
439 | * if a wake up happens on the other thread just after | |
440 | * the poll, there is nothing to read. | |
441 | */ | |
442 | continue; | |
443 | } | |
444 | if (ret < 0) { | |
445 | error_report("%s: Failed to read full userfault message: %s", | |
446 | __func__, strerror(errno)); | |
447 | break; | |
448 | } else { | |
449 | error_report("%s: Read %d bytes from userfaultfd expected %zd", | |
450 | __func__, ret, sizeof(msg)); | |
451 | break; /* Lost alignment, don't know what we'd read next */ | |
452 | } | |
453 | } | |
454 | if (msg.event != UFFD_EVENT_PAGEFAULT) { | |
455 | error_report("%s: Read unexpected event %ud from userfaultfd", | |
456 | __func__, msg.event); | |
457 | continue; /* It's not a page fault, shouldn't happen */ | |
458 | } | |
459 | ||
460 | rb = qemu_ram_block_from_host( | |
461 | (void *)(uintptr_t)msg.arg.pagefault.address, | |
f615f396 | 462 | true, &rb_offset); |
c4faeed2 DDAG |
463 | if (!rb) { |
464 | error_report("postcopy_ram_fault_thread: Fault outside guest: %" | |
465 | PRIx64, (uint64_t)msg.arg.pagefault.address); | |
466 | break; | |
467 | } | |
468 | ||
469 | rb_offset &= ~(hostpagesize - 1); | |
470 | trace_postcopy_ram_fault_thread_request(msg.arg.pagefault.address, | |
471 | qemu_ram_get_idstr(rb), | |
472 | rb_offset); | |
473 | ||
474 | /* | |
475 | * Send the request to the source - we want to request one | |
476 | * of our host page sizes (which is >= TPS) | |
477 | */ | |
478 | if (rb != last_rb) { | |
479 | last_rb = rb; | |
480 | migrate_send_rp_req_pages(mis, qemu_ram_get_idstr(rb), | |
481 | rb_offset, hostpagesize); | |
482 | } else { | |
483 | /* Save some space */ | |
484 | migrate_send_rp_req_pages(mis, NULL, | |
485 | rb_offset, hostpagesize); | |
486 | } | |
487 | } | |
488 | trace_postcopy_ram_fault_thread_exit(); | |
f0a227ad DDAG |
489 | return NULL; |
490 | } | |
491 | ||
492 | int postcopy_ram_enable_notify(MigrationIncomingState *mis) | |
493 | { | |
c4faeed2 DDAG |
494 | /* Open the fd for the kernel to give us userfaults */ |
495 | mis->userfault_fd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); | |
496 | if (mis->userfault_fd == -1) { | |
497 | error_report("%s: Failed to open userfault fd: %s", __func__, | |
498 | strerror(errno)); | |
499 | return -1; | |
500 | } | |
501 | ||
502 | /* | |
503 | * Although the host check already tested the API, we need to | |
504 | * do the check again as an ABI handshake on the new fd. | |
505 | */ | |
506 | if (!ufd_version_check(mis->userfault_fd)) { | |
507 | return -1; | |
508 | } | |
509 | ||
510 | /* Now an eventfd we use to tell the fault-thread to quit */ | |
511 | mis->userfault_quit_fd = eventfd(0, EFD_CLOEXEC); | |
512 | if (mis->userfault_quit_fd == -1) { | |
513 | error_report("%s: Opening userfault_quit_fd: %s", __func__, | |
514 | strerror(errno)); | |
515 | close(mis->userfault_fd); | |
516 | return -1; | |
517 | } | |
518 | ||
f0a227ad DDAG |
519 | qemu_sem_init(&mis->fault_thread_sem, 0); |
520 | qemu_thread_create(&mis->fault_thread, "postcopy/fault", | |
521 | postcopy_ram_fault_thread, mis, QEMU_THREAD_JOINABLE); | |
522 | qemu_sem_wait(&mis->fault_thread_sem); | |
523 | qemu_sem_destroy(&mis->fault_thread_sem); | |
c4faeed2 | 524 | mis->have_fault_thread = true; |
f0a227ad DDAG |
525 | |
526 | /* Mark so that we get notified of accesses to unwritten areas */ | |
527 | if (qemu_ram_foreach_block(ram_block_enable_notify, mis)) { | |
528 | return -1; | |
529 | } | |
530 | ||
371ff5a3 DDAG |
531 | /* |
532 | * Ballooning can mark pages as absent while we're postcopying | |
533 | * that would cause false userfaults. | |
534 | */ | |
535 | qemu_balloon_inhibit(true); | |
536 | ||
c4faeed2 DDAG |
537 | trace_postcopy_ram_enable_notify(); |
538 | ||
f0a227ad DDAG |
539 | return 0; |
540 | } | |
541 | ||
696ed9a9 DDAG |
542 | /* |
543 | * Place a host page (from) at (host) atomically | |
544 | * returns 0 on success | |
545 | */ | |
df9ff5e1 DDAG |
546 | int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from, |
547 | size_t pagesize) | |
696ed9a9 DDAG |
548 | { |
549 | struct uffdio_copy copy_struct; | |
550 | ||
551 | copy_struct.dst = (uint64_t)(uintptr_t)host; | |
552 | copy_struct.src = (uint64_t)(uintptr_t)from; | |
df9ff5e1 | 553 | copy_struct.len = pagesize; |
696ed9a9 DDAG |
554 | copy_struct.mode = 0; |
555 | ||
556 | /* copy also acks to the kernel waking the stalled thread up | |
557 | * TODO: We can inhibit that ack and only do it if it was requested | |
558 | * which would be slightly cheaper, but we'd have to be careful | |
559 | * of the order of updating our page state. | |
560 | */ | |
561 | if (ioctl(mis->userfault_fd, UFFDIO_COPY, ©_struct)) { | |
562 | int e = errno; | |
df9ff5e1 DDAG |
563 | error_report("%s: %s copy host: %p from: %p (size: %zd)", |
564 | __func__, strerror(e), host, from, pagesize); | |
696ed9a9 DDAG |
565 | |
566 | return -e; | |
567 | } | |
568 | ||
569 | trace_postcopy_place_page(host); | |
570 | return 0; | |
571 | } | |
572 | ||
573 | /* | |
574 | * Place a zero page at (host) atomically | |
575 | * returns 0 on success | |
576 | */ | |
df9ff5e1 DDAG |
577 | int postcopy_place_page_zero(MigrationIncomingState *mis, void *host, |
578 | size_t pagesize) | |
696ed9a9 | 579 | { |
df9ff5e1 | 580 | trace_postcopy_place_page_zero(host); |
696ed9a9 | 581 | |
df9ff5e1 DDAG |
582 | if (pagesize == getpagesize()) { |
583 | struct uffdio_zeropage zero_struct; | |
584 | zero_struct.range.start = (uint64_t)(uintptr_t)host; | |
585 | zero_struct.range.len = getpagesize(); | |
586 | zero_struct.mode = 0; | |
696ed9a9 | 587 | |
df9ff5e1 DDAG |
588 | if (ioctl(mis->userfault_fd, UFFDIO_ZEROPAGE, &zero_struct)) { |
589 | int e = errno; | |
590 | error_report("%s: %s zero host: %p", | |
591 | __func__, strerror(e), host); | |
696ed9a9 | 592 | |
df9ff5e1 DDAG |
593 | return -e; |
594 | } | |
595 | } else { | |
596 | /* TODO: The kernel can't use UFFDIO_ZEROPAGE for hugepages */ | |
597 | assert(0); | |
696ed9a9 DDAG |
598 | } |
599 | ||
696ed9a9 DDAG |
600 | return 0; |
601 | } | |
602 | ||
603 | /* | |
604 | * Returns a target page of memory that can be mapped at a later point in time | |
605 | * using postcopy_place_page | |
606 | * The same address is used repeatedly, postcopy_place_page just takes the | |
607 | * backing page away. | |
608 | * Returns: Pointer to allocated page | |
609 | * | |
610 | */ | |
611 | void *postcopy_get_tmp_page(MigrationIncomingState *mis) | |
612 | { | |
613 | if (!mis->postcopy_tmp_page) { | |
df9ff5e1 | 614 | mis->postcopy_tmp_page = mmap(NULL, mis->largest_page_size, |
696ed9a9 DDAG |
615 | PROT_READ | PROT_WRITE, MAP_PRIVATE | |
616 | MAP_ANONYMOUS, -1, 0); | |
0e8b3cdf EY |
617 | if (mis->postcopy_tmp_page == MAP_FAILED) { |
618 | mis->postcopy_tmp_page = NULL; | |
696ed9a9 DDAG |
619 | error_report("%s: %s", __func__, strerror(errno)); |
620 | return NULL; | |
621 | } | |
622 | } | |
623 | ||
624 | return mis->postcopy_tmp_page; | |
625 | } | |
626 | ||
eb59db53 DDAG |
627 | #else |
628 | /* No target OS support, stubs just fail */ | |
629 | bool postcopy_ram_supported_by_host(void) | |
630 | { | |
631 | error_report("%s: No OS support", __func__); | |
632 | return false; | |
633 | } | |
634 | ||
1caddf8a DDAG |
635 | int postcopy_ram_incoming_init(MigrationIncomingState *mis, size_t ram_pages) |
636 | { | |
637 | error_report("postcopy_ram_incoming_init: No OS support"); | |
638 | return -1; | |
639 | } | |
640 | ||
641 | int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis) | |
642 | { | |
643 | assert(0); | |
644 | return -1; | |
645 | } | |
646 | ||
f9527107 DDAG |
647 | int postcopy_ram_prepare_discard(MigrationIncomingState *mis) |
648 | { | |
649 | assert(0); | |
650 | return -1; | |
651 | } | |
652 | ||
f0a227ad DDAG |
653 | int postcopy_ram_enable_notify(MigrationIncomingState *mis) |
654 | { | |
655 | assert(0); | |
656 | return -1; | |
657 | } | |
696ed9a9 | 658 | |
df9ff5e1 DDAG |
659 | int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from, |
660 | size_t pagesize) | |
696ed9a9 DDAG |
661 | { |
662 | assert(0); | |
663 | return -1; | |
664 | } | |
665 | ||
df9ff5e1 DDAG |
666 | int postcopy_place_page_zero(MigrationIncomingState *mis, void *host, |
667 | size_t pagesize) | |
696ed9a9 DDAG |
668 | { |
669 | assert(0); | |
670 | return -1; | |
671 | } | |
672 | ||
673 | void *postcopy_get_tmp_page(MigrationIncomingState *mis) | |
674 | { | |
675 | assert(0); | |
676 | return NULL; | |
677 | } | |
678 | ||
eb59db53 DDAG |
679 | #endif |
680 | ||
e0b266f0 DDAG |
681 | /* ------------------------------------------------------------------------- */ |
682 | ||
683 | /** | |
684 | * postcopy_discard_send_init: Called at the start of each RAMBlock before | |
685 | * asking to discard individual ranges. | |
686 | * | |
687 | * @ms: The current migration state. | |
688 | * @offset: the bitmap offset of the named RAMBlock in the migration | |
689 | * bitmap. | |
690 | * @name: RAMBlock that discards will operate on. | |
691 | * | |
692 | * returns: a new PDS. | |
693 | */ | |
694 | PostcopyDiscardState *postcopy_discard_send_init(MigrationState *ms, | |
695 | unsigned long offset, | |
696 | const char *name) | |
697 | { | |
698 | PostcopyDiscardState *res = g_malloc0(sizeof(PostcopyDiscardState)); | |
699 | ||
700 | if (res) { | |
701 | res->ramblock_name = name; | |
702 | res->offset = offset; | |
703 | } | |
704 | ||
705 | return res; | |
706 | } | |
707 | ||
708 | /** | |
709 | * postcopy_discard_send_range: Called by the bitmap code for each chunk to | |
710 | * discard. May send a discard message, may just leave it queued to | |
711 | * be sent later. | |
712 | * | |
713 | * @ms: Current migration state. | |
714 | * @pds: Structure initialised by postcopy_discard_send_init(). | |
715 | * @start,@length: a range of pages in the migration bitmap in the | |
716 | * RAM block passed to postcopy_discard_send_init() (length=1 is one page) | |
717 | */ | |
718 | void postcopy_discard_send_range(MigrationState *ms, PostcopyDiscardState *pds, | |
719 | unsigned long start, unsigned long length) | |
720 | { | |
721 | size_t tp_bits = qemu_target_page_bits(); | |
722 | /* Convert to byte offsets within the RAM block */ | |
723 | pds->start_list[pds->cur_entry] = (start - pds->offset) << tp_bits; | |
724 | pds->length_list[pds->cur_entry] = length << tp_bits; | |
725 | trace_postcopy_discard_send_range(pds->ramblock_name, start, length); | |
726 | pds->cur_entry++; | |
727 | pds->nsentwords++; | |
728 | ||
729 | if (pds->cur_entry == MAX_DISCARDS_PER_COMMAND) { | |
730 | /* Full set, ship it! */ | |
89a02a9f HZ |
731 | qemu_savevm_send_postcopy_ram_discard(ms->to_dst_file, |
732 | pds->ramblock_name, | |
e0b266f0 DDAG |
733 | pds->cur_entry, |
734 | pds->start_list, | |
735 | pds->length_list); | |
736 | pds->nsentcmds++; | |
737 | pds->cur_entry = 0; | |
738 | } | |
739 | } | |
740 | ||
741 | /** | |
742 | * postcopy_discard_send_finish: Called at the end of each RAMBlock by the | |
743 | * bitmap code. Sends any outstanding discard messages, frees the PDS | |
744 | * | |
745 | * @ms: Current migration state. | |
746 | * @pds: Structure initialised by postcopy_discard_send_init(). | |
747 | */ | |
748 | void postcopy_discard_send_finish(MigrationState *ms, PostcopyDiscardState *pds) | |
749 | { | |
750 | /* Anything unsent? */ | |
751 | if (pds->cur_entry) { | |
89a02a9f HZ |
752 | qemu_savevm_send_postcopy_ram_discard(ms->to_dst_file, |
753 | pds->ramblock_name, | |
e0b266f0 DDAG |
754 | pds->cur_entry, |
755 | pds->start_list, | |
756 | pds->length_list); | |
757 | pds->nsentcmds++; | |
758 | } | |
759 | ||
760 | trace_postcopy_discard_send_finish(pds->ramblock_name, pds->nsentwords, | |
761 | pds->nsentcmds); | |
762 | ||
763 | g_free(pds); | |
764 | } |