| 1 | /* |
| 2 | * QEMU Hyper-V Dynamic Memory Protocol driver |
| 3 | * |
| 4 | * Copyright (C) 2020-2023 Oracle and/or its affiliates. |
| 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 7 | * See the COPYING file in the top-level directory. |
| 8 | */ |
| 9 | |
| 10 | #include "qemu/osdep.h" |
| 11 | #include "system/ramblock.h" |
| 12 | #include "hv-balloon-internal.h" |
| 13 | #include "hv-balloon-our_range_memslots.h" |
| 14 | #include "trace.h" |
| 15 | |
| 16 | /* OurRange */ |
| 17 | static void our_range_init(OurRange *our_range, uint64_t start, uint64_t count) |
| 18 | { |
| 19 | assert(count <= UINT64_MAX - start); |
| 20 | our_range->range.start = start; |
| 21 | our_range->range.count = count; |
| 22 | |
| 23 | hvb_page_range_tree_init(&our_range->removed_guest); |
| 24 | hvb_page_range_tree_init(&our_range->removed_both); |
| 25 | |
| 26 | /* mark the whole range as unused but for potential use */ |
| 27 | our_range->added = 0; |
| 28 | our_range->unusable_tail = 0; |
| 29 | } |
| 30 | |
| 31 | static void our_range_destroy(OurRange *our_range) |
| 32 | { |
| 33 | hvb_page_range_tree_destroy(&our_range->removed_guest); |
| 34 | hvb_page_range_tree_destroy(&our_range->removed_both); |
| 35 | } |
| 36 | |
| 37 | void hvb_our_range_clear_removed_trees(OurRange *our_range) |
| 38 | { |
| 39 | hvb_page_range_tree_destroy(&our_range->removed_guest); |
| 40 | hvb_page_range_tree_destroy(&our_range->removed_both); |
| 41 | hvb_page_range_tree_init(&our_range->removed_guest); |
| 42 | hvb_page_range_tree_init(&our_range->removed_both); |
| 43 | } |
| 44 | |
| 45 | void hvb_our_range_mark_added(OurRange *our_range, uint64_t additional_size) |
| 46 | { |
| 47 | assert(additional_size <= UINT64_MAX - our_range->added); |
| 48 | |
| 49 | our_range->added += additional_size; |
| 50 | |
| 51 | assert(our_range->added <= UINT64_MAX - our_range->unusable_tail); |
| 52 | assert(our_range->added + our_range->unusable_tail <= |
| 53 | our_range->range.count); |
| 54 | } |
| 55 | |
| 56 | /* OurRangeMemslots */ |
| 57 | static void our_range_memslots_init_slots(OurRangeMemslots *our_range, |
| 58 | MemoryRegion *backing_mr, |
| 59 | Object *memslot_owner) |
| 60 | { |
| 61 | OurRangeMemslotsSlots *memslots = &our_range->slots; |
| 62 | unsigned int idx; |
| 63 | uint64_t memslot_offset; |
| 64 | |
| 65 | assert(memslots->count > 0); |
| 66 | memslots->slots = g_new0(MemoryRegion, memslots->count); |
| 67 | |
| 68 | /* Initialize our memslots, but don't map them yet. */ |
| 69 | assert(memslots->size_each > 0); |
| 70 | for (idx = 0, memslot_offset = 0; idx < memslots->count; |
| 71 | idx++, memslot_offset += memslots->size_each) { |
| 72 | uint64_t memslot_size; |
| 73 | g_autofree char *name = NULL; |
| 74 | |
| 75 | /* The size of the last memslot might be smaller. */ |
| 76 | if (idx == memslots->count - 1) { |
| 77 | uint64_t region_size; |
| 78 | |
| 79 | assert(our_range->mr); |
| 80 | region_size = memory_region_size(our_range->mr); |
| 81 | memslot_size = region_size - memslot_offset; |
| 82 | } else { |
| 83 | memslot_size = memslots->size_each; |
| 84 | } |
| 85 | |
| 86 | name = g_strdup_printf("memslot-%u", idx); |
| 87 | memory_region_init_alias(&memslots->slots[idx], memslot_owner, name, |
| 88 | backing_mr, memslot_offset, memslot_size); |
| 89 | /* |
| 90 | * We want to be able to atomically and efficiently activate/deactivate |
| 91 | * individual memslots without affecting adjacent memslots in memory |
| 92 | * notifiers. |
| 93 | */ |
| 94 | memory_region_set_unmergeable(&memslots->slots[idx], true); |
| 95 | } |
| 96 | |
| 97 | memslots->mapped_count = 0; |
| 98 | } |
| 99 | |
| 100 | OurRangeMemslots *hvb_our_range_memslots_new(uint64_t addr, |
| 101 | MemoryRegion *parent_mr, |
| 102 | MemoryRegion *backing_mr, |
| 103 | Object *memslot_owner, |
| 104 | unsigned int memslot_count, |
| 105 | uint64_t memslot_size) |
| 106 | { |
| 107 | OurRangeMemslots *our_range; |
| 108 | |
| 109 | our_range = g_malloc(sizeof(*our_range)); |
| 110 | our_range_init(&our_range->range, |
| 111 | addr / HV_BALLOON_PAGE_SIZE, |
| 112 | memory_region_size(parent_mr) / HV_BALLOON_PAGE_SIZE); |
| 113 | our_range->slots.size_each = memslot_size; |
| 114 | our_range->slots.count = memslot_count; |
| 115 | our_range->mr = parent_mr; |
| 116 | our_range_memslots_init_slots(our_range, backing_mr, memslot_owner); |
| 117 | |
| 118 | return our_range; |
| 119 | } |
| 120 | |
| 121 | static void our_range_memslots_free_memslots(OurRangeMemslots *our_range) |
| 122 | { |
| 123 | OurRangeMemslotsSlots *memslots = &our_range->slots; |
| 124 | unsigned int idx; |
| 125 | uint64_t offset; |
| 126 | |
| 127 | memory_region_transaction_begin(); |
| 128 | for (idx = 0, offset = 0; idx < memslots->mapped_count; |
| 129 | idx++, offset += memslots->size_each) { |
| 130 | trace_hv_balloon_unmap_slot(idx, memslots->count, offset); |
| 131 | assert(memory_region_is_mapped(&memslots->slots[idx])); |
| 132 | memory_region_del_subregion(our_range->mr, &memslots->slots[idx]); |
| 133 | } |
| 134 | memory_region_transaction_commit(); |
| 135 | |
| 136 | for (idx = 0; idx < memslots->count; idx++) { |
| 137 | object_unparent(OBJECT(&memslots->slots[idx])); |
| 138 | } |
| 139 | |
| 140 | g_clear_pointer(&our_range->slots.slots, g_free); |
| 141 | } |
| 142 | |
| 143 | void hvb_our_range_memslots_free(OurRangeMemslots *our_range) |
| 144 | { |
| 145 | OurRangeMemslotsSlots *memslots = &our_range->slots; |
| 146 | MemoryRegion *hostmem_mr; |
| 147 | RAMBlock *rb; |
| 148 | |
| 149 | assert(our_range->slots.count > 0); |
| 150 | assert(our_range->slots.slots); |
| 151 | |
| 152 | hostmem_mr = memslots->slots[0].alias; |
| 153 | rb = hostmem_mr->ram_block; |
| 154 | ram_block_discard_range(rb, 0, qemu_ram_get_used_length(rb)); |
| 155 | |
| 156 | our_range_memslots_free_memslots(our_range); |
| 157 | our_range_destroy(&our_range->range); |
| 158 | g_free(our_range); |
| 159 | } |
| 160 | |
| 161 | void hvb_our_range_memslots_ensure_mapped_additional(OurRangeMemslots *our_range, |
| 162 | uint64_t additional_map_size) |
| 163 | { |
| 164 | OurRangeMemslotsSlots *memslots = &our_range->slots; |
| 165 | uint64_t total_map_size; |
| 166 | unsigned int idx; |
| 167 | uint64_t offset; |
| 168 | |
| 169 | total_map_size = (our_range->range.added + additional_map_size) * |
| 170 | HV_BALLOON_PAGE_SIZE; |
| 171 | idx = memslots->mapped_count; |
| 172 | assert(memslots->size_each > 0); |
| 173 | offset = idx * memslots->size_each; |
| 174 | |
| 175 | /* |
| 176 | * Activate all memslots covered by the newly added region in a single |
| 177 | * transaction. |
| 178 | */ |
| 179 | memory_region_transaction_begin(); |
| 180 | for ( ; idx < memslots->count; |
| 181 | idx++, offset += memslots->size_each) { |
| 182 | /* |
| 183 | * If this memslot starts beyond or at the end of the range to map so |
| 184 | * does every next one. |
| 185 | */ |
| 186 | if (offset >= total_map_size) { |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Instead of enabling/disabling memslot, we add/remove them. This |
| 192 | * should make address space updates faster, because we don't have to |
| 193 | * loop over many disabled subregions. |
| 194 | */ |
| 195 | trace_hv_balloon_map_slot(idx, memslots->count, offset); |
| 196 | assert(!memory_region_is_mapped(&memslots->slots[idx])); |
| 197 | memory_region_add_subregion(our_range->mr, offset, |
| 198 | &memslots->slots[idx]); |
| 199 | |
| 200 | memslots->mapped_count++; |
| 201 | } |
| 202 | memory_region_transaction_commit(); |
| 203 | } |