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

Marcelo Tosatti
2008-03-12T19:17:23Z

[-- Attachment #1: virtio-ball1 --]
[-- Type: text/plain, Size: 3799 bytes --]

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

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

Index: kvm-userspace.balloon/libkvm/libkvm.c
===================================================================
--- kvm-userspace.balloon.orig/libkvm/libkvm.c
+++ 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;
Index: kvm-userspace.balloon/libkvm/libkvm.h
===================================================================
--- kvm-userspace.balloon.orig/libkvm/libkvm.h
+++ 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
Index: kvm-userspace.balloon/qemu/hw/virtio-balloon.c
===================================================================
--- kvm-userspace.balloon.orig/qemu/hw/virtio-balloon.c
+++ 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;
Index: kvm-userspace.balloon/qemu/qemu-kvm.c
===================================================================
--- kvm-userspace.balloon.orig/qemu/qemu-kvm.c
+++ 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) {
Index: kvm-userspace.balloon/qemu/qemu-kvm.h
===================================================================
--- kvm-userspace.balloon.orig/qemu/qemu-kvm.h
+++ 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);