From patchwork Wed May 27 12:50:25 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avi Kivity X-Patchwork-Id: 26462 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n4RCpkVU001883 for ; Wed, 27 May 2009 12:51:46 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1763414AbZE0Mua (ORCPT ); Wed, 27 May 2009 08:50:30 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1763402AbZE0Mua (ORCPT ); Wed, 27 May 2009 08:50:30 -0400 Received: from mx2.redhat.com ([66.187.237.31]:42787 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1763372AbZE0Mu1 (ORCPT ); Wed, 27 May 2009 08:50:27 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n4RCoSMx008504; Wed, 27 May 2009 08:50:28 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n4RCoRVj026658; Wed, 27 May 2009 08:50:27 -0400 Received: from cleopatra.tlv.redhat.com (cleopatra.tlv.redhat.com [10.35.255.11]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n4RCoQoU010255; Wed, 27 May 2009 08:50:26 -0400 Received: from balrog.qumranet.com (dhcp-1-197.tlv.redhat.com [10.35.1.197]) by cleopatra.tlv.redhat.com (Postfix) with ESMTP id 0179A250ABF; Wed, 27 May 2009 15:50:25 +0300 (IDT) Message-ID: <4A1D3711.2070603@redhat.com> Date: Wed, 27 May 2009 15:50:25 +0300 From: Avi Kivity User-Agent: Thunderbird 2.0.0.21 (X11/20090320) MIME-Version: 1.0 To: nicolas prochazka CC: kvm@vger.kernel.org Subject: Re: Remove qemu_alloc_physram() References: <2803e73b0905270308l70cf562jaca72bf67d1aafff@mail.gmail.com> In-Reply-To: <2803e73b0905270308l70cf562jaca72bf67d1aafff@mail.gmail.com> X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org nicolas prochazka wrote: > Hello, > Since this patch : > http://git.kernel.org/?p=virt/kvm/qemu-kvm.git;a=commit;h=0e8d16ba06486ebf79f131f28121774715975248 > Remove qemu_alloc_physram() > Obsolete. > > function alloc_mem_area(memory, &map_len, mem_path); is never calling in code, > and it seems that's -mem-path ( or -mempath) /hugepages does not > work for me now. > before this patch, i can see for example in /proc/meminfo : > HugePages_Total: 2560 > HugePages_Free: 2301 > when i start one kvm/qemu > > now , HupePages_Free never down , hugepages_free = hugepages_total. > > Please test the attached patch. diff --git a/exec.c b/exec.c index e241f05..c024b8b 100644 --- a/exec.c +++ b/exec.c @@ -2484,6 +2484,113 @@ static ram_addr_t kqemu_ram_alloc(ram_addr_t size) } #endif +#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; + extern int mem_prealloc; + + 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, "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. + */ + ftruncate(fd, memory); + +#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("alloc_mem_area: can't mmap hugetlbfs pages"); + close(fd); + return (NULL); + } + return area; +} + +#else + +static void *file_ram_alloc(ram_addr_t memory, const char *path) +{ + return NULL; +} + +#endif + +extern const char *mem_path; + ram_addr_t qemu_ram_alloc(ram_addr_t size) { RAMBlock *new_block; @@ -2497,7 +2604,10 @@ ram_addr_t qemu_ram_alloc(ram_addr_t size) size = TARGET_PAGE_ALIGN(size); new_block = qemu_malloc(sizeof(*new_block)); - new_block->host = qemu_vmalloc(size); + new_block->host = file_ram_alloc(size, mem_path); + if (!new_block->host) { + new_block->host = qemu_vmalloc(size); + } new_block->offset = last_ram_offset; new_block->length = size; diff --git a/hw/pc.c b/hw/pc.c index 9e99b7c..585a4ef 100644 --- a/hw/pc.c +++ b/hw/pc.c @@ -914,14 +914,6 @@ static void pc_init1(ram_addr_t ram_size, hw_error("To much RAM for 32-bit physical address"); #else ram_addr = qemu_ram_alloc(above_4g_mem_size); - if (hpagesize) { - if (ram_addr & (hpagesize-1)) { - unsigned long aligned_addr; - aligned_addr = (ram_addr + hpagesize - 1) & ~(hpagesize-1); - qemu_ram_alloc(aligned_addr - ram_addr); - ram_addr = aligned_addr; - } - } cpu_register_physical_memory(0x100000000ULL, above_4g_mem_size, ram_addr); diff --git a/vl.c b/vl.c index db8265b..8aea7d6 100644 --- a/vl.c +++ b/vl.c @@ -41,7 +41,6 @@ #include #include #include -#include #include #include #if defined(__NetBSD__) @@ -268,7 +267,6 @@ const char *mem_path = NULL; #ifdef MAP_POPULATE int mem_prealloc = 1; /* force preallocation of physical target memory */ #endif -long hpagesize = 0; #ifdef TARGET_ARM int old_param = 0; #endif @@ -4856,90 +4854,6 @@ int qemu_uuid_parse(const char *str, uint8_t *uuid) #define MAX_NET_CLIENTS 32 -#ifdef USE_KVM - -#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 *alloc_mem_area(size_t memory, unsigned long *len, const char *path) -{ - char *filename; - void *area; - int fd; -#ifdef MAP_POPULATE - int flags; -#endif - - if (!kvm_has_sync_mmu()) { - fprintf(stderr, "host lacks mmu notifiers, disabling --mem-path\n"); - return NULL; - } - - if (asprintf(&filename, "%s/kvm.XXXXXX", path) == -1) - return NULL; - - hpagesize = gethugepagesize(path); - if (!hpagesize) - 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. - */ - ftruncate(fd, memory); - -#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("alloc_mem_area: can't mmap hugetlbfs pages"); - close(fd); - return (NULL); - } - *len = memory; - return area; -} -#endif - #ifndef _WIN32 static void termsig_handler(int signal)