Skip to content

Commit

Permalink
KVM: Unmap kernel-allocated memory on slot destruction
Browse files Browse the repository at this point in the history
kvm_vm_ioctl_set_memory_region() is able to remove memory in addition to
adding it.  Therefore when using kernel swapping support for old userspaces,
we need to munmap the memory if the user request to remove it

Signed-off-by: Izik Eidus <[email protected]>
Signed-off-by: Avi Kivity <[email protected]>
  • Loading branch information
Izik Eidus authored and avikivity committed Jan 30, 2008
1 parent 5f43238 commit 80b14b5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/kvm/kvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ struct kvm_memory_slot {
unsigned long *rmap;
unsigned long *dirty_bitmap;
unsigned long userspace_addr;
int user_alloc;
};

struct kvm {
Expand Down
14 changes: 14 additions & 0 deletions drivers/kvm/kvm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,

memset(new.rmap, 0, npages * sizeof(*new.rmap));

new.user_alloc = user_alloc;
if (user_alloc)
new.userspace_addr = mem->userspace_addr;
else {
Expand All @@ -727,6 +728,19 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
if (IS_ERR((void *)new.userspace_addr))
goto out_unlock;
}
} else {
if (!old.user_alloc && old.rmap) {
int ret;

down_write(&current->mm->mmap_sem);
ret = do_munmap(current->mm, old.userspace_addr,
old.npages * PAGE_SIZE);
up_write(&current->mm->mmap_sem);
if (ret < 0)
printk(KERN_WARNING
"kvm_vm_ioctl_set_memory_region: "
"failed to munmap memory\n");
}
}

/* Allocate page dirty bitmap if needed */
Expand Down

0 comments on commit 80b14b5

Please sign in to comment.