public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Anthony Liguori <anthony-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
To: Izik Eidus <izike-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
Cc: kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
Subject: Re: [PATCH 3/4] Swapping
Date: Sat, 13 Oct 2007 19:36:22 -0500	[thread overview]
Message-ID: <47116486.4070609@codemonkey.ws> (raw)
In-Reply-To: <471156D9.3030700-atKUWr5tajBWk0Htik3J/w@public.gmane.org>

[-- Attachment #1: Type: text/plain, Size: 762 bytes --]

Izik Eidus wrote:
> Anthony Liguori wrote:
>>
>> I think it's just a matter of calling do_mmap() with the appropriate 
>> parameters.  It looks likes there's some drivers call do_mmap() 
>> directly.
>>
> yea, i think you right, this is excellent idea!, when we will merge 
> the swapping to kvm, we will add swapping support to older userspace.

Here's a patch against your series.  The memset in kvmctl ends up making 
the guest use all physical memory to start off with but I did confirm 
that once the system is under memory pressure, the guest's memory 
becomes swappable.  Of course, it's quite painful :-)

A nice thing though is that a lot of the code becomes a bit cleaner and 
we can eliminate the phys_mem array entirely.

Regards,

Anthony Liguori



[-- Attachment #2: swap-old-userspace.diff --]
[-- Type: text/x-patch, Size: 4754 bytes --]

Subject: [PATCH] Allocate userspace memory for older userspace
From: Anthony Liguori <aliguori-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
Cc: Avi Kivity <avi-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
Cc: Izik Eidus <izike-atKUWr5tajBWk0Htik3J/w@public.gmane.org>

Allocate a userspace buffer for older userspaces.  Also eliminate phys_mem
buffer.  The memset() in kvmctl really kills initial memory usage but swapping
does even with old userspaces.

Signed-off-by: Anthony Liguori <aliguori-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>

diff --git a/drivers/kvm/kvm.h b/drivers/kvm/kvm.h
index 74b427f..c904ea3 100644
--- a/drivers/kvm/kvm.h
+++ b/drivers/kvm/kvm.h
@@ -405,10 +405,8 @@ struct kvm_memory_slot {
 	gfn_t base_gfn;
 	unsigned long npages;
 	unsigned long flags;
-	struct page **phys_mem;
 	unsigned long *rmap;
 	unsigned long *dirty_bitmap;
-	int user_alloc; /* user allocated memory */
 	unsigned long userspace_addr;
 };
 
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c
index f58d49b..aa5666f 100644
--- a/drivers/kvm/kvm_main.c
+++ b/drivers/kvm/kvm_main.c
@@ -41,6 +41,7 @@
 #include <linux/profile.h>
 #include <linux/kvm_para.h>
 #include <linux/pagemap.h>
+#include <linux/mman.h>
 
 #include <asm/processor.h>
 #include <asm/msr.h>
@@ -319,34 +320,18 @@ static struct kvm *kvm_create_vm(void)
 	return kvm;
 }
 
-static void kvm_free_kernel_physmem(struct kvm_memory_slot *free)
-{
-	int i;
-
-	for (i = 0; i < free->npages; ++i)
-		if (free->phys_mem[i])
-			__free_page(free->phys_mem[i]);
-}
-
 /*
  * Free any memory in @free but not in @dont.
  */
 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
 				  struct kvm_memory_slot *dont)
 {
-	if (!dont || free->phys_mem != dont->phys_mem)
-		if (free->phys_mem) {
-			if (!free->user_alloc)
-				kvm_free_kernel_physmem(free);
-			vfree(free->phys_mem);
-		}
 	if (!dont || free->rmap != dont->rmap)
 		vfree(free->rmap);
 
 	if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
 		vfree(free->dirty_bitmap);
 
-	free->phys_mem = NULL;
 	free->npages = 0;
 	free->dirty_bitmap = NULL;
 }
@@ -731,10 +716,6 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
 			goto out_unlock;
 	}
 
-	/* Deallocate if slot is being removed */
-	if (!npages)
-		new.phys_mem = NULL;
-
 	/* Free page dirty bitmap if unneeded */
 	if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
 		new.dirty_bitmap = NULL;
@@ -742,29 +723,27 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
 	r = -ENOMEM;
 
 	/* Allocate if a slot is being created */
-	if (npages && !new.phys_mem) {
-		new.phys_mem = vmalloc(npages * sizeof(struct page *));
-
-		if (!new.phys_mem)
-			goto out_unlock;
-
+	if (npages) {
 		new.rmap = vmalloc(npages * sizeof(struct page *));
 
 		if (!new.rmap)
 			goto out_unlock;
 
-		memset(new.phys_mem, 0, npages * sizeof(struct page *));
 		memset(new.rmap, 0, npages * sizeof(*new.rmap));
-		if (user_alloc) {
-			new.user_alloc = 1;
+
+		if (user_alloc)
 			new.userspace_addr = mem->userspace_addr;
-		} else {
-			for (i = 0; i < npages; ++i) {
-				new.phys_mem[i] = alloc_page(GFP_HIGHUSER
-							     | __GFP_ZERO);
-				if (!new.phys_mem[i])
-					goto out_unlock;
-			}
+		else {
+			down_write(&current->mm->mmap_sem);
+			new.userspace_addr = do_mmap(NULL, 0,
+						     npages * PAGE_SIZE,
+						     PROT_READ | PROT_WRITE,
+						     MAP_SHARED | MAP_ANONYMOUS,
+						     0);
+			up_write(&current->mm->mmap_sem);
+
+			if (new.userspace_addr > -1024UL)
+				goto out_unlock;
 		}
 	}
 
@@ -1029,6 +1008,8 @@ struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
 {
 	struct kvm_memory_slot *slot;
+	struct page *page[1];
+	int npages;
 
 	gfn = unalias_gfn(kvm, gfn);
 	slot = __gfn_to_memslot(kvm, gfn);
@@ -1036,24 +1017,19 @@ struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
 		get_page(bad_page);
 		return bad_page;
 	}
-	if (slot->user_alloc) {
-		struct page *page[1];
-		int npages;
-
-		down_read(&current->mm->mmap_sem);
-		npages = get_user_pages(current, current->mm,
-					slot->userspace_addr
-					+ (gfn - slot->base_gfn) * PAGE_SIZE, 1,
-					1, 0, page, NULL);
-		up_read(&current->mm->mmap_sem);
-		if (npages != 1) {
-			get_page(bad_page);
-			return bad_page;
-		}
-		return page[0];
+
+	down_read(&current->mm->mmap_sem);
+	npages = get_user_pages(current, current->mm,
+				slot->userspace_addr
+				+ (gfn - slot->base_gfn) * PAGE_SIZE, 1,
+				1, 0, page, NULL);
+	up_read(&current->mm->mmap_sem);
+	if (npages != 1) {
+		get_page(bad_page);
+		return bad_page;
 	}
-	get_page(slot->phys_mem[gfn - slot->base_gfn]);
-	return slot->phys_mem[gfn - slot->base_gfn];
+
+	return page[0];
 }
 EXPORT_SYMBOL_GPL(gfn_to_page);
 

[-- Attachment #3: Type: text/plain, Size: 314 bytes --]

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/

[-- Attachment #4: Type: text/plain, Size: 186 bytes --]

_______________________________________________
kvm-devel mailing list
kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/kvm-devel

  parent reply	other threads:[~2007-10-14  0:36 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-13  2:10 [PATCH 3/4] Swapping Izik Eidus
     [not found] ` <47102919.6070802-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-10-13 20:04   ` Anthony Liguori
     [not found]     ` <471124D4.3090901-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
2007-10-13 20:13       ` Izik Eidus
     [not found]         ` <471126D9.4030204-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-10-13 20:41           ` Izik Eidus
     [not found]             ` <47112D66.4020500-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-10-13 23:17               ` Anthony Liguori
     [not found]                 ` <47115207.3090909-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
2007-10-13 23:26                   ` Izik Eidus
     [not found]                     ` <4711542F.2060306-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-10-13 23:35                       ` Anthony Liguori
     [not found]                         ` <4711563D.8020000-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
2007-10-13 23:38                           ` Izik Eidus
     [not found]                             ` <471156D9.3030700-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-10-14  0:36                               ` Anthony Liguori [this message]
     [not found]                                 ` <47116486.4070609-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
2007-10-14  6:12                                   ` Izik Eidus
2007-10-15 19:26                                   ` Anthony Liguori
     [not found]                                     ` <4713BED9.7000302-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
2007-10-15 20:01                                       ` Izik Eidus
     [not found]                                         ` <4713C700.5010304-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-10-15 20:03                                           ` Izik Eidus
2007-10-14  6:02                           ` Avi Kivity
     [not found]                             ` <4711B101.7070305-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-10-14  7:46                               ` Avi Kivity
2007-10-14 14:57                               ` Anthony Liguori
     [not found]                                 ` <47122E57.7060308-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
2007-10-14 16:08                                   ` Avi Kivity
     [not found]                                     ` <47123EFA.1010808-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-10-14 17:11                                       ` Anthony Liguori
     [not found]                                         ` <47124DBB.9030708-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
2007-10-14 17:16                                           ` Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47116486.4070609@codemonkey.ws \
    --to=anthony-rdkfgonbjusknkdkm+me6a@public.gmane.org \
    --cc=izike-atKUWr5tajBWk0Htik3J/w@public.gmane.org \
    --cc=kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox