Kexec Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Michael Holzheu <holzheu@linux.vnet.ibm.com>
To: Dave Young <dyoung@redhat.com>, Simon Horman <horms@verge.net.au>
Cc: stefan.roscher@de.ibm.com, kexec@lists.infradead.org
Subject: [PATCH v2] kexec/s390x: use mmap instead of read for slurp_file()
Date: Tue, 27 Oct 2015 13:35:20 +0100	[thread overview]
Message-ID: <20151027133520.30653ba3@holzheu> (raw)
In-Reply-To: <20151026073139.GB2916@dhcp-128-73.nay.redhat.com>

On Mon, 26 Oct 2015 15:31:39 +0800
Dave Young <dyoung@redhat.com> wrote:

[snip]

> IMHO adding a slurp_file_mmap for s390x use is a better way since the
> huge initramfs is not a general case.

Ok, what about the following patch:
---
[PATCH] kexec/s390x: use mmap instead of read for slurp_file()

The slurp_fd() function allocates memory and uses the read() system call.
This results in double memory consumption for image and initrd:

 1) Memory allocated in user space by the kexec tool
 2) Memory allocated in kernel by the kexec() system call

Therefore use mmap() to reduce the runtime memory consumption of the kexec tool.

The following use case illustrates the usefulness of this patch a bit more:

 1) Boot a 4 GB Linux system
 2) Read kernel and 1,5 GB ramdisk from external source into local
    tmpfs (ram)
 3) kexec the kernel and ramdisk

 Without this patch for the kexec runtime we need:

 1,5 GB (tmpfs) + 1,5 GB (kexec malloc) + 1,5 GB (kernel memory) = 4,5 GB

Signed-off-by: Michael Holzheu <holzheu@linux.vnet.ibm.com>
---
 kexec/arch/s390/kexec-image.c |    2 +-
 kexec/kexec.c                 |   40 ++++++++++++++++++++++++++++++++++------
 kexec/kexec.h                 |    1 +
 3 files changed, 36 insertions(+), 7 deletions(-)

--- a/kexec/arch/s390/kexec-image.c
+++ b/kexec/arch/s390/kexec-image.c
@@ -101,7 +101,7 @@ image_s390_load(int argc, char **argv, c
 	 * we load the ramdisk directly behind the image with 1 MiB alignment.
 	 */
 	if (ramdisk) {
-		rd_buffer = slurp_file(ramdisk, &ramdisk_len);
+		rd_buffer = slurp_file_mmap(ramdisk, &ramdisk_len);
 		if (rd_buffer == NULL) {
 			fprintf(stderr, "Could not read ramdisk.\n");
 			return -1;
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -29,6 +29,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/reboot.h>
+#include <sys/mman.h>
 #include <unistd.h>
 #include <fcntl.h>
 #ifndef _O_BINARY
@@ -481,13 +482,19 @@ static int add_backup_segments(struct ke
 	return 0;
 }
 
-static char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread)
+static char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread,
+		      int use_mmap)
 {
 	char *buf;
 	off_t progress;
 	ssize_t result;
 
-	buf = xmalloc(size);
+	if (use_mmap) {
+		buf = mmap(NULL, size, PROT_READ|PROT_WRITE,
+			   MAP_ANON|MAP_PRIVATE, -1, 0);
+	} else {
+		buf = xmalloc(size);
+	}
 	progress = 0;
 	while (progress < size) {
 		result = read(fd, buf + progress, size - progress);
@@ -496,7 +503,10 @@ static char *slurp_fd(int fd, const char
 				continue;
 			fprintf(stderr, "Read on %s failed: %s\n", filename,
 				strerror(errno));
-			free(buf);
+			if (use_mmap)
+				munmap(buf, size);
+			else
+				free(buf);
 			close(fd);
 			return NULL;
 		}
@@ -514,7 +524,8 @@ static char *slurp_fd(int fd, const char
 	return buf;
 }
 
-char *slurp_file(const char *filename, off_t *r_size)
+static char *slurp_file_generic(const char *filename, off_t *r_size,
+				int use_mmap)
 {
 	int fd;
 	char *buf;
@@ -552,11 +563,18 @@ char *slurp_file(const char *filename, o
 		if (err < 0)
 			die("Can not seek to the begin of file %s: %s\n",
 					filename, strerror(errno));
+		buf = slurp_fd(fd, filename, size, &nread, use_mmap);
 	} else {
 		size = stats.st_size;
+		if (use_mmap) {
+			buf = mmap(NULL, size, PROT_READ | PROT_WRITE,
+				   MAP_PRIVATE, fd, 0);
+			nread = stats.st_size;
+		} else {
+			buf = slurp_fd(fd, filename, size, &nread, 0);
+		}
 	}
 
-	buf = slurp_fd(fd, filename, size, &nread);
 	if (!buf)
 		die("Cannot read %s", filename);
 
@@ -567,6 +585,16 @@ char *slurp_file(const char *filename, o
 	return buf;
 }
 
+char *slurp_file(const char *filename, off_t *r_size)
+{
+	return slurp_file_generic(filename, r_size, 0);
+}
+
+char *slurp_file_mmap(const char *filename, off_t *r_size)
+{
+	return slurp_file_generic(filename, r_size, 1);
+}
+
 /* This functions reads either specified number of bytes from the file or
    lesser if EOF is met. */
 
@@ -583,7 +611,7 @@ char *slurp_file_len(const char *filenam
 		return 0;
 	}
 
-	return slurp_fd(fd, filename, size, nread);
+	return slurp_fd(fd, filename, size, nread, 0);
 }
 
 char *slurp_decompress_file(const char *filename, off_t *r_size)
--- a/kexec/kexec.h
+++ b/kexec/kexec.h
@@ -253,6 +253,7 @@ extern void die(const char *fmt, ...)
 extern void *xmalloc(size_t size);
 extern void *xrealloc(void *ptr, size_t size);
 extern char *slurp_file(const char *filename, off_t *r_size);
+extern char *slurp_file_mmap(const char *filename, off_t *r_size);
 extern char *slurp_file_len(const char *filename, off_t size, off_t *nread);
 extern char *slurp_decompress_file(const char *filename, off_t *r_size);
 extern unsigned long virt_to_phys(unsigned long addr);


_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

  reply	other threads:[~2015-10-27 12:46 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-23  3:10 [PATCH] Revert "kexec: use mmap instead of read for slurp_file()" Dave Young
2015-10-23 15:09 ` Michael Holzheu
2015-10-26  7:31   ` Dave Young
2015-10-27 12:35     ` Michael Holzheu [this message]
2015-10-28  6:46       ` [PATCH v2] kexec/s390x: use mmap instead of read for slurp_file() Dave Young
2015-10-28  9:57         ` Michael Holzheu
2015-10-29  6:37           ` Dave Young
2015-10-29 15:26             ` Michael Holzheu
2015-10-30  2:03               ` Dave Young
2015-10-30 10:13                 ` Michael Holzheu
2015-10-30 13:39                   ` Dave Young
2015-10-30 15:02                     ` [PATCH v3] " Michael Holzheu
2015-11-09  1:59                       ` Simon Horman
2015-10-26  4:30 ` [PATCH] Revert "kexec: use mmap instead of read for slurp_file()" Simon Horman
2015-11-03  0:11 ` Geoff Levand
2015-11-03  1:15   ` Dave Young

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=20151027133520.30653ba3@holzheu \
    --to=holzheu@linux.vnet.ibm.com \
    --cc=dyoung@redhat.com \
    --cc=horms@verge.net.au \
    --cc=kexec@lists.infradead.org \
    --cc=stefan.roscher@de.ibm.com \
    /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