kvm: guest_memfd enhancement and fix for KVM_SET_USER_MEMORY_REGION2
v2 cover kvm: guest_memfd enhancement and fix for KVM_SET_USER_MEMORY_REGION2
Patch 1 changes to check guest memfd CAP on VM instance.
Patch 2 is a fix for KVM_SET_USER_MEMORY_REGION2.
Patch 3 is an enhancement for guest_memfd_offset.
---
Changes in v2:
- Collect R-B tags;
- Use {} to zero out the struct instead of memset() in patch2;
- Initialize guest_memfd_offset at the original source in patch3;
Xiaoyao Li (3):
accel/kvm: Switch to check KVM_CAP_GUEST_MEMFD and
KVM_CAP_USER_MEMORY2 on VM
accel/kvm: Zero out mem explicitly in kvm_set_user_memory_region() accel/kvm: Set guest_memfd_offset to non-zero value only when
guest_memfd is valid
accel/kvm/kvm-all.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/3 accel/kvm: Switch to check KVM_CAP_GUEST_MEMFD and KVM_CAP_USER_MEMORY2 on VM
It returns more accruate result on checking KVM_CAP_GUEST_MEMFD and KVM_CAP_USER_MEMORY2 on VM instance instead of on KVM platform.
--- accel/kvm/kvm-all.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
accel/kvm/kvm-all.c +2 -2
@@ -2776,8 +2776,8 @@ static int kvm_init(AccelState *as, MachineState *ms) kvm_supported_memory_attributes =kvm_vm_check_extension (s,KVM_CAP_MEMORY_ATTRIBUTES ); kvm_guest_memfd_supported = -kvm_check_extension (s,KVM_CAP_GUEST_MEMFD ) && -kvm_check_extension (s,KVM_CAP_USER_MEMORY2 ) && +kvm_vm_check_extension (s,KVM_CAP_GUEST_MEMFD ) && +kvm_vm_check_extension (s,KVM_CAP_USER_MEMORY2 ) && (kvm_supported_memory_attributes &KVM_MEMORY_ATTRIBUTE_PRIVATE ); kvm_pre_fault_memory_supported =kvm_vm_check_extension (s,KVM_CAP_PRE_FAULT_MEMORY );
Signature · 1 line
2.43.0
PATCH v2 2/3 accel/kvm: Zero out mem explicitly in kvm_set_user_memory_region()
In reply to Xiaoyao LiZero out the entire mem explicitly before it's used, to ensure the unused feilds (pad1, pad2) are all zeros. Otherwise, it might cause problem when the pad fields are extended by future KVM.
---
changes in v2:
- using {} instead of memset(); (suggested by Philippe)
--- accel/kvm/kvm-all.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
accel/kvm/kvm-all.c +1 -1
@@ -358,7 +358,7 @@ int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,static int kvm_set_user_memory_region (KVMMemoryListener *kml,KVMSlot *slot,bool new) {KVMState *s = kvm_state; -struct kvm_userspace_memory_region2 mem; +struct kvm_userspace_memory_region2 mem = {};int ret; mem.slot = slot->slot | (kml->as_id <<16 );
Signature · 1 line
2.43.0
PATCH v2 3/3 accel/kvm: Set guest_memfd_offset to non-zero value only when guest_memfd is valid
In reply to Xiaoyao LiCurrent QEMU unconditionally sets the guest_memfd_offset of KVMSlot in kvm_set_phys_mem(), which leads to the trace of kvm_set_user_memory looks:
kvm_set_user_memory AddrSpace#0 Slot#4 flags=0x2 gpa=0xe0000 size=0x20000 ua=0x7f5840de0000 guest_memfd=-1 guest_memfd_offset=0x3e0000 ret=0
It's confusing that the guest_memfd_offset has a non-zero value while the guest_memfd is invalid (-1).
Change to only set guest_memfd_offset when guest_memfd is valid and leave it as 0 when no valid guest_memfd.
---
Changes in v2:
- initialize guest_memfd_offset in kvm_set_phys_mem(); (Phillippe)
--- accel/kvm/kvm-all.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +2 -1
@@ -1595,7 +1595,8 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, mem->ram = ram; mem->flags =kvm_mem_flags (mr); mem->guest_memfd = mr->ram_block->guest_memfd; - mem->guest_memfd_offset = (uint8_t *)ram - mr->ram_block->host; + mem->guest_memfd_offset = mem->guest_memfd >=0 ? + (uint8_t *)ram - mr->ram_block->host :0 ;kvm_slot_init_dirty_bitmap (mem); err =kvm_set_user_memory_region (kml, mem, true);
Signature · 1 line
2.43.0
Quoted 5 lines · Xiaoyao Li
Patch 1 changes to check guest memfd CAP on VM instance.
Patch 2 is a fix for
KVM_SET_USER_MEMORY_REGION2.Patch 3 is an enhancement for
guest_memfd_offset.
Gentle ping.
Quoted 16 lines
---
Changes in v2:
- Collect R-B tags;
- Use {} to zero out the struct instead ofmemset()in patch2;
- Initializeguest_memfd_offsetat the original source in patch3;Xiaoyao Li (3):
accel/kvm: Switch to checkKVM_CAP_GUEST_MEMFDand
KVM_CAP_USER_MEMORY2on VM
accel/kvm: Zero out mem explicitly inkvm_set_user_memory_region()accel/kvm: Setguest_memfd_offsetto non-zero value only when
guest_memfdis validaccel/kvm/kvm-all.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
Paolo Bonzini
AppliedQueued, thanks.
Paolo