From patchwork Fri Oct 30 18:26:56 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 538473 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 25F2A14139E for ; Sat, 31 Oct 2015 05:26:50 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965867AbbJ3S0f (ORCPT ); Fri, 30 Oct 2015 14:26:35 -0400 Received: from foss.arm.com ([217.140.101.70]:45264 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965301AbbJ3S0d (ORCPT ); Fri, 30 Oct 2015 14:26:33 -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 3B11346B; Fri, 30 Oct 2015 11:26:25 -0700 (PDT) Received: from e104803-lin.lan (unknown [10.1.203.153]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 229573F308; Fri, 30 Oct 2015 11:26:31 -0700 (PDT) From: Andre Przywara To: will.deacon@arm.com, kvm@vger.kernel.org Cc: kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, kvm-ppc@vger.kernel.org Subject: [PATCH 3/7] powerpc: use read_file() in kernel and initrd loading Date: Fri, 30 Oct 2015 18:26:56 +0000 Message-Id: <1446229620-28088-4-git-send-email-andre.przywara@arm.com> X-Mailer: git-send-email 2.5.1 In-Reply-To: <1446229620-28088-1-git-send-email-andre.przywara@arm.com> References: <1446229620-28088-1-git-send-email-andre.przywara@arm.com> Sender: kvm-ppc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm-ppc@vger.kernel.org Replace the unsafe read-loops in the powerpc kernel image loading function with our new and safe read_file() wrapper. This should fix random fails in kernel image loading, especially from pipes and sockets. Signed-off-by: Andre Przywara --- powerpc/kvm.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/powerpc/kvm.c b/powerpc/kvm.c index 13bba30..2b0bddd 100644 --- a/powerpc/kvm.c +++ b/powerpc/kvm.c @@ -162,19 +162,22 @@ bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd, { void *p; void *k_start; - void *i_start; - int nr; + ssize_t filesize; if (lseek(fd_kernel, 0, SEEK_SET) < 0) die_perror("lseek"); p = k_start = guest_flat_to_host(kvm, KERNEL_LOAD_ADDR); - while ((nr = read(fd_kernel, p, 65536)) > 0) - p += nr; - - pr_info("Loaded kernel to 0x%x (%ld bytes)", KERNEL_LOAD_ADDR, (long)(p-k_start)); + filesize = read_file(fd_kernel, p, INITRD_LOAD_ADDR - KERNEL_LOAD_ADDR); + if (filesize < 0) { + if (errno == ENOMEM) + die("Kernel overlaps initrd!"); + die_perror("kernel read"); + } + pr_info("Loaded kernel to 0x%x (%ld bytes)", KERNEL_LOAD_ADDR, + filesize); if (fd_initrd != -1) { if (lseek(fd_initrd, 0, SEEK_SET) < 0) die_perror("lseek"); @@ -183,19 +186,20 @@ bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd, die("Kernel overlaps initrd!"); /* Round up kernel size to 8byte alignment, and load initrd right after. */ - i_start = p = guest_flat_to_host(kvm, INITRD_LOAD_ADDR); - - while (((nr = read(fd_initrd, p, 65536)) > 0) && - p < (kvm->ram_start + kvm->ram_size)) - p += nr; - - if (p >= (kvm->ram_start + kvm->ram_size)) - die("initrd too big to contain in guest RAM.\n"); + p = guest_flat_to_host(kvm, INITRD_LOAD_ADDR); + + filesize = read_file(fd_initrd, p, + (kvm->ram_start + kvm->ram_size) - p); + if (filesize < 0) { + if (errno == ENOMEM) + die("initrd too big to contain in guest RAM.\n"); + die_perror("initrd read"); + } pr_info("Loaded initrd to 0x%x (%ld bytes)", - INITRD_LOAD_ADDR, (long)(p-i_start)); + INITRD_LOAD_ADDR, filesize); kvm->arch.initrd_gra = INITRD_LOAD_ADDR; - kvm->arch.initrd_size = p-i_start; + kvm->arch.initrd_size = filesize; } else { kvm->arch.initrd_size = 0; }