diff mbox series

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

Message ID 20190117215300.29694-3-brijesh.singh@amd.com (mailing list archive)
State New, archived
Headers show
Series Fix SEV VM device assignment | expand

Commit Message

Brijesh Singh Jan. 17, 2019, 9:53 p.m. UTC
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(+)

Comments

Alex Williamson Feb. 4, 2019, 5:59 p.m. UTC | #1
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
Brijesh Singh Feb. 4, 2019, 9:03 p.m. UTC | #2
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.
diff mbox series

Patch

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;