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

Simon Horman horms at verge.net.au
Tue Sep 1 18:07:21 PDT 2015


Hi Michael,

sorry for the delay in responding to your patch.

On Tue, Aug 18, 2015 at 06:17:23PM +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 memory
> consumption of the kexec tool.

I'm not opposed to this change but I also don't see a strong motivation for
it.  I would imagine that the memory saving is not that large. And that the
memory consumption disappears when the presumably short-lived kexec process
exits.

> 
> Signed-off-by: Michael Holzheu <holzheu at linux.vnet.ibm.com>
> ---
>  kexec/kexec.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/kexec/kexec.c b/kexec/kexec.c
> index 8ce6885..fecf061 100644
> --- a/kexec/kexec.c
> +++ b/kexec/kexec.c
> @@ -26,6 +26,7 @@
>  #include <stdlib.h>
>  #include <errno.h>
>  #include <limits.h>
> +#include <sys/mman.h>
>  #include <sys/types.h>
>  #include <sys/stat.h>
>  #include <sys/reboot.h>
> @@ -552,11 +553,12 @@ 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;
> +		size = nread = stats.st_size;
> +		buf = mmap(NULL, size,
> +			   PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
>  	}
> -
> -	buf = slurp_fd(fd, filename, size, &nread);
>  	if (!buf)
>  		die("Cannot read %s", filename);
>  
> -- 
> 2.3.0
> 



More information about the kexec mailing list