From patchwork Thu Jul 30 10:52:30 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 6901351 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 15414C05AC for ; Thu, 30 Jul 2015 10:52:59 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 39FE1202E5 for ; Thu, 30 Jul 2015 10:52:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 304A420303 for ; Thu, 30 Jul 2015 10:52:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755443AbbG3Kwp (ORCPT ); Thu, 30 Jul 2015 06:52:45 -0400 Received: from foss.arm.com ([217.140.101.70]:40043 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755335AbbG3Kwn (ORCPT ); Thu, 30 Jul 2015 06:52:43 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 315FB75; Thu, 30 Jul 2015 03:52:51 -0700 (PDT) Received: from e104803-lin.lan (unknown [10.1.203.153]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 4D4413F21A; Thu, 30 Jul 2015 03:52:42 -0700 (PDT) From: Andre Przywara To: will.deacon@arm.com, kvm@vger.kernel.org Cc: marc.zyngier@arm.com, kvmarm@lists.cs.columbia.edu, kvm-ppc@vger.kernel.org Subject: [PATCH 13/14] MIPS: use read wrappers in kernel loading Date: Thu, 30 Jul 2015 11:52:30 +0100 Message-Id: <1438253551-2378-14-git-send-email-andre.przywara@arm.com> X-Mailer: git-send-email 2.3.5 In-Reply-To: <1438253551-2378-1-git-send-email-andre.przywara@arm.com> References: <1438253551-2378-1-git-send-email-andre.przywara@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Replace the unsafe read-loops used in the MIPS kernel image loading with our safe read_file() and read_in_full() wrappers. This should fix random fails in kernel image loading, especially from pipes and sockets. Signed-off-by: Andre Przywara --- mips/kvm.c | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/mips/kvm.c b/mips/kvm.c index d970ee0..2f0d61b 100644 --- a/mips/kvm.c +++ b/mips/kvm.c @@ -169,21 +169,27 @@ static bool load_flat_binary(struct kvm *kvm, int fd_kernel, const void *buf, { void *p; void *k_start; - int nr; + ssize_t kernel_size; p = k_start = guest_flat_to_host(kvm, KERNEL_LOAD_ADDR); memcpy(p, buf, buflen); p += buflen; - while ((nr = read(fd_kernel, p, 65536)) > 0) - p += nr; + kernel_size = read_file(fd_kernel, p, + kvm->cfg.ram_size - KERNEL_LOAD_ADDR); + if (kernel_size == -1) { + if (errno == ENOMEM) + die("kernel too big for guest memory"); + else + die_perror("kernel read"); + } kvm->arch.is64bit = true; kvm->arch.entry_point = 0xffffffff81000000ull; - pr_info("Loaded kernel to 0x%x (%ld bytes)", KERNEL_LOAD_ADDR, - (long int)(p - k_start)); + pr_info("Loaded kernel to 0x%x (%zd bytes)", KERNEL_LOAD_ADDR, + kernel_size); return true; } @@ -199,7 +205,6 @@ static bool kvm__arch_get_elf_64_info(Elf64_Ehdr *ehdr, int fd_kernel, struct kvm__arch_elf_info *ei) { int i; - size_t nr; Elf64_Phdr phdr; if (ehdr->e_phentsize != sizeof(phdr)) { @@ -214,8 +219,7 @@ static bool kvm__arch_get_elf_64_info(Elf64_Ehdr *ehdr, int fd_kernel, phdr.p_type = PT_NULL; for (i = 0; i < ehdr->e_phnum; i++) { - nr = read(fd_kernel, &phdr, sizeof(phdr)); - if (nr != sizeof(phdr)) { + if (read_in_full(fd_kernel, &phdr, sizeof(phdr)) != sizeof(phdr)) { pr_info("Couldn't read %d bytes for ELF PHDR.", (int)sizeof(phdr)); return false; } @@ -245,7 +249,6 @@ static bool kvm__arch_get_elf_32_info(Elf32_Ehdr *ehdr, int fd_kernel, struct kvm__arch_elf_info *ei) { int i; - size_t nr; Elf32_Phdr phdr; if (ehdr->e_phentsize != sizeof(phdr)) { @@ -260,8 +263,7 @@ static bool kvm__arch_get_elf_32_info(Elf32_Ehdr *ehdr, int fd_kernel, phdr.p_type = PT_NULL; for (i = 0; i < ehdr->e_phnum; i++) { - nr = read(fd_kernel, &phdr, sizeof(phdr)); - if (nr != sizeof(phdr)) { + if (read_in_full(fd_kernel, &phdr, sizeof(phdr)) != sizeof(phdr)) { pr_info("Couldn't read %d bytes for ELF PHDR.", (int)sizeof(phdr)); return false; } @@ -292,7 +294,6 @@ union ElfHeaders { static bool load_elf_binary(struct kvm *kvm, int fd_kernel, union ElfHeaders *eh) { - size_t nr; char *p; struct kvm__arch_elf_info ei; @@ -331,13 +332,9 @@ static bool load_elf_binary(struct kvm *kvm, int fd_kernel, pr_info("ELF Loading 0x%lx bytes from 0x%llx to 0x%llx", (unsigned long)ei.len, (unsigned long long)ei.offset, (unsigned long long)ei.load_addr); - do { - nr = read(fd_kernel, p, ei.len); - if (nr < 0) - die_perror("read"); - p += nr; - ei.len -= nr; - } while (ei.len); + + if (read_in_full(fd_kernel, p, ei.len) != (ssize_t)ei.len) + die_perror("read"); return true; }