Patchwork [3/6] kvm: workaround a possible KVM bug when using KVM_MEM_READONLY

login
register
mail settings
Submitter jordan.l.justen@intel.com
Date April 28, 2013, 8:32 a.m.
Message ID <1367137941-4310-4-git-send-email-jordan.l.justen@intel.com>
Download mbox | patch
Permalink /patch/240255/
State New
Headers show

Comments

jordan.l.justen@intel.com - April 28, 2013, 8:32 a.m.
On a Linux 3.8.0 based kernel, I occasionally saw a situation
where the memory region would continue to trap on memory
read even though KVM_MEM_READONLY was set.

I found that if I set the slot to a size of 0, and before
setting the slot, it would then behave as expected.

Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Cc: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
---
 kvm-all.c |    7 +++++++
 1 file changed, 7 insertions(+)
Jan Kiszka - April 29, 2013, 10:29 a.m.
On 2013-04-28 10:32, Jordan Justen wrote:
> On a Linux 3.8.0 based kernel, I occasionally saw a situation
> where the memory region would continue to trap on memory
> read even though KVM_MEM_READONLY was set.

Only 3.8.0? Did you bisect the issue down to the causing commit? Is it
fixed in later versions?

Jan

> 
> I found that if I set the slot to a size of 0, and before
> setting the slot, it would then behave as expected.
> 
> Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
> Cc: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
> ---
>  kvm-all.c |    7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/kvm-all.c b/kvm-all.c
> index 95e6bf2..e2ddbcb 100644
> --- a/kvm-all.c
> +++ b/kvm-all.c
> @@ -205,6 +205,13 @@ static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
>      if (s->migration_log) {
>          mem.flags |= KVM_MEM_LOG_DIRTY_PAGES;
>      }
> +    if (mem.flags & KVM_MEM_READONLY && mem.memory_size != 0) {
> +        /* Workaround an issue with setting a READONLY slot. Set the
> +         * slot size to 0 before setting the slot to the desired value. */
> +        mem.memory_size = 0;
> +        kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
> +        mem.memory_size = slot->memory_size;
> +    }
>      return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
>  }
>  
>
Jordan Justen - April 29, 2013, 6:37 p.m.
On Mon, Apr 29, 2013 at 3:29 AM, Jan Kiszka <jan.kiszka@siemens.com> wrote:
> On 2013-04-28 10:32, Jordan Justen wrote:
>> On a Linux 3.8.0 based kernel, I occasionally saw a situation
>> where the memory region would continue to trap on memory
>> read even though KVM_MEM_READONLY was set.
>
> Only 3.8.0? Did you bisect the issue down to the causing commit? Is it
> fixed in later versions?

I'm sorry, I have not tried to bisect, nor have I tried a newer kernel version.

Speculating a bit, it seems that a trap to the region might cause the
issue. This is what happens in the failing case:
* Disable mem region
* Trap on access to region
* Enable readonly region
* Next read access will trap when it shouldn't

Here is the what happen with the work-around:
* Disable mem region
* Trap on access to region
* (Re-)disable mem region (work-around adds this)
* Enable readonly region
* Next read access will not trap (proper behavior)

-Jordan
Xiao Guangrong - May 3, 2013, 6:13 a.m.
On 04/28/2013 04:32 PM, Jordan Justen wrote:
> On a Linux 3.8.0 based kernel, I occasionally saw a situation
> where the memory region would continue to trap on memory
> read even though KVM_MEM_READONLY was set.
> 
> I found that if I set the slot to a size of 0, and before
> setting the slot, it would then behave as expected.

Yes, the KVM_MEM_READONLY flag can not be directly changed, see
the commit 75d61fbcf563373696578570e914f555e12c8d97 on kvm tree.

Do you think the way which deletes the memslot before changing
the KVM_MEM_READONLY hurts the performance? If yes, i will
make it be directly changed.

Thanks for you implementing the READONLY memory in Qemu.

Reviewed-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>

Patch

diff --git a/kvm-all.c b/kvm-all.c
index 95e6bf2..e2ddbcb 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -205,6 +205,13 @@  static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
     if (s->migration_log) {
         mem.flags |= KVM_MEM_LOG_DIRTY_PAGES;
     }
+    if (mem.flags & KVM_MEM_READONLY && mem.memory_size != 0) {
+        /* Workaround an issue with setting a READONLY slot. Set the
+         * slot size to 0 before setting the slot to the desired value. */
+        mem.memory_size = 0;
+        kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
+        mem.memory_size = slot->memory_size;
+    }
     return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
 }