Skip to contentSkip to conversation

QEMU balloon support

PATCH Applied Marcelo Tosatti 3 participants 9 messages Mar 12, 2008 – Mar 17, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover QEMU balloon support

This patchset resends Anthony's QEMU balloon support plus:

- Truncates the target size to ram size
- Enables madvise() conditioned on KVM_ZAP_GFN ioctl

Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#1 lore raw

PATCH 1/3 QEMU support for virtio balloon driver

From: Anthony Liguori <[email protected]>

This patch adds support to QEMU for Rusty's recently introduce virtio balloon driver. The user-facing portions of this are the introduction of a "balloon" and "info balloon" command in the monitor.

qemu/Makefile.target +1 -1
Index: kvm-userspace.balloon/qemu/Makefile.target
===================================================================
@@ -577,7 +577,7 @@ OBJS += e1000.o
 OBJS+= hypercall.o
 
 # virtio devices
-OBJS += virtio.o virtio-net.o virtio-blk.o
+OBJS += virtio.o virtio-net.o virtio-blk.o virtio-balloon.o
 
 ifeq ($(TARGET_BASE_ARCH), i386)
 # Hardware support
qemu/balloon.h new file +14 -0
Index: kvm-userspace.balloon/qemu/balloon.h
===================================================================
@@ -0,0 +1,14 @@
+#ifndef _QEMU_BALLOON_H
+#define _QEMU_BALLOON_H
+
+#include "cpu-defs.h"
+
+typedef ram_addr_t (QEMUBalloonEvent)(void *opaque, ram_addr_t target);
+
+void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque);
+
+void qemu_balloon(ram_addr_t target);
+
+ram_addr_t qemu_balloon_status(void);
+
+#endif
qemu/hw/pc.c +3 -0
Index: kvm-userspace.balloon/qemu/hw/pc.c
===================================================================
@@ -1120,6 +1120,9 @@ static void pc_init1(ram_addr_t ram_size
 	}
     }
 
+    if (pci_enabled)
+	virtio_balloon_init(pci_bus, 0x1AF4, 0x1002);
+
     if (extboot_drive != -1) {
 	DriveInfo *info = &drives_table[extboot_drive];
 	int cyls, heads, secs;
qemu/hw/pc.h +3 -0
Index: kvm-userspace.balloon/qemu/hw/pc.h
===================================================================
@@ -153,6 +153,9 @@ void virtio_net_poll(void);
 void *virtio_blk_init(PCIBus *bus, uint16_t vendor, uint16_t device,
 		      BlockDriverState *bs);
 
+/* virtio-balloon.h */
+void *virtio_balloon_init(PCIBus *bus, uint16_t vendor, uint16_t device);
+
 /* extboot.c */
 
 void extboot_init(BlockDriverState *bs, int cmd);
qemu/hw/virtio-balloon.c new file +145 -0

145 lines not on this page · raw message

qemu/hw/virtio-blk.c +1 -1
Index: kvm-userspace.balloon/qemu/hw/virtio-blk.c
===================================================================
@@ -153,7 +153,7 @@ void *virtio_blk_init(PCIBus *bus, uint1
 				       0x01, 0x80, 0x00,
 				       16, sizeof(VirtIOBlock));
 
-    s->vdev.update_config = virtio_blk_update_config;
+    s->vdev.get_config = virtio_blk_update_config;
     s->vdev.get_features = virtio_blk_get_features;
     s->bs = bs;
 
qemu/hw/virtio-net.c +1 -1
Index: kvm-userspace.balloon/qemu/hw/virtio-net.c
===================================================================
@@ -288,7 +288,7 @@ void *virtio_net_init(PCIBus *bus, NICIn
 				     0x02, 0x00, 0x00,
 				     6, sizeof(VirtIONet));
 
-    n->vdev.update_config = virtio_net_update_config;
+    n->vdev.get_config = virtio_net_update_config;
     n->vdev.get_features = virtio_net_get_features;
     n->rx_vq = virtio_add_queue(&n->vdev, 512, virtio_net_handle_rx);
     n->tx_vq = virtio_add_queue(&n->vdev, 128, virtio_net_handle_tx);
qemu/hw/virtio.c +18 -1
Index: kvm-userspace.balloon/qemu/hw/virtio.c
===================================================================
@@ -307,6 +307,9 @@ static void virtio_config_writeb(void *o
 	return;
 
     memcpy(vdev->config + addr, &val, sizeof(val));
+
+    if (vdev->set_config)
+	vdev->set_config(vdev, vdev->config);
 }
 
 static void virtio_config_writew(void *opaque, uint32_t addr, uint32_t data)
@@ -319,6 +322,9 @@ static void virtio_config_writew(void *o
 	return;
 
     memcpy(vdev->config + addr, &val, sizeof(val));
+
+    if (vdev->set_config)
+	vdev->set_config(vdev, vdev->config);
 }
 
 static void virtio_config_writel(void *opaque, uint32_t addr, uint32_t data)
@@ -331,6 +337,9 @@ static void virtio_config_writel(void *o
 	return;
 
     memcpy(vdev->config + addr, &val, sizeof(val));
+
+    if (vdev->set_config)
+	vdev->set_config(vdev, vdev->config);
 }
 
 static void virtio_map(PCIDevice *pci_dev, int region_num,
@@ -359,7 +368,7 @@ static void virtio_map(PCIDevice *pci_de
 	register_ioport_read(addr + 20, vdev->config_len, 4,
 			     virtio_config_readl, vdev);
 
-	vdev->update_config(vdev, vdev->config);
+	vdev->get_config(vdev, vdev->config);
     }
 }
 
@@ -383,6 +392,14 @@ VirtQueue *virtio_add_queue(VirtIODevice
     return &vdev->vq[i];
 }
 
+void virtio_notify_config(VirtIODevice *vdev)
+{
+    /* make sure we have the latest config */
+    vdev->get_config(vdev, vdev->config);
+    vdev->isr = 3;
+    virtio_update_irq(vdev);
+}
+
 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
 {
     /* Always notify when queue is empty */
qemu/hw/virtio.h +4 -1
Index: kvm-userspace.balloon/qemu/hw/virtio.h
===================================================================
@@ -118,7 +118,8 @@ struct VirtIODevice
     void *config;
     uint32_t (*get_features)(VirtIODevice *vdev);
     void (*set_features)(VirtIODevice *vdev, uint32_t val);
-    void (*update_config)(VirtIODevice *vdev, uint8_t *config);
+    void (*get_config)(VirtIODevice *vdev, uint8_t *config);
+    void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
     VirtQueue vq[VIRTIO_PCI_QUEUE_MAX];
 };
 
@@ -140,4 +141,6 @@ int virtqueue_pop(VirtQueue *vq, VirtQue
 
 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
 
+void virtio_notify_config(VirtIODevice *vdev);
+
 #endif
qemu/monitor.c +22 -0
Index: kvm-userspace.balloon/qemu/monitor.c
===================================================================
@@ -35,6 +35,7 @@
 #include "audio/audio.h"
 #include "disas.h"
 #include "migration.h"
+#include "balloon.h"
 #include <dirent.h>
 
 #include "qemu-kvm.h"
@@ -1281,6 +1282,23 @@ static void do_wav_capture (const char *
 }
 #endif
 
+static void do_balloon(int value)
+{
+    ram_addr_t target = value;
+    qemu_balloon(target << 20);
+}
+
+static void do_info_balloon(void)
+{
+    ram_addr_t actual;
+
+    actual = qemu_balloon_status();
+    if (actual == 0)
+	term_printf("Ballooning not activated in VM\n");
+    else
+	term_printf("balloon: actual=%d\n", (int)(actual >> 20));
+}
+
 static term_cmd_t term_cmds[] = {
     { "help|?", "s?", do_help,
       "[cmd]", "show the help" },
@@ -1359,6 +1377,8 @@ static term_cmd_t term_cmds[] = {
     { "migrate_set_speed", "s", do_migrate_set_speed,
       "value", "set maximum speed (in bytes) for migrations" },
     { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
+    { "balloon", "i", do_balloon,
+       "target", "request VM to change it's memory allocation (in MB)" },
     { NULL, NULL, },
 };
 
@@ -1421,6 +1441,8 @@ static term_cmd_t info_cmds[] = {
 #endif
     { "migration", "", do_info_migration,
       "", "show migration information" },
+    { "balloon", "", do_info_balloon,
+      "", "show balloon information" },
     { NULL, NULL, },
 };
 
qemu/vl.c +26 -0
Index: kvm-userspace.balloon/qemu/vl.c
===================================================================
@@ -38,6 +38,7 @@
 #include "block.h"
 #include "audio/audio.h"
 #include "migration.h"
+#include "balloon.h"
 #include "qemu-kvm.h"
 
 #include <unistd.h>
@@ -513,6 +514,31 @@ void hw_error(const char *fmt, ...)
     abort();
 }
 
+/***************/
+/* ballooning */
+
+static QEMUBalloonEvent *qemu_balloon_event;
+void *qemu_balloon_event_opaque;
+
+void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
+{
+    qemu_balloon_event = func;
+    qemu_balloon_event_opaque = opaque;
+}
+
+void qemu_balloon(ram_addr_t target)
+{
+    if (qemu_balloon_event)
+	qemu_balloon_event(qemu_balloon_event_opaque, target);
+}
+
+ram_addr_t qemu_balloon_status(void)
+{
+    if (qemu_balloon_event)
+	return qemu_balloon_event(qemu_balloon_event_opaque, 0);
+    return 0;
+}
+
 /***********************************************************/
 /* keyboard/mouse */
 
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#2 lore raw

PATCH 2/3 QEMU: balloon: dont allow target larger than ram size

In reply to Marcelo Tosatti

Truncate the target size to be at most ram size.

qemu/hw/virtio-balloon.c +5 -0
Index: kvm-userspace.balloon/qemu/hw/virtio-balloon.c
===================================================================
@@ -16,6 +16,7 @@
 #include "pc.h"
 #include "balloon.h"
 #include "sysemu.h"
+#include "console.h"
 
 #include <sys/mman.h>
 
@@ -115,6 +116,10 @@ static ram_addr_t virtio_balloon_to_targ
     VirtIOBalloon *dev = opaque;
 
     if (target) {
+        if (target > ram_size) {
+            term_printf("target larger than ram size, truncating\n");
+            target = ram_size;
+        }
 	dev->num_pages = (ram_size - target) >> TARGET_PAGE_BITS;
 	virtio_notify_config(&dev->vdev);
     }
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#3 lore raw

PATCH 3/3 QEMU: balloon: zap any shadow mappings to a page before madvise(MADV_DONTNEED)

In reply to Marcelo Tosatti

Call into kvm to know whether we can madvise(MADV_DONTNEED) pages.

libkvm/libkvm.c +14 -0
Index: kvm-userspace.balloon/libkvm/libkvm.c
===================================================================
@@ -840,6 +840,20 @@ int kvm_is_ready_for_interrupt_injection
 	return run->ready_for_interrupt_injection;
 }
 
+int kvm_zap_single_gfn(kvm_context_t kvm, unsigned long gfn)
+{
+	int r = -1;
+
+#ifdef KVM_CAP_ZAP_GFN
+	r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_ZAP_GFN);
+	if (r > 0)
+		r = ioctl(kvm->vm_fd, KVM_ZAP_GFN, &gfn);
+	else
+		r = -1;
+#endif
+	return r;
+}
+
 int kvm_run(kvm_context_t kvm, int vcpu)
 {
 	int r;
libkvm/libkvm.h +1 -0
Index: kvm-userspace.balloon/libkvm/libkvm.h
===================================================================
@@ -422,6 +422,7 @@ int kvm_get_dirty_pages_range(kvm_contex
 			      unsigned long end_addr, void *buf, void*opaque,
 			      int (*cb)(unsigned long start, unsigned long len,
 					void*bitmap, void *opaque));
+int kvm_zap_single_gfn(kvm_context_t kvm, unsigned long gfn);
 
 /*!
  * \brief Create a memory alias
qemu/hw/virtio-balloon.c +11 -11
Index: kvm-userspace.balloon/qemu/hw/virtio-balloon.c
===================================================================
@@ -17,6 +17,7 @@
 #include "balloon.h"
 #include "sysemu.h"
 #include "console.h"
+#include "qemu-kvm.h"
 
 #include <sys/mman.h>
 
@@ -60,22 +61,21 @@ static void virtio_balloon_handle_output
 	unsigned int wlen = 0;
 
 	for (i = 0; i < elem.out_num; i++) {
-	    int flags;
 	    uint32_t *pfns = elem.out_sg[i].iov_base;
 	    unsigned int n_pfns = elem.out_sg[i].iov_len / 4;
 	    int j;
 
 	    for (j = 0; j < n_pfns; j++) {
-		if (vq == s->dvq) /* deflate */
-		    flags = MADV_WILLNEED;
-		else /* inflate */
-		    flags = MADV_DONTNEED;
-
-#if 0
-		/* can't use this until we have mmu notifier support */
-		madvise(phys_ram_base + (pfns[j] << TARGET_PAGE_BITS),
-			TARGET_PAGE_SIZE, flags);
-#endif
+		if (vq == s->dvq)
+                    /* deflate */
+		        madvise(phys_ram_base + (pfns[j] << TARGET_PAGE_BITS),
+			        TARGET_PAGE_SIZE, MADV_WILLNEED);
+		else { /* inflate */
+			if (kvm_zap_gfn(pfns[j]) == 0)
+				madvise(phys_ram_base +
+					(pfns[j] << TARGET_PAGE_BITS),
+					TARGET_PAGE_SIZE, MADV_DONTNEED);
+		}
 	    }
 
 	    wlen += elem.out_sg[i].iov_len;
qemu/qemu-kvm.c +5 -0
Index: kvm-userspace.balloon/qemu/qemu-kvm.c
===================================================================
@@ -65,6 +65,11 @@ static void sig_ipi_handler(int n)
 {
 }
 
+int kvm_zap_gfn(unsigned long gfn)
+{
+	return kvm_zap_single_gfn(kvm_context, gfn);
+}
+
 void kvm_update_interrupt_request(CPUState *env)
 {
     if (env && vcpu && env != vcpu->env) {
qemu/qemu-kvm.h +1 -0
Index: kvm-userspace.balloon/qemu/qemu-kvm.h
===================================================================
@@ -24,6 +24,7 @@ int kvm_qemu_init_env(CPUState *env);
 int kvm_qemu_check_extension(int ext);
 void kvm_apic_init(CPUState *env);
 int kvm_set_irq(int irq, int level);
+int kvm_zap_gfn(unsigned long gfn);
 
 int kvm_physical_memory_set_dirty_tracking(int enable);
 int kvm_update_dirty_pages_log(void);
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#4 lore raw
In reply to Marcelo Tosatti
Quoted 6 lines · Marcelo Tosatti

This patchset resends Anthony's QEMU balloon support plus:

- Truncates the target size to ram size
- Enables madvise() conditioned on KVM_ZAP_GFN ioctl

Once mmu notifiers are in, KVM_ZAP_GFN isn't needed. So we have three possible situations:

- zap needed, but not available: don't madvise()
- zap needed and available: zap and madvise()
- zap unneeded: madvise()

Did you find out what's causing the errors in the first place (if zap is not used)? It worries me greatly.

Signature · 7 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#5 lore raw

Hi Avi,

Good that you're back.

Quoted 8 lines · Avi Kivity
Quoted 6 lines · Marcelo Tosatti

This patchset resends Anthony's QEMU balloon support plus:

- Truncates the target size to ram size
- Enables madvise() conditioned on KVM_ZAP_GFN ioctl

Quoted 6 lines

Once mmu notifiers are in, KVM_ZAP_GFN isn't needed. So we have three possible situations:

- zap needed, but not available: don't madvise()
- zap needed and available: zap and madvise()
- zap unneeded: madvise()

Right. That is what the patch does. You just have to fill in "have_mmu_notifiers" here:

+int kvm_zap_single_gfn(struct kvm *kvm, gfn_t gfn) +{
+ unsigned long addr;
+ int have_mmu_notifiers = 0;
+
+ down_read(&kvm->slots_lock);
+ addr = gfn_to_hva(kvm, gfn);
+
+ if (kvm_is_error_hva(addr)) {
+ up_read(&kvm->slots_lock);
+ return -EINVAL;
+ }
+
+ if (!have_mmu_notifiers) {

+               spin_lock(&kvm->mmu_lock);
+               rmap_nuke(kvm, gfn);
+               spin_unlock(&kvm->mmu_lock);

+ }
+ up_read(&kvm->slots_lock);

So as to avoid rmap_nuke, since that will be done through the madvise() path.

Quoted 2 lines

Did you find out what's causing the errors in the first place (if zap is not used)? It worries me greatly.

Yes, the problem is that the rmap code does not handle the qemu process mappings from vanishing while there is a present rmap. If that happens, and there is a fault for a gfn whose qemu mapping has been removed, a different physical zero page will be allocated:

	rmap a -> gfn 0 -> physical host page 0
	mapping for gfn 0 gets removed
	guest faults in gfn 0 through the same pte "chain"
	rmap a -> gfn 0 -> physical host page 1

When instantiating the shadow mapping for the second time, the "is_rmap_pte" check succeeds, so we release the reference grabbed by gfn_to_page() at mmu_set_spte(). We now have a shadow mapping pointing to a physical page without having an additional reference on that page.

The following makes the host not crash under such a condition, but the condition itself is invalid leading to inconsistent state on the guest. So IMHO it shouldnt be allowed to happen in the first place.

arch/x86/kvm/mmu.c +16 -0
index f0cdfba..4c93b79 100644
@@ -1009,6 +1009,21 @@ struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t
+gva)
        return page;
 }
 
+static int was_spte_rmapped(struct kvm *kvm, u64 *spte, struct page *page)
+{
+       int ret = 0;
+       unsigned long host_pfn = (*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
+
+       if (is_rmap_pte(*spte)) {
+               if (host_pfn != page_to_pfn(page))
+                       rmap_remove(kvm, spte);
+               else
+                       ret = 1;
+       }
+
+       return ret;
+}
+
 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
                         unsigned pt_access, unsigned pte_access,

int user_fault, int write_fault, int dirty,
@@ -1016,7 +1031,7 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 +*shadow_pte,
struct page *page)

 {
        u64 spte;
-       int was_rmapped = is_rmap_pte(*shadow_pte);
+       int was_rmapped = was_spte_rmapped(vcpu->kvm, shadow_pte, page);
        int was_writeble = is_writeble_pte(*shadow_pte);

        /*

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#6 lore raw
Quoted 46 lines · Marcelo Tosatti

Hi Avi,

Good that you're back.

Quoted 9 lines · Avi Kivity
Quoted 7 lines · Marcelo Tosatti

This patchset resends Anthony's QEMU balloon support plus:

- Truncates the target size to ram size
- Enables madvise() conditioned on KVM_ZAP_GFN ioctl

Quoted 7 lines

Once mmu notifiers are in, KVM_ZAP_GFN isn't needed. So we have three possible situations:

- zap needed, but not available: don't madvise()
- zap needed and available: zap and madvise()
- zap unneeded: madvise()

Right. That is what the patch does. You just have to fill in "have_mmu_notifiers" here:

(quoted diff) +19 -0
+int kvm_zap_single_gfn(struct kvm *kvm, gfn_t gfn)
+{
+       unsigned long addr;
+       int have_mmu_notifiers = 0;
+
+       down_read(&kvm->slots_lock);
+       addr = gfn_to_hva(kvm, gfn);
+
+       if (kvm_is_error_hva(addr)) {
+               up_read(&kvm->slots_lock);
+               return -EINVAL;
+       }
+
+       if (!have_mmu_notifiers) {
+               spin_lock(&kvm->mmu_lock);
+               rmap_nuke(kvm, gfn);
+               spin_unlock(&kvm->mmu_lock);
+       }
+       up_read(&kvm->slots_lock);
Quoted 4 lines

So as to avoid rmap_nuke, since that will be done through the madvise() path.

Why not do it in userspace?

I'll likely merge zap directly into the backwards compatibility support (it won't ever appear in mainline since mmu notifiers will be merged sooner).

Quoted 14 lines
Quoted 3 lines

Did you find out what's causing the errors in the first place (if zap is not used)? It worries me greatly.

Yes, the problem is that the rmap code does not handle the qemu process mappings from vanishing while there is a present rmap. If that happens, and there is a fault for a gfn whose qemu mapping has been removed, a different physical zero page will be allocated:

	rmap a -> gfn 0 -> physical host page 0
	mapping for gfn 0 gets removed
	guest faults in gfn 0 through the same pte "chain"
	rmap a -> gfn 0 -> physical host page 1
Quoted 10 lines

When instantiating the shadow mapping for the second time, the "is_rmap_pte" check succeeds, so we release the reference grabbed by gfn_to_page() at mmu_set_spte(). We now have a shadow mapping pointing to a physical page without having an additional reference on that page.

The following makes the host not crash under such a condition, but the condition itself is invalid leading to inconsistent state on the guest. So IMHO it shouldnt be allowed to happen in the first place.

The only way to prevent it is with mmu notifiers, which we may not have for 2.6.25. So I'd like to send this for 2.6.25-rc.

Please add a signoff.

Quoted 5 lines
arch/x86/kvm/mmu.c +0 -0
index f0cdfba..4c93b79 100644

arch/x86/kvm/mmu.c struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t · line 1017

Quoted 9 lines
arch/x86/kvm/mmu.c +5 -0
@@ -1009,6 +1009,21 @@ struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t
+gva)
        return page;
 }
 
+static int was_spte_rmapped(struct kvm *kvm, u64 *spte, struct page *page)
+{
+       int ret = 0;
+       unsigned long host_pfn = (*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
 

hfn_t hfn

Quoted 15 lines
arch/x86/kvm/mmu.c +11 -0
+
+       if (is_rmap_pte(*spte)) {
+               if (host_pfn != page_to_pfn(page))
+                       rmap_remove(kvm, spte);
+               else
+                       ret = 1;
+       }
+
+       return ret;
+}
+
 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
                         unsigned pt_access, unsigned pte_access,
                         int user_fault, int write_fault, int dirty,

arch/x86/kvm/mmu.c static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 · line 1037

Quoted 8 lines
arch/x86/kvm/mmu.c +2 -1
@@ -1016,7 +1031,7 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64
+*shadow_pte,
                         struct page *page)
 {
        u64 spte;
-       int was_rmapped = is_rmap_pte(*shadow_pte);
+       int was_rmapped = was_spte_rmapped(vcpu->kvm, shadow_pte, page);
        int was_writeble = is_writeble_pte(*shadow_pte);
 

Calling code with side effects in an initializer is a bit confusing. I suggest simply inlining the code into mmu_set_spte().

Signature · 7 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#7 lore raw
Quoted 8 lines · Avi Kivity
Quoted 6 lines

+ up_read(&kvm->slots_lock);

So as to avoid rmap_nuke, since that will be done through the madvise() path.

Why not do it in userspace?

I don't see any way of knowing whether you have or not mmu notifiers from userspace except adding an ioctl.

Quoted 3 lines

I'll likely merge zap directly into the backwards compatibility support (it won't ever appear in mainline since mmu notifiers will be merged sooner).

Even with mmu notifiers QEMU needs to ask the host kernel "do you have mmu notifiers?" before zapping any page, otherwise the guest will crash.

So some method of finding if the host kernel has mmu notifiers needs to be in place even in mainline.

And what about allocation of ioctl numbers? Will you reserve the number used for ZAP_GFN on mainline?

Quoted 19 lines
Quoted 19 lines
Quoted 3 lines

Did you find out what's causing the errors in the first place (if zap is not used)? It worries me greatly.

Yes, the problem is that the rmap code does not handle the qemu process mappings from vanishing while there is a present rmap. If that happens, and there is a fault for a gfn whose qemu mapping has been removed, a different physical zero page will be allocated:

	rmap a -> gfn 0 -> physical host page 0
	mapping for gfn 0 gets removed
	guest faults in gfn 0 through the same pte "chain"
	rmap a -> gfn 0 -> physical host page 1

When instantiating the shadow mapping for the second time, the "is_rmap_pte" check succeeds, so we release the reference grabbed by gfn_to_page() at mmu_set_spte(). We now have a shadow mapping pointing to a physical page without having an additional reference on that page.

Quoted 8 lines
Quoted 5 lines

The following makes the host not crash under such a condition, but the condition itself is invalid leading to inconsistent state on the guest. So IMHO it shouldnt be allowed to happen in the first place.

The only way to prevent it is with mmu notifiers, which we may not have for 2.6.25. So I'd like to send this for 2.6.25-rc.

Well, you can prevent it by never allowing a page from being removed while there are shadow mappings for it. So while this fixes the host crash, it won't fix the guest crash.

KVM: MMU: handle page removal with shadow mapping

Do not assume that a shadow mapping will always point to the same host frame number. Fixes crash with madvise(MADV_DONTNEED).

arch/x86/kvm/mmu.c +9 -1
index f0cdfba..4caa1a0 100644
@@ -1016,8 +1016,16 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
 			 struct page *page)
 {
 	u64 spte;
-	int was_rmapped = is_rmap_pte(*shadow_pte);
+	int was_rmapped = 0;
 	int was_writeble = is_writeble_pte(*shadow_pte);
+	hfn_t host_pfn = (*shadow_pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
+
+	if (is_rmap_pte(*shadow_pte)) {
+		if (host_pfn != page_to_pfn(page))
+			rmap_remove(vcpu->kvm, shadow_pte);
+		else
+			was_rmapped = 1;
+	}
 
 	/*
 	 * If we overwrite a PTE page pointer with a 2MB PMD, unlink

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

Applied
#8 lore raw
Quoted 10 lines · Marcelo Tosatti
Quoted 7 lines · Avi Kivity
Quoted 7 lines

+ up_read(&kvm->slots_lock);

So as to avoid rmap_nuke, since that will be done through the madvise() path.

Quoted 7 lines
Quoted 2 lines

Why not do it in userspace?

I don't see any way of knowing whether you have or not mmu notifiers from userspace except adding an ioctl.

KVM_CHECK_CAPABILITY (KVM_CAP_MMU_NOTIFIERS)

Quoted 9 lines
Quoted 4 lines

I'll likely merge zap directly into the backwards compatibility support (it won't ever appear in mainline since mmu notifiers will be merged sooner).

Even with mmu notifiers QEMU needs to ask the host kernel "do you have mmu notifiers?" before zapping any page, otherwise the guest will crash.

If we drop the shadow ptes explicitly, it shouldn't crash?

Quoted 4 lines

So some method of finding if the host kernel has mmu notifiers needs to be in place even in mainline.

Yes, the capability test.

Quoted 4 lines

And what about allocation of ioctl numbers? Will you reserve the number used for ZAP_GFN on mainline?

We can allocate those from the end and hope they never meet.

Quoted 6 lines

KVM: MMU: handle page removal with shadow mapping

Do not assume that a shadow mapping will always point to the same host frame number. Fixes crash with madvise(MADV_DONTNEED).

Applied, thanks.

Signature · 7 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/