From patchwork Sun Apr 4 19:34:09 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 51233 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A1825B7D47 for ; Thu, 29 Apr 2010 06:08:14 +1000 (EST) Received: from localhost ([127.0.0.1]:35328 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O7DYI-0001ST-9K for incoming@patchwork.ozlabs.org; Wed, 28 Apr 2010 16:08:10 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O7D6s-0005lH-3F for qemu-devel@nongnu.org; Wed, 28 Apr 2010 15:39:50 -0400 Received: from [140.186.70.92] (port=51708 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O7D6m-0005hA-9K for qemu-devel@nongnu.org; Wed, 28 Apr 2010 15:39:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O7D6k-0007V3-7k for qemu-devel@nongnu.org; Wed, 28 Apr 2010 15:39:44 -0400 Received: from are.twiddle.net ([75.149.56.221]:55981) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O7D6j-0007Ti-Sr for qemu-devel@nongnu.org; Wed, 28 Apr 2010 15:39:42 -0400 Received: by are.twiddle.net (Postfix, from userid 5000) id F022FB09; Wed, 28 Apr 2010 12:39:40 -0700 (PDT) Message-Id: <7b93becdec1552384732bb3814b7b0ea66a9471c.1272483393.git.rth@twiddle.net> In-Reply-To: References: From: Richard Henderson Date: Sun, 4 Apr 2010 12:34:09 -0700 To: qemu-devel@nongnu.org X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) Cc: Subject: [Qemu-devel] [PATCH 04/14] linux-user: Reduce lseek+reads while loading elf files. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Define BPRM_BUF_SIZE to 4k and read that amount initially. If the data we want from the binary is in this buffer, use it instead of reading from the file again. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 105 ++++++++++++++++++++--------------------------- linux-user/linuxload.c | 17 +++----- linux-user/qemu.h | 7 +++- 3 files changed, 58 insertions(+), 71 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index ab741fd..962f9ba 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1140,7 +1140,8 @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex, int interpreter_fd, - abi_ulong *interp_load_addr) + abi_ulong *interp_load_addr, + char bprm_buf[BPRM_BUF_SIZE]) { struct elf_phdr *elf_phdata = NULL; struct elf_phdr *eppnt; @@ -1183,17 +1184,15 @@ static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex, return ~((abi_ulong)0UL); } - retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET); - if(retval >= 0) { - retval = read(interpreter_fd, - (char *) elf_phdata, - sizeof(struct elf_phdr) * interp_elf_ex->e_phnum); - } - if (retval < 0) { - perror("load_elf_interp"); - exit(-1); - free (elf_phdata); - return retval; + i = interp_elf_ex->e_phnum * sizeof(struct elf_phdr); + if (interp_elf_ex->e_phoff + i <= BPRM_BUF_SIZE) { + memcpy(elf_phdata, bprm_buf + interp_elf_ex->e_phoff, i); + } else { + retval = pread(interpreter_fd, elf_phdata, i, interp_elf_ex->e_phoff); + if (retval != i) { + perror("load_elf_interp"); + exit(-1); + } } #ifdef BSWAP_NEEDED eppnt = elf_phdata; @@ -1451,17 +1450,15 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, return -ENOMEM; } - retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET); - if(retval > 0) { - retval = read(bprm->fd, (char *) elf_phdata, - elf_ex.e_phentsize * elf_ex.e_phnum); - } - - if (retval < 0) { - perror("load_elf_binary"); - exit(-1); - free (elf_phdata); - return -errno; + i = elf_ex.e_phnum * sizeof(struct elf_phdr); + if (elf_ex.e_phoff + i <= BPRM_BUF_SIZE) { + memcpy(elf_phdata, bprm->buf + elf_ex.e_phoff, i); + } else { + retval = pread(bprm->fd, (char *) elf_phdata, i, elf_ex.e_phoff); + if (retval != i) { + perror("load_elf_binary"); + exit(-1); + } } #ifdef BSWAP_NEEDED @@ -1505,13 +1502,16 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, return -ENOMEM; } - retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET); - if(retval >= 0) { - retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz); - } - if(retval < 0) { - perror("load_elf_binary2"); - exit(-1); + if (elf_ppnt->p_offset + elf_ppnt->p_filesz <= BPRM_BUF_SIZE) { + memcpy(elf_interpreter, bprm->buf + elf_ppnt->p_offset, + elf_ppnt->p_filesz); + } else { + retval = pread(bprm->fd, elf_interpreter, elf_ppnt->p_filesz, + elf_ppnt->p_offset); + if (retval != elf_ppnt->p_filesz) { + perror("load_elf_binary2"); + exit(-1); + } } /* If the program interpreter is one of these two, @@ -1525,39 +1525,24 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, ibcs2_interpreter = 1; } -#if 0 - printf("Using ELF interpreter %s\n", path(elf_interpreter)); -#endif - if (retval >= 0) { - retval = open(path(elf_interpreter), O_RDONLY); - if(retval >= 0) { - interpreter_fd = retval; - } - else { - perror(elf_interpreter); - exit(-1); - /* retval = -errno; */ - } - } + retval = open(path(elf_interpreter), O_RDONLY); + if (retval < 0) { + perror(elf_interpreter); + exit(-1); + } + interpreter_fd = retval; - if (retval >= 0) { - retval = lseek(interpreter_fd, 0, SEEK_SET); - if(retval >= 0) { - retval = read(interpreter_fd,bprm->buf,128); - } - } - if (retval >= 0) { - interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */ - interp_elf_ex = *((struct elfhdr *) bprm->buf); /* elf exec-header */ - } - if (retval < 0) { + retval = read(interpreter_fd, bprm->buf, BPRM_BUF_SIZE); + if (retval < 0) { perror("load_elf_binary3"); exit(-1); - free (elf_phdata); - free(elf_interpreter); - close(bprm->fd); - return retval; } + if (retval < BPRM_BUF_SIZE) { + memset(bprm->buf, 0, BPRM_BUF_SIZE - retval); + } + + interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */ + interp_elf_ex = *((struct elfhdr *) bprm->buf); /* elf exec-header */ } elf_ppnt++; } @@ -1746,7 +1731,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, } else if (interpreter_type & 2) { elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd, - &interp_load_addr); + &interp_load_addr, bprm->buf); } reloc_func_desc = interp_load_addr; diff --git a/linux-user/linuxload.c b/linux-user/linuxload.c index 13ad9aa..9ee27c3 100644 --- a/linux-user/linuxload.c +++ b/linux-user/linuxload.c @@ -96,18 +96,16 @@ static int prepare_binprm(struct linux_binprm *bprm) } } - retval = lseek(bprm->fd, 0L, SEEK_SET); - if(retval >= 0) { - retval = read(bprm->fd, bprm->buf, 128); - } - if(retval < 0) { + retval = read(bprm->fd, bprm->buf, BPRM_BUF_SIZE); + if (retval < 0) { perror("prepare_binprm"); exit(-1); - /* return(-errno); */ } - else { - return(retval); + if (retval < BPRM_BUF_SIZE) { + /* Make sure the rest of the loader won't read garbage. */ + memset(bprm->buf + retval, 0, BPRM_BUF_SIZE - retval); } + return retval; } /* Construct the envp and argv tables on the target stack. */ @@ -163,8 +161,7 @@ int loader_exec(const char * filename, char ** argv, char ** envp, int i; bprm->p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int); - for (i=0 ; ipage[i] = NULL; + memset(bprm->page, 0, sizeof(bprm->page)); retval = open(filename, O_RDONLY); if (retval < 0) return retval; diff --git a/linux-user/qemu.h b/linux-user/qemu.h index dab3597..f7c8b65 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -31,6 +31,7 @@ * task_struct fields in the kernel */ struct image_info { + abi_ulong load_bias; abi_ulong load_addr; abi_ulong start_code; abi_ulong end_code; @@ -143,12 +144,16 @@ extern unsigned long mmap_min_addr; */ #define MAX_ARG_PAGES 33 +/* Read a good amount of data initially, to hopefully get all the + program headers loaded. */ +#define BPRM_BUF_SIZE 4096 + /* * This structure is used to hold the arguments that are * used when loading binaries. */ struct linux_binprm { - char buf[128]; + char buf[BPRM_BUF_SIZE] __attribute__((aligned)); void *page[MAX_ARG_PAGES]; abi_ulong p; int fd;