kvm: Guet_memfd enhancement and fix for KVM_SET_USER_MEMORY_REGION2
cover kvm: Guet_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.
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 mem.guest_memfd_offset only when guest_memfd is valid
accel/kvm/kvm-all.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0
PATCH 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 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.
--- accel/kvm/kvm-all.c | 1 + 1 file changed, 1 insertion(+)
accel/kvm/kvm-all.c +1 -0
@@ -361,6 +361,7 @@ static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, boostruct kvm_userspace_memory_region2 mem;int ret; +memset (&mem,0 ,sizeof (mem)); mem.slot = slot->slot | (kml->as_id <<16 ); mem.guest_phys_addr = slot->start_addr; mem.userspace_addr = (unsigned long )slot->ram;
Signature · 1 line
2.43.0
Re: [PATCH 2/3] accel/kvm: Zero out mem explicitly in kvm_set_user_memory_region()
Quoted 15 lines · Xiaoyao Li
Zero 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.
Fixes ce5a983233b4 ("kvm: Enable KVM_SET_USER_MEMORY_REGION2 for memslot")Signed-off-by Xiaoyao Li <[email protected]>--- accel/kvm/kvm-all.c | 1 + 1 file changed, 1 insertion(+)accel/kvm/kvm-all.c +0
-0
accel/kvm/kvm-all.c static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, boo · line 361
Quoted 1 line
accel/kvm/kvm-all.c +0
-0@@ -361,6 +361,7 @@ static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, boostruct kvm_userspace_memory_region2 mem;
Or:
struct kvm_userspace_memory_region2 mem = { };Anyhow,
Quoted 6 lines
accel/kvm/kvm-all.c +1
-0int ret; +memset (&mem,0 ,sizeof (mem)); mem.slot = slot->slot | (kml->as_id <<16 ); mem.guest_phys_addr = slot->start_addr; mem.userspace_addr = (unsigned long )slot->ram;
PATCH 3/3 accel/kvm: Set mem.guest_memfd_offset only when guest_memfd is valid
In reply to Xiaoyao LiCurrent QEMU unconditionally sets the mmem.guest_memfd_offset in kvm_set_user_memory_region(), which leads to the trace of kvm_set_user_memory look as:
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.
--- accel/kvm/kvm-all.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +3 -1
@@ -367,7 +367,9 @@ static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, boo mem.userspace_addr = (unsigned long )slot->ram; mem.flags = slot->flags; mem.guest_memfd = slot->guest_memfd; - mem.guest_memfd_offset = slot->guest_memfd_offset; +if (slot->guest_memfd >=0 ) { + mem.guest_memfd_offset = slot->guest_memfd_offset; + }if (slot->memory_size && !new && (mem.flags ^ slot->old_flags) &KVM_MEM_READONLY ) {/* Set the slot size to 0 before setting the slot to the desired
Signature · 1 line
2.43.0
Re: [PATCH 3/3] accel/kvm: Set mem.guest_memfd_offset only when guest_memfd is valid
Quoted 21 lines · Xiaoyao Li
Current QEMU unconditionally sets the mmem.guest_memfd_offset in
kvm_set_user_memory_region(), which leads to the trace ofkvm_set_user_memorylook as:
kvm_set_user_memoryAddrSpace#0 Slot#4 flags=0x2 gpa=0xe0000 size=0x20000 ua=0x7f5840de0000 guest_memfd=-1 guest_memfd_offset=0x3e0000 ret=0It's confusing that the
guest_memfd_offsethas a non-zero value while theguest_memfdis invalid (-1).Change to only set
guest_memfd_offsetwhenguest_memfdis valid.Signed-off-by Xiaoyao Li <[email protected]>--- accel/kvm/kvm-all.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)accel/kvm/kvm-all.c +0
-0
accel/kvm/kvm-all.c static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, boo · line 372
Quoted 7 lines
accel/kvm/kvm-all.c +3
-1@@ -367,7 +367,9 @@ static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, boo mem.userspace_addr = (unsigned long )slot->ram; mem.flags = slot->flags; mem.guest_memfd = slot->guest_memfd; - mem.guest_memfd_offset = slot->guest_memfd_offset; +if (slot->guest_memfd >=0 ) { + mem.guest_memfd_offset = slot->guest_memfd_offset; + }
IMHO the issue is in when the KVMSlot is filled in kvm_set_phys_mem():
-- >8 --
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 = mr->ram_block->guest_memfd + ? (uint8_t *)ram - mr->ram_block->host :0 ;kvm_slot_init_dirty_bitmap (mem); err =kvm_set_user_memory_region (kml, mem, true);
---
Re: [PATCH 3/3] accel/kvm: Set mem.guest_memfd_offset only when guest_memfd is valid
Quoted 17 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Xiaoyao Li
Current QEMU unconditionally sets the mmem.guest_memfd_offset in
kvm_set_user_memory_region(), which leads to the trace ofkvm_set_user_memorylook as:
kvm_set_user_memoryAddrSpace#0 Slot#4 flags=0x2 gpa=0xe0000 size=0x20000 ua=0x7f5840de0000 guest_memfd=-1 guest_memfd_offset=0x3e0000 ret=0It's confusing that the
guest_memfd_offsethas a non-zero value while theguest_memfdis invalid (-1).Change to only set
guest_memfd_offsetwhenguest_memfdis valid.Signed-off-by Xiaoyao Li <[email protected]>---
accel/kvm/kvm-all.c| 4 +++-1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines
Quoted 5 lines
accel/kvm/kvm-all.c +0
-0@@ -367,7 +367,9 @@ static intQuoted 8 lines
kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, boo mem.userspace_addr = (unsigned long)slot->ram; mem.flags =
slot->flags;
mem.guest_memfd =slot->guest_memfd;
- mem.guest_memfd_offset =slot->guest_memfd_offset;
+ if (slot->guest_memfd>= 0) {
+ mem.guest_memfd_offset =slot->guest_memfd_offset; + }IMHO the issue is in when the KVMSlot is filled in
kvm_set_phys_mem():
Agreed, fix it in the origin。
accel/kvm/kvm-all.c static void kvm_set_phys_mem(KVMMemoryListener *kml, · line 1599
Quoted 12 lines
-- >8 --
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 = mr->ram_block->guest_memfd + ? (uint8_t*)ram - mr->ram_block->host : 0;
it should be
mr->ram_block->guest_memfd >= 0 ? (uint8_t*)ram - mr->ram_block->host : 0;
I will use the fixed version in v2.
Thanks!
Quoted 3 lines
accel/kvm/kvm-all.c +0
-1kvm_slot_init_dirty_bitmap(mem); err = kvm_set_user_memory_region(kml, mem, true); ---