Skip to contentSkip to conversation

always assign userspace_addr

PATCH In review Glauber Costa 4 participants 11 messages Nov 18, 2008 – Nov 25, 2008 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 always assign userspace_addr

Currently, kvm only sets new.userspace_addr in slots that were just allocated. This is not the intended behaviour, and actually breaks when we try to use the slots to implement aliases, for example.

Cirrus VGA aliases maps and address to a userspace address, and then keep mapping this same address to different locations until the whole screen is filled.

The solution is to assign new.userspace_addr no matter what, so we can be sure that whenever the guest changes this field, it sees the change being reflected in the code.

---
 virt/kvm/kvm_main.c |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)
virt/kvm/kvm_main.c +9 -9
index a87f45e..fc3abf0 100644
@@ -762,15 +762,6 @@ int __kvm_set_memory_region(struct kvm *kvm,
 		memset(new.rmap, 0, npages * sizeof(*new.rmap));
 
 		new.user_alloc = user_alloc;
-		/*
-		 * hva_to_rmmap() serialzies with the mmu_lock and to be
-		 * safe it has to ignore memslots with !user_alloc &&
-		 * !userspace_addr.
-		 */
-		if (user_alloc)
-			new.userspace_addr = mem->userspace_addr;
-		else
-			new.userspace_addr = 0;
 	}
 	if (npages && !new.lpage_info) {
 		int largepages = npages / KVM_PAGES_PER_HPAGE;
@@ -791,6 +782,15 @@ int __kvm_set_memory_region(struct kvm *kvm,
 		if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
 			new.lpage_info[largepages-1].write_count = 1;
 	}
+	/*
+	 * hva_to_rmmap() serialzies with the mmu_lock and to be
+	 * safe it has to ignore memslots with !user_alloc &&
+	 * !userspace_addr.
+	 */
+	if (npages && user_alloc)
+		new.userspace_addr = mem->userspace_addr;
+	else
+		new.userspace_addr = 0;
 
 	/* Allocate page dirty bitmap if needed */
 	if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
Signature · 1 line
1.5.6.5
#1 lore raw
Quoted 9 lines · Glauber Costa

Currently, kvm only sets new.userspace_addr in slots that were just allocated. This is not the intended behaviour, and actually breaks when we try to use the slots to implement aliases, for example.

Cirrus VGA aliases maps and address to a userspace address, and then keep mapping this same address to different locations until the whole screen is filled.

Quoted 6 lines

The solution is to assign new.userspace_addr no matter what, so we can be sure that whenever the guest changes this field, it sees the change being reflected in the code.

I think this is masking a much bigger problem.

Quoted 12 lines
---
 virt/kvm/kvm_main.c |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)
virt/kvm/kvm_main.c +0 -0
index a87f45e..fc3abf0 100644
@@ -762,15 +762,6 @@ int __kvm_set_memory_region(struct kvm *kvm,
 		memset(new.rmap, 0, npages * sizeof(*new.rmap));
 
 		new.user_alloc = user_alloc;

virt/kvm/kvm_main.c int __kvm_set_memory_region(struct kvm *kvm, · line 765

Quoted 10 lines
virt/kvm/kvm_main.c +0 -9
-		/*
-		 * hva_to_rmmap() serialzies with the mmu_lock and to be
-		 * safe it has to ignore memslots with !user_alloc &&
-		 * !userspace_addr.
-		 */
-		if (user_alloc)
-			new.userspace_addr = mem->userspace_addr;
-		else
-			new.userspace_addr = 0;
 

This is guarded in:

virt/kvm/kvm_main.c int __kvm_set_memory_region(struct kvm *kvm, · line 766

Quoted 1 line
virt/kvm/kvm_main.c +0 -0
    if (npages && !new.rmap) {

In this case, npages > 0 but !new.rmap is already allocated. But this is a new slot? The problem is that when we delete the slot, the rmap never gets freed. This means that if we delete a slot, then create a new slot which happens to be a different size, we use the old rmap and potentially overrun that buffer.

So I think we need a fix that properly frees the rmap when the slot is destroyed.

Regards,

Anthony Liguori

#2 lore raw
Quoted 44 lines · Anthony Liguori
Quoted 9 lines · Glauber Costa

Currently, kvm only sets new.userspace_addr in slots that were just allocated. This is not the intended behaviour, and actually breaks when we try to use the slots to implement aliases, for example.

Cirrus VGA aliases maps and address to a userspace address, and then keep mapping this same address to different locations until the whole screen is filled.

Quoted 6 lines

The solution is to assign new.userspace_addr no matter what, so we can be sure that whenever the guest changes this field, it sees the change being reflected in the code.

I think this is masking a much bigger problem.

Quoted 11 lines
---
 virt/kvm/kvm_main.c |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)
virt/kvm/kvm_main.c +0 -0
index a87f45e..fc3abf0 100644
@@ -762,15 +762,6 @@ int __kvm_set_memory_region(struct kvm *kvm,
 		memset(new.rmap, 0, npages * sizeof(*new.rmap));
  		new.user_alloc = user_alloc;

virt/kvm/kvm_main.c int __kvm_set_memory_region(struct kvm *kvm, · line 764

Quoted 10 lines
virt/kvm/kvm_main.c +0 -9
-		/*
-		 * hva_to_rmmap() serialzies with the mmu_lock and to be
-		 * safe it has to ignore memslots with !user_alloc &&
-		 * !userspace_addr.
-		 */
-		if (user_alloc)
-			new.userspace_addr = mem->userspace_addr;
-		else
-			new.userspace_addr = 0;
 

This is guarded in:

Quoted 7 lines

virt/kvm/kvm_main.c int __kvm_set_memory_region(struct kvm *kvm, · line 765

Quoted 1 line
virt/kvm/kvm_main.c +0 -0
    if (npages && !new.rmap) {

In this case, npages > 0 but !new.rmap is already allocated. But this is a new slot? The problem is that when we delete the slot, the rmap never gets freed. This means that if we delete a slot, then create a new slot which happens to be a different size, we use the old rmap and potentially overrun that buffer.

Oh yeah, it does get freed.

The delete path ends up in a kvm_free_physmem_slot, which will effectively vfree() the rmap structure. In fact, my userspace use case worked totally properly when I deleted the slot prior to re-registering it.

The problem here is when there is an already existant slot, and we are trying to change some information about it. The problem you are concerned basically does not exist, because it would raise only if we are changing the slot size. The code says:

        /* Disallow changing a memory slot's size. */
        r = -EINVAL;
        if (npages && old.npages && npages != old.npages)
                goto out_free;

And this seem pretty much as the expected behaviour to me. (At least, it is a reasonable behaviour, although one could argue that it could easily be different)

#3 lore raw
Quoted 51 lines · Glauber Costa
Quoted 49 lines · Anthony Liguori
Quoted 9 lines · Glauber Costa

Currently, kvm only sets new.userspace_addr in slots that were just allocated. This is not the intended behaviour, and actually breaks when we try to use the slots to implement aliases, for example.

Cirrus VGA aliases maps and address to a userspace address, and then keep mapping this same address to different locations until the whole screen is filled.

Quoted 7 lines

The solution is to assign new.userspace_addr no matter what, so we can be sure that whenever the guest changes this field, it sees the change being reflected in the code.

I think this is masking a much bigger problem.

Quoted 12 lines
---
 virt/kvm/kvm_main.c |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)
virt/kvm/kvm_main.c +0 -1
index a87f45e..fc3abf0 100644
@@ -762,15 +762,6 @@ int __kvm_set_memory_region(struct kvm *kvm,
 		memset(new.rmap, 0, npages * sizeof(*new.rmap));
  		new.user_alloc = user_alloc;
-		/*

virt/kvm/kvm_main.c int __kvm_set_memory_region(struct kvm *kvm, · line 765

Quoted 10 lines
virt/kvm/kvm_main.c +0 -8
-		 * hva_to_rmmap() serialzies with the mmu_lock and to be
-		 * safe it has to ignore memslots with !user_alloc &&
-		 * !userspace_addr.
-		 */
-		if (user_alloc)
-			new.userspace_addr = mem->userspace_addr;
-		else
-			new.userspace_addr = 0;
 
 

This is guarded in:

Quoted 2 lines
virt/kvm/kvm_main.c +0 -0
    if (npages && !new.rmap) {
 
Quoted 9 lines
Quoted 2 lines

In this case, npages > 0 but !new.rmap is already allocated. But this is a new slot? The problem is that when we delete the slot, the rmap never gets freed. This means that if we delete a slot, then create a new slot which happens to be a different size, we use the old rmap and potentially overrun that buffer.

Oh yeah, it does get freed.

The delete path ends up in a kvm_free_physmem_slot, which will effectively vfree() the rmap structure. In fact, my userspace use case worked totally properly when I deleted the slot prior to re-registering it.

That's not how I read the code. I see:

Quoted 6 lines

static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
struct kvm_memory_slot *dont)

{
    if (!dont || free->rmap != dont->rmap)
        vfree(free->rmap);

And it's called as kvm_free_physmem_slot(&old, &new);

new is assigned to old to start out with so old.rmap will equal new.rmap.

Quoted 5 lines

The problem here is when there is an already existant slot, and we are trying to change some information about it. The problem you are concerned basically does not exist, because it would raise only if we are changing the slot size. The code says:

If a memory slot exists, the current code always deletes it and creates a new one so this problem shouldn't exist. See

Quoted 5 lines
            /* unregister whole slot */
            memcpy(&slot, mem, sizeof(slot));
            mem->memory_size = 0;
            kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, mem);

But the problem still exists even with this code. I checked.

So if you have something working without modifying the kernel, can you post it?

Regards,

Anthony Liguori

#4 lore raw
Quoted 9 lines · Anthony Liguori

But the problem still exists even with this code. I checked.

So if you have something working without modifying the kernel, can you post it?

Regards,

Anthony Liguori

Ok, how do you feel about this one?

My proposal is to always delete a memslot before reusing the space, but controlling this behaviour by a flag, so we can maintain backwards compatibility with people using older versions of the interface.

include/linux/kvm.h +1 -0
index f18b86f..66ee286 100644
@@ -39,6 +39,7 @@ struct kvm_userspace_memory_region {
 
 /* for kvm_memory_region::flags */
 #define KVM_MEM_LOG_DIRTY_PAGES  1UL
+#define KVM_MEM_FREE_BEFORE_ALLOC 2UL
 
 
 /* for KVM_IRQ_LINE */
virt/kvm/kvm_main.c +33 -22

92 lines not on this page · raw message

#5 lore raw
Quoted 8 lines · Glauber Costa
Quoted 6 lines · Anthony Liguori

But the problem still exists even with this code. I checked.

So if you have something working without modifying the kernel, can you post it?

Regards,

Quoted 9 lines
Quoted 2 lines

Anthony Liguori

Ok, how do you feel about this one?

My proposal is to always delete a memslot before reusing the space, but controlling this behaviour by a flag, so we can maintain backwards compatibility with people using older versions of the interface.

Is the old behavior ever correct? I think it's always wrong.

Regards,

Anthony Liguori

#6 lore raw
In reply to Anthony Liguori
Quoted 10 lines · Anthony Liguori

That's not how I read the code. I see:

Quoted 6 lines

static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
struct kvm_memory_slot *dont)

{
    if (!dont || free->rmap != dont->rmap)
        vfree(free->rmap);
Quoted 4 lines

And it's called as kvm_free_physmem_slot(&old, &new);

new is assigned to old to start out with so old.rmap will equal new.rmap.

Hm, if !npages we should just kvm_free_physmem_slot(&old, NULL).

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#7 lore raw
Quoted 11 lines · Avi Kivity
Quoted 10 lines · Anthony Liguori

That's not how I read the code. I see:

Quoted 6 lines

static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
struct kvm_memory_slot *dont)

{
    if (!dont || free->rmap != dont->rmap)
        vfree(free->rmap);
Quoted 6 lines
Quoted 4 lines

And it's called as kvm_free_physmem_slot(&old, &new);

new is assigned to old to start out with so old.rmap will equal new.rmap.

Hm, if !npages we should just kvm_free_physmem_slot(&old, NULL).

Actually, I believe we need a little bit more than that, because we can have valid rmaps in flight.

Tell me what you think about this.

virt/kvm/kvm_main.c +9 -6
index b1953ee..f605bba 100644
@@ -735,11 +735,17 @@ int __kvm_set_memory_region(struct kvm *kvm,
 	base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
 	npages = mem->memory_size >> PAGE_SHIFT;
 
-	if (!npages)
-		mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
-
 	new = old = *memslot;
 
+        if (!npages) {
+                mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
+                kvm_arch_flush_shadow(kvm);
+                kvm_free_physmem_slot(memslot, NULL);
+                kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
+                goto out;
+        }
+
+
 	new.base_gfn = base_gfn;
 	new.npages = npages;
 	new.flags = mem->flags;
@@ -812,9 +818,6 @@ int __kvm_set_memory_region(struct kvm *kvm,
 	}
 #endif /* not defined CONFIG_S390 */
 
-	if (!npages)
-		kvm_arch_flush_shadow(kvm);
-
 	spin_lock(&kvm->mmu_lock);
 	if (mem->slot >= kvm->nmemslots)
 		kvm->nmemslots = mem->slot + 1;
#8 lore raw
Quoted 14 lines
virt/kvm/kvm_main.c +0 -3
index b1953ee..f605bba 100644
@@ -735,11 +735,17 @@ int __kvm_set_memory_region(struct kvm *kvm,
       base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
       npages = mem->memory_size >> PAGE_SHIFT;
 
-       if (!npages)
-               mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
-
       new = old = *memslot;
 

virt/kvm/kvm_main.c int __kvm_set_memory_region(struct kvm *kvm, · line 749

Quoted 10 lines
virt/kvm/kvm_main.c +9 -0
+        if (!npages) {
+                mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
+                kvm_arch_flush_shadow(kvm);
+                kvm_free_physmem_slot(memslot, NULL);
+                kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
+                goto out;
+        }
+
+
       new.base_gfn = base_gfn;

Any comments about this version? In the absense of it, I'll submit a version with a SoB for inclusion.

Quoted 14 lines
virt/kvm/kvm_main.c +0 -3
       new.npages = npages;
       new.flags = mem->flags;
@@ -812,9 +818,6 @@ int __kvm_set_memory_region(struct kvm *kvm,
       }
 #endif /* not defined CONFIG_S390 */
 
-       if (!npages)
-               kvm_arch_flush_shadow(kvm);
-
       spin_lock(&kvm->mmu_lock);
       if (mem->slot >= kvm->nmemslots)
               kvm->nmemslots = mem->slot + 1;
Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#9 lore raw
In reply to Glauber Costa
Quoted 19 lines · Glauber Costa
Quoted 2 lines

Hm, if !npages we should just kvm_free_physmem_slot(&old, NULL).

Actually, I believe we need a little bit more than that, because we can have valid rmaps in flight.

Tell me what you think about this.

virt/kvm/kvm_main.c +0 -3
index b1953ee..f605bba 100644
@@ -735,11 +735,17 @@ int __kvm_set_memory_region(struct kvm *kvm,
 	base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
 	npages = mem->memory_size >> PAGE_SHIFT;
 
-	if (!npages)
-		mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
-

virt/kvm/kvm_main.c int __kvm_set_memory_region(struct kvm *kvm, · line 747

Quoted 10 lines
virt/kvm/kvm_main.c +7 -0
 	new = old = *memslot;
 
+        if (!npages) {
+                mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
+                kvm_arch_flush_shadow(kvm);
+                kvm_free_physmem_slot(memslot, NULL);
+                kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
+                goto out;
+        }
 

Why doesn't the kvm_free_physmem_slot() at the end of the normal path work? I don't like adding a new path, for example you missed the iommu call.

Signature · 1 line
error compiling committee.c: too many arguments to function
#10 lore raw
In reply to Anthony Liguori
Quoted 10 lines · Anthony Liguori

That's not how I read the code. I see:

Quoted 6 lines

static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
struct kvm_memory_slot *dont)

{
    if (!dont || free->rmap != dont->rmap)
        vfree(free->rmap);
Quoted 4 lines

And it's called as kvm_free_physmem_slot(&old, &new);

new is assigned to old to start out with so old.rmap will equal new.rmap.

Hm, if !npages we should just kvm_free_physmem_slot(&old, NULL).

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.