From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=37918 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OReGs-0003XG-3u for qemu-devel@nongnu.org; Thu, 24 Jun 2010 00:42:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OReGo-0001VB-PX for qemu-devel@nongnu.org; Thu, 24 Jun 2010 00:42:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:30632) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OReGo-0001V6-Il for qemu-devel@nongnu.org; Thu, 24 Jun 2010 00:42:34 -0400 From: Alex Williamson Date: Wed, 23 Jun 2010 22:42:23 -0600 Message-ID: <20100624044223.16168.20922.stgit@localhost.localdomain> In-Reply-To: <20100624044046.16168.32804.stgit@localhost.localdomain> References: <20100624044046.16168.32804.stgit@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] [PATCH 13/15] qemu_ram_free: Implement it List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: jan.kiszka@siemens.com, armbru@redhat.com, alex.williamson@redhat.com, kraxel@redhat.com, cam@cs.ualberta.ca, paul@codesourcery.com Now that we can support a ram_addr_t space with holes, we can implement qemu_ram_free(). Signed-off-by: Alex Williamson --- cpu-all.h | 3 +++ exec.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/cpu-all.h b/cpu-all.h index 5d8342b..224ca40 100644 --- a/cpu-all.h +++ b/cpu-all.h @@ -867,6 +867,9 @@ typedef struct RAMBlock { ram_addr_t length; char idstr[256]; QLIST_ENTRY(RAMBlock) next; +#if defined(__linux__) && !defined(TARGET_S390X) + int fd; +#endif } RAMBlock; typedef struct RAMList { diff --git a/exec.c b/exec.c index a136c13..e8108d7 100644 --- a/exec.c +++ b/exec.c @@ -2699,7 +2699,9 @@ static long gethugepagesize(const char *path) return fs.f_bsize; } -static void *file_ram_alloc(ram_addr_t memory, const char *path) +static void *file_ram_alloc(RAMBlock *block, + ram_addr_t memory, + const char *path) { char *filename; void *area; @@ -2762,19 +2764,35 @@ static void *file_ram_alloc(ram_addr_t memory, const char *path) close(fd); return (NULL); } + block->fd = fd; return area; } #endif static ram_addr_t find_ram_offset(ram_addr_t size) { - RAMBlock *block; - ram_addr_t last = 0; + RAMBlock *block, *next_block; + ram_addr_t offset, mingap = ULONG_MAX; + + if (QLIST_EMPTY(&ram_list.blocks)) + return 0; - QLIST_FOREACH(block, &ram_list.blocks, next) - last = MAX(last, block->offset + block->length); + QLIST_FOREACH(block, &ram_list.blocks, next) { + ram_addr_t end, next = ULONG_MAX; - return last; + end = block->offset + block->length; + + QLIST_FOREACH(next_block, &ram_list.blocks, next) { + if (next_block->offset >= end) { + next = MIN(next, next_block->offset); + } + } + if (next - end >= size && next - end < mingap) { + offset = end; + mingap = next - end; + } + } + return offset; } ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size) @@ -2810,7 +2828,7 @@ ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size) if (mem_path) { #if defined (__linux__) && !defined(TARGET_S390X) - new_block->host = file_ram_alloc(size, mem_path); + new_block->host = file_ram_alloc(new_block, size, mem_path); if (!new_block->host) { new_block->host = qemu_vmalloc(size); #ifdef MADV_MERGEABLE @@ -2852,7 +2870,32 @@ ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size) void qemu_ram_free(ram_addr_t addr) { - /* TODO: implement this. */ + RAMBlock *block; + + QLIST_FOREACH(block, &ram_list.blocks, next) { + if (addr == block->offset) { + QLIST_REMOVE(block, next); + if (mem_path) { +#if defined (__linux__) && !defined(TARGET_S390X) + if (block->fd) { + munmap(block->host, block->length); + close(block->fd); + } else { + qemu_vfree(block->host); + } +#endif + } else { +#if defined(TARGET_S390X) && defined(CONFIG_KVM) + munmap(block->host, block->length); +#else + qemu_vfree(block->host); +#endif + } + qemu_free(block); + return; + } + } + } /* Return a host pointer to ram allocated with qemu_ram_alloc.