guest_memfd: Fix handling for conversions of MMIO ranges
PATCH 1/4 accel/kvm: Add helper for handling conversions of MMIO holes
In reply to Michael RothWhen converting GPA ranges containing MMIO holes, the general intended logic is to skip MMIO ranges as long as it is a private->shared conversion, and to fail otherwise.
To better enable some future refactorings to address bugs in the current code, go ahead and consolidate all of the MMIO handling to a self-contained helper.
The only intended functional change is that the error message for shared->private conversion of MMIO ranges corresponding to a non-RAM/non-ROM region now report the range of the region that specifically caused the failure, as opposed to reporting the entire conversion range.
--- accel/kvm/kvm-all.c | 96 ++++++++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 32 deletions(-)
accel/kvm/kvm-all.c +64 -32
@@ -3339,6 +3339,67 @@ static void kvm_eat_signals(CPUState *cpu) }while (sigismember (&chkset,SIG_IPI )); } +/* +* This handles checking whether a conversion overlaps with any holes in +* the given section/range, and indicates whether or not the section/range +* can be safely skipped (which is generally allowed as long as the +* conversion is private->shared and not the other way around). +* +* Returns 0 if section is normal guest-memfd-backed memory, or is a hole or +* non-RAM/ROM region that should be skipped as MMIO. In the latter case, the +* 'skip' parameter will be set. Returns < 0 if the conversion request is not +* valid. +*/ +static int handle_memory_hole (MemoryRegionSection *section,bool to_private, +bool *skip) +{ +MemoryRegion *mr = section->mr; + +if (!mr) { +/* +* TDX requires vMMIO region to be shared to inject #VE to guest. +* OVMF issues conservatively MapGPA(shared) on 32bit PCI MMIO +* region, and vIO-APIC 0xFEC00000 4K page. +* OVMF assigns 32bit PCI MMIO region to +* [top of low memory: typically 2GB=0xC000000, 0xFC00000) +* +* If the current lookup failed to find any regions, then skip the +* entire remaining range as a hole/MMIO. +*/ +if (!to_private) { + *skip = true; +return 0 ; + } +return -EINVAL ; + } + +/* +* Because vMMIO region must be shared, guest TD may convert vMMIO +* region to shared explicitly. Don't complain such case. See +* memory_region_type() for checking if the region is MMIO region. +*/ +if (!memory_region_has_guest_memfd (mr)) { +if (!to_private && + !memory_region_is_ram (mr) && + !memory_region_is_ram_device (mr) && + !memory_region_is_rom (mr) && + !memory_region_is_romd (mr)) { + *skip = true; +return 0 ; + }else { +error_report ("Convert non guest_memfd backed memory region " +"(0x%" HWADDR_PRIx" + 0x%" HWADDR_PRIx") to %s" , + section->offset_within_address_space, +int128_get64 (section->size), + to_private ?"private" :"shared" ); +return -EINVAL ; + } + } + + *skip = false; +return 0 ; +} +int kvm_convert_memory (hwaddr start,hwaddr size,bool to_private) {MemoryRegionSection section;
@@ -3346,6 +3407,7 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)MemoryRegion *mr;RAMBlock *rb;void *addr; +bool skip;int ret = -EINVAL ;trace_kvm_convert_memory (start, size, to_private ?"shared_to_private" :"private_to_shared" );
@@ -3361,39 +3423,9 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private) section =memory_region_find (get_system_memory (), start, size); mr = section.mr; -if (!mr) { -/* -* Ignore converting non-assigned region to shared. -* -* TDX requires vMMIO region to be shared to inject #VE to guest. -* OVMF issues conservatively MapGPA(shared) on 32bit PCI MMIO region, -* and vIO-APIC 0xFEC00000 4K page. -* OVMF assigns 32bit PCI MMIO region to -* [top of low memory: typically 2GB=0xC000000, 0xFC00000) -*/ -if (!to_private) { -return 0 ; - } -return ret; - } -if (!memory_region_has_guest_memfd (mr)) { -/* -* Because vMMIO region must be shared, guest TD may convert vMMIO -* region to shared explicitly. Don't complain such case. See -* memory_region_type() for checking if the region is MMIO region. -*/ -if (!to_private && - !memory_region_is_ram (mr) && - !memory_region_is_ram_device (mr) && - !memory_region_is_rom (mr) && - !memory_region_is_romd (mr)) { - ret =0 ; - }else { -error_report ("Convert non guest_memfd backed memory region " -"(0x%" HWADDR_PRIx" ,+ 0x%" HWADDR_PRIx") to %s" , - start, size, to_private ?"private" :"shared" ); - } + ret =handle_memory_hole (§ion, to_private, &skip); +if (ret || skip) { goto out_unref; }
Signature · 1 line
2.43.0
PATCH 2/4 accel/kvm: Fix kvm_convert_memory() calls crossing memory regions
In reply to Michael RothFrom: Ashish Kalra <[email protected]>
Page conversion calls can span multiple memory regions, potentially resulting in a conversion failure if the memory range being converted extends beyond the boundaries of the referenced memory region.
Handle the case of page conversion calls straddling across memory regions by looping through the subregions and handling conversions and related work section by section.
--- accel/kvm/kvm-all.c | 94 ++++++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 31 deletions(-)
accel/kvm/kvm-all.c +63 -31
@@ -3400,54 +3400,35 @@ static int handle_memory_hole(MemoryRegionSection *section, bool to_private,return 0 ; } -int kvm_convert_memory (hwaddr start,hwaddr size,bool to_private) +static int kvm_convert_section (MemoryRegionSection *section,bool to_private) { -MemoryRegionSection section; +hwaddr start = section->offset_within_address_space; +hwaddr size =int128_get64 (section->size); +MemoryRegion *mr = section->mr;ram_addr_t offset; -MemoryRegion *mr;RAMBlock *rb;void *addr; -bool skip;int ret = -EINVAL ; -trace_kvm_convert_memory (start, size, to_private ?"shared_to_private" :"private_to_shared" ); - -if (!QEMU_PTR_IS_ALIGNED (start,qemu_real_host_page_size ()) || - !QEMU_PTR_IS_ALIGNED (size,qemu_real_host_page_size ())) { -return ret; - } - -if (!size) { -return ret; - } - - section =memory_region_find (get_system_memory (), start, size); - mr = section.mr; - - ret =handle_memory_hole (§ion, to_private, &skip); -if (ret || skip) { - goto out_unref; - } -if (to_private) { ret =kvm_set_memory_attributes_private (start, size); }else { ret =kvm_set_memory_attributes_shared (start, size); }if (ret) { - goto out_unref; +return ret; } - addr =memory_region_get_ram_ptr (mr) + section.offset_within_region; + addr =memory_region_get_ram_ptr (mr) + section->offset_within_region; rb =qemu_ram_block_from_host (addr, false, &offset); ret =ram_block_attributes_state_change (rb->attributes, offset, size, to_private);if (ret) {error_report ("Failed to notify the listener the state change of " -"(0x%" HWADDR_PRIx" + 0x%" HWADDR_PRIx") to %s" , - start, size, to_private ?"private" :"shared" ); - goto out_unref; +"(0x%" HWADDR_PRIx" + 0x%" HWADDR_PRIx") to %s, ret %d" , + start, size, to_private ?"private" :"shared" , ret); +return ret; }if (to_private) {
@@ -3456,15 +3437,66 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)* shared memory is backed by hugetlb, which is supposed to be * pre-allocated and doesn't need to be discarded */ - goto out_unref; +return 0 ; } ret =ram_block_discard_range (rb, offset, size); }else { ret =ram_block_discard_guest_memfd_range (rb, offset, size); } -out_unref: -memory_region_unref (mr); +return ret; +} + +int kvm_convert_memory (hwaddr start,hwaddr size,bool to_private) +{ +int ret = -EINVAL ; + +trace_kvm_convert_memory (start, size, to_private ?"shared_to_private" :"private_to_shared" ); + +if (!QEMU_PTR_IS_ALIGNED (start,qemu_real_host_page_size ()) || + !QEMU_PTR_IS_ALIGNED (size,qemu_real_host_page_size ())) { +return ret; + } + +/* +* Page conversions can span multiple memory regions, for example, if two +* memory backends are added to support two different NUMA nodes/policies. +* Handle the covered sections accordingly. +*/ +while (size) { +MemoryRegionSection section =memory_region_find (get_system_memory (), + start, size); +hwaddr section_end; +bool skip; + +/* +* If there's no region present, then the current hole "section" +* consumes the entire remaining range. In that case, update the +* relevant indices to terminate the loop after this iteration. +*/ + section_end = section.mr + ? section.offset_within_address_space +int128_get64 (section.size) + : start + size; +assert (section_end > start); +assert (section_end - start <= size); + + ret =handle_memory_hole (§ion, to_private, &skip); +if (ret || skip) { +memory_region_unref (section.mr); +break ; + } + + ret =kvm_convert_section (§ion, to_private); +memory_region_unref (section.mr); + +if (ret) { +break ; + } + + size -= section_end - start; + start = section_end; + } +return ret; }
Signature · 1 line
2.43.0
PATCH 3/4 accel/kvm: Fix handling of MMIO holes at start of conversion ranges
In reply to Michael RothCurrently MMIO checks are done for ranges where memory_region_find() finds no regions within the entire range, or for cases where non-RAM/ROM regions at the beginning of the range.
However, if the first region in the range is a normal RAM/ROM region, then the portion of the conversion range that overlaps the region is processed normally, but any MMIO holes that might be present at the beginning are ignored, so the checks are bypassed for those ranges.
Plumb the 'start' GPA that was used to query memory_region_find(), and pass that into the MMIO-processing helper so that these gaps can be detected and MMIO checks can be applied appropriately.
--- accel/kvm/kvm-all.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
accel/kvm/kvm-all.c +16 -2
@@ -3349,9 +3349,14 @@ static void kvm_eat_signals(CPUState *cpu)* non-RAM/ROM region that should be skipped as MMIO. In the latter case, the * 'skip' parameter will be set. Returns < 0 if the conversion request is not * valid. +* +* 'start' corresponds to the starting range memory_region_find() was +* called for, and is used to determine if there are any MMIO holes preceding +* the region passed in so the appropriate checks can be made on those +* ranges. */ static int handle_memory_hole (MemoryRegionSection *section,bool to_private, -bool *skip) +hwaddr start,bool *skip) {MemoryRegion *mr = section->mr;
@@ -3396,6 +3401,15 @@ static int handle_memory_hole(MemoryRegionSection *section, bool to_private, } } +/* +* In this case the region should be processed as normal +* guest_memfd-backed RAM/ROM, but still need to check if there are +* preceding holes to apply the MMIO checks against. +*/ +if (start < section->offset_within_address_space && to_private) { +return -EINVAL ; + } + *skip = false;return 0 ; }
@@ -3480,7 +3494,7 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)assert (section_end > start);assert (section_end - start <= size); - ret =handle_memory_hole (§ion, to_private, &skip); + ret =handle_memory_hole (§ion, to_private, start, &skip);if (ret || skip) {memory_region_unref (section.mr);break ;
Signature · 1 line
2.43.0
PATCH 4/4 accel/kvm: Fix handling of conversion ranges with multiple MMIO holes
In reply to Michael RothThe current handling will properly handle MMIO checks for holes anchored at the beginning of the conversion range, but misses subsequent holes. The current handle_memory_hole() helper already processes all MMIO ranges up to and including the sections found at each iteration, so handle this by simply calling handle_memory_hole() for each additional section/sub-range.
--- accel/kvm/kvm-all.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +6 -1
@@ -3497,7 +3497,12 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private) ret =handle_memory_hole (§ion, to_private, start, &skip);if (ret || skip) {memory_region_unref (section.mr); -break ; +if (ret) { +break ; + } + size -= section_end - start; + start = section_end; +continue ; } ret =kvm_convert_section (§ion, to_private);
Signature · 1 line
2.43.0
cover guest_memfd: Fix handling for conversions of MMIO ranges
This patchset is also available at:
https://github.com/amdese/qemu/commits/gmem-fix-mmio-conversions-v1
and is based on top of qemu master (3f89b5de5b)
Overview
--------
Calls to kvm_convert_memory() currently only produce the intended behavior if the entire range is within an MMIO hole/region, or is completely covered by a guest_memfd-backed region.
In reality this is not the case:
1) some ranges contain multiple regions (e.g. multiple guest NUMA nodes backed by separate guest_memfd backends).
2) some ranges contain multiple MMIO holes (e.g. a range that contains 2 non-adjacent regions, or a single region followed by a hole)
These scenarios bring about multiple unhandled corner cases that this series is meant to address.
The section-by-section processing is also heavily guided by requirements that will come about when enabling in-place conversion support (where memory attribute tracking will move down into each guest_memfd instance and further require region-by-region processing of each range). That will be posted as a follow-up series based on this patchset.
NOTES/TODO
----------
- If TDX folks could test this that would be appreciated as SNP guests don't seem to trigger some of the original cases that were introduced for TDX guests and I only have access to SNP systems atm.
Thanks,
Mike
----------------------------------------------------------------
Ashish Kalra (1):
accel/kvm: Fix kvm_convert_memory() calls crossing memory regions
Michael Roth (3):
accel/kvm: Add helper for handling conversions of MMIO holes accel/kvm: Fix handling of MMIO holes at start of conversion ranges accel/kvm: Fix handling of conversion ranges with multiple MMIO holes
accel/kvm/kvm-all.c | 167 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 125 insertions(+), 42 deletions(-)