Fix SEV VM device assignment
cover Fix SEV VM device assignment
Fix: https://bugzilla.redhat.com/show_bug.cgi?id=1667249
Brijesh Singh (2):
memory: Fix the memory region type assignment order target/i386: sev: Do not pin the ram device memory region
memory.c | 9 ++++++++- target/i386/sev.c | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-)
Signature · 1 line
2.17.1
PATCH 1/2 memory: Fix the memory region type assignment order
Currently, a callback registered through the RAMBlock notifier is not able to get the memory region type (i.e callback is not able to use memory_region_is_ram_device function). This is because mr->ram assignment happens _after_ the memory is allocated whereas the callback is executed during allocation.
--- memory.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
memory.c +8 -1
@@ -1652,10 +1652,17 @@ void memory_region_init_ram_device_ptr(MemoryRegion *mr,uint64_t size, void *ptr) { -memory_region_init_ram_ptr (mr ,owner ,name ,size ,ptr ); +memory_region_init (mr ,owner ,name ,size ); + mr->ram = true; + mr->terminates = true; mr->ram_device = true; mr->ops = &ram_device_mem_ops; mr->opaque = mr; + mr->destructor = memory_region_destructor_ram; + mr->dirty_log_mask =tcg_enabled () ? (1 <<DIRTY_MEMORY_CODE ) :0 ; +/* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */ +assert (ptr !=NULL ); + mr->ram_block =qemu_ram_alloc_from_ptr (size, ptr, mr, &error_fatal); } void memory_region_init_alias(MemoryRegion *mr,
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH 1/2] memory: Fix the memory region type assignment order
Quoted 8 lines · Brijesh
Currently, a callback registered through the RAMBlock notifier is not able to get the memory region type (i.e callback is not able to use
memory_region_is_ram_devicefunction). This is becausemr->ramassignment happens _after_ the memory is allocated whereas the callback is executed during allocation.
s/Sugegsted/Suggested/
Quoted 19 lines
Cc Paolo Bonzini <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- memory.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)memory.c +3
-1@@ -1652,10 +1652,17 @@ void memory_region_init_ram_device_ptr(MemoryRegion *mr,uint64_t size, void *ptr) { -memory_region_init_ram_ptr (mr ,owner ,name ,size ,ptr ); +memory_region_init (mr ,owner ,name ,size ); + mr->ram = true; + mr->terminates = true; mr->ram_device = true;
memory.c void memory_region_init_ram_device_ptr(MemoryRegion *mr, · line 1668
Quoted 10 lines
memory.c +5
-0mr->ops = &ram_device_mem_ops; mr->opaque = mr; + mr->destructor = memory_region_destructor_ram; + mr->dirty_log_mask =tcg_enabled () ? (1 <<DIRTY_MEMORY_CODE ) :0 ; +/* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */ +assert (ptr !=NULL ); + mr->ram_block =qemu_ram_alloc_from_ptr (size, ptr, mr, &error_fatal); } void memory_region_init_alias(MemoryRegion *mr,
PATCH 2/2 target/i386: sev: Do not pin the ram device memory region
In reply to Singh, BrijeshThe RAM device presents a memory region that should be handled as an IO region and should not be pinned.
In the case of the vfio-pci, RAM device represents a MMIO BAR and the memory region is not backed by pages hence KVM_MEMORY_ENCRYPT_REG_REGION fails to lock the memory range.
--- target/i386/sev.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
target/i386/sev.c +11 -0
@@ -131,6 +131,17 @@ sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size) {int r;struct kvm_enc_region range; +ram_addr_t offset; +MemoryRegion *mr; + + mr =memory_region_from_host (host, &offset); +/* +* The RAM device presents a memory region that should be treated +* as IO region and should not be pinned. +*/ +if (memory_region_is_ram_device (mr)) { +return ; + } range.addr = (__u64 )(unsigned long )host; range.size = size;
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH 2/2] target/i386: sev: Do not pin the ram device memory region
Quoted 27 lines · Brijesh
The RAM device presents a memory region that should be handled as an IO region and should not be pinned.
In the case of the vfio-pci, RAM device represents a MMIO BAR and the memory region is not backed by pages hence
KVM_MEMORY_ENCRYPT_REG_REGIONfails to lock the memory range.Cc Alex Williamson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- target/i386/sev.c | 11 +++++++++++ 1 file changed, 11 insertions(+)target/i386/sev.c +4
-0@@ -131,6 +131,17 @@ sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size) {int r;struct kvm_enc_region range; +ram_addr_t offset; +MemoryRegion *mr; + + mr =memory_region_from_host (host, &offset);
target/i386/sev.c sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size) · line 147
Quoted 10 lines
target/i386/sev.c +7
-0+/* +* The RAM device presents a memory region that should be treated +* as IO region and should not be pinned. +*/ +if (memory_region_is_ram_device (mr)) { +return ; + } range.addr = (__u64 )(unsigned long )host; range.size = size;
memory_region_from_host() can return NULL, which would give you a segfault at memory_region_is_ram_device(), so you might want to test mr on it's own first and decide which path that would take. Thanks,
Alex
Re: [Qemu-devel] [PATCH 2/2] target/i386: sev: Do not pin the ram device memory region
Quoted 38 lines · Alex Williamson
Quoted 35 lines · Brijesh
The RAM device presents a memory region that should be handled as an IO region and should not be pinned.
In the case of the vfio-pci, RAM device represents a MMIO BAR and the memory region is not backed by pages hence
KVM_MEMORY_ENCRYPT_REG_REGIONfails to lock the memory range.Cc Alex Williamson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- target/i386/sev.c | 11 +++++++++++ 1 file changed, 11 insertions(+)target/i386/sev.c +11
-0@@ -131,6 +131,17 @@ sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size) {int r;struct kvm_enc_region range; +ram_addr_t offset; +MemoryRegion *mr; + + mr =memory_region_from_host (host, &offset); +/* +* The RAM device presents a memory region that should be treated +* as IO region and should not be pinned. +*/ +if (memory_region_is_ram_device (mr)) { +return ; + }
Quoted 8 lines
target/i386/sev.c sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size) · line 147
Quoted 2 lines
target/i386/sev.c +0
-0range.addr = (__u64 )(unsigned long )host; range.size = size;
memory_region_from_host()can return NULL, which would give you a segfault atmemory_region_is_ram_device(), so you might want to test mr on it's own first and decide which path that would take. Thanks,
Ah, thanks for catching it. I will fix in v2.
Hi Alex and Paolo,
Any comments on the patch.
thanks
Quoted 10 lines · Brijesh
Fix: https://bugzilla.redhat.com/show_bug.cgi?id=1667249
Brijesh Singh (2):
memory: Fix the memory region type assignment order target/i386: sev: Do not pin the ram device memory regionmemory.c | 9 ++++++++- target/i386/sev.c | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-)