Skip to contentSkip to conversation

KVM: coalesced_mmio: NULLify the pointers before freeing ring page and dev

PATCH In review Takuya Yoshikawa 2 participants 10 messages Mar 12, 2010 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: coalesced_mmio: NULLify the pointers before freeing ring page and dev

kvm_coalesced_mmio_init() keeps to hold the addresses of a coalesced mmio ring page and dev even after it has freed them.

This may trigger problems, e.g., if we call kvm_coalesced_mmio_free() in kvm_destroy_vm() or kvm_vm_ioctl_register_coalesced_mmio() afterward.

This patch avoids such problems by NULLifying the pointers.

---
 virt/kvm/coalesced_mmio.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
virt/kvm/coalesced_mmio.c +2 -0
index 5169736..11776b7 100644
@@ -119,8 +119,10 @@ int kvm_coalesced_mmio_init(struct kvm *kvm)
 	return ret;
 
 out_free_dev:
+	kvm->coalesced_mmio_dev = NULL;
 	kfree(dev);
 out_free_page:
+	kvm->coalesced_mmio_ring = NULL;
 	__free_page(page);
 out_err:
 	return ret;
Signature · 1 line
1.6.3.3
#1 lore raw
Quoted 8 lines · Takuya Yoshikawa

kvm_coalesced_mmio_init() keeps to hold the addresses of a coalesced mmio ring page and dev even after it has freed them.

This may trigger problems, e.g., if we call kvm_coalesced_mmio_free() in kvm_destroy_vm() or kvm_vm_ioctl_register_coalesced_mmio() afterward.

This patch avoids such problems by NULLifying the pointers.

After this patch, I think we also need to do some check in kvm_vcpu_fault() for coalesced_mmio_ring, since the coalesced_mmio may not be init correctly. This is other issue, so I will send a new patch for this.

Quoted 21 lines
---
 virt/kvm/coalesced_mmio.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
virt/kvm/coalesced_mmio.c +2 -0
index 5169736..11776b7 100644
@@ -119,8 +119,10 @@ int kvm_coalesced_mmio_init(struct kvm *kvm)
 	return ret;
 
 out_free_dev:
+	kvm->coalesced_mmio_dev = NULL;
 	kfree(dev);
 out_free_page:
+	kvm->coalesced_mmio_ring = NULL;
 	__free_page(page);
 out_err:
 	return ret;
#2 lore raw
Quoted 7 lines · Wei Yongjun
Quoted 6 lines · Takuya Yoshikawa

kvm_coalesced_mmio_init() keeps to hold the addresses of a coalesced mmio ring page and dev even after it has freed them.

This may trigger problems, e.g., if we call kvm_coalesced_mmio_free() in kvm_destroy_vm() or kvm_vm_ioctl_register_coalesced_mmio() afterward.

Quoted 7 lines
Quoted 2 lines

This patch avoids such problems by NULLifying the pointers.

After this patch, I think we also need to do some check in kvm_vcpu_fault() for coalesced_mmio_ring, since the coalesced_mmio may not be init correctly. This is other issue, so I will send a new patch for this.

Eh, thanks.

Quoted 26 lines
Quoted 12 lines
---
 virt/kvm/coalesced_mmio.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
virt/kvm/coalesced_mmio.c +0 -0
index 5169736..11776b7 100644

virt/kvm/coalesced_mmio.c int kvm_coalesced_mmio_init(struct kvm *kvm) · line 128

Quoted 10 lines
virt/kvm/coalesced_mmio.c +2 -0
@@ -119,8 +119,10 @@ int kvm_coalesced_mmio_init(struct kvm *kvm)
 	return ret;
 
 out_free_dev:
+	kvm->coalesced_mmio_dev = NULL;
 	kfree(dev);
 out_free_page:
+	kvm->coalesced_mmio_ring = NULL;
 	__free_page(page);
 out_err:
 	return ret;

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw

PATCH 1/1 KVM: fix to not use NULL kvm->coalesced_mmio_ring in kvm_vcpu_fault()

In reply to Takuya Yoshikawa

If coalesced_mmio init fail, the kvm->coalesced_mmio_ring will be set to NULL. If so, we should return VM_FAULT_SIGBUS in kvm_vcpu_fault() even if vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET.

---
 virt/kvm/kvm_main.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
virt/kvm/kvm_main.c +2 -1
index e758ef7..0e06a6d 100644
@@ -1253,7 +1253,8 @@ static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 		page = virt_to_page(vcpu->arch.pio_data);
 #endif
 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
-	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
+	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET &&
+		 vcpu->kvm->coalesced_mmio_ring)
 		page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
 #endif
 	else
Signature · 1 line
1.6.3.3
#4 lore raw

Re: [PATCH] KVM: fix to not use NULL kvm->coalesced_mmio_ring in kvm_vcpu_fault()

Quoted 14 lines · Wei Yongjun

If coalesced_mmio init fail, the kvm->coalesced_mmio_ring will be set to NULL. If so, we should return VM_FAULT_SIGBUS in kvm_vcpu_fault() even if vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET.

---
 virt/kvm/kvm_main.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
virt/kvm/kvm_main.c +0 -0
index e758ef7..0e06a6d 100644

virt/kvm/kvm_main.c static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · line 1260

Quoted 9 lines
virt/kvm/kvm_main.c +2 -1
@@ -1253,7 +1253,8 @@ static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 		page = virt_to_page(vcpu->arch.pio_data);
 #endif
 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
-	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
+	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET &&
+		 vcpu->kvm->coalesced_mmio_ring)
 		page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
 #endif
 	else

Btw, I am not certain if we can continue the normal path even if kvm_coalesced_mmio_init() fails.

#5 lore raw

PATCH 1/1 KVM: fix kvm_coalesced_mmio_init()'s error handling

In reply to Takuya Yoshikawa

This version may be better.

Thanks,
Takuya

===
kvm_coalesced_mmio_init() keeps to hold the addresses of a coalesced mmio ring page and dev even after it has freed them.

Also, if this function fails, though it must be rare, it seems to be suggesting the system's serious state.

This patch changes the error handling for this function to fix these issues.

---
 virt/kvm/coalesced_mmio.c |    2 ++
 virt/kvm/kvm_main.c       |    4 +++-
 2 files changed, 5 insertions(+), 1 deletions(-)
virt/kvm/coalesced_mmio.c +2 -0
index 5169736..11776b7 100644
@@ -119,8 +119,10 @@ int kvm_coalesced_mmio_init(struct kvm *kvm)
 	return ret;
 
 out_free_dev:
+	kvm->coalesced_mmio_dev = NULL;
 	kfree(dev);
 out_free_page:
+	kvm->coalesced_mmio_ring = NULL;
 	__free_page(page);
 out_err:
 	return ret;
virt/kvm/kvm_main.c +3 -1
index e758ef7..9e72067 100644
@@ -419,7 +419,9 @@ static struct kvm *kvm_create_vm(void)
 	list_add(&kvm->vm_list, &vm_list);
 	spin_unlock(&kvm_lock);
 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
-	kvm_coalesced_mmio_init(kvm);
+	r = kvm_coalesced_mmio_init(kvm);
+	if (r < 0)
+		goto out_err;
 #endif
 out:
 	return kvm;
Signature · 1 line
1.6.3.3
#6 lore raw

Re: [PATCH -v2] KVM: fix kvm_coalesced_mmio_init()'s error handling

Quoted 5 lines · Takuya Yoshikawa

This version may be better.

Thanks,
Takuya

Quoted 9 lines

===
kvm_coalesced_mmio_init() keeps to hold the addresses of a coalesced mmio ring page and dev even after it has freed them.

Also, if this function fails, though it must be rare, it seems to be suggesting the system's serious state.

This patch changes the error handling for this function to fix these issues.

We must also unregister mmu_notifier in the error path.

Quoted 37 lines
---
 virt/kvm/coalesced_mmio.c |    2 ++
 virt/kvm/kvm_main.c       |    4 +++-
 2 files changed, 5 insertions(+), 1 deletions(-)
virt/kvm/coalesced_mmio.c +2 -0
index 5169736..11776b7 100644
@@ -119,8 +119,10 @@ int kvm_coalesced_mmio_init(struct kvm *kvm)
 	return ret;
 
 out_free_dev:
+	kvm->coalesced_mmio_dev = NULL;
 	kfree(dev);
 out_free_page:
+	kvm->coalesced_mmio_ring = NULL;
 	__free_page(page);
 out_err:
 	return ret;
virt/kvm/kvm_main.c +3 -1
index e758ef7..9e72067 100644
@@ -419,7 +419,9 @@ static struct kvm *kvm_create_vm(void)
 	list_add(&kvm->vm_list, &vm_list);
 	spin_unlock(&kvm_lock);
 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
-	kvm_coalesced_mmio_init(kvm);
+	r = kvm_coalesced_mmio_init(kvm);
+	if (r < 0)
+		goto out_err;
 #endif
 out:
 	return kvm;
#7 lore raw

Re: [PATCH -v2] KVM: fix kvm_coalesced_mmio_init()'s error handling

Quoted 10 lines · Wei Yongjun
Quoted 9 lines · Takuya Yoshikawa

This version may be better.

Thanks,
Takuya

===
kvm_coalesced_mmio_init() keeps to hold the addresses of a coalesced mmio ring page and dev even after it has freed them.

Quoted 7 lines
Quoted 5 lines

Also, if this function fails, though it must be rare, it seems to be suggesting the system's serious state.

This patch changes the error handling for this function to fix these issues.

We must also unregister mmu_notifier in the error path.

Oh, sorry.

Quoted 38 lines
Quoted 38 lines
---
 virt/kvm/coalesced_mmio.c |    2 ++
 virt/kvm/kvm_main.c       |    4 +++-
 2 files changed, 5 insertions(+), 1 deletions(-)
virt/kvm/coalesced_mmio.c +2 -0
index 5169736..11776b7 100644
@@ -119,8 +119,10 @@ int kvm_coalesced_mmio_init(struct kvm *kvm)
 	return ret;
 
 out_free_dev:
+	kvm->coalesced_mmio_dev = NULL;
 	kfree(dev);
 out_free_page:
+	kvm->coalesced_mmio_ring = NULL;
 	__free_page(page);
 out_err:
 	return ret;
virt/kvm/kvm_main.c +3 -1
index e758ef7..9e72067 100644
@@ -419,7 +419,9 @@ static struct kvm *kvm_create_vm(void)
 	list_add(&kvm->vm_list, &vm_list);
 	spin_unlock(&kvm_lock);
 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
-	kvm_coalesced_mmio_init(kvm);
+	r = kvm_coalesced_mmio_init(kvm);
+	if (r < 0)
+		goto out_err;
 #endif
 out:
 	return kvm;
#8 lore raw

PATCH 1/2 KVM: introduce kvm_uninit_mmu_notifier()

In reply to Takuya Yoshikawa

We have kvm_init_mmu_notifier() for registering mmu notifier. This patch makes the counterpart for unregister.

---
 virt/kvm/kvm_main.c |   16 +++++++++++-----
 1 files changed, 11 insertions(+), 5 deletions(-)
virt/kvm/kvm_main.c +11 -5
index e758ef7..64b792c 100644
@@ -359,6 +359,11 @@ static int kvm_init_mmu_notifier(struct kvm *kvm)
 	return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
 }
 
+static void kvm_uninit_mmu_notifier(struct kvm *kvm)
+{
+	mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
+}
+
 #else  /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
 
 static int kvm_init_mmu_notifier(struct kvm *kvm)
@@ -366,6 +371,11 @@ static int kvm_init_mmu_notifier(struct kvm *kvm)
 	return 0;
 }
 
+static void kvm_uninit_mmu_notifier(struct kvm *kvm)
+{
+	kvm_arch_flush_shadow(kvm);
+}
+
 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
 
 static struct kvm *kvm_create_vm(void)
@@ -485,11 +495,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	for (i = 0; i < KVM_NR_BUSES; i++)
 		kvm_io_bus_destroy(kvm->buses[i]);
 	kvm_coalesced_mmio_free(kvm);
-#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
-	mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
-#else
-	kvm_arch_flush_shadow(kvm);
-#endif
+	kvm_uninit_mmu_notifier(kvm);
 	kvm_arch_destroy_vm(kvm);
 	hardware_disable_all();
 	mmdrop(mm);
Signature · 1 line
1.6.3.3
#9 lore raw

Re: [PATCH -v3 1/2] KVM: introduce kvm_uninit_mmu_notifier()

I made this patch for the coalesced mmio's error handling work.

Though, I wanted to finish this work, I have no time this week any more. I'll do the remaining part next week, sorry.

Quoted 49 lines · Takuya Yoshikawa

We have kvm_init_mmu_notifier() for registering mmu notifier. This patch makes the counterpart for unregister.

---
 virt/kvm/kvm_main.c |   16 +++++++++++-----
 1 files changed, 11 insertions(+), 5 deletions(-)
virt/kvm/kvm_main.c +11 -5
index e758ef7..64b792c 100644
@@ -359,6 +359,11 @@ static int kvm_init_mmu_notifier(struct kvm *kvm)
 	return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
 }
 
+static void kvm_uninit_mmu_notifier(struct kvm *kvm)
+{
+	mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
+}
+
 #else  /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
 
 static int kvm_init_mmu_notifier(struct kvm *kvm)
@@ -366,6 +371,11 @@ static int kvm_init_mmu_notifier(struct kvm *kvm)
 	return 0;
 }
 
+static void kvm_uninit_mmu_notifier(struct kvm *kvm)
+{
+	kvm_arch_flush_shadow(kvm);
+}
+
 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
 
 static struct kvm *kvm_create_vm(void)
@@ -485,11 +495,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	for (i = 0; i < KVM_NR_BUSES; i++)
 		kvm_io_bus_destroy(kvm->buses[i]);
 	kvm_coalesced_mmio_free(kvm);
-#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
-	mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
-#else
-	kvm_arch_flush_shadow(kvm);
-#endif
+	kvm_uninit_mmu_notifier(kvm);
 	kvm_arch_destroy_vm(kvm);
 	hardware_disable_all();
 	mmdrop(mm);