All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rusty Russell <rusty-8n+1lVoiYb80n/F98K4Iww@public.gmane.org>
To: virtualization-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
Cc: kvm-devel
	<kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org>,
	lguest <lguest-mnsaURCQ41sdnm+yROfE0A@public.gmane.org>
Subject: [RFC PATCH 1/5] lguest: mmap backing file
Date: Thu, 20 Mar 2008 17:05:44 +1100	[thread overview]
Message-ID: <200803201705.44422.rusty@rustcorp.com.au> (raw)
In-Reply-To: <200803201659.14344.rusty-8n+1lVoiYb80n/F98K4Iww@public.gmane.org>

From: Paul TBBle Hampson <Paul.Hampson-vM6MUUi4OUAAvxtiuMwx3w@public.gmane.org>

This creates a file in $HOME/.lguest/ to directly back the RAM and DMA memory
mappings created by map_zeroed_pages.

Signed-off-by: Rusty Russell <rusty-8n+1lVoiYb80n/F98K4Iww@public.gmane.org>
---
 Documentation/lguest/lguest.c |   59 ++++++++++++++++++++++++++++--------------
 1 file changed, 40 insertions(+), 19 deletions(-)

diff --git a/Documentation/lguest/lguest.c b/Documentation/lguest/lguest.c
--- a/Documentation/lguest/lguest.c
+++ b/Documentation/lguest/lguest.c
@@ -236,19 +236,51 @@ static int open_or_die(const char *name,
 	return fd;
 }
 
-/* map_zeroed_pages() takes a number of pages. */
+/* unlink_memfile() removes the backing file for the Guest's memory, if we exit
+ * cleanly. */
+static char memfile_path[PATH_MAX];
+
+static void unlink_memfile(void)
+{
+	unlink(memfile_path);
+}
+
+/* map_zeroed_pages() takes a number of pages, and creates a mapping file where
+ * this Guest's memory lives. */
 static void *map_zeroed_pages(unsigned int num)
 {
-	int fd = open_or_die("/dev/zero", O_RDONLY);
+	int fd;
 	void *addr;
 
-	/* We use a private mapping (ie. if we write to the page, it will be
-	 * copied). */
+	/* We create a .lguest directory in the user's home, to put the memory
+	 * files into. */
+	snprintf(memfile_path, PATH_MAX, "%s/.lguest", getenv("HOME") ?: "");
+	if (mkdir(memfile_path, S_IRWXU) != 0 && errno != EEXIST)
+		err(1, "Creating directory %s", memfile_path);
+
+	/* Name the memfiles by the process ID of this launcher. */
+	snprintf(memfile_path, PATH_MAX, "%s/.lguest/%u",
+		 getenv("HOME") ?: "", getpid());
+	fd = open(memfile_path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
+	if (fd < 0)
+		err(1, "Creating memory backing file %s", memfile_path);
+
+	/* Make sure we remove it when we're finished. */
+	atexit(unlink_memfile);
+
+	/* Now, we opened it with O_TRUNC, so the file is 0 bytes long.  Here
+	 * we expand it to the length we need, and it will be filled with
+	 * zeroes. */
+	if (ftruncate(fd, num * getpagesize()) != 0)
+		err(1, "Truncating file %s %u pages", memfile_path, num);
+
+	/* We use a shared mapping, so others can share with us. */
 	addr = mmap(NULL, getpagesize() * num,
-		    PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, fd, 0);
+		    PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED, fd, 0);
 	if (addr == MAP_FAILED)
 		err(1, "Mmaping %u pages of /dev/zero", num);
 
+	verbose("Memory backing file is %s @ %p\n", memfile_path, addr);
 	return addr;
 }
 
@@ -263,23 +295,12 @@ static void *get_pages(unsigned int num)
 	return addr;
 }
 
-/* This routine is used to load the kernel or initrd.  It tries mmap, but if
- * that fails (Plan 9's kernel file isn't nicely aligned on page boundaries),
- * it falls back to reading the memory in. */
+/* This routine is used to load the kernel or initrd.  We used to mmap, but now
+ * we simply read it in, so it will be present in the shared underlying
+ * file. */
 static void map_at(int fd, void *addr, unsigned long offset, unsigned long len)
 {
 	ssize_t r;
-
-	/* We map writable even though for some segments are marked read-only.
-	 * The kernel really wants to be writable: it patches its own
-	 * instructions.
-	 *
-	 * MAP_PRIVATE means that the page won't be copied until a write is
-	 * done to it.  This allows us to share untouched memory between
-	 * Guests. */
-	if (mmap(addr, len, PROT_READ|PROT_WRITE|PROT_EXEC,
-		 MAP_FIXED|MAP_PRIVATE, fd, offset) != MAP_FAILED)
-		return;
 
 	/* pread does a seek and a read in one shot: saves a few lines. */
 	r = pread(fd, addr, len, offset);

  parent reply	other threads:[~2008-03-20  6:05 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-20  5:59 [RFC PATCH 0/4] Inter-guest virtio I/O example with lguest Rusty Russell
2008-03-20  6:05 ` [RFC PATCH 1/5] lguest: mmap backing file Rusty Russell
     [not found] ` <200803201659.14344.rusty-8n+1lVoiYb80n/F98K4Iww@public.gmane.org>
2008-03-20  6:05   ` Rusty Russell [this message]
2008-03-20  6:22     ` [RFC PATCH 2/5] lguest: Encapsulate Guest memory ready for dealing with other Guests Rusty Russell
2008-03-20  8:16     ` [Lguest] [RFC PATCH 1/5] lguest: mmap backing file Tim Post
2008-03-20  8:16     ` Tim Post
     [not found]       ` <1206000960.6873.124.camel-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
2008-03-20 14:07         ` Paul TBBle Hampson
2008-03-21  0:29         ` Rusty Russell
2008-03-20 14:07       ` [Lguest] " Paul TBBle Hampson
2008-03-21  0:29       ` Rusty Russell
2008-03-20 14:04     ` [kvm-devel] " Anthony Liguori
     [not found]     ` <200803201705.44422.rusty-8n+1lVoiYb80n/F98K4Iww@public.gmane.org>
2008-03-20  6:22       ` [RFC PATCH 2/5] lguest: Encapsulate Guest memory ready for dealing with other Guests Rusty Russell
2008-03-20  6:36         ` [RFC PATCH 3/5] lguest: separate out virtqueue info from device info Rusty Russell
     [not found]           ` <200803201736.01883.rusty-8n+1lVoiYb80n/F98K4Iww@public.gmane.org>
2008-03-20  6:40             ` [RFC PATCH 4/5] lguest: ignore bad virtqueues Rusty Russell
2008-03-20  6:45               ` [RFC PATCH 5/5] lguest: Inter-guest networking Rusty Russell
2008-03-20  6:45               ` Rusty Russell
2008-03-20  6:40           ` [RFC PATCH 4/5] lguest: ignore bad virtqueues Rusty Russell
2008-03-20  6:36         ` [RFC PATCH 3/5] lguest: separate out virtqueue info from device info Rusty Russell
2008-03-20 14:04       ` [kvm-devel] [RFC PATCH 1/5] lguest: mmap backing file Anthony Liguori
2008-03-20 14:32         ` [Lguest] " Paul TBBle Hampson
     [not found]         ` <47E26EE1.5030706-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
2008-03-20 14:32           ` Paul TBBle Hampson
2008-03-20 15:07           ` Avi Kivity
2008-03-20 15:24             ` Anthony Liguori
2008-03-20 15:24             ` Anthony Liguori
2008-03-20 22:12           ` [kvm-devel] " Rusty Russell
2008-03-20 23:46             ` Anthony Liguori
2008-03-23  9:11               ` Avi Kivity
2008-03-23  9:11               ` [kvm-devel] " Avi Kivity
2008-03-20 23:46             ` Anthony Liguori
2008-03-20 15:07         ` Avi Kivity
2008-03-20 22:12         ` Rusty Russell
2008-03-20  6:54   ` [kvm-devel] [RFC PATCH 0/4] Inter-guest virtio I/O example with lguest Avi Kivity
     [not found]     ` <47E20A35.2000600-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2008-03-20 13:55       ` Anthony Liguori
2008-03-20 14:27         ` Avi Kivity
     [not found]         ` <47E26CC1.8080900-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
2008-03-20 14:27           ` Avi Kivity
2008-03-20 14:39             ` Anthony Liguori
     [not found]             ` <47E27461.4090404-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2008-03-20 14:39               ` Anthony Liguori
2008-03-20 14:55                 ` Avi Kivity
2008-03-20 15:05                   ` [kvm-devel] " Anthony Liguori
2008-03-20 15:05                   ` Anthony Liguori
2008-03-20 15:36                     ` Avi Kivity
2008-03-20 15:52                       ` [kvm-devel] " Anthony Liguori
     [not found]                       ` <47E28482.9010501-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2008-03-20 15:52                         ` Anthony Liguori
2008-03-20 15:36                     ` Avi Kivity
2008-03-20 14:55                 ` Avi Kivity
2008-03-20 13:55     ` Anthony Liguori
2008-03-20 22:14     ` Rusty Russell
2008-03-20 22:14     ` Rusty Russell
2008-03-20 14:11   ` [kvm-devel] " Anthony Liguori
2008-03-23 12:05     ` Rusty Russell
2008-03-23 12:05     ` Rusty Russell
2008-03-20  6:54 ` [kvm-devel] " Avi Kivity
2008-03-20 14:11 ` Anthony Liguori

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=200803201705.44422.rusty@rustcorp.com.au \
    --to=rusty-8n+1lvoiyb80n/f98k4iww@public.gmane.org \
    --cc=kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org \
    --cc=lguest-mnsaURCQ41sdnm+yROfE0A@public.gmane.org \
    --cc=virtualization-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.