Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
Juan Quintela | 76cc7b5 | 2015-05-08 13:20:21 +0200 | [diff] [blame] | 5 | * Copyright (c) 2011-2015 Red Hat Inc |
| 6 | * |
| 7 | * Authors: |
| 8 | * Juan Quintela <quintela@redhat.com> |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | * THE SOFTWARE. |
| 27 | */ |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 28 | |
Peter Maydell | 1393a48 | 2016-01-26 18:16:54 +0000 | [diff] [blame] | 29 | #include "qemu/osdep.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 30 | #include "qemu/cutils.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 31 | #include "qemu/bitops.h" |
| 32 | #include "qemu/bitmap.h" |
Peter Maydell | b85ea5f | 2022-02-08 20:08:52 +0000 | [diff] [blame] | 33 | #include "qemu/madvise.h" |
Juan Quintela | 7205c9e | 2015-05-08 13:54:36 +0200 | [diff] [blame] | 34 | #include "qemu/main-loop.h" |
Juan Quintela | 709e3fe | 2017-04-05 21:47:50 +0200 | [diff] [blame] | 35 | #include "xbzrle.h" |
Lukas Straub | b5ca336 | 2023-04-20 11:48:20 +0200 | [diff] [blame] | 36 | #include "ram-compress.h" |
Juan Quintela | 7b1e1a2 | 2017-04-17 20:26:27 +0200 | [diff] [blame] | 37 | #include "ram.h" |
Juan Quintela | 6666c96 | 2017-04-24 20:07:27 +0200 | [diff] [blame] | 38 | #include "migration.h" |
Juan Quintela | 947701c | 2023-04-26 19:04:06 +0200 | [diff] [blame] | 39 | #include "migration-stats.h" |
Juan Quintela | f2a8f0a | 2017-04-24 13:42:55 +0200 | [diff] [blame] | 40 | #include "migration/register.h" |
Juan Quintela | 7b1e1a2 | 2017-04-17 20:26:27 +0200 | [diff] [blame] | 41 | #include "migration/misc.h" |
Juan Quintela | 08a0aee | 2017-04-20 18:52:18 +0200 | [diff] [blame] | 42 | #include "qemu-file.h" |
Juan Quintela | be07b0a | 2017-04-20 13:12:24 +0200 | [diff] [blame] | 43 | #include "postcopy-ram.h" |
Michael S. Tsirkin | 53d37d3 | 2018-05-03 22:50:51 +0300 | [diff] [blame] | 44 | #include "page_cache.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 45 | #include "qemu/error-report.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 46 | #include "qapi/error.h" |
Juan Quintela | ab7cbb0 | 2019-05-15 13:37:46 +0200 | [diff] [blame] | 47 | #include "qapi/qapi-types-migration.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 48 | #include "qapi/qapi-events-migration.h" |
Hyman Huang(黄勇) | acac51b | 2023-06-08 00:12:40 +0800 | [diff] [blame] | 49 | #include "qapi/qapi-commands-migration.h" |
Juan Quintela | 8acabf6 | 2017-10-05 22:00:31 +0200 | [diff] [blame] | 50 | #include "qapi/qmp/qerror.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 51 | #include "trace.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 52 | #include "exec/ram_addr.h" |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 53 | #include "exec/target_page.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 54 | #include "qemu/rcu_queue.h" |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 55 | #include "migration/colo.h" |
Michael S. Tsirkin | 53d37d3 | 2018-05-03 22:50:51 +0300 | [diff] [blame] | 56 | #include "block.h" |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 57 | #include "sysemu/cpu-throttle.h" |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 58 | #include "savevm.h" |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 59 | #include "qemu/iov.h" |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 60 | #include "multifd.h" |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 61 | #include "sysemu/runstate.h" |
Juan Quintela | 4840817 | 2023-10-11 22:35:16 +0200 | [diff] [blame] | 62 | #include "rdma.h" |
Juan Quintela | 1f0776f | 2023-03-01 21:18:45 +0100 | [diff] [blame] | 63 | #include "options.h" |
Hyman Huang(黄勇) | acac51b | 2023-06-08 00:12:40 +0800 | [diff] [blame] | 64 | #include "sysemu/dirtylimit.h" |
| 65 | #include "sysemu/kvm.h" |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 66 | |
Lukas Straub | e5fdf92 | 2021-07-04 18:14:44 +0200 | [diff] [blame] | 67 | #include "hw/boards.h" /* for machine_dump_guest_core() */ |
| 68 | |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 69 | #if defined(__linux__) |
| 70 | #include "qemu/userfaultfd.h" |
| 71 | #endif /* defined(__linux__) */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 72 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 73 | /***********************************************************/ |
| 74 | /* ram save/restore */ |
| 75 | |
Juan Quintela | 7b54876 | 2022-07-28 10:14:42 +0200 | [diff] [blame] | 76 | /* |
| 77 | * RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it |
| 78 | * worked for pages that were filled with the same char. We switched |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 79 | * it to only search for the zero value. And to avoid confusion with |
Juan Quintela | 7b54876 | 2022-07-28 10:14:42 +0200 | [diff] [blame] | 80 | * RAM_SAVE_FLAG_COMPRESS_PAGE just rename it. |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 81 | */ |
Juan Quintela | 7b54876 | 2022-07-28 10:14:42 +0200 | [diff] [blame] | 82 | /* |
| 83 | * RAM_SAVE_FLAG_FULL was obsoleted in 2009, it can be reused now |
| 84 | */ |
| 85 | #define RAM_SAVE_FLAG_FULL 0x01 |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 86 | #define RAM_SAVE_FLAG_ZERO 0x02 |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 87 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
| 88 | #define RAM_SAVE_FLAG_PAGE 0x08 |
| 89 | #define RAM_SAVE_FLAG_EOS 0x10 |
| 90 | #define RAM_SAVE_FLAG_CONTINUE 0x20 |
| 91 | #define RAM_SAVE_FLAG_XBZRLE 0x40 |
Juan Quintela | 10cb333 | 2023-10-11 22:35:22 +0200 | [diff] [blame] | 92 | /* 0x80 is reserved in rdma.h for RAM_SAVE_FLAG_HOOK */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 93 | #define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100 |
Juan Quintela | 294e5a4 | 2022-06-21 13:36:11 +0200 | [diff] [blame] | 94 | #define RAM_SAVE_FLAG_MULTIFD_FLUSH 0x200 |
Juan Quintela | 7b54876 | 2022-07-28 10:14:42 +0200 | [diff] [blame] | 95 | /* We can't use any flag that is bigger than 0x200 */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 96 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 97 | XBZRLECacheStats xbzrle_counters; |
| 98 | |
Peter Xu | f166876 | 2022-10-11 17:55:55 -0400 | [diff] [blame] | 99 | /* used by the search for pages to send */ |
| 100 | struct PageSearchStatus { |
| 101 | /* The migration channel used for a specific host page */ |
| 102 | QEMUFile *pss_channel; |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 103 | /* Last block from where we have sent data */ |
| 104 | RAMBlock *last_sent_block; |
Peter Xu | f166876 | 2022-10-11 17:55:55 -0400 | [diff] [blame] | 105 | /* Current block being searched */ |
| 106 | RAMBlock *block; |
| 107 | /* Current page to search from */ |
| 108 | unsigned long page; |
| 109 | /* Set once we wrap around */ |
| 110 | bool complete_round; |
Peter Xu | f166876 | 2022-10-11 17:55:55 -0400 | [diff] [blame] | 111 | /* Whether we're sending a host page */ |
| 112 | bool host_page_sending; |
| 113 | /* The start/end of current host page. Invalid if host_page_sending==false */ |
| 114 | unsigned long host_page_start; |
| 115 | unsigned long host_page_end; |
| 116 | }; |
| 117 | typedef struct PageSearchStatus PageSearchStatus; |
| 118 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 119 | /* struct contains XBZRLE cache and a static page |
| 120 | used by the compression */ |
| 121 | static struct { |
| 122 | /* buffer used for XBZRLE encoding */ |
| 123 | uint8_t *encoded_buf; |
| 124 | /* buffer for storing page content */ |
| 125 | uint8_t *current_buf; |
| 126 | /* Cache for XBZRLE, Protected by lock. */ |
| 127 | PageCache *cache; |
| 128 | QemuMutex lock; |
Juan Quintela | c00e092 | 2017-05-09 16:22:01 +0200 | [diff] [blame] | 129 | /* it will store a page full of zeros */ |
| 130 | uint8_t *zero_target_page; |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 131 | /* buffer used for XBZRLE decoding */ |
| 132 | uint8_t *decoded_buf; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 133 | } XBZRLE; |
| 134 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 135 | static void XBZRLE_cache_lock(void) |
| 136 | { |
Juan Quintela | 87dca0c | 2023-03-01 22:20:13 +0100 | [diff] [blame] | 137 | if (migrate_xbzrle()) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 138 | qemu_mutex_lock(&XBZRLE.lock); |
Bihong Yu | f4c51a6 | 2020-10-20 11:10:45 +0800 | [diff] [blame] | 139 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void XBZRLE_cache_unlock(void) |
| 143 | { |
Juan Quintela | 87dca0c | 2023-03-01 22:20:13 +0100 | [diff] [blame] | 144 | if (migrate_xbzrle()) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 145 | qemu_mutex_unlock(&XBZRLE.lock); |
Bihong Yu | f4c51a6 | 2020-10-20 11:10:45 +0800 | [diff] [blame] | 146 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 149 | /** |
| 150 | * xbzrle_cache_resize: resize the xbzrle cache |
| 151 | * |
Daniel P. Berrangé | cbde7be | 2021-02-19 18:40:12 +0000 | [diff] [blame] | 152 | * This function is called from migrate_params_apply in main |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 153 | * thread, possibly while a migration is in progress. A running |
| 154 | * migration may be using the cache and might finish during this call, |
| 155 | * hence changes to the cache are protected by XBZRLE.lock(). |
| 156 | * |
Juan Quintela | c9dede2 | 2017-10-06 23:03:55 +0200 | [diff] [blame] | 157 | * Returns 0 for success or -1 for error |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 158 | * |
| 159 | * @new_size: new cache size |
Juan Quintela | 8acabf6 | 2017-10-05 22:00:31 +0200 | [diff] [blame] | 160 | * @errp: set *errp if the check failed, with reason |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 161 | */ |
Markus Armbruster | 8b9407a | 2021-02-02 15:17:32 +0100 | [diff] [blame] | 162 | int xbzrle_cache_resize(uint64_t new_size, Error **errp) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 163 | { |
| 164 | PageCache *new_cache; |
Juan Quintela | c9dede2 | 2017-10-06 23:03:55 +0200 | [diff] [blame] | 165 | int64_t ret = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 166 | |
Juan Quintela | 8acabf6 | 2017-10-05 22:00:31 +0200 | [diff] [blame] | 167 | /* Check for truncation */ |
| 168 | if (new_size != (size_t)new_size) { |
| 169 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", |
| 170 | "exceeding address space"); |
| 171 | return -1; |
| 172 | } |
| 173 | |
Juan Quintela | 2a313e5 | 2017-10-06 23:00:12 +0200 | [diff] [blame] | 174 | if (new_size == migrate_xbzrle_cache_size()) { |
| 175 | /* nothing to do */ |
Juan Quintela | c9dede2 | 2017-10-06 23:03:55 +0200 | [diff] [blame] | 176 | return 0; |
Juan Quintela | 2a313e5 | 2017-10-06 23:00:12 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 179 | XBZRLE_cache_lock(); |
| 180 | |
| 181 | if (XBZRLE.cache != NULL) { |
Juan Quintela | 80f8dfd | 2017-10-06 22:30:45 +0200 | [diff] [blame] | 182 | new_cache = cache_init(new_size, TARGET_PAGE_SIZE, errp); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 183 | if (!new_cache) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 184 | ret = -1; |
| 185 | goto out; |
| 186 | } |
| 187 | |
| 188 | cache_fini(XBZRLE.cache); |
| 189 | XBZRLE.cache = new_cache; |
| 190 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 191 | out: |
| 192 | XBZRLE_cache_unlock(); |
| 193 | return ret; |
| 194 | } |
| 195 | |
Peter Xu | 20123ee | 2022-10-11 17:55:46 -0400 | [diff] [blame] | 196 | static bool postcopy_preempt_active(void) |
| 197 | { |
| 198 | return migrate_postcopy_preempt() && migration_in_postcopy(); |
| 199 | } |
| 200 | |
David Hildenbrand | f161c88 | 2023-07-06 09:56:08 +0200 | [diff] [blame] | 201 | bool migrate_ram_is_ignored(RAMBlock *block) |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 202 | { |
| 203 | return !qemu_ram_is_migratable(block) || |
Steve Sistare | b0182e5 | 2023-06-07 08:18:36 -0700 | [diff] [blame] | 204 | (migrate_ignore_shared() && qemu_ram_is_shared(block) |
| 205 | && qemu_ram_is_named_file(block)); |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 206 | } |
| 207 | |
Dr. David Alan Gilbert | 343f632 | 2018-06-05 17:25:45 +0100 | [diff] [blame] | 208 | #undef RAMBLOCK_FOREACH |
| 209 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 210 | int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque) |
| 211 | { |
| 212 | RAMBlock *block; |
| 213 | int ret = 0; |
| 214 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 215 | RCU_READ_LOCK_GUARD(); |
| 216 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 217 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 218 | ret = func(block, opaque); |
| 219 | if (ret) { |
| 220 | break; |
| 221 | } |
| 222 | } |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 223 | return ret; |
| 224 | } |
| 225 | |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 226 | static void ramblock_recv_map_init(void) |
| 227 | { |
| 228 | RAMBlock *rb; |
| 229 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 230 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 231 | assert(!rb->receivedmap); |
| 232 | rb->receivedmap = bitmap_new(rb->max_length >> qemu_target_page_bits()); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | int ramblock_recv_bitmap_test(RAMBlock *rb, void *host_addr) |
| 237 | { |
| 238 | return test_bit(ramblock_recv_bitmap_offset(host_addr, rb), |
| 239 | rb->receivedmap); |
| 240 | } |
| 241 | |
Dr. David Alan Gilbert | 1cba9f6 | 2018-03-12 17:21:08 +0000 | [diff] [blame] | 242 | bool ramblock_recv_bitmap_test_byte_offset(RAMBlock *rb, uint64_t byte_offset) |
| 243 | { |
| 244 | return test_bit(byte_offset >> TARGET_PAGE_BITS, rb->receivedmap); |
| 245 | } |
| 246 | |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 247 | void ramblock_recv_bitmap_set(RAMBlock *rb, void *host_addr) |
| 248 | { |
| 249 | set_bit_atomic(ramblock_recv_bitmap_offset(host_addr, rb), rb->receivedmap); |
| 250 | } |
| 251 | |
| 252 | void ramblock_recv_bitmap_set_range(RAMBlock *rb, void *host_addr, |
| 253 | size_t nr) |
| 254 | { |
| 255 | bitmap_set_atomic(rb->receivedmap, |
| 256 | ramblock_recv_bitmap_offset(host_addr, rb), |
| 257 | nr); |
| 258 | } |
| 259 | |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 260 | #define RAMBLOCK_RECV_BITMAP_ENDING (0x0123456789abcdefULL) |
| 261 | |
| 262 | /* |
| 263 | * Format: bitmap_size (8 bytes) + whole_bitmap (N bytes). |
| 264 | * |
| 265 | * Returns >0 if success with sent bytes, or <0 if error. |
| 266 | */ |
| 267 | int64_t ramblock_recv_bitmap_send(QEMUFile *file, |
| 268 | const char *block_name) |
| 269 | { |
| 270 | RAMBlock *block = qemu_ram_block_by_name(block_name); |
| 271 | unsigned long *le_bitmap, nbits; |
| 272 | uint64_t size; |
| 273 | |
| 274 | if (!block) { |
| 275 | error_report("%s: invalid block name: %s", __func__, block_name); |
| 276 | return -1; |
| 277 | } |
| 278 | |
David Hildenbrand | 898ba90 | 2021-04-29 13:27:06 +0200 | [diff] [blame] | 279 | nbits = block->postcopy_length >> TARGET_PAGE_BITS; |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * Make sure the tmp bitmap buffer is big enough, e.g., on 32bit |
| 283 | * machines we may need 4 more bytes for padding (see below |
| 284 | * comment). So extend it a bit before hand. |
| 285 | */ |
| 286 | le_bitmap = bitmap_new(nbits + BITS_PER_LONG); |
| 287 | |
| 288 | /* |
| 289 | * Always use little endian when sending the bitmap. This is |
| 290 | * required that when source and destination VMs are not using the |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 291 | * same endianness. (Note: big endian won't work.) |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 292 | */ |
| 293 | bitmap_to_le(le_bitmap, block->receivedmap, nbits); |
| 294 | |
| 295 | /* Size of the bitmap, in bytes */ |
Peter Xu | a725ef9 | 2018-07-10 17:18:55 +0800 | [diff] [blame] | 296 | size = DIV_ROUND_UP(nbits, 8); |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * size is always aligned to 8 bytes for 64bit machines, but it |
| 300 | * may not be true for 32bit machines. We need this padding to |
| 301 | * make sure the migration can survive even between 32bit and |
| 302 | * 64bit machines. |
| 303 | */ |
| 304 | size = ROUND_UP(size, 8); |
| 305 | |
| 306 | qemu_put_be64(file, size); |
| 307 | qemu_put_buffer(file, (const uint8_t *)le_bitmap, size); |
| 308 | /* |
| 309 | * Mark as an end, in case the middle part is screwed up due to |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 310 | * some "mysterious" reason. |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 311 | */ |
| 312 | qemu_put_be64(file, RAMBLOCK_RECV_BITMAP_ENDING); |
| 313 | qemu_fflush(file); |
| 314 | |
Peter Xu | bf26990 | 2018-05-25 09:50:42 +0800 | [diff] [blame] | 315 | g_free(le_bitmap); |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 316 | |
| 317 | if (qemu_file_get_error(file)) { |
| 318 | return qemu_file_get_error(file); |
| 319 | } |
| 320 | |
| 321 | return size + sizeof(size); |
| 322 | } |
| 323 | |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 324 | /* |
| 325 | * An outstanding page request, on the source, having been received |
| 326 | * and queued |
| 327 | */ |
| 328 | struct RAMSrcPageRequest { |
| 329 | RAMBlock *rb; |
| 330 | hwaddr offset; |
| 331 | hwaddr len; |
| 332 | |
| 333 | QSIMPLEQ_ENTRY(RAMSrcPageRequest) next_req; |
| 334 | }; |
| 335 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 336 | /* State of RAM for migration */ |
| 337 | struct RAMState { |
Peter Xu | f166876 | 2022-10-11 17:55:55 -0400 | [diff] [blame] | 338 | /* |
| 339 | * PageSearchStatus structures for the channels when send pages. |
| 340 | * Protected by the bitmap_mutex. |
| 341 | */ |
| 342 | PageSearchStatus pss[RAM_CHANNEL_MAX]; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 343 | /* UFFD file descriptor, used in 'write-tracking' migration */ |
| 344 | int uffdio_fd; |
Juan Quintela | 8d80e19 | 2022-05-10 19:37:36 +0200 | [diff] [blame] | 345 | /* total ram size in bytes */ |
| 346 | uint64_t ram_bytes_total; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 347 | /* Last block that we have visited searching for dirty pages */ |
| 348 | RAMBlock *last_seen_block; |
Juan Quintela | 269ace2 | 2017-03-21 15:23:31 +0100 | [diff] [blame] | 349 | /* Last dirty target page we have sent */ |
| 350 | ram_addr_t last_page; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 351 | /* last ram version we have seen */ |
| 352 | uint32_t last_version; |
Juan Quintela | 8d820d6 | 2017-03-13 19:35:50 +0100 | [diff] [blame] | 353 | /* How many times we have dirty too many pages */ |
| 354 | int dirty_rate_high_cnt; |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 355 | /* these variables are used for bitmap sync */ |
| 356 | /* last time we did a full bitmap_sync */ |
| 357 | int64_t time_last_bitmap_sync; |
Juan Quintela | eac7415 | 2017-03-28 14:59:01 +0200 | [diff] [blame] | 358 | /* bytes transferred at start_time */ |
Juan Quintela | c4bdf0c | 2017-03-28 14:59:54 +0200 | [diff] [blame] | 359 | uint64_t bytes_xfer_prev; |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 360 | /* number of dirty pages since start_time */ |
Juan Quintela | 68908ed | 2017-03-28 15:05:53 +0200 | [diff] [blame] | 361 | uint64_t num_dirty_pages_period; |
Juan Quintela | b5833fd | 2017-03-13 19:49:19 +0100 | [diff] [blame] | 362 | /* xbzrle misses since the beginning of the period */ |
| 363 | uint64_t xbzrle_cache_miss_prev; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 364 | /* Amount of xbzrle pages since the beginning of the period */ |
| 365 | uint64_t xbzrle_pages_prev; |
| 366 | /* Amount of xbzrle encoded bytes since the beginning of the period */ |
| 367 | uint64_t xbzrle_bytes_prev; |
Juan Quintela | f3095cc | 2023-05-04 13:53:23 +0200 | [diff] [blame] | 368 | /* Are we really using XBZRLE (e.g., after the first round). */ |
| 369 | bool xbzrle_started; |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 370 | /* Are we on the last stage of migration */ |
| 371 | bool last_stage; |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 372 | |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 373 | /* total handled target pages at the beginning of period */ |
| 374 | uint64_t target_page_count_prev; |
| 375 | /* total handled target pages since start */ |
| 376 | uint64_t target_page_count; |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 377 | /* number of dirty bits in the bitmap */ |
Peter Xu | 2dfaf12 | 2017-08-02 17:41:19 +0800 | [diff] [blame] | 378 | uint64_t migration_dirty_pages; |
Peter Xu | f166876 | 2022-10-11 17:55:55 -0400 | [diff] [blame] | 379 | /* |
| 380 | * Protects: |
| 381 | * - dirty/clear bitmap |
| 382 | * - migration_dirty_pages |
| 383 | * - pss structures |
| 384 | */ |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 385 | QemuMutex bitmap_mutex; |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 386 | /* The RAMBlock used in the last src_page_requests */ |
| 387 | RAMBlock *last_req_rb; |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 388 | /* Queue of outstanding page requests from the destination */ |
| 389 | QemuMutex src_page_req_mutex; |
Paolo Bonzini | b58deb3 | 2018-12-06 11:58:10 +0100 | [diff] [blame] | 390 | QSIMPLEQ_HEAD(, RAMSrcPageRequest) src_page_requests; |
Peter Xu | 1015ff5 | 2023-10-04 18:02:36 -0400 | [diff] [blame] | 391 | |
| 392 | /* |
| 393 | * This is only used when postcopy is in recovery phase, to communicate |
| 394 | * between the migration thread and the return path thread on dirty |
| 395 | * bitmap synchronizations. This field is unused in other stages of |
| 396 | * RAM migration. |
| 397 | */ |
| 398 | unsigned int postcopy_bmap_sync_requested; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 399 | }; |
| 400 | typedef struct RAMState RAMState; |
| 401 | |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 402 | static RAMState *ram_state; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 403 | |
Wei Wang | bd22706 | 2018-12-11 16:24:51 +0800 | [diff] [blame] | 404 | static NotifierWithReturnList precopy_notifier_list; |
| 405 | |
Peter Xu | a1fe28d | 2022-01-19 16:09:18 +0800 | [diff] [blame] | 406 | /* Whether postcopy has queued requests? */ |
| 407 | static bool postcopy_has_request(RAMState *rs) |
| 408 | { |
| 409 | return !QSIMPLEQ_EMPTY_ATOMIC(&rs->src_page_requests); |
| 410 | } |
| 411 | |
Wei Wang | bd22706 | 2018-12-11 16:24:51 +0800 | [diff] [blame] | 412 | void precopy_infrastructure_init(void) |
| 413 | { |
| 414 | notifier_with_return_list_init(&precopy_notifier_list); |
| 415 | } |
| 416 | |
| 417 | void precopy_add_notifier(NotifierWithReturn *n) |
| 418 | { |
| 419 | notifier_with_return_list_add(&precopy_notifier_list, n); |
| 420 | } |
| 421 | |
| 422 | void precopy_remove_notifier(NotifierWithReturn *n) |
| 423 | { |
| 424 | notifier_with_return_remove(n); |
| 425 | } |
| 426 | |
| 427 | int precopy_notify(PrecopyNotifyReason reason, Error **errp) |
| 428 | { |
| 429 | PrecopyNotifyData pnd; |
| 430 | pnd.reason = reason; |
| 431 | pnd.errp = errp; |
| 432 | |
| 433 | return notifier_with_return_list_notify(&precopy_notifier_list, &pnd); |
| 434 | } |
| 435 | |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 436 | uint64_t ram_bytes_remaining(void) |
| 437 | { |
Dr. David Alan Gilbert | bae416e | 2017-12-15 11:51:23 +0000 | [diff] [blame] | 438 | return ram_state ? (ram_state->migration_dirty_pages * TARGET_PAGE_SIZE) : |
| 439 | 0; |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Juan Quintela | 26a2606 | 2022-02-22 21:02:03 +0100 | [diff] [blame] | 442 | void ram_transferred_add(uint64_t bytes) |
David Edmondson | 4c2d0f6 | 2021-12-21 09:34:40 +0000 | [diff] [blame] | 443 | { |
David Edmondson | ae68066 | 2021-12-21 09:34:41 +0000 | [diff] [blame] | 444 | if (runstate_is_running()) { |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 445 | stat64_add(&mig_stats.precopy_bytes, bytes); |
David Edmondson | ae68066 | 2021-12-21 09:34:41 +0000 | [diff] [blame] | 446 | } else if (migration_in_postcopy()) { |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 447 | stat64_add(&mig_stats.postcopy_bytes, bytes); |
David Edmondson | ae68066 | 2021-12-21 09:34:41 +0000 | [diff] [blame] | 448 | } else { |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 449 | stat64_add(&mig_stats.downtime_bytes, bytes); |
David Edmondson | ae68066 | 2021-12-21 09:34:41 +0000 | [diff] [blame] | 450 | } |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 451 | stat64_add(&mig_stats.transferred, bytes); |
David Edmondson | 4c2d0f6 | 2021-12-21 09:34:40 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Juan Quintela | 4010ba3 | 2021-12-15 20:10:39 +0100 | [diff] [blame] | 454 | struct MigrationOps { |
| 455 | int (*ram_save_target_page)(RAMState *rs, PageSearchStatus *pss); |
| 456 | }; |
| 457 | typedef struct MigrationOps MigrationOps; |
| 458 | |
| 459 | MigrationOps *migration_ops; |
| 460 | |
Peter Xu | 9358982 | 2022-10-11 17:55:57 -0400 | [diff] [blame] | 461 | static int ram_save_host_page_urgent(PageSearchStatus *pss); |
| 462 | |
Peter Xu | ebd88a4 | 2022-10-11 17:55:54 -0400 | [diff] [blame] | 463 | /* NOTE: page is the PFN not real ram_addr_t. */ |
| 464 | static void pss_init(PageSearchStatus *pss, RAMBlock *rb, ram_addr_t page) |
| 465 | { |
| 466 | pss->block = rb; |
| 467 | pss->page = page; |
| 468 | pss->complete_round = false; |
| 469 | } |
| 470 | |
Peter Xu | 9358982 | 2022-10-11 17:55:57 -0400 | [diff] [blame] | 471 | /* |
| 472 | * Check whether two PSSs are actively sending the same page. Return true |
| 473 | * if it is, false otherwise. |
| 474 | */ |
| 475 | static bool pss_overlap(PageSearchStatus *pss1, PageSearchStatus *pss2) |
| 476 | { |
| 477 | return pss1->host_page_sending && pss2->host_page_sending && |
| 478 | (pss1->host_page_start == pss2->host_page_start); |
| 479 | } |
| 480 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 481 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 482 | * save_page_header: write page header to wire |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 483 | * |
| 484 | * If this is the 1st block, it also writes the block identification |
| 485 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 486 | * Returns the number of bytes written |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 487 | * |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 488 | * @pss: current PSS channel status |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 489 | * @block: block that contains the page we want to send |
| 490 | * @offset: offset inside the block for the page |
| 491 | * in the lower bits, it contains flags |
| 492 | */ |
Lukas Straub | 37502df | 2023-04-02 17:06:32 +0000 | [diff] [blame] | 493 | static size_t save_page_header(PageSearchStatus *pss, QEMUFile *f, |
| 494 | RAMBlock *block, ram_addr_t offset) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 495 | { |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 496 | size_t size, len; |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 497 | bool same_block = (block == pss->last_sent_block); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 498 | |
Peter Xu | 10661f1 | 2022-10-11 17:55:48 -0400 | [diff] [blame] | 499 | if (same_block) { |
Juan Quintela | 2479569 | 2017-03-21 11:45:01 +0100 | [diff] [blame] | 500 | offset |= RAM_SAVE_FLAG_CONTINUE; |
| 501 | } |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 502 | qemu_put_be64(f, offset); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 503 | size = 8; |
| 504 | |
Peter Xu | 10661f1 | 2022-10-11 17:55:48 -0400 | [diff] [blame] | 505 | if (!same_block) { |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 506 | len = strlen(block->idstr); |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 507 | qemu_put_byte(f, len); |
| 508 | qemu_put_buffer(f, (uint8_t *)block->idstr, len); |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 509 | size += 1 + len; |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 510 | pss->last_sent_block = block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 511 | } |
| 512 | return size; |
| 513 | } |
| 514 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 515 | /** |
Olaf Hering | 179a808 | 2021-07-08 18:21:59 +0200 | [diff] [blame] | 516 | * mig_throttle_guest_down: throttle down the guest |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 517 | * |
| 518 | * Reduce amount of guest cpu execution to hopefully slow down memory |
| 519 | * writes. If guest dirty memory rate is reduced below the rate at |
| 520 | * which we can transfer pages to the destination then we should be |
| 521 | * able to complete migration. Some workloads dirty memory way too |
| 522 | * fast and will not effectively converge, even with auto-converge. |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 523 | */ |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 524 | static void mig_throttle_guest_down(uint64_t bytes_dirty_period, |
| 525 | uint64_t bytes_dirty_threshold) |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 526 | { |
Juan Quintela | 2a8ec38 | 2023-03-02 01:22:44 +0100 | [diff] [blame] | 527 | uint64_t pct_initial = migrate_cpu_throttle_initial(); |
Juan Quintela | 9605c2a | 2023-03-02 10:20:49 +0100 | [diff] [blame] | 528 | uint64_t pct_increment = migrate_cpu_throttle_increment(); |
Juan Quintela | 873f674 | 2023-03-02 10:29:51 +0100 | [diff] [blame] | 529 | bool pct_tailslow = migrate_cpu_throttle_tailslow(); |
Juan Quintela | 24155bd | 2023-03-02 01:13:01 +0100 | [diff] [blame] | 530 | int pct_max = migrate_max_cpu_throttle(); |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 531 | |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 532 | uint64_t throttle_now = cpu_throttle_get_percentage(); |
| 533 | uint64_t cpu_now, cpu_ideal, throttle_inc; |
| 534 | |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 535 | /* We have not started throttling yet. Let's start it. */ |
| 536 | if (!cpu_throttle_active()) { |
| 537 | cpu_throttle_set(pct_initial); |
| 538 | } else { |
| 539 | /* Throttling already on, just increase the rate */ |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 540 | if (!pct_tailslow) { |
| 541 | throttle_inc = pct_increment; |
| 542 | } else { |
| 543 | /* Compute the ideal CPU percentage used by Guest, which may |
| 544 | * make the dirty rate match the dirty rate threshold. */ |
| 545 | cpu_now = 100 - throttle_now; |
| 546 | cpu_ideal = cpu_now * (bytes_dirty_threshold * 1.0 / |
| 547 | bytes_dirty_period); |
| 548 | throttle_inc = MIN(cpu_now - cpu_ideal, pct_increment); |
| 549 | } |
| 550 | cpu_throttle_set(MIN(throttle_now + throttle_inc, pct_max)); |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
Rao, Lei | 91fe9a8 | 2021-11-09 11:04:54 +0800 | [diff] [blame] | 554 | void mig_throttle_counter_reset(void) |
| 555 | { |
| 556 | RAMState *rs = ram_state; |
| 557 | |
| 558 | rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 559 | rs->num_dirty_pages_period = 0; |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 560 | rs->bytes_xfer_prev = stat64_get(&mig_stats.transferred); |
Rao, Lei | 91fe9a8 | 2021-11-09 11:04:54 +0800 | [diff] [blame] | 561 | } |
| 562 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 563 | /** |
| 564 | * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache |
| 565 | * |
| 566 | * @current_addr: address for the zero page |
| 567 | * |
| 568 | * Update the xbzrle cache to reflect a page that's been sent as all 0. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 569 | * The important thing is that a stale (not-yet-0'd) page be replaced |
| 570 | * by the new data. |
| 571 | * As a bonus, if the page wasn't in the cache it gets added so that |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 572 | * when a small write is made into the 0'd page it gets XBZRLE sent. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 573 | */ |
Fabiano Rosas | 8f47d4e | 2023-10-11 15:46:00 -0300 | [diff] [blame] | 574 | static void xbzrle_cache_zero_page(ram_addr_t current_addr) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 575 | { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 576 | /* We don't care if this fails to allocate a new cache page |
| 577 | * as long as it updated an old one */ |
Juan Quintela | c00e092 | 2017-05-09 16:22:01 +0200 | [diff] [blame] | 578 | cache_insert(XBZRLE.cache, current_addr, XBZRLE.zero_target_page, |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 579 | stat64_get(&mig_stats.dirty_sync_count)); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | #define ENCODING_FLAG_XBZRLE 0x1 |
| 583 | |
| 584 | /** |
| 585 | * save_xbzrle_page: compress and send current page |
| 586 | * |
| 587 | * Returns: 1 means that we wrote the page |
| 588 | * 0 means that page is identical to the one already sent |
| 589 | * -1 means that xbzrle would be longer than normal |
| 590 | * |
Juan Quintela | 5a98773 | 2017-03-13 19:39:02 +0100 | [diff] [blame] | 591 | * @rs: current RAM state |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 592 | * @pss: current PSS channel |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 593 | * @current_data: pointer to the address of the page contents |
| 594 | * @current_addr: addr of the page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 595 | * @block: block that contains the page we want to send |
| 596 | * @offset: offset inside the block for the page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 597 | */ |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 598 | static int save_xbzrle_page(RAMState *rs, PageSearchStatus *pss, |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 599 | uint8_t **current_data, ram_addr_t current_addr, |
| 600 | RAMBlock *block, ram_addr_t offset) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 601 | { |
| 602 | int encoded_len = 0, bytes_xbzrle; |
| 603 | uint8_t *prev_cached_page; |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 604 | QEMUFile *file = pss->pss_channel; |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 605 | uint64_t generation = stat64_get(&mig_stats.dirty_sync_count); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 606 | |
Juan Quintela | 536b5a4 | 2023-04-11 18:02:34 +0200 | [diff] [blame] | 607 | if (!cache_is_cached(XBZRLE.cache, current_addr, generation)) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 608 | xbzrle_counters.cache_miss++; |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 609 | if (!rs->last_stage) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 610 | if (cache_insert(XBZRLE.cache, current_addr, *current_data, |
Juan Quintela | 536b5a4 | 2023-04-11 18:02:34 +0200 | [diff] [blame] | 611 | generation) == -1) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 612 | return -1; |
| 613 | } else { |
| 614 | /* update *current_data when the page has been |
| 615 | inserted into cache */ |
| 616 | *current_data = get_cached_data(XBZRLE.cache, current_addr); |
| 617 | } |
| 618 | } |
| 619 | return -1; |
| 620 | } |
| 621 | |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 622 | /* |
| 623 | * Reaching here means the page has hit the xbzrle cache, no matter what |
| 624 | * encoding result it is (normal encoding, overflow or skipping the page), |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 625 | * count the page as encoded. This is used to calculate the encoding rate. |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 626 | * |
| 627 | * Example: 2 pages (8KB) being encoded, first page encoding generates 2KB, |
| 628 | * 2nd page turns out to be skipped (i.e. no new bytes written to the |
| 629 | * page), the overall encoding rate will be 8KB / 2KB = 4, which has the |
| 630 | * skipped page included. In this way, the encoding rate can tell if the |
| 631 | * guest page is good for xbzrle encoding. |
| 632 | */ |
| 633 | xbzrle_counters.pages++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 634 | prev_cached_page = get_cached_data(XBZRLE.cache, current_addr); |
| 635 | |
| 636 | /* save current buffer into memory */ |
| 637 | memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE); |
| 638 | |
| 639 | /* XBZRLE encoding (if there is no overflow) */ |
Richard Henderson | 7ba7db9 | 2023-05-17 20:00:30 -0700 | [diff] [blame] | 640 | encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf, |
| 641 | TARGET_PAGE_SIZE, XBZRLE.encoded_buf, |
| 642 | TARGET_PAGE_SIZE); |
Wei Yang | ca35380 | 2019-06-10 08:41:59 +0800 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * Update the cache contents, so that it corresponds to the data |
| 646 | * sent, in all cases except where we skip the page. |
| 647 | */ |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 648 | if (!rs->last_stage && encoded_len != 0) { |
Wei Yang | ca35380 | 2019-06-10 08:41:59 +0800 | [diff] [blame] | 649 | memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE); |
| 650 | /* |
| 651 | * In the case where we couldn't compress, ensure that the caller |
| 652 | * sends the data from the cache, since the guest might have |
| 653 | * changed the RAM since we copied it. |
| 654 | */ |
| 655 | *current_data = prev_cached_page; |
| 656 | } |
| 657 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 658 | if (encoded_len == 0) { |
Juan Quintela | 55c4446 | 2017-01-23 22:32:05 +0100 | [diff] [blame] | 659 | trace_save_xbzrle_page_skipping(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 660 | return 0; |
| 661 | } else if (encoded_len == -1) { |
Juan Quintela | 55c4446 | 2017-01-23 22:32:05 +0100 | [diff] [blame] | 662 | trace_save_xbzrle_page_overflow(); |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 663 | xbzrle_counters.overflow++; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 664 | xbzrle_counters.bytes += TARGET_PAGE_SIZE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 665 | return -1; |
| 666 | } |
| 667 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 668 | /* Send XBZRLE based compressed page */ |
Lukas Straub | 37502df | 2023-04-02 17:06:32 +0000 | [diff] [blame] | 669 | bytes_xbzrle = save_page_header(pss, pss->pss_channel, block, |
Juan Quintela | 204b88b | 2017-03-15 09:16:57 +0100 | [diff] [blame] | 670 | offset | RAM_SAVE_FLAG_XBZRLE); |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 671 | qemu_put_byte(file, ENCODING_FLAG_XBZRLE); |
| 672 | qemu_put_be16(file, encoded_len); |
| 673 | qemu_put_buffer(file, XBZRLE.encoded_buf, encoded_len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 674 | bytes_xbzrle += encoded_len + 1 + 2; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 675 | /* |
| 676 | * Like compressed_size (please see update_compress_thread_counts), |
| 677 | * the xbzrle encoded bytes don't count the 8 byte header with |
| 678 | * RAM_SAVE_FLAG_CONTINUE. |
| 679 | */ |
| 680 | xbzrle_counters.bytes += bytes_xbzrle - 8; |
David Edmondson | 4c2d0f6 | 2021-12-21 09:34:40 +0000 | [diff] [blame] | 681 | ram_transferred_add(bytes_xbzrle); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 682 | |
| 683 | return 1; |
| 684 | } |
| 685 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 686 | /** |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 687 | * pss_find_next_dirty: find the next dirty page of current ramblock |
Dr. David Alan Gilbert | f3f491f | 2015-11-05 18:11:01 +0000 | [diff] [blame] | 688 | * |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 689 | * This function updates pss->page to point to the next dirty page index |
| 690 | * within the ramblock to migrate, or the end of ramblock when nothing |
| 691 | * found. Note that when pss->host_page_sending==true it means we're |
| 692 | * during sending a host page, so we won't look for dirty page that is |
| 693 | * outside the host page boundary. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 694 | * |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 695 | * @pss: the current page search status |
Dr. David Alan Gilbert | f3f491f | 2015-11-05 18:11:01 +0000 | [diff] [blame] | 696 | */ |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 697 | static void pss_find_next_dirty(PageSearchStatus *pss) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 698 | { |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 699 | RAMBlock *rb = pss->block; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 700 | unsigned long size = rb->used_length >> TARGET_PAGE_BITS; |
| 701 | unsigned long *bitmap = rb->bmap; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 702 | |
David Hildenbrand | f161c88 | 2023-07-06 09:56:08 +0200 | [diff] [blame] | 703 | if (migrate_ram_is_ignored(rb)) { |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 704 | /* Points directly to the end, so we know no dirty page */ |
| 705 | pss->page = size; |
| 706 | return; |
Cédric Le Goater | b895de5 | 2018-05-14 08:57:00 +0200 | [diff] [blame] | 707 | } |
| 708 | |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 709 | /* |
| 710 | * If during sending a host page, only look for dirty pages within the |
| 711 | * current host page being send. |
| 712 | */ |
| 713 | if (pss->host_page_sending) { |
| 714 | assert(pss->host_page_end); |
| 715 | size = MIN(size, pss->host_page_end); |
| 716 | } |
| 717 | |
| 718 | pss->page = find_next_bit(bitmap, size, pss->page); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 719 | } |
| 720 | |
David Hildenbrand | 1230a25 | 2021-09-04 18:09:07 +0200 | [diff] [blame] | 721 | static void migration_clear_memory_region_dirty_bitmap(RAMBlock *rb, |
Wei Wang | 3143577d | 2021-07-22 04:30:55 -0400 | [diff] [blame] | 722 | unsigned long page) |
| 723 | { |
| 724 | uint8_t shift; |
| 725 | hwaddr size, start; |
| 726 | |
| 727 | if (!rb->clear_bmap || !clear_bmap_test_and_clear(rb, page)) { |
| 728 | return; |
| 729 | } |
| 730 | |
| 731 | shift = rb->clear_bmap_shift; |
| 732 | /* |
| 733 | * CLEAR_BITMAP_SHIFT_MIN should always guarantee this... this |
| 734 | * can make things easier sometimes since then start address |
| 735 | * of the small chunk will always be 64 pages aligned so the |
| 736 | * bitmap will always be aligned to unsigned long. We should |
| 737 | * even be able to remove this restriction but I'm simply |
| 738 | * keeping it. |
| 739 | */ |
| 740 | assert(shift >= 6); |
| 741 | |
| 742 | size = 1ULL << (TARGET_PAGE_BITS + shift); |
David Hildenbrand | 7648297 | 2021-10-11 19:53:44 +0200 | [diff] [blame] | 743 | start = QEMU_ALIGN_DOWN((ram_addr_t)page << TARGET_PAGE_BITS, size); |
Wei Wang | 3143577d | 2021-07-22 04:30:55 -0400 | [diff] [blame] | 744 | trace_migration_bitmap_clear_dirty(rb->idstr, start, size, page); |
| 745 | memory_region_clear_dirty_bitmap(rb->mr, start, size); |
| 746 | } |
| 747 | |
| 748 | static void |
David Hildenbrand | 1230a25 | 2021-09-04 18:09:07 +0200 | [diff] [blame] | 749 | migration_clear_memory_region_dirty_bitmap_range(RAMBlock *rb, |
Wei Wang | 3143577d | 2021-07-22 04:30:55 -0400 | [diff] [blame] | 750 | unsigned long start, |
| 751 | unsigned long npages) |
| 752 | { |
| 753 | unsigned long i, chunk_pages = 1UL << rb->clear_bmap_shift; |
| 754 | unsigned long chunk_start = QEMU_ALIGN_DOWN(start, chunk_pages); |
| 755 | unsigned long chunk_end = QEMU_ALIGN_UP(start + npages, chunk_pages); |
| 756 | |
| 757 | /* |
| 758 | * Clear pages from start to start + npages - 1, so the end boundary is |
| 759 | * exclusive. |
| 760 | */ |
| 761 | for (i = chunk_start; i < chunk_end; i += chunk_pages) { |
David Hildenbrand | 1230a25 | 2021-09-04 18:09:07 +0200 | [diff] [blame] | 762 | migration_clear_memory_region_dirty_bitmap(rb, i); |
Wei Wang | 3143577d | 2021-07-22 04:30:55 -0400 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 766 | /* |
| 767 | * colo_bitmap_find_diry:find contiguous dirty pages from start |
| 768 | * |
| 769 | * Returns the page offset within memory region of the start of the contiguout |
| 770 | * dirty page |
| 771 | * |
| 772 | * @rs: current RAM state |
| 773 | * @rb: RAMBlock where to search for dirty pages |
| 774 | * @start: page where we start the search |
| 775 | * @num: the number of contiguous dirty pages |
| 776 | */ |
| 777 | static inline |
| 778 | unsigned long colo_bitmap_find_dirty(RAMState *rs, RAMBlock *rb, |
| 779 | unsigned long start, unsigned long *num) |
| 780 | { |
| 781 | unsigned long size = rb->used_length >> TARGET_PAGE_BITS; |
| 782 | unsigned long *bitmap = rb->bmap; |
| 783 | unsigned long first, next; |
| 784 | |
| 785 | *num = 0; |
| 786 | |
David Hildenbrand | f161c88 | 2023-07-06 09:56:08 +0200 | [diff] [blame] | 787 | if (migrate_ram_is_ignored(rb)) { |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 788 | return size; |
| 789 | } |
| 790 | |
| 791 | first = find_next_bit(bitmap, size, start); |
| 792 | if (first >= size) { |
| 793 | return first; |
| 794 | } |
| 795 | next = find_next_zero_bit(bitmap, size, first + 1); |
| 796 | assert(next >= first); |
| 797 | *num = next - first; |
| 798 | return first; |
| 799 | } |
| 800 | |
Juan Quintela | 06b1068 | 2017-03-21 15:18:05 +0100 | [diff] [blame] | 801 | static inline bool migration_bitmap_clear_dirty(RAMState *rs, |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 802 | RAMBlock *rb, |
| 803 | unsigned long page) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 804 | { |
| 805 | bool ret; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 806 | |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 807 | /* |
| 808 | * Clear dirty bitmap if needed. This _must_ be called before we |
| 809 | * send any of the page in the chunk because we need to make sure |
| 810 | * we can capture further page content changes when we sync dirty |
| 811 | * log the next time. So as long as we are going to send any of |
| 812 | * the page in the chunk we clear the remote dirty bitmap for all. |
| 813 | * Clearing it earlier won't be a problem, but too late will. |
| 814 | */ |
David Hildenbrand | 1230a25 | 2021-09-04 18:09:07 +0200 | [diff] [blame] | 815 | migration_clear_memory_region_dirty_bitmap(rb, page); |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 816 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 817 | ret = test_and_clear_bit(page, rb->bmap); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 818 | if (ret) { |
Juan Quintela | 0d8ec88 | 2017-03-13 21:21:41 +0100 | [diff] [blame] | 819 | rs->migration_dirty_pages--; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 820 | } |
Wei Wang | 386a907 | 2018-12-11 16:24:49 +0800 | [diff] [blame] | 821 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 822 | return ret; |
| 823 | } |
| 824 | |
David Hildenbrand | be39b4c | 2021-10-11 19:53:41 +0200 | [diff] [blame] | 825 | static void dirty_bitmap_clear_section(MemoryRegionSection *section, |
| 826 | void *opaque) |
| 827 | { |
| 828 | const hwaddr offset = section->offset_within_region; |
| 829 | const hwaddr size = int128_get64(section->size); |
| 830 | const unsigned long start = offset >> TARGET_PAGE_BITS; |
| 831 | const unsigned long npages = size >> TARGET_PAGE_BITS; |
| 832 | RAMBlock *rb = section->mr->ram_block; |
| 833 | uint64_t *cleared_bits = opaque; |
| 834 | |
| 835 | /* |
| 836 | * We don't grab ram_state->bitmap_mutex because we expect to run |
| 837 | * only when starting migration or during postcopy recovery where |
| 838 | * we don't have concurrent access. |
| 839 | */ |
| 840 | if (!migration_in_postcopy() && !migrate_background_snapshot()) { |
| 841 | migration_clear_memory_region_dirty_bitmap_range(rb, start, npages); |
| 842 | } |
| 843 | *cleared_bits += bitmap_count_one_with_offset(rb->bmap, start, npages); |
| 844 | bitmap_clear(rb->bmap, start, npages); |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * Exclude all dirty pages from migration that fall into a discarded range as |
| 849 | * managed by a RamDiscardManager responsible for the mapped memory region of |
| 850 | * the RAMBlock. Clear the corresponding bits in the dirty bitmaps. |
| 851 | * |
| 852 | * Discarded pages ("logically unplugged") have undefined content and must |
| 853 | * not get migrated, because even reading these pages for migration might |
| 854 | * result in undesired behavior. |
| 855 | * |
| 856 | * Returns the number of cleared bits in the RAMBlock dirty bitmap. |
| 857 | * |
| 858 | * Note: The result is only stable while migrating (precopy/postcopy). |
| 859 | */ |
| 860 | static uint64_t ramblock_dirty_bitmap_clear_discarded_pages(RAMBlock *rb) |
| 861 | { |
| 862 | uint64_t cleared_bits = 0; |
| 863 | |
| 864 | if (rb->mr && rb->bmap && memory_region_has_ram_discard_manager(rb->mr)) { |
| 865 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(rb->mr); |
| 866 | MemoryRegionSection section = { |
| 867 | .mr = rb->mr, |
| 868 | .offset_within_region = 0, |
| 869 | .size = int128_make64(qemu_ram_get_used_length(rb)), |
| 870 | }; |
| 871 | |
| 872 | ram_discard_manager_replay_discarded(rdm, §ion, |
| 873 | dirty_bitmap_clear_section, |
| 874 | &cleared_bits); |
| 875 | } |
| 876 | return cleared_bits; |
| 877 | } |
| 878 | |
David Hildenbrand | 9470c5e | 2021-10-11 19:53:43 +0200 | [diff] [blame] | 879 | /* |
| 880 | * Check if a host-page aligned page falls into a discarded range as managed by |
| 881 | * a RamDiscardManager responsible for the mapped memory region of the RAMBlock. |
| 882 | * |
| 883 | * Note: The result is only stable while migrating (precopy/postcopy). |
| 884 | */ |
| 885 | bool ramblock_page_is_discarded(RAMBlock *rb, ram_addr_t start) |
| 886 | { |
| 887 | if (rb->mr && memory_region_has_ram_discard_manager(rb->mr)) { |
| 888 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(rb->mr); |
| 889 | MemoryRegionSection section = { |
| 890 | .mr = rb->mr, |
| 891 | .offset_within_region = start, |
| 892 | .size = int128_make64(qemu_ram_pagesize(rb)), |
| 893 | }; |
| 894 | |
| 895 | return !ram_discard_manager_is_populated(rdm, §ion); |
| 896 | } |
| 897 | return false; |
| 898 | } |
| 899 | |
Peter Xu | 267691b | 2019-06-03 14:50:46 +0800 | [diff] [blame] | 900 | /* Called with RCU critical section */ |
Wei Yang | 7a3e957 | 2019-08-08 11:31:55 +0800 | [diff] [blame] | 901 | static void ramblock_sync_dirty_bitmap(RAMState *rs, RAMBlock *rb) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 902 | { |
Keqian Zhu | fb61358 | 2020-06-22 11:20:37 +0800 | [diff] [blame] | 903 | uint64_t new_dirty_pages = |
| 904 | cpu_physical_memory_sync_dirty_bitmap(rb, 0, rb->used_length); |
| 905 | |
| 906 | rs->migration_dirty_pages += new_dirty_pages; |
| 907 | rs->num_dirty_pages_period += new_dirty_pages; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 908 | } |
| 909 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 910 | /** |
| 911 | * ram_pagesize_summary: calculate all the pagesizes of a VM |
| 912 | * |
| 913 | * Returns a summary bitmap of the page sizes of all RAMBlocks |
| 914 | * |
| 915 | * For VMs with just normal pages this is equivalent to the host page |
| 916 | * size. If it's got some huge pages then it's the OR of all the |
| 917 | * different page sizes. |
Dr. David Alan Gilbert | e8ca1db | 2017-02-24 18:28:29 +0000 | [diff] [blame] | 918 | */ |
| 919 | uint64_t ram_pagesize_summary(void) |
| 920 | { |
| 921 | RAMBlock *block; |
| 922 | uint64_t summary = 0; |
| 923 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 924 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Dr. David Alan Gilbert | e8ca1db | 2017-02-24 18:28:29 +0000 | [diff] [blame] | 925 | summary |= block->page_size; |
| 926 | } |
| 927 | |
| 928 | return summary; |
| 929 | } |
| 930 | |
Xiao Guangrong | aecbfe9 | 2019-01-11 14:37:30 +0800 | [diff] [blame] | 931 | uint64_t ram_get_total_transferred_pages(void) |
| 932 | { |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 933 | return stat64_get(&mig_stats.normal_pages) + |
| 934 | stat64_get(&mig_stats.zero_pages) + |
Juan Quintela | 8258f2f | 2023-10-19 13:07:24 +0200 | [diff] [blame] | 935 | compress_ram_pages() + xbzrle_counters.pages; |
Xiao Guangrong | aecbfe9 | 2019-01-11 14:37:30 +0800 | [diff] [blame] | 936 | } |
| 937 | |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 938 | static void migration_update_rates(RAMState *rs, int64_t end_time) |
| 939 | { |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 940 | uint64_t page_count = rs->target_page_count - rs->target_page_count_prev; |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 941 | |
| 942 | /* calculate period counters */ |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 943 | stat64_set(&mig_stats.dirty_pages_rate, |
Juan Quintela | 72f8e58 | 2023-04-11 18:19:05 +0200 | [diff] [blame] | 944 | rs->num_dirty_pages_period * 1000 / |
| 945 | (end_time - rs->time_last_bitmap_sync)); |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 946 | |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 947 | if (!page_count) { |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 948 | return; |
| 949 | } |
| 950 | |
Juan Quintela | 87dca0c | 2023-03-01 22:20:13 +0100 | [diff] [blame] | 951 | if (migrate_xbzrle()) { |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 952 | double encoded_size, unencoded_size; |
| 953 | |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 954 | xbzrle_counters.cache_miss_rate = (double)(xbzrle_counters.cache_miss - |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 955 | rs->xbzrle_cache_miss_prev) / page_count; |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 956 | rs->xbzrle_cache_miss_prev = xbzrle_counters.cache_miss; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 957 | unencoded_size = (xbzrle_counters.pages - rs->xbzrle_pages_prev) * |
| 958 | TARGET_PAGE_SIZE; |
| 959 | encoded_size = xbzrle_counters.bytes - rs->xbzrle_bytes_prev; |
Wei Wang | 9227140 | 2020-06-17 13:13:05 -0700 | [diff] [blame] | 960 | if (xbzrle_counters.pages == rs->xbzrle_pages_prev || !encoded_size) { |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 961 | xbzrle_counters.encoding_rate = 0; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 962 | } else { |
| 963 | xbzrle_counters.encoding_rate = unencoded_size / encoded_size; |
| 964 | } |
| 965 | rs->xbzrle_pages_prev = xbzrle_counters.pages; |
| 966 | rs->xbzrle_bytes_prev = xbzrle_counters.bytes; |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 967 | } |
Juan Quintela | fb36fb2 | 2023-10-19 13:07:20 +0200 | [diff] [blame] | 968 | compress_update_rates(page_count); |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 969 | } |
| 970 | |
Hyman Huang(黄勇) | acac51b | 2023-06-08 00:12:40 +0800 | [diff] [blame] | 971 | /* |
| 972 | * Enable dirty-limit to throttle down the guest |
| 973 | */ |
| 974 | static void migration_dirty_limit_guest(void) |
| 975 | { |
| 976 | /* |
| 977 | * dirty page rate quota for all vCPUs fetched from |
| 978 | * migration parameter 'vcpu_dirty_limit' |
| 979 | */ |
| 980 | static int64_t quota_dirtyrate; |
| 981 | MigrationState *s = migrate_get_current(); |
| 982 | |
| 983 | /* |
| 984 | * If dirty limit already enabled and migration parameter |
| 985 | * vcpu-dirty-limit untouched. |
| 986 | */ |
| 987 | if (dirtylimit_in_service() && |
| 988 | quota_dirtyrate == s->parameters.vcpu_dirty_limit) { |
| 989 | return; |
| 990 | } |
| 991 | |
| 992 | quota_dirtyrate = s->parameters.vcpu_dirty_limit; |
| 993 | |
| 994 | /* |
| 995 | * Set all vCPU a quota dirtyrate, note that the second |
| 996 | * parameter will be ignored if setting all vCPU for the vm |
| 997 | */ |
| 998 | qmp_set_vcpu_dirty_limit(false, -1, quota_dirtyrate, NULL); |
| 999 | trace_migration_dirty_limit_guest(quota_dirtyrate); |
| 1000 | } |
| 1001 | |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1002 | static void migration_trigger_throttle(RAMState *rs) |
| 1003 | { |
Juan Quintela | 6499efd | 2023-03-02 00:59:13 +0100 | [diff] [blame] | 1004 | uint64_t threshold = migrate_throttle_trigger_threshold(); |
Peter Xu | 23b7576 | 2022-10-11 17:55:51 -0400 | [diff] [blame] | 1005 | uint64_t bytes_xfer_period = |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 1006 | stat64_get(&mig_stats.transferred) - rs->bytes_xfer_prev; |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1007 | uint64_t bytes_dirty_period = rs->num_dirty_pages_period * TARGET_PAGE_SIZE; |
| 1008 | uint64_t bytes_dirty_threshold = bytes_xfer_period * threshold / 100; |
| 1009 | |
| 1010 | /* During block migration the auto-converge logic incorrectly detects |
| 1011 | * that ram migration makes no progress. Avoid this by disabling the |
| 1012 | * throttling logic during the bulk phase of block migration. */ |
Hyman Huang(黄勇) | bb9993c | 2023-06-07 23:32:51 +0800 | [diff] [blame] | 1013 | if (blk_mig_bulk_active()) { |
| 1014 | return; |
| 1015 | } |
| 1016 | |
Hyman Huang(黄勇) | 310ad56 | 2023-06-15 21:29:44 +0800 | [diff] [blame] | 1017 | /* |
| 1018 | * The following detection logic can be refined later. For now: |
| 1019 | * Check to see if the ratio between dirtied bytes and the approx. |
| 1020 | * amount of bytes that just got transferred since the last time |
| 1021 | * we were in this routine reaches the threshold. If that happens |
| 1022 | * twice, start or increase throttling. |
| 1023 | */ |
| 1024 | if ((bytes_dirty_period > bytes_dirty_threshold) && |
| 1025 | (++rs->dirty_rate_high_cnt >= 2)) { |
| 1026 | rs->dirty_rate_high_cnt = 0; |
| 1027 | if (migrate_auto_converge()) { |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1028 | trace_migration_throttle(); |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 1029 | mig_throttle_guest_down(bytes_dirty_period, |
| 1030 | bytes_dirty_threshold); |
Hyman Huang(黄勇) | acac51b | 2023-06-08 00:12:40 +0800 | [diff] [blame] | 1031 | } else if (migrate_dirty_limit()) { |
| 1032 | migration_dirty_limit_guest(); |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | |
Gavin Shan | 1e493be | 2023-05-09 12:21:19 +1000 | [diff] [blame] | 1037 | static void migration_bitmap_sync(RAMState *rs, bool last_stage) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1038 | { |
| 1039 | RAMBlock *block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1040 | int64_t end_time; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1041 | |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 1042 | stat64_add(&mig_stats.dirty_sync_count, 1); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1043 | |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 1044 | if (!rs->time_last_bitmap_sync) { |
| 1045 | rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | trace_migration_bitmap_sync_start(); |
Gavin Shan | 1e493be | 2023-05-09 12:21:19 +1000 | [diff] [blame] | 1049 | memory_global_dirty_log_sync(last_stage); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1050 | |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 1051 | qemu_mutex_lock(&rs->bitmap_mutex); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 1052 | WITH_RCU_READ_LOCK_GUARD() { |
| 1053 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 1054 | ramblock_sync_dirty_bitmap(rs, block); |
| 1055 | } |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 1056 | stat64_set(&mig_stats.dirty_bytes_last_sync, ram_bytes_remaining()); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1057 | } |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 1058 | qemu_mutex_unlock(&rs->bitmap_mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1059 | |
Paolo Bonzini | 9458a9a | 2018-02-06 18:37:39 +0100 | [diff] [blame] | 1060 | memory_global_after_dirty_log_sync(); |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 1061 | trace_migration_bitmap_sync_end(rs->num_dirty_pages_period); |
Chao Fan | 1ffb5df | 2017-03-14 09:55:07 +0800 | [diff] [blame] | 1062 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1063 | end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 1064 | |
| 1065 | /* more than 1 second = 1000 millisecons */ |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 1066 | if (end_time > rs->time_last_bitmap_sync + 1000) { |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1067 | migration_trigger_throttle(rs); |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 1068 | |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 1069 | migration_update_rates(rs, end_time); |
| 1070 | |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 1071 | rs->target_page_count_prev = rs->target_page_count; |
Felipe Franciosi | d693c6f | 2017-05-24 17:10:01 +0100 | [diff] [blame] | 1072 | |
| 1073 | /* reset period counters */ |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 1074 | rs->time_last_bitmap_sync = end_time; |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 1075 | rs->num_dirty_pages_period = 0; |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 1076 | rs->bytes_xfer_prev = stat64_get(&mig_stats.transferred); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1077 | } |
Juan Quintela | b890902 | 2023-03-01 22:08:09 +0100 | [diff] [blame] | 1078 | if (migrate_events()) { |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 1079 | uint64_t generation = stat64_get(&mig_stats.dirty_sync_count); |
Juan Quintela | 536b5a4 | 2023-04-11 18:02:34 +0200 | [diff] [blame] | 1080 | qapi_event_send_migration_pass(generation); |
Dr. David Alan Gilbert | 4addcd4 | 2015-12-16 11:47:36 +0000 | [diff] [blame] | 1081 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Gavin Shan | 1e493be | 2023-05-09 12:21:19 +1000 | [diff] [blame] | 1084 | static void migration_bitmap_sync_precopy(RAMState *rs, bool last_stage) |
Wei Wang | bd22706 | 2018-12-11 16:24:51 +0800 | [diff] [blame] | 1085 | { |
| 1086 | Error *local_err = NULL; |
| 1087 | |
| 1088 | /* |
| 1089 | * The current notifier usage is just an optimization to migration, so we |
| 1090 | * don't stop the normal migration process in the error case. |
| 1091 | */ |
| 1092 | if (precopy_notify(PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC, &local_err)) { |
| 1093 | error_report_err(local_err); |
Vladimir Sementsov-Ogievskiy | b4a1733 | 2020-03-24 18:36:29 +0300 | [diff] [blame] | 1094 | local_err = NULL; |
Wei Wang | bd22706 | 2018-12-11 16:24:51 +0800 | [diff] [blame] | 1095 | } |
| 1096 | |
Gavin Shan | 1e493be | 2023-05-09 12:21:19 +1000 | [diff] [blame] | 1097 | migration_bitmap_sync(rs, last_stage); |
Wei Wang | bd22706 | 2018-12-11 16:24:51 +0800 | [diff] [blame] | 1098 | |
| 1099 | if (precopy_notify(PRECOPY_NOTIFY_AFTER_BITMAP_SYNC, &local_err)) { |
| 1100 | error_report_err(local_err); |
| 1101 | } |
| 1102 | } |
| 1103 | |
Juan Quintela | a4dbaf8 | 2021-12-16 10:19:38 +0100 | [diff] [blame] | 1104 | void ram_release_page(const char *rbname, uint64_t offset) |
Juan Quintela | 47fe16f | 2021-12-16 09:58:49 +0100 | [diff] [blame] | 1105 | { |
| 1106 | if (!migrate_release_ram() || !migration_in_postcopy()) { |
| 1107 | return; |
| 1108 | } |
| 1109 | |
| 1110 | ram_discard_range(rbname, offset, TARGET_PAGE_SIZE); |
| 1111 | } |
| 1112 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1113 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1114 | * save_zero_page: send the zero page to the stream |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1115 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1116 | * Returns the number of pages written. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1117 | * |
Fabiano Rosas | ccc09db | 2023-10-11 15:46:02 -0300 | [diff] [blame] | 1118 | * @rs: current RAM state |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 1119 | * @pss: current PSS channel |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1120 | * @offset: offset inside the block for the page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1121 | */ |
Juan Quintela | e8e4e7a | 2023-06-13 13:38:30 +0200 | [diff] [blame] | 1122 | static int save_zero_page(RAMState *rs, PageSearchStatus *pss, |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 1123 | ram_addr_t offset) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1124 | { |
Juan Quintela | e8e4e7a | 2023-06-13 13:38:30 +0200 | [diff] [blame] | 1125 | uint8_t *p = pss->block->host + offset; |
Fabiano Rosas | 8697eb8 | 2023-10-11 15:46:03 -0300 | [diff] [blame] | 1126 | QEMUFile *file = pss->pss_channel; |
| 1127 | int len = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1128 | |
Fabiano Rosas | 8697eb8 | 2023-10-11 15:46:03 -0300 | [diff] [blame] | 1129 | if (!buffer_is_zero(p, TARGET_PAGE_SIZE)) { |
| 1130 | return 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1131 | } |
Fabiano Rosas | ccc09db | 2023-10-11 15:46:02 -0300 | [diff] [blame] | 1132 | |
Juan Quintela | e8e4e7a | 2023-06-13 13:38:30 +0200 | [diff] [blame] | 1133 | len += save_page_header(pss, file, pss->block, offset | RAM_SAVE_FLAG_ZERO); |
Fabiano Rosas | 8697eb8 | 2023-10-11 15:46:03 -0300 | [diff] [blame] | 1134 | qemu_put_byte(file, 0); |
| 1135 | len += 1; |
Juan Quintela | e8e4e7a | 2023-06-13 13:38:30 +0200 | [diff] [blame] | 1136 | ram_release_page(pss->block->idstr, offset); |
Fabiano Rosas | 8697eb8 | 2023-10-11 15:46:03 -0300 | [diff] [blame] | 1137 | |
Fabiano Rosas | ccc09db | 2023-10-11 15:46:02 -0300 | [diff] [blame] | 1138 | stat64_add(&mig_stats.zero_pages, 1); |
| 1139 | ram_transferred_add(len); |
| 1140 | |
| 1141 | /* |
| 1142 | * Must let xbzrle know, otherwise a previous (now 0'd) cached |
| 1143 | * page would be stale. |
| 1144 | */ |
| 1145 | if (rs->xbzrle_started) { |
| 1146 | XBZRLE_cache_lock(); |
Juan Quintela | e8e4e7a | 2023-06-13 13:38:30 +0200 | [diff] [blame] | 1147 | xbzrle_cache_zero_page(pss->block->offset + offset); |
Fabiano Rosas | ccc09db | 2023-10-11 15:46:02 -0300 | [diff] [blame] | 1148 | XBZRLE_cache_unlock(); |
| 1149 | } |
| 1150 | |
Fabiano Rosas | 8697eb8 | 2023-10-11 15:46:03 -0300 | [diff] [blame] | 1151 | return len; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1154 | /* |
| 1155 | * @pages: the number of pages written by the control path, |
| 1156 | * < 0 - error |
| 1157 | * > 0 - number of pages written |
| 1158 | * |
| 1159 | * Return true if the pages has been saved, otherwise false is returned. |
| 1160 | */ |
Juan Quintela | 944853c | 2023-06-13 13:36:24 +0200 | [diff] [blame] | 1161 | static bool control_save_page(PageSearchStatus *pss, |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 1162 | ram_addr_t offset, int *pages) |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1163 | { |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1164 | int ret; |
| 1165 | |
Juan Quintela | 944853c | 2023-06-13 13:36:24 +0200 | [diff] [blame] | 1166 | ret = rdma_control_save_page(pss->pss_channel, pss->block->offset, offset, |
Juan Quintela | e493008 | 2023-10-11 22:35:20 +0200 | [diff] [blame] | 1167 | TARGET_PAGE_SIZE); |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1168 | if (ret == RAM_SAVE_CONTROL_NOT_SUPP) { |
| 1169 | return false; |
| 1170 | } |
| 1171 | |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1172 | if (ret == RAM_SAVE_CONTROL_DELAYED) { |
Juan Quintela | 9c53d36 | 2023-05-15 21:57:08 +0200 | [diff] [blame] | 1173 | *pages = 1; |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1174 | return true; |
| 1175 | } |
Juan Quintela | 9c53d36 | 2023-05-15 21:57:08 +0200 | [diff] [blame] | 1176 | *pages = ret; |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1177 | return true; |
| 1178 | } |
| 1179 | |
Xiao Guangrong | 65dacaa | 2018-03-30 15:51:27 +0800 | [diff] [blame] | 1180 | /* |
| 1181 | * directly send the page to the stream |
| 1182 | * |
| 1183 | * Returns the number of pages written. |
| 1184 | * |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 1185 | * @pss: current PSS channel |
Xiao Guangrong | 65dacaa | 2018-03-30 15:51:27 +0800 | [diff] [blame] | 1186 | * @block: block that contains the page we want to send |
| 1187 | * @offset: offset inside the block for the page |
| 1188 | * @buf: the page to be sent |
| 1189 | * @async: send to page asyncly |
| 1190 | */ |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 1191 | static int save_normal_page(PageSearchStatus *pss, RAMBlock *block, |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 1192 | ram_addr_t offset, uint8_t *buf, bool async) |
Xiao Guangrong | 65dacaa | 2018-03-30 15:51:27 +0800 | [diff] [blame] | 1193 | { |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 1194 | QEMUFile *file = pss->pss_channel; |
| 1195 | |
Lukas Straub | 37502df | 2023-04-02 17:06:32 +0000 | [diff] [blame] | 1196 | ram_transferred_add(save_page_header(pss, pss->pss_channel, block, |
David Edmondson | 4c2d0f6 | 2021-12-21 09:34:40 +0000 | [diff] [blame] | 1197 | offset | RAM_SAVE_FLAG_PAGE)); |
Xiao Guangrong | 65dacaa | 2018-03-30 15:51:27 +0800 | [diff] [blame] | 1198 | if (async) { |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 1199 | qemu_put_buffer_async(file, buf, TARGET_PAGE_SIZE, |
Dr. David Alan Gilbert | f912ec5 | 2022-04-06 11:25:15 +0100 | [diff] [blame] | 1200 | migrate_release_ram() && |
Xiao Guangrong | 65dacaa | 2018-03-30 15:51:27 +0800 | [diff] [blame] | 1201 | migration_in_postcopy()); |
| 1202 | } else { |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 1203 | qemu_put_buffer(file, buf, TARGET_PAGE_SIZE); |
Xiao Guangrong | 65dacaa | 2018-03-30 15:51:27 +0800 | [diff] [blame] | 1204 | } |
David Edmondson | 4c2d0f6 | 2021-12-21 09:34:40 +0000 | [diff] [blame] | 1205 | ram_transferred_add(TARGET_PAGE_SIZE); |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 1206 | stat64_add(&mig_stats.normal_pages, 1); |
Xiao Guangrong | 65dacaa | 2018-03-30 15:51:27 +0800 | [diff] [blame] | 1207 | return 1; |
| 1208 | } |
| 1209 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1210 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1211 | * ram_save_page: send the given page to the stream |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1212 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1213 | * Returns the number of pages written. |
Dr. David Alan Gilbert | 3fd3c4b | 2015-12-10 16:31:46 +0000 | [diff] [blame] | 1214 | * < 0 - error |
| 1215 | * >=0 - Number of pages written - this might legally be 0 |
| 1216 | * if xbzrle noticed the page was the same. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1217 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1218 | * @rs: current RAM state |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1219 | * @block: block that contains the page we want to send |
| 1220 | * @offset: offset inside the block for the page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1221 | */ |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 1222 | static int ram_save_page(RAMState *rs, PageSearchStatus *pss) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1223 | { |
| 1224 | int pages = -1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1225 | uint8_t *p; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1226 | bool send_async = true; |
zhanghailiang | a08f689 | 2016-01-15 11:37:44 +0800 | [diff] [blame] | 1227 | RAMBlock *block = pss->block; |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 1228 | ram_addr_t offset = ((ram_addr_t)pss->page) << TARGET_PAGE_BITS; |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1229 | ram_addr_t current_addr = block->offset + offset; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1230 | |
Dr. David Alan Gilbert | 2f68e39 | 2015-08-13 11:51:30 +0100 | [diff] [blame] | 1231 | p = block->host + offset; |
Dr. David Alan Gilbert | 1db9d8e | 2017-04-26 19:37:21 +0100 | [diff] [blame] | 1232 | trace_ram_save_page(block->idstr, (uint64_t)offset, p); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1233 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1234 | XBZRLE_cache_lock(); |
Juan Quintela | f3095cc | 2023-05-04 13:53:23 +0200 | [diff] [blame] | 1235 | if (rs->xbzrle_started && !migration_in_postcopy()) { |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 1236 | pages = save_xbzrle_page(rs, pss, &p, current_addr, |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 1237 | block, offset); |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 1238 | if (!rs->last_stage) { |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1239 | /* Can't send this cached data async, since the cache page |
| 1240 | * might get updated before it gets to the wire |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1241 | */ |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1242 | send_async = false; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | /* XBZRLE overflow or normal page */ |
| 1247 | if (pages == -1) { |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 1248 | pages = save_normal_page(pss, block, offset, p, send_async); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | XBZRLE_cache_unlock(); |
| 1252 | |
| 1253 | return pages; |
| 1254 | } |
| 1255 | |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 1256 | static int ram_save_multifd_page(QEMUFile *file, RAMBlock *block, |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 1257 | ram_addr_t offset) |
| 1258 | { |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 1259 | if (multifd_queue_page(file, block, offset) < 0) { |
Ivan Ren | 713f762 | 2019-06-25 21:18:17 +0800 | [diff] [blame] | 1260 | return -1; |
| 1261 | } |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 1262 | stat64_add(&mig_stats.normal_pages, 1); |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 1263 | |
| 1264 | return 1; |
| 1265 | } |
| 1266 | |
Juan Quintela | 742ec5f | 2023-10-19 13:07:21 +0200 | [diff] [blame] | 1267 | int compress_send_queued_data(CompressParam *param) |
Lukas Straub | 3e81763 | 2023-04-20 11:48:06 +0200 | [diff] [blame] | 1268 | { |
| 1269 | PageSearchStatus *pss = &ram_state->pss[RAM_CHANNEL_PRECOPY]; |
| 1270 | MigrationState *ms = migrate_get_current(); |
| 1271 | QEMUFile *file = ms->to_dst_file; |
| 1272 | int len = 0; |
| 1273 | |
| 1274 | RAMBlock *block = param->block; |
| 1275 | ram_addr_t offset = param->offset; |
| 1276 | |
| 1277 | if (param->result == RES_NONE) { |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | assert(block == pss->last_sent_block); |
| 1282 | |
| 1283 | if (param->result == RES_ZEROPAGE) { |
Lukas Straub | 4024cc8 | 2023-04-20 11:48:31 +0200 | [diff] [blame] | 1284 | assert(qemu_file_buffer_empty(param->file)); |
Lukas Straub | 3e81763 | 2023-04-20 11:48:06 +0200 | [diff] [blame] | 1285 | len += save_page_header(pss, file, block, offset | RAM_SAVE_FLAG_ZERO); |
| 1286 | qemu_put_byte(file, 0); |
| 1287 | len += 1; |
| 1288 | ram_release_page(block->idstr, offset); |
| 1289 | } else if (param->result == RES_COMPRESS) { |
Lukas Straub | 4024cc8 | 2023-04-20 11:48:31 +0200 | [diff] [blame] | 1290 | assert(!qemu_file_buffer_empty(param->file)); |
Lukas Straub | 3e81763 | 2023-04-20 11:48:06 +0200 | [diff] [blame] | 1291 | len += save_page_header(pss, file, block, |
| 1292 | offset | RAM_SAVE_FLAG_COMPRESS_PAGE); |
| 1293 | len += qemu_put_qemu_file(file, param->file); |
| 1294 | } else { |
| 1295 | abort(); |
| 1296 | } |
| 1297 | |
Lukas Straub | 680628d | 2023-04-20 11:48:10 +0200 | [diff] [blame] | 1298 | update_compress_thread_counts(param, len); |
| 1299 | |
Lukas Straub | 3e81763 | 2023-04-20 11:48:06 +0200 | [diff] [blame] | 1300 | return len; |
| 1301 | } |
| 1302 | |
Juan Quintela | 31e2ac7 | 2022-06-21 13:29:36 +0200 | [diff] [blame] | 1303 | #define PAGE_ALL_CLEAN 0 |
| 1304 | #define PAGE_TRY_AGAIN 1 |
| 1305 | #define PAGE_DIRTY_FOUND 2 |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1306 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1307 | * find_dirty_block: find the next dirty page and update any state |
| 1308 | * associated with the search process. |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1309 | * |
Juan Quintela | 31e2ac7 | 2022-06-21 13:29:36 +0200 | [diff] [blame] | 1310 | * Returns: |
Juan Quintela | 294e5a4 | 2022-06-21 13:36:11 +0200 | [diff] [blame] | 1311 | * <0: An error happened |
Juan Quintela | 31e2ac7 | 2022-06-21 13:29:36 +0200 | [diff] [blame] | 1312 | * PAGE_ALL_CLEAN: no dirty page found, give up |
| 1313 | * PAGE_TRY_AGAIN: no dirty page found, retry for next block |
| 1314 | * PAGE_DIRTY_FOUND: dirty page found |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1315 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1316 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1317 | * @pss: data about the state of the current dirty page scan |
| 1318 | * @again: set to false if the search has scanned the whole of RAM |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1319 | */ |
Juan Quintela | 31e2ac7 | 2022-06-21 13:29:36 +0200 | [diff] [blame] | 1320 | static int find_dirty_block(RAMState *rs, PageSearchStatus *pss) |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1321 | { |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 1322 | /* Update pss->page for the next dirty bit in ramblock */ |
| 1323 | pss_find_next_dirty(pss); |
| 1324 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1325 | if (pss->complete_round && pss->block == rs->last_seen_block && |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1326 | pss->page >= rs->last_page) { |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1327 | /* |
| 1328 | * We've been once around the RAM and haven't found anything. |
| 1329 | * Give up. |
| 1330 | */ |
Juan Quintela | 31e2ac7 | 2022-06-21 13:29:36 +0200 | [diff] [blame] | 1331 | return PAGE_ALL_CLEAN; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1332 | } |
David Hildenbrand | 542147f | 2021-04-29 13:27:08 +0200 | [diff] [blame] | 1333 | if (!offset_in_ramblock(pss->block, |
| 1334 | ((ram_addr_t)pss->page) << TARGET_PAGE_BITS)) { |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1335 | /* Didn't find anything in this RAM Block */ |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1336 | pss->page = 0; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1337 | pss->block = QLIST_NEXT_RCU(pss->block, next); |
| 1338 | if (!pss->block) { |
Juan Quintela | d4f3448 | 2023-10-11 22:55:48 +0200 | [diff] [blame] | 1339 | if (migrate_multifd() && |
| 1340 | !migrate_multifd_flush_after_each_section()) { |
Juan Quintela | 294e5a4 | 2022-06-21 13:36:11 +0200 | [diff] [blame] | 1341 | QEMUFile *f = rs->pss[RAM_CHANNEL_PRECOPY].pss_channel; |
| 1342 | int ret = multifd_send_sync_main(f); |
| 1343 | if (ret < 0) { |
| 1344 | return ret; |
| 1345 | } |
| 1346 | qemu_put_be64(f, RAM_SAVE_FLAG_MULTIFD_FLUSH); |
| 1347 | qemu_fflush(f); |
| 1348 | } |
Xiao Guangrong | 48df9d8 | 2018-09-06 15:00:59 +0800 | [diff] [blame] | 1349 | /* |
| 1350 | * If memory migration starts over, we will meet a dirtied page |
| 1351 | * which may still exists in compression threads's ring, so we |
| 1352 | * should flush the compressed data to make sure the new page |
| 1353 | * is not overwritten by the old one in the destination. |
| 1354 | * |
| 1355 | * Also If xbzrle is on, stop using the data compression at this |
| 1356 | * point. In theory, xbzrle can do better than compression. |
| 1357 | */ |
Juan Quintela | 8020bc9 | 2023-10-19 13:07:22 +0200 | [diff] [blame] | 1358 | compress_flush_data(); |
Xiao Guangrong | 48df9d8 | 2018-09-06 15:00:59 +0800 | [diff] [blame] | 1359 | |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1360 | /* Hit the end of the list */ |
| 1361 | pss->block = QLIST_FIRST_RCU(&ram_list.blocks); |
| 1362 | /* Flag that we've looped */ |
| 1363 | pss->complete_round = true; |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 1364 | /* After the first round, enable XBZRLE. */ |
Juan Quintela | 87dca0c | 2023-03-01 22:20:13 +0100 | [diff] [blame] | 1365 | if (migrate_xbzrle()) { |
Juan Quintela | f3095cc | 2023-05-04 13:53:23 +0200 | [diff] [blame] | 1366 | rs->xbzrle_started = true; |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 1367 | } |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1368 | } |
| 1369 | /* Didn't find anything this time, but try again on the new block */ |
Juan Quintela | 31e2ac7 | 2022-06-21 13:29:36 +0200 | [diff] [blame] | 1370 | return PAGE_TRY_AGAIN; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1371 | } else { |
Juan Quintela | 31e2ac7 | 2022-06-21 13:29:36 +0200 | [diff] [blame] | 1372 | /* We've found something */ |
| 1373 | return PAGE_DIRTY_FOUND; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1377 | /** |
| 1378 | * unqueue_page: gets a page of the queue |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1379 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1380 | * Helper for 'get_queued_page' - gets a page off the queue |
| 1381 | * |
| 1382 | * Returns the block of the page (or NULL if none available) |
| 1383 | * |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1384 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1385 | * @offset: used to return the offset within the RAMBlock |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1386 | */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1387 | static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1388 | { |
Peter Xu | a1fe28d | 2022-01-19 16:09:18 +0800 | [diff] [blame] | 1389 | struct RAMSrcPageRequest *entry; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1390 | RAMBlock *block = NULL; |
| 1391 | |
Peter Xu | a1fe28d | 2022-01-19 16:09:18 +0800 | [diff] [blame] | 1392 | if (!postcopy_has_request(rs)) { |
Xiao Guangrong | ae526e3 | 2018-08-21 16:10:25 +0800 | [diff] [blame] | 1393 | return NULL; |
| 1394 | } |
| 1395 | |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame] | 1396 | QEMU_LOCK_GUARD(&rs->src_page_req_mutex); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1397 | |
Peter Xu | a1fe28d | 2022-01-19 16:09:18 +0800 | [diff] [blame] | 1398 | /* |
| 1399 | * This should _never_ change even after we take the lock, because no one |
| 1400 | * should be taking anything off the request list other than us. |
| 1401 | */ |
| 1402 | assert(postcopy_has_request(rs)); |
| 1403 | |
| 1404 | entry = QSIMPLEQ_FIRST(&rs->src_page_requests); |
| 1405 | block = entry->rb; |
| 1406 | *offset = entry->offset; |
| 1407 | |
Thomas Huth | 777f53c | 2022-08-02 08:19:49 +0200 | [diff] [blame] | 1408 | if (entry->len > TARGET_PAGE_SIZE) { |
| 1409 | entry->len -= TARGET_PAGE_SIZE; |
| 1410 | entry->offset += TARGET_PAGE_SIZE; |
Peter Xu | a1fe28d | 2022-01-19 16:09:18 +0800 | [diff] [blame] | 1411 | } else { |
| 1412 | memory_region_unref(block->mr); |
| 1413 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); |
| 1414 | g_free(entry); |
| 1415 | migration_consume_urgent_request(); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1416 | } |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1417 | |
| 1418 | return block; |
| 1419 | } |
| 1420 | |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1421 | #if defined(__linux__) |
| 1422 | /** |
| 1423 | * poll_fault_page: try to get next UFFD write fault page and, if pending fault |
| 1424 | * is found, return RAM block pointer and page offset |
| 1425 | * |
| 1426 | * Returns pointer to the RAMBlock containing faulting page, |
| 1427 | * NULL if no write faults are pending |
| 1428 | * |
| 1429 | * @rs: current RAM state |
| 1430 | * @offset: page offset from the beginning of the block |
| 1431 | */ |
| 1432 | static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) |
| 1433 | { |
| 1434 | struct uffd_msg uffd_msg; |
| 1435 | void *page_address; |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1436 | RAMBlock *block; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1437 | int res; |
| 1438 | |
| 1439 | if (!migrate_background_snapshot()) { |
| 1440 | return NULL; |
| 1441 | } |
| 1442 | |
| 1443 | res = uffd_read_events(rs->uffdio_fd, &uffd_msg, 1); |
| 1444 | if (res <= 0) { |
| 1445 | return NULL; |
| 1446 | } |
| 1447 | |
| 1448 | page_address = (void *)(uintptr_t) uffd_msg.arg.pagefault.address; |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1449 | block = qemu_ram_block_from_host(page_address, false, offset); |
| 1450 | assert(block && (block->flags & RAM_UF_WRITEPROTECT) != 0); |
| 1451 | return block; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * ram_save_release_protection: release UFFD write protection after |
| 1456 | * a range of pages has been saved |
| 1457 | * |
| 1458 | * @rs: current RAM state |
| 1459 | * @pss: page-search-status structure |
| 1460 | * @start_page: index of the first page in the range relative to pss->block |
| 1461 | * |
| 1462 | * Returns 0 on success, negative value in case of an error |
| 1463 | */ |
| 1464 | static int ram_save_release_protection(RAMState *rs, PageSearchStatus *pss, |
| 1465 | unsigned long start_page) |
| 1466 | { |
| 1467 | int res = 0; |
| 1468 | |
| 1469 | /* Check if page is from UFFD-managed region. */ |
| 1470 | if (pss->block->flags & RAM_UF_WRITEPROTECT) { |
| 1471 | void *page_address = pss->block->host + (start_page << TARGET_PAGE_BITS); |
Peter Xu | 258f5c98 | 2022-01-19 16:09:15 +0800 | [diff] [blame] | 1472 | uint64_t run_length = (pss->page - start_page) << TARGET_PAGE_BITS; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1473 | |
| 1474 | /* Flush async buffers before un-protect. */ |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 1475 | qemu_fflush(pss->pss_channel); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1476 | /* Un-protect memory range. */ |
| 1477 | res = uffd_change_protection(rs->uffdio_fd, page_address, run_length, |
| 1478 | false, false); |
| 1479 | } |
| 1480 | |
| 1481 | return res; |
| 1482 | } |
| 1483 | |
| 1484 | /* ram_write_tracking_available: check if kernel supports required UFFD features |
| 1485 | * |
| 1486 | * Returns true if supports, false otherwise |
| 1487 | */ |
| 1488 | bool ram_write_tracking_available(void) |
| 1489 | { |
| 1490 | uint64_t uffd_features; |
| 1491 | int res; |
| 1492 | |
| 1493 | res = uffd_query_features(&uffd_features); |
| 1494 | return (res == 0 && |
| 1495 | (uffd_features & UFFD_FEATURE_PAGEFAULT_FLAG_WP) != 0); |
| 1496 | } |
| 1497 | |
| 1498 | /* ram_write_tracking_compatible: check if guest configuration is |
| 1499 | * compatible with 'write-tracking' |
| 1500 | * |
| 1501 | * Returns true if compatible, false otherwise |
| 1502 | */ |
| 1503 | bool ram_write_tracking_compatible(void) |
| 1504 | { |
| 1505 | const uint64_t uffd_ioctls_mask = BIT(_UFFDIO_WRITEPROTECT); |
| 1506 | int uffd_fd; |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1507 | RAMBlock *block; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1508 | bool ret = false; |
| 1509 | |
| 1510 | /* Open UFFD file descriptor */ |
| 1511 | uffd_fd = uffd_create_fd(UFFD_FEATURE_PAGEFAULT_FLAG_WP, false); |
| 1512 | if (uffd_fd < 0) { |
| 1513 | return false; |
| 1514 | } |
| 1515 | |
| 1516 | RCU_READ_LOCK_GUARD(); |
| 1517 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1518 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1519 | uint64_t uffd_ioctls; |
| 1520 | |
| 1521 | /* Nothing to do with read-only and MMIO-writable regions */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1522 | if (block->mr->readonly || block->mr->rom_device) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1523 | continue; |
| 1524 | } |
| 1525 | /* Try to register block memory via UFFD-IO to track writes */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1526 | if (uffd_register_memory(uffd_fd, block->host, block->max_length, |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1527 | UFFDIO_REGISTER_MODE_WP, &uffd_ioctls)) { |
| 1528 | goto out; |
| 1529 | } |
| 1530 | if ((uffd_ioctls & uffd_ioctls_mask) != uffd_ioctls_mask) { |
| 1531 | goto out; |
| 1532 | } |
| 1533 | } |
| 1534 | ret = true; |
| 1535 | |
| 1536 | out: |
| 1537 | uffd_close_fd(uffd_fd); |
| 1538 | return ret; |
| 1539 | } |
| 1540 | |
David Hildenbrand | f7b9dcf | 2021-10-11 19:53:45 +0200 | [diff] [blame] | 1541 | static inline void populate_read_range(RAMBlock *block, ram_addr_t offset, |
| 1542 | ram_addr_t size) |
| 1543 | { |
David Hildenbrand | 5f19a44 | 2023-01-05 13:45:24 +0100 | [diff] [blame] | 1544 | const ram_addr_t end = offset + size; |
| 1545 | |
David Hildenbrand | f7b9dcf | 2021-10-11 19:53:45 +0200 | [diff] [blame] | 1546 | /* |
| 1547 | * We read one byte of each page; this will preallocate page tables if |
| 1548 | * required and populate the shared zeropage on MAP_PRIVATE anonymous memory |
| 1549 | * where no page was populated yet. This might require adaption when |
| 1550 | * supporting other mappings, like shmem. |
| 1551 | */ |
David Hildenbrand | 5f19a44 | 2023-01-05 13:45:24 +0100 | [diff] [blame] | 1552 | for (; offset < end; offset += block->page_size) { |
David Hildenbrand | f7b9dcf | 2021-10-11 19:53:45 +0200 | [diff] [blame] | 1553 | char tmp = *((char *)block->host + offset); |
| 1554 | |
| 1555 | /* Don't optimize the read out */ |
| 1556 | asm volatile("" : "+r" (tmp)); |
| 1557 | } |
| 1558 | } |
| 1559 | |
David Hildenbrand | 6fee3a1 | 2021-10-11 19:53:46 +0200 | [diff] [blame] | 1560 | static inline int populate_read_section(MemoryRegionSection *section, |
| 1561 | void *opaque) |
| 1562 | { |
| 1563 | const hwaddr size = int128_get64(section->size); |
| 1564 | hwaddr offset = section->offset_within_region; |
| 1565 | RAMBlock *block = section->mr->ram_block; |
| 1566 | |
| 1567 | populate_read_range(block, offset, size); |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1571 | /* |
David Hildenbrand | f7b9dcf | 2021-10-11 19:53:45 +0200 | [diff] [blame] | 1572 | * ram_block_populate_read: preallocate page tables and populate pages in the |
| 1573 | * RAM block by reading a byte of each page. |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1574 | * |
| 1575 | * Since it's solely used for userfault_fd WP feature, here we just |
| 1576 | * hardcode page size to qemu_real_host_page_size. |
| 1577 | * |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1578 | * @block: RAM block to populate |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1579 | */ |
David Hildenbrand | 6fee3a1 | 2021-10-11 19:53:46 +0200 | [diff] [blame] | 1580 | static void ram_block_populate_read(RAMBlock *rb) |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1581 | { |
David Hildenbrand | 6fee3a1 | 2021-10-11 19:53:46 +0200 | [diff] [blame] | 1582 | /* |
| 1583 | * Skip populating all pages that fall into a discarded range as managed by |
| 1584 | * a RamDiscardManager responsible for the mapped memory region of the |
| 1585 | * RAMBlock. Such discarded ("logically unplugged") parts of a RAMBlock |
| 1586 | * must not get populated automatically. We don't have to track |
| 1587 | * modifications via userfaultfd WP reliably, because these pages will |
| 1588 | * not be part of the migration stream either way -- see |
| 1589 | * ramblock_dirty_bitmap_exclude_discarded_pages(). |
| 1590 | * |
| 1591 | * Note: The result is only stable while migrating (precopy/postcopy). |
| 1592 | */ |
| 1593 | if (rb->mr && memory_region_has_ram_discard_manager(rb->mr)) { |
| 1594 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(rb->mr); |
| 1595 | MemoryRegionSection section = { |
| 1596 | .mr = rb->mr, |
| 1597 | .offset_within_region = 0, |
| 1598 | .size = rb->mr->size, |
| 1599 | }; |
| 1600 | |
| 1601 | ram_discard_manager_replay_populated(rdm, §ion, |
| 1602 | populate_read_section, NULL); |
| 1603 | } else { |
| 1604 | populate_read_range(rb, 0, rb->used_length); |
| 1605 | } |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * ram_write_tracking_prepare: prepare for UFFD-WP memory tracking |
| 1610 | */ |
| 1611 | void ram_write_tracking_prepare(void) |
| 1612 | { |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1613 | RAMBlock *block; |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1614 | |
| 1615 | RCU_READ_LOCK_GUARD(); |
| 1616 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1617 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1618 | /* Nothing to do with read-only and MMIO-writable regions */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1619 | if (block->mr->readonly || block->mr->rom_device) { |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1620 | continue; |
| 1621 | } |
| 1622 | |
| 1623 | /* |
| 1624 | * Populate pages of the RAM block before enabling userfault_fd |
| 1625 | * write protection. |
| 1626 | * |
| 1627 | * This stage is required since ioctl(UFFDIO_WRITEPROTECT) with |
| 1628 | * UFFDIO_WRITEPROTECT_MODE_WP mode setting would silently skip |
| 1629 | * pages with pte_none() entries in page table. |
| 1630 | */ |
David Hildenbrand | f7b9dcf | 2021-10-11 19:53:45 +0200 | [diff] [blame] | 1631 | ram_block_populate_read(block); |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1632 | } |
| 1633 | } |
| 1634 | |
David Hildenbrand | e41c577 | 2023-01-05 13:45:28 +0100 | [diff] [blame] | 1635 | static inline int uffd_protect_section(MemoryRegionSection *section, |
| 1636 | void *opaque) |
| 1637 | { |
| 1638 | const hwaddr size = int128_get64(section->size); |
| 1639 | const hwaddr offset = section->offset_within_region; |
| 1640 | RAMBlock *rb = section->mr->ram_block; |
| 1641 | int uffd_fd = (uintptr_t)opaque; |
| 1642 | |
| 1643 | return uffd_change_protection(uffd_fd, rb->host + offset, size, true, |
| 1644 | false); |
| 1645 | } |
| 1646 | |
| 1647 | static int ram_block_uffd_protect(RAMBlock *rb, int uffd_fd) |
| 1648 | { |
| 1649 | assert(rb->flags & RAM_UF_WRITEPROTECT); |
| 1650 | |
| 1651 | /* See ram_block_populate_read() */ |
| 1652 | if (rb->mr && memory_region_has_ram_discard_manager(rb->mr)) { |
| 1653 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(rb->mr); |
| 1654 | MemoryRegionSection section = { |
| 1655 | .mr = rb->mr, |
| 1656 | .offset_within_region = 0, |
| 1657 | .size = rb->mr->size, |
| 1658 | }; |
| 1659 | |
| 1660 | return ram_discard_manager_replay_populated(rdm, §ion, |
| 1661 | uffd_protect_section, |
| 1662 | (void *)(uintptr_t)uffd_fd); |
| 1663 | } |
| 1664 | return uffd_change_protection(uffd_fd, rb->host, |
| 1665 | rb->used_length, true, false); |
| 1666 | } |
| 1667 | |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1668 | /* |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1669 | * ram_write_tracking_start: start UFFD-WP memory tracking |
| 1670 | * |
| 1671 | * Returns 0 for success or negative value in case of error |
| 1672 | */ |
| 1673 | int ram_write_tracking_start(void) |
| 1674 | { |
| 1675 | int uffd_fd; |
| 1676 | RAMState *rs = ram_state; |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1677 | RAMBlock *block; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1678 | |
| 1679 | /* Open UFFD file descriptor */ |
| 1680 | uffd_fd = uffd_create_fd(UFFD_FEATURE_PAGEFAULT_FLAG_WP, true); |
| 1681 | if (uffd_fd < 0) { |
| 1682 | return uffd_fd; |
| 1683 | } |
| 1684 | rs->uffdio_fd = uffd_fd; |
| 1685 | |
| 1686 | RCU_READ_LOCK_GUARD(); |
| 1687 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1688 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1689 | /* Nothing to do with read-only and MMIO-writable regions */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1690 | if (block->mr->readonly || block->mr->rom_device) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1691 | continue; |
| 1692 | } |
| 1693 | |
| 1694 | /* Register block memory with UFFD to track writes */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1695 | if (uffd_register_memory(rs->uffdio_fd, block->host, |
| 1696 | block->max_length, UFFDIO_REGISTER_MODE_WP, NULL)) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1697 | goto fail; |
| 1698 | } |
David Hildenbrand | 72ef3a3 | 2023-01-05 13:45:25 +0100 | [diff] [blame] | 1699 | block->flags |= RAM_UF_WRITEPROTECT; |
| 1700 | memory_region_ref(block->mr); |
| 1701 | |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1702 | /* Apply UFFD write protection to the block memory range */ |
David Hildenbrand | e41c577 | 2023-01-05 13:45:28 +0100 | [diff] [blame] | 1703 | if (ram_block_uffd_protect(block, uffd_fd)) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1704 | goto fail; |
| 1705 | } |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1706 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1707 | trace_ram_write_tracking_ramblock_start(block->idstr, block->page_size, |
| 1708 | block->host, block->max_length); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | return 0; |
| 1712 | |
| 1713 | fail: |
| 1714 | error_report("ram_write_tracking_start() failed: restoring initial memory state"); |
| 1715 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1716 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 1717 | if ((block->flags & RAM_UF_WRITEPROTECT) == 0) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1718 | continue; |
| 1719 | } |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1720 | uffd_unregister_memory(rs->uffdio_fd, block->host, block->max_length); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1721 | /* Cleanup flags and remove reference */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1722 | block->flags &= ~RAM_UF_WRITEPROTECT; |
| 1723 | memory_region_unref(block->mr); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | uffd_close_fd(uffd_fd); |
| 1727 | rs->uffdio_fd = -1; |
| 1728 | return -1; |
| 1729 | } |
| 1730 | |
| 1731 | /** |
| 1732 | * ram_write_tracking_stop: stop UFFD-WP memory tracking and remove protection |
| 1733 | */ |
| 1734 | void ram_write_tracking_stop(void) |
| 1735 | { |
| 1736 | RAMState *rs = ram_state; |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1737 | RAMBlock *block; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1738 | |
| 1739 | RCU_READ_LOCK_GUARD(); |
| 1740 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1741 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 1742 | if ((block->flags & RAM_UF_WRITEPROTECT) == 0) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1743 | continue; |
| 1744 | } |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1745 | uffd_unregister_memory(rs->uffdio_fd, block->host, block->max_length); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1746 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1747 | trace_ram_write_tracking_ramblock_stop(block->idstr, block->page_size, |
| 1748 | block->host, block->max_length); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1749 | |
| 1750 | /* Cleanup flags and remove reference */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1751 | block->flags &= ~RAM_UF_WRITEPROTECT; |
| 1752 | memory_region_unref(block->mr); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | /* Finally close UFFD file descriptor */ |
| 1756 | uffd_close_fd(rs->uffdio_fd); |
| 1757 | rs->uffdio_fd = -1; |
| 1758 | } |
| 1759 | |
| 1760 | #else |
| 1761 | /* No target OS support, stubs just fail or ignore */ |
| 1762 | |
| 1763 | static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) |
| 1764 | { |
| 1765 | (void) rs; |
| 1766 | (void) offset; |
| 1767 | |
| 1768 | return NULL; |
| 1769 | } |
| 1770 | |
| 1771 | static int ram_save_release_protection(RAMState *rs, PageSearchStatus *pss, |
| 1772 | unsigned long start_page) |
| 1773 | { |
| 1774 | (void) rs; |
| 1775 | (void) pss; |
| 1776 | (void) start_page; |
| 1777 | |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
| 1781 | bool ram_write_tracking_available(void) |
| 1782 | { |
| 1783 | return false; |
| 1784 | } |
| 1785 | |
| 1786 | bool ram_write_tracking_compatible(void) |
| 1787 | { |
| 1788 | assert(0); |
| 1789 | return false; |
| 1790 | } |
| 1791 | |
| 1792 | int ram_write_tracking_start(void) |
| 1793 | { |
| 1794 | assert(0); |
| 1795 | return -1; |
| 1796 | } |
| 1797 | |
| 1798 | void ram_write_tracking_stop(void) |
| 1799 | { |
| 1800 | assert(0); |
| 1801 | } |
| 1802 | #endif /* defined(__linux__) */ |
| 1803 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1804 | /** |
Li Qiang | ff1543a | 2019-05-24 23:28:32 -0700 | [diff] [blame] | 1805 | * get_queued_page: unqueue a page from the postcopy requests |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1806 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1807 | * Skips pages that are already sent (!dirty) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1808 | * |
Wei Yang | a5f7b1a | 2019-05-11 07:37:29 +0800 | [diff] [blame] | 1809 | * Returns true if a queued page is found |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1810 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1811 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1812 | * @pss: data about the state of the current dirty page scan |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1813 | */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1814 | static bool get_queued_page(RAMState *rs, PageSearchStatus *pss) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1815 | { |
| 1816 | RAMBlock *block; |
| 1817 | ram_addr_t offset; |
Thomas Huth | 777f53c | 2022-08-02 08:19:49 +0200 | [diff] [blame] | 1818 | bool dirty; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1819 | |
Thomas Huth | 777f53c | 2022-08-02 08:19:49 +0200 | [diff] [blame] | 1820 | do { |
| 1821 | block = unqueue_page(rs, &offset); |
| 1822 | /* |
| 1823 | * We're sending this page, and since it's postcopy nothing else |
| 1824 | * will dirty it, and we must make sure it doesn't get sent again |
| 1825 | * even if this queue request was received after the background |
| 1826 | * search already sent it. |
| 1827 | */ |
| 1828 | if (block) { |
| 1829 | unsigned long page; |
| 1830 | |
| 1831 | page = offset >> TARGET_PAGE_BITS; |
| 1832 | dirty = test_bit(page, block->bmap); |
| 1833 | if (!dirty) { |
| 1834 | trace_get_queued_page_not_dirty(block->idstr, (uint64_t)offset, |
| 1835 | page); |
| 1836 | } else { |
| 1837 | trace_get_queued_page(block->idstr, (uint64_t)offset, page); |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | } while (block && !dirty); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1842 | |
Peter Xu | b062106 | 2022-10-11 17:55:58 -0400 | [diff] [blame] | 1843 | if (!block) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1844 | /* |
| 1845 | * Poll write faults too if background snapshot is enabled; that's |
| 1846 | * when we have vcpus got blocked by the write protected pages. |
| 1847 | */ |
| 1848 | block = poll_fault_page(rs, &offset); |
| 1849 | } |
| 1850 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1851 | if (block) { |
| 1852 | /* |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1853 | * We want the background search to continue from the queued page |
| 1854 | * since the guest is likely to want other pages near to the page |
| 1855 | * it just requested. |
| 1856 | */ |
| 1857 | pss->block = block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1858 | pss->page = offset >> TARGET_PAGE_BITS; |
Wei Yang | 422314e | 2019-06-05 09:08:28 +0800 | [diff] [blame] | 1859 | |
| 1860 | /* |
| 1861 | * This unqueued page would break the "one round" check, even is |
| 1862 | * really rare. |
| 1863 | */ |
| 1864 | pss->complete_round = false; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | return !!block; |
| 1868 | } |
| 1869 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1870 | /** |
Juan Quintela | 5e58f96 | 2017-04-03 22:06:54 +0200 | [diff] [blame] | 1871 | * migration_page_queue_free: drop any remaining pages in the ram |
| 1872 | * request queue |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1873 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1874 | * It should be empty at the end anyway, but in error cases there may |
| 1875 | * be some left. in case that there is any page left, we drop it. |
| 1876 | * |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1877 | */ |
Juan Quintela | 83c1338 | 2017-05-04 11:45:01 +0200 | [diff] [blame] | 1878 | static void migration_page_queue_free(RAMState *rs) |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1879 | { |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1880 | struct RAMSrcPageRequest *mspr, *next_mspr; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1881 | /* This queue generally should be empty - but in the case of a failed |
| 1882 | * migration might have some droppings in. |
| 1883 | */ |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 1884 | RCU_READ_LOCK_GUARD(); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1885 | QSIMPLEQ_FOREACH_SAFE(mspr, &rs->src_page_requests, next_req, next_mspr) { |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1886 | memory_region_unref(mspr->rb->mr); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1887 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1888 | g_free(mspr); |
| 1889 | } |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1893 | * ram_save_queue_pages: queue the page for transmission |
| 1894 | * |
| 1895 | * A request from postcopy destination for example. |
| 1896 | * |
| 1897 | * Returns zero on success or negative on error |
| 1898 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1899 | * @rbname: Name of the RAMBLock of the request. NULL means the |
| 1900 | * same that last one. |
| 1901 | * @start: starting address from the start of the RAMBlock |
| 1902 | * @len: length (in bytes) to send |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1903 | */ |
Juan Quintela | 9650689 | 2017-03-14 18:41:03 +0100 | [diff] [blame] | 1904 | int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t len) |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1905 | { |
| 1906 | RAMBlock *ramblock; |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1907 | RAMState *rs = ram_state; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1908 | |
Juan Quintela | aff3f66 | 2023-04-26 19:37:19 +0200 | [diff] [blame] | 1909 | stat64_add(&mig_stats.postcopy_requests, 1); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 1910 | RCU_READ_LOCK_GUARD(); |
| 1911 | |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1912 | if (!rbname) { |
| 1913 | /* Reuse last RAMBlock */ |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 1914 | ramblock = rs->last_req_rb; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1915 | |
| 1916 | if (!ramblock) { |
| 1917 | /* |
| 1918 | * Shouldn't happen, we can't reuse the last RAMBlock if |
| 1919 | * it's the 1st request. |
| 1920 | */ |
| 1921 | error_report("ram_save_queue_pages no previous block"); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 1922 | return -1; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1923 | } |
| 1924 | } else { |
| 1925 | ramblock = qemu_ram_block_by_name(rbname); |
| 1926 | |
| 1927 | if (!ramblock) { |
| 1928 | /* We shouldn't be asked for a non-existent RAMBlock */ |
| 1929 | error_report("ram_save_queue_pages no block '%s'", rbname); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 1930 | return -1; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1931 | } |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 1932 | rs->last_req_rb = ramblock; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1933 | } |
| 1934 | trace_ram_save_queue_pages(ramblock->idstr, start, len); |
David Hildenbrand | 542147f | 2021-04-29 13:27:08 +0200 | [diff] [blame] | 1935 | if (!offset_in_ramblock(ramblock, start + len - 1)) { |
Juan Quintela | 9458ad6 | 2015-11-10 17:42:05 +0100 | [diff] [blame] | 1936 | error_report("%s request overrun start=" RAM_ADDR_FMT " len=" |
| 1937 | RAM_ADDR_FMT " blocklen=" RAM_ADDR_FMT, |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1938 | __func__, start, len, ramblock->used_length); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 1939 | return -1; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
Peter Xu | 9358982 | 2022-10-11 17:55:57 -0400 | [diff] [blame] | 1942 | /* |
| 1943 | * When with postcopy preempt, we send back the page directly in the |
| 1944 | * rp-return thread. |
| 1945 | */ |
| 1946 | if (postcopy_preempt_active()) { |
| 1947 | ram_addr_t page_start = start >> TARGET_PAGE_BITS; |
| 1948 | size_t page_size = qemu_ram_pagesize(ramblock); |
| 1949 | PageSearchStatus *pss = &ram_state->pss[RAM_CHANNEL_POSTCOPY]; |
| 1950 | int ret = 0; |
| 1951 | |
| 1952 | qemu_mutex_lock(&rs->bitmap_mutex); |
| 1953 | |
| 1954 | pss_init(pss, ramblock, page_start); |
| 1955 | /* |
| 1956 | * Always use the preempt channel, and make sure it's there. It's |
| 1957 | * safe to access without lock, because when rp-thread is running |
| 1958 | * we should be the only one who operates on the qemufile |
| 1959 | */ |
| 1960 | pss->pss_channel = migrate_get_current()->postcopy_qemufile_src; |
Peter Xu | 9358982 | 2022-10-11 17:55:57 -0400 | [diff] [blame] | 1961 | assert(pss->pss_channel); |
| 1962 | |
| 1963 | /* |
| 1964 | * It must be either one or multiple of host page size. Just |
| 1965 | * assert; if something wrong we're mostly split brain anyway. |
| 1966 | */ |
| 1967 | assert(len % page_size == 0); |
| 1968 | while (len) { |
| 1969 | if (ram_save_host_page_urgent(pss)) { |
| 1970 | error_report("%s: ram_save_host_page_urgent() failed: " |
| 1971 | "ramblock=%s, start_addr=0x"RAM_ADDR_FMT, |
| 1972 | __func__, ramblock->idstr, start); |
| 1973 | ret = -1; |
| 1974 | break; |
| 1975 | } |
| 1976 | /* |
| 1977 | * NOTE: after ram_save_host_page_urgent() succeeded, pss->page |
| 1978 | * will automatically be moved and point to the next host page |
| 1979 | * we're going to send, so no need to update here. |
| 1980 | * |
| 1981 | * Normally QEMU never sends >1 host page in requests, so |
| 1982 | * logically we don't even need that as the loop should only |
| 1983 | * run once, but just to be consistent. |
| 1984 | */ |
| 1985 | len -= page_size; |
| 1986 | }; |
| 1987 | qemu_mutex_unlock(&rs->bitmap_mutex); |
| 1988 | |
| 1989 | return ret; |
| 1990 | } |
| 1991 | |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1992 | struct RAMSrcPageRequest *new_entry = |
Markus Armbruster | b21e238 | 2022-03-15 15:41:56 +0100 | [diff] [blame] | 1993 | g_new0(struct RAMSrcPageRequest, 1); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1994 | new_entry->rb = ramblock; |
| 1995 | new_entry->offset = start; |
| 1996 | new_entry->len = len; |
| 1997 | |
| 1998 | memory_region_ref(ramblock->mr); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1999 | qemu_mutex_lock(&rs->src_page_req_mutex); |
| 2000 | QSIMPLEQ_INSERT_TAIL(&rs->src_page_requests, new_entry, next_req); |
Dr. David Alan Gilbert | e03a34f | 2018-06-13 11:26:42 +0100 | [diff] [blame] | 2001 | migration_make_urgent_request(); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 2002 | qemu_mutex_unlock(&rs->src_page_req_mutex); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2003 | |
| 2004 | return 0; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 2007 | /* |
| 2008 | * try to compress the page before posting it out, return true if the page |
| 2009 | * has been properly handled by compression, otherwise needs other |
| 2010 | * paths to handle it |
| 2011 | */ |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 2012 | static bool save_compress_page(RAMState *rs, PageSearchStatus *pss, |
Juan Quintela | d637a18 | 2023-06-13 13:34:12 +0200 | [diff] [blame] | 2013 | ram_addr_t offset) |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 2014 | { |
Juan Quintela | 4e400f9 | 2023-10-19 13:07:16 +0200 | [diff] [blame] | 2015 | if (!migrate_compress()) { |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 2016 | return false; |
| 2017 | } |
| 2018 | |
| 2019 | /* |
| 2020 | * When starting the process of a new block, the first page of |
| 2021 | * the block should be sent out before other pages in the same |
| 2022 | * block, and all the pages in last block should have been sent |
| 2023 | * out, keeping this order is important, because the 'cont' flag |
| 2024 | * is used to avoid resending the block name. |
| 2025 | * |
| 2026 | * We post the fist page as normal page as compression will take |
| 2027 | * much CPU resource. |
| 2028 | */ |
Juan Quintela | d637a18 | 2023-06-13 13:34:12 +0200 | [diff] [blame] | 2029 | if (pss->block != pss->last_sent_block) { |
Juan Quintela | 8020bc9 | 2023-10-19 13:07:22 +0200 | [diff] [blame] | 2030 | compress_flush_data(); |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 2031 | return false; |
| 2032 | } |
| 2033 | |
Juan Quintela | 250b1d7 | 2023-10-19 13:07:19 +0200 | [diff] [blame] | 2034 | return compress_page_with_multi_thread(pss->block, offset, |
Juan Quintela | 742ec5f | 2023-10-19 13:07:21 +0200 | [diff] [blame] | 2035 | compress_send_queued_data); |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 2036 | } |
| 2037 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2038 | /** |
Juan Quintela | 4010ba3 | 2021-12-15 20:10:39 +0100 | [diff] [blame] | 2039 | * ram_save_target_page_legacy: save one target page |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2040 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2041 | * Returns the number of pages written |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2042 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2043 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2044 | * @pss: data about the page we want to send |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2045 | */ |
Juan Quintela | 4010ba3 | 2021-12-15 20:10:39 +0100 | [diff] [blame] | 2046 | static int ram_save_target_page_legacy(RAMState *rs, PageSearchStatus *pss) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2047 | { |
Xiao Guangrong | a8ec91f | 2018-03-30 15:51:25 +0800 | [diff] [blame] | 2048 | RAMBlock *block = pss->block; |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 2049 | ram_addr_t offset = ((ram_addr_t)pss->page) << TARGET_PAGE_BITS; |
Xiao Guangrong | a8ec91f | 2018-03-30 15:51:25 +0800 | [diff] [blame] | 2050 | int res; |
| 2051 | |
Juan Quintela | 944853c | 2023-06-13 13:36:24 +0200 | [diff] [blame] | 2052 | if (control_save_page(pss, offset, &res)) { |
Xiao Guangrong | a8ec91f | 2018-03-30 15:51:25 +0800 | [diff] [blame] | 2053 | return res; |
| 2054 | } |
| 2055 | |
Juan Quintela | d637a18 | 2023-06-13 13:34:12 +0200 | [diff] [blame] | 2056 | if (save_compress_page(rs, pss, offset)) { |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 2057 | return 1; |
Xiao Guangrong | d7400a3 | 2018-03-30 15:51:26 +0800 | [diff] [blame] | 2058 | } |
| 2059 | |
Juan Quintela | e8e4e7a | 2023-06-13 13:38:30 +0200 | [diff] [blame] | 2060 | if (save_zero_page(rs, pss, offset)) { |
Fabiano Rosas | 8697eb8 | 2023-10-11 15:46:03 -0300 | [diff] [blame] | 2061 | return 1; |
Xiao Guangrong | d7400a3 | 2018-03-30 15:51:26 +0800 | [diff] [blame] | 2062 | } |
| 2063 | |
Xiao Guangrong | da3f56c | 2018-03-30 15:51:28 +0800 | [diff] [blame] | 2064 | /* |
Peter Xu | 6f39c90 | 2022-10-04 14:24:30 -0400 | [diff] [blame] | 2065 | * Do not use multifd in postcopy as one whole host page should be |
| 2066 | * placed. Meanwhile postcopy requires atomic update of pages, so even |
| 2067 | * if host page size == guest page size the dest guest during run may |
| 2068 | * still see partially copied pages which is data corruption. |
Xiao Guangrong | da3f56c | 2018-03-30 15:51:28 +0800 | [diff] [blame] | 2069 | */ |
Juan Quintela | 51b0754 | 2023-03-01 22:10:29 +0100 | [diff] [blame] | 2070 | if (migrate_multifd() && !migration_in_postcopy()) { |
Peter Xu | 61717ea | 2022-10-11 17:55:53 -0400 | [diff] [blame] | 2071 | return ram_save_multifd_page(pss->pss_channel, block, offset); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 2074 | return ram_save_page(rs, pss); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 2077 | /* Should be called before sending a host page */ |
| 2078 | static void pss_host_page_prepare(PageSearchStatus *pss) |
| 2079 | { |
| 2080 | /* How many guest pages are there in one host page? */ |
| 2081 | size_t guest_pfns = qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS; |
| 2082 | |
| 2083 | pss->host_page_sending = true; |
Peter Xu | 301d7ff | 2023-01-20 11:31:47 -0500 | [diff] [blame] | 2084 | if (guest_pfns <= 1) { |
| 2085 | /* |
| 2086 | * This covers both when guest psize == host psize, or when guest |
| 2087 | * has larger psize than the host (guest_pfns==0). |
| 2088 | * |
| 2089 | * For the latter, we always send one whole guest page per |
| 2090 | * iteration of the host page (example: an Alpha VM on x86 host |
| 2091 | * will have guest psize 8K while host psize 4K). |
| 2092 | */ |
| 2093 | pss->host_page_start = pss->page; |
| 2094 | pss->host_page_end = pss->page + 1; |
| 2095 | } else { |
| 2096 | /* |
| 2097 | * The host page spans over multiple guest pages, we send them |
| 2098 | * within the same host page iteration. |
| 2099 | */ |
| 2100 | pss->host_page_start = ROUND_DOWN(pss->page, guest_pfns); |
| 2101 | pss->host_page_end = ROUND_UP(pss->page + 1, guest_pfns); |
| 2102 | } |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | /* |
| 2106 | * Whether the page pointed by PSS is within the host page being sent. |
| 2107 | * Must be called after a previous pss_host_page_prepare(). |
| 2108 | */ |
| 2109 | static bool pss_within_range(PageSearchStatus *pss) |
| 2110 | { |
| 2111 | ram_addr_t ram_addr; |
| 2112 | |
| 2113 | assert(pss->host_page_sending); |
| 2114 | |
| 2115 | /* Over host-page boundary? */ |
| 2116 | if (pss->page >= pss->host_page_end) { |
| 2117 | return false; |
| 2118 | } |
| 2119 | |
| 2120 | ram_addr = ((ram_addr_t)pss->page) << TARGET_PAGE_BITS; |
| 2121 | |
| 2122 | return offset_in_ramblock(pss->block, ram_addr); |
| 2123 | } |
| 2124 | |
| 2125 | static void pss_host_page_finish(PageSearchStatus *pss) |
| 2126 | { |
| 2127 | pss->host_page_sending = false; |
| 2128 | /* This is not needed, but just to reset it */ |
| 2129 | pss->host_page_start = pss->host_page_end = 0; |
| 2130 | } |
| 2131 | |
Peter Xu | 9358982 | 2022-10-11 17:55:57 -0400 | [diff] [blame] | 2132 | /* |
| 2133 | * Send an urgent host page specified by `pss'. Need to be called with |
| 2134 | * bitmap_mutex held. |
| 2135 | * |
| 2136 | * Returns 0 if save host page succeeded, false otherwise. |
| 2137 | */ |
| 2138 | static int ram_save_host_page_urgent(PageSearchStatus *pss) |
| 2139 | { |
| 2140 | bool page_dirty, sent = false; |
| 2141 | RAMState *rs = ram_state; |
| 2142 | int ret = 0; |
| 2143 | |
| 2144 | trace_postcopy_preempt_send_host_page(pss->block->idstr, pss->page); |
| 2145 | pss_host_page_prepare(pss); |
| 2146 | |
| 2147 | /* |
| 2148 | * If precopy is sending the same page, let it be done in precopy, or |
| 2149 | * we could send the same page in two channels and none of them will |
| 2150 | * receive the whole page. |
| 2151 | */ |
| 2152 | if (pss_overlap(pss, &ram_state->pss[RAM_CHANNEL_PRECOPY])) { |
| 2153 | trace_postcopy_preempt_hit(pss->block->idstr, |
| 2154 | pss->page << TARGET_PAGE_BITS); |
| 2155 | return 0; |
| 2156 | } |
| 2157 | |
| 2158 | do { |
| 2159 | page_dirty = migration_bitmap_clear_dirty(rs, pss->block, pss->page); |
| 2160 | |
| 2161 | if (page_dirty) { |
| 2162 | /* Be strict to return code; it must be 1, or what else? */ |
Juan Quintela | 4010ba3 | 2021-12-15 20:10:39 +0100 | [diff] [blame] | 2163 | if (migration_ops->ram_save_target_page(rs, pss) != 1) { |
Peter Xu | 9358982 | 2022-10-11 17:55:57 -0400 | [diff] [blame] | 2164 | error_report_once("%s: ram_save_target_page failed", __func__); |
| 2165 | ret = -1; |
| 2166 | goto out; |
| 2167 | } |
| 2168 | sent = true; |
| 2169 | } |
| 2170 | pss_find_next_dirty(pss); |
| 2171 | } while (pss_within_range(pss)); |
| 2172 | out: |
| 2173 | pss_host_page_finish(pss); |
| 2174 | /* For urgent requests, flush immediately if sent */ |
| 2175 | if (sent) { |
| 2176 | qemu_fflush(pss->pss_channel); |
| 2177 | } |
| 2178 | return ret; |
| 2179 | } |
| 2180 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2181 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2182 | * ram_save_host_page: save a whole host page |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2183 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2184 | * Starting at *offset send pages up to the end of the current host |
| 2185 | * page. It's valid for the initial offset to point into the middle of |
| 2186 | * a host page in which case the remainder of the hostpage is sent. |
| 2187 | * Only dirty target pages are sent. Note that the host page size may |
| 2188 | * be a huge page for this block. |
Peter Xu | f332155 | 2022-10-11 17:55:50 -0400 | [diff] [blame] | 2189 | * |
Dr. David Alan Gilbert | 1eb3fc0 | 2017-05-17 17:58:09 +0100 | [diff] [blame] | 2190 | * The saving stops at the boundary of the used_length of the block |
| 2191 | * if the RAMBlock isn't a multiple of the host page size. |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2192 | * |
Peter Xu | f332155 | 2022-10-11 17:55:50 -0400 | [diff] [blame] | 2193 | * The caller must be with ram_state.bitmap_mutex held to call this |
| 2194 | * function. Note that this function can temporarily release the lock, but |
| 2195 | * when the function is returned it'll make sure the lock is still held. |
| 2196 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2197 | * Returns the number of pages written or negative on error |
| 2198 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2199 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2200 | * @pss: data about the page we want to send |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2201 | */ |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 2202 | static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2203 | { |
Peter Xu | f332155 | 2022-10-11 17:55:50 -0400 | [diff] [blame] | 2204 | bool page_dirty, preempt_active = postcopy_preempt_active(); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2205 | int tmppages, pages = 0; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 2206 | size_t pagesize_bits = |
| 2207 | qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2208 | unsigned long start_page = pss->page; |
| 2209 | int res; |
Dr. David Alan Gilbert | 4c011c3 | 2017-02-24 18:28:39 +0000 | [diff] [blame] | 2210 | |
David Hildenbrand | f161c88 | 2023-07-06 09:56:08 +0200 | [diff] [blame] | 2211 | if (migrate_ram_is_ignored(pss->block)) { |
Cédric Le Goater | b895de5 | 2018-05-14 08:57:00 +0200 | [diff] [blame] | 2212 | error_report("block %s should not be migrated !", pss->block->idstr); |
| 2213 | return 0; |
| 2214 | } |
| 2215 | |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 2216 | /* Update host page boundary information */ |
| 2217 | pss_host_page_prepare(pss); |
| 2218 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2219 | do { |
Peter Xu | f332155 | 2022-10-11 17:55:50 -0400 | [diff] [blame] | 2220 | page_dirty = migration_bitmap_clear_dirty(rs, pss->block, pss->page); |
Xiao Guangrong | 1faa566 | 2018-03-30 15:51:24 +0800 | [diff] [blame] | 2221 | |
Peter Xu | f332155 | 2022-10-11 17:55:50 -0400 | [diff] [blame] | 2222 | /* Check the pages is dirty and if it is send it */ |
| 2223 | if (page_dirty) { |
Kunkun Jiang | ba1b7c8 | 2021-03-16 20:57:16 +0800 | [diff] [blame] | 2224 | /* |
Peter Xu | f332155 | 2022-10-11 17:55:50 -0400 | [diff] [blame] | 2225 | * Properly yield the lock only in postcopy preempt mode |
| 2226 | * because both migration thread and rp-return thread can |
| 2227 | * operate on the bitmaps. |
Kunkun Jiang | ba1b7c8 | 2021-03-16 20:57:16 +0800 | [diff] [blame] | 2228 | */ |
Peter Xu | f332155 | 2022-10-11 17:55:50 -0400 | [diff] [blame] | 2229 | if (preempt_active) { |
| 2230 | qemu_mutex_unlock(&rs->bitmap_mutex); |
Kunkun Jiang | ba1b7c8 | 2021-03-16 20:57:16 +0800 | [diff] [blame] | 2231 | } |
Juan Quintela | 4010ba3 | 2021-12-15 20:10:39 +0100 | [diff] [blame] | 2232 | tmppages = migration_ops->ram_save_target_page(rs, pss); |
Peter Xu | f332155 | 2022-10-11 17:55:50 -0400 | [diff] [blame] | 2233 | if (tmppages >= 0) { |
| 2234 | pages += tmppages; |
| 2235 | /* |
| 2236 | * Allow rate limiting to happen in the middle of huge pages if |
| 2237 | * something is sent in the current iteration. |
| 2238 | */ |
| 2239 | if (pagesize_bits > 1 && tmppages > 0) { |
| 2240 | migration_rate_limit(); |
| 2241 | } |
| 2242 | } |
| 2243 | if (preempt_active) { |
| 2244 | qemu_mutex_lock(&rs->bitmap_mutex); |
| 2245 | } |
| 2246 | } else { |
| 2247 | tmppages = 0; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2248 | } |
Peter Xu | f332155 | 2022-10-11 17:55:50 -0400 | [diff] [blame] | 2249 | |
| 2250 | if (tmppages < 0) { |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 2251 | pss_host_page_finish(pss); |
Peter Xu | f332155 | 2022-10-11 17:55:50 -0400 | [diff] [blame] | 2252 | return tmppages; |
| 2253 | } |
| 2254 | |
Peter Xu | d9e474e | 2022-10-11 17:55:52 -0400 | [diff] [blame] | 2255 | pss_find_next_dirty(pss); |
| 2256 | } while (pss_within_range(pss)); |
| 2257 | |
| 2258 | pss_host_page_finish(pss); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2259 | |
| 2260 | res = ram_save_release_protection(rs, pss, start_page); |
| 2261 | return (res < 0 ? res : pages); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2262 | } |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2263 | |
| 2264 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2265 | * ram_find_and_save_block: finds a dirty page and sends it to f |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2266 | * |
| 2267 | * Called within an RCU critical section. |
| 2268 | * |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 2269 | * Returns the number of pages written where zero means no dirty pages, |
| 2270 | * or negative on error |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2271 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2272 | * @rs: current RAM state |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2273 | * |
| 2274 | * On systems where host-page-size > target-page-size it will send all the |
| 2275 | * pages in a host page that are dirty. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2276 | */ |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 2277 | static int ram_find_and_save_block(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2278 | { |
Peter Xu | f166876 | 2022-10-11 17:55:55 -0400 | [diff] [blame] | 2279 | PageSearchStatus *pss = &rs->pss[RAM_CHANNEL_PRECOPY]; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2280 | int pages = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2281 | |
Ashijeet Acharya | 0827b9e | 2017-02-08 19:58:45 +0530 | [diff] [blame] | 2282 | /* No dirty page as there is zero RAM */ |
Juan Quintela | 8d80e19 | 2022-05-10 19:37:36 +0200 | [diff] [blame] | 2283 | if (!rs->ram_bytes_total) { |
Ashijeet Acharya | 0827b9e | 2017-02-08 19:58:45 +0530 | [diff] [blame] | 2284 | return pages; |
| 2285 | } |
| 2286 | |
Peter Xu | 4934a5d | 2022-10-04 14:24:26 -0400 | [diff] [blame] | 2287 | /* |
| 2288 | * Always keep last_seen_block/last_page valid during this procedure, |
| 2289 | * because find_dirty_block() relies on these values (e.g., we compare |
| 2290 | * last_seen_block with pss.block to see whether we searched all the |
| 2291 | * ramblocks) to detect the completion of migration. Having NULL value |
| 2292 | * of last_seen_block can conditionally cause below loop to run forever. |
| 2293 | */ |
| 2294 | if (!rs->last_seen_block) { |
| 2295 | rs->last_seen_block = QLIST_FIRST_RCU(&ram_list.blocks); |
| 2296 | rs->last_page = 0; |
| 2297 | } |
| 2298 | |
Peter Xu | f166876 | 2022-10-11 17:55:55 -0400 | [diff] [blame] | 2299 | pss_init(pss, rs->last_seen_block, rs->last_page); |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 2300 | |
Juan Quintela | 31e2ac7 | 2022-06-21 13:29:36 +0200 | [diff] [blame] | 2301 | while (true){ |
Juan Quintela | 51efd36 | 2022-06-21 13:20:35 +0200 | [diff] [blame] | 2302 | if (!get_queued_page(rs, pss)) { |
Peter Xu | b062106 | 2022-10-11 17:55:58 -0400 | [diff] [blame] | 2303 | /* priority queue empty, so just search for something dirty */ |
Juan Quintela | 31e2ac7 | 2022-06-21 13:29:36 +0200 | [diff] [blame] | 2304 | int res = find_dirty_block(rs, pss); |
| 2305 | if (res != PAGE_DIRTY_FOUND) { |
| 2306 | if (res == PAGE_ALL_CLEAN) { |
Juan Quintela | 51efd36 | 2022-06-21 13:20:35 +0200 | [diff] [blame] | 2307 | break; |
Juan Quintela | 31e2ac7 | 2022-06-21 13:29:36 +0200 | [diff] [blame] | 2308 | } else if (res == PAGE_TRY_AGAIN) { |
| 2309 | continue; |
Juan Quintela | 294e5a4 | 2022-06-21 13:36:11 +0200 | [diff] [blame] | 2310 | } else if (res < 0) { |
| 2311 | pages = res; |
| 2312 | break; |
Juan Quintela | 51efd36 | 2022-06-21 13:20:35 +0200 | [diff] [blame] | 2313 | } |
| 2314 | } |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2315 | } |
Juan Quintela | 51efd36 | 2022-06-21 13:20:35 +0200 | [diff] [blame] | 2316 | pages = ram_save_host_page(rs, pss); |
Juan Quintela | 31e2ac7 | 2022-06-21 13:29:36 +0200 | [diff] [blame] | 2317 | if (pages) { |
| 2318 | break; |
| 2319 | } |
| 2320 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2321 | |
Peter Xu | f166876 | 2022-10-11 17:55:55 -0400 | [diff] [blame] | 2322 | rs->last_seen_block = pss->block; |
| 2323 | rs->last_page = pss->page; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2324 | |
| 2325 | return pages; |
| 2326 | } |
| 2327 | |
Juan Quintela | 8008a27 | 2022-05-10 19:18:19 +0200 | [diff] [blame] | 2328 | static uint64_t ram_bytes_total_with_ignored(void) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2329 | { |
| 2330 | RAMBlock *block; |
| 2331 | uint64_t total = 0; |
| 2332 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2333 | RCU_READ_LOCK_GUARD(); |
| 2334 | |
Juan Quintela | 8008a27 | 2022-05-10 19:18:19 +0200 | [diff] [blame] | 2335 | RAMBLOCK_FOREACH_MIGRATABLE(block) { |
| 2336 | total += block->used_length; |
Peter Xu | 99e1558 | 2017-05-12 12:17:39 +0800 | [diff] [blame] | 2337 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2338 | return total; |
| 2339 | } |
| 2340 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2341 | uint64_t ram_bytes_total(void) |
| 2342 | { |
Juan Quintela | 8008a27 | 2022-05-10 19:18:19 +0200 | [diff] [blame] | 2343 | RAMBlock *block; |
| 2344 | uint64_t total = 0; |
| 2345 | |
| 2346 | RCU_READ_LOCK_GUARD(); |
| 2347 | |
| 2348 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 2349 | total += block->used_length; |
| 2350 | } |
| 2351 | return total; |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2352 | } |
| 2353 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 2354 | static void xbzrle_load_setup(void) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2355 | { |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 2356 | XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2357 | } |
| 2358 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 2359 | static void xbzrle_load_cleanup(void) |
| 2360 | { |
| 2361 | g_free(XBZRLE.decoded_buf); |
| 2362 | XBZRLE.decoded_buf = NULL; |
| 2363 | } |
| 2364 | |
Peter Xu | 7d7c96b | 2017-10-19 14:31:58 +0800 | [diff] [blame] | 2365 | static void ram_state_cleanup(RAMState **rsp) |
| 2366 | { |
Dr. David Alan Gilbert | b9ccaf6 | 2018-02-12 16:03:39 +0000 | [diff] [blame] | 2367 | if (*rsp) { |
| 2368 | migration_page_queue_free(*rsp); |
| 2369 | qemu_mutex_destroy(&(*rsp)->bitmap_mutex); |
| 2370 | qemu_mutex_destroy(&(*rsp)->src_page_req_mutex); |
| 2371 | g_free(*rsp); |
| 2372 | *rsp = NULL; |
| 2373 | } |
Peter Xu | 7d7c96b | 2017-10-19 14:31:58 +0800 | [diff] [blame] | 2374 | } |
| 2375 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 2376 | static void xbzrle_cleanup(void) |
| 2377 | { |
| 2378 | XBZRLE_cache_lock(); |
| 2379 | if (XBZRLE.cache) { |
| 2380 | cache_fini(XBZRLE.cache); |
| 2381 | g_free(XBZRLE.encoded_buf); |
| 2382 | g_free(XBZRLE.current_buf); |
| 2383 | g_free(XBZRLE.zero_target_page); |
| 2384 | XBZRLE.cache = NULL; |
| 2385 | XBZRLE.encoded_buf = NULL; |
| 2386 | XBZRLE.current_buf = NULL; |
| 2387 | XBZRLE.zero_target_page = NULL; |
| 2388 | } |
| 2389 | XBZRLE_cache_unlock(); |
| 2390 | } |
| 2391 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 2392 | static void ram_save_cleanup(void *opaque) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2393 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2394 | RAMState **rsp = opaque; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2395 | RAMBlock *block; |
Juan Quintela | eb859c5 | 2017-03-13 21:51:55 +0100 | [diff] [blame] | 2396 | |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2397 | /* We don't use dirty log with background snapshots */ |
| 2398 | if (!migrate_background_snapshot()) { |
| 2399 | /* caller have hold iothread lock or is in a bh, so there is |
| 2400 | * no writing race against the migration bitmap |
| 2401 | */ |
Hyman Huang(黄勇) | 63b41db | 2021-06-29 16:01:19 +0000 | [diff] [blame] | 2402 | if (global_dirty_tracking & GLOBAL_DIRTY_MIGRATION) { |
| 2403 | /* |
| 2404 | * do not stop dirty log without starting it, since |
| 2405 | * memory_global_dirty_log_stop will assert that |
| 2406 | * memory_global_dirty_log_start/stop used in pairs |
| 2407 | */ |
| 2408 | memory_global_dirty_log_stop(GLOBAL_DIRTY_MIGRATION); |
| 2409 | } |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2410 | } |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2411 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2412 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2413 | g_free(block->clear_bmap); |
| 2414 | block->clear_bmap = NULL; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2415 | g_free(block->bmap); |
| 2416 | block->bmap = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 2419 | xbzrle_cleanup(); |
Juan Quintela | f0afa33 | 2017-06-28 11:52:28 +0200 | [diff] [blame] | 2420 | compress_threads_save_cleanup(); |
Peter Xu | 7d7c96b | 2017-10-19 14:31:58 +0800 | [diff] [blame] | 2421 | ram_state_cleanup(rsp); |
Juan Quintela | 4010ba3 | 2021-12-15 20:10:39 +0100 | [diff] [blame] | 2422 | g_free(migration_ops); |
| 2423 | migration_ops = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2424 | } |
| 2425 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2426 | static void ram_state_reset(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2427 | { |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 2428 | int i; |
| 2429 | |
| 2430 | for (i = 0; i < RAM_CHANNEL_MAX; i++) { |
| 2431 | rs->pss[i].last_sent_block = NULL; |
| 2432 | } |
| 2433 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2434 | rs->last_seen_block = NULL; |
Juan Quintela | 269ace2 | 2017-03-21 15:23:31 +0100 | [diff] [blame] | 2435 | rs->last_page = 0; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2436 | rs->last_version = ram_list.version; |
Juan Quintela | f3095cc | 2023-05-04 13:53:23 +0200 | [diff] [blame] | 2437 | rs->xbzrle_started = false; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | #define MAX_WAIT 50 /* ms, half buffered_file limit */ |
| 2441 | |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2442 | /* **** functions for postcopy ***** */ |
| 2443 | |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 2444 | void ram_postcopy_migrated_memory_release(MigrationState *ms) |
| 2445 | { |
| 2446 | struct RAMBlock *block; |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 2447 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2448 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2449 | unsigned long *bitmap = block->bmap; |
| 2450 | unsigned long range = block->used_length >> TARGET_PAGE_BITS; |
| 2451 | unsigned long run_start = find_next_zero_bit(bitmap, range, 0); |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 2452 | |
| 2453 | while (run_start < range) { |
| 2454 | unsigned long run_end = find_next_bit(bitmap, range, run_start + 1); |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 2455 | ram_discard_range(block->idstr, |
| 2456 | ((ram_addr_t)run_start) << TARGET_PAGE_BITS, |
| 2457 | ((ram_addr_t)(run_end - run_start)) |
| 2458 | << TARGET_PAGE_BITS); |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 2459 | run_start = find_next_zero_bit(bitmap, range, run_end + 1); |
| 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2464 | /** |
| 2465 | * postcopy_send_discard_bm_ram: discard a RAMBlock |
| 2466 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2467 | * Callback from postcopy_each_ram_send_discard for each RAMBlock |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2468 | * |
| 2469 | * @ms: current migration state |
Wei Yang | 89dab31 | 2019-07-15 10:05:49 +0800 | [diff] [blame] | 2470 | * @block: RAMBlock to discard |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2471 | */ |
Philippe Mathieu-Daudé | 9e7d122 | 2021-12-30 17:05:25 +0100 | [diff] [blame] | 2472 | static void postcopy_send_discard_bm_ram(MigrationState *ms, RAMBlock *block) |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2473 | { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2474 | unsigned long end = block->used_length >> TARGET_PAGE_BITS; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2475 | unsigned long current; |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2476 | unsigned long *bitmap = block->bmap; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2477 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2478 | for (current = 0; current < end; ) { |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2479 | unsigned long one = find_next_bit(bitmap, end, current); |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 2480 | unsigned long zero, discard_length; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2481 | |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 2482 | if (one >= end) { |
| 2483 | break; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2484 | } |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 2485 | |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2486 | zero = find_next_zero_bit(bitmap, end, one + 1); |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 2487 | |
| 2488 | if (zero >= end) { |
| 2489 | discard_length = end - one; |
| 2490 | } else { |
| 2491 | discard_length = zero - one; |
| 2492 | } |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 2493 | postcopy_discard_send_range(ms, one, discard_length); |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 2494 | current = one + discard_length; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2495 | } |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Peter Xu | f30c2e5 | 2021-12-07 19:50:13 +0800 | [diff] [blame] | 2498 | static void postcopy_chunk_hostpages_pass(MigrationState *ms, RAMBlock *block); |
| 2499 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2500 | /** |
| 2501 | * postcopy_each_ram_send_discard: discard all RAMBlocks |
| 2502 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2503 | * Utility for the outgoing postcopy code. |
| 2504 | * Calls postcopy_send_discard_bm_ram for each RAMBlock |
| 2505 | * passing it bitmap indexes and name. |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2506 | * (qemu_ram_foreach_block ends up passing unscaled lengths |
| 2507 | * which would mean postcopy code would have to deal with target page) |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2508 | * |
| 2509 | * @ms: current migration state |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2510 | */ |
Peter Xu | 739fcc1 | 2021-12-07 19:50:14 +0800 | [diff] [blame] | 2511 | static void postcopy_each_ram_send_discard(MigrationState *ms) |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2512 | { |
| 2513 | struct RAMBlock *block; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2514 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2515 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 2516 | postcopy_discard_send_init(ms, block->idstr); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2517 | |
| 2518 | /* |
Peter Xu | f30c2e5 | 2021-12-07 19:50:13 +0800 | [diff] [blame] | 2519 | * Deal with TPS != HPS and huge pages. It discard any partially sent |
| 2520 | * host-page size chunks, mark any partially dirty host-page size |
| 2521 | * chunks as all dirty. In this case the host-page is the host-page |
| 2522 | * for the particular RAMBlock, i.e. it might be a huge page. |
| 2523 | */ |
| 2524 | postcopy_chunk_hostpages_pass(ms, block); |
| 2525 | |
| 2526 | /* |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2527 | * Postcopy sends chunks of bitmap over the wire, but it |
| 2528 | * just needs indexes at this point, avoids it having |
| 2529 | * target page specific code. |
| 2530 | */ |
Peter Xu | 739fcc1 | 2021-12-07 19:50:14 +0800 | [diff] [blame] | 2531 | postcopy_send_discard_bm_ram(ms, block); |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 2532 | postcopy_discard_send_finish(ms); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2533 | } |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2536 | /** |
Wei Yang | 8324ef8 | 2019-08-19 14:18:41 +0800 | [diff] [blame] | 2537 | * postcopy_chunk_hostpages_pass: canonicalize bitmap in hostpages |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2538 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2539 | * Helper for postcopy_chunk_hostpages; it's called twice to |
| 2540 | * canonicalize the two bitmaps, that are similar, but one is |
| 2541 | * inverted. |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2542 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2543 | * Postcopy requires that all target pages in a hostpage are dirty or |
| 2544 | * clean, not a mix. This function canonicalizes the bitmaps. |
| 2545 | * |
| 2546 | * @ms: current migration state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2547 | * @block: block that contains the page we want to canonicalize |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2548 | */ |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2549 | static void postcopy_chunk_hostpages_pass(MigrationState *ms, RAMBlock *block) |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2550 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2551 | RAMState *rs = ram_state; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2552 | unsigned long *bitmap = block->bmap; |
Dr. David Alan Gilbert | 29c5917 | 2017-02-24 18:28:31 +0000 | [diff] [blame] | 2553 | unsigned int host_ratio = block->page_size / TARGET_PAGE_SIZE; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2554 | unsigned long pages = block->used_length >> TARGET_PAGE_BITS; |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2555 | unsigned long run_start; |
| 2556 | |
Dr. David Alan Gilbert | 29c5917 | 2017-02-24 18:28:31 +0000 | [diff] [blame] | 2557 | if (block->page_size == TARGET_PAGE_SIZE) { |
| 2558 | /* Easy case - TPS==HPS for a non-huge page RAMBlock */ |
| 2559 | return; |
| 2560 | } |
| 2561 | |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2562 | /* Find a dirty page */ |
| 2563 | run_start = find_next_bit(bitmap, pages, 0); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2564 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2565 | while (run_start < pages) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2566 | |
| 2567 | /* |
| 2568 | * If the start of this run of pages is in the middle of a host |
| 2569 | * page, then we need to fixup this host page. |
| 2570 | */ |
Wei Yang | 9dec3cc | 2019-08-06 08:46:48 +0800 | [diff] [blame] | 2571 | if (QEMU_IS_ALIGNED(run_start, host_ratio)) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2572 | /* Find the end of this run */ |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2573 | run_start = find_next_zero_bit(bitmap, pages, run_start + 1); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2574 | /* |
| 2575 | * If the end isn't at the start of a host page, then the |
| 2576 | * run doesn't finish at the end of a host page |
| 2577 | * and we need to discard. |
| 2578 | */ |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Wei Yang | 9dec3cc | 2019-08-06 08:46:48 +0800 | [diff] [blame] | 2581 | if (!QEMU_IS_ALIGNED(run_start, host_ratio)) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2582 | unsigned long page; |
Wei Yang | dad45ab | 2019-08-06 08:46:47 +0800 | [diff] [blame] | 2583 | unsigned long fixup_start_addr = QEMU_ALIGN_DOWN(run_start, |
| 2584 | host_ratio); |
| 2585 | run_start = QEMU_ALIGN_UP(run_start, host_ratio); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2586 | |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2587 | /* Clean up the bitmap */ |
| 2588 | for (page = fixup_start_addr; |
| 2589 | page < fixup_start_addr + host_ratio; page++) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2590 | /* |
| 2591 | * Remark them as dirty, updating the count for any pages |
| 2592 | * that weren't previously dirty. |
| 2593 | */ |
Juan Quintela | 0d8ec88 | 2017-03-13 21:21:41 +0100 | [diff] [blame] | 2594 | rs->migration_dirty_pages += !test_and_set_bit(page, bitmap); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2595 | } |
| 2596 | } |
| 2597 | |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2598 | /* Find the next dirty page for the next iteration */ |
| 2599 | run_start = find_next_bit(bitmap, pages, run_start); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2603 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2604 | * ram_postcopy_send_discard_bitmap: transmit the discard bitmap |
| 2605 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2606 | * Transmit the set of pages to be discarded after precopy to the target |
| 2607 | * these are pages that: |
| 2608 | * a) Have been previously transmitted but are now dirty again |
| 2609 | * b) Pages that have never been transmitted, this ensures that |
| 2610 | * any pages on the destination that have been mapped by background |
| 2611 | * tasks get discarded (transparent huge pages is the specific concern) |
| 2612 | * Hopefully this is pretty sparse |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2613 | * |
| 2614 | * @ms: current migration state |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2615 | */ |
Peter Xu | 739fcc1 | 2021-12-07 19:50:14 +0800 | [diff] [blame] | 2616 | void ram_postcopy_send_discard_bitmap(MigrationState *ms) |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2617 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2618 | RAMState *rs = ram_state; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2619 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2620 | RCU_READ_LOCK_GUARD(); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2621 | |
| 2622 | /* This should be our last sync, the src is now paused */ |
Gavin Shan | 1e493be | 2023-05-09 12:21:19 +1000 | [diff] [blame] | 2623 | migration_bitmap_sync(rs, false); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2624 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2625 | /* Easiest way to make sure we don't resume in the middle of a host-page */ |
Peter Xu | ec6f3ab | 2022-10-11 17:55:56 -0400 | [diff] [blame] | 2626 | rs->pss[RAM_CHANNEL_PRECOPY].last_sent_block = NULL; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2627 | rs->last_seen_block = NULL; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2628 | rs->last_page = 0; |
| 2629 | |
Peter Xu | 739fcc1 | 2021-12-07 19:50:14 +0800 | [diff] [blame] | 2630 | postcopy_each_ram_send_discard(ms); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2631 | |
Peter Xu | 739fcc1 | 2021-12-07 19:50:14 +0800 | [diff] [blame] | 2632 | trace_ram_postcopy_send_discard_bitmap(); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2635 | /** |
| 2636 | * ram_discard_range: discard dirtied pages at the beginning of postcopy |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2637 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2638 | * Returns zero on success |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2639 | * |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2640 | * @rbname: name of the RAMBlock of the request. NULL means the |
| 2641 | * same that last one. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2642 | * @start: RAMBlock starting page |
| 2643 | * @length: RAMBlock size |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2644 | */ |
Juan Quintela | aaa2064 | 2017-03-21 11:35:24 +0100 | [diff] [blame] | 2645 | int ram_discard_range(const char *rbname, uint64_t start, size_t length) |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2646 | { |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2647 | trace_ram_discard_range(rbname, start, length); |
Dr. David Alan Gilbert | d3a5038 | 2017-02-24 18:28:32 +0000 | [diff] [blame] | 2648 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2649 | RCU_READ_LOCK_GUARD(); |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2650 | RAMBlock *rb = qemu_ram_block_by_name(rbname); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2651 | |
| 2652 | if (!rb) { |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2653 | error_report("ram_discard_range: Failed to find block '%s'", rbname); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 2654 | return -1; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
Peter Xu | 814bb08 | 2018-07-23 20:33:02 +0800 | [diff] [blame] | 2657 | /* |
| 2658 | * On source VM, we don't need to update the received bitmap since |
| 2659 | * we don't even have one. |
| 2660 | */ |
| 2661 | if (rb->receivedmap) { |
| 2662 | bitmap_clear(rb->receivedmap, start >> qemu_target_page_bits(), |
| 2663 | length >> qemu_target_page_bits()); |
| 2664 | } |
| 2665 | |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 2666 | return ram_block_discard_range(rb, start, length); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 2669 | /* |
| 2670 | * For every allocation, we will try not to crash the VM if the |
| 2671 | * allocation failed. |
| 2672 | */ |
| 2673 | static int xbzrle_init(void) |
| 2674 | { |
| 2675 | Error *local_err = NULL; |
| 2676 | |
Juan Quintela | 87dca0c | 2023-03-01 22:20:13 +0100 | [diff] [blame] | 2677 | if (!migrate_xbzrle()) { |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 2678 | return 0; |
| 2679 | } |
| 2680 | |
| 2681 | XBZRLE_cache_lock(); |
| 2682 | |
| 2683 | XBZRLE.zero_target_page = g_try_malloc0(TARGET_PAGE_SIZE); |
| 2684 | if (!XBZRLE.zero_target_page) { |
| 2685 | error_report("%s: Error allocating zero page", __func__); |
| 2686 | goto err_out; |
| 2687 | } |
| 2688 | |
| 2689 | XBZRLE.cache = cache_init(migrate_xbzrle_cache_size(), |
| 2690 | TARGET_PAGE_SIZE, &local_err); |
| 2691 | if (!XBZRLE.cache) { |
| 2692 | error_report_err(local_err); |
| 2693 | goto free_zero_page; |
| 2694 | } |
| 2695 | |
| 2696 | XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE); |
| 2697 | if (!XBZRLE.encoded_buf) { |
| 2698 | error_report("%s: Error allocating encoded_buf", __func__); |
| 2699 | goto free_cache; |
| 2700 | } |
| 2701 | |
| 2702 | XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE); |
| 2703 | if (!XBZRLE.current_buf) { |
| 2704 | error_report("%s: Error allocating current_buf", __func__); |
| 2705 | goto free_encoded_buf; |
| 2706 | } |
| 2707 | |
| 2708 | /* We are all good */ |
| 2709 | XBZRLE_cache_unlock(); |
| 2710 | return 0; |
| 2711 | |
| 2712 | free_encoded_buf: |
| 2713 | g_free(XBZRLE.encoded_buf); |
| 2714 | XBZRLE.encoded_buf = NULL; |
| 2715 | free_cache: |
| 2716 | cache_fini(XBZRLE.cache); |
| 2717 | XBZRLE.cache = NULL; |
| 2718 | free_zero_page: |
| 2719 | g_free(XBZRLE.zero_target_page); |
| 2720 | XBZRLE.zero_target_page = NULL; |
| 2721 | err_out: |
| 2722 | XBZRLE_cache_unlock(); |
| 2723 | return -ENOMEM; |
| 2724 | } |
| 2725 | |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2726 | static int ram_state_init(RAMState **rsp) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2727 | { |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2728 | *rsp = g_try_new0(RAMState, 1); |
| 2729 | |
| 2730 | if (!*rsp) { |
| 2731 | error_report("%s: Init ramstate fail", __func__); |
| 2732 | return -1; |
| 2733 | } |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2734 | |
| 2735 | qemu_mutex_init(&(*rsp)->bitmap_mutex); |
| 2736 | qemu_mutex_init(&(*rsp)->src_page_req_mutex); |
| 2737 | QSIMPLEQ_INIT(&(*rsp)->src_page_requests); |
Juan Quintela | 8d80e19 | 2022-05-10 19:37:36 +0200 | [diff] [blame] | 2738 | (*rsp)->ram_bytes_total = ram_bytes_total(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2739 | |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2740 | /* |
Ivan Ren | 40c4d4a | 2019-07-14 22:51:19 +0800 | [diff] [blame] | 2741 | * Count the total number of pages used by ram blocks not including any |
| 2742 | * gaps due to alignment or unplugs. |
Wei Yang | 0315851 | 2019-06-04 14:17:27 +0800 | [diff] [blame] | 2743 | * This must match with the initial values of dirty bitmap. |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2744 | */ |
Juan Quintela | 8d80e19 | 2022-05-10 19:37:36 +0200 | [diff] [blame] | 2745 | (*rsp)->migration_dirty_pages = (*rsp)->ram_bytes_total >> TARGET_PAGE_BITS; |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2746 | ram_state_reset(*rsp); |
| 2747 | |
| 2748 | return 0; |
| 2749 | } |
| 2750 | |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2751 | static void ram_list_init_bitmaps(void) |
| 2752 | { |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2753 | MigrationState *ms = migrate_get_current(); |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2754 | RAMBlock *block; |
| 2755 | unsigned long pages; |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2756 | uint8_t shift; |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2757 | |
| 2758 | /* Skip setting bitmap if there is no RAM */ |
| 2759 | if (ram_bytes_total()) { |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2760 | shift = ms->clear_bitmap_shift; |
| 2761 | if (shift > CLEAR_BITMAP_SHIFT_MAX) { |
| 2762 | error_report("clear_bitmap_shift (%u) too big, using " |
| 2763 | "max value (%u)", shift, CLEAR_BITMAP_SHIFT_MAX); |
| 2764 | shift = CLEAR_BITMAP_SHIFT_MAX; |
| 2765 | } else if (shift < CLEAR_BITMAP_SHIFT_MIN) { |
| 2766 | error_report("clear_bitmap_shift (%u) too small, using " |
| 2767 | "min value (%u)", shift, CLEAR_BITMAP_SHIFT_MIN); |
| 2768 | shift = CLEAR_BITMAP_SHIFT_MIN; |
| 2769 | } |
| 2770 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2771 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2772 | pages = block->max_length >> TARGET_PAGE_BITS; |
Wei Yang | 0315851 | 2019-06-04 14:17:27 +0800 | [diff] [blame] | 2773 | /* |
| 2774 | * The initial dirty bitmap for migration must be set with all |
| 2775 | * ones to make sure we'll migrate every guest RAM page to |
| 2776 | * destination. |
Ivan Ren | 40c4d4a | 2019-07-14 22:51:19 +0800 | [diff] [blame] | 2777 | * Here we set RAMBlock.bmap all to 1 because when rebegin a |
| 2778 | * new migration after a failed migration, ram_list. |
| 2779 | * dirty_memory[DIRTY_MEMORY_MIGRATION] don't include the whole |
| 2780 | * guest memory. |
Wei Yang | 0315851 | 2019-06-04 14:17:27 +0800 | [diff] [blame] | 2781 | */ |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2782 | block->bmap = bitmap_new(pages); |
Ivan Ren | 40c4d4a | 2019-07-14 22:51:19 +0800 | [diff] [blame] | 2783 | bitmap_set(block->bmap, 0, pages); |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2784 | block->clear_bmap_shift = shift; |
| 2785 | block->clear_bmap = bitmap_new(clear_bmap_size(pages, shift)); |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2786 | } |
| 2787 | } |
| 2788 | } |
| 2789 | |
David Hildenbrand | be39b4c | 2021-10-11 19:53:41 +0200 | [diff] [blame] | 2790 | static void migration_bitmap_clear_discarded_pages(RAMState *rs) |
| 2791 | { |
| 2792 | unsigned long pages; |
| 2793 | RAMBlock *rb; |
| 2794 | |
| 2795 | RCU_READ_LOCK_GUARD(); |
| 2796 | |
| 2797 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
| 2798 | pages = ramblock_dirty_bitmap_clear_discarded_pages(rb); |
| 2799 | rs->migration_dirty_pages -= pages; |
| 2800 | } |
| 2801 | } |
| 2802 | |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2803 | static void ram_init_bitmaps(RAMState *rs) |
| 2804 | { |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2805 | qemu_mutex_lock_ramlist(); |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2806 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2807 | WITH_RCU_READ_LOCK_GUARD() { |
| 2808 | ram_list_init_bitmaps(); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2809 | /* We don't use dirty log with background snapshots */ |
| 2810 | if (!migrate_background_snapshot()) { |
Hyman Huang(黄勇) | 63b41db | 2021-06-29 16:01:19 +0000 | [diff] [blame] | 2811 | memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION); |
Gavin Shan | 1e493be | 2023-05-09 12:21:19 +1000 | [diff] [blame] | 2812 | migration_bitmap_sync_precopy(rs, false); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2813 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2814 | } |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2815 | qemu_mutex_unlock_ramlist(); |
David Hildenbrand | be39b4c | 2021-10-11 19:53:41 +0200 | [diff] [blame] | 2816 | |
| 2817 | /* |
| 2818 | * After an eventual first bitmap sync, fixup the initial bitmap |
| 2819 | * containing all 1s to exclude any discarded pages from migration. |
| 2820 | */ |
| 2821 | migration_bitmap_clear_discarded_pages(rs); |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2822 | } |
| 2823 | |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2824 | static int ram_init_all(RAMState **rsp) |
| 2825 | { |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2826 | if (ram_state_init(rsp)) { |
| 2827 | return -1; |
| 2828 | } |
| 2829 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 2830 | if (xbzrle_init()) { |
| 2831 | ram_state_cleanup(rsp); |
| 2832 | return -1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2833 | } |
| 2834 | |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2835 | ram_init_bitmaps(*rsp); |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2836 | |
| 2837 | return 0; |
| 2838 | } |
| 2839 | |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 2840 | static void ram_state_resume_prepare(RAMState *rs, QEMUFile *out) |
| 2841 | { |
| 2842 | RAMBlock *block; |
| 2843 | uint64_t pages = 0; |
| 2844 | |
| 2845 | /* |
| 2846 | * Postcopy is not using xbzrle/compression, so no need for that. |
| 2847 | * Also, since source are already halted, we don't need to care |
| 2848 | * about dirty page logging as well. |
| 2849 | */ |
| 2850 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2851 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 2852 | pages += bitmap_count_one(block->bmap, |
| 2853 | block->used_length >> TARGET_PAGE_BITS); |
| 2854 | } |
| 2855 | |
| 2856 | /* This may not be aligned with current bitmaps. Recalculate. */ |
| 2857 | rs->migration_dirty_pages = pages; |
| 2858 | |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 2859 | ram_state_reset(rs); |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 2860 | |
| 2861 | /* Update RAMState cache of output QEMUFile */ |
Peter Xu | 7f401b8 | 2022-10-11 17:55:59 -0400 | [diff] [blame] | 2862 | rs->pss[RAM_CHANNEL_PRECOPY].pss_channel = out; |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 2863 | |
| 2864 | trace_ram_state_resume_prepare(pages); |
| 2865 | } |
| 2866 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2867 | /* |
Wei Wang | 6bcb05f | 2018-12-11 16:24:50 +0800 | [diff] [blame] | 2868 | * This function clears bits of the free pages reported by the caller from the |
| 2869 | * migration dirty bitmap. @addr is the host address corresponding to the |
| 2870 | * start of the continuous guest free pages, and @len is the total bytes of |
| 2871 | * those pages. |
| 2872 | */ |
| 2873 | void qemu_guest_free_page_hint(void *addr, size_t len) |
| 2874 | { |
| 2875 | RAMBlock *block; |
| 2876 | ram_addr_t offset; |
| 2877 | size_t used_len, start, npages; |
| 2878 | MigrationState *s = migrate_get_current(); |
| 2879 | |
| 2880 | /* This function is currently expected to be used during live migration */ |
| 2881 | if (!migration_is_setup_or_active(s->state)) { |
| 2882 | return; |
| 2883 | } |
| 2884 | |
| 2885 | for (; len > 0; len -= used_len, addr += used_len) { |
| 2886 | block = qemu_ram_block_from_host(addr, false, &offset); |
| 2887 | if (unlikely(!block || offset >= block->used_length)) { |
| 2888 | /* |
| 2889 | * The implementation might not support RAMBlock resize during |
| 2890 | * live migration, but it could happen in theory with future |
| 2891 | * updates. So we add a check here to capture that case. |
| 2892 | */ |
| 2893 | error_report_once("%s unexpected error", __func__); |
| 2894 | return; |
| 2895 | } |
| 2896 | |
| 2897 | if (len <= block->used_length - offset) { |
| 2898 | used_len = len; |
| 2899 | } else { |
| 2900 | used_len = block->used_length - offset; |
| 2901 | } |
| 2902 | |
| 2903 | start = offset >> TARGET_PAGE_BITS; |
| 2904 | npages = used_len >> TARGET_PAGE_BITS; |
| 2905 | |
| 2906 | qemu_mutex_lock(&ram_state->bitmap_mutex); |
Wei Wang | 3143577d | 2021-07-22 04:30:55 -0400 | [diff] [blame] | 2907 | /* |
| 2908 | * The skipped free pages are equavalent to be sent from clear_bmap's |
| 2909 | * perspective, so clear the bits from the memory region bitmap which |
| 2910 | * are initially set. Otherwise those skipped pages will be sent in |
| 2911 | * the next round after syncing from the memory region bitmap. |
| 2912 | */ |
David Hildenbrand | 1230a25 | 2021-09-04 18:09:07 +0200 | [diff] [blame] | 2913 | migration_clear_memory_region_dirty_bitmap_range(block, start, npages); |
Wei Wang | 6bcb05f | 2018-12-11 16:24:50 +0800 | [diff] [blame] | 2914 | ram_state->migration_dirty_pages -= |
| 2915 | bitmap_count_one_with_offset(block->bmap, start, npages); |
| 2916 | bitmap_clear(block->bmap, start, npages); |
| 2917 | qemu_mutex_unlock(&ram_state->bitmap_mutex); |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | /* |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2922 | * Each of ram_save_setup, ram_save_iterate and ram_save_complete has |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2923 | * long-running RCU critical section. When rcu-reclaims in the code |
| 2924 | * start to become numerous it will be necessary to reduce the |
| 2925 | * granularity of these critical sections. |
| 2926 | */ |
| 2927 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2928 | /** |
| 2929 | * ram_save_setup: Setup RAM for migration |
| 2930 | * |
| 2931 | * Returns zero to indicate success and negative for error |
| 2932 | * |
| 2933 | * @f: QEMUFile where to send the data |
| 2934 | * @opaque: RAMState pointer |
| 2935 | */ |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2936 | static int ram_save_setup(QEMUFile *f, void *opaque) |
| 2937 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2938 | RAMState **rsp = opaque; |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2939 | RAMBlock *block; |
Leonardo Bras | 33d7097 | 2022-05-13 03:28:35 -0300 | [diff] [blame] | 2940 | int ret; |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2941 | |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 2942 | if (compress_threads_save_setup()) { |
| 2943 | return -1; |
| 2944 | } |
| 2945 | |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2946 | /* migration has already setup the bitmap, reuse it. */ |
| 2947 | if (!migration_in_colo_state()) { |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2948 | if (ram_init_all(rsp) != 0) { |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 2949 | compress_threads_save_cleanup(); |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2950 | return -1; |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2951 | } |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2952 | } |
Peter Xu | 7f401b8 | 2022-10-11 17:55:59 -0400 | [diff] [blame] | 2953 | (*rsp)->pss[RAM_CHANNEL_PRECOPY].pss_channel = f; |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2954 | |
Dr. David Alan Gilbert | 0e6ebd4 | 2019-10-07 15:36:38 +0100 | [diff] [blame] | 2955 | WITH_RCU_READ_LOCK_GUARD() { |
Juan Quintela | 8008a27 | 2022-05-10 19:18:19 +0200 | [diff] [blame] | 2956 | qemu_put_be64(f, ram_bytes_total_with_ignored() |
| 2957 | | RAM_SAVE_FLAG_MEM_SIZE); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2958 | |
Dr. David Alan Gilbert | 0e6ebd4 | 2019-10-07 15:36:38 +0100 | [diff] [blame] | 2959 | RAMBLOCK_FOREACH_MIGRATABLE(block) { |
| 2960 | qemu_put_byte(f, strlen(block->idstr)); |
| 2961 | qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); |
| 2962 | qemu_put_be64(f, block->used_length); |
| 2963 | if (migrate_postcopy_ram() && block->page_size != |
| 2964 | qemu_host_page_size) { |
| 2965 | qemu_put_be64(f, block->page_size); |
| 2966 | } |
| 2967 | if (migrate_ignore_shared()) { |
| 2968 | qemu_put_be64(f, block->mr->addr); |
| 2969 | } |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2970 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2971 | } |
| 2972 | |
Juan Quintela | b1b3838 | 2023-10-11 22:35:23 +0200 | [diff] [blame] | 2973 | ret = rdma_registration_start(f, RAM_CONTROL_SETUP); |
Juan Quintela | 4840817 | 2023-10-11 22:35:16 +0200 | [diff] [blame] | 2974 | if (ret < 0) { |
| 2975 | qemu_file_set_error(f, ret); |
Peter Xu | a232670 | 2023-10-24 12:39:33 -0400 | [diff] [blame^] | 2976 | return ret; |
Juan Quintela | 4840817 | 2023-10-11 22:35:16 +0200 | [diff] [blame] | 2977 | } |
Juan Quintela | 5f5b885 | 2023-10-11 22:35:17 +0200 | [diff] [blame] | 2978 | |
Juan Quintela | b1b3838 | 2023-10-11 22:35:23 +0200 | [diff] [blame] | 2979 | ret = rdma_registration_stop(f, RAM_CONTROL_SETUP); |
Juan Quintela | 5f5b885 | 2023-10-11 22:35:17 +0200 | [diff] [blame] | 2980 | if (ret < 0) { |
| 2981 | qemu_file_set_error(f, ret); |
Peter Xu | a232670 | 2023-10-24 12:39:33 -0400 | [diff] [blame^] | 2982 | return ret; |
Juan Quintela | 5f5b885 | 2023-10-11 22:35:17 +0200 | [diff] [blame] | 2983 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2984 | |
Juan Quintela | 4010ba3 | 2021-12-15 20:10:39 +0100 | [diff] [blame] | 2985 | migration_ops = g_malloc0(sizeof(MigrationOps)); |
| 2986 | migration_ops->ram_save_target_page = ram_save_target_page_legacy; |
Fiona Ebner | 930e239 | 2023-10-13 12:58:39 +0200 | [diff] [blame] | 2987 | |
| 2988 | qemu_mutex_unlock_iothread(); |
李皆俊 | 8ebb6ec | 2023-03-17 09:57:13 +0000 | [diff] [blame] | 2989 | ret = multifd_send_sync_main(f); |
Fiona Ebner | 930e239 | 2023-10-13 12:58:39 +0200 | [diff] [blame] | 2990 | qemu_mutex_lock_iothread(); |
Leonardo Bras | 33d7097 | 2022-05-13 03:28:35 -0300 | [diff] [blame] | 2991 | if (ret < 0) { |
| 2992 | return ret; |
| 2993 | } |
| 2994 | |
Juan Quintela | d4f3448 | 2023-10-11 22:55:48 +0200 | [diff] [blame] | 2995 | if (migrate_multifd() && !migrate_multifd_flush_after_each_section()) { |
Juan Quintela | 294e5a4 | 2022-06-21 13:36:11 +0200 | [diff] [blame] | 2996 | qemu_put_be64(f, RAM_SAVE_FLAG_MULTIFD_FLUSH); |
| 2997 | } |
| 2998 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2999 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
Juan Quintela | 35374cb | 2018-04-18 10:13:21 +0200 | [diff] [blame] | 3000 | qemu_fflush(f); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3001 | |
| 3002 | return 0; |
| 3003 | } |
| 3004 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3005 | /** |
| 3006 | * ram_save_iterate: iterative stage for migration |
| 3007 | * |
| 3008 | * Returns zero to indicate success and negative for error |
| 3009 | * |
| 3010 | * @f: QEMUFile where to send the data |
| 3011 | * @opaque: RAMState pointer |
| 3012 | */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3013 | static int ram_save_iterate(QEMUFile *f, void *opaque) |
| 3014 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 3015 | RAMState **temp = opaque; |
| 3016 | RAMState *rs = *temp; |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 3017 | int ret = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3018 | int i; |
| 3019 | int64_t t0; |
Thomas Huth | 5c90308 | 2016-11-04 14:10:17 +0100 | [diff] [blame] | 3020 | int done = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3021 | |
Peter Lieven | b255734 | 2018-03-08 12:18:24 +0100 | [diff] [blame] | 3022 | if (blk_mig_bulk_active()) { |
| 3023 | /* Avoid transferring ram during bulk phase of block migration as |
| 3024 | * the bulk phase will usually take a long time and transferring |
| 3025 | * ram updates during that time is pointless. */ |
| 3026 | goto out; |
| 3027 | } |
| 3028 | |
Peter Xu | 63268c4 | 2021-06-30 16:08:05 -0400 | [diff] [blame] | 3029 | /* |
| 3030 | * We'll take this lock a little bit long, but it's okay for two reasons. |
| 3031 | * Firstly, the only possible other thread to take it is who calls |
| 3032 | * qemu_guest_free_page_hint(), which should be rare; secondly, see |
| 3033 | * MAX_WAIT (if curious, further see commit 4508bd9ed8053ce) below, which |
| 3034 | * guarantees that we'll at least released it in a regular basis. |
| 3035 | */ |
| 3036 | qemu_mutex_lock(&rs->bitmap_mutex); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3037 | WITH_RCU_READ_LOCK_GUARD() { |
| 3038 | if (ram_list.version != rs->last_version) { |
| 3039 | ram_state_reset(rs); |
Dr. David Alan Gilbert | e03a34f | 2018-06-13 11:26:42 +0100 | [diff] [blame] | 3040 | } |
| 3041 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3042 | /* Read version before ram_list.blocks */ |
| 3043 | smp_rmb(); |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 3044 | |
Juan Quintela | b1b3838 | 2023-10-11 22:35:23 +0200 | [diff] [blame] | 3045 | ret = rdma_registration_start(f, RAM_CONTROL_ROUND); |
Juan Quintela | 4840817 | 2023-10-11 22:35:16 +0200 | [diff] [blame] | 3046 | if (ret < 0) { |
| 3047 | qemu_file_set_error(f, ret); |
Peter Xu | a232670 | 2023-10-24 12:39:33 -0400 | [diff] [blame^] | 3048 | goto out; |
Juan Quintela | 4840817 | 2023-10-11 22:35:16 +0200 | [diff] [blame] | 3049 | } |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 3050 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3051 | t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
| 3052 | i = 0; |
Juan Quintela | e1fde0e | 2023-05-15 21:56:58 +0200 | [diff] [blame] | 3053 | while ((ret = migration_rate_exceeded(f)) == 0 || |
Peter Xu | a1fe28d | 2022-01-19 16:09:18 +0800 | [diff] [blame] | 3054 | postcopy_has_request(rs)) { |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3055 | int pages; |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 3056 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3057 | if (qemu_file_get_error(f)) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3058 | break; |
| 3059 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3060 | |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 3061 | pages = ram_find_and_save_block(rs); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3062 | /* no more pages to sent */ |
| 3063 | if (pages == 0) { |
| 3064 | done = 1; |
| 3065 | break; |
| 3066 | } |
| 3067 | |
| 3068 | if (pages < 0) { |
| 3069 | qemu_file_set_error(f, pages); |
| 3070 | break; |
| 3071 | } |
| 3072 | |
| 3073 | rs->target_page_count += pages; |
| 3074 | |
| 3075 | /* |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3076 | * During postcopy, it is necessary to make sure one whole host |
| 3077 | * page is sent in one chunk. |
| 3078 | */ |
| 3079 | if (migrate_postcopy_ram()) { |
Juan Quintela | 8020bc9 | 2023-10-19 13:07:22 +0200 | [diff] [blame] | 3080 | compress_flush_data(); |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3081 | } |
| 3082 | |
| 3083 | /* |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3084 | * we want to check in the 1st loop, just in case it was the 1st |
| 3085 | * time and we had to sync the dirty bitmap. |
| 3086 | * qemu_clock_get_ns() is a bit expensive, so we only check each |
| 3087 | * some iterations |
| 3088 | */ |
| 3089 | if ((i & 63) == 0) { |
| 3090 | uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / |
| 3091 | 1000000; |
| 3092 | if (t1 > MAX_WAIT) { |
| 3093 | trace_ram_save_iterate_big_wait(t1, i); |
| 3094 | break; |
| 3095 | } |
| 3096 | } |
| 3097 | i++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3098 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3099 | } |
Peter Xu | 63268c4 | 2021-06-30 16:08:05 -0400 | [diff] [blame] | 3100 | qemu_mutex_unlock(&rs->bitmap_mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3101 | |
| 3102 | /* |
| 3103 | * Must occur before EOS (or any QEMUFile operation) |
| 3104 | * because of RDMA protocol. |
| 3105 | */ |
Juan Quintela | b1b3838 | 2023-10-11 22:35:23 +0200 | [diff] [blame] | 3106 | ret = rdma_registration_stop(f, RAM_CONTROL_ROUND); |
Juan Quintela | 5f5b885 | 2023-10-11 22:35:17 +0200 | [diff] [blame] | 3107 | if (ret < 0) { |
| 3108 | qemu_file_set_error(f, ret); |
| 3109 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3110 | |
Peter Lieven | b255734 | 2018-03-08 12:18:24 +0100 | [diff] [blame] | 3111 | out: |
Juan Quintela | b69a022 | 2020-01-22 11:36:12 +0100 | [diff] [blame] | 3112 | if (ret >= 0 |
| 3113 | && migration_is_setup_or_active(migrate_get_current()->state)) { |
Juan Quintela | d4f3448 | 2023-10-11 22:55:48 +0200 | [diff] [blame] | 3114 | if (migrate_multifd() && migrate_multifd_flush_after_each_section()) { |
Juan Quintela | b05292c | 2022-06-21 12:21:32 +0200 | [diff] [blame] | 3115 | ret = multifd_send_sync_main(rs->pss[RAM_CHANNEL_PRECOPY].pss_channel); |
| 3116 | if (ret < 0) { |
| 3117 | return ret; |
| 3118 | } |
Leonardo Bras | 33d7097 | 2022-05-13 03:28:35 -0300 | [diff] [blame] | 3119 | } |
| 3120 | |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 3121 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 3122 | qemu_fflush(f); |
David Edmondson | 4c2d0f6 | 2021-12-21 09:34:40 +0000 | [diff] [blame] | 3123 | ram_transferred_add(8); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3124 | |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 3125 | ret = qemu_file_get_error(f); |
| 3126 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3127 | if (ret < 0) { |
| 3128 | return ret; |
| 3129 | } |
| 3130 | |
Thomas Huth | 5c90308 | 2016-11-04 14:10:17 +0100 | [diff] [blame] | 3131 | return done; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3132 | } |
| 3133 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3134 | /** |
| 3135 | * ram_save_complete: function called to send the remaining amount of ram |
| 3136 | * |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 3137 | * Returns zero to indicate success or negative on error |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3138 | * |
| 3139 | * Called with iothread lock |
| 3140 | * |
| 3141 | * @f: QEMUFile where to send the data |
| 3142 | * @opaque: RAMState pointer |
| 3143 | */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3144 | static int ram_save_complete(QEMUFile *f, void *opaque) |
| 3145 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 3146 | RAMState **temp = opaque; |
| 3147 | RAMState *rs = *temp; |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 3148 | int ret = 0; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 3149 | |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 3150 | rs->last_stage = !migration_in_colo_state(); |
| 3151 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3152 | WITH_RCU_READ_LOCK_GUARD() { |
| 3153 | if (!migration_in_postcopy()) { |
Gavin Shan | 1e493be | 2023-05-09 12:21:19 +1000 | [diff] [blame] | 3154 | migration_bitmap_sync_precopy(rs, true); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3155 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3156 | |
Juan Quintela | b1b3838 | 2023-10-11 22:35:23 +0200 | [diff] [blame] | 3157 | ret = rdma_registration_start(f, RAM_CONTROL_FINISH); |
Juan Quintela | 4840817 | 2023-10-11 22:35:16 +0200 | [diff] [blame] | 3158 | if (ret < 0) { |
| 3159 | qemu_file_set_error(f, ret); |
Peter Xu | a232670 | 2023-10-24 12:39:33 -0400 | [diff] [blame^] | 3160 | return ret; |
Juan Quintela | 4840817 | 2023-10-11 22:35:16 +0200 | [diff] [blame] | 3161 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3162 | |
| 3163 | /* try transferring iterative blocks of memory */ |
| 3164 | |
| 3165 | /* flush all remaining blocks regardless of rate limiting */ |
Peter Xu | c13221b | 2022-10-11 17:55:45 -0400 | [diff] [blame] | 3166 | qemu_mutex_lock(&rs->bitmap_mutex); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3167 | while (true) { |
| 3168 | int pages; |
| 3169 | |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 3170 | pages = ram_find_and_save_block(rs); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3171 | /* no more blocks to sent */ |
| 3172 | if (pages == 0) { |
| 3173 | break; |
| 3174 | } |
| 3175 | if (pages < 0) { |
Peter Xu | a232670 | 2023-10-24 12:39:33 -0400 | [diff] [blame^] | 3176 | qemu_mutex_unlock(&rs->bitmap_mutex); |
| 3177 | return pages; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3178 | } |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 3179 | } |
Peter Xu | c13221b | 2022-10-11 17:55:45 -0400 | [diff] [blame] | 3180 | qemu_mutex_unlock(&rs->bitmap_mutex); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3181 | |
Juan Quintela | 8020bc9 | 2023-10-19 13:07:22 +0200 | [diff] [blame] | 3182 | compress_flush_data(); |
Juan Quintela | 5f5b885 | 2023-10-11 22:35:17 +0200 | [diff] [blame] | 3183 | |
Peter Xu | a232670 | 2023-10-24 12:39:33 -0400 | [diff] [blame^] | 3184 | ret = rdma_registration_stop(f, RAM_CONTROL_FINISH); |
| 3185 | if (ret < 0) { |
| 3186 | qemu_file_set_error(f, ret); |
| 3187 | return ret; |
Juan Quintela | 5f5b885 | 2023-10-11 22:35:17 +0200 | [diff] [blame] | 3188 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3189 | } |
| 3190 | |
Peter Xu | 7f401b8 | 2022-10-11 17:55:59 -0400 | [diff] [blame] | 3191 | ret = multifd_send_sync_main(rs->pss[RAM_CHANNEL_PRECOPY].pss_channel); |
Leonardo Bras | 33d7097 | 2022-05-13 03:28:35 -0300 | [diff] [blame] | 3192 | if (ret < 0) { |
| 3193 | return ret; |
| 3194 | } |
| 3195 | |
Juan Quintela | d4f3448 | 2023-10-11 22:55:48 +0200 | [diff] [blame] | 3196 | if (migrate_multifd() && !migrate_multifd_flush_after_each_section()) { |
Juan Quintela | 294e5a4 | 2022-06-21 13:36:11 +0200 | [diff] [blame] | 3197 | qemu_put_be64(f, RAM_SAVE_FLAG_MULTIFD_FLUSH); |
| 3198 | } |
Leonardo Bras | 33d7097 | 2022-05-13 03:28:35 -0300 | [diff] [blame] | 3199 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 3200 | qemu_fflush(f); |
| 3201 | |
| 3202 | return 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3203 | } |
| 3204 | |
Juan Quintela | 24beea4 | 2023-02-08 14:48:02 +0100 | [diff] [blame] | 3205 | static void ram_state_pending_estimate(void *opaque, uint64_t *must_precopy, |
| 3206 | uint64_t *can_postcopy) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3207 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 3208 | RAMState **temp = opaque; |
| 3209 | RAMState *rs = *temp; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3210 | |
Juan Quintela | c8df4a7 | 2022-10-03 02:00:03 +0200 | [diff] [blame] | 3211 | uint64_t remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3212 | |
Juan Quintela | c8df4a7 | 2022-10-03 02:00:03 +0200 | [diff] [blame] | 3213 | if (migrate_postcopy_ram()) { |
| 3214 | /* We can do postcopy, and all the data is postcopiable */ |
Juan Quintela | 24beea4 | 2023-02-08 14:48:02 +0100 | [diff] [blame] | 3215 | *can_postcopy += remaining_size; |
Juan Quintela | c8df4a7 | 2022-10-03 02:00:03 +0200 | [diff] [blame] | 3216 | } else { |
Juan Quintela | 24beea4 | 2023-02-08 14:48:02 +0100 | [diff] [blame] | 3217 | *must_precopy += remaining_size; |
Juan Quintela | c8df4a7 | 2022-10-03 02:00:03 +0200 | [diff] [blame] | 3218 | } |
| 3219 | } |
| 3220 | |
Juan Quintela | 24beea4 | 2023-02-08 14:48:02 +0100 | [diff] [blame] | 3221 | static void ram_state_pending_exact(void *opaque, uint64_t *must_precopy, |
| 3222 | uint64_t *can_postcopy) |
Juan Quintela | c8df4a7 | 2022-10-03 02:00:03 +0200 | [diff] [blame] | 3223 | { |
Juan Quintela | 28ef533 | 2023-04-12 22:30:20 +0200 | [diff] [blame] | 3224 | MigrationState *s = migrate_get_current(); |
Juan Quintela | c8df4a7 | 2022-10-03 02:00:03 +0200 | [diff] [blame] | 3225 | RAMState **temp = opaque; |
| 3226 | RAMState *rs = *temp; |
| 3227 | |
| 3228 | uint64_t remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
| 3229 | |
Juan Quintela | 28ef533 | 2023-04-12 22:30:20 +0200 | [diff] [blame] | 3230 | if (!migration_in_postcopy() && remaining_size < s->threshold_size) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3231 | qemu_mutex_lock_iothread(); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3232 | WITH_RCU_READ_LOCK_GUARD() { |
Gavin Shan | 1e493be | 2023-05-09 12:21:19 +1000 | [diff] [blame] | 3233 | migration_bitmap_sync_precopy(rs, false); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3234 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3235 | qemu_mutex_unlock_iothread(); |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 3236 | remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3237 | } |
Dr. David Alan Gilbert | c31b098 | 2015-11-05 18:10:54 +0000 | [diff] [blame] | 3238 | |
Vladimir Sementsov-Ogievskiy | 86e1167 | 2017-07-10 19:30:15 +0300 | [diff] [blame] | 3239 | if (migrate_postcopy_ram()) { |
| 3240 | /* We can do postcopy, and all the data is postcopiable */ |
Juan Quintela | 24beea4 | 2023-02-08 14:48:02 +0100 | [diff] [blame] | 3241 | *can_postcopy += remaining_size; |
Vladimir Sementsov-Ogievskiy | 86e1167 | 2017-07-10 19:30:15 +0300 | [diff] [blame] | 3242 | } else { |
Juan Quintela | 24beea4 | 2023-02-08 14:48:02 +0100 | [diff] [blame] | 3243 | *must_precopy += remaining_size; |
Vladimir Sementsov-Ogievskiy | 86e1167 | 2017-07-10 19:30:15 +0300 | [diff] [blame] | 3244 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) |
| 3248 | { |
| 3249 | unsigned int xh_len; |
| 3250 | int xh_flags; |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 3251 | uint8_t *loaded_data; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3252 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3253 | /* extract RLE header */ |
| 3254 | xh_flags = qemu_get_byte(f); |
| 3255 | xh_len = qemu_get_be16(f); |
| 3256 | |
| 3257 | if (xh_flags != ENCODING_FLAG_XBZRLE) { |
| 3258 | error_report("Failed to load XBZRLE page - wrong compression!"); |
| 3259 | return -1; |
| 3260 | } |
| 3261 | |
| 3262 | if (xh_len > TARGET_PAGE_SIZE) { |
| 3263 | error_report("Failed to load XBZRLE page - len overflow!"); |
| 3264 | return -1; |
| 3265 | } |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3266 | loaded_data = XBZRLE.decoded_buf; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3267 | /* load data and decode */ |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3268 | /* it can change loaded_data to point to an internal buffer */ |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 3269 | qemu_get_buffer_in_place(f, &loaded_data, xh_len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3270 | |
| 3271 | /* decode RLE */ |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 3272 | if (xbzrle_decode_buffer(loaded_data, xh_len, host, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3273 | TARGET_PAGE_SIZE) == -1) { |
| 3274 | error_report("Failed to load XBZRLE page - decode error!"); |
| 3275 | return -1; |
| 3276 | } |
| 3277 | |
| 3278 | return 0; |
| 3279 | } |
| 3280 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3281 | /** |
| 3282 | * ram_block_from_stream: read a RAMBlock id from the migration stream |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3283 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3284 | * Must be called from within a rcu critical section. |
| 3285 | * |
| 3286 | * Returns a pointer from within the RCU-protected ram_list. |
| 3287 | * |
Peter Xu | 755e8d7 | 2022-03-01 16:39:07 +0800 | [diff] [blame] | 3288 | * @mis: the migration incoming state pointer |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3289 | * @f: QEMUFile where to read the data from |
| 3290 | * @flags: Page flags (mostly to see if it's a continuation of previous block) |
Peter Xu | c01b16e | 2022-07-07 14:55:04 -0400 | [diff] [blame] | 3291 | * @channel: the channel we're using |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3292 | */ |
Peter Xu | 755e8d7 | 2022-03-01 16:39:07 +0800 | [diff] [blame] | 3293 | static inline RAMBlock *ram_block_from_stream(MigrationIncomingState *mis, |
Peter Xu | c01b16e | 2022-07-07 14:55:04 -0400 | [diff] [blame] | 3294 | QEMUFile *f, int flags, |
| 3295 | int channel) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3296 | { |
Peter Xu | c01b16e | 2022-07-07 14:55:04 -0400 | [diff] [blame] | 3297 | RAMBlock *block = mis->last_recv_block[channel]; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3298 | char id[256]; |
| 3299 | uint8_t len; |
| 3300 | |
| 3301 | if (flags & RAM_SAVE_FLAG_CONTINUE) { |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3302 | if (!block) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3303 | error_report("Ack, bad migration stream!"); |
| 3304 | return NULL; |
| 3305 | } |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3306 | return block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | len = qemu_get_byte(f); |
| 3310 | qemu_get_buffer(f, (uint8_t *)id, len); |
| 3311 | id[len] = 0; |
| 3312 | |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 3313 | block = qemu_ram_block_by_name(id); |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3314 | if (!block) { |
| 3315 | error_report("Can't find block %s", id); |
| 3316 | return NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3317 | } |
| 3318 | |
David Hildenbrand | f161c88 | 2023-07-06 09:56:08 +0200 | [diff] [blame] | 3319 | if (migrate_ram_is_ignored(block)) { |
Cédric Le Goater | b895de5 | 2018-05-14 08:57:00 +0200 | [diff] [blame] | 3320 | error_report("block %s should not be migrated !", id); |
| 3321 | return NULL; |
| 3322 | } |
| 3323 | |
Peter Xu | c01b16e | 2022-07-07 14:55:04 -0400 | [diff] [blame] | 3324 | mis->last_recv_block[channel] = block; |
Peter Xu | 755e8d7 | 2022-03-01 16:39:07 +0800 | [diff] [blame] | 3325 | |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3326 | return block; |
| 3327 | } |
| 3328 | |
| 3329 | static inline void *host_from_ram_block_offset(RAMBlock *block, |
| 3330 | ram_addr_t offset) |
| 3331 | { |
| 3332 | if (!offset_in_ramblock(block, offset)) { |
| 3333 | return NULL; |
| 3334 | } |
| 3335 | |
| 3336 | return block->host + offset; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3337 | } |
| 3338 | |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3339 | static void *host_page_from_ram_block_offset(RAMBlock *block, |
| 3340 | ram_addr_t offset) |
| 3341 | { |
| 3342 | /* Note: Explicitly no check against offset_in_ramblock(). */ |
| 3343 | return (void *)QEMU_ALIGN_DOWN((uintptr_t)(block->host + offset), |
| 3344 | block->page_size); |
| 3345 | } |
| 3346 | |
| 3347 | static ram_addr_t host_page_offset_from_ram_block_offset(RAMBlock *block, |
| 3348 | ram_addr_t offset) |
| 3349 | { |
| 3350 | return ((uintptr_t)block->host + offset) & (block->page_size - 1); |
| 3351 | } |
| 3352 | |
Lukas Straub | 871cfc5 | 2023-05-08 21:10:52 +0200 | [diff] [blame] | 3353 | void colo_record_bitmap(RAMBlock *block, ram_addr_t *normal, uint32_t pages) |
| 3354 | { |
| 3355 | qemu_mutex_lock(&ram_state->bitmap_mutex); |
| 3356 | for (int i = 0; i < pages; i++) { |
| 3357 | ram_addr_t offset = normal[i]; |
| 3358 | ram_state->migration_dirty_pages += !test_and_set_bit( |
| 3359 | offset >> TARGET_PAGE_BITS, |
| 3360 | block->bmap); |
| 3361 | } |
| 3362 | qemu_mutex_unlock(&ram_state->bitmap_mutex); |
| 3363 | } |
| 3364 | |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3365 | static inline void *colo_cache_from_block_offset(RAMBlock *block, |
zhanghailiang | 8af6637 | 2020-02-24 14:54:11 +0800 | [diff] [blame] | 3366 | ram_addr_t offset, bool record_bitmap) |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3367 | { |
| 3368 | if (!offset_in_ramblock(block, offset)) { |
| 3369 | return NULL; |
| 3370 | } |
| 3371 | if (!block->colo_cache) { |
| 3372 | error_report("%s: colo_cache is NULL in block :%s", |
| 3373 | __func__, block->idstr); |
| 3374 | return NULL; |
| 3375 | } |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3376 | |
| 3377 | /* |
| 3378 | * During colo checkpoint, we need bitmap of these migrated pages. |
| 3379 | * It help us to decide which pages in ram cache should be flushed |
| 3380 | * into VM's RAM later. |
| 3381 | */ |
Lukas Straub | 871cfc5 | 2023-05-08 21:10:52 +0200 | [diff] [blame] | 3382 | if (record_bitmap) { |
| 3383 | colo_record_bitmap(block, &offset, 1); |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3384 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3385 | return block->colo_cache + offset; |
| 3386 | } |
| 3387 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3388 | /** |
Juan Quintela | 7091dab | 2023-10-19 10:52:59 +0200 | [diff] [blame] | 3389 | * ram_handle_zero: handle the zero page case |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3390 | * |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3391 | * If a page (or a whole RDMA chunk) has been |
| 3392 | * determined to be zero, then zap it. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3393 | * |
| 3394 | * @host: host address for the zero page |
| 3395 | * @ch: what the page is filled from. We only support zero |
| 3396 | * @size: size of the zero page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3397 | */ |
Juan Quintela | 7091dab | 2023-10-19 10:52:59 +0200 | [diff] [blame] | 3398 | void ram_handle_zero(void *host, uint64_t size) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3399 | { |
Juan Quintela | 7091dab | 2023-10-19 10:52:59 +0200 | [diff] [blame] | 3400 | if (!buffer_is_zero(host, size)) { |
| 3401 | memset(host, 0, size); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3402 | } |
| 3403 | } |
| 3404 | |
Rao, Lei | b70cb3b | 2020-10-16 13:52:01 +0800 | [diff] [blame] | 3405 | static void colo_init_ram_state(void) |
| 3406 | { |
| 3407 | ram_state_init(&ram_state); |
Rao, Lei | b70cb3b | 2020-10-16 13:52:01 +0800 | [diff] [blame] | 3408 | } |
| 3409 | |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3410 | /* |
| 3411 | * colo cache: this is for secondary VM, we cache the whole |
| 3412 | * memory of the secondary VM, it is need to hold the global lock |
| 3413 | * to call this helper. |
| 3414 | */ |
| 3415 | int colo_init_ram_cache(void) |
| 3416 | { |
| 3417 | RAMBlock *block; |
| 3418 | |
Paolo Bonzini | 44901b5 | 2019-12-13 15:07:22 +0100 | [diff] [blame] | 3419 | WITH_RCU_READ_LOCK_GUARD() { |
| 3420 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3421 | block->colo_cache = qemu_anon_ram_alloc(block->used_length, |
David Hildenbrand | 8dbe22c | 2021-05-10 13:43:21 +0200 | [diff] [blame] | 3422 | NULL, false, false); |
Paolo Bonzini | 44901b5 | 2019-12-13 15:07:22 +0100 | [diff] [blame] | 3423 | if (!block->colo_cache) { |
| 3424 | error_report("%s: Can't alloc memory for COLO cache of block %s," |
| 3425 | "size 0x" RAM_ADDR_FMT, __func__, block->idstr, |
| 3426 | block->used_length); |
| 3427 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3428 | if (block->colo_cache) { |
| 3429 | qemu_anon_ram_free(block->colo_cache, block->used_length); |
| 3430 | block->colo_cache = NULL; |
| 3431 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3432 | } |
Paolo Bonzini | 44901b5 | 2019-12-13 15:07:22 +0100 | [diff] [blame] | 3433 | return -errno; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3434 | } |
Lukas Straub | e5fdf92 | 2021-07-04 18:14:44 +0200 | [diff] [blame] | 3435 | if (!machine_dump_guest_core(current_machine)) { |
| 3436 | qemu_madvise(block->colo_cache, block->used_length, |
| 3437 | QEMU_MADV_DONTDUMP); |
| 3438 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3439 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3440 | } |
Paolo Bonzini | 44901b5 | 2019-12-13 15:07:22 +0100 | [diff] [blame] | 3441 | |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3442 | /* |
| 3443 | * Record the dirty pages that sent by PVM, we use this dirty bitmap together |
| 3444 | * with to decide which page in cache should be flushed into SVM's RAM. Here |
| 3445 | * we use the same name 'ram_bitmap' as for migration. |
| 3446 | */ |
| 3447 | if (ram_bytes_total()) { |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3448 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3449 | unsigned long pages = block->max_length >> TARGET_PAGE_BITS; |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3450 | block->bmap = bitmap_new(pages); |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3451 | } |
| 3452 | } |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3453 | |
Rao, Lei | b70cb3b | 2020-10-16 13:52:01 +0800 | [diff] [blame] | 3454 | colo_init_ram_state(); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3455 | return 0; |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3456 | } |
| 3457 | |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3458 | /* TODO: duplicated with ram_init_bitmaps */ |
| 3459 | void colo_incoming_start_dirty_log(void) |
| 3460 | { |
| 3461 | RAMBlock *block = NULL; |
| 3462 | /* For memory_global_dirty_log_start below. */ |
| 3463 | qemu_mutex_lock_iothread(); |
| 3464 | qemu_mutex_lock_ramlist(); |
| 3465 | |
Gavin Shan | 1e493be | 2023-05-09 12:21:19 +1000 | [diff] [blame] | 3466 | memory_global_dirty_log_sync(false); |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3467 | WITH_RCU_READ_LOCK_GUARD() { |
| 3468 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3469 | ramblock_sync_dirty_bitmap(ram_state, block); |
| 3470 | /* Discard this dirty bitmap record */ |
| 3471 | bitmap_zero(block->bmap, block->max_length >> TARGET_PAGE_BITS); |
| 3472 | } |
Hyman Huang(黄勇) | 63b41db | 2021-06-29 16:01:19 +0000 | [diff] [blame] | 3473 | memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION); |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3474 | } |
| 3475 | ram_state->migration_dirty_pages = 0; |
| 3476 | qemu_mutex_unlock_ramlist(); |
| 3477 | qemu_mutex_unlock_iothread(); |
| 3478 | } |
| 3479 | |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3480 | /* It is need to hold the global lock to call this helper */ |
| 3481 | void colo_release_ram_cache(void) |
| 3482 | { |
| 3483 | RAMBlock *block; |
| 3484 | |
Hyman Huang(黄勇) | 63b41db | 2021-06-29 16:01:19 +0000 | [diff] [blame] | 3485 | memory_global_dirty_log_stop(GLOBAL_DIRTY_MIGRATION); |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3486 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3487 | g_free(block->bmap); |
| 3488 | block->bmap = NULL; |
| 3489 | } |
| 3490 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3491 | WITH_RCU_READ_LOCK_GUARD() { |
| 3492 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3493 | if (block->colo_cache) { |
| 3494 | qemu_anon_ram_free(block->colo_cache, block->used_length); |
| 3495 | block->colo_cache = NULL; |
| 3496 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3497 | } |
| 3498 | } |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3499 | ram_state_cleanup(&ram_state); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3500 | } |
| 3501 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3502 | /** |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3503 | * ram_load_setup: Setup RAM for migration incoming side |
| 3504 | * |
| 3505 | * Returns zero to indicate success and negative for error |
| 3506 | * |
| 3507 | * @f: QEMUFile where to receive the data |
| 3508 | * @opaque: RAMState pointer |
| 3509 | */ |
| 3510 | static int ram_load_setup(QEMUFile *f, void *opaque) |
| 3511 | { |
| 3512 | xbzrle_load_setup(); |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3513 | ramblock_recv_map_init(); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3514 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3515 | return 0; |
| 3516 | } |
| 3517 | |
| 3518 | static int ram_load_cleanup(void *opaque) |
| 3519 | { |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3520 | RAMBlock *rb; |
Junyan He | 56eb90a | 2018-07-18 15:48:03 +0800 | [diff] [blame] | 3521 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3522 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
Beata Michalska | bd108a4 | 2019-11-21 00:08:42 +0000 | [diff] [blame] | 3523 | qemu_ram_block_writeback(rb); |
Junyan He | 56eb90a | 2018-07-18 15:48:03 +0800 | [diff] [blame] | 3524 | } |
| 3525 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3526 | xbzrle_load_cleanup(); |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3527 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3528 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3529 | g_free(rb->receivedmap); |
| 3530 | rb->receivedmap = NULL; |
| 3531 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3532 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3533 | return 0; |
| 3534 | } |
| 3535 | |
| 3536 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3537 | * ram_postcopy_incoming_init: allocate postcopy data structures |
| 3538 | * |
| 3539 | * Returns 0 for success and negative if there was one error |
| 3540 | * |
| 3541 | * @mis: current migration incoming state |
| 3542 | * |
| 3543 | * Allocate data structures etc needed by incoming migration with |
| 3544 | * postcopy-ram. postcopy-ram's similarly names |
| 3545 | * postcopy_ram_incoming_init does the work. |
Dr. David Alan Gilbert | 1caddf8 | 2015-11-05 18:11:03 +0000 | [diff] [blame] | 3546 | */ |
| 3547 | int ram_postcopy_incoming_init(MigrationIncomingState *mis) |
| 3548 | { |
David Hildenbrand | c136180 | 2018-06-20 22:27:36 +0200 | [diff] [blame] | 3549 | return postcopy_ram_incoming_init(mis); |
Dr. David Alan Gilbert | 1caddf8 | 2015-11-05 18:11:03 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3552 | /** |
| 3553 | * ram_load_postcopy: load a page in postcopy case |
| 3554 | * |
| 3555 | * Returns 0 for success or -errno in case of error |
| 3556 | * |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3557 | * Called in postcopy mode by ram_load(). |
| 3558 | * rcu_read_lock is taken prior to this being called. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3559 | * |
| 3560 | * @f: QEMUFile where to send the data |
Peter Xu | 36f62f1 | 2022-07-07 14:55:02 -0400 | [diff] [blame] | 3561 | * @channel: the channel to use for loading |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3562 | */ |
Peter Xu | 36f62f1 | 2022-07-07 14:55:02 -0400 | [diff] [blame] | 3563 | int ram_load_postcopy(QEMUFile *f, int channel) |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3564 | { |
| 3565 | int flags = 0, ret = 0; |
| 3566 | bool place_needed = false; |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3567 | bool matches_target_page_size = false; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3568 | MigrationIncomingState *mis = migration_incoming_get_current(); |
Peter Xu | 36f62f1 | 2022-07-07 14:55:02 -0400 | [diff] [blame] | 3569 | PostcopyTmpPage *tmp_page = &mis->postcopy_tmp_pages[channel]; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3570 | |
| 3571 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { |
| 3572 | ram_addr_t addr; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3573 | void *page_buffer = NULL; |
| 3574 | void *place_source = NULL; |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 3575 | RAMBlock *block = NULL; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3576 | uint8_t ch; |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3577 | int len; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3578 | |
| 3579 | addr = qemu_get_be64(f); |
Peter Xu | 7a9ddfb | 2018-02-08 18:31:05 +0800 | [diff] [blame] | 3580 | |
| 3581 | /* |
| 3582 | * If qemu file error, we should stop here, and then "addr" |
| 3583 | * may be invalid |
| 3584 | */ |
| 3585 | ret = qemu_file_get_error(f); |
| 3586 | if (ret) { |
| 3587 | break; |
| 3588 | } |
| 3589 | |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3590 | flags = addr & ~TARGET_PAGE_MASK; |
| 3591 | addr &= TARGET_PAGE_MASK; |
| 3592 | |
Peter Xu | 36f62f1 | 2022-07-07 14:55:02 -0400 | [diff] [blame] | 3593 | trace_ram_load_postcopy_loop(channel, (uint64_t)addr, flags); |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3594 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | |
| 3595 | RAM_SAVE_FLAG_COMPRESS_PAGE)) { |
Peter Xu | c01b16e | 2022-07-07 14:55:04 -0400 | [diff] [blame] | 3596 | block = ram_block_from_stream(mis, f, flags, channel); |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3597 | if (!block) { |
| 3598 | ret = -EINVAL; |
| 3599 | break; |
| 3600 | } |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3601 | |
David Hildenbrand | 898ba90 | 2021-04-29 13:27:06 +0200 | [diff] [blame] | 3602 | /* |
| 3603 | * Relying on used_length is racy and can result in false positives. |
| 3604 | * We might place pages beyond used_length in case RAM was shrunk |
| 3605 | * while in postcopy, which is fine - trying to place via |
| 3606 | * UFFDIO_COPY/UFFDIO_ZEROPAGE will never segfault. |
| 3607 | */ |
| 3608 | if (!block->host || addr >= block->postcopy_length) { |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3609 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
| 3610 | ret = -EINVAL; |
| 3611 | break; |
| 3612 | } |
Peter Xu | 77dadc3 | 2022-03-01 16:39:04 +0800 | [diff] [blame] | 3613 | tmp_page->target_pages++; |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3614 | matches_target_page_size = block->page_size == TARGET_PAGE_SIZE; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3615 | /* |
Dr. David Alan Gilbert | 28abd20 | 2017-02-24 18:28:37 +0000 | [diff] [blame] | 3616 | * Postcopy requires that we place whole host pages atomically; |
| 3617 | * these may be huge pages for RAMBlocks that are backed by |
| 3618 | * hugetlbfs. |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3619 | * To make it atomic, the data is read into a temporary page |
| 3620 | * that's moved into place later. |
| 3621 | * The migration protocol uses, possibly smaller, target-pages |
| 3622 | * however the source ensures it always sends all the components |
Wei Yang | 91ba442 | 2019-11-07 20:39:06 +0800 | [diff] [blame] | 3623 | * of a host page in one chunk. |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3624 | */ |
Peter Xu | 77dadc3 | 2022-03-01 16:39:04 +0800 | [diff] [blame] | 3625 | page_buffer = tmp_page->tmp_huge_page + |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3626 | host_page_offset_from_ram_block_offset(block, addr); |
| 3627 | /* If all TP are zero then we can optimise the place */ |
Peter Xu | 77dadc3 | 2022-03-01 16:39:04 +0800 | [diff] [blame] | 3628 | if (tmp_page->target_pages == 1) { |
| 3629 | tmp_page->host_addr = |
| 3630 | host_page_from_ram_block_offset(block, addr); |
| 3631 | } else if (tmp_page->host_addr != |
| 3632 | host_page_from_ram_block_offset(block, addr)) { |
Dr. David Alan Gilbert | c53b7dd | 2015-11-05 18:11:12 +0000 | [diff] [blame] | 3633 | /* not the 1st TP within the HP */ |
Peter Xu | 36f62f1 | 2022-07-07 14:55:02 -0400 | [diff] [blame] | 3634 | error_report("Non-same host page detected on channel %d: " |
Peter Xu | cfc7dc8 | 2022-03-01 16:39:05 +0800 | [diff] [blame] | 3635 | "Target host page %p, received host page %p " |
| 3636 | "(rb %s offset 0x"RAM_ADDR_FMT" target_pages %d)", |
Peter Xu | 36f62f1 | 2022-07-07 14:55:02 -0400 | [diff] [blame] | 3637 | channel, tmp_page->host_addr, |
Peter Xu | cfc7dc8 | 2022-03-01 16:39:05 +0800 | [diff] [blame] | 3638 | host_page_from_ram_block_offset(block, addr), |
| 3639 | block->idstr, addr, tmp_page->target_pages); |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3640 | ret = -EINVAL; |
| 3641 | break; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | /* |
| 3645 | * If it's the last part of a host page then we place the host |
| 3646 | * page |
| 3647 | */ |
Peter Xu | 77dadc3 | 2022-03-01 16:39:04 +0800 | [diff] [blame] | 3648 | if (tmp_page->target_pages == |
| 3649 | (block->page_size / TARGET_PAGE_SIZE)) { |
Wei Yang | 4cbb3c6 | 2019-11-07 20:39:04 +0800 | [diff] [blame] | 3650 | place_needed = true; |
Wei Yang | 4cbb3c6 | 2019-11-07 20:39:04 +0800 | [diff] [blame] | 3651 | } |
Peter Xu | 77dadc3 | 2022-03-01 16:39:04 +0800 | [diff] [blame] | 3652 | place_source = tmp_page->tmp_huge_page; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
| 3655 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 3656 | case RAM_SAVE_FLAG_ZERO: |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3657 | ch = qemu_get_byte(f); |
Juan Quintela | 413d64f | 2023-10-19 10:52:58 +0200 | [diff] [blame] | 3658 | if (ch != 0) { |
| 3659 | error_report("Found a zero page with value %d", ch); |
| 3660 | ret = -EINVAL; |
| 3661 | break; |
| 3662 | } |
Wei Yang | 2e36bc1 | 2019-11-07 20:39:02 +0800 | [diff] [blame] | 3663 | /* |
| 3664 | * Can skip to set page_buffer when |
| 3665 | * this is a zero page and (block->page_size == TARGET_PAGE_SIZE). |
| 3666 | */ |
Juan Quintela | 413d64f | 2023-10-19 10:52:58 +0200 | [diff] [blame] | 3667 | if (!matches_target_page_size) { |
Wei Yang | 2e36bc1 | 2019-11-07 20:39:02 +0800 | [diff] [blame] | 3668 | memset(page_buffer, ch, TARGET_PAGE_SIZE); |
| 3669 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3670 | break; |
| 3671 | |
| 3672 | case RAM_SAVE_FLAG_PAGE: |
Peter Xu | 77dadc3 | 2022-03-01 16:39:04 +0800 | [diff] [blame] | 3673 | tmp_page->all_zero = false; |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3674 | if (!matches_target_page_size) { |
| 3675 | /* For huge pages, we always use temporary buffer */ |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3676 | qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE); |
| 3677 | } else { |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3678 | /* |
| 3679 | * For small pages that matches target page size, we |
| 3680 | * avoid the qemu_file copy. Instead we directly use |
| 3681 | * the buffer of QEMUFile to place the page. Note: we |
| 3682 | * cannot do any QEMUFile operation before using that |
| 3683 | * buffer to make sure the buffer is valid when |
| 3684 | * placing the page. |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3685 | */ |
| 3686 | qemu_get_buffer_in_place(f, (uint8_t **)&place_source, |
| 3687 | TARGET_PAGE_SIZE); |
| 3688 | } |
| 3689 | break; |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3690 | case RAM_SAVE_FLAG_COMPRESS_PAGE: |
Peter Xu | 77dadc3 | 2022-03-01 16:39:04 +0800 | [diff] [blame] | 3691 | tmp_page->all_zero = false; |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3692 | len = qemu_get_be32(f); |
| 3693 | if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) { |
| 3694 | error_report("Invalid compressed data length: %d", len); |
| 3695 | ret = -EINVAL; |
| 3696 | break; |
| 3697 | } |
| 3698 | decompress_data_with_multi_threads(f, page_buffer, len); |
| 3699 | break; |
Juan Quintela | 294e5a4 | 2022-06-21 13:36:11 +0200 | [diff] [blame] | 3700 | case RAM_SAVE_FLAG_MULTIFD_FLUSH: |
| 3701 | multifd_recv_sync_main(); |
| 3702 | break; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3703 | case RAM_SAVE_FLAG_EOS: |
| 3704 | /* normal exit */ |
Juan Quintela | d4f3448 | 2023-10-11 22:55:48 +0200 | [diff] [blame] | 3705 | if (migrate_multifd() && |
| 3706 | migrate_multifd_flush_after_each_section()) { |
Juan Quintela | b05292c | 2022-06-21 12:21:32 +0200 | [diff] [blame] | 3707 | multifd_recv_sync_main(); |
| 3708 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3709 | break; |
| 3710 | default: |
Bihong Yu | 29fccad | 2020-10-20 11:10:42 +0800 | [diff] [blame] | 3711 | error_report("Unknown combination of migration flags: 0x%x" |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3712 | " (postcopy mode)", flags); |
| 3713 | ret = -EINVAL; |
Peter Xu | 7a9ddfb | 2018-02-08 18:31:05 +0800 | [diff] [blame] | 3714 | break; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3717 | /* Got the whole host page, wait for decompress before placing. */ |
| 3718 | if (place_needed) { |
| 3719 | ret |= wait_for_decompress_done(); |
| 3720 | } |
| 3721 | |
Peter Xu | 7a9ddfb | 2018-02-08 18:31:05 +0800 | [diff] [blame] | 3722 | /* Detect for any possible file errors */ |
| 3723 | if (!ret && qemu_file_get_error(f)) { |
| 3724 | ret = qemu_file_get_error(f); |
| 3725 | } |
| 3726 | |
| 3727 | if (!ret && place_needed) { |
Peter Xu | 77dadc3 | 2022-03-01 16:39:04 +0800 | [diff] [blame] | 3728 | if (tmp_page->all_zero) { |
| 3729 | ret = postcopy_place_page_zero(mis, tmp_page->host_addr, block); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3730 | } else { |
Peter Xu | 77dadc3 | 2022-03-01 16:39:04 +0800 | [diff] [blame] | 3731 | ret = postcopy_place_page(mis, tmp_page->host_addr, |
| 3732 | place_source, block); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3733 | } |
David Hildenbrand | ddf35bd | 2020-04-21 10:52:56 +0200 | [diff] [blame] | 3734 | place_needed = false; |
Peter Xu | 77dadc3 | 2022-03-01 16:39:04 +0800 | [diff] [blame] | 3735 | postcopy_temp_page_reset(tmp_page); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3736 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | return ret; |
| 3740 | } |
| 3741 | |
Daniel Henrique Barboza | acab30b | 2017-11-16 20:35:26 -0200 | [diff] [blame] | 3742 | static bool postcopy_is_running(void) |
| 3743 | { |
| 3744 | PostcopyState ps = postcopy_state_get(); |
| 3745 | return ps >= POSTCOPY_INCOMING_LISTENING && ps < POSTCOPY_INCOMING_END; |
| 3746 | } |
| 3747 | |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3748 | /* |
| 3749 | * Flush content of RAM cache into SVM's memory. |
| 3750 | * Only flush the pages that be dirtied by PVM or SVM or both. |
| 3751 | */ |
Lukas Straub | 24fa16f | 2020-05-11 13:10:51 +0200 | [diff] [blame] | 3752 | void colo_flush_ram_cache(void) |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3753 | { |
| 3754 | RAMBlock *block = NULL; |
| 3755 | void *dst_host; |
| 3756 | void *src_host; |
| 3757 | unsigned long offset = 0; |
| 3758 | |
Gavin Shan | 1e493be | 2023-05-09 12:21:19 +1000 | [diff] [blame] | 3759 | memory_global_dirty_log_sync(false); |
Lukas Straub | 9d63840 | 2023-05-08 21:10:55 +0200 | [diff] [blame] | 3760 | qemu_mutex_lock(&ram_state->bitmap_mutex); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3761 | WITH_RCU_READ_LOCK_GUARD() { |
| 3762 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3763 | ramblock_sync_dirty_bitmap(ram_state, block); |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3764 | } |
| 3765 | } |
| 3766 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3767 | trace_colo_flush_ram_cache_begin(ram_state->migration_dirty_pages); |
| 3768 | WITH_RCU_READ_LOCK_GUARD() { |
| 3769 | block = QLIST_FIRST_RCU(&ram_list.blocks); |
| 3770 | |
| 3771 | while (block) { |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 3772 | unsigned long num = 0; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3773 | |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 3774 | offset = colo_bitmap_find_dirty(ram_state, block, offset, &num); |
David Hildenbrand | 542147f | 2021-04-29 13:27:08 +0200 | [diff] [blame] | 3775 | if (!offset_in_ramblock(block, |
| 3776 | ((ram_addr_t)offset) << TARGET_PAGE_BITS)) { |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3777 | offset = 0; |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 3778 | num = 0; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3779 | block = QLIST_NEXT_RCU(block, next); |
| 3780 | } else { |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 3781 | unsigned long i = 0; |
| 3782 | |
| 3783 | for (i = 0; i < num; i++) { |
| 3784 | migration_bitmap_clear_dirty(ram_state, block, offset + i); |
| 3785 | } |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 3786 | dst_host = block->host |
| 3787 | + (((ram_addr_t)offset) << TARGET_PAGE_BITS); |
| 3788 | src_host = block->colo_cache |
| 3789 | + (((ram_addr_t)offset) << TARGET_PAGE_BITS); |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 3790 | memcpy(dst_host, src_host, TARGET_PAGE_SIZE * num); |
| 3791 | offset += num; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3792 | } |
| 3793 | } |
| 3794 | } |
Lukas Straub | 9d63840 | 2023-05-08 21:10:55 +0200 | [diff] [blame] | 3795 | qemu_mutex_unlock(&ram_state->bitmap_mutex); |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3796 | trace_colo_flush_ram_cache_end(); |
| 3797 | } |
| 3798 | |
Nikolay Borisov | 2f5ced5 | 2023-10-11 15:45:59 -0300 | [diff] [blame] | 3799 | static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length) |
| 3800 | { |
| 3801 | int ret = 0; |
| 3802 | /* ADVISE is earlier, it shows the source has the postcopy capability on */ |
| 3803 | bool postcopy_advised = migration_incoming_postcopy_advised(); |
| 3804 | |
| 3805 | assert(block); |
| 3806 | |
| 3807 | if (!qemu_ram_is_migratable(block)) { |
| 3808 | error_report("block %s should not be migrated !", block->idstr); |
| 3809 | return -EINVAL; |
| 3810 | } |
| 3811 | |
| 3812 | if (length != block->used_length) { |
| 3813 | Error *local_err = NULL; |
| 3814 | |
| 3815 | ret = qemu_ram_resize(block, length, &local_err); |
| 3816 | if (local_err) { |
| 3817 | error_report_err(local_err); |
Peter Xu | 2c36076 | 2023-10-17 16:38:55 -0400 | [diff] [blame] | 3818 | return ret; |
Nikolay Borisov | 2f5ced5 | 2023-10-11 15:45:59 -0300 | [diff] [blame] | 3819 | } |
| 3820 | } |
| 3821 | /* For postcopy we need to check hugepage sizes match */ |
| 3822 | if (postcopy_advised && migrate_postcopy_ram() && |
| 3823 | block->page_size != qemu_host_page_size) { |
| 3824 | uint64_t remote_page_size = qemu_get_be64(f); |
| 3825 | if (remote_page_size != block->page_size) { |
| 3826 | error_report("Mismatched RAM page size %s " |
| 3827 | "(local) %zd != %" PRId64, block->idstr, |
| 3828 | block->page_size, remote_page_size); |
Peter Xu | 2c36076 | 2023-10-17 16:38:55 -0400 | [diff] [blame] | 3829 | return -EINVAL; |
Nikolay Borisov | 2f5ced5 | 2023-10-11 15:45:59 -0300 | [diff] [blame] | 3830 | } |
| 3831 | } |
| 3832 | if (migrate_ignore_shared()) { |
| 3833 | hwaddr addr = qemu_get_be64(f); |
| 3834 | if (migrate_ram_is_ignored(block) && |
| 3835 | block->mr->addr != addr) { |
| 3836 | error_report("Mismatched GPAs for block %s " |
| 3837 | "%" PRId64 "!= %" PRId64, block->idstr, |
| 3838 | (uint64_t)addr, (uint64_t)block->mr->addr); |
Peter Xu | 2c36076 | 2023-10-17 16:38:55 -0400 | [diff] [blame] | 3839 | return -EINVAL; |
Nikolay Borisov | 2f5ced5 | 2023-10-11 15:45:59 -0300 | [diff] [blame] | 3840 | } |
| 3841 | } |
| 3842 | ret = rdma_block_notification_handle(f, block->idstr); |
| 3843 | if (ret < 0) { |
| 3844 | qemu_file_set_error(f, ret); |
| 3845 | } |
| 3846 | |
| 3847 | return ret; |
| 3848 | } |
| 3849 | |
| 3850 | static int parse_ramblocks(QEMUFile *f, ram_addr_t total_ram_bytes) |
| 3851 | { |
| 3852 | int ret = 0; |
| 3853 | |
| 3854 | /* Synchronize RAM block list */ |
| 3855 | while (!ret && total_ram_bytes) { |
| 3856 | RAMBlock *block; |
| 3857 | char id[256]; |
| 3858 | ram_addr_t length; |
| 3859 | int len = qemu_get_byte(f); |
| 3860 | |
| 3861 | qemu_get_buffer(f, (uint8_t *)id, len); |
| 3862 | id[len] = 0; |
| 3863 | length = qemu_get_be64(f); |
| 3864 | |
| 3865 | block = qemu_ram_block_by_name(id); |
| 3866 | if (block) { |
| 3867 | ret = parse_ramblock(f, block, length); |
| 3868 | } else { |
| 3869 | error_report("Unknown ramblock \"%s\", cannot accept " |
| 3870 | "migration", id); |
| 3871 | ret = -EINVAL; |
| 3872 | } |
| 3873 | total_ram_bytes -= length; |
| 3874 | } |
| 3875 | |
| 3876 | return ret; |
| 3877 | } |
| 3878 | |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 3879 | /** |
| 3880 | * ram_load_precopy: load pages in precopy case |
| 3881 | * |
| 3882 | * Returns 0 for success or -errno in case of error |
| 3883 | * |
| 3884 | * Called in precopy mode by ram_load(). |
| 3885 | * rcu_read_lock is taken prior to this being called. |
| 3886 | * |
| 3887 | * @f: QEMUFile where to send the data |
| 3888 | */ |
| 3889 | static int ram_load_precopy(QEMUFile *f) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3890 | { |
Peter Xu | 755e8d7 | 2022-03-01 16:39:07 +0800 | [diff] [blame] | 3891 | MigrationIncomingState *mis = migration_incoming_get_current(); |
Yury Kotov | e65cec5 | 2019-11-25 16:36:32 +0300 | [diff] [blame] | 3892 | int flags = 0, ret = 0, invalid_flags = 0, len = 0, i = 0; |
Nikolay Borisov | 2f5ced5 | 2023-10-11 15:45:59 -0300 | [diff] [blame] | 3893 | |
Juan Quintela | a7a94d1 | 2023-03-01 22:03:48 +0100 | [diff] [blame] | 3894 | if (!migrate_compress()) { |
Juan Quintela | edc6012 | 2016-11-02 12:40:46 +0100 | [diff] [blame] | 3895 | invalid_flags |= RAM_SAVE_FLAG_COMPRESS_PAGE; |
| 3896 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3897 | |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 3898 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { |
Nikolay Borisov | 2f5ced5 | 2023-10-11 15:45:59 -0300 | [diff] [blame] | 3899 | ram_addr_t addr; |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3900 | void *host = NULL, *host_bak = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3901 | uint8_t ch; |
| 3902 | |
Yury Kotov | e65cec5 | 2019-11-25 16:36:32 +0300 | [diff] [blame] | 3903 | /* |
| 3904 | * Yield periodically to let main loop run, but an iteration of |
| 3905 | * the main loop is expensive, so do it each some iterations |
| 3906 | */ |
| 3907 | if ((i & 32767) == 0 && qemu_in_coroutine()) { |
| 3908 | aio_co_schedule(qemu_get_current_aio_context(), |
| 3909 | qemu_coroutine_self()); |
| 3910 | qemu_coroutine_yield(); |
| 3911 | } |
| 3912 | i++; |
| 3913 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3914 | addr = qemu_get_be64(f); |
| 3915 | flags = addr & ~TARGET_PAGE_MASK; |
| 3916 | addr &= TARGET_PAGE_MASK; |
| 3917 | |
Juan Quintela | edc6012 | 2016-11-02 12:40:46 +0100 | [diff] [blame] | 3918 | if (flags & invalid_flags) { |
| 3919 | if (flags & invalid_flags & RAM_SAVE_FLAG_COMPRESS_PAGE) { |
| 3920 | error_report("Received an unexpected compressed page"); |
| 3921 | } |
| 3922 | |
| 3923 | ret = -EINVAL; |
| 3924 | break; |
| 3925 | } |
| 3926 | |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 3927 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3928 | RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) { |
Peter Xu | c01b16e | 2022-07-07 14:55:04 -0400 | [diff] [blame] | 3929 | RAMBlock *block = ram_block_from_stream(mis, f, flags, |
| 3930 | RAM_CHANNEL_PRECOPY); |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3931 | |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3932 | host = host_from_ram_block_offset(block, addr); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3933 | /* |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3934 | * After going into COLO stage, we should not load the page |
| 3935 | * into SVM's memory directly, we put them into colo_cache firstly. |
| 3936 | * NOTE: We need to keep a copy of SVM's ram in colo_cache. |
| 3937 | * Previously, we copied all these memory in preparing stage of COLO |
| 3938 | * while we need to stop VM, which is a time-consuming process. |
| 3939 | * Here we optimize it by a trick, back-up every page while in |
| 3940 | * migration process while COLO is enabled, though it affects the |
| 3941 | * speed of the migration, but it obviously reduce the downtime of |
| 3942 | * back-up all SVM'S memory in COLO preparing stage. |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3943 | */ |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3944 | if (migration_incoming_colo_enabled()) { |
| 3945 | if (migration_incoming_in_colo_state()) { |
| 3946 | /* In COLO stage, put all pages into cache temporarily */ |
zhanghailiang | 8af6637 | 2020-02-24 14:54:11 +0800 | [diff] [blame] | 3947 | host = colo_cache_from_block_offset(block, addr, true); |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3948 | } else { |
| 3949 | /* |
| 3950 | * In migration stage but before COLO stage, |
| 3951 | * Put all pages into both cache and SVM's memory. |
| 3952 | */ |
zhanghailiang | 8af6637 | 2020-02-24 14:54:11 +0800 | [diff] [blame] | 3953 | host_bak = colo_cache_from_block_offset(block, addr, false); |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3954 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3955 | } |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3956 | if (!host) { |
| 3957 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
| 3958 | ret = -EINVAL; |
| 3959 | break; |
| 3960 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3961 | if (!migration_incoming_in_colo_state()) { |
| 3962 | ramblock_recv_bitmap_set(block, host); |
| 3963 | } |
| 3964 | |
Dr. David Alan Gilbert | 1db9d8e | 2017-04-26 19:37:21 +0100 | [diff] [blame] | 3965 | trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host); |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3968 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { |
| 3969 | case RAM_SAVE_FLAG_MEM_SIZE: |
Nikolay Borisov | 2f5ced5 | 2023-10-11 15:45:59 -0300 | [diff] [blame] | 3970 | ret = parse_ramblocks(f, addr); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3971 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3972 | |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 3973 | case RAM_SAVE_FLAG_ZERO: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3974 | ch = qemu_get_byte(f); |
Juan Quintela | 413d64f | 2023-10-19 10:52:58 +0200 | [diff] [blame] | 3975 | if (ch != 0) { |
| 3976 | error_report("Found a zero page with value %d", ch); |
| 3977 | ret = -EINVAL; |
| 3978 | break; |
| 3979 | } |
Juan Quintela | 7091dab | 2023-10-19 10:52:59 +0200 | [diff] [blame] | 3980 | ram_handle_zero(host, TARGET_PAGE_SIZE); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3981 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3982 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3983 | case RAM_SAVE_FLAG_PAGE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3984 | qemu_get_buffer(f, host, TARGET_PAGE_SIZE); |
| 3985 | break; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3986 | |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3987 | case RAM_SAVE_FLAG_COMPRESS_PAGE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3988 | len = qemu_get_be32(f); |
| 3989 | if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) { |
| 3990 | error_report("Invalid compressed data length: %d", len); |
| 3991 | ret = -EINVAL; |
| 3992 | break; |
| 3993 | } |
Dr. David Alan Gilbert | c1bc662 | 2015-12-16 11:47:38 +0000 | [diff] [blame] | 3994 | decompress_data_with_multi_threads(f, host, len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3995 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3996 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3997 | case RAM_SAVE_FLAG_XBZRLE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3998 | if (load_xbzrle(f, addr, host) < 0) { |
| 3999 | error_report("Failed to decompress XBZRLE page at " |
| 4000 | RAM_ADDR_FMT, addr); |
| 4001 | ret = -EINVAL; |
| 4002 | break; |
| 4003 | } |
| 4004 | break; |
Juan Quintela | 294e5a4 | 2022-06-21 13:36:11 +0200 | [diff] [blame] | 4005 | case RAM_SAVE_FLAG_MULTIFD_FLUSH: |
| 4006 | multifd_recv_sync_main(); |
| 4007 | break; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4008 | case RAM_SAVE_FLAG_EOS: |
| 4009 | /* normal exit */ |
Juan Quintela | d4f3448 | 2023-10-11 22:55:48 +0200 | [diff] [blame] | 4010 | if (migrate_multifd() && |
| 4011 | migrate_multifd_flush_after_each_section()) { |
Juan Quintela | b05292c | 2022-06-21 12:21:32 +0200 | [diff] [blame] | 4012 | multifd_recv_sync_main(); |
| 4013 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4014 | break; |
Juan Quintela | 5f1e754 | 2023-05-04 13:44:39 +0200 | [diff] [blame] | 4015 | case RAM_SAVE_FLAG_HOOK: |
Juan Quintela | b1b3838 | 2023-10-11 22:35:23 +0200 | [diff] [blame] | 4016 | ret = rdma_registration_handle(f); |
Juan Quintela | f6d6c08 | 2023-10-11 22:35:18 +0200 | [diff] [blame] | 4017 | if (ret < 0) { |
| 4018 | qemu_file_set_error(f, ret); |
| 4019 | } |
Juan Quintela | 5f1e754 | 2023-05-04 13:44:39 +0200 | [diff] [blame] | 4020 | break; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4021 | default: |
Juan Quintela | 5f1e754 | 2023-05-04 13:44:39 +0200 | [diff] [blame] | 4022 | error_report("Unknown combination of migration flags: 0x%x", flags); |
| 4023 | ret = -EINVAL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4024 | } |
| 4025 | if (!ret) { |
| 4026 | ret = qemu_file_get_error(f); |
| 4027 | } |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 4028 | if (!ret && host_bak) { |
| 4029 | memcpy(host_bak, host, TARGET_PAGE_SIZE); |
| 4030 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4031 | } |
| 4032 | |
Wei Yang | ca1a6b7 | 2019-11-07 20:39:03 +0800 | [diff] [blame] | 4033 | ret |= wait_for_decompress_done(); |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 4034 | return ret; |
| 4035 | } |
| 4036 | |
| 4037 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 4038 | { |
| 4039 | int ret = 0; |
| 4040 | static uint64_t seq_iter; |
| 4041 | /* |
| 4042 | * If system is running in postcopy mode, page inserts to host memory must |
| 4043 | * be atomic |
| 4044 | */ |
| 4045 | bool postcopy_running = postcopy_is_running(); |
| 4046 | |
| 4047 | seq_iter++; |
| 4048 | |
| 4049 | if (version_id != 4) { |
| 4050 | return -EINVAL; |
| 4051 | } |
| 4052 | |
| 4053 | /* |
| 4054 | * This RCU critical section can be very long running. |
| 4055 | * When RCU reclaims in the code start to become numerous, |
| 4056 | * it will be necessary to reduce the granularity of this |
| 4057 | * critical section. |
| 4058 | */ |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 4059 | WITH_RCU_READ_LOCK_GUARD() { |
| 4060 | if (postcopy_running) { |
Peter Xu | 36f62f1 | 2022-07-07 14:55:02 -0400 | [diff] [blame] | 4061 | /* |
| 4062 | * Note! Here RAM_CHANNEL_PRECOPY is the precopy channel of |
| 4063 | * postcopy migration, we have another RAM_CHANNEL_POSTCOPY to |
| 4064 | * service fast page faults. |
| 4065 | */ |
| 4066 | ret = ram_load_postcopy(f, RAM_CHANNEL_PRECOPY); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 4067 | } else { |
| 4068 | ret = ram_load_precopy(f); |
| 4069 | } |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 4070 | } |
Juan Quintela | 55c4446 | 2017-01-23 22:32:05 +0100 | [diff] [blame] | 4071 | trace_ram_load_complete(ret, seq_iter); |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 4072 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4073 | return ret; |
| 4074 | } |
| 4075 | |
Vladimir Sementsov-Ogievskiy | c646762 | 2017-07-10 19:30:14 +0300 | [diff] [blame] | 4076 | static bool ram_has_postcopy(void *opaque) |
| 4077 | { |
Junyan He | 469dd51 | 2018-07-18 15:48:02 +0800 | [diff] [blame] | 4078 | RAMBlock *rb; |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 4079 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
Junyan He | 469dd51 | 2018-07-18 15:48:02 +0800 | [diff] [blame] | 4080 | if (ramblock_is_pmem(rb)) { |
| 4081 | info_report("Block: %s, host: %p is a nvdimm memory, postcopy" |
| 4082 | "is not supported now!", rb->idstr, rb->host); |
| 4083 | return false; |
| 4084 | } |
| 4085 | } |
| 4086 | |
Vladimir Sementsov-Ogievskiy | c646762 | 2017-07-10 19:30:14 +0300 | [diff] [blame] | 4087 | return migrate_postcopy_ram(); |
| 4088 | } |
| 4089 | |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4090 | /* Sync all the dirty bitmap with destination VM. */ |
| 4091 | static int ram_dirty_bitmap_sync_all(MigrationState *s, RAMState *rs) |
| 4092 | { |
| 4093 | RAMBlock *block; |
| 4094 | QEMUFile *file = s->to_dst_file; |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4095 | |
| 4096 | trace_ram_dirty_bitmap_sync_start(); |
| 4097 | |
Peter Xu | 1015ff5 | 2023-10-04 18:02:36 -0400 | [diff] [blame] | 4098 | qatomic_set(&rs->postcopy_bmap_sync_requested, 0); |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 4099 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4100 | qemu_savevm_send_recv_bitmap(file, block->idstr); |
| 4101 | trace_ram_dirty_bitmap_request(block->idstr); |
Peter Xu | 1015ff5 | 2023-10-04 18:02:36 -0400 | [diff] [blame] | 4102 | qatomic_inc(&rs->postcopy_bmap_sync_requested); |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | trace_ram_dirty_bitmap_sync_wait(); |
| 4106 | |
| 4107 | /* Wait until all the ramblocks' dirty bitmap synced */ |
Peter Xu | 1015ff5 | 2023-10-04 18:02:36 -0400 | [diff] [blame] | 4108 | while (qatomic_read(&rs->postcopy_bmap_sync_requested)) { |
Peter Xu | 5e79a4b | 2023-10-04 18:02:37 -0400 | [diff] [blame] | 4109 | migration_rp_wait(s); |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | trace_ram_dirty_bitmap_sync_complete(); |
| 4113 | |
| 4114 | return 0; |
| 4115 | } |
| 4116 | |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4117 | /* |
| 4118 | * Read the received bitmap, revert it as the initial dirty bitmap. |
| 4119 | * This is only used when the postcopy migration is paused but wants |
| 4120 | * to resume from a middle point. |
| 4121 | */ |
| 4122 | int ram_dirty_bitmap_reload(MigrationState *s, RAMBlock *block) |
| 4123 | { |
| 4124 | int ret = -EINVAL; |
Peter Xu | 43044ac | 2021-07-22 13:58:38 -0400 | [diff] [blame] | 4125 | /* from_dst_file is always valid because we're within rp_thread */ |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4126 | QEMUFile *file = s->rp_state.from_dst_file; |
Philippe Mathieu-Daudé | 1a36e4c | 2023-10-11 04:36:27 +0200 | [diff] [blame] | 4127 | g_autofree unsigned long *le_bitmap = NULL; |
| 4128 | unsigned long nbits = block->used_length >> TARGET_PAGE_BITS; |
Peter Xu | a725ef9 | 2018-07-10 17:18:55 +0800 | [diff] [blame] | 4129 | uint64_t local_size = DIV_ROUND_UP(nbits, 8); |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4130 | uint64_t size, end_mark; |
Peter Xu | 1015ff5 | 2023-10-04 18:02:36 -0400 | [diff] [blame] | 4131 | RAMState *rs = ram_state; |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4132 | |
| 4133 | trace_ram_dirty_bitmap_reload_begin(block->idstr); |
| 4134 | |
| 4135 | if (s->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { |
| 4136 | error_report("%s: incorrect state %s", __func__, |
| 4137 | MigrationStatus_str(s->state)); |
| 4138 | return -EINVAL; |
| 4139 | } |
| 4140 | |
| 4141 | /* |
| 4142 | * Note: see comments in ramblock_recv_bitmap_send() on why we |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 4143 | * need the endianness conversion, and the paddings. |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4144 | */ |
| 4145 | local_size = ROUND_UP(local_size, 8); |
| 4146 | |
| 4147 | /* Add paddings */ |
| 4148 | le_bitmap = bitmap_new(nbits + BITS_PER_LONG); |
| 4149 | |
| 4150 | size = qemu_get_be64(file); |
| 4151 | |
| 4152 | /* The size of the bitmap should match with our ramblock */ |
| 4153 | if (size != local_size) { |
| 4154 | error_report("%s: ramblock '%s' bitmap size mismatch " |
| 4155 | "(0x%"PRIx64" != 0x%"PRIx64")", __func__, |
| 4156 | block->idstr, size, local_size); |
Philippe Mathieu-Daudé | 1a36e4c | 2023-10-11 04:36:27 +0200 | [diff] [blame] | 4157 | return -EINVAL; |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | size = qemu_get_buffer(file, (uint8_t *)le_bitmap, local_size); |
| 4161 | end_mark = qemu_get_be64(file); |
| 4162 | |
| 4163 | ret = qemu_file_get_error(file); |
| 4164 | if (ret || size != local_size) { |
| 4165 | error_report("%s: read bitmap failed for ramblock '%s': %d" |
| 4166 | " (size 0x%"PRIx64", got: 0x%"PRIx64")", |
| 4167 | __func__, block->idstr, ret, local_size, size); |
Philippe Mathieu-Daudé | 1a36e4c | 2023-10-11 04:36:27 +0200 | [diff] [blame] | 4168 | return -EIO; |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4169 | } |
| 4170 | |
| 4171 | if (end_mark != RAMBLOCK_RECV_BITMAP_ENDING) { |
Philippe Mathieu-Daudé | af3bbbe | 2020-11-03 12:25:58 +0100 | [diff] [blame] | 4172 | error_report("%s: ramblock '%s' end mark incorrect: 0x%"PRIx64, |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4173 | __func__, block->idstr, end_mark); |
Philippe Mathieu-Daudé | 1a36e4c | 2023-10-11 04:36:27 +0200 | [diff] [blame] | 4174 | return -EINVAL; |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | /* |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 4178 | * Endianness conversion. We are during postcopy (though paused). |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4179 | * The dirty bitmap won't change. We can directly modify it. |
| 4180 | */ |
| 4181 | bitmap_from_le(block->bmap, le_bitmap, nbits); |
| 4182 | |
| 4183 | /* |
| 4184 | * What we received is "received bitmap". Revert it as the initial |
| 4185 | * dirty bitmap for this ramblock. |
| 4186 | */ |
| 4187 | bitmap_complement(block->bmap, block->bmap, nbits); |
| 4188 | |
David Hildenbrand | be39b4c | 2021-10-11 19:53:41 +0200 | [diff] [blame] | 4189 | /* Clear dirty bits of discarded ranges that we don't want to migrate. */ |
| 4190 | ramblock_dirty_bitmap_clear_discarded_pages(block); |
| 4191 | |
| 4192 | /* We'll recalculate migration_dirty_pages in ram_state_resume_prepare(). */ |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4193 | trace_ram_dirty_bitmap_reload_complete(block->idstr); |
| 4194 | |
Peter Xu | 1015ff5 | 2023-10-04 18:02:36 -0400 | [diff] [blame] | 4195 | qatomic_dec(&rs->postcopy_bmap_sync_requested); |
| 4196 | |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4197 | /* |
Peter Xu | 5e79a4b | 2023-10-04 18:02:37 -0400 | [diff] [blame] | 4198 | * We succeeded to sync bitmap for current ramblock. Always kick the |
| 4199 | * migration thread to check whether all requested bitmaps are |
| 4200 | * reloaded. NOTE: it's racy to only kick when requested==0, because |
| 4201 | * we don't know whether the migration thread may still be increasing |
| 4202 | * it. |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4203 | */ |
Peter Xu | 5e79a4b | 2023-10-04 18:02:37 -0400 | [diff] [blame] | 4204 | migration_rp_kick(s); |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4205 | |
Philippe Mathieu-Daudé | 1a36e4c | 2023-10-11 04:36:27 +0200 | [diff] [blame] | 4206 | return 0; |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4207 | } |
| 4208 | |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4209 | static int ram_resume_prepare(MigrationState *s, void *opaque) |
| 4210 | { |
| 4211 | RAMState *rs = *(RAMState **)opaque; |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 4212 | int ret; |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4213 | |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 4214 | ret = ram_dirty_bitmap_sync_all(s, rs); |
| 4215 | if (ret) { |
| 4216 | return ret; |
| 4217 | } |
| 4218 | |
| 4219 | ram_state_resume_prepare(rs, s->to_dst_file); |
| 4220 | |
| 4221 | return 0; |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4222 | } |
| 4223 | |
Peter Xu | 36f62f1 | 2022-07-07 14:55:02 -0400 | [diff] [blame] | 4224 | void postcopy_preempt_shutdown_file(MigrationState *s) |
| 4225 | { |
| 4226 | qemu_put_be64(s->postcopy_qemufile_src, RAM_SAVE_FLAG_EOS); |
| 4227 | qemu_fflush(s->postcopy_qemufile_src); |
| 4228 | } |
| 4229 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4230 | static SaveVMHandlers savevm_ram_handlers = { |
Juan Quintela | 9907e84 | 2017-06-28 11:52:24 +0200 | [diff] [blame] | 4231 | .save_setup = ram_save_setup, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4232 | .save_live_iterate = ram_save_iterate, |
Dr. David Alan Gilbert | 763c906 | 2015-11-05 18:11:00 +0000 | [diff] [blame] | 4233 | .save_live_complete_postcopy = ram_save_complete, |
Dr. David Alan Gilbert | a3e06c3 | 2015-11-05 18:10:41 +0000 | [diff] [blame] | 4234 | .save_live_complete_precopy = ram_save_complete, |
Vladimir Sementsov-Ogievskiy | c646762 | 2017-07-10 19:30:14 +0300 | [diff] [blame] | 4235 | .has_postcopy = ram_has_postcopy, |
Juan Quintela | c8df4a7 | 2022-10-03 02:00:03 +0200 | [diff] [blame] | 4236 | .state_pending_exact = ram_state_pending_exact, |
| 4237 | .state_pending_estimate = ram_state_pending_estimate, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4238 | .load_state = ram_load, |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 4239 | .save_cleanup = ram_save_cleanup, |
| 4240 | .load_setup = ram_load_setup, |
| 4241 | .load_cleanup = ram_load_cleanup, |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4242 | .resume_prepare = ram_resume_prepare, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4243 | }; |
| 4244 | |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4245 | static void ram_mig_ram_block_resized(RAMBlockNotifier *n, void *host, |
| 4246 | size_t old_size, size_t new_size) |
| 4247 | { |
David Hildenbrand | cc61c70 | 2021-04-29 13:27:04 +0200 | [diff] [blame] | 4248 | PostcopyState ps = postcopy_state_get(); |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4249 | ram_addr_t offset; |
| 4250 | RAMBlock *rb = qemu_ram_block_from_host(host, false, &offset); |
| 4251 | Error *err = NULL; |
| 4252 | |
Dmitry Frolov | f75ed59 | 2023-10-10 13:48:53 +0300 | [diff] [blame] | 4253 | if (!rb) { |
| 4254 | error_report("RAM block not found"); |
| 4255 | return; |
| 4256 | } |
| 4257 | |
David Hildenbrand | f161c88 | 2023-07-06 09:56:08 +0200 | [diff] [blame] | 4258 | if (migrate_ram_is_ignored(rb)) { |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4259 | return; |
| 4260 | } |
| 4261 | |
| 4262 | if (!migration_is_idle()) { |
| 4263 | /* |
| 4264 | * Precopy code on the source cannot deal with the size of RAM blocks |
| 4265 | * changing at random points in time - especially after sending the |
| 4266 | * RAM block sizes in the migration stream, they must no longer change. |
| 4267 | * Abort and indicate a proper reason. |
| 4268 | */ |
| 4269 | error_setg(&err, "RAM block '%s' resized during precopy.", rb->idstr); |
Laurent Vivier | 458fecc | 2021-09-29 16:43:10 +0200 | [diff] [blame] | 4270 | migration_cancel(err); |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4271 | error_free(err); |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4272 | } |
David Hildenbrand | cc61c70 | 2021-04-29 13:27:04 +0200 | [diff] [blame] | 4273 | |
| 4274 | switch (ps) { |
| 4275 | case POSTCOPY_INCOMING_ADVISE: |
| 4276 | /* |
| 4277 | * Update what ram_postcopy_incoming_init()->init_range() does at the |
| 4278 | * time postcopy was advised. Syncing RAM blocks with the source will |
| 4279 | * result in RAM resizes. |
| 4280 | */ |
| 4281 | if (old_size < new_size) { |
| 4282 | if (ram_discard_range(rb->idstr, old_size, new_size - old_size)) { |
| 4283 | error_report("RAM block '%s' discard of resized RAM failed", |
| 4284 | rb->idstr); |
| 4285 | } |
| 4286 | } |
David Hildenbrand | 898ba90 | 2021-04-29 13:27:06 +0200 | [diff] [blame] | 4287 | rb->postcopy_length = new_size; |
David Hildenbrand | cc61c70 | 2021-04-29 13:27:04 +0200 | [diff] [blame] | 4288 | break; |
| 4289 | case POSTCOPY_INCOMING_NONE: |
| 4290 | case POSTCOPY_INCOMING_RUNNING: |
| 4291 | case POSTCOPY_INCOMING_END: |
| 4292 | /* |
| 4293 | * Once our guest is running, postcopy does no longer care about |
| 4294 | * resizes. When growing, the new memory was not available on the |
| 4295 | * source, no handler needed. |
| 4296 | */ |
| 4297 | break; |
| 4298 | default: |
| 4299 | error_report("RAM block '%s' resized during postcopy state: %d", |
| 4300 | rb->idstr, ps); |
| 4301 | exit(-1); |
| 4302 | } |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | static RAMBlockNotifier ram_mig_ram_notifier = { |
| 4306 | .ram_block_resized = ram_mig_ram_block_resized, |
| 4307 | }; |
| 4308 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4309 | void ram_mig_init(void) |
| 4310 | { |
| 4311 | qemu_mutex_init(&XBZRLE.lock); |
Dr. David Alan Gilbert | ce62df5 | 2019-08-22 12:54:33 +0100 | [diff] [blame] | 4312 | register_savevm_live("ram", 0, 4, &savevm_ram_handlers, &ram_state); |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4313 | ram_block_notifier_add(&ram_mig_ram_notifier); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4314 | } |