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