[PATCH] kexec: use mmap instead of read for slurp_file()

Geoff Levand geoff at infradead.org
Wed Oct 14 17:05:52 PDT 2015


Hi,

On Fri, 2015-09-04 at 14:11 +0200, Michael Holzheu wrote:
> 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() for non-character devices 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 at linux.vnet.ibm.com>
> ---
>  kexec/kexec.c | 8 +++++---
> 

>  		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;
> +> 	> 	> size = nread = stats.st_size;
> +> 	> 	> buf = mmap(NULL, size,

With this change the caller can't tell if buf was malloc'ed or mmaped.  The
only safe thing it can do is to not call free() on the returned buf, this will
lead to memory leakage for malloc'ed buffers.

-Geoff




More information about the kexec mailing list