Re: swapping with MMU Notifiers V2

Carsten Otte
2008-01-29T16:14:14Z

Andrea Arcangeli wrote:
> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> index ea4764b..9349160 100644
> --- a/include/linux/kvm_host.h
> +++ b/include/linux/kvm_host.h
> @@ -15,6 +15,7 @@
> #include <linux/sched.h>
> #include <linux/mm.h>
> #include <linux/preempt.h>
> +#include <linux/mmu_notifier.h>
> #include <asm/signal.h>
>
> #include <linux/kvm.h>
> @@ -118,6 +119,7 @@ struct kvm {
> struct kvm_io_bus pio_bus;
> struct kvm_vm_stat stat;
> struct kvm_arch arch;
> + struct mmu_notifier mmu_notifier;
> };
>
> /* The guest did something we don't support. */
This should not be in struct kvm, it should go to x86's kvm_arch. This
is x86 specific, we don't need a notifier since the core-vm will just
page out our guest memory like regular userspace mem.

> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 8fc12dc..bb4747c 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -165,6 +165,7 @@ static struct kvm *kvm_create_vm(void)
>
> kvm->mm = current->mm;
> atomic_inc(&kvm->mm->mm_count);
> + mmu_notifier_register(&kvm->mmu_notifier, kvm->mm);
> spin_lock_init(&kvm->mmu_lock);
> kvm_io_bus_init(&kvm->pio_bus);
> mutex_init(&kvm->lock);
to kvm_arch_create_vm please

> @@ -1265,7 +1266,11 @@ static int kvm_resume(struct sys_device *dev)
> }
>
> static struct sysdev_class kvm_sysdev_class = {
> +#ifdef set_kset_name
> set_kset_name("kvm"),
> +#else
> + .name = "kvm",
> +#endif
> .suspend = kvm_suspend,
> .resume = kvm_resume,
> };

> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 4295623..a67e38f 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -298,7 +299,15 @@ int __kvm_set_memory_region(struct kvm *kvm,
> memset(new.rmap, 0, npages * sizeof(*new.rmap));
>
> new.user_alloc = user_alloc;
> - new.userspace_addr = mem->userspace_addr;
> + /*
> + * 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;
> }
>
> /* Allocate page dirty bitmap if needed */
> @@ -311,14 +320,18 @@ int __kvm_set_memory_region(struct kvm *kvm,
> memset(new.dirty_bitmap, 0, dirty_bytes);
> }
>
> + spin_lock(&kvm->mmu_lock);
> if (mem->slot >= kvm->nmemslots)
> kvm->nmemslots = mem->slot + 1;
>
> *memslot = new;
> + spin_unlock(&kvm->mmu_lock);
>
> r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
> if (r) {
> + spin_lock(&kvm->mmu_lock);
> *memslot = old;
> + spin_unlock(&kvm->mmu_lock);
> goto out_free;
> }
>
>
>
This needs to go to arch too.