qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 0/2] Fix SEV VM device assignment
@ 2019-01-17 21:53 Singh, Brijesh
  2019-01-17 21:53 ` [Qemu-devel] [PATCH 1/2] memory: Fix the memory region type assignment order Singh, Brijesh
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Singh, Brijesh @ 2019-01-17 21:53 UTC (permalink / raw)
  To: qemu-devel@nongnu.org; +Cc: alex.williamson@redhat.com, Singh, 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 region

 memory.c          |  9 ++++++++-
 target/i386/sev.c | 11 +++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

-- 
2.17.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Qemu-devel] [PATCH 2/2] target/i386: sev: Do not pin the ram device memory region
  2019-01-17 21:53 [Qemu-devel] [PATCH 0/2] Fix SEV VM device assignment Singh, Brijesh
  2019-01-17 21:53 ` [Qemu-devel] [PATCH 1/2] memory: Fix the memory region type assignment order Singh, Brijesh
@ 2019-01-17 21:53 ` Singh, Brijesh
  2019-02-04 17:59   ` Alex Williamson
  2019-02-04 17:40 ` [Qemu-devel] [PATCH 0/2] Fix SEV VM device assignment Singh, Brijesh
  2 siblings, 1 reply; 7+ messages in thread
From: Singh, Brijesh @ 2019-01-17 21:53 UTC (permalink / raw)
  To: qemu-devel@nongnu.org
  Cc: alex.williamson@redhat.com, Singh, Brijesh, Paolo Bonzini

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.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1667249
Cc: Alex Williamson <alex.williamson@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
---
 target/i386/sev.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/target/i386/sev.c b/target/i386/sev.c
index 20b2d325d8..3e9d5c02fa 100644
--- a/target/i386/sev.c
+++ b/target/i386/sev.c
@@ -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;
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Qemu-devel] [PATCH 1/2] memory: Fix the memory region type assignment order
  2019-01-17 21:53 [Qemu-devel] [PATCH 0/2] Fix SEV VM device assignment Singh, Brijesh
@ 2019-01-17 21:53 ` Singh, Brijesh
  2019-02-04 17:53   ` Alex Williamson
  2019-01-17 21:53 ` [Qemu-devel] [PATCH 2/2] target/i386: sev: Do not pin the ram device memory region Singh, Brijesh
  2019-02-04 17:40 ` [Qemu-devel] [PATCH 0/2] Fix SEV VM device assignment Singh, Brijesh
  2 siblings, 1 reply; 7+ messages in thread
From: Singh, Brijesh @ 2019-01-17 21:53 UTC (permalink / raw)
  To: qemu-devel@nongnu.org
  Cc: alex.williamson@redhat.com, Singh, Brijesh, Paolo Bonzini

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.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1667249
Sugegsted-by: Alex Williamson <alex.williamson@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
---
 memory.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/memory.c b/memory.c
index 61d66e4441..9ec15349dd 100644
--- a/memory.c
+++ b/memory.c
@@ -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,
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH 0/2] Fix SEV VM device assignment
  2019-01-17 21:53 [Qemu-devel] [PATCH 0/2] Fix SEV VM device assignment Singh, Brijesh
  2019-01-17 21:53 ` [Qemu-devel] [PATCH 1/2] memory: Fix the memory region type assignment order Singh, Brijesh
  2019-01-17 21:53 ` [Qemu-devel] [PATCH 2/2] target/i386: sev: Do not pin the ram device memory region Singh, Brijesh
@ 2019-02-04 17:40 ` Singh, Brijesh
  2 siblings, 0 replies; 7+ messages in thread
From: Singh, Brijesh @ 2019-02-04 17:40 UTC (permalink / raw)
  To: qemu-devel@nongnu.org; +Cc: Singh, Brijesh, alex.williamson@redhat.com

Hi Alex and Paolo,

Any comments on the patch.

thanks


On 1/17/19 3:53 PM, Singh, Brijesh wrote:
> 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(-)
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH 1/2] memory: Fix the memory region type assignment order
  2019-01-17 21:53 ` [Qemu-devel] [PATCH 1/2] memory: Fix the memory region type assignment order Singh, Brijesh
@ 2019-02-04 17:53   ` Alex Williamson
  0 siblings, 0 replies; 7+ messages in thread
From: Alex Williamson @ 2019-02-04 17:53 UTC (permalink / raw)
  To: Singh, Brijesh; +Cc: qemu-devel@nongnu.org, Paolo Bonzini

On Thu, 17 Jan 2019 21:53:16 +0000
"Singh, Brijesh" <brijesh.singh@amd.com> wrote:

> 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.
> 
> Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1667249
> Sugegsted-by: Alex Williamson <alex.williamson@redhat.com>

s/Sugegsted/Suggested/

> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
> ---
>  memory.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/memory.c b/memory.c
> index 61d66e4441..9ec15349dd 100644
> --- a/memory.c
> +++ b/memory.c
> @@ -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,

Reviewed-by: Alex Williamson <alex.williamson@redhat.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH 2/2] target/i386: sev: Do not pin the ram device memory region
  2019-01-17 21:53 ` [Qemu-devel] [PATCH 2/2] target/i386: sev: Do not pin the ram device memory region Singh, Brijesh
@ 2019-02-04 17:59   ` Alex Williamson
  2019-02-04 21:03     ` Singh, Brijesh
  0 siblings, 1 reply; 7+ messages in thread
From: Alex Williamson @ 2019-02-04 17:59 UTC (permalink / raw)
  To: Singh, Brijesh; +Cc: qemu-devel@nongnu.org, Paolo Bonzini

On Thu, 17 Jan 2019 21:53:16 +0000
"Singh, Brijesh" <brijesh.singh@amd.com> wrote:

> 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.
> 
> Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1667249
> Cc: Alex Williamson <alex.williamson@redhat.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
> ---
>  target/i386/sev.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/target/i386/sev.c b/target/i386/sev.c
> index 20b2d325d8..3e9d5c02fa 100644
> --- a/target/i386/sev.c
> +++ b/target/i386/sev.c
> @@ -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;


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH 2/2] target/i386: sev: Do not pin the ram device memory region
  2019-02-04 17:59   ` Alex Williamson
@ 2019-02-04 21:03     ` Singh, Brijesh
  0 siblings, 0 replies; 7+ messages in thread
From: Singh, Brijesh @ 2019-02-04 21:03 UTC (permalink / raw)
  To: Alex Williamson; +Cc: Singh, Brijesh, qemu-devel@nongnu.org, Paolo Bonzini



On 2/4/19 11:59 AM, Alex Williamson wrote:
> On Thu, 17 Jan 2019 21:53:16 +0000
> "Singh, Brijesh" <brijesh.singh@amd.com> wrote:
> 
>> 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.
>>
>> Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1667249
>> Cc: Alex Williamson <alex.williamson@redhat.com>
>> Cc: Paolo Bonzini <pbonzini@redhat.com>
>> Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
>> ---
>>   target/i386/sev.c | 11 +++++++++++
>>   1 file changed, 11 insertions(+)
>>
>> diff --git a/target/i386/sev.c b/target/i386/sev.c
>> index 20b2d325d8..3e9d5c02fa 100644
>> --- a/target/i386/sev.c
>> +++ b/target/i386/sev.c
>> @@ -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;
> 
> 
> 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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-02-04 21:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-01-17 21:53 [Qemu-devel] [PATCH 0/2] Fix SEV VM device assignment Singh, Brijesh
2019-01-17 21:53 ` [Qemu-devel] [PATCH 1/2] memory: Fix the memory region type assignment order Singh, Brijesh
2019-02-04 17:53   ` Alex Williamson
2019-01-17 21:53 ` [Qemu-devel] [PATCH 2/2] target/i386: sev: Do not pin the ram device memory region Singh, Brijesh
2019-02-04 17:59   ` Alex Williamson
2019-02-04 21:03     ` Singh, Brijesh
2019-02-04 17:40 ` [Qemu-devel] [PATCH 0/2] Fix SEV VM device assignment Singh, Brijesh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).