From patchwork Thu Apr 21 12:34:02 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 724691 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p3LCYTeI023700 for ; Thu, 21 Apr 2011 12:34:29 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752248Ab1DUMe0 (ORCPT ); Thu, 21 Apr 2011 08:34:26 -0400 Received: from mail-wy0-f174.google.com ([74.125.82.174]:48946 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751693Ab1DUMeZ (ORCPT ); Thu, 21 Apr 2011 08:34:25 -0400 Received: by wya21 with SMTP id 21so1385623wya.19 for ; Thu, 21 Apr 2011 05:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer; bh=25vxvPL0tFzbAuLt1DXtuOiyBcsIUQlrbfsbh1fqgNM=; b=oR5Ruv1bfaQIzYq6wnN1FFsQ/0nk/hRj6lEB3sf3SIQIalLgSKhdkss2QNzuDw8mna n2KLzq0VDE4bPLZC54rjMW6w882WeSmsT5B6/TzOIZbmBU89osDwZzR9Oi3vMMXO5uam u4hk1ebqgRk6qzVoilrcMvxLhP2IazNK1j24A= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=Boo7fA9gFCRXW1qVZmIb5Ic6Hxdc50/XZcS7MBqG1hN5/E+T+n35pyWp26ajMTcNlw SGw1OgeKYap5N5fKSIEvNTDb07KbsuX+o0h1hJtlLRJgQSjvYbIxtaTwgg/BBazGyZ1u n1NC99XJBZ6P45onDWuURjDztkqfdnTAA5z2c= Received: by 10.227.139.14 with SMTP id c14mr8860599wbu.55.1303389264502; Thu, 21 Apr 2011 05:34:24 -0700 (PDT) Received: from localhost.localdomain (bzq-79-179-204-49.red.bezeqint.net [79.179.204.49]) by mx.google.com with ESMTPS id h11sm1168075wbc.9.2011.04.21.05.34.22 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 21 Apr 2011 05:34:23 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, prasadjoshi124@gmail.com, kvm@vger.kernel.org, Sasha Levin Subject: [PATCH] kvm tools: Use mmap to allocate guest RAM Date: Thu, 21 Apr 2011 15:34:02 +0300 Message-Id: <1303389242-29657-1-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.5.rc1 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Thu, 21 Apr 2011 12:34:29 +0000 (UTC) Using mmap to allocate the RAM enables us to allocate large blocks of memory for the guest, allowing to boot guests with a large RAM. Since if we try KVM_SET_USER_MEMORY_REGION with a large memory block KVM used to Oops (now it just fails), we had to move the actual ioctl to after we KVM_CREATE_VCPU. Signed-off-by: Sasha Levin --- tools/kvm/include/kvm/kvm.h | 2 +- tools/kvm/kvm-run.c | 5 ++++- tools/kvm/kvm.c | 21 +++++++++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h index fc76f98..3a44c53 100644 --- a/tools/kvm/include/kvm/kvm.h +++ b/tools/kvm/include/kvm/kvm.h @@ -25,7 +25,7 @@ struct kvm { struct interrupt_table interrupt_table; }; -struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size); +struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size, bool use_backing); void kvm__init_ram(struct kvm *self); void kvm__delete(struct kvm *self); bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename, diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c index fb8dcae7..a925a98 100644 --- a/tools/kvm/kvm-run.c +++ b/tools/kvm/kvm-run.c @@ -69,6 +69,7 @@ static const char *host_ip_addr; static const char *guest_mac; static const char *script; static bool single_step; +static bool use_backing; static bool readonly_image; extern bool ioport_debug; extern int active_console; @@ -84,6 +85,8 @@ static const struct option options[] = { OPT_GROUP("Basic options:"), OPT_INTEGER('\0', "cpus", &nrcpus, "Number of CPUs"), OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), + OPT_BOOLEAN('\0', "use-backing", &use_backing, + "Use a backing file for virtual RAM"), OPT_STRING('i', "image", &image_filename, "image", "Disk image"), OPT_BOOLEAN('\0', "readonly", &readonly_image, "Don't write changes back to disk image"), @@ -366,7 +369,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) term_init(); - kvm = kvm__init(kvm_dev, ram_size); + kvm = kvm__init(kvm_dev, ram_size, use_backing); memset(real_cmdline, 0, sizeof(real_cmdline)); strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 "); diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c index eb1a46c..5e312e9 100644 --- a/tools/kvm/kvm.c +++ b/tools/kvm/kvm.c @@ -170,11 +170,12 @@ void kvm__init_ram(struct kvm *self) die_perror("KVM_SET_USER_MEMORY_REGION ioctl"); } -struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size) +struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size, bool use_backing) { struct kvm_pit_config pit_config = { .flags = 0, }; struct kvm *self; int ret; + int backfd; if (!kvm__cpu_supports_vm()) die("Your CPU does not support hardware virtualization"); @@ -214,7 +215,23 @@ struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size) self->ram_size = ram_size; - self->ram_start = mmap(NULL, ram_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); + if (use_backing) { + char tmp_template[] = "/tmp/kvm_ram.XXXXXX"; + + backfd = mkstemp(tmp_template); + if (backfd < 0) + die("Failed creating RAM backing file"); + + unlink(tmp_template); + + if (ftruncate(backfd, ram_size) < 0) + die("Failed resizing RAM backing file"); + + self->ram_start = mmap(NULL, ram_size, PROT_READ | PROT_WRITE, MAP_SHARED, backfd, 0); + } else { + self->ram_start = mmap(NULL, ram_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); + } + if (self->ram_start == MAP_FAILED) die("out of memory");