Skip to contentSkip to conversation

Fix SEV VM device assignment

PATCH Superseded Singh, Brijesh 2 participants 7 messages Jan 17, 2019 – Feb 4, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index 61d66e4441..9ec15349dd 100644
@@ -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
Reviewed-by on v1 1/2
#2 lore raw

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_device function). This is because mr->ram assignment happens _after_ the memory is allocated whereas the callback is executed during allocation.

s/Sugegsted/Suggested/

Quoted 19 lines
---
 memory.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
memory.c +3 -1
index 61d66e4441..9ec15349dd 100644
@@ -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 -0
     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,
#3 lore raw

PATCH 2/2 target/i386: sev: Do not pin the ram device memory region

In reply to Singh, 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_REGION fails to lock the memory range.

---
 target/i386/sev.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
target/i386/sev.c +11 -0
index 20b2d325d8..3e9d5c02fa 100644
@@ -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
#4 lore raw

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_REGION fails to lock the memory range.

---
 target/i386/sev.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
target/i386/sev.c +4 -0
index 20b2d325d8..3e9d5c02fa 100644
@@ -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

#5 lore raw

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_REGION fails to lock the memory range.

---
  target/i386/sev.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
target/i386/sev.c +11 -0
index 20b2d325d8..3e9d5c02fa 100644
@@ -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 -0
      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,

Ah, thanks for catching it. I will fix in v2.