summaryrefslogtreecommitdiff
path: root/kexec/kexec.c
diff options
context:
space:
mode:
authorMichael Holzheu <holzheu@linux.vnet.ibm.com>2015-10-30 16:02:04 +0100
committerSimon Horman <horms@verge.net.au>2015-11-09 10:58:53 +0900
commit95741713e790fa6bde7780bbfb772ad88e81a744 (patch)
treeee32757b0773ee921605002ecfb88d736b189edb /kexec/kexec.c
parent8d8c6bcee59306482d86d7c524f511a08ad02de6 (diff)
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 The following illustrates the use case that we have on s390x: 1) Boot a 4 GB Linux system 2) Copy kernel and 1,5 GB ramdisk from external source into tmpfs (ram) 3) Use kexec to boot kernel with ramdisk Therefore for kexec runtime we need: 1,5 GB (tmpfs) + 1,5 GB (kexec malloc) + 1,5 GB (kernel memory) = 4,5 GB This patch introduces slurp_file_mmap() which for "normal" files uses mmap() instead of malloc()/read(). This reduces the runtime memory consumption of the kexec tool as follows: 1,5 GB (tmpfs) + 1,5 GB (kernel memory) = 3 GB Signed-off-by: Michael Holzheu <holzheu@linux.vnet.ibm.com> Reviewed-by: Dave Young <dyoung@redhat.com> Signed-off-by: Simon Horman <horms@verge.net.au>
Diffstat (limited to 'kexec/kexec.c')
-rw-r--r--kexec/kexec.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/kexec/kexec.c b/kexec/kexec.c
index b9f1816..ca1e81d 100644
--- 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
@@ -514,7 +515,8 @@ static char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread)
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 +554,17 @@ char *slurp_file(const char *filename, off_t *r_size)
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);
} else {
size = stats.st_size;
+ if (use_mmap) {
+ buf = mmap(NULL, size, PROT_READ|PROT_WRITE,
+ MAP_PRIVATE, fd, 0);
+ nread = size;
+ } else {
+ buf = slurp_fd(fd, filename, size, &nread);
+ }
}
-
- buf = slurp_fd(fd, filename, size, &nread);
if (!buf)
die("Cannot read %s", filename);
@@ -567,6 +575,23 @@ char *slurp_file(const char *filename, off_t *r_size)
return buf;
}
+/*
+ * Read file into malloced buffer.
+ */
+char *slurp_file(const char *filename, off_t *r_size)
+{
+ return slurp_file_generic(filename, r_size, 0);
+}
+
+/*
+ * Map "normal" file or read "character device" into malloced buffer.
+ * You must not use free, realloc, etc. for the returned buffer.
+ */
+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. */