From patchwork Wed Feb 24 21:11:20 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 46206 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 DBA26B7C33 for ; Thu, 25 Feb 2010 08:20:03 +1100 (EST) Received: from localhost ([127.0.0.1]:47275 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NkOeG-0002le-Uo for incoming@patchwork.ozlabs.org; Wed, 24 Feb 2010 16:20:00 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NkOb0-00028n-5A for qemu-devel@nongnu.org; Wed, 24 Feb 2010 16:16:38 -0500 Received: from [199.232.76.173] (port=51044 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NkOaz-00028P-LZ for qemu-devel@nongnu.org; Wed, 24 Feb 2010 16:16:37 -0500 Received: from Debian-exim by monty-python.gnu.org with spam-scanned (Exim 4.60) (envelope-from ) id 1NkOax-0003yi-Gh for qemu-devel@nongnu.org; Wed, 24 Feb 2010 16:16:37 -0500 Received: from mx1.redhat.com ([209.132.183.28]:39682) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NkOav-0003xz-T8 for qemu-devel@nongnu.org; Wed, 24 Feb 2010 16:16:34 -0500 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o1OLGU3K005221 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 24 Feb 2010 16:16:30 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o1OLGU7T011621; Wed, 24 Feb 2010 16:16:30 -0500 Received: from amt.cnet (vpn-11-25.rdu.redhat.com [10.11.11.25]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id o1OLGSju006793; Wed, 24 Feb 2010 16:16:28 -0500 Received: from amt.cnet (amt.cnet [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id B02A566E11C; Wed, 24 Feb 2010 18:15:44 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.3/8.14.3/Submit) id o1OLFiUf014265; Wed, 24 Feb 2010 18:15:44 -0300 Message-Id: <20100224211507.913712224@amt.cnet> User-Agent: quilt/0.47-1 Date: Wed, 24 Feb 2010 18:11:20 -0300 From: Marcelo Tosatti To: kvm@vger.kernel.org, qemu-devel@nongnu.org References: <20100224211117.958583246@amt.cnet> Content-Disposition: inline; filename=mempath X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: john cooper , Marcelo Tosatti , avi@redhat.com Subject: [Qemu-devel] [patch uq/master 2/2] Add option to use file backed guest memory 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 Port qemu-kvm's -mem-path and -mem-prealloc options. These are useful for backing guest memory with huge pages via hugetlbfs. Signed-off-by: Marcelo Tosatti CC: john cooper Index: qemu-kvm/cpu-all.h =================================================================== --- qemu-kvm.orig/cpu-all.h +++ qemu-kvm/cpu-all.h @@ -850,6 +850,9 @@ extern uint8_t *phys_ram_dirty; extern ram_addr_t ram_size; extern ram_addr_t last_ram_offset; +extern const char *mem_path; +extern int mem_prealloc; + /* physical memory access */ /* MMIO pages are identified by a combination of an IO device index and Index: qemu-kvm/exec.c =================================================================== --- qemu-kvm.orig/exec.c +++ qemu-kvm/exec.c @@ -2513,6 +2513,111 @@ void qemu_flush_coalesced_mmio_buffer(vo kvm_flush_coalesced_mmio_buffer(); } +#ifdef __linux__ + +#include + +#define HUGETLBFS_MAGIC 0x958458f6 + +static long gethugepagesize(const char *path) +{ + struct statfs fs; + int ret; + + do { + ret = statfs(path, &fs); + } while (ret != 0 && errno == EINTR); + + if (ret != 0) { + perror("statfs"); + return 0; + } + + if (fs.f_type != HUGETLBFS_MAGIC) + fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path); + + return fs.f_bsize; +} + +static void *file_ram_alloc(ram_addr_t memory, const char *path) +{ + char *filename; + void *area; + int fd; +#ifdef MAP_POPULATE + int flags; +#endif + unsigned long hpagesize; + + if (!path) { + return NULL; + } + + hpagesize = gethugepagesize(path); + if (!hpagesize) { + return NULL; + } + + if (memory < hpagesize) { + return NULL; + } + + if (kvm_enabled() && !kvm_has_sync_mmu()) { + fprintf(stderr, "kvm: host lacks mmu notifiers, disabling -mem-path\n"); + return NULL; + } + + if (asprintf(&filename, "%s/kvm.XXXXXX", path) == -1) { + return NULL; + } + + fd = mkstemp(filename); + if (fd < 0) { + perror("mkstemp"); + free(filename); + return NULL; + } + unlink(filename); + free(filename); + + memory = (memory+hpagesize-1) & ~(hpagesize-1); + + /* + * ftruncate is not supported by hugetlbfs in older + * hosts, so don't bother checking for errors. + * If anything goes wrong with it under other filesystems, + * mmap will fail. + */ + if (ftruncate(fd, memory)) + perror("ftruncate"); + +#ifdef MAP_POPULATE + /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case + * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED + * to sidestep this quirk. + */ + flags = mem_prealloc ? MAP_POPULATE|MAP_SHARED : MAP_PRIVATE; + area = mmap(0, memory, PROT_READ|PROT_WRITE, flags, fd, 0); +#else + area = mmap(0, memory, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); +#endif + if (area == MAP_FAILED) { + perror("file_ram_alloc: can't mmap RAM pages"); + close(fd); + return (NULL); + } + return area; +} + +#else + +static void *file_ram_alloc(ram_addr_t memory, const char *path) +{ + return NULL; +} + +#endif + ram_addr_t qemu_ram_alloc(ram_addr_t size) { RAMBlock *new_block; @@ -2520,16 +2625,20 @@ ram_addr_t qemu_ram_alloc(ram_addr_t siz size = TARGET_PAGE_ALIGN(size); new_block = qemu_malloc(sizeof(*new_block)); + new_block->host = file_ram_alloc(size, mem_path); + if (!new_block->host) { #if defined(TARGET_S390X) && defined(CONFIG_KVM) - /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */ - new_block->host = mmap((void*)0x1000000, size, PROT_EXEC|PROT_READ|PROT_WRITE, - MAP_SHARED | MAP_ANONYMOUS, -1, 0); + /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */ + new_block->host = mmap((void*)0x1000000, size, + PROT_EXEC|PROT_READ|PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); #else - new_block->host = qemu_vmalloc(size); + new_block->host = qemu_vmalloc(size); #endif #ifdef MADV_MERGEABLE - madvise(new_block->host, size, MADV_MERGEABLE); + madvise(new_block->host, size, MADV_MERGEABLE); #endif + } new_block->offset = last_ram_offset; new_block->length = size; Index: qemu-kvm/qemu-options.hx =================================================================== --- qemu-kvm.orig/qemu-options.hx +++ qemu-kvm/qemu-options.hx @@ -314,6 +314,22 @@ a suffix of ``M'' or ``G'' can be used t gigabytes respectively. ETEXI +DEF("mem-path", HAS_ARG, QEMU_OPTION_mempath, + "-mem-path FILE provide backing storage for guest RAM\n") +STEXI +@item -mem-path @var{path} +Allocate guest RAM from a temporarily created file in @var{path}. +ETEXI + +#ifdef MAP_POPULATE +DEF("mem-prealloc", 0, QEMU_OPTION_mem_prealloc, + "-mem-prealloc preallocate guest memory (use with -mem-path)\n") +STEXI +@item -mem-prealloc +Preallocate memory when using -mem-path. +ETEXI +#endif + DEF("k", HAS_ARG, QEMU_OPTION_k, "-k language use keyboard layout (for example 'fr' for French)\n") STEXI Index: qemu-kvm/vl.c =================================================================== --- qemu-kvm.orig/vl.c +++ qemu-kvm/vl.c @@ -186,6 +186,10 @@ static DisplayState *display_state; DisplayType display_type = DT_DEFAULT; const char* keyboard_layout = NULL; ram_addr_t ram_size; +const char *mem_path = NULL; +#ifdef MAP_POPULATE +int mem_prealloc = 1; /* force preallocation of physical target memory */ +#endif int nb_nics; NICInfo nd_table[MAX_NICS]; int vm_running; @@ -5252,6 +5256,13 @@ int main(int argc, char **argv, char **e ram_size = value; break; } + case QEMU_OPTION_mempath: + mem_path = optarg; + break; +#ifdef MAP_POPULATE + case QEMU_OPTION_mem_prealloc: + mem_prealloc = !mem_prealloc; +#endif case QEMU_OPTION_d: { int mask;